isdn_net.c 82.2 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/* $Id: isdn_net.c,v 1.140.6.11 2001/11/06 20:58:28 kai Exp $
Linus Torvalds's avatar
Linus Torvalds committed
2
 *
Linus Torvalds's avatar
Linus Torvalds committed
3 4 5 6 7 8
 * Linux ISDN subsystem, network interfaces and related functions (linklevel).
 *
 * Copyright 1994-1998  by Fritz Elfert (fritz@isdn4linux.de)
 * Copyright 1995,96    by Thinking Objects Software GmbH Wuerzburg
 * Copyright 1995,96    by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
 *
Linus Torvalds's avatar
Linus Torvalds committed
9 10
 * This software may be used and distributed according to the terms
 * of the GNU General Public License, incorporated herein by reference.
Linus Torvalds's avatar
Linus Torvalds committed
11
 *
12 13 14 15 16 17 18 19
 * Data Over Voice (DOV) support added - Guy Ellis 23-Mar-02 
 *                                       guy@traverse.com.au
 * Outgoing calls - looks for a 'V' in first char of dialed number
 * Incoming calls - checks first character of eaz as follows:
 *   Numeric - accept DATA only - original functionality
 *   'V'     - accept VOICE (DOV) only
 *   'B'     - accept BOTH DATA and DOV types
 *
Linus Torvalds's avatar
Linus Torvalds committed
20
 * Jan 2001: fix CISCO HDLC      Bjoern A. Zeeb <i4l@zabbadoz.net>
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23 24
 *           for info on the protocol, see 
 *           http://i4l.zabbadoz.net/i4l/cisco-hdlc.txt
 */

Linus Torvalds's avatar
Linus Torvalds committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <linux/config.h>
#include <linux/isdn.h>
#include <net/arp.h>
#include <net/dst.h>
#include <net/pkt_sched.h>
#include <linux/inetdevice.h>
#include "isdn_common.h"
#include "isdn_net.h"
#ifdef CONFIG_ISDN_PPP
#include "isdn_ppp.h"
#endif
#ifdef CONFIG_ISDN_X25
#include <linux/concap.h>
#include "isdn_concap.h"
#endif

41
enum {
Kai Germaschewski's avatar
Kai Germaschewski committed
42 43 44 45 46 47 48
	ST_NULL,
	ST_OUT_WAIT_DCONN,
	ST_OUT_WAIT_BCONN,
	ST_IN_WAIT_DCONN,
	ST_IN_WAIT_BCONN,
	ST_ACTIVE,
	ST_WAIT_BEFORE_CB,
49
};
Linus Torvalds's avatar
Linus Torvalds committed
50

51 52 53 54 55 56
enum {
	ST_CHARGE_NULL,
	ST_CHARGE_GOT_CINF,  /* got a first charge info */
	ST_CHARGE_HAVE_CINT, /* got a second chare info and thus the timing */
};

57 58
/* keep clear of ISDN_CMD_* and ISDN_STAT_* */
enum {
59 60 61 62 63 64
	EV_NET_DIAL            = 0x200,
	EV_NET_TIMER_IN_DCONN  = 0x201,
	EV_NET_TIMER_IN_BCONN  = 0x202,
	EV_NET_TIMER_OUT_DCONN = 0x203,
	EV_NET_TIMER_OUT_BCONN = 0x204,
	EV_NET_TIMER_CB        = 0x205,
65 66
};

67 68
LIST_HEAD(isdn_net_devs); /* Linked list of isdn_net_dev's */

Linus Torvalds's avatar
Linus Torvalds committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/*
 * Outline of new tbusy handling: 
 *
 * Old method, roughly spoken, consisted of setting tbusy when entering
 * isdn_net_start_xmit() and at several other locations and clearing
 * it from isdn_net_start_xmit() thread when sending was successful.
 *
 * With 2.3.x multithreaded network core, to prevent problems, tbusy should
 * only be set by the isdn_net_start_xmit() thread and only when a tx-busy
 * condition is detected. Other threads (in particular isdn_net_stat_callb())
 * are only allowed to clear tbusy.
 *
 * -HE
 */

/*
 * About SOFTNET:
 * Most of the changes were pretty obvious and basically done by HE already.
 *
 * One problem of the isdn net device code is that is uses struct net_device
 * for masters and slaves. However, only master interface are registered to 
 * the network layer, and therefore, it only makes sense to call netif_* 
 * functions on them.
 *
 * --KG
 */

/* 
 * Find out if the netdevice has been ifup-ed yet.
 * For slaves, look at the corresponding master.
 */
static __inline__ int isdn_net_device_started(isdn_net_dev *n)
{
102
	isdn_net_local *lp = &n->local;
Linus Torvalds's avatar
Linus Torvalds committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	struct net_device *dev;
	
	if (lp->master) 
		dev = lp->master;
	else
		dev = &n->dev;
	return netif_running(dev);
}

/*
 * wake up the network -> net_device queue.
 * For slaves, wake the corresponding master interface.
 */
static __inline__ void isdn_net_device_wake_queue(isdn_net_local *lp)
{
	if (lp->master) 
		netif_wake_queue(lp->master);
	else
		netif_wake_queue(&lp->netdev->dev);
}

/*
 * stop the network -> net_device queue.
 * For slaves, stop the corresponding master interface.
 */
static __inline__ void isdn_net_device_stop_queue(isdn_net_local *lp)
{
	if (lp->master)
		netif_stop_queue(lp->master);
	else
		netif_stop_queue(&lp->netdev->dev);
}

/*
 * find out if the net_device which this lp belongs to (lp can be
 * master or slave) is busy. It's busy iff all (master and slave) 
 * queues are busy
 */
static __inline__ int isdn_net_device_busy(isdn_net_local *lp)
{
	isdn_net_local *nlp;
	isdn_net_dev *nd;
	unsigned long flags;

	if (!isdn_net_lp_busy(lp))
		return 0;

	if (lp->master)
		nd = ((isdn_net_local *) lp->master->priv)->netdev;
	else
		nd = lp->netdev;
	
	spin_lock_irqsave(&nd->queue_lock, flags);
	nlp = lp->next;
	while (nlp != lp) {
		if (!isdn_net_lp_busy(nlp)) {
			spin_unlock_irqrestore(&nd->queue_lock, flags);
			return 0;
		}
		nlp = nlp->next;
	}
	spin_unlock_irqrestore(&nd->queue_lock, flags);
	return 1;
}

static __inline__ void isdn_net_inc_frame_cnt(isdn_net_local *lp)
{
	atomic_inc(&lp->frame_cnt);
	if (isdn_net_device_busy(lp))
		isdn_net_device_stop_queue(lp);
}

static __inline__ void isdn_net_dec_frame_cnt(isdn_net_local *lp)
{
	atomic_dec(&lp->frame_cnt);

	if (!(isdn_net_device_busy(lp))) {
		if (!skb_queue_empty(&lp->super_tx_queue)) {
			queue_task(&lp->tqueue, &tq_immediate);
Linus Torvalds's avatar
Linus Torvalds committed
182
			mark_bh(IMMEDIATE_BH);
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
		} else {
			isdn_net_device_wake_queue(lp);
		}
       }                                                                      
}

static __inline__ void isdn_net_zero_frame_cnt(isdn_net_local *lp)
{
	atomic_set(&lp->frame_cnt, 0);
}

/* For 2.2.x we leave the transmitter busy timeout at 2 secs, just 
 * to be safe.
 * For 2.3.x we push it up to 20 secs, because call establishment
 * (in particular callback) may take such a long time, and we 
 * don't want confusing messages in the log. However, there is a slight
 * possibility that this large timeout will break other things like MPPP,
 * which might rely on the tx timeout. If so, we'll find out this way...
 */

#define ISDN_NET_TX_TIMEOUT (20*HZ) 

/* Prototypes */

int isdn_net_force_dial_lp(isdn_net_local *);
static int isdn_net_start_xmit(struct sk_buff *, struct net_device *);
209
static void do_dialout(isdn_net_local *lp);
Linus Torvalds's avatar
Linus Torvalds committed
210

Linus Torvalds's avatar
Linus Torvalds committed
211 212 213
static void isdn_net_ciscohdlck_connected(isdn_net_local *lp);
static void isdn_net_ciscohdlck_disconnected(isdn_net_local *lp);

214 215
static int isdn_net_handle_event(isdn_net_local *lp, int pr, void *arg);

Linus Torvalds's avatar
Linus Torvalds committed
216
char *isdn_net_revision = "$Revision: 1.140.6.11 $";
Linus Torvalds's avatar
Linus Torvalds committed
217 218 219 220 221 222 223 224

 /*
  * Code for raw-networking over ISDN
  */

static void
isdn_net_unreachable(struct net_device *dev, struct sk_buff *skb, char *reason)
{
225 226 227 228 229 230 231 232
	u_short proto = ntohs(skb->protocol);
	
	printk(KERN_DEBUG "isdn_net: %s: %s, signalling dst_link_failure %s\n",
	       dev->name,
	       (reason != NULL) ? reason : "unknown",
	       (proto != ETH_P_IP) ? "Protocol != ETH_P_IP" : "");
	
	dst_link_failure(skb);
Linus Torvalds's avatar
Linus Torvalds committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
}

static void
isdn_net_reset(struct net_device *dev)
{
#ifdef CONFIG_ISDN_X25
	struct concap_device_ops * dops =
		( (isdn_net_local *) dev->priv ) -> dops;
	struct concap_proto * cprot =
		( (isdn_net_local *) dev->priv ) -> netdev -> cprot;
#endif
	ulong flags;

	/* not sure if the cli() is needed at all --KG */
	save_flags(flags);
	cli();                  /* Avoid glitch on writes to CMD regs */
#ifdef CONFIG_ISDN_X25
	if( cprot && cprot -> pops && dops )
		cprot -> pops -> restart ( cprot, dev, dops );
#endif
	restore_flags(flags);
}

/* Open/initialize the board. */
static int
isdn_net_open(struct net_device *dev)
{
	int i;
	struct net_device *p;
	struct in_device *in_dev;

	/* moved here from isdn_net_reset, because only the master has an
	   interface associated which is supposed to be started. BTW:
	   we need to call netif_start_queue, not netif_wake_queue here */
	netif_start_queue(dev);

	isdn_net_reset(dev);
	/* Fill in the MAC-level header (not needed, but for compatibility... */
	for (i = 0; i < ETH_ALEN - sizeof(u32); i++)
		dev->dev_addr[i] = 0xfc;
	if ((in_dev = dev->ip_ptr) != NULL) {
		/*
		 *      Any address will do - we take the first
		 */
		struct in_ifaddr *ifa = in_dev->ifa_list;
		if (ifa != NULL)
			memcpy(dev->dev_addr+2, &ifa->ifa_local, 4);
	}

	/* If this interface has slaves, start them also */

	if ((p = (((isdn_net_local *) dev->priv)->slave))) {
		while (p) {
			isdn_net_reset(p);
			p = (((isdn_net_local *) p->priv)->slave);
		}
	}
	isdn_MOD_INC_USE_COUNT();
	return 0;
}

/*
 * Assign an ISDN-channel to a net-interface
 */
static void
isdn_net_bind_channel(isdn_net_local * lp, int idx)
{
	ulong flags;

	save_flags(flags);
	cli();
	lp->flags |= ISDN_NET_CONNECTED;
305
	lp->isdn_slot = idx;
306 307
	isdn_slot_set_rx_netdev(lp->isdn_slot, lp->netdev);
	isdn_slot_set_st_netdev(lp->isdn_slot, lp->netdev);
Linus Torvalds's avatar
Linus Torvalds committed
308 309 310 311 312 313 314 315 316 317 318 319 320
	restore_flags(flags);
}

/*
 * unbind a net-interface (resets interface after an error)
 */
static void
isdn_net_unbind_channel(isdn_net_local * lp)
{
	ulong flags;

	save_flags(flags);
	cli();
Linus Torvalds's avatar
Linus Torvalds committed
321 322
	skb_queue_purge(&lp->super_tx_queue);

Linus Torvalds's avatar
Linus Torvalds committed
323 324 325 326 327 328 329
	if (!lp->master) {	/* reset only master device */
		/* Moral equivalent of dev_purge_queues():
		   BEWARE! This chunk of code cannot be called from hardware
		   interrupt handler. I hope it is true. --ANK
		 */
		qdisc_reset(lp->netdev->dev.qdisc);
	}
Kai Germaschewski's avatar
Kai Germaschewski committed
330 331 332 333 334 335
	lp->dialstate = ST_NULL;
	if (lp->isdn_slot >= 0) {
		isdn_slot_set_rx_netdev(lp->isdn_slot, NULL);
		isdn_slot_set_st_netdev(lp->isdn_slot, NULL);
		isdn_slot_free(lp->isdn_slot, ISDN_USAGE_NET);
	}
Linus Torvalds's avatar
Linus Torvalds committed
336
	lp->flags &= ~ISDN_NET_CONNECTED;
337
	lp->isdn_slot = -1;
Linus Torvalds's avatar
Linus Torvalds committed
338 339 340 341 342

	restore_flags(flags);
}

/*
343
 * Perform auto-hangup for net-interfaces.
Linus Torvalds's avatar
Linus Torvalds committed
344 345 346 347 348 349 350 351
 *
 * auto-hangup:
 * Increment idle-counter (this counter is reset on any incoming or
 * outgoing packet), if counter exceeds configured limit either do a
 * hangup immediately or - if configured - wait until just before the next
 * charge-info.
 */

352
static void isdn_net_hup_timer(unsigned long data)
353 354 355 356 357
{
	isdn_net_local *lp = (isdn_net_local *) data;

	if (!(lp->flags & ISDN_NET_CONNECTED) || lp->dialstate != ST_ACTIVE) {
		isdn_BUG();
358
		return;
359 360 361 362 363 364
	}
	
	dbg_net_dial("%s: huptimer %d, onhtime %d, chargetime %ld, chargeint %d\n",
		     l->name, l->huptimer, l->onhtime, l->chargetime, l->chargeint);

	if (!(lp->onhtime))
365
		return;
366 367
	
	if (lp->huptimer++ <= lp->onhtime)
368
		goto mod_timer;
369 370 371 372 373 374 375

	if (lp->hupflags & ISDN_MANCHARGE && lp->hupflags & ISDN_CHARGEHUP) {
		while (time_after(jiffies, lp->chargetime + lp->chargeint))
			lp->chargetime += lp->chargeint;
		
		if (time_after(jiffies, lp->chargetime + lp->chargeint - 2 * HZ)) {
			if (lp->outgoing || lp->hupflags & ISDN_INHUP) {
376
				isdn_net_hangup(lp);
377
				return;
378 379 380 381 382 383
			}
		}
	} else if (lp->outgoing) {
		if (lp->hupflags & ISDN_CHARGEHUP) {
			if (lp->charge_state != ST_CHARGE_HAVE_CINT) {
				dbg_net_dial("%s: did not get CINT\n", lp->name);
384
				isdn_net_hangup(lp);
385
				return;
386 387 388
			} else if (time_after(jiffies, lp->chargetime + lp->chargeint)) {
				dbg_net_dial("%s: chtime = %lu, chint = %d\n",
					     lp->name, lp->chargetime, lp->chargeint);
389
				isdn_net_hangup(lp);
390
				return;
391 392 393
			}
		}
	} else if (lp->hupflags & ISDN_INHUP) {
394
		isdn_net_hangup(lp);
395
		return;
396
	}
397 398
 mod_timer:
	mod_timer(&lp->hup_timer, lp->hup_timer.expires + HZ);
399 400
}

Linus Torvalds's avatar
Linus Torvalds committed
401 402 403 404 405
static void isdn_net_lp_disconnected(isdn_net_local *lp)
{
	isdn_net_rm_from_bundle(lp);
}

