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.
6 * Version: @(#)slip.c 0.8.3 12/24/94
8 * Authors: Laurence Culhane, <loz@holmes.demon.co.uk>
9 * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
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.
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
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.
57 #define SL_CHECK_TRANSMIT
58 #include <linux/config.h>
59 #include <linux/module.h>
60 #include <linux/moduleparam.h>
62 #include <asm/system.h>
63 #include <asm/uaccess.h>
64 #include <linux/bitops.h>
65 #include <linux/string.h>
67 #include <linux/interrupt.h>
69 #include <linux/tty.h>
70 #include <linux/errno.h>
71 #include <linux/netdevice.h>
72 #include <linux/etherdevice.h>
73 #include <linux/skbuff.h>
74 #include <linux/rtnetlink.h>
75 #include <linux/if_arp.h>
76 #include <linux/if_slip.h>
77 #include <linux/delay.h>
78 #include <linux/init.h>
82 #include <linux/tcp.h>
83 #include <net/slhc_vj.h>
86 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
88 static struct net_device **slip_devs;
90 static int slip_maxdev = SL_NRUNIT;
91 module_param(slip_maxdev, int, 0);
92 MODULE_PARM_DESC(slip_maxdev, "Maximum number of slip devices");
94 static int slip_esc(unsigned char *p, unsigned char *d, int len);
95 static void slip_unesc(struct slip *sl, unsigned char c);
96 #ifdef CONFIG_SLIP_MODE_SLIP6
97 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
98 static void slip_unesc6(struct slip *sl, unsigned char c);
100 #ifdef CONFIG_SLIP_SMART
101 static void sl_keepalive(unsigned long sls);
102 static void sl_outfill(unsigned long sls);
103 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd);
106 /********************************
107 * Buffer administration routines:
112 * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
113 * sl_realloc_bufs provides strong atomicity and reallocation
114 * on actively running device.
115 *********************************/
118 Allocate channel buffers.
122 sl_alloc_bufs(struct slip *sl, int mtu)
128 #ifdef SL_INCLUDE_CSLIP
130 struct slcompress *slcomp = NULL;
134 * Allocate the SLIP frame buffers:
136 * rbuff Receive buffer.
137 * xbuff Transmit buffer.
138 * cbuff Temporary compression buffer.
143 * allow for arrival of larger UDP packets, even if we say not to
144 * also fixes a bug in which SunOS sends 512-byte packets even with
149 rbuff = kmalloc(len + 4, GFP_KERNEL);
152 xbuff = kmalloc(len + 4, GFP_KERNEL);
155 #ifdef SL_INCLUDE_CSLIP
156 cbuff = kmalloc(len + 4, GFP_KERNEL);
159 slcomp = slhc_init(16, 16);
163 spin_lock_bh(&sl->lock);
164 if (sl->tty == NULL) {
165 spin_unlock_bh(&sl->lock);
173 rbuff = xchg(&sl->rbuff, rbuff);
174 xbuff = xchg(&sl->xbuff, xbuff);
175 #ifdef SL_INCLUDE_CSLIP
176 cbuff = xchg(&sl->cbuff, cbuff);
177 slcomp = xchg(&sl->slcomp, slcomp);
178 #ifdef CONFIG_SLIP_MODE_SLIP6
183 spin_unlock_bh(&sl->lock);
188 #ifdef SL_INCLUDE_CSLIP
198 /* Free a SLIP channel buffers. */
200 sl_free_bufs(struct slip *sl)
202 /* Free all SLIP frame buffers. */
203 kfree(xchg(&sl->rbuff, NULL));
204 kfree(xchg(&sl->xbuff, NULL));
205 #ifdef SL_INCLUDE_CSLIP
206 kfree(xchg(&sl->cbuff, NULL));
207 slhc_free(xchg(&sl->slcomp, NULL));
212 Reallocate slip channel buffers.
215 static int sl_realloc_bufs(struct slip *sl, int mtu)
218 struct net_device *dev = sl->dev;
219 unsigned char *xbuff, *rbuff;
220 #ifdef SL_INCLUDE_CSLIP
221 unsigned char *cbuff;
226 * allow for arrival of larger UDP packets, even if we say not to
227 * also fixes a bug in which SunOS sends 512-byte packets even with
233 xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
234 rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
235 #ifdef SL_INCLUDE_CSLIP
236 cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
240 #ifdef SL_INCLUDE_CSLIP
241 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
243 if (xbuff == NULL || rbuff == NULL) {
245 if (mtu >= sl->mtu) {
246 printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n",
253 spin_lock_bh(&sl->lock);
259 xbuff = xchg(&sl->xbuff, xbuff);
260 rbuff = xchg(&sl->rbuff, rbuff);
261 #ifdef SL_INCLUDE_CSLIP
262 cbuff = xchg(&sl->cbuff, cbuff);
265 if (sl->xleft <= len) {
266 memcpy(sl->xbuff, sl->xhead, sl->xleft);
272 sl->xhead = sl->xbuff;
275 if (sl->rcount <= len) {
276 memcpy(sl->rbuff, rbuff, sl->rcount);
279 sl->rx_over_errors++;
280 set_bit(SLF_ERROR, &sl->flags);
289 spin_unlock_bh(&sl->lock);
294 #ifdef SL_INCLUDE_CSLIP
301 /* Set the "sending" flag. This must be atomic hence the set_bit. */
303 sl_lock(struct slip *sl)
305 netif_stop_queue(sl->dev);
309 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
311 sl_unlock(struct slip *sl)
313 netif_wake_queue(sl->dev);
316 /* Send one completely decapsulated IP datagram to the IP layer. */
318 sl_bump(struct slip *sl)
324 #ifdef SL_INCLUDE_CSLIP
325 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
327 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
328 /* ignore compressed packets when CSLIP is off */
329 if (!(sl->mode & SL_MODE_CSLIP)) {
330 printk(KERN_WARNING "%s: compressed packet ignored\n", sl->dev->name);
333 /* make sure we've reserved enough space for uncompress to use */
334 if (count + 80 > sl->buffsize) {
335 sl->rx_over_errors++;
338 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
342 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
343 if (!(sl->mode & SL_MODE_CSLIP)) {
344 /* turn on header compression */
345 sl->mode |= SL_MODE_CSLIP;
346 sl->mode &= ~SL_MODE_ADAPTIVE;
347 printk(KERN_INFO "%s: header compression turned on\n", sl->dev->name);
349 sl->rbuff[0] &= 0x4f;
350 if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
355 #endif /* SL_INCLUDE_CSLIP */
359 skb = dev_alloc_skb(count);
361 printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", sl->dev->name);
366 memcpy(skb_put(skb,count), sl->rbuff, count);
367 skb->mac.raw=skb->data;
368 skb->protocol=htons(ETH_P_IP);
370 sl->dev->last_rx = jiffies;
374 /* Encapsulate one IP datagram and stuff into a TTY queue. */
376 sl_encaps(struct slip *sl, unsigned char *icp, int len)
381 if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */
382 printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name);
389 #ifdef SL_INCLUDE_CSLIP
390 if (sl->mode & SL_MODE_CSLIP) {
391 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
394 #ifdef CONFIG_SLIP_MODE_SLIP6
395 if(sl->mode & SL_MODE_SLIP6)
396 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
399 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
401 /* Order of next two lines is *very* important.
402 * When we are sending a little amount of data,
403 * the transfer may be completed inside driver.write()
404 * routine, because it's running with interrupts enabled.
405 * In this case we *never* got WRITE_WAKEUP event,
406 * if we did not request it before write operation.
407 * 14 Oct 1994 Dmitry Gorodchanin.
409 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
410 actual = sl->tty->driver->write(sl->tty, sl->xbuff, count);
411 #ifdef SL_CHECK_TRANSMIT
412 sl->dev->trans_start = jiffies;
414 sl->xleft = count - actual;
415 sl->xhead = sl->xbuff + actual;
416 #ifdef CONFIG_SLIP_SMART
418 clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
423 * Called by the driver when there's room for more data. If we have
424 * more packets to send, we send them here.
426 static void slip_write_wakeup(struct tty_struct *tty)
429 struct slip *sl = (struct slip *) tty->disc_data;
431 /* First make sure we're connected. */
432 if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
435 if (sl->xleft <= 0) {
436 /* Now serial buffer is almost free & we can start
437 * transmission of another packet */
439 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
444 actual = tty->driver->write(tty, sl->xhead, sl->xleft);
449 static void sl_tx_timeout(struct net_device *dev)
451 struct slip *sl = netdev_priv(dev);
453 spin_lock(&sl->lock);
455 if (netif_queue_stopped(dev)) {
456 if (!netif_running(dev))
459 /* May be we must check transmitter timeout here ?
460 * 14 Oct 1994 Dmitry Gorodchanin.
462 #ifdef SL_CHECK_TRANSMIT
463 if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
464 /* 20 sec timeout not reached */
467 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
468 (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft) ?
469 "bad line quality" : "driver error");
471 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
477 spin_unlock(&sl->lock);
481 /* Encapsulate an IP datagram and kick it into a TTY queue. */
483 sl_xmit(struct sk_buff *skb, struct net_device *dev)
485 struct slip *sl = netdev_priv(dev);
487 spin_lock(&sl->lock);
488 if (!netif_running(dev)) {
489 spin_unlock(&sl->lock);
490 printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
494 if (sl->tty == NULL) {
495 spin_unlock(&sl->lock);
501 sl->tx_bytes+=skb->len;
502 sl_encaps(sl, skb->data, skb->len);
503 spin_unlock(&sl->lock);
510 /******************************************
511 * Routines looking at netdevice side.
512 ******************************************/
514 /* Netdevice UP -> DOWN routine */
517 sl_close(struct net_device *dev)
519 struct slip *sl = netdev_priv(dev);
521 spin_lock_bh(&sl->lock);
523 /* TTY discipline is running. */
524 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
526 netif_stop_queue(dev);
529 spin_unlock_bh(&sl->lock);
534 /* Netdevice DOWN -> UP routine */
536 static int sl_open(struct net_device *dev)
538 struct slip *sl = netdev_priv(dev);
543 sl->flags &= (1 << SLF_INUSE);
544 netif_start_queue(dev);
548 /* Netdevice change MTU request */
550 static int sl_change_mtu(struct net_device *dev, int new_mtu)
552 struct slip *sl = netdev_priv(dev);
554 if (new_mtu < 68 || new_mtu > 65534)
557 if (new_mtu != dev->mtu)
558 return sl_realloc_bufs(sl, new_mtu);
562 /* Netdevice get statistics request */
564 static struct net_device_stats *
565 sl_get_stats(struct net_device *dev)
567 static struct net_device_stats stats;
568 struct slip *sl = netdev_priv(dev);
569 #ifdef SL_INCLUDE_CSLIP
570 struct slcompress *comp;
573 memset(&stats, 0, sizeof(struct net_device_stats));
575 stats.rx_packets = sl->rx_packets;
576 stats.tx_packets = sl->tx_packets;
577 stats.rx_bytes = sl->rx_bytes;
578 stats.tx_bytes = sl->tx_bytes;
579 stats.rx_dropped = sl->rx_dropped;
580 stats.tx_dropped = sl->tx_dropped;
581 stats.tx_errors = sl->tx_errors;
582 stats.rx_errors = sl->rx_errors;
583 stats.rx_over_errors = sl->rx_over_errors;
584 #ifdef SL_INCLUDE_CSLIP
585 stats.rx_fifo_errors = sl->rx_compressed;
586 stats.tx_fifo_errors = sl->tx_compressed;
587 stats.collisions = sl->tx_misses;
590 stats.rx_fifo_errors += comp->sls_i_compressed;
591 stats.rx_dropped += comp->sls_i_tossed;
592 stats.tx_fifo_errors += comp->sls_o_compressed;
593 stats.collisions += comp->sls_o_misses;
595 #endif /* CONFIG_INET */
599 /* Netdevice register callback */
601 static int sl_init(struct net_device *dev)
603 struct slip *sl = netdev_priv(dev);
606 * Finish setting up the DEVICE info.
610 dev->type = ARPHRD_SLIP + sl->mode;
611 #ifdef SL_CHECK_TRANSMIT
612 dev->tx_timeout = sl_tx_timeout;
613 dev->watchdog_timeo = 20*HZ;
619 static void sl_uninit(struct net_device *dev)
621 struct slip *sl = netdev_priv(dev);
626 static void sl_setup(struct net_device *dev)
629 dev->uninit = sl_uninit;
631 dev->destructor = free_netdev;
632 dev->stop = sl_close;
633 dev->get_stats = sl_get_stats;
634 dev->change_mtu = sl_change_mtu;
635 dev->hard_start_xmit = sl_xmit;
636 #ifdef CONFIG_SLIP_SMART
637 dev->do_ioctl = sl_ioctl;
639 dev->hard_header_len = 0;
641 dev->tx_queue_len = 10;
643 SET_MODULE_OWNER(dev);
645 /* New-style flags. */
646 dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
649 /******************************************
650 Routines looking at TTY side.
651 ******************************************/
655 * Handle the 'receiver data ready' interrupt.
656 * This function is called by the 'tty_io' module in the kernel when
657 * a block of SLIP data has been received, which can now be decapsulated
658 * and sent on to some IP layer for further processing. This will not
659 * be re-entered while running but other ldisc functions may be called
663 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
665 struct slip *sl = (struct slip *) tty->disc_data;
667 if (!sl || sl->magic != SLIP_MAGIC ||
668 !netif_running(sl->dev))
671 /* Read the characters out of the buffer */
674 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
680 #ifdef CONFIG_SLIP_MODE_SLIP6
681 if (sl->mode & SL_MODE_SLIP6)
682 slip_unesc6(sl, *cp++);
685 slip_unesc(sl, *cp++);
689 /************************************
690 * slip_open helper routines.
691 ************************************/
693 /* Collect hanged up channels */
695 static void sl_sync(void)
698 struct net_device *dev;
701 for (i = 0; i < slip_maxdev; i++) {
702 if ((dev = slip_devs[i]) == NULL)
705 sl = netdev_priv(dev);
706 if (sl->tty || sl->leased)
708 if (dev->flags&IFF_UP)
714 /* Find a free SLIP channel, and link in this `tty' line. */
721 struct net_device *dev = NULL;
724 if (slip_devs == NULL)
725 return NULL; /* Master array missing ! */
727 for (i = 0; i < slip_maxdev; i++) {
732 sl = netdev_priv(dev);
734 if (sl->line != line)
739 /* Clear ESCAPE & ERROR flags */
740 sl->flags &= (1 << SLF_INUSE);
747 if (current->pid == sl->pid) {
748 if (sl->line == line && score < 3) {
759 if (sl->line == line && score < 1) {
774 sl = netdev_priv(dev);
775 sl->flags &= (1 << SLF_INUSE);
780 /* Sorry, too many, all slots in use */
781 if (i >= slip_maxdev)
785 sl = netdev_priv(dev);
786 if (test_bit(SLF_INUSE, &sl->flags)) {
787 unregister_netdevice(dev);
795 sprintf(name, "sl%d", i);
797 dev = alloc_netdev(sizeof(*sl), name, sl_setup);
803 sl = netdev_priv(dev);
805 /* Initialize channel control data */
806 sl->magic = SLIP_MAGIC;
808 spin_lock_init(&sl->lock);
809 sl->mode = SL_MODE_DEFAULT;
810 #ifdef CONFIG_SLIP_SMART
811 init_timer(&sl->keepalive_timer); /* initialize timer_list struct */
812 sl->keepalive_timer.data=(unsigned long)sl;
813 sl->keepalive_timer.function=sl_keepalive;
814 init_timer(&sl->outfill_timer);
815 sl->outfill_timer.data=(unsigned long)sl;
816 sl->outfill_timer.function=sl_outfill;
824 * Open the high-level part of the SLIP channel.
825 * This function is called by the TTY module when the
826 * SLIP line discipline is called for. Because we are
827 * sure the tty line exists, we only have to link it to
828 * a free SLIP channel...
830 * Called in process context serialized from other ldisc calls.
833 static int slip_open(struct tty_struct *tty)
838 if(!capable(CAP_NET_ADMIN))
841 /* RTnetlink lock is misused here to serialize concurrent
842 opens of slip channels. There are better ways, but it is
847 /* Collect hanged up channels. */
850 sl = (struct slip *) tty->disc_data;
853 /* First make sure we're not already connected. */
854 if (sl && sl->magic == SLIP_MAGIC)
857 /* OK. Find a free SLIP channel to use. */
859 if ((sl = sl_alloc(tty_devnum(tty))) == NULL)
864 sl->line = tty_devnum(tty);
865 sl->pid = current->pid;
867 if (!test_bit(SLF_INUSE, &sl->flags)) {
868 /* Perform the low-level SLIP initialization. */
869 if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
872 set_bit(SLF_INUSE, &sl->flags);
874 if ((err = register_netdevice(sl->dev)))
878 #ifdef CONFIG_SLIP_SMART
880 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
881 add_timer (&sl->keepalive_timer);
884 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
885 add_timer (&sl->outfill_timer);
889 /* Done. We have linked the TTY line to a channel. */
891 tty->receive_room = 65536; /* We don't flow control */
892 return sl->dev->base_addr;
899 tty->disc_data = NULL;
900 clear_bit(SLF_INUSE, &sl->flags);
905 /* Count references from TTY module */
911 FIXME: 1,2 are fixed 3 was never true anyway.
913 Let me to blame a bit.
914 1. TTY module calls this funstion on soft interrupt.
915 2. TTY module calls this function WITH MASKED INTERRUPTS!
916 3. TTY module does not notify us about line discipline
919 Seems, now it is clean. The solution is to consider netdevice and
920 line discipline sides as two independent threads.
922 By-product (not desired): sl? does not feel hangups and remains open.
923 It is supposed, that user level program (dip, diald, slattach...)
924 will catch SIGHUP and make the rest of work.
926 I see no way to make more with current tty code. --ANK
930 * Close down a SLIP channel.
931 * This means flushing out any pending queues, and then returning. This
932 * call is serialized against other ldisc functions.
935 slip_close(struct tty_struct *tty)
937 struct slip *sl = (struct slip *) tty->disc_data;
939 /* First make sure we're connected. */
940 if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
943 tty->disc_data = NULL;
948 /* VSV = very important to remove timers */
949 #ifdef CONFIG_SLIP_SMART
950 del_timer_sync(&sl->keepalive_timer);
951 del_timer_sync(&sl->outfill_timer);
954 /* Count references from TTY module */
957 /************************************************************************
958 * STANDARD SLIP ENCAPSULATION *
959 ************************************************************************/
962 slip_esc(unsigned char *s, unsigned char *d, int len)
964 unsigned char *ptr = d;
968 * Send an initial END character to flush out any
969 * data that may have accumulated in the receiver
976 * For each byte in the packet, send the appropriate
977 * character sequence, according to the SLIP protocol.
999 static void slip_unesc(struct slip *sl, unsigned char s)
1004 #ifdef CONFIG_SLIP_SMART
1005 /* drop keeptest bit = VSV */
1006 if (test_bit(SLF_KEEPTEST, &sl->flags))
1007 clear_bit(SLF_KEEPTEST, &sl->flags);
1010 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1013 clear_bit(SLF_ESCAPE, &sl->flags);
1018 set_bit(SLF_ESCAPE, &sl->flags);
1021 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1026 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1031 if (!test_bit(SLF_ERROR, &sl->flags)) {
1032 if (sl->rcount < sl->buffsize) {
1033 sl->rbuff[sl->rcount++] = s;
1036 sl->rx_over_errors++;
1037 set_bit(SLF_ERROR, &sl->flags);
1042 #ifdef CONFIG_SLIP_MODE_SLIP6
1043 /************************************************************************
1044 * 6 BIT SLIP ENCAPSULATION *
1045 ************************************************************************/
1048 slip_esc6(unsigned char *s, unsigned char *d, int len)
1050 unsigned char *ptr = d;
1053 unsigned short v = 0;
1057 * Send an initial END character to flush out any
1058 * data that may have accumulated in the receiver
1059 * due to line noise.
1065 * Encode the packet into printable ascii characters
1068 for (i = 0; i < len; ++i) {
1069 v = (v << 8) | s[i];
1073 c = 0x30 + ((v >> bits) & 0x3F);
1078 c = 0x30 + ((v << (6 - bits)) & 0x3F);
1086 slip_unesc6(struct slip *sl, unsigned char s)
1091 #ifdef CONFIG_SLIP_SMART
1092 /* drop keeptest bit = VSV */
1093 if (test_bit(SLF_KEEPTEST, &sl->flags))
1094 clear_bit(SLF_KEEPTEST, &sl->flags);
1097 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1103 } else if (s >= 0x30 && s < 0x70) {
1104 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1106 if (sl->xbits >= 8) {
1108 c = (unsigned char)(sl->xdata >> sl->xbits);
1109 if (!test_bit(SLF_ERROR, &sl->flags)) {
1110 if (sl->rcount < sl->buffsize) {
1111 sl->rbuff[sl->rcount++] = c;
1114 sl->rx_over_errors++;
1115 set_bit(SLF_ERROR, &sl->flags);
1120 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1122 /* Perform I/O control on an active SLIP channel. */
1123 static int slip_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1125 struct slip *sl = (struct slip *) tty->disc_data;
1127 int __user *p = (int __user *)arg;
1129 /* First make sure we're connected. */
1130 if (!sl || sl->magic != SLIP_MAGIC) {
1136 tmp = strlen(sl->dev->name) + 1;
1137 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1142 if (put_user(sl->mode, p))
1147 if (get_user(tmp, p))
1149 #ifndef SL_INCLUDE_CSLIP
1150 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1154 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1155 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1156 /* return -EINVAL; */
1157 tmp &= ~SL_MODE_ADAPTIVE;
1160 #ifndef CONFIG_SLIP_MODE_SLIP6
1161 if (tmp & SL_MODE_SLIP6) {
1166 sl->dev->type = ARPHRD_SLIP+sl->mode;
1172 #ifdef CONFIG_SLIP_SMART
1173 /* VSV changes start here */
1174 case SIOCSKEEPALIVE:
1175 if (get_user(tmp, p))
1177 if (tmp > 255) /* max for unchar */
1180 spin_lock_bh(&sl->lock);
1182 spin_unlock_bh(&sl->lock);
1185 if ((sl->keepalive = (unchar) tmp) != 0) {
1186 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1187 set_bit(SLF_KEEPTEST, &sl->flags);
1189 del_timer (&sl->keepalive_timer);
1191 spin_unlock_bh(&sl->lock);
1194 case SIOCGKEEPALIVE:
1195 if (put_user(sl->keepalive, p))
1200 if (get_user(tmp, p))
1202 if (tmp > 255) /* max for unchar */
1204 spin_lock_bh(&sl->lock);
1206 spin_unlock_bh(&sl->lock);
1209 if ((sl->outfill = (unchar) tmp) != 0){
1210 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1211 set_bit(SLF_OUTWAIT, &sl->flags);
1213 del_timer (&sl->outfill_timer);
1215 spin_unlock_bh(&sl->lock);
1219 if (put_user(sl->outfill, p))
1222 /* VSV changes end */
1225 /* Allow stty to read, but not set, the serial port */
1228 return n_tty_ioctl(tty, file, cmd, arg);
1231 return -ENOIOCTLCMD;
1235 /* VSV changes start here */
1236 #ifdef CONFIG_SLIP_SMART
1237 /* function do_ioctl called from net/core/dev.c
1238 to allow get/set outfill/keepalive parameter
1241 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
1243 struct slip *sl = netdev_priv(dev);
1244 unsigned long *p = (unsigned long *)&rq->ifr_ifru;
1246 if (sl == NULL) /* Allocation failed ?? */
1249 spin_lock_bh(&sl->lock);
1252 spin_unlock_bh(&sl->lock);
1257 case SIOCSKEEPALIVE:
1258 /* max for unchar */
1259 if ((unsigned)*p > 255) {
1260 spin_unlock_bh(&sl->lock);
1263 sl->keepalive = (unchar) *p;
1264 if (sl->keepalive != 0) {
1265 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1266 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1267 set_bit(SLF_KEEPTEST, &sl->flags);
1269 del_timer(&sl->keepalive_timer);
1273 case SIOCGKEEPALIVE:
1278 if ((unsigned)*p > 255) { /* max for unchar */
1279 spin_unlock_bh(&sl->lock);
1282 if ((sl->outfill = (unchar)*p) != 0){
1283 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1284 set_bit(SLF_OUTWAIT, &sl->flags);
1286 del_timer (&sl->outfill_timer);
1295 /* Resolve race condition, when ioctl'ing hanged up
1296 and opened by another process device.
1298 if (sl->tty != current->signal->tty && sl->pid != current->pid) {
1299 spin_unlock_bh(&sl->lock);
1310 spin_unlock_bh(&sl->lock);
1314 /* VSV changes end */
1316 static struct tty_ldisc sl_ldisc = {
1317 .owner = THIS_MODULE,
1318 .magic = TTY_LDISC_MAGIC,
1321 .close = slip_close,
1322 .ioctl = slip_ioctl,
1323 .receive_buf = slip_receive_buf,
1324 .write_wakeup = slip_write_wakeup,
1327 static int __init slip_init(void)
1331 if (slip_maxdev < 4)
1332 slip_maxdev = 4; /* Sanity */
1334 printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1335 #ifdef CONFIG_SLIP_MODE_SLIP6
1336 " (6 bit encapsulation enabled)"
1339 SLIP_VERSION, slip_maxdev );
1340 #if defined(SL_INCLUDE_CSLIP)
1341 printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
1343 #ifdef CONFIG_SLIP_SMART
1344 printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1347 slip_devs = kmalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL);
1349 printk(KERN_ERR "SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1353 /* Clear the pointer array, we allocate devices when we need them */
1354 memset(slip_devs, 0, sizeof(struct net_device *)*slip_maxdev);
1356 /* Fill in our line protocol discipline, and register it */
1357 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1358 printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
1364 static void __exit slip_exit(void)
1367 struct net_device *dev;
1369 unsigned long timeout = jiffies + HZ;
1372 if (slip_devs == NULL)
1375 /* First of all: check for active disciplines and hangup them.
1379 msleep_interruptible(100);
1382 for (i = 0; i < slip_maxdev; i++) {
1386 sl = netdev_priv(dev);
1387 spin_lock_bh(&sl->lock);
1390 tty_hangup(sl->tty);
1392 spin_unlock_bh(&sl->lock);
1394 } while (busy && time_before(jiffies, timeout));
1397 for (i = 0; i < slip_maxdev; i++) {
1401 slip_devs[i] = NULL;
1403 sl = netdev_priv(dev);
1405 printk(KERN_ERR "%s: tty discipline still running\n",
1407 /* Intentionally leak the control block. */
1408 dev->destructor = NULL;
1411 unregister_netdev(dev);
1417 if ((i = tty_unregister_ldisc(N_SLIP)))
1419 printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
1423 module_init(slip_init);
1424 module_exit(slip_exit);
1426 #ifdef CONFIG_SLIP_SMART
1428 * This is start of the code for multislip style line checking
1429 * added by Stanislav Voronyi. All changes before marked VSV
1432 static void sl_outfill(unsigned long sls)
1434 struct slip *sl=(struct slip *)sls;
1436 spin_lock(&sl->lock);
1438 if (sl->tty == NULL)
1443 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1445 /* no packets were transmitted, do outfill */
1446 #ifdef CONFIG_SLIP_MODE_SLIP6
1447 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1449 unsigned char s = END;
1451 /* put END into tty queue. Is it right ??? */
1452 if (!netif_queue_stopped(sl->dev))
1454 /* if device busy no outfill */
1455 sl->tty->driver->write(sl->tty, &s, 1);
1459 set_bit(SLF_OUTWAIT, &sl->flags);
1461 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1464 spin_unlock(&sl->lock);
1467 static void sl_keepalive(unsigned long sls)
1469 struct slip *sl=(struct slip *)sls;
1471 spin_lock(&sl->lock);
1473 if (sl->tty == NULL)
1478 if(test_bit(SLF_KEEPTEST, &sl->flags))
1480 /* keepalive still high :(, we must hangup */
1481 if( sl->outfill ) /* outfill timer must be deleted too */
1482 (void)del_timer(&sl->outfill_timer);
1483 printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1484 tty_hangup(sl->tty); /* this must hangup tty & close slip */
1485 /* I think we need not something else */
1489 set_bit(SLF_KEEPTEST, &sl->flags);
1491 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1495 spin_unlock(&sl->lock);
1499 MODULE_LICENSE("GPL");
1500 MODULE_ALIAS_LDISC(N_SLIP);