Remove obsolete #include <linux/config.h>
[linux-2.6.git] / 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(lp->netdev->dev.qdisc);
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", l->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                                                                l->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                                                                l->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", lp->name);
446                                         printk(KERN_INFO "%s: Chargesum is %d\n", lp->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", lp->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                                        lp->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", lp->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                                                lp->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                                                lp->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", lp->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", lp->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", lp->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, lp->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                                         slp->name, lp->name);
840                                 isdn_net_hangup(lp->slave);
841                         }
842                 }
843                 printk(KERN_INFO "isdn_net: local hangup %s\n", lp->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", lp->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         unsigned short source;
869         unsigned short dest;
870 } ip_ports;
871
872 static void
873 isdn_net_log_skb(struct sk_buff * skb, isdn_net_local * lp)
874 {
875         u_char *p = skb->nh.raw; /* hopefully, this was set correctly */
876         unsigned short proto = ntohs(skb->protocol);
877         int data_ofs;
878         ip_ports *ipp;
879         char addinfo[100];
880
881         addinfo[0] = '\0';
882         /* This check stolen from 2.1.72 dev_queue_xmit_nit() */
883         if (skb->nh.raw < skb->data || skb->nh.raw >= skb->tail) {
884                 /* fall back to old isdn_net_log_packet method() */
885                 char * buf = skb->data;
886
887                 printk(KERN_DEBUG "isdn_net: protocol %04x is buggy, dev %s\n", skb->protocol, lp->name);
888                 p = buf;
889                 proto = ETH_P_IP;
890                 switch (lp->p_encap) {
891                         case ISDN_NET_ENCAP_IPTYP:
892                                 proto = ntohs(*(unsigned short *) &buf[0]);
893                                 p = &buf[2];
894                                 break;
895                         case ISDN_NET_ENCAP_ETHER:
896                                 proto = ntohs(*(unsigned short *) &buf[12]);
897                                 p = &buf[14];
898                                 break;
899                         case ISDN_NET_ENCAP_CISCOHDLC:
900                                 proto = ntohs(*(unsigned short *) &buf[2]);
901                                 p = &buf[4];
902                                 break;
903 #ifdef CONFIG_ISDN_PPP
904                         case ISDN_NET_ENCAP_SYNCPPP:
905                                 proto = ntohs(skb->protocol);
906                                 p = &buf[IPPP_MAX_HEADER];
907                                 break;
908 #endif
909                 }
910         }
911         data_ofs = ((p[0] & 15) * 4);
912         switch (proto) {
913                 case ETH_P_IP:
914                         switch (p[9]) {
915                                 case 1:
916                                         strcpy(addinfo, " ICMP");
917                                         break;
918                                 case 2:
919                                         strcpy(addinfo, " IGMP");
920                                         break;
921                                 case 4:
922                                         strcpy(addinfo, " IPIP");
923                                         break;
924                                 case 6:
925                                         ipp = (ip_ports *) (&p[data_ofs]);
926                                         sprintf(addinfo, " TCP, port: %d -> %d", ntohs(ipp->source),
927                                                 ntohs(ipp->dest));
928                                         break;
929                                 case 8:
930                                         strcpy(addinfo, " EGP");
931                                         break;
932                                 case 12:
933                                         strcpy(addinfo, " PUP");
934                                         break;
935                                 case 17:
936                                         ipp = (ip_ports *) (&p[data_ofs]);
937                                         sprintf(addinfo, " UDP, port: %d -> %d", ntohs(ipp->source),
938                                                 ntohs(ipp->dest));
939                                         break;
940                                 case 22:
941                                         strcpy(addinfo, " IDP");
942                                         break;
943                         }
944                         printk(KERN_INFO
945                                 "OPEN: %d.%d.%d.%d -> %d.%d.%d.%d%s\n",
946
947                                p[12], p[13], p[14], p[15],
948                                p[16], p[17], p[18], p[19],
949                                addinfo);
950                         break;
951                 case ETH_P_ARP:
952                         printk(KERN_INFO
953                                 "OPEN: ARP %d.%d.%d.%d -> *.*.*.* ?%d.%d.%d.%d\n",
954                                p[14], p[15], p[16], p[17],
955                                p[24], p[25], p[26], p[27]);
956                         break;
957         }
958 }
959
960 /*
961  * this function is used to send supervisory data, i.e. data which was
962  * not received from the network layer, but e.g. frames from ipppd, CCP
963  * reset frames etc.
964  */
965 void isdn_net_write_super(isdn_net_local *lp, struct sk_buff *skb)
966 {
967         if (in_irq()) {
968                 // we can't grab the lock from irq context, 
969                 // so we just queue the packet
970                 skb_queue_tail(&lp->super_tx_queue, skb);
971                 schedule_work(&lp->tqueue);
972                 return;
973         }
974
975         spin_lock_bh(&lp->xmit_lock);
976         if (!isdn_net_lp_busy(lp)) {
977                 isdn_net_writebuf_skb(lp, skb);
978         } else {
979                 skb_queue_tail(&lp->super_tx_queue, skb);
980         }
981         spin_unlock_bh(&lp->xmit_lock);
982 }
983
984 /*
985  * called from tq_immediate
986  */
987 static void isdn_net_softint(void *private)
988 {
989         isdn_net_local *lp = private;
990         struct sk_buff *skb;
991
992         spin_lock_bh(&lp->xmit_lock);
993         while (!isdn_net_lp_busy(lp)) {
994                 skb = skb_dequeue(&lp->super_tx_queue);
995                 if (!skb)
996                         break;
997                 isdn_net_writebuf_skb(lp, skb);                                
998         }
999         spin_unlock_bh(&lp->xmit_lock);
1000 }
1001
1002 /* 
1003  * all frames sent from the (net) LL to a HL driver should go via this function
1004  * it's serialized by the caller holding the lp->xmit_lock spinlock
1005  */
1006 void isdn_net_writebuf_skb(isdn_net_local *lp, struct sk_buff *skb)
1007 {
1008         int ret;
1009         int len = skb->len;     /* save len */
1010
1011         /* before obtaining the lock the caller should have checked that
1012            the lp isn't busy */
1013         if (isdn_net_lp_busy(lp)) {
1014                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1015                 goto error;
1016         }
1017
1018         if (!(lp->flags & ISDN_NET_CONNECTED)) {
1019                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1020                 goto error;
1021         }
1022         ret = isdn_writebuf_skb_stub(lp->isdn_device, lp->isdn_channel, 1, skb);
1023         if (ret != len) {
1024                 /* we should never get here */
1025                 printk(KERN_WARNING "%s: HL driver queue full\n", lp->name);
1026                 goto error;
1027         }
1028         
1029         lp->transcount += len;
1030         isdn_net_inc_frame_cnt(lp);
1031         return;
1032
1033  error:
1034         dev_kfree_skb(skb);
1035         lp->stats.tx_errors++;
1036
1037 }
1038
1039
1040 /*
1041  *  Helper function for isdn_net_start_xmit.
1042  *  When called, the connection is already established.
1043  *  Based on cps-calculation, check if device is overloaded.
1044  *  If so, and if a slave exists, trigger dialing for it.
1045  *  If any slave is online, deliver packets using a simple round robin
1046  *  scheme.
1047  *
1048  *  Return: 0 on success, !0 on failure.
1049  */
1050
1051 static int
1052 isdn_net_xmit(struct net_device *ndev, struct sk_buff *skb)
1053 {
1054         isdn_net_dev *nd;
1055         isdn_net_local *slp;
1056         isdn_net_local *lp = (isdn_net_local *) ndev->priv;
1057         int retv = 0;
1058
1059         if (((isdn_net_local *) (ndev->priv))->master) {
1060                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1061                 dev_kfree_skb(skb);
1062                 return 0;
1063         }
1064
1065         /* For the other encaps the header has already been built */
1066 #ifdef CONFIG_ISDN_PPP
1067         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
1068                 return isdn_ppp_xmit(skb, ndev);
1069         }
1070 #endif
1071         nd = ((isdn_net_local *) ndev->priv)->netdev;
1072         lp = isdn_net_get_locked_lp(nd);
1073         if (!lp) {
1074                 printk(KERN_WARNING "%s: all channels busy - requeuing!\n", ndev->name);
1075                 return 1;
1076         }
1077         /* we have our lp locked from now on */
1078
1079         /* Reset hangup-timeout */
1080         lp->huptimer = 0; // FIXME?
1081         isdn_net_writebuf_skb(lp, skb);
1082         spin_unlock_bh(&lp->xmit_lock);
1083
1084         /* the following stuff is here for backwards compatibility.
1085          * in future, start-up and hangup of slaves (based on current load)
1086          * should move to userspace and get based on an overall cps
1087          * calculation
1088          */
1089         if (lp->cps > lp->triggercps) {
1090                 if (lp->slave) {
1091                         if (!lp->sqfull) {
1092                                 /* First time overload: set timestamp only */
1093                                 lp->sqfull = 1;
1094                                 lp->sqfull_stamp = jiffies;
1095                         } else {
1096                                 /* subsequent overload: if slavedelay exceeded, start dialing */
1097                                 if (time_after(jiffies, lp->sqfull_stamp + lp->slavedelay)) {
1098                                         slp = lp->slave->priv;
1099                                         if (!(slp->flags & ISDN_NET_CONNECTED)) {
1100                                                 isdn_net_force_dial_lp((isdn_net_local *) lp->slave->priv);
1101                                         }
1102                                 }
1103                         }
1104                 }
1105         } else {
1106                 if (lp->sqfull && time_after(jiffies, lp->sqfull_stamp + lp->slavedelay + (10 * HZ))) {
1107                         lp->sqfull = 0;
1108                 }
1109                 /* this is a hack to allow auto-hangup for slaves on moderate loads */
1110                 nd->queue = nd->local;
1111         }
1112
1113         return retv;
1114
1115 }
1116
1117 static void
1118 isdn_net_adjust_hdr(struct sk_buff *skb, struct net_device *dev)
1119 {
1120         isdn_net_local *lp = (isdn_net_local *) dev->priv;
1121         if (!skb)
1122                 return;
1123         if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
1124                 int pullsize = (ulong)skb->nh.raw - (ulong)skb->data - ETH_HLEN;
1125                 if (pullsize > 0) {
1126                         printk(KERN_DEBUG "isdn_net: Pull junk %d\n", pullsize);
1127                         skb_pull(skb, pullsize);
1128                 }
1129         }
1130 }
1131
1132
1133 static void isdn_net_tx_timeout(struct net_device * ndev)
1134 {
1135         isdn_net_local *lp = (isdn_net_local *) ndev->priv;
1136
1137         printk(KERN_WARNING "isdn_tx_timeout dev %s dialstate %d\n", ndev->name, lp->dialstate);
1138         if (!lp->dialstate){
1139                 lp->stats.tx_errors++;
1140                 /*
1141                  * There is a certain probability that this currently
1142                  * works at all because if we always wake up the interface,
1143                  * then upper layer will try to send the next packet
1144                  * immediately. And then, the old clean_up logic in the
1145                  * driver will hopefully continue to work as it used to do.
1146                  *
1147                  * This is rather primitive right know, we better should
1148                  * clean internal queues here, in particular for multilink and
1149                  * ppp, and reset HL driver's channel, too.   --HE
1150                  *
1151                  * actually, this may not matter at all, because ISDN hardware
1152                  * should not see transmitter hangs at all IMO
1153                  * changed KERN_DEBUG to KERN_WARNING to find out if this is 
1154                  * ever called   --KG
1155                  */
1156         }
1157         ndev->trans_start = jiffies;
1158         netif_wake_queue(ndev);
1159 }
1160
1161 /*
1162  * Try sending a packet.
1163  * If this interface isn't connected to a ISDN-Channel, find a free channel,
1164  * and start dialing.
1165  */
1166 static int
1167 isdn_net_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1168 {
1169         isdn_net_local *lp = (isdn_net_local *) ndev->priv;
1170 #ifdef CONFIG_ISDN_X25
1171         struct concap_proto * cprot = lp -> netdev -> cprot;
1172 /* At this point hard_start_xmit() passes control to the encapsulation
1173    protocol (if present).
1174    For X.25 auto-dialing is completly bypassed because:
1175    - It does not conform with the semantics of a reliable datalink
1176      service as needed by X.25 PLP.
1177    - I don't want that the interface starts dialing when the network layer
1178      sends a message which requests to disconnect the lapb link (or if it
1179      sends any other message not resulting in data transmission).
1180    Instead, dialing will be initiated by the encapsulation protocol entity
1181    when a dl_establish request is received from the upper layer.
1182 */
1183         if (cprot && cprot -> pops) {
1184                 int ret = cprot -> pops -> encap_and_xmit ( cprot , skb);
1185
1186                 if (ret)
1187                         netif_stop_queue(ndev);
1188                 return ret;
1189         } else
1190 #endif
1191         /* auto-dialing xmit function */
1192         {
1193 #ifdef ISDN_DEBUG_NET_DUMP
1194                 u_char *buf;
1195 #endif
1196                 isdn_net_adjust_hdr(skb, ndev);
1197 #ifdef ISDN_DEBUG_NET_DUMP
1198                 buf = skb->data;
1199                 isdn_dumppkt("S:", buf, skb->len, 40);
1200 #endif
1201
1202                 if (!(lp->flags & ISDN_NET_CONNECTED)) {
1203                         int chi;
1204                         /* only do autodial if allowed by config */
1205                         if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) {
1206                                 isdn_net_unreachable(ndev, skb, "dial rejected: interface not in dialmode `auto'");
1207                                 dev_kfree_skb(skb);
1208                                 return 0;
1209                         }
1210                         if (lp->phone[1]) {
1211                                 ulong flags;
1212
1213                                 if(lp->dialwait_timer <= 0)
1214                                         if(lp->dialstarted > 0 && lp->dialtimeout > 0 && time_before(jiffies, lp->dialstarted + lp->dialtimeout + lp->dialwait))
1215                                                 lp->dialwait_timer = lp->dialstarted + lp->dialtimeout + lp->dialwait;
1216
1217                                 if(lp->dialwait_timer > 0) {
1218                                         if(time_before(jiffies, lp->dialwait_timer)) {
1219                                                 isdn_net_unreachable(ndev, skb, "dial rejected: retry-time not reached");
1220                                                 dev_kfree_skb(skb);
1221                                                 return 0;
1222                                         } else
1223                                                 lp->dialwait_timer = 0;
1224                                 }
1225                                 /* Grab a free ISDN-Channel */
1226                                 spin_lock_irqsave(&dev->lock, flags);
1227                                 if (((chi =
1228                                      isdn_get_free_channel(
1229                                                         ISDN_USAGE_NET,
1230                                                         lp->l2_proto,
1231                                                         lp->l3_proto,
1232                                                         lp->pre_device,
1233                                                         lp->pre_channel,
1234                                                         lp->msn)
1235                                                         ) < 0) &&
1236                                         ((chi =
1237                                      isdn_get_free_channel(
1238                                                         ISDN_USAGE_NET,
1239                                                         lp->l2_proto,
1240                                                         lp->l3_proto,
1241                                                         lp->pre_device,
1242                                                         lp->pre_channel^1,
1243                                                         lp->msn)
1244                                                         ) < 0)) {
1245                                         spin_unlock_irqrestore(&dev->lock, flags);
1246                                         isdn_net_unreachable(ndev, skb,
1247                                                            "No channel");
1248                                         dev_kfree_skb(skb);
1249                                         return 0;
1250                                 }
1251                                 /* Log packet, which triggered dialing */
1252                                 if (dev->net_verbose)
1253                                         isdn_net_log_skb(skb, lp);
1254                                 lp->dialstate = 1;
1255                                 /* Connect interface with channel */
1256                                 isdn_net_bind_channel(lp, chi);
1257 #ifdef CONFIG_ISDN_PPP
1258                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
1259                                         /* no 'first_skb' handling for syncPPP */
1260                                         if (isdn_ppp_bind(lp) < 0) {
1261                                                 dev_kfree_skb(skb);
1262                                                 isdn_net_unbind_channel(lp);
1263                                                 spin_unlock_irqrestore(&dev->lock, flags);
1264                                                 return 0;       /* STN (skb to nirvana) ;) */
1265                                         }
1266 #ifdef CONFIG_IPPP_FILTER
1267                                         if (isdn_ppp_autodial_filter(skb, lp)) {
1268                                                 isdn_ppp_free(lp);
1269                                                 isdn_net_unbind_channel(lp);
1270                                                 spin_unlock_irqrestore(&dev->lock, flags);
1271                                                 isdn_net_unreachable(ndev, skb, "dial rejected: packet filtered");
1272                                                 dev_kfree_skb(skb);
1273                                                 return 0;
1274                                         }
1275 #endif
1276                                         spin_unlock_irqrestore(&dev->lock, flags);
1277                                         isdn_net_dial();        /* Initiate dialing */
1278                                         netif_stop_queue(ndev);
1279                                         return 1;       /* let upper layer requeue skb packet */
1280                                 }
1281 #endif
1282                                 /* Initiate dialing */
1283                                 spin_unlock_irqrestore(&dev->lock, flags);
1284                                 isdn_net_dial();
1285                                 isdn_net_device_stop_queue(lp);
1286                                 return 1;
1287                         } else {
1288                                 isdn_net_unreachable(ndev, skb,
1289                                                      "No phone number");
1290                                 dev_kfree_skb(skb);
1291                                 return 0;
1292                         }
1293                 } else {
1294                         /* Device is connected to an ISDN channel */ 
1295                         ndev->trans_start = jiffies;
1296                         if (!lp->dialstate) {
1297                                 /* ISDN connection is established, try sending */
1298                                 int ret;
1299                                 ret = (isdn_net_xmit(ndev, skb));
1300                                 if(ret) netif_stop_queue(ndev);
1301                                 return ret;
1302                         } else
1303                                 netif_stop_queue(ndev);
1304                 }
1305         }
1306         return 1;
1307 }
1308
1309 /*
1310  * Shutdown a net-interface.
1311  */
1312 static int
1313 isdn_net_close(struct net_device *dev)
1314 {
1315         struct net_device *p;
1316 #ifdef CONFIG_ISDN_X25
1317         struct concap_proto * cprot =
1318                 ( (isdn_net_local *) dev->priv ) -> netdev -> cprot;
1319         /* printk(KERN_DEBUG "isdn_net_close %s\n" , dev-> name ); */
1320 #endif
1321
1322 #ifdef CONFIG_ISDN_X25
1323         if( cprot && cprot -> pops ) cprot -> pops -> close( cprot );
1324 #endif
1325         netif_stop_queue(dev);
1326         if ((p = (((isdn_net_local *) dev->priv)->slave))) {
1327                 /* If this interface has slaves, stop them also */
1328                 while (p) {
1329 #ifdef CONFIG_ISDN_X25
1330                         cprot = ( (isdn_net_local *) p->priv )
1331                                 -> netdev -> cprot;
1332                         if( cprot && cprot -> pops )
1333                                 cprot -> pops -> close( cprot );
1334 #endif
1335                         isdn_net_hangup(p);
1336                         p = (((isdn_net_local *) p->priv)->slave);
1337                 }
1338         }
1339         isdn_net_hangup(dev);
1340         isdn_unlock_drivers();
1341         return 0;
1342 }
1343
1344 /*
1345  * Get statistics
1346  */
1347 static struct net_device_stats *
1348 isdn_net_get_stats(struct net_device *dev)
1349 {
1350         isdn_net_local *lp = (isdn_net_local *) dev->priv;
1351         return &lp->stats;
1352 }
1353
1354 /*      This is simply a copy from std. eth.c EXCEPT we pull ETH_HLEN
1355  *      instead of dev->hard_header_len off. This is done because the
1356  *      lowlevel-driver has already pulled off its stuff when we get
1357  *      here and this routine only gets called with p_encap == ETHER.
1358  *      Determine the packet's protocol ID. The rule here is that we
1359  *      assume 802.3 if the type field is short enough to be a length.
1360  *      This is normal practice and works for any 'now in use' protocol.
1361  */
1362
1363 static unsigned short
1364 isdn_net_type_trans(struct sk_buff *skb, struct net_device *dev)
1365 {
1366         struct ethhdr *eth;
1367         unsigned char *rawp;
1368
1369         skb->mac.raw = skb->data;
1370         skb_pull(skb, ETH_HLEN);
1371         eth = eth_hdr(skb);
1372
1373         if (*eth->h_dest & 1) {
1374                 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
1375                         skb->pkt_type = PACKET_BROADCAST;
1376                 else
1377                         skb->pkt_type = PACKET_MULTICAST;
1378         }
1379         /*
1380          *      This ALLMULTI check should be redundant by 1.4
1381          *      so don't forget to remove it.
1382          */
1383
1384         else if (dev->flags & (IFF_PROMISC /*| IFF_ALLMULTI*/)) {
1385                 if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
1386                         skb->pkt_type = PACKET_OTHERHOST;
1387         }
1388         if (ntohs(eth->h_proto) >= 1536)
1389                 return eth->h_proto;
1390
1391         rawp = skb->data;
1392
1393         /*
1394          *      This is a magic hack to spot IPX packets. Older Novell breaks
1395          *      the protocol design and runs IPX over 802.3 without an 802.2 LLC
1396          *      layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
1397          *      won't work for fault tolerant netware but does for the rest.
1398          */
1399         if (*(unsigned short *) rawp == 0xFFFF)
1400                 return htons(ETH_P_802_3);
1401         /*
1402          *      Real 802.2 LLC
1403          */
1404         return htons(ETH_P_802_2);
1405 }
1406
1407
1408 /* 
1409  * CISCO HDLC keepalive specific stuff
1410  */
1411 static struct sk_buff*
1412 isdn_net_ciscohdlck_alloc_skb(isdn_net_local *lp, int len)
1413 {
1414         unsigned short hl = dev->drv[lp->isdn_device]->interface->hl_hdrlen;
1415         struct sk_buff *skb;
1416
1417         skb = alloc_skb(hl + len, GFP_ATOMIC);
1418         if (skb)
1419                 skb_reserve(skb, hl);
1420         else 
1421                 printk("isdn out of mem at %s:%d!\n", __FILE__, __LINE__);
1422         return skb;
1423 }
1424
1425 /* cisco hdlck device private ioctls */
1426 static int
1427 isdn_ciscohdlck_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1428 {
1429         isdn_net_local *lp = (isdn_net_local *) dev->priv;
1430         unsigned long len = 0;
1431         unsigned long expires = 0;
1432         int tmp = 0;
1433         int period = lp->cisco_keepalive_period;
1434         s8 debserint = lp->cisco_debserint;
1435         int rc = 0;
1436
1437         if (lp->p_encap != ISDN_NET_ENCAP_CISCOHDLCK)
1438                 return -EINVAL;
1439
1440         switch (cmd) {
1441                 /* get/set keepalive period */
1442                 case SIOCGKEEPPERIOD:
1443                         len = (unsigned long)sizeof(lp->cisco_keepalive_period);
1444                         if (copy_to_user(ifr->ifr_data,
1445                                 &lp->cisco_keepalive_period, len))
1446                                 rc = -EFAULT;
1447                         break;
1448                 case SIOCSKEEPPERIOD:
1449                         tmp = lp->cisco_keepalive_period;
1450                         len = (unsigned long)sizeof(lp->cisco_keepalive_period);
1451                         if (copy_from_user(&period, ifr->ifr_data, len))
1452                                 rc = -EFAULT;
1453                         if ((period > 0) && (period <= 32767))
1454                                 lp->cisco_keepalive_period = period;
1455                         else
1456                                 rc = -EINVAL;
1457                         if (!rc && (tmp != lp->cisco_keepalive_period)) {
1458                                 expires = (unsigned long)(jiffies +
1459                                         lp->cisco_keepalive_period * HZ);
1460                                 mod_timer(&lp->cisco_timer, expires);
1461                                 printk(KERN_INFO "%s: Keepalive period set "
1462                                         "to %d seconds.\n",
1463                                         lp->name, lp->cisco_keepalive_period);
1464                         }
1465                         break;
1466
1467                 /* get/set debugging */
1468                 case SIOCGDEBSERINT:
1469                         len = (unsigned long)sizeof(lp->cisco_debserint);
1470                         if (copy_to_user(ifr->ifr_data,
1471                                 &lp->cisco_debserint, len))
1472                                 rc = -EFAULT;
1473                         break;
1474                 case SIOCSDEBSERINT:
1475                         len = (unsigned long)sizeof(lp->cisco_debserint);
1476                         if (copy_from_user(&debserint,
1477                                 ifr->ifr_data, len))
1478                                 rc = -EFAULT;
1479                         if ((debserint >= 0) && (debserint <= 64))
1480                                 lp->cisco_debserint = debserint;
1481                         else
1482                                 rc = -EINVAL;
1483                         break;
1484
1485                 default:
1486                         rc = -EINVAL;
1487                         break;
1488         }
1489         return (rc);
1490 }
1491
1492 /* called via cisco_timer.function */
1493 static void
1494 isdn_net_ciscohdlck_slarp_send_keepalive(unsigned long data)
1495 {
1496         isdn_net_local *lp = (isdn_net_local *) data;
1497         struct sk_buff *skb;
1498         unsigned char *p;
1499         unsigned long last_cisco_myseq = lp->cisco_myseq;
1500         int myseq_diff = 0;
1501
1502         if (!(lp->flags & ISDN_NET_CONNECTED) || lp->dialstate) {
1503                 printk("isdn BUG at %s:%d!\n", __FILE__, __LINE__);
1504                 return;
1505         }
1506         lp->cisco_myseq++;
1507
1508         myseq_diff = (lp->cisco_myseq - lp->cisco_mineseen);
1509         if ((lp->cisco_line_state) && ((myseq_diff >= 3)||(myseq_diff <= -3))) {
1510                 /* line up -> down */
1511                 lp->cisco_line_state = 0;
1512                 printk (KERN_WARNING
1513                                 "UPDOWN: Line protocol on Interface %s,"
1514                                 " changed state to down\n", lp->name);
1515                 /* should stop routing higher-level data accross */
1516         } else if ((!lp->cisco_line_state) &&
1517                 (myseq_diff >= 0) && (myseq_diff <= 2)) {
1518                 /* line down -> up */
1519                 lp->cisco_line_state = 1;
1520                 printk (KERN_WARNING
1521                                 "UPDOWN: Line protocol on Interface %s,"
1522                                 " changed state to up\n", lp->name);
1523                 /* restart routing higher-level data accross */
1524         }
1525
1526         if (lp->cisco_debserint)
1527                 printk (KERN_DEBUG "%s: HDLC "
1528                         "myseq %lu, mineseen %lu%c, yourseen %lu, %s\n",
1529                         lp->name, last_cisco_myseq, lp->cisco_mineseen,
1530                         ((last_cisco_myseq == lp->cisco_mineseen) ? '*' : 040),
1531                         lp->cisco_yourseq,
1532                         ((lp->cisco_line_state) ? "line up" : "line down"));
1533
1534         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1535         if (!skb)
1536                 return;
1537
1538         p = skb_put(skb, 4 + 14);
1539
1540         /* cisco header */
1541         p += put_u8 (p, CISCO_ADDR_UNICAST);
1542         p += put_u8 (p, CISCO_CTRL);
1543         p += put_u16(p, CISCO_TYPE_SLARP);
1544
1545         /* slarp keepalive */
1546         p += put_u32(p, CISCO_SLARP_KEEPALIVE);
1547         p += put_u32(p, lp->cisco_myseq);
1548         p += put_u32(p, lp->cisco_yourseq);
1549         p += put_u16(p, 0xffff); // reliablity, always 0xffff
1550
1551         isdn_net_write_super(lp, skb);
1552
1553         lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
1554         
1555         add_timer(&lp->cisco_timer);
1556 }
1557
1558 static void
1559 isdn_net_ciscohdlck_slarp_send_request(isdn_net_local *lp)
1560 {
1561         struct sk_buff *skb;
1562         unsigned char *p;
1563
1564         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1565         if (!skb)
1566                 return;
1567
1568         p = skb_put(skb, 4 + 14);
1569
1570         /* cisco header */
1571         p += put_u8 (p, CISCO_ADDR_UNICAST);
1572         p += put_u8 (p, CISCO_CTRL);
1573         p += put_u16(p, CISCO_TYPE_SLARP);
1574
1575         /* slarp request */
1576         p += put_u32(p, CISCO_SLARP_REQUEST);
1577         p += put_u32(p, 0); // address
1578         p += put_u32(p, 0); // netmask
1579         p += put_u16(p, 0); // unused
1580
1581         isdn_net_write_super(lp, skb);
1582 }
1583
1584 static void 
1585 isdn_net_ciscohdlck_connected(isdn_net_local *lp)
1586 {
1587         lp->cisco_myseq = 0;
1588         lp->cisco_mineseen = 0;
1589         lp->cisco_yourseq = 0;
1590         lp->cisco_keepalive_period = ISDN_TIMER_KEEPINT;
1591         lp->cisco_last_slarp_in = 0;
1592         lp->cisco_line_state = 0;
1593         lp->cisco_debserint = 0;
1594
1595         /* send slarp request because interface/seq.no.s reset */
1596         isdn_net_ciscohdlck_slarp_send_request(lp);
1597
1598         init_timer(&lp->cisco_timer);
1599         lp->cisco_timer.data = (unsigned long) lp;
1600         lp->cisco_timer.function = isdn_net_ciscohdlck_slarp_send_keepalive;
1601         lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
1602         add_timer(&lp->cisco_timer);
1603 }
1604
1605 static void 
1606 isdn_net_ciscohdlck_disconnected(isdn_net_local *lp)
1607 {
1608         del_timer(&lp->cisco_timer);
1609 }
1610
1611 static void
1612 isdn_net_ciscohdlck_slarp_send_reply(isdn_net_local *lp)
1613 {
1614         struct sk_buff *skb;
1615         unsigned char *p;
1616         struct in_device *in_dev = NULL;
1617         u32 addr = 0;           /* local ipv4 address */
1618         u32 mask = 0;           /* local netmask */
1619
1620         if ((in_dev = lp->netdev->dev.ip_ptr) != NULL) {
1621                 /* take primary(first) address of interface */
1622                 struct in_ifaddr *ifa = in_dev->ifa_list;
1623                 if (ifa != NULL) {
1624                         addr = ifa->ifa_local;
1625                         mask = ifa->ifa_mask;
1626                 }
1627         }
1628
1629         skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
1630         if (!skb)
1631                 return;
1632
1633         p = skb_put(skb, 4 + 14);
1634
1635         /* cisco header */
1636         p += put_u8 (p, CISCO_ADDR_UNICAST);
1637         p += put_u8 (p, CISCO_CTRL);
1638         p += put_u16(p, CISCO_TYPE_SLARP);
1639
1640         /* slarp reply, send own ip/netmask; if values are nonsense remote
1641          * should think we are unable to provide it with an address via SLARP */
1642         p += put_u32(p, CISCO_SLARP_REPLY);
1643         p += put_u32(p, addr);  // address
1644         p += put_u32(p, mask);  // netmask
1645         p += put_u16(p, 0);     // unused
1646
1647         isdn_net_write_super(lp, skb);
1648 }
1649
1650 static void
1651 isdn_net_ciscohdlck_slarp_in(isdn_net_local *lp, struct sk_buff *skb)
1652 {
1653         unsigned char *p;
1654         int period;
1655         u32 code;
1656         u32 my_seq, addr;
1657         u32 your_seq, mask;
1658         u32 local;
1659         u16 unused;
1660
1661         if (skb->len < 14)
1662                 return;
1663
1664         p = skb->data;
1665         p += get_u32(p, &code);
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 = ntohl(*(u32 *)p);
1674                 mask = ntohl(*(u32 *)(p+4));
1675                 if (mask != 0xfffffffc)
1676                         goto slarp_reply_out;
1677                 if ((addr & 3) == 0 || (addr & 3) == 3)
1678                         goto slarp_reply_out;
1679                 local = addr ^ 3;
1680                 printk(KERN_INFO "%s: got slarp reply: "
1681                         "remote ip: %d.%d.%d.%d, "
1682                         "local ip: %d.%d.%d.%d "
1683                         "mask: %d.%d.%d.%d\n",
1684                        lp->name,
1685                        HIPQUAD(addr),
1686                        HIPQUAD(local),
1687                        HIPQUAD(mask));
1688                 break;
1689   slarp_reply_out:
1690                  printk(KERN_INFO "%s: got invalid slarp "
1691                                  "reply (%d.%d.%d.%d/%d.%d.%d.%d) "
1692                                  "- ignored\n", lp->name,
1693                                  HIPQUAD(addr), HIPQUAD(mask));
1694                 break;
1695         case CISCO_SLARP_KEEPALIVE:
1696                 period = (int)((jiffies - lp->cisco_last_slarp_in
1697                                 + HZ/2 - 1) / HZ);
1698                 if (lp->cisco_debserint &&
1699                                 (period != lp->cisco_keepalive_period) &&
1700                                 lp->cisco_last_slarp_in) {
1701                         printk(KERN_DEBUG "%s: Keepalive period mismatch - "
1702                                 "is %d but should be %d.\n",
1703                                 lp->name, period, lp->cisco_keepalive_period);
1704                 }
1705                 lp->cisco_last_slarp_in = jiffies;
1706                 p += get_u32(p, &my_seq);
1707                 p += get_u32(p, &your_seq);
1708                 p += get_u16(p, &unused);
1709                 lp->cisco_yourseq = my_seq;
1710                 lp->cisco_mineseen = your_seq;
1711                 break;
1712         }
1713 }
1714
1715 static void
1716 isdn_net_ciscohdlck_receive(isdn_net_local *lp, struct sk_buff *skb)
1717 {
1718         unsigned char *p;
1719         u8 addr;
1720         u8 ctrl;
1721         u16 type;
1722         
1723         if (skb->len < 4)
1724                 goto out_free;
1725
1726         p = skb->data;
1727         p += get_u8 (p, &addr);
1728         p += get_u8 (p, &ctrl);
1729         p += get_u16(p, &type);
1730         skb_pull(skb, 4);
1731         
1732         if (addr != CISCO_ADDR_UNICAST && addr != CISCO_ADDR_BROADCAST) {
1733                 printk(KERN_WARNING "%s: Unknown Cisco addr 0x%02x\n",
1734                        lp->name, addr);
1735                 goto out_free;
1736         }
1737         if (ctrl != CISCO_CTRL) {
1738                 printk(KERN_WARNING "%s: Unknown Cisco ctrl 0x%02x\n",
1739                        lp->name, ctrl);
1740                 goto out_free;
1741         }
1742
1743         switch (type) {
1744         case CISCO_TYPE_SLARP:
1745                 isdn_net_ciscohdlck_slarp_in(lp, skb);
1746                 goto out_free;
1747         case CISCO_TYPE_CDP:
1748                 if (lp->cisco_debserint)
1749                         printk(KERN_DEBUG "%s: Received CDP packet. use "
1750                                 "\"no cdp enable\" on cisco.\n", lp->name);
1751                 goto out_free;
1752         default:
1753                 /* no special cisco protocol */
1754                 skb->protocol = htons(type);
1755                 netif_rx(skb);
1756                 return;
1757         }
1758
1759  out_free:
1760         kfree_skb(skb);
1761 }
1762
1763 /*
1764  * Got a packet from ISDN-Channel.
1765  */
1766 static void
1767 isdn_net_receive(struct net_device *ndev, struct sk_buff *skb)
1768 {
1769         isdn_net_local *lp = (isdn_net_local *) ndev->priv;
1770         isdn_net_local *olp = lp;       /* original 'lp' */
1771 #ifdef CONFIG_ISDN_X25
1772         struct concap_proto *cprot = lp -> netdev -> cprot;
1773 #endif
1774         lp->transcount += skb->len;
1775
1776         lp->stats.rx_packets++;
1777         lp->stats.rx_bytes += skb->len;
1778         if (lp->master) {
1779                 /* Bundling: If device is a slave-device, deliver to master, also
1780                  * handle master's statistics and hangup-timeout
1781                  */
1782                 ndev = lp->master;
1783                 lp = (isdn_net_local *) ndev->priv;
1784                 lp->stats.rx_packets++;
1785                 lp->stats.rx_bytes += skb->len;
1786         }
1787         skb->dev = ndev;
1788         skb->pkt_type = PACKET_HOST;
1789         skb->mac.raw = skb->data;
1790 #ifdef ISDN_DEBUG_NET_DUMP
1791         isdn_dumppkt("R:", skb->data, skb->len, 40);
1792 #endif
1793         switch (lp->p_encap) {
1794                 case ISDN_NET_ENCAP_ETHER:
1795                         /* Ethernet over ISDN */
1796                         olp->huptimer = 0;
1797                         lp->huptimer = 0;
1798                         skb->protocol = isdn_net_type_trans(skb, ndev);
1799                         break;
1800                 case ISDN_NET_ENCAP_UIHDLC:
1801                         /* HDLC with UI-frame (for ispa with -h1 option) */
1802                         olp->huptimer = 0;
1803                         lp->huptimer = 0;
1804                         skb_pull(skb, 2);
1805                         /* Fall through */
1806                 case ISDN_NET_ENCAP_RAWIP:
1807                         /* RAW-IP without MAC-Header */
1808                         olp->huptimer = 0;
1809                         lp->huptimer = 0;
1810                         skb->protocol = htons(ETH_P_IP);
1811                         break;
1812                 case ISDN_NET_ENCAP_CISCOHDLCK:
1813                         isdn_net_ciscohdlck_receive(lp, skb);
1814                         return;
1815                 case ISDN_NET_ENCAP_CISCOHDLC:
1816                         /* CISCO-HDLC IP with type field and  fake I-frame-header */
1817                         skb_pull(skb, 2);
1818                         /* Fall through */
1819                 case ISDN_NET_ENCAP_IPTYP:
1820                         /* IP with type field */
1821                         olp->huptimer = 0;
1822                         lp->huptimer = 0;
1823                         skb->protocol = *(unsigned short *) &(skb->data[0]);
1824                         skb_pull(skb, 2);
1825                         if (*(unsigned short *) skb->data == 0xFFFF)
1826                                 skb->protocol = htons(ETH_P_802_3);
1827                         break;
1828 #ifdef CONFIG_ISDN_PPP
1829                 case ISDN_NET_ENCAP_SYNCPPP:
1830                         /* huptimer is done in isdn_ppp_push_higher */
1831                         isdn_ppp_receive(lp->netdev, olp, skb);
1832                         return;
1833 #endif
1834
1835                 default:
1836 #ifdef CONFIG_ISDN_X25
1837                   /* try if there are generic sync_device receiver routines */
1838                         if(cprot) if(cprot -> pops)
1839                                 if( cprot -> pops -> data_ind){
1840                                         cprot -> pops -> data_ind(cprot,skb);
1841                                         return;
1842                                 };
1843 #endif /* CONFIG_ISDN_X25 */
1844                         printk(KERN_WARNING "%s: unknown encapsulation, dropping\n",
1845                                lp->name);
1846                         kfree_skb(skb);
1847                         return;
1848         }
1849
1850         netif_rx(skb);
1851         return;
1852 }
1853
1854 /*
1855  * A packet arrived via ISDN. Search interface-chain for a corresponding
1856  * interface. If found, deliver packet to receiver-function and return 1,
1857  * else return 0.
1858  */
1859 int
1860 isdn_net_rcv_skb(int idx, struct sk_buff *skb)
1861 {
1862         isdn_net_dev *p = dev->rx_netdev[idx];
1863
1864         if (p) {
1865                 isdn_net_local *lp = p->local;
1866                 if ((lp->flags & ISDN_NET_CONNECTED) &&
1867                     (!lp->dialstate)) {
1868                         isdn_net_receive(&p->dev, skb);
1869                         return 1;
1870                 }
1871         }
1872         return 0;
1873 }
1874
1875 static int
1876 my_eth_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
1877               void *daddr, void *saddr, unsigned len)
1878 {
1879         struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
1880
1881         /*
1882          * Set the protocol type. For a packet of type ETH_P_802_3 we
1883          * put the length here instead. It is up to the 802.2 layer to
1884          * carry protocol information.
1885          */
1886
1887         if (type != ETH_P_802_3)
1888                 eth->h_proto = htons(type);
1889         else
1890                 eth->h_proto = htons(len);
1891
1892         /*
1893          * Set the source hardware address.
1894          */
1895         if (saddr)
1896                 memcpy(eth->h_source, saddr, dev->addr_len);
1897         else
1898                 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
1899
1900         /*
1901          * Anyway, the loopback-device should never use this function...
1902          */
1903
1904         if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
1905                 memset(eth->h_dest, 0, dev->addr_len);
1906                 return ETH_HLEN /*(dev->hard_header_len)*/;
1907         }
1908         if (daddr) {
1909                 memcpy(eth->h_dest, daddr, dev->addr_len);
1910                 return ETH_HLEN /*dev->hard_header_len*/;
1911         }
1912         return -ETH_HLEN /*dev->hard_header_len*/;
1913 }
1914
1915 /*
1916  *  build an header
1917  *  depends on encaps that is being used.
1918  */
1919
1920 static int
1921 isdn_net_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
1922                 void *daddr, void *saddr, unsigned plen)
1923 {
1924         isdn_net_local *lp = dev->priv;
1925         unsigned char *p;
1926         ushort len = 0;
1927
1928         switch (lp->p_encap) {
1929                 case ISDN_NET_ENCAP_ETHER:
1930                         len = my_eth_header(skb, dev, type, daddr, saddr, plen);
1931                         break;
1932 #ifdef CONFIG_ISDN_PPP
1933                 case ISDN_NET_ENCAP_SYNCPPP:
1934                         /* stick on a fake header to keep fragmentation code happy. */
1935                         len = IPPP_MAX_HEADER;
1936                         skb_push(skb,len);
1937                         break;
1938 #endif
1939                 case ISDN_NET_ENCAP_RAWIP:
1940                         printk(KERN_WARNING "isdn_net_header called with RAW_IP!\n");
1941                         len = 0;
1942                         break;
1943                 case ISDN_NET_ENCAP_IPTYP:
1944                         /* ethernet type field */
1945                         *((ushort *) skb_push(skb, 2)) = htons(type);
1946                         len = 2;
1947                         break;
1948                 case ISDN_NET_ENCAP_UIHDLC:
1949                         /* HDLC with UI-Frames (for ispa with -h1 option) */
1950                         *((ushort *) skb_push(skb, 2)) = htons(0x0103);
1951                         len = 2;
1952                         break;
1953                 case ISDN_NET_ENCAP_CISCOHDLC:
1954                 case ISDN_NET_ENCAP_CISCOHDLCK:
1955                         p = skb_push(skb, 4);
1956                         p += put_u8 (p, CISCO_ADDR_UNICAST);
1957                         p += put_u8 (p, CISCO_CTRL);
1958                         p += put_u16(p, type);
1959                         len = 4;
1960                         break;
1961 #ifdef CONFIG_ISDN_X25
1962                 default:
1963                   /* try if there are generic concap protocol routines */
1964                         if( lp-> netdev -> cprot ){
1965                                 printk(KERN_WARNING "isdn_net_header called with concap_proto!\n");
1966                                 len = 0;
1967                                 break;
1968                         }
1969                         break;
1970 #endif /* CONFIG_ISDN_X25 */
1971         }
1972         return len;
1973 }
1974
1975 /* We don't need to send arp, because we have point-to-point connections. */
1976 static int
1977 isdn_net_rebuild_header(struct sk_buff *skb)
1978 {
1979         struct net_device *dev = skb->dev;
1980         isdn_net_local *lp = dev->priv;
1981         int ret = 0;
1982
1983         if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
1984                 struct ethhdr *eth = (struct ethhdr *) skb->data;
1985
1986                 /*
1987                  *      Only ARP/IP is currently supported
1988                  */
1989
1990                 if (eth->h_proto != htons(ETH_P_IP)) {
1991                         printk(KERN_WARNING
1992                                "isdn_net: %s don't know how to resolve type %d addresses?\n",
1993                                dev->name, (int) eth->h_proto);
1994                         memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
1995                         return 0;
1996                 }
1997                 /*
1998                  *      Try to get ARP to resolve the header.
1999                  */
2000 #ifdef CONFIG_INET
2001                 ret = arp_find(eth->h_dest, skb);
2002 #endif
2003         }
2004         return ret;
2005 }
2006
2007 /*
2008  * Interface-setup. (just after registering a new interface)
2009  */
2010 static int
2011 isdn_net_init(struct net_device *ndev)
2012 {
2013         ushort max_hlhdr_len = 0;
2014         isdn_net_local *lp = (isdn_net_local *) ndev->priv;
2015         int drvidx, i;
2016
2017         ether_setup(ndev);
2018         lp->org_hhc = ndev->hard_header_cache;
2019         lp->org_hcu = ndev->header_cache_update;
2020
2021         /* Setup the generic properties */
2022
2023         ndev->hard_header = NULL;
2024         ndev->hard_header_cache = NULL;
2025         ndev->header_cache_update = NULL;
2026         ndev->mtu = 1500;
2027         ndev->flags = IFF_NOARP|IFF_POINTOPOINT;
2028         ndev->type = ARPHRD_ETHER;
2029         ndev->addr_len = ETH_ALEN;
2030
2031         /* for clients with MPPP maybe higher values better */
2032         ndev->tx_queue_len = 30;
2033
2034         for (i = 0; i < ETH_ALEN; i++)
2035                 ndev->broadcast[i] = 0xff;
2036
2037         /* The ISDN-specific entries in the device structure. */
2038         ndev->open = &isdn_net_open;
2039         ndev->hard_start_xmit = &isdn_net_start_xmit;
2040
2041         /*
2042          *  up till binding we ask the protocol layer to reserve as much
2043          *  as we might need for HL layer
2044          */
2045
2046         for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
2047                 if (dev->drv[drvidx])
2048                         if (max_hlhdr_len < dev->drv[drvidx]->interface->hl_hdrlen)
2049                                 max_hlhdr_len = dev->drv[drvidx]->interface->hl_hdrlen;
2050
2051         ndev->hard_header_len = ETH_HLEN + max_hlhdr_len;
2052         ndev->stop = &isdn_net_close;
2053         ndev->get_stats = &isdn_net_get_stats;
2054         ndev->rebuild_header = &isdn_net_rebuild_header;
2055         ndev->do_ioctl = NULL;
2056         return 0;
2057 }
2058
2059 static void
2060 isdn_net_swapbind(int drvidx)
2061 {
2062         isdn_net_dev *p;
2063
2064 #ifdef ISDN_DEBUG_NET_ICALL
2065         printk(KERN_DEBUG "n_fi: swapping ch of %d\n", drvidx);
2066 #endif
2067         p = dev->netdev;
2068         while (p) {
2069                 if (p->local->pre_device == drvidx)
2070                         switch (p->local->pre_channel) {
2071                                 case 0:
2072                                         p->local->pre_channel = 1;
2073                                         break;
2074                                 case 1:
2075                                         p->local->pre_channel = 0;
2076                                         break;
2077                         }
2078                 p = (isdn_net_dev *) p->next;
2079         }
2080 }
2081
2082 static void
2083 isdn_net_swap_usage(int i1, int i2)
2084 {
2085         int u1 = dev->usage[i1] & ISDN_USAGE_EXCLUSIVE;
2086         int u2 = dev->usage[i2] & ISDN_USAGE_EXCLUSIVE;
2087
2088 #ifdef ISDN_DEBUG_NET_ICALL
2089         printk(KERN_DEBUG "n_fi: usage of %d and %d\n", i1, i2);
2090 #endif
2091         dev->usage[i1] &= ~ISDN_USAGE_EXCLUSIVE;
2092         dev->usage[i1] |= u2;
2093         dev->usage[i2] &= ~ISDN_USAGE_EXCLUSIVE;
2094         dev->usage[i2] |= u1;
2095         isdn_info_update();
2096 }
2097
2098 /*
2099  * An incoming call-request has arrived.
2100  * Search the interface-chain for an appropriate interface.
2101  * If found, connect the interface to the ISDN-channel and initiate
2102  * D- and B-Channel-setup. If secure-flag is set, accept only
2103  * configured phone-numbers. If callback-flag is set, initiate
2104  * callback-dialing.
2105  *
2106  * Return-Value: 0 = No appropriate interface for this call.
2107  *               1 = Call accepted
2108  *               2 = Reject call, wait cbdelay, then call back
2109  *               3 = Reject call
2110  *               4 = Wait cbdelay, then call back
2111  *               5 = No appropriate interface for this call,
2112  *                   would eventually match if CID was longer.
2113  */
2114
2115 int
2116 isdn_net_find_icall(int di, int ch, int idx, setup_parm *setup)
2117 {
2118         char *eaz;
2119         int si1;
2120         int si2;
2121         int ematch;
2122         int wret;
2123         int swapped;
2124         int sidx = 0;
2125         u_long flags;
2126         isdn_net_dev *p;
2127         isdn_net_phone *n;
2128         char nr[32];
2129         char *my_eaz;
2130
2131         /* Search name in netdev-chain */
2132         if (!setup->phone[0]) {
2133                 nr[0] = '0';
2134                 nr[1] = '\0';
2135                 printk(KERN_INFO "isdn_net: Incoming call without OAD, assuming '0'\n");
2136         } else
2137                 strcpy(nr, setup->phone);
2138         si1 = (int) setup->si1;
2139         si2 = (int) setup->si2;
2140         if (!setup->eazmsn[0]) {
2141                 printk(KERN_WARNING "isdn_net: Incoming call without CPN, assuming '0'\n");
2142                 eaz = "0";
2143         } else
2144                 eaz = setup->eazmsn;
2145         if (dev->net_verbose > 1)
2146                 printk(KERN_INFO "isdn_net: call from %s,%d,%d -> %s\n", nr, si1, si2, eaz);
2147         /* Accept DATA and VOICE calls at this stage
2148          * local eaz is checked later for allowed call types
2149          */
2150         if ((si1 != 7) && (si1 != 1)) {
2151                 if (dev->net_verbose > 1)
2152                         printk(KERN_INFO "isdn_net: Service-Indicator not 1 or 7, ignored\n");
2153                 return 0;
2154         }
2155         n = (isdn_net_phone *) 0;
2156         p = dev->netdev;
2157         ematch = wret = swapped = 0;
2158 #ifdef ISDN_DEBUG_NET_ICALL
2159         printk(KERN_DEBUG "n_fi: di=%d ch=%d idx=%d usg=%d\n", di, ch, idx,
2160                 dev->usage[idx]);
2161 #endif
2162         while (p) {
2163                 int matchret;
2164                 isdn_net_local *lp = p->local;
2165
2166                 /* If last check has triggered as binding-swap, revert it */
2167                 switch (swapped) {
2168                         case 2:
2169                                 isdn_net_swap_usage(idx, sidx);
2170                                 /* fall through */
2171                         case 1:
2172                                 isdn_net_swapbind(di);
2173                                 break;
2174                 }
2175                 swapped = 0;
2176                 /* check acceptable call types for DOV */
2177                 my_eaz = isdn_map_eaz2msn(lp->msn, di);
2178                 if (si1 == 1) { /* it's a DOV call, check if we allow it */
2179                         if (*my_eaz == 'v' || *my_eaz == 'V' ||
2180                             *my_eaz == 'b' || *my_eaz == 'B')
2181                                 my_eaz++; /* skip to allow a match */
2182                         else
2183                                 my_eaz = NULL; /* force non match */
2184                 } else { /* it's a DATA call, check if we allow it */
2185                         if (*my_eaz == 'b' || *my_eaz == 'B')
2186                                 my_eaz++; /* skip to allow a match */
2187                 }
2188                 if (my_eaz)
2189                         matchret = isdn_msncmp(eaz, my_eaz);
2190                 else
2191                         matchret = 1;
2192                 if (!matchret)
2193                         ematch = 1;
2194
2195                 /* Remember if more numbers eventually can match */
2196                 if (matchret > wret)
2197                         wret = matchret;
2198 #ifdef ISDN_DEBUG_NET_ICALL
2199                 printk(KERN_DEBUG "n_fi: if='%s', l.msn=%s, l.flags=%d, l.dstate=%d\n",
2200                        lp->name, lp->msn, lp->flags, lp->dialstate);
2201 #endif
2202                 if ((!matchret) &&                                        /* EAZ is matching   */
2203                     (((!(lp->flags & ISDN_NET_CONNECTED)) &&              /* but not connected */
2204                       (USG_NONE(dev->usage[idx]))) ||                     /* and ch. unused or */
2205                      ((((lp->dialstate == 4) || (lp->dialstate == 12)) && /* if dialing        */
2206                        (!(lp->flags & ISDN_NET_CALLBACK)))                /* but no callback   */
2207                      )))
2208                          {
2209 #ifdef ISDN_DEBUG_NET_ICALL
2210                         printk(KERN_DEBUG "n_fi: match1, pdev=%d pch=%d\n",
2211                                lp->pre_device, lp->pre_channel);
2212 #endif
2213                         if (dev->usage[idx] & ISDN_USAGE_EXCLUSIVE) {
2214                                 if ((lp->pre_channel != ch) ||
2215                                     (lp->pre_device != di)) {
2216                                         /* Here we got a problem:
2217                                          * If using an ICN-Card, an incoming call is always signaled on
2218                                          * on the first channel of the card, if both channels are
2219                                          * down. However this channel may be bound exclusive. If the
2220                                          * second channel is free, this call should be accepted.
2221                                          * The solution is horribly but it runs, so what:
2222                                          * We exchange the exclusive bindings of the two channels, the
2223                                          * corresponding variables in the interface-structs.
2224                                          */
2225                                         if (ch == 0) {
2226                                                 sidx = isdn_dc2minor(di, 1);
2227 #ifdef ISDN_DEBUG_NET_ICALL
2228                                                 printk(KERN_DEBUG "n_fi: ch is 0\n");
2229 #endif
2230                                                 if (USG_NONE(dev->usage[sidx])) {
2231                                                         /* Second Channel is free, now see if it is bound
2232                                                          * exclusive too. */
2233                                                         if (dev->usage[sidx] & ISDN_USAGE_EXCLUSIVE) {
2234 #ifdef ISDN_DEBUG_NET_ICALL
2235                                                                 printk(KERN_DEBUG "n_fi: 2nd channel is down and bound\n");
2236 #endif
2237                                                                 /* Yes, swap bindings only, if the original
2238                                                                  * binding is bound to channel 1 of this driver */
2239                                                                 if ((lp->pre_device == di) &&
2240                                                                     (lp->pre_channel == 1)) {
2241                                                                         isdn_net_swapbind(di);
2242                                                                         swapped = 1;
2243                                                                 } else {
2244                                                                         /* ... else iterate next device */
2245                                                                         p = (isdn_net_dev *) p->next;
2246                                                                         continue;
2247                                                                 }
2248                                                         } else {
2249 #ifdef ISDN_DEBUG_NET_ICALL
2250                                                                 printk(KERN_DEBUG "n_fi: 2nd channel is down and unbound\n");
2251 #endif
2252                                                                 /* No, swap always and swap excl-usage also */
2253                                                                 isdn_net_swap_usage(idx, sidx);
2254                                                                 isdn_net_swapbind(di);
2255                                                                 swapped = 2;
2256                                                         }
2257                                                         /* Now check for exclusive binding again */
2258 #ifdef ISDN_DEBUG_NET_ICALL
2259                                                         printk(KERN_DEBUG "n_fi: final check\n");
2260 #endif
2261                                                         if ((dev->usage[idx] & ISDN_USAGE_EXCLUSIVE) &&
2262                                                             ((lp->pre_channel != ch) ||
2263                                                              (lp->pre_device != di))) {
2264 #ifdef ISDN_DEBUG_NET_ICALL
2265                                                                 printk(KERN_DEBUG "n_fi: final check failed\n");
2266 #endif
2267                                                                 p = (isdn_net_dev *) p->next;
2268                                                                 continue;
2269                                                         }
2270                                                 }
2271                                         } else {
2272                                                 /* We are already on the second channel, so nothing to do */
2273 #ifdef ISDN_DEBUG_NET_ICALL
2274                                                 printk(KERN_DEBUG "n_fi: already on 2nd channel\n");
2275 #endif
2276                                         }
2277                                 }
2278                         }
2279 #ifdef ISDN_DEBUG_NET_ICALL
2280                         printk(KERN_DEBUG "n_fi: match2\n");
2281 #endif
2282                         n = lp->phone[0];
2283                         if (lp->flags & ISDN_NET_SECURE) {
2284                                 while (n) {
2285                                         if (!isdn_msncmp(nr, n->num))
2286                                                 break;
2287                                         n = (isdn_net_phone *) n->next;
2288                                 }
2289                         }
2290                         if (n || (!(lp->flags & ISDN_NET_SECURE))) {
2291 #ifdef ISDN_DEBUG_NET_ICALL
2292                                 printk(KERN_DEBUG "n_fi: match3\n");
2293 #endif
2294                                 /* matching interface found */
2295
2296                                 /*
2297                                  * Is the state STOPPED?
2298                                  * If so, no dialin is allowed,
2299                                  * so reject actively.
2300                                  * */
2301                                 if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
2302                                         printk(KERN_INFO "incoming call, interface %s `stopped' -> rejected\n",
2303                                                lp->name);
2304                                         return 3;
2305                                 }
2306                                 /*
2307                                  * Is the interface up?
2308                                  * If not, reject the call actively.
2309                                  */
2310                                 if (!isdn_net_device_started(p)) {
2311                                         printk(KERN_INFO "%s: incoming call, interface down -> rejected\n",
2312                                                lp->name);
2313                                         return 3;
2314                                 }
2315                                 /* Interface is up, now see if it's a slave. If so, see if
2316                                  * it's master and parent slave is online. If not, reject the call.
2317                                  */
2318                                 if (lp->master) {
2319                                         isdn_net_local *mlp = (isdn_net_local *) lp->master->priv;
2320                                         printk(KERN_DEBUG "ICALLslv: %s\n", lp->name);
2321                                         printk(KERN_DEBUG "master=%s\n", mlp->name);
2322                                         if (mlp->flags & ISDN_NET_CONNECTED) {
2323                                                 printk(KERN_DEBUG "master online\n");
2324                                                 /* Master is online, find parent-slave (master if first slave) */
2325                                                 while (mlp->slave) {
2326                                                         if ((isdn_net_local *) mlp->slave->priv == lp)
2327                                                                 break;
2328                                                         mlp = (isdn_net_local *) mlp->slave->priv;
2329                                                 }
2330                                         } else
2331                                                 printk(KERN_DEBUG "master offline\n");
2332                                         /* Found parent, if it's offline iterate next device */
2333                                         printk(KERN_DEBUG "mlpf: %d\n", mlp->flags & ISDN_NET_CONNECTED);
2334                                         if (!(mlp->flags & ISDN_NET_CONNECTED)) {
2335                                                 p = (isdn_net_dev *) p->next;
2336                                                 continue;
2337                                         }
2338                                 } 
2339                                 if (lp->flags & ISDN_NET_CALLBACK) {
2340                                         int chi;
2341                                         /*
2342                                          * Is the state MANUAL?
2343                                          * If so, no callback can be made,
2344                                          * so reject actively.
2345                                          * */
2346                                         if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
2347                                                 printk(KERN_INFO "incoming call for callback, interface %s `off' -> rejected\n",
2348                                                        lp->name);
2349                                                 return 3;
2350                                         }
2351                                         printk(KERN_DEBUG "%s: call from %s -> %s, start callback\n",
2352                                                lp->name, nr, eaz);
2353                                         if (lp->phone[1]) {
2354                                                 /* Grab a free ISDN-Channel */
2355                                                 spin_lock_irqsave(&dev->lock, flags);
2356                                                 if ((chi = 
2357                                                         isdn_get_free_channel(
2358                                                                 ISDN_USAGE_NET,
2359                                                                 lp->l2_proto,
2360                                                                 lp->l3_proto,
2361                                                                 lp->pre_device,
2362                                                                 lp->pre_channel,
2363                                                                 lp->msn)
2364                                                                 ) < 0) {
2365
2366                                                         printk(KERN_WARNING "isdn_net_find_icall: No channel for %s\n", lp->name);
2367                                                         spin_unlock_irqrestore(&dev->lock, flags);
2368                                                         return 0;
2369                                                 }
2370                                                 /* Setup dialstate. */
2371                                                 lp->dtimer = 0;
2372                                                 lp->dialstate = 11;
2373                                                 /* Connect interface with channel */
2374                                                 isdn_net_bind_channel(lp, chi);
2375 #ifdef CONFIG_ISDN_PPP
2376                                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2377                                                         if (isdn_ppp_bind(lp) < 0) {
2378                                                                 spin_unlock_irqrestore(&dev->lock, flags);
2379                                                                 isdn_net_unbind_channel(lp);
2380                                                                 return 0;
2381                                                         }
2382 #endif
2383                                                 spin_unlock_irqrestore(&dev->lock, flags);
2384                                                 /* Initiate dialing by returning 2 or 4 */
2385                                                 return (lp->flags & ISDN_NET_CBHUP) ? 2 : 4;
2386                                         } else
2387                                                 printk(KERN_WARNING "isdn_net: %s: No phone number\n", lp->name);
2388                                         return 0;
2389                                 } else {
2390                                         printk(KERN_DEBUG "%s: call from %s -> %s accepted\n", lp->name, nr,
2391                                                eaz);
2392                                         /* if this interface is dialing, it does it probably on a different
2393                                            device, so free this device */
2394                                         if ((lp->dialstate == 4) || (lp->dialstate == 12)) {
2395 #ifdef CONFIG_ISDN_PPP
2396                                                 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2397                                                         isdn_ppp_free(lp);
2398 #endif
2399                                                 isdn_net_lp_disconnected(lp);
2400                                                 isdn_free_channel(lp->isdn_device, lp->isdn_channel,
2401                                                          ISDN_USAGE_NET);
2402                                         }
2403                                         spin_lock_irqsave(&dev->lock, flags);
2404                                         dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
2405                                         dev->usage[idx] |= ISDN_USAGE_NET;
2406                                         strcpy(dev->num[idx], nr);
2407                                         isdn_info_update();
2408                                         dev->st_netdev[idx] = lp->netdev;
2409                                         lp->isdn_device = di;
2410                                         lp->isdn_channel = ch;
2411                                         lp->ppp_slot = -1;
2412                                         lp->flags |= ISDN_NET_CONNECTED;
2413                                         lp->dialstate = 7;
2414                                         lp->dtimer = 0;
2415                                         lp->outgoing = 0;
2416                                         lp->huptimer = 0;
2417                                         lp->hupflags |= ISDN_WAITCHARGE;
2418                                         lp->hupflags &= ~ISDN_HAVECHARGE;
2419 #ifdef CONFIG_ISDN_PPP
2420                                         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
2421                                                 if (isdn_ppp_bind(lp) < 0) {
2422                                                         isdn_net_unbind_channel(lp);
2423                                                         spin_unlock_irqrestore(&dev->lock, flags);
2424                                                         return 0;
2425                                                 }
2426                                         }
2427 #endif
2428                                         spin_unlock_irqrestore(&dev->lock, flags);
2429                                         return 1;
2430                                 }
2431                         }
2432                 }
2433                 p = (isdn_net_dev *) p->next;
2434         }
2435         /* If none of configured EAZ/MSN matched and not verbose, be silent */
2436         if (!ematch || dev->net_verbose)
2437                 printk(KERN_INFO "isdn_net: call from %s -> %d %s ignored\n", nr, di, eaz);
2438         return (wret == 2)?5:0;
2439 }
2440
2441 /*
2442  * Search list of net-interfaces for an interface with given name.
2443  */
2444 isdn_net_dev *
2445 isdn_net_findif(char *name)
2446 {
2447         isdn_net_dev *p = dev->netdev;
2448
2449         while (p) {
2450                 if (!strcmp(p->local->name, name))
2451                         return p;
2452                 p = (isdn_net_dev *) p->next;
2453         }
2454         return (isdn_net_dev *) NULL;
2455 }
2456
2457 /*
2458  * Force a net-interface to dial out.
2459  * This is called from the userlevel-routine below or
2460  * from isdn_net_start_xmit().
2461  */
2462 static int
2463 isdn_net_force_dial_lp(isdn_net_local * lp)
2464 {
2465         if ((!(lp->flags & ISDN_NET_CONNECTED)) && !lp->dialstate) {
2466                 int chi;
2467                 if (lp->phone[1]) {
2468                         ulong flags;
2469
2470                         /* Grab a free ISDN-Channel */
2471                         spin_lock_irqsave(&dev->lock, flags);
2472                         if ((chi = isdn_get_free_channel(
2473                                         ISDN_USAGE_NET,
2474                                         lp->l2_proto,
2475                                         lp->l3_proto,
2476                                         lp->pre_device,
2477                                         lp->pre_channel,
2478                                         lp->msn)) < 0) {
2479                                 printk(KERN_WARNING "isdn_net_force_dial: No channel for %s\n", lp->name);
2480                                 spin_unlock_irqrestore(&dev->lock, flags);
2481                                 return -EAGAIN;
2482                         }
2483                         lp->dialstate = 1;
2484                         /* Connect interface with channel */
2485                         isdn_net_bind_channel(lp, chi);
2486 #ifdef CONFIG_ISDN_PPP
2487                         if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2488                                 if (isdn_ppp_bind(lp) < 0) {
2489                                         isdn_net_unbind_channel(lp);
2490                                         spin_unlock_irqrestore(&dev->lock, flags);
2491                                         return -EAGAIN;
2492                                 }
2493 #endif
2494                         /* Initiate dialing */
2495                         spin_unlock_irqrestore(&dev->lock, flags);
2496                         isdn_net_dial();
2497                         return 0;
2498                 } else
2499                         return -EINVAL;
2500         } else
2501                 return -EBUSY;
2502 }
2503
2504 /*
2505  * This is called from certain upper protocol layers (multilink ppp
2506  * and x25iface encapsulation module) that want to initiate dialing
2507  * themselves.
2508  */
2509 int
2510 isdn_net_dial_req(isdn_net_local * lp)
2511 {
2512         /* is there a better error code? */
2513         if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) return -EBUSY;
2514
2515         return isdn_net_force_dial_lp(lp);
2516 }
2517
2518 /*
2519  * Force a net-interface to dial out.
2520  * This is always called from within userspace (ISDN_IOCTL_NET_DIAL).
2521  */
2522 int
2523 isdn_net_force_dial(char *name)
2524 {
2525         isdn_net_dev *p = isdn_net_findif(name);
2526
2527         if (!p)
2528                 return -ENODEV;
2529         return (isdn_net_force_dial_lp(p->local));
2530 }
2531
2532 /*
2533  * Allocate a new network-interface and initialize its data structures.
2534  */
2535 char *
2536 isdn_net_new(char *name, struct net_device *master)
2537 {
2538         isdn_net_dev *netdev;
2539
2540         /* Avoid creating an existing interface */
2541         if (isdn_net_findif(name)) {
2542                 printk(KERN_WARNING "isdn_net: interface %s already exists\n", name);
2543                 return NULL;
2544         }
2545         if (!(netdev = (isdn_net_dev *) kmalloc(sizeof(isdn_net_dev), GFP_KERNEL))) {
2546                 printk(KERN_WARNING "isdn_net: Could not allocate net-device\n");
2547                 return NULL;
2548         }
2549         memset(netdev, 0, sizeof(isdn_net_dev));
2550         if (!(netdev->local = (isdn_net_local *) kmalloc(sizeof(isdn_net_local), GFP_KERNEL))) {
2551                 printk(KERN_WARNING "isdn_net: Could not allocate device locals\n");
2552                 kfree(netdev);
2553                 return NULL;
2554         }
2555         memset(netdev->local, 0, sizeof(isdn_net_local));
2556         if (name == NULL)
2557                 strcpy(netdev->local->name, "         ");
2558         else
2559                 strcpy(netdev->local->name, name);
2560         strcpy(netdev->dev.name, netdev->local->name);
2561         netdev->dev.priv = netdev->local;
2562         netdev->dev.init = isdn_net_init;
2563         netdev->local->p_encap = ISDN_NET_ENCAP_RAWIP;
2564         if (master) {
2565                 /* Device shall be a slave */
2566                 struct net_device *p = (((isdn_net_local *) master->priv)->slave);
2567                 struct net_device *q = master;
2568
2569                 netdev->local->master = master;
2570                 /* Put device at end of slave-chain */
2571                 while (p) {
2572                         q = p;
2573                         p = (((isdn_net_local *) p->priv)->slave);
2574                 }
2575                 ((isdn_net_local *) q->priv)->slave = &(netdev->dev);
2576         } else {
2577                 /* Device shall be a master */
2578                 /*
2579                  * Watchdog timer (currently) for master only.
2580                  */
2581                 netdev->dev.tx_timeout = isdn_net_tx_timeout;
2582                 netdev->dev.watchdog_timeo = ISDN_NET_TX_TIMEOUT;
2583                 if (register_netdev(&netdev->dev) != 0) {
2584                         printk(KERN_WARNING "isdn_net: Could not register net-device\n");
2585                         kfree(netdev->local);
2586                         kfree(netdev);
2587                         return NULL;
2588                 }
2589         }
2590         netdev->local->magic = ISDN_NET_MAGIC;
2591
2592         netdev->queue = netdev->local;
2593         spin_lock_init(&netdev->queue_lock);
2594
2595         netdev->local->last = netdev->local;
2596         netdev->local->netdev = netdev;
2597         netdev->local->next = netdev->local;
2598
2599         INIT_WORK(&netdev->local->tqueue, (void *)(void *) isdn_net_softint, netdev->local);
2600         spin_lock_init(&netdev->local->xmit_lock);
2601
2602         netdev->local->isdn_device = -1;
2603         netdev->local->isdn_channel = -1;
2604         netdev->local->pre_device = -1;
2605         netdev->local->pre_channel = -1;
2606         netdev->local->exclusive = -1;
2607         netdev->local->ppp_slot = -1;
2608         netdev->local->pppbind = -1;
2609         skb_queue_head_init(&netdev->local->super_tx_queue);
2610         netdev->local->l2_proto = ISDN_PROTO_L2_X75I;
2611         netdev->local->l3_proto = ISDN_PROTO_L3_TRANS;
2612         netdev->local->triggercps = 6000;
2613         netdev->local->slavedelay = 10 * HZ;
2614         netdev->local->hupflags = ISDN_INHUP;   /* Do hangup even on incoming calls */
2615         netdev->local->onhtime = 10;    /* Default hangup-time for saving costs
2616            of those who forget configuring this */
2617         netdev->local->dialmax = 1;
2618         netdev->local->flags = ISDN_NET_CBHUP | ISDN_NET_DM_MANUAL;     /* Hangup before Callback, manual dial */
2619         netdev->local->cbdelay = 25;    /* Wait 5 secs before Callback */
2620         netdev->local->dialtimeout = -1;  /* Infinite Dial-Timeout */
2621         netdev->local->dialwait = 5 * HZ; /* Wait 5 sec. after failed dial */
2622         netdev->local->dialstarted = 0;   /* Jiffies of last dial-start */
2623         netdev->local->dialwait_timer = 0;  /* Jiffies of earliest next dial-start */
2624
2625         /* Put into to netdev-chain */
2626         netdev->next = (void *) dev->netdev;
2627         dev->netdev = netdev;
2628         return netdev->dev.name;
2629 }
2630
2631 char *
2632 isdn_net_newslave(char *parm)
2633 {
2634         char *p = strchr(parm, ',');
2635         isdn_net_dev *n;
2636         char newname[10];
2637
2638         if (p) {
2639                 /* Slave-Name MUST not be empty */
2640                 if (!strlen(p + 1))
2641                         return NULL;
2642                 strcpy(newname, p + 1);
2643                 *p = 0;
2644                 /* Master must already exist */
2645                 if (!(n = isdn_net_findif(parm)))
2646                         return NULL;
2647                 /* Master must be a real interface, not a slave */
2648                 if (n->local->master)
2649                         return NULL;
2650                 /* Master must not be started yet */
2651                 if (isdn_net_device_started(n)) 
2652                         return NULL;
2653                 return (isdn_net_new(newname, &(n->dev)));
2654         }
2655         return NULL;
2656 }
2657
2658 /*
2659  * Set interface-parameters.
2660  * Always set all parameters, so the user-level application is responsible
2661  * for not overwriting existing setups. It has to get the current
2662  * setup first, if only selected parameters are to be changed.
2663  */
2664 int
2665 isdn_net_setcfg(isdn_net_ioctl_cfg * cfg)
2666 {
2667         isdn_net_dev *p = isdn_net_findif(cfg->name);
2668         ulong features;
2669         int i;
2670         int drvidx;
2671         int chidx;
2672         char drvid[25];
2673
2674         if (p) {
2675                 isdn_net_local *lp = p->local;
2676
2677                 /* See if any registered driver supports the features we want */
2678                 features = ((1 << cfg->l2_proto) << ISDN_FEATURE_L2_SHIFT) |
2679                         ((1 << cfg->l3_proto) << ISDN_FEATURE_L3_SHIFT);
2680                 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2681                         if (dev->drv[i])
2682                                 if ((dev->drv[i]->interface->features & features) == features)
2683                                         break;
2684                 if (i == ISDN_MAX_DRIVERS) {
2685                         printk(KERN_WARNING "isdn_net: No driver with selected features\n");
2686                         return -ENODEV;
2687                 }
2688                 if (lp->p_encap != cfg->p_encap){
2689 #ifdef CONFIG_ISDN_X25
2690                         struct concap_proto * cprot = p -> cprot;
2691 #endif
2692                         if (isdn_net_device_started(p)) {
2693                                 printk(KERN_WARNING "%s: cannot change encap when if is up\n",
2694                                        lp->name);
2695                                 return -EBUSY;
2696                         }
2697 #ifdef CONFIG_ISDN_X25
2698                         if( cprot && cprot -> pops )
2699                                 cprot -> pops -> proto_del ( cprot );
2700                         p -> cprot = NULL;
2701                         lp -> dops = NULL;
2702                         /* ... ,  prepare for configuration of new one ... */
2703                         switch ( cfg -> p_encap ){
2704                         case ISDN_NET_ENCAP_X25IFACE:
2705                                 lp -> dops = &isdn_concap_reliable_dl_dops;
2706                         }
2707                         /* ... and allocate new one ... */
2708                         p -> cprot = isdn_concap_new( cfg -> p_encap );
2709                         /* p -> cprot == NULL now if p_encap is not supported
2710                            by means of the concap_proto mechanism */
2711                         /* the protocol is not configured yet; this will
2712                            happen later when isdn_net_reset() is called */
2713 #endif
2714                 }
2715                 switch ( cfg->p_encap ) {
2716                 case ISDN_NET_ENCAP_SYNCPPP:
2717 #ifndef CONFIG_ISDN_PPP
2718                         printk(KERN_WARNING "%s: SyncPPP support not configured\n",
2719                                lp->name);
2720                         return -EINVAL;
2721 #else
2722                         p->dev.type = ARPHRD_PPP;       /* change ARP type */
2723                         p->dev.addr_len = 0;
2724                         p->dev.do_ioctl = isdn_ppp_dev_ioctl;
2725 #endif
2726                         break;
2727                 case ISDN_NET_ENCAP_X25IFACE:
2728 #ifndef CONFIG_ISDN_X25
2729                         printk(KERN_WARNING "%s: isdn-x25 support not configured\n",
2730                                p->local->name);
2731                         return -EINVAL;
2732 #else
2733                         p->dev.type = ARPHRD_X25;       /* change ARP type */
2734                         p->dev.addr_len = 0;
2735 #endif
2736                         break;
2737                 case ISDN_NET_ENCAP_CISCOHDLCK:
2738                         p->dev.do_ioctl = isdn_ciscohdlck_dev_ioctl;
2739                         break;
2740                 default:
2741                         if( cfg->p_encap >= 0 &&
2742                             cfg->p_encap <= ISDN_NET_ENCAP_MAX_ENCAP )
2743                                 break;
2744                         printk(KERN_WARNING
2745                                "%s: encapsulation protocol %d not supported\n",
2746                                p->local->name, cfg->p_encap);
2747                         return -EINVAL;
2748                 }
2749                 if (strlen(cfg->drvid)) {
2750                         /* A bind has been requested ... */
2751                         char *c,
2752                         *e;
2753
2754                         drvidx = -1;
2755                         chidx = -1;
2756                         strcpy(drvid, cfg->drvid);
2757                         if ((c = strchr(drvid, ','))) {
2758                                 /* The channel-number is appended to the driver-Id with a comma */
2759                                 chidx = (int) simple_strtoul(c + 1, &e, 10);
2760                                 if (e == c)
2761                                         chidx = -1;
2762                                 *c = '\0';
2763                         }
2764                         for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2765                                 /* Lookup driver-Id in array */
2766                                 if (!(strcmp(dev->drvid[i], drvid))) {
2767                                         drvidx = i;
2768                                         break;
2769                                 }
2770                         if ((drvidx == -1) || (chidx == -1))
2771                                 /* Either driver-Id or channel-number invalid */
2772                                 return -ENODEV;
2773                 } else {
2774                         /* Parameters are valid, so get them */
2775                         drvidx = lp->pre_device;
2776                         chidx = lp->pre_channel;
2777                 }
2778                 if (cfg->exclusive > 0) {
2779                         unsigned long flags;
2780
2781                         /* If binding is exclusive, try to grab the channel */
2782                         spin_lock_irqsave(&dev->lock, flags);
2783                         if ((i = isdn_get_free_channel(ISDN_USAGE_NET,
2784                                 lp->l2_proto, lp->l3_proto, drvidx,
2785                                 chidx, lp->msn)) < 0) {
2786                                 /* Grab failed, because desired channel is in use */
2787                                 lp->exclusive = -1;
2788                                 spin_unlock_irqrestore(&dev->lock, flags);
2789                                 return -EBUSY;
2790                         }
2791                         /* All went ok, so update isdninfo */
2792                         dev->usage[i] = ISDN_USAGE_EXCLUSIVE;
2793                         isdn_info_update();
2794                         spin_unlock_irqrestore(&dev->lock, flags);
2795                         lp->exclusive = i;
2796                 } else {
2797                         /* Non-exclusive binding or unbind. */
2798                         lp->exclusive = -1;
2799                         if ((lp->pre_device != -1) && (cfg->exclusive == -1)) {
2800                                 isdn_unexclusive_channel(lp->pre_device, lp->pre_channel);
2801                                 isdn_free_channel(lp->pre_device, lp->pre_channel, ISDN_USAGE_NET);
2802                                 drvidx = -1;
2803                                 chidx = -1;
2804                         }
2805                 }
2806                 strcpy(lp->msn, cfg->eaz);
2807                 lp->pre_device = drvidx;
2808                 lp->pre_channel = chidx;
2809                 lp->onhtime = cfg->onhtime;
2810                 lp->charge = cfg->charge;
2811                 lp->l2_proto = cfg->l2_proto;
2812                 lp->l3_proto = cfg->l3_proto;
2813                 lp->cbdelay = cfg->cbdelay;
2814                 lp->dialmax = cfg->dialmax;
2815                 lp->triggercps = cfg->triggercps;
2816                 lp->slavedelay = cfg->slavedelay * HZ;
2817                 lp->pppbind = cfg->pppbind;
2818                 lp->dialtimeout = cfg->dialtimeout >= 0 ? cfg->dialtimeout * HZ : -1;
2819                 lp->dialwait = cfg->dialwait * HZ;
2820                 if (cfg->secure)
2821                         lp->flags |= ISDN_NET_SECURE;
2822                 else
2823                         lp->flags &= ~ISDN_NET_SECURE;
2824                 if (cfg->cbhup)
2825                         lp->flags |= ISDN_NET_CBHUP;
2826                 else
2827                         lp->flags &= ~ISDN_NET_CBHUP;
2828                 switch (cfg->callback) {
2829                         case 0:
2830                                 lp->flags &= ~(ISDN_NET_CALLBACK | ISDN_NET_CBOUT);
2831                                 break;
2832                         case 1:
2833                                 lp->flags |= ISDN_NET_CALLBACK;
2834                                 lp->flags &= ~ISDN_NET_CBOUT;
2835                                 break;
2836                         case 2:
2837                                 lp->flags |= ISDN_NET_CBOUT;
2838                                 lp->flags &= ~ISDN_NET_CALLBACK;
2839                                 break;
2840                 }
2841                 lp->flags &= ~ISDN_NET_DIALMODE_MASK;   /* first all bits off */
2842                 if (cfg->dialmode && !(cfg->dialmode & ISDN_NET_DIALMODE_MASK)) {
2843                         /* old isdnctrl version, where only 0 or 1 is given */
2844                         printk(KERN_WARNING
2845                              "Old isdnctrl version detected! Please update.\n");
2846                         lp->flags |= ISDN_NET_DM_OFF; /* turn on `off' bit */
2847                 }
2848                 else {
2849                         lp->flags |= cfg->dialmode;  /* turn on selected bits */
2850                 }
2851                 if (cfg->chargehup)
2852                         lp->hupflags |= ISDN_CHARGEHUP;
2853                 else
2854                         lp->hupflags &= ~ISDN_CHARGEHUP;
2855                 if (cfg->ihup)
2856                         lp->hupflags |= ISDN_INHUP;
2857                 else
2858                         lp->hupflags &= ~ISDN_INHUP;
2859                 if (cfg->chargeint > 10) {
2860                         lp->hupflags |= ISDN_CHARGEHUP | ISDN_HAVECHARGE | ISDN_MANCHARGE;
2861                         lp->chargeint = cfg->chargeint * HZ;
2862                 }
2863                 if (cfg->p_encap != lp->p_encap) {
2864                         if (cfg->p_encap == ISDN_NET_ENCAP_RAWIP) {
2865                                 p->dev.hard_header = NULL;
2866                                 p->dev.hard_header_cache = NULL;
2867                                 p->dev.header_cache_update = NULL;
2868                                 p->dev.flags = IFF_NOARP|IFF_POINTOPOINT;
2869                         } else {
2870                                 p->dev.hard_header = isdn_net_header;
2871                                 if (cfg->p_encap == ISDN_NET_ENCAP_ETHER) {
2872                                         p->dev.hard_header_cache = lp->org_hhc;
2873                                         p->dev.header_cache_update = lp->org_hcu;
2874                                         p->dev.flags = IFF_BROADCAST | IFF_MULTICAST;
2875                                 } else {
2876                                         p->dev.hard_header_cache = NULL;
2877                                         p->dev.header_cache_update = NULL;
2878                                         p->dev.flags = IFF_NOARP|IFF_POINTOPOINT;
2879                                 }
2880                         }
2881                 }
2882                 lp->p_encap = cfg->p_encap;
2883                 return 0;
2884         }
2885         return -ENODEV;
2886 }
2887
2888 /*
2889  * Perform get-interface-parameters.ioctl
2890  */
2891 int
2892 isdn_net_getcfg(isdn_net_ioctl_cfg * cfg)
2893 {
2894         isdn_net_dev *p = isdn_net_findif(cfg->name);
2895
2896         if (p) {
2897                 isdn_net_local *lp = p->local;
2898
2899                 strcpy(cfg->eaz, lp->msn);
2900                 cfg->exclusive = lp->exclusive;
2901                 if (lp->pre_device >= 0) {
2902                         sprintf(cfg->drvid, "%s,%d", dev->drvid[lp->pre_device],
2903                                 lp->pre_channel);
2904                 } else
2905                         cfg->drvid[0] = '\0';
2906                 cfg->onhtime = lp->onhtime;
2907                 cfg->charge = lp->charge;
2908                 cfg->l2_proto = lp->l2_proto;
2909                 cfg->l3_proto = lp->l3_proto;
2910                 cfg->p_encap = lp->p_encap;
2911                 cfg->secure = (lp->flags & ISDN_NET_SECURE) ? 1 : 0;
2912                 cfg->callback = 0;
2913                 if (lp->flags & ISDN_NET_CALLBACK)
2914                         cfg->callback = 1;
2915                 if (lp->flags & ISDN_NET_CBOUT)
2916                         cfg->callback = 2;
2917                 cfg->cbhup = (lp->flags & ISDN_NET_CBHUP) ? 1 : 0;
2918                 cfg->dialmode = lp->flags & ISDN_NET_DIALMODE_MASK;
2919                 cfg->chargehup = (lp->hupflags & 4) ? 1 : 0;
2920                 cfg->ihup = (lp->hupflags & 8) ? 1 : 0;
2921                 cfg->cbdelay = lp->cbdelay;
2922                 cfg->dialmax = lp->dialmax;
2923                 cfg->triggercps = lp->triggercps;
2924                 cfg->slavedelay = lp->slavedelay / HZ;
2925                 cfg->chargeint = (lp->hupflags & ISDN_CHARGEHUP) ?
2926                     (lp->chargeint / HZ) : 0;
2927                 cfg->pppbind = lp->pppbind;
2928                 cfg->dialtimeout = lp->dialtimeout >= 0 ? lp->dialtimeout / HZ : -1;
2929                 cfg->dialwait = lp->dialwait / HZ;
2930                 if (lp->slave)
2931                         strcpy(cfg->slave, ((isdn_net_local *) lp->slave->priv)->name);
2932                 else
2933                         cfg->slave[0] = '\0';
2934                 if (lp->master)
2935                         strcpy(cfg->master, ((isdn_net_local *) lp->master->priv)->name);
2936                 else
2937                         cfg->master[0] = '\0';
2938                 return 0;
2939         }
2940         return -ENODEV;
2941 }
2942
2943 /*
2944  * Add a phone-number to an interface.
2945  */
2946 int
2947 isdn_net_addphone(isdn_net_ioctl_phone * phone)
2948 {
2949         isdn_net_dev *p = isdn_net_findif(phone->name);
2950         isdn_net_phone *n;
2951
2952         if (p) {
2953                 if (!(n = (isdn_net_phone *) kmalloc(sizeof(isdn_net_phone), GFP_KERNEL)))
2954                         return -ENOMEM;
2955                 strcpy(n->num, phone->phone);
2956                 n->next = p->local->phone[phone->outgoing & 1];
2957                 p->local->phone[phone->outgoing & 1] = n;
2958                 return 0;
2959         }
2960         return -ENODEV;
2961 }
2962
2963 /*
2964  * Copy a string of all phone-numbers of an interface to user space.
2965  * This might sleep and must be called with the isdn semaphore down.
2966  */
2967 int
2968 isdn_net_getphones(isdn_net_ioctl_phone * phone, char __user *phones)
2969 {
2970         isdn_net_dev *p = isdn_net_findif(phone->name);
2971         int inout = phone->outgoing & 1;
2972         int more = 0;
2973         int count = 0;
2974         isdn_net_phone *n;
2975
2976         if (!p)
2977                 return -ENODEV;
2978         inout &= 1;
2979         for (n = p->local->phone[inout]; n; n = n->next) {
2980                 if (more) {
2981                         put_user(' ', phones++);
2982                         count++;
2983                 }
2984                 if (copy_to_user(phones, n->num, strlen(n->num) + 1)) {
2985                         return -EFAULT;
2986                 }
2987                 phones += strlen(n->num);
2988                 count += strlen(n->num);
2989                 more = 1;
2990         }
2991         put_user(0, phones);
2992         count++;
2993         return count;
2994 }
2995
2996 /*
2997  * Copy a string containing the peer's phone number of a connected interface
2998  * to user space.
2999  */
3000 int
3001 isdn_net_getpeer(isdn_net_ioctl_phone *phone, isdn_net_ioctl_phone __user *peer)
3002 {
3003         isdn_net_dev *p = isdn_net_findif(phone->name);
3004         int ch, dv, idx;
3005
3006         if (!p) return -ENODEV;
3007         /*
3008          * Theoretical race: while this executes, the remote number might
3009          * become invalid (hang up) or change (new connection), resulting
3010          * in (partially) wrong number copied to user. This race
3011          * currently ignored.
3012          */
3013         ch = p->local->isdn_channel;
3014         dv = p->local->isdn_device;
3015         if(ch<0 && dv<0) return -ENOTCONN;
3016         idx = isdn_dc2minor(dv, ch);
3017         if (idx<0) return -ENODEV;
3018         /* for pre-bound channels, we need this extra check */
3019         if ( strncmp(dev->num[idx],"???",3) == 0 ) return -ENOTCONN;
3020         strncpy(phone->phone,dev->num[idx],ISDN_MSNLEN);
3021         phone->outgoing=USG_OUTGOING(dev->usage[idx]);
3022         if ( copy_to_user(peer,phone,sizeof(*peer)) ) return -EFAULT;
3023         return 0;
3024 }
3025 /*
3026  * Delete a phone-number from an interface.
3027  */
3028 int
3029 isdn_net_delphone(isdn_net_ioctl_phone * phone)
3030 {
3031         isdn_net_dev *p = isdn_net_findif(phone->name);
3032         int inout = phone->outgoing & 1;
3033         isdn_net_phone *n;
3034         isdn_net_phone *m;
3035
3036         if (p) {
3037                 n = p->local->phone[inout];
3038                 m = NULL;
3039                 while (n) {
3040                         if (!strcmp(n->num, phone->phone)) {
3041                                 if (p->local->dial == n)
3042                                         p->local->dial = n->next;
3043                                 if (m)
3044                                         m->next = n->next;
3045                                 else
3046                                         p->local->phone[inout] = n->next;
3047                                 kfree(n);
3048                                 return 0;
3049                         }
3050                         m = n;
3051                         n = (isdn_net_phone *) n->next;
3052                 }
3053                 return -EINVAL;
3054         }
3055         return -ENODEV;
3056 }
3057
3058 /*
3059  * Delete all phone-numbers of an interface.
3060  */
3061 static int
3062 isdn_net_rmallphone(isdn_net_dev * p)
3063 {
3064         isdn_net_phone *n;
3065         isdn_net_phone *m;
3066         int i;
3067
3068         for (i = 0; i < 2; i++) {
3069                 n = p->local->phone[i];
3070                 while (n) {
3071                         m = n->next;
3072                         kfree(n);
3073                         n = m;
3074                 }
3075                 p->local->phone[i] = NULL;
3076         }
3077         p->local->dial = NULL;
3078         return 0;
3079 }
3080
3081 /*
3082  * Force a hangup of a network-interface.
3083  */
3084 int
3085 isdn_net_force_hangup(char *name)
3086 {
3087         isdn_net_dev *p = isdn_net_findif(name);
3088         struct net_device *q;
3089
3090         if (p) {
3091                 if (p->local->isdn_device < 0)
3092                         return 1;
3093                 q = p->local->slave;
3094                 /* If this interface has slaves, do a hangup for them also. */
3095                 while (q) {
3096                         isdn_net_hangup(q);
3097                         q = (((isdn_net_local *) q->priv)->slave);
3098                 }
3099                 isdn_net_hangup(&p->dev);
3100                 return 0;
3101         }
3102         return -ENODEV;
3103 }
3104
3105 /*
3106  * Helper-function for isdn_net_rm: Do the real work.
3107  */
3108 static int
3109 isdn_net_realrm(isdn_net_dev * p, isdn_net_dev * q)
3110 {
3111         u_long flags;
3112
3113         if (isdn_net_device_started(p)) {
3114                 return -EBUSY;
3115         }
3116 #ifdef CONFIG_ISDN_X25
3117         if( p -> cprot && p -> cprot -> pops )
3118                 p -> cprot -> pops -> proto_del ( p -> cprot );
3119 #endif
3120         /* Free all phone-entries */
3121         isdn_net_rmallphone(p);
3122         /* If interface is bound exclusive, free channel-usage */
3123         if (p->local->exclusive != -1)
3124                 isdn_unexclusive_channel(p->local->pre_device, p->local->pre_channel);
3125         if (p->local->master) {
3126                 /* It's a slave-device, so update master's slave-pointer if necessary */
3127                 if (((isdn_net_local *) (p->local->master->priv))->slave == &p->dev)
3128                         ((isdn_net_local *) (p->local->master->priv))->slave = p->local->slave;
3129         } else {
3130                 /* Unregister only if it's a master-device */
3131                 p->dev.hard_header_cache = p->local->org_hhc;
3132                 p->dev.header_cache_update = p->local->org_hcu;
3133                 unregister_netdev(&p->dev);
3134         }
3135         /* Unlink device from chain */
3136         spin_lock_irqsave(&dev->lock, flags);
3137         if (q)
3138                 q->next = p->next;
3139         else
3140                 dev->netdev = p->next;
3141         if (p->local->slave) {
3142                 /* If this interface has a slave, remove it also */
3143                 char *slavename = ((isdn_net_local *) (p->local->slave->priv))->name;
3144                 isdn_net_dev *n = dev->netdev;
3145                 q = NULL;
3146                 while (n) {
3147                         if (!strcmp(n->local->name, slavename)) {
3148                                 spin_unlock_irqrestore(&dev->lock, flags);
3149                                 isdn_net_realrm(n, q);
3150                                 spin_lock_irqsave(&dev->lock, flags);
3151                                 break;
3152                         }
3153                         q = n;
3154                         n = (isdn_net_dev *) n->next;
3155                 }
3156         }
3157         spin_unlock_irqrestore(&dev->lock, flags);
3158         /* If no more net-devices remain, disable auto-hangup timer */
3159         if (dev->netdev == NULL)
3160                 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 0);
3161         kfree(p->local);
3162         kfree(p);
3163
3164         return 0;
3165 }
3166
3167 /*
3168  * Remove a single network-interface.
3169  */
3170 int
3171 isdn_net_rm(char *name)
3172 {
3173         u_long flags;
3174         isdn_net_dev *p;
3175         isdn_net_dev *q;
3176
3177         /* Search name in netdev-chain */
3178         spin_lock_irqsave(&dev->lock, flags);
3179         p = dev->netdev;
3180         q = NULL;
3181         while (p) {
3182                 if (!strcmp(p->local->name, name)) {
3183                         spin_unlock_irqrestore(&dev->lock, flags);
3184                         return (isdn_net_realrm(p, q));
3185                 }
3186                 q = p;
3187                 p = (isdn_net_dev *) p->next;
3188         }
3189         spin_unlock_irqrestore(&dev->lock, flags);
3190         /* If no more net-devices remain, disable auto-hangup timer */
3191         if (dev->netdev == NULL)
3192                 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 0);
3193         return -ENODEV;
3194 }
3195
3196 /*
3197  * Remove all network-interfaces
3198  */
3199 int
3200 isdn_net_rmall(void)
3201 {
3202         u_long flags;
3203         int ret;
3204
3205         /* Walk through netdev-chain */
3206         spin_lock_irqsave(&dev->lock, flags);
3207         while (dev->netdev) {
3208                 if (!dev->netdev->local->master) {
3209                         /* Remove master-devices only, slaves get removed with their master */
3210                         spin_unlock_irqrestore(&dev->lock, flags);
3211                         if ((ret = isdn_net_realrm(dev->netdev, NULL))) {
3212                                 return ret;
3213                         }
3214                         spin_lock_irqsave(&dev->lock, flags);
3215                 }
3216         }
3217         dev->netdev = NULL;
3218         spin_unlock_irqrestore(&dev->lock, flags);
3219         return 0;
3220 }