406 407 408 409 410 411
static void isdn_net_connected(isdn_net_local *lp)
{
#ifdef CONFIG_ISDN_X25
	struct concap_proto *cprot = lp -> netdev -> cprot;
	struct concap_proto_ops *pops = cprot ? cprot -> pops : 0;
#endif
Kai Germaschewski's avatar
Kai Germaschewski committed
412
	lp->dialstate = ST_ACTIVE;
413 414 415
	lp->hup_timer.expires = jiffies + HZ;
	add_timer(&lp->hup_timer);

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	if (lp->p_encap == ISDN_NET_ENCAP_CISCOHDLCK)
		isdn_net_ciscohdlck_connected(lp);
	if (lp->p_encap != ISDN_NET_ENCAP_SYNCPPP) {
		if (lp->master) { /* is lp a slave? */
			isdn_net_dev *nd = ((isdn_net_local *)lp->master->priv)->netdev;
			isdn_net_add_to_bundle(nd, lp);
		}
	}
	printk(KERN_INFO "isdn_net: %s connected\n", lp->name);
	/* If first Chargeinfo comes before B-Channel connect,
	 * we correct the timestamp here.
	 */
	lp->chargetime = jiffies;
	
	/* reset dial-timeout */
	lp->dialstarted = 0;
	lp->dialwait_timer = 0;
	
434 435 436 437
	lp->transcount = 0;
	lp->cps = 0;
	lp->last_jiffies = jiffies;

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
#ifdef CONFIG_ISDN_PPP
	if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
		isdn_ppp_wakeup_daemon(lp);
#endif
#ifdef CONFIG_ISDN_X25
	/* try if there are generic concap receiver routines */
	if( pops )
		if( pops->connect_ind)
			pops->connect_ind(cprot);
#endif /* CONFIG_ISDN_X25 */
	/* ppp needs to do negotiations first */
	if (lp->p_encap != ISDN_NET_ENCAP_SYNCPPP)
		isdn_net_device_wake_queue(lp);
}

Linus Torvalds's avatar
Linus Torvalds committed
453 454 455 456 457 458 459 460 461
/*
 * Handle status-messages from ISDN-interfacecard.
 * This function is called from within the main-status-dispatcher
 * isdn_status_callback, which itself is called from the low-level driver.
 * Return: 1 = Event handled, 0 = not for us or unknown Event.
 */
int
isdn_net_stat_callback(int idx, isdn_ctrl *c)
{
462
	isdn_net_dev *p = isdn_slot_st_netdev(idx);
463
	isdn_net_local *lp;
Linus Torvalds's avatar
Linus Torvalds committed
464 465
	int cmd = c->command;

Kai Germaschewski's avatar
Kai Germaschewski committed
466 467
	if (!p) {
		HERE;
468
		return 0;
Kai Germaschewski's avatar
Kai Germaschewski committed
469
	}
470
	lp = &p->local;
471

472
	return isdn_net_handle_event(lp, cmd, c);
Linus Torvalds's avatar
Linus Torvalds committed
473 474
}

475 476 477
static void
isdn_net_dial_timer(unsigned long data)
{
Kai Germaschewski's avatar
Kai Germaschewski committed
478
	isdn_net_local *lp = (isdn_net_local *) data;
479 480 481 482

	isdn_net_handle_event(lp, lp->dial_event, NULL);
}

483 484 485
/* Initiate dialout. Set phone-number-pointer to first number
 * of interface.
 */
486
static void
487 488 489 490 491 492
init_dialout(isdn_net_local *lp)
{
	unsigned long flags;

	save_flags(flags);
	cli();
493
	lp->dial = 0;
494 495 496 497 498 499 500 501
	restore_flags(flags);

	if (lp->dialtimeout > 0 &&
	    (lp->dialstarted == 0 || 
	     time_after(jiffies, lp->dialstarted + lp->dialtimeout + lp->dialwait))) {
		lp->dialstarted = jiffies;
		lp->dialwait_timer = 0;
	}
502
	lp->dialretry = 0;
503
	do_dialout(lp);
504 505 506 507 508 509
}

/* Setup interface, dial current phone-number, switch to next number.
 * If list of phone-numbers is exhausted, increment
 * retry-counter.
 */
510
static void
511 512
do_dialout(isdn_net_local *lp)
{
513
	int i;
514
	unsigned long flags;
515
	struct isdn_net_phone *phone;
516 517 518 519 520 521 522 523 524 525
	struct dial_info dial = {
		.l2_proto = lp->l2_proto,
		.l3_proto = lp->l3_proto,
		.si1      = 7,
		.si2      = 0,
		.msn      = lp->msn,
	};

	if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF)
		return;
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
	
	if (list_empty(&lp->phone[1]))
		return;

	i = 0;
	list_for_each_entry(phone, &lp->phone[1], list) {
		if (i++ == lp->dial)
			goto found;
	}
	/* otherwise start in front */
	phone = list_entry(lp->phone[1].next, struct isdn_net_phone, list);
	lp->dial = 0;
	lp->dialretry++;
	
	if (lp->dialretry > lp->dialmax) {
		if (lp->dialtimeout == 0) {
			lp->dialwait_timer = jiffies + lp->dialwait;
			lp->dialstarted = 0;
		}
		isdn_net_hangup(lp);
		return;
	}

 found:
	lp->dial++;
	dial.phone = phone->num;
552

553 554 555 556 557 558
	if(lp->dialtimeout > 0 &&
	   time_after(jiffies, lp->dialstarted + lp->dialtimeout)) {
		   lp->dialwait_timer = jiffies + lp->dialwait;
		   lp->dialstarted = 0;
		   isdn_net_hangup(lp);
		   return;
559
	}
560 561 562 563 564
	/*
	 * Switch to next number or back to start if at end of list.
	 */
	isdn_slot_dial(lp->isdn_slot, &dial);

565 566
	lp->huptimer = 0;
	lp->outgoing = 1;
567 568 569 570 571
	if (lp->chargeint)
		lp->charge_state = ST_CHARGE_HAVE_CINT;
	else
		lp->charge_state = ST_CHARGE_NULL;

572
	if (lp->cbdelay && (lp->flags & ISDN_NET_CBOUT)) {
Kai Germaschewski's avatar
Kai Germaschewski committed
573
		lp->dial_timer.expires = jiffies + lp->cbdelay;
574 575 576 577 578 579 580
		lp->dial_event = EV_NET_TIMER_CB;
	} else {
		lp->dial_timer.expires = jiffies + 10 * HZ;
		lp->dial_event = EV_NET_TIMER_OUT_DCONN;
	}
	lp->dialstate = ST_OUT_WAIT_DCONN;
	add_timer(&lp->dial_timer);
581 582
}

583 584 585
/* For EV_NET_DIAL, returns 1 if timer callback is needed 
 * For ISDN_STAT_*, returns 1 if event was for us 
 */
