1 /*
2  * slip.c	This module implements the SLIP protocol for kernel-based
3  *		devices like TTY.  It interfaces between a raw TTY, and the
4  *		kernel's INET protocol layers.
5  *
6  * Version:	@(#)slip.c	0.8.3	12/24/94
7  *
8  * Authors:	Laurence Culhane, <loz@holmes.demon.co.uk>
9  *		Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
10  *
11  * Fixes:
12  *		Alan Cox	: 	Sanity checks and avoid tx overruns.
13  *					Has a new sl->mtu field.
14  *		Alan Cox	: 	Found cause of overrun. ifconfig sl0 mtu upwards.
15  *					Driver now spots this and grows/shrinks its buffers(hack!).
16  *					Memory leak if you run out of memory setting up a slip driver fixed.
17  *		Matt Dillon	:	Printable slip (borrowed from NET2E)
18  *	Pauline Middelink	:	Slip driver fixes.
19  *		Alan Cox	:	Honours the old SL_COMPRESSED flag
20  *		Alan Cox	:	KISS AX.25 and AXUI IP support
21  *		Michael Riepe	:	Automatic CSLIP recognition added
22  *		Charles Hedrick :	CSLIP header length problem fix.
23  *		Alan Cox	:	Corrected non-IP cases of the above.
24  *		Alan Cox	:	Now uses hardware type as per FvK.
25  *		Alan Cox	:	Default to 192.168.0.0 (RFC 1597)
26  *		A.N.Kuznetsov	:	dev_tint() recursion fix.
27  *	Dmitry Gorodchanin	:	SLIP memory leaks
28  *      Dmitry Gorodchanin      :       Code cleanup. Reduce tty driver
29  *                                      buffering from 4096 to 256 bytes.
30  *                                      Improving SLIP response time.
31  *                                      CONFIG_SLIP_MODE_SLIP6.
32  *                                      ifconfig sl? up & down now works correctly.
33  *					Modularization.
34  *              Alan Cox        :       Oops - fix AX.25 buffer lengths
35  *      Dmitry Gorodchanin      :       Even more cleanups. Preserve CSLIP
36  *                                      statistics. Include CSLIP code only
37  *                                      if it really needed.
38  *		Alan Cox	:	Free slhc buffers in the right place.
39  *		Alan Cox	:	Allow for digipeated IP over AX.25
40  *		Matti Aarnio	:	Dynamic SLIP devices, with ideas taken
41  *					from Jim Freeman's <jfree@caldera.com>
42  *					dynamic PPP devices.  We do NOT kfree()
43  *					device entries, just reg./unreg. them
44  *					as they are needed.  We kfree() them
45  *					at module cleanup.
46  *					With MODULE-loading ``insmod'', user can
47  *					issue parameter:   slip_maxdev=1024
48  *					(Or how much he/she wants.. Default is 256)
49  * *	Stanislav Voronyi	:	Slip line checking, with ideas taken
50  *					from multislip BSDI driver which was written
51  *					by Igor Chechik, RELCOM Corp. Only algorithms
52  * 					have been ported to Linux SLIP driver.
53  *	Vitaly E. Lavrov	:	Sane behaviour on tty hangup.
54  *	Alexey Kuznetsov	:	Cleanup interfaces to tty&netdevice modules.
55  */
56 
57 #define SL_CHECK_TRANSMIT
58 #include <linux/config.h>
59 #include <linux/module.h>
60 
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
63 #include <asm/bitops.h>
64 #include <linux/string.h>
65 #include <linux/mm.h>
66 #include <linux/interrupt.h>
67 #include <linux/in.h>
68 #include <linux/tty.h>
69 #include <linux/errno.h>
70 #include <linux/netdevice.h>
71 #include <linux/etherdevice.h>
72 #include <linux/skbuff.h>
73 #include <linux/rtnetlink.h>
74 #include <linux/if_arp.h>
75 #include <linux/if_slip.h>
76 #include <linux/init.h>
77 #include "slip.h"
78 #ifdef CONFIG_INET
79 #include <linux/ip.h>
80 #include <linux/tcp.h>
81 #include <net/slhc_vj.h>
82 #endif
83 
84 #ifdef MODULE
85 #define SLIP_VERSION    "0.8.4-NET3.019-NEWTTY-MODULAR"
86 #else
87 #define	SLIP_VERSION	"0.8.4-NET3.019-NEWTTY"
88 #endif
89 
90 
91 typedef struct slip_ctrl {
92 	struct slip	ctrl;		/* SLIP things			*/
93 	struct net_device	dev;		/* the device			*/
94 } slip_ctrl_t;
95 static slip_ctrl_t	**slip_ctrls;
96 
97 int slip_maxdev = SL_NRUNIT;		/* Can be overridden with insmod! */
98 MODULE_PARM(slip_maxdev, "i");
99 MODULE_PARM_DESC(slip_maxdev, "Maximum number of slip devices");
100 
101 static struct tty_ldisc	sl_ldisc;
102 
103 static int slip_esc(unsigned char *p, unsigned char *d, int len);
104 static void slip_unesc(struct slip *sl, unsigned char c);
105 #ifdef CONFIG_SLIP_MODE_SLIP6
106 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
107 static void slip_unesc6(struct slip *sl, unsigned char c);
108 #endif
109 #ifdef CONFIG_SLIP_SMART
110 static void sl_keepalive(unsigned long sls);
111 static void sl_outfill(unsigned long sls);
112 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd);
113 #endif
114 
115 /********************************
116 *  Buffer administration routines:
117 *	sl_alloc_bufs()
118 *	sl_free_bufs()
119 *	sl_realloc_bufs()
120 *
121 * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
122 *	sl_realloc_bufs provides strong atomicity and reallocation
123 *	on actively running device.
124 *********************************/
125 
126 /*
127    Allocate channel buffers.
128  */
129 
130 static int
sl_alloc_bufs(struct slip * sl,int mtu)131 sl_alloc_bufs(struct slip *sl, int mtu)
132 {
133 	int err = -ENOBUFS;
134 	unsigned long len;
135 	char * rbuff = NULL;
136 	char * xbuff = NULL;
137 #ifdef SL_INCLUDE_CSLIP
138 	char * cbuff = NULL;
139 	struct slcompress *slcomp = NULL;
140 #endif
141 
142 	/*
143 	 * Allocate the SLIP frame buffers:
144 	 *
145 	 * rbuff	Receive buffer.
146 	 * xbuff	Transmit buffer.
147 	 * cbuff        Temporary compression buffer.
148 	 */
149 	len = mtu * 2;
150 
151 	/*
152 	 * allow for arrival of larger UDP packets, even if we say not to
153 	 * also fixes a bug in which SunOS sends 512-byte packets even with
154 	 * an MSS of 128
155 	 */
156 	if (len < 576 * 2)
157 		len = 576 * 2;
158 	rbuff = kmalloc(len + 4, GFP_KERNEL);
159 	if (rbuff == NULL)
160 		goto err_exit;
161 	xbuff = kmalloc(len + 4, GFP_KERNEL);
162 	if (xbuff == NULL)
163 		goto err_exit;
164 #ifdef SL_INCLUDE_CSLIP
165 	cbuff = kmalloc(len + 4, GFP_KERNEL);
166 	if (cbuff == NULL)
167 		goto err_exit;
168 	slcomp = slhc_init(16, 16);
169 	if (slcomp == NULL)
170 		goto err_exit;
171 #endif
172 	spin_lock_bh(&sl->lock);
173 	if (sl->tty == NULL) {
174 		spin_unlock_bh(&sl->lock);
175 		err = -ENODEV;
176 		goto err_exit;
177 	}
178 	sl->mtu	     = mtu;
179 	sl->buffsize = len;
180 	sl->rcount   = 0;
181 	sl->xleft    = 0;
182 	rbuff = xchg(&sl->rbuff, rbuff);
183 	xbuff = xchg(&sl->xbuff, xbuff);
184 #ifdef SL_INCLUDE_CSLIP
185 	cbuff = xchg(&sl->cbuff, cbuff);
186 	slcomp = xchg(&sl->slcomp, slcomp);
187 #ifdef CONFIG_SLIP_MODE_SLIP6
188 	sl->xdata    = 0;
189 	sl->xbits    = 0;
190 #endif
191 #endif
192 	spin_unlock_bh(&sl->lock);
193 	err = 0;
194 
195 	/* Cleanup */
196 err_exit:
197 #ifdef SL_INCLUDE_CSLIP
198 	if (cbuff)
199 		kfree(cbuff);
200 	if (slcomp)
201 		slhc_free(slcomp);
202 #endif
203 	if (xbuff)
204 		kfree(xbuff);
205 	if (rbuff)
206 		kfree(rbuff);
207 	return err;
208 }
209 
210 /* Free a SLIP channel buffers. */
211 static void
sl_free_bufs(struct slip * sl)212 sl_free_bufs(struct slip *sl)
213 {
214 	void * tmp;
215 
216 	/* Free all SLIP frame buffers. */
217 	if ((tmp = xchg(&sl->rbuff, NULL)) != NULL)
218 		kfree(tmp);
219 	if ((tmp = xchg(&sl->xbuff, NULL)) != NULL)
220 		kfree(tmp);
221 #ifdef SL_INCLUDE_CSLIP
222 	if ((tmp = xchg(&sl->cbuff, NULL)) != NULL)
223 		kfree(tmp);
224 	if ((tmp = xchg(&sl->slcomp, NULL)) != NULL)
225 		slhc_free(tmp);
226 #endif
227 }
228 
229 /*
230    Reallocate slip channel buffers.
231  */
232 
sl_realloc_bufs(struct slip * sl,int mtu)233 static int sl_realloc_bufs(struct slip *sl, int mtu)
234 {
235 	int err = 0;
236 	struct net_device *dev = sl->dev;
237 	unsigned char *xbuff, *rbuff;
238 #ifdef SL_INCLUDE_CSLIP
239 	unsigned char *cbuff;
240 #endif
241 	int len = mtu * 2;
242 
243 /*
244  * allow for arrival of larger UDP packets, even if we say not to
245  * also fixes a bug in which SunOS sends 512-byte packets even with
246  * an MSS of 128
247  */
248 	if (len < 576 * 2)
249 		len = 576 * 2;
250 
251 	xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
252 	rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
253 #ifdef SL_INCLUDE_CSLIP
254 	cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
255 #endif
256 
257 
258 #ifdef SL_INCLUDE_CSLIP
259 	if (xbuff == NULL || rbuff == NULL || cbuff == NULL)  {
260 #else
261 	if (xbuff == NULL || rbuff == NULL)  {
262 #endif
263 		if (mtu >= sl->mtu) {
264 			printk("%s: unable to grow slip buffers, MTU change cancelled.\n",
265 			       dev->name);
266 			err = -ENOBUFS;
267 		}
268 		goto done;
269 	}
270 
271 	spin_lock_bh(&sl->lock);
272 
273 	err = -ENODEV;
274 	if (sl->tty == NULL)
275 		goto done_on_bh;
276 
277 	xbuff    = xchg(&sl->xbuff, xbuff);
278 	rbuff    = xchg(&sl->rbuff, rbuff);
279 #ifdef SL_INCLUDE_CSLIP
280 	cbuff    = xchg(&sl->cbuff, cbuff);
281 #endif
282 	if (sl->xleft)  {
283 		if (sl->xleft <= len)  {
284 			memcpy(sl->xbuff, sl->xhead, sl->xleft);
285 		} else  {
286 			sl->xleft = 0;
287 			sl->tx_dropped++;
288 		}
289 	}
290 	sl->xhead = sl->xbuff;
291 
292 	if (sl->rcount)  {
293 		if (sl->rcount <= len) {
294 			memcpy(sl->rbuff, rbuff, sl->rcount);
295 		} else  {
296 			sl->rcount = 0;
297 			sl->rx_over_errors++;
298 			set_bit(SLF_ERROR, &sl->flags);
299 		}
300 	}
301 	sl->mtu      = mtu;
302 	dev->mtu      = mtu;
303 	sl->buffsize = len;
304 	err = 0;
305 
306 done_on_bh:
307 	spin_unlock_bh(&sl->lock);
308 
309 done:
310 	if (xbuff)
311 		kfree(xbuff);
312 	if (rbuff)
313 		kfree(rbuff);
314 #ifdef SL_INCLUDE_CSLIP
315 	if (cbuff)
316 		kfree(cbuff);
317 #endif
318 	return err;
319 }
320 
321 
322 /* Set the "sending" flag.  This must be atomic hence the set_bit. */
323 static inline void
324 sl_lock(struct slip *sl)
325 {
326 	netif_stop_queue(sl->dev);
327 }
328 
329 
330 /* Clear the "sending" flag.  This must be atomic, hence the ASM. */
331 static inline void
332 sl_unlock(struct slip *sl)
333 {
334 	netif_wake_queue(sl->dev);
335 }
336 
337 /* Send one completely decapsulated IP datagram to the IP layer. */
338 static void
339 sl_bump(struct slip *sl)
340 {
341 	struct sk_buff *skb;
342 	int count;
343 
344 	count = sl->rcount;
345 #ifdef SL_INCLUDE_CSLIP
346 	if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
347 		unsigned char c;
348 		if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
349 			/* ignore compressed packets when CSLIP is off */
350 			if (!(sl->mode & SL_MODE_CSLIP)) {
351 				printk("%s: compressed packet ignored\n", sl->dev->name);
352 				return;
353 			}
354 			/* make sure we've reserved enough space for uncompress to use */
355 			if (count + 80 > sl->buffsize) {
356 				sl->rx_over_errors++;
357 				return;
358 			}
359 			count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
360 			if (count <= 0) {
361 				return;
362 			}
363 		} else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
364 			if (!(sl->mode & SL_MODE_CSLIP)) {
365 				/* turn on header compression */
366 				sl->mode |= SL_MODE_CSLIP;
367 				sl->mode &= ~SL_MODE_ADAPTIVE;
368 				printk("%s: header compression turned on\n", sl->dev->name);
369 			}
370 			sl->rbuff[0] &= 0x4f;
371 			if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
372 				return;
373 			}
374 		}
375 	}
376 #endif  /* SL_INCLUDE_CSLIP */
377 
378 	sl->rx_bytes+=count;
379 
380 	skb = dev_alloc_skb(count);
381 	if (skb == NULL)  {
382 		printk("%s: memory squeeze, dropping packet.\n", sl->dev->name);
383 		sl->rx_dropped++;
384 		return;
385 	}
386 	skb->dev = sl->dev;
387 	memcpy(skb_put(skb,count), sl->rbuff, count);
388 	skb->mac.raw=skb->data;
389 	skb->protocol=htons(ETH_P_IP);
390 	netif_rx(skb);
391 	sl->dev->last_rx = jiffies;
392 	sl->rx_packets++;
393 }
394 
395 /* Encapsulate one IP datagram and stuff into a TTY queue. */
396 static void
397 sl_encaps(struct slip *sl, unsigned char *icp, int len)
398 {
399 	unsigned char *p;
400 	int actual, count;
401 
402 	if (len > sl->mtu) {		/* Sigh, shouldn't occur BUT ... */
403 		printk ("%s: truncating oversized transmit packet!\n", sl->dev->name);
404 		sl->tx_dropped++;
405 		sl_unlock(sl);
406 		return;
407 	}
408 
409 	p = icp;
410 #ifdef SL_INCLUDE_CSLIP
411 	if (sl->mode & SL_MODE_CSLIP)  {
412 		len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
413 	}
414 #endif
415 #ifdef CONFIG_SLIP_MODE_SLIP6
416 	if(sl->mode & SL_MODE_SLIP6)
417 		count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
418 	else
419 #endif
420 		count = slip_esc(p, (unsigned char *) sl->xbuff, len);
421 
422 	/* Order of next two lines is *very* important.
423 	 * When we are sending a little amount of data,
424 	 * the transfer may be completed inside driver.write()
425 	 * routine, because it's running with interrupts enabled.
426 	 * In this case we *never* got WRITE_WAKEUP event,
427 	 * if we did not request it before write operation.
428 	 *       14 Oct 1994  Dmitry Gorodchanin.
429 	 */
430 	sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
431 	actual = sl->tty->driver.write(sl->tty, 0, sl->xbuff, count);
432 #ifdef SL_CHECK_TRANSMIT
433 	sl->dev->trans_start = jiffies;
434 #endif
435 	sl->xleft = count - actual;
436 	sl->xhead = sl->xbuff + actual;
437 #ifdef CONFIG_SLIP_SMART
438 	/* VSV */
439 	clear_bit(SLF_OUTWAIT, &sl->flags);	/* reset outfill flag */
440 #endif
441 }
442 
443 /*
444  * Called by the driver when there's room for more data.  If we have
445  * more packets to send, we send them here.
446  */
447 static void slip_write_wakeup(struct tty_struct *tty)
448 {
449 	int actual;
450 	struct slip *sl = (struct slip *) tty->disc_data;
451 
452 	/* First make sure we're connected. */
453 	if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
454 		return;
455 	}
456 	if (sl->xleft <= 0)  {
457 		/* Now serial buffer is almost free & we can start
458 		 * transmission of another packet */
459 		sl->tx_packets++;
460 		tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
461 		sl_unlock(sl);
462 		return;
463 	}
464 
465 	actual = tty->driver.write(tty, 0, sl->xhead, sl->xleft);
466 	sl->xleft -= actual;
467 	sl->xhead += actual;
468 }
469 
470 static void sl_tx_timeout(struct net_device *dev)
471 {
472 	struct slip *sl = (struct slip*)(dev->priv);
473 
474 	spin_lock(&sl->lock);
475 
476 	if (netif_queue_stopped(dev)) {
477 		struct slip *sl = (struct slip*)(dev->priv);
478 
479 		if (!netif_running(dev))
480 			goto out;
481 
482 		/* May be we must check transmitter timeout here ?
483 		 *      14 Oct 1994 Dmitry Gorodchanin.
484 		 */
485 #ifdef SL_CHECK_TRANSMIT
486 		if (time_before(jiffies, dev->trans_start + 20 * HZ))  {
487 			/* 20 sec timeout not reached */
488 			goto out;
489 		}
490 		printk("%s: transmit timed out, %s?\n", dev->name,
491 		       (sl->tty->driver.chars_in_buffer(sl->tty) || sl->xleft) ?
492 		       "bad line quality" : "driver error");
493 		sl->xleft = 0;
494 		sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
495 		sl_unlock(sl);
496 #endif
497 	}
498 
499 out:
500 	spin_unlock(&sl->lock);
501 }
502 
503 
504 /* Encapsulate an IP datagram and kick it into a TTY queue. */
505 static int
506 sl_xmit(struct sk_buff *skb, struct net_device *dev)
507 {
508 	struct slip *sl = (struct slip*)(dev->priv);
509 
510 	spin_lock(&sl->lock);
511 	if (!netif_running(dev))  {
512 		spin_unlock(&sl->lock);
513 		printk("%s: xmit call when iface is down\n", dev->name);
514 		dev_kfree_skb(skb);
515 		return 0;
516 	}
517 	if (sl->tty == NULL) {
518 		spin_unlock(&sl->lock);
519 		dev_kfree_skb(skb);
520 		return 0;
521 	}
522 
523 	sl_lock(sl);
524 	sl->tx_bytes+=skb->len;
525 	sl_encaps(sl, skb->data, skb->len);
526 	spin_unlock(&sl->lock);
527 
528 	dev_kfree_skb(skb);
529 	return 0;
530 }
531 
532 
533 /******************************************
534  *   Routines looking at netdevice side.
535  ******************************************/
536 
537 /* Netdevice UP -> DOWN routine */
538 
539 static int
540 sl_close(struct net_device *dev)
541 {
542 	struct slip *sl = (struct slip*)(dev->priv);
543 
544 	spin_lock_bh(&sl->lock);
545 	if (sl->tty) {
546 		/* TTY discipline is running. */
547 		sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
548 	}
549 	netif_stop_queue(dev);
550 	sl->rcount   = 0;
551 	sl->xleft    = 0;
552 	spin_unlock_bh(&sl->lock);
553 
554 	return 0;
555 }
556 
557 /* Netdevice DOWN -> UP routine */
558 
559 static int sl_open(struct net_device *dev)
560 {
561 	struct slip *sl = (struct slip*)(dev->priv);
562 
563 	if (sl->tty==NULL)
564 		return -ENODEV;
565 
566 	sl->flags &= (1 << SLF_INUSE);
567 	netif_start_queue(dev);
568 	return 0;
569 }
570 
571 /* Netdevice change MTU request */
572 
573 static int sl_change_mtu(struct net_device *dev, int new_mtu)
574 {
575 	struct slip *sl = (struct slip*)(dev->priv);
576 
577 	if (new_mtu < 68 || new_mtu > 65534)
578 		return -EINVAL;
579 
580 	if (new_mtu != dev->mtu)
581 		return sl_realloc_bufs(sl, new_mtu);
582 	return 0;
583 }
584 
585 /* Netdevice get statistics request */
586 
587 static struct net_device_stats *
588 sl_get_stats(struct net_device *dev)
589 {
590 	static struct net_device_stats stats;
591 	struct slip *sl = (struct slip*)(dev->priv);
592 #ifdef SL_INCLUDE_CSLIP
593 	struct slcompress *comp;
594 #endif
595 
596 	memset(&stats, 0, sizeof(struct net_device_stats));
597 
598 	stats.rx_packets     = sl->rx_packets;
599 	stats.tx_packets     = sl->tx_packets;
600 	stats.rx_bytes	     = sl->rx_bytes;
601 	stats.tx_bytes	     = sl->tx_bytes;
602 	stats.rx_dropped     = sl->rx_dropped;
603 	stats.tx_dropped     = sl->tx_dropped;
604 	stats.tx_errors      = sl->tx_errors;
605 	stats.rx_errors      = sl->rx_errors;
606 	stats.rx_over_errors = sl->rx_over_errors;
607 #ifdef SL_INCLUDE_CSLIP
608 	stats.rx_fifo_errors = sl->rx_compressed;
609 	stats.tx_fifo_errors = sl->tx_compressed;
610 	stats.collisions     = sl->tx_misses;
611 	comp = sl->slcomp;
612 	if (comp) {
613 		stats.rx_fifo_errors += comp->sls_i_compressed;
614 		stats.rx_dropped     += comp->sls_i_tossed;
615 		stats.tx_fifo_errors += comp->sls_o_compressed;
616 		stats.collisions     += comp->sls_o_misses;
617 	}
618 #endif /* CONFIG_INET */
619 	return (&stats);
620 }
621 
622 /* Netdevice register callback */
623 
624 static int sl_init(struct net_device *dev)
625 {
626 	struct slip *sl = (struct slip*)(dev->priv);
627 
628 	/*
629 	 *	Finish setting up the DEVICE info.
630 	 */
631 
632 	dev->mtu		= sl->mtu;
633 	dev->hard_start_xmit	= sl_xmit;
634 #ifdef SL_CHECK_TRANSMIT
635 	dev->tx_timeout		= sl_tx_timeout;
636 	dev->watchdog_timeo	= 20*HZ;
637 #endif
638 	dev->open		= sl_open;
639 	dev->stop		= sl_close;
640 	dev->get_stats	        = sl_get_stats;
641 	dev->change_mtu		= sl_change_mtu;
642 #ifdef CONFIG_SLIP_SMART
643 	dev->do_ioctl		= sl_ioctl;
644 #endif
645 	dev->hard_header_len	= 0;
646 	dev->addr_len		= 0;
647 	dev->type		= ARPHRD_SLIP + sl->mode;
648 	dev->tx_queue_len	= 10;
649 
650 	SET_MODULE_OWNER(dev);
651 
652 	/* New-style flags. */
653 	dev->flags		= IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
654 
655 	return 0;
656 }
657 
658 
659 /******************************************
660   Routines looking at TTY side.
661  ******************************************/
662 
663 
664 static int slip_receive_room(struct tty_struct *tty)
665 {
666 	return 65536;  /* We can handle an infinite amount of data. :-) */
667 }
668 
669 /*
670  * Handle the 'receiver data ready' interrupt.
671  * This function is called by the 'tty_io' module in the kernel when
672  * a block of SLIP data has been received, which can now be decapsulated
673  * and sent on to some IP layer for further processing. This will not
674  * be re-entered while running but other ldisc functions may be called
675  * in parallel
676  */
677 
678 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
679 {
680 	struct slip *sl = (struct slip *) tty->disc_data;
681 
682 	if (!sl || sl->magic != SLIP_MAGIC ||
683 	    !netif_running(sl->dev))
684 		return;
685 
686 	/* Read the characters out of the buffer */
687 	while (count--) {
688 		if (fp && *fp++) {
689 			if (!test_and_set_bit(SLF_ERROR, &sl->flags))  {
690 				sl->rx_errors++;
691 			}
692 			cp++;
693 			continue;
694 		}
695 #ifdef CONFIG_SLIP_MODE_SLIP6
696 		if (sl->mode & SL_MODE_SLIP6)
697 			slip_unesc6(sl, *cp++);
698 		else
699 #endif
700 			slip_unesc(sl, *cp++);
701 	}
702 }
703 
704 /************************************
705  *  slip_open helper routines.
706  ************************************/
707 
708 /* Collect hanged up channels */
709 
710 static void sl_sync(void)
711 {
712 	int i;
713 
714 	for (i = 0; i < slip_maxdev; i++) {
715 		slip_ctrl_t *slp = slip_ctrls[i];
716 		if (slp == NULL)
717 			break;
718 		if (slp->ctrl.tty || slp->ctrl.leased)
719 			continue;
720 		if (slp->dev.flags&IFF_UP)
721 			dev_close(&slp->dev);
722 	}
723 }
724 
725 /* Find a free SLIP channel, and link in this `tty' line. */
726 static struct slip *
727 sl_alloc(kdev_t line)
728 {
729 	struct slip *sl;
730 	slip_ctrl_t *slp = NULL;
731 	int i;
732 	int sel = -1;
733 	int score = -1;
734 
735 	if (slip_ctrls == NULL)
736 		return NULL;	/* Master array missing ! */
737 
738 	for (i = 0; i < slip_maxdev; i++) {
739 		slp = slip_ctrls[i];
740 		if (slp == NULL)
741 			break;
742 
743 		if (slp->ctrl.leased) {
744 			if (slp->ctrl.line != line)
745 				continue;
746 			if (slp->ctrl.tty)
747 				return NULL;
748 
749 			/* Clear ESCAPE & ERROR flags */
750 			slp->ctrl.flags &= (1 << SLF_INUSE);
751 			return &slp->ctrl;
752 		}
753 
754 		if (slp->ctrl.tty)
755 			continue;
756 
757 		if (current->pid == slp->ctrl.pid) {
758 			if (slp->ctrl.line == line && score < 3) {
759 				sel = i;
760 				score = 3;
761 				continue;
762 			}
763 			if (score < 2) {
764 				sel = i;
765 				score = 2;
766 			}
767 			continue;
768 		}
769 		if (slp->ctrl.line == line && score < 1) {
770 			sel = i;
771 			score = 1;
772 			continue;
773 		}
774 		if (score < 0) {
775 			sel = i;
776 			score = 0;
777 		}
778 	}
779 
780 	if (sel >= 0) {
781 		i = sel;
782 		slp = slip_ctrls[i];
783 		if (score > 1) {
784 			slp->ctrl.flags &= (1 << SLF_INUSE);
785 			return &slp->ctrl;
786 		}
787 	}
788 
789 	/* Sorry, too many, all slots in use */
790 	if (i >= slip_maxdev)
791 		return NULL;
792 
793 	if (slp) {
794 		if (test_bit(SLF_INUSE, &slp->ctrl.flags)) {
795 			unregister_netdevice(&slp->dev);
796 			sl_free_bufs(&slp->ctrl);
797 		}
798 	} else if ((slp = (slip_ctrl_t *)kmalloc(sizeof(slip_ctrl_t),GFP_KERNEL)) == NULL)
799 		return NULL;
800 
801 	memset(slp, 0, sizeof(slip_ctrl_t));
802 
803 	sl = &slp->ctrl;
804 	/* Initialize channel control data */
805 	sl->magic       = SLIP_MAGIC;
806 	sl->dev	      	= &slp->dev;
807 	spin_lock_init(&sl->lock);
808 	sl->mode        = SL_MODE_DEFAULT;
809 	sprintf(slp->dev.name, "sl%d", i);
810 	slp->dev.base_addr    = i;
811 	slp->dev.priv         = (void*)sl;
812 	slp->dev.init         = sl_init;
813 #ifdef CONFIG_SLIP_SMART
814 	init_timer(&sl->keepalive_timer);	/* initialize timer_list struct */
815 	sl->keepalive_timer.data=(unsigned long)sl;
816 	sl->keepalive_timer.function=sl_keepalive;
817 	init_timer(&sl->outfill_timer);
818 	sl->outfill_timer.data=(unsigned long)sl;
819 	sl->outfill_timer.function=sl_outfill;
820 #endif
821 	slip_ctrls[i] 	      = slp;
822 	return &slp->ctrl;
823 }
824 
825 /*
826  * Open the high-level part of the SLIP channel.
827  * This function is called by the TTY module when the
828  * SLIP line discipline is called for.  Because we are
829  * sure the tty line exists, we only have to link it to
830  * a free SLIP channel...
831  *
832  * Called in process context serialized from other ldisc calls.
833  */
834 
835 static int slip_open(struct tty_struct *tty)
836 {
837 	struct slip *sl;
838 	int err;
839 
840 	if(!capable(CAP_NET_ADMIN))
841 		return -EPERM;
842 
843 	MOD_INC_USE_COUNT;
844 
845 	/* RTnetlink lock is misused here to serialize concurrent
846 	   opens of slip channels. There are better ways, but it is
847 	   the simplest one.
848 	 */
849 	rtnl_lock();
850 
851 	/* Collect hanged up channels. */
852 	sl_sync();
853 
854 	sl = (struct slip *) tty->disc_data;
855 
856 	err = -EEXIST;
857 	/* First make sure we're not already connected. */
858 	if (sl && sl->magic == SLIP_MAGIC)
859 		goto err_exit;
860 
861 	/* OK.  Find a free SLIP channel to use. */
862 	err = -ENFILE;
863 	if ((sl = sl_alloc(tty->device)) == NULL)
864 		goto err_exit;
865 
866 	sl->tty = tty;
867 	tty->disc_data = sl;
868 	sl->line = tty->device;
869 	sl->pid = current->pid;
870 	if (tty->driver.flush_buffer)
871 		tty->driver.flush_buffer(tty);
872 
873 	if (!test_bit(SLF_INUSE, &sl->flags)) {
874 		/* Perform the low-level SLIP initialization. */
875 		if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
876 			goto err_free_chan;
877 
878 		if (register_netdevice(sl->dev)) {
879 			sl_free_bufs(sl);
880 			goto err_free_chan;
881 		}
882 
883 		set_bit(SLF_INUSE, &sl->flags);
884 	}
885 
886 #ifdef CONFIG_SLIP_SMART
887 	if (sl->keepalive) {
888 		sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
889 		add_timer (&sl->keepalive_timer);
890 	}
891 	if (sl->outfill) {
892 		sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
893 		add_timer (&sl->outfill_timer);
894 	}
895 #endif
896 
897 	/* Done.  We have linked the TTY line to a channel. */
898 	rtnl_unlock();
899 	return sl->dev->base_addr;
900 
901 err_free_chan:
902 	sl->tty = NULL;
903 	tty->disc_data = NULL;
904 	clear_bit(SLF_INUSE, &sl->flags);
905 
906 err_exit:
907 	rtnl_unlock();
908 
909 	/* Count references from TTY module */
910 	MOD_DEC_USE_COUNT;
911 	return err;
912 }
913 
914 /*
915 
916   FIXME: 1,2 are fixed 3 was never true anyway.
917 
918    Let me to blame a bit.
919    1. TTY module calls this funstion on soft interrupt.
920    2. TTY module calls this function WITH MASKED INTERRUPTS!
921    3. TTY module does not notify us about line discipline
922       shutdown,
923 
924    Seems, now it is clean. The solution is to consider netdevice and
925    line discipline sides as two independent threads.
926 
927    By-product (not desired): sl? does not feel hangups and remains open.
928    It is supposed, that user level program (dip, diald, slattach...)
929    will catch SIGHUP and make the rest of work.
930 
931    I see no way to make more with current tty code. --ANK
932  */
933 
934 /*
935  * Close down a SLIP channel.
936  * This means flushing out any pending queues, and then returning. This
937  * call is serialized against other ldisc functions.
938  */
939 static void
940 slip_close(struct tty_struct *tty)
941 {
942 	struct slip *sl = (struct slip *) tty->disc_data;
943 
944 	/* First make sure we're connected. */
945 	if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
946 		return;
947 
948 	tty->disc_data = 0;
949 	sl->tty = NULL;
950 	if (!sl->leased)
951 		sl->line = 0;
952 
953 	/* VSV = very important to remove timers */
954 #ifdef CONFIG_SLIP_SMART
955 	del_timer_sync(&sl->keepalive_timer);
956 	del_timer_sync(&sl->outfill_timer);
957 #endif
958 
959 	/* Count references from TTY module */
960 	MOD_DEC_USE_COUNT;
961 }
962 
963  /************************************************************************
964   *			STANDARD SLIP ENCAPSULATION		  	 *
965   ************************************************************************/
966 
967 int
968 slip_esc(unsigned char *s, unsigned char *d, int len)
969 {
970 	unsigned char *ptr = d;
971 	unsigned char c;
972 
973 	/*
974 	 * Send an initial END character to flush out any
975 	 * data that may have accumulated in the receiver
976 	 * due to line noise.
977 	 */
978 
979 	*ptr++ = END;
980 
981 	/*
982 	 * For each byte in the packet, send the appropriate
983 	 * character sequence, according to the SLIP protocol.
984 	 */
985 
986 	while (len-- > 0) {
987 		switch(c = *s++) {
988 		 case END:
989 			*ptr++ = ESC;
990 			*ptr++ = ESC_END;
991 			break;
992 		 case ESC:
993 			*ptr++ = ESC;
994 			*ptr++ = ESC_ESC;
995 			break;
996 		 default:
997 			*ptr++ = c;
998 			break;
999 		}
1000 	}
1001 	*ptr++ = END;
1002 	return (ptr - d);
1003 }
1004 
1005 static void slip_unesc(struct slip *sl, unsigned char s)
1006 {
1007 
1008 	switch(s) {
1009 	 case END:
1010 #ifdef CONFIG_SLIP_SMART
1011 		/* drop keeptest bit = VSV */
1012 		if (test_bit(SLF_KEEPTEST, &sl->flags))
1013 			clear_bit(SLF_KEEPTEST, &sl->flags);
1014 #endif
1015 
1016 		if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
1017 			sl_bump(sl);
1018 		}
1019 		clear_bit(SLF_ESCAPE, &sl->flags);
1020 		sl->rcount = 0;
1021 		return;
1022 
1023 	 case ESC:
1024 		set_bit(SLF_ESCAPE, &sl->flags);
1025 		return;
1026 	 case ESC_ESC:
1027 		if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))  {
1028 			s = ESC;
1029 		}
1030 		break;
1031 	 case ESC_END:
1032 		if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))  {
1033 			s = END;
1034 		}
1035 		break;
1036 	}
1037 	if (!test_bit(SLF_ERROR, &sl->flags))  {
1038 		if (sl->rcount < sl->buffsize)  {
1039 			sl->rbuff[sl->rcount++] = s;
1040 			return;
1041 		}
1042 		sl->rx_over_errors++;
1043 		set_bit(SLF_ERROR, &sl->flags);
1044 	}
1045 }
1046 
1047 
1048 #ifdef CONFIG_SLIP_MODE_SLIP6
1049 /************************************************************************
1050  *			 6 BIT SLIP ENCAPSULATION			*
1051  ************************************************************************/
1052 
1053 int
1054 slip_esc6(unsigned char *s, unsigned char *d, int len)
1055 {
1056 	unsigned char *ptr = d;
1057 	unsigned char c;
1058 	int i;
1059 	unsigned short v = 0;
1060 	short bits = 0;
1061 
1062 	/*
1063 	 * Send an initial END character to flush out any
1064 	 * data that may have accumulated in the receiver
1065 	 * due to line noise.
1066 	 */
1067 
1068 	*ptr++ = 0x70;
1069 
1070 	/*
1071 	 * Encode the packet into printable ascii characters
1072 	 */
1073 
1074 	for (i = 0; i < len; ++i) {
1075 		v = (v << 8) | s[i];
1076 		bits += 8;
1077 		while (bits >= 6) {
1078 			bits -= 6;
1079 			c = 0x30 + ((v >> bits) & 0x3F);
1080 			*ptr++ = c;
1081 		}
1082 	}
1083 	if (bits) {
1084 		c = 0x30 + ((v << (6 - bits)) & 0x3F);
1085 		*ptr++ = c;
1086 	}
1087 	*ptr++ = 0x70;
1088 	return ptr - d;
1089 }
1090 
1091 void
1092 slip_unesc6(struct slip *sl, unsigned char s)
1093 {
1094 	unsigned char c;
1095 
1096 	if (s == 0x70) {
1097 #ifdef CONFIG_SLIP_SMART
1098 		/* drop keeptest bit = VSV */
1099 		if (test_bit(SLF_KEEPTEST, &sl->flags))
1100 			clear_bit(SLF_KEEPTEST, &sl->flags);
1101 #endif
1102 
1103 		if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
1104 			sl_bump(sl);
1105 		}
1106 		sl->rcount = 0;
1107 		sl->xbits = 0;
1108 		sl->xdata = 0;
1109  	} else if (s >= 0x30 && s < 0x70) {
1110 		sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1111 		sl->xbits += 6;
1112 		if (sl->xbits >= 8) {
1113 			sl->xbits -= 8;
1114 			c = (unsigned char)(sl->xdata >> sl->xbits);
1115 			if (!test_bit(SLF_ERROR, &sl->flags))  {
1116 				if (sl->rcount < sl->buffsize)  {
1117 					sl->rbuff[sl->rcount++] = c;
1118 					return;
1119 				}
1120 				sl->rx_over_errors++;
1121 				set_bit(SLF_ERROR, &sl->flags);
1122 			}
1123 		}
1124  	}
1125 }
1126 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1127 
1128 /* Perform I/O control on an active SLIP channel. */
1129 static int
1130 slip_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
1131 {
1132 	struct slip *sl = (struct slip *) tty->disc_data;
1133 	unsigned int tmp;
1134 
1135 	/* First make sure we're connected. */
1136 	if (!sl || sl->magic != SLIP_MAGIC) {
1137 		return -EINVAL;
1138 	}
1139 
1140 	switch(cmd) {
1141 	 case SIOCGIFNAME:
1142 		/* Please, do not put this line under copy_to_user,
1143 		   it breaks my old poor gcc on alpha --ANK
1144 		 */
1145 		tmp = strlen(sl->dev->name) + 1;
1146 		if (copy_to_user(arg, sl->dev->name, tmp))
1147 			return -EFAULT;
1148 		return 0;
1149 
1150 	case SIOCGIFENCAP:
1151 		if (put_user(sl->mode, (int *)arg))
1152 			return -EFAULT;
1153 		return 0;
1154 
1155 	case SIOCSIFENCAP:
1156 		if (get_user(tmp,(int *)arg))
1157 			return -EFAULT;
1158 #ifndef SL_INCLUDE_CSLIP
1159 		if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))  {
1160 			return -EINVAL;
1161 		}
1162 #else
1163 		if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1164 		    (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))  {
1165 			/* return -EINVAL; */
1166 			tmp &= ~SL_MODE_ADAPTIVE;
1167 		}
1168 #endif
1169 #ifndef CONFIG_SLIP_MODE_SLIP6
1170 		if (tmp & SL_MODE_SLIP6)  {
1171 			return -EINVAL;
1172 		}
1173 #endif
1174 		sl->mode = tmp;
1175 		sl->dev->type = ARPHRD_SLIP+sl->mode;
1176 		return 0;
1177 
1178 	 case SIOCSIFHWADDR:
1179 		return -EINVAL;
1180 
1181 #ifdef CONFIG_SLIP_SMART
1182 	/* VSV changes start here */
1183         case SIOCSKEEPALIVE:
1184 		if (get_user(tmp,(int *)arg))
1185 			return -EFAULT;
1186                 if (tmp > 255) /* max for unchar */
1187 			return -EINVAL;
1188 
1189 		spin_lock_bh(&sl->lock);
1190 		if (!sl->tty) {
1191 			spin_unlock_bh(&sl->lock);
1192 			return -ENODEV;
1193 		}
1194 		if ((sl->keepalive = (unchar) tmp) != 0) {
1195 			mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1196 			set_bit(SLF_KEEPTEST, &sl->flags);
1197                 } else {
1198                         del_timer (&sl->keepalive_timer);
1199 		}
1200 		spin_unlock_bh(&sl->lock);
1201 		return 0;
1202 
1203         case SIOCGKEEPALIVE:
1204 		if (put_user(sl->keepalive, (int *)arg))
1205 			return -EFAULT;
1206 		return 0;
1207 
1208         case SIOCSOUTFILL:
1209 		if (get_user(tmp,(int *)arg))
1210 			return -EFAULT;
1211                 if (tmp > 255) /* max for unchar */
1212 			return -EINVAL;
1213 		spin_lock_bh(&sl->lock);
1214 		if (!sl->tty) {
1215 			spin_unlock_bh(&sl->lock);
1216 			return -ENODEV;
1217 		}
1218                 if ((sl->outfill = (unchar) tmp) != 0){
1219 			mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1220 			set_bit(SLF_OUTWAIT, &sl->flags);
1221 		} else {
1222                         del_timer (&sl->outfill_timer);
1223 		}
1224 		spin_unlock_bh(&sl->lock);
1225                 return 0;
1226 
1227         case SIOCGOUTFILL:
1228 		if (put_user(sl->outfill, (int *)arg))
1229 			return -EFAULT;
1230 		return 0;
1231 	/* VSV changes end */
1232 #endif
1233 
1234 	/* Allow stty to read, but not set, the serial port */
1235 	case TCGETS:
1236 	case TCGETA:
1237 		return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1238 
1239 	default:
1240 		return -ENOIOCTLCMD;
1241 	}
1242 }
1243 
1244 /* VSV changes start here */
1245 #ifdef CONFIG_SLIP_SMART
1246 /* function do_ioctl called from net/core/dev.c
1247    to allow get/set outfill/keepalive parameter
1248    by ifconfig                                 */
1249 
1250 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
1251 {
1252 	struct slip *sl = (struct slip*)(dev->priv);
1253 
1254 	if (sl == NULL)		/* Allocation failed ?? */
1255 		return -ENODEV;
1256 
1257 	spin_lock_bh(&sl->lock);
1258 
1259 	if (!sl->tty) {
1260 		spin_unlock_bh(&sl->lock);
1261 		return -ENODEV;
1262 	}
1263 
1264 	switch(cmd){
1265         case SIOCSKEEPALIVE:
1266 		/* max for unchar */
1267                 if (((unsigned int)((unsigned long)rq->ifr_data)) > 255) {
1268 			spin_unlock_bh(&sl->lock);
1269 			return -EINVAL;
1270 		}
1271 		sl->keepalive = (unchar) ((unsigned long)rq->ifr_data);
1272 		if (sl->keepalive != 0) {
1273 			sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1274 			mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1275 			set_bit(SLF_KEEPTEST, &sl->flags);
1276                 } else {
1277                         del_timer(&sl->keepalive_timer);
1278 		}
1279 		break;
1280 
1281         case SIOCGKEEPALIVE:
1282 		rq->ifr_data=(caddr_t)((unsigned long)sl->keepalive);
1283 		break;
1284 
1285         case SIOCSOUTFILL:
1286                 if (((unsigned)((unsigned long)rq->ifr_data)) > 255) { /* max for unchar */
1287 			spin_unlock_bh(&sl->lock);
1288 			return -EINVAL;
1289 		}
1290                 if ((sl->outfill = (unchar)((unsigned long) rq->ifr_data)) != 0){
1291 			mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1292 			set_bit(SLF_OUTWAIT, &sl->flags);
1293 		} else {
1294                         del_timer (&sl->outfill_timer);
1295 		}
1296                 break;
1297 
1298         case SIOCGOUTFILL:
1299 		rq->ifr_data=(caddr_t)((unsigned long)sl->outfill);
1300 		break;
1301 
1302         case SIOCSLEASE:
1303 		/* Resolve race condition, when ioctl'ing hanged up
1304 		   and opened by another process device.
1305 		 */
1306 		if (sl->tty != current->tty && sl->pid != current->pid) {
1307 			spin_unlock_bh(&sl->lock);
1308 			return -EPERM;
1309 		}
1310 		sl->leased = 0;
1311                 if ((unsigned long)rq->ifr_data)
1312 			sl->leased = 1;
1313                 break;
1314 
1315         case SIOCGLEASE:
1316 		rq->ifr_data=(caddr_t)((unsigned long)sl->leased);
1317 	};
1318 	spin_unlock_bh(&sl->lock);
1319 	return 0;
1320 }
1321 #endif
1322 /* VSV changes end */
1323 
1324 /* Initialize SLIP control device -- register SLIP line discipline */
1325 
1326 int __init slip_init_ctrl_dev(void)
1327 {
1328 	int status;
1329 
1330 	if (slip_maxdev < 4) slip_maxdev = 4; /* Sanity */
1331 
1332 	printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1333 #ifdef CONFIG_SLIP_MODE_SLIP6
1334 	       " (6 bit encapsulation enabled)"
1335 #endif
1336 	       ".\n",
1337 	       SLIP_VERSION, slip_maxdev );
1338 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1339 	printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1340 #endif
1341 #ifdef CONFIG_SLIP_SMART
1342 	printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1343 #endif
1344 
1345 	slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1346 	if (slip_ctrls == NULL)
1347 	{
1348 		printk("SLIP: Can't allocate slip_ctrls[] array!  Uaargh! (-> No SLIP available)\n");
1349 		return -ENOMEM;
1350 	}
1351 
1352 	/* Clear the pointer array, we allocate devices when we need them */
1353 	memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev); /* Pointers */
1354 
1355 	/* Fill in our line protocol discipline, and register it */
1356 	memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1357 	sl_ldisc.magic  = TTY_LDISC_MAGIC;
1358 	sl_ldisc.name   = "slip";
1359 	sl_ldisc.flags  = 0;
1360 	sl_ldisc.open   = slip_open;
1361 	sl_ldisc.close  = slip_close;
1362 	sl_ldisc.read   = NULL;
1363 	sl_ldisc.write  = NULL;
1364 	sl_ldisc.ioctl  = (int (*)(struct tty_struct *, struct file *,
1365 				   unsigned int, unsigned long)) slip_ioctl;
1366 	sl_ldisc.poll   = NULL;
1367 	sl_ldisc.receive_buf = slip_receive_buf;
1368 	sl_ldisc.receive_room = slip_receive_room;
1369 	sl_ldisc.write_wakeup = slip_write_wakeup;
1370 	if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0)  {
1371 		printk("SLIP: can't register line discipline (err = %d)\n", status);
1372 	}
1373 
1374 
1375 	return status;
1376 }
1377 
1378 
1379 
1380 #ifdef MODULE
1381 
1382 int
1383 init_module(void)
1384 {
1385 	return slip_init_ctrl_dev();
1386 }
1387 
1388 void
1389 cleanup_module(void)
1390 {
1391 	int i;
1392 
1393 	if (slip_ctrls != NULL) {
1394 		unsigned long timeout = jiffies + HZ;
1395 		int busy = 0;
1396 
1397 		/* First of all: check for active disciplines and hangup them.
1398 		 */
1399 		do {
1400 			if (busy)
1401 				yield();
1402 
1403 			busy = 0;
1404 			local_bh_disable();
1405 			for (i = 0; i < slip_maxdev; i++) {
1406 				struct slip_ctrl *slc = slip_ctrls[i];
1407 				if (!slc)
1408 					continue;
1409 				spin_lock(&slc->ctrl.lock);
1410 				if (slc->ctrl.tty) {
1411 					busy++;
1412 					tty_hangup(slc->ctrl.tty);
1413 				}
1414 				spin_unlock(&slc->ctrl.lock);
1415 			}
1416 			local_bh_enable();
1417 		} while (busy && time_before(jiffies, timeout));
1418 
1419 		busy = 0;
1420 		for (i = 0; i < slip_maxdev; i++) {
1421 			struct slip_ctrl *slc = slip_ctrls[i];
1422 			if (slc) {
1423 				unregister_netdev(&slc->dev);
1424 				if (slc->ctrl.tty) {
1425 					printk("%s: tty discipline is still running\n", slc->dev.name);
1426 					/* Pin module forever */
1427 					MOD_INC_USE_COUNT;
1428 					busy++;
1429 					continue;
1430 				}
1431 				sl_free_bufs(&slc->ctrl);
1432 				kfree(slc);
1433 				slip_ctrls[i] = NULL;
1434 			}
1435 		}
1436 		if (!busy) {
1437 			kfree(slip_ctrls);
1438 			slip_ctrls = NULL;
1439 		}
1440 	}
1441 	if ((i = tty_register_ldisc(N_SLIP, NULL)))
1442 	{
1443 		printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1444 	}
1445 }
1446 #endif /* MODULE */
1447 
1448 #ifdef CONFIG_SLIP_SMART
1449 /*
1450  * This is start of the code for multislip style line checking
1451  * added by Stanislav Voronyi. All changes before marked VSV
1452  */
1453 
1454 static void sl_outfill(unsigned long sls)
1455 {
1456 	struct slip *sl=(struct slip *)sls;
1457 
1458 	spin_lock(&sl->lock);
1459 
1460 	if (sl->tty == NULL)
1461 		goto out;
1462 
1463 	if(sl->outfill)
1464 	{
1465 		if( test_bit(SLF_OUTWAIT, &sl->flags) )
1466 		{
1467 			/* no packets were transmitted, do outfill */
1468 #ifdef CONFIG_SLIP_MODE_SLIP6
1469 			unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1470 #else
1471 			unsigned char s = END;
1472 #endif
1473 			/* put END into tty queue. Is it right ??? */
1474 			if (!netif_queue_stopped(sl->dev))
1475 			{
1476 				/* if device busy no outfill */
1477 				sl->tty->driver.write(sl->tty, 0, &s, 1);
1478 			}
1479 		}
1480 		else
1481 			set_bit(SLF_OUTWAIT, &sl->flags);
1482 
1483 		mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1484 	}
1485 out:
1486 	spin_unlock(&sl->lock);
1487 }
1488 
1489 static void sl_keepalive(unsigned long sls)
1490 {
1491 	struct slip *sl=(struct slip *)sls;
1492 
1493 	spin_lock(&sl->lock);
1494 
1495 	if (sl->tty == NULL)
1496 		goto out;
1497 
1498 	if( sl->keepalive)
1499 	{
1500 		if(test_bit(SLF_KEEPTEST, &sl->flags))
1501 		{
1502 			/* keepalive still high :(, we must hangup */
1503 			if( sl->outfill ) /* outfill timer must be deleted too */
1504 				(void)del_timer(&sl->outfill_timer);
1505 			printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1506 			tty_hangup(sl->tty); /* this must hangup tty & close slip */
1507 			/* I think we need not something else */
1508 			goto out;
1509 		}
1510 		else
1511 			set_bit(SLF_KEEPTEST, &sl->flags);
1512 
1513 		mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1514 	}
1515 
1516 out:
1517 	spin_unlock(&sl->lock);
1518 }
1519 
1520 #endif
1521 MODULE_LICENSE("GPL");
1522