* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / isdn / isdn_net.c
blob9d54f01274e82df77965a8cdd0a5135b892f7ee5
1 /* $Id: isdn_net.c,v 1.89 1999/08/22 20:26:03 calle Exp $
3 * Linux ISDN subsystem, network interfaces and related functions (linklevel).
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)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
12 * any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * $Log: isdn_net.c,v $
24 * Revision 1.89 1999/08/22 20:26:03 calle
25 * backported changes from kernel 2.3.14:
26 * - several #include "config.h" gone, others come.
27 * - "struct device" changed to "struct net_device" in 2.3.14, added a
28 * define in isdn_compat.h for older kernel versions.
30 * Revision 1.88 1999/07/07 10:13:31 detabc
31 * remove unused messages
33 * Revision 1.87 1999/07/06 07:53:53 calle
34 * calls to dev_alloc_skb waste 16 bytes of memory, if we calculate the
35 * right header space for the lowlevel driver. using alloc_skb instead.
37 * Revision 1.86 1999/06/09 10:12:05 paul
38 * thinko in previous patch
40 * Revision 1.85 1999/06/07 19:42:39 paul
41 * isdn_net_getpeer() fixed to return correct `outgoing' flag
43 * Revision 1.84 1999/04/18 14:06:55 fritz
44 * Removed TIMRU stuff.
46 * Revision 1.83 1999/04/12 12:33:23 fritz
47 * Changes from 2.0 tree.
49 * Revision 1.82 1999/01/17 00:55:58 he
50 * added mark_bh in BCONN statcallb and cleaned up some dead code
52 * Revision 1.81 1999/01/15 16:36:52 he
53 * replaced icmp_send() by dst_link_failure()
55 * Revision 1.80 1998/12/01 13:06:22 paul
56 * Also huptimeout with dialmode == manual
58 * Revision 1.79 1998/10/30 17:55:27 he
59 * dialmode for x25iface and multulink ppp
61 * Revision 1.78 1998/10/26 18:20:46 he
62 * re-inserted p=p->next in isdn_net_find_icall() (fixes kernel lock up
63 * on incoming call not matching the first interface)
65 * Revision 1.77 1998/10/23 10:18:44 paul
66 * Implementation of "dialmode" (successor of "status")
67 * You also need current isdnctrl for this!
69 * Revision 1.76 1998/09/07 22:00:05 he
70 * flush method for 2.1.118 and above
71 * updated IIOCTLNETGPN
73 * Revision 1.75 1998/08/31 21:09:50 he
74 * new ioctl IIOCNETGPN for /dev/isdninfo (get network interface'
75 * peer phone number)
77 * Revision 1.74 1998/07/30 11:28:32 paul
78 * printk message only appeared when status is off and interface is rawIP,
79 * which is confusing for people who don't know about "isdnctrl status <if> on".
81 * Revision 1.73 1998/06/26 22:01:37 keil
82 * tx_queue_len = 5 was too small
84 * Revision 1.72 1998/06/26 15:12:31 fritz
85 * Added handling of STAT_ICALL with incomplete CPN.
86 * Added AT&L for ttyI emulator.
87 * Added more locking stuff in tty_write.
89 * Revision 1.71 1998/06/18 22:43:08 fritz
90 * Bugfix: Setting ndev->do_ioctl had beed accidetly removed at cleanup.
92 * Revision 1.70 1998/06/17 19:50:49 he
93 * merged with 2.1.10[34] (cosmetics and udelay() -> mdelay())
94 * brute force fix to avoid Ugh's in isdn_tty_write()
95 * cleaned up some dead code
97 * Revision 1.69 1998/06/09 12:27:37 cal
98 * Changed default of local netdev flags: ISDN_NET_STOPPED is default now,
99 * so autodial is suppressed for that device until it is switched on using
100 * 'isdnctrl status dev-name on'.
104 * Revision 1.66 1998/05/26 22:39:24 he
105 * sync'ed with 2.1.102 where appropriate (CAPABILITY changes)
106 * concap typo
107 * cleared dev.tbusy in isdn_net BCONN status callback
109 * Revision 1.61 1998/04/16 19:19:42 keil
110 * Fix from vger (tx max qlength)
112 * Revision 1.60 1998/04/14 16:28:49 he
113 * Fixed user space access with interrupts off and remaining
114 * copy_{to,from}_user() -> -EFAULT return codes
116 * Revision 1.59 1998/03/07 22:37:33 fritz
117 * Bugfix: restore_flags missing.
119 * Revision 1.58 1998/03/07 18:21:05 cal
120 * Dynamic Timeout-Rule-Handling vs. 971110 included
122 * Revision 1.57 1998/02/25 18:31:13 fritz
123 * Added debugging output in adjust_header.
125 * Revision 1.56 1998/02/25 17:49:42 he
126 * Changed return codes caused be failing copy_{to,from}_user to -EFAULT
128 * Revision 1.55 1998/02/23 19:38:22 fritz
129 * Corrected check for modified feature-flags.
131 * Revision 1.54 1998/02/20 17:15:07 fritz
132 * Changes for recent kernels.
133 * Ugly workaround for adjusting Ethernet frames with recent kernels.
134 * replaced direct calls to lowlevel-driver command by common hook.
136 * Revision 1.53 1998/01/31 22:05:54 keil
137 * Lots of changes for X.25 support:
138 * Added generic support for connection-controlling encapsulation protocols
139 * Added support of BHUP status message
140 * Added support for additional p_encap X25IFACE
141 * Added support for kernels >= 2.1.72
143 * Revision 1.52 1998/01/31 19:29:51 calle
144 * Merged changes from and for 2.1.82, not tested only compiled ...
146 * Revision 1.51 1997/10/09 21:28:50 fritz
147 * New HL<->LL interface:
148 * New BSENT callback with nr. of bytes included.
149 * Sending without ACK.
150 * New L1 error status (not yet in use).
151 * Cleaned up obsolete structures.
152 * Implemented Cisco-SLARP.
153 * Changed local net-interface data to be dynamically allocated.
154 * Removed old 2.0 compatibility stuff.
156 * Revision 1.50 1997/10/01 09:20:32 fritz
157 * Removed old compatibility stuff for 2.0.X kernels.
158 * From now on, this code is for 2.1.X ONLY!
159 * Old stuff is still in the separate branch.
161 * Revision 1.49 1997/08/21 14:38:13 fritz
162 * Bugfix: Did not compile without SyncPPP.
164 * Revision 1.48 1997/06/22 11:57:15 fritz
165 * Added ability to adjust slave triggerlevel.
167 * Revision 1.47 1997/06/21 10:52:05 fritz
168 * Removed wrong SET_SKB_FREE in isdn_net_send_skb()
170 * Revision 1.46 1997/06/17 13:05:24 hipp
171 * Applied Eric's underflow-patches (slightly modified)
173 * Revision 1.45 1997/06/10 16:24:22 hipp
174 * hard_header changes for syncPPP (now behaves like RAWIP)
176 * Revision 1.44 1997/05/27 15:17:26 fritz
177 * Added changes for recent 2.1.x kernels:
178 * changed return type of isdn_close
179 * queue_task_* -> queue_task
180 * clear/set_bit -> test_and_... where apropriate.
181 * changed type of hard_header_cache parameter.
183 * Revision 1.43 1997/03/30 16:51:13 calle
184 * changed calls to copy_from_user/copy_to_user and removed verify_area
185 * were possible.
187 * Revision 1.42 1997/03/11 08:43:51 fritz
188 * Perform a hangup if number is deleted while dialing.
190 * Revision 1.41 1997/03/08 08:16:31 fritz
191 * Bugfix: Deleting a phone number during dial gave unpredictable results.
193 * Revision 1.40 1997/03/05 21:16:08 fritz
194 * Fix: did not compile with 2.1.27
196 * Revision 1.39 1997/03/04 21:36:52 fritz
197 * Added sending ICMP messages when no connetion is possible.
199 * Revision 1.38 1997/02/23 23:41:14 fritz
200 * Bugfix: Slave interfaces have to be hung up before master.
202 * Revision 1.37 1997/02/11 18:32:51 fritz
203 * Bugfix in isdn_ppp_free_mpqueue().
205 * Revision 1.36 1997/02/10 21:31:11 fritz
206 * Changed setup-interface (incoming and outgoing).
208 * Revision 1.35 1997/02/10 20:12:45 fritz
209 * Changed interface for reporting incoming calls.
211 * Revision 1.34 1997/02/03 23:15:07 fritz
212 * Reformatted according CodingStyle.
213 * replaced arp_find prototype by proper include.
214 * made dev_purge_queues static.
215 * Bugfix in bogocps calculation.
216 * removed isdn_net_receive_callback - was never used ;-)
217 * Misc. fixes for Kernel 2.1.X comaptibility.
219 * Revision 1.33 1997/01/17 01:19:25 fritz
220 * Applied chargeint patch.
222 * Revision 1.32 1997/01/14 01:29:31 fritz
223 * Bugfix: isdn_net_hangup() did not reset ISDN_NET_CONNECTED.
225 * Revision 1.31 1997/01/11 23:30:42 fritz
226 * Speed up dial statemachine.
228 * Revision 1.30 1996/11/25 17:20:50 hipp
229 * fixed pppbind bug in isdn_net_find_icall()
231 * Revision 1.29 1996/11/13 02:31:38 fritz
232 * Minor cleanup.
234 * Revision 1.28 1996/10/27 20:49:06 keil
235 * bugfix to compile without MPP
237 * Revision 1.27 1996/10/25 18:46:01 fritz
238 * Another bugfix in isdn_net_autohup()
240 * Revision 1.26 1996/10/23 23:05:36 fritz
241 * Bugfix: Divide by zero in isdn_net_autohup()
243 * Revision 1.25 1996/10/22 23:13:58 fritz
244 * Changes for compatibility to 2.0.X and 2.1.X kernels.
246 * Revision 1.24 1996/10/11 13:57:40 fritz
247 * Bugfix: Error in BogoCPS calculation.
249 * Revision 1.23 1996/09/23 01:58:08 fritz
250 * Fix: With syncPPP encapsulation, discard LCP packets
251 * when calculating hangup timeout.
253 * Revision 1.22 1996/09/23 00:03:37 fritz
254 * Fix: did not compile without CONFIG_ISDN_PPP
256 * Revision 1.21 1996/09/07 12:44:50 hipp
257 * (hopefully) fixed callback problem with syncPPP
258 * syncPPP network devices now show PPP link encap
260 * Revision 1.20 1996/08/29 20:06:03 fritz
261 * Bugfix: Transmission timeout had been much to low.
263 * Revision 1.19 1996/08/12 16:24:32 hipp
264 * removed some (now) obsolete functions for syncPPP in rebuild_header etc.
266 * Revision 1.18 1996/07/03 13:48:51 hipp
267 * bugfix: Call dev_purge_queues() only for master device
269 * Revision 1.17 1996/06/25 18:37:37 fritz
270 * Fixed return count for empty return string in isdn_net_getphones().
272 * Revision 1.16 1996/06/24 17:48:08 fritz
273 * Bugfixes:
274 * - Did not free channel on unbinding.
275 * - ioctl returned wrong callback settings.
277 * Revision 1.15 1996/06/16 17:42:54 tsbogend
278 * fixed problem with IP addresses on Linux/Alpha (long is 8 byte there)
280 * Revision 1.14 1996/06/11 14:54:08 hipp
281 * minor bugfix in isdn_net_send_skb
282 * changes in BSENT callback handler for syncPPP
283 * added lp->sav_skb stuff
285 * Revision 1.13 1996/06/06 14:25:44 fritz
286 * Changed loglevel of "incoming ... without OAD" message, since
287 * with audio support this is quite normal.
289 * Revision 1.12 1996/06/05 02:36:45 fritz
290 * Minor bugfixes by M. Hipp.
292 * Revision 1.11 1996/05/18 01:36:59 fritz
293 * Added spelling corrections and some minor changes
294 * to stay in sync with kernel.
296 * Revision 1.10 1996/05/17 03:49:01 fritz
297 * Some cleanup.
299 * Revision 1.9 1996/05/06 11:34:57 hipp
300 * fixed a few bugs
302 * Revision 1.8 1996/04/30 21:04:40 fritz
303 * Test commit
305 * Revision 1.7 1996/04/30 11:10:42 fritz
306 * Added Michael's ippp-bind patch.
308 * Revision 1.6 1996/04/30 09:34:35 fritz
309 * Removed compatibility-macros.
311 * Revision 1.5 1996/04/20 16:28:38 fritz
312 * Made more parameters of the dial statemachine user-configurable and
313 * added hangup after dial for more reliability using callback.
314 * Changed all io going through generic routines in isdn_common.c
315 * Added missing call to dev_free_skb on failed dialing.
316 * Added uihdlc encapsulation.
317 * Fixed isdn_net_setcfg not to destroy interface-flags anymore.
318 * Misc. typos.
320 * Revision 1.4 1996/02/19 15:23:38 fritz
321 * Bugfix: Sync-PPP packets got compressed twice, when resent due to
322 * send-queue-full reject.
324 * Revision 1.3 1996/02/11 02:22:28 fritz
325 * Changed status- receive-callbacks to use pointer-arrays for finding
326 * a corresponding interface instead of looping over all interfaces.
327 * Activate Auto-hangup-timer only when interface is online.
328 * Some bugfixes in the dialing-statemachine.
329 * Lot of bugfixes in sk_buff'ized encapsulation handling.
330 * For speedup connection-setup after dialing, remember sk_buf that triggered
331 * dialing.
332 * Fixed isdn_net_log_packet according to different encapsulations.
333 * Correct ARP-handling for ETHERNET-encapsulation.
335 * Revision 1.2 1996/01/22 05:05:12 fritz
336 * Changed returncode-logic for isdn_net_start_xmit() and its
337 * helper-functions.
338 * Changed handling of buildheader for RAWIP and ETHERNET-encapsulation.
340 * Revision 1.1 1996/01/09 04:12:34 fritz
341 * Initial revision
345 #include <linux/config.h>
346 #define __NO_VERSION__
347 #include <linux/module.h>
348 #include <linux/isdn.h>
349 #include <net/arp.h>
350 #include <net/dst.h>
351 #include <net/pkt_sched.h>
352 #include <linux/inetdevice.h>
353 #include "isdn_common.h"
354 #include "isdn_net.h"
355 #ifdef CONFIG_ISDN_PPP
356 #include "isdn_ppp.h"
357 #endif
358 #ifdef CONFIG_ISDN_X25
359 #include <linux/concap.h>
360 #include "isdn_concap.h"
361 #endif
363 /* Prototypes */
365 int isdn_net_force_dial_lp(isdn_net_local *);
366 static int isdn_net_start_xmit(struct sk_buff *, struct net_device *);
367 static int isdn_net_xmit(struct net_device *, isdn_net_local *, struct sk_buff *);
369 char *isdn_net_revision = "$Revision: 1.89 $";
372 * Code for raw-networking over ISDN
375 static void
376 isdn_net_unreachable(struct net_device *dev, struct sk_buff *skb, char *reason)
379 if(skb) {
381 u_short proto = ntohs(skb->protocol);
383 printk(KERN_DEBUG "isdn_net: %s: %s, signalling dst_link_failure %s\n",
384 dev->name,
385 (reason != NULL) ? reason : "unknown",
386 (proto != ETH_P_IP) ? "Protocol != ETH_P_IP" : "");
388 dst_link_failure(skb);
390 else { /* dial not triggered by rawIP packet */
391 printk(KERN_DEBUG "isdn_net: %s: %s\n",
392 dev->name,
393 (reason != NULL) ? reason : "reason unknown");
397 static void
398 isdn_net_reset(struct net_device *dev)
400 #ifdef CONFIG_ISDN_X25
401 struct concap_device_ops * dops =
402 ( (isdn_net_local *) dev->priv ) -> dops;
403 struct concap_proto * cprot =
404 ( (isdn_net_local *) dev->priv ) -> netdev -> cprot;
405 #endif
406 ulong flags;
408 save_flags(flags);
409 cli(); /* Avoid glitch on writes to CMD regs */
410 dev->interrupt = 0;
411 dev->tbusy = 0;
412 #ifdef CONFIG_ISDN_X25
413 if( cprot && cprot -> pops && dops )
414 cprot -> pops -> restart ( cprot, dev, dops );
415 #endif
416 restore_flags(flags);
419 /* Open/initialize the board. */
420 static int
421 isdn_net_open(struct net_device *dev)
423 int i;
424 struct net_device *p;
425 struct in_device *in_dev;
427 isdn_net_reset(dev);
428 dev->start = 1;
429 /* Fill in the MAC-level header (not needed, but for compatibility... */
430 for (i = 0; i < ETH_ALEN - sizeof(u32); i++)
431 dev->dev_addr[i] = 0xfc;
432 if ((in_dev = dev->ip_ptr) != NULL) {
434 * Any address will do - we take the first
436 struct in_ifaddr *ifa = in_dev->ifa_list;
437 if (ifa != NULL)
438 memcpy(dev->dev_addr+2, &ifa->ifa_local, 4);
441 /* If this interface has slaves, start them also */
443 if ((p = (((isdn_net_local *) dev->priv)->slave))) {
444 while (p) {
445 isdn_net_reset(p);
446 p->start = 1;
447 p = (((isdn_net_local *) p->priv)->slave);
450 isdn_MOD_INC_USE_COUNT();
451 return 0;
455 * Assign an ISDN-channel to a net-interface
457 static void
458 isdn_net_bind_channel(isdn_net_local * lp, int idx)
460 ulong flags;
462 save_flags(flags);
463 cli();
464 lp->flags |= ISDN_NET_CONNECTED;
465 lp->isdn_device = dev->drvmap[idx];
466 lp->isdn_channel = dev->chanmap[idx];
467 dev->rx_netdev[idx] = lp->netdev;
468 dev->st_netdev[idx] = lp->netdev;
469 restore_flags(flags);
473 * unbind a net-interface (resets interface after an error)
475 static void
476 isdn_net_unbind_channel(isdn_net_local * lp)
478 ulong flags;
480 save_flags(flags);
481 cli();
482 if (lp->first_skb) {
483 dev_kfree_skb(lp->first_skb);
484 lp->first_skb = NULL;
486 if (lp->sav_skb) {
487 dev_kfree_skb(lp->sav_skb);
488 lp->sav_skb = NULL;
490 if (!lp->master) { /* reset only master device */
491 /* Moral equivalent of dev_purge_queues():
492 BEWARE! This chunk of code cannot be called from hardware
493 interrupt handler. I hope it is true. --ANK
495 qdisc_reset(lp->netdev->dev.qdisc);
497 lp->dialstate = 0;
498 dev->rx_netdev[isdn_dc2minor(lp->isdn_device, lp->isdn_channel)] = NULL;
499 dev->st_netdev[isdn_dc2minor(lp->isdn_device, lp->isdn_channel)] = NULL;
500 isdn_free_channel(lp->isdn_device, lp->isdn_channel, ISDN_USAGE_NET);
501 lp->flags &= ~ISDN_NET_CONNECTED;
502 lp->isdn_device = -1;
503 lp->isdn_channel = -1;
505 restore_flags(flags);
509 * Perform auto-hangup and cps-calculation for net-interfaces.
511 * auto-hangup:
512 * Increment idle-counter (this counter is reset on any incoming or
513 * outgoing packet), if counter exceeds configured limit either do a
514 * hangup immediately or - if configured - wait until just before the next
515 * charge-info.
517 * cps-calculation (needed for dynamic channel-bundling):
518 * Since this function is called every second, simply reset the
519 * byte-counter of the interface after copying it to the cps-variable.
521 unsigned long last_jiffies = -HZ;
523 void
524 isdn_net_autohup()
526 isdn_net_dev *p = dev->netdev;
527 int anymore;
529 anymore = 0;
530 while (p) {
531 isdn_net_local *l = p->local;
532 if ((jiffies - last_jiffies) == 0)
533 l->cps = l->transcount;
534 else
535 l->cps = (l->transcount * HZ) / (jiffies - last_jiffies);
536 l->transcount = 0;
537 if (dev->net_verbose > 3)
538 printk(KERN_DEBUG "%s: %d bogocps\n", l->name, l->cps);
539 if ((l->flags & ISDN_NET_CONNECTED) && (!l->dialstate)) {
540 anymore = 1;
541 l->huptimer++;
543 * if there is some dialmode where timeout-hangup
544 * should _not_ be done, check for that here
546 if ((l->onhtime) &&
547 (l->huptimer > l->onhtime))
549 if (l->hupflags & ISDN_MANCHARGE &&
550 l->hupflags & ISDN_CHARGEHUP) {
551 while (jiffies - l->chargetime > l->chargeint)
552 l->chargetime += l->chargeint;
553 if (jiffies - l->chargetime >= l->chargeint - 2 * HZ)
554 if (l->outgoing || l->hupflags & ISDN_INHUP)
555 isdn_net_hangup(&p->dev);
556 } else if (l->outgoing) {
557 if (l->hupflags & ISDN_CHARGEHUP) {
558 if (l->hupflags & ISDN_WAITCHARGE) {
559 printk(KERN_DEBUG "isdn_net: Hupflags of %s are %X\n",
560 l->name, l->hupflags);
561 isdn_net_hangup(&p->dev);
562 } else if (jiffies - l->chargetime > l->chargeint) {
563 printk(KERN_DEBUG
564 "isdn_net: %s: chtime = %d, chint = %d\n",
565 l->name, l->chargetime, l->chargeint);
566 isdn_net_hangup(&p->dev);
568 } else
569 isdn_net_hangup(&p->dev);
570 } else if (l->hupflags & ISDN_INHUP)
571 isdn_net_hangup(&p->dev);
574 if(dev->global_flags & ISDN_GLOBAL_STOPPED || (ISDN_NET_DIALMODE(*l) == ISDN_NET_DM_OFF)) {
575 isdn_net_hangup(&p->dev);
576 break;
579 p = (isdn_net_dev *) p->next;
581 last_jiffies = jiffies;
582 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, anymore);
586 * Handle status-messages from ISDN-interfacecard.
587 * This function is called from within the main-status-dispatcher
588 * isdn_status_callback, which itself is called from the low-level driver.
589 * Return: 1 = Event handled, 0 = not for us or unknown Event.
592 isdn_net_stat_callback(int idx, isdn_ctrl *c)
594 isdn_net_dev *p = dev->st_netdev[idx];
595 int cmd = c->command;
597 if (p) {
598 isdn_net_local *lp = p->local;
599 #ifdef CONFIG_ISDN_X25
600 struct concap_proto *cprot = lp -> netdev -> cprot;
601 struct concap_proto_ops *pops = cprot ? cprot -> pops : 0;
602 #endif
603 switch (cmd) {
604 case ISDN_STAT_BSENT:
605 /* A packet has successfully been sent out */
606 if ((lp->flags & ISDN_NET_CONNECTED) &&
607 (!lp->dialstate)) {
608 lp->stats.tx_packets++;
609 lp->stats.tx_bytes += c->parm.length;
610 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP && lp->sav_skb) {
611 struct net_device *mdev;
612 if (lp->master)
613 mdev = lp->master;
614 else
615 mdev = &lp->netdev->dev;
616 if (!isdn_net_send_skb(mdev, lp, lp->sav_skb)) {
617 lp->sav_skb = NULL;
618 mark_bh(NET_BH);
619 } else {
620 return 1;
623 if (test_and_clear_bit(0, (void *) &(p->dev.tbusy)))
624 mark_bh(NET_BH);
626 return 1;
627 case ISDN_STAT_DCONN:
628 /* D-Channel is up */
629 switch (lp->dialstate) {
630 case 4:
631 case 7:
632 case 8:
633 lp->dialstate++;
634 return 1;
635 case 12:
636 lp->dialstate = 5;
637 return 1;
639 break;
640 case ISDN_STAT_DHUP:
641 /* Either D-Channel-hangup or error during dialout */
642 #ifdef CONFIG_ISDN_X25
643 /* If we are not connencted then dialing had
644 failed. If there are generic encap protocol
645 receiver routines signal the closure of
646 the link*/
648 if( !(lp->flags & ISDN_NET_CONNECTED)
649 && pops && pops -> disconn_ind )
650 pops -> disconn_ind(cprot);
651 #endif /* CONFIG_ISDN_X25 */
652 if ((!lp->dialstate) && (lp->flags & ISDN_NET_CONNECTED)) {
653 #ifdef CONFIG_ISDN_PPP
654 isdn_ppp_free(lp);
655 #endif
656 isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
657 printk(KERN_INFO "%s: remote hangup\n", lp->name);
658 printk(KERN_INFO "%s: Chargesum is %d\n", lp->name,
659 lp->charge);
660 isdn_net_unbind_channel(lp);
661 return 1;
663 break;
664 #ifdef CONFIG_ISDN_X25
665 case ISDN_STAT_BHUP:
666 /* B-Channel-hangup */
667 /* try if there are generic encap protocol
668 receiver routines and signal the closure of
669 the link */
670 if( pops && pops -> disconn_ind ){
671 pops -> disconn_ind(cprot);
672 return 1;
674 break;
675 #endif /* CONFIG_ISDN_X25 */
676 case ISDN_STAT_BCONN:
677 /* B-Channel is up */
678 switch (lp->dialstate) {
679 case 5:
680 case 6:
681 case 7:
682 case 8:
683 case 9:
684 case 10:
685 case 12:
686 if (lp->dialstate <= 6) {
687 dev->usage[idx] |= ISDN_USAGE_OUTGOING;
688 isdn_info_update();
689 } else
690 dev->rx_netdev[idx] = p;
691 lp->dialstate = 0;
692 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 1);
693 if (lp->p_encap == ISDN_NET_ENCAP_CISCOHDLCK)
694 isdn_timer_ctrl(ISDN_TIMER_KEEPALIVE, 1);
695 printk(KERN_INFO "isdn_net: %s connected\n", lp->name);
696 /* If first Chargeinfo comes before B-Channel connect,
697 * we correct the timestamp here.
699 lp->chargetime = jiffies;
700 printk(KERN_DEBUG "isdn_net: chargetime of %s now %d\n",
701 lp->name, lp->chargetime);
703 /* reset dial-timeout */
704 lp->dialstarted = 0;
705 lp->dialwait_timer = 0;
707 /* Immediately send first skb to speed up arp */
708 #ifdef CONFIG_ISDN_PPP
709 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
710 isdn_ppp_wakeup_daemon(lp);
711 #endif
712 #ifdef CONFIG_ISDN_X25
713 /* try if there are generic concap receiver routines */
714 if( pops )
715 if( pops->connect_ind)
716 pops->connect_ind(cprot);
717 #endif /* CONFIG_ISDN_X25 */
718 if (lp->first_skb) {
720 if (!(isdn_net_xmit(&p->dev, lp, lp->first_skb)))
721 lp->first_skb = NULL;
723 else {
725 * dev.tbusy is usually cleared implicitly by isdn_net_xmit(,,lp->first_skb).
726 * With an empty lp->first_skb, we need to do this ourselves
728 lp->netdev->dev.tbusy = 0;
729 mark_bh(NET_BH);
731 return 1;
733 break;
734 case ISDN_STAT_NODCH:
735 /* No D-Channel avail. */
736 if (lp->dialstate == 4) {
737 lp->dialstate--;
738 return 1;
740 break;
741 case ISDN_STAT_CINF:
742 /* Charge-info from TelCo. Calculate interval between
743 * charge-infos and set timestamp for last info for
744 * usage by isdn_net_autohup()
746 lp->charge++;
747 if (lp->hupflags & ISDN_HAVECHARGE) {
748 lp->hupflags &= ~ISDN_WAITCHARGE;
749 lp->chargeint = jiffies - lp->chargetime - (2 * HZ);
751 if (lp->hupflags & ISDN_WAITCHARGE)
752 lp->hupflags |= ISDN_HAVECHARGE;
753 lp->chargetime = jiffies;
754 printk(KERN_DEBUG "isdn_net: Got CINF chargetime of %s now %d\n",
755 lp->name, lp->chargetime);
756 return 1;
759 return 0;
763 * Check, if a number contains wildcard-characters, in which case it
764 * is for incoming purposes only.
766 static int
767 isdn_net_checkwild(char *num)
769 return ((strchr(num, '?')) ||
770 (strchr(num, '*')) ||
771 (strchr(num, '[')) ||
772 (strchr(num, ']')) ||
773 (strchr(num, '^')));
777 * Perform dialout for net-interfaces and timeout-handling for
778 * D-Channel-up and B-Channel-up Messages.
779 * This function is initially called from within isdn_net_start_xmit() or
780 * or isdn_net_find_icall() after initializing the dialstate for an
781 * interface. If further calls are needed, the function schedules itself
782 * for a timer-callback via isdn_timer_function().
783 * The dialstate is also affected by incoming status-messages from
784 * the ISDN-Channel which are handled in isdn_net_stat_callback() above.
786 void
787 isdn_net_dial(void)
789 isdn_net_dev *p = dev->netdev;
790 int anymore = 0;
791 int i;
792 int flags;
793 isdn_ctrl cmd;
795 while (p) {
796 isdn_net_local *lp = p->local;
798 #ifdef ISDN_DEBUG_NET_DIAL
799 if (lp->dialstate)
800 printk(KERN_DEBUG "%s: dialstate=%d\n", lp->name, lp->dialstate);
801 #endif
802 switch (lp->dialstate) {
803 case 0:
804 /* Nothing to do for this interface */
805 break;
806 case 1:
807 /* Initiate dialout. Set phone-number-pointer to first number
808 * of interface.
810 save_flags(flags);
811 cli();
812 lp->dial = lp->phone[1];
813 restore_flags(flags);
814 if (!lp->dial) {
815 printk(KERN_WARNING "%s: phone number deleted?\n",
816 lp->name);
817 isdn_net_hangup(&p->dev);
818 break;
820 anymore = 1;
822 if(lp->dialtimeout > 0)
823 if(lp->dialstarted == 0 || jiffies > (lp->dialstarted + lp->dialtimeout + lp->dialwait)) {
824 lp->dialstarted = jiffies;
825 lp->dialwait_timer = 0;
828 lp->dialstate++;
829 /* Fall through */
830 case 2:
831 /* Prepare dialing. Clear EAZ, then set EAZ. */
832 cmd.driver = lp->isdn_device;
833 cmd.arg = lp->isdn_channel;
834 cmd.command = ISDN_CMD_CLREAZ;
835 isdn_command(&cmd);
836 sprintf(cmd.parm.num, "%s", isdn_map_eaz2msn(lp->msn, cmd.driver));
837 cmd.command = ISDN_CMD_SETEAZ;
838 isdn_command(&cmd);
839 lp->dialretry = 0;
840 anymore = 1;
841 lp->dialstate++;
842 /* Fall through */
843 case 3:
844 /* Setup interface, dial current phone-number, switch to next number.
845 * If list of phone-numbers is exhausted, increment
846 * retry-counter.
848 if(dev->global_flags & ISDN_GLOBAL_STOPPED || (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF)) {
849 char *s;
850 if (dev->global_flags & ISDN_GLOBAL_STOPPED)
851 s = "dial suppressed: isdn system stopped";
852 else
853 s = "dial suppressed: dialmode `off'";
854 isdn_net_unreachable(&p->dev, lp->first_skb, s);
855 isdn_net_hangup(&p->dev);
856 break;
858 cmd.driver = lp->isdn_device;
859 cmd.command = ISDN_CMD_SETL2;
860 cmd.arg = lp->isdn_channel + (lp->l2_proto << 8);
861 isdn_command(&cmd);
862 cmd.driver = lp->isdn_device;
863 cmd.command = ISDN_CMD_SETL3;
864 cmd.arg = lp->isdn_channel + (lp->l3_proto << 8);
865 isdn_command(&cmd);
866 cmd.driver = lp->isdn_device;
867 cmd.arg = lp->isdn_channel;
868 save_flags(flags);
869 cli();
870 if (!lp->dial) {
871 restore_flags(flags);
872 printk(KERN_WARNING "%s: phone number deleted?\n",
873 lp->name);
874 isdn_net_hangup(&p->dev);
875 break;
877 if (!strcmp(lp->dial->num, "LEASED")) {
878 restore_flags(flags);
879 lp->dialstate = 4;
880 printk(KERN_INFO "%s: Open leased line ...\n", lp->name);
881 } else {
882 if(lp->dialtimeout > 0)
883 if(jiffies > (lp->dialstarted + lp->dialtimeout)) {
884 restore_flags(flags);
885 lp->dialwait_timer = jiffies + lp->dialwait;
886 lp->dialstarted = 0;
887 isdn_net_unreachable(&p->dev, lp->first_skb, "dial: timed out");
888 isdn_net_hangup(&p->dev);
889 break;
892 sprintf(cmd.parm.setup.phone, "%s", lp->dial->num);
894 * Switch to next number or back to start if at end of list.
896 if (!(lp->dial = (isdn_net_phone *) lp->dial->next)) {
897 lp->dial = lp->phone[1];
898 lp->dialretry++;
900 if (lp->dialretry > lp->dialmax) {
901 restore_flags(flags);
902 if (lp->dialtimeout == 0) {
903 lp->dialwait_timer = jiffies + lp->dialwait;
904 lp->dialstarted = 0;
905 isdn_net_unreachable(&p->dev, lp->first_skb, "dial: tried all numbers dialmax times");
907 isdn_net_hangup(&p->dev);
908 break;
911 restore_flags(flags);
912 cmd.driver = lp->isdn_device;
913 cmd.command = ISDN_CMD_DIAL;
914 cmd.parm.setup.si1 = 7;
915 cmd.parm.setup.si2 = 0;
916 sprintf(cmd.parm.setup.eazmsn, "%s",
917 isdn_map_eaz2msn(lp->msn, cmd.driver));
918 i = isdn_dc2minor(lp->isdn_device, lp->isdn_channel);
919 if (i >= 0) {
920 strcpy(dev->num[i], cmd.parm.setup.phone);
921 isdn_info_update();
923 printk(KERN_INFO "%s: dialing %d %s...\n", lp->name,
924 lp->dialretry, cmd.parm.setup.phone);
925 lp->dtimer = 0;
926 #ifdef ISDN_DEBUG_NET_DIAL
927 printk(KERN_DEBUG "dial: d=%d c=%d\n", lp->isdn_device,
928 lp->isdn_channel);
929 #endif
930 isdn_command(&cmd);
932 lp->huptimer = 0;
933 lp->outgoing = 1;
934 if (lp->chargeint) {
935 lp->hupflags |= ISDN_HAVECHARGE;
936 lp->hupflags &= ~ISDN_WAITCHARGE;
937 } else {
938 lp->hupflags |= ISDN_WAITCHARGE;
939 lp->hupflags &= ~ISDN_HAVECHARGE;
941 anymore = 1;
942 lp->dialstate =
943 (lp->cbdelay &&
944 (lp->flags & ISDN_NET_CBOUT)) ? 12 : 4;
945 break;
946 case 4:
947 /* Wait for D-Channel-connect.
948 * If timeout, switch back to state 3.
949 * Dialmax-handling moved to state 3.
951 if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
952 lp->dialstate = 3;
953 anymore = 1;
954 break;
955 case 5:
956 /* Got D-Channel-Connect, send B-Channel-request */
957 cmd.driver = lp->isdn_device;
958 cmd.arg = lp->isdn_channel;
959 cmd.command = ISDN_CMD_ACCEPTB;
960 anymore = 1;
961 lp->dtimer = 0;
962 lp->dialstate++;
963 isdn_command(&cmd);
964 break;
965 case 6:
966 /* Wait for B- or D-Channel-connect. If timeout,
967 * switch back to state 3.
969 #ifdef ISDN_DEBUG_NET_DIAL
970 printk(KERN_DEBUG "dialtimer2: %d\n", lp->dtimer);
971 #endif
972 if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
973 lp->dialstate = 3;
974 anymore = 1;
975 break;
976 case 7:
977 /* Got incoming Call, setup L2 and L3 protocols,
978 * then wait for D-Channel-connect
980 #ifdef ISDN_DEBUG_NET_DIAL
981 printk(KERN_DEBUG "dialtimer4: %d\n", lp->dtimer);
982 #endif
983 cmd.driver = lp->isdn_device;
984 cmd.command = ISDN_CMD_SETL2;
985 cmd.arg = lp->isdn_channel + (lp->l2_proto << 8);
986 isdn_command(&cmd);
987 cmd.driver = lp->isdn_device;
988 cmd.command = ISDN_CMD_SETL3;
989 cmd.arg = lp->isdn_channel + (lp->l3_proto << 8);
990 isdn_command(&cmd);
991 if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT15)
992 isdn_net_hangup(&p->dev);
993 else {
994 anymore = 1;
995 lp->dialstate++;
997 break;
998 case 9:
999 /* Got incoming D-Channel-Connect, send B-Channel-request */
1000 cmd.driver = lp->isdn_device;
1001 cmd.arg = lp->isdn_channel;
1002 cmd.command = ISDN_CMD_ACCEPTB;
1003 isdn_command(&cmd);
1004 anymore = 1;
1005 lp->dtimer = 0;
1006 lp->dialstate++;
1007 break;
1008 case 8:
1009 case 10:
1010 /* Wait for B- or D-channel-connect */
1011 #ifdef ISDN_DEBUG_NET_DIAL
1012 printk(KERN_DEBUG "dialtimer4: %d\n", lp->dtimer);
1013 #endif
1014 if (lp->dtimer++ > ISDN_TIMER_DTIMEOUT10)
1015 isdn_net_hangup(&p->dev);
1016 else
1017 anymore = 1;
1018 break;
1019 case 11:
1020 /* Callback Delay */
1021 if (lp->dtimer++ > lp->cbdelay)
1022 lp->dialstate = 1;
1023 anymore = 1;
1024 break;
1025 case 12:
1026 /* Remote does callback. Hangup after cbdelay, then wait for incoming
1027 * call (in state 4).
1029 if (lp->dtimer++ > lp->cbdelay)
1031 printk(KERN_INFO "%s: hangup waiting for callback ...\n", lp->name);
1032 lp->dtimer = 0;
1033 lp->dialstate = 4;
1034 cmd.driver = lp->isdn_device;
1035 cmd.command = ISDN_CMD_HANGUP;
1036 cmd.arg = lp->isdn_channel;
1037 isdn_command(&cmd);
1038 isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
1040 anymore = 1;
1041 break;
1042 default:
1043 printk(KERN_WARNING "isdn_net: Illegal dialstate %d for device %s\n",
1044 lp->dialstate, lp->name);
1046 p = (isdn_net_dev *) p->next;
1048 isdn_timer_ctrl(ISDN_TIMER_NETDIAL, anymore);
1052 * Perform hangup for a net-interface.
1054 void
1055 isdn_net_hangup(struct net_device *d)
1057 isdn_net_local *lp = (isdn_net_local *) d->priv;
1058 isdn_ctrl cmd;
1059 #ifdef CONFIG_ISDN_X25
1060 struct concap_proto *cprot = lp -> netdev -> cprot;
1061 struct concap_proto_ops *pops = cprot ? cprot -> pops : 0;
1062 #endif
1064 if (lp->flags & ISDN_NET_CONNECTED) {
1065 printk(KERN_INFO "isdn_net: local hangup %s\n", lp->name);
1066 #ifdef CONFIG_ISDN_PPP
1067 isdn_ppp_free(lp);
1068 #endif
1069 #ifdef CONFIG_ISDN_X25
1070 /* try if there are generic encap protocol
1071 receiver routines and signal the closure of
1072 the link */
1073 if( pops && pops -> disconn_ind )
1074 pops -> disconn_ind(cprot);
1075 #endif /* CONFIG_ISDN_X25 */
1077 cmd.driver = lp->isdn_device;
1078 cmd.command = ISDN_CMD_HANGUP;
1079 cmd.arg = lp->isdn_channel;
1080 isdn_command(&cmd);
1081 printk(KERN_INFO "%s: Chargesum is %d\n", lp->name, lp->charge);
1082 isdn_all_eaz(lp->isdn_device, lp->isdn_channel);
1084 isdn_net_unbind_channel(lp);
1087 typedef struct {
1088 unsigned short source;
1089 unsigned short dest;
1090 } ip_ports;
1092 static void
1093 isdn_net_log_skb(struct sk_buff * skb, isdn_net_local * lp)
1095 u_char *p = skb->nh.raw; /* hopefully, this was set correctly */
1096 unsigned short proto = ntohs(skb->protocol);
1097 int data_ofs;
1098 ip_ports *ipp;
1099 char addinfo[100];
1101 addinfo[0] = '\0';
1102 /* This check stolen from 2.1.72 dev_queue_xmit_nit() */
1103 if (skb->nh.raw < skb->data || skb->nh.raw >= skb->tail) {
1104 /* fall back to old isdn_net_log_packet method() */
1105 char * buf = skb->data;
1107 printk(KERN_DEBUG "isdn_net: protocol %04x is buggy, dev %s\n", skb->protocol, lp->name);
1108 p = buf;
1109 proto = ETH_P_IP;
1110 switch (lp->p_encap) {
1111 case ISDN_NET_ENCAP_IPTYP:
1112 proto = ntohs(*(unsigned short *) &buf[0]);
1113 p = &buf[2];
1114 break;
1115 case ISDN_NET_ENCAP_ETHER:
1116 proto = ntohs(*(unsigned short *) &buf[12]);
1117 p = &buf[14];
1118 break;
1119 case ISDN_NET_ENCAP_CISCOHDLC:
1120 proto = ntohs(*(unsigned short *) &buf[2]);
1121 p = &buf[4];
1122 break;
1123 #ifdef CONFIG_ISDN_PPP
1124 case ISDN_NET_ENCAP_SYNCPPP:
1125 proto = ntohs(skb->protocol);
1126 p = &buf[IPPP_MAX_HEADER];
1127 break;
1128 #endif
1131 data_ofs = ((p[0] & 15) * 4);
1132 switch (proto) {
1133 case ETH_P_IP:
1134 switch (p[9]) {
1135 case 1:
1136 strcpy(addinfo, " ICMP");
1137 break;
1138 case 2:
1139 strcpy(addinfo, " IGMP");
1140 break;
1141 case 4:
1142 strcpy(addinfo, " IPIP");
1143 break;
1144 case 6:
1145 ipp = (ip_ports *) (&p[data_ofs]);
1146 sprintf(addinfo, " TCP, port: %d -> %d", ntohs(ipp->source),
1147 ntohs(ipp->dest));
1148 break;
1149 case 8:
1150 strcpy(addinfo, " EGP");
1151 break;
1152 case 12:
1153 strcpy(addinfo, " PUP");
1154 break;
1155 case 17:
1156 ipp = (ip_ports *) (&p[data_ofs]);
1157 sprintf(addinfo, " UDP, port: %d -> %d", ntohs(ipp->source),
1158 ntohs(ipp->dest));
1159 break;
1160 case 22:
1161 strcpy(addinfo, " IDP");
1162 break;
1164 printk(KERN_INFO "OPEN: %d.%d.%d.%d -> %d.%d.%d.%d%s\n",
1165 p[12], p[13], p[14], p[15],
1166 p[16], p[17], p[18], p[19],
1167 addinfo);
1168 break;
1169 case ETH_P_ARP:
1170 printk(KERN_INFO "OPEN: ARP %d.%d.%d.%d -> *.*.*.* ?%d.%d.%d.%d\n",
1171 p[14], p[15], p[16], p[17],
1172 p[24], p[25], p[26], p[27]);
1173 break;
1178 * Generic routine to send out an skbuf.
1179 * If lowlevel-device does not support support skbufs, use
1180 * standard send-routine, else send directly.
1182 * Return: 0 on success, !0 on failure.
1183 * Side-effects: ndev->tbusy is cleared on success.
1186 isdn_net_send_skb(struct net_device *ndev, isdn_net_local * lp,
1187 struct sk_buff *skb)
1189 int ret;
1190 int len = skb->len; /* save len */
1192 ret = isdn_writebuf_skb_stub(lp->isdn_device, lp->isdn_channel, 1, skb);
1193 if (ret == len) {
1194 lp->transcount += len;
1195 clear_bit(0, (void *) &(ndev->tbusy));
1196 return 0;
1198 if (ret < 0) {
1199 dev_kfree_skb(skb);
1200 lp->stats.tx_errors++;
1201 clear_bit(0, (void *) &(ndev->tbusy));
1202 return 0;
1204 return 1;
1208 * Helper function for isdn_net_start_xmit.
1209 * When called, the connection is already established.
1210 * Based on cps-calculation, check if device is overloaded.
1211 * If so, and if a slave exists, trigger dialing for it.
1212 * If any slave is online, deliver packets using a simple round robin
1213 * scheme.
1215 * Return: 0 on success, !0 on failure.
1218 static int
1219 isdn_net_xmit(struct net_device *ndev, isdn_net_local * lp, struct sk_buff *skb)
1221 int ret;
1223 /* For the other encaps the header has already been built */
1224 #ifdef CONFIG_ISDN_PPP
1225 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
1226 return isdn_ppp_xmit(skb, ndev);
1228 #endif
1229 /* Reset hangup-timeout */
1230 lp->huptimer = 0;
1231 if (lp->cps > lp->triggercps) {
1232 /* Device overloaded */
1235 * Packet-delivery via round-robin over master
1236 * and all connected slaves.
1238 if (lp->master)
1239 /* Slaves always deliver themselves */
1240 ret = isdn_net_send_skb(ndev, lp, skb);
1241 else {
1242 isdn_net_local *slp = (isdn_net_local *) (lp->srobin->priv);
1243 /* Master delivers via srobin and maintains srobin */
1244 if (lp->srobin == ndev)
1245 ret = isdn_net_send_skb(ndev, lp, skb);
1246 else
1247 ret = ndev->tbusy = isdn_net_start_xmit(skb, lp->srobin);
1248 lp->srobin = (slp->slave) ? slp->slave : ndev;
1249 slp = (isdn_net_local *) (lp->srobin->priv);
1250 if (!((slp->flags & ISDN_NET_CONNECTED) && (slp->dialstate == 0)))
1251 lp->srobin = ndev;
1253 /* Slave-startup using delay-variable */
1254 if (lp->slave) {
1255 if (!lp->sqfull) {
1256 /* First time overload: set timestamp only */
1257 lp->sqfull = 1;
1258 lp->sqfull_stamp = jiffies;
1259 } else {
1260 /* subsequent overload: if slavedelay exceeded, start dialing */
1261 if ((jiffies - lp->sqfull_stamp) > lp->slavedelay)
1262 isdn_net_force_dial_lp((isdn_net_local *) lp->slave->priv);
1265 } else {
1266 /* Not overloaded, deliver locally */
1267 ret = isdn_net_send_skb(ndev, lp, skb);
1268 if (lp->sqfull && ((jiffies - lp->sqfull_stamp) > (lp->slavedelay + (10 * HZ))))
1269 lp->sqfull = 0;
1271 return ret;
1274 static void
1275 isdn_net_adjust_hdr(struct sk_buff *skb, struct net_device *dev)
1277 isdn_net_local *lp = (isdn_net_local *) dev->priv;
1278 if (!skb)
1279 return;
1280 if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
1281 int pullsize = (ulong)skb->nh.raw - (ulong)skb->data - ETH_HLEN;
1282 if (pullsize > 0) {
1283 printk(KERN_DEBUG "isdn_net: Pull junk %d\n", pullsize);
1284 skb_pull(skb, pullsize);
1290 * Try sending a packet.
1291 * If this interface isn't connected to a ISDN-Channel, find a free channel,
1292 * and start dialing.
1294 static int
1295 isdn_net_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1297 isdn_net_local *lp = (isdn_net_local *) ndev->priv;
1298 #ifdef CONFIG_ISDN_X25
1299 struct concap_proto * cprot = lp -> netdev -> cprot;
1300 #endif
1302 if (ndev->tbusy) {
1303 if (jiffies - ndev->trans_start < (2 * HZ))
1304 return 1;
1305 if (!lp->dialstate)
1306 lp->stats.tx_errors++;
1307 ndev->trans_start = jiffies;
1309 ndev->tbusy = 1; /* left instead of obsolete test_and_set_bit() */
1310 #ifdef CONFIG_ISDN_X25
1311 /* At this point hard_start_xmit() passes control to the encapsulation
1312 protocol (if present).
1313 For X.25 auto-dialing is completly bypassed because:
1314 - It does not conform with the semantics of a reliable datalink
1315 service as needed by X.25 PLP.
1316 - I don't want that the interface starts dialing when the network layer
1317 sends a message which requests to disconnect the lapb link (or if it
1318 sends any other message not resulting in data transmission).
1319 Instead, dialing will be initiated by the encapsulation protocol entity
1320 when a dl_establish request is received from the upper layer.
1322 if( cprot ) {
1323 return cprot -> pops -> encap_and_xmit ( cprot , skb);
1324 } else
1325 #endif
1326 /* auto-dialing xmit function */
1328 #ifdef ISDN_DEBUG_NET_DUMP
1329 u_char *buf;
1330 #endif
1331 isdn_net_adjust_hdr(skb, ndev);
1332 #ifdef ISDN_DEBUG_NET_DUMP
1333 buf = skb->data;
1334 isdn_dumppkt("S:", buf, skb->len, 40);
1335 #endif
1336 if (!(lp->flags & ISDN_NET_CONNECTED)) {
1337 int chi;
1338 /* only do autodial if allowed by config */
1339 if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) {
1340 isdn_net_unreachable(ndev, skb, "dial rejected: interface not in dialmode `auto'");
1341 dev_kfree_skb(skb);
1342 ndev->tbusy = 0;
1343 return 0;
1345 if (lp->phone[1]) {
1346 ulong flags;
1347 save_flags(flags);
1348 cli();
1350 if(lp->dialwait_timer <= 0)
1351 if(lp->dialstarted > 0 && lp->dialtimeout > 0 && jiffies < lp->dialstarted + lp->dialtimeout + lp->dialwait)
1352 lp->dialwait_timer = lp->dialstarted + lp->dialtimeout + lp->dialwait;
1354 if(lp->dialwait_timer > 0) {
1355 if(jiffies < lp->dialwait_timer) {
1356 isdn_net_unreachable(ndev, skb, "dial rejected: retry-time not reached");
1357 dev_kfree_skb(skb);
1358 ndev->tbusy = 0;
1359 restore_flags(flags);
1360 return 0;
1361 } else
1362 lp->dialwait_timer = 0;
1365 /* Grab a free ISDN-Channel */
1366 if (((chi =
1367 isdn_get_free_channel(ISDN_USAGE_NET,
1368 lp->l2_proto,
1369 lp->l3_proto,
1370 lp->pre_device,
1371 lp->pre_channel)) < 0) &&
1372 ((chi =
1373 isdn_get_free_channel(ISDN_USAGE_NET,
1374 lp->l2_proto,
1375 lp->l3_proto,
1376 lp->pre_device,
1377 lp->pre_channel^1)) < 0)) {
1378 restore_flags(flags);
1379 isdn_net_unreachable(ndev, skb,
1380 "No channel");
1381 dev_kfree_skb(skb);
1382 ndev->tbusy = 0;
1383 return 0;
1385 /* Log packet, which triggered dialing */
1386 if (dev->net_verbose)
1387 isdn_net_log_skb(skb, lp);
1388 lp->dialstate = 1;
1389 /* Connect interface with channel */
1390 isdn_net_bind_channel(lp, chi);
1391 #ifdef CONFIG_ISDN_PPP
1392 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
1393 /* no 'first_skb' handling for syncPPP */
1394 if (isdn_ppp_bind(lp) < 0) {
1395 dev_kfree_skb(skb);
1396 isdn_net_unbind_channel(lp);
1397 restore_flags(flags);
1398 return 0; /* STN (skb to nirvana) ;) */
1400 restore_flags(flags);
1401 isdn_net_dial(); /* Initiate dialing */
1402 return 1; /* let upper layer requeue skb packet */
1404 #endif
1405 /* remember first skb to speed up arp
1406 * when using encap ETHER
1408 if (lp->first_skb) {
1409 printk(KERN_WARNING "isdn_net_start_xmit: First skb already set!\n");
1410 dev_kfree_skb(lp->first_skb);
1411 lp->first_skb = NULL;
1413 lp->first_skb = skb;
1414 /* Initiate dialing */
1415 ndev->tbusy = 0;
1416 restore_flags(flags);
1417 isdn_net_dial();
1418 return 0;
1419 } else {
1420 isdn_net_unreachable(ndev, skb,
1421 "No phone number");
1422 dev_kfree_skb(skb);
1423 ndev->tbusy = 0;
1424 return 0;
1426 } else {
1427 /* Connection is established, try sending */
1428 ndev->trans_start = jiffies;
1429 if (!lp->dialstate) {
1430 if (lp->first_skb) {
1431 if (isdn_net_xmit(ndev, lp, lp->first_skb))
1432 return 1;
1433 lp->first_skb = NULL;
1435 return (isdn_net_xmit(ndev, lp, skb));
1436 } else
1437 ndev->tbusy = 1;
1440 return 1;
1444 * Shutdown a net-interface.
1446 static int
1447 isdn_net_close(struct net_device *dev)
1449 struct net_device *p;
1450 #ifdef CONFIG_ISDN_X25
1451 struct concap_proto * cprot =
1452 ( (isdn_net_local *) dev->priv ) -> netdev -> cprot;
1453 /* printk(KERN_DEBUG "isdn_net_close %s\n" , dev-> name ); */
1454 #endif
1456 #ifdef CONFIG_ISDN_X25
1457 if( cprot && cprot -> pops ) cprot -> pops -> close( cprot );
1458 #endif
1459 dev->tbusy = 1;
1460 dev->start = 0;
1461 if ((p = (((isdn_net_local *) dev->priv)->slave))) {
1462 /* If this interface has slaves, stop them also */
1463 while (p) {
1464 #ifdef CONFIG_ISDN_X25
1465 cprot = ( (isdn_net_local *) p->priv )
1466 -> netdev -> cprot;
1467 if( cprot && cprot -> pops )
1468 cprot -> pops -> close( cprot );
1469 #endif
1470 isdn_net_hangup(p);
1471 p->tbusy = 1;
1472 p->start = 0;
1473 p = (((isdn_net_local *) p->priv)->slave);
1476 isdn_net_hangup(dev);
1477 isdn_MOD_DEC_USE_COUNT();
1478 return 0;
1482 * Get statistics
1484 static struct enet_statistics *
1485 isdn_net_get_stats(struct net_device *dev)
1487 isdn_net_local *lp = (isdn_net_local *) dev->priv;
1488 return &lp->stats;
1491 /* This is simply a copy from std. eth.c EXCEPT we pull ETH_HLEN
1492 * instead of dev->hard_header_len off. This is done because the
1493 * lowlevel-driver has already pulled off its stuff when we get
1494 * here and this routine only gets called with p_encap == ETHER.
1495 * Determine the packet's protocol ID. The rule here is that we
1496 * assume 802.3 if the type field is short enough to be a length.
1497 * This is normal practice and works for any 'now in use' protocol.
1500 static unsigned short
1501 isdn_net_type_trans(struct sk_buff *skb, struct net_device *dev)
1503 struct ethhdr *eth;
1504 unsigned char *rawp;
1506 skb->mac.raw = skb->data;
1507 skb_pull(skb, ETH_HLEN);
1508 eth = skb->mac.ethernet;
1510 if (*eth->h_dest & 1) {
1511 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
1512 skb->pkt_type = PACKET_BROADCAST;
1513 else
1514 skb->pkt_type = PACKET_MULTICAST;
1517 * This ALLMULTI check should be redundant by 1.4
1518 * so don't forget to remove it.
1521 else if (dev->flags & (IFF_PROMISC /*| IFF_ALLMULTI*/)) {
1522 if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
1523 skb->pkt_type = PACKET_OTHERHOST;
1525 if (ntohs(eth->h_proto) >= 1536)
1526 return eth->h_proto;
1528 rawp = skb->data;
1531 * This is a magic hack to spot IPX packets. Older Novell breaks
1532 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
1533 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
1534 * won't work for fault tolerant netware but does for the rest.
1536 if (*(unsigned short *) rawp == 0xFFFF)
1537 return htons(ETH_P_802_3);
1539 * Real 802.2 LLC
1541 return htons(ETH_P_802_2);
1544 static void
1545 isdn_net_slarp_send(isdn_net_local *lp, int is_reply)
1547 unsigned short hl = dev->drv[lp->isdn_device]->interface->hl_hdrlen;
1548 struct sk_buff *skb = alloc_skb(hl + sizeof(cisco_hdr) + sizeof(cisco_slarp), GFP_ATOMIC);
1549 unsigned long t = (jiffies / HZ * 1000000);
1550 int len;
1551 cisco_hdr *ch;
1552 cisco_slarp *s;
1554 if (!skb) {
1555 printk(KERN_WARNING
1556 "%s: Could not allocate SLARP reply\n", lp->name);
1557 return;
1559 skb_reserve(skb, hl);
1560 ch = (cisco_hdr *)skb_put(skb, sizeof(cisco_hdr));
1561 ch->addr = CISCO_ADDR_UNICAST;
1562 ch->ctrl = 0;
1563 ch->type = htons(CISCO_TYPE_SLARP);
1564 s = (cisco_slarp *)skb_put(skb, sizeof(cisco_slarp));
1565 if (is_reply) {
1566 s->code = htonl(CISCO_SLARP_REPLY);
1567 memset(&s->slarp.reply.ifaddr, 0, sizeof(__u32));
1568 memset(&s->slarp.reply.netmask, 0, sizeof(__u32));
1569 } else {
1570 lp->cisco_myseq++;
1571 s->code = htonl(CISCO_SLARP_KEEPALIVE);
1572 s->slarp.keepalive.my_seq = htonl(lp->cisco_myseq);
1573 s->slarp.keepalive.your_seq = htonl(lp->cisco_yourseq);
1575 s->rel = 0xffff;
1576 s->t1 = t >> 16;
1577 s->t0 = t & 0xffff;
1578 len = skb->len;
1579 if (isdn_writebuf_skb_stub(lp->isdn_device, lp->isdn_channel, 0, skb) != len)
1580 dev_kfree_skb(skb);
1583 static void
1584 isdn_net_slarp_in(isdn_net_local *lp, struct sk_buff *skb)
1586 cisco_slarp *s = (cisco_slarp *)skb->data;
1588 switch (ntohl(s->code)) {
1589 case CISCO_SLARP_REQUEST:
1590 isdn_net_slarp_send(lp, 1);
1591 break;
1592 case CISCO_SLARP_REPLY:
1593 /* Ignore replies */
1594 break;
1595 case CISCO_SLARP_KEEPALIVE:
1596 lp->cisco_yourseq = s->slarp.keepalive.my_seq;
1597 if (ntohl(s->slarp.keepalive.my_seq == lp->cisco_myseq)) {
1598 if (lp->cisco_loop++ == 2) {
1599 printk(KERN_WARNING "%s: Keepalive Loop\n",
1600 lp->name);
1601 lp->cisco_myseq ^= jiffies;
1603 } else
1604 lp->cisco_loop = 0;
1605 break;
1607 kfree_skb(skb);
1611 * Called every 10 sec. via timer-interrupt if
1612 * any network-interface has Cisco-Keepalive-Encapsulation
1613 * and is online.
1614 * Send Keepalive-Packet and re-schedule.
1616 void
1617 isdn_net_slarp_out(void)
1619 isdn_net_dev *p = dev->netdev;
1620 int anymore = 0;
1622 while (p) {
1623 isdn_net_local *l = p->local;
1624 if ((l->p_encap == ISDN_NET_ENCAP_CISCOHDLCK) &&
1625 (l->flags & ISDN_NET_CONNECTED) &&
1626 (!l->dialstate) ) {
1627 anymore = 1;
1628 isdn_net_slarp_send(l, 0);
1630 p = (isdn_net_dev *) p->next;
1632 isdn_timer_ctrl(ISDN_TIMER_KEEPALIVE, anymore);
1636 * Got a packet from ISDN-Channel.
1638 static void
1639 isdn_net_receive(struct net_device *ndev, struct sk_buff *skb)
1641 isdn_net_local *lp = (isdn_net_local *) ndev->priv;
1642 isdn_net_local *olp = lp; /* original 'lp' */
1643 #ifdef CONFIG_ISDN_PPP
1644 int proto = PPP_PROTOCOL(skb->data);
1645 #endif
1646 #ifdef CONFIG_ISDN_X25
1647 struct concap_proto *cprot = lp -> netdev -> cprot;
1648 #endif
1649 cisco_hdr *ch;
1651 lp->transcount += skb->len;
1653 lp->stats.rx_packets++;
1654 lp->stats.rx_bytes += skb->len;
1655 if (lp->master) {
1656 /* Bundling: If device is a slave-device, deliver to master, also
1657 * handle master's statistics and hangup-timeout
1659 ndev = lp->master;
1660 lp = (isdn_net_local *) ndev->priv;
1661 lp->stats.rx_packets++;
1662 lp->stats.rx_bytes += skb->len;
1665 skb->dev = ndev;
1666 skb->pkt_type = PACKET_HOST;
1667 skb->mac.raw = skb->data;
1668 #ifdef ISDN_DEBUG_NET_DUMP
1669 isdn_dumppkt("R:", skb->data, skb->len, 40);
1670 #endif
1671 switch (lp->p_encap) {
1672 case ISDN_NET_ENCAP_ETHER:
1673 /* Ethernet over ISDN */
1674 olp->huptimer = 0;
1675 lp->huptimer = 0;
1676 skb->protocol = isdn_net_type_trans(skb, ndev);
1677 break;
1678 case ISDN_NET_ENCAP_UIHDLC:
1679 /* HDLC with UI-frame (for ispa with -h1 option) */
1680 olp->huptimer = 0;
1681 lp->huptimer = 0;
1682 skb_pull(skb, 2);
1683 /* Fall through */
1684 case ISDN_NET_ENCAP_RAWIP:
1685 /* RAW-IP without MAC-Header */
1686 olp->huptimer = 0;
1687 lp->huptimer = 0;
1688 skb->protocol = htons(ETH_P_IP);
1689 break;
1690 case ISDN_NET_ENCAP_CISCOHDLCK:
1691 ch = (cisco_hdr *)skb->data;
1692 if ((ch->addr != CISCO_ADDR_UNICAST) &&
1693 (ch->addr != CISCO_ADDR_BROADCAST) ) {
1694 printk(KERN_WARNING "%s: Unknown Cisco addr 0x%02x\n",
1695 lp->name, ch->addr);
1696 kfree_skb(skb);
1697 return;
1699 if (ch->ctrl != 0) {
1700 printk(KERN_WARNING "%s: Unknown Cisco ctrl 0x%02x\n",
1701 lp->name, ch->ctrl);
1702 kfree_skb(skb);
1703 return;
1705 switch (ntohs(ch->type)) {
1706 case CISCO_TYPE_INET:
1707 skb_pull(skb, 4);
1708 skb->protocol = htons(ETH_P_IP);
1709 break;
1710 case CISCO_TYPE_SLARP:
1711 skb_pull(skb, 4);
1712 isdn_net_slarp_in(olp, skb);
1713 return;
1714 default:
1715 printk(KERN_WARNING "%s: Unknown Cisco type 0x%04x\n",
1716 lp->name, ch->type);
1717 kfree_skb(skb);
1718 return;
1720 break;
1721 case ISDN_NET_ENCAP_CISCOHDLC:
1722 /* CISCO-HDLC IP with type field and fake I-frame-header */
1723 skb_pull(skb, 2);
1724 /* Fall through */
1725 case ISDN_NET_ENCAP_IPTYP:
1726 /* IP with type field */
1727 olp->huptimer = 0;
1728 lp->huptimer = 0;
1729 skb->protocol = *(unsigned short *) &(skb->data[0]);
1730 skb_pull(skb, 2);
1731 if (*(unsigned short *) skb->data == 0xFFFF)
1732 skb->protocol = htons(ETH_P_802_3);
1733 break;
1734 #ifdef CONFIG_ISDN_PPP
1735 case ISDN_NET_ENCAP_SYNCPPP:
1737 * If encapsulation is syncppp, don't reset
1738 * huptimer on LCP packets.
1740 if (proto != PPP_LCP) {
1741 olp->huptimer = 0;
1742 lp->huptimer = 0;
1744 isdn_ppp_receive(lp->netdev, olp, skb);
1745 return;
1746 #endif
1747 default:
1748 #ifdef CONFIG_ISDN_X25
1749 /* try if there are generic sync_device receiver routines */
1750 if(cprot) if(cprot -> pops)
1751 if( cprot -> pops -> data_ind){
1752 cprot -> pops -> data_ind(cprot,skb);
1753 return;
1755 #endif /* CONFIG_ISDN_X25 */
1756 printk(KERN_WARNING "%s: unknown encapsulation, dropping\n",
1757 lp->name);
1758 kfree_skb(skb);
1759 return;
1762 netif_rx(skb);
1763 return;
1767 * A packet arrived via ISDN. Search interface-chain for a corresponding
1768 * interface. If found, deliver packet to receiver-function and return 1,
1769 * else return 0.
1772 isdn_net_rcv_skb(int idx, struct sk_buff *skb)
1774 isdn_net_dev *p = dev->rx_netdev[idx];
1776 if (p) {
1777 isdn_net_local *lp = p->local;
1778 if ((lp->flags & ISDN_NET_CONNECTED) &&
1779 (!lp->dialstate)) {
1780 isdn_net_receive(&p->dev, skb);
1781 return 1;
1784 return 0;
1787 static int
1788 my_eth_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
1789 void *daddr, void *saddr, unsigned len)
1791 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
1794 * Set the protocol type. For a packet of type ETH_P_802_3 we
1795 * put the length here instead. It is up to the 802.2 layer to
1796 * carry protocol information.
1799 if (type != ETH_P_802_3)
1800 eth->h_proto = htons(type);
1801 else
1802 eth->h_proto = htons(len);
1805 * Set the source hardware address.
1807 if (saddr)
1808 memcpy(eth->h_source, saddr, dev->addr_len);
1809 else
1810 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
1813 * Anyway, the loopback-device should never use this function...
1816 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
1817 memset(eth->h_dest, 0, dev->addr_len);
1818 return ETH_HLEN /*(dev->hard_header_len)*/;
1820 if (daddr) {
1821 memcpy(eth->h_dest, daddr, dev->addr_len);
1822 return ETH_HLEN /*dev->hard_header_len*/;
1824 return -ETH_HLEN /*dev->hard_header_len*/;
1828 * build an header
1829 * depends on encaps that is being used.
1832 static int
1833 isdn_net_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
1834 void *daddr, void *saddr, unsigned plen)
1836 isdn_net_local *lp = dev->priv;
1837 ushort len = 0;
1839 switch (lp->p_encap) {
1840 case ISDN_NET_ENCAP_ETHER:
1841 len = my_eth_header(skb, dev, type, daddr, saddr, plen);
1842 break;
1843 #ifdef CONFIG_ISDN_PPP
1844 case ISDN_NET_ENCAP_SYNCPPP:
1845 /* stick on a fake header to keep fragmentation code happy. */
1846 len = IPPP_MAX_HEADER;
1847 skb_push(skb,len);
1848 break;
1849 #endif
1850 case ISDN_NET_ENCAP_RAWIP:
1851 printk(KERN_WARNING "isdn_net_header called with RAW_IP!\n");
1852 len = 0;
1853 break;
1854 case ISDN_NET_ENCAP_IPTYP:
1855 /* ethernet type field */
1856 *((ushort *) skb_push(skb, 2)) = htons(type);
1857 len = 2;
1858 break;
1859 case ISDN_NET_ENCAP_UIHDLC:
1860 /* HDLC with UI-Frames (for ispa with -h1 option) */
1861 *((ushort *) skb_push(skb, 2)) = htons(0x0103);
1862 len = 2;
1863 break;
1864 case ISDN_NET_ENCAP_CISCOHDLC:
1865 skb_push(skb, 4);
1866 skb->data[0] = 0x0f;
1867 skb->data[1] = 0x00;
1868 *((ushort *) & skb->data[2]) = htons(type);
1869 len = 4;
1870 break;
1871 #ifdef CONFIG_ISDN_X25
1872 default:
1873 /* try if there are generic concap protocol routines */
1874 if( lp-> netdev -> cprot ){
1875 printk(KERN_WARNING "isdn_net_header called with concap_proto!\n");
1876 len = 0;
1877 break;
1879 break;
1880 #endif /* CONFIG_ISDN_X25 */
1882 return len;
1885 /* We don't need to send arp, because we have point-to-point connections. */
1886 static int
1887 isdn_net_rebuild_header(struct sk_buff *skb)
1889 struct net_device *dev = skb->dev;
1890 isdn_net_local *lp = dev->priv;
1891 int ret = 0;
1893 if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
1894 struct ethhdr *eth = (struct ethhdr *) skb->data;
1897 * Only ARP/IP is currently supported
1900 if (eth->h_proto != htons(ETH_P_IP)) {
1901 printk(KERN_WARNING
1902 "isdn_net: %s don't know how to resolve type %d addresses?\n",
1903 dev->name, (int) eth->h_proto);
1904 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
1905 return 0;
1908 * Try to get ARP to resolve the header.
1910 #ifdef CONFIG_INET
1911 ret = arp_find(eth->h_dest, skb);
1912 #endif
1914 return ret;
1918 * Interface-setup. (called just after registering a new interface)
1920 static int
1921 isdn_net_init(struct net_device *ndev)
1923 ushort max_hlhdr_len = 0;
1924 isdn_net_local *lp = (isdn_net_local *) ndev->priv;
1925 int drvidx,
1928 if (ndev == NULL) {
1929 printk(KERN_WARNING "isdn_net_init: dev = NULL!\n");
1930 return -ENODEV;
1932 if (ndev->priv == NULL) {
1933 printk(KERN_WARNING "isdn_net_init: dev->priv = NULL!\n");
1934 return -ENODEV;
1936 ether_setup(ndev);
1937 lp->org_hhc = ndev->hard_header_cache;
1938 lp->org_hcu = ndev->header_cache_update;
1940 /* Setup the generic properties */
1942 ndev->hard_header = NULL;
1943 ndev->hard_header_cache = NULL;
1944 ndev->header_cache_update = NULL;
1945 ndev->mtu = 1500;
1946 ndev->flags = IFF_NOARP|IFF_POINTOPOINT;
1947 ndev->type = ARPHRD_ETHER;
1948 ndev->addr_len = ETH_ALEN;
1950 /* for clients with MPPP maybe higher values better */
1951 ndev->tx_queue_len = 30;
1953 for (i = 0; i < ETH_ALEN; i++)
1954 ndev->broadcast[i] = 0xff;
1956 /* The ISDN-specific entries in the device structure. */
1957 ndev->open = &isdn_net_open;
1958 ndev->hard_start_xmit = &isdn_net_start_xmit;
1961 * up till binding we ask the protocol layer to reserve as much
1962 * as we might need for HL layer
1965 for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
1966 if (dev->drv[drvidx])
1967 if (max_hlhdr_len < dev->drv[drvidx]->interface->hl_hdrlen)
1968 max_hlhdr_len = dev->drv[drvidx]->interface->hl_hdrlen;
1970 ndev->hard_header_len = ETH_HLEN + max_hlhdr_len;
1971 ndev->stop = &isdn_net_close;
1972 ndev->get_stats = &isdn_net_get_stats;
1973 ndev->rebuild_header = &isdn_net_rebuild_header;
1974 #ifdef CONFIG_ISDN_PPP
1975 ndev->do_ioctl = isdn_ppp_dev_ioctl;
1976 #endif
1977 return 0;
1980 static void
1981 isdn_net_swapbind(int drvidx)
1983 isdn_net_dev *p;
1985 #ifdef ISDN_DEBUG_NET_ICALL
1986 printk(KERN_DEBUG "n_fi: swapping ch of %d\n", drvidx);
1987 #endif
1988 p = dev->netdev;
1989 while (p) {
1990 if (p->local->pre_device == drvidx)
1991 switch (p->local->pre_channel) {
1992 case 0:
1993 p->local->pre_channel = 1;
1994 break;
1995 case 1:
1996 p->local->pre_channel = 0;
1997 break;
1999 p = (isdn_net_dev *) p->next;
2003 static void
2004 isdn_net_swap_usage(int i1, int i2)
2006 int u1 = dev->usage[i1] & ISDN_USAGE_EXCLUSIVE;
2007 int u2 = dev->usage[i2] & ISDN_USAGE_EXCLUSIVE;
2009 #ifdef ISDN_DEBUG_NET_ICALL
2010 printk(KERN_DEBUG "n_fi: usage of %d and %d\n", i1, i2);
2011 #endif
2012 dev->usage[i1] &= ~ISDN_USAGE_EXCLUSIVE;
2013 dev->usage[i1] |= u2;
2014 dev->usage[i2] &= ~ISDN_USAGE_EXCLUSIVE;
2015 dev->usage[i2] |= u1;
2016 isdn_info_update();
2020 * An incoming call-request has arrived.
2021 * Search the interface-chain for an appropriate interface.
2022 * If found, connect the interface to the ISDN-channel and initiate
2023 * D- and B-Channel-setup. If secure-flag is set, accept only
2024 * configured phone-numbers. If callback-flag is set, initiate
2025 * callback-dialing.
2027 * Return-Value: 0 = No appropriate interface for this call.
2028 * 1 = Call accepted
2029 * 2 = Reject call, wait cbdelay, then call back
2030 * 3 = Reject call
2031 * 4 = Wait cbdelay, then call back
2032 * 5 = No appropriate interface for this call,
2033 * would eventually match if CID was longer.
2036 isdn_net_find_icall(int di, int ch, int idx, setup_parm setup)
2038 char *eaz;
2039 int si1;
2040 int si2;
2041 int ematch;
2042 int wret;
2043 int swapped;
2044 int sidx = 0;
2045 isdn_net_dev *p;
2046 isdn_net_phone *n;
2047 ulong flags;
2048 char nr[32];
2050 /* Search name in netdev-chain */
2051 save_flags(flags);
2052 cli();
2053 if (!setup.phone[0]) {
2054 nr[0] = '0';
2055 nr[1] = '\0';
2056 printk(KERN_INFO "isdn_net: Incoming call without OAD, assuming '0'\n");
2057 } else
2058 strcpy(nr, setup.phone);
2059 si1 = (int) setup.si1;
2060 si2 = (int) setup.si2;
2061 if (!setup.eazmsn[0]) {
2062 printk(KERN_WARNING "isdn_net: Incoming call without CPN, assuming '0'\n");
2063 eaz = "0";
2064 } else
2065 eaz = setup.eazmsn;
2066 if (dev->net_verbose > 1)
2067 printk(KERN_INFO "isdn_net: call from %s,%d,%d -> %s\n", nr, si1, si2, eaz);
2068 /* Accept only calls with Si1 = 7 (Data-Transmission) */
2069 if (si1 != 7) {
2070 restore_flags(flags);
2071 if (dev->net_verbose > 1)
2072 printk(KERN_INFO "isdn_net: Service-Indicator not 7, ignored\n");
2073 return 0;
2075 n = (isdn_net_phone *) 0;
2076 p = dev->netdev;
2077 ematch = wret = swapped = 0;
2078 #ifdef ISDN_DEBUG_NET_ICALL
2079 printk(KERN_DEBUG "n_fi: di=%d ch=%d idx=%d usg=%d\n", di, ch, idx,
2080 dev->usage[idx]);
2081 #endif
2082 while (p) {
2083 int matchret;
2084 isdn_net_local *lp = p->local;
2086 /* If last check has triggered as binding-swap, revert it */
2087 switch (swapped) {
2088 case 2:
2089 isdn_net_swap_usage(idx, sidx);
2090 /* fall through */
2091 case 1:
2092 isdn_net_swapbind(di);
2093 break;
2095 swapped = 0;
2096 if (!(matchret = isdn_wildmat(eaz, isdn_map_eaz2msn(lp->msn, di))))
2097 ematch = 1;
2098 /* Remember if more numbers eventually can match */
2099 if (matchret > wret)
2100 wret = matchret;
2101 #ifdef ISDN_DEBUG_NET_ICALL
2102 printk(KERN_DEBUG "n_fi: if='%s', l.msn=%s, l.flags=%d, l.dstate=%d\n",
2103 lp->name, lp->msn, lp->flags, lp->dialstate);
2104 #endif
2105 if ((!matchret) && /* EAZ is matching */
2106 (((!(lp->flags & ISDN_NET_CONNECTED)) && /* but not connected */
2107 (USG_NONE(dev->usage[idx]))) || /* and ch. unused or */
2108 ((((lp->dialstate == 4) || (lp->dialstate == 12)) && /* if dialing */
2109 (!(lp->flags & ISDN_NET_CALLBACK))) /* but no callback */
2112 #ifdef ISDN_DEBUG_NET_ICALL
2113 printk(KERN_DEBUG "n_fi: match1, pdev=%d pch=%d\n",
2114 lp->pre_device, lp->pre_channel);
2115 #endif
2116 if (dev->usage[idx] & ISDN_USAGE_EXCLUSIVE) {
2117 if ((lp->pre_channel != ch) ||
2118 (lp->pre_device != di)) {
2119 /* Here we got a problem:
2120 * If using an ICN-Card, an incoming call is always signaled on
2121 * on the first channel of the card, if both channels are
2122 * down. However this channel may be bound exclusive. If the
2123 * second channel is free, this call should be accepted.
2124 * The solution is horribly but it runs, so what:
2125 * We exchange the exclusive bindings of the two channels, the
2126 * corresponding variables in the interface-structs.
2128 if (ch == 0) {
2129 sidx = isdn_dc2minor(di, 1);
2130 #ifdef ISDN_DEBUG_NET_ICALL
2131 printk(KERN_DEBUG "n_fi: ch is 0\n");
2132 #endif
2133 if (USG_NONE(dev->usage[sidx])) {
2134 /* Second Channel is free, now see if it is bound
2135 * exclusive too. */
2136 if (dev->usage[sidx] & ISDN_USAGE_EXCLUSIVE) {
2137 #ifdef ISDN_DEBUG_NET_ICALL
2138 printk(KERN_DEBUG "n_fi: 2nd channel is down and bound\n");
2139 #endif
2140 /* Yes, swap bindings only, if the original
2141 * binding is bound to channel 1 of this driver */
2142 if ((lp->pre_device == di) &&
2143 (lp->pre_channel == 1)) {
2144 isdn_net_swapbind(di);
2145 swapped = 1;
2146 } else {
2147 /* ... else iterate next device */
2148 p = (isdn_net_dev *) p->next;
2149 continue;
2151 } else {
2152 #ifdef ISDN_DEBUG_NET_ICALL
2153 printk(KERN_DEBUG "n_fi: 2nd channel is down and unbound\n");
2154 #endif
2155 /* No, swap always and swap excl-usage also */
2156 isdn_net_swap_usage(idx, sidx);
2157 isdn_net_swapbind(di);
2158 swapped = 2;
2160 /* Now check for exclusive binding again */
2161 #ifdef ISDN_DEBUG_NET_ICALL
2162 printk(KERN_DEBUG "n_fi: final check\n");
2163 #endif
2164 if ((dev->usage[idx] & ISDN_USAGE_EXCLUSIVE) &&
2165 ((lp->pre_channel != ch) ||
2166 (lp->pre_device != di))) {
2167 #ifdef ISDN_DEBUG_NET_ICALL
2168 printk(KERN_DEBUG "n_fi: final check failed\n");
2169 #endif
2170 p = (isdn_net_dev *) p->next;
2171 continue;
2174 } else {
2175 /* We are already on the second channel, so nothing to do */
2176 #ifdef ISDN_DEBUG_NET_ICALL
2177 printk(KERN_DEBUG "n_fi: already on 2nd channel\n");
2178 #endif
2182 #ifdef ISDN_DEBUG_NET_ICALL
2183 printk(KERN_DEBUG "n_fi: match2\n");
2184 #endif
2185 n = lp->phone[0];
2186 if (lp->flags & ISDN_NET_SECURE) {
2187 while (n) {
2188 if (!isdn_wildmat(nr, n->num))
2189 break;
2190 n = (isdn_net_phone *) n->next;
2193 if (n || (!(lp->flags & ISDN_NET_SECURE))) {
2194 #ifdef ISDN_DEBUG_NET_ICALL
2195 printk(KERN_DEBUG "n_fi: match3\n");
2196 #endif
2197 /* matching interface found */
2200 * Is the state STOPPED?
2201 * If so, no dialin is allowed,
2202 * so reject actively.
2203 * */
2204 if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
2205 restore_flags(flags);
2206 printk(KERN_INFO "incoming call, interface %s `stopped' -> rejected\n",
2207 lp->name);
2208 return 3;
2211 * Is the interface up?
2212 * If not, reject the call actively.
2214 if (!p->dev.start) {
2215 restore_flags(flags);
2216 printk(KERN_INFO "%s: incoming call, interface down -> rejected\n",
2217 lp->name);
2218 return 3;
2220 /* Interface is up, now see if it's a slave. If so, see if
2221 * it's master and parent slave is online. If not, reject the call.
2223 if (lp->master) {
2224 isdn_net_local *mlp = (isdn_net_local *) lp->master->priv;
2225 printk(KERN_DEBUG "ICALLslv: %s\n", lp->name);
2226 printk(KERN_DEBUG "master=%s\n", mlp->name);
2227 if (mlp->flags & ISDN_NET_CONNECTED) {
2228 printk(KERN_DEBUG "master online\n");
2229 /* Master is online, find parent-slave (master if first slave) */
2230 while (mlp->slave) {
2231 if ((isdn_net_local *) mlp->slave->priv == lp)
2232 break;
2233 mlp = (isdn_net_local *) mlp->slave->priv;
2235 } else
2236 printk(KERN_DEBUG "master offline\n");
2237 /* Found parent, if it's offline iterate next device */
2238 printk(KERN_DEBUG "mlpf: %d\n", mlp->flags & ISDN_NET_CONNECTED);
2239 if (!(mlp->flags & ISDN_NET_CONNECTED)) {
2240 p = (isdn_net_dev *) p->next;
2241 continue;
2244 if (lp->flags & ISDN_NET_CALLBACK) {
2245 int chi;
2247 * Is the state MANUAL?
2248 * If so, no callback can be made,
2249 * so reject actively.
2250 * */
2251 if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
2252 restore_flags(flags);
2253 printk(KERN_INFO "incoming call for callback, interface %s `off' -> rejected\n",
2254 lp->name);
2255 return 3;
2257 printk(KERN_DEBUG "%s: call from %s -> %s, start callback\n",
2258 lp->name, nr, eaz);
2259 if (lp->phone[1]) {
2260 /* Grab a free ISDN-Channel */
2261 if ((chi = isdn_get_free_channel(ISDN_USAGE_NET, lp->l2_proto,
2262 lp->l3_proto,
2263 lp->pre_device,
2264 lp->pre_channel)) < 0) {
2265 printk(KERN_WARNING "isdn_net_find_icall: No channel for %s\n", lp->name);
2266 restore_flags(flags);
2267 return 0;
2269 /* Setup dialstate. */
2270 lp->dtimer = 0;
2271 lp->dialstate = 11;
2272 /* Connect interface with channel */
2273 isdn_net_bind_channel(lp, chi);
2274 #ifdef CONFIG_ISDN_PPP
2275 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2276 if (isdn_ppp_bind(lp) < 0) {
2277 isdn_net_unbind_channel(lp);
2278 restore_flags(flags);
2279 return 0;
2281 #endif
2282 /* Initiate dialing by returning 2 or 4 */
2283 restore_flags(flags);
2284 return (lp->flags & ISDN_NET_CBHUP) ? 2 : 4;
2285 } else
2286 printk(KERN_WARNING "isdn_net: %s: No phone number\n", lp->name);
2287 restore_flags(flags);
2288 return 0;
2289 } else {
2290 printk(KERN_DEBUG "%s: call from %s -> %s accepted\n", lp->name, nr,
2291 eaz);
2292 /* if this interface is dialing, it does it probably on a different
2293 device, so free this device */
2294 if ((lp->dialstate == 4) || (lp->dialstate == 12)) {
2295 #ifdef CONFIG_ISDN_PPP
2296 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2297 isdn_ppp_free(lp);
2298 #endif
2299 isdn_free_channel(lp->isdn_device, lp->isdn_channel,
2300 ISDN_USAGE_NET);
2302 dev->usage[idx] &= ISDN_USAGE_EXCLUSIVE;
2303 dev->usage[idx] |= ISDN_USAGE_NET;
2304 strcpy(dev->num[idx], nr);
2305 isdn_info_update();
2306 dev->st_netdev[idx] = lp->netdev;
2307 lp->isdn_device = di;
2308 lp->isdn_channel = ch;
2309 lp->ppp_slot = -1;
2310 lp->flags |= ISDN_NET_CONNECTED;
2311 lp->dialstate = 7;
2312 lp->dtimer = 0;
2313 lp->outgoing = 0;
2314 lp->huptimer = 0;
2315 lp->hupflags |= ISDN_WAITCHARGE;
2316 lp->hupflags &= ~ISDN_HAVECHARGE;
2317 #ifdef CONFIG_ISDN_PPP
2318 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2319 if (isdn_ppp_bind(lp) < 0) {
2320 isdn_net_unbind_channel(lp);
2321 restore_flags(flags);
2322 return 0;
2324 #endif
2325 restore_flags(flags);
2326 return 1;
2330 p = (isdn_net_dev *) p->next;
2332 /* If none of configured EAZ/MSN matched and not verbose, be silent */
2333 if (!ematch || dev->net_verbose)
2334 printk(KERN_INFO "isdn_net: call from %s -> %d %s ignored\n", nr, di, eaz);
2335 restore_flags(flags);
2336 return (wret == 2)?5:0;
2340 * Search list of net-interfaces for an interface with given name.
2342 isdn_net_dev *
2343 isdn_net_findif(char *name)
2345 isdn_net_dev *p = dev->netdev;
2347 while (p) {
2348 if (!strcmp(p->local->name, name))
2349 return p;
2350 p = (isdn_net_dev *) p->next;
2352 return (isdn_net_dev *) NULL;
2356 * Force a net-interface to dial out.
2357 * This is called from the userlevel-routine below or
2358 * from isdn_net_start_xmit().
2361 isdn_net_force_dial_lp(isdn_net_local * lp)
2363 if ((!(lp->flags & ISDN_NET_CONNECTED)) && !lp->dialstate) {
2364 int chi;
2365 if (lp->phone[1]) {
2366 ulong flags;
2367 save_flags(flags);
2368 cli();
2370 /* Grab a free ISDN-Channel */
2371 if ((chi = isdn_get_free_channel(ISDN_USAGE_NET, lp->l2_proto,
2372 lp->l3_proto,
2373 lp->pre_device,
2374 lp->pre_channel)) < 0) {
2375 printk(KERN_WARNING "isdn_net_force_dial: No channel for %s\n", lp->name);
2376 restore_flags(flags);
2377 return -EAGAIN;
2379 lp->dialstate = 1;
2380 /* Connect interface with channel */
2381 isdn_net_bind_channel(lp, chi);
2382 #ifdef CONFIG_ISDN_PPP
2383 if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2384 if (isdn_ppp_bind(lp) < 0) {
2385 isdn_net_unbind_channel(lp);
2386 restore_flags(flags);
2387 return -EAGAIN;
2389 #endif
2390 /* Initiate dialing */
2391 restore_flags(flags);
2392 isdn_net_dial();
2393 return 0;
2394 } else
2395 return -EINVAL;
2396 } else
2397 return -EBUSY;
2401 * This is called from certain upper protocol layers (multilink ppp
2402 * and x25iface encapsulation module) that want to initiate dialing
2403 * themselves.
2406 isdn_net_dial_req(isdn_net_local * lp)
2408 /* is there a better error code? */
2409 if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) return -EBUSY;
2411 return isdn_net_force_dial_lp(lp);
2415 * Force a net-interface to dial out.
2416 * This is always called from within userspace (ISDN_IOCTL_NET_DIAL).
2419 isdn_net_force_dial(char *name)
2421 isdn_net_dev *p = isdn_net_findif(name);
2423 if (!p)
2424 return -ENODEV;
2425 return (isdn_net_force_dial_lp(p->local));
2429 * Allocate a new network-interface and initialize its data structures.
2431 char *
2432 isdn_net_new(char *name, struct net_device *master)
2434 isdn_net_dev *netdev;
2436 /* Avoid creating an existing interface */
2437 if (isdn_net_findif(name)) {
2438 printk(KERN_WARNING "isdn_net: interface %s already exists\n", name);
2439 return NULL;
2441 if (!(netdev = (isdn_net_dev *) kmalloc(sizeof(isdn_net_dev), GFP_KERNEL))) {
2442 printk(KERN_WARNING "isdn_net: Could not allocate net-device\n");
2443 return NULL;
2445 memset(netdev, 0, sizeof(isdn_net_dev));
2446 if (!(netdev->local = (isdn_net_local *) kmalloc(sizeof(isdn_net_local), GFP_KERNEL))) {
2447 printk(KERN_WARNING "isdn_net: Could not allocate device locals\n");
2448 return NULL;
2450 memset(netdev->local, 0, sizeof(isdn_net_local));
2451 if (name == NULL)
2452 strcpy(netdev->local->name, " ");
2453 else
2454 strcpy(netdev->local->name, name);
2455 netdev->dev.name = netdev->local->name;
2456 netdev->dev.priv = netdev->local;
2457 netdev->dev.init = isdn_net_init;
2458 netdev->local->p_encap = ISDN_NET_ENCAP_RAWIP;
2459 if (master) {
2460 /* Device shall be a slave */
2461 struct net_device *p = (((isdn_net_local *) master->priv)->slave);
2462 struct net_device *q = master;
2464 netdev->local->master = master;
2465 /* Put device at end of slave-chain */
2466 while (p) {
2467 q = p;
2468 p = (((isdn_net_local *) p->priv)->slave);
2470 ((isdn_net_local *) q->priv)->slave = &(netdev->dev);
2471 q->interrupt = 0;
2472 q->tbusy = 0;
2473 q->start = master->start;
2474 } else {
2475 /* Device shall be a master */
2476 if (register_netdev(&netdev->dev) != 0) {
2477 printk(KERN_WARNING "isdn_net: Could not register net-device\n");
2478 kfree(netdev->local);
2479 kfree(netdev);
2480 return NULL;
2483 netdev->local->magic = ISDN_NET_MAGIC;
2485 #ifdef CONFIG_ISDN_PPP
2486 netdev->mp_last = NULL; /* mpqueue is empty */
2487 netdev->ib.next_num = 0;
2488 netdev->ib.last = NULL;
2489 #endif
2490 netdev->queue = netdev->local;
2491 netdev->local->last = netdev->local;
2492 netdev->local->netdev = netdev;
2493 netdev->local->next = netdev->local;
2495 netdev->local->isdn_device = -1;
2496 netdev->local->isdn_channel = -1;
2497 netdev->local->pre_device = -1;
2498 netdev->local->pre_channel = -1;
2499 netdev->local->exclusive = -1;
2500 netdev->local->ppp_slot = -1;
2501 netdev->local->pppbind = -1;
2502 netdev->local->sav_skb = NULL;
2503 netdev->local->first_skb = NULL;
2504 netdev->local->l2_proto = ISDN_PROTO_L2_X75I;
2505 netdev->local->l3_proto = ISDN_PROTO_L3_TRANS;
2506 netdev->local->triggercps = 6000;
2507 netdev->local->slavedelay = 10 * HZ;
2508 netdev->local->srobin = &netdev->dev;
2509 netdev->local->hupflags = ISDN_INHUP; /* Do hangup even on incoming calls */
2510 netdev->local->onhtime = 10; /* Default hangup-time for saving costs
2511 of those who forget configuring this */
2512 netdev->local->dialmax = 1;
2513 netdev->local->flags = ISDN_NET_CBHUP | ISDN_NET_DM_MANUAL; /* Hangup before Callback, manual dial */
2514 netdev->local->cbdelay = 25; /* Wait 5 secs before Callback */
2515 netdev->local->dialtimeout = -1; /* Infinite Dial-Timeout */
2516 netdev->local->dialwait = 5 * HZ; /* Wait 5 sec. after failed dial */
2517 netdev->local->dialstarted = 0; /* Jiffies of last dial-start */
2518 netdev->local->dialwait_timer = 0; /* Jiffies of earliest next dial-start */
2520 /* Put into to netdev-chain */
2521 netdev->next = (void *) dev->netdev;
2522 dev->netdev = netdev;
2523 return netdev->dev.name;
2526 char *
2527 isdn_net_newslave(char *parm)
2529 char *p = strchr(parm, ',');
2530 isdn_net_dev *n;
2531 char newname[10];
2533 if (p) {
2534 /* Slave-Name MUST not be empty */
2535 if (!strlen(p + 1))
2536 return NULL;
2537 strcpy(newname, p + 1);
2538 *p = 0;
2539 /* Master must already exist */
2540 if (!(n = isdn_net_findif(parm)))
2541 return NULL;
2542 /* Master must be a real interface, not a slave */
2543 if (n->local->master)
2544 return NULL;
2545 /* Master must not be started yet */
2546 if (n->dev.start)
2547 return NULL;
2548 return (isdn_net_new(newname, &(n->dev)));
2550 return NULL;
2554 * Set interface-parameters.
2555 * Always set all parameters, so the user-level application is responsible
2556 * for not overwriting existing setups. It has to get the current
2557 * setup first, if only selected parameters are to be changed.
2560 isdn_net_setcfg(isdn_net_ioctl_cfg * cfg)
2562 isdn_net_dev *p = isdn_net_findif(cfg->name);
2563 ulong features;
2564 int i;
2565 int drvidx;
2566 int chidx;
2567 char drvid[25];
2568 #ifdef CONFIG_ISDN_X25
2569 ulong flags;
2570 #endif
2571 if (p) {
2572 isdn_net_local *lp = p->local;
2574 /* See if any registered driver supports the features we want */
2575 features = ((1 << cfg->l2_proto) << ISDN_FEATURE_L2_SHIFT) |
2576 ((1 << cfg->l3_proto) << ISDN_FEATURE_L3_SHIFT);
2577 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2578 if (dev->drv[i])
2579 if ((dev->drv[i]->interface->features & features) == features)
2580 break;
2581 if (i == ISDN_MAX_DRIVERS) {
2582 printk(KERN_WARNING "isdn_net: No driver with selected features\n");
2583 return -ENODEV;
2585 if (lp->p_encap != cfg->p_encap){
2586 #ifdef CONFIG_ISDN_X25
2587 struct concap_proto * cprot = p -> cprot;
2588 #endif
2589 if (p->dev.start) {
2590 printk(KERN_WARNING
2591 "%s: cannot change encap when if is up\n",
2592 lp->name);
2593 return -EBUSY;
2595 #ifdef CONFIG_ISDN_X25
2596 /* delete old encapsulation protocol if present ... */
2597 save_flags(flags);
2598 cli(); /* avoid races with incoming events trying to
2599 call cprot->pops methods */
2600 if( cprot && cprot -> pops )
2601 cprot -> pops -> proto_del ( cprot );
2602 p -> cprot = NULL;
2603 lp -> dops = NULL;
2604 restore_flags(flags);
2605 /* ... , prepare for configuration of new one ... */
2606 switch ( cfg -> p_encap ){
2607 case ISDN_NET_ENCAP_X25IFACE:
2608 lp -> dops = &isdn_concap_reliable_dl_dops;
2610 /* ... and allocate new one ... */
2611 p -> cprot = isdn_concap_new( cfg -> p_encap );
2612 /* p -> cprot == NULL now if p_encap is not supported
2613 by means of the concap_proto mechanism */
2614 /* the protocol is not configured yet; this will
2615 happen later when isdn_net_reset() is called */
2616 #endif
2618 switch ( cfg->p_encap ) {
2619 case ISDN_NET_ENCAP_SYNCPPP:
2620 #ifndef CONFIG_ISDN_PPP
2621 printk(KERN_WARNING "%s: SyncPPP support not configured\n",
2622 lp->name);
2623 return -EINVAL;
2624 #else
2625 p->dev.type = ARPHRD_PPP; /* change ARP type */
2626 p->dev.addr_len = 0;
2627 #endif
2628 break;
2629 case ISDN_NET_ENCAP_X25IFACE:
2630 #ifndef CONFIG_ISDN_X25
2631 printk(KERN_WARNING "%s: isdn-x25 support not configured\n",
2632 p->local->name);
2633 return -EINVAL;
2634 #else
2635 p->dev.type = ARPHRD_X25; /* change ARP type */
2636 p->dev.addr_len = 0;
2637 #endif
2638 break;
2639 default:
2640 if( cfg->p_encap >= 0 &&
2641 cfg->p_encap <= ISDN_NET_ENCAP_MAX_ENCAP )
2642 break;
2643 printk(KERN_WARNING
2644 "%s: encapsulation protocol %d not supported\n",
2645 p->local->name, cfg->p_encap);
2646 return -EINVAL;
2648 if (strlen(cfg->drvid)) {
2649 /* A bind has been requested ... */
2650 char *c,
2653 drvidx = -1;
2654 chidx = -1;
2655 strcpy(drvid, cfg->drvid);
2656 if ((c = strchr(drvid, ','))) {
2657 /* The channel-number is appended to the driver-Id with a comma */
2658 chidx = (int) simple_strtoul(c + 1, &e, 10);
2659 if (e == c)
2660 chidx = -1;
2661 *c = '\0';
2663 for (i = 0; i < ISDN_MAX_DRIVERS; i++)
2664 /* Lookup driver-Id in array */
2665 if (!(strcmp(dev->drvid[i], drvid))) {
2666 drvidx = i;
2667 break;
2669 if ((drvidx == -1) || (chidx == -1))
2670 /* Either driver-Id or channel-number invalid */
2671 return -ENODEV;
2672 } else {
2673 /* Parameters are valid, so get them */
2674 drvidx = lp->pre_device;
2675 chidx = lp->pre_channel;
2677 if (cfg->exclusive > 0) {
2678 int flags;
2680 /* If binding is exclusive, try to grab the channel */
2681 save_flags(flags);
2682 if ((i = isdn_get_free_channel(ISDN_USAGE_NET, lp->l2_proto,
2683 lp->l3_proto,
2684 drvidx,
2685 chidx)) < 0) {
2686 /* Grab failed, because desired channel is in use */
2687 lp->exclusive = -1;
2688 restore_flags(flags);
2689 return -EBUSY;
2691 /* All went ok, so update isdninfo */
2692 dev->usage[i] = ISDN_USAGE_EXCLUSIVE;
2693 isdn_info_update();
2694 restore_flags(flags);
2695 lp->exclusive = i;
2696 } else {
2697 /* Non-exclusive binding or unbind. */
2698 lp->exclusive = -1;
2699 if ((lp->pre_device != -1) && (cfg->exclusive == -1)) {
2700 isdn_unexclusive_channel(lp->pre_device, lp->pre_channel);
2701 isdn_free_channel(lp->pre_device, lp->pre_channel, ISDN_USAGE_NET);
2702 drvidx = -1;
2703 chidx = -1;
2706 strcpy(lp->msn, cfg->eaz);
2707 lp->pre_device = drvidx;
2708 lp->pre_channel = chidx;
2709 lp->onhtime = cfg->onhtime;
2710 lp->charge = cfg->charge;
2711 lp->l2_proto = cfg->l2_proto;
2712 lp->l3_proto = cfg->l3_proto;
2713 lp->cbdelay = cfg->cbdelay;
2714 lp->dialmax = cfg->dialmax;
2715 lp->triggercps = cfg->triggercps;
2716 lp->slavedelay = cfg->slavedelay * HZ;
2717 lp->pppbind = cfg->pppbind;
2718 lp->dialtimeout = cfg->dialtimeout >= 0 ? cfg->dialtimeout * HZ : -1;
2719 lp->dialwait = cfg->dialwait * HZ;
2720 if (cfg->secure)
2721 lp->flags |= ISDN_NET_SECURE;
2722 else
2723 lp->flags &= ~ISDN_NET_SECURE;
2724 if (cfg->cbhup)
2725 lp->flags |= ISDN_NET_CBHUP;
2726 else
2727 lp->flags &= ~ISDN_NET_CBHUP;
2728 switch (cfg->callback) {
2729 case 0:
2730 lp->flags &= ~(ISDN_NET_CALLBACK | ISDN_NET_CBOUT);
2731 break;
2732 case 1:
2733 lp->flags |= ISDN_NET_CALLBACK;
2734 lp->flags &= ~ISDN_NET_CBOUT;
2735 break;
2736 case 2:
2737 lp->flags |= ISDN_NET_CBOUT;
2738 lp->flags &= ~ISDN_NET_CALLBACK;
2739 break;
2741 lp->flags &= ~ISDN_NET_DIALMODE_MASK; /* first all bits off */
2742 if (cfg->dialmode && !(cfg->dialmode & ISDN_NET_DIALMODE_MASK)) {
2743 /* old isdnctrl version, where only 0 or 1 is given */
2744 printk(KERN_WARNING
2745 "Old isdnctrl version detected! Please update.\n");
2746 lp->flags |= ISDN_NET_DM_OFF; /* turn on `off' bit */
2748 else {
2749 lp->flags |= cfg->dialmode; /* turn on selected bits */
2751 if (cfg->chargehup)
2752 lp->hupflags |= ISDN_CHARGEHUP;
2753 else
2754 lp->hupflags &= ~ISDN_CHARGEHUP;
2755 if (cfg->ihup)
2756 lp->hupflags |= ISDN_INHUP;
2757 else
2758 lp->hupflags &= ~ISDN_INHUP;
2759 if (cfg->chargeint > 10) {
2760 lp->hupflags |= ISDN_CHARGEHUP | ISDN_HAVECHARGE | ISDN_MANCHARGE;
2761 lp->chargeint = cfg->chargeint * HZ;
2763 if (cfg->p_encap != lp->p_encap) {
2764 if (cfg->p_encap == ISDN_NET_ENCAP_RAWIP) {
2765 p->dev.hard_header = NULL;
2766 p->dev.hard_header_cache = NULL;
2767 p->dev.header_cache_update = NULL;
2768 p->dev.flags = IFF_NOARP|IFF_POINTOPOINT;
2769 } else {
2770 p->dev.hard_header = isdn_net_header;
2771 if (cfg->p_encap == ISDN_NET_ENCAP_ETHER) {
2772 p->dev.hard_header_cache = lp->org_hhc;
2773 p->dev.header_cache_update = lp->org_hcu;
2774 p->dev.flags = IFF_BROADCAST | IFF_MULTICAST;
2775 } else {
2776 p->dev.hard_header_cache = NULL;
2777 p->dev.header_cache_update = NULL;
2778 p->dev.flags = IFF_NOARP|IFF_POINTOPOINT;
2782 lp->p_encap = cfg->p_encap;
2783 return 0;
2785 return -ENODEV;
2789 * Perform get-interface-parameters.ioctl
2792 isdn_net_getcfg(isdn_net_ioctl_cfg * cfg)
2794 isdn_net_dev *p = isdn_net_findif(cfg->name);
2796 if (p) {
2797 isdn_net_local *lp = p->local;
2799 strcpy(cfg->eaz, lp->msn);
2800 cfg->exclusive = lp->exclusive;
2801 if (lp->pre_device >= 0) {
2802 sprintf(cfg->drvid, "%s,%d", dev->drvid[lp->pre_device],
2803 lp->pre_channel);
2804 } else
2805 cfg->drvid[0] = '\0';
2806 cfg->onhtime = lp->onhtime;
2807 cfg->charge = lp->charge;
2808 cfg->l2_proto = lp->l2_proto;
2809 cfg->l3_proto = lp->l3_proto;
2810 cfg->p_encap = lp->p_encap;
2811 cfg->secure = (lp->flags & ISDN_NET_SECURE) ? 1 : 0;
2812 cfg->callback = 0;
2813 if (lp->flags & ISDN_NET_CALLBACK)
2814 cfg->callback = 1;
2815 if (lp->flags & ISDN_NET_CBOUT)
2816 cfg->callback = 2;
2817 cfg->cbhup = (lp->flags & ISDN_NET_CBHUP) ? 1 : 0;
2818 cfg->dialmode = lp->flags & ISDN_NET_DIALMODE_MASK;
2819 cfg->chargehup = (lp->hupflags & 4) ? 1 : 0;
2820 cfg->ihup = (lp->hupflags & 8) ? 1 : 0;
2821 cfg->cbdelay = lp->cbdelay;
2822 cfg->dialmax = lp->dialmax;
2823 cfg->triggercps = lp->triggercps;
2824 cfg->slavedelay = lp->slavedelay / HZ;
2825 cfg->chargeint = (lp->hupflags & ISDN_CHARGEHUP) ?
2826 (lp->chargeint / HZ) : 0;
2827 cfg->pppbind = lp->pppbind;
2828 cfg->dialtimeout = lp->dialtimeout >= 0 ? lp->dialtimeout / HZ : -1;
2829 cfg->dialwait = lp->dialwait / HZ;
2830 if (lp->slave)
2831 strcpy(cfg->slave, ((isdn_net_local *) lp->slave->priv)->name);
2832 else
2833 cfg->slave[0] = '\0';
2834 if (lp->master)
2835 strcpy(cfg->master, ((isdn_net_local *) lp->master->priv)->name);
2836 else
2837 cfg->master[0] = '\0';
2838 return 0;
2840 return -ENODEV;
2844 * Add a phone-number to an interface.
2847 isdn_net_addphone(isdn_net_ioctl_phone * phone)
2849 isdn_net_dev *p = isdn_net_findif(phone->name);
2850 isdn_net_phone *n;
2852 if (isdn_net_checkwild(phone->phone) && (phone->outgoing & 1))
2853 return -EINVAL;
2854 if (p) {
2855 if (!(n = (isdn_net_phone *) kmalloc(sizeof(isdn_net_phone), GFP_KERNEL)))
2856 return -ENOMEM;
2857 strcpy(n->num, phone->phone);
2858 n->next = p->local->phone[phone->outgoing & 1];
2859 p->local->phone[phone->outgoing & 1] = n;
2860 return 0;
2862 return -ENODEV;
2866 * Copy a string of all phone-numbers of an interface to user space.
2867 * This might sleep and must be called with the isdn semaphore down.
2870 isdn_net_getphones(isdn_net_ioctl_phone * phone, char *phones)
2872 isdn_net_dev *p = isdn_net_findif(phone->name);
2873 int inout = phone->outgoing & 1;
2874 int more = 0;
2875 int count = 0;
2876 isdn_net_phone *n;
2878 if (!p)
2879 return -ENODEV;
2880 inout &= 1;
2881 for (n = p->local->phone[inout]; n; n = n->next) {
2882 if (more) {
2883 put_user(' ', phones++);
2884 count++;
2886 if (copy_to_user(phones, n->num, strlen(n->num) + 1)) {
2887 return -EFAULT;
2889 phones += strlen(n->num);
2890 count += strlen(n->num);
2891 more = 1;
2893 put_user(0, phones);
2894 count++;
2895 return count;
2899 * Copy a string containing the peer's phone number of a connected interface
2900 * to user space.
2903 isdn_net_getpeer(isdn_net_ioctl_phone *phone, isdn_net_ioctl_phone *peer)
2905 isdn_net_dev *p = isdn_net_findif(phone->name);
2906 int ch, dv, idx;
2908 if (!p) return -ENODEV;
2910 * Theoretical race: while this executes, the remote number might
2911 * become invalid (hang up) or change (new connection), resulting
2912 * in (partially) wrong number copied to user. This race
2913 * currently ignored.
2915 ch = p->local->isdn_channel;
2916 dv = p->local->isdn_device;
2917 if(ch<0 && dv<0) return -ENOTCONN;
2918 idx = isdn_dc2minor(dv, ch);
2919 if (idx<0) return -ENODEV;
2920 /* for pre-bound channels, we need this extra check */
2921 if ( strncmp(dev->num[idx],"???",3) == 0 ) return -ENOTCONN;
2922 strncpy(phone->phone,dev->num[idx],ISDN_MSNLEN);
2923 phone->outgoing=USG_OUTGOING(dev->usage[idx]);
2924 if ( copy_to_user(peer,phone,sizeof(*peer)) ) return -EFAULT;
2925 return 0;
2928 * Delete a phone-number from an interface.
2931 isdn_net_delphone(isdn_net_ioctl_phone * phone)
2933 isdn_net_dev *p = isdn_net_findif(phone->name);
2934 int inout = phone->outgoing & 1;
2935 isdn_net_phone *n;
2936 isdn_net_phone *m;
2937 int flags;
2939 if (p) {
2940 save_flags(flags);
2941 cli();
2942 n = p->local->phone[inout];
2943 m = NULL;
2944 while (n) {
2945 if (!strcmp(n->num, phone->phone)) {
2946 if (p->local->dial == n)
2947 p->local->dial = n->next;
2948 if (m)
2949 m->next = n->next;
2950 else
2951 p->local->phone[inout] = n->next;
2952 kfree(n);
2953 restore_flags(flags);
2954 return 0;
2956 m = n;
2957 n = (isdn_net_phone *) n->next;
2959 restore_flags(flags);
2960 return -EINVAL;
2962 return -ENODEV;
2966 * Delete all phone-numbers of an interface.
2968 static int
2969 isdn_net_rmallphone(isdn_net_dev * p)
2971 isdn_net_phone *n;
2972 isdn_net_phone *m;
2973 int flags;
2974 int i;
2976 save_flags(flags);
2977 cli();
2978 for (i = 0; i < 2; i++) {
2979 n = p->local->phone[i];
2980 while (n) {
2981 m = n->next;
2982 kfree(n);
2983 n = m;
2985 p->local->phone[i] = NULL;
2987 p->local->dial = NULL;
2988 restore_flags(flags);
2989 return 0;
2993 * Force a hangup of a network-interface.
2996 isdn_net_force_hangup(char *name)
2998 isdn_net_dev *p = isdn_net_findif(name);
2999 struct net_device *q;
3001 if (p) {
3002 if (p->local->isdn_device < 0)
3003 return 1;
3004 q = p->local->slave;
3005 /* If this interface has slaves, do a hangup for them also. */
3006 while (q) {
3007 isdn_net_hangup(q);
3008 q = (((isdn_net_local *) q->priv)->slave);
3010 isdn_net_hangup(&p->dev);
3011 return 0;
3013 return -ENODEV;
3017 * Helper-function for isdn_net_rm: Do the real work.
3019 static int
3020 isdn_net_realrm(isdn_net_dev * p, isdn_net_dev * q)
3022 int flags;
3024 save_flags(flags);
3025 cli();
3026 if (p->local->master) {
3027 /* If it's a slave, it may be removed even if it is busy. However
3028 * it has to be hung up first.
3030 isdn_net_hangup(&p->dev);
3031 p->dev.start = 0;
3033 if (p->dev.start) {
3034 restore_flags(flags);
3035 return -EBUSY;
3037 #ifdef CONFIG_ISDN_X25
3038 if( p -> cprot && p -> cprot -> pops )
3039 p -> cprot -> pops -> proto_del ( p -> cprot );
3040 #endif
3041 /* Free all phone-entries */
3042 isdn_net_rmallphone(p);
3043 /* If interface is bound exclusive, free channel-usage */
3044 if (p->local->exclusive != -1)
3045 isdn_unexclusive_channel(p->local->pre_device, p->local->pre_channel);
3046 if (p->local->master) {
3047 /* It's a slave-device, so update master's slave-pointer if necessary */
3048 if (((isdn_net_local *) (p->local->master->priv))->slave == &p->dev)
3049 ((isdn_net_local *) (p->local->master->priv))->slave = p->local->slave;
3050 } else {
3051 /* Unregister only if it's a master-device */
3052 p->dev.hard_header_cache = p->local->org_hhc;
3053 p->dev.header_cache_update = p->local->org_hcu;
3054 unregister_netdev(&p->dev);
3056 /* Unlink device from chain */
3057 if (q)
3058 q->next = p->next;
3059 else
3060 dev->netdev = p->next;
3061 if (p->local->slave) {
3062 /* If this interface has a slave, remove it also */
3063 char *slavename = ((isdn_net_local *) (p->local->slave->priv))->name;
3064 isdn_net_dev *n = dev->netdev;
3065 q = NULL;
3066 while (n) {
3067 if (!strcmp(n->local->name, slavename)) {
3068 isdn_net_realrm(n, q);
3069 break;
3071 q = n;
3072 n = (isdn_net_dev *) n->next;
3075 /* If no more net-devices remain, disable auto-hangup timer */
3076 if (dev->netdev == NULL)
3077 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 0);
3078 restore_flags(flags);
3080 kfree(p->local);
3081 kfree(p);
3083 return 0;
3087 * Remove a single network-interface.
3090 isdn_net_rm(char *name)
3092 isdn_net_dev *p;
3093 isdn_net_dev *q;
3095 /* Search name in netdev-chain */
3096 p = dev->netdev;
3097 q = NULL;
3098 while (p) {
3099 if (!strcmp(p->local->name, name))
3100 return (isdn_net_realrm(p, q));
3101 q = p;
3102 p = (isdn_net_dev *) p->next;
3104 /* If no more net-devices remain, disable auto-hangup timer */
3105 if (dev->netdev == NULL)
3106 isdn_timer_ctrl(ISDN_TIMER_NETHANGUP, 0);
3107 return -ENODEV;
3111 * Remove all network-interfaces
3114 isdn_net_rmall(void)
3116 int flags;
3117 int ret;
3119 /* Walk through netdev-chain */
3120 save_flags(flags);
3121 cli();
3122 while (dev->netdev) {
3123 if (!dev->netdev->local->master) {
3124 /* Remove master-devices only, slaves get removed with their master */
3125 if ((ret = isdn_net_realrm(dev->netdev, NULL))) {
3126 restore_flags(flags);
3127 return ret;
3131 dev->netdev = NULL;
3132 restore_flags(flags);
3133 return 0;