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