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>
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
63 #include <asm/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/init.h>
80 #include <linux/tcp.h>
81 #include <net/slhc_vj.h>
84 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
86 static struct net_device
**slip_devs
;
88 int slip_maxdev
= SL_NRUNIT
; /* Can be overridden with insmod! */
89 MODULE_PARM(slip_maxdev
, "i");
90 MODULE_PARM_DESC(slip_maxdev
, "Maximum number of slip devices");
92 static int slip_esc(unsigned char *p
, unsigned char *d
, int len
);
93 static void slip_unesc(struct slip
*sl
, unsigned char c
);
94 #ifdef CONFIG_SLIP_MODE_SLIP6
95 static int slip_esc6(unsigned char *p
, unsigned char *d
, int len
);
96 static void slip_unesc6(struct slip
*sl
, unsigned char c
);
98 #ifdef CONFIG_SLIP_SMART
99 static void sl_keepalive(unsigned long sls
);
100 static void sl_outfill(unsigned long sls
);
101 static int sl_ioctl(struct net_device
*dev
,struct ifreq
*rq
,int cmd
);
104 /********************************
105 * Buffer administration routines:
110 * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
111 * sl_realloc_bufs provides strong atomicity and reallocation
112 * on actively running device.
113 *********************************/
116 Allocate channel buffers.
120 sl_alloc_bufs(struct slip
*sl
, int mtu
)
126 #ifdef SL_INCLUDE_CSLIP
128 struct slcompress
*slcomp
= NULL
;
132 * Allocate the SLIP frame buffers:
134 * rbuff Receive buffer.
135 * xbuff Transmit buffer.
136 * cbuff Temporary compression buffer.
141 * allow for arrival of larger UDP packets, even if we say not to
142 * also fixes a bug in which SunOS sends 512-byte packets even with
147 rbuff
= kmalloc(len
+ 4, GFP_KERNEL
);
150 xbuff
= kmalloc(len
+ 4, GFP_KERNEL
);
153 #ifdef SL_INCLUDE_CSLIP
154 cbuff
= kmalloc(len
+ 4, GFP_KERNEL
);
157 slcomp
= slhc_init(16, 16);
161 spin_lock_bh(&sl
->lock
);
162 if (sl
->tty
== NULL
) {
163 spin_unlock_bh(&sl
->lock
);
171 rbuff
= xchg(&sl
->rbuff
, rbuff
);
172 xbuff
= xchg(&sl
->xbuff
, xbuff
);
173 #ifdef SL_INCLUDE_CSLIP
174 cbuff
= xchg(&sl
->cbuff
, cbuff
);
175 slcomp
= xchg(&sl
->slcomp
, slcomp
);
176 #ifdef CONFIG_SLIP_MODE_SLIP6
181 spin_unlock_bh(&sl
->lock
);
186 #ifdef SL_INCLUDE_CSLIP
199 /* Free a SLIP channel buffers. */
201 sl_free_bufs(struct slip
*sl
)
205 /* Free all SLIP frame buffers. */
206 if ((tmp
= xchg(&sl
->rbuff
, NULL
)) != NULL
)
208 if ((tmp
= xchg(&sl
->xbuff
, NULL
)) != NULL
)
210 #ifdef SL_INCLUDE_CSLIP
211 if ((tmp
= xchg(&sl
->cbuff
, NULL
)) != NULL
)
213 if ((tmp
= xchg(&sl
->slcomp
, NULL
)) != NULL
)
219 Reallocate slip channel buffers.
222 static int sl_realloc_bufs(struct slip
*sl
, int mtu
)
225 struct net_device
*dev
= sl
->dev
;
226 unsigned char *xbuff
, *rbuff
;
227 #ifdef SL_INCLUDE_CSLIP
228 unsigned char *cbuff
;
233 * allow for arrival of larger UDP packets, even if we say not to
234 * also fixes a bug in which SunOS sends 512-byte packets even with
240 xbuff
= (unsigned char *) kmalloc (len
+ 4, GFP_ATOMIC
);
241 rbuff
= (unsigned char *) kmalloc (len
+ 4, GFP_ATOMIC
);
242 #ifdef SL_INCLUDE_CSLIP
243 cbuff
= (unsigned char *) kmalloc (len
+ 4, GFP_ATOMIC
);
247 #ifdef SL_INCLUDE_CSLIP
248 if (xbuff
== NULL
|| rbuff
== NULL
|| cbuff
== NULL
) {
250 if (xbuff
== NULL
|| rbuff
== NULL
) {
252 if (mtu
>= sl
->mtu
) {
253 printk(KERN_WARNING
"%s: unable to grow slip buffers, MTU change cancelled.\n",
260 spin_lock_bh(&sl
->lock
);
266 xbuff
= xchg(&sl
->xbuff
, xbuff
);
267 rbuff
= xchg(&sl
->rbuff
, rbuff
);
268 #ifdef SL_INCLUDE_CSLIP
269 cbuff
= xchg(&sl
->cbuff
, cbuff
);
272 if (sl
->xleft
<= len
) {
273 memcpy(sl
->xbuff
, sl
->xhead
, sl
->xleft
);
279 sl
->xhead
= sl
->xbuff
;
282 if (sl
->rcount
<= len
) {
283 memcpy(sl
->rbuff
, rbuff
, sl
->rcount
);
286 sl
->rx_over_errors
++;
287 set_bit(SLF_ERROR
, &sl
->flags
);
296 spin_unlock_bh(&sl
->lock
);
303 #ifdef SL_INCLUDE_CSLIP
311 /* Set the "sending" flag. This must be atomic hence the set_bit. */
313 sl_lock(struct slip
*sl
)
315 netif_stop_queue(sl
->dev
);
319 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
321 sl_unlock(struct slip
*sl
)
323 netif_wake_queue(sl
->dev
);
326 /* Send one completely decapsulated IP datagram to the IP layer. */
328 sl_bump(struct slip
*sl
)
334 #ifdef SL_INCLUDE_CSLIP
335 if (sl
->mode
& (SL_MODE_ADAPTIVE
| SL_MODE_CSLIP
)) {
337 if ((c
= sl
->rbuff
[0]) & SL_TYPE_COMPRESSED_TCP
) {
338 /* ignore compressed packets when CSLIP is off */
339 if (!(sl
->mode
& SL_MODE_CSLIP
)) {
340 printk(KERN_WARNING
"%s: compressed packet ignored\n", sl
->dev
->name
);
343 /* make sure we've reserved enough space for uncompress to use */
344 if (count
+ 80 > sl
->buffsize
) {
345 sl
->rx_over_errors
++;
348 count
= slhc_uncompress(sl
->slcomp
, sl
->rbuff
, count
);
352 } else if (c
>= SL_TYPE_UNCOMPRESSED_TCP
) {
353 if (!(sl
->mode
& SL_MODE_CSLIP
)) {
354 /* turn on header compression */
355 sl
->mode
|= SL_MODE_CSLIP
;
356 sl
->mode
&= ~SL_MODE_ADAPTIVE
;
357 printk(KERN_INFO
"%s: header compression turned on\n", sl
->dev
->name
);
359 sl
->rbuff
[0] &= 0x4f;
360 if (slhc_remember(sl
->slcomp
, sl
->rbuff
, count
) <= 0) {
365 #endif /* SL_INCLUDE_CSLIP */
369 skb
= dev_alloc_skb(count
);
371 printk(KERN_WARNING
"%s: memory squeeze, dropping packet.\n", sl
->dev
->name
);
376 memcpy(skb_put(skb
,count
), sl
->rbuff
, count
);
377 skb
->mac
.raw
=skb
->data
;
378 skb
->protocol
=htons(ETH_P_IP
);
380 sl
->dev
->last_rx
= jiffies
;
384 /* Encapsulate one IP datagram and stuff into a TTY queue. */
386 sl_encaps(struct slip
*sl
, unsigned char *icp
, int len
)
391 if (len
> sl
->mtu
) { /* Sigh, shouldn't occur BUT ... */
392 printk(KERN_WARNING
"%s: truncating oversized transmit packet!\n", sl
->dev
->name
);
399 #ifdef SL_INCLUDE_CSLIP
400 if (sl
->mode
& SL_MODE_CSLIP
) {
401 len
= slhc_compress(sl
->slcomp
, p
, len
, sl
->cbuff
, &p
, 1);
404 #ifdef CONFIG_SLIP_MODE_SLIP6
405 if(sl
->mode
& SL_MODE_SLIP6
)
406 count
= slip_esc6(p
, (unsigned char *) sl
->xbuff
, len
);
409 count
= slip_esc(p
, (unsigned char *) sl
->xbuff
, len
);
411 /* Order of next two lines is *very* important.
412 * When we are sending a little amount of data,
413 * the transfer may be completed inside driver.write()
414 * routine, because it's running with interrupts enabled.
415 * In this case we *never* got WRITE_WAKEUP event,
416 * if we did not request it before write operation.
417 * 14 Oct 1994 Dmitry Gorodchanin.
419 sl
->tty
->flags
|= (1 << TTY_DO_WRITE_WAKEUP
);
420 actual
= sl
->tty
->driver
->write(sl
->tty
, 0, sl
->xbuff
, count
);
421 #ifdef SL_CHECK_TRANSMIT
422 sl
->dev
->trans_start
= jiffies
;
424 sl
->xleft
= count
- actual
;
425 sl
->xhead
= sl
->xbuff
+ actual
;
426 #ifdef CONFIG_SLIP_SMART
428 clear_bit(SLF_OUTWAIT
, &sl
->flags
); /* reset outfill flag */
433 * Called by the driver when there's room for more data. If we have
434 * more packets to send, we send them here.
436 static void slip_write_wakeup(struct tty_struct
*tty
)
439 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
441 /* First make sure we're connected. */
442 if (!sl
|| sl
->magic
!= SLIP_MAGIC
|| !netif_running(sl
->dev
)) {
445 if (sl
->xleft
<= 0) {
446 /* Now serial buffer is almost free & we can start
447 * transmission of another packet */
449 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
454 actual
= tty
->driver
->write(tty
, 0, sl
->xhead
, sl
->xleft
);
459 static void sl_tx_timeout(struct net_device
*dev
)
461 struct slip
*sl
= (struct slip
*)(dev
->priv
);
463 spin_lock(&sl
->lock
);
465 if (netif_queue_stopped(dev
)) {
466 struct slip
*sl
= (struct slip
*)(dev
->priv
);
468 if (!netif_running(dev
))
471 /* May be we must check transmitter timeout here ?
472 * 14 Oct 1994 Dmitry Gorodchanin.
474 #ifdef SL_CHECK_TRANSMIT
475 if (time_before(jiffies
, dev
->trans_start
+ 20 * HZ
)) {
476 /* 20 sec timeout not reached */
479 printk(KERN_WARNING
"%s: transmit timed out, %s?\n", dev
->name
,
480 (sl
->tty
->driver
->chars_in_buffer(sl
->tty
) || sl
->xleft
) ?
481 "bad line quality" : "driver error");
483 sl
->tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
489 spin_unlock(&sl
->lock
);
493 /* Encapsulate an IP datagram and kick it into a TTY queue. */
495 sl_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
497 struct slip
*sl
= (struct slip
*)(dev
->priv
);
499 spin_lock(&sl
->lock
);
500 if (!netif_running(dev
)) {
501 spin_unlock(&sl
->lock
);
502 printk(KERN_WARNING
"%s: xmit call when iface is down\n", dev
->name
);
506 if (sl
->tty
== NULL
) {
507 spin_unlock(&sl
->lock
);
513 sl
->tx_bytes
+=skb
->len
;
514 sl_encaps(sl
, skb
->data
, skb
->len
);
515 spin_unlock(&sl
->lock
);
522 /******************************************
523 * Routines looking at netdevice side.
524 ******************************************/
526 /* Netdevice UP -> DOWN routine */
529 sl_close(struct net_device
*dev
)
531 struct slip
*sl
= (struct slip
*)(dev
->priv
);
533 spin_lock_bh(&sl
->lock
);
535 /* TTY discipline is running. */
536 sl
->tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
538 netif_stop_queue(dev
);
541 spin_unlock_bh(&sl
->lock
);
546 /* Netdevice DOWN -> UP routine */
548 static int sl_open(struct net_device
*dev
)
550 struct slip
*sl
= (struct slip
*)(dev
->priv
);
555 sl
->flags
&= (1 << SLF_INUSE
);
556 netif_start_queue(dev
);
560 /* Netdevice change MTU request */
562 static int sl_change_mtu(struct net_device
*dev
, int new_mtu
)
564 struct slip
*sl
= (struct slip
*)(dev
->priv
);
566 if (new_mtu
< 68 || new_mtu
> 65534)
569 if (new_mtu
!= dev
->mtu
)
570 return sl_realloc_bufs(sl
, new_mtu
);
574 /* Netdevice get statistics request */
576 static struct net_device_stats
*
577 sl_get_stats(struct net_device
*dev
)
579 static struct net_device_stats stats
;
580 struct slip
*sl
= (struct slip
*)(dev
->priv
);
581 #ifdef SL_INCLUDE_CSLIP
582 struct slcompress
*comp
;
585 memset(&stats
, 0, sizeof(struct net_device_stats
));
587 stats
.rx_packets
= sl
->rx_packets
;
588 stats
.tx_packets
= sl
->tx_packets
;
589 stats
.rx_bytes
= sl
->rx_bytes
;
590 stats
.tx_bytes
= sl
->tx_bytes
;
591 stats
.rx_dropped
= sl
->rx_dropped
;
592 stats
.tx_dropped
= sl
->tx_dropped
;
593 stats
.tx_errors
= sl
->tx_errors
;
594 stats
.rx_errors
= sl
->rx_errors
;
595 stats
.rx_over_errors
= sl
->rx_over_errors
;
596 #ifdef SL_INCLUDE_CSLIP
597 stats
.rx_fifo_errors
= sl
->rx_compressed
;
598 stats
.tx_fifo_errors
= sl
->tx_compressed
;
599 stats
.collisions
= sl
->tx_misses
;
602 stats
.rx_fifo_errors
+= comp
->sls_i_compressed
;
603 stats
.rx_dropped
+= comp
->sls_i_tossed
;
604 stats
.tx_fifo_errors
+= comp
->sls_o_compressed
;
605 stats
.collisions
+= comp
->sls_o_misses
;
607 #endif /* CONFIG_INET */
611 /* Netdevice register callback */
613 static int sl_init(struct net_device
*dev
)
615 struct slip
*sl
= (struct slip
*)(dev
->priv
);
618 * Finish setting up the DEVICE info.
622 dev
->type
= ARPHRD_SLIP
+ sl
->mode
;
623 #ifdef SL_CHECK_TRANSMIT
624 dev
->tx_timeout
= sl_tx_timeout
;
625 dev
->watchdog_timeo
= 20*HZ
;
631 static void sl_uninit(struct net_device
*dev
)
633 struct slip
*sl
= (struct slip
*)(dev
->priv
);
638 static void sl_setup(struct net_device
*dev
)
641 dev
->uninit
= sl_uninit
;
643 dev
->destructor
= free_netdev
;
644 dev
->stop
= sl_close
;
645 dev
->get_stats
= sl_get_stats
;
646 dev
->change_mtu
= sl_change_mtu
;
647 dev
->hard_start_xmit
= sl_xmit
;
648 #ifdef CONFIG_SLIP_SMART
649 dev
->do_ioctl
= sl_ioctl
;
651 dev
->hard_header_len
= 0;
653 dev
->tx_queue_len
= 10;
655 SET_MODULE_OWNER(dev
);
657 /* New-style flags. */
658 dev
->flags
= IFF_NOARP
|IFF_POINTOPOINT
|IFF_MULTICAST
;
661 /******************************************
662 Routines looking at TTY side.
663 ******************************************/
666 static int slip_receive_room(struct tty_struct
*tty
)
668 return 65536; /* We can handle an infinite amount of data. :-) */
672 * Handle the 'receiver data ready' interrupt.
673 * This function is called by the 'tty_io' module in the kernel when
674 * a block of SLIP data has been received, which can now be decapsulated
675 * and sent on to some IP layer for further processing. This will not
676 * be re-entered while running but other ldisc functions may be called
680 static void slip_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
, char *fp
, int count
)
682 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
684 if (!sl
|| sl
->magic
!= SLIP_MAGIC
||
685 !netif_running(sl
->dev
))
688 /* Read the characters out of the buffer */
691 if (!test_and_set_bit(SLF_ERROR
, &sl
->flags
)) {
697 #ifdef CONFIG_SLIP_MODE_SLIP6
698 if (sl
->mode
& SL_MODE_SLIP6
)
699 slip_unesc6(sl
, *cp
++);
702 slip_unesc(sl
, *cp
++);
706 /************************************
707 * slip_open helper routines.
708 ************************************/
710 /* Collect hanged up channels */
712 static void sl_sync(void)
715 struct net_device
*dev
;
718 for (i
= 0; i
< slip_maxdev
; i
++) {
719 if ((dev
= slip_devs
[i
]) == NULL
)
723 if (sl
->tty
|| sl
->leased
)
725 if (dev
->flags
&IFF_UP
)
731 /* Find a free SLIP channel, and link in this `tty' line. */
738 struct net_device
*dev
= NULL
;
741 if (slip_devs
== NULL
)
742 return NULL
; /* Master array missing ! */
744 for (i
= 0; i
< slip_maxdev
; i
++) {
751 if (sl
->line
!= line
)
756 /* Clear ESCAPE & ERROR flags */
757 sl
->flags
&= (1 << SLF_INUSE
);
764 if (current
->pid
== sl
->pid
) {
765 if (sl
->line
== line
&& score
< 3) {
776 if (sl
->line
== line
&& score
< 1) {
792 sl
->flags
&= (1 << SLF_INUSE
);
797 /* Sorry, too many, all slots in use */
798 if (i
>= slip_maxdev
)
803 if (test_bit(SLF_INUSE
, &sl
->flags
)) {
804 unregister_netdevice(dev
);
812 sprintf(name
, "sl%d", i
);
814 dev
= alloc_netdev(sizeof(*sl
), name
, sl_setup
);
822 /* Initialize channel control data */
823 sl
->magic
= SLIP_MAGIC
;
825 spin_lock_init(&sl
->lock
);
826 sl
->mode
= SL_MODE_DEFAULT
;
827 #ifdef CONFIG_SLIP_SMART
828 init_timer(&sl
->keepalive_timer
); /* initialize timer_list struct */
829 sl
->keepalive_timer
.data
=(unsigned long)sl
;
830 sl
->keepalive_timer
.function
=sl_keepalive
;
831 init_timer(&sl
->outfill_timer
);
832 sl
->outfill_timer
.data
=(unsigned long)sl
;
833 sl
->outfill_timer
.function
=sl_outfill
;
841 * Open the high-level part of the SLIP channel.
842 * This function is called by the TTY module when the
843 * SLIP line discipline is called for. Because we are
844 * sure the tty line exists, we only have to link it to
845 * a free SLIP channel...
847 * Called in process context serialized from other ldisc calls.
850 static int slip_open(struct tty_struct
*tty
)
855 if(!capable(CAP_NET_ADMIN
))
858 /* RTnetlink lock is misused here to serialize concurrent
859 opens of slip channels. There are better ways, but it is
864 /* Collect hanged up channels. */
867 sl
= (struct slip
*) tty
->disc_data
;
870 /* First make sure we're not already connected. */
871 if (sl
&& sl
->magic
== SLIP_MAGIC
)
874 /* OK. Find a free SLIP channel to use. */
876 if ((sl
= sl_alloc(tty_devnum(tty
))) == NULL
)
881 sl
->line
= tty_devnum(tty
);
882 sl
->pid
= current
->pid
;
884 /* FIXME: already done before we were called - seems this can go */
885 if (tty
->driver
->flush_buffer
)
886 tty
->driver
->flush_buffer(tty
);
888 if (!test_bit(SLF_INUSE
, &sl
->flags
)) {
889 /* Perform the low-level SLIP initialization. */
890 if ((err
= sl_alloc_bufs(sl
, SL_MTU
)) != 0)
893 set_bit(SLF_INUSE
, &sl
->flags
);
895 if ((err
= register_netdevice(sl
->dev
)))
899 #ifdef CONFIG_SLIP_SMART
901 sl
->keepalive_timer
.expires
=jiffies
+sl
->keepalive
*HZ
;
902 add_timer (&sl
->keepalive_timer
);
905 sl
->outfill_timer
.expires
=jiffies
+sl
->outfill
*HZ
;
906 add_timer (&sl
->outfill_timer
);
910 /* Done. We have linked the TTY line to a channel. */
912 return sl
->dev
->base_addr
;
919 tty
->disc_data
= NULL
;
920 clear_bit(SLF_INUSE
, &sl
->flags
);
925 /* Count references from TTY module */
931 FIXME: 1,2 are fixed 3 was never true anyway.
933 Let me to blame a bit.
934 1. TTY module calls this funstion on soft interrupt.
935 2. TTY module calls this function WITH MASKED INTERRUPTS!
936 3. TTY module does not notify us about line discipline
939 Seems, now it is clean. The solution is to consider netdevice and
940 line discipline sides as two independent threads.
942 By-product (not desired): sl? does not feel hangups and remains open.
943 It is supposed, that user level program (dip, diald, slattach...)
944 will catch SIGHUP and make the rest of work.
946 I see no way to make more with current tty code. --ANK
950 * Close down a SLIP channel.
951 * This means flushing out any pending queues, and then returning. This
952 * call is serialized against other ldisc functions.
955 slip_close(struct tty_struct
*tty
)
957 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
959 /* First make sure we're connected. */
960 if (!sl
|| sl
->magic
!= SLIP_MAGIC
|| sl
->tty
!= tty
)
963 tty
->disc_data
= NULL
;
968 /* VSV = very important to remove timers */
969 #ifdef CONFIG_SLIP_SMART
970 del_timer_sync(&sl
->keepalive_timer
);
971 del_timer_sync(&sl
->outfill_timer
);
974 /* Count references from TTY module */
977 /************************************************************************
978 * STANDARD SLIP ENCAPSULATION *
979 ************************************************************************/
982 slip_esc(unsigned char *s
, unsigned char *d
, int len
)
984 unsigned char *ptr
= d
;
988 * Send an initial END character to flush out any
989 * data that may have accumulated in the receiver
996 * For each byte in the packet, send the appropriate
997 * character sequence, according to the SLIP protocol.
1019 static void slip_unesc(struct slip
*sl
, unsigned char s
)
1024 #ifdef CONFIG_SLIP_SMART
1025 /* drop keeptest bit = VSV */
1026 if (test_bit(SLF_KEEPTEST
, &sl
->flags
))
1027 clear_bit(SLF_KEEPTEST
, &sl
->flags
);
1030 if (!test_and_clear_bit(SLF_ERROR
, &sl
->flags
) && (sl
->rcount
> 2)) {
1033 clear_bit(SLF_ESCAPE
, &sl
->flags
);
1038 set_bit(SLF_ESCAPE
, &sl
->flags
);
1041 if (test_and_clear_bit(SLF_ESCAPE
, &sl
->flags
)) {
1046 if (test_and_clear_bit(SLF_ESCAPE
, &sl
->flags
)) {
1051 if (!test_bit(SLF_ERROR
, &sl
->flags
)) {
1052 if (sl
->rcount
< sl
->buffsize
) {
1053 sl
->rbuff
[sl
->rcount
++] = s
;
1056 sl
->rx_over_errors
++;
1057 set_bit(SLF_ERROR
, &sl
->flags
);
1062 #ifdef CONFIG_SLIP_MODE_SLIP6
1063 /************************************************************************
1064 * 6 BIT SLIP ENCAPSULATION *
1065 ************************************************************************/
1068 slip_esc6(unsigned char *s
, unsigned char *d
, int len
)
1070 unsigned char *ptr
= d
;
1073 unsigned short v
= 0;
1077 * Send an initial END character to flush out any
1078 * data that may have accumulated in the receiver
1079 * due to line noise.
1085 * Encode the packet into printable ascii characters
1088 for (i
= 0; i
< len
; ++i
) {
1089 v
= (v
<< 8) | s
[i
];
1093 c
= 0x30 + ((v
>> bits
) & 0x3F);
1098 c
= 0x30 + ((v
<< (6 - bits
)) & 0x3F);
1106 slip_unesc6(struct slip
*sl
, unsigned char s
)
1111 #ifdef CONFIG_SLIP_SMART
1112 /* drop keeptest bit = VSV */
1113 if (test_bit(SLF_KEEPTEST
, &sl
->flags
))
1114 clear_bit(SLF_KEEPTEST
, &sl
->flags
);
1117 if (!test_and_clear_bit(SLF_ERROR
, &sl
->flags
) && (sl
->rcount
> 2)) {
1123 } else if (s
>= 0x30 && s
< 0x70) {
1124 sl
->xdata
= (sl
->xdata
<< 6) | ((s
- 0x30) & 0x3F);
1126 if (sl
->xbits
>= 8) {
1128 c
= (unsigned char)(sl
->xdata
>> sl
->xbits
);
1129 if (!test_bit(SLF_ERROR
, &sl
->flags
)) {
1130 if (sl
->rcount
< sl
->buffsize
) {
1131 sl
->rbuff
[sl
->rcount
++] = c
;
1134 sl
->rx_over_errors
++;
1135 set_bit(SLF_ERROR
, &sl
->flags
);
1140 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1142 /* Perform I/O control on an active SLIP channel. */
1143 static int slip_ioctl(struct tty_struct
*tty
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
1145 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
1147 int __user
*p
= (int __user
*)arg
;
1149 /* First make sure we're connected. */
1150 if (!sl
|| sl
->magic
!= SLIP_MAGIC
) {
1156 tmp
= strlen(sl
->dev
->name
) + 1;
1157 if (copy_to_user((void __user
*)arg
, sl
->dev
->name
, tmp
))
1162 if (put_user(sl
->mode
, p
))
1167 if (get_user(tmp
, p
))
1169 #ifndef SL_INCLUDE_CSLIP
1170 if (tmp
& (SL_MODE_CSLIP
|SL_MODE_ADAPTIVE
)) {
1174 if ((tmp
& (SL_MODE_ADAPTIVE
| SL_MODE_CSLIP
)) ==
1175 (SL_MODE_ADAPTIVE
| SL_MODE_CSLIP
)) {
1176 /* return -EINVAL; */
1177 tmp
&= ~SL_MODE_ADAPTIVE
;
1180 #ifndef CONFIG_SLIP_MODE_SLIP6
1181 if (tmp
& SL_MODE_SLIP6
) {
1186 sl
->dev
->type
= ARPHRD_SLIP
+sl
->mode
;
1192 #ifdef CONFIG_SLIP_SMART
1193 /* VSV changes start here */
1194 case SIOCSKEEPALIVE
:
1195 if (get_user(tmp
, p
))
1197 if (tmp
> 255) /* max for unchar */
1200 spin_lock_bh(&sl
->lock
);
1202 spin_unlock_bh(&sl
->lock
);
1205 if ((sl
->keepalive
= (unchar
) tmp
) != 0) {
1206 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1207 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1209 del_timer (&sl
->keepalive_timer
);
1211 spin_unlock_bh(&sl
->lock
);
1214 case SIOCGKEEPALIVE
:
1215 if (put_user(sl
->keepalive
, p
))
1220 if (get_user(tmp
, p
))
1222 if (tmp
> 255) /* max for unchar */
1224 spin_lock_bh(&sl
->lock
);
1226 spin_unlock_bh(&sl
->lock
);
1229 if ((sl
->outfill
= (unchar
) tmp
) != 0){
1230 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1231 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1233 del_timer (&sl
->outfill_timer
);
1235 spin_unlock_bh(&sl
->lock
);
1239 if (put_user(sl
->outfill
, p
))
1242 /* VSV changes end */
1245 /* Allow stty to read, but not set, the serial port */
1248 return n_tty_ioctl(tty
, file
, cmd
, arg
);
1251 return -ENOIOCTLCMD
;
1255 /* VSV changes start here */
1256 #ifdef CONFIG_SLIP_SMART
1257 /* function do_ioctl called from net/core/dev.c
1258 to allow get/set outfill/keepalive parameter
1261 static int sl_ioctl(struct net_device
*dev
,struct ifreq
*rq
,int cmd
)
1263 struct slip
*sl
= (struct slip
*)(dev
->priv
);
1264 unsigned long *p
= (unsigned long *)&rq
->ifr_ifru
;
1266 if (sl
== NULL
) /* Allocation failed ?? */
1269 spin_lock_bh(&sl
->lock
);
1272 spin_unlock_bh(&sl
->lock
);
1277 case SIOCSKEEPALIVE
:
1278 /* max for unchar */
1279 if ((unsigned)*p
> 255) {
1280 spin_unlock_bh(&sl
->lock
);
1283 sl
->keepalive
= (unchar
) *p
;
1284 if (sl
->keepalive
!= 0) {
1285 sl
->keepalive_timer
.expires
=jiffies
+sl
->keepalive
*HZ
;
1286 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1287 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1289 del_timer(&sl
->keepalive_timer
);
1293 case SIOCGKEEPALIVE
:
1298 if ((unsigned)*p
> 255) { /* max for unchar */
1299 spin_unlock_bh(&sl
->lock
);
1302 if ((sl
->outfill
= (unchar
)*p
) != 0){
1303 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1304 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1306 del_timer (&sl
->outfill_timer
);
1315 /* Resolve race condition, when ioctl'ing hanged up
1316 and opened by another process device.
1318 if (sl
->tty
!= current
->signal
->tty
&& sl
->pid
!= current
->pid
) {
1319 spin_unlock_bh(&sl
->lock
);
1330 spin_unlock_bh(&sl
->lock
);
1334 /* VSV changes end */
1336 static struct tty_ldisc sl_ldisc
= {
1337 .owner
= THIS_MODULE
,
1338 .magic
= TTY_LDISC_MAGIC
,
1341 .close
= slip_close
,
1342 .ioctl
= slip_ioctl
,
1343 .receive_buf
= slip_receive_buf
,
1344 .receive_room
= slip_receive_room
,
1345 .write_wakeup
= slip_write_wakeup
,
1348 static int __init
slip_init(void)
1352 if (slip_maxdev
< 4)
1353 slip_maxdev
= 4; /* Sanity */
1355 printk(KERN_INFO
"SLIP: version %s (dynamic channels, max=%d)"
1356 #ifdef CONFIG_SLIP_MODE_SLIP6
1357 " (6 bit encapsulation enabled)"
1360 SLIP_VERSION
, slip_maxdev
);
1361 #if defined(SL_INCLUDE_CSLIP)
1362 printk(KERN_INFO
"CSLIP: code copyright 1989 Regents of the University of California.\n");
1364 #ifdef CONFIG_SLIP_SMART
1365 printk(KERN_INFO
"SLIP linefill/keepalive option.\n");
1368 slip_devs
= kmalloc(sizeof(struct net_device
*)*slip_maxdev
, GFP_KERNEL
);
1370 printk(KERN_ERR
"SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1374 /* Clear the pointer array, we allocate devices when we need them */
1375 memset(slip_devs
, 0, sizeof(struct net_device
*)*slip_maxdev
);
1377 /* Fill in our line protocol discipline, and register it */
1378 if ((status
= tty_register_ldisc(N_SLIP
, &sl_ldisc
)) != 0) {
1379 printk(KERN_ERR
"SLIP: can't register line discipline (err = %d)\n", status
);
1385 static void __exit
slip_exit(void)
1388 struct net_device
*dev
;
1390 unsigned long timeout
= jiffies
+ HZ
;
1393 if (slip_devs
== NULL
)
1396 /* First of all: check for active disciplines and hangup them.
1400 set_current_state(TASK_INTERRUPTIBLE
);
1401 schedule_timeout(HZ
/ 10);
1405 for (i
= 0; i
< slip_maxdev
; i
++) {
1410 spin_lock_bh(&sl
->lock
);
1413 tty_hangup(sl
->tty
);
1415 spin_unlock_bh(&sl
->lock
);
1417 } while (busy
&& time_before(jiffies
, timeout
));
1420 for (i
= 0; i
< slip_maxdev
; i
++) {
1424 slip_devs
[i
] = NULL
;
1428 printk(KERN_ERR
"%s: tty discipline still running\n",
1430 /* Intentionally leak the control block. */
1431 dev
->destructor
= NULL
;
1434 unregister_netdev(dev
);
1440 if ((i
= tty_register_ldisc(N_SLIP
, NULL
)))
1442 printk(KERN_ERR
"SLIP: can't unregister line discipline (err = %d)\n", i
);
1446 module_init(slip_init
);
1447 module_exit(slip_exit
);
1449 #ifdef CONFIG_SLIP_SMART
1451 * This is start of the code for multislip style line checking
1452 * added by Stanislav Voronyi. All changes before marked VSV
1455 static void sl_outfill(unsigned long sls
)
1457 struct slip
*sl
=(struct slip
*)sls
;
1459 spin_lock(&sl
->lock
);
1461 if (sl
->tty
== NULL
)
1466 if( test_bit(SLF_OUTWAIT
, &sl
->flags
) )
1468 /* no packets were transmitted, do outfill */
1469 #ifdef CONFIG_SLIP_MODE_SLIP6
1470 unsigned char s
= (sl
->mode
& SL_MODE_SLIP6
)?0x70:END
;
1472 unsigned char s
= END
;
1474 /* put END into tty queue. Is it right ??? */
1475 if (!netif_queue_stopped(sl
->dev
))
1477 /* if device busy no outfill */
1478 sl
->tty
->driver
->write(sl
->tty
, 0, &s
, 1);
1482 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1484 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1487 spin_unlock(&sl
->lock
);
1490 static void sl_keepalive(unsigned long sls
)
1492 struct slip
*sl
=(struct slip
*)sls
;
1494 spin_lock(&sl
->lock
);
1496 if (sl
->tty
== NULL
)
1501 if(test_bit(SLF_KEEPTEST
, &sl
->flags
))
1503 /* keepalive still high :(, we must hangup */
1504 if( sl
->outfill
) /* outfill timer must be deleted too */
1505 (void)del_timer(&sl
->outfill_timer
);
1506 printk(KERN_DEBUG
"%s: no packets received during keepalive timeout, hangup.\n", sl
->dev
->name
);
1507 tty_hangup(sl
->tty
); /* this must hangup tty & close slip */
1508 /* I think we need not something else */
1512 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1514 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1518 spin_unlock(&sl
->lock
);
1522 MODULE_LICENSE("GPL");
1523 MODULE_ALIAS_LDISC(N_SLIP
);