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/module.h>
59 #include <linux/moduleparam.h>
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
63 #include <linux/bitops.h>
64 #include <linux/string.h>
66 #include <linux/interrupt.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/delay.h>
77 #include <linux/init.h>
81 #include <linux/tcp.h>
82 #include <net/slhc_vj.h>
85 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
87 static struct net_device
**slip_devs
;
89 static int slip_maxdev
= SL_NRUNIT
;
90 module_param(slip_maxdev
, int, 0);
91 MODULE_PARM_DESC(slip_maxdev
, "Maximum number of slip devices");
93 static int slip_esc(unsigned char *p
, unsigned char *d
, int len
);
94 static void slip_unesc(struct slip
*sl
, unsigned char c
);
95 #ifdef CONFIG_SLIP_MODE_SLIP6
96 static int slip_esc6(unsigned char *p
, unsigned char *d
, int len
);
97 static void slip_unesc6(struct slip
*sl
, unsigned char c
);
99 #ifdef CONFIG_SLIP_SMART
100 static void sl_keepalive(unsigned long sls
);
101 static void sl_outfill(unsigned long sls
);
102 static int sl_ioctl(struct net_device
*dev
,struct ifreq
*rq
,int cmd
);
105 /********************************
106 * Buffer administration routines:
111 * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
112 * sl_realloc_bufs provides strong atomicity and reallocation
113 * on actively running device.
114 *********************************/
117 Allocate channel buffers.
121 sl_alloc_bufs(struct slip
*sl
, int mtu
)
127 #ifdef SL_INCLUDE_CSLIP
129 struct slcompress
*slcomp
= NULL
;
133 * Allocate the SLIP frame buffers:
135 * rbuff Receive buffer.
136 * xbuff Transmit buffer.
137 * cbuff Temporary compression buffer.
142 * allow for arrival of larger UDP packets, even if we say not to
143 * also fixes a bug in which SunOS sends 512-byte packets even with
148 rbuff
= kmalloc(len
+ 4, GFP_KERNEL
);
151 xbuff
= kmalloc(len
+ 4, GFP_KERNEL
);
154 #ifdef SL_INCLUDE_CSLIP
155 cbuff
= kmalloc(len
+ 4, GFP_KERNEL
);
158 slcomp
= slhc_init(16, 16);
162 spin_lock_bh(&sl
->lock
);
163 if (sl
->tty
== NULL
) {
164 spin_unlock_bh(&sl
->lock
);
172 rbuff
= xchg(&sl
->rbuff
, rbuff
);
173 xbuff
= xchg(&sl
->xbuff
, xbuff
);
174 #ifdef SL_INCLUDE_CSLIP
175 cbuff
= xchg(&sl
->cbuff
, cbuff
);
176 slcomp
= xchg(&sl
->slcomp
, slcomp
);
177 #ifdef CONFIG_SLIP_MODE_SLIP6
182 spin_unlock_bh(&sl
->lock
);
187 #ifdef SL_INCLUDE_CSLIP
197 /* Free a SLIP channel buffers. */
199 sl_free_bufs(struct slip
*sl
)
201 /* Free all SLIP frame buffers. */
202 kfree(xchg(&sl
->rbuff
, NULL
));
203 kfree(xchg(&sl
->xbuff
, NULL
));
204 #ifdef SL_INCLUDE_CSLIP
205 kfree(xchg(&sl
->cbuff
, NULL
));
206 slhc_free(xchg(&sl
->slcomp
, NULL
));
211 Reallocate slip channel buffers.
214 static int sl_realloc_bufs(struct slip
*sl
, int mtu
)
217 struct net_device
*dev
= sl
->dev
;
218 unsigned char *xbuff
, *rbuff
;
219 #ifdef SL_INCLUDE_CSLIP
220 unsigned char *cbuff
;
225 * allow for arrival of larger UDP packets, even if we say not to
226 * also fixes a bug in which SunOS sends 512-byte packets even with
232 xbuff
= (unsigned char *) kmalloc (len
+ 4, GFP_ATOMIC
);
233 rbuff
= (unsigned char *) kmalloc (len
+ 4, GFP_ATOMIC
);
234 #ifdef SL_INCLUDE_CSLIP
235 cbuff
= (unsigned char *) kmalloc (len
+ 4, GFP_ATOMIC
);
239 #ifdef SL_INCLUDE_CSLIP
240 if (xbuff
== NULL
|| rbuff
== NULL
|| cbuff
== NULL
) {
242 if (xbuff
== NULL
|| rbuff
== NULL
) {
244 if (mtu
>= sl
->mtu
) {
245 printk(KERN_WARNING
"%s: unable to grow slip buffers, MTU change cancelled.\n",
252 spin_lock_bh(&sl
->lock
);
258 xbuff
= xchg(&sl
->xbuff
, xbuff
);
259 rbuff
= xchg(&sl
->rbuff
, rbuff
);
260 #ifdef SL_INCLUDE_CSLIP
261 cbuff
= xchg(&sl
->cbuff
, cbuff
);
264 if (sl
->xleft
<= len
) {
265 memcpy(sl
->xbuff
, sl
->xhead
, sl
->xleft
);
271 sl
->xhead
= sl
->xbuff
;
274 if (sl
->rcount
<= len
) {
275 memcpy(sl
->rbuff
, rbuff
, sl
->rcount
);
278 sl
->rx_over_errors
++;
279 set_bit(SLF_ERROR
, &sl
->flags
);
288 spin_unlock_bh(&sl
->lock
);
293 #ifdef SL_INCLUDE_CSLIP
300 /* Set the "sending" flag. This must be atomic hence the set_bit. */
302 sl_lock(struct slip
*sl
)
304 netif_stop_queue(sl
->dev
);
308 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
310 sl_unlock(struct slip
*sl
)
312 netif_wake_queue(sl
->dev
);
315 /* Send one completely decapsulated IP datagram to the IP layer. */
317 sl_bump(struct slip
*sl
)
323 #ifdef SL_INCLUDE_CSLIP
324 if (sl
->mode
& (SL_MODE_ADAPTIVE
| SL_MODE_CSLIP
)) {
326 if ((c
= sl
->rbuff
[0]) & SL_TYPE_COMPRESSED_TCP
) {
327 /* ignore compressed packets when CSLIP is off */
328 if (!(sl
->mode
& SL_MODE_CSLIP
)) {
329 printk(KERN_WARNING
"%s: compressed packet ignored\n", sl
->dev
->name
);
332 /* make sure we've reserved enough space for uncompress to use */
333 if (count
+ 80 > sl
->buffsize
) {
334 sl
->rx_over_errors
++;
337 count
= slhc_uncompress(sl
->slcomp
, sl
->rbuff
, count
);
341 } else if (c
>= SL_TYPE_UNCOMPRESSED_TCP
) {
342 if (!(sl
->mode
& SL_MODE_CSLIP
)) {
343 /* turn on header compression */
344 sl
->mode
|= SL_MODE_CSLIP
;
345 sl
->mode
&= ~SL_MODE_ADAPTIVE
;
346 printk(KERN_INFO
"%s: header compression turned on\n", sl
->dev
->name
);
348 sl
->rbuff
[0] &= 0x4f;
349 if (slhc_remember(sl
->slcomp
, sl
->rbuff
, count
) <= 0) {
354 #endif /* SL_INCLUDE_CSLIP */
358 skb
= dev_alloc_skb(count
);
360 printk(KERN_WARNING
"%s: memory squeeze, dropping packet.\n", sl
->dev
->name
);
365 memcpy(skb_put(skb
,count
), sl
->rbuff
, count
);
366 skb
->mac
.raw
=skb
->data
;
367 skb
->protocol
=htons(ETH_P_IP
);
369 sl
->dev
->last_rx
= jiffies
;
373 /* Encapsulate one IP datagram and stuff into a TTY queue. */
375 sl_encaps(struct slip
*sl
, unsigned char *icp
, int len
)
380 if (len
> sl
->mtu
) { /* Sigh, shouldn't occur BUT ... */
381 printk(KERN_WARNING
"%s: truncating oversized transmit packet!\n", sl
->dev
->name
);
388 #ifdef SL_INCLUDE_CSLIP
389 if (sl
->mode
& SL_MODE_CSLIP
) {
390 len
= slhc_compress(sl
->slcomp
, p
, len
, sl
->cbuff
, &p
, 1);
393 #ifdef CONFIG_SLIP_MODE_SLIP6
394 if(sl
->mode
& SL_MODE_SLIP6
)
395 count
= slip_esc6(p
, (unsigned char *) sl
->xbuff
, len
);
398 count
= slip_esc(p
, (unsigned char *) sl
->xbuff
, len
);
400 /* Order of next two lines is *very* important.
401 * When we are sending a little amount of data,
402 * the transfer may be completed inside driver.write()
403 * routine, because it's running with interrupts enabled.
404 * In this case we *never* got WRITE_WAKEUP event,
405 * if we did not request it before write operation.
406 * 14 Oct 1994 Dmitry Gorodchanin.
408 sl
->tty
->flags
|= (1 << TTY_DO_WRITE_WAKEUP
);
409 actual
= sl
->tty
->driver
->write(sl
->tty
, sl
->xbuff
, count
);
410 #ifdef SL_CHECK_TRANSMIT
411 sl
->dev
->trans_start
= jiffies
;
413 sl
->xleft
= count
- actual
;
414 sl
->xhead
= sl
->xbuff
+ actual
;
415 #ifdef CONFIG_SLIP_SMART
417 clear_bit(SLF_OUTWAIT
, &sl
->flags
); /* reset outfill flag */
422 * Called by the driver when there's room for more data. If we have
423 * more packets to send, we send them here.
425 static void slip_write_wakeup(struct tty_struct
*tty
)
428 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
430 /* First make sure we're connected. */
431 if (!sl
|| sl
->magic
!= SLIP_MAGIC
|| !netif_running(sl
->dev
)) {
434 if (sl
->xleft
<= 0) {
435 /* Now serial buffer is almost free & we can start
436 * transmission of another packet */
438 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
443 actual
= tty
->driver
->write(tty
, sl
->xhead
, sl
->xleft
);
448 static void sl_tx_timeout(struct net_device
*dev
)
450 struct slip
*sl
= netdev_priv(dev
);
452 spin_lock(&sl
->lock
);
454 if (netif_queue_stopped(dev
)) {
455 if (!netif_running(dev
))
458 /* May be we must check transmitter timeout here ?
459 * 14 Oct 1994 Dmitry Gorodchanin.
461 #ifdef SL_CHECK_TRANSMIT
462 if (time_before(jiffies
, dev
->trans_start
+ 20 * HZ
)) {
463 /* 20 sec timeout not reached */
466 printk(KERN_WARNING
"%s: transmit timed out, %s?\n", dev
->name
,
467 (sl
->tty
->driver
->chars_in_buffer(sl
->tty
) || sl
->xleft
) ?
468 "bad line quality" : "driver error");
470 sl
->tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
476 spin_unlock(&sl
->lock
);
480 /* Encapsulate an IP datagram and kick it into a TTY queue. */
482 sl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
484 struct slip
*sl
= netdev_priv(dev
);
486 spin_lock(&sl
->lock
);
487 if (!netif_running(dev
)) {
488 spin_unlock(&sl
->lock
);
489 printk(KERN_WARNING
"%s: xmit call when iface is down\n", dev
->name
);
493 if (sl
->tty
== NULL
) {
494 spin_unlock(&sl
->lock
);
500 sl
->tx_bytes
+=skb
->len
;
501 sl_encaps(sl
, skb
->data
, skb
->len
);
502 spin_unlock(&sl
->lock
);
509 /******************************************
510 * Routines looking at netdevice side.
511 ******************************************/
513 /* Netdevice UP -> DOWN routine */
516 sl_close(struct net_device
*dev
)
518 struct slip
*sl
= netdev_priv(dev
);
520 spin_lock_bh(&sl
->lock
);
522 /* TTY discipline is running. */
523 sl
->tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
525 netif_stop_queue(dev
);
528 spin_unlock_bh(&sl
->lock
);
533 /* Netdevice DOWN -> UP routine */
535 static int sl_open(struct net_device
*dev
)
537 struct slip
*sl
= netdev_priv(dev
);
542 sl
->flags
&= (1 << SLF_INUSE
);
543 netif_start_queue(dev
);
547 /* Netdevice change MTU request */
549 static int sl_change_mtu(struct net_device
*dev
, int new_mtu
)
551 struct slip
*sl
= netdev_priv(dev
);
553 if (new_mtu
< 68 || new_mtu
> 65534)
556 if (new_mtu
!= dev
->mtu
)
557 return sl_realloc_bufs(sl
, new_mtu
);
561 /* Netdevice get statistics request */
563 static struct net_device_stats
*
564 sl_get_stats(struct net_device
*dev
)
566 static struct net_device_stats stats
;
567 struct slip
*sl
= netdev_priv(dev
);
568 #ifdef SL_INCLUDE_CSLIP
569 struct slcompress
*comp
;
572 memset(&stats
, 0, sizeof(struct net_device_stats
));
574 stats
.rx_packets
= sl
->rx_packets
;
575 stats
.tx_packets
= sl
->tx_packets
;
576 stats
.rx_bytes
= sl
->rx_bytes
;
577 stats
.tx_bytes
= sl
->tx_bytes
;
578 stats
.rx_dropped
= sl
->rx_dropped
;
579 stats
.tx_dropped
= sl
->tx_dropped
;
580 stats
.tx_errors
= sl
->tx_errors
;
581 stats
.rx_errors
= sl
->rx_errors
;
582 stats
.rx_over_errors
= sl
->rx_over_errors
;
583 #ifdef SL_INCLUDE_CSLIP
584 stats
.rx_fifo_errors
= sl
->rx_compressed
;
585 stats
.tx_fifo_errors
= sl
->tx_compressed
;
586 stats
.collisions
= sl
->tx_misses
;
589 stats
.rx_fifo_errors
+= comp
->sls_i_compressed
;
590 stats
.rx_dropped
+= comp
->sls_i_tossed
;
591 stats
.tx_fifo_errors
+= comp
->sls_o_compressed
;
592 stats
.collisions
+= comp
->sls_o_misses
;
594 #endif /* CONFIG_INET */
598 /* Netdevice register callback */
600 static int sl_init(struct net_device
*dev
)
602 struct slip
*sl
= netdev_priv(dev
);
605 * Finish setting up the DEVICE info.
609 dev
->type
= ARPHRD_SLIP
+ sl
->mode
;
610 #ifdef SL_CHECK_TRANSMIT
611 dev
->tx_timeout
= sl_tx_timeout
;
612 dev
->watchdog_timeo
= 20*HZ
;
618 static void sl_uninit(struct net_device
*dev
)
620 struct slip
*sl
= netdev_priv(dev
);
625 static void sl_setup(struct net_device
*dev
)
628 dev
->uninit
= sl_uninit
;
630 dev
->destructor
= free_netdev
;
631 dev
->stop
= sl_close
;
632 dev
->get_stats
= sl_get_stats
;
633 dev
->change_mtu
= sl_change_mtu
;
634 dev
->hard_start_xmit
= sl_xmit
;
635 #ifdef CONFIG_SLIP_SMART
636 dev
->do_ioctl
= sl_ioctl
;
638 dev
->hard_header_len
= 0;
640 dev
->tx_queue_len
= 10;
642 SET_MODULE_OWNER(dev
);
644 /* New-style flags. */
645 dev
->flags
= IFF_NOARP
|IFF_POINTOPOINT
|IFF_MULTICAST
;
648 /******************************************
649 Routines looking at TTY side.
650 ******************************************/
654 * Handle the 'receiver data ready' interrupt.
655 * This function is called by the 'tty_io' module in the kernel when
656 * a block of SLIP data has been received, which can now be decapsulated
657 * and sent on to some IP layer for further processing. This will not
658 * be re-entered while running but other ldisc functions may be called
662 static void slip_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
, char *fp
, int count
)
664 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
666 if (!sl
|| sl
->magic
!= SLIP_MAGIC
||
667 !netif_running(sl
->dev
))
670 /* Read the characters out of the buffer */
673 if (!test_and_set_bit(SLF_ERROR
, &sl
->flags
)) {
679 #ifdef CONFIG_SLIP_MODE_SLIP6
680 if (sl
->mode
& SL_MODE_SLIP6
)
681 slip_unesc6(sl
, *cp
++);
684 slip_unesc(sl
, *cp
++);
688 /************************************
689 * slip_open helper routines.
690 ************************************/
692 /* Collect hanged up channels */
694 static void sl_sync(void)
697 struct net_device
*dev
;
700 for (i
= 0; i
< slip_maxdev
; i
++) {
701 if ((dev
= slip_devs
[i
]) == NULL
)
704 sl
= netdev_priv(dev
);
705 if (sl
->tty
|| sl
->leased
)
707 if (dev
->flags
&IFF_UP
)
713 /* Find a free SLIP channel, and link in this `tty' line. */
720 struct net_device
*dev
= NULL
;
723 if (slip_devs
== NULL
)
724 return NULL
; /* Master array missing ! */
726 for (i
= 0; i
< slip_maxdev
; i
++) {
731 sl
= netdev_priv(dev
);
733 if (sl
->line
!= line
)
738 /* Clear ESCAPE & ERROR flags */
739 sl
->flags
&= (1 << SLF_INUSE
);
746 if (current
->pid
== sl
->pid
) {
747 if (sl
->line
== line
&& score
< 3) {
758 if (sl
->line
== line
&& score
< 1) {
773 sl
= netdev_priv(dev
);
774 sl
->flags
&= (1 << SLF_INUSE
);
779 /* Sorry, too many, all slots in use */
780 if (i
>= slip_maxdev
)
784 sl
= netdev_priv(dev
);
785 if (test_bit(SLF_INUSE
, &sl
->flags
)) {
786 printk(KERN_WARNING
"%s: netdev in use on open, will fail.\n",
788 unregister_netdevice(dev
);
796 sprintf(name
, "sl%d", i
);
798 dev
= alloc_netdev(sizeof(*sl
), name
, sl_setup
);
804 sl
= netdev_priv(dev
);
806 /* Initialize channel control data */
807 sl
->magic
= SLIP_MAGIC
;
809 spin_lock_init(&sl
->lock
);
810 sl
->mode
= SL_MODE_DEFAULT
;
811 #ifdef CONFIG_SLIP_SMART
812 init_timer(&sl
->keepalive_timer
); /* initialize timer_list struct */
813 sl
->keepalive_timer
.data
=(unsigned long)sl
;
814 sl
->keepalive_timer
.function
=sl_keepalive
;
815 init_timer(&sl
->outfill_timer
);
816 sl
->outfill_timer
.data
=(unsigned long)sl
;
817 sl
->outfill_timer
.function
=sl_outfill
;
825 * Open the high-level part of the SLIP channel.
826 * This function is called by the TTY module when the
827 * SLIP line discipline is called for. Because we are
828 * sure the tty line exists, we only have to link it to
829 * a free SLIP channel...
831 * Called in process context serialized from other ldisc calls.
834 static int slip_open(struct tty_struct
*tty
)
839 if(!capable(CAP_NET_ADMIN
))
842 /* RTnetlink lock is misused here to serialize concurrent
843 opens of slip channels. There are better ways, but it is
848 /* Collect hanged up channels. */
851 sl
= (struct slip
*) tty
->disc_data
;
854 /* First make sure we're not already connected. */
855 if (sl
&& sl
->magic
== SLIP_MAGIC
)
858 /* OK. Find a free SLIP channel to use. */
860 if ((sl
= sl_alloc(tty_devnum(tty
))) == NULL
)
865 sl
->line
= tty_devnum(tty
);
866 sl
->pid
= current
->pid
;
868 if (!test_bit(SLF_INUSE
, &sl
->flags
)) {
869 /* Perform the low-level SLIP initialization. */
870 if ((err
= sl_alloc_bufs(sl
, SL_MTU
)) != 0)
873 set_bit(SLF_INUSE
, &sl
->flags
);
875 if ((err
= register_netdevice(sl
->dev
)))
879 #ifdef CONFIG_SLIP_SMART
881 sl
->keepalive_timer
.expires
=jiffies
+sl
->keepalive
*HZ
;
882 add_timer (&sl
->keepalive_timer
);
885 sl
->outfill_timer
.expires
=jiffies
+sl
->outfill
*HZ
;
886 add_timer (&sl
->outfill_timer
);
890 /* Done. We have linked the TTY line to a channel. */
892 tty
->receive_room
= 65536; /* We don't flow control */
893 return sl
->dev
->base_addr
;
900 tty
->disc_data
= NULL
;
901 clear_bit(SLF_INUSE
, &sl
->flags
);
906 /* Count references from TTY module */
912 FIXME: 1,2 are fixed 3 was never true anyway.
914 Let me to blame a bit.
915 1. TTY module calls this funstion on soft interrupt.
916 2. TTY module calls this function WITH MASKED INTERRUPTS!
917 3. TTY module does not notify us about line discipline
920 Seems, now it is clean. The solution is to consider netdevice and
921 line discipline sides as two independent threads.
923 By-product (not desired): sl? does not feel hangups and remains open.
924 It is supposed, that user level program (dip, diald, slattach...)
925 will catch SIGHUP and make the rest of work.
927 I see no way to make more with current tty code. --ANK
931 * Close down a SLIP channel.
932 * This means flushing out any pending queues, and then returning. This
933 * call is serialized against other ldisc functions.
936 slip_close(struct tty_struct
*tty
)
938 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
939 struct net_device
*dev
= sl
->dev
;
941 /* First make sure we're connected. */
942 if (!sl
|| sl
->magic
!= SLIP_MAGIC
|| sl
->tty
!= tty
)
946 tty
->disc_data
= NULL
;
951 /* VSV = very important to remove timers */
952 #ifdef CONFIG_SLIP_SMART
953 del_timer_sync(&sl
->keepalive_timer
);
954 del_timer_sync(&sl
->outfill_timer
);
957 /* Count references from TTY module */
959 /* unregister the netdev, otherwise the sysfs workq'd unregister
960 * causes us to fail with EEXIST when registering on open */
962 if (dev
&& test_bit(SLF_INUSE
, &sl
->flags
)) {
963 slip_devs
[dev
->base_addr
] = NULL
;
965 unregister_netdevice(dev
);
966 clear_bit(SLF_INUSE
, &sl
->flags
);
971 /************************************************************************
972 * STANDARD SLIP ENCAPSULATION *
973 ************************************************************************/
976 slip_esc(unsigned char *s
, unsigned char *d
, int len
)
978 unsigned char *ptr
= d
;
982 * Send an initial END character to flush out any
983 * data that may have accumulated in the receiver
990 * For each byte in the packet, send the appropriate
991 * character sequence, according to the SLIP protocol.
1013 static void slip_unesc(struct slip
*sl
, unsigned char s
)
1018 #ifdef CONFIG_SLIP_SMART
1019 /* drop keeptest bit = VSV */
1020 if (test_bit(SLF_KEEPTEST
, &sl
->flags
))
1021 clear_bit(SLF_KEEPTEST
, &sl
->flags
);
1024 if (!test_and_clear_bit(SLF_ERROR
, &sl
->flags
) && (sl
->rcount
> 2)) {
1027 clear_bit(SLF_ESCAPE
, &sl
->flags
);
1032 set_bit(SLF_ESCAPE
, &sl
->flags
);
1035 if (test_and_clear_bit(SLF_ESCAPE
, &sl
->flags
)) {
1040 if (test_and_clear_bit(SLF_ESCAPE
, &sl
->flags
)) {
1045 if (!test_bit(SLF_ERROR
, &sl
->flags
)) {
1046 if (sl
->rcount
< sl
->buffsize
) {
1047 sl
->rbuff
[sl
->rcount
++] = s
;
1050 sl
->rx_over_errors
++;
1051 set_bit(SLF_ERROR
, &sl
->flags
);
1056 #ifdef CONFIG_SLIP_MODE_SLIP6
1057 /************************************************************************
1058 * 6 BIT SLIP ENCAPSULATION *
1059 ************************************************************************/
1062 slip_esc6(unsigned char *s
, unsigned char *d
, int len
)
1064 unsigned char *ptr
= d
;
1067 unsigned short v
= 0;
1071 * Send an initial END character to flush out any
1072 * data that may have accumulated in the receiver
1073 * due to line noise.
1079 * Encode the packet into printable ascii characters
1082 for (i
= 0; i
< len
; ++i
) {
1083 v
= (v
<< 8) | s
[i
];
1087 c
= 0x30 + ((v
>> bits
) & 0x3F);
1092 c
= 0x30 + ((v
<< (6 - bits
)) & 0x3F);
1100 slip_unesc6(struct slip
*sl
, unsigned char s
)
1105 #ifdef CONFIG_SLIP_SMART
1106 /* drop keeptest bit = VSV */
1107 if (test_bit(SLF_KEEPTEST
, &sl
->flags
))
1108 clear_bit(SLF_KEEPTEST
, &sl
->flags
);
1111 if (!test_and_clear_bit(SLF_ERROR
, &sl
->flags
) && (sl
->rcount
> 2)) {
1117 } else if (s
>= 0x30 && s
< 0x70) {
1118 sl
->xdata
= (sl
->xdata
<< 6) | ((s
- 0x30) & 0x3F);
1120 if (sl
->xbits
>= 8) {
1122 c
= (unsigned char)(sl
->xdata
>> sl
->xbits
);
1123 if (!test_bit(SLF_ERROR
, &sl
->flags
)) {
1124 if (sl
->rcount
< sl
->buffsize
) {
1125 sl
->rbuff
[sl
->rcount
++] = c
;
1128 sl
->rx_over_errors
++;
1129 set_bit(SLF_ERROR
, &sl
->flags
);
1134 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1136 /* Perform I/O control on an active SLIP channel. */
1137 static int slip_ioctl(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1139 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
1141 int __user
*p
= (int __user
*)arg
;
1143 /* First make sure we're connected. */
1144 if (!sl
|| sl
->magic
!= SLIP_MAGIC
) {
1150 tmp
= strlen(sl
->dev
->name
) + 1;
1151 if (copy_to_user((void __user
*)arg
, sl
->dev
->name
, tmp
))
1156 if (put_user(sl
->mode
, p
))
1161 if (get_user(tmp
, p
))
1163 #ifndef SL_INCLUDE_CSLIP
1164 if (tmp
& (SL_MODE_CSLIP
|SL_MODE_ADAPTIVE
)) {
1168 if ((tmp
& (SL_MODE_ADAPTIVE
| SL_MODE_CSLIP
)) ==
1169 (SL_MODE_ADAPTIVE
| SL_MODE_CSLIP
)) {
1170 /* return -EINVAL; */
1171 tmp
&= ~SL_MODE_ADAPTIVE
;
1174 #ifndef CONFIG_SLIP_MODE_SLIP6
1175 if (tmp
& SL_MODE_SLIP6
) {
1180 sl
->dev
->type
= ARPHRD_SLIP
+sl
->mode
;
1186 #ifdef CONFIG_SLIP_SMART
1187 /* VSV changes start here */
1188 case SIOCSKEEPALIVE
:
1189 if (get_user(tmp
, p
))
1191 if (tmp
> 255) /* max for unchar */
1194 spin_lock_bh(&sl
->lock
);
1196 spin_unlock_bh(&sl
->lock
);
1199 if ((sl
->keepalive
= (unchar
) tmp
) != 0) {
1200 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1201 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1203 del_timer (&sl
->keepalive_timer
);
1205 spin_unlock_bh(&sl
->lock
);
1208 case SIOCGKEEPALIVE
:
1209 if (put_user(sl
->keepalive
, p
))
1214 if (get_user(tmp
, p
))
1216 if (tmp
> 255) /* max for unchar */
1218 spin_lock_bh(&sl
->lock
);
1220 spin_unlock_bh(&sl
->lock
);
1223 if ((sl
->outfill
= (unchar
) tmp
) != 0){
1224 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1225 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1227 del_timer (&sl
->outfill_timer
);
1229 spin_unlock_bh(&sl
->lock
);
1233 if (put_user(sl
->outfill
, p
))
1236 /* VSV changes end */
1239 /* Allow stty to read, but not set, the serial port */
1242 return n_tty_ioctl(tty
, file
, cmd
, arg
);
1245 return -ENOIOCTLCMD
;
1249 /* VSV changes start here */
1250 #ifdef CONFIG_SLIP_SMART
1251 /* function do_ioctl called from net/core/dev.c
1252 to allow get/set outfill/keepalive parameter
1255 static int sl_ioctl(struct net_device
*dev
,struct ifreq
*rq
,int cmd
)
1257 struct slip
*sl
= netdev_priv(dev
);
1258 unsigned long *p
= (unsigned long *)&rq
->ifr_ifru
;
1260 if (sl
== NULL
) /* Allocation failed ?? */
1263 spin_lock_bh(&sl
->lock
);
1266 spin_unlock_bh(&sl
->lock
);
1271 case SIOCSKEEPALIVE
:
1272 /* max for unchar */
1273 if ((unsigned)*p
> 255) {
1274 spin_unlock_bh(&sl
->lock
);
1277 sl
->keepalive
= (unchar
) *p
;
1278 if (sl
->keepalive
!= 0) {
1279 sl
->keepalive_timer
.expires
=jiffies
+sl
->keepalive
*HZ
;
1280 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1281 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1283 del_timer(&sl
->keepalive_timer
);
1287 case SIOCGKEEPALIVE
:
1292 if ((unsigned)*p
> 255) { /* max for unchar */
1293 spin_unlock_bh(&sl
->lock
);
1296 if ((sl
->outfill
= (unchar
)*p
) != 0){
1297 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1298 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1300 del_timer (&sl
->outfill_timer
);
1309 /* Resolve race condition, when ioctl'ing hanged up
1310 and opened by another process device.
1312 if (sl
->tty
!= current
->signal
->tty
&& sl
->pid
!= current
->pid
) {
1313 spin_unlock_bh(&sl
->lock
);
1324 spin_unlock_bh(&sl
->lock
);
1328 /* VSV changes end */
1330 static struct tty_ldisc sl_ldisc
= {
1331 .owner
= THIS_MODULE
,
1332 .magic
= TTY_LDISC_MAGIC
,
1335 .close
= slip_close
,
1336 .ioctl
= slip_ioctl
,
1337 .receive_buf
= slip_receive_buf
,
1338 .write_wakeup
= slip_write_wakeup
,
1341 static int __init
slip_init(void)
1345 if (slip_maxdev
< 4)
1346 slip_maxdev
= 4; /* Sanity */
1348 printk(KERN_INFO
"SLIP: version %s (dynamic channels, max=%d)"
1349 #ifdef CONFIG_SLIP_MODE_SLIP6
1350 " (6 bit encapsulation enabled)"
1353 SLIP_VERSION
, slip_maxdev
);
1354 #if defined(SL_INCLUDE_CSLIP)
1355 printk(KERN_INFO
"CSLIP: code copyright 1989 Regents of the University of California.\n");
1357 #ifdef CONFIG_SLIP_SMART
1358 printk(KERN_INFO
"SLIP linefill/keepalive option.\n");
1361 slip_devs
= kmalloc(sizeof(struct net_device
*)*slip_maxdev
, GFP_KERNEL
);
1363 printk(KERN_ERR
"SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1367 /* Clear the pointer array, we allocate devices when we need them */
1368 memset(slip_devs
, 0, sizeof(struct net_device
*)*slip_maxdev
);
1370 /* Fill in our line protocol discipline, and register it */
1371 if ((status
= tty_register_ldisc(N_SLIP
, &sl_ldisc
)) != 0) {
1372 printk(KERN_ERR
"SLIP: can't register line discipline (err = %d)\n", status
);
1378 static void __exit
slip_exit(void)
1381 struct net_device
*dev
;
1383 unsigned long timeout
= jiffies
+ HZ
;
1386 if (slip_devs
== NULL
)
1389 /* First of all: check for active disciplines and hangup them.
1393 msleep_interruptible(100);
1396 for (i
= 0; i
< slip_maxdev
; i
++) {
1400 sl
= netdev_priv(dev
);
1401 spin_lock_bh(&sl
->lock
);
1404 tty_hangup(sl
->tty
);
1406 spin_unlock_bh(&sl
->lock
);
1408 } while (busy
&& time_before(jiffies
, timeout
));
1411 for (i
= 0; i
< slip_maxdev
; i
++) {
1415 slip_devs
[i
] = NULL
;
1417 sl
= netdev_priv(dev
);
1419 printk(KERN_ERR
"%s: tty discipline still running\n",
1421 /* Intentionally leak the control block. */
1422 dev
->destructor
= NULL
;
1425 unregister_netdev(dev
);
1431 if ((i
= tty_unregister_ldisc(N_SLIP
)))
1433 printk(KERN_ERR
"SLIP: can't unregister line discipline (err = %d)\n", i
);
1437 module_init(slip_init
);
1438 module_exit(slip_exit
);
1440 #ifdef CONFIG_SLIP_SMART
1442 * This is start of the code for multislip style line checking
1443 * added by Stanislav Voronyi. All changes before marked VSV
1446 static void sl_outfill(unsigned long sls
)
1448 struct slip
*sl
=(struct slip
*)sls
;
1450 spin_lock(&sl
->lock
);
1452 if (sl
->tty
== NULL
)
1457 if( test_bit(SLF_OUTWAIT
, &sl
->flags
) )
1459 /* no packets were transmitted, do outfill */
1460 #ifdef CONFIG_SLIP_MODE_SLIP6
1461 unsigned char s
= (sl
->mode
& SL_MODE_SLIP6
)?0x70:END
;
1463 unsigned char s
= END
;
1465 /* put END into tty queue. Is it right ??? */
1466 if (!netif_queue_stopped(sl
->dev
))
1468 /* if device busy no outfill */
1469 sl
->tty
->driver
->write(sl
->tty
, &s
, 1);
1473 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1475 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1478 spin_unlock(&sl
->lock
);
1481 static void sl_keepalive(unsigned long sls
)
1483 struct slip
*sl
=(struct slip
*)sls
;
1485 spin_lock(&sl
->lock
);
1487 if (sl
->tty
== NULL
)
1492 if(test_bit(SLF_KEEPTEST
, &sl
->flags
))
1494 /* keepalive still high :(, we must hangup */
1495 if( sl
->outfill
) /* outfill timer must be deleted too */
1496 (void)del_timer(&sl
->outfill_timer
);
1497 printk(KERN_DEBUG
"%s: no packets received during keepalive timeout, hangup.\n", sl
->dev
->name
);
1498 tty_hangup(sl
->tty
); /* this must hangup tty & close slip */
1499 /* I think we need not something else */
1503 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1505 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1509 spin_unlock(&sl
->lock
);
1513 MODULE_LICENSE("GPL");
1514 MODULE_ALIAS_LDISC(N_SLIP
);