586 587
static int
isdn_net_handle_event(isdn_net_local *lp, int pr, void *arg)
Linus Torvalds's avatar
Linus Torvalds committed
588
{
589 590 591 592
#ifdef CONFIG_ISDN_X25
	struct concap_proto *cprot = lp -> netdev -> cprot;
	struct concap_proto_ops *pops = cprot ? cprot -> pops : 0;
#endif
593
	isdn_net_dev *p = lp->netdev;
594
	isdn_ctrl *c = arg;
Linus Torvalds's avatar
Linus Torvalds committed
595 596
	isdn_ctrl cmd;

Kai Germaschewski's avatar
Kai Germaschewski committed
597
	dbg_net_dial("%s: dialstate=%d pr=%#x\n", lp->name, lp->dialstate,pr);
598 599

	switch (lp->dialstate) {
Kai Germaschewski's avatar
Kai Germaschewski committed
600
	case ST_ACTIVE:
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
		switch (pr) {
		case ISDN_STAT_BSENT:
			/* A packet has successfully been sent out */
			if (lp->flags & ISDN_NET_CONNECTED) {
				isdn_net_dec_frame_cnt(lp);
				lp->stats.tx_packets++;
				lp->stats.tx_bytes += c->parm.length;
				return 1;
			}
			break;
		case ISDN_STAT_DHUP:
			/* Either D-Channel-hangup or error during dialout */
#ifdef CONFIG_ISDN_X25 // FIXME handle != ST_0?
			/* If we are not connencted then dialing had
			   failed. If there are generic encap protocol
			   receiver routines signal the closure of
			   the link*/
			
			if (!(lp->flags & ISDN_NET_CONNECTED)
			    && pops && pops->disconn_ind)
				pops -> disconn_ind(cprot);
#endif /* CONFIG_ISDN_X25 */
			if (lp->flags & ISDN_NET_CONNECTED) {
				if (lp->p_encap == ISDN_NET_ENCAP_CISCOHDLCK)
					isdn_net_ciscohdlck_disconnected(lp);
#ifdef CONFIG_ISDN_PPP
				if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
					isdn_ppp_free(lp);
#endif
				isdn_net_lp_disconnected(lp);
				isdn_slot_all_eaz(lp->isdn_slot);
				printk(KERN_INFO "%s: remote hangup\n", lp->name);
				printk(KERN_INFO "%s: Chargesum is %d\n", lp->name,
				       lp->charge);
				isdn_net_unbind_channel(lp);
				return 1;
			}
			break;
#ifdef CONFIG_ISDN_X25 // FIXME handle != ST_0?
		case ISDN_STAT_BHUP:
			/* B-Channel-hangup */
			/* try if there are generic encap protocol
			   receiver routines and signal the closure of
			   the link */
			if( pops  &&  pops -> disconn_ind ){
				pops -> disconn_ind(cprot);
				return 1;
			}
			break;
#endif /* CONFIG_ISDN_X25 */
		case ISDN_STAT_CINF:
			/* Charge-info from TelCo. Calculate interval between
			 * charge-infos and set timestamp for last info for
			 * usage by isdn_net_autohup()
			 */
			lp->charge++;
657 658 659 660 661 662 663 664 665 666
			switch (lp->charge_state) {
			case ST_CHARGE_NULL:
				lp->charge_state = ST_CHARGE_GOT_CINF;
				break;
			case ST_CHARGE_GOT_CINF:
				lp->charge_state = ST_CHARGE_HAVE_CINT;
				/* fall through */
			case ST_CHARGE_HAVE_CINT:
				lp->chargeint = jiffies - lp->chargetime - 2 * HZ;
				break;
667 668
			}
			lp->chargetime = jiffies;
669
			dbg_net_dial("%s: got CINF\n", lp->name);
670 671
			return 1;
		}
672
		break;
673
	case ST_OUT_WAIT_DCONN:
674
		switch (pr) {
675
		case EV_NET_TIMER_OUT_DCONN:
676 677 678
			/* try again */
			do_dialout(lp);
			return 1;
679 680 681 682
		case EV_NET_TIMER_CB:
			/* Remote does callback. Hangup after cbdelay, 
			 * then wait for incoming call */
			printk(KERN_INFO "%s: hangup waiting for callback ...\n", lp->name);
683
			isdn_net_hangup(lp);
684 685
			return 1;
		case ISDN_STAT_DCONN:
686
			/* Got D-Channel-Connect, send B-Channel-request */
687
			del_timer(&lp->dial_timer);
688
			lp->dialstate = ST_OUT_WAIT_BCONN;
689
			isdn_slot_command(lp->isdn_slot, ISDN_CMD_ACCEPTB, &cmd);
690 691 692
			lp->dial_timer.expires = jiffies + 10 * HZ;
			lp->dial_event = EV_NET_TIMER_OUT_BCONN;
			add_timer(&lp->dial_timer);
693
			return 1;
Kai Germaschewski's avatar
Kai Germaschewski committed
694 695 696 697 698 699
		case ISDN_STAT_DHUP:
			del_timer(&lp->dial_timer);
			isdn_slot_all_eaz(lp->isdn_slot);
			printk(KERN_INFO "%s: remote hangup\n", lp->name);
			isdn_net_unbind_channel(lp);
			return 1;
700 701
		}
		break;
702
	case ST_OUT_WAIT_BCONN:
703
		switch (pr) {
704 705 706
		case EV_NET_TIMER_OUT_BCONN:
			/* try again */
			do_dialout(lp);
707
			return 1;
708
		case ISDN_STAT_BCONN:
709
			del_timer(&lp->dial_timer);
710 711 712
			isdn_slot_set_usage(lp->isdn_slot, isdn_slot_usage(lp->isdn_slot) | ISDN_USAGE_OUTGOING);
			isdn_net_connected(lp);
			return 1;
Kai Germaschewski's avatar
Kai Germaschewski committed
713 714 715 716 717 718
		case ISDN_STAT_DHUP:
			del_timer(&lp->dial_timer);
			isdn_slot_all_eaz(lp->isdn_slot);
			printk(KERN_INFO "%s: remote hangup\n", lp->name);
			isdn_net_unbind_channel(lp);
			return 1;
719 720
		}
		break;
721
	case ST_IN_WAIT_DCONN:
722
		switch (pr) {
723
		case EV_NET_TIMER_IN_DCONN:
724
			isdn_net_hangup(lp);
Kai Germaschewski's avatar
Kai Germaschewski committed
725
			return 1;
726
		case ISDN_STAT_DCONN:
727
			del_timer(&lp->dial_timer);
728
			lp->dialstate = ST_IN_WAIT_BCONN;
729 730 731 732
			isdn_slot_command(lp->isdn_slot, ISDN_CMD_ACCEPTB, &cmd);
			lp->dial_timer.expires = jiffies + 10 * HZ;
			lp->dial_event = EV_NET_TIMER_IN_BCONN;
			add_timer(&lp->dial_timer);
733
			return 1;
Kai Germaschewski's avatar
Kai Germaschewski committed
734 735 736 737 738 739
		case ISDN_STAT_DHUP:
			del_timer(&lp->dial_timer);
			isdn_slot_all_eaz(lp->isdn_slot);
			printk(KERN_INFO "%s: remote hangup\n", lp->name);
			isdn_net_unbind_channel(lp);
			return 1;
740 741
		}
		break;
742
	case ST_IN_WAIT_BCONN:
743
		switch (pr) {
744
		case EV_NET_TIMER_IN_BCONN:
745
			isdn_net_hangup(lp);
746
			break;
747
		case ISDN_STAT_BCONN:
748
			del_timer(&lp->dial_timer);
749 750 751
			isdn_slot_set_rx_netdev(lp->isdn_slot, p);
			isdn_net_connected(lp);
			return 1;
Kai Germaschewski's avatar
Kai Germaschewski committed
752 753 754 755 756 757
		case ISDN_STAT_DHUP:
			del_timer(&lp->dial_timer);
			isdn_slot_all_eaz(lp->isdn_slot);
			printk(KERN_INFO "%s: remote hangup\n", lp->name);
			isdn_net_unbind_channel(lp);
			return 1;
758 759
		}
		break;
760
	case ST_WAIT_BEFORE_CB:
761
		switch (pr) {
762
		case EV_NET_TIMER_CB:
763
			/* Callback Delay */
764
			init_dialout(lp);
765
			return 1;
Linus Torvalds's avatar
Linus Torvalds committed
766
		}
767 768 769 770 771
		break;
	default:
		isdn_BUG();
		break;
	}
Kai Germaschewski's avatar
Kai Germaschewski committed
772
	printk("NOT HANDLED?\n");
773 774 775
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
776 777 778 779
/*
 * Perform hangup for a net-interface.
 */
void
780
isdn_net_hangup(isdn_net_local *lp)
Linus Torvalds's avatar
Linus Torvalds committed
781 782 783 784 785 786 787
{
	isdn_ctrl cmd;
#ifdef CONFIG_ISDN_X25
	struct concap_proto *cprot = lp -> netdev -> cprot;
	struct concap_proto_ops *pops = cprot ? cprot -> pops : 0;
#endif

788
	del_timer_sync(&lp->hup_timer);
Linus Torvalds's avatar
Linus Torvalds committed
789 790 791 792 793 794 795
	if (lp->flags & ISDN_NET_CONNECTED) {
		if (lp->slave != NULL) {
			isdn_net_local *slp = (isdn_net_local *)lp->slave->priv;
			if (slp->flags & ISDN_NET_CONNECTED) {
				printk(KERN_INFO
					"isdn_net: hang up slave %s before %s\n",
					slp->name, lp->name);
796
				isdn_net_hangup(slp);
Linus Torvalds's avatar
Linus Torvalds committed
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
			}
		}
		printk(KERN_INFO "isdn_net: local hangup %s\n", lp->name);
#ifdef CONFIG_ISDN_PPP
		if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
			isdn_ppp_free(lp);
#endif
		isdn_net_lp_disconnected(lp);
#ifdef CONFIG_ISDN_X25
		/* try if there are generic encap protocol
		   receiver routines and signal the closure of
		   the link */
		if( pops && pops -> disconn_ind )
		  pops -> disconn_ind(cprot);
#endif /* CONFIG_ISDN_X25 */

813
		isdn_slot_command(lp->isdn_slot, ISDN_CMD_HANGUP, &cmd);
Linus Torvalds's avatar
Linus Torvalds committed
814
		printk(KERN_INFO "%s: Chargesum is %d\n", lp->name, lp->charge);
815
		isdn_slot_all_eaz(lp->isdn_slot);
Linus Torvalds's avatar
Linus Torvalds committed
816 817 818 819
	}
	isdn_net_unbind_channel(lp);
}

820 821 822 823 824 825 826 827 828 829 830
void
isdn_net_hangup_all()
{
	struct list_head *l;

	list_for_each(l, &isdn_net_devs) {
		isdn_net_dev *p = list_entry(l, isdn_net_dev, global_list);
		isdn_net_hangup(&p->local);
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
typedef struct {
	unsigned short source;
	unsigned short dest;
} ip_ports;

static void
isdn_net_log_skb(struct sk_buff * skb, isdn_net_local * lp)
{
	u_char *p = skb->nh.raw; /* hopefully, this was set correctly */
	unsigned short proto = ntohs(skb->protocol);
	int data_ofs;
	ip_ports *ipp;
	char addinfo[100];

	addinfo[0] = '\0';
	/* This check stolen from 2.1.72 dev_queue_xmit_nit() */
	if (skb->nh.raw < skb->data || skb->nh.raw >= skb->tail) {
		/* fall back to old isdn_net_log_packet method() */
		char * buf = skb->data;

		printk(KERN_DEBUG "isdn_net: protocol %04x is buggy, dev %s\n", skb->protocol, lp->name);
		p = buf;
		proto = ETH_P_IP;
		switch (lp->p_encap) {
			case ISDN_NET_ENCAP_IPTYP:
				proto = ntohs(*(unsigned short *) &buf[0]);
				p = &buf[2];
				break;
			case ISDN_NET_ENCAP_ETHER:
				proto = ntohs(*(unsigned short *) &buf[12]);
				p = &buf[14];
				break;
			case ISDN_NET_ENCAP_CISCOHDLC:
				proto = ntohs(*(unsigned short *) &buf[2]);
				p = &buf[4];
				break;
#ifdef CONFIG_ISDN_PPP
			case ISDN_NET_ENCAP_SYNCPPP:
				proto = ntohs(skb->protocol);
				p = &buf[IPPP_MAX_HEADER];
				break;
#endif
		}
	}
	data_ofs = ((p[0] & 15) * 4);
	switch (proto) {
		case ETH_P_IP:
			switch (p[9]) {
				case 1:
					strcpy(addinfo, " ICMP");
					break;
				case 2:
					strcpy(addinfo, " IGMP");
					break;
				case 4:
					strcpy(addinfo, " IPIP");
					break;
				case 6:
					ipp = (ip_ports *) (&p[data_ofs]);
					sprintf(addinfo, " TCP, port: %d -> %d", ntohs(ipp->source),
						ntohs(ipp->dest));
					break;
				case 8:
					strcpy(addinfo, " EGP");
					break;
				case 12:
					strcpy(addinfo, " PUP");
					break;
				case 17:
					ipp = (ip_ports *) (&p[data_ofs]);
					sprintf(addinfo, " UDP, port: %d -> %d", ntohs(ipp->source),
						ntohs(ipp->dest));
					break;
				case 22:
					strcpy(addinfo, " IDP");
					break;
			}
			printk(KERN_INFO
				"OPEN: %d.%d.%d.%d -> %d.%d.%d.%d%s\n",

			       p[12], p[13], p[14], p[15],
			       p[16], p[17], p[18], p[19],
			       addinfo);
			break;
		case ETH_P_ARP:
			printk(KERN_INFO
				"OPEN: ARP %d.%d.%d.%d -> *.*.*.* ?%d.%d.%d.%d\n",
			       p[14], p[15], p[16], p[17],
			       p[24], p[25], p[26], p[27]);
			break;
	}
}

/*
 * this function is used to send supervisory data, i.e. data which was
 * not received from the network layer, but e.g. frames from ipppd, CCP
 * reset frames etc.
 */
void isdn_net_write_super(isdn_net_local *lp, struct sk_buff *skb)
{
Linus Torvalds's avatar
Linus Torvalds committed
931
	if (in_irq()) {
Linus Torvalds's avatar
Linus Torvalds committed
932 933 934 935
		// we can't grab the lock from irq context, 
		// so we just queue the packet
		skb_queue_tail(&lp->super_tx_queue, skb); 
		queue_task(&lp->tqueue, &tq_immediate);
Linus Torvalds's avatar
Linus Torvalds committed
936
		mark_bh(IMMEDIATE_BH);
Linus Torvalds's avatar
Linus Torvalds committed
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
		return;
	}

	spin_lock_bh(&lp->xmit_lock);
	if (!isdn_net_lp_busy(lp)) {
		isdn_net_writebuf_skb(lp, skb);
	} else {
		skb_queue_tail(&lp->super_tx_queue, skb);
	}
	spin_unlock_bh(&lp->xmit_lock);
}

/*
 * called from tq_immediate
 */
static void isdn_net_softint(void *private)
{
	isdn_net_local *lp = private;
	struct sk_buff *skb;

	spin_lock_bh(&lp->xmit_lock);
	while (!isdn_net_lp_busy(lp)) {
		skb = skb_dequeue(&lp->super_tx_queue);
		if (!skb)
			break;
		isdn_net_writebuf_skb(lp, skb);                                
	}
	spin_unlock_bh(&lp->xmit_lock);
}

/* 
 * all frames sent from the (net) LL to a HL driver should go via this function
 * it's serialized by the caller holding the lp->xmit_lock spinlock
 */
void isdn_net_writebuf_skb(isdn_net_local *lp, struct sk_buff *skb)
{
	int ret;
	int len = skb->len;     /* save len */

	/* before obtaining the lock the caller should have checked that
	   the lp isn't busy */
	if (isdn_net_lp_busy(lp)) {
Kai Germaschewski's avatar
Kai Germaschewski committed
979
		isdn_BUG();
Linus Torvalds's avatar
Linus Torvalds committed
980 981 982 983
		goto error;
	}

	if (!(lp->flags & ISDN_NET_CONNECTED)) {
Kai Germaschewski's avatar
Kai Germaschewski committed
984
		isdn_BUG();
Linus Torvalds's avatar
Linus Torvalds committed
985 986
		goto error;
	}
987
	ret = isdn_slot_write(lp->isdn_slot, skb);
Linus Torvalds's avatar
Linus Torvalds committed
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	if (ret != len) {
		/* we should never get here */
		printk(KERN_WARNING "%s: HL driver queue full\n", lp->name);
		goto error;
	}
	
	lp->transcount += len;
	isdn_net_inc_frame_cnt(lp);
	return;

 error:
	dev_kfree_skb(skb);
	lp->stats.tx_errors++;

}


/*
 *  Helper function for isdn_net_start_xmit.
 *  When called, the connection is already established.
 *  Based on cps-calculation, check if device is overloaded.
 *  If so, and if a slave exists, trigger dialing for it.
 *  If any slave is online, deliver packets using a simple round robin
 *  scheme.
 *
 *  Return: 0 on success, !0 on failure.
 */

static int
isdn_net_xmit(struct net_device *ndev, struct sk_buff *skb)
{
	isdn_net_dev *nd;
	isdn_net_local *slp;
	isdn_net_local *lp = (isdn_net_local *) ndev->priv;
	int retv = 0;

	if (((isdn_net_local *) (ndev->priv))->master) {
1025
		isdn_BUG();
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
		dev_kfree_skb(skb);
		return 0;
	}

	/* For the other encaps the header has already been built */
#ifdef CONFIG_ISDN_PPP
	if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
		return isdn_ppp_xmit(skb, ndev);
	}
#endif
	nd = ((isdn_net_local *) ndev->priv)->netdev;
	lp = isdn_net_get_locked_lp(nd);
	if (!lp) {
		printk(KERN_WARNING "%s: all channels busy - requeuing!\n", ndev->name);
		return 1;
	}
	/* we have our lp locked from now on */

	/* Reset hangup-timeout */
	lp->huptimer = 0; // FIXME?
	isdn_net_writebuf_skb(lp, skb);
	spin_unlock_bh(&lp->xmit_lock);

	/* the following stuff is here for backwards compatibility.
	 * in future, start-up and hangup of slaves (based on current load)
	 * should move to userspace and get based on an overall cps
	 * calculation
	 */
1054 1055 1056 1057 1058 1059 1060 1061
	if (jiffies != lp->last_jiffies) {
		lp->cps = lp->transcount * HZ / (jiffies - lp->last_jiffies);
		lp->last_jiffies = jiffies;
		lp->transcount = 0;
	}
	if (dev->net_verbose > 3)
		printk(KERN_DEBUG "%s: %d bogocps\n", lp->name, lp->cps);

Linus Torvalds's avatar
Linus Torvalds committed
1062 1063 1064 1065 1066 1067 1068 1069
	if (lp->cps > lp->triggercps) {
		if (lp->slave) {
			if (!lp->sqfull) {
				/* First time overload: set timestamp only */
				lp->sqfull = 1;
				lp->sqfull_stamp = jiffies;
			} else {
				/* subsequent overload: if slavedelay exceeded, start dialing */
Linus Torvalds's avatar
Linus Torvalds committed
1070
				if (time_after(jiffies, lp->sqfull_stamp + lp->slavedelay)) {
Linus Torvalds's avatar
Linus Torvalds committed
1071 1072 1073 1074 1075 1076 1077 1078
					slp = lp->slave->priv;
					if (!(slp->flags & ISDN_NET_CONNECTED)) {
						isdn_net_force_dial_lp((isdn_net_local *) lp->slave->priv);
					}
				}
			}
		}
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
1079
		if (lp->sqfull && time_after(jiffies, lp->sqfull_stamp + lp->slavedelay + (10 * HZ))) {
Linus Torvalds's avatar
Linus Torvalds committed
1080 1081 1082
			lp->sqfull = 0;
		}
		/* this is a hack to allow auto-hangup for slaves on moderate loads */
1083
		nd->queue = &nd->local;
Linus Torvalds's avatar
Linus Torvalds committed
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
	}

	return retv;

}

static void
isdn_net_adjust_hdr(struct sk_buff *skb, struct net_device *dev)
{
	isdn_net_local *lp = (isdn_net_local *) dev->priv;
	if (!skb)
		return;
	if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
		int pullsize = (ulong)skb->nh.raw - (ulong)skb->data - ETH_HLEN;
		if (pullsize > 0) {
			printk(KERN_DEBUG "isdn_net: Pull junk %d\n", pullsize);
			skb_pull(skb, pullsize);
		}
	}
}


void isdn_net_tx_timeout(struct net_device * ndev)
{
	isdn_net_local *lp = (isdn_net_local *) ndev->priv;

	printk(KERN_WARNING "isdn_tx_timeout dev %s dialstate %d\n", ndev->name, lp->dialstate);
Kai Germaschewski's avatar
Kai Germaschewski committed
1111
	if (lp->dialstate == ST_ACTIVE){
Linus Torvalds's avatar
Linus Torvalds committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
		lp->stats.tx_errors++;
                /*
		 * There is a certain probability that this currently
		 * works at all because if we always wake up the interface,
		 * then upper layer will try to send the next packet
		 * immediately. And then, the old clean_up logic in the
		 * driver will hopefully continue to work as it used to do.
		 *
		 * This is rather primitive right know, we better should
		 * clean internal queues here, in particular for multilink and
		 * ppp, and reset HL driver's channel, too.   --HE
		 *
		 * actually, this may not matter at all, because ISDN hardware
		 * should not see transmitter hangs at all IMO
		 * changed KERN_DEBUG to KERN_WARNING to find out if this is 
		 * ever called   --KG
		 */
	}
	ndev->trans_start = jiffies;
	netif_wake_queue(ndev);
}

/*
 * Try sending a packet.
 * If this interface isn't connected to a ISDN-Channel, find a free channel,
 * and start dialing.
 */
static int
isdn_net_start_xmit(struct sk_buff *skb, struct net_device *ndev)
{
1142
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	isdn_net_local *lp = (isdn_net_local *) ndev->priv;
#ifdef CONFIG_ISDN_X25
	struct concap_proto * cprot = lp -> netdev -> cprot;
#endif
#ifdef CONFIG_ISDN_X25
/* At this point hard_start_xmit() passes control to the encapsulation
   protocol (if present).
   For X.25 auto-dialing is completly bypassed because:
   - It does not conform with the semantics of a reliable datalink
     service as needed by X.25 PLP.
   - I don't want that the interface starts dialing when the network layer
     sends a message which requests to disconnect the lapb link (or if it
     sends any other message not resulting in data transmission).
   Instead, dialing will be initiated by the encapsulation protocol entity
   when a dl_establish request is received from the upper layer.
*/
	if( cprot ) {
		int ret = cprot -> pops -> encap_and_xmit ( cprot , skb);
		if(ret) netif_stop_queue(ndev);
		return ret;
	} else
#endif
	/* auto-dialing xmit function */
	{
		isdn_net_adjust_hdr(skb, ndev);
1168
		isdn_dumppkt("S:", skb->data, skb->len, 40);
Linus Torvalds's avatar
Linus Torvalds committed
1169 1170 1171 1172 1173 1174 1175 1176 1177

		if (!(lp->flags & ISDN_NET_CONNECTED)) {
			int chi;
			/* only do autodial if allowed by config */
			if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) {
				isdn_net_unreachable(ndev, skb, "dial rejected: interface not in dialmode `auto'");
				dev_kfree_skb(skb);
				return 0;
			}
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

			save_flags(flags);
			cli();

			if(lp->dialwait_timer <= 0)
				if(lp->dialstarted > 0 && lp->dialtimeout > 0 && time_before(jiffies, lp->dialstarted + lp->dialtimeout + lp->dialwait))
					lp->dialwait_timer = lp->dialstarted + lp->dialtimeout + lp->dialwait;
			
			if(lp->dialwait_timer > 0) {
				if(time_before(jiffies, lp->dialwait_timer)) {
					isdn_net_unreachable(ndev, skb, "dial rejected: retry-time not reached");
Linus Torvalds's avatar
Linus Torvalds committed
1189
					dev_kfree_skb(skb);
1190
					restore_flags(flags);
Linus Torvalds's avatar
Linus Torvalds committed
1191
					return 0;
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
				} else
					lp->dialwait_timer = 0;
			}
			/* Grab a free ISDN-Channel */
			if (((chi =
			      isdn_get_free_slot(
				      ISDN_USAGE_NET,
				      lp->l2_proto,
				      lp->l3_proto,
				      lp->pre_device,
				      lp->pre_channel,
				      lp->msn)
				     ) < 0) &&
			    ((chi =
			      isdn_get_free_slot(
				      ISDN_USAGE_NET,
				      lp->l2_proto,
				      lp->l3_proto,
				      lp->pre_device,
				      lp->pre_channel^1,
				      lp->msn)
				    ) < 0)) {
				restore_flags(flags);
				isdn_net_unreachable(ndev, skb,
						     "No channel");
				dev_kfree_skb(skb);
				return 0;
			}
			/* Log packet, which triggered dialing */
			if (dev->net_verbose)
				isdn_net_log_skb(skb, lp);
			/* Connect interface with channel */
			isdn_net_bind_channel(lp, chi);
Linus Torvalds's avatar
Linus Torvalds committed
1225
#ifdef CONFIG_ISDN_PPP
1226 1227 1228 1229 1230
			if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP) {
				/* no 'first_skb' handling for syncPPP */
				if (isdn_ppp_bind(lp) < 0) {
					dev_kfree_skb(skb);
					isdn_net_unbind_channel(lp);
Linus Torvalds's avatar
Linus Torvalds committed
1231
					restore_flags(flags);
1232
					return 0;	/* STN (skb to nirvana) ;) */
Linus Torvalds's avatar
Linus Torvalds committed
1233 1234
				}
				restore_flags(flags);
1235
				init_dialout(lp);
1236 1237
				netif_stop_queue(ndev);
				return 1;	/* let upper layer requeue skb packet */
Linus Torvalds's avatar
Linus Torvalds committed
1238
			}
1239 1240 1241 1242 1243 1244
#endif
			/* Initiate dialing */
			restore_flags(flags);
			init_dialout(lp);
			isdn_net_device_stop_queue(lp);
			return 1;
Linus Torvalds's avatar
Linus Torvalds committed
1245 1246 1247
		} else {
			/* Device is connected to an ISDN channel */ 
			ndev->trans_start = jiffies;
Kai Germaschewski's avatar
Kai Germaschewski committed
1248
			if (lp->dialstate == ST_ACTIVE) {
Linus Torvalds's avatar
Linus Torvalds committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
				/* ISDN connection is established, try sending */
				int ret;
				ret = (isdn_net_xmit(ndev, skb));
				if(ret) netif_stop_queue(ndev);
				return ret;
			} else
				netif_stop_queue(ndev);
		}
	}
	return 1;
}

