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
= (void (*)(struct net_device
*))kfree
;
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.
678 static void slip_receive_buf(struct tty_struct
*tty
, const unsigned char *cp
, char *fp
, int count
)
680 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
682 if (!sl
|| sl
->magic
!= SLIP_MAGIC
||
683 !netif_running(sl
->dev
))
686 /* Read the characters out of the buffer */
689 if (!test_and_set_bit(SLF_ERROR
, &sl
->flags
)) {
695 #ifdef CONFIG_SLIP_MODE_SLIP6
696 if (sl
->mode
& SL_MODE_SLIP6
)
697 slip_unesc6(sl
, *cp
++);
700 slip_unesc(sl
, *cp
++);
704 /************************************
705 * slip_open helper routines.
706 ************************************/
708 /* Collect hanged up channels */
710 static void sl_sync(void)
713 struct net_device
*dev
;
716 for (i
= 0; i
< slip_maxdev
; i
++) {
717 if ((dev
= slip_devs
[i
]) == NULL
)
721 if (sl
->tty
|| sl
->leased
)
723 if (dev
->flags
&IFF_UP
)
729 /* Find a free SLIP channel, and link in this `tty' line. */
736 struct net_device
*dev
= NULL
;
739 if (slip_devs
== NULL
)
740 return NULL
; /* Master array missing ! */
742 for (i
= 0; i
< slip_maxdev
; i
++) {
749 if (sl
->line
!= line
)
754 /* Clear ESCAPE & ERROR flags */
755 sl
->flags
&= (1 << SLF_INUSE
);
762 if (current
->pid
== sl
->pid
) {
763 if (sl
->line
== line
&& score
< 3) {
774 if (sl
->line
== line
&& score
< 1) {
790 sl
->flags
&= (1 << SLF_INUSE
);
795 /* Sorry, too many, all slots in use */
796 if (i
>= slip_maxdev
)
801 if (test_bit(SLF_INUSE
, &sl
->flags
)) {
802 unregister_netdevice(dev
);
810 sprintf(name
, "sl%d", i
);
812 dev
= alloc_netdev(sizeof(*sl
), name
, sl_setup
);
820 /* Initialize channel control data */
821 sl
->magic
= SLIP_MAGIC
;
823 spin_lock_init(&sl
->lock
);
824 sl
->mode
= SL_MODE_DEFAULT
;
825 #ifdef CONFIG_SLIP_SMART
826 init_timer(&sl
->keepalive_timer
); /* initialize timer_list struct */
827 sl
->keepalive_timer
.data
=(unsigned long)sl
;
828 sl
->keepalive_timer
.function
=sl_keepalive
;
829 init_timer(&sl
->outfill_timer
);
830 sl
->outfill_timer
.data
=(unsigned long)sl
;
831 sl
->outfill_timer
.function
=sl_outfill
;
839 * Open the high-level part of the SLIP channel.
840 * This function is called by the TTY module when the
841 * SLIP line discipline is called for. Because we are
842 * sure the tty line exists, we only have to link it to
843 * a free SLIP channel...
846 slip_open(struct tty_struct
*tty
)
851 if(!capable(CAP_NET_ADMIN
))
854 /* RTnetlink lock is misused here to serialize concurrent
855 opens of slip channels. There are better ways, but it is
860 /* Collect hanged up channels. */
863 sl
= (struct slip
*) tty
->disc_data
;
866 /* First make sure we're not already connected. */
867 if (sl
&& sl
->magic
== SLIP_MAGIC
)
870 /* OK. Find a free SLIP channel to use. */
872 if ((sl
= sl_alloc(tty
->device
)) == NULL
)
877 sl
->line
= tty
->device
;
878 sl
->pid
= current
->pid
;
879 if (tty
->driver
->flush_buffer
)
880 tty
->driver
->flush_buffer(tty
);
881 if (tty
->ldisc
.flush_buffer
)
882 tty
->ldisc
.flush_buffer(tty
);
884 if (!test_bit(SLF_INUSE
, &sl
->flags
)) {
885 /* Perform the low-level SLIP initialization. */
886 if ((err
= sl_alloc_bufs(sl
, SL_MTU
)) != 0)
889 set_bit(SLF_INUSE
, &sl
->flags
);
891 if ((err
= register_netdevice(sl
->dev
)))
895 #ifdef CONFIG_SLIP_SMART
897 sl
->keepalive_timer
.expires
=jiffies
+sl
->keepalive
*HZ
;
898 add_timer (&sl
->keepalive_timer
);
901 sl
->outfill_timer
.expires
=jiffies
+sl
->outfill
*HZ
;
902 add_timer (&sl
->outfill_timer
);
906 /* Done. We have linked the TTY line to a channel. */
908 return sl
->dev
->base_addr
;
915 tty
->disc_data
= NULL
;
916 clear_bit(SLF_INUSE
, &sl
->flags
);
921 /* Count references from TTY module */
926 Let me to blame a bit.
927 1. TTY module calls this funstion on soft interrupt.
928 2. TTY module calls this function WITH MASKED INTERRUPTS!
929 3. TTY module does not notify us about line discipline
932 Seems, now it is clean. The solution is to consider netdevice and
933 line discipline sides as two independent threads.
935 By-product (not desired): sl? does not feel hangups and remains open.
936 It is supposed, that user level program (dip, diald, slattach...)
937 will catch SIGHUP and make the rest of work.
939 I see no way to make more with current tty code. --ANK
943 * Close down a SLIP channel.
944 * This means flushing out any pending queues, and then restoring the
945 * TTY line discipline to what it was before it got hooked to SLIP
946 * (which usually is TTY again).
949 slip_close(struct tty_struct
*tty
)
951 struct slip
*sl
= (struct slip
*) tty
->disc_data
;
953 /* First make sure we're connected. */
954 if (!sl
|| sl
->magic
!= SLIP_MAGIC
|| sl
->tty
!= tty
)
962 /* VSV = very important to remove timers */
963 #ifdef CONFIG_SLIP_SMART
964 del_timer_sync(&sl
->keepalive_timer
);
965 del_timer_sync(&sl
->outfill_timer
);
968 /* Count references from TTY module */
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
;
1142 /* First make sure we're connected. */
1143 if (!sl
|| sl
->magic
!= SLIP_MAGIC
) {
1149 tmp
= strlen(sl
->dev
->name
) + 1;
1150 if (copy_to_user((void *)arg
, sl
->dev
->name
, tmp
))
1155 if (put_user(sl
->mode
, (int *)arg
))
1160 if (get_user(tmp
,(int *)arg
))
1162 #ifndef SL_INCLUDE_CSLIP
1163 if (tmp
& (SL_MODE_CSLIP
|SL_MODE_ADAPTIVE
)) {
1167 if ((tmp
& (SL_MODE_ADAPTIVE
| SL_MODE_CSLIP
)) ==
1168 (SL_MODE_ADAPTIVE
| SL_MODE_CSLIP
)) {
1169 /* return -EINVAL; */
1170 tmp
&= ~SL_MODE_ADAPTIVE
;
1173 #ifndef CONFIG_SLIP_MODE_SLIP6
1174 if (tmp
& SL_MODE_SLIP6
) {
1179 sl
->dev
->type
= ARPHRD_SLIP
+sl
->mode
;
1185 #ifdef CONFIG_SLIP_SMART
1186 /* VSV changes start here */
1187 case SIOCSKEEPALIVE
:
1188 if (get_user(tmp
,(int *)arg
))
1190 if (tmp
> 255) /* max for unchar */
1193 spin_lock_bh(&sl
->lock
);
1195 spin_unlock_bh(&sl
->lock
);
1198 if ((sl
->keepalive
= (unchar
) tmp
) != 0) {
1199 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1200 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1202 del_timer (&sl
->keepalive_timer
);
1204 spin_unlock_bh(&sl
->lock
);
1207 case SIOCGKEEPALIVE
:
1208 if (put_user(sl
->keepalive
, (int *)arg
))
1213 if (get_user(tmp
,(int *)arg
))
1215 if (tmp
> 255) /* max for unchar */
1217 spin_lock_bh(&sl
->lock
);
1219 spin_unlock_bh(&sl
->lock
);
1222 if ((sl
->outfill
= (unchar
) tmp
) != 0){
1223 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1224 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1226 del_timer (&sl
->outfill_timer
);
1228 spin_unlock_bh(&sl
->lock
);
1232 if (put_user(sl
->outfill
, (int *)arg
))
1235 /* VSV changes end */
1238 /* Allow stty to read, but not set, the serial port */
1241 return n_tty_ioctl(tty
, file
, cmd
, arg
);
1244 return -ENOIOCTLCMD
;
1248 /* VSV changes start here */
1249 #ifdef CONFIG_SLIP_SMART
1250 /* function do_ioctl called from net/core/dev.c
1251 to allow get/set outfill/keepalive parameter
1254 static int sl_ioctl(struct net_device
*dev
,struct ifreq
*rq
,int cmd
)
1256 struct slip
*sl
= (struct slip
*)(dev
->priv
);
1258 if (sl
== NULL
) /* Allocation failed ?? */
1261 spin_lock_bh(&sl
->lock
);
1264 spin_unlock_bh(&sl
->lock
);
1269 case SIOCSKEEPALIVE
:
1270 /* max for unchar */
1271 if (((unsigned int)((unsigned long)rq
->ifr_data
)) > 255) {
1272 spin_unlock_bh(&sl
->lock
);
1275 sl
->keepalive
= (unchar
) ((unsigned long)rq
->ifr_data
);
1276 if (sl
->keepalive
!= 0) {
1277 sl
->keepalive_timer
.expires
=jiffies
+sl
->keepalive
*HZ
;
1278 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1279 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1281 del_timer(&sl
->keepalive_timer
);
1285 case SIOCGKEEPALIVE
:
1286 rq
->ifr_data
=(caddr_t
)((unsigned long)sl
->keepalive
);
1290 if (((unsigned)((unsigned long)rq
->ifr_data
)) > 255) { /* max for unchar */
1291 spin_unlock_bh(&sl
->lock
);
1294 if ((sl
->outfill
= (unchar
)((unsigned long) rq
->ifr_data
)) != 0){
1295 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1296 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1298 del_timer (&sl
->outfill_timer
);
1303 rq
->ifr_data
=(caddr_t
)((unsigned long)sl
->outfill
);
1307 /* Resolve race condition, when ioctl'ing hanged up
1308 and opened by another process device.
1310 if (sl
->tty
!= current
->tty
&& sl
->pid
!= current
->pid
) {
1311 spin_unlock_bh(&sl
->lock
);
1315 if ((unsigned long)rq
->ifr_data
)
1320 rq
->ifr_data
=(caddr_t
)((unsigned long)sl
->leased
);
1322 spin_unlock_bh(&sl
->lock
);
1326 /* VSV changes end */
1328 static struct tty_ldisc sl_ldisc
= {
1329 .owner
= THIS_MODULE
,
1330 .magic
= TTY_LDISC_MAGIC
,
1333 .close
= slip_close
,
1334 .ioctl
= slip_ioctl
,
1335 .receive_buf
= slip_receive_buf
,
1336 .receive_room
= slip_receive_room
,
1337 .write_wakeup
= slip_write_wakeup
,
1340 static int __init
slip_init(void)
1344 if (slip_maxdev
< 4)
1345 slip_maxdev
= 4; /* Sanity */
1347 printk(KERN_INFO
"SLIP: version %s (dynamic channels, max=%d)"
1348 #ifdef CONFIG_SLIP_MODE_SLIP6
1349 " (6 bit encapsulation enabled)"
1352 SLIP_VERSION
, slip_maxdev
);
1353 #if defined(SL_INCLUDE_CSLIP)
1354 printk(KERN_INFO
"CSLIP: code copyright 1989 Regents of the University of California.\n");
1356 #ifdef CONFIG_SLIP_SMART
1357 printk(KERN_INFO
"SLIP linefill/keepalive option.\n");
1360 slip_devs
= kmalloc(sizeof(struct net_device
*)*slip_maxdev
, GFP_KERNEL
);
1362 printk(KERN_ERR
"SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1366 /* Clear the pointer array, we allocate devices when we need them */
1367 memset(slip_devs
, 0, sizeof(struct net_device
*)*slip_maxdev
);
1369 /* Fill in our line protocol discipline, and register it */
1370 if ((status
= tty_register_ldisc(N_SLIP
, &sl_ldisc
)) != 0) {
1371 printk(KERN_ERR
"SLIP: can't register line discipline (err = %d)\n", status
);
1376 static void __exit
slip_exit(void)
1379 struct net_device
*dev
;
1381 unsigned long timeout
= jiffies
+ HZ
;
1384 if (slip_devs
== NULL
)
1387 /* First of all: check for active disciplines and hangup them.
1391 current
->state
= TASK_INTERRUPTIBLE
;
1392 schedule_timeout(HZ
/ 10);
1393 current
->state
= TASK_RUNNING
;
1397 for (i
= 0; i
< slip_maxdev
; i
++) {
1402 spin_lock_bh(&sl
->lock
);
1405 tty_hangup(sl
->tty
);
1407 spin_unlock_bh(&sl
->lock
);
1409 } while (busy
&& time_before(jiffies
, timeout
));
1412 for (i
= 0; i
< slip_maxdev
; i
++) {
1416 slip_devs
[i
] = NULL
;
1420 printk(KERN_ERR
"%s: tty discipline still running\n",
1422 /* Intentionally leak the control block. */
1423 dev
->destructor
= NULL
;
1426 unregister_netdev(dev
);
1432 if ((i
= tty_register_ldisc(N_SLIP
, NULL
)))
1434 printk(KERN_ERR
"SLIP: can't unregister line discipline (err = %d)\n", i
);
1438 module_init(slip_init
);
1439 module_exit(slip_exit
);
1441 #ifdef CONFIG_SLIP_SMART
1443 * This is start of the code for multislip style line checking
1444 * added by Stanislav Voronyi. All changes before marked VSV
1447 static void sl_outfill(unsigned long sls
)
1449 struct slip
*sl
=(struct slip
*)sls
;
1451 spin_lock(&sl
->lock
);
1453 if (sl
->tty
== NULL
)
1458 if( test_bit(SLF_OUTWAIT
, &sl
->flags
) )
1460 /* no packets were transmitted, do outfill */
1461 #ifdef CONFIG_SLIP_MODE_SLIP6
1462 unsigned char s
= (sl
->mode
& SL_MODE_SLIP6
)?0x70:END
;
1464 unsigned char s
= END
;
1466 /* put END into tty queue. Is it right ??? */
1467 if (!netif_queue_stopped(sl
->dev
))
1469 /* if device busy no outfill */
1470 sl
->tty
->driver
->write(sl
->tty
, 0, &s
, 1);
1474 set_bit(SLF_OUTWAIT
, &sl
->flags
);
1476 mod_timer(&sl
->outfill_timer
, jiffies
+sl
->outfill
*HZ
);
1479 spin_unlock(&sl
->lock
);
1482 static void sl_keepalive(unsigned long sls
)
1484 struct slip
*sl
=(struct slip
*)sls
;
1486 spin_lock(&sl
->lock
);
1488 if (sl
->tty
== NULL
)
1493 if(test_bit(SLF_KEEPTEST
, &sl
->flags
))
1495 /* keepalive still high :(, we must hangup */
1496 if( sl
->outfill
) /* outfill timer must be deleted too */
1497 (void)del_timer(&sl
->outfill_timer
);
1498 printk(KERN_DEBUG
"%s: no packets received during keepalive timeout, hangup.\n", sl
->dev
->name
);
1499 tty_hangup(sl
->tty
); /* this must hangup tty & close slip */
1500 /* I think we need not something else */
1504 set_bit(SLF_KEEPTEST
, &sl
->flags
);
1506 mod_timer(&sl
->keepalive_timer
, jiffies
+sl
->keepalive
*HZ
);
1510 spin_unlock(&sl
->lock
);
1514 MODULE_LICENSE("GPL");