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