/*
 * Shutdown a net-interface.
 */
static int
isdn_net_close(struct net_device *dev)
{
	struct net_device *p;
#ifdef CONFIG_ISDN_X25
	struct concap_proto * cprot =
		( (isdn_net_local *) dev->priv ) -> netdev -> cprot;
	/* printk(KERN_DEBUG "isdn_net_close %s\n" , dev-> name ); */
#endif

#ifdef CONFIG_ISDN_X25
	if( cprot && cprot -> pops ) cprot -> pops -> close( cprot );
#endif
	netif_stop_queue(dev);
	if ((p = (((isdn_net_local *) dev->priv)->slave))) {
		/* If this interface has slaves, stop them also */
		while (p) {
#ifdef CONFIG_ISDN_X25
			cprot = ( (isdn_net_local *) p->priv )
				-> netdev -> cprot;
			if( cprot && cprot -> pops )
				cprot -> pops -> close( cprot );
#endif
1287
			isdn_net_hangup(p->priv);
Linus Torvalds's avatar
Linus Torvalds committed
1288 1289 1290
			p = (((isdn_net_local *) p->priv)->slave);
		}
	}
1291
	isdn_net_hangup(dev->priv);
Linus Torvalds's avatar
Linus Torvalds committed
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
	isdn_MOD_DEC_USE_COUNT();
	return 0;
}

/*
 * Get statistics
 */
static struct net_device_stats *
isdn_net_get_stats(struct net_device *dev)
{
	isdn_net_local *lp = (isdn_net_local *) dev->priv;
	return &lp->stats;
}

/*      This is simply a copy from std. eth.c EXCEPT we pull ETH_HLEN
 *      instead of dev->hard_header_len off. This is done because the
 *      lowlevel-driver has already pulled off its stuff when we get
 *      here and this routine only gets called with p_encap == ETHER.
 *      Determine the packet's protocol ID. The rule here is that we
 *      assume 802.3 if the type field is short enough to be a length.
 *      This is normal practice and works for any 'now in use' protocol.
 */

static unsigned short
isdn_net_type_trans(struct sk_buff *skb, struct net_device *dev)
{
	struct ethhdr *eth;
	unsigned char *rawp;

	skb->mac.raw = skb->data;
	skb_pull(skb, ETH_HLEN);
	eth = skb->mac.ethernet;

	if (*eth->h_dest & 1) {
		if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
			skb->pkt_type = PACKET_BROADCAST;
		else
			skb->pkt_type = PACKET_MULTICAST;
	}
	/*
	 *      This ALLMULTI check should be redundant by 1.4
	 *      so don't forget to remove it.
	 */

	else if (dev->flags & (IFF_PROMISC /*| IFF_ALLMULTI*/)) {
		if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
			skb->pkt_type = PACKET_OTHERHOST;
	}
	if (ntohs(eth->h_proto) >= 1536)
		return eth->h_proto;

	rawp = skb->data;

	/*
	 *      This is a magic hack to spot IPX packets. Older Novell breaks
	 *      the protocol design and runs IPX over 802.3 without an 802.2 LLC
	 *      layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
	 *      won't work for fault tolerant netware but does for the rest.
	 */
	if (*(unsigned short *) rawp == 0xFFFF)
		return htons(ETH_P_802_3);
	/*
	 *      Real 802.2 LLC
	 */
	return htons(ETH_P_802_2);
}

Linus Torvalds's avatar
Linus Torvalds committed
1359 1360 1361 1362 1363 1364

/* 
 * CISCO HDLC keepalive specific stuff
 */
static struct sk_buff*
isdn_net_ciscohdlck_alloc_skb(isdn_net_local *lp, int len)
Linus Torvalds's avatar
Linus Torvalds committed
1365
{
1366
	unsigned short hl = isdn_slot_hdrlen(lp->isdn_slot);
Linus Torvalds's avatar
Linus Torvalds committed
1367
	struct sk_buff *skb;
Linus Torvalds's avatar
Linus Torvalds committed
1368

Linus Torvalds's avatar
Linus Torvalds committed
1369
	skb = alloc_skb(hl + len, GFP_ATOMIC);
Linus Torvalds's avatar
Linus Torvalds committed
1370
	if (!skb) {
Linus Torvalds's avatar
Linus Torvalds committed
1371 1372
		printk("isdn out of mem at %s:%d!\n", __FILE__, __LINE__);
		return 0;
Linus Torvalds's avatar
Linus Torvalds committed
1373 1374
	}
	skb_reserve(skb, hl);
Linus Torvalds's avatar
Linus Torvalds committed
1375
	return skb;
Linus Torvalds's avatar
Linus Torvalds committed
1376 1377
}

