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