Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6] / drivers / isdn / i4l / isdn_net.c
1 /* $Id: isdn_net.c,v 1.1.2.2 2004/01/12 22:37:19 keil Exp $
2  *
3  * Linux ISDN subsystem, network interfaces and related functions (linklevel).
4  *
5  * Copyright 1994-1998  by Fritz Elfert (fritz@isdn4linux.de)
6  * Copyright 1995,96    by Thinking Objects Software GmbH Wuerzburg
7  * Copyright 1995,96    by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
8  *
9  * This software may be used and distributed according to the terms
10  * of the GNU General Public License, incorporated herein by reference.
11  *
12  * Data Over Voice (DOV) support added - Guy Ellis 23-Mar-02 
13  *                                       guy@traverse.com.au
14  * Outgoing calls - looks for a 'V' in first char of dialed number
15  * Incoming calls - checks first character of eaz as follows:
16  *   Numeric - accept DATA only - original functionality
17  *   'V'     - accept VOICE (DOV) only
18  *   'B'     - accept BOTH DATA and DOV types
19  *
20  * Jan 2001: fix CISCO HDLC      Bjoern A. Zeeb <i4l@zabbadoz.net>
21  *           for info on the protocol, see 
22  *           http://i4l.zabbadoz.net/i4l/cisco-hdlc.txt
23  */
24
25 #include <linux/isdn.h>
26 #include <net/arp.h>
27 #include <net/dst.h>
28 #include <net/pkt_sched.h>
29 #include <linux/inetdevice.h>
30 #include "isdn_common.h"
31 #include "isdn_net.h"
32 #ifdef CONFIG_ISDN_PPP
33 #include "isdn_ppp.h"
34 #endif
35 #ifdef CONFIG_ISDN_X25
36 #include <linux/concap.h>
37 #include "isdn_concap.h"
38 #endif
39
40
41 /*
42  * Outline of new tbusy handling: 
43  *
44  * Old method, roughly spoken, consisted of setting tbusy when entering
45  * isdn_net_start_xmit() and at several other locations and clearing
46  * it from isdn_net_start_xmit() thread when sending was successful.
47  *
48  * With 2.3.x multithreaded network core, to prevent problems, tbusy should
49  * only be set by the isdn_net_start_xmit() thread and only when a tx-busy
50  * condition is detected. Other threads (in particular isdn_net_stat_callb())
51  * are only allowed to clear tbusy.
52  *
53  * -HE
54  */
55
56 /*
57  * About SOFTNET:
58  * Most of the changes were pretty obvious and basically done by HE already.
59  *
60  * One problem of the isdn net device code is that is uses struct net_device
61  * for masters and slaves. However, only master interface are registered to 
62  * the network layer, and therefore, it only makes sense to call netif_* 
63  * functions on them.
64  *
65  * --KG
66  */
67
68 /* 
69  * Find out if the netdevice has been ifup-ed yet.
70  * For slaves, look at the corresponding master.
71  */
72 static __inline__ int isdn_net_device_started(isdn_net_dev *n)
73 {
74         isdn_net_local *lp = n->local;
75         struct net_device *dev;
76         
77         if (lp->master) 
78                 dev = lp->master;
79         else
80                 dev = n->dev;
81         return netif_running(dev);
82 }
83
84 /*
85  * wake up the network -> net_device queue.
86  * For slaves, wake the corresponding master interface.
87  */
88 static __inline__ void isdn_net_device_wake_queue(isdn_net_local *lp)
89 {
90         if (lp->master) 
91                 netif_wake_queue(lp->master);
92         else
93                 netif_wake_queue(lp->netdev->dev);
94 }
95
96 /*
97  * stop the network -> net_device queue.
98  * For slaves, stop the corresponding master interface.
99  */
100 static __inline__ void isdn_net_device_stop_queue(isdn_net_local *lp)
101 {
102         if (lp->master)
103                 netif_stop_queue(lp->master);
104         else
105                 netif_stop_queue(lp->netdev->dev);
106 }
107
108 /*
109  * find out if the net_device which this lp belongs to (lp can be
110  * master or slave) is busy. It's busy iff all (master and slave) 
111  * queues are busy
112  */
113 static __inline__ int isdn_net_device_busy(isdn_net_local *lp)
114 {
115         isdn_net_local *nlp;
116         isdn_net_dev *nd;
117         unsigned long flags;
118
119         if (!isdn_net_lp_busy(lp))
120                 return 0;
121
122         if (lp->master)
123                 nd = ISDN_MASTER_PRIV(lp)->netdev;
124         else
125                 nd = lp->netdev;
126         
127         spin_lock_irqsave(&nd->queue_lock, flags);
128         nlp = lp->next;
129         while (nlp != lp) {
130                 if (!isdn_net_lp_busy(nlp)) {
131                         spin_unlock_irqrestore(&nd->queue_lock, flags);
132                         return 0;
133                 }
134                 nlp = nlp->next;
135         }
136         spin_unlock_irqrestore(&nd->queue_lock, flags);
137         return 1;
138 }
139
140 static __inline__ void isdn_net_inc_frame_cnt(isdn_net_local *lp)
141 {
142         atomic_inc(&lp->frame_cnt);
143         if (isdn_net_device_busy(lp))
144                 isdn_net_device_stop_queue(lp);
145 }
146
147 static __inline__ void isdn_net_dec_frame_cnt(isdn_net_local *lp)
148 {
149         atomic_dec(&lp->frame_cnt);
150
151         if (!(isdn_net_device_busy(lp))) {
152                 if (!skb_queue_empty(&lp->super_tx_queue)) {
153                         schedule_work(&lp->tqueue);
154                 } else {
155                         isdn_net_device_wake_queue(lp);
156                 }
157        }                                                                      
158 }
159
160 static __inline__ void isdn_net_zero_frame_cnt(isdn_net_local *lp)
161 {
162         atomic_set(&lp->frame_cnt, 0);
163 }
164
165 /* For 2.2.x we leave the transmitter busy timeout at 2 secs, just 
166  * to be safe.
167  * For 2.3.x we push it up to 20 secs, because call establishment
168  * (in particular callback) may take such a long time, and we 
169  * don't want confusing messages in the log. However, there is a slight
170  * possibility that this large timeout will break other things like MPPP,
171  * which might rely on the tx timeout. If so, we'll find out this way...
172  */
173
174 #define ISDN_NET_TX_TIMEOUT (20*HZ) 
175
176 /* Prototypes */
177
178 static int isdn_net_force_dial_lp(isdn_net_local *);
179 static int isdn_net_start_xmit(struct sk_buff *, struct net_device *);
180
181 static void isdn_net_ciscohdlck_connected(isdn_net_local *lp);
182 static void isdn_net_ciscohdlck_disconnected(isdn_net_local *lp);
183
184 char *isdn_net_revision = "$Revision: 1.1.2.2 $";
185
186  /*
187   * Code for raw-networking over ISDN
188   */
189
190 static void
191 isdn_net_unreachable(struct net_device *dev, struct sk_buff *skb, char *reason)
192 {
193         if(skb) {
194
195                 u_short proto = ntohs(skb->protocol);
196
197                 printk(KERN_DEBUG "isdn_net: %s: %s, signalling dst_link_failure %s\n",
198                        dev->name,
199                        (reason != NULL) ? reason : "unknown",
200                        (proto != ETH_P_IP) ? "Protocol != ETH_P_IP" : "");
201                 
202                 dst_link_failure(skb);
203         }
204         else {  /* dial not triggered by rawIP packet */
205                 printk(KERN_DEBUG "isdn_net: %s: %s\n",
206                            dev->name,
207                            (reason != NULL) ? reason : "reason unknown");
208         }
209 }
210
211 static void
212 isdn_net_reset(struct net_device *dev)
213 {
214 #ifdef CONFIG_ISDN_X25
215         struct concap_device_ops * dops =
216                 ((isdn_net_local *) netdev_priv(dev))->dops;
217         struct concap_proto * cprot =
218                 ((isdn_net_local *) netdev_priv(dev))->netdev->cprot;
219 #endif
220 #ifdef CONFIG_ISDN_X25
221         if( cprot && cprot -> pops && dops )
222                 cprot -> pops -> restart ( cprot, dev, dops );
223 #endif
224 }
225
226 /* Open/initialize the board. */
227 static int
228 isdn_net_open(struct net_device *dev)
229 {
230         int i;
231         struct net_device *p;
232         struct in_device *in_dev;
233
234         /* moved here from isdn_net_reset, because only the master has an
235            interface associated which is supposed to be started. BTW:
236            we need to call netif_start_queue, not netif_wake_queue here */
237         netif_start_queue(dev);
238
239         isdn_net_reset(dev);
240         /* Fill in the MAC-level header (not needed, but for compatibility... */
241         for (i = 0; i < ETH_ALEN - sizeof(u32); i++)
242                 dev->dev_addr[i] = 0xfc;
243         if ((in_dev = dev->ip_ptr) != NULL) {
244                 /*
245                  *      Any address will do - we take the first
246                  */
247                 struct in_ifaddr *ifa = in_dev->ifa_list;
248                 if (ifa != NULL)
249                         memcpy(dev->dev_addr+2, &ifa->ifa_local, 4);
250         }
251
252         /* If this interface has slaves, start them also */
253         p = MASTER_TO_SLAVE(dev);
254         if (p) {
255                 while (p) {
256                         isdn_net_reset(p);
257                         p = MASTER_TO_SLAVE(p);
258                 }
259         }
260         isdn_lock_drivers();
261         return 0;
262 }
263
264 /*
265  * Assign an ISDN-channel to a net-interface
266  */
267 static void
268 isdn_net_bind_channel(isdn_net_local * lp, int idx)
269 {
270         lp->flags |= ISDN_NET_CONNECTED;
271         lp->isdn_device = dev->drvmap[idx];
272         lp->isdn_channel = dev->chanmap[idx];
273         dev->rx_netdev[idx] = lp->netdev;
274         dev->st_netdev[idx] = lp->netdev;
275 }
276
277 /*
278  * unbind a net-interface (resets interface after an error)
279  */
280 static void
281 isdn_net_unbind_channel(isdn_net_local * lp)
282 {
283         skb_queue_purge(&lp->super_tx_queue);
284
285         if (!lp->master) {      /* reset only master device */
286                 /* Moral equivalent of dev_purge_queues():
287                    BEWARE! This chunk of code cannot be called from hardware
288                    interrupt handler. I hope it is true. --ANK
289                  */
290                 qdisc_reset_all_tx(lp->netdev->dev);
291         }
292         lp->dialstate = 0;
293         dev->rx_netdev[isdn_dc2minor(lp->isdn_device, lp->isdn_channel)] = NULL;
294         dev->st_netdev[isdn_dc2minor(lp->isdn_device, lp->isdn_channel)] = NULL;
295         isdn_free_channel(lp->isdn_device, lp->isdn_channel, ISDN_USAGE_NET);
296         lp->flags &= ~ISDN_NET_CONNECTED;
297         lp->isdn_device = -1;
298         lp->isdn_channel = -1;
299 }
300
301 /*
302  * Perform auto-hangup and cps-calculation for net-interfaces.
303  *
304  * auto-hangup:
305  * Increment idle-counter (this counter is reset on any incoming or
306  * outgoing packet), if counter exceeds configured limit either do a
307  * hangup immediately or - if configured - wait until just before the next
308  * charge-info.
309  *
310  * cps-calculation (needed for dynamic channel-bundling):
311  * Since this function is called every second, simply reset the
312  * byte-counter of the interface after copying it to the cps-variable.
313  */
314 static unsigned long last_jiffies = -HZ;
315
316 void
317 isdn_net_autohup(void)
318 {
319         isdn_net_dev *p = dev->netdev;
320         int anymore;
321
322         anymore = 0;
323         while (p) {
324                 isdn_net_local *l = p->local;
325                 if (jiffies == last_jiffies)
326                         l->cps = l->transcount;
327                 else
328                         l->cps = (l->transcount * HZ) / (jiffies - last_jiffies);
329                 l->transcount = 0;
330                 if (dev->net_verbose > 3)
331                         printk(KERN_DEBUG "%s: %d bogocps\n", p->dev->name, l->cps);
332                 if ((l->flags & ISDN_NET_CONNECTED) && (!l->dialstate)) {
333                         anymore = 1;
334                         l->huptimer++;
335                         /*
336                          * if there is some dialmode where timeout-hangup
337                          * should _not_ be done, check for that here
338                          */
339                         if ((l->onhtime) &&
340                             (l->huptimer > l->onhtime))
341                         {
342                                 if (l->hupflags & ISDN_MANCHARGE &&
343                                     l->hupflags & ISDN_CHARGEHUP) {
344                                         while (time_after(jiffies, l->chargetime + l->chargeint))
345                                                 l->chargetime += l->chargeint;
346                                         if (time_after(jiffies, l->chargetime + l->chargeint - 2 * HZ))
347                                                 if (l->outgoing || l->hupflags & ISDN_INHUP)
348                                                         isdn_net_hangup(p->dev);
349                                 } else if (l->outgoing) {
350                                         if (l->hupflags & ISDN_CHARGEHUP) {
351                                                 if (l->hupflags & ISDN_WAITCHARGE) {
352                                                         printk(KERN_DEBUG "isdn_net: Hupflags of %s are %X\n",
353                                                                p->dev->name, l->hupflags);
354                                                         isdn_net_hangup(p->dev);
355                                                 } else if (time_after(jiffies, l->chargetime + l->chargeint)) {
356                                                         printk(KERN_DEBUG
357                                                                "isdn_net: %s: chtime = %lu, chint = %d\n",
358                                                                p->dev->name, l->chargetime, l->chargeint);
359                                                         isdn_net_hangup(p->dev);
360                                                 }
361                                         } else
362                                                 isdn_net_hangup(p->dev);
363                                 } else if (l->hupflags & ISDN_INHUP)
364                                         isdn_net_hangup(p->dev);
365                         }
366
367                         if(dev->global_flags & ISDN_GLOBAL_STOPPED || (ISDN_NET_DIALMODE(*l) == ISDN_NET_DM_OFF)) {
368                                 isdn_net_hangup(p->dev);
369                                 break;
370                         }
371                 }
372                 p = (isdn_net_dev *) p->next;
373         }
374         last_jiffies = jiffies;
375         isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, anymore);
376 }
377
378 static void isdn_net_lp_disconnected(isdn_net_local *lp)
379 {
380         isdn_net_rm_from_bundle(lp);
381 }
382
383 /*
384  * Handle status-messages from ISDN-interfacecard.
385  * This function is called from within the main-status-dispatcher
386  * isdn_status_callback, which itself is called from the low-level driver.
387  * Return: 1 = Event handled, 0 = not for us or unknown Event.
388  */
389 int
390 isdn_net_stat_callback(int idx, isdn_ctrl *c)
391 {
392         isdn_net_dev *p = dev->st_netdev[idx];
393         int cmd = c->command;
394
395         if (p) {
396                 isdn_net_local *lp = p->local;
397 #ifdef CONFIG_ISDN_X25
398                 struct concap_proto *cprot = lp->netdev->cprot;
399                 struct concap_proto_ops *pops = cprot ? cprot->pops : NULL;
400 #endif
401                 switch (cmd) {
402                         case ISDN_STAT_BSENT:
403                                 /* A packet has successfully been sent out */
404                                 if ((lp->flags & ISDN_NET_CONNECTED) &&
405                                     (!lp->dialstate)) {
406                                         isdn_net_dec_frame_cnt(lp);
407                                         lp->stats.tx_packets++;
408                                         lp->stats.tx_bytes += c->parm.length;
409                                 }
410                                 return 1;
411                         case ISDN_STAT_DCONN:
412                                 /* D-Channel is up */
413                                 switch (lp->dialstate) {
414                                         case 4:
415                                         case 7:
416                                         case 8:
417                                                 lp->dialstate++;
418                                                 return 1;
419                                         case 12:
420                                                 lp->dialstate = 5;
421                                                 return 1;
422                                 }
423                                 break;
424                         case ISDN_STAT_DHUP:
425                                 /* Either D-Channel-hangup or error during dialout */
426 #ifdef CONFIG_ISDN_X25
427                                 /* If we are not connencted then dialing had
428                                    failed. If there are generic encap protocol
429                                    receiver routines signal the closure of
430                                    the link*/
431
432                                 if( !(lp->flags & ISDN_NET_CONNECTED)
433                                     && pops && pops -> disconn_ind )
434                                         pops -> disconn_ind(cprot);
435 #endif /* CONFIG_ISDN_X25 */
436                                 if ((!lp->dialstate) && (lp->flags & ISDN_NET_CONNECTED)) {
437                                         if (lp->p_encap == ISDN_NET_ENCAP_CISCOHDLCK)
438                                                 isdn_net_ciscohdlck_disconnected(lp);
439 #ifdef CONFIG_ISDN_PPP
440                                         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
441                                                 isdn_ppp_free(lp);
442 #endif
443                                         isdn_net_lp_disconnected(lp);
444                                         isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
445                                         printk(KERN_INFO "%s: remote hangup\n", p->dev->name);
446                                         printk(KERN_INFO "%s: Chargesum is %d\n", p->dev->name,
447                                                lp->charge);
448                                         isdn_net_unbind_channel(lp);
449                                         return 1;
450                                 }
451                                 break;
452 #ifdef CONFIG_ISDN_X25
453                         case ISDN_STAT_BHUP:
454                                 /* B-Channel-hangup */
455                                 /* try if there are generic encap protocol
456                                    receiver routines and signal the closure of
457                                    the link */
458                                 if( pops  &&  pops -> disconn_ind ){
459                                                 pops -> disconn_ind(cprot);
460                                                 return 1;
461                                         }
462                                 break;
463 #endif /* CONFIG_ISDN_X25 */
464                         case ISDN_STAT_BCONN:
465                                 /* B-Channel is up */
466                                 isdn_net_zero_frame_cnt(lp);
467                                 switch (lp->dialstate) {
468                                         case 5:
469                                         case 6:
470                                         case 7:
471                                         case 8:
472                                         case 9:
473                                         case 10:
474                                         case 12:
475                                                 if (lp->dialstate <= 6) {
476                                                         dev->usage[idx] |= ISDN_USAGE_OUTGOING;
477                                                         isdn_info_update();
478                                                 } else
479                                                         dev->rx_netdev[idx] = p;
480                                                 lp->dialstate = 0;
481                                                 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 1);
482                                                 if (lp->p_encap == ISDN_NET_ENCAP_CISCOHDLCK)
483                                                         isdn_net_ciscohdlck_connected(lp);
484                                                 if (lp->p_encap != ISDN_NET_ENCAP_SYNCPPP) {
485                                                         if (lp->master) { /* is lp a slave? */
486                                                                 isdn_net_dev *nd = ISDN_MASTER_PRIV(lp)->netdev;
487                                                                 isdn_net_add_to_bundle(nd, lp);
488                                                         }
489                                                 }
490                                                 printk(KERN_INFO "isdn_net: %s connected\n", p->dev->name);
491                                                 /* If first Chargeinfo comes before B-Channel connect,
492                                                  * we correct the timestamp here.
493                                                  */
494                                                 lp->chargetime = jiffies;
495
496                                                 /* reset dial-timeout */
497                                                 lp->dialstarted = 0;
498                                                 lp->dialwait_timer = 0;
499
500 #ifdef CONFIG_ISDN_PPP
501                                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
502                                                         isdn_ppp_wakeup_daemon(lp);
503 #endif
504 #ifdef CONFIG_ISDN_X25
505                                                 /* try if there are generic concap receiver routines */
506                                                 if( pops )
507                                                         if( pops->connect_ind)
508                                                                 pops->connect_ind(cprot);
509 #endif /* CONFIG_ISDN_X25 */
510                                                 /* ppp needs to do negotiations first */
511                                                 if (lp->p_encap != ISDN_NET_ENCAP_SYNCPPP)
512                                                         isdn_net_device_wake_queue(lp);
513                                                 return 1;
514                                 }
515                                 break;
516                         case ISDN_STAT_NODCH:
517                                 /* No D-Channel avail. */
518                                 if (lp->dialstate == 4) {
519                                         lp->dialstate--;
520                                         return 1;
521                                 }
522                                 break;
523                         case ISDN_STAT_CINF:
524                                 /* Charge-info from TelCo. Calculate interval between
525                                  * charge-infos and set timestamp for last info for
526                                  * usage by isdn_net_autohup()
527                                  */
528                                 lp->charge++;
529                                 if (lp->hupflags & ISDN_HAVECHARGE) {
530                                         lp->hupflags &= ~ISDN_WAITCHARGE;
531                                         lp->chargeint = jiffies - lp->chargetime - (2 * HZ);
532                                 }
533                                 if (lp->hupflags & ISDN_WAITCHARGE)
534                                         lp->hupflags |= ISDN_HAVECHARGE;
535                                 lp->chargetime = jiffies;
536                                 printk(KERN_DEBUG "isdn_net: Got CINF chargetime of %s now %lu\n",
537                                        p->dev->name, lp->chargetime);
538                                 return 1;
539                 }
540         }
541         return 0;
542 }
543
544 /*
545  * Perform dialout for net-interfaces and timeout-handling for
546  * D-Channel-up and B-Channel-up Messages.
547  * This function is initially called from within isdn_net_start_xmit() or
548  * or isdn_net_find_icall() after initializing the dialstate for an
549  * interface. If further calls are needed, the function schedules itself
550  * for a timer-callback via isdn_timer_function().
551  * The dialstate is also affected by incoming status-messages from
552  * the ISDN-Channel which are handled in isdn_net_stat_callback() above.
553  */
554 void
555 isdn_net_dial(void)
556 {
557         isdn_net_dev *p = dev->netdev;
558         int anymore = 0;
559         int i;
560         isdn_ctrl cmd;
561         u_char *phone_number;
562
563         while (p) {
564                 isdn_net_local *lp = p->local;
565
566 #ifdef ISDN_DEBUG_NET_DIAL
567                 if (lp->dialstate)
568                         printk(KERN_DEBUG "%s: dialstate=%d\n", p->dev->name, lp->dialstate);
569 #endif
570                 switch (lp->dialstate) {
571                         case 0:
572                                 /* Nothing to do for this interface */
573                                 break;
574                         case 1:
575                                 /* Initiate dialout. Set phone-number-pointer to first number
576                                  * of interface.
577                                  */
578                                 lp->dial = lp->phone[1];
579                                 if (!lp->dial) {
580                                         printk(KERN_WARNING "%s: phone number deleted?\n",
581                                                p->dev->name);
582                                         isdn_net_hangup(p->dev);
583                                         break;
584                                 }
585                                 anymore = 1;
586
587                                 if(lp->dialtimeout > 0)
588                                         if(lp->dialstarted == 0 || time_after(jiffies, lp->dialstarted + lp->dialtimeout + lp->dialwait)) {
589                                                 lp->dialstarted = jiffies;
590                                                 lp->dialwait_timer = 0;
591                                         }
592
593                                 lp->dialstate++;
594                                 /* Fall through */
595                         case 2:
596                                 /* Prepare dialing. Clear EAZ, then set EAZ. */
597                                 cmd.driver = lp->isdn_device;
598                                 cmd.arg = lp->isdn_channel;
599                                 cmd.command = ISDN_CMD_CLREAZ;
600                                 isdn_command(&cmd);
601                                 sprintf(cmd.parm.num, "%s", isdn_map_eaz2msn(lp->msn, cmd.driver));
602                                 cmd.command = ISDN_CMD_SETEAZ;
603                                 isdn_command(&cmd);
604                                 lp->dialretry = 0;
605                                 anymore = 1;
606                                 lp->dialstate++;
607                                 /* Fall through */
608                         case 3:
609                                 /* Setup interface, dial current phone-number, switch to next number.
610                                  * If list of phone-numbers is exhausted, increment
611                                  * retry-counter.
612                                  */
613                                 if(dev->global_flags & ISDN_GLOBAL_STOPPED || (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF)) {
614                                         char *s;
615                                         if (dev->global_flags & ISDN_GLOBAL_STOPPED)
616                                                 s = "dial suppressed: isdn system stopped";
617                                         else
618                                                 s = "dial suppressed: dialmode `off'";
619                                         isdn_net_unreachable(p->dev, NULL, s);
620                                         isdn_net_hangup(p->dev);
621                                         break;
622                                 }
623                                 cmd.driver = lp->isdn_device;
624                                 cmd.command = ISDN_CMD_SETL2;
625                                 cmd.arg = lp->isdn_channel + (lp->l2_proto << 8);
626                                 isdn_command(&cmd);
627                                 cmd.driver = lp->isdn_device;
628                                 cmd.command = ISDN_CMD_SETL3;
629                                 cmd.arg = lp->isdn_channel + (lp->l3_proto << 8);
630                                 isdn_command(&cmd);
631                                 cmd.driver = lp->isdn_device;
632                                 cmd.arg = lp->isdn_channel;
633                                 if (!lp->dial) {
634                                         printk(KERN_WARNING "%s: phone number deleted?\n",
635                                                p->dev->name);
636                                         isdn_net_hangup(p->dev);
637                                         break;
638                                 }
639                                 if (!strncmp(lp->dial->num, "LEASED", strlen("LEASED"))) {
640                                         lp->dialstate = 4;
641                                         printk(KERN_INFO "%s: Open leased line ...\n", p->dev->name);
642                                 } else {
643                                         if(lp->dialtimeout > 0)
644                                                 if (time_after(jiffies, lp->dialstarted + lp->dialtimeout)) {
645                                                         lp->dialwait_timer = jiffies + lp->dialwait;
646                                                         lp->dialstarted = 0;
647                                                         isdn_net_unreachable(p->dev, NULL, "dial: timed out");
648                                                         isdn_net_hangup(p->dev);
649                                                         break;
650                                                 }
651
652                                         cmd.driver = lp->isdn_device;
653                                         cmd.command = ISDN_CMD_DIAL;
654                                         cmd.parm.setup.si2 = 0;
655
656                                         /* check for DOV */
657                                         phone_number = lp->dial->num;
658                                         if ((*phone_number == 'v') ||
659                                             (*phone_number == 'V')) { /* DOV call */
660                                                 cmd.parm.setup.si1 = 1;
661                                         } else { /* DATA call */
662                                                 cmd.parm.setup.si1 = 7;
663                                         }
664
665                                         strcpy(cmd.parm.setup.phone, phone_number);
666                                         /*
667                                          * Switch to next number or back to start if at end of list.
668                                          */
669                                         if (!(lp->dial = (isdn_net_phone *) lp->dial->next)) {
670                                                 lp->dial = lp->phone[1];
671                                                 lp->dialretry++;
672
673                                                 if (lp->dialretry > lp->dialmax) {
674                                                         if (lp->dialtimeout == 0) {
675                                                                 lp->dialwait_timer = jiffies + lp->dialwait;
676                                                                 lp->dialstarted = 0;
677                                                                 isdn_net_unreachable(p->dev, NULL, "dial: tried all numbers dialmax times");
678                                                         }
679                                                         isdn_net_hangup(p->dev);
680                                                         break;
681                                                 }
682                                         }
683                                         sprintf(cmd.parm.setup.eazmsn, "%s",
684                                                 isdn_map_eaz2msn(lp->msn, cmd.driver));
685                                         i = isdn_dc2minor(lp->isdn_device, lp->isdn_channel);
686                                         if (i >= 0) {
687                                                 strcpy(dev->num[i], cmd.parm.setup.phone);
688                                                 dev->usage[i] |= ISDN_USAGE_OUTGOING;
689                                                 isdn_info_update();
690                                         }
691                                         printk(KERN_INFO "%s: dialing %d %s... %s\n", p->dev->name,
692                                                lp->dialretry, cmd.parm.setup.phone,
693                                                (cmd.parm.setup.si1 == 1) ? "DOV" : "");
694                                         lp->dtimer = 0;
695 #ifdef ISDN_DEBUG_NET_DIAL
696                                         printk(KERN_DEBUG "dial: d=%d c=%d\n", lp->isdn_device,
697                                                lp->isdn_channel);
698 #endif
699                                         isdn_command(&cmd);
700                                 }
701                                 lp->huptimer = 0;
702                                 lp->outgoing = 1;
703                                 if (lp->chargeint) {
704                                         lp->hupflags |= ISDN_HAVECHARGE;
705                                         lp->hupflags &= ~ISDN_WAITCHARGE;
706                                 } else {
707                                         lp->hupflags |= ISDN_WAITCHARGE;
708                                         lp->hupflags &= ~ISDN_HAVECHARGE;
709                                 }
710                                 anymore = 1;
711                                 lp->dialstate =
712                                     (lp->cbdelay &&
713                                      (lp->flags & ISDN_NET_CBOUT)) ? 12 : 4;
714                                 break;
715                         case 4:
716                                 /* Wait for D-Channel-connect.
717                                  * If timeout, switch back to state 3.
718                                  * Dialmax-handling moved to state 3.
719                                  */
720                                 if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
721                                         lp->dialstate = 3;
722                                 anymore = 1;
723                                 break;
724                         case 5:
725                                 /* Got D-Channel-Connect, send B-Channel-request */
726                                 cmd.driver = lp->isdn_device;
727                                 cmd.arg = lp->isdn_channel;
728                                 cmd.command = ISDN_CMD_ACCEPTB;
729                                 anymore = 1;
730                                 lp->dtimer = 0;
731                                 lp->dialstate++;
732                                 isdn_command(&cmd);
733                                 break;
734                         case 6:
735                                 /* Wait for B- or D-Channel-connect. If timeout,
736                                  * switch back to state 3.
737                                  */
738 #ifdef ISDN_DEBUG_NET_DIAL
739                                 printk(KERN_DEBUG "dialtimer2: %d\n", lp->dtimer);
740 #endif
741                                 if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
742                                         lp->dialstate = 3;
743                                 anymore = 1;
744                                 break;
745                         case 7:
746                                 /* Got incoming Call, setup L2 and L3 protocols,
747                                  * then wait for D-Channel-connect
748                                  */
749 #ifdef ISDN_DEBUG_NET_DIAL
750                                 printk(KERN_DEBUG "dialtimer4: %d\n", lp->dtimer);
751 #endif
752                                 cmd.driver = lp->isdn_device;
753                                 cmd.command = ISDN_CMD_SETL2;
754                                 cmd.arg = lp->isdn_channel + (lp->l2_proto << 8);
755                                 isdn_command(&cmd);
756                                 cmd.driver = lp->isdn_device;
757                                 cmd.command = ISDN_CMD_SETL3;
758                                 cmd.arg = lp->isdn_channel + (lp->l3_proto << 8);
759                                 isdn_command(&cmd);
760                                 if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT15)
761                                         isdn_net_hangup(p->dev);
762                                 else {
763                                         anymore = 1;
764                                         lp->dialstate++;
765                                 }
766                                 break;
767                         case 9:
768                                 /* Got incoming D-Channel-Connect, send B-Channel-request */
769                                 cmd.driver = lp->isdn_device;
770                                 cmd.arg = lp->isdn_channel;
771                                 cmd.command = ISDN_CMD_ACCEPTB;
772                                 isdn_command(&cmd);
773                                 anymore = 1;
774                                 lp->dtimer = 0;
775                                 lp->dialstate++;
776                                 break;
777                         case 8:
778                         case 10:
779                                 /*  Wait for B- or D-channel-connect */
780 #ifdef ISDN_DEBUG_NET_DIAL
781                                 printk(KERN_DEBUG "dialtimer4: %d\n", lp->dtimer);
782 #endif
783                                 if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
784                                         isdn_net_hangup(p->dev);
785                                 else
786                                         anymore = 1;
787                                 break;
788                         case 11:
789                                 /* Callback Delay */
790                                 if (lp->dtimer++ > lp->cbdelay)
791                                         lp->dialstate = 1;
792                                 anymore = 1;
793                                 break;
794                         case 12:
795                                 /* Remote does callback. Hangup after cbdelay, then wait for incoming
796                                  * call (in state 4).
797                                  */
798                                 if (lp->dtimer++ > lp->cbdelay)
799                                 {
800                                         printk(KERN_INFO "%s: hangup waiting for callback ...\n", p->dev->name);
801                                         lp->dtimer = 0;
802                                         lp->dialstate = 4;
803                                         cmd.driver = lp->isdn_device;
804                                         cmd.command = ISDN_CMD_HANGUP;
805                                         cmd.arg = lp->isdn_channel;
806                                         isdn_command(&cmd);
807                                         isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
808                                 }
809                                 anymore = 1;
810                                 break;
811                         default:
812                                 printk(KERN_WARNING "isdn_net: Illegal dialstate %d for device %s\n",
813                                        lp->dialstate, p->dev->name);
814                 }
815                 p = (isdn_net_dev *) p->next;
816         }
817         isdn_timer_ctrl(ISDN_TIMER_NETDIAL, anymore);
818 }
819
820 /*
821  * Perform hangup for a net-interface.
822  */
823 void
824 isdn_net_hangup(struct net_device *d)
825 {
826         isdn_net_local *lp = (isdn_net_local *) netdev_priv(d);
827         isdn_ctrl cmd;
828 #ifdef CONFIG_ISDN_X25
829         struct concap_proto *cprot = lp->netdev->cprot;
830         struct concap_proto_ops *pops = cprot ? cprot->pops : NULL;
831 #endif
832
833         if (lp->flags & ISDN_NET_CONNECTED) {
834                 if (lp->slave != NULL) {
835                         isdn_net_local *slp = ISDN_SLAVE_PRIV(lp);
836                         if (slp->flags & ISDN_NET_CONNECTED) {
837                                 printk(KERN_INFO
838                                         "isdn_net: hang up slave %s before %s\n",
839                                         lp->slave->name, d->name);
840                                 isdn_net_hangup(lp->slave);
841                         }
842                 }
843                 printk(KERN_INFO "isdn_net: local hangup %s\n", d->name);
844 #ifdef CONFIG_ISDN_PPP
845                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
846                         isdn_ppp_free(lp);
847 #endif
848                 isdn_net_lp_disconnected(lp);
849 #ifdef CONFIG_ISDN_X25
850                 /* try if there are generic encap protocol
851                    receiver routines and signal the closure of
852                    the link */
853                 if( pops && pops -> disconn_ind )
854                   pops -> disconn_ind(cprot);
855 #endif /* CONFIG_ISDN_X25 */
856
857                 cmd.driver = lp->isdn_device;
858                 cmd.command = ISDN_CMD_HANGUP;
859                 cmd.arg = lp->isdn_channel;
860                 isdn_command(&cmd);
861                 printk(KERN_INFO "%s: Chargesum is %d\n", d->name, lp->charge);
862                 isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
863         }
864         isdn_net_unbind_channel(lp);
865 }
866
867 typedef struct {
868         __be16 source;
869         __be16 dest;
870 } ip_ports;
871
872 static void
873 isdn_net_log_skb(struct sk_buff * skb, isdn_net_local * lp)
874 {
875         /* hopefully, this was set correctly */
876         const u_char *p = skb_network_header(skb);
877         unsigned short proto = ntohs(skb->protocol);
878         int data_ofs;
879         ip_ports *ipp;
880         char addinfo[100];
881
882         addinfo[0] = '\0';
883         /* This check stolen from 2.1.72 dev_queue_xmit_nit() */
884         if (p < skb->data || skb->network_header >= skb->tail) {
885                 /* fall back to old isdn_net_log_packet method() */
886                 char * buf = skb->data;
887
888                 printk(KERN_DEBUG "isdn_net: protocol %04x is buggy, dev %s\n", skb->protocol, lp->netdev->dev->name);
889                 p = buf;
890                 proto = ETH_P_IP;
891                 switch (lp->p_encap) {
892                         case ISDN_NET_ENCAP_IPTYP:
893                                 proto = ntohs(*(__be16 *)&buf[0]);
894                                 p = &buf[2];
895                                 break;
896                         case ISDN_NET_ENCAP_ETHER:
897                                 proto = ntohs(*(__be16 *)&buf[12]);
898                                 p = &buf[14];
899                                 break;
900                         case ISDN_NET_ENCAP_CISCOHDLC:
901                                 proto = ntohs(*(__be16 *)&buf[2]);
902                                 p = &buf[4];
903                                 break;
904 #ifdef CONFIG_ISDN_PPP
905                         case ISDN_NET_ENCAP_SYNCPPP:
906                                 proto = ntohs(skb->protocol);
907                                 p = &buf[IPPP_MAX_HEADER];
908                                 break;
909 #endif
910                 }
911         }
912         data_ofs = ((p[0] & 15) * 4);
913         switch (proto) {
914                 case ETH_P_IP:
915                         switch (p[9]) {
916                                 case 1:
917                                         strcpy(addinfo, " ICMP");
918                                         break;
919                                 case 2:
920                                         strcpy(addinfo, " IGMP");
921                                         break;
922                                 case 4:
923                                         strcpy(addinfo, " IPIP");
924                                         break;
925                                 case 6:
926                                         ipp = (ip_ports *) (&p[data_ofs]);
927                                         sprintf(addinfo, " TCP, port: %d -> %d", ntohs(ipp->source),
928                                                 ntohs(ipp->dest));
929                                         break;
930                                 case 8:
931                                         strcpy(addinfo, " EGP");
932                                         break;
933                                 case 12:
934                                         strcpy(addinfo, " PUP");
935                                         break;
936                                 case 17:
937                                         ipp = (ip_ports *) (&p[data_ofs]);
938                                         sprintf(addinfo, " UDP, port: %d -> %d", ntohs(ipp->source),
939                                                 ntohs(ipp->dest));
940                                         break;
941                                 case 22:
942                                         strcpy(addinfo, " IDP");
943                                         break;
944                         }
945                         printk(KERN_INFO "OPEN: %pI4 -> %pI4%s\n",
946                                p + 12, p + 16, addinfo);
947                         break;
948                 case ETH_P_ARP:
949                         printk(KERN_INFO "OPEN: ARP %pI4 -> *.*.*.* ?%pI4\n",
950                                p + 14, p + 24);
951                         break;
952         }
953 }
954
955 /*
956  * this function is used to send supervisory data, i.e. data which was
957  * not received from the network layer, but e.g. frames from ipppd, CCP
958  * reset frames etc.
959  */
960 void isdn_net_write_super(isdn_net_local *lp, struct sk_buff *skb)
961 {
962         if (in_irq()) {
963                 // we can't grab the lock from irq context, 
964                 // so we just queue the packet
965                 skb_queue_tail(&lp->super_tx_queue, skb);
966                 schedule_work(&lp->tqueue);
967                 return;
968         }
969
970         spin_lock_bh(&lp->xmit_lock);
971         if (!isdn_net_lp_busy(lp)) {
972                 isdn_net_writebuf_skb(lp, skb);
973         } else {
974                 skb_queue_tail(&lp->super_tx_queue, skb);
975         }
976         spin_unlock_bh(&lp->xmit_lock);
977 }
978
979 /*
980  * called from tq_immediate
981  */
982 static void isdn_net_softint(struct work_struct *work)
983 {
984         isdn_net_local *lp = container_of(work, isdn_net_local, tqueue);
985         struct sk_buff *skb;
986
987         spin_lock_bh(&lp->xmit_lock);
988         while (!isdn_net_lp_busy(lp)) {
989                 skb = skb_dequeue(&lp->super_tx_queue);
990                 if (!skb)
991                         break;
992                 isdn_net_writebuf_skb(lp, skb);                                
993         }
994         spin_unlock_bh(&lp->xmit_lock);
995 }
996
997 /* 
998  * all frames sent from the (net) LL to a HL driver should go via this function
999  * it's serialized by the caller holding the lp->xmit_lock spinlock
1000  */
1001 void isdn_net_writebuf_skb(isdn_net_local *lp, struct sk_buff *skb)
1002 {
1003         int ret;
1004         int len = skb->len;     /* save len */
1005
1006         /* before obtaining the lock the caller should have checked that
1007            the lp isn't busy */
1008         if (isdn_net_lp_busy(lp)) {
1009                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1010                 goto error;
1011         }
1012
1013         if (!(lp->flags & ISDN_NET_CONNECTED)) {
1014                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1015                 goto error;
1016         }
1017         ret = isdn_writebuf_skb_stub(lp->isdn_device, lp->isdn_channel, 1, skb);
1018         if (ret != len) {
1019                 /* we should never get here */
1020                 printk(KERN_WARNING "%s: HL driver queue full\n", lp->netdev->dev->name);
1021                 goto error;
1022         }
1023         
1024         lp->transcount += len;
1025         isdn_net_inc_frame_cnt(lp);
1026         return;
1027
1028  error:
1029         dev_kfree_skb(skb);
1030         lp->stats.tx_errors++;
1031
1032 }
1033
1034
1035 /*
1036  *  Helper function for isdn_net_start_xmit.
1037  *  When called, the connection is already established.
1038  *  Based on cps-calculation, check if device is overloaded.
1039  *  If so, and if a slave exists, trigger dialing for it.
1040  *  If any slave is online, deliver packets using a simple round robin
1041  *  scheme.
1042  *
1043  *  Return: 0 on success, !0 on failure.
1044  */
1045
1046 static int
1047 isdn_net_xmit(struct net_device *ndev, struct sk_buff *skb)
1048 {
1049         isdn_net_dev *nd;
1050         isdn_net_local *slp;
1051         isdn_net_local *lp = (isdn_net_local *) netdev_priv(ndev);
1052         int retv = 0;
1053
1054         if (((isdn_net_local *) netdev_priv(ndev))->master) {
1055                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1056                 dev_kfree_skb(skb);
1057                 return 0;
1058         }
1059
1060         /* For the other encaps the header has already been built */
1061 #ifdef CONFIG_ISDN_PPP
1062         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
1063                 return isdn_ppp_xmit(skb, ndev);
1064         }
1065 #endif
1066         nd = ((isdn_net_local *) netdev_priv(ndev))->netdev;
1067         lp = isdn_net_get_locked_lp(nd);
1068         if (!lp) {
1069                 printk(KERN_WARNING "%s: all channels busy - requeuing!\n", ndev->name);
1070                 return 1;
1071         }
1072         /* we have our lp locked from now on */
1073
1074         /* Reset hangup-timeout */
1075         lp->huptimer = 0; // FIXME?
1076         isdn_net_writebuf_skb(lp, skb);
1077         spin_unlock_bh(&lp->xmit_lock);
1078
1079         /* the following stuff is here for backwards compatibility.
1080          * in future, start-up and hangup of slaves (based on current load)
1081          * should move to userspace and get based on an overall cps
1082          * calculation
1083          */
1084         if (lp->cps > lp->triggercps) {
1085                 if (lp->slave) {
1086                         if (!lp->sqfull) {
1087                                 /* First time overload: set timestamp only */
1088                                 lp->sqfull = 1;
1089                                 lp->sqfull_stamp = jiffies;
1090                         } else {
1091                                 /* subsequent overload: if slavedelay exceeded, start dialing */
1092                                 if (time_after(jiffies, lp->sqfull_stamp + lp->slavedelay)) {
1093                                         slp = ISDN_SLAVE_PRIV(lp);
1094                                         if (!(slp->flags & ISDN_NET_CONNECTED)) {
1095                                                 isdn_net_force_dial_lp(ISDN_SLAVE_PRIV(lp));
1096                                         }
1097                                 }
1098                         }
1099                 }
1100         } else {
1101                 if (lp->sqfull && time_after(jiffies, lp->sqfull_stamp + lp->slavedelay + (10 * HZ))) {
1102                         lp->sqfull = 0;
1103                 }
1104                 /* this is a hack to allow auto-hangup for slaves on moderate loads */
1105                 nd->queue = nd->local;
1106         }
1107
1108         return retv;
1109
1110 }
1111
1112 static void
1113 isdn_net_adjust_hdr(struct sk_buff *skb, struct net_device *dev)
1114 {
1115         isdn_net_local *lp = (isdn_net_local *) netdev_priv(dev);
1116         if (!skb)
1117                 return;
1118         if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
1119                 const int pullsize = skb_network_offset(skb) - ETH_HLEN;
1120                 if (pullsize > 0) {
1121                         printk(KERN_DEBUG "isdn_net: Pull junk %d\n", pullsize);
1122                         skb_pull(skb, pullsize);
1123                 }
1124         }
1125 }
1126
1127
1128 static void isdn_net_tx_timeout(struct net_device * ndev)
1129 {
1130         isdn_net_local *lp = (isdn_net_local *) netdev_priv(ndev);
1131
1132         printk(KERN_WARNING "isdn_tx_timeout dev %s dialstate %d\n", ndev->name, lp->dialstate);
1133         if (!lp->dialstate){
1134                 lp->stats.tx_errors++;
1135                 /*
1136                  * There is a certain probability that this currently
1137                  * works at all because if we always wake up the interface,
1138                  * then upper layer will try to send the next packet
1139                  * immediately. And then, the old clean_up logic in the
1140                  * driver will hopefully continue to work as it used to do.
1141                  *
1142                  * This is rather primitive right know, we better should
1143                  * clean internal queues here, in particular for multilink and
1144                  * ppp, and reset HL driver's channel, too.   --HE
1145                  *
1146                  * actually, this may not matter at all, because ISDN hardware
1147                  * should not see transmitter hangs at all IMO
1148                  * changed KERN_DEBUG to KERN_WARNING to find out if this is 
1149                  * ever called   --KG
1150                  */
1151         }
1152         ndev->trans_start = jiffies;
1153         netif_wake_queue(ndev);
1154 }
1155
1156 /*
1157  * Try sending a packet.
1158  * If this interface isn't connected to a ISDN-Channel, find a free channel,
1159  * and start dialing.
1160  */
1161 static int
1162 isdn_net_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1163 {
1164         isdn_net_local *lp = (isdn_net_local *) netdev_priv(ndev);
1165 #ifdef CONFIG_ISDN_X25
1166         struct concap_proto * cprot = lp -> netdev -> cprot;
1167 /* At this point hard_start_xmit() passes control to the encapsulation
1168    protocol (if present).
1169    For X.25 auto-dialing is completly bypassed because:
1170    - It does not conform with the semantics of a reliable datalink
1171      service as needed by X.25 PLP.
1172    - I don't want that the interface starts dialing when the network layer
1173      sends a message which requests to disconnect the lapb link (or if it
1174      sends any other message not resulting in data transmission).
1175    Instead, dialing will be initiated by the encapsulation protocol entity
1176    when a dl_establish request is received from the upper layer.
1177 */
1178         if (cprot && cprot -> pops) {
1179                 int ret = cprot -> pops -> encap_and_xmit ( cprot , skb);
1180
1181                 if (ret)
1182                         netif_stop_queue(ndev);
1183                 return ret;
1184         } else
1185 #endif
1186         /* auto-dialing xmit function */
1187         {
1188 #ifdef ISDN_DEBUG_NET_DUMP
1189                 u_char *buf;
1190 #endif
1191                 isdn_net_adjust_hdr(skb, ndev);
1192 #ifdef ISDN_DEBUG_NET_DUMP
1193                 buf = skb->data;
1194                 isdn_dumppkt("S:", buf, skb->len, 40);
1195 #endif
1196
1197                 if (!(lp->flags & ISDN_NET_CONNECTED)) {
1198                         int chi;
1199                         /* only do autodial if allowed by config */
1200                         if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) {
1201                                 isdn_net_unreachable(ndev, skb, "dial rejected: interface not in dialmode `auto'");
1202                                 dev_kfree_skb(skb);
1203                                 return 0;
1204                         }
1205                         if (lp->phone[1]) {
1206                                 ulong flags;
1207
1208                                 if(lp->dialwait_timer <= 0)
1209                                         if(lp->dialstarted > 0 && lp->dialtimeout > 0 && time_before(jiffies, lp->dialstarted + lp->dialtimeout + lp->dialwait))
1210                                                 lp->dialwait_timer = lp->dialstarted + lp->dialtimeout + lp->dialwait;
1211
1212                                 if(lp->dialwait_timer > 0) {
1213                                         if(time_before(jiffies, lp->dialwait_timer)) {
1214                                                 isdn_net_unreachable(ndev, skb, "dial rejected: retry-time not reached");
1215                                                 dev_kfree_skb(skb);
1216                                                 return 0;
1217                                         } else
1218                                                 lp->dialwait_timer = 0;
1219                                 }
1220                                 /* Grab a free ISDN-Channel */
1221                                 spin_lock_irqsave(&dev->lock, flags);
1222                                 if (((chi =
1223                                      isdn_get_free_channel(
1224                                                         ISDN_USAGE_NET,
1225                                                         lp->l2_proto,
1226                                                         lp->l3_proto,
1227                                                         lp->pre_device,
1228                                                         lp->pre_channel,
1229                                                         lp->msn)
1230                                                         ) < 0) &&
1231                                         ((chi =
1232                                      isdn_get_free_channel(
1233                                                         ISDN_USAGE_NET,
1234                                                         lp->l2_proto,
1235                                                         lp->l3_proto,
1236                                                         lp->pre_device,
1237                                                         lp->pre_channel^1,
1238                                                         lp->msn)
1239                                                         ) < 0)) {
1240                                         spin_unlock_irqrestore(&dev->lock, flags);
1241                                         isdn_net_unreachable(ndev, skb,
1242                                                            "No channel");
1243                                         dev_kfree_skb(skb);
1244                                         return 0;
1245                                 }
1246                                 /* Log packet, which triggered dialing */
1247                                 if (dev->net_verbose)
1248                                         isdn_net_log_skb(skb, lp);
1249                                 lp->dialstate = 1;
1250                                 /* Connect interface with channel */
1251                                 isdn_net_bind_channel(lp, chi);
1252 #ifdef CONFIG_ISDN_PPP
1253                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
1254                                         /* no 'first_skb' handling for syncPPP */
1255                                         if (isdn_ppp_bind(lp) < 0) {
1256                                                 dev_kfree_skb(skb);
1257                                                 isdn_net_unbind_channel(lp);
1258                                                 spin_unlock_irqrestore(&dev->lock, flags);
1259                                                 return 0;       /* STN (skb to nirvana) ;) */
1260                                         }
1261 #ifdef CONFIG_IPPP_FILTER
1262                                         if (isdn_ppp_autodial_filter(skb, lp)) {
1263                                                 isdn_ppp_free(lp);
1264                                                 isdn_net_unbind_channel(lp);
1265                                                 spin_unlock_irqrestore(&dev->lock, flags);
1266                                                 isdn_net_unreachable(ndev, skb, "dial rejected: packet filtered");
1267                                                 dev_kfree_skb(skb);
1268                                                 return 0;
1269                                         }
1270 #endif
1271                                         spin_unlock_irqrestore(&dev->lock, flags);
1272                                         isdn_net_dial();        /* Initiate dialing */
1273                                         netif_stop_queue(ndev);
1274                                         return 1;       /* let upper layer requeue skb packet */
1275                                 }
1276 #endif
1277                                 /* Initiate dialing */
1278                                 spin_unlock_irqrestore(&dev->lock, flags);
1279                                 isdn_net_dial();
1280                                 isdn_net_device_stop_queue(lp);
1281                                 return 1;
1282                         } else {
1283                                 isdn_net_unreachable(ndev, skb,
1284                                                      "No phone number");
1285                                 dev_kfree_skb(skb);
1286                                 return 0;
1287                         }
1288                 } else {
1289                         /* Device is connected to an ISDN channel */ 
1290                         ndev->trans_start = jiffies;
1291                         if (!lp->dialstate) {
1292                                 /* ISDN connection is established, try sending */
1293                                 int ret;
1294                                 ret = (isdn_net_xmit(ndev, skb));
1295                                 if(ret) netif_stop_queue(ndev);
1296                                 return ret;
1297                         } else
1298                                 netif_stop_queue(ndev);
1299                 }
1300         }
1301         return 1;
1302 }
1303
1304 /*
1305  * Shutdown a net-interface.
1306  */
1307 static int
1308 isdn_net_close(struct net_device *dev)
1309 {
1310         struct net_device *p;
1311 #ifdef CONFIG_ISDN_X25
1312         struct concap_proto * cprot =
1313                 ((isdn_net_local *) netdev_priv(dev))->netdev->cprot;
1314         /* printk(KERN_DEBUG "isdn_net_close %s\n" , dev-> name ); */
1315 #endif
1316
1317 #ifdef CONFIG_ISDN_X25
1318         if( cprot && cprot -> pops ) cprot -> pops -> close( cprot );
1319 #endif
1320         netif_stop_queue(dev);
1321         p = MASTER_TO_SLAVE(dev);
1322         if (p) {
1323                 /* If this interface has slaves, stop them also */
1324                 while (p) {
1325 #ifdef CONFIG_ISDN_X25
1326                         cprot = ((isdn_net_local *) netdev_priv(p))
1327                                 -> netdev -> cprot;
1328                         if( cprot && cprot -> pops )
1329                                 cprot -> pops -> close( cprot );
1330 #endif
1331                         isdn_net_hangup(p);
1332                         p = MASTER_TO_SLAVE(p);
1333                 }
1334         }
1335         isdn_net_hangup(dev);
1336         isdn_unlock_drivers();
1337         return 0;
1338 }
1339
1340 /*
1341  * Get statistics
1342  */
1343 static struct net_device_stats *
1344 isdn_net_get_stats(struct net_device *dev)
1345 {
1346         isdn_net_local *lp = (isdn_net_local *) netdev_priv(dev);
1347         return &lp->stats;
1348 }
1349
1350 /*      This is simply a copy from std. eth.c EXCEPT we pull ETH_HLEN
1351  *      instead of dev->hard_header_len off. This is done because the
1352  *      lowlevel-driver has already pulled off its stuff when we get
1353  *      here and this routine only gets called with p_encap == ETHER.
1354  *      Determine the packet's protocol ID. The rule here is that we
1355  *      assume 802.3 if the type field is short enough to be a length.
1356  *      This is normal practice and works for any 'now in use' protocol.
1357  */
1358
1359 static __be16
1360 isdn_net_type_trans(struct sk_buff *skb, struct net_device *dev)
1361 {
1362         struct ethhdr *eth;
1363         unsigned char *rawp;
1364
1365         skb_reset_mac_header(skb);
1366         skb_pull(skb, ETH_HLEN);
1367         eth = eth_hdr(skb);
1368
1369         if (*eth->h_dest & 1) {
1370                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
1371                         skb->pkt_type = PACKET_BROADCAST;
1372                 else
1373                         skb->pkt_type = PACKET_MULTICAST;
1374         }
1375         /*
1376          *      This ALLMULTI check should be redundant by 1.4
1377          *      so don't forget to remove it.
1378          */
1379
1380         else if (dev->flags & (IFF_PROMISC /*| IFF_ALLMULTI*/)) {
1381                 if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
1382                         skb->pkt_type = PACKET_OTHERHOST;
1383         }
1384         if (ntohs(eth->h_proto) >= 1536)
1385                 return eth->h_proto;
1386
1387         rawp = skb->data;
1388
1389         /*
1390          *      This is a magic hack to spot IPX packets. Older Novell breaks
1391          *      the protocol design and runs IPX over 802.3 without an 802.2 LLC
1392          *      layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
1393          *      won't work for fault tolerant netware but does for the rest.
1394          */
1395         if (*(unsigned short *) rawp == 0xFFFF)
1396                 return htons(ETH_P_802_3);
1397         /*
1398          *      Real 802.2 LLC
1399          */
1400         return htons(ETH_P_802_2);
1401 }
1402
1403
1404 /* 
1405  * CISCO HDLC keepalive specific stuff
1406  */
1407 static struct sk_buff*
1408 isdn_net_ciscohdlck_alloc_skb(isdn_net_local *lp, int len)
1409 {
1410         unsigned short hl = dev->drv[lp->isdn_device]->interface->hl_hdrlen;
1411         struct sk_buff *skb;
1412
1413         skb = alloc_skb(hl + len, GFP_ATOMIC);
1414         if (skb)
1415                 skb_reserve(skb, hl);
1416         else 
1417                 printk("isdn out of mem at %s:%d!\n", __FILE__, __LINE__);
1418         return skb;
1419 }
1420
1421 /* cisco hdlck device private ioctls */
1422 static int
1423 isdn_ciscohdlck_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1424 {
1425         isdn_net_local *lp = (isdn_net_local *) netdev_priv(dev);
1426         unsigned long len = 0;
1427         unsigned long expires = 0;
1428         int tmp = 0;
1429         int period = lp->cisco_keepalive_period;
1430         s8 debserint = lp->cisco_debserint;
1431         int rc = 0;
1432
1433         if (lp->p_encap != ISDN_NET_ENCAP_CISCOHDLCK)
1434                 return -EINVAL;
1435
1436         switch (cmd) {
1437                 /* get/set keepalive period */
1438                 case SIOCGKEEPPERIOD:
1439                         len = (unsigned long)sizeof(lp->cisco_keepalive_period);
1440                         if (copy_to_user(ifr->ifr_data,
1441                                 &lp->cisco_keepalive_period, len))
1442                                 rc = -EFAULT;
1443                         break;
1444                 case SIOCSKEEPPERIOD:
1445                         tmp = lp->cisco_keepalive_period;
1446                         len = (unsigned long)sizeof(lp->cisco_keepalive_period);
1447                         if (copy_from_user(&period, ifr->ifr_data, len))
1448                                 rc = -EFAULT;
1449                         if ((period > 0) && (period <= 32767))
1450                                 lp->cisco_keepalive_period = period;
1451                         else
1452                                 rc = -EINVAL;
1453                         if (!rc && (tmp != lp->cisco_keepalive_period)) {
1454                                 expires = (unsigned long)(jiffies +
1455                                         lp->cisco_keepalive_period * HZ);
1456                                 mod_timer(&lp->cisco_timer, expires);
1457                                 printk(KERN_INFO "%s: Keepalive period set "
1458                                         "to %d seconds.\n",
1459                                         dev->name, lp->cisco_keepalive_period);
1460                         }
1461                         break;
1462
1463                 /* get/set debugging */
1464                 case SIOCGDEBSERINT:
1465                         len = (unsigned long)sizeof(lp->cisco_debserint);
1466                         if (copy_to_user(ifr->ifr_data,
1467                                 &lp->cisco_debserint, len))
1468                                 rc = -EFAULT;
1469                         break;
1470                 case SIOCSDEBSERINT:
1471                         len = (unsigned long)sizeof(lp->cisco_debserint);
1472                         if (copy_from_user(&debserint,
1473                                 ifr->ifr_data, len))
1474                                 rc = -EFAULT;
1475                         if ((debserint >= 0) && (debserint <= 64))
1476                                 lp->cisco_debserint = debserint;
1477                         else
1478                                 rc = -EINVAL;
1479                         break;
1480
1481                 default:
1482                         rc = -EINVAL;
1483                         break;
1484         }
1485         return (rc);
1486 }
1487
1488
1489 static int isdn_net_ioctl(struct net_device *dev,
1490                           struct ifreq *ifr, int cmd)
1491 {
1492         isdn_net_local *lp = (isdn_net_local *) netdev_priv(dev);
1493
1494         switch (lp->p_encap) {
1495 #ifdef CONFIG_ISDN_PPP
1496         case ISDN_NET_ENCAP_SYNCPPP:
1497                 return isdn_ppp_dev_ioctl(dev, ifr, cmd);
1498 #endif
1499         case ISDN_NET_ENCAP_CISCOHDLCK:
1500                 return isdn_ciscohdlck_dev_ioctl(dev, ifr, cmd);
1501         default:
1502                 return -EINVAL;
1503         }
1504 }
1505
1506 /* called via cisco_timer.function */
1507 static void
1508 isdn_net_ciscohdlck_slarp_send_keepalive(unsigned long data)
1509 {
1510         isdn_net_local *lp = (isdn_net_local *) data;
1511         struct sk_buff *skb;
1512         unsigned char *p;
1513         unsigned long last_cisco_myseq = lp->cisco_myseq;
1514         int myseq_diff = 0;
1515
1516         if (!(lp->flags & ISDN_NET_CONNECTED) || lp->dialstate) {
1517                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1518                 return;
1519         }
1520         lp->cisco_myseq++;
1521
1522         myseq_diff = (lp->cisco_myseq - lp->cisco_mineseen);
1523         if ((lp->cisco_line_state) && ((myseq_diff >= 3)||(myseq_diff <= -3))) {
1524                 /* line up -> down */
1525                 lp->cisco_line_state = 0;
1526                 printk (KERN_WARNING
1527                                 "UPDOWN: Line protocol on Interface %s,"
1528                                 " changed state to down\n", lp->netdev->dev->name);
1529                 /* should stop routing higher-level data accross */
1530         } else if ((!lp->cisco_line_state) &&
1531                 (myseq_diff >= 0) && (myseq_diff <= 2)) {
1532                 /* line down -> up */
1533                 lp->cisco_line_state = 1;
1534                 printk (KERN_WARNING
1535                                 "UPDOWN: Line protocol on Interface %s,"
1536                                 " changed state to up\n", lp->netdev->dev->name);
1537                 /* restart routing higher-level data accross */
1538         }
1539
1540         if (lp->cisco_debserint)
1541                 printk (KERN_DEBUG "%s: HDLC "
1542                         "myseq %lu, mineseen %lu%c, yourseen %lu, %s\n",
1543                         lp->netdev->dev->name, last_cisco_myseq, lp->cisco_mineseen,
1544                         ((last_cisco_myseq == lp->cisco_mineseen) ? '*' : 040),
1545                         lp->cisco_yourseq,
1546                         ((lp->cisco_line_state) ? "line up" : "line down"));
1547
1548         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1549         if (!skb)
1550                 return;
1551
1552         p = skb_put(skb, 4 + 14);
1553
1554         /* cisco header */
1555         *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
1556         *(u8 *)(p + 1) = CISCO_CTRL;
1557         *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
1558
1559         /* slarp keepalive */
1560         *(__be32 *)(p +  4) = cpu_to_be32(CISCO_SLARP_KEEPALIVE);
1561         *(__be32 *)(p +  8) = cpu_to_be32(lp->cisco_myseq);
1562         *(__be32 *)(p + 12) = cpu_to_be32(lp->cisco_yourseq);
1563         *(__be16 *)(p + 16) = cpu_to_be16(0xffff); // reliablity, always 0xffff
1564         p += 18;
1565
1566         isdn_net_write_super(lp, skb);
1567
1568         lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
1569         
1570         add_timer(&lp->cisco_timer);
1571 }
1572
1573 static void
1574 isdn_net_ciscohdlck_slarp_send_request(isdn_net_local *lp)
1575 {
1576         struct sk_buff *skb;
1577         unsigned char *p;
1578
1579         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1580         if (!skb)
1581                 return;
1582
1583         p = skb_put(skb, 4 + 14);
1584
1585         /* cisco header */
1586         *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
1587         *(u8 *)(p + 1) = CISCO_CTRL;
1588         *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
1589
1590         /* slarp request */
1591         *(__be32 *)(p +  4) = cpu_to_be32(CISCO_SLARP_REQUEST);
1592         *(__be32 *)(p +  8) = cpu_to_be32(0); // address
1593         *(__be32 *)(p + 12) = cpu_to_be32(0); // netmask
1594         *(__be16 *)(p + 16) = cpu_to_be16(0); // unused
1595         p += 18;
1596
1597         isdn_net_write_super(lp, skb);
1598 }
1599
1600 static void 
1601 isdn_net_ciscohdlck_connected(isdn_net_local *lp)
1602 {
1603         lp->cisco_myseq = 0;
1604         lp->cisco_mineseen = 0;
1605         lp->cisco_yourseq = 0;
1606         lp->cisco_keepalive_period = ISDN_TIMER_KEEPINT;
1607         lp->cisco_last_slarp_in = 0;
1608         lp->cisco_line_state = 0;
1609         lp->cisco_debserint = 0;
1610
1611         /* send slarp request because interface/seq.no.s reset */
1612         isdn_net_ciscohdlck_slarp_send_request(lp);
1613
1614         init_timer(&lp->cisco_timer);
1615         lp->cisco_timer.data = (unsigned long) lp;
1616         lp->cisco_timer.function = isdn_net_ciscohdlck_slarp_send_keepalive;
1617         lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
1618         add_timer(&lp->cisco_timer);
1619 }
1620
1621 static void 
1622 isdn_net_ciscohdlck_disconnected(isdn_net_local *lp)
1623 {
1624         del_timer(&lp->cisco_timer);
1625 }
1626
1627 static void
1628 isdn_net_ciscohdlck_slarp_send_reply(isdn_net_local *lp)
1629 {
1630         struct sk_buff *skb;
1631         unsigned char *p;
1632         struct in_device *in_dev = NULL;
1633         __be32 addr = 0;                /* local ipv4 address */
1634         __be32 mask = 0;                /* local netmask */
1635
1636         if ((in_dev = lp->netdev->dev->ip_ptr) != NULL) {
1637                 /* take primary(first) address of interface */
1638                 struct in_ifaddr *ifa = in_dev->ifa_list;
1639                 if (ifa != NULL) {
1640                         addr = ifa->ifa_local;
1641                         mask = ifa->ifa_mask;
1642                 }
1643         }
1644
1645         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1646         if (!skb)
1647                 return;
1648
1649         p = skb_put(skb, 4 + 14);
1650
1651         /* cisco header */
1652         *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
1653         *(u8 *)(p + 1) = CISCO_CTRL;
1654         *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
1655
1656         /* slarp reply, send own ip/netmask; if values are nonsense remote
1657          * should think we are unable to provide it with an address via SLARP */
1658         *(__be32 *)(p +  4) = cpu_to_be32(CISCO_SLARP_REPLY);
1659         *(__be32 *)(p +  8) = addr; // address
1660         *(__be32 *)(p + 12) = mask; // netmask
1661         *(__be16 *)(p + 16) = cpu_to_be16(0); // unused
1662         p += 18;
1663
1664         isdn_net_write_super(lp, skb);
1665 }
1666
1667 static void
1668 isdn_net_ciscohdlck_slarp_in(isdn_net_local *lp, struct sk_buff *skb)
1669 {
1670         unsigned char *p;
1671         int period;
1672         u32 code;
1673         u32 my_seq;
1674         u32 your_seq;
1675         __be32 local;
1676         __be32 *addr, *mask;
1677         u16 unused;
1678
1679         if (skb->len < 14)
1680                 return;
1681
1682         p = skb->data;
1683         code = be32_to_cpup((__be32 *)p);
1684         p += 4;
1685
1686         switch (code) {
1687         case CISCO_SLARP_REQUEST:
1688                 lp->cisco_yourseq = 0;
1689                 isdn_net_ciscohdlck_slarp_send_reply(lp);
1690                 break;
1691         case CISCO_SLARP_REPLY:
1692                 addr = (__be32 *)p;
1693                 mask = (__be32 *)(p + 4);
1694                 if (*mask != cpu_to_be32(0xfffffffc))
1695                         goto slarp_reply_out;
1696                 if ((*addr & cpu_to_be32(3)) == cpu_to_be32(0) ||
1697                     (*addr & cpu_to_be32(3)) == cpu_to_be32(3))
1698                         goto slarp_reply_out;
1699                 local = *addr ^ cpu_to_be32(3);
1700                 printk(KERN_INFO "%s: got slarp reply: remote ip: %pI4, local ip: %pI4 mask: %pI4\n",
1701                        lp->netdev->dev->name, addr, &local, mask);
1702                 break;
1703   slarp_reply_out:
1704                 printk(KERN_INFO "%s: got invalid slarp reply (%pI4/%pI4) - ignored\n",
1705                        lp->netdev->dev->name, addr, mask);
1706                 break;
1707         case CISCO_SLARP_KEEPALIVE:
1708                 period = (int)((jiffies - lp->cisco_last_slarp_in
1709                                 + HZ/2 - 1) / HZ);
1710                 if (lp->cisco_debserint &&
1711                                 (period != lp->cisco_keepalive_period) &&
1712                                 lp->cisco_last_slarp_in) {
1713                         printk(KERN_DEBUG "%s: Keepalive period mismatch - "
1714                                 "is %d but should be %d.\n",
1715                                 lp->netdev->dev->name, period,
1716                                 lp->cisco_keepalive_period);
1717                 }
1718                 lp->cisco_last_slarp_in = jiffies;
1719                 my_seq = be32_to_cpup((__be32 *)(p + 0));
1720                 your_seq = be32_to_cpup((__be32 *)(p + 4));
1721                 unused = be16_to_cpup((__be16 *)(p + 8));
1722                 p += 10;
1723                 lp->cisco_yourseq = my_seq;
1724                 lp->cisco_mineseen = your_seq;
1725                 break;
1726         }
1727 }
1728
1729 static void
1730 isdn_net_ciscohdlck_receive(isdn_net_local *lp, struct sk_buff *skb)
1731 {
1732         unsigned char *p;
1733         u8 addr;
1734         u8 ctrl;
1735         u16 type;
1736         
1737         if (skb->len < 4)
1738                 goto out_free;
1739
1740         p = skb->data;
1741         addr = *(u8 *)(p + 0);
1742         ctrl = *(u8 *)(p + 1);
1743         type = be16_to_cpup((__be16 *)(p + 2));
1744         p += 4;
1745         skb_pull(skb, 4);
1746         
1747         if (addr != CISCO_ADDR_UNICAST && addr != CISCO_ADDR_BROADCAST) {
1748                 printk(KERN_WARNING "%s: Unknown Cisco addr 0x%02x\n",
1749                        lp->netdev->dev->name, addr);
1750                 goto out_free;
1751         }
1752         if (ctrl != CISCO_CTRL) {
1753                 printk(KERN_WARNING "%s: Unknown Cisco ctrl 0x%02x\n",
1754                        lp->netdev->dev->name, ctrl);
1755                 goto out_free;
1756         }
1757
1758         switch (type) {
1759         case CISCO_TYPE_SLARP:
1760                 isdn_net_ciscohdlck_slarp_in(lp, skb);
1761                 goto out_free;
1762         case CISCO_TYPE_CDP:
1763                 if (lp->cisco_debserint)
1764                         printk(KERN_DEBUG "%s: Received CDP packet. use "
1765                                 "\"no cdp enable\" on cisco.\n",
1766                                 lp->netdev->dev->name);
1767                 goto out_free;
1768         default:
1769                 /* no special cisco protocol */
1770                 skb->protocol = htons(type);
1771                 netif_rx(skb);
1772                 return;
1773         }
1774
1775  out_free:
1776         kfree_skb(skb);
1777 }
1778
1779 /*
1780  * Got a packet from ISDN-Channel.
1781  */
1782 static void
1783 isdn_net_receive(struct net_device *ndev, struct sk_buff *skb)
1784 {
1785         isdn_net_local *lp = (isdn_net_local *) netdev_priv(ndev);
1786         isdn_net_local *olp = lp;       /* original 'lp' */
1787 #ifdef CONFIG_ISDN_X25
1788         struct concap_proto *cprot = lp -> netdev -> cprot;
1789 #endif
1790         lp->transcount += skb->len;
1791
1792         lp->stats.rx_packets++;
1793         lp->stats.rx_bytes += skb->len;
1794         if (lp->master) {
1795                 /* Bundling: If device is a slave-device, deliver to master, also
1796                  * handle master's statistics and hangup-timeout
1797                  */
1798                 ndev = lp->master;
1799                 lp = (isdn_net_local *) netdev_priv(ndev);
1800                 lp->stats.rx_packets++;
1801                 lp->stats.rx_bytes += skb->len;
1802         }
1803         skb->dev = ndev;
1804         skb->pkt_type = PACKET_HOST;
1805         skb_reset_mac_header(skb);
1806 #ifdef ISDN_DEBUG_NET_DUMP
1807         isdn_dumppkt("R:", skb->data, skb->len, 40);
1808 #endif
1809         switch (lp->p_encap) {
1810                 case ISDN_NET_ENCAP_ETHER:
1811                         /* Ethernet over ISDN */
1812                         olp->huptimer = 0;
1813                         lp->huptimer = 0;
1814                         skb->protocol = isdn_net_type_trans(skb, ndev);
1815                         break;
1816                 case ISDN_NET_ENCAP_UIHDLC:
1817                         /* HDLC with UI-frame (for ispa with -h1 option) */
1818                         olp->huptimer = 0;
1819                         lp->huptimer = 0;
1820                         skb_pull(skb, 2);
1821                         /* Fall through */
1822                 case ISDN_NET_ENCAP_RAWIP:
1823                         /* RAW-IP without MAC-Header */
1824                         olp->huptimer = 0;
1825                         lp->huptimer = 0;
1826                         skb->protocol = htons(ETH_P_IP);
1827                         break;
1828                 case ISDN_NET_ENCAP_CISCOHDLCK:
1829                         isdn_net_ciscohdlck_receive(lp, skb);
1830                         return;
1831                 case ISDN_NET_ENCAP_CISCOHDLC:
1832                         /* CISCO-HDLC IP with type field and  fake I-frame-header */
1833                         skb_pull(skb, 2);
1834                         /* Fall through */
1835                 case ISDN_NET_ENCAP_IPTYP:
1836                         /* IP with type field */
1837                         olp->huptimer = 0;
1838                         lp->huptimer = 0;
1839                         skb->protocol = *(__be16 *)&(skb->data[0]);
1840                         skb_pull(skb, 2);
1841                         if (*(unsigned short *) skb->data == 0xFFFF)
1842                                 skb->protocol = htons(ETH_P_802_3);
1843                         break;
1844 #ifdef CONFIG_ISDN_PPP
1845                 case ISDN_NET_ENCAP_SYNCPPP:
1846                         /* huptimer is done in isdn_ppp_push_higher */
1847                         isdn_ppp_receive(lp->netdev, olp, skb);
1848                         return;
1849 #endif
1850
1851                 default:
1852 #ifdef CONFIG_ISDN_X25
1853                   /* try if there are generic sync_device receiver routines */
1854                         if(cprot) if(cprot -> pops)
1855                                 if( cprot -> pops -> data_ind){
1856                                         cprot -> pops -> data_ind(cprot,skb);
1857                                         return;
1858                                 };
1859 #endif /* CONFIG_ISDN_X25 */
1860                         printk(KERN_WARNING "%s: unknown encapsulation, dropping\n",
1861                                lp->netdev->dev->name);
1862                         kfree_skb(skb);
1863                         return;
1864         }
1865
1866         netif_rx(skb);
1867         return;
1868 }
1869
1870 /*
1871  * A packet arrived via ISDN. Search interface-chain for a corresponding
1872  * interface. If found, deliver packet to receiver-function and return 1,
1873  * else return 0.
1874  */
1875 int
1876 isdn_net_rcv_skb(int idx, struct sk_buff *skb)
1877 {
1878         isdn_net_dev *p = dev->rx_netdev[idx];
1879
1880         if (p) {
1881                 isdn_net_local *lp = p->local;
1882                 if ((lp->flags & ISDN_NET_CONNECTED) &&
1883                     (!lp->dialstate)) {
1884                         isdn_net_receive(p->dev, skb);
1885                         return 1;
1886                 }
1887         }
1888         return 0;
1889 }
1890
1891 /*
1892  *  build an header
1893  *  depends on encaps that is being used.
1894  */
1895
1896 static int isdn_net_header(struct sk_buff *skb, struct net_device *dev,
1897                            unsigned short type,
1898                            const void *daddr, const void *saddr, unsigned plen)
1899 {
1900         isdn_net_local *lp = netdev_priv(dev);
1901         unsigned char *p;
1902         ushort len = 0;
1903
1904         switch (lp->p_encap) {
1905                 case ISDN_NET_ENCAP_ETHER:
1906                         len = eth_header(skb, dev, type, daddr, saddr, plen);
1907                         break;
1908 #ifdef CONFIG_ISDN_PPP
1909                 case ISDN_NET_ENCAP_SYNCPPP:
1910                         /* stick on a fake header to keep fragmentation code happy. */
1911                         len = IPPP_MAX_HEADER;
1912                         skb_push(skb,len);
1913                         break;
1914 #endif
1915                 case ISDN_NET_ENCAP_RAWIP:
1916                         printk(KERN_WARNING "isdn_net_header called with RAW_IP!\n");
1917                         len = 0;
1918                         break;
1919                 case ISDN_NET_ENCAP_IPTYP:
1920                         /* ethernet type field */
1921                         *((__be16 *)skb_push(skb, 2)) = htons(type);
1922                         len = 2;
1923                         break;
1924                 case ISDN_NET_ENCAP_UIHDLC:
1925                         /* HDLC with UI-Frames (for ispa with -h1 option) */
1926                         *((__be16 *)skb_push(skb, 2)) = htons(0x0103);
1927                         len = 2;
1928                         break;
1929                 case ISDN_NET_ENCAP_CISCOHDLC:
1930                 case ISDN_NET_ENCAP_CISCOHDLCK:
1931                         p = skb_push(skb, 4);
1932                         *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
1933                         *(u8 *)(p + 1) = CISCO_CTRL;
1934                         *(__be16 *)(p + 2) = cpu_to_be16(type);
1935                         p += 4;
1936                         len = 4;
1937                         break;
1938 #ifdef CONFIG_ISDN_X25
1939                 default:
1940                   /* try if there are generic concap protocol routines */
1941                         if( lp-> netdev -> cprot ){
1942                                 printk(KERN_WARNING "isdn_net_header called with concap_proto!\n");
1943                                 len = 0;
1944                                 break;
1945                         }
1946                         break;
1947 #endif /* CONFIG_ISDN_X25 */
1948         }
1949         return len;
1950 }
1951
1952 /* We don't need to send arp, because we have point-to-point connections. */
1953 static int
1954 isdn_net_rebuild_header(struct sk_buff *skb)
1955 {
1956         struct net_device *dev = skb->dev;
1957         isdn_net_local *lp = netdev_priv(dev);
1958         int ret = 0;
1959
1960         if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
1961                 struct ethhdr *eth = (struct ethhdr *) skb->data;
1962
1963                 /*
1964                  *      Only ARP/IP is currently supported
1965                  */
1966
1967                 if (eth->h_proto != htons(ETH_P_IP)) {
1968                         printk(KERN_WARNING
1969                                "isdn_net: %s don't know how to resolve type %d addresses?\n",
1970                                dev->name, (int) eth->h_proto);
1971                         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
1972                         return 0;
1973                 }
1974                 /*
1975                  *      Try to get ARP to resolve the header.
1976                  */
1977 #ifdef CONFIG_INET
1978                 ret = arp_find(eth->h_dest, skb);
1979 #endif
1980         }
1981         return ret;
1982 }
1983
1984 static int isdn_header_cache(const struct neighbour *neigh, struct hh_cache *hh)
1985 {
1986         const struct net_device *dev = neigh->dev;
1987         isdn_net_local *lp = netdev_priv(dev);
1988
1989         if (lp->p_encap == ISDN_NET_ENCAP_ETHER)
1990                 return eth_header_cache(neigh, hh);
1991         return -1;
1992 }
1993
1994 static void isdn_header_cache_update(struct hh_cache *hh,
1995                                      const struct net_device *dev,
1996                                      const unsigned char *haddr)
1997 {
1998         isdn_net_local *lp = netdev_priv(dev);
1999         if (lp->p_encap == ISDN_NET_ENCAP_ETHER)
2000                 eth_header_cache_update(hh, dev, haddr);
2001 }
2002
2003 static const struct header_ops isdn_header_ops = {
2004         .create = isdn_net_header,
2005         .rebuild = isdn_net_rebuild_header,
2006         .cache = isdn_header_cache,
2007         .cache_update = isdn_header_cache_update,
2008 };
2009
2010 /*
2011  * Interface-setup. (just after registering a new interface)
2012  */
2013 static int
2014 isdn_net_init(struct net_device *ndev)
2015 {
2016         ushort max_hlhdr_len = 0;
2017         int drvidx;
2018
2019         /*
2020          *  up till binding we ask the protocol layer to reserve as much
2021          *  as we might need for HL layer
2022          */
2023
2024         for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2025                 if (dev->drv[drvidx])
2026                         if (max_hlhdr_len < dev->drv[drvidx]->interface->hl_hdrlen)
2027                                 max_hlhdr_len = dev->drv[drvidx]->interface->hl_hdrlen;
2028
2029         ndev->hard_header_len = ETH_HLEN + max_hlhdr_len;
2030         return 0;
2031 }
2032
2033 static void
2034 isdn_net_swapbind(int drvidx)
2035 {
2036         isdn_net_dev *p;
2037
2038 #ifdef ISDN_DEBUG_NET_ICALL
2039         printk(KERN_DEBUG "n_fi: swapping ch of %d\n", drvidx);
2040 #endif
2041         p = dev->netdev;
2042         while (p) {
2043                 if (p->local->pre_device == drvidx)
2044                         switch (p->local->pre_channel) {
2045                                 case 0:
2046                                         p->local->pre_channel = 1;
2047                                         break;
2048                                 case 1:
2049                                         p->local->pre_channel = 0;
2050                                         break;
2051                         }
2052                 p = (isdn_net_dev *) p->next;
2053         }
2054 }
2055
2056 static void
2057 isdn_net_swap_usage(int i1, int i2)
2058 {
2059         int u1 = dev->usage[i1] & ISDN_USAGE_EXCLUSIVE;
2060         int u2 = dev->usage[i2] & ISDN_USAGE_EXCLUSIVE;
2061
2062 #ifdef ISDN_DEBUG_NET_ICALL
2063         printk(KERN_DEBUG "n_fi: usage of %d and %d\n", i1, i2);
2064 #endif
2065         dev->usage[i1] &= ~ISDN_USAGE_EXCLUSIVE;
2066         dev->usage[i1] |= u2;
2067         dev->usage[i2] &= ~ISDN_USAGE_EXCLUSIVE;
2068         dev->usage[i2] |= u1;
2069         isdn_info_update();
2070 }
2071
2072 /*
2073  * An incoming call-request has arrived.
2074  * Search the interface-chain for an appropriate interface.
2075  * If found, connect the interface to the ISDN-channel and initiate
2076  * D- and B-Channel-setup. If secure-flag is set, accept only
2077  * configured phone-numbers. If callback-flag is set, initiate
2078  * callback-dialing.
2079  *
2080  * Return-Value: 0 = No appropriate interface for this call.
2081  *               1 = Call accepted
2082  *               2 = Reject call, wait cbdelay, then call back
2083  *               3 = Reject call
2084  *               4 = Wait cbdelay, then call back
2085  *               5 = No appropriate interface for this call,
2086  *                   would eventually match if CID was longer.
2087  */
2088
2089 int
2090 isdn_net_find_icall(int di, int ch, int idx, setup_parm *setup)
2091 {
2092         char *eaz;
2093         int si1;
2094         int si2;
2095         int ematch;
2096         int wret;
2097         int swapped;
2098         int sidx = 0;
2099         u_long flags;
2100         isdn_net_dev *p;
2101         isdn_net_phone *n;
2102         char nr[ISDN_MSNLEN];
2103         char *my_eaz;
2104
2105         /* Search name in netdev-chain */
2106         if (!setup->phone[0]) {
2107                 nr[0] = '0';
2108                 nr[1] = '\0';
2109                 printk(KERN_INFO "isdn_net: Incoming call without OAD, assuming '0'\n");
2110         } else
2111                 strlcpy(nr, setup->phone, ISDN_MSNLEN);
2112         si1 = (int) setup->si1;
2113         si2 = (int) setup->si2;
2114         if (!setup->eazmsn[0]) {
2115                 printk(KERN_WARNING "isdn_net: Incoming call without CPN, assuming '0'\n");
2116                 eaz = "0";
2117         } else
2118                 eaz = setup->eazmsn;
2119         if (dev->net_verbose > 1)
2120                 printk(KERN_INFO "isdn_net: call from %s,%d,%d -> %s\n", nr, si1, si2, eaz);
2121         /* Accept DATA and VOICE calls at this stage
2122          * local eaz is checked later for allowed call types
2123          */
2124         if ((si1 != 7) && (si1 != 1)) {
2125                 if (dev->net_verbose > 1)
2126                         printk(KERN_INFO "isdn_net: Service-Indicator not 1 or 7, ignored\n");
2127                 return 0;
2128         }
2129         n = (isdn_net_phone *) 0;
2130         p = dev->netdev;
2131         ematch = wret = swapped = 0;
2132 #ifdef ISDN_DEBUG_NET_ICALL
2133         printk(KERN_DEBUG "n_fi: di=%d ch=%d idx=%d usg=%d\n", di, ch, idx,
2134                 dev->usage[idx]);
2135 #endif
2136         while (p) {
2137                 int matchret;
2138                 isdn_net_local *lp = p->local;
2139
2140                 /* If last check has triggered as binding-swap, revert it */
2141                 switch (swapped) {
2142                         case 2:
2143                                 isdn_net_swap_usage(idx, sidx);
2144                                 /* fall through */
2145                         case 1:
2146                                 isdn_net_swapbind(di);
2147                                 break;
2148                 }
2149                 swapped = 0;
2150                 /* check acceptable call types for DOV */
2151                 my_eaz = isdn_map_eaz2msn(lp->msn, di);
2152                 if (si1 == 1) { /* it's a DOV call, check if we allow it */
2153                         if (*my_eaz == 'v' || *my_eaz == 'V' ||
2154                             *my_eaz == 'b' || *my_eaz == 'B')
2155                                 my_eaz++; /* skip to allow a match */
2156                         else
2157                                 my_eaz = NULL; /* force non match */
2158                 } else { /* it's a DATA call, check if we allow it */
2159                         if (*my_eaz == 'b' || *my_eaz == 'B')
2160                                 my_eaz++; /* skip to allow a match */
2161                 }
2162                 if (my_eaz)
2163                         matchret = isdn_msncmp(eaz, my_eaz);
2164                 else
2165                         matchret = 1;
2166                 if (!matchret)
2167                         ematch = 1;
2168
2169                 /* Remember if more numbers eventually can match */
2170                 if (matchret > wret)
2171                         wret = matchret;
2172 #ifdef ISDN_DEBUG_NET_ICALL
2173                 printk(KERN_DEBUG "n_fi: if='%s', l.msn=%s, l.flags=%d, l.dstate=%d\n",
2174                        p->dev->name, lp->msn, lp->flags, lp->dialstate);
2175 #endif
2176                 if ((!matchret) &&                                        /* EAZ is matching   */
2177                     (((!(lp->flags & ISDN_NET_CONNECTED)) &&              /* but not connected */
2178                       (USG_NONE(dev->usage[idx]))) ||                     /* and ch. unused or */
2179                      ((((lp->dialstate == 4) || (lp->dialstate == 12)) && /* if dialing        */
2180                        (!(lp->flags & ISDN_NET_CALLBACK)))                /* but no callback   */
2181                      )))
2182                          {
2183 #ifdef ISDN_DEBUG_NET_ICALL
2184                         printk(KERN_DEBUG "n_fi: match1, pdev=%d pch=%d\n",
2185                                lp->pre_device, lp->pre_channel);
2186 #endif
2187                         if (dev->usage[idx] & ISDN_USAGE_EXCLUSIVE) {
2188                                 if ((lp->pre_channel != ch) ||
2189                                     (lp->pre_device != di)) {
2190                                         /* Here we got a problem:
2191                                          * If using an ICN-Card, an incoming call is always signaled on
2192                                          * on the first channel of the card, if both channels are
2193                                          * down. However this channel may be bound exclusive. If the
2194                                          * second channel is free, this call should be accepted.
2195                                          * The solution is horribly but it runs, so what:
2196                                          * We exchange the exclusive bindings of the two channels, the
2197                                          * corresponding variables in the interface-structs.
2198                                          */
2199                                         if (ch == 0) {
2200                                                 sidx = isdn_dc2minor(di, 1);
2201 #ifdef ISDN_DEBUG_NET_ICALL
2202                                                 printk(KERN_DEBUG "n_fi: ch is 0\n");
2203 #endif
2204                                                 if (USG_NONE(dev->usage[sidx])) {
2205                                                         /* Second Channel is free, now see if it is bound
2206                                                          * exclusive too. */
2207                                                         if (dev->usage[sidx] & ISDN_USAGE_EXCLUSIVE) {
2208 #ifdef ISDN_DEBUG_NET_ICALL
2209                                                                 printk(KERN_DEBUG "n_fi: 2nd channel is down and bound\n");
2210 #endif
2211                                                                 /* Yes, swap bindings only, if the original
2212                                                                  * binding is bound to channel 1 of this driver */
2213                                                                 if ((lp->pre_device == di) &&
2214                                                                     (lp->pre_channel == 1)) {
2215                                                                         isdn_net_swapbind(di);
2216                                                                         swapped = 1;
2217                                                                 } else {
2218                                                                         /* ... else iterate next device */
2219                                                                         p = (isdn_net_dev *) p->next;
2220                                                                         continue;
2221                                                                 }
2222                                                         } else {
2223 #ifdef ISDN_DEBUG_NET_ICALL
2224                                                                 printk(KERN_DEBUG "n_fi: 2nd channel is down and unbound\n");
2225 #endif
2226                                                                 /* No, swap always and swap excl-usage also */
2227                                                                 isdn_net_swap_usage(idx, sidx);
2228                                                                 isdn_net_swapbind(di);
2229                                                                 swapped = 2;
2230                                                         }
2231                                                         /* Now check for exclusive binding again */
2232 #ifdef ISDN_DEBUG_NET_ICALL
2233                                                         printk(KERN_DEBUG "n_fi: final check\n");
2234 #endif
2235                                                         if ((dev->usage[idx] & ISDN_USAGE_EXCLUSIVE) &&
2236                                                             ((lp->pre_channel != ch) ||
2237                                                              (lp->pre_device != di))) {
2238 #ifdef ISDN_DEBUG_NET_ICALL
2239                                                                 printk(KERN_DEBUG "n_fi: final check failed\n");
2240 #endif
2241                                                                 p = (isdn_net_dev *) p->next;
2242                                                                 continue;
2243                                                         }
2244                                                 }
2245                                         } else {
2246                                                 /* We are already on the second channel, so nothing to do */
2247 #ifdef ISDN_DEBUG_NET_ICALL
2248                                                 printk(KERN_DEBUG "n_fi: already on 2nd channel\n");
2249 #endif
2250                                         }
2251                                 }
2252                         }
2253 #ifdef ISDN_DEBUG_NET_ICALL
2254                         printk(KERN_DEBUG "n_fi: match2\n");
2255 #endif
2256                         n = lp->phone[0];
2257                         if (lp->flags & ISDN_NET_SECURE) {
2258                                 while (n) {
2259                                         if (!isdn_msncmp(nr, n->num))
2260                                                 break;
2261                                         n = (isdn_net_phone *) n->next;
2262                                 }
2263                         }
2264                         if (n || (!(lp->flags & ISDN_NET_SECURE))) {
2265 #ifdef ISDN_DEBUG_NET_ICALL
2266                                 printk(KERN_DEBUG "n_fi: match3\n");
2267 #endif
2268                                 /* matching interface found */
2269
2270                                 /*
2271                                  * Is the state STOPPED?
2272                                  * If so, no dialin is allowed,
2273                                  * so reject actively.
2274                                  * */
2275                                 if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
2276                                         printk(KERN_INFO "incoming call, interface %s `stopped' -> rejected\n",
2277                                                p->dev->name);
2278                                         return 3;
2279                                 }
2280                                 /*
2281                                  * Is the interface up?
2282                                  * If not, reject the call actively.
2283                                  */
2284                                 if (!isdn_net_device_started(p)) {
2285                                         printk(KERN_INFO "%s: incoming call, interface down -> rejected\n",
2286                                                p->dev->name);
2287                                         return 3;
2288                                 }
2289                                 /* Interface is up, now see if it's a slave. If so, see if
2290                                  * it's master and parent slave is online. If not, reject the call.
2291                                  */
2292                                 if (lp->master) {
2293                                         isdn_net_local *mlp = ISDN_MASTER_PRIV(lp);
2294                                         printk(KERN_DEBUG "ICALLslv: %s\n", p->dev->name);
2295                                         printk(KERN_DEBUG "master=%s\n", lp->master->name);
2296                                         if (mlp->flags & ISDN_NET_CONNECTED) {
2297                                                 printk(KERN_DEBUG "master online\n");
2298                                                 /* Master is online, find parent-slave (master if first slave) */
2299                                                 while (mlp->slave) {
2300                                                         if (ISDN_SLAVE_PRIV(mlp) == lp)
2301                                                                 break;
2302                                                         mlp = ISDN_SLAVE_PRIV(mlp);
2303                                                 }
2304                                         } else
2305                                                 printk(KERN_DEBUG "master offline\n");
2306                                         /* Found parent, if it's offline iterate next device */
2307                                         printk(KERN_DEBUG "mlpf: %d\n", mlp->flags & ISDN_NET_CONNECTED);
2308                                         if (!(mlp->flags & ISDN_NET_CONNECTED)) {
2309                                                 p = (isdn_net_dev *) p->next;
2310                                                 continue;
2311                                         }
2312                                 } 
2313                                 if (lp->flags & ISDN_NET_CALLBACK) {
2314                                         int chi;
2315                                         /*
2316                                          * Is the state MANUAL?
2317                                          * If so, no callback can be made,
2318                                          * so reject actively.
2319                                          * */
2320                                         if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
2321                                                 printk(KERN_INFO "incoming call for callback, interface %s `off' -> rejected\n",
2322                                                        p->dev->name);
2323                                                 return 3;
2324                                         }
2325                                         printk(KERN_DEBUG "%s: call from %s -> %s, start callback\n",
2326                                                p->dev->name, nr, eaz);
2327                                         if (lp->phone[1]) {
2328                                                 /* Grab a free ISDN-Channel */
2329                                                 spin_lock_irqsave(&dev->lock, flags);
2330                                                 if ((chi = 
2331                                                         isdn_get_free_channel(
2332                                                                 ISDN_USAGE_NET,
2333                                                                 lp->l2_proto,
2334                                                                 lp->l3_proto,
2335                                                                 lp->pre_device,
2336                                                                 lp->pre_channel,
2337                                                                 lp->msn)
2338                                                                 ) < 0) {
2339
2340                                                         printk(KERN_WARNING "isdn_net_find_icall: No channel for %s\n",
2341                                                                 p->dev->name);
2342                                                         spin_unlock_irqrestore(&dev->lock, flags);
2343                                                         return 0;
2344                                                 }
2345                                                 /* Setup dialstate. */
2346                                                 lp->dtimer = 0;
2347                                                 lp->dialstate = 11;
2348                                                 /* Connect interface with channel */
2349                                                 isdn_net_bind_channel(lp, chi);
2350 #ifdef CONFIG_ISDN_PPP
2351                                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2352                                                         if (isdn_ppp_bind(lp) < 0) {
2353                                                                 spin_unlock_irqrestore(&dev->lock, flags);
2354                                                                 isdn_net_unbind_channel(lp);
2355                                                                 return 0;
2356                                                         }
2357 #endif
2358                                                 spin_unlock_irqrestore(&dev->lock, flags);
2359                                                 /* Initiate dialing by returning 2 or 4 */
2360                                                 return (lp->flags & ISDN_NET_CBHUP) ? 2 : 4;
2361                                         } else
2362                                                 printk(KERN_WARNING "isdn_net: %s: No phone number\n",
2363                                                         p->dev->name);
2364                                         return 0;
2365                                 } else {
2366                                         printk(KERN_DEBUG "%s: call from %s -> %s accepted\n",
2367                                                 p->dev->name, nr, eaz);
2368                                         /* if this interface is dialing, it does it probably on a different
2369                                            device, so free this device */
2370                                         if ((lp->dialstate == 4) || (lp->dialstate == 12)) {
2371 #ifdef CONFIG_ISDN_PPP
2372                                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2373                                                         isdn_ppp_free(lp);
2374 #endif
2375                                                 isdn_net_lp_disconnected(lp);
2376                                                 isdn_free_channel(lp->isdn_device, lp->isdn_channel,
2377                                                          ISDN_USAGE_NET);
2378                                         }
2379                                         spin_lock_irqsave(&dev->lock, flags);
2380                                         dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
2381                                         dev->usage[idx] |= ISDN_USAGE_NET;
2382                                         strcpy(dev->num[idx], nr);
2383                                         isdn_info_update();
2384                                         dev->st_netdev[idx] = lp->netdev;
2385                                         lp->isdn_device = di;
2386                                         lp->isdn_channel = ch;
2387                                         lp->ppp_slot = -1;
2388                                         lp->flags |= ISDN_NET_CONNECTED;
2389                                         lp->dialstate = 7;
2390                                         lp->dtimer = 0;
2391                                         lp->outgoing = 0;
2392                                         lp->huptimer = 0;
2393                                         lp->hupflags |= ISDN_WAITCHARGE;
2394                                         lp->hupflags &= ~ISDN_HAVECHARGE;
2395 #ifdef CONFIG_ISDN_PPP
2396                                         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
2397                                                 if (isdn_ppp_bind(lp) < 0) {
2398                                                         isdn_net_unbind_channel(lp);
2399                                                         spin_unlock_irqrestore(&dev->lock, flags);
2400                                                         return 0;
2401                                                 }
2402                                         }
2403 #endif
2404                                         spin_unlock_irqrestore(&dev->lock, flags);
2405                                         return 1;
2406                                 }
2407                         }
2408                 }
2409                 p = (isdn_net_dev *) p->next;
2410         }
2411         /* If none of configured EAZ/MSN matched and not verbose, be silent */
2412         if (!ematch || dev->net_verbose)
2413                 printk(KERN_INFO "isdn_net: call from %s -> %d %s ignored\n", nr, di, eaz);
2414         return (wret == 2)?5:0;
2415 }
2416
2417 /*
2418  * Search list of net-interfaces for an interface with given name.
2419  */
2420 isdn_net_dev *
2421 isdn_net_findif(char *name)
2422 {
2423         isdn_net_dev *p = dev->netdev;
2424
2425         while (p) {
2426                 if (!strcmp(p->dev->name, name))
2427                         return p;
2428                 p = (isdn_net_dev *) p->next;
2429         }
2430         return (isdn_net_dev *) NULL;
2431 }
2432
2433 /*
2434  * Force a net-interface to dial out.
2435  * This is called from the userlevel-routine below or
2436  * from isdn_net_start_xmit().
2437  */
2438 static int
2439 isdn_net_force_dial_lp(isdn_net_local * lp)
2440 {
2441         if ((!(lp->flags & ISDN_NET_CONNECTED)) && !lp->dialstate) {
2442                 int chi;
2443                 if (lp->phone[1]) {
2444                         ulong flags;
2445
2446                         /* Grab a free ISDN-Channel */
2447                         spin_lock_irqsave(&dev->lock, flags);
2448                         if ((chi = isdn_get_free_channel(
2449                                         ISDN_USAGE_NET,
2450                                         lp->l2_proto,
2451                                         lp->l3_proto,
2452                                         lp->pre_device,
2453                                         lp->pre_channel,
2454                                         lp->msn)) < 0) {
2455                                 printk(KERN_WARNING "isdn_net_force_dial: No channel for %s\n",
2456                                         lp->netdev->dev->name);
2457                                 spin_unlock_irqrestore(&dev->lock, flags);
2458                                 return -EAGAIN;
2459                         }
2460                         lp->dialstate = 1;
2461                         /* Connect interface with channel */
2462                         isdn_net_bind_channel(lp, chi);
2463 #ifdef CONFIG_ISDN_PPP
2464                         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2465                                 if (isdn_ppp_bind(lp) < 0) {
2466                                         isdn_net_unbind_channel(lp);
2467                                         spin_unlock_irqrestore(&dev->lock, flags);
2468                                         return -EAGAIN;
2469                                 }
2470 #endif
2471                         /* Initiate dialing */
2472                         spin_unlock_irqrestore(&dev->lock, flags);
2473                         isdn_net_dial();
2474                         return 0;
2475                 } else
2476                         return -EINVAL;
2477         } else
2478                 return -EBUSY;
2479 }
2480
2481 /*
2482  * This is called from certain upper protocol layers (multilink ppp
2483  * and x25iface encapsulation module) that want to initiate dialing
2484  * themselves.
2485  */
2486 int
2487 isdn_net_dial_req(isdn_net_local * lp)
2488 {
2489         /* is there a better error code? */
2490         if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) return -EBUSY;
2491
2492         return isdn_net_force_dial_lp(lp);
2493 }
2494
2495 /*
2496  * Force a net-interface to dial out.
2497  * This is always called from within userspace (ISDN_IOCTL_NET_DIAL).
2498  */
2499 int
2500 isdn_net_force_dial(char *name)
2501 {
2502         isdn_net_dev *p = isdn_net_findif(name);
2503
2504         if (!p)
2505                 return -ENODEV;
2506         return (isdn_net_force_dial_lp(p->local));
2507 }
2508
2509 /* The ISDN-specific entries in the device structure. */
2510 static const struct net_device_ops isdn_netdev_ops = {
2511         .ndo_init             = isdn_net_init,
2512         .ndo_open             = isdn_net_open,
2513         .ndo_stop             = isdn_net_close,
2514         .ndo_do_ioctl         = isdn_net_ioctl,
2515
2516         .ndo_validate_addr    = NULL,
2517         .ndo_start_xmit       = isdn_net_start_xmit,
2518         .ndo_get_stats        = isdn_net_get_stats,
2519         .ndo_tx_timeout       = isdn_net_tx_timeout,
2520 };
2521
2522 /*
2523  * Helper for alloc_netdev()
2524  */
2525 static void _isdn_setup(struct net_device *dev)
2526 {
2527         isdn_net_local *lp = netdev_priv(dev);
2528
2529         ether_setup(dev);
2530
2531         dev->flags = IFF_NOARP | IFF_POINTOPOINT;
2532         /* Setup the generic properties */
2533         dev->mtu = 1500;
2534         dev->flags = IFF_NOARP|IFF_POINTOPOINT;
2535         dev->type = ARPHRD_ETHER;
2536         dev->addr_len = ETH_ALEN;
2537         dev->header_ops = NULL;
2538         dev->netdev_ops = &isdn_netdev_ops;
2539
2540         /* for clients with MPPP maybe higher values better */
2541         dev->tx_queue_len = 30;
2542
2543         lp->p_encap = ISDN_NET_ENCAP_RAWIP;
2544         lp->magic = ISDN_NET_MAGIC;
2545         lp->last = lp;
2546         lp->next = lp;
2547         lp->isdn_device = -1;
2548         lp->isdn_channel = -1;
2549         lp->pre_device = -1;
2550         lp->pre_channel = -1;
2551         lp->exclusive = -1;
2552         lp->ppp_slot = -1;
2553         lp->pppbind = -1;
2554         skb_queue_head_init(&lp->super_tx_queue);
2555         lp->l2_proto = ISDN_PROTO_L2_X75I;
2556         lp->l3_proto = ISDN_PROTO_L3_TRANS;
2557         lp->triggercps = 6000;
2558         lp->slavedelay = 10 * HZ;
2559         lp->hupflags = ISDN_INHUP;      /* Do hangup even on incoming calls */
2560         lp->onhtime = 10;       /* Default hangup-time for saving costs */
2561         lp->dialmax = 1;
2562         /* Hangup before Callback, manual dial */
2563         lp->flags = ISDN_NET_CBHUP | ISDN_NET_DM_MANUAL;
2564         lp->cbdelay = 25;       /* Wait 5 secs before Callback */
2565         lp->dialtimeout = -1;  /* Infinite Dial-Timeout */
2566         lp->dialwait = 5 * HZ; /* Wait 5 sec. after failed dial */
2567         lp->dialstarted = 0;   /* Jiffies of last dial-start */
2568         lp->dialwait_timer = 0;  /* Jiffies of earliest next dial-start */
2569 }
2570
2571 /*
2572  * Allocate a new network-interface and initialize its data structures.
2573  */
2574 char *
2575 isdn_net_new(char *name, struct net_device *master)
2576 {
2577         isdn_net_dev *netdev;
2578
2579         /* Avoid creating an existing interface */
2580         if (isdn_net_findif(name)) {
2581                 printk(KERN_WARNING "isdn_net: interface %s already exists\n", name);
2582                 return NULL;
2583         }
2584         if (name == NULL)
2585                 return NULL;
2586         if (!(netdev = kzalloc(sizeof(isdn_net_dev), GFP_KERNEL))) {
2587                 printk(KERN_WARNING "isdn_net: Could not allocate net-device\n");
2588                 return NULL;
2589         }
2590         netdev->dev = alloc_netdev(sizeof(isdn_net_local), name, _isdn_setup);
2591         if (!netdev->dev) {
2592                 printk(KERN_WARNING "isdn_net: Could not allocate network device\n");
2593                 kfree(netdev);
2594                 return NULL;
2595         }
2596         netdev->local = netdev_priv(netdev->dev);
2597
2598         if (master) {
2599                 /* Device shall be a slave */
2600                 struct net_device *p = MASTER_TO_SLAVE(master);
2601                 struct net_device *q = master;
2602
2603                 netdev->local->master = master;
2604                 /* Put device at end of slave-chain */
2605                 while (p) {
2606                         q = p;
2607                         p = MASTER_TO_SLAVE(p);
2608                 }
2609                 MASTER_TO_SLAVE(q) = netdev->dev;
2610         } else {
2611                 /* Device shall be a master */
2612                 /*
2613                  * Watchdog timer (currently) for master only.
2614                  */
2615                 netdev->dev->watchdog_timeo = ISDN_NET_TX_TIMEOUT;
2616                 if (register_netdev(netdev->dev) != 0) {
2617                         printk(KERN_WARNING "isdn_net: Could not register net-device\n");
2618                         free_netdev(netdev->dev);
2619                         kfree(netdev);
2620                         return NULL;
2621                 }
2622         }
2623         netdev->queue = netdev->local;
2624         spin_lock_init(&netdev->queue_lock);
2625
2626         netdev->local->netdev = netdev;
2627
2628         INIT_WORK(&netdev->local->tqueue, isdn_net_softint);
2629         spin_lock_init(&netdev->local->xmit_lock);
2630
2631         /* Put into to netdev-chain */
2632         netdev->next = (void *) dev->netdev;
2633         dev->netdev = netdev;
2634         return netdev->dev->name;
2635 }
2636
2637 char *
2638 isdn_net_newslave(char *parm)
2639 {
2640         char *p = strchr(parm, ',');
2641         isdn_net_dev *n;
2642         char newname[10];
2643
2644         if (p) {
2645                 /* Slave-Name MUST not be empty */
2646                 if (!strlen(p + 1))
2647                         return NULL;
2648                 strcpy(newname, p + 1);
2649                 *p = 0;
2650                 /* Master must already exist */
2651                 if (!(n = isdn_net_findif(parm)))
2652                         return NULL;
2653                 /* Master must be a real interface, not a slave */
2654                 if (n->local->master)
2655                         return NULL;
2656                 /* Master must not be started yet */
2657                 if (isdn_net_device_started(n)) 
2658                         return NULL;
2659                 return (isdn_net_new(newname, n->dev));
2660         }
2661         return NULL;
2662 }
2663
2664 /*
2665  * Set interface-parameters.
2666  * Always set all parameters, so the user-level application is responsible
2667  * for not overwriting existing setups. It has to get the current
2668  * setup first, if only selected parameters are to be changed.
2669  */
2670 int
2671 isdn_net_setcfg(isdn_net_ioctl_cfg * cfg)
2672 {
2673         isdn_net_dev *p = isdn_net_findif(cfg->name);
2674         ulong features;
2675         int i;
2676         int drvidx;
2677         int chidx;
2678         char drvid[25];
2679
2680         if (p) {
2681                 isdn_net_local *lp = p->local;
2682
2683                 /* See if any registered driver supports the features we want */
2684                 features = ((1 << cfg->l2_proto) << ISDN_FEATURE_L2_SHIFT) |
2685                         ((1 << cfg->l3_proto) << ISDN_FEATURE_L3_SHIFT);
2686                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2687                         if (dev->drv[i])
2688                                 if ((dev->drv[i]->interface->features & features) == features)
2689                                         break;
2690                 if (i == ISDN_MAX_DRIVERS) {
2691                         printk(KERN_WARNING "isdn_net: No driver with selected features\n");
2692                         return -ENODEV;
2693                 }
2694                 if (lp->p_encap != cfg->p_encap){
2695 #ifdef CONFIG_ISDN_X25
2696                         struct concap_proto * cprot = p -> cprot;
2697 #endif
2698                         if (isdn_net_device_started(p)) {
2699                                 printk(KERN_WARNING "%s: cannot change encap when if is up\n",
2700                                        p->dev->name);
2701                                 return -EBUSY;
2702                         }
2703 #ifdef CONFIG_ISDN_X25
2704                         if( cprot && cprot -> pops )
2705                                 cprot -> pops -> proto_del ( cprot );
2706                         p -> cprot = NULL;
2707                         lp -> dops = NULL;
2708                         /* ... ,  prepare for configuration of new one ... */
2709                         switch ( cfg -> p_encap ){
2710                         case ISDN_NET_ENCAP_X25IFACE:
2711                                 lp -> dops = &isdn_concap_reliable_dl_dops;
2712                         }
2713                         /* ... and allocate new one ... */
2714                         p -> cprot = isdn_concap_new( cfg -> p_encap );
2715                         /* p -> cprot == NULL now if p_encap is not supported
2716                            by means of the concap_proto mechanism */
2717                         /* the protocol is not configured yet; this will
2718                            happen later when isdn_net_reset() is called */
2719 #endif
2720                 }
2721                 switch ( cfg->p_encap ) {
2722                 case ISDN_NET_ENCAP_SYNCPPP:
2723 #ifndef CONFIG_ISDN_PPP
2724                         printk(KERN_WARNING "%s: SyncPPP support not configured\n",
2725                                p->dev->name);
2726                         return -EINVAL;
2727 #else
2728                         p->dev->type = ARPHRD_PPP;      /* change ARP type */
2729                         p->dev->addr_len = 0;
2730 #endif
2731                         break;
2732                 case ISDN_NET_ENCAP_X25IFACE:
2733 #ifndef CONFIG_ISDN_X25
2734                         printk(KERN_WARNING "%s: isdn-x25 support not configured\n",
2735                                p->dev->name);
2736                         return -EINVAL;
2737 #else
2738                         p->dev->type = ARPHRD_X25;      /* change ARP type */
2739                         p->dev->addr_len = 0;
2740 #endif
2741                         break;
2742                 case ISDN_NET_ENCAP_CISCOHDLCK:
2743                         break;
2744                 default:
2745                         if( cfg->p_encap >= 0 &&
2746                             cfg->p_encap <= ISDN_NET_ENCAP_MAX_ENCAP )
2747                                 break;
2748                         printk(KERN_WARNING
2749                                "%s: encapsulation protocol %d not supported\n",
2750                                p->dev->name, cfg->p_encap);
2751                         return -EINVAL;
2752                 }
2753                 if (strlen(cfg->drvid)) {
2754                         /* A bind has been requested ... */
2755                         char *c,
2756                         *e;
2757
2758                         drvidx = -1;
2759                         chidx = -1;
2760                         strcpy(drvid, cfg->drvid);
2761                         if ((c = strchr(drvid, ','))) {
2762                                 /* The channel-number is appended to the driver-Id with a comma */
2763                                 chidx = (int) simple_strtoul(c + 1, &e, 10);
2764                                 if (e == c)
2765                                         chidx = -1;
2766                                 *c = '\0';
2767                         }
2768                         for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2769                                 /* Lookup driver-Id in array */
2770                                 if (!(strcmp(dev->drvid[i], drvid))) {
2771                                         drvidx = i;
2772                                         break;
2773                                 }
2774                         if ((drvidx == -1) || (chidx == -1))
2775                                 /* Either driver-Id or channel-number invalid */
2776                                 return -ENODEV;
2777                 } else {
2778                         /* Parameters are valid, so get them */
2779                         drvidx = lp->pre_device;
2780                         chidx = lp->pre_channel;
2781                 }
2782                 if (cfg->exclusive > 0) {
2783                         unsigned long flags;
2784
2785                         /* If binding is exclusive, try to grab the channel */
2786                         spin_lock_irqsave(&dev->lock, flags);
2787                         if ((i = isdn_get_free_channel(ISDN_USAGE_NET,
2788                                 lp->l2_proto, lp->l3_proto, drvidx,
2789                                 chidx, lp->msn)) < 0) {
2790                                 /* Grab failed, because desired channel is in use */
2791                                 lp->exclusive = -1;
2792                                 spin_unlock_irqrestore(&dev->lock, flags);
2793                                 return -EBUSY;
2794                         }
2795                         /* All went ok, so update isdninfo */
2796                         dev->usage[i] = ISDN_USAGE_EXCLUSIVE;
2797                         isdn_info_update();
2798                         spin_unlock_irqrestore(&dev->lock, flags);
2799                         lp->exclusive = i;
2800                 } else {
2801                         /* Non-exclusive binding or unbind. */
2802                         lp->exclusive = -1;
2803                         if ((lp->pre_device != -1) && (cfg->exclusive == -1)) {
2804                                 isdn_unexclusive_channel(lp->pre_device, lp->pre_channel);
2805                                 isdn_free_channel(lp->pre_device, lp->pre_channel, ISDN_USAGE_NET);
2806                                 drvidx = -1;
2807                                 chidx = -1;
2808                         }
2809                 }
2810                 strlcpy(lp->msn, cfg->eaz, sizeof(lp->msn));
2811                 lp->pre_device = drvidx;
2812                 lp->pre_channel = chidx;
2813                 lp->onhtime = cfg->onhtime;
2814                 lp->charge = cfg->charge;
2815                 lp->l2_proto = cfg->l2_proto;
2816                 lp->l3_proto = cfg->l3_proto;
2817                 lp->cbdelay = cfg->cbdelay;
2818                 lp->dialmax = cfg->dialmax;
2819                 lp->triggercps = cfg->triggercps;
2820                 lp->slavedelay = cfg->slavedelay * HZ;
2821                 lp->pppbind = cfg->pppbind;
2822                 lp->dialtimeout = cfg->dialtimeout >= 0 ? cfg->dialtimeout * HZ : -1;
2823                 lp->dialwait = cfg->dialwait * HZ;
2824                 if (cfg->secure)
2825                         lp->flags |= ISDN_NET_SECURE;
2826                 else
2827                         lp->flags &= ~ISDN_NET_SECURE;
2828                 if (cfg->cbhup)
2829                         lp->flags |= ISDN_NET_CBHUP;
2830                 else
2831                         lp->flags &= ~ISDN_NET_CBHUP;
2832                 switch (cfg->callback) {
2833                         case 0:
2834                                 lp->flags &= ~(ISDN_NET_CALLBACK | ISDN_NET_CBOUT);
2835                                 break;
2836                         case 1:
2837                                 lp->flags |= ISDN_NET_CALLBACK;
2838                                 lp->flags &= ~ISDN_NET_CBOUT;
2839                                 break;
2840                         case 2:
2841                                 lp->flags |= ISDN_NET_CBOUT;
2842                                 lp->flags &= ~ISDN_NET_CALLBACK;
2843                                 break;
2844                 }
2845                 lp->flags &= ~ISDN_NET_DIALMODE_MASK;   /* first all bits off */
2846                 if (cfg->dialmode && !(cfg->dialmode & ISDN_NET_DIALMODE_MASK)) {
2847                         /* old isdnctrl version, where only 0 or 1 is given */
2848                         printk(KERN_WARNING
2849                              "Old isdnctrl version detected! Please update.\n");
2850                         lp->flags |= ISDN_NET_DM_OFF; /* turn on `off' bit */
2851                 }
2852                 else {
2853                         lp->flags |= cfg->dialmode;  /* turn on selected bits */
2854                 }
2855                 if (cfg->chargehup)
2856                         lp->hupflags |= ISDN_CHARGEHUP;
2857                 else
2858                         lp->hupflags &= ~ISDN_CHARGEHUP;
2859                 if (cfg->ihup)
2860                         lp->hupflags |= ISDN_INHUP;
2861                 else
2862                         lp->hupflags &= ~ISDN_INHUP;
2863                 if (cfg->chargeint > 10) {
2864                         lp->hupflags |= ISDN_CHARGEHUP | ISDN_HAVECHARGE | ISDN_MANCHARGE;
2865                         lp->chargeint = cfg->chargeint * HZ;
2866                 }
2867                 if (cfg->p_encap != lp->p_encap) {
2868                         if (cfg->p_encap == ISDN_NET_ENCAP_RAWIP) {
2869                                 p->dev->header_ops = NULL;
2870                                 p->dev->flags = IFF_NOARP|IFF_POINTOPOINT;
2871                         } else {
2872                                 p->dev->header_ops = &isdn_header_ops;
2873                                 if (cfg->p_encap == ISDN_NET_ENCAP_ETHER)
2874                                         p->dev->flags = IFF_BROADCAST | IFF_MULTICAST;
2875                                 else
2876                                         p->dev->flags = IFF_NOARP|IFF_POINTOPOINT;
2877                         }
2878                 }
2879                 lp->p_encap = cfg->p_encap;
2880                 return 0;
2881         }
2882         return -ENODEV;
2883 }
2884
2885 /*
2886  * Perform get-interface-parameters.ioctl
2887  */
2888 int
2889 isdn_net_getcfg(isdn_net_ioctl_cfg * cfg)
2890 {
2891         isdn_net_dev *p = isdn_net_findif(cfg->name);
2892
2893         if (p) {
2894                 isdn_net_local *lp = p->local;
2895
2896                 strcpy(cfg->eaz, lp->msn);
2897                 cfg->exclusive = lp->exclusive;
2898                 if (lp->pre_device >= 0) {
2899                         sprintf(cfg->drvid, "%s,%d", dev->drvid[lp->pre_device],
2900                                 lp->pre_channel);
2901                 } else
2902                         cfg->drvid[0] = '\0';
2903                 cfg->onhtime = lp->onhtime;
2904                 cfg->charge = lp->charge;
2905                 cfg->l2_proto = lp->l2_proto;
2906                 cfg->l3_proto = lp->l3_proto;
2907                 cfg->p_encap = lp->p_encap;
2908                 cfg->secure = (lp->flags & ISDN_NET_SECURE) ? 1 : 0;
2909                 cfg->callback = 0;
2910                 if (lp->flags & ISDN_NET_CALLBACK)
2911                         cfg->callback = 1;
2912                 if (lp->flags & ISDN_NET_CBOUT)
2913                         cfg->callback = 2;
2914                 cfg->cbhup = (lp->flags & ISDN_NET_CBHUP) ? 1 : 0;
2915                 cfg->dialmode = lp->flags & ISDN_NET_DIALMODE_MASK;
2916                 cfg->chargehup = (lp->hupflags & 4) ? 1 : 0;
2917                 cfg->ihup = (lp->hupflags & 8) ? 1 : 0;
2918                 cfg->cbdelay = lp->cbdelay;
2919                 cfg->dialmax = lp->dialmax;
2920                 cfg->triggercps = lp->triggercps;
2921                 cfg->slavedelay = lp->slavedelay / HZ;
2922                 cfg->chargeint = (lp->hupflags & ISDN_CHARGEHUP) ?
2923                     (lp->chargeint / HZ) : 0;
2924                 cfg->pppbind = lp->pppbind;
2925                 cfg->dialtimeout = lp->dialtimeout >= 0 ? lp->dialtimeout / HZ : -1;
2926                 cfg->dialwait = lp->dialwait / HZ;
2927                 if (lp->slave) {
2928                         if (strlen(lp->slave->name) > 8)
2929                                 strcpy(cfg->slave, "too-long");
2930                         else
2931                                 strcpy(cfg->slave, lp->slave->name);
2932                 } else
2933                         cfg->slave[0] = '\0';
2934                 if (lp->master) {
2935                         if (strlen(lp->master->name) > 8)
2936                                 strcpy(cfg->master, "too-long");
2937                         strcpy(cfg->master, lp->master->name);
2938                 } else
2939                         cfg->master[0] = '\0';
2940                 return 0;
2941         }
2942         return -ENODEV;
2943 }
2944
2945 /*
2946  * Add a phone-number to an interface.
2947  */
2948 int
2949 isdn_net_addphone(isdn_net_ioctl_phone * phone)
2950 {
2951         isdn_net_dev *p = isdn_net_findif(phone->name);
2952         isdn_net_phone *n;
2953
2954         if (p) {
2955                 if (!(n = kmalloc(sizeof(isdn_net_phone), GFP_KERNEL)))
2956                         return -ENOMEM;
2957                 strlcpy(n->num, phone->phone, sizeof(n->num));
2958                 n->next = p->local->phone[phone->outgoing & 1];
2959                 p->local->phone[phone->outgoing & 1] = n;
2960                 return 0;
2961         }
2962         return -ENODEV;
2963 }
2964
2965 /*
2966  * Copy a string of all phone-numbers of an interface to user space.
2967  * This might sleep and must be called with the isdn semaphore down.
2968  */
2969 int
2970 isdn_net_getphones(isdn_net_ioctl_phone * phone, char __user *phones)
2971 {
2972         isdn_net_dev *p = isdn_net_findif(phone->name);
2973         int inout = phone->outgoing & 1;
2974         int more = 0;
2975         int count = 0;
2976         isdn_net_phone *n;
2977
2978         if (!p)
2979                 return -ENODEV;
2980         inout &= 1;
2981         for (n = p->local->phone[inout]; n; n = n->next) {
2982                 if (more) {
2983                         put_user(' ', phones++);
2984                         count++;
2985                 }
2986                 if (copy_to_user(phones, n->num, strlen(n->num) + 1)) {
2987                         return -EFAULT;
2988                 }
2989                 phones += strlen(n->num);
2990                 count += strlen(n->num);
2991                 more = 1;
2992         }
2993         put_user(0, phones);
2994         count++;
2995         return count;
2996 }
2997
2998 /*
2999  * Copy a string containing the peer's phone number of a connected interface
3000  * to user space.
3001  */
3002 int
3003 isdn_net_getpeer(isdn_net_ioctl_phone *phone, isdn_net_ioctl_phone __user *peer)
3004 {
3005         isdn_net_dev *p = isdn_net_findif(phone->name);
3006         int ch, dv, idx;
3007
3008         if (!p)
3009                 return -ENODEV;
3010         /*
3011          * Theoretical race: while this executes, the remote number might
3012          * become invalid (hang up) or change (new connection), resulting
3013          * in (partially) wrong number copied to user. This race
3014          * currently ignored.
3015          */
3016         ch = p->local->isdn_channel;
3017         dv = p->local->isdn_device;
3018         if(ch < 0 && dv < 0)
3019                 return -ENOTCONN;
3020         idx = isdn_dc2minor(dv, ch);
3021         if (idx <0 )
3022                 return -ENODEV;
3023         /* for pre-bound channels, we need this extra check */
3024         if (strncmp(dev->num[idx], "???", 3) == 0)
3025                 return -ENOTCONN;
3026         strncpy(phone->phone, dev->num[idx], ISDN_MSNLEN);
3027         phone->outgoing = USG_OUTGOING(dev->usage[idx]);
3028         if (copy_to_user(peer, phone, sizeof(*peer)))
3029                 return -EFAULT;
3030         return 0;
3031 }
3032 /*
3033  * Delete a phone-number from an interface.
3034  */
3035 int
3036 isdn_net_delphone(isdn_net_ioctl_phone * phone)
3037 {
3038         isdn_net_dev *p = isdn_net_findif(phone->name);
3039         int inout = phone->outgoing & 1;
3040         isdn_net_phone *n;
3041         isdn_net_phone *m;
3042
3043         if (p) {
3044                 n = p->local->phone[inout];
3045                 m = NULL;
3046                 while (n) {
3047                         if (!strcmp(n->num, phone->phone)) {
3048                                 if (p->local->dial == n)
3049                                         p->local->dial = n->next;
3050                                 if (m)
3051                                         m->next = n->next;
3052                                 else
3053                                         p->local->phone[inout] = n->next;
3054                                 kfree(n);
3055                                 return 0;
3056                         }
3057                         m = n;
3058                         n = (isdn_net_phone *) n->next;
3059                 }
3060                 return -EINVAL;
3061         }
3062         return -ENODEV;
3063 }
3064
3065 /*
3066  * Delete all phone-numbers of an interface.
3067  */
3068 static int
3069 isdn_net_rmallphone(isdn_net_dev * p)
3070 {
3071         isdn_net_phone *n;
3072         isdn_net_phone *m;
3073         int i;
3074
3075         for (i = 0; i < 2; i++) {
3076                 n = p->local->phone[i];
3077                 while (n) {
3078                         m = n->next;
3079                         kfree(n);
3080                         n = m;
3081                 }
3082                 p->local->phone[i] = NULL;
3083         }
3084         p->local->dial = NULL;
3085         return 0;
3086 }
3087
3088 /*
3089  * Force a hangup of a network-interface.
3090  */
3091 int
3092 isdn_net_force_hangup(char *name)
3093 {
3094         isdn_net_dev *p = isdn_net_findif(name);
3095         struct net_device *q;
3096
3097         if (p) {
3098                 if (p->local->isdn_device < 0)
3099                         return 1;
3100                 q = p->local->slave;
3101                 /* If this interface has slaves, do a hangup for them also. */
3102                 while (q) {
3103                         isdn_net_hangup(q);
3104                         q = MASTER_TO_SLAVE(q);
3105                 }
3106                 isdn_net_hangup(p->dev);
3107                 return 0;
3108         }
3109         return -ENODEV;
3110 }
3111
3112 /*
3113  * Helper-function for isdn_net_rm: Do the real work.
3114  */
3115 static int
3116 isdn_net_realrm(isdn_net_dev * p, isdn_net_dev * q)
3117 {
3118         u_long flags;
3119
3120         if (isdn_net_device_started(p)) {
3121                 return -EBUSY;
3122         }
3123 #ifdef CONFIG_ISDN_X25
3124         if( p -> cprot && p -> cprot -> pops )
3125                 p -> cprot -> pops -> proto_del ( p -> cprot );
3126 #endif
3127         /* Free all phone-entries */
3128         isdn_net_rmallphone(p);
3129         /* If interface is bound exclusive, free channel-usage */
3130         if (p->local->exclusive != -1)
3131                 isdn_unexclusive_channel(p->local->pre_device, p->local->pre_channel);
3132         if (p->local->master) {
3133                 /* It's a slave-device, so update master's slave-pointer if necessary */
3134                 if (((isdn_net_local *) ISDN_MASTER_PRIV(p->local))->slave ==
3135                     p->dev)
3136                         ((isdn_net_local *)ISDN_MASTER_PRIV(p->local))->slave =
3137                                 p->local->slave;
3138         } else {
3139                 /* Unregister only if it's a master-device */
3140                 unregister_netdev(p->dev);
3141         }
3142         /* Unlink device from chain */
3143         spin_lock_irqsave(&dev->lock, flags);
3144         if (q)
3145                 q->next = p->next;
3146         else
3147                 dev->netdev = p->next;
3148         if (p->local->slave) {
3149                 /* If this interface has a slave, remove it also */
3150                 char *slavename = p->local->slave->name;
3151                 isdn_net_dev *n = dev->netdev;
3152                 q = NULL;
3153                 while (n) {
3154                         if (!strcmp(n->dev->name, slavename)) {
3155                                 spin_unlock_irqrestore(&dev->lock, flags);
3156                                 isdn_net_realrm(n, q);
3157                                 spin_lock_irqsave(&dev->lock, flags);
3158                                 break;
3159                         }
3160                         q = n;
3161                         n = (isdn_net_dev *)n->next;
3162                 }
3163         }
3164         spin_unlock_irqrestore(&dev->lock, flags);
3165         /* If no more net-devices remain, disable auto-hangup timer */
3166         if (dev->netdev == NULL)
3167                 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 0);
3168         free_netdev(p->dev);
3169         kfree(p);
3170
3171         return 0;
3172 }
3173
3174 /*
3175  * Remove a single network-interface.
3176  */
3177 int
3178 isdn_net_rm(char *name)
3179 {
3180         u_long flags;
3181         isdn_net_dev *p;
3182         isdn_net_dev *q;
3183
3184         /* Search name in netdev-chain */
3185         spin_lock_irqsave(&dev->lock, flags);
3186         p = dev->netdev;
3187         q = NULL;
3188         while (p) {
3189                 if (!strcmp(p->dev->name, name)) {
3190                         spin_unlock_irqrestore(&dev->lock, flags);
3191                         return (isdn_net_realrm(p, q));
3192                 }
3193                 q = p;
3194                 p = (isdn_net_dev *) p->next;
3195         }
3196         spin_unlock_irqrestore(&dev->lock, flags);
3197         /* If no more net-devices remain, disable auto-hangup timer */
3198         if (dev->netdev == NULL)
3199                 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 0);
3200         return -ENODEV;
3201 }
3202
3203 /*
3204  * Remove all network-interfaces
3205  */
3206 int
3207 isdn_net_rmall(void)
3208 {
3209         u_long flags;
3210         int ret;
3211
3212         /* Walk through netdev-chain */
3213         spin_lock_irqsave(&dev->lock, flags);
3214         while (dev->netdev) {
3215                 if (!dev->netdev->local->master) {
3216                         /* Remove master-devices only, slaves get removed with their master */
3217                         spin_unlock_irqrestore(&dev->lock, flags);
3218                         if ((ret = isdn_net_realrm(dev->netdev, NULL))) {
3219                                 return ret;
3220                         }
3221                         spin_lock_irqsave(&dev->lock, flags);
3222                 }
3223         }
3224         dev->netdev = NULL;
3225         spin_unlock_irqrestore(&dev->lock, flags);
3226         return 0;
3227 }