Linus Torvalds's avatar
Linus Torvalds committed
1378 1379 1380
/* cisco hdlck device private ioctls */
int
isdn_ciscohdlck_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Linus Torvalds's avatar
Linus Torvalds committed
1381
{
Linus Torvalds's avatar
Linus Torvalds committed
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	isdn_net_local *lp = (isdn_net_local *) dev->priv;
	unsigned long len = 0;
	unsigned long expires = 0;
	int tmp = 0;
	int period = lp->cisco_keepalive_period;
	char debserint = lp->cisco_debserint;
	int rc = 0;

	if (lp->p_encap != ISDN_NET_ENCAP_CISCOHDLCK)
		return -EINVAL;

	switch (cmd) {
		/* get/set keepalive period */
		case SIOCGKEEPPERIOD:
			len = (unsigned long)sizeof(lp->cisco_keepalive_period);
			if (copy_to_user((char *)ifr->ifr_ifru.ifru_data,
				(int *)&lp->cisco_keepalive_period, len))
				rc = -EFAULT;
			break;
		case SIOCSKEEPPERIOD:
			tmp = lp->cisco_keepalive_period;
			len = (unsigned long)sizeof(lp->cisco_keepalive_period);
			if (copy_from_user((int *)&period,
				(char *)ifr->ifr_ifru.ifru_data, len))
				rc = -EFAULT;
			if ((period > 0) && (period <= 32767))
				lp->cisco_keepalive_period = period;
			else
				rc = -EINVAL;
			if (!rc && (tmp != lp->cisco_keepalive_period)) {
				expires = (unsigned long)(jiffies +
					lp->cisco_keepalive_period * HZ);
				mod_timer(&lp->cisco_timer, expires);
				printk(KERN_INFO "%s: Keepalive period set "
					"to %d seconds.\n",
					lp->name, lp->cisco_keepalive_period);
			}
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1420

Linus Torvalds's avatar
Linus Torvalds committed
1421 1422 1423 1424 1425 1426
		/* get/set debugging */
		case SIOCGDEBSERINT:
			len = (unsigned long)sizeof(lp->cisco_debserint);
			if (copy_to_user((char *)ifr->ifr_ifru.ifru_data,
				(char *)&lp->cisco_debserint, len))
				rc = -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
1427
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1428 1429 1430 1431 1432 1433 1434 1435 1436
		case SIOCSDEBSERINT:
			len = (unsigned long)sizeof(lp->cisco_debserint);
			if (copy_from_user((char *)&debserint,
				(char *)ifr->ifr_ifru.ifru_data, len))
				rc = -EFAULT;
			if ((debserint >= 0) && (debserint <= 64))
				lp->cisco_debserint = debserint;
			else
				rc = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1437
			break;
Linus Torvalds's avatar
Linus Torvalds committed
1438 1439 1440

		default:
			rc = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1441 1442
			break;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1443
	return (rc);
Linus Torvalds's avatar
Linus Torvalds committed
1444 1445
}

Linus Torvalds's avatar
Linus Torvalds committed
1446 1447 1448
/* called via cisco_timer.function */
static void
isdn_net_ciscohdlck_slarp_send_keepalive(unsigned long data)
Linus Torvalds's avatar
Linus Torvalds committed
1449
{
Linus Torvalds's avatar
Linus Torvalds committed
1450 1451 1452 1453 1454
	isdn_net_local *lp = (isdn_net_local *) data;
	struct sk_buff *skb;
	unsigned char *p;
	unsigned long last_cisco_myseq = lp->cisco_myseq;
	int myseq_diff = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1455

Kai Germaschewski's avatar
Kai Germaschewski committed
1456 1457
	if (!(lp->flags & ISDN_NET_CONNECTED) || lp->dialstate != ST_ACTIVE) {
		isdn_BUG();
Linus Torvalds's avatar
Linus Torvalds committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
		return;
	}
	lp->cisco_myseq++;

	myseq_diff = (lp->cisco_myseq - lp->cisco_mineseen);
	if ((lp->cisco_line_state) && ((myseq_diff >= 3)||(myseq_diff <= -3))) {
		/* line up -> down */
		lp->cisco_line_state = 0;
		printk (KERN_WARNING
				"UPDOWN: Line protocol on Interface %s,"
				" changed state to down\n", lp->name);
		/* should stop routing higher-level data accross */
	} else if ((!lp->cisco_line_state) &&
		(myseq_diff >= 0) && (myseq_diff <= 2)) {
		/* line down -> up */
		lp->cisco_line_state = 1;
		printk (KERN_WARNING
				"UPDOWN: Line protocol on Interface %s,"
				" changed state to up\n", lp->name);
		/* restart routing higher-level data accross */
	}

	if (lp->cisco_debserint)
		printk (KERN_DEBUG "%s: HDLC "
			"myseq %lu, mineseen %lu%c, yourseen %lu, %s\n",
			lp->name, last_cisco_myseq, lp->cisco_mineseen,
			((last_cisco_myseq == lp->cisco_mineseen) ? '*' : 040),
			lp->cisco_yourseq,
			((lp->cisco_line_state) ? "line up" : "line down"));

	skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
	if (!skb)
		return;

	p = skb_put(skb, 4 + 14);

	/* cisco header */
	p += put_u8 (p, CISCO_ADDR_UNICAST);
	p += put_u8 (p, CISCO_CTRL);
	p += put_u16(p, CISCO_TYPE_SLARP);

	/* slarp keepalive */
	p += put_u32(p, CISCO_SLARP_KEEPALIVE);
	p += put_u32(p, lp->cisco_myseq);
	p += put_u32(p, lp->cisco_yourseq);
	p += put_u16(p, 0xffff); // reliablity, always 0xffff

	isdn_net_write_super(lp, skb);

	lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
	
	add_timer(&lp->cisco_timer);
}

static void
isdn_net_ciscohdlck_slarp_send_request(isdn_net_local *lp)
{
	struct sk_buff *skb;
	unsigned char *p;

	skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
	if (!skb)
		return;

	p = skb_put(skb, 4 + 14);

	/* cisco header */
	p += put_u8 (p, CISCO_ADDR_UNICAST);
	p += put_u8 (p, CISCO_CTRL);
	p += put_u16(p, CISCO_TYPE_SLARP);

	/* slarp request */
	p += put_u32(p, CISCO_SLARP_REQUEST);
	p += put_u32(p, 0); // address
	p += put_u32(p, 0); // netmask
	p += put_u16(p, 0); // unused

	isdn_net_write_super(lp, skb);
}

static void 
isdn_net_ciscohdlck_connected(isdn_net_local *lp)
{
	lp->cisco_myseq = 0;
	lp->cisco_mineseen = 0;
	lp->cisco_yourseq = 0;
	lp->cisco_keepalive_period = ISDN_TIMER_KEEPINT;
	lp->cisco_last_slarp_in = 0;
	lp->cisco_line_state = 0;
	lp->cisco_debserint = 0;

	/* send slarp request because interface/seq.no.s reset */
	isdn_net_ciscohdlck_slarp_send_request(lp);

	init_timer(&lp->cisco_timer);
	lp->cisco_timer.data = (unsigned long) lp;
	lp->cisco_timer.function = isdn_net_ciscohdlck_slarp_send_keepalive;
	lp->cisco_timer.expires = jiffies + lp->cisco_keepalive_period * HZ;
	add_timer(&lp->cisco_timer);
}

static void 
isdn_net_ciscohdlck_disconnected(isdn_net_local *lp)
{
	del_timer(&lp->cisco_timer);
}

static void
isdn_net_ciscohdlck_slarp_send_reply(isdn_net_local *lp)
{
	struct sk_buff *skb;
	unsigned char *p;
	struct in_device *in_dev = NULL;
	u32 addr = 0;		/* local ipv4 address */
	u32 mask = 0;		/* local netmask */

	if ((in_dev = lp->netdev->dev.ip_ptr) != NULL) {
		/* take primary(first) address of interface */
		struct in_ifaddr *ifa = in_dev->ifa_list;
		if (ifa != NULL) {
			addr = ifa->ifa_local;
			mask = ifa->ifa_mask;
Linus Torvalds's avatar
Linus Torvalds committed
1580 1581
		}
	}
Linus Torvalds's avatar
Linus Torvalds committed
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608

	skb = isdn_net_ciscohdlck_alloc_skb(lp, 4 + 14);
	if (!skb)
		return;

	p = skb_put(skb, 4 + 14);

	/* cisco header */
	p += put_u8 (p, CISCO_ADDR_UNICAST);
	p += put_u8 (p, CISCO_CTRL);
	p += put_u16(p, CISCO_TYPE_SLARP);

	/* slarp reply, send own ip/netmask; if values are nonsense remote
	 * should think we are unable to provide it with an address via SLARP */
	p += put_u32(p, CISCO_SLARP_REPLY);
	p += put_u32(p, addr);	// address
	p += put_u32(p, mask);	// netmask
	p += put_u16(p, 0);	// unused

	isdn_net_write_super(lp, skb);
}

static void
isdn_net_ciscohdlck_slarp_in(isdn_net_local *lp, struct sk_buff *skb)
{
	unsigned char *p;
	int period;
Linus Torvalds's avatar
Linus Torvalds committed
1609 1610 1611 1612 1613
	u32 code;
	u32 my_seq, addr;
	u32 your_seq, mask;
	u32 local;
	u16 unused;
Linus Torvalds's avatar
Linus Torvalds committed
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626

	if (skb->len < 14)
		return;

	p = skb->data;
	p += get_u32(p, &code);
	
	switch (code) {
	case CISCO_SLARP_REQUEST:
		lp->cisco_yourseq = 0;
		isdn_net_ciscohdlck_slarp_send_reply(lp);
		break;
	case CISCO_SLARP_REPLY:
Linus Torvalds's avatar
Linus Torvalds committed
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
		addr = ntohl(*(u32 *)p);
		mask = ntohl(*(u32 *)(p+4));
		if (mask != 0xfffffffc)
			goto slarp_reply_out;
		if ((addr & 3) == 0 || (addr & 3) == 3)
			goto slarp_reply_out;
		local = addr ^ 3;
		printk(KERN_INFO "%s: got slarp reply: "
			"remote ip: %d.%d.%d.%d, "
			"local ip: %d.%d.%d.%d "
			"mask: %d.%d.%d.%d\n",
		       lp->name,
		       HIPQUAD(addr),
		       HIPQUAD(local),
		       HIPQUAD(mask));
		break;
  slarp_reply_out:
		 printk(KERN_INFO "%s: got invalid slarp "
				 "reply (%d.%d.%d.%d/%d.%d.%d.%d) "
				 "- ignored\n", lp->name,
				 HIPQUAD(addr), HIPQUAD(mask));
Linus Torvalds's avatar
Linus Torvalds committed
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
		break;
	case CISCO_SLARP_KEEPALIVE:
		period = (int)((jiffies - lp->cisco_last_slarp_in
				+ HZ/2 - 1) / HZ);
		if (lp->cisco_debserint &&
				(period != lp->cisco_keepalive_period) &&
				lp->cisco_last_slarp_in) {
			printk(KERN_DEBUG "%s: Keepalive period mismatch - "
				"is %d but should be %d.\n",
				lp->name, period, lp->cisco_keepalive_period);
		}
		lp->cisco_last_slarp_in = jiffies;
		p += get_u32(p, &my_seq);
		p += get_u32(p, &your_seq);
		p += get_u16(p, &unused);
		lp->cisco_yourseq = my_seq;
		lp->cisco_mineseen = your_seq;
		break;
	}
}

static void
isdn_net_ciscohdlck_receive(isdn_net_local *lp, struct sk_buff *skb)
{
	unsigned char *p;
Linus Torvalds's avatar
Linus Torvalds committed
1673 1674 1675
 	u8 addr;
 	u8 ctrl;
 	u16 type;
Linus Torvalds's avatar
Linus Torvalds committed
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
	
	if (skb->len < 4)
		goto out_free;

	p = skb->data;
	p += get_u8 (p, &addr);
	p += get_u8 (p, &ctrl);
	p += get_u16(p, &type);
	skb_pull(skb, 4);
	
	if (addr != CISCO_ADDR_UNICAST && addr != CISCO_ADDR_BROADCAST) {
		printk(KERN_WARNING "%s: Unknown Cisco addr 0x%02x\n",
		       lp->name, addr);
		goto out_free;
	}
	if (ctrl != CISCO_CTRL) {
		printk(KERN_WARNING "%s: Unknown Cisco ctrl 0x%02x\n",
		       lp->name, ctrl);
		goto out_free;
	}

	switch (type) {
	case CISCO_TYPE_SLARP:
		isdn_net_ciscohdlck_slarp_in(lp, skb);
		goto out_free;
Linus Torvalds's avatar
Linus Torvalds committed
1701 1702 1703 1704 1705
	case CISCO_TYPE_CDP:
		if (lp->cisco_debserint)
			printk(KERN_DEBUG "%s: Received CDP packet. use "
				"\"no cdp enable\" on cisco.\n", lp->name);
		goto out_free;
Linus Torvalds's avatar
Linus Torvalds committed
1706
	default:
Kai Germaschewski's avatar
Kai Germaschewski committed
1707 1708 1709 1710
		/* no special cisco protocol */
		skb->protocol = htons(type);
		netif_rx(skb);
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1711 1712 1713 1714
	}

 out_free:
	kfree_skb(skb);
Linus Torvalds's avatar
Linus Torvalds committed
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
}

/*
 * Got a packet from ISDN-Channel.
 */
static void
isdn_net_receive(struct net_device *ndev, struct sk_buff *skb)
{
	isdn_net_local *lp = (isdn_net_local *) ndev->priv;
	isdn_net_local *olp = lp;	/* original 'lp' */
#ifdef CONFIG_ISDN_PPP
	int proto = PPP_PROTOCOL(skb->data);
#endif
#ifdef CONFIG_ISDN_X25
	struct concap_proto *cprot = lp -> netdev -> cprot;
#endif
	lp->transcount += skb->len;

	lp->stats.rx_packets++;
	lp->stats.rx_bytes += skb->len;
	if (lp->master) {
		/* Bundling: If device is a slave-device, deliver to master, also
		 * handle master's statistics and hangup-timeout
		 */
		ndev = lp->master;
		lp = (isdn_net_local *) ndev->priv;
		lp->stats.rx_packets++;
		lp->stats.rx_bytes += skb->len;
	}
	skb->dev = ndev;
	skb->pkt_type = PACKET_HOST;
	skb->mac.raw = skb->data;
	isdn_dumppkt("R:", skb->data, skb->len, 40);
	switch (lp->p_encap) {
		case ISDN_NET_ENCAP_ETHER:
			/* Ethernet over ISDN */
			olp->huptimer = 0;
			lp->huptimer = 0;
			skb->protocol = isdn_net_type_trans(skb, ndev);
			break;
		case ISDN_NET_ENCAP_UIHDLC:
			/* HDLC with UI-frame (for ispa with -h1 option) */
			olp->huptimer = 0;
			lp->huptimer = 0;
			skb_pull(skb, 2);
			/* Fall through */
		case ISDN_NET_ENCAP_RAWIP:
			/* RAW-IP without MAC-Header */
			olp->huptimer = 0;
			lp->huptimer = 0;
			skb->protocol = htons(ETH_P_IP);
			break;
		case ISDN_NET_ENCAP_CISCOHDLCK:
Linus Torvalds's avatar
Linus Torvalds committed
1768 1769
			isdn_net_ciscohdlck_receive(lp, skb);
			return;
Linus Torvalds's avatar
Linus Torvalds committed
1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
		case ISDN_NET_ENCAP_CISCOHDLC:
			/* CISCO-HDLC IP with type field and  fake I-frame-header */
			skb_pull(skb, 2);
			/* Fall through */
		case ISDN_NET_ENCAP_IPTYP:
			/* IP with type field */
			olp->huptimer = 0;
			lp->huptimer = 0;
			skb->protocol = *(unsigned short *) &(skb->data[0]);
			skb_pull(skb, 2);
			if (*(unsigned short *) skb->data == 0xFFFF)
				skb->protocol = htons(ETH_P_802_3);
			break;
#ifdef CONFIG_ISDN_PPP
		case ISDN_NET_ENCAP_SYNCPPP:
			/*
			 * If encapsulation is syncppp, don't reset
			 * huptimer on LCP packets.
			 */
			if (proto != PPP_LCP) {
				olp->huptimer = 0;
				lp->huptimer = 0;
			}
			isdn_ppp_receive(lp->netdev, olp, skb);
			return;
#endif

		default:
#ifdef CONFIG_ISDN_X25
		  /* try if there are generic sync_device receiver routines */
			if(cprot) if(cprot -> pops)
				if( cprot -> pops -> data_ind){
					cprot -> pops -> data_ind(cprot,skb);
					return;
				};
#endif /* CONFIG_ISDN_X25 */
			printk(KERN_WARNING "%s: unknown encapsulation, dropping\n",
			       lp->name);
			kfree_skb(skb);
			return;
	}

	netif_rx(skb);
	return;
}

/*
 * A packet arrived via ISDN. Search interface-chain for a corresponding
 * interface. If found, deliver packet to receiver-function and return 1,
 * else return 0.
 */
int
isdn_net_rcv_skb(int idx, struct sk_buff *skb)
{
1824
	isdn_net_dev *p = isdn_slot_rx_netdev(idx);
Linus Torvalds's avatar
Linus Torvalds committed
1825 1826

	if (p) {
1827
		isdn_net_local *lp = &p->local;
Linus Torvalds's avatar
Linus Torvalds committed
1828
		if ((lp->flags & ISDN_NET_CONNECTED) &&
Kai Germaschewski's avatar
Kai Germaschewski committed
1829
		    (lp->dialstate == ST_ACTIVE)) {
Linus Torvalds's avatar
Linus Torvalds committed
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
			isdn_net_receive(&p->dev, skb);
			return 1;
		}
	}
	return 0;
}

static int
my_eth_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
	      void *daddr, void *saddr, unsigned len)
{
	struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);

	/*
	 * Set the protocol type. For a packet of type ETH_P_802_3 we
	 * put the length here instead. It is up to the 802.2 layer to
	 * carry protocol information.
	 */

	if (type != ETH_P_802_3)
		eth->h_proto = htons(type);
	else
		eth->h_proto = htons(len);

	/*
	 * Set the source hardware address.
	 */
	if (saddr)
		memcpy(eth->h_source, saddr, dev->addr_len);
	else
		memcpy(eth->h_source, dev->dev_addr, dev->addr_len);

	/*
	 * Anyway, the loopback-device should never use this function...
	 */

	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
		memset(eth->h_dest, 0, dev->addr_len);
		return ETH_HLEN /*(dev->hard_header_len)*/;
	}
	if (daddr) {
		memcpy(eth->h_dest, daddr, dev->addr_len);
		return ETH_HLEN /*dev->hard_header_len*/;
	}
	return -ETH_HLEN /*dev->hard_header_len*/;
}

/*
 *  build an header
 *  depends on encaps that is being used.
 */

static int
isdn_net_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
		void *daddr, void *saddr, unsigned plen)
{
	isdn_net_local *lp = dev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
1887
	unsigned char *p;
Linus Torvalds's avatar
Linus Torvalds committed
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
	ushort len = 0;

	switch (lp->p_encap) {
		case ISDN_NET_ENCAP_ETHER:
			len = my_eth_header(skb, dev, type, daddr, saddr, plen);
			break;
#ifdef CONFIG_ISDN_PPP
		case ISDN_NET_ENCAP_SYNCPPP:
			/* stick on a fake header to keep fragmentation code happy. */
			len = IPPP_MAX_HEADER;
			skb_push(skb,len);
			break;
#endif
		case ISDN_NET_ENCAP_RAWIP:
			printk(KERN_WARNING "isdn_net_header called with RAW_IP!\n");
			len = 0;
			break;
		case ISDN_NET_ENCAP_IPTYP:
			/* ethernet type field */
			*((ushort *) skb_push(skb, 2)) = htons(type);
			len = 2;
			break;
		case ISDN_NET_ENCAP_UIHDLC:
			/* HDLC with UI-Frames (for ispa with -h1 option) */
			*((ushort *) skb_push(skb, 2)) = htons(0x0103);
			len = 2;
			break;
		case ISDN_NET_ENCAP_CISCOHDLC:
Linus Torvalds's avatar
Linus Torvalds committed
1916 1917 1918 1919 1920
		case ISDN_NET_ENCAP_CISCOHDLCK:
			p = skb_push(skb, 4);
			p += put_u8 (p, CISCO_ADDR_UNICAST);
			p += put_u8 (p, CISCO_CTRL);
			p += put_u16(p, type);
Linus Torvalds's avatar
Linus Torvalds committed
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
			len = 4;
			break;
#ifdef CONFIG_ISDN_X25
		default:
		  /* try if there are generic concap protocol routines */
			if( lp-> netdev -> cprot ){
				printk(KERN_WARNING "isdn_net_header called with concap_proto!\n");
				len = 0;
				break;
			}
			break;
#endif /* CONFIG_ISDN_X25 */
	}
	return len;
}

/* We don't need to send arp, because we have point-to-point connections. */
static int
isdn_net_rebuild_header(struct sk_buff *skb)
{
	struct net_device *dev = skb->dev;
	isdn_net_local *lp = dev->priv;
	int ret = 0;

	if (lp->p_encap == ISDN_NET_ENCAP_ETHER) {
		struct ethhdr *eth = (struct ethhdr *) skb->data;

		/*
		 *      Only ARP/IP is currently supported
		 */

		if (eth->h_proto != htons(ETH_P_IP)) {
			printk(KERN_WARNING
			       "isdn_net: %s don't know how to resolve type %d addresses?\n",
			       dev->name, (int) eth->h_proto);
			memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
			return 0;
		}
		/*
		 *      Try to get ARP to resolve the header.
		 */
#ifdef CONFIG_INET
		ret = arp_find(eth->h_dest, skb);
#endif
	}
	return ret;
}

/*
 * Interface-setup. (just after registering a new interface)
 */
static int
isdn_net_init(struct net_device *ndev)
{
	ushort max_hlhdr_len = 0;
	isdn_net_local *lp = (isdn_net_local *) ndev->priv;
Linus Torvalds's avatar
Linus Torvalds committed
1977
	int drvidx, i;
Linus Torvalds's avatar
Linus Torvalds committed
1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016

	ether_setup(ndev);
	lp->org_hhc = ndev->hard_header_cache;
	lp->org_hcu = ndev->header_cache_update;

	/* Setup the generic properties */

	ndev->hard_header = NULL;
	ndev->hard_header_cache = NULL;
	ndev->header_cache_update = NULL;
	ndev->mtu = 1500;
	ndev->flags = IFF_NOARP|IFF_POINTOPOINT;
	ndev->type = ARPHRD_ETHER;
	ndev->addr_len = ETH_ALEN;

	/* for clients with MPPP maybe higher values better */
	ndev->tx_queue_len = 30;

	for (i = 0; i < ETH_ALEN; i++)
		ndev->broadcast[i] = 0xff;

	/* The ISDN-specific entries in the device structure. */
	ndev->open = &isdn_net_open;
	ndev->hard_start_xmit = &isdn_net_start_xmit;

	/*
	 *  up till binding we ask the protocol layer to reserve as much
	 *  as we might need for HL layer
	 */

	for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
		if (dev->drv[drvidx])
			if (max_hlhdr_len < dev->drv[drvidx]->interface->hl_hdrlen)
				max_hlhdr_len = dev->drv[drvidx]->interface->hl_hdrlen;

	ndev->hard_header_len = ETH_HLEN + max_hlhdr_len;
	ndev->stop = &isdn_net_close;
	ndev->get_stats = &isdn_net_get_stats;
	ndev->rebuild_header = &isdn_net_rebuild_header;
Linus Torvalds's avatar
Linus Torvalds committed
2017
	ndev->do_ioctl = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
2018 2019 2020 2021 2022 2023
	return 0;
}

