[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / drivers / net / slip.c
blob8f7841c0374d38d7caaaf7b6003882c83cc1582b
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>
60 #include <linux/moduleparam.h>
62 #include <asm/system.h>
63 #include <asm/uaccess.h>
64 #include <linux/bitops.h>
65 #include <linux/string.h>
66 #include <linux/mm.h>
67 #include <linux/interrupt.h>
68 #include <linux/in.h>
69 #include <linux/tty.h>
70 #include <linux/errno.h>
71 #include <linux/netdevice.h>
72 #include <linux/etherdevice.h>
73 #include <linux/skbuff.h>
74 #include <linux/rtnetlink.h>
75 #include <linux/if_arp.h>
76 #include <linux/if_slip.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 void * tmp;
203 /* Free all SLIP frame buffers. */
204 tmp = xchg(&sl->rbuff, NULL);
205 kfree(tmp);
206 tmp = xchg(&sl->xbuff, NULL);
207 kfree(tmp);
208 #ifdef SL_INCLUDE_CSLIP
209 tmp = xchg(&sl->cbuff, NULL);
210 kfree(tmp);
211 if ((tmp = xchg(&sl->slcomp, NULL)) != NULL)
212 slhc_free(tmp);
213 #endif
217 Reallocate slip channel buffers.
220 static int sl_realloc_bufs(struct slip *sl, int mtu)
222 int err = 0;
223 struct net_device *dev = sl->dev;
224 unsigned char *xbuff, *rbuff;
225 #ifdef SL_INCLUDE_CSLIP
226 unsigned char *cbuff;
227 #endif
228 int len = mtu * 2;
231 * allow for arrival of larger UDP packets, even if we say not to
232 * also fixes a bug in which SunOS sends 512-byte packets even with
233 * an MSS of 128
235 if (len < 576 * 2)
236 len = 576 * 2;
238 xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
239 rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
240 #ifdef SL_INCLUDE_CSLIP
241 cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
242 #endif
245 #ifdef SL_INCLUDE_CSLIP
246 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
247 #else
248 if (xbuff == NULL || rbuff == NULL) {
249 #endif
250 if (mtu >= sl->mtu) {
251 printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n",
252 dev->name);
253 err = -ENOBUFS;
255 goto done;
258 spin_lock_bh(&sl->lock);
260 err = -ENODEV;
261 if (sl->tty == NULL)
262 goto done_on_bh;
264 xbuff = xchg(&sl->xbuff, xbuff);
265 rbuff = xchg(&sl->rbuff, rbuff);
266 #ifdef SL_INCLUDE_CSLIP
267 cbuff = xchg(&sl->cbuff, cbuff);
268 #endif
269 if (sl->xleft) {
270 if (sl->xleft <= len) {
271 memcpy(sl->xbuff, sl->xhead, sl->xleft);
272 } else {
273 sl->xleft = 0;
274 sl->tx_dropped++;
277 sl->xhead = sl->xbuff;
279 if (sl->rcount) {
280 if (sl->rcount <= len) {
281 memcpy(sl->rbuff, rbuff, sl->rcount);
282 } else {
283 sl->rcount = 0;
284 sl->rx_over_errors++;
285 set_bit(SLF_ERROR, &sl->flags);
288 sl->mtu = mtu;
289 dev->mtu = mtu;
290 sl->buffsize = len;
291 err = 0;
293 done_on_bh:
294 spin_unlock_bh(&sl->lock);
296 done:
297 kfree(xbuff);
298 kfree(rbuff);
299 #ifdef SL_INCLUDE_CSLIP
300 kfree(cbuff);
301 #endif
302 return err;
306 /* Set the "sending" flag. This must be atomic hence the set_bit. */
307 static inline void
308 sl_lock(struct slip *sl)
310 netif_stop_queue(sl->dev);
314 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
315 static inline void
316 sl_unlock(struct slip *sl)
318 netif_wake_queue(sl->dev);
321 /* Send one completely decapsulated IP datagram to the IP layer. */
322 static void
323 sl_bump(struct slip *sl)
325 struct sk_buff *skb;
326 int count;
328 count = sl->rcount;
329 #ifdef SL_INCLUDE_CSLIP
330 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
331 unsigned char c;
332 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
333 /* ignore compressed packets when CSLIP is off */
334 if (!(sl->mode & SL_MODE_CSLIP)) {
335 printk(KERN_WARNING "%s: compressed packet ignored\n", sl->dev->name);
336 return;
338 /* make sure we've reserved enough space for uncompress to use */
339 if (count + 80 > sl->buffsize) {
340 sl->rx_over_errors++;
341 return;
343 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
344 if (count <= 0) {
345 return;
347 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
348 if (!(sl->mode & SL_MODE_CSLIP)) {
349 /* turn on header compression */
350 sl->mode |= SL_MODE_CSLIP;
351 sl->mode &= ~SL_MODE_ADAPTIVE;
352 printk(KERN_INFO "%s: header compression turned on\n", sl->dev->name);
354 sl->rbuff[0] &= 0x4f;
355 if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
356 return;
360 #endif /* SL_INCLUDE_CSLIP */
362 sl->rx_bytes+=count;
364 skb = dev_alloc_skb(count);
365 if (skb == NULL) {
366 printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", sl->dev->name);
367 sl->rx_dropped++;
368 return;
370 skb->dev = sl->dev;
371 memcpy(skb_put(skb,count), sl->rbuff, count);
372 skb->mac.raw=skb->data;
373 skb->protocol=htons(ETH_P_IP);
374 netif_rx(skb);
375 sl->dev->last_rx = jiffies;
376 sl->rx_packets++;
379 /* Encapsulate one IP datagram and stuff into a TTY queue. */
380 static void
381 sl_encaps(struct slip *sl, unsigned char *icp, int len)
383 unsigned char *p;
384 int actual, count;
386 if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */
387 printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name);
388 sl->tx_dropped++;
389 sl_unlock(sl);
390 return;
393 p = icp;
394 #ifdef SL_INCLUDE_CSLIP
395 if (sl->mode & SL_MODE_CSLIP) {
396 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
398 #endif
399 #ifdef CONFIG_SLIP_MODE_SLIP6
400 if(sl->mode & SL_MODE_SLIP6)
401 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
402 else
403 #endif
404 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
406 /* Order of next two lines is *very* important.
407 * When we are sending a little amount of data,
408 * the transfer may be completed inside driver.write()
409 * routine, because it's running with interrupts enabled.
410 * In this case we *never* got WRITE_WAKEUP event,
411 * if we did not request it before write operation.
412 * 14 Oct 1994 Dmitry Gorodchanin.
414 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
415 actual = sl->tty->driver->write(sl->tty, sl->xbuff, count);
416 #ifdef SL_CHECK_TRANSMIT
417 sl->dev->trans_start = jiffies;
418 #endif
419 sl->xleft = count - actual;
420 sl->xhead = sl->xbuff + actual;
421 #ifdef CONFIG_SLIP_SMART
422 /* VSV */
423 clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
424 #endif
428 * Called by the driver when there's room for more data. If we have
429 * more packets to send, we send them here.
431 static void slip_write_wakeup(struct tty_struct *tty)
433 int actual;
434 struct slip *sl = (struct slip *) tty->disc_data;
436 /* First make sure we're connected. */
437 if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
438 return;
440 if (sl->xleft <= 0) {
441 /* Now serial buffer is almost free & we can start
442 * transmission of another packet */
443 sl->tx_packets++;
444 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
445 sl_unlock(sl);
446 return;
449 actual = tty->driver->write(tty, sl->xhead, sl->xleft);
450 sl->xleft -= actual;
451 sl->xhead += actual;
454 static void sl_tx_timeout(struct net_device *dev)
456 struct slip *sl = netdev_priv(dev);
458 spin_lock(&sl->lock);
460 if (netif_queue_stopped(dev)) {
461 if (!netif_running(dev))
462 goto out;
464 /* May be we must check transmitter timeout here ?
465 * 14 Oct 1994 Dmitry Gorodchanin.
467 #ifdef SL_CHECK_TRANSMIT
468 if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
469 /* 20 sec timeout not reached */
470 goto out;
472 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
473 (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft) ?
474 "bad line quality" : "driver error");
475 sl->xleft = 0;
476 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
477 sl_unlock(sl);
478 #endif
481 out:
482 spin_unlock(&sl->lock);
486 /* Encapsulate an IP datagram and kick it into a TTY queue. */
487 static int
488 sl_xmit(struct sk_buff *skb, struct net_device *dev)
490 struct slip *sl = netdev_priv(dev);
492 spin_lock(&sl->lock);
493 if (!netif_running(dev)) {
494 spin_unlock(&sl->lock);
495 printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
496 dev_kfree_skb(skb);
497 return 0;
499 if (sl->tty == NULL) {
500 spin_unlock(&sl->lock);
501 dev_kfree_skb(skb);
502 return 0;
505 sl_lock(sl);
506 sl->tx_bytes+=skb->len;
507 sl_encaps(sl, skb->data, skb->len);
508 spin_unlock(&sl->lock);
510 dev_kfree_skb(skb);
511 return 0;
515 /******************************************
516 * Routines looking at netdevice side.
517 ******************************************/
519 /* Netdevice UP -> DOWN routine */
521 static int
522 sl_close(struct net_device *dev)
524 struct slip *sl = netdev_priv(dev);
526 spin_lock_bh(&sl->lock);
527 if (sl->tty) {
528 /* TTY discipline is running. */
529 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
531 netif_stop_queue(dev);
532 sl->rcount = 0;
533 sl->xleft = 0;
534 spin_unlock_bh(&sl->lock);
536 return 0;
539 /* Netdevice DOWN -> UP routine */
541 static int sl_open(struct net_device *dev)
543 struct slip *sl = netdev_priv(dev);
545 if (sl->tty==NULL)
546 return -ENODEV;
548 sl->flags &= (1 << SLF_INUSE);
549 netif_start_queue(dev);
550 return 0;
553 /* Netdevice change MTU request */
555 static int sl_change_mtu(struct net_device *dev, int new_mtu)
557 struct slip *sl = netdev_priv(dev);
559 if (new_mtu < 68 || new_mtu > 65534)
560 return -EINVAL;
562 if (new_mtu != dev->mtu)
563 return sl_realloc_bufs(sl, new_mtu);
564 return 0;
567 /* Netdevice get statistics request */
569 static struct net_device_stats *
570 sl_get_stats(struct net_device *dev)
572 static struct net_device_stats stats;
573 struct slip *sl = netdev_priv(dev);
574 #ifdef SL_INCLUDE_CSLIP
575 struct slcompress *comp;
576 #endif
578 memset(&stats, 0, sizeof(struct net_device_stats));
580 stats.rx_packets = sl->rx_packets;
581 stats.tx_packets = sl->tx_packets;
582 stats.rx_bytes = sl->rx_bytes;
583 stats.tx_bytes = sl->tx_bytes;
584 stats.rx_dropped = sl->rx_dropped;
585 stats.tx_dropped = sl->tx_dropped;
586 stats.tx_errors = sl->tx_errors;
587 stats.rx_errors = sl->rx_errors;
588 stats.rx_over_errors = sl->rx_over_errors;
589 #ifdef SL_INCLUDE_CSLIP
590 stats.rx_fifo_errors = sl->rx_compressed;
591 stats.tx_fifo_errors = sl->tx_compressed;
592 stats.collisions = sl->tx_misses;
593 comp = sl->slcomp;
594 if (comp) {
595 stats.rx_fifo_errors += comp->sls_i_compressed;
596 stats.rx_dropped += comp->sls_i_tossed;
597 stats.tx_fifo_errors += comp->sls_o_compressed;
598 stats.collisions += comp->sls_o_misses;
600 #endif /* CONFIG_INET */
601 return (&stats);
604 /* Netdevice register callback */
606 static int sl_init(struct net_device *dev)
608 struct slip *sl = netdev_priv(dev);
611 * Finish setting up the DEVICE info.
614 dev->mtu = sl->mtu;
615 dev->type = ARPHRD_SLIP + sl->mode;
616 #ifdef SL_CHECK_TRANSMIT
617 dev->tx_timeout = sl_tx_timeout;
618 dev->watchdog_timeo = 20*HZ;
619 #endif
620 return 0;
624 static void sl_uninit(struct net_device *dev)
626 struct slip *sl = netdev_priv(dev);
628 sl_free_bufs(sl);
631 static void sl_setup(struct net_device *dev)
633 dev->init = sl_init;
634 dev->uninit = sl_uninit;
635 dev->open = sl_open;
636 dev->destructor = free_netdev;
637 dev->stop = sl_close;
638 dev->get_stats = sl_get_stats;
639 dev->change_mtu = sl_change_mtu;
640 dev->hard_start_xmit = sl_xmit;
641 #ifdef CONFIG_SLIP_SMART
642 dev->do_ioctl = sl_ioctl;
643 #endif
644 dev->hard_header_len = 0;
645 dev->addr_len = 0;
646 dev->tx_queue_len = 10;
648 SET_MODULE_OWNER(dev);
650 /* New-style flags. */
651 dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
654 /******************************************
655 Routines looking at TTY side.
656 ******************************************/
659 static int slip_receive_room(struct tty_struct *tty)
661 return 65536; /* We can handle an infinite amount of data. :-) */
665 * Handle the 'receiver data ready' interrupt.
666 * This function is called by the 'tty_io' module in the kernel when
667 * a block of SLIP data has been received, which can now be decapsulated
668 * and sent on to some IP layer for further processing. This will not
669 * be re-entered while running but other ldisc functions may be called
670 * in parallel
673 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
675 struct slip *sl = (struct slip *) tty->disc_data;
677 if (!sl || sl->magic != SLIP_MAGIC ||
678 !netif_running(sl->dev))
679 return;
681 /* Read the characters out of the buffer */
682 while (count--) {
683 if (fp && *fp++) {
684 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
685 sl->rx_errors++;
687 cp++;
688 continue;
690 #ifdef CONFIG_SLIP_MODE_SLIP6
691 if (sl->mode & SL_MODE_SLIP6)
692 slip_unesc6(sl, *cp++);
693 else
694 #endif
695 slip_unesc(sl, *cp++);
699 /************************************
700 * slip_open helper routines.
701 ************************************/
703 /* Collect hanged up channels */
705 static void sl_sync(void)
707 int i;
708 struct net_device *dev;
709 struct slip *sl;
711 for (i = 0; i < slip_maxdev; i++) {
712 if ((dev = slip_devs[i]) == NULL)
713 break;
715 sl = netdev_priv(dev);
716 if (sl->tty || sl->leased)
717 continue;
718 if (dev->flags&IFF_UP)
719 dev_close(dev);
724 /* Find a free SLIP channel, and link in this `tty' line. */
725 static struct slip *
726 sl_alloc(dev_t line)
728 int i;
729 int sel = -1;
730 int score = -1;
731 struct net_device *dev = NULL;
732 struct slip *sl;
734 if (slip_devs == NULL)
735 return NULL; /* Master array missing ! */
737 for (i = 0; i < slip_maxdev; i++) {
738 dev = slip_devs[i];
739 if (dev == NULL)
740 break;
742 sl = netdev_priv(dev);
743 if (sl->leased) {
744 if (sl->line != line)
745 continue;
746 if (sl->tty)
747 return NULL;
749 /* Clear ESCAPE & ERROR flags */
750 sl->flags &= (1 << SLF_INUSE);
751 return sl;
754 if (sl->tty)
755 continue;
757 if (current->pid == sl->pid) {
758 if (sl->line == line && score < 3) {
759 sel = i;
760 score = 3;
761 continue;
763 if (score < 2) {
764 sel = i;
765 score = 2;
767 continue;
769 if (sl->line == line && score < 1) {
770 sel = i;
771 score = 1;
772 continue;
774 if (score < 0) {
775 sel = i;
776 score = 0;
780 if (sel >= 0) {
781 i = sel;
782 dev = slip_devs[i];
783 if (score > 1) {
784 sl = netdev_priv(dev);
785 sl->flags &= (1 << SLF_INUSE);
786 return sl;
790 /* Sorry, too many, all slots in use */
791 if (i >= slip_maxdev)
792 return NULL;
794 if (dev) {
795 sl = netdev_priv(dev);
796 if (test_bit(SLF_INUSE, &sl->flags)) {
797 unregister_netdevice(dev);
798 dev = NULL;
799 slip_devs[i] = NULL;
803 if (!dev) {
804 char name[IFNAMSIZ];
805 sprintf(name, "sl%d", i);
807 dev = alloc_netdev(sizeof(*sl), name, sl_setup);
808 if (!dev)
809 return NULL;
810 dev->base_addr = i;
813 sl = netdev_priv(dev);
815 /* Initialize channel control data */
816 sl->magic = SLIP_MAGIC;
817 sl->dev = dev;
818 spin_lock_init(&sl->lock);
819 sl->mode = SL_MODE_DEFAULT;
820 #ifdef CONFIG_SLIP_SMART
821 init_timer(&sl->keepalive_timer); /* initialize timer_list struct */
822 sl->keepalive_timer.data=(unsigned long)sl;
823 sl->keepalive_timer.function=sl_keepalive;
824 init_timer(&sl->outfill_timer);
825 sl->outfill_timer.data=(unsigned long)sl;
826 sl->outfill_timer.function=sl_outfill;
827 #endif
828 slip_devs[i] = dev;
830 return sl;
834 * Open the high-level part of the SLIP channel.
835 * This function is called by the TTY module when the
836 * SLIP line discipline is called for. Because we are
837 * sure the tty line exists, we only have to link it to
838 * a free SLIP channel...
840 * Called in process context serialized from other ldisc calls.
843 static int slip_open(struct tty_struct *tty)
845 struct slip *sl;
846 int err;
848 if(!capable(CAP_NET_ADMIN))
849 return -EPERM;
851 /* RTnetlink lock is misused here to serialize concurrent
852 opens of slip channels. There are better ways, but it is
853 the simplest one.
855 rtnl_lock();
857 /* Collect hanged up channels. */
858 sl_sync();
860 sl = (struct slip *) tty->disc_data;
862 err = -EEXIST;
863 /* First make sure we're not already connected. */
864 if (sl && sl->magic == SLIP_MAGIC)
865 goto err_exit;
867 /* OK. Find a free SLIP channel to use. */
868 err = -ENFILE;
869 if ((sl = sl_alloc(tty_devnum(tty))) == NULL)
870 goto err_exit;
872 sl->tty = tty;
873 tty->disc_data = sl;
874 sl->line = tty_devnum(tty);
875 sl->pid = current->pid;
877 /* FIXME: already done before we were called - seems this can go */
878 if (tty->driver->flush_buffer)
879 tty->driver->flush_buffer(tty);
881 if (!test_bit(SLF_INUSE, &sl->flags)) {
882 /* Perform the low-level SLIP initialization. */
883 if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
884 goto err_free_chan;
886 set_bit(SLF_INUSE, &sl->flags);
888 if ((err = register_netdevice(sl->dev)))
889 goto err_free_bufs;
892 #ifdef CONFIG_SLIP_SMART
893 if (sl->keepalive) {
894 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
895 add_timer (&sl->keepalive_timer);
897 if (sl->outfill) {
898 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
899 add_timer (&sl->outfill_timer);
901 #endif
903 /* Done. We have linked the TTY line to a channel. */
904 rtnl_unlock();
905 return sl->dev->base_addr;
907 err_free_bufs:
908 sl_free_bufs(sl);
910 err_free_chan:
911 sl->tty = NULL;
912 tty->disc_data = NULL;
913 clear_bit(SLF_INUSE, &sl->flags);
915 err_exit:
916 rtnl_unlock();
918 /* Count references from TTY module */
919 return err;
924 FIXME: 1,2 are fixed 3 was never true anyway.
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 returning. This
945 * call is serialized against other ldisc functions.
947 static void
948 slip_close(struct tty_struct *tty)
950 struct slip *sl = (struct slip *) tty->disc_data;
952 /* First make sure we're connected. */
953 if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
954 return;
956 tty->disc_data = NULL;
957 sl->tty = NULL;
958 if (!sl->leased)
959 sl->line = 0;
961 /* VSV = very important to remove timers */
962 #ifdef CONFIG_SLIP_SMART
963 del_timer_sync(&sl->keepalive_timer);
964 del_timer_sync(&sl->outfill_timer);
965 #endif
967 /* Count references from TTY module */
970 /************************************************************************
971 * STANDARD SLIP ENCAPSULATION *
972 ************************************************************************/
975 slip_esc(unsigned char *s, unsigned char *d, int len)
977 unsigned char *ptr = d;
978 unsigned char c;
981 * Send an initial END character to flush out any
982 * data that may have accumulated in the receiver
983 * due to line noise.
986 *ptr++ = END;
989 * For each byte in the packet, send the appropriate
990 * character sequence, according to the SLIP protocol.
993 while (len-- > 0) {
994 switch(c = *s++) {
995 case END:
996 *ptr++ = ESC;
997 *ptr++ = ESC_END;
998 break;
999 case ESC:
1000 *ptr++ = ESC;
1001 *ptr++ = ESC_ESC;
1002 break;
1003 default:
1004 *ptr++ = c;
1005 break;
1008 *ptr++ = END;
1009 return (ptr - d);
1012 static void slip_unesc(struct slip *sl, unsigned char s)
1015 switch(s) {
1016 case END:
1017 #ifdef CONFIG_SLIP_SMART
1018 /* drop keeptest bit = VSV */
1019 if (test_bit(SLF_KEEPTEST, &sl->flags))
1020 clear_bit(SLF_KEEPTEST, &sl->flags);
1021 #endif
1023 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1024 sl_bump(sl);
1026 clear_bit(SLF_ESCAPE, &sl->flags);
1027 sl->rcount = 0;
1028 return;
1030 case ESC:
1031 set_bit(SLF_ESCAPE, &sl->flags);
1032 return;
1033 case ESC_ESC:
1034 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1035 s = ESC;
1037 break;
1038 case ESC_END:
1039 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1040 s = END;
1042 break;
1044 if (!test_bit(SLF_ERROR, &sl->flags)) {
1045 if (sl->rcount < sl->buffsize) {
1046 sl->rbuff[sl->rcount++] = s;
1047 return;
1049 sl->rx_over_errors++;
1050 set_bit(SLF_ERROR, &sl->flags);
1055 #ifdef CONFIG_SLIP_MODE_SLIP6
1056 /************************************************************************
1057 * 6 BIT SLIP ENCAPSULATION *
1058 ************************************************************************/
1061 slip_esc6(unsigned char *s, unsigned char *d, int len)
1063 unsigned char *ptr = d;
1064 unsigned char c;
1065 int i;
1066 unsigned short v = 0;
1067 short bits = 0;
1070 * Send an initial END character to flush out any
1071 * data that may have accumulated in the receiver
1072 * due to line noise.
1075 *ptr++ = 0x70;
1078 * Encode the packet into printable ascii characters
1081 for (i = 0; i < len; ++i) {
1082 v = (v << 8) | s[i];
1083 bits += 8;
1084 while (bits >= 6) {
1085 bits -= 6;
1086 c = 0x30 + ((v >> bits) & 0x3F);
1087 *ptr++ = c;
1090 if (bits) {
1091 c = 0x30 + ((v << (6 - bits)) & 0x3F);
1092 *ptr++ = c;
1094 *ptr++ = 0x70;
1095 return ptr - d;
1098 void
1099 slip_unesc6(struct slip *sl, unsigned char s)
1101 unsigned char c;
1103 if (s == 0x70) {
1104 #ifdef CONFIG_SLIP_SMART
1105 /* drop keeptest bit = VSV */
1106 if (test_bit(SLF_KEEPTEST, &sl->flags))
1107 clear_bit(SLF_KEEPTEST, &sl->flags);
1108 #endif
1110 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1111 sl_bump(sl);
1113 sl->rcount = 0;
1114 sl->xbits = 0;
1115 sl->xdata = 0;
1116 } else if (s >= 0x30 && s < 0x70) {
1117 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1118 sl->xbits += 6;
1119 if (sl->xbits >= 8) {
1120 sl->xbits -= 8;
1121 c = (unsigned char)(sl->xdata >> sl->xbits);
1122 if (!test_bit(SLF_ERROR, &sl->flags)) {
1123 if (sl->rcount < sl->buffsize) {
1124 sl->rbuff[sl->rcount++] = c;
1125 return;
1127 sl->rx_over_errors++;
1128 set_bit(SLF_ERROR, &sl->flags);
1133 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1135 /* Perform I/O control on an active SLIP channel. */
1136 static int slip_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1138 struct slip *sl = (struct slip *) tty->disc_data;
1139 unsigned int tmp;
1140 int __user *p = (int __user *)arg;
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 __user *)arg, sl->dev->name, tmp))
1151 return -EFAULT;
1152 return 0;
1154 case SIOCGIFENCAP:
1155 if (put_user(sl->mode, p))
1156 return -EFAULT;
1157 return 0;
1159 case SIOCSIFENCAP:
1160 if (get_user(tmp, p))
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, p))
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, p))
1209 return -EFAULT;
1210 return 0;
1212 case SIOCSOUTFILL:
1213 if (get_user(tmp, p))
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, p))
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 = netdev_priv(dev);
1257 unsigned long *p = (unsigned long *)&rq->ifr_ifru;
1259 if (sl == NULL) /* Allocation failed ?? */
1260 return -ENODEV;
1262 spin_lock_bh(&sl->lock);
1264 if (!sl->tty) {
1265 spin_unlock_bh(&sl->lock);
1266 return -ENODEV;
1269 switch(cmd){
1270 case SIOCSKEEPALIVE:
1271 /* max for unchar */
1272 if ((unsigned)*p > 255) {
1273 spin_unlock_bh(&sl->lock);
1274 return -EINVAL;
1276 sl->keepalive = (unchar) *p;
1277 if (sl->keepalive != 0) {
1278 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1279 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1280 set_bit(SLF_KEEPTEST, &sl->flags);
1281 } else {
1282 del_timer(&sl->keepalive_timer);
1284 break;
1286 case SIOCGKEEPALIVE:
1287 *p = sl->keepalive;
1288 break;
1290 case SIOCSOUTFILL:
1291 if ((unsigned)*p > 255) { /* max for unchar */
1292 spin_unlock_bh(&sl->lock);
1293 return -EINVAL;
1295 if ((sl->outfill = (unchar)*p) != 0){
1296 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1297 set_bit(SLF_OUTWAIT, &sl->flags);
1298 } else {
1299 del_timer (&sl->outfill_timer);
1301 break;
1303 case SIOCGOUTFILL:
1304 *p = sl->outfill;
1305 break;
1307 case SIOCSLEASE:
1308 /* Resolve race condition, when ioctl'ing hanged up
1309 and opened by another process device.
1311 if (sl->tty != current->signal->tty && sl->pid != current->pid) {
1312 spin_unlock_bh(&sl->lock);
1313 return -EPERM;
1315 sl->leased = 0;
1316 if (*p)
1317 sl->leased = 1;
1318 break;
1320 case SIOCGLEASE:
1321 *p = sl->leased;
1323 spin_unlock_bh(&sl->lock);
1324 return 0;
1326 #endif
1327 /* VSV changes end */
1329 static struct tty_ldisc sl_ldisc = {
1330 .owner = THIS_MODULE,
1331 .magic = TTY_LDISC_MAGIC,
1332 .name = "slip",
1333 .open = slip_open,
1334 .close = slip_close,
1335 .ioctl = slip_ioctl,
1336 .receive_buf = slip_receive_buf,
1337 .receive_room = slip_receive_room,
1338 .write_wakeup = slip_write_wakeup,
1341 static int __init slip_init(void)
1343 int status;
1345 if (slip_maxdev < 4)
1346 slip_maxdev = 4; /* Sanity */
1348 printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1349 #ifdef CONFIG_SLIP_MODE_SLIP6
1350 " (6 bit encapsulation enabled)"
1351 #endif
1352 ".\n",
1353 SLIP_VERSION, slip_maxdev );
1354 #if defined(SL_INCLUDE_CSLIP)
1355 printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
1356 #endif
1357 #ifdef CONFIG_SLIP_SMART
1358 printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1359 #endif
1361 slip_devs = kmalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL);
1362 if (!slip_devs) {
1363 printk(KERN_ERR "SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1364 return -ENOMEM;
1367 /* Clear the pointer array, we allocate devices when we need them */
1368 memset(slip_devs, 0, sizeof(struct net_device *)*slip_maxdev);
1370 /* Fill in our line protocol discipline, and register it */
1371 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1372 printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
1373 kfree(slip_devs);
1375 return status;
1378 static void __exit slip_exit(void)
1380 int i;
1381 struct net_device *dev;
1382 struct slip *sl;
1383 unsigned long timeout = jiffies + HZ;
1384 int busy = 0;
1386 if (slip_devs == NULL)
1387 return;
1389 /* First of all: check for active disciplines and hangup them.
1391 do {
1392 if (busy) {
1393 set_current_state(TASK_INTERRUPTIBLE);
1394 schedule_timeout(HZ / 10);
1397 busy = 0;
1398 for (i = 0; i < slip_maxdev; i++) {
1399 dev = slip_devs[i];
1400 if (!dev)
1401 continue;
1402 sl = netdev_priv(dev);
1403 spin_lock_bh(&sl->lock);
1404 if (sl->tty) {
1405 busy++;
1406 tty_hangup(sl->tty);
1408 spin_unlock_bh(&sl->lock);
1410 } while (busy && time_before(jiffies, timeout));
1413 for (i = 0; i < slip_maxdev; i++) {
1414 dev = slip_devs[i];
1415 if (!dev)
1416 continue;
1417 slip_devs[i] = NULL;
1419 sl = netdev_priv(dev);
1420 if (sl->tty) {
1421 printk(KERN_ERR "%s: tty discipline still running\n",
1422 dev->name);
1423 /* Intentionally leak the control block. */
1424 dev->destructor = NULL;
1427 unregister_netdev(dev);
1430 kfree(slip_devs);
1431 slip_devs = NULL;
1433 if ((i = tty_register_ldisc(N_SLIP, NULL)))
1435 printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
1439 module_init(slip_init);
1440 module_exit(slip_exit);
1442 #ifdef CONFIG_SLIP_SMART
1444 * This is start of the code for multislip style line checking
1445 * added by Stanislav Voronyi. All changes before marked VSV
1448 static void sl_outfill(unsigned long sls)
1450 struct slip *sl=(struct slip *)sls;
1452 spin_lock(&sl->lock);
1454 if (sl->tty == NULL)
1455 goto out;
1457 if(sl->outfill)
1459 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1461 /* no packets were transmitted, do outfill */
1462 #ifdef CONFIG_SLIP_MODE_SLIP6
1463 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1464 #else
1465 unsigned char s = END;
1466 #endif
1467 /* put END into tty queue. Is it right ??? */
1468 if (!netif_queue_stopped(sl->dev))
1470 /* if device busy no outfill */
1471 sl->tty->driver->write(sl->tty, &s, 1);
1474 else
1475 set_bit(SLF_OUTWAIT, &sl->flags);
1477 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1479 out:
1480 spin_unlock(&sl->lock);
1483 static void sl_keepalive(unsigned long sls)
1485 struct slip *sl=(struct slip *)sls;
1487 spin_lock(&sl->lock);
1489 if (sl->tty == NULL)
1490 goto out;
1492 if( sl->keepalive)
1494 if(test_bit(SLF_KEEPTEST, &sl->flags))
1496 /* keepalive still high :(, we must hangup */
1497 if( sl->outfill ) /* outfill timer must be deleted too */
1498 (void)del_timer(&sl->outfill_timer);
1499 printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1500 tty_hangup(sl->tty); /* this must hangup tty & close slip */
1501 /* I think we need not something else */
1502 goto out;
1504 else
1505 set_bit(SLF_KEEPTEST, &sl->flags);
1507 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1510 out:
1511 spin_unlock(&sl->lock);
1514 #endif
1515 MODULE_LICENSE("GPL");
1516 MODULE_ALIAS_LDISC(N_SLIP);