Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[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_net_local *) lp->master->priv)->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 *) dev->priv ) -> dops;
217         struct concap_proto * cprot =
218                 ( (isdn_net_local *) dev->priv ) -> 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
254         if ((p = (((isdn_net_local *) dev->priv)->slave))) {
255                 while (p) {
256                         isdn_net_reset(p);
257                         p = (((isdn_net_local *) p->priv)->slave);
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_net_local *)lp->master->priv)->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 *) d->priv;
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_net_local *)lp->slave->priv;
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 *) ndev->priv;
1052         int retv = 0;
1053
1054         if (((isdn_net_local *) (ndev->priv))->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 *) ndev->priv)->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 = lp->slave->priv;
1094                                         if (!(slp->flags & ISDN_NET_CONNECTED)) {
1095                                                 isdn_net_force_dial_lp((isdn_net_local *) lp->slave->priv);
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 *) dev->priv;
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 *) ndev->priv;
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 *) ndev->priv;
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 *) dev->priv ) -> 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         if ((p = (((isdn_net_local *) dev->priv)->slave))) {
1322                 /* If this interface has slaves, stop them also */
1323                 while (p) {
1324 #ifdef CONFIG_ISDN_X25
1325                         cprot = ( (isdn_net_local *) p->priv )
1326                                 -> netdev -> cprot;
1327                         if( cprot && cprot -> pops )
1328                                 cprot -> pops -> close( cprot );
1329 #endif
1330                         isdn_net_hangup(p);
1331                         p = (((isdn_net_local *) p->priv)->slave);
1332                 }
1333         }
1334         isdn_net_hangup(dev);
1335         isdn_unlock_drivers();
1336         return 0;
1337 }
1338
1339 /*
1340  * Get statistics
1341  */
1342 static struct net_device_stats *
1343 isdn_net_get_stats(struct net_device *dev)
1344 {
1345         isdn_net_local *lp = (isdn_net_local *) dev->priv;
1346         return &lp->stats;
1347 }
1348
1349 /*      This is simply a copy from std. eth.c EXCEPT we pull ETH_HLEN
1350  *      instead of dev->hard_header_len off. This is done because the
1351  *      lowlevel-driver has already pulled off its stuff when we get
1352  *      here and this routine only gets called with p_encap == ETHER.
1353  *      Determine the packet's protocol ID. The rule here is that we
1354  *      assume 802.3 if the type field is short enough to be a length.
1355  *      This is normal practice and works for any 'now in use' protocol.
1356  */
1357
1358 static __be16
1359 isdn_net_type_trans(struct sk_buff *skb, struct net_device *dev)
1360 {
1361         struct ethhdr *eth;
1362         unsigned char *rawp;
1363
1364         skb_reset_mac_header(skb);
1365         skb_pull(skb, ETH_HLEN);
1366         eth = eth_hdr(skb);
1367
1368         if (*eth->h_dest & 1) {
1369                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
1370                         skb->pkt_type = PACKET_BROADCAST;
1371                 else
1372                         skb->pkt_type = PACKET_MULTICAST;
1373         }
1374         /*
1375          *      This ALLMULTI check should be redundant by 1.4
1376          *      so don't forget to remove it.
1377          */
1378
1379         else if (dev->flags & (IFF_PROMISC /*| IFF_ALLMULTI*/)) {
1380                 if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
1381                         skb->pkt_type = PACKET_OTHERHOST;
1382         }
1383         if (ntohs(eth->h_proto) >= 1536)
1384                 return eth->h_proto;
1385
1386         rawp = skb->data;
1387
1388         /*
1389          *      This is a magic hack to spot IPX packets. Older Novell breaks
1390          *      the protocol design and runs IPX over 802.3 without an 802.2 LLC
1391          *      layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
1392          *      won't work for fault tolerant netware but does for the rest.
1393          */
1394         if (*(unsigned short *) rawp == 0xFFFF)
1395                 return htons(ETH_P_802_3);
1396         /*
1397          *      Real 802.2 LLC
1398          */
1399         return htons(ETH_P_802_2);
1400 }
1401
1402
1403 /* 
1404  * CISCO HDLC keepalive specific stuff
1405  */
1406 static struct sk_buff*
1407 isdn_net_ciscohdlck_alloc_skb(isdn_net_local *lp, int len)
1408 {
1409         unsigned short hl = dev->drv[lp->isdn_device]->interface->hl_hdrlen;
1410         struct sk_buff *skb;
1411
1412         skb = alloc_skb(hl + len, GFP_ATOMIC);
1413         if (skb)
1414                 skb_reserve(skb, hl);
1415         else 
1416                 printk("isdn out of mem at %s:%d!\n", __FILE__, __LINE__);
1417         return skb;
1418 }
1419
1420 /* cisco hdlck device private ioctls */
1421 static int
1422 isdn_ciscohdlck_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1423 {
1424         isdn_net_local *lp = (isdn_net_local *) dev->priv;
1425         unsigned long len = 0;
1426         unsigned long expires = 0;
1427         int tmp = 0;
1428         int period = lp->cisco_keepalive_period;
1429         s8 debserint = lp->cisco_debserint;
1430         int rc = 0;
1431
1432         if (lp->p_encap != ISDN_NET_ENCAP_CISCOHDLCK)
1433                 return -EINVAL;
1434
1435         switch (cmd) {
1436                 /* get/set keepalive period */
1437                 case SIOCGKEEPPERIOD:
1438                         len = (unsigned long)sizeof(lp->cisco_keepalive_period);
1439                         if (copy_to_user(ifr->ifr_data,
1440                                 &lp->cisco_keepalive_period, len))
1441                                 rc = -EFAULT;
1442                         break;
1443                 case SIOCSKEEPPERIOD:
1444                         tmp = lp->cisco_keepalive_period;
1445                         len = (unsigned long)sizeof(lp->cisco_keepalive_period);
1446                         if (copy_from_user(&period, ifr->ifr_data, len))
1447                                 rc = -EFAULT;
1448                         if ((period > 0) && (period <= 32767))
1449                                 lp->cisco_keepalive_period = period;
1450                         else
1451                                 rc = -EINVAL;
1452                         if (!rc && (tmp != lp->cisco_keepalive_period)) {
1453                                 expires = (unsigned long)(jiffies +
1454                                         lp->cisco_keepalive_period * HZ);
1455                                 mod_timer(&lp->cisco_timer, expires);
1456                                 printk(KERN_INFO "%s: Keepalive period set "
1457                                         "to %d seconds.\n",
1458                                         dev->name, lp->cisco_keepalive_period);
1459                         }
1460                         break;
1461
1462                 /* get/set debugging */
1463                 case SIOCGDEBSERINT:
1464                         len = (unsigned long)sizeof(lp->cisco_debserint);
1465                         if (copy_to_user(ifr->ifr_data,
1466                                 &lp->cisco_debserint, len))
1467                                 rc = -EFAULT;
1468                         break;
1469                 case SIOCSDEBSERINT:
1470                         len = (unsigned long)sizeof(lp->cisco_debserint);
1471                         if (copy_from_user(&debserint,
1472                                 ifr->ifr_data, len))
1473                                 rc = -EFAULT;
1474                         if ((debserint >= 0) && (debserint <= 64))
1475                                 lp->cisco_debserint = debserint;
1476                         else
1477                                 rc = -EINVAL;
1478                         break;
1479
1480                 default:
1481                         rc = -EINVAL;
1482                         break;
1483         }
1484         return (rc);
1485 }
1486
1487 /* called via cisco_timer.function */
1488 static void
1489 isdn_net_ciscohdlck_slarp_send_keepalive(unsigned long data)
1490 {
1491         isdn_net_local *lp = (isdn_net_local *) data;
1492         struct sk_buff *skb;
1493         unsigned char *p;
1494         unsigned long last_cisco_myseq = lp->cisco_myseq;
1495         int myseq_diff = 0;
1496
1497         if (!(lp->flags & ISDN_NET_CONNECTED) || lp->dialstate) {
1498                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1499                 return;
1500         }
1501         lp->cisco_myseq++;
1502
1503         myseq_diff = (lp->cisco_myseq - lp->cisco_mineseen);
1504         if ((lp->cisco_line_state) && ((myseq_diff >= 3)||(myseq_diff <= -3))) {
1505                 /* line up -> down */
1506                 lp->cisco_line_state = 0;
1507                 printk (KERN_WARNING
1508                                 "UPDOWN: Line protocol on Interface %s,"
1509                                 " changed state to down\n", lp->netdev->dev->name);
1510                 /* should stop routing higher-level data accross */
1511         } else if ((!lp->cisco_line_state) &&
1512                 (myseq_diff >= 0) && (myseq_diff <= 2)) {
1513                 /* line down -> up */
1514                 lp->cisco_line_state = 1;
1515                 printk (KERN_WARNING
1516                                 "UPDOWN: Line protocol on Interface %s,"
1517                                 " changed state to up\n", lp->netdev->dev->name);
1518                 /* restart routing higher-level data accross */
1519         }
1520
1521         if (lp->cisco_debserint)
1522                 printk (KERN_DEBUG "%s: HDLC "
1523                         "myseq %lu, mineseen %lu%c, yourseen %lu, %s\n",
1524                         lp->netdev->dev->name, last_cisco_myseq, lp->cisco_mineseen,
1525                         ((last_cisco_myseq == lp->cisco_mineseen) ? '*' : 040),
1526                         lp->cisco_yourseq,
1527                         ((lp->cisco_line_state) ? "line up" : "line down"));
1528
1529         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1530         if (!skb)
1531                 return;
1532
1533         p = skb_put(skb, 4 + 14);
1534
1535         /* cisco header */
1536         *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
1537         *(u8 *)(p + 1) = CISCO_CTRL;
1538         *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
1539
1540         /* slarp keepalive */
1541         *(__be32 *)(p +  4) = cpu_to_be32(CISCO_SLARP_KEEPALIVE);
1542         *(__be32 *)(p +  8) = cpu_to_be32(lp->cisco_myseq);
1543         *(__be32 *)(p + 12) = cpu_to_be32(lp->cisco_yourseq);
1544         *(__be16 *)(p + 16) = cpu_to_be16(0xffff); // reliablity, always 0xffff
1545         p += 18;
1546
1547         isdn_net_write_super(lp, skb);
1548
1549         lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
1550         
1551         add_timer(&lp->cisco_timer);
1552 }
1553
1554 static void
1555 isdn_net_ciscohdlck_slarp_send_request(isdn_net_local *lp)
1556 {
1557         struct sk_buff *skb;
1558         unsigned char *p;
1559
1560         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1561         if (!skb)
1562                 return;
1563
1564         p = skb_put(skb, 4 + 14);
1565
1566         /* cisco header */
1567         *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
1568         *(u8 *)(p + 1) = CISCO_CTRL;
1569         *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
1570
1571         /* slarp request */
1572         *(__be32 *)(p +  4) = cpu_to_be32(CISCO_SLARP_REQUEST);
1573         *(__be32 *)(p +  8) = cpu_to_be32(0); // address
1574         *(__be32 *)(p + 12) = cpu_to_be32(0); // netmask
1575         *(__be16 *)(p + 16) = cpu_to_be16(0); // unused
1576         p += 18;
1577
1578         isdn_net_write_super(lp, skb);
1579 }
1580
1581 static void 
1582 isdn_net_ciscohdlck_connected(isdn_net_local *lp)
1583 {
1584         lp->cisco_myseq = 0;
1585         lp->cisco_mineseen = 0;
1586         lp->cisco_yourseq = 0;
1587         lp->cisco_keepalive_period = ISDN_TIMER_KEEPINT;
1588         lp->cisco_last_slarp_in = 0;
1589         lp->cisco_line_state = 0;
1590         lp->cisco_debserint = 0;
1591
1592         /* send slarp request because interface/seq.no.s reset */
1593         isdn_net_ciscohdlck_slarp_send_request(lp);
1594
1595         init_timer(&lp->cisco_timer);
1596         lp->cisco_timer.data = (unsigned long) lp;
1597         lp->cisco_timer.function = isdn_net_ciscohdlck_slarp_send_keepalive;
1598         lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
1599         add_timer(&lp->cisco_timer);
1600 }
1601
1602 static void 
1603 isdn_net_ciscohdlck_disconnected(isdn_net_local *lp)
1604 {
1605         del_timer(&lp->cisco_timer);
1606 }
1607
1608 static void
1609 isdn_net_ciscohdlck_slarp_send_reply(isdn_net_local *lp)
1610 {
1611         struct sk_buff *skb;
1612         unsigned char *p;
1613         struct in_device *in_dev = NULL;
1614         __be32 addr = 0;                /* local ipv4 address */
1615         __be32 mask = 0;                /* local netmask */
1616
1617         if ((in_dev = lp->netdev->dev->ip_ptr) != NULL) {
1618                 /* take primary(first) address of interface */
1619                 struct in_ifaddr *ifa = in_dev->ifa_list;
1620                 if (ifa != NULL) {
1621                         addr = ifa->ifa_local;
1622                         mask = ifa->ifa_mask;
1623                 }
1624         }
1625
1626         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1627         if (!skb)
1628                 return;
1629
1630         p = skb_put(skb, 4 + 14);
1631
1632         /* cisco header */
1633         *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
1634         *(u8 *)(p + 1) = CISCO_CTRL;
1635         *(__be16 *)(p + 2) = cpu_to_be16(CISCO_TYPE_SLARP);
1636
1637         /* slarp reply, send own ip/netmask; if values are nonsense remote
1638          * should think we are unable to provide it with an address via SLARP */
1639         *(__be32 *)(p +  4) = cpu_to_be32(CISCO_SLARP_REPLY);
1640         *(__be32 *)(p +  8) = addr; // address
1641         *(__be32 *)(p + 12) = mask; // netmask
1642         *(__be16 *)(p + 16) = cpu_to_be16(0); // unused
1643         p += 18;
1644
1645         isdn_net_write_super(lp, skb);
1646 }
1647
1648 static void
1649 isdn_net_ciscohdlck_slarp_in(isdn_net_local *lp, struct sk_buff *skb)
1650 {
1651         unsigned char *p;
1652         int period;
1653         u32 code;
1654         u32 my_seq;
1655         u32 your_seq;
1656         __be32 local;
1657         __be32 *addr, *mask;
1658         u16 unused;
1659
1660         if (skb->len < 14)
1661                 return;
1662
1663         p = skb->data;
1664         code = be32_to_cpup((__be32 *)p);
1665         p += 4;
1666
1667         switch (code) {
1668         case CISCO_SLARP_REQUEST:
1669                 lp->cisco_yourseq = 0;
1670                 isdn_net_ciscohdlck_slarp_send_reply(lp);
1671                 break;
1672         case CISCO_SLARP_REPLY:
1673                 addr = (__be32 *)p;
1674                 mask = (__be32 *)(p + 4);
1675                 if (*mask != cpu_to_be32(0xfffffffc))
1676                         goto slarp_reply_out;
1677                 if ((*addr & cpu_to_be32(3)) == cpu_to_be32(0) ||
1678                     (*addr & cpu_to_be32(3)) == cpu_to_be32(3))
1679                         goto slarp_reply_out;
1680                 local = *addr ^ cpu_to_be32(3);
1681                 printk(KERN_INFO "%s: got slarp reply: remote ip: %pI4, local ip: %pI4 mask: %pI4\n",
1682                        lp->netdev->dev->name, addr, &local, mask);
1683                 break;
1684   slarp_reply_out:
1685                 printk(KERN_INFO "%s: got invalid slarp reply (%pI4/%pI4) - ignored\n",
1686                        lp->netdev->dev->name, addr, mask);
1687                 break;
1688         case CISCO_SLARP_KEEPALIVE:
1689                 period = (int)((jiffies - lp->cisco_last_slarp_in
1690                                 + HZ/2 - 1) / HZ);
1691                 if (lp->cisco_debserint &&
1692                                 (period != lp->cisco_keepalive_period) &&
1693                                 lp->cisco_last_slarp_in) {
1694                         printk(KERN_DEBUG "%s: Keepalive period mismatch - "
1695                                 "is %d but should be %d.\n",
1696                                 lp->netdev->dev->name, period,
1697                                 lp->cisco_keepalive_period);
1698                 }
1699                 lp->cisco_last_slarp_in = jiffies;
1700                 my_seq = be32_to_cpup((__be32 *)(p + 0));
1701                 your_seq = be32_to_cpup((__be32 *)(p + 4));
1702                 unused = be16_to_cpup((__be16 *)(p + 8));
1703                 p += 10;
1704                 lp->cisco_yourseq = my_seq;
1705                 lp->cisco_mineseen = your_seq;
1706                 break;
1707         }
1708 }
1709
1710 static void
1711 isdn_net_ciscohdlck_receive(isdn_net_local *lp, struct sk_buff *skb)
1712 {
1713         unsigned char *p;
1714         u8 addr;
1715         u8 ctrl;
1716         u16 type;
1717         
1718         if (skb->len < 4)
1719                 goto out_free;
1720
1721         p = skb->data;
1722         addr = *(u8 *)(p + 0);
1723         ctrl = *(u8 *)(p + 1);
1724         type = be16_to_cpup((__be16 *)(p + 2));
1725         p += 4;
1726         skb_pull(skb, 4);
1727         
1728         if (addr != CISCO_ADDR_UNICAST && addr != CISCO_ADDR_BROADCAST) {
1729                 printk(KERN_WARNING "%s: Unknown Cisco addr 0x%02x\n",
1730                        lp->netdev->dev->name, addr);
1731                 goto out_free;
1732         }
1733         if (ctrl != CISCO_CTRL) {
1734                 printk(KERN_WARNING "%s: Unknown Cisco ctrl 0x%02x\n",
1735                        lp->netdev->dev->name, ctrl);
1736                 goto out_free;
1737         }
1738
1739         switch (type) {
1740         case CISCO_TYPE_SLARP:
1741                 isdn_net_ciscohdlck_slarp_in(lp, skb);
1742                 goto out_free;
1743         case CISCO_TYPE_CDP:
1744                 if (lp->cisco_debserint)
1745                         printk(KERN_DEBUG "%s: Received CDP packet. use "
1746                                 "\"no cdp enable\" on cisco.\n",
1747                                 lp->netdev->dev->name);
1748                 goto out_free;
1749         default:
1750                 /* no special cisco protocol */
1751                 skb->protocol = htons(type);
1752                 netif_rx(skb);
1753                 return;
1754         }
1755
1756  out_free:
1757         kfree_skb(skb);
1758 }
1759
1760 /*
1761  * Got a packet from ISDN-Channel.
1762  */
1763 static void
1764 isdn_net_receive(struct net_device *ndev, struct sk_buff *skb)
1765 {
1766         isdn_net_local *lp = (isdn_net_local *) ndev->priv;
1767         isdn_net_local *olp = lp;       /* original 'lp' */
1768 #ifdef CONFIG_ISDN_X25
1769         struct concap_proto *cprot = lp -> netdev -> cprot;
1770 #endif
1771         lp->transcount += skb->len;
1772
1773         lp->stats.rx_packets++;
1774         lp->stats.rx_bytes += skb->len;
1775         if (lp->master) {
1776                 /* Bundling: If device is a slave-device, deliver to master, also
1777                  * handle master's statistics and hangup-timeout
1778                  */
1779                 ndev = lp->master;
1780                 lp = (isdn_net_local *) ndev->priv;
1781                 lp->stats.rx_packets++;
1782                 lp->stats.rx_bytes += skb->len;
1783         }
1784         skb->dev = ndev;
1785         skb->pkt_type = PACKET_HOST;
1786         skb_reset_mac_header(skb);
1787 #ifdef ISDN_DEBUG_NET_DUMP
1788         isdn_dumppkt("R:", skb->data, skb->len, 40);
1789 #endif
1790         switch (lp->p_encap) {
1791                 case ISDN_NET_ENCAP_ETHER:
1792                         /* Ethernet over ISDN */
1793                         olp->huptimer = 0;
1794                         lp->huptimer = 0;
1795                         skb->protocol = isdn_net_type_trans(skb, ndev);
1796                         break;
1797                 case ISDN_NET_ENCAP_UIHDLC:
1798                         /* HDLC with UI-frame (for ispa with -h1 option) */
1799                         olp->huptimer = 0;
1800                         lp->huptimer = 0;
1801                         skb_pull(skb, 2);
1802                         /* Fall through */
1803                 case ISDN_NET_ENCAP_RAWIP:
1804                         /* RAW-IP without MAC-Header */
1805                         olp->huptimer = 0;
1806                         lp->huptimer = 0;
1807                         skb->protocol = htons(ETH_P_IP);
1808                         break;
1809                 case ISDN_NET_ENCAP_CISCOHDLCK:
1810                         isdn_net_ciscohdlck_receive(lp, skb);
1811                         return;
1812                 case ISDN_NET_ENCAP_CISCOHDLC:
1813                         /* CISCO-HDLC IP with type field and  fake I-frame-header */
1814                         skb_pull(skb, 2);
1815                         /* Fall through */
1816                 case ISDN_NET_ENCAP_IPTYP:
1817                         /* IP with type field */
1818                         olp->huptimer = 0;
1819                         lp->huptimer = 0;
1820                         skb->protocol = *(__be16 *)&(skb->data[0]);
1821                         skb_pull(skb, 2);
1822                         if (*(unsigned short *) skb->data == 0xFFFF)
1823                                 skb->protocol = htons(ETH_P_802_3);
1824                         break;
1825 #ifdef CONFIG_ISDN_PPP
1826                 case ISDN_NET_ENCAP_SYNCPPP:
1827                         /* huptimer is done in isdn_ppp_push_higher */
1828                         isdn_ppp_receive(lp->netdev, olp, skb);
1829                         return;
1830 #endif
1831
1832                 default:
1833 #ifdef CONFIG_ISDN_X25
1834                   /* try if there are generic sync_device receiver routines */
1835                         if(cprot) if(cprot -> pops)
1836                                 if( cprot -> pops -> data_ind){
1837                                         cprot -> pops -> data_ind(cprot,skb);
1838                                         return;
1839                                 };
1840 #endif /* CONFIG_ISDN_X25 */
1841                         printk(KERN_WARNING "%s: unknown encapsulation, dropping\n",
1842                                lp->netdev->dev->name);
1843                         kfree_skb(skb);
1844                         return;
1845         }
1846
1847         netif_rx(skb);
1848         return;
1849 }
1850
1851 /*
1852  * A packet arrived via ISDN. Search interface-chain for a corresponding
1853  * interface. If found, deliver packet to receiver-function and return 1,
1854  * else return 0.
1855  */
1856 int
1857 isdn_net_rcv_skb(int idx, struct sk_buff *skb)
1858 {
1859         isdn_net_dev *p = dev->rx_netdev[idx];
1860
1861         if (p) {
1862                 isdn_net_local *lp = p->local;
1863                 if ((lp->flags & ISDN_NET_CONNECTED) &&
1864                     (!lp->dialstate)) {
1865                         isdn_net_receive(p->dev, skb);
1866                         return 1;
1867                 }
1868         }
1869         return 0;
1870 }
1871
1872 /*
1873  *  build an header
1874  *  depends on encaps that is being used.
1875  */
1876
1877 static int isdn_net_header(struct sk_buff *skb, struct net_device *dev,
1878                            unsigned short type,
1879                            const void *daddr, const void *saddr, unsigned plen)
1880 {
1881         isdn_net_local *lp = dev->priv;
1882         unsigned char *p;
1883         ushort len = 0;
1884
1885         switch (lp->p_encap) {
1886                 case ISDN_NET_ENCAP_ETHER:
1887                         len = eth_header(skb, dev, type, daddr, saddr, plen);
1888                         break;
1889 #ifdef CONFIG_ISDN_PPP
1890                 case ISDN_NET_ENCAP_SYNCPPP:
1891                         /* stick on a fake header to keep fragmentation code happy. */
1892                         len = IPPP_MAX_HEADER;
1893                         skb_push(skb,len);
1894                         break;
1895 #endif
1896                 case ISDN_NET_ENCAP_RAWIP:
1897                         printk(KERN_WARNING "isdn_net_header called with RAW_IP!\n");
1898                         len = 0;
1899                         break;
1900                 case ISDN_NET_ENCAP_IPTYP:
1901                         /* ethernet type field */
1902                         *((__be16 *)skb_push(skb, 2)) = htons(type);
1903                         len = 2;
1904                         break;
1905                 case ISDN_NET_ENCAP_UIHDLC:
1906                         /* HDLC with UI-Frames (for ispa with -h1 option) */
1907                         *((__be16 *)skb_push(skb, 2)) = htons(0x0103);
1908                         len = 2;
1909                         break;
1910                 case ISDN_NET_ENCAP_CISCOHDLC:
1911                 case ISDN_NET_ENCAP_CISCOHDLCK:
1912                         p = skb_push(skb, 4);
1913                         *(u8 *)(p + 0) = CISCO_ADDR_UNICAST;
1914                         *(u8 *)(p + 1) = CISCO_CTRL;
1915                         *(__be16 *)(p + 2) = cpu_to_be16(type);
1916                         p += 4;
1917                         len = 4;
1918                         break;
1919 #ifdef CONFIG_ISDN_X25
1920                 default:
1921                   /* try if there are generic concap protocol routines */
1922                         if( lp-> netdev -> cprot ){
1923                                 printk(KERN_WARNING "isdn_net_header called with concap_proto!\n");
1924                                 len = 0;
1925                                 break;
1926                         }
1927                         break;
1928 #endif /* CONFIG_ISDN_X25 */
1929         }
1930         return len;
1931 }
1932
1933 /* We don't need to send arp, because we have point-to-point connections. */
1934 static int
1935 isdn_net_rebuild_header(struct sk_buff *skb)
1936 {
1937         struct net_device *dev = skb->dev;
1938         isdn_net_local *lp = dev->priv;
1939         int ret = 0;
1940
1941         if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
1942                 struct ethhdr *eth = (struct ethhdr *) skb->data;
1943
1944                 /*
1945                  *      Only ARP/IP is currently supported
1946                  */
1947
1948                 if (eth->h_proto != htons(ETH_P_IP)) {
1949                         printk(KERN_WARNING
1950                                "isdn_net: %s don't know how to resolve type %d addresses?\n",
1951                                dev->name, (int) eth->h_proto);
1952                         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
1953                         return 0;
1954                 }
1955                 /*
1956                  *      Try to get ARP to resolve the header.
1957                  */
1958 #ifdef CONFIG_INET
1959                 ret = arp_find(eth->h_dest, skb);
1960 #endif
1961         }
1962         return ret;
1963 }
1964
1965 static int isdn_header_cache(const struct neighbour *neigh, struct hh_cache *hh)
1966 {
1967         const struct net_device *dev = neigh->dev;
1968         isdn_net_local *lp = dev->priv;
1969
1970         if (lp->p_encap == ISDN_NET_ENCAP_ETHER)
1971                 return eth_header_cache(neigh, hh);
1972         return -1;
1973 }
1974
1975 static void isdn_header_cache_update(struct hh_cache *hh,
1976                                      const struct net_device *dev,
1977                                      const unsigned char *haddr)
1978 {
1979         isdn_net_local *lp = dev->priv;
1980         if (lp->p_encap == ISDN_NET_ENCAP_ETHER)
1981                 eth_header_cache_update(hh, dev, haddr);
1982 }
1983
1984 static const struct header_ops isdn_header_ops = {
1985         .create = isdn_net_header,
1986         .rebuild = isdn_net_rebuild_header,
1987         .cache = isdn_header_cache,
1988         .cache_update = isdn_header_cache_update,
1989 };
1990
1991 /*
1992  * Interface-setup. (just after registering a new interface)
1993  */
1994 static int
1995 isdn_net_init(struct net_device *ndev)
1996 {
1997         ushort max_hlhdr_len = 0;
1998         int drvidx;
1999
2000         ether_setup(ndev);
2001         ndev->header_ops = NULL;
2002
2003         /* Setup the generic properties */
2004         ndev->mtu = 1500;
2005         ndev->flags = IFF_NOARP|IFF_POINTOPOINT;
2006         ndev->type = ARPHRD_ETHER;
2007         ndev->addr_len = ETH_ALEN;
2008         ndev->validate_addr = NULL;
2009
2010         /* for clients with MPPP maybe higher values better */
2011         ndev->tx_queue_len = 30;
2012
2013         /* The ISDN-specific entries in the device structure. */
2014         ndev->open = &isdn_net_open;
2015         ndev->hard_start_xmit = &isdn_net_start_xmit;
2016
2017         /*
2018          *  up till binding we ask the protocol layer to reserve as much
2019          *  as we might need for HL layer
2020          */
2021
2022         for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2023                 if (dev->drv[drvidx])
2024                         if (max_hlhdr_len < dev->drv[drvidx]->interface->hl_hdrlen)
2025                                 max_hlhdr_len = dev->drv[drvidx]->interface->hl_hdrlen;
2026
2027         ndev->hard_header_len = ETH_HLEN + max_hlhdr_len;
2028         ndev->stop = &isdn_net_close;
2029         ndev->get_stats = &isdn_net_get_stats;
2030         ndev->do_ioctl = NULL;
2031         return 0;
2032 }
2033
2034 static void
2035 isdn_net_swapbind(int drvidx)
2036 {
2037         isdn_net_dev *p;
2038
2039 #ifdef ISDN_DEBUG_NET_ICALL
2040         printk(KERN_DEBUG "n_fi: swapping ch of %d\n", drvidx);
2041 #endif
2042         p = dev->netdev;
2043         while (p) {
2044                 if (p->local->pre_device == drvidx)
2045                         switch (p->local->pre_channel) {
2046                                 case 0:
2047                                         p->local->pre_channel = 1;
2048                                         break;
2049                                 case 1:
2050                                         p->local->pre_channel = 0;
2051                                         break;
2052                         }
2053                 p = (isdn_net_dev *) p->next;
2054         }
2055 }
2056
2057 static void
2058 isdn_net_swap_usage(int i1, int i2)
2059 {
2060         int u1 = dev->usage[i1] & ISDN_USAGE_EXCLUSIVE;
2061         int u2 = dev->usage[i2] & ISDN_USAGE_EXCLUSIVE;
2062
2063 #ifdef ISDN_DEBUG_NET_ICALL
2064         printk(KERN_DEBUG "n_fi: usage of %d and %d\n", i1, i2);
2065 #endif
2066         dev->usage[i1] &= ~ISDN_USAGE_EXCLUSIVE;
2067         dev->usage[i1] |= u2;
2068         dev->usage[i2] &= ~ISDN_USAGE_EXCLUSIVE;
2069         dev->usage[i2] |= u1;
2070         isdn_info_update();
2071 }
2072
2073 /*
2074  * An incoming call-request has arrived.
2075  * Search the interface-chain for an appropriate interface.
2076  * If found, connect the interface to the ISDN-channel and initiate
2077  * D- and B-Channel-setup. If secure-flag is set, accept only
2078  * configured phone-numbers. If callback-flag is set, initiate
2079  * callback-dialing.
2080  *
2081  * Return-Value: 0 = No appropriate interface for this call.
2082  *               1 = Call accepted
2083  *               2 = Reject call, wait cbdelay, then call back
2084  *               3 = Reject call
2085  *               4 = Wait cbdelay, then call back
2086  *               5 = No appropriate interface for this call,
2087  *                   would eventually match if CID was longer.
2088  */
2089
2090 int
2091 isdn_net_find_icall(int di, int ch, int idx, setup_parm *setup)
2092 {
2093         char *eaz;
2094         int si1;
2095         int si2;
2096         int ematch;
2097         int wret;
2098         int swapped;
2099         int sidx = 0;
2100         u_long flags;
2101         isdn_net_dev *p;
2102         isdn_net_phone *n;
2103         char nr[ISDN_MSNLEN];
2104         char *my_eaz;
2105
2106         /* Search name in netdev-chain */
2107         if (!setup->phone[0]) {
2108                 nr[0] = '0';
2109                 nr[1] = '\0';
2110                 printk(KERN_INFO "isdn_net: Incoming call without OAD, assuming '0'\n");
2111         } else
2112                 strlcpy(nr, setup->phone, ISDN_MSNLEN);
2113         si1 = (int) setup->si1;
2114         si2 = (int) setup->si2;
2115         if (!setup->eazmsn[0]) {
2116                 printk(KERN_WARNING "isdn_net: Incoming call without CPN, assuming '0'\n");
2117                 eaz = "0";
2118         } else
2119                 eaz = setup->eazmsn;
2120         if (dev->net_verbose > 1)
2121                 printk(KERN_INFO "isdn_net: call from %s,%d,%d -> %s\n", nr, si1, si2, eaz);
2122         /* Accept DATA and VOICE calls at this stage
2123          * local eaz is checked later for allowed call types
2124          */
2125         if ((si1 != 7) && (si1 != 1)) {
2126                 if (dev->net_verbose > 1)
2127                         printk(KERN_INFO "isdn_net: Service-Indicator not 1 or 7, ignored\n");
2128                 return 0;
2129         }
2130         n = (isdn_net_phone *) 0;
2131         p = dev->netdev;
2132         ematch = wret = swapped = 0;
2133 #ifdef ISDN_DEBUG_NET_ICALL
2134         printk(KERN_DEBUG "n_fi: di=%d ch=%d idx=%d usg=%d\n", di, ch, idx,
2135                 dev->usage[idx]);
2136 #endif
2137         while (p) {
2138                 int matchret;
2139                 isdn_net_local *lp = p->local;
2140
2141                 /* If last check has triggered as binding-swap, revert it */
2142                 switch (swapped) {
2143                         case 2:
2144                                 isdn_net_swap_usage(idx, sidx);
2145                                 /* fall through */
2146                         case 1:
2147                                 isdn_net_swapbind(di);
2148                                 break;
2149                 }
2150                 swapped = 0;
2151                 /* check acceptable call types for DOV */
2152                 my_eaz = isdn_map_eaz2msn(lp->msn, di);
2153                 if (si1 == 1) { /* it's a DOV call, check if we allow it */
2154                         if (*my_eaz == 'v' || *my_eaz == 'V' ||
2155                             *my_eaz == 'b' || *my_eaz == 'B')
2156                                 my_eaz++; /* skip to allow a match */
2157                         else
2158                                 my_eaz = NULL; /* force non match */
2159                 } else { /* it's a DATA call, check if we allow it */
2160                         if (*my_eaz == 'b' || *my_eaz == 'B')
2161                                 my_eaz++; /* skip to allow a match */
2162                 }
2163                 if (my_eaz)
2164                         matchret = isdn_msncmp(eaz, my_eaz);
2165                 else
2166                         matchret = 1;
2167                 if (!matchret)
2168                         ematch = 1;
2169
2170                 /* Remember if more numbers eventually can match */
2171                 if (matchret > wret)
2172                         wret = matchret;
2173 #ifdef ISDN_DEBUG_NET_ICALL
2174                 printk(KERN_DEBUG "n_fi: if='%s', l.msn=%s, l.flags=%d, l.dstate=%d\n",
2175                        p->dev->name, lp->msn, lp->flags, lp->dialstate);
2176 #endif
2177                 if ((!matchret) &&                                        /* EAZ is matching   */
2178                     (((!(lp->flags & ISDN_NET_CONNECTED)) &&              /* but not connected */
2179                       (USG_NONE(dev->usage[idx]))) ||                     /* and ch. unused or */
2180                      ((((lp->dialstate == 4) || (lp->dialstate == 12)) && /* if dialing        */
2181                        (!(lp->flags & ISDN_NET_CALLBACK)))                /* but no callback   */
2182                      )))
2183                          {
2184 #ifdef ISDN_DEBUG_NET_ICALL
2185                         printk(KERN_DEBUG "n_fi: match1, pdev=%d pch=%d\n",
2186                                lp->pre_device, lp->pre_channel);
2187 #endif
2188                         if (dev->usage[idx] & ISDN_USAGE_EXCLUSIVE) {
2189                                 if ((lp->pre_channel != ch) ||
2190                                     (lp->pre_device != di)) {
2191                                         /* Here we got a problem:
2192                                          * If using an ICN-Card, an incoming call is always signaled on
2193                                          * on the first channel of the card, if both channels are
2194                                          * down. However this channel may be bound exclusive. If the
2195                                          * second channel is free, this call should be accepted.
2196                                          * The solution is horribly but it runs, so what:
2197                                          * We exchange the exclusive bindings of the two channels, the
2198                                          * corresponding variables in the interface-structs.
2199                                          */
2200                                         if (ch == 0) {
2201                                                 sidx = isdn_dc2minor(di, 1);
2202 #ifdef ISDN_DEBUG_NET_ICALL
2203                                                 printk(KERN_DEBUG "n_fi: ch is 0\n");
2204 #endif
2205                                                 if (USG_NONE(dev->usage[sidx])) {
2206                                                         /* Second Channel is free, now see if it is bound
2207                                                          * exclusive too. */
2208                                                         if (dev->usage[sidx] & ISDN_USAGE_EXCLUSIVE) {
2209 #ifdef ISDN_DEBUG_NET_ICALL
2210                                                                 printk(KERN_DEBUG "n_fi: 2nd channel is down and bound\n");
2211 #endif
2212                                                                 /* Yes, swap bindings only, if the original
2213                                                                  * binding is bound to channel 1 of this driver */
2214                                                                 if ((lp->pre_device == di) &&
2215                                                                     (lp->pre_channel == 1)) {
2216                                                                         isdn_net_swapbind(di);
2217                                                                         swapped = 1;
2218                                                                 } else {
2219                                                                         /* ... else iterate next device */
2220                                                                         p = (isdn_net_dev *) p->next;
2221                                                                         continue;
2222                                                                 }
2223                                                         } else {
2224 #ifdef ISDN_DEBUG_NET_ICALL
2225                                                                 printk(KERN_DEBUG "n_fi: 2nd channel is down and unbound\n");
2226 #endif
2227                                                                 /* No, swap always and swap excl-usage also */
2228                                                                 isdn_net_swap_usage(idx, sidx);
2229                                                                 isdn_net_swapbind(di);
2230                                                                 swapped = 2;
2231                                                         }
2232                                                         /* Now check for exclusive binding again */
2233 #ifdef ISDN_DEBUG_NET_ICALL
2234                                                         printk(KERN_DEBUG "n_fi: final check\n");
2235 #endif
2236                                                         if ((dev->usage[idx] & ISDN_USAGE_EXCLUSIVE) &&
2237                                                             ((lp->pre_channel != ch) ||
2238                                                              (lp->pre_device != di))) {
2239 #ifdef ISDN_DEBUG_NET_ICALL
2240                                                                 printk(KERN_DEBUG "n_fi: final check failed\n");
2241 #endif
2242                                                                 p = (isdn_net_dev *) p->next;
2243                                                                 continue;
2244                                                         }
2245                                                 }
2246                                         } else {
2247                                                 /* We are already on the second channel, so nothing to do */
2248 #ifdef ISDN_DEBUG_NET_ICALL
2249                                                 printk(KERN_DEBUG "n_fi: already on 2nd channel\n");
2250 #endif
2251                                         }
2252                                 }
2253                         }
2254 #ifdef ISDN_DEBUG_NET_ICALL
2255                         printk(KERN_DEBUG "n_fi: match2\n");
2256 #endif
2257                         n = lp->phone[0];
2258                         if (lp->flags & ISDN_NET_SECURE) {
2259                                 while (n) {
2260                                         if (!isdn_msncmp(nr, n->num))
2261                                                 break;
2262                                         n = (isdn_net_phone *) n->next;
2263                                 }
2264                         }
2265                         if (n || (!(lp->flags & ISDN_NET_SECURE))) {
2266 #ifdef ISDN_DEBUG_NET_ICALL
2267                                 printk(KERN_DEBUG "n_fi: match3\n");
2268 #endif
2269                                 /* matching interface found */
2270
2271                                 /*
2272                                  * Is the state STOPPED?
2273                                  * If so, no dialin is allowed,
2274                                  * so reject actively.
2275                                  * */
2276                                 if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
2277                                         printk(KERN_INFO "incoming call, interface %s `stopped' -> rejected\n",
2278                                                p->dev->name);
2279                                         return 3;
2280                                 }
2281                                 /*
2282                                  * Is the interface up?
2283                                  * If not, reject the call actively.
2284                                  */
2285                                 if (!isdn_net_device_started(p)) {
2286                                         printk(KERN_INFO "%s: incoming call, interface down -> rejected\n",
2287                                                p->dev->name);
2288                                         return 3;
2289                                 }
2290                                 /* Interface is up, now see if it's a slave. If so, see if
2291                                  * it's master and parent slave is online. If not, reject the call.
2292                                  */
2293                                 if (lp->master) {
2294                                         isdn_net_local *mlp = (isdn_net_local *) lp->master->priv;
2295                                         printk(KERN_DEBUG "ICALLslv: %s\n", p->dev->name);
2296                                         printk(KERN_DEBUG "master=%s\n", lp->master->name);
2297                                         if (mlp->flags & ISDN_NET_CONNECTED) {
2298                                                 printk(KERN_DEBUG "master online\n");
2299                                                 /* Master is online, find parent-slave (master if first slave) */
2300                                                 while (mlp->slave) {
2301                                                         if ((isdn_net_local *) mlp->slave->priv == lp)
2302                                                                 break;
2303                                                         mlp = (isdn_net_local *) mlp->slave->priv;
2304                                                 }
2305                                         } else
2306                                                 printk(KERN_DEBUG "master offline\n");
2307                                         /* Found parent, if it's offline iterate next device */
2308                                         printk(KERN_DEBUG "mlpf: %d\n", mlp->flags & ISDN_NET_CONNECTED);
2309                                         if (!(mlp->flags & ISDN_NET_CONNECTED)) {
2310                                                 p = (isdn_net_dev *) p->next;
2311                                                 continue;
2312                                         }
2313                                 } 
2314                                 if (lp->flags & ISDN_NET_CALLBACK) {
2315                                         int chi;
2316                                         /*
2317                                          * Is the state MANUAL?
2318                                          * If so, no callback can be made,
2319                                          * so reject actively.
2320                                          * */
2321                                         if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
2322                                                 printk(KERN_INFO "incoming call for callback, interface %s `off' -> rejected\n",
2323                                                        p->dev->name);
2324                                                 return 3;
2325                                         }
2326                                         printk(KERN_DEBUG "%s: call from %s -> %s, start callback\n",
2327                                                p->dev->name, nr, eaz);
2328                                         if (lp->phone[1]) {
2329                                                 /* Grab a free ISDN-Channel */
2330                                                 spin_lock_irqsave(&dev->lock, flags);
2331                                                 if ((chi = 
2332                                                         isdn_get_free_channel(
2333                                                                 ISDN_USAGE_NET,
2334                                                                 lp->l2_proto,
2335                                                                 lp->l3_proto,
2336                                                                 lp->pre_device,
2337                                                                 lp->pre_channel,
2338                                                                 lp->msn)
2339                                                                 ) < 0) {
2340
2341                                                         printk(KERN_WARNING "isdn_net_find_icall: No channel for %s\n",
2342                                                                 p->dev->name);
2343                                                         spin_unlock_irqrestore(&dev->lock, flags);
2344                                                         return 0;
2345                                                 }
2346                                                 /* Setup dialstate. */
2347                                                 lp->dtimer = 0;
2348                                                 lp->dialstate = 11;
2349                                                 /* Connect interface with channel */
2350                                                 isdn_net_bind_channel(lp, chi);
2351 #ifdef CONFIG_ISDN_PPP
2352                                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2353                                                         if (isdn_ppp_bind(lp) < 0) {
2354                                                                 spin_unlock_irqrestore(&dev->lock, flags);
2355                                                                 isdn_net_unbind_channel(lp);
2356                                                                 return 0;
2357                                                         }
2358 #endif
2359                                                 spin_unlock_irqrestore(&dev->lock, flags);
2360                                                 /* Initiate dialing by returning 2 or 4 */
2361                                                 return (lp->flags & ISDN_NET_CBHUP) ? 2 : 4;
2362                                         } else
2363                                                 printk(KERN_WARNING "isdn_net: %s: No phone number\n",
2364                                                         p->dev->name);
2365                                         return 0;
2366                                 } else {
2367                                         printk(KERN_DEBUG "%s: call from %s -> %s accepted\n",
2368                                                 p->dev->name, nr, eaz);
2369                                         /* if this interface is dialing, it does it probably on a different
2370                                            device, so free this device */
2371                                         if ((lp->dialstate == 4) || (lp->dialstate == 12)) {
2372 #ifdef CONFIG_ISDN_PPP
2373                                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2374                                                         isdn_ppp_free(lp);
2375 #endif
2376                                                 isdn_net_lp_disconnected(lp);
2377                                                 isdn_free_channel(lp->isdn_device, lp->isdn_channel,
2378                                                          ISDN_USAGE_NET);
2379                                         }
2380                                         spin_lock_irqsave(&dev->lock, flags);
2381                                         dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
2382                                         dev->usage[idx] |= ISDN_USAGE_NET;
2383                                         strcpy(dev->num[idx], nr);
2384                                         isdn_info_update();
2385                                         dev->st_netdev[idx] = lp->netdev;
2386                                         lp->isdn_device = di;
2387                                         lp->isdn_channel = ch;
2388                                         lp->ppp_slot = -1;
2389                                         lp->flags |= ISDN_NET_CONNECTED;
2390                                         lp->dialstate = 7;
2391                                         lp->dtimer = 0;
2392                                         lp->outgoing = 0;
2393                                         lp->huptimer = 0;
2394                                         lp->hupflags |= ISDN_WAITCHARGE;
2395                                         lp->hupflags &= ~ISDN_HAVECHARGE;
2396 #ifdef CONFIG_ISDN_PPP
2397                                         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
2398                                                 if (isdn_ppp_bind(lp) < 0) {
2399                                                         isdn_net_unbind_channel(lp);
2400                                                         spin_unlock_irqrestore(&dev->lock, flags);
2401                                                         return 0;
2402                                                 }
2403                                         }
2404 #endif
2405                                         spin_unlock_irqrestore(&dev->lock, flags);
2406                                         return 1;
2407                                 }
2408                         }
2409                 }
2410                 p = (isdn_net_dev *) p->next;
2411         }
2412         /* If none of configured EAZ/MSN matched and not verbose, be silent */
2413         if (!ematch || dev->net_verbose)
2414                 printk(KERN_INFO "isdn_net: call from %s -> %d %s ignored\n", nr, di, eaz);
2415         return (wret == 2)?5:0;
2416 }
2417
2418 /*
2419  * Search list of net-interfaces for an interface with given name.
2420  */
2421 isdn_net_dev *
2422 isdn_net_findif(char *name)
2423 {
2424         isdn_net_dev *p = dev->netdev;
2425
2426         while (p) {
2427                 if (!strcmp(p->dev->name, name))
2428                         return p;
2429                 p = (isdn_net_dev *) p->next;
2430         }
2431         return (isdn_net_dev *) NULL;
2432 }
2433
2434 /*
2435  * Force a net-interface to dial out.
2436  * This is called from the userlevel-routine below or
2437  * from isdn_net_start_xmit().
2438  */
2439 static int
2440 isdn_net_force_dial_lp(isdn_net_local * lp)
2441 {
2442         if ((!(lp->flags & ISDN_NET_CONNECTED)) && !lp->dialstate) {
2443                 int chi;
2444                 if (lp->phone[1]) {
2445                         ulong flags;
2446
2447                         /* Grab a free ISDN-Channel */
2448                         spin_lock_irqsave(&dev->lock, flags);
2449                         if ((chi = isdn_get_free_channel(
2450                                         ISDN_USAGE_NET,
2451                                         lp->l2_proto,
2452                                         lp->l3_proto,
2453                                         lp->pre_device,
2454                                         lp->pre_channel,
2455                                         lp->msn)) < 0) {
2456                                 printk(KERN_WARNING "isdn_net_force_dial: No channel for %s\n",
2457                                         lp->netdev->dev->name);
2458                                 spin_unlock_irqrestore(&dev->lock, flags);
2459                                 return -EAGAIN;
2460                         }
2461                         lp->dialstate = 1;
2462                         /* Connect interface with channel */
2463                         isdn_net_bind_channel(lp, chi);
2464 #ifdef CONFIG_ISDN_PPP
2465                         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2466                                 if (isdn_ppp_bind(lp) < 0) {
2467                                         isdn_net_unbind_channel(lp);
2468                                         spin_unlock_irqrestore(&dev->lock, flags);
2469                                         return -EAGAIN;
2470                                 }
2471 #endif
2472                         /* Initiate dialing */
2473                         spin_unlock_irqrestore(&dev->lock, flags);
2474                         isdn_net_dial();
2475                         return 0;
2476                 } else
2477                         return -EINVAL;
2478         } else
2479                 return -EBUSY;
2480 }
2481
2482 /*
2483  * This is called from certain upper protocol layers (multilink ppp
2484  * and x25iface encapsulation module) that want to initiate dialing
2485  * themselves.
2486  */
2487 int
2488 isdn_net_dial_req(isdn_net_local * lp)
2489 {
2490         /* is there a better error code? */
2491         if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) return -EBUSY;
2492
2493         return isdn_net_force_dial_lp(lp);
2494 }
2495
2496 /*
2497  * Force a net-interface to dial out.
2498  * This is always called from within userspace (ISDN_IOCTL_NET_DIAL).
2499  */
2500 int
2501 isdn_net_force_dial(char *name)
2502 {
2503         isdn_net_dev *p = isdn_net_findif(name);
2504
2505         if (!p)
2506                 return -ENODEV;
2507         return (isdn_net_force_dial_lp(p->local));
2508 }
2509
2510 /*
2511  * Helper for alloc_netdev()
2512  */
2513 static void _isdn_setup(struct net_device *dev)
2514 {
2515         isdn_net_local *lp = dev->priv;
2516
2517         dev->flags = IFF_NOARP | IFF_POINTOPOINT;
2518         lp->p_encap = ISDN_NET_ENCAP_RAWIP;
2519         lp->magic = ISDN_NET_MAGIC;
2520         lp->last = lp;
2521         lp->next = lp;
2522         lp->isdn_device = -1;
2523         lp->isdn_channel = -1;
2524         lp->pre_device = -1;
2525         lp->pre_channel = -1;
2526         lp->exclusive = -1;
2527         lp->ppp_slot = -1;
2528         lp->pppbind = -1;
2529         skb_queue_head_init(&lp->super_tx_queue);
2530         lp->l2_proto = ISDN_PROTO_L2_X75I;
2531         lp->l3_proto = ISDN_PROTO_L3_TRANS;
2532         lp->triggercps = 6000;
2533         lp->slavedelay = 10 * HZ;
2534         lp->hupflags = ISDN_INHUP;      /* Do hangup even on incoming calls */
2535         lp->onhtime = 10;       /* Default hangup-time for saving costs */
2536         lp->dialmax = 1;
2537         /* Hangup before Callback, manual dial */
2538         lp->flags = ISDN_NET_CBHUP | ISDN_NET_DM_MANUAL;
2539         lp->cbdelay = 25;       /* Wait 5 secs before Callback */
2540         lp->dialtimeout = -1;  /* Infinite Dial-Timeout */
2541         lp->dialwait = 5 * HZ; /* Wait 5 sec. after failed dial */
2542         lp->dialstarted = 0;   /* Jiffies of last dial-start */
2543         lp->dialwait_timer = 0;  /* Jiffies of earliest next dial-start */
2544 }
2545
2546 /*
2547  * Allocate a new network-interface and initialize its data structures.
2548  */
2549 char *
2550 isdn_net_new(char *name, struct net_device *master)
2551 {
2552         isdn_net_dev *netdev;
2553
2554         /* Avoid creating an existing interface */
2555         if (isdn_net_findif(name)) {
2556                 printk(KERN_WARNING "isdn_net: interface %s already exists\n", name);
2557                 return NULL;
2558         }
2559         if (name == NULL)
2560                 return NULL;
2561         if (!(netdev = kzalloc(sizeof(isdn_net_dev), GFP_KERNEL))) {
2562                 printk(KERN_WARNING "isdn_net: Could not allocate net-device\n");
2563                 return NULL;
2564         }
2565         netdev->dev = alloc_netdev(sizeof(isdn_net_local), name, _isdn_setup);
2566         if (!netdev->dev) {
2567                 printk(KERN_WARNING "isdn_net: Could not allocate network device\n");
2568                 kfree(netdev);
2569                 return NULL;
2570         }
2571         netdev->local = netdev->dev->priv;
2572         netdev->dev->init = isdn_net_init;
2573         if (master) {
2574                 /* Device shall be a slave */
2575                 struct net_device *p = (((isdn_net_local *) master->priv)->slave);
2576                 struct net_device *q = master;
2577
2578                 netdev->local->master = master;
2579                 /* Put device at end of slave-chain */
2580                 while (p) {
2581                         q = p;
2582                         p = (((isdn_net_local *) p->priv)->slave);
2583                 }
2584                 ((isdn_net_local *) q->priv)->slave = netdev->dev;
2585         } else {
2586                 /* Device shall be a master */
2587                 /*
2588                  * Watchdog timer (currently) for master only.
2589                  */
2590                 netdev->dev->tx_timeout = isdn_net_tx_timeout;
2591                 netdev->dev->watchdog_timeo = ISDN_NET_TX_TIMEOUT;
2592                 if (register_netdev(netdev->dev) != 0) {
2593                         printk(KERN_WARNING "isdn_net: Could not register net-device\n");
2594                         free_netdev(netdev->dev);
2595                         kfree(netdev);
2596                         return NULL;
2597                 }
2598         }
2599         netdev->queue = netdev->local;
2600         spin_lock_init(&netdev->queue_lock);
2601
2602         netdev->local->netdev = netdev;
2603
2604         INIT_WORK(&netdev->local->tqueue, isdn_net_softint);
2605         spin_lock_init(&netdev->local->xmit_lock);
2606
2607         /* Put into to netdev-chain */
2608         netdev->next = (void *) dev->netdev;
2609         dev->netdev = netdev;
2610         return netdev->dev->name;
2611 }
2612
2613 char *
2614 isdn_net_newslave(char *parm)
2615 {
2616         char *p = strchr(parm, ',');
2617         isdn_net_dev *n;
2618         char newname[10];
2619
2620         if (p) {
2621                 /* Slave-Name MUST not be empty */
2622                 if (!strlen(p + 1))
2623                         return NULL;
2624                 strcpy(newname, p + 1);
2625                 *p = 0;
2626                 /* Master must already exist */
2627                 if (!(n = isdn_net_findif(parm)))
2628                         return NULL;
2629                 /* Master must be a real interface, not a slave */
2630                 if (n->local->master)
2631                         return NULL;
2632                 /* Master must not be started yet */
2633                 if (isdn_net_device_started(n)) 
2634                         return NULL;
2635                 return (isdn_net_new(newname, n->dev));
2636         }
2637         return NULL;
2638 }
2639
2640 /*
2641  * Set interface-parameters.
2642  * Always set all parameters, so the user-level application is responsible
2643  * for not overwriting existing setups. It has to get the current
2644  * setup first, if only selected parameters are to be changed.
2645  */
2646 int
2647 isdn_net_setcfg(isdn_net_ioctl_cfg * cfg)
2648 {
2649         isdn_net_dev *p = isdn_net_findif(cfg->name);
2650         ulong features;
2651         int i;
2652         int drvidx;
2653         int chidx;
2654         char drvid[25];
2655
2656         if (p) {
2657                 isdn_net_local *lp = p->local;
2658
2659                 /* See if any registered driver supports the features we want */
2660                 features = ((1 << cfg->l2_proto) << ISDN_FEATURE_L2_SHIFT) |
2661                         ((1 << cfg->l3_proto) << ISDN_FEATURE_L3_SHIFT);
2662                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2663                         if (dev->drv[i])
2664                                 if ((dev->drv[i]->interface->features & features) == features)
2665                                         break;
2666                 if (i == ISDN_MAX_DRIVERS) {
2667                         printk(KERN_WARNING "isdn_net: No driver with selected features\n");
2668                         return -ENODEV;
2669                 }
2670                 if (lp->p_encap != cfg->p_encap){
2671 #ifdef CONFIG_ISDN_X25
2672                         struct concap_proto * cprot = p -> cprot;
2673 #endif
2674                         if (isdn_net_device_started(p)) {
2675                                 printk(KERN_WARNING "%s: cannot change encap when if is up\n",
2676                                        p->dev->name);
2677                                 return -EBUSY;
2678                         }
2679 #ifdef CONFIG_ISDN_X25
2680                         if( cprot && cprot -> pops )
2681                                 cprot -> pops -> proto_del ( cprot );
2682                         p -> cprot = NULL;
2683                         lp -> dops = NULL;
2684                         /* ... ,  prepare for configuration of new one ... */
2685                         switch ( cfg -> p_encap ){
2686                         case ISDN_NET_ENCAP_X25IFACE:
2687                                 lp -> dops = &isdn_concap_reliable_dl_dops;
2688                         }
2689                         /* ... and allocate new one ... */
2690                         p -> cprot = isdn_concap_new( cfg -> p_encap );
2691                         /* p -> cprot == NULL now if p_encap is not supported
2692                            by means of the concap_proto mechanism */
2693                         /* the protocol is not configured yet; this will
2694                            happen later when isdn_net_reset() is called */
2695 #endif
2696                 }
2697                 switch ( cfg->p_encap ) {
2698                 case ISDN_NET_ENCAP_SYNCPPP:
2699 #ifndef CONFIG_ISDN_PPP
2700                         printk(KERN_WARNING "%s: SyncPPP support not configured\n",
2701                                p->dev->name);
2702                         return -EINVAL;
2703 #else
2704                         p->dev->type = ARPHRD_PPP;      /* change ARP type */
2705                         p->dev->addr_len = 0;
2706                         p->dev->do_ioctl = isdn_ppp_dev_ioctl;
2707 #endif
2708                         break;
2709                 case ISDN_NET_ENCAP_X25IFACE:
2710 #ifndef CONFIG_ISDN_X25
2711                         printk(KERN_WARNING "%s: isdn-x25 support not configured\n",
2712                                p->dev->name);
2713                         return -EINVAL;
2714 #else
2715                         p->dev->type = ARPHRD_X25;      /* change ARP type */
2716                         p->dev->addr_len = 0;
2717 #endif
2718                         break;
2719                 case ISDN_NET_ENCAP_CISCOHDLCK:
2720                         p->dev->do_ioctl = isdn_ciscohdlck_dev_ioctl;
2721                         break;
2722                 default:
2723                         if( cfg->p_encap >= 0 &&
2724                             cfg->p_encap <= ISDN_NET_ENCAP_MAX_ENCAP )
2725                                 break;
2726                         printk(KERN_WARNING
2727                                "%s: encapsulation protocol %d not supported\n",
2728                                p->dev->name, cfg->p_encap);
2729                         return -EINVAL;
2730                 }
2731                 if (strlen(cfg->drvid)) {
2732                         /* A bind has been requested ... */
2733                         char *c,
2734                         *e;
2735
2736                         drvidx = -1;
2737                         chidx = -1;
2738                         strcpy(drvid, cfg->drvid);
2739                         if ((c = strchr(drvid, ','))) {
2740                                 /* The channel-number is appended to the driver-Id with a comma */
2741                                 chidx = (int) simple_strtoul(c + 1, &e, 10);
2742                                 if (e == c)
2743                                         chidx = -1;
2744                                 *c = '\0';
2745                         }
2746                         for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2747                                 /* Lookup driver-Id in array */
2748                                 if (!(strcmp(dev->drvid[i], drvid))) {
2749                                         drvidx = i;
2750                                         break;
2751                                 }
2752                         if ((drvidx == -1) || (chidx == -1))
2753                                 /* Either driver-Id or channel-number invalid */
2754                                 return -ENODEV;
2755                 } else {
2756                         /* Parameters are valid, so get them */
2757                         drvidx = lp->pre_device;
2758                         chidx = lp->pre_channel;
2759                 }
2760                 if (cfg->exclusive > 0) {
2761                         unsigned long flags;
2762
2763                         /* If binding is exclusive, try to grab the channel */
2764                         spin_lock_irqsave(&dev->lock, flags);
2765                         if ((i = isdn_get_free_channel(ISDN_USAGE_NET,
2766                                 lp->l2_proto, lp->l3_proto, drvidx,
2767                                 chidx, lp->msn)) < 0) {
2768                                 /* Grab failed, because desired channel is in use */
2769                                 lp->exclusive = -1;
2770                                 spin_unlock_irqrestore(&dev->lock, flags);
2771                                 return -EBUSY;
2772                         }
2773                         /* All went ok, so update isdninfo */
2774                         dev->usage[i] = ISDN_USAGE_EXCLUSIVE;
2775                         isdn_info_update();
2776                         spin_unlock_irqrestore(&dev->lock, flags);
2777                         lp->exclusive = i;
2778                 } else {
2779                         /* Non-exclusive binding or unbind. */
2780                         lp->exclusive = -1;
2781                         if ((lp->pre_device != -1) && (cfg->exclusive == -1)) {
2782                                 isdn_unexclusive_channel(lp->pre_device, lp->pre_channel);
2783                                 isdn_free_channel(lp->pre_device, lp->pre_channel, ISDN_USAGE_NET);
2784                                 drvidx = -1;
2785                                 chidx = -1;
2786                         }
2787                 }
2788                 strlcpy(lp->msn, cfg->eaz, sizeof(lp->msn));
2789                 lp->pre_device = drvidx;
2790                 lp->pre_channel = chidx;
2791                 lp->onhtime = cfg->onhtime;
2792                 lp->charge = cfg->charge;
2793                 lp->l2_proto = cfg->l2_proto;
2794                 lp->l3_proto = cfg->l3_proto;
2795                 lp->cbdelay = cfg->cbdelay;
2796                 lp->dialmax = cfg->dialmax;
2797                 lp->triggercps = cfg->triggercps;
2798                 lp->slavedelay = cfg->slavedelay * HZ;
2799                 lp->pppbind = cfg->pppbind;
2800                 lp->dialtimeout = cfg->dialtimeout >= 0 ? cfg->dialtimeout * HZ : -1;
2801                 lp->dialwait = cfg->dialwait * HZ;
2802                 if (cfg->secure)
2803                         lp->flags |= ISDN_NET_SECURE;
2804                 else
2805                         lp->flags &= ~ISDN_NET_SECURE;
2806                 if (cfg->cbhup)
2807                         lp->flags |= ISDN_NET_CBHUP;
2808                 else
2809                         lp->flags &= ~ISDN_NET_CBHUP;
2810                 switch (cfg->callback) {
2811                         case 0:
2812                                 lp->flags &= ~(ISDN_NET_CALLBACK | ISDN_NET_CBOUT);
2813                                 break;
2814                         case 1:
2815                                 lp->flags |= ISDN_NET_CALLBACK;
2816                                 lp->flags &= ~ISDN_NET_CBOUT;
2817                                 break;
2818                         case 2:
2819                                 lp->flags |= ISDN_NET_CBOUT;
2820                                 lp->flags &= ~ISDN_NET_CALLBACK;
2821                                 break;
2822                 }
2823                 lp->flags &= ~ISDN_NET_DIALMODE_MASK;   /* first all bits off */
2824                 if (cfg->dialmode && !(cfg->dialmode & ISDN_NET_DIALMODE_MASK)) {
2825                         /* old isdnctrl version, where only 0 or 1 is given */
2826                         printk(KERN_WARNING
2827                              "Old isdnctrl version detected! Please update.\n");
2828                         lp->flags |= ISDN_NET_DM_OFF; /* turn on `off' bit */
2829                 }
2830                 else {
2831                         lp->flags |= cfg->dialmode;  /* turn on selected bits */
2832                 }
2833                 if (cfg->chargehup)
2834                         lp->hupflags |= ISDN_CHARGEHUP;
2835                 else
2836                         lp->hupflags &= ~ISDN_CHARGEHUP;
2837                 if (cfg->ihup)
2838                         lp->hupflags |= ISDN_INHUP;
2839                 else
2840                         lp->hupflags &= ~ISDN_INHUP;
2841                 if (cfg->chargeint > 10) {
2842                         lp->hupflags |= ISDN_CHARGEHUP | ISDN_HAVECHARGE | ISDN_MANCHARGE;
2843                         lp->chargeint = cfg->chargeint * HZ;
2844                 }
2845                 if (cfg->p_encap != lp->p_encap) {
2846                         if (cfg->p_encap == ISDN_NET_ENCAP_RAWIP) {
2847                                 p->dev->header_ops = NULL;
2848                                 p->dev->flags = IFF_NOARP|IFF_POINTOPOINT;
2849                         } else {
2850                                 p->dev->header_ops = &isdn_header_ops;
2851                                 if (cfg->p_encap == ISDN_NET_ENCAP_ETHER)
2852                                         p->dev->flags = IFF_BROADCAST | IFF_MULTICAST;
2853                                 else
2854                                         p->dev->flags = IFF_NOARP|IFF_POINTOPOINT;
2855                         }
2856                 }
2857                 lp->p_encap = cfg->p_encap;
2858                 return 0;
2859         }
2860         return -ENODEV;
2861 }
2862
2863 /*
2864  * Perform get-interface-parameters.ioctl
2865  */
2866 int
2867 isdn_net_getcfg(isdn_net_ioctl_cfg * cfg)
2868 {
2869         isdn_net_dev *p = isdn_net_findif(cfg->name);
2870
2871         if (p) {
2872                 isdn_net_local *lp = p->local;
2873
2874                 strcpy(cfg->eaz, lp->msn);
2875                 cfg->exclusive = lp->exclusive;
2876                 if (lp->pre_device >= 0) {
2877                         sprintf(cfg->drvid, "%s,%d", dev->drvid[lp->pre_device],
2878                                 lp->pre_channel);
2879                 } else
2880                         cfg->drvid[0] = '\0';
2881                 cfg->onhtime = lp->onhtime;
2882                 cfg->charge = lp->charge;
2883                 cfg->l2_proto = lp->l2_proto;
2884                 cfg->l3_proto = lp->l3_proto;
2885                 cfg->p_encap = lp->p_encap;
2886                 cfg->secure = (lp->flags & ISDN_NET_SECURE) ? 1 : 0;
2887                 cfg->callback = 0;
2888                 if (lp->flags & ISDN_NET_CALLBACK)
2889                         cfg->callback = 1;
2890                 if (lp->flags & ISDN_NET_CBOUT)
2891                         cfg->callback = 2;
2892                 cfg->cbhup = (lp->flags & ISDN_NET_CBHUP) ? 1 : 0;
2893                 cfg->dialmode = lp->flags & ISDN_NET_DIALMODE_MASK;
2894                 cfg->chargehup = (lp->hupflags & 4) ? 1 : 0;
2895                 cfg->ihup = (lp->hupflags & 8) ? 1 : 0;
2896                 cfg->cbdelay = lp->cbdelay;
2897                 cfg->dialmax = lp->dialmax;
2898                 cfg->triggercps = lp->triggercps;
2899                 cfg->slavedelay = lp->slavedelay / HZ;
2900                 cfg->chargeint = (lp->hupflags & ISDN_CHARGEHUP) ?
2901                     (lp->chargeint / HZ) : 0;
2902                 cfg->pppbind = lp->pppbind;
2903                 cfg->dialtimeout = lp->dialtimeout >= 0 ? lp->dialtimeout / HZ : -1;
2904                 cfg->dialwait = lp->dialwait / HZ;
2905                 if (lp->slave) {
2906                         if (strlen(lp->slave->name) > 8)
2907                                 strcpy(cfg->slave, "too-long");
2908                         else
2909                                 strcpy(cfg->slave, lp->slave->name);
2910                 } else
2911                         cfg->slave[0] = '\0';
2912                 if (lp->master) {
2913                         if (strlen(lp->master->name) > 8)
2914                                 strcpy(cfg->master, "too-long");
2915                         strcpy(cfg->master, lp->master->name);
2916                 } else
2917                         cfg->master[0] = '\0';
2918                 return 0;
2919         }
2920         return -ENODEV;
2921 }
2922
2923 /*
2924  * Add a phone-number to an interface.
2925  */
2926 int
2927 isdn_net_addphone(isdn_net_ioctl_phone * phone)
2928 {
2929         isdn_net_dev *p = isdn_net_findif(phone->name);
2930         isdn_net_phone *n;
2931
2932         if (p) {
2933                 if (!(n = kmalloc(sizeof(isdn_net_phone), GFP_KERNEL)))
2934                         return -ENOMEM;
2935                 strlcpy(n->num, phone->phone, sizeof(n->num));
2936                 n->next = p->local->phone[phone->outgoing & 1];
2937                 p->local->phone[phone->outgoing & 1] = n;
2938                 return 0;
2939         }
2940         return -ENODEV;
2941 }
2942
2943 /*
2944  * Copy a string of all phone-numbers of an interface to user space.
2945  * This might sleep and must be called with the isdn semaphore down.
2946  */
2947 int
2948 isdn_net_getphones(isdn_net_ioctl_phone * phone, char __user *phones)
2949 {
2950         isdn_net_dev *p = isdn_net_findif(phone->name);
2951         int inout = phone->outgoing & 1;
2952         int more = 0;
2953         int count = 0;
2954         isdn_net_phone *n;
2955
2956         if (!p)
2957                 return -ENODEV;
2958         inout &= 1;
2959         for (n = p->local->phone[inout]; n; n = n->next) {
2960                 if (more) {
2961                         put_user(' ', phones++);
2962                         count++;
2963                 }
2964                 if (copy_to_user(phones, n->num, strlen(n->num) + 1)) {
2965                         return -EFAULT;
2966                 }
2967                 phones += strlen(n->num);
2968                 count += strlen(n->num);
2969                 more = 1;
2970         }
2971         put_user(0, phones);
2972         count++;
2973         return count;
2974 }
2975
2976 /*
2977  * Copy a string containing the peer's phone number of a connected interface
2978  * to user space.
2979  */
2980 int
2981 isdn_net_getpeer(isdn_net_ioctl_phone *phone, isdn_net_ioctl_phone __user *peer)
2982 {
2983         isdn_net_dev *p = isdn_net_findif(phone->name);
2984         int ch, dv, idx;
2985
2986         if (!p)
2987                 return -ENODEV;
2988         /*
2989          * Theoretical race: while this executes, the remote number might
2990          * become invalid (hang up) or change (new connection), resulting
2991          * in (partially) wrong number copied to user. This race
2992          * currently ignored.
2993          */
2994         ch = p->local->isdn_channel;
2995         dv = p->local->isdn_device;
2996         if(ch < 0 && dv < 0)
2997                 return -ENOTCONN;
2998         idx = isdn_dc2minor(dv, ch);
2999         if (idx <0 )
3000                 return -ENODEV;
3001         /* for pre-bound channels, we need this extra check */
3002         if (strncmp(dev->num[idx], "???", 3) == 0)
3003                 return -ENOTCONN;
3004         strncpy(phone->phone, dev->num[idx], ISDN_MSNLEN);
3005         phone->outgoing = USG_OUTGOING(dev->usage[idx]);
3006         if (copy_to_user(peer, phone, sizeof(*peer)))
3007                 return -EFAULT;
3008         return 0;
3009 }
3010 /*
3011  * Delete a phone-number from an interface.
3012  */
3013 int
3014 isdn_net_delphone(isdn_net_ioctl_phone * phone)
3015 {
3016         isdn_net_dev *p = isdn_net_findif(phone->name);
3017         int inout = phone->outgoing & 1;
3018         isdn_net_phone *n;
3019         isdn_net_phone *m;
3020
3021         if (p) {
3022                 n = p->local->phone[inout];
3023                 m = NULL;
3024                 while (n) {
3025                         if (!strcmp(n->num, phone->phone)) {
3026                                 if (p->local->dial == n)
3027                                         p->local->dial = n->next;
3028                                 if (m)
3029                                         m->next = n->next;
3030                                 else
3031                                         p->local->phone[inout] = n->next;
3032                                 kfree(n);
3033                                 return 0;
3034                         }
3035                         m = n;
3036                         n = (isdn_net_phone *) n->next;
3037                 }
3038                 return -EINVAL;
3039         }
3040         return -ENODEV;
3041 }
3042
3043 /*
3044  * Delete all phone-numbers of an interface.
3045  */
3046 static int
3047 isdn_net_rmallphone(isdn_net_dev * p)
3048 {
3049         isdn_net_phone *n;
3050         isdn_net_phone *m;
3051         int i;
3052
3053         for (i = 0; i < 2; i++) {
3054                 n = p->local->phone[i];
3055                 while (n) {
3056                         m = n->next;
3057                         kfree(n);
3058                         n = m;
3059                 }
3060                 p->local->phone[i] = NULL;
3061         }
3062         p->local->dial = NULL;
3063         return 0;
3064 }
3065
3066 /*
3067  * Force a hangup of a network-interface.
3068  */
3069 int
3070 isdn_net_force_hangup(char *name)
3071 {
3072         isdn_net_dev *p = isdn_net_findif(name);
3073         struct net_device *q;
3074
3075         if (p) {
3076                 if (p->local->isdn_device < 0)
3077                         return 1;
3078                 q = p->local->slave;
3079                 /* If this interface has slaves, do a hangup for them also. */
3080                 while (q) {
3081                         isdn_net_hangup(q);
3082                         q = (((isdn_net_local *) q->priv)->slave);
3083                 }
3084                 isdn_net_hangup(p->dev);
3085                 return 0;
3086         }
3087         return -ENODEV;
3088 }
3089
3090 /*
3091  * Helper-function for isdn_net_rm: Do the real work.
3092  */
3093 static int
3094 isdn_net_realrm(isdn_net_dev * p, isdn_net_dev * q)
3095 {
3096         u_long flags;
3097
3098         if (isdn_net_device_started(p)) {
3099                 return -EBUSY;
3100         }
3101 #ifdef CONFIG_ISDN_X25
3102         if( p -> cprot && p -> cprot -> pops )
3103                 p -> cprot -> pops -> proto_del ( p -> cprot );
3104 #endif
3105         /* Free all phone-entries */
3106         isdn_net_rmallphone(p);
3107         /* If interface is bound exclusive, free channel-usage */
3108         if (p->local->exclusive != -1)
3109                 isdn_unexclusive_channel(p->local->pre_device, p->local->pre_channel);
3110         if (p->local->master) {
3111                 /* It's a slave-device, so update master's slave-pointer if necessary */
3112                 if (((isdn_net_local *) (p->local->master->priv))->slave == p->dev)
3113                         ((isdn_net_local *) (p->local->master->priv))->slave = p->local->slave;
3114         } else {
3115                 /* Unregister only if it's a master-device */
3116                 unregister_netdev(p->dev);
3117         }
3118         /* Unlink device from chain */
3119         spin_lock_irqsave(&dev->lock, flags);
3120         if (q)
3121                 q->next = p->next;
3122         else
3123                 dev->netdev = p->next;
3124         if (p->local->slave) {
3125                 /* If this interface has a slave, remove it also */
3126                 char *slavename = p->local->slave->name;
3127                 isdn_net_dev *n = dev->netdev;
3128                 q = NULL;
3129                 while (n) {
3130                         if (!strcmp(n->dev->name, slavename)) {
3131                                 spin_unlock_irqrestore(&dev->lock, flags);
3132                                 isdn_net_realrm(n, q);
3133                                 spin_lock_irqsave(&dev->lock, flags);
3134                                 break;
3135                         }
3136                         q = n;
3137                         n = (isdn_net_dev *)n->next;
3138                 }
3139         }
3140         spin_unlock_irqrestore(&dev->lock, flags);
3141         /* If no more net-devices remain, disable auto-hangup timer */
3142         if (dev->netdev == NULL)
3143                 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 0);
3144         free_netdev(p->dev);
3145         kfree(p);
3146
3147         return 0;
3148 }
3149
3150 /*
3151  * Remove a single network-interface.
3152  */
3153 int
3154 isdn_net_rm(char *name)
3155 {
3156         u_long flags;
3157         isdn_net_dev *p;
3158         isdn_net_dev *q;
3159
3160         /* Search name in netdev-chain */
3161         spin_lock_irqsave(&dev->lock, flags);
3162         p = dev->netdev;
3163         q = NULL;
3164         while (p) {
3165                 if (!strcmp(p->dev->name, name)) {
3166                         spin_unlock_irqrestore(&dev->lock, flags);
3167                         return (isdn_net_realrm(p, q));
3168                 }
3169                 q = p;
3170                 p = (isdn_net_dev *) p->next;
3171         }
3172         spin_unlock_irqrestore(&dev->lock, flags);
3173         /* If no more net-devices remain, disable auto-hangup timer */
3174         if (dev->netdev == NULL)
3175                 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 0);
3176         return -ENODEV;
3177 }
3178
3179 /*
3180  * Remove all network-interfaces
3181  */
3182 int
3183 isdn_net_rmall(void)
3184 {
3185         u_long flags;
3186         int ret;
3187
3188         /* Walk through netdev-chain */
3189         spin_lock_irqsave(&dev->lock, flags);
3190         while (dev->netdev) {
3191                 if (!dev->netdev->local->master) {
3192                         /* Remove master-devices only, slaves get removed with their master */
3193                         spin_unlock_irqrestore(&dev->lock, flags);
3194                         if ((ret = isdn_net_realrm(dev->netdev, NULL))) {
3195                                 return ret;
3196                         }
3197                         spin_lock_irqsave(&dev->lock, flags);
3198                 }
3199         }
3200         dev->netdev = NULL;
3201         spin_unlock_irqrestore(&dev->lock, flags);
3202         return 0;
3203 }