static void
isdn_net_swapbind(int drvidx)
{
2024
	struct list_head *l;
Linus Torvalds's avatar
Linus Torvalds committed
2025

2026
	dbg_net_icall("n_fi: swapping ch of %d\n", drvidx);
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
	list_for_each(l, &isdn_net_devs) {
		isdn_net_dev *p = list_entry(l, isdn_net_dev, global_list);
		if (p->local.pre_device != drvidx)
			continue;

		switch (p->local.pre_channel) {
		case 0:
			p->local.pre_channel = 1;
			break;
		case 1:
			p->local.pre_channel = 0;
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
2040 2041 2042 2043 2044 2045
	}
}

static void
isdn_net_swap_usage(int i1, int i2)
{
2046 2047
	int u1 = isdn_slot_usage(i1);
	int u2 = isdn_slot_usage(i2);
Linus Torvalds's avatar
Linus Torvalds committed
2048

2049
	dbg_net_icall("n_fi: usage of %d and %d\n", i1, i2);
2050 2051
	isdn_slot_set_usage(i1, (u1 & ~ISDN_USAGE_EXCLUSIVE) | (u2 & ISDN_USAGE_EXCLUSIVE));
	isdn_slot_set_usage(i2, (u2 & ~ISDN_USAGE_EXCLUSIVE) | (u1 & ISDN_USAGE_EXCLUSIVE));
Linus Torvalds's avatar
Linus Torvalds committed
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
}

/*
 * An incoming call-request has arrived.
 * Search the interface-chain for an appropriate interface.
 * If found, connect the interface to the ISDN-channel and initiate
 * D- and B-Channel-setup. If secure-flag is set, accept only
 * configured phone-numbers. If callback-flag is set, initiate
 * callback-dialing.
 *
 * Return-Value: 0 = No appropriate interface for this call.
 *               1 = Call accepted
 *               2 = Reject call, wait cbdelay, then call back
 *               3 = Reject call
 *               4 = Wait cbdelay, then call back
 *               5 = No appropriate interface for this call,
 *                   would eventually match if CID was longer.
 */
int
isdn_net_find_icall(int di, int ch, int idx, setup_parm *setup)
{
	char *eaz;
	int si1;
	int si2;
	int ematch;
	int wret;
	int swapped;
	int sidx = 0;
2080
	struct list_head *l;
2081
	struct isdn_net_phone *n;
Linus Torvalds's avatar
Linus Torvalds committed
2082 2083
	ulong flags;
	char nr[32];
2084
	char *my_eaz;
2085
	isdn_ctrl cmd;
2086

2087
	int slot = isdn_dc2minor(di, ch);
Linus Torvalds's avatar
Linus Torvalds committed
2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
	/* Search name in netdev-chain */
	save_flags(flags);
	cli();
	if (!setup->phone[0]) {
		nr[0] = '0';
		nr[1] = '\0';
		printk(KERN_INFO "isdn_net: Incoming call without OAD, assuming '0'\n");
	} else
		strcpy(nr, setup->phone);
	si1 = (int) setup->si1;
	si2 = (int) setup->si2;
	if (!setup->eazmsn[0]) {
		printk(KERN_WARNING "isdn_net: Incoming call without CPN, assuming '0'\n");
		eaz = "0";
	} else
		eaz = setup->eazmsn;
	if (dev->net_verbose > 1)
		printk(KERN_INFO "isdn_net: call from %s,%d,%d -> %s\n", nr, si1, si2, eaz);
2106 2107 2108 2109 2110 2111 2112 2113 2114
        /* Accept DATA and VOICE calls at this stage
        local eaz is checked later for allowed call types */
        if ((si1 != 7) && (si1 != 1)) {
                restore_flags(flags);
                if (dev->net_verbose > 1)
                        printk(KERN_INFO "isdn_net: Service-Indicator not 1 or 7, ignored\n");
                return 0;
        }

2115
	n = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
2116
	ematch = wret = swapped = 0;
2117
	dbg_net_icall("n_fi: di=%d ch=%d idx=%d usg=%d\n", di, ch, idx,
Kai Germaschewski's avatar
Kai Germaschewski committed
2118
		      isdn_slot_usage(idx));
2119 2120

	list_for_each(l, &isdn_net_devs) {
Linus Torvalds's avatar
Linus Torvalds committed
2121
		int matchret;
2122
		isdn_net_dev *p = list_entry(l, isdn_net_dev, global_list);
2123
		isdn_net_local *lp = &p->local;
Linus Torvalds's avatar
Linus Torvalds committed
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134

		/* If last check has triggered as binding-swap, revert it */
		switch (swapped) {
			case 2:
				isdn_net_swap_usage(idx, sidx);
				/* fall through */
			case 1:
				isdn_net_swapbind(di);
				break;
		}
		swapped = 0;
2135
                /* check acceptable call types for DOV */
2136
                my_eaz = isdn_slot_map_eaz2msn(slot, lp->msn);
2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
                if (si1 == 1) { /* it's a DOV call, check if we allow it */
                        if (*my_eaz == 'v' || *my_eaz == 'V' ||
			    *my_eaz == 'b' || *my_eaz == 'B')
                                my_eaz++; /* skip to allow a match */
                        else
                                my_eaz = 0; /* force non match */
                } else { /* it's a DATA call, check if we allow it */
                        if (*my_eaz == 'b' || *my_eaz == 'B')
                                my_eaz++; /* skip to allow a match */
                }
                if (my_eaz)
                        matchret = isdn_msncmp(eaz, my_eaz);
                else
                        matchret = 1;
                if (!matchret)
                        ematch = 1;

Linus Torvalds's avatar
Linus Torvalds committed
2154 2155 2156
		/* Remember if more numbers eventually can match */
		if (matchret > wret)
			wret = matchret;
2157
		dbg_net_icall("n_fi: if='%s', l.msn=%s, l.flags=%d, l.dstate=%d\n",
Linus Torvalds's avatar
Linus Torvalds committed
2158 2159 2160
		       lp->name, lp->msn, lp->flags, lp->dialstate);
		if ((!matchret) &&                                        /* EAZ is matching   */
		    (((!(lp->flags & ISDN_NET_CONNECTED)) &&              /* but not connected */
2161
		      (USG_NONE(isdn_slot_usage(idx)))) ||                     /* and ch. unused or */
2162
		     ((((lp->dialstate == ST_OUT_WAIT_DCONN) || (lp->dialstate == ST_OUT_WAIT_DCONN)) && /* if dialing        */
Linus Torvalds's avatar
Linus Torvalds committed
2163 2164 2165
		       (!(lp->flags & ISDN_NET_CALLBACK)))                /* but no callback   */
		     )))
			 {
2166
			dbg_net_icall("n_fi: match1, pdev=%d pch=%d\n",
Linus Torvalds's avatar
Linus Torvalds committed
2167
			       lp->pre_device, lp->pre_channel);
2168
			if (isdn_slot_usage(idx) & ISDN_USAGE_EXCLUSIVE) {
Linus Torvalds's avatar
Linus Torvalds committed
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
				if ((lp->pre_channel != ch) ||
				    (lp->pre_device != di)) {
					/* Here we got a problem:
					 * If using an ICN-Card, an incoming call is always signaled on
					 * on the first channel of the card, if both channels are
					 * down. However this channel may be bound exclusive. If the
					 * second channel is free, this call should be accepted.
					 * The solution is horribly but it runs, so what:
					 * We exchange the exclusive bindings of the two channels, the
					 * corresponding variables in the interface-structs.
					 */
					if (ch == 0) {
						sidx = isdn_dc2minor(di, 1);
2182
						dbg_net_icall("n_fi: ch is 0\n");
2183
						if (USG_NONE(isdn_slot_usage(sidx))) {
Linus Torvalds's avatar
Linus Torvalds committed
2184 2185
							/* Second Channel is free, now see if it is bound
							 * exclusive too. */
2186
							if (isdn_slot_usage(sidx) & ISDN_USAGE_EXCLUSIVE) {
2187
								dbg_net_icall("n_fi: 2nd channel is down and bound\n");
Linus Torvalds's avatar
Linus Torvalds committed
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
								/* Yes, swap bindings only, if the original
								 * binding is bound to channel 1 of this driver */
								if ((lp->pre_device == di) &&
								    (lp->pre_channel == 1)) {
									isdn_net_swapbind(di);
									swapped = 1;
								} else {
									/* ... else iterate next device */
									continue;
								}
							} else {
2199
								dbg_net_icall("n_fi: 2nd channel is down and unbound\n");
Linus Torvalds's avatar
Linus Torvalds committed
2200 2201 2202 2203 2204 2205
								/* No, swap always and swap excl-usage also */
								isdn_net_swap_usage(idx, sidx);
								isdn_net_swapbind(di);
								swapped = 2;
							}
							/* Now check for exclusive binding again */
2206
							dbg_net_icall("n_fi: final check\n");
2207
							if ((isdn_slot_usage(idx) & ISDN_USAGE_EXCLUSIVE) &&
Linus Torvalds's avatar
Linus Torvalds committed
2208 2209
							    ((lp->pre_channel != ch) ||
							     (lp->pre_device != di))) {
2210
								dbg_net_icall("n_fi: final check failed\n");
Linus Torvalds's avatar
Linus Torvalds committed
2211 2212 2213 2214 2215
								continue;
							}
						}
					} else {
						/* We are already on the second channel, so nothing to do */
2216
						dbg_net_icall("n_fi: already on 2nd channel\n");
Linus Torvalds's avatar
Linus Torvalds committed
2217 2218 2219
					}
				}
			}
2220
			dbg_net_icall("n_fi: match2\n");
Linus Torvalds's avatar
Linus Torvalds committed
2221
			if (lp->flags & ISDN_NET_SECURE) {
2222
				list_for_each_entry(n, &lp->phone[0], list) {
Linus Torvalds's avatar
Linus Torvalds committed
2223 2224 2225
					if (!isdn_msncmp(nr, n->num))
						break;
				}
2226
				continue;
Linus Torvalds's avatar
Linus Torvalds committed
2227
			}
2228 2229
			dbg_net_icall("n_fi: match3\n");
			/* matching interface found */
Linus Torvalds's avatar
Linus Torvalds committed
2230

2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276
			/*
			 * Is the state STOPPED?
			 * If so, no dialin is allowed,
			 * so reject actively.
			 * */
			if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
				restore_flags(flags);
				printk(KERN_INFO "incoming call, interface %s `stopped' -> rejected\n",
				       lp->name);
				return 3;
			}
			/*
			 * Is the interface up?
			 * If not, reject the call actively.
			 */
			if (!isdn_net_device_started(p)) {
				restore_flags(flags);
				printk(KERN_INFO "%s: incoming call, interface down -> rejected\n",
				       lp->name);
				return 3;
			}
			/* Interface is up, now see if it's a slave. If so, see if
			 * it's master and parent slave is online. If not, reject the call.
			 */
			if (lp->master) {
				isdn_net_local *mlp = (isdn_net_local *) lp->master->priv;
				printk(KERN_DEBUG "ICALLslv: %s\n", lp->name);
				printk(KERN_DEBUG "master=%s\n", mlp->name);
				if (mlp->flags & ISDN_NET_CONNECTED) {
					printk(KERN_DEBUG "master online\n");
					/* Master is online, find parent-slave (master if first slave) */
					while (mlp->slave) {
						if ((isdn_net_local *) mlp->slave->priv == lp)
							break;
						mlp = (isdn_net_local *) mlp->slave->priv;
					}
				} else
					printk(KERN_DEBUG "master offline\n");
				/* Found parent, if it's offline iterate next device */
				printk(KERN_DEBUG "mlpf: %d\n", mlp->flags & ISDN_NET_CONNECTED);
				if (!(mlp->flags & ISDN_NET_CONNECTED)) {
					continue;
				}
			} 
			if (lp->flags & ISDN_NET_CALLBACK) {
				int chi;
Linus Torvalds's avatar
Linus Torvalds committed
2277
				/*
2278 2279
				 * Is the state MANUAL?
				 * If so, no callback can be made,
Linus Torvalds's avatar
Linus Torvalds committed
2280 2281 2282 2283
				 * so reject actively.
				 * */
				if (ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_OFF) {
					restore_flags(flags);
2284
					printk(KERN_INFO "incoming call for callback, interface %s `off' -> rejected\n",
Linus Torvalds's avatar
Linus Torvalds committed
2285 2286 2287
					       lp->name);
					return 3;
				}
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302
				printk(KERN_DEBUG "%s: call from %s -> %s, start callback\n",
				       lp->name, nr, eaz);

				/* Grab a free ISDN-Channel */
				if ((chi = 
				     isdn_get_free_slot(
					     ISDN_USAGE_NET,
					     lp->l2_proto,
					     lp->l3_proto,
					     lp->pre_device,
					     lp->pre_channel,
					     lp->msn)
					    ) < 0) {
					
					printk(KERN_WARNING "isdn_net_find_icall: No channel for %s\n", lp->name);
Linus Torvalds's avatar
Linus Torvalds committed
2303
					restore_flags(flags);
2304
					return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2305
				}
2306 2307 2308 2309 2310 2311 2312 2313
				/* Setup dialstate. */
				lp->dial_timer.expires = jiffies + lp->cbdelay;
				lp->dial_event = EV_NET_TIMER_CB;
				add_timer(&lp->dial_timer);
				
				lp->dialstate = ST_WAIT_BEFORE_CB;
				/* Connect interface with channel */
				isdn_net_bind_channel(lp, chi);
Linus Torvalds's avatar
Linus Torvalds committed
2314
#ifdef CONFIG_ISDN_PPP
2315 2316 2317
				if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
					if (isdn_ppp_bind(lp) < 0) {
						isdn_net_unbind_channel(lp);
Linus Torvalds's avatar
Linus Torvalds committed
2318
						restore_flags(flags);
2319
						return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2320
					}
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330
#endif
				/* Initiate dialing by returning 2 or 4 */
				restore_flags(flags);
				return (lp->flags & ISDN_NET_CBHUP) ? 2 : 4;
			} else {
				printk(KERN_DEBUG "%s: call from %s -> %s accepted\n", lp->name, nr,
				       eaz);
				/* if this interface is dialing, it does it probably on a different
				   device, so free this device */
				if (lp->dialstate == ST_OUT_WAIT_DCONN) {
Linus Torvalds's avatar
Linus Torvalds committed
2331 2332
#ifdef CONFIG_ISDN_PPP
					if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
2333
						isdn_ppp_free(lp);
Linus Torvalds's avatar
Linus Torvalds committed
2334
#endif
2335 2336 2337
					isdn_net_lp_disconnected(lp);
					isdn_slot_free(lp->isdn_slot,
						       ISDN_USAGE_NET);
Linus Torvalds's avatar
Linus Torvalds committed
2338
				}
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
				strcpy(isdn_slot_num(idx), nr);
				isdn_slot_set_usage(idx, (isdn_slot_usage(idx) & ISDN_USAGE_EXCLUSIVE) | ISDN_USAGE_NET);
				isdn_slot_set_st_netdev(idx, lp->netdev);
				lp->isdn_slot = slot;
				lp->ppp_slot = -1;
				lp->flags |= ISDN_NET_CONNECTED;
				
				lp->outgoing = 0;
				lp->huptimer = 0;
				lp->charge_state = ST_CHARGE_NULL;
				/* Got incoming Call, setup L2 and L3 protocols,
				 * then wait for D-Channel-connect
				 */
				cmd.arg = lp->l2_proto << 8;
				isdn_slot_command(lp->isdn_slot, ISDN_CMD_SETL2, &cmd);
				cmd.arg = lp->l3_proto << 8;
				isdn_slot_command(lp->isdn_slot, ISDN_CMD_SETL3, &cmd);
				
				lp->dial_timer.expires = jiffies + 15 * HZ;
				lp->dial_event = EV_NET_TIMER_IN_DCONN;
				add_timer(&lp->dial_timer);
				lp->dialstate = ST_IN_WAIT_DCONN;
				
#ifdef CONFIG_ISDN_PPP
				if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
					if (isdn_ppp_bind(lp) < 0) {
						isdn_net_unbind_channel(lp);
						restore_flags(flags);
						return 0;
					}
#endif
				restore_flags(flags);
				return 1;
Linus Torvalds's avatar
Linus Torvalds committed
2372
			}
2373
			 }
Linus Torvalds's avatar
Linus Torvalds committed
2374 2375 2376
	}
	/* If none of configured EAZ/MSN matched and not verbose, be silent */
	if (!ematch || dev->net_verbose)
2377
		printk(KERN_INFO "isdn_net: call from %s -> %d %s ignored\n", nr, slot, eaz);
Linus Torvalds's avatar
Linus Torvalds committed
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
	restore_flags(flags);
	return (wret == 2)?5:0;
}

/*
 * Search list of net-interfaces for an interface with given name.
 */
isdn_net_dev *
isdn_net_findif(char *name)
{
2388
	struct list_head *l;
Linus Torvalds's avatar
Linus Torvalds committed
2389

2390 2391
	list_for_each(l, &isdn_net_devs) {
		isdn_net_dev *p = list_entry(l, isdn_net_dev, global_list);
2392
		if (!strcmp(p->local.name, name))
Linus Torvalds's avatar
Linus Torvalds committed
2393 2394
			return p;
	}
2395
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
}

