[PATCH] DVB core update
[linux-2.6/history.git] / drivers / net / slip.c
blob5944346cc267da912172760f93ba8807d225d3ec
1 /*
2 * slip.c This module implements the SLIP protocol for kernel-based
3 * devices like TTY. It interfaces between a raw TTY, and the
4 * kernel's INET protocol layers.
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>
11 * Fixes:
12 * Alan Cox : Sanity checks and avoid tx overruns.
13 * Has a new sl->mtu field.
14 * Alan Cox : Found cause of overrun. ifconfig sl0 mtu upwards.
15 * Driver now spots this and grows/shrinks its buffers(hack!).
16 * Memory leak if you run out of memory setting up a slip driver fixed.
17 * Matt Dillon : Printable slip (borrowed from NET2E)
18 * Pauline Middelink : Slip driver fixes.
19 * Alan Cox : Honours the old SL_COMPRESSED flag
20 * Alan Cox : KISS AX.25 and AXUI IP support
21 * Michael Riepe : Automatic CSLIP recognition added
22 * Charles Hedrick : CSLIP header length problem fix.
23 * Alan Cox : Corrected non-IP cases of the above.
24 * Alan Cox : Now uses hardware type as per FvK.
25 * Alan Cox : Default to 192.168.0.0 (RFC 1597)
26 * A.N.Kuznetsov : dev_tint() recursion fix.
27 * Dmitry Gorodchanin : SLIP memory leaks
28 * Dmitry Gorodchanin : Code cleanup. Reduce tty driver
29 * buffering from 4096 to 256 bytes.
30 * Improving SLIP response time.
31 * CONFIG_SLIP_MODE_SLIP6.
32 * ifconfig sl? up & down now works correctly.
33 * Modularization.
34 * Alan Cox : Oops - fix AX.25 buffer lengths
35 * Dmitry Gorodchanin : Even more cleanups. Preserve CSLIP
36 * statistics. Include CSLIP code only
37 * if it really needed.
38 * Alan Cox : Free slhc buffers in the right place.
39 * Alan Cox : Allow for digipeated IP over AX.25
40 * Matti Aarnio : Dynamic SLIP devices, with ideas taken
41 * from Jim Freeman's <jfree@caldera.com>
42 * dynamic PPP devices. We do NOT kfree()
43 * device entries, just reg./unreg. them
44 * as they are needed. We kfree() them
45 * at module cleanup.
46 * With MODULE-loading ``insmod'', user can
47 * issue parameter: slip_maxdev=1024
48 * (Or how much he/she wants.. Default is 256)
49 * * Stanislav Voronyi : Slip line checking, with ideas taken
50 * from multislip BSDI driver which was written
51 * by Igor Chechik, RELCOM Corp. Only algorithms
52 * have been ported to Linux SLIP driver.
53 * Vitaly E. Lavrov : Sane behaviour on tty hangup.
54 * Alexey Kuznetsov : Cleanup interfaces to tty&netdevice modules.
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>
65 #include <linux/mm.h>
66 #include <linux/interrupt.h>
67 #include <linux/in.h>
68 #include <linux/tty.h>
69 #include <linux/errno.h>
70 #include <linux/netdevice.h>
71 #include <linux/etherdevice.h>
72 #include <linux/skbuff.h>
73 #include <linux/rtnetlink.h>
74 #include <linux/if_arp.h>
75 #include <linux/if_slip.h>
76 #include <linux/init.h>
77 #include "slip.h"
78 #ifdef CONFIG_INET
79 #include <linux/ip.h>
80 #include <linux/tcp.h>
81 #include <net/slhc_vj.h>
82 #endif
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);
97 #endif
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);
102 #endif
104 /********************************
105 * Buffer administration routines:
106 * sl_alloc_bufs()
107 * sl_free_bufs()
108 * sl_realloc_bufs()
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.
119 static int
120 sl_alloc_bufs(struct slip *sl, int mtu)
122 int err = -ENOBUFS;
123 unsigned long len;
124 char * rbuff = NULL;
125 char * xbuff = NULL;
126 #ifdef SL_INCLUDE_CSLIP
127 char * cbuff = NULL;
128 struct slcompress *slcomp = NULL;
129 #endif
132 * Allocate the SLIP frame buffers:
134 * rbuff Receive buffer.
135 * xbuff Transmit buffer.
136 * cbuff Temporary compression buffer.
138 len = mtu * 2;
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
143 * an MSS of 128
145 if (len < 576 * 2)
146 len = 576 * 2;
147 rbuff = kmalloc(len + 4, GFP_KERNEL);
148 if (rbuff == NULL)
149 goto err_exit;
150 xbuff = kmalloc(len + 4, GFP_KERNEL);
151 if (xbuff == NULL)
152 goto err_exit;
153 #ifdef SL_INCLUDE_CSLIP
154 cbuff = kmalloc(len + 4, GFP_KERNEL);
155 if (cbuff == NULL)
156 goto err_exit;
157 slcomp = slhc_init(16, 16);
158 if (slcomp == NULL)
159 goto err_exit;
160 #endif
161 spin_lock_bh(&sl->lock);
162 if (sl->tty == NULL) {
163 spin_unlock_bh(&sl->lock);
164 err = -ENODEV;
165 goto err_exit;
167 sl->mtu = mtu;
168 sl->buffsize = len;
169 sl->rcount = 0;
170 sl->xleft = 0;
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
177 sl->xdata = 0;
178 sl->xbits = 0;
179 #endif
180 #endif
181 spin_unlock_bh(&sl->lock);
182 err = 0;
184 /* Cleanup */
185 err_exit:
186 #ifdef SL_INCLUDE_CSLIP
187 if (cbuff)
188 kfree(cbuff);
189 if (slcomp)
190 slhc_free(slcomp);
191 #endif
192 if (xbuff)
193 kfree(xbuff);
194 if (rbuff)
195 kfree(rbuff);
196 return err;
199 /* Free a SLIP channel buffers. */
200 static void
201 sl_free_bufs(struct slip *sl)
203 void * tmp;
205 /* Free all SLIP frame buffers. */
206 if ((tmp = xchg(&sl->rbuff, NULL)) != NULL)
207 kfree(tmp);
208 if ((tmp = xchg(&sl->xbuff, NULL)) != NULL)
209 kfree(tmp);
210 #ifdef SL_INCLUDE_CSLIP
211 if ((tmp = xchg(&sl->cbuff, NULL)) != NULL)
212 kfree(tmp);
213 if ((tmp = xchg(&sl->slcomp, NULL)) != NULL)
214 slhc_free(tmp);
215 #endif
219 Reallocate slip channel buffers.
222 static int sl_realloc_bufs(struct slip *sl, int mtu)
224 int err = 0;
225 struct net_device *dev = sl->dev;
226 unsigned char *xbuff, *rbuff;
227 #ifdef SL_INCLUDE_CSLIP
228 unsigned char *cbuff;
229 #endif
230 int len = mtu * 2;
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
235 * an MSS of 128
237 if (len < 576 * 2)
238 len = 576 * 2;
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);
244 #endif
247 #ifdef SL_INCLUDE_CSLIP
248 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
249 #else
250 if (xbuff == NULL || rbuff == NULL) {
251 #endif
252 if (mtu >= sl->mtu) {
253 printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n",
254 dev->name);
255 err = -ENOBUFS;
257 goto done;
260 spin_lock_bh(&sl->lock);
262 err = -ENODEV;
263 if (sl->tty == NULL)
264 goto done_on_bh;
266 xbuff = xchg(&sl->xbuff, xbuff);
267 rbuff = xchg(&sl->rbuff, rbuff);
268 #ifdef SL_INCLUDE_CSLIP
269 cbuff = xchg(&sl->cbuff, cbuff);
270 #endif
271 if (sl->xleft) {
272 if (sl->xleft <= len) {
273 memcpy(sl->xbuff, sl->xhead, sl->xleft);
274 } else {
275 sl->xleft = 0;
276 sl->tx_dropped++;
279 sl->xhead = sl->xbuff;
281 if (sl->rcount) {
282 if (sl->rcount <= len) {
283 memcpy(sl->rbuff, rbuff, sl->rcount);
284 } else {
285 sl->rcount = 0;
286 sl->rx_over_errors++;
287 set_bit(SLF_ERROR, &sl->flags);
290 sl->mtu = mtu;
291 dev->mtu = mtu;
292 sl->buffsize = len;
293 err = 0;
295 done_on_bh:
296 spin_unlock_bh(&sl->lock);
298 done:
299 if (xbuff)
300 kfree(xbuff);
301 if (rbuff)
302 kfree(rbuff);
303 #ifdef SL_INCLUDE_CSLIP
304 if (cbuff)
305 kfree(cbuff);
306 #endif
307 return err;
311 /* Set the "sending" flag. This must be atomic hence the set_bit. */
312 static inline void
313 sl_lock(struct slip *sl)
315 netif_stop_queue(sl->dev);
319 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
320 static inline void
321 sl_unlock(struct slip *sl)
323 netif_wake_queue(sl->dev);
326 /* Send one completely decapsulated IP datagram to the IP layer. */
327 static void
328 sl_bump(struct slip *sl)
330 struct sk_buff *skb;
331 int count;
333 count = sl->rcount;
334 #ifdef SL_INCLUDE_CSLIP
335 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
336 unsigned char c;
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);
341 return;
343 /* make sure we've reserved enough space for uncompress to use */
344 if (count + 80 > sl->buffsize) {
345 sl->rx_over_errors++;
346 return;
348 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
349 if (count <= 0) {
350 return;
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) {
361 return;
365 #endif /* SL_INCLUDE_CSLIP */
367 sl->rx_bytes+=count;
369 skb = dev_alloc_skb(count);
370 if (skb == NULL) {
371 printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", sl->dev->name);
372 sl->rx_dropped++;
373 return;
375 skb->dev = sl->dev;
376 memcpy(skb_put(skb,count), sl->rbuff, count);
377 skb->mac.raw=skb->data;
378 skb->protocol=htons(ETH_P_IP);
379 netif_rx(skb);
380 sl->dev->last_rx = jiffies;
381 sl->rx_packets++;
384 /* Encapsulate one IP datagram and stuff into a TTY queue. */
385 static void
386 sl_encaps(struct slip *sl, unsigned char *icp, int len)
388 unsigned char *p;
389 int actual, count;
391 if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */
392 printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name);
393 sl->tx_dropped++;
394 sl_unlock(sl);
395 return;
398 p = icp;
399 #ifdef SL_INCLUDE_CSLIP
400 if (sl->mode & SL_MODE_CSLIP) {
401 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
403 #endif
404 #ifdef CONFIG_SLIP_MODE_SLIP6
405 if(sl->mode & SL_MODE_SLIP6)
406 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
407 else
408 #endif
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;
423 #endif
424 sl->xleft = count - actual;
425 sl->xhead = sl->xbuff + actual;
426 #ifdef CONFIG_SLIP_SMART
427 /* VSV */
428 clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
429 #endif
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)
438 int actual;
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)) {
443 return;
445 if (sl->xleft <= 0) {
446 /* Now serial buffer is almost free & we can start
447 * transmission of another packet */
448 sl->tx_packets++;
449 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
450 sl_unlock(sl);
451 return;
454 actual = tty->driver->write(tty, 0, sl->xhead, sl->xleft);
455 sl->xleft -= actual;
456 sl->xhead += actual;
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))
469 goto out;
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 */
477 goto out;
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");
482 sl->xleft = 0;
483 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
484 sl_unlock(sl);
485 #endif
488 out:
489 spin_unlock(&sl->lock);
493 /* Encapsulate an IP datagram and kick it into a TTY queue. */
494 static int
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);
503 dev_kfree_skb(skb);
504 return 0;
506 if (sl->tty == NULL) {
507 spin_unlock(&sl->lock);
508 dev_kfree_skb(skb);
509 return 0;
512 sl_lock(sl);
513 sl->tx_bytes+=skb->len;
514 sl_encaps(sl, skb->data, skb->len);
515 spin_unlock(&sl->lock);
517 dev_kfree_skb(skb);
518 return 0;
522 /******************************************
523 * Routines looking at netdevice side.
524 ******************************************/
526 /* Netdevice UP -> DOWN routine */
528 static int
529 sl_close(struct net_device *dev)
531 struct slip *sl = (struct slip*)(dev->priv);
533 spin_lock_bh(&sl->lock);
534 if (sl->tty) {
535 /* TTY discipline is running. */
536 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
538 netif_stop_queue(dev);
539 sl->rcount = 0;
540 sl->xleft = 0;
541 spin_unlock_bh(&sl->lock);
543 return 0;
546 /* Netdevice DOWN -> UP routine */
548 static int sl_open(struct net_device *dev)
550 struct slip *sl = (struct slip*)(dev->priv);
552 if (sl->tty==NULL)
553 return -ENODEV;
555 sl->flags &= (1 << SLF_INUSE);
556 netif_start_queue(dev);
557 return 0;
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)
567 return -EINVAL;
569 if (new_mtu != dev->mtu)
570 return sl_realloc_bufs(sl, new_mtu);
571 return 0;
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;
583 #endif
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;
600 comp = sl->slcomp;
601 if (comp) {
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 */
608 return (&stats);
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.
621 dev->mtu = sl->mtu;
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;
626 #endif
627 return 0;
631 static void sl_uninit(struct net_device *dev)
633 struct slip *sl = (struct slip*)(dev->priv);
635 sl_free_bufs(sl);
638 static void sl_setup(struct net_device *dev)
640 dev->init = sl_init;
641 dev->uninit = sl_uninit;
642 dev->open = sl_open;
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;
650 #endif
651 dev->hard_header_len = 0;
652 dev->addr_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))
684 return;
686 /* Read the characters out of the buffer */
687 while (count--) {
688 if (fp && *fp++) {
689 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
690 sl->rx_errors++;
692 cp++;
693 continue;
695 #ifdef CONFIG_SLIP_MODE_SLIP6
696 if (sl->mode & SL_MODE_SLIP6)
697 slip_unesc6(sl, *cp++);
698 else
699 #endif
700 slip_unesc(sl, *cp++);
704 /************************************
705 * slip_open helper routines.
706 ************************************/
708 /* Collect hanged up channels */
710 static void sl_sync(void)
712 int i;
713 struct net_device *dev;
714 struct slip *sl;
716 for (i = 0; i < slip_maxdev; i++) {
717 if ((dev = slip_devs[i]) == NULL)
718 break;
720 sl = dev->priv;
721 if (sl->tty || sl->leased)
722 continue;
723 if (dev->flags&IFF_UP)
724 dev_close(dev);
729 /* Find a free SLIP channel, and link in this `tty' line. */
730 static struct slip *
731 sl_alloc(dev_t line)
733 int i;
734 int sel = -1;
735 int score = -1;
736 struct net_device *dev = NULL;
737 struct slip *sl;
739 if (slip_devs == NULL)
740 return NULL; /* Master array missing ! */
742 for (i = 0; i < slip_maxdev; i++) {
743 dev = slip_devs[i];
744 if (dev == NULL)
745 break;
747 sl = dev->priv;
748 if (sl->leased) {
749 if (sl->line != line)
750 continue;
751 if (sl->tty)
752 return NULL;
754 /* Clear ESCAPE & ERROR flags */
755 sl->flags &= (1 << SLF_INUSE);
756 return sl;
759 if (sl->tty)
760 continue;
762 if (current->pid == sl->pid) {
763 if (sl->line == line && score < 3) {
764 sel = i;
765 score = 3;
766 continue;
768 if (score < 2) {
769 sel = i;
770 score = 2;
772 continue;
774 if (sl->line == line && score < 1) {
775 sel = i;
776 score = 1;
777 continue;
779 if (score < 0) {
780 sel = i;
781 score = 0;
785 if (sel >= 0) {
786 i = sel;
787 dev = slip_devs[i];
788 if (score > 1) {
789 sl = dev->priv;
790 sl->flags &= (1 << SLF_INUSE);
791 return sl;
795 /* Sorry, too many, all slots in use */
796 if (i >= slip_maxdev)
797 return NULL;
799 if (dev) {
800 sl = dev->priv;
801 if (test_bit(SLF_INUSE, &sl->flags)) {
802 unregister_netdevice(dev);
803 dev = NULL;
804 slip_devs[i] = NULL;
808 if (!dev) {
809 char name[IFNAMSIZ];
810 sprintf(name, "sl%d", i);
812 dev = alloc_netdev(sizeof(*sl), name, sl_setup);
813 if (!dev)
814 return NULL;
815 dev->base_addr = i;
818 sl = dev->priv;
820 /* Initialize channel control data */
821 sl->magic = SLIP_MAGIC;
822 sl->dev = dev;
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;
832 #endif
833 slip_devs[i] = dev;
835 return sl;
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...
845 static int
846 slip_open(struct tty_struct *tty)
848 struct slip *sl;
849 int err;
851 if(!capable(CAP_NET_ADMIN))
852 return -EPERM;
854 /* RTnetlink lock is misused here to serialize concurrent
855 opens of slip channels. There are better ways, but it is
856 the simplest one.
858 rtnl_lock();
860 /* Collect hanged up channels. */
861 sl_sync();
863 sl = (struct slip *) tty->disc_data;
865 err = -EEXIST;
866 /* First make sure we're not already connected. */
867 if (sl && sl->magic == SLIP_MAGIC)
868 goto err_exit;
870 /* OK. Find a free SLIP channel to use. */
871 err = -ENFILE;
872 if ((sl = sl_alloc(tty->device)) == NULL)
873 goto err_exit;
875 sl->tty = tty;
876 tty->disc_data = sl;
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)
887 goto err_free_chan;
889 set_bit(SLF_INUSE, &sl->flags);
891 if ((err = register_netdevice(sl->dev)))
892 goto err_free_bufs;
895 #ifdef CONFIG_SLIP_SMART
896 if (sl->keepalive) {
897 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
898 add_timer (&sl->keepalive_timer);
900 if (sl->outfill) {
901 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
902 add_timer (&sl->outfill_timer);
904 #endif
906 /* Done. We have linked the TTY line to a channel. */
907 rtnl_unlock();
908 return sl->dev->base_addr;
910 err_free_bufs:
911 sl_free_bufs(sl);
913 err_free_chan:
914 sl->tty = NULL;
915 tty->disc_data = NULL;
916 clear_bit(SLF_INUSE, &sl->flags);
918 err_exit:
919 rtnl_unlock();
921 /* Count references from TTY module */
922 return err;
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
930 shutdown,
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).
948 static void
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)
955 return;
957 tty->disc_data = 0;
958 sl->tty = NULL;
959 if (!sl->leased)
960 sl->line = 0;
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);
966 #endif
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;
979 unsigned char c;
982 * Send an initial END character to flush out any
983 * data that may have accumulated in the receiver
984 * due to line noise.
987 *ptr++ = END;
990 * For each byte in the packet, send the appropriate
991 * character sequence, according to the SLIP protocol.
994 while (len-- > 0) {
995 switch(c = *s++) {
996 case END:
997 *ptr++ = ESC;
998 *ptr++ = ESC_END;
999 break;
1000 case ESC:
1001 *ptr++ = ESC;
1002 *ptr++ = ESC_ESC;
1003 break;
1004 default:
1005 *ptr++ = c;
1006 break;
1009 *ptr++ = END;
1010 return (ptr - d);
1013 static void slip_unesc(struct slip *sl, unsigned char s)
1016 switch(s) {
1017 case END:
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);
1022 #endif
1024 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1025 sl_bump(sl);
1027 clear_bit(SLF_ESCAPE, &sl->flags);
1028 sl->rcount = 0;
1029 return;
1031 case ESC:
1032 set_bit(SLF_ESCAPE, &sl->flags);
1033 return;
1034 case ESC_ESC:
1035 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1036 s = ESC;
1038 break;
1039 case ESC_END:
1040 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1041 s = END;
1043 break;
1045 if (!test_bit(SLF_ERROR, &sl->flags)) {
1046 if (sl->rcount < sl->buffsize) {
1047 sl->rbuff[sl->rcount++] = s;
1048 return;
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;
1065 unsigned char c;
1066 int i;
1067 unsigned short v = 0;
1068 short bits = 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.
1076 *ptr++ = 0x70;
1079 * Encode the packet into printable ascii characters
1082 for (i = 0; i < len; ++i) {
1083 v = (v << 8) | s[i];
1084 bits += 8;
1085 while (bits >= 6) {
1086 bits -= 6;
1087 c = 0x30 + ((v >> bits) & 0x3F);
1088 *ptr++ = c;
1091 if (bits) {
1092 c = 0x30 + ((v << (6 - bits)) & 0x3F);
1093 *ptr++ = c;
1095 *ptr++ = 0x70;
1096 return ptr - d;
1099 void
1100 slip_unesc6(struct slip *sl, unsigned char s)
1102 unsigned char c;
1104 if (s == 0x70) {
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);
1109 #endif
1111 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1112 sl_bump(sl);
1114 sl->rcount = 0;
1115 sl->xbits = 0;
1116 sl->xdata = 0;
1117 } else if (s >= 0x30 && s < 0x70) {
1118 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1119 sl->xbits += 6;
1120 if (sl->xbits >= 8) {
1121 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;
1126 return;
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;
1140 unsigned int tmp;
1142 /* First make sure we're connected. */
1143 if (!sl || sl->magic != SLIP_MAGIC) {
1144 return -EINVAL;
1147 switch(cmd) {
1148 case SIOCGIFNAME:
1149 tmp = strlen(sl->dev->name) + 1;
1150 if (copy_to_user((void *)arg, sl->dev->name, tmp))
1151 return -EFAULT;
1152 return 0;
1154 case SIOCGIFENCAP:
1155 if (put_user(sl->mode, (int *)arg))
1156 return -EFAULT;
1157 return 0;
1159 case SIOCSIFENCAP:
1160 if (get_user(tmp,(int *)arg))
1161 return -EFAULT;
1162 #ifndef SL_INCLUDE_CSLIP
1163 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1164 return -EINVAL;
1166 #else
1167 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1168 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1169 /* return -EINVAL; */
1170 tmp &= ~SL_MODE_ADAPTIVE;
1172 #endif
1173 #ifndef CONFIG_SLIP_MODE_SLIP6
1174 if (tmp & SL_MODE_SLIP6) {
1175 return -EINVAL;
1177 #endif
1178 sl->mode = tmp;
1179 sl->dev->type = ARPHRD_SLIP+sl->mode;
1180 return 0;
1182 case SIOCSIFHWADDR:
1183 return -EINVAL;
1185 #ifdef CONFIG_SLIP_SMART
1186 /* VSV changes start here */
1187 case SIOCSKEEPALIVE:
1188 if (get_user(tmp,(int *)arg))
1189 return -EFAULT;
1190 if (tmp > 255) /* max for unchar */
1191 return -EINVAL;
1193 spin_lock_bh(&sl->lock);
1194 if (!sl->tty) {
1195 spin_unlock_bh(&sl->lock);
1196 return -ENODEV;
1198 if ((sl->keepalive = (unchar) tmp) != 0) {
1199 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1200 set_bit(SLF_KEEPTEST, &sl->flags);
1201 } else {
1202 del_timer (&sl->keepalive_timer);
1204 spin_unlock_bh(&sl->lock);
1205 return 0;
1207 case SIOCGKEEPALIVE:
1208 if (put_user(sl->keepalive, (int *)arg))
1209 return -EFAULT;
1210 return 0;
1212 case SIOCSOUTFILL:
1213 if (get_user(tmp,(int *)arg))
1214 return -EFAULT;
1215 if (tmp > 255) /* max for unchar */
1216 return -EINVAL;
1217 spin_lock_bh(&sl->lock);
1218 if (!sl->tty) {
1219 spin_unlock_bh(&sl->lock);
1220 return -ENODEV;
1222 if ((sl->outfill = (unchar) tmp) != 0){
1223 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1224 set_bit(SLF_OUTWAIT, &sl->flags);
1225 } else {
1226 del_timer (&sl->outfill_timer);
1228 spin_unlock_bh(&sl->lock);
1229 return 0;
1231 case SIOCGOUTFILL:
1232 if (put_user(sl->outfill, (int *)arg))
1233 return -EFAULT;
1234 return 0;
1235 /* VSV changes end */
1236 #endif
1238 /* Allow stty to read, but not set, the serial port */
1239 case TCGETS:
1240 case TCGETA:
1241 return n_tty_ioctl(tty, file, cmd, arg);
1243 default:
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
1252 by ifconfig */
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 ?? */
1259 return -ENODEV;
1261 spin_lock_bh(&sl->lock);
1263 if (!sl->tty) {
1264 spin_unlock_bh(&sl->lock);
1265 return -ENODEV;
1268 switch(cmd){
1269 case SIOCSKEEPALIVE:
1270 /* max for unchar */
1271 if (((unsigned int)((unsigned long)rq->ifr_data)) > 255) {
1272 spin_unlock_bh(&sl->lock);
1273 return -EINVAL;
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);
1280 } else {
1281 del_timer(&sl->keepalive_timer);
1283 break;
1285 case SIOCGKEEPALIVE:
1286 rq->ifr_data=(caddr_t)((unsigned long)sl->keepalive);
1287 break;
1289 case SIOCSOUTFILL:
1290 if (((unsigned)((unsigned long)rq->ifr_data)) > 255) { /* max for unchar */
1291 spin_unlock_bh(&sl->lock);
1292 return -EINVAL;
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);
1297 } else {
1298 del_timer (&sl->outfill_timer);
1300 break;
1302 case SIOCGOUTFILL:
1303 rq->ifr_data=(caddr_t)((unsigned long)sl->outfill);
1304 break;
1306 case SIOCSLEASE:
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);
1312 return -EPERM;
1314 sl->leased = 0;
1315 if ((unsigned long)rq->ifr_data)
1316 sl->leased = 1;
1317 break;
1319 case SIOCGLEASE:
1320 rq->ifr_data=(caddr_t)((unsigned long)sl->leased);
1322 spin_unlock_bh(&sl->lock);
1323 return 0;
1325 #endif
1326 /* VSV changes end */
1328 static struct tty_ldisc sl_ldisc = {
1329 .owner = THIS_MODULE,
1330 .magic = TTY_LDISC_MAGIC,
1331 .name = "slip",
1332 .open = slip_open,
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)
1342 int status;
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)"
1350 #endif
1351 ".\n",
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");
1355 #endif
1356 #ifdef CONFIG_SLIP_SMART
1357 printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1358 #endif
1360 slip_devs = kmalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL);
1361 if (!slip_devs) {
1362 printk(KERN_ERR "SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1363 return -ENOMEM;
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);
1373 return status;
1376 static void __exit slip_exit(void)
1378 int i;
1379 struct net_device *dev;
1380 struct slip *sl;
1381 unsigned long timeout = jiffies + HZ;
1382 int busy = 0;
1384 if (slip_devs == NULL)
1385 return;
1387 /* First of all: check for active disciplines and hangup them.
1389 do {
1390 if (busy) {
1391 current->state = TASK_INTERRUPTIBLE;
1392 schedule_timeout(HZ / 10);
1393 current->state = TASK_RUNNING;
1396 busy = 0;
1397 for (i = 0; i < slip_maxdev; i++) {
1398 dev = slip_devs[i];
1399 if (!dev)
1400 continue;
1401 sl = dev->priv;
1402 spin_lock_bh(&sl->lock);
1403 if (sl->tty) {
1404 busy++;
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++) {
1413 dev = slip_devs[i];
1414 if (!dev)
1415 continue;
1416 slip_devs[i] = NULL;
1418 sl = dev->priv;
1419 if (sl->tty) {
1420 printk(KERN_ERR "%s: tty discipline still running\n",
1421 dev->name);
1422 /* Intentionally leak the control block. */
1423 dev->destructor = NULL;
1426 unregister_netdev(dev);
1429 kfree(slip_devs);
1430 slip_devs = NULL;
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)
1454 goto out;
1456 if(sl->outfill)
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;
1463 #else
1464 unsigned char s = END;
1465 #endif
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);
1473 else
1474 set_bit(SLF_OUTWAIT, &sl->flags);
1476 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1478 out:
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)
1489 goto out;
1491 if( sl->keepalive)
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 */
1501 goto out;
1503 else
1504 set_bit(SLF_KEEPTEST, &sl->flags);
1506 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1509 out:
1510 spin_unlock(&sl->lock);
1513 #endif
1514 MODULE_LICENSE("GPL");