pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / net / slip.c
blob335b7cc80ebaf3aaf430af776f08d716dd215dbf
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/module.h>
59 #include <linux/moduleparam.h>
61 #include <asm/system.h>
62 #include <asm/uaccess.h>
63 #include <linux/bitops.h>
64 #include <linux/string.h>
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/delay.h>
77 #include <linux/init.h>
78 #include "slip.h"
79 #ifdef CONFIG_INET
80 #include <linux/ip.h>
81 #include <linux/tcp.h>
82 #include <net/slhc_vj.h>
83 #endif
85 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
87 static struct net_device **slip_devs;
89 static int slip_maxdev = SL_NRUNIT;
90 module_param(slip_maxdev, int, 0);
91 MODULE_PARM_DESC(slip_maxdev, "Maximum number of slip devices");
93 static int slip_esc(unsigned char *p, unsigned char *d, int len);
94 static void slip_unesc(struct slip *sl, unsigned char c);
95 #ifdef CONFIG_SLIP_MODE_SLIP6
96 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
97 static void slip_unesc6(struct slip *sl, unsigned char c);
98 #endif
99 #ifdef CONFIG_SLIP_SMART
100 static void sl_keepalive(unsigned long sls);
101 static void sl_outfill(unsigned long sls);
102 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd);
103 #endif
105 /********************************
106 * Buffer administration routines:
107 * sl_alloc_bufs()
108 * sl_free_bufs()
109 * sl_realloc_bufs()
111 * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
112 * sl_realloc_bufs provides strong atomicity and reallocation
113 * on actively running device.
114 *********************************/
117 Allocate channel buffers.
120 static int
121 sl_alloc_bufs(struct slip *sl, int mtu)
123 int err = -ENOBUFS;
124 unsigned long len;
125 char * rbuff = NULL;
126 char * xbuff = NULL;
127 #ifdef SL_INCLUDE_CSLIP
128 char * cbuff = NULL;
129 struct slcompress *slcomp = NULL;
130 #endif
133 * Allocate the SLIP frame buffers:
135 * rbuff Receive buffer.
136 * xbuff Transmit buffer.
137 * cbuff Temporary compression buffer.
139 len = mtu * 2;
142 * allow for arrival of larger UDP packets, even if we say not to
143 * also fixes a bug in which SunOS sends 512-byte packets even with
144 * an MSS of 128
146 if (len < 576 * 2)
147 len = 576 * 2;
148 rbuff = kmalloc(len + 4, GFP_KERNEL);
149 if (rbuff == NULL)
150 goto err_exit;
151 xbuff = kmalloc(len + 4, GFP_KERNEL);
152 if (xbuff == NULL)
153 goto err_exit;
154 #ifdef SL_INCLUDE_CSLIP
155 cbuff = kmalloc(len + 4, GFP_KERNEL);
156 if (cbuff == NULL)
157 goto err_exit;
158 slcomp = slhc_init(16, 16);
159 if (slcomp == NULL)
160 goto err_exit;
161 #endif
162 spin_lock_bh(&sl->lock);
163 if (sl->tty == NULL) {
164 spin_unlock_bh(&sl->lock);
165 err = -ENODEV;
166 goto err_exit;
168 sl->mtu = mtu;
169 sl->buffsize = len;
170 sl->rcount = 0;
171 sl->xleft = 0;
172 rbuff = xchg(&sl->rbuff, rbuff);
173 xbuff = xchg(&sl->xbuff, xbuff);
174 #ifdef SL_INCLUDE_CSLIP
175 cbuff = xchg(&sl->cbuff, cbuff);
176 slcomp = xchg(&sl->slcomp, slcomp);
177 #ifdef CONFIG_SLIP_MODE_SLIP6
178 sl->xdata = 0;
179 sl->xbits = 0;
180 #endif
181 #endif
182 spin_unlock_bh(&sl->lock);
183 err = 0;
185 /* Cleanup */
186 err_exit:
187 #ifdef SL_INCLUDE_CSLIP
188 kfree(cbuff);
189 if (slcomp)
190 slhc_free(slcomp);
191 #endif
192 kfree(xbuff);
193 kfree(rbuff);
194 return err;
197 /* Free a SLIP channel buffers. */
198 static void
199 sl_free_bufs(struct slip *sl)
201 /* Free all SLIP frame buffers. */
202 kfree(xchg(&sl->rbuff, NULL));
203 kfree(xchg(&sl->xbuff, NULL));
204 #ifdef SL_INCLUDE_CSLIP
205 kfree(xchg(&sl->cbuff, NULL));
206 slhc_free(xchg(&sl->slcomp, NULL));
207 #endif
211 Reallocate slip channel buffers.
214 static int sl_realloc_bufs(struct slip *sl, int mtu)
216 int err = 0;
217 struct net_device *dev = sl->dev;
218 unsigned char *xbuff, *rbuff;
219 #ifdef SL_INCLUDE_CSLIP
220 unsigned char *cbuff;
221 #endif
222 int len = mtu * 2;
225 * allow for arrival of larger UDP packets, even if we say not to
226 * also fixes a bug in which SunOS sends 512-byte packets even with
227 * an MSS of 128
229 if (len < 576 * 2)
230 len = 576 * 2;
232 xbuff = kmalloc(len + 4, GFP_ATOMIC);
233 rbuff = kmalloc(len + 4, GFP_ATOMIC);
234 #ifdef SL_INCLUDE_CSLIP
235 cbuff = kmalloc(len + 4, GFP_ATOMIC);
236 #endif
239 #ifdef SL_INCLUDE_CSLIP
240 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
241 #else
242 if (xbuff == NULL || rbuff == NULL) {
243 #endif
244 if (mtu >= sl->mtu) {
245 printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n",
246 dev->name);
247 err = -ENOBUFS;
249 goto done;
252 spin_lock_bh(&sl->lock);
254 err = -ENODEV;
255 if (sl->tty == NULL)
256 goto done_on_bh;
258 xbuff = xchg(&sl->xbuff, xbuff);
259 rbuff = xchg(&sl->rbuff, rbuff);
260 #ifdef SL_INCLUDE_CSLIP
261 cbuff = xchg(&sl->cbuff, cbuff);
262 #endif
263 if (sl->xleft) {
264 if (sl->xleft <= len) {
265 memcpy(sl->xbuff, sl->xhead, sl->xleft);
266 } else {
267 sl->xleft = 0;
268 sl->tx_dropped++;
271 sl->xhead = sl->xbuff;
273 if (sl->rcount) {
274 if (sl->rcount <= len) {
275 memcpy(sl->rbuff, rbuff, sl->rcount);
276 } else {
277 sl->rcount = 0;
278 sl->rx_over_errors++;
279 set_bit(SLF_ERROR, &sl->flags);
282 sl->mtu = mtu;
283 dev->mtu = mtu;
284 sl->buffsize = len;
285 err = 0;
287 done_on_bh:
288 spin_unlock_bh(&sl->lock);
290 done:
291 kfree(xbuff);
292 kfree(rbuff);
293 #ifdef SL_INCLUDE_CSLIP
294 kfree(cbuff);
295 #endif
296 return err;
300 /* Set the "sending" flag. This must be atomic hence the set_bit. */
301 static inline void
302 sl_lock(struct slip *sl)
304 netif_stop_queue(sl->dev);
308 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
309 static inline void
310 sl_unlock(struct slip *sl)
312 netif_wake_queue(sl->dev);
315 /* Send one completely decapsulated IP datagram to the IP layer. */
316 static void
317 sl_bump(struct slip *sl)
319 struct sk_buff *skb;
320 int count;
322 count = sl->rcount;
323 #ifdef SL_INCLUDE_CSLIP
324 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
325 unsigned char c;
326 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
327 /* ignore compressed packets when CSLIP is off */
328 if (!(sl->mode & SL_MODE_CSLIP)) {
329 printk(KERN_WARNING "%s: compressed packet ignored\n", sl->dev->name);
330 return;
332 /* make sure we've reserved enough space for uncompress to use */
333 if (count + 80 > sl->buffsize) {
334 sl->rx_over_errors++;
335 return;
337 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
338 if (count <= 0) {
339 return;
341 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
342 if (!(sl->mode & SL_MODE_CSLIP)) {
343 /* turn on header compression */
344 sl->mode |= SL_MODE_CSLIP;
345 sl->mode &= ~SL_MODE_ADAPTIVE;
346 printk(KERN_INFO "%s: header compression turned on\n", sl->dev->name);
348 sl->rbuff[0] &= 0x4f;
349 if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
350 return;
354 #endif /* SL_INCLUDE_CSLIP */
356 sl->rx_bytes+=count;
358 skb = dev_alloc_skb(count);
359 if (skb == NULL) {
360 printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", sl->dev->name);
361 sl->rx_dropped++;
362 return;
364 skb->dev = sl->dev;
365 memcpy(skb_put(skb,count), sl->rbuff, count);
366 skb_reset_mac_header(skb);
367 skb->protocol=htons(ETH_P_IP);
368 netif_rx(skb);
369 sl->dev->last_rx = jiffies;
370 sl->rx_packets++;
373 /* Encapsulate one IP datagram and stuff into a TTY queue. */
374 static void
375 sl_encaps(struct slip *sl, unsigned char *icp, int len)
377 unsigned char *p;
378 int actual, count;
380 if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */
381 printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name);
382 sl->tx_dropped++;
383 sl_unlock(sl);
384 return;
387 p = icp;
388 #ifdef SL_INCLUDE_CSLIP
389 if (sl->mode & SL_MODE_CSLIP) {
390 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
392 #endif
393 #ifdef CONFIG_SLIP_MODE_SLIP6
394 if(sl->mode & SL_MODE_SLIP6)
395 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
396 else
397 #endif
398 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
400 /* Order of next two lines is *very* important.
401 * When we are sending a little amount of data,
402 * the transfer may be completed inside driver.write()
403 * routine, because it's running with interrupts enabled.
404 * In this case we *never* got WRITE_WAKEUP event,
405 * if we did not request it before write operation.
406 * 14 Oct 1994 Dmitry Gorodchanin.
408 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
409 actual = sl->tty->driver->write(sl->tty, sl->xbuff, count);
410 #ifdef SL_CHECK_TRANSMIT
411 sl->dev->trans_start = jiffies;
412 #endif
413 sl->xleft = count - actual;
414 sl->xhead = sl->xbuff + actual;
415 #ifdef CONFIG_SLIP_SMART
416 /* VSV */
417 clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
418 #endif
422 * Called by the driver when there's room for more data. If we have
423 * more packets to send, we send them here.
425 static void slip_write_wakeup(struct tty_struct *tty)
427 int actual;
428 struct slip *sl = (struct slip *) tty->disc_data;
430 /* First make sure we're connected. */
431 if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
432 return;
434 if (sl->xleft <= 0) {
435 /* Now serial buffer is almost free & we can start
436 * transmission of another packet */
437 sl->tx_packets++;
438 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
439 sl_unlock(sl);
440 return;
443 actual = tty->driver->write(tty, sl->xhead, sl->xleft);
444 sl->xleft -= actual;
445 sl->xhead += actual;
448 static void sl_tx_timeout(struct net_device *dev)
450 struct slip *sl = netdev_priv(dev);
452 spin_lock(&sl->lock);
454 if (netif_queue_stopped(dev)) {
455 if (!netif_running(dev))
456 goto out;
458 /* May be we must check transmitter timeout here ?
459 * 14 Oct 1994 Dmitry Gorodchanin.
461 #ifdef SL_CHECK_TRANSMIT
462 if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
463 /* 20 sec timeout not reached */
464 goto out;
466 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
467 (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft) ?
468 "bad line quality" : "driver error");
469 sl->xleft = 0;
470 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
471 sl_unlock(sl);
472 #endif
475 out:
476 spin_unlock(&sl->lock);
480 /* Encapsulate an IP datagram and kick it into a TTY queue. */
481 static int
482 sl_xmit(struct sk_buff *skb, struct net_device *dev)
484 struct slip *sl = netdev_priv(dev);
486 spin_lock(&sl->lock);
487 if (!netif_running(dev)) {
488 spin_unlock(&sl->lock);
489 printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
490 dev_kfree_skb(skb);
491 return 0;
493 if (sl->tty == NULL) {
494 spin_unlock(&sl->lock);
495 dev_kfree_skb(skb);
496 return 0;
499 sl_lock(sl);
500 sl->tx_bytes+=skb->len;
501 sl_encaps(sl, skb->data, skb->len);
502 spin_unlock(&sl->lock);
504 dev_kfree_skb(skb);
505 return 0;
509 /******************************************
510 * Routines looking at netdevice side.
511 ******************************************/
513 /* Netdevice UP -> DOWN routine */
515 static int
516 sl_close(struct net_device *dev)
518 struct slip *sl = netdev_priv(dev);
520 spin_lock_bh(&sl->lock);
521 if (sl->tty) {
522 /* TTY discipline is running. */
523 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
525 netif_stop_queue(dev);
526 sl->rcount = 0;
527 sl->xleft = 0;
528 spin_unlock_bh(&sl->lock);
530 return 0;
533 /* Netdevice DOWN -> UP routine */
535 static int sl_open(struct net_device *dev)
537 struct slip *sl = netdev_priv(dev);
539 if (sl->tty==NULL)
540 return -ENODEV;
542 sl->flags &= (1 << SLF_INUSE);
543 netif_start_queue(dev);
544 return 0;
547 /* Netdevice change MTU request */
549 static int sl_change_mtu(struct net_device *dev, int new_mtu)
551 struct slip *sl = netdev_priv(dev);
553 if (new_mtu < 68 || new_mtu > 65534)
554 return -EINVAL;
556 if (new_mtu != dev->mtu)
557 return sl_realloc_bufs(sl, new_mtu);
558 return 0;
561 /* Netdevice get statistics request */
563 static struct net_device_stats *
564 sl_get_stats(struct net_device *dev)
566 static struct net_device_stats stats;
567 struct slip *sl = netdev_priv(dev);
568 #ifdef SL_INCLUDE_CSLIP
569 struct slcompress *comp;
570 #endif
572 memset(&stats, 0, sizeof(struct net_device_stats));
574 stats.rx_packets = sl->rx_packets;
575 stats.tx_packets = sl->tx_packets;
576 stats.rx_bytes = sl->rx_bytes;
577 stats.tx_bytes = sl->tx_bytes;
578 stats.rx_dropped = sl->rx_dropped;
579 stats.tx_dropped = sl->tx_dropped;
580 stats.tx_errors = sl->tx_errors;
581 stats.rx_errors = sl->rx_errors;
582 stats.rx_over_errors = sl->rx_over_errors;
583 #ifdef SL_INCLUDE_CSLIP
584 stats.rx_fifo_errors = sl->rx_compressed;
585 stats.tx_fifo_errors = sl->tx_compressed;
586 stats.collisions = sl->tx_misses;
587 comp = sl->slcomp;
588 if (comp) {
589 stats.rx_fifo_errors += comp->sls_i_compressed;
590 stats.rx_dropped += comp->sls_i_tossed;
591 stats.tx_fifo_errors += comp->sls_o_compressed;
592 stats.collisions += comp->sls_o_misses;
594 #endif /* CONFIG_INET */
595 return (&stats);
598 /* Netdevice register callback */
600 static int sl_init(struct net_device *dev)
602 struct slip *sl = netdev_priv(dev);
605 * Finish setting up the DEVICE info.
608 dev->mtu = sl->mtu;
609 dev->type = ARPHRD_SLIP + sl->mode;
610 #ifdef SL_CHECK_TRANSMIT
611 dev->tx_timeout = sl_tx_timeout;
612 dev->watchdog_timeo = 20*HZ;
613 #endif
614 return 0;
618 static void sl_uninit(struct net_device *dev)
620 struct slip *sl = netdev_priv(dev);
622 sl_free_bufs(sl);
625 static void sl_setup(struct net_device *dev)
627 dev->init = sl_init;
628 dev->uninit = sl_uninit;
629 dev->open = sl_open;
630 dev->destructor = free_netdev;
631 dev->stop = sl_close;
632 dev->get_stats = sl_get_stats;
633 dev->change_mtu = sl_change_mtu;
634 dev->hard_start_xmit = sl_xmit;
635 #ifdef CONFIG_SLIP_SMART
636 dev->do_ioctl = sl_ioctl;
637 #endif
638 dev->hard_header_len = 0;
639 dev->addr_len = 0;
640 dev->tx_queue_len = 10;
642 /* New-style flags. */
643 dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
646 /******************************************
647 Routines looking at TTY side.
648 ******************************************/
652 * Handle the 'receiver data ready' interrupt.
653 * This function is called by the 'tty_io' module in the kernel when
654 * a block of SLIP data has been received, which can now be decapsulated
655 * and sent on to some IP layer for further processing. This will not
656 * be re-entered while running but other ldisc functions may be called
657 * in parallel
660 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
662 struct slip *sl = (struct slip *) tty->disc_data;
664 if (!sl || sl->magic != SLIP_MAGIC ||
665 !netif_running(sl->dev))
666 return;
668 /* Read the characters out of the buffer */
669 while (count--) {
670 if (fp && *fp++) {
671 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
672 sl->rx_errors++;
674 cp++;
675 continue;
677 #ifdef CONFIG_SLIP_MODE_SLIP6
678 if (sl->mode & SL_MODE_SLIP6)
679 slip_unesc6(sl, *cp++);
680 else
681 #endif
682 slip_unesc(sl, *cp++);
686 /************************************
687 * slip_open helper routines.
688 ************************************/
690 /* Collect hanged up channels */
692 static void sl_sync(void)
694 int i;
695 struct net_device *dev;
696 struct slip *sl;
698 for (i = 0; i < slip_maxdev; i++) {
699 if ((dev = slip_devs[i]) == NULL)
700 break;
702 sl = netdev_priv(dev);
703 if (sl->tty || sl->leased)
704 continue;
705 if (dev->flags&IFF_UP)
706 dev_close(dev);
711 /* Find a free SLIP channel, and link in this `tty' line. */
712 static struct slip *
713 sl_alloc(dev_t line)
715 int i;
716 int sel = -1;
717 int score = -1;
718 struct net_device *dev = NULL;
719 struct slip *sl;
721 if (slip_devs == NULL)
722 return NULL; /* Master array missing ! */
724 for (i = 0; i < slip_maxdev; i++) {
725 dev = slip_devs[i];
726 if (dev == NULL)
727 break;
729 sl = netdev_priv(dev);
730 if (sl->leased) {
731 if (sl->line != line)
732 continue;
733 if (sl->tty)
734 return NULL;
736 /* Clear ESCAPE & ERROR flags */
737 sl->flags &= (1 << SLF_INUSE);
738 return sl;
741 if (sl->tty)
742 continue;
744 if (current->pid == sl->pid) {
745 if (sl->line == line && score < 3) {
746 sel = i;
747 score = 3;
748 continue;
750 if (score < 2) {
751 sel = i;
752 score = 2;
754 continue;
756 if (sl->line == line && score < 1) {
757 sel = i;
758 score = 1;
759 continue;
761 if (score < 0) {
762 sel = i;
763 score = 0;
767 if (sel >= 0) {
768 i = sel;
769 dev = slip_devs[i];
770 if (score > 1) {
771 sl = netdev_priv(dev);
772 sl->flags &= (1 << SLF_INUSE);
773 return sl;
777 /* Sorry, too many, all slots in use */
778 if (i >= slip_maxdev)
779 return NULL;
781 if (dev) {
782 sl = netdev_priv(dev);
783 if (test_bit(SLF_INUSE, &sl->flags)) {
784 unregister_netdevice(dev);
785 dev = NULL;
786 slip_devs[i] = NULL;
790 if (!dev) {
791 char name[IFNAMSIZ];
792 sprintf(name, "sl%d", i);
794 dev = alloc_netdev(sizeof(*sl), name, sl_setup);
795 if (!dev)
796 return NULL;
797 dev->base_addr = i;
800 sl = netdev_priv(dev);
802 /* Initialize channel control data */
803 sl->magic = SLIP_MAGIC;
804 sl->dev = dev;
805 spin_lock_init(&sl->lock);
806 sl->mode = SL_MODE_DEFAULT;
807 #ifdef CONFIG_SLIP_SMART
808 init_timer(&sl->keepalive_timer); /* initialize timer_list struct */
809 sl->keepalive_timer.data=(unsigned long)sl;
810 sl->keepalive_timer.function=sl_keepalive;
811 init_timer(&sl->outfill_timer);
812 sl->outfill_timer.data=(unsigned long)sl;
813 sl->outfill_timer.function=sl_outfill;
814 #endif
815 slip_devs[i] = dev;
817 return sl;
821 * Open the high-level part of the SLIP channel.
822 * This function is called by the TTY module when the
823 * SLIP line discipline is called for. Because we are
824 * sure the tty line exists, we only have to link it to
825 * a free SLIP channel...
827 * Called in process context serialized from other ldisc calls.
830 static int slip_open(struct tty_struct *tty)
832 struct slip *sl;
833 int err;
835 if(!capable(CAP_NET_ADMIN))
836 return -EPERM;
838 /* RTnetlink lock is misused here to serialize concurrent
839 opens of slip channels. There are better ways, but it is
840 the simplest one.
842 rtnl_lock();
844 /* Collect hanged up channels. */
845 sl_sync();
847 sl = (struct slip *) tty->disc_data;
849 err = -EEXIST;
850 /* First make sure we're not already connected. */
851 if (sl && sl->magic == SLIP_MAGIC)
852 goto err_exit;
854 /* OK. Find a free SLIP channel to use. */
855 err = -ENFILE;
856 if ((sl = sl_alloc(tty_devnum(tty))) == NULL)
857 goto err_exit;
859 sl->tty = tty;
860 tty->disc_data = sl;
861 sl->line = tty_devnum(tty);
862 sl->pid = current->pid;
864 if (!test_bit(SLF_INUSE, &sl->flags)) {
865 /* Perform the low-level SLIP initialization. */
866 if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
867 goto err_free_chan;
869 set_bit(SLF_INUSE, &sl->flags);
871 if ((err = register_netdevice(sl->dev)))
872 goto err_free_bufs;
875 #ifdef CONFIG_SLIP_SMART
876 if (sl->keepalive) {
877 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
878 add_timer (&sl->keepalive_timer);
880 if (sl->outfill) {
881 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
882 add_timer (&sl->outfill_timer);
884 #endif
886 /* Done. We have linked the TTY line to a channel. */
887 rtnl_unlock();
888 tty->receive_room = 65536; /* We don't flow control */
889 return sl->dev->base_addr;
891 err_free_bufs:
892 sl_free_bufs(sl);
894 err_free_chan:
895 sl->tty = NULL;
896 tty->disc_data = NULL;
897 clear_bit(SLF_INUSE, &sl->flags);
899 err_exit:
900 rtnl_unlock();
902 /* Count references from TTY module */
903 return err;
908 FIXME: 1,2 are fixed 3 was never true anyway.
910 Let me to blame a bit.
911 1. TTY module calls this funstion on soft interrupt.
912 2. TTY module calls this function WITH MASKED INTERRUPTS!
913 3. TTY module does not notify us about line discipline
914 shutdown,
916 Seems, now it is clean. The solution is to consider netdevice and
917 line discipline sides as two independent threads.
919 By-product (not desired): sl? does not feel hangups and remains open.
920 It is supposed, that user level program (dip, diald, slattach...)
921 will catch SIGHUP and make the rest of work.
923 I see no way to make more with current tty code. --ANK
927 * Close down a SLIP channel.
928 * This means flushing out any pending queues, and then returning. This
929 * call is serialized against other ldisc functions.
931 static void
932 slip_close(struct tty_struct *tty)
934 struct slip *sl = (struct slip *) tty->disc_data;
936 /* First make sure we're connected. */
937 if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
938 return;
940 tty->disc_data = NULL;
941 sl->tty = NULL;
942 if (!sl->leased)
943 sl->line = 0;
945 /* VSV = very important to remove timers */
946 #ifdef CONFIG_SLIP_SMART
947 del_timer_sync(&sl->keepalive_timer);
948 del_timer_sync(&sl->outfill_timer);
949 #endif
951 /* Count references from TTY module */
954 /************************************************************************
955 * STANDARD SLIP ENCAPSULATION *
956 ************************************************************************/
958 static int
959 slip_esc(unsigned char *s, unsigned char *d, int len)
961 unsigned char *ptr = d;
962 unsigned char c;
965 * Send an initial END character to flush out any
966 * data that may have accumulated in the receiver
967 * due to line noise.
970 *ptr++ = END;
973 * For each byte in the packet, send the appropriate
974 * character sequence, according to the SLIP protocol.
977 while (len-- > 0) {
978 switch(c = *s++) {
979 case END:
980 *ptr++ = ESC;
981 *ptr++ = ESC_END;
982 break;
983 case ESC:
984 *ptr++ = ESC;
985 *ptr++ = ESC_ESC;
986 break;
987 default:
988 *ptr++ = c;
989 break;
992 *ptr++ = END;
993 return (ptr - d);
996 static void slip_unesc(struct slip *sl, unsigned char s)
999 switch(s) {
1000 case END:
1001 #ifdef CONFIG_SLIP_SMART
1002 /* drop keeptest bit = VSV */
1003 if (test_bit(SLF_KEEPTEST, &sl->flags))
1004 clear_bit(SLF_KEEPTEST, &sl->flags);
1005 #endif
1007 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1008 sl_bump(sl);
1010 clear_bit(SLF_ESCAPE, &sl->flags);
1011 sl->rcount = 0;
1012 return;
1014 case ESC:
1015 set_bit(SLF_ESCAPE, &sl->flags);
1016 return;
1017 case ESC_ESC:
1018 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1019 s = ESC;
1021 break;
1022 case ESC_END:
1023 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1024 s = END;
1026 break;
1028 if (!test_bit(SLF_ERROR, &sl->flags)) {
1029 if (sl->rcount < sl->buffsize) {
1030 sl->rbuff[sl->rcount++] = s;
1031 return;
1033 sl->rx_over_errors++;
1034 set_bit(SLF_ERROR, &sl->flags);
1039 #ifdef CONFIG_SLIP_MODE_SLIP6
1040 /************************************************************************
1041 * 6 BIT SLIP ENCAPSULATION *
1042 ************************************************************************/
1045 slip_esc6(unsigned char *s, unsigned char *d, int len)
1047 unsigned char *ptr = d;
1048 unsigned char c;
1049 int i;
1050 unsigned short v = 0;
1051 short bits = 0;
1054 * Send an initial END character to flush out any
1055 * data that may have accumulated in the receiver
1056 * due to line noise.
1059 *ptr++ = 0x70;
1062 * Encode the packet into printable ascii characters
1065 for (i = 0; i < len; ++i) {
1066 v = (v << 8) | s[i];
1067 bits += 8;
1068 while (bits >= 6) {
1069 bits -= 6;
1070 c = 0x30 + ((v >> bits) & 0x3F);
1071 *ptr++ = c;
1074 if (bits) {
1075 c = 0x30 + ((v << (6 - bits)) & 0x3F);
1076 *ptr++ = c;
1078 *ptr++ = 0x70;
1079 return ptr - d;
1082 void
1083 slip_unesc6(struct slip *sl, unsigned char s)
1085 unsigned char c;
1087 if (s == 0x70) {
1088 #ifdef CONFIG_SLIP_SMART
1089 /* drop keeptest bit = VSV */
1090 if (test_bit(SLF_KEEPTEST, &sl->flags))
1091 clear_bit(SLF_KEEPTEST, &sl->flags);
1092 #endif
1094 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1095 sl_bump(sl);
1097 sl->rcount = 0;
1098 sl->xbits = 0;
1099 sl->xdata = 0;
1100 } else if (s >= 0x30 && s < 0x70) {
1101 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1102 sl->xbits += 6;
1103 if (sl->xbits >= 8) {
1104 sl->xbits -= 8;
1105 c = (unsigned char)(sl->xdata >> sl->xbits);
1106 if (!test_bit(SLF_ERROR, &sl->flags)) {
1107 if (sl->rcount < sl->buffsize) {
1108 sl->rbuff[sl->rcount++] = c;
1109 return;
1111 sl->rx_over_errors++;
1112 set_bit(SLF_ERROR, &sl->flags);
1117 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1119 /* Perform I/O control on an active SLIP channel. */
1120 static int slip_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1122 struct slip *sl = (struct slip *) tty->disc_data;
1123 unsigned int tmp;
1124 int __user *p = (int __user *)arg;
1126 /* First make sure we're connected. */
1127 if (!sl || sl->magic != SLIP_MAGIC) {
1128 return -EINVAL;
1131 switch(cmd) {
1132 case SIOCGIFNAME:
1133 tmp = strlen(sl->dev->name) + 1;
1134 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1135 return -EFAULT;
1136 return 0;
1138 case SIOCGIFENCAP:
1139 if (put_user(sl->mode, p))
1140 return -EFAULT;
1141 return 0;
1143 case SIOCSIFENCAP:
1144 if (get_user(tmp, p))
1145 return -EFAULT;
1146 #ifndef SL_INCLUDE_CSLIP
1147 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1148 return -EINVAL;
1150 #else
1151 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1152 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1153 /* return -EINVAL; */
1154 tmp &= ~SL_MODE_ADAPTIVE;
1156 #endif
1157 #ifndef CONFIG_SLIP_MODE_SLIP6
1158 if (tmp & SL_MODE_SLIP6) {
1159 return -EINVAL;
1161 #endif
1162 sl->mode = tmp;
1163 sl->dev->type = ARPHRD_SLIP+sl->mode;
1164 return 0;
1166 case SIOCSIFHWADDR:
1167 return -EINVAL;
1169 #ifdef CONFIG_SLIP_SMART
1170 /* VSV changes start here */
1171 case SIOCSKEEPALIVE:
1172 if (get_user(tmp, p))
1173 return -EFAULT;
1174 if (tmp > 255) /* max for unchar */
1175 return -EINVAL;
1177 spin_lock_bh(&sl->lock);
1178 if (!sl->tty) {
1179 spin_unlock_bh(&sl->lock);
1180 return -ENODEV;
1182 if ((sl->keepalive = (unchar) tmp) != 0) {
1183 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1184 set_bit(SLF_KEEPTEST, &sl->flags);
1185 } else {
1186 del_timer (&sl->keepalive_timer);
1188 spin_unlock_bh(&sl->lock);
1189 return 0;
1191 case SIOCGKEEPALIVE:
1192 if (put_user(sl->keepalive, p))
1193 return -EFAULT;
1194 return 0;
1196 case SIOCSOUTFILL:
1197 if (get_user(tmp, p))
1198 return -EFAULT;
1199 if (tmp > 255) /* max for unchar */
1200 return -EINVAL;
1201 spin_lock_bh(&sl->lock);
1202 if (!sl->tty) {
1203 spin_unlock_bh(&sl->lock);
1204 return -ENODEV;
1206 if ((sl->outfill = (unchar) tmp) != 0){
1207 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1208 set_bit(SLF_OUTWAIT, &sl->flags);
1209 } else {
1210 del_timer (&sl->outfill_timer);
1212 spin_unlock_bh(&sl->lock);
1213 return 0;
1215 case SIOCGOUTFILL:
1216 if (put_user(sl->outfill, p))
1217 return -EFAULT;
1218 return 0;
1219 /* VSV changes end */
1220 #endif
1222 /* Allow stty to read, but not set, the serial port */
1223 case TCGETS:
1224 case TCGETA:
1225 return n_tty_ioctl(tty, file, cmd, arg);
1227 default:
1228 return -ENOIOCTLCMD;
1232 /* VSV changes start here */
1233 #ifdef CONFIG_SLIP_SMART
1234 /* function do_ioctl called from net/core/dev.c
1235 to allow get/set outfill/keepalive parameter
1236 by ifconfig */
1238 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
1240 struct slip *sl = netdev_priv(dev);
1241 unsigned long *p = (unsigned long *)&rq->ifr_ifru;
1243 if (sl == NULL) /* Allocation failed ?? */
1244 return -ENODEV;
1246 spin_lock_bh(&sl->lock);
1248 if (!sl->tty) {
1249 spin_unlock_bh(&sl->lock);
1250 return -ENODEV;
1253 switch(cmd){
1254 case SIOCSKEEPALIVE:
1255 /* max for unchar */
1256 if ((unsigned)*p > 255) {
1257 spin_unlock_bh(&sl->lock);
1258 return -EINVAL;
1260 sl->keepalive = (unchar) *p;
1261 if (sl->keepalive != 0) {
1262 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1263 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1264 set_bit(SLF_KEEPTEST, &sl->flags);
1265 } else {
1266 del_timer(&sl->keepalive_timer);
1268 break;
1270 case SIOCGKEEPALIVE:
1271 *p = sl->keepalive;
1272 break;
1274 case SIOCSOUTFILL:
1275 if ((unsigned)*p > 255) { /* max for unchar */
1276 spin_unlock_bh(&sl->lock);
1277 return -EINVAL;
1279 if ((sl->outfill = (unchar)*p) != 0){
1280 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1281 set_bit(SLF_OUTWAIT, &sl->flags);
1282 } else {
1283 del_timer (&sl->outfill_timer);
1285 break;
1287 case SIOCGOUTFILL:
1288 *p = sl->outfill;
1289 break;
1291 case SIOCSLEASE:
1292 /* Resolve race condition, when ioctl'ing hanged up
1293 and opened by another process device.
1295 if (sl->tty != current->signal->tty && sl->pid != current->pid) {
1296 spin_unlock_bh(&sl->lock);
1297 return -EPERM;
1299 sl->leased = 0;
1300 if (*p)
1301 sl->leased = 1;
1302 break;
1304 case SIOCGLEASE:
1305 *p = sl->leased;
1307 spin_unlock_bh(&sl->lock);
1308 return 0;
1310 #endif
1311 /* VSV changes end */
1313 static struct tty_ldisc sl_ldisc = {
1314 .owner = THIS_MODULE,
1315 .magic = TTY_LDISC_MAGIC,
1316 .name = "slip",
1317 .open = slip_open,
1318 .close = slip_close,
1319 .ioctl = slip_ioctl,
1320 .receive_buf = slip_receive_buf,
1321 .write_wakeup = slip_write_wakeup,
1324 static int __init slip_init(void)
1326 int status;
1328 if (slip_maxdev < 4)
1329 slip_maxdev = 4; /* Sanity */
1331 printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1332 #ifdef CONFIG_SLIP_MODE_SLIP6
1333 " (6 bit encapsulation enabled)"
1334 #endif
1335 ".\n",
1336 SLIP_VERSION, slip_maxdev );
1337 #if defined(SL_INCLUDE_CSLIP)
1338 printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
1339 #endif
1340 #ifdef CONFIG_SLIP_SMART
1341 printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1342 #endif
1344 slip_devs = kzalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL);
1345 if (!slip_devs) {
1346 printk(KERN_ERR "SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1347 return -ENOMEM;
1350 /* Fill in our line protocol discipline, and register it */
1351 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1352 printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
1353 kfree(slip_devs);
1355 return status;
1358 static void __exit slip_exit(void)
1360 int i;
1361 struct net_device *dev;
1362 struct slip *sl;
1363 unsigned long timeout = jiffies + HZ;
1364 int busy = 0;
1366 if (slip_devs == NULL)
1367 return;
1369 /* First of all: check for active disciplines and hangup them.
1371 do {
1372 if (busy)
1373 msleep_interruptible(100);
1375 busy = 0;
1376 for (i = 0; i < slip_maxdev; i++) {
1377 dev = slip_devs[i];
1378 if (!dev)
1379 continue;
1380 sl = netdev_priv(dev);
1381 spin_lock_bh(&sl->lock);
1382 if (sl->tty) {
1383 busy++;
1384 tty_hangup(sl->tty);
1386 spin_unlock_bh(&sl->lock);
1388 } while (busy && time_before(jiffies, timeout));
1391 for (i = 0; i < slip_maxdev; i++) {
1392 dev = slip_devs[i];
1393 if (!dev)
1394 continue;
1395 slip_devs[i] = NULL;
1397 sl = netdev_priv(dev);
1398 if (sl->tty) {
1399 printk(KERN_ERR "%s: tty discipline still running\n",
1400 dev->name);
1401 /* Intentionally leak the control block. */
1402 dev->destructor = NULL;
1405 unregister_netdev(dev);
1408 kfree(slip_devs);
1409 slip_devs = NULL;
1411 if ((i = tty_unregister_ldisc(N_SLIP)))
1413 printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
1417 module_init(slip_init);
1418 module_exit(slip_exit);
1420 #ifdef CONFIG_SLIP_SMART
1422 * This is start of the code for multislip style line checking
1423 * added by Stanislav Voronyi. All changes before marked VSV
1426 static void sl_outfill(unsigned long sls)
1428 struct slip *sl=(struct slip *)sls;
1430 spin_lock(&sl->lock);
1432 if (sl->tty == NULL)
1433 goto out;
1435 if(sl->outfill)
1437 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1439 /* no packets were transmitted, do outfill */
1440 #ifdef CONFIG_SLIP_MODE_SLIP6
1441 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1442 #else
1443 unsigned char s = END;
1444 #endif
1445 /* put END into tty queue. Is it right ??? */
1446 if (!netif_queue_stopped(sl->dev))
1448 /* if device busy no outfill */
1449 sl->tty->driver->write(sl->tty, &s, 1);
1452 else
1453 set_bit(SLF_OUTWAIT, &sl->flags);
1455 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1457 out:
1458 spin_unlock(&sl->lock);
1461 static void sl_keepalive(unsigned long sls)
1463 struct slip *sl=(struct slip *)sls;
1465 spin_lock(&sl->lock);
1467 if (sl->tty == NULL)
1468 goto out;
1470 if( sl->keepalive)
1472 if(test_bit(SLF_KEEPTEST, &sl->flags))
1474 /* keepalive still high :(, we must hangup */
1475 if( sl->outfill ) /* outfill timer must be deleted too */
1476 (void)del_timer(&sl->outfill_timer);
1477 printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1478 tty_hangup(sl->tty); /* this must hangup tty & close slip */
1479 /* I think we need not something else */
1480 goto out;
1482 else
1483 set_bit(SLF_KEEPTEST, &sl->flags);
1485 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1488 out:
1489 spin_unlock(&sl->lock);
1492 #endif
1493 MODULE_LICENSE("GPL");
1494 MODULE_ALIAS_LDISC(N_SLIP);