/*
 * Force a net-interface to dial out.
 * This is called from the userlevel-routine below or
 * from isdn_net_start_xmit().
 */
int
isdn_net_force_dial_lp(isdn_net_local * lp)
{
2406 2407
	int chi;
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
2408

2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
	if (lp->flags & ISDN_NET_CONNECTED || lp->dialstate != ST_NULL)
		return -EBUSY;

	save_flags(flags);
	cli();
	
	/* Grab a free ISDN-Channel */
	chi = isdn_get_free_slot(ISDN_USAGE_NET, lp->l2_proto, lp->l3_proto,
				 lp->pre_device, lp->pre_channel, lp->msn);
	if (chi < 0) {
		printk(KERN_WARNING "isdn_net_force_dial: No channel for %s\n", lp->name);
		restore_flags(flags);
		return -EAGAIN;
	}
	/* Connect interface with channel */
	isdn_net_bind_channel(lp, chi);
Linus Torvalds's avatar
Linus Torvalds committed
2425
#ifdef CONFIG_ISDN_PPP
2426 2427 2428
	if (lp->p_encap == ISDN_NET_ENCAP_SYNCPPP)
		if (isdn_ppp_bind(lp) < 0) {
			isdn_net_unbind_channel(lp);
Linus Torvalds's avatar
Linus Torvalds committed
2429
			restore_flags(flags);
2430 2431 2432 2433 2434 2435 2436 2437
			return -EAGAIN;
		}
#endif
	/* Initiate dialing */
	restore_flags(flags);
	init_dialout(lp);

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
}

/*
 * This is called from certain upper protocol layers (multilink ppp
 * and x25iface encapsulation module) that want to initiate dialing
 * themselves.
 */
int
isdn_net_dial_req(isdn_net_local * lp)
{
	/* is there a better error code? */
	if (!(ISDN_NET_DIALMODE(*lp) == ISDN_NET_DM_AUTO)) return -EBUSY;

	return isdn_net_force_dial_lp(lp);
}

/*
 * Force a net-interface to dial out.
 * This is always called from within userspace (ISDN_IOCTL_NET_DIAL).
 */
int
isdn_net_force_dial(char *name)
{
	isdn_net_dev *p = isdn_net_findif(name);

	if (!p)
		return -ENODEV;
2465
	return (isdn_net_force_dial_lp(&p->local));
Linus Torvalds's avatar
Linus Torvalds committed
2466 2467 2468 2469 2470
}

/*
 * Allocate a new network-interface and initialize its data structures.
 */
2471
int
Linus Torvalds's avatar
Linus Torvalds committed
2472 2473
isdn_net_new(char *name, struct net_device *master)
{
2474
	int retval;
Linus Torvalds's avatar
Linus Torvalds committed
2475 2476 2477 2478 2479
	isdn_net_dev *netdev;

	/* Avoid creating an existing interface */
	if (isdn_net_findif(name)) {
		printk(KERN_WARNING "isdn_net: interface %s already exists\n", name);
2480
		return -EEXIST;
Linus Torvalds's avatar
Linus Torvalds committed
2481
	}
2482
	if (!(netdev = kmalloc(sizeof(isdn_net_dev), GFP_KERNEL))) {
Linus Torvalds's avatar
Linus Torvalds committed
2483
		printk(KERN_WARNING "isdn_net: Could not allocate net-device\n");
2484
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
2485 2486
	}
	memset(netdev, 0, sizeof(isdn_net_dev));
2487 2488
	strcpy(netdev->local.name, name);
	strcpy(netdev->dev.name, name);
2489
	netdev->dev.priv = &netdev->local;
Linus Torvalds's avatar
Linus Torvalds committed
2490
	netdev->dev.init = isdn_net_init;
2491
	netdev->local.p_encap = ISDN_NET_ENCAP_RAWIP;
Linus Torvalds's avatar
Linus Torvalds committed
2492 2493 2494 2495 2496
	if (master) {
		/* Device shall be a slave */
		struct net_device *p = (((isdn_net_local *) master->priv)->slave);
		struct net_device *q = master;

2497
		netdev->local.master = master;
Linus Torvalds's avatar
Linus Torvalds committed
2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
		/* Put device at end of slave-chain */
		while (p) {
			q = p;
			p = (((isdn_net_local *) p->priv)->slave);
		}
		((isdn_net_local *) q->priv)->slave = &(netdev->dev);
	} else {
		/* Device shall be a master */
		/*
		 * Watchdog timer (currently) for master only.
		 */
		netdev->dev.tx_timeout = isdn_net_tx_timeout;
		netdev->dev.watchdog_timeo = ISDN_NET_TX_TIMEOUT;
2511 2512
		retval = register_netdev(&netdev->dev);
		if (retval) {
Linus Torvalds's avatar
Linus Torvalds committed
2513 2514
			printk(KERN_WARNING "isdn_net: Could not register net-device\n");
			kfree(netdev);
2515
			return retval;
Linus Torvalds's avatar
Linus Torvalds committed
2516 2517
		}
	}
2518
	netdev->local.magic = ISDN_NET_MAGIC;
Linus Torvalds's avatar
Linus Torvalds committed
2519

2520
	netdev->queue = &netdev->local;
Linus Torvalds's avatar
Linus Torvalds committed
2521 2522
	spin_lock_init(&netdev->queue_lock);

2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544
	netdev->local.last = &netdev->local;
	netdev->local.netdev = netdev;
	netdev->local.next = &netdev->local;

	netdev->local.tqueue.sync = 0;
	netdev->local.tqueue.routine = isdn_net_softint;
	netdev->local.tqueue.data = &netdev->local;
	spin_lock_init(&netdev->local.xmit_lock);

	netdev->local.isdn_slot = -1;
	netdev->local.pre_device = -1;
	netdev->local.pre_channel = -1;
	netdev->local.exclusive = -1;
	netdev->local.ppp_slot = -1;
	netdev->local.pppbind = -1;
	skb_queue_head_init(&netdev->local.super_tx_queue);
	netdev->local.l2_proto = ISDN_PROTO_L2_X75I;
	netdev->local.l3_proto = ISDN_PROTO_L3_TRANS;
	netdev->local.triggercps = 6000;
	netdev->local.slavedelay = 10 * HZ;
	netdev->local.hupflags = ISDN_INHUP;	/* Do hangup even on incoming calls */
	netdev->local.onhtime = 10;	/* Default hangup-time for saving costs
Linus Torvalds's avatar
Linus Torvalds committed
2545
	   of those who forget configuring this */
2546 2547
	netdev->local.dialmax = 1;
	netdev->local.flags = ISDN_NET_CBHUP | ISDN_NET_DM_MANUAL;	/* Hangup before Callback, manual dial */
Kai Germaschewski's avatar
Kai Germaschewski committed
2548
	netdev->local.cbdelay = 5 * HZ;	/* Wait 5 secs before Callback */
2549 2550 2551 2552
	netdev->local.dialtimeout = -1;  /* Infinite Dial-Timeout */
	netdev->local.dialwait = 5 * HZ; /* Wait 5 sec. after failed dial */
	netdev->local.dialstarted = 0;   /* Jiffies of last dial-start */
	netdev->local.dialwait_timer = 0;  /* Jiffies of earliest next dial-start */
Linus Torvalds's avatar
Linus Torvalds committed
2553

2554 2555 2556
	init_timer(&netdev->local.dial_timer);
	netdev->local.dial_timer.data = (unsigned long) &netdev->local;
	netdev->local.dial_timer.function = isdn_net_dial_timer;
2557 2558 2559
	init_timer(&netdev->local.hup_timer);
	netdev->local.hup_timer.data = (unsigned long) &netdev->local;
	netdev->local.hup_timer.function = isdn_net_hup_timer;
2560

Linus Torvalds's avatar
Linus Torvalds committed
2561
	/* Put into to netdev-chain */
2562
	list_add(&netdev->global_list, &isdn_net_devs);
2563
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2564 2565
}

2566
int
Linus Torvalds's avatar
Linus Torvalds committed
2567 2568 2569
isdn_net_newslave(char *parm)
{
	char *p = strchr(parm, ',');
2570
	isdn_net_dev *m;
Linus Torvalds's avatar
Linus Torvalds committed
2571

2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587
	/* Slave-Name MUST not be empty */
	if (!p || !p[1])
		return -EINVAL;

	*p = 0;
	/* Master must already exist */
	if (!(m = isdn_net_findif(parm)))
		return -ESRCH;
	/* Master must be a real interface, not a slave */
	if (m->local.master)
		return -ENXIO;
	/* Master must not be started yet */
	if (isdn_net_device_started(m)) 
		return -EBUSY;

	return isdn_net_new(p+1, &m->dev);
Linus Torvalds's avatar
Linus Torvalds committed
2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
}

/*
 * Set interface-parameters.
 * Always set all parameters, so the user-level application is responsible
 * for not overwriting existing setups. It has to get the current
 * setup first, if only selected parameters are to be changed.
 */
int
isdn_net_setcfg(isdn_net_ioctl_cfg * cfg)
{
	isdn_net_dev *p = isdn_net_findif(cfg->name);
	ulong features;
	int i;
	int drvidx;
	int chidx;
	char drvid[25];
#ifdef CONFIG_ISDN_X25
	ulong flags;
#endif
	if (p) {
2609
		isdn_net_local *lp = &p->local;
Linus Torvalds's avatar
Linus Torvalds committed
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662

		/* See if any registered driver supports the features we want */
		features = ((1 << cfg->l2_proto) << ISDN_FEATURE_L2_SHIFT) |
			((1 << cfg->l3_proto) << ISDN_FEATURE_L3_SHIFT);
		for (i = 0; i < ISDN_MAX_DRIVERS; i++)
			if (dev->drv[i])
				if ((dev->drv[i]->interface->features & features) == features)
					break;
		if (i == ISDN_MAX_DRIVERS) {
			printk(KERN_WARNING "isdn_net: No driver with selected features\n");
			return -ENODEV;
		}
		if (lp->p_encap != cfg->p_encap){
#ifdef CONFIG_ISDN_X25
			struct concap_proto * cprot = p -> cprot;
#endif
			if (isdn_net_device_started(p)) {
				printk(KERN_WARNING "%s: cannot change encap when if is up\n",
				       lp->name);
				return -EBUSY;
			}
#ifdef CONFIG_ISDN_X25
			/* delete old encapsulation protocol if present ... */
			save_flags(flags);
			cli(); /* avoid races with incoming events trying to
				  call cprot->pops methods */
			if( cprot && cprot -> pops )
				cprot -> pops -> proto_del ( cprot );
			p -> cprot = NULL;
			lp -> dops = NULL;
			restore_flags(flags);
			/* ... ,  prepare for configuration of new one ... */
			switch ( cfg -> p_encap ){
			case ISDN_NET_ENCAP_X25IFACE:
				lp -> dops = &isdn_concap_reliable_dl_dops;
			}
			/* ... and allocate new one ... */
			p -> cprot = isdn_concap_new( cfg -> p_encap );
			/* p -> cprot == NULL now if p_encap is not supported
			   by means of the concap_proto mechanism */
			/* the protocol is not configured yet; this will
			   happen later when isdn_net_reset() is called */
#endif
		}
		switch ( cfg->p_encap ) {
		case ISDN_NET_ENCAP_SYNCPPP:
#ifndef CONFIG_ISDN_PPP
			printk(KERN_WARNING "%s: SyncPPP support not configured\n",
			       lp->name);
			return -EINVAL;
#else
			p->dev.type = ARPHRD_PPP;	/* change ARP type */
			p->dev.addr_len = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2663
			p->dev.do_ioctl = isdn_ppp_dev_ioctl;
Linus Torvalds's avatar
Linus Torvalds committed
2664 2665 2666 2667 2668
#endif
			break;
		case ISDN_NET_ENCAP_X25IFACE:
#ifndef CONFIG_ISDN_X25
			printk(KERN_WARNING "%s: isdn-x25 support not configured\n",
2669
			       p->local.name);
Linus Torvalds's avatar
Linus Torvalds committed
2670 2671 2672 2673 2674 2675
			return -EINVAL;
#else
			p->dev.type = ARPHRD_X25;	/* change ARP type */
			p->dev.addr_len = 0;
#endif
			break;
Linus Torvalds's avatar
Linus Torvalds committed
2676 2677 2678
		case ISDN_NET_ENCAP_CISCOHDLCK:
			p->dev.do_ioctl = isdn_ciscohdlck_dev_ioctl;
			break;
Linus Torvalds's avatar
Linus Torvalds committed
2679 2680 2681 2682 2683 2684
		default:
			if( cfg->p_encap >= 0 &&
			    cfg->p_encap <= ISDN_NET_ENCAP_MAX_ENCAP )
				break;
			printk(KERN_WARNING
			       "%s: encapsulation protocol %d not supported\n",
2685
			       p->local.name, cfg->p_encap);
Linus Torvalds's avatar
Linus Torvalds committed
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
			return -EINVAL;
		}
		if (strlen(cfg->drvid)) {
			/* A bind has been requested ... */
			char *c,
			*e;

			drvidx = -1;
			chidx = -1;
			strcpy(drvid, cfg->drvid);
			if ((c = strchr(drvid, ','))) {
				/* The channel-number is appended to the driver-Id with a comma */
				chidx = (int) simple_strtoul(c + 1, &e, 10);
				if (e == c)
					chidx = -1;
				*c = '\0';
			}
			for (i = 0; i < ISDN_MAX_DRIVERS; i++)
				/* Lookup driver-Id in array */
				if (!(strcmp(dev->drvid[i], drvid))) {
					drvidx = i;
					break;
				}
			if ((drvidx == -1) || (chidx == -1))
				/* Either driver-Id or channel-number invalid */
				return -ENODEV;
		} else {
			/* Parameters are valid, so get them */
			drvidx = lp->pre_device;
			chidx = lp->pre_channel;
		}
		if (cfg->exclusive > 0) {
Linus Torvalds's avatar
Linus Torvalds committed
2718
			unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
2719 2720 2721

			/* If binding is exclusive, try to grab the channel */
			save_flags(flags);
2722
			if ((i = isdn_get_free_slot(ISDN_USAGE_NET,
Linus Torvalds's avatar
Linus Torvalds committed
2723 2724 2725 2726 2727 2728 2729 2730
				lp->l2_proto, lp->l3_proto, drvidx,
				chidx, lp->msn)) < 0) {
				/* Grab failed, because desired channel is in use */
				lp->exclusive = -1;
				restore_flags(flags);
				return -EBUSY;
			}
			/* All went ok, so update isdninfo */
2731
			isdn_slot_set_usage(i, ISDN_USAGE_EXCLUSIVE);
Linus Torvalds's avatar
Linus Torvalds committed
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750
			restore_flags(flags);
			lp->exclusive = i;
		} else {
			/* Non-exclusive binding or unbind. */
			lp->exclusive = -1;
			if ((lp->pre_device != -1) && (cfg->exclusive == -1)) {
				isdn_unexclusive_channel(lp->pre_device, lp->pre_channel);
				isdn_free_channel(lp->pre_device, lp->pre_channel, ISDN_USAGE_NET);
				drvidx = -1;
				chidx = -1;
			}
		}
		strcpy(lp->msn, cfg->eaz);
		lp->pre_device = drvidx;
		lp->pre_channel = chidx;
		lp->onhtime = cfg->onhtime;
		lp->charge = cfg->charge;
		lp->l2_proto = cfg->l2_proto;
		lp->l3_proto = cfg->l3_proto;
Kai Germaschewski's avatar
Kai Germaschewski committed
2751
		lp->cbdelay = cfg->cbdelay * HZ / 5;
Linus Torvalds's avatar
Linus Torvalds committed
2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
		lp->dialmax = cfg->dialmax;
		lp->triggercps = cfg->triggercps;
		lp->slavedelay = cfg->slavedelay * HZ;
		lp->pppbind = cfg->pppbind;
		lp->dialtimeout = cfg->dialtimeout >= 0 ? cfg->dialtimeout * HZ : -1;
		lp->dialwait = cfg->dialwait * HZ;
		if (cfg->secure)
			lp->flags |= ISDN_NET_SECURE;
		else
			lp->flags &= ~ISDN_NET_SECURE;
		if (cfg->cbhup)
			lp->flags |= ISDN_NET_CBHUP;
		else
			lp->flags &= ~ISDN_NET_CBHUP;
		switch (cfg->callback) {
			case 0:
				lp->flags &= ~(ISDN_NET_CALLBACK | ISDN_NET_CBOUT);
				break;
			case 1:
				lp->flags |= ISDN_NET_CALLBACK;
				lp->flags &= ~ISDN_NET_CBOUT;
				break;
			case 2:
				lp->flags |= ISDN_NET_CBOUT;
				lp->flags &= ~ISDN_NET_CALLBACK;
				break;
		}
		lp->flags &= ~ISDN_NET_DIALMODE_MASK;	/* first all bits off */
		if (cfg->dialmode && !(cfg->dialmode & ISDN_NET_DIALMODE_MASK)) {
			/* old isdnctrl version, where only 0 or 1 is given */
			printk(KERN_WARNING
			     "Old isdnctrl version detected! Please update.\n");
			lp->flags |= ISDN_NET_DM_OFF; /* turn on `off' bit */
		}
		else {
			lp->flags |= cfg->dialmode;  /* turn on selected bits */
		}
2789 2790 2791
		if (lp->flags & ISDN_NET_DM_OFF)
			isdn_net_hangup(lp);

Linus Torvalds's avatar
Linus Torvalds committed
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
		if (cfg->chargehup)
			lp->hupflags |= ISDN_CHARGEHUP;
		else
			lp->hupflags &= ~ISDN_CHARGEHUP;
		if (cfg->ihup)
			lp->hupflags |= ISDN_INHUP;
		else
			lp->hupflags &= ~ISDN_INHUP;
		if (cfg->chargeint > 10) {
			lp->chargeint = cfg->chargeint * HZ;
2802 2803
			lp->charge_state = ST_CHARGE_HAVE_CINT;
			lp->hupflags |= ISDN_MANCHARGE;
Linus Torvalds's avatar
Linus Torvalds committed
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
		}
		if (cfg->p_encap != lp->p_encap) {
			if (cfg->p_encap == ISDN_NET_ENCAP_RAWIP) {
				p->dev.hard_header = NULL;
				p->dev.hard_header_cache = NULL;
				p->dev.header_cache_update = NULL;
				p->dev.flags = IFF_NOARP|IFF_POINTOPOINT;
			} else {
				p->dev.hard_header = isdn_net_header;
				if (cfg->p_encap == ISDN_NET_ENCAP_ETHER) {
					p->dev.hard_header_cache = lp->org_hhc;
					p->dev.header_cache_update = lp->org_hcu;
					p->dev.flags = IFF_BROADCAST | IFF_MULTICAST;
				} else {
					p->dev.hard_header_cache = NULL;
					p->dev.header_cache_update = NULL;
					p->dev.flags = IFF_NOARP|IFF_POINTOPOINT;
				}
			}
		}
		lp->p_encap = cfg->p_encap;
		return 0;
	}
	return -ENODEV;
}

/*
 * Perform get-interface-parameters.ioctl
 */
int
isdn_net_getcfg(isdn_net_ioctl_cfg * cfg)
{
	isdn_net_dev *p = isdn_net_findif(cfg->name);

	if (p) {
2839
		isdn_net_local *lp = &p->local;
Linus Torvalds's avatar
Linus Torvalds committed
2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862

		strcpy(cfg->eaz, lp->msn);
		cfg->exclusive = lp->exclusive;
		if (lp->pre_device >= 0) {
			sprintf(cfg->drvid, "%s,%d", dev->drvid[lp->pre_device],
				lp->pre_channel);
		} else
			cfg->drvid[0] = '\0';
		cfg->onhtime = lp->onhtime;
		cfg->charge = lp->charge;
		cfg->l2_proto = lp->l2_proto;
		cfg->l3_proto = lp->l3_proto;
		cfg->p_encap = lp->p_encap;
		cfg->secure = (lp->flags & ISDN_NET_SECURE) ? 1 : 0;
		cfg->callback = 0;
		if (lp->flags & ISDN_NET_CALLBACK)
			cfg->callback = 1;
		if (lp->flags & ISDN_NET_CBOUT)
			cfg->callback = 2;
		cfg->cbhup = (lp->flags & ISDN_NET_CBHUP) ? 1 : 0;
		cfg->dialmode = lp->flags & ISDN_NET_DIALMODE_MASK;
		cfg->chargehup = (lp->hupflags & 4) ? 1 : 0;
		cfg->ihup = (lp->hupflags & 8) ? 1 : 0;
Kai Germaschewski's avatar
Kai Germaschewski committed
2863
		cfg->cbdelay = lp->cbdelay * 5 / HZ;
Linus Torvalds's avatar
Linus Torvalds committed
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891
		cfg->dialmax = lp->dialmax;
		cfg->triggercps = lp->triggercps;
		cfg->slavedelay = lp->slavedelay / HZ;
		cfg->chargeint = (lp->hupflags & ISDN_CHARGEHUP) ?
		    (lp->chargeint / HZ) : 0;
		cfg->pppbind = lp->pppbind;
		cfg->dialtimeout = lp->dialtimeout >= 0 ? lp->dialtimeout / HZ : -1;
		cfg->dialwait = lp->dialwait / HZ;
		if (lp->slave)
			strcpy(cfg->slave, ((isdn_net_local *) lp->slave->priv)->name);
		else
			cfg->slave[0] = '\0';
		if (lp->master)
			strcpy(cfg->master, ((isdn_net_local *) lp->master->priv)->name);
		else
			cfg->master[0] = '\0';
		return 0;
	}
	return -ENODEV;
}

/*
 * Add a phone-number to an interface.
 */
int
isdn_net_addphone(isdn_net_ioctl_phone * phone)
{
	isdn_net_dev *p = isdn_net_findif(phone->name);
2892
	struct isdn_net_phone *n;
Linus Torvalds's avatar
Linus Torvalds committed
2893

2894 2895 2896 2897 2898 2899 2900 2901
	if (!p)
		return -ENODEV;

	n = kmalloc(sizeof(*n), GFP_KERNEL);
	if (!n)
		return -ENOMEM;

	strcpy(n->num, phone->phone);
2902
	list_add_tail(&n->list, &p->local.phone[phone->outgoing & 1]);
2903
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
}

/*
 * Copy a string of all phone-numbers of an interface to user space.
 * This might sleep and must be called with the isdn semaphore down.
 */
int
isdn_net_getphones(isdn_net_ioctl_phone * phone, char *phones)
{
	isdn_net_dev *p = isdn_net_findif(phone->name);
	int inout = phone->outgoing & 1;
	int more = 0;
	int count = 0;
2917
	struct isdn_net_phone *n;
Linus Torvalds's avatar
Linus Torvalds committed
2918 2919 2920

	if (!p)
		return -ENODEV;
2921

Linus Torvalds's avatar
Linus Torvalds committed
2922
	inout &= 1;
2923
	list_for_each_entry(n, &p->local.phone[inout], list) {
Linus Torvalds's avatar
Linus Torvalds committed
2924
		if (more) {
2925 2926
			if (put_user(' ', phones++))
				return -EFAULT;
Linus Torvalds's avatar
Linus Torvalds committed
2927 2928 2929 2930 2931 2932 2933 2934 2935
			count++;
		}
		if (copy_to_user(phones, n->num, strlen(n->num) + 1)) {
			return -EFAULT;
		}
		phones += strlen(n->num);
		count += strlen(n->num);
		more = 1;
	}
2936 2937 2938
	if (put_user(0, phones))
		return -EFAULT;

Linus Torvalds's avatar
Linus Torvalds committed
2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
	count++;
	return count;
}

/*
 * Copy a string containing the peer's phone number of a connected interface
 * to user space.
 */
int
isdn_net_getpeer(isdn_net_ioctl_phone *phone, isdn_net_ioctl_phone *peer)
{
	isdn_net_dev *p = isdn_net_findif(phone->name);
2951
	int idx;
Linus Torvalds's avatar
Linus Torvalds committed
2952 2953 2954 2955 2956 2957 2958 2959

	if (!p) return -ENODEV;
	/*
	 * Theoretical race: while this executes, the remote number might
	 * become invalid (hang up) or change (new connection), resulting
         * in (partially) wrong number copied to user. This race
	 * currently ignored.
	 */
2960
	idx = p->local.isdn_slot;
2961
	if (idx<0) return -ENOTCONN;
Linus Torvalds's avatar
Linus Torvalds committed
2962
	/* for pre-bound channels, we need this extra check */
2963 2964
	if (strncmp(isdn_slot_num(idx),"???",3) == 0 ) return -ENOTCONN;
	strncpy(phone->phone,isdn_slot_num(idx),ISDN_MSNLEN);
2965
	phone->outgoing=USG_OUTGOING(isdn_slot_usage(idx));
Linus Torvalds's avatar
Linus Torvalds committed
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
	if ( copy_to_user(peer,phone,sizeof(*peer)) ) return -EFAULT;
	return 0;
}
/*
 * Delete a phone-number from an interface.
 */
int
isdn_net_delphone(isdn_net_ioctl_phone * phone)
{
	isdn_net_dev *p = isdn_net_findif(phone->name);
	int inout = phone->outgoing & 1;
2977
	struct isdn_net_phone *n;
Linus Torvalds's avatar
Linus Torvalds committed
2978
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
2979

2980 2981 2982 2983 2984
	if (!p)
		return -ENODEV;

	save_flags(flags);
	cli();
2985
	list_for_each_entry(n, &p->local.phone[inout], list) {
2986
		if (!strcmp(n->num, phone->phone)) {
2987
			list_del(&n->list);
2988 2989 2990
			kfree(n);
			restore_flags(flags);
			return 0;
Linus Torvalds's avatar
Linus Torvalds committed
2991 2992
		}
	}
2993 2994
	restore_flags(flags);
	return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
2995 2996 2997 2998 2999 3000 3001 3002
}

/*
 * Delete all phone-numbers of an interface.
 */
static int
isdn_net_rmallphone(isdn_net_dev * p)
{
3003
	struct isdn_net_phone *n;
Linus Torvalds's avatar
Linus Torvalds committed
3004
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
3005 3006 3007 3008 3009
	int i;

	save_flags(flags);
	cli();
	for (i = 0; i < 2; i++) {
3010 3011 3012
		while (!list_empty(&p->local.phone[i])) {
			n = list_entry(p->local.phone[i].next, struct isdn_net_phone, list);
			list_del(&n->list);
Linus Torvalds's avatar
Linus Torvalds committed
3013 3014 3015
			kfree(n);
		}
	}
3016
	p->local.dial = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030
	restore_flags(flags);
	return 0;
}

/*
 * Force a hangup of a network-interface.
 */
int
isdn_net_force_hangup(char *name)
{
	isdn_net_dev *p = isdn_net_findif(name);
	struct net_device *q;

	if (p) {
3031
		if (p->local.isdn_slot < 0)
Linus Torvalds's avatar
Linus Torvalds committed
3032
			return 1;
3033
		q = p->local.slave;
Linus Torvalds's avatar
Linus Torvalds committed
3034 3035
		/* If this interface has slaves, do a hangup for them also. */
		while (q) {
3036
			isdn_net_hangup(&p->local);
Linus Torvalds's avatar
Linus Torvalds committed
3037 3038
			q = (((isdn_net_local *) q->priv)->slave);
		}
3039
		isdn_net_hangup(&p->local);
Linus Torvalds's avatar
Linus Torvalds committed
3040 3041 3042 3043 3044 3045 3046 3047 3048
		return 0;
	}
	return -ENODEV;
}

/*
 * Helper-function for isdn_net_rm: Do the real work.
 */
static int
3049
isdn_net_realrm(isdn_net_dev *p)
Linus Torvalds's avatar
Linus Torvalds committed
3050
{
Linus Torvalds's avatar
Linus Torvalds committed
3051
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065

	save_flags(flags);
	cli();
	if (isdn_net_device_started(p)) {
		restore_flags(flags);
		return -EBUSY;
	}
#ifdef CONFIG_ISDN_X25
	if( p -> cprot && p -> cprot -> pops )
		p -> cprot -> pops -> proto_del ( p -> cprot );
#endif
	/* Free all phone-entries */
	isdn_net_rmallphone(p);
	/* If interface is bound exclusive, free channel-usage */
3066 3067 3068
	if (p->local.exclusive != -1)
		isdn_unexclusive_channel(p->local.pre_device, p->local.pre_channel);
	if (p->local.master) {
Linus Torvalds's avatar
Linus Torvalds committed
3069
		/* It's a slave-device, so update master's slave-pointer if necessary */
3070 3071
		if (((isdn_net_local *) (p->local.master->priv))->slave == &p->dev)
			((isdn_net_local *) (p->local.master->priv))->slave = p->local.slave;
Linus Torvalds's avatar
Linus Torvalds committed
3072 3073
	} else {
		/* Unregister only if it's a master-device */
3074 3075
		p->dev.hard_header_cache = p->local.org_hhc;
		p->dev.header_cache_update = p->local.org_hcu;
Linus Torvalds's avatar
Linus Torvalds committed
3076 3077 3078
		unregister_netdev(&p->dev);
	}
	/* Unlink device from chain */
3079
	list_del(&p->global_list);
3080
	if (p->local.slave) {
Linus Torvalds's avatar
Linus Torvalds committed
3081
		/* If this interface has a slave, remove it also */
3082
		char *slavename = ((isdn_net_local *) (p->local.slave->priv))->name;
3083 3084 3085 3086
		struct list_head *l;

		list_for_each(l, &isdn_net_devs) {
			isdn_net_dev *n = list_entry(l, isdn_net_dev, global_list);
3087
			if (!strcmp(n->local.name, slavename)) {
3088
				isdn_net_realrm(n);
Linus Torvalds's avatar
Linus Torvalds committed
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104
				break;
			}
		}
	}
	restore_flags(flags);
	kfree(p);

	return 0;
}

/*
 * Remove a single network-interface.
 */
int
isdn_net_rm(char *name)
{
3105
	struct list_head *l;
Linus Torvalds's avatar
Linus Torvalds committed
3106 3107

	/* Search name in netdev-chain */
3108 3109
	list_for_each(l, &isdn_net_devs) {
		isdn_net_dev *p = list_entry(l, isdn_net_dev, global_list);
3110
		if (!strcmp(p->local.name, name))
3111
			return isdn_net_realrm(p);
Linus Torvalds's avatar
Linus Torvalds committed
3112 3113 3114 3115 3116 3117 3118 3119 3120 3121
	}
	return -ENODEV;
}

/*
 * Remove all network-interfaces
 */
int
isdn_net_rmall(void)
{
Linus Torvalds's avatar
Linus Torvalds committed
3122
	unsigned long flags;
Linus Torvalds's avatar
Linus Torvalds committed
3123 3124 3125 3126 3127
	int ret;

	/* Walk through netdev-chain */
	save_flags(flags);
	cli();
3128 3129 3130 3131 3132 3133
	while (!list_empty(&isdn_net_devs)) {
		isdn_net_dev *p = list_entry(isdn_net_devs.next, isdn_net_dev, global_list);

		/* Remove master-devices only, slaves get removed with their master */
		if (!p->local.master) {
			if ((ret = isdn_net_realrm(p))) {
Linus Torvalds's avatar
Linus Torvalds committed
3134 3135 3136 3137 3138 3139 3140 3141
				restore_flags(flags);
				return ret;
			}
		}
	}
	restore_flags(flags);
	return 0;
}