MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / slip.c
blob0c1995bc284863d282e214611587f756ce8a21b0
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 = free_netdev;
644 dev->stop = sl_close;
645 dev->get_stats = sl_get_stats;
646 dev->change_mtu = sl_change_mtu;
647 dev->hard_start_xmit = sl_xmit;
648 #ifdef CONFIG_SLIP_SMART
649 dev->do_ioctl = sl_ioctl;
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. This will not
676 * be re-entered while running but other ldisc functions may be called
677 * in parallel
680 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
682 struct slip *sl = (struct slip *) tty->disc_data;
684 if (!sl || sl->magic != SLIP_MAGIC ||
685 !netif_running(sl->dev))
686 return;
688 /* Read the characters out of the buffer */
689 while (count--) {
690 if (fp && *fp++) {
691 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
692 sl->rx_errors++;
694 cp++;
695 continue;
697 #ifdef CONFIG_SLIP_MODE_SLIP6
698 if (sl->mode & SL_MODE_SLIP6)
699 slip_unesc6(sl, *cp++);
700 else
701 #endif
702 slip_unesc(sl, *cp++);
706 /************************************
707 * slip_open helper routines.
708 ************************************/
710 /* Collect hanged up channels */
712 static void sl_sync(void)
714 int i;
715 struct net_device *dev;
716 struct slip *sl;
718 for (i = 0; i < slip_maxdev; i++) {
719 if ((dev = slip_devs[i]) == NULL)
720 break;
722 sl = dev->priv;
723 if (sl->tty || sl->leased)
724 continue;
725 if (dev->flags&IFF_UP)
726 dev_close(dev);
731 /* Find a free SLIP channel, and link in this `tty' line. */
732 static struct slip *
733 sl_alloc(dev_t line)
735 int i;
736 int sel = -1;
737 int score = -1;
738 struct net_device *dev = NULL;
739 struct slip *sl;
741 if (slip_devs == NULL)
742 return NULL; /* Master array missing ! */
744 for (i = 0; i < slip_maxdev; i++) {
745 dev = slip_devs[i];
746 if (dev == NULL)
747 break;
749 sl = dev->priv;
750 if (sl->leased) {
751 if (sl->line != line)
752 continue;
753 if (sl->tty)
754 return NULL;
756 /* Clear ESCAPE & ERROR flags */
757 sl->flags &= (1 << SLF_INUSE);
758 return sl;
761 if (sl->tty)
762 continue;
764 if (current->pid == sl->pid) {
765 if (sl->line == line && score < 3) {
766 sel = i;
767 score = 3;
768 continue;
770 if (score < 2) {
771 sel = i;
772 score = 2;
774 continue;
776 if (sl->line == line && score < 1) {
777 sel = i;
778 score = 1;
779 continue;
781 if (score < 0) {
782 sel = i;
783 score = 0;
787 if (sel >= 0) {
788 i = sel;
789 dev = slip_devs[i];
790 if (score > 1) {
791 sl = dev->priv;
792 sl->flags &= (1 << SLF_INUSE);
793 return sl;
797 /* Sorry, too many, all slots in use */
798 if (i >= slip_maxdev)
799 return NULL;
801 if (dev) {
802 sl = dev->priv;
803 if (test_bit(SLF_INUSE, &sl->flags)) {
804 unregister_netdevice(dev);
805 dev = NULL;
806 slip_devs[i] = NULL;
810 if (!dev) {
811 char name[IFNAMSIZ];
812 sprintf(name, "sl%d", i);
814 dev = alloc_netdev(sizeof(*sl), name, sl_setup);
815 if (!dev)
816 return NULL;
817 dev->base_addr = i;
820 sl = dev->priv;
822 /* Initialize channel control data */
823 sl->magic = SLIP_MAGIC;
824 sl->dev = dev;
825 spin_lock_init(&sl->lock);
826 sl->mode = SL_MODE_DEFAULT;
827 #ifdef CONFIG_SLIP_SMART
828 init_timer(&sl->keepalive_timer); /* initialize timer_list struct */
829 sl->keepalive_timer.data=(unsigned long)sl;
830 sl->keepalive_timer.function=sl_keepalive;
831 init_timer(&sl->outfill_timer);
832 sl->outfill_timer.data=(unsigned long)sl;
833 sl->outfill_timer.function=sl_outfill;
834 #endif
835 slip_devs[i] = dev;
837 return sl;
841 * Open the high-level part of the SLIP channel.
842 * This function is called by the TTY module when the
843 * SLIP line discipline is called for. Because we are
844 * sure the tty line exists, we only have to link it to
845 * a free SLIP channel...
847 * Called in process context serialized from other ldisc calls.
850 static int slip_open(struct tty_struct *tty)
852 struct slip *sl;
853 int err;
855 if(!capable(CAP_NET_ADMIN))
856 return -EPERM;
858 /* RTnetlink lock is misused here to serialize concurrent
859 opens of slip channels. There are better ways, but it is
860 the simplest one.
862 rtnl_lock();
864 /* Collect hanged up channels. */
865 sl_sync();
867 sl = (struct slip *) tty->disc_data;
869 err = -EEXIST;
870 /* First make sure we're not already connected. */
871 if (sl && sl->magic == SLIP_MAGIC)
872 goto err_exit;
874 /* OK. Find a free SLIP channel to use. */
875 err = -ENFILE;
876 if ((sl = sl_alloc(tty_devnum(tty))) == NULL)
877 goto err_exit;
879 sl->tty = tty;
880 tty->disc_data = sl;
881 sl->line = tty_devnum(tty);
882 sl->pid = current->pid;
884 /* FIXME: already done before we were called - seems this can go */
885 if (tty->driver->flush_buffer)
886 tty->driver->flush_buffer(tty);
888 if (!test_bit(SLF_INUSE, &sl->flags)) {
889 /* Perform the low-level SLIP initialization. */
890 if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
891 goto err_free_chan;
893 set_bit(SLF_INUSE, &sl->flags);
895 if ((err = register_netdevice(sl->dev)))
896 goto err_free_bufs;
899 #ifdef CONFIG_SLIP_SMART
900 if (sl->keepalive) {
901 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
902 add_timer (&sl->keepalive_timer);
904 if (sl->outfill) {
905 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
906 add_timer (&sl->outfill_timer);
908 #endif
910 /* Done. We have linked the TTY line to a channel. */
911 rtnl_unlock();
912 return sl->dev->base_addr;
914 err_free_bufs:
915 sl_free_bufs(sl);
917 err_free_chan:
918 sl->tty = NULL;
919 tty->disc_data = NULL;
920 clear_bit(SLF_INUSE, &sl->flags);
922 err_exit:
923 rtnl_unlock();
925 /* Count references from TTY module */
926 return err;
931 FIXME: 1,2 are fixed 3 was never true anyway.
933 Let me to blame a bit.
934 1. TTY module calls this funstion on soft interrupt.
935 2. TTY module calls this function WITH MASKED INTERRUPTS!
936 3. TTY module does not notify us about line discipline
937 shutdown,
939 Seems, now it is clean. The solution is to consider netdevice and
940 line discipline sides as two independent threads.
942 By-product (not desired): sl? does not feel hangups and remains open.
943 It is supposed, that user level program (dip, diald, slattach...)
944 will catch SIGHUP and make the rest of work.
946 I see no way to make more with current tty code. --ANK
950 * Close down a SLIP channel.
951 * This means flushing out any pending queues, and then returning. This
952 * call is serialized against other ldisc functions.
954 static void
955 slip_close(struct tty_struct *tty)
957 struct slip *sl = (struct slip *) tty->disc_data;
959 /* First make sure we're connected. */
960 if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
961 return;
963 tty->disc_data = NULL;
964 sl->tty = NULL;
965 if (!sl->leased)
966 sl->line = 0;
968 /* VSV = very important to remove timers */
969 #ifdef CONFIG_SLIP_SMART
970 del_timer_sync(&sl->keepalive_timer);
971 del_timer_sync(&sl->outfill_timer);
972 #endif
974 /* Count references from TTY module */
977 /************************************************************************
978 * STANDARD SLIP ENCAPSULATION *
979 ************************************************************************/
982 slip_esc(unsigned char *s, unsigned char *d, int len)
984 unsigned char *ptr = d;
985 unsigned char c;
988 * Send an initial END character to flush out any
989 * data that may have accumulated in the receiver
990 * due to line noise.
993 *ptr++ = END;
996 * For each byte in the packet, send the appropriate
997 * character sequence, according to the SLIP protocol.
1000 while (len-- > 0) {
1001 switch(c = *s++) {
1002 case END:
1003 *ptr++ = ESC;
1004 *ptr++ = ESC_END;
1005 break;
1006 case ESC:
1007 *ptr++ = ESC;
1008 *ptr++ = ESC_ESC;
1009 break;
1010 default:
1011 *ptr++ = c;
1012 break;
1015 *ptr++ = END;
1016 return (ptr - d);
1019 static void slip_unesc(struct slip *sl, unsigned char s)
1022 switch(s) {
1023 case END:
1024 #ifdef CONFIG_SLIP_SMART
1025 /* drop keeptest bit = VSV */
1026 if (test_bit(SLF_KEEPTEST, &sl->flags))
1027 clear_bit(SLF_KEEPTEST, &sl->flags);
1028 #endif
1030 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1031 sl_bump(sl);
1033 clear_bit(SLF_ESCAPE, &sl->flags);
1034 sl->rcount = 0;
1035 return;
1037 case ESC:
1038 set_bit(SLF_ESCAPE, &sl->flags);
1039 return;
1040 case ESC_ESC:
1041 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1042 s = ESC;
1044 break;
1045 case ESC_END:
1046 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1047 s = END;
1049 break;
1051 if (!test_bit(SLF_ERROR, &sl->flags)) {
1052 if (sl->rcount < sl->buffsize) {
1053 sl->rbuff[sl->rcount++] = s;
1054 return;
1056 sl->rx_over_errors++;
1057 set_bit(SLF_ERROR, &sl->flags);
1062 #ifdef CONFIG_SLIP_MODE_SLIP6
1063 /************************************************************************
1064 * 6 BIT SLIP ENCAPSULATION *
1065 ************************************************************************/
1068 slip_esc6(unsigned char *s, unsigned char *d, int len)
1070 unsigned char *ptr = d;
1071 unsigned char c;
1072 int i;
1073 unsigned short v = 0;
1074 short bits = 0;
1077 * Send an initial END character to flush out any
1078 * data that may have accumulated in the receiver
1079 * due to line noise.
1082 *ptr++ = 0x70;
1085 * Encode the packet into printable ascii characters
1088 for (i = 0; i < len; ++i) {
1089 v = (v << 8) | s[i];
1090 bits += 8;
1091 while (bits >= 6) {
1092 bits -= 6;
1093 c = 0x30 + ((v >> bits) & 0x3F);
1094 *ptr++ = c;
1097 if (bits) {
1098 c = 0x30 + ((v << (6 - bits)) & 0x3F);
1099 *ptr++ = c;
1101 *ptr++ = 0x70;
1102 return ptr - d;
1105 void
1106 slip_unesc6(struct slip *sl, unsigned char s)
1108 unsigned char c;
1110 if (s == 0x70) {
1111 #ifdef CONFIG_SLIP_SMART
1112 /* drop keeptest bit = VSV */
1113 if (test_bit(SLF_KEEPTEST, &sl->flags))
1114 clear_bit(SLF_KEEPTEST, &sl->flags);
1115 #endif
1117 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1118 sl_bump(sl);
1120 sl->rcount = 0;
1121 sl->xbits = 0;
1122 sl->xdata = 0;
1123 } else if (s >= 0x30 && s < 0x70) {
1124 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1125 sl->xbits += 6;
1126 if (sl->xbits >= 8) {
1127 sl->xbits -= 8;
1128 c = (unsigned char)(sl->xdata >> sl->xbits);
1129 if (!test_bit(SLF_ERROR, &sl->flags)) {
1130 if (sl->rcount < sl->buffsize) {
1131 sl->rbuff[sl->rcount++] = c;
1132 return;
1134 sl->rx_over_errors++;
1135 set_bit(SLF_ERROR, &sl->flags);
1140 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1142 /* Perform I/O control on an active SLIP channel. */
1143 static int slip_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1145 struct slip *sl = (struct slip *) tty->disc_data;
1146 unsigned int tmp;
1147 int __user *p = (int __user *)arg;
1149 /* First make sure we're connected. */
1150 if (!sl || sl->magic != SLIP_MAGIC) {
1151 return -EINVAL;
1154 switch(cmd) {
1155 case SIOCGIFNAME:
1156 tmp = strlen(sl->dev->name) + 1;
1157 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
1158 return -EFAULT;
1159 return 0;
1161 case SIOCGIFENCAP:
1162 if (put_user(sl->mode, p))
1163 return -EFAULT;
1164 return 0;
1166 case SIOCSIFENCAP:
1167 if (get_user(tmp, p))
1168 return -EFAULT;
1169 #ifndef SL_INCLUDE_CSLIP
1170 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1171 return -EINVAL;
1173 #else
1174 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1175 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1176 /* return -EINVAL; */
1177 tmp &= ~SL_MODE_ADAPTIVE;
1179 #endif
1180 #ifndef CONFIG_SLIP_MODE_SLIP6
1181 if (tmp & SL_MODE_SLIP6) {
1182 return -EINVAL;
1184 #endif
1185 sl->mode = tmp;
1186 sl->dev->type = ARPHRD_SLIP+sl->mode;
1187 return 0;
1189 case SIOCSIFHWADDR:
1190 return -EINVAL;
1192 #ifdef CONFIG_SLIP_SMART
1193 /* VSV changes start here */
1194 case SIOCSKEEPALIVE:
1195 if (get_user(tmp, p))
1196 return -EFAULT;
1197 if (tmp > 255) /* max for unchar */
1198 return -EINVAL;
1200 spin_lock_bh(&sl->lock);
1201 if (!sl->tty) {
1202 spin_unlock_bh(&sl->lock);
1203 return -ENODEV;
1205 if ((sl->keepalive = (unchar) tmp) != 0) {
1206 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1207 set_bit(SLF_KEEPTEST, &sl->flags);
1208 } else {
1209 del_timer (&sl->keepalive_timer);
1211 spin_unlock_bh(&sl->lock);
1212 return 0;
1214 case SIOCGKEEPALIVE:
1215 if (put_user(sl->keepalive, p))
1216 return -EFAULT;
1217 return 0;
1219 case SIOCSOUTFILL:
1220 if (get_user(tmp, p))
1221 return -EFAULT;
1222 if (tmp > 255) /* max for unchar */
1223 return -EINVAL;
1224 spin_lock_bh(&sl->lock);
1225 if (!sl->tty) {
1226 spin_unlock_bh(&sl->lock);
1227 return -ENODEV;
1229 if ((sl->outfill = (unchar) tmp) != 0){
1230 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1231 set_bit(SLF_OUTWAIT, &sl->flags);
1232 } else {
1233 del_timer (&sl->outfill_timer);
1235 spin_unlock_bh(&sl->lock);
1236 return 0;
1238 case SIOCGOUTFILL:
1239 if (put_user(sl->outfill, p))
1240 return -EFAULT;
1241 return 0;
1242 /* VSV changes end */
1243 #endif
1245 /* Allow stty to read, but not set, the serial port */
1246 case TCGETS:
1247 case TCGETA:
1248 return n_tty_ioctl(tty, file, cmd, arg);
1250 default:
1251 return -ENOIOCTLCMD;
1255 /* VSV changes start here */
1256 #ifdef CONFIG_SLIP_SMART
1257 /* function do_ioctl called from net/core/dev.c
1258 to allow get/set outfill/keepalive parameter
1259 by ifconfig */
1261 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
1263 struct slip *sl = (struct slip*)(dev->priv);
1264 unsigned long *p = (unsigned long *)&rq->ifr_ifru;
1266 if (sl == NULL) /* Allocation failed ?? */
1267 return -ENODEV;
1269 spin_lock_bh(&sl->lock);
1271 if (!sl->tty) {
1272 spin_unlock_bh(&sl->lock);
1273 return -ENODEV;
1276 switch(cmd){
1277 case SIOCSKEEPALIVE:
1278 /* max for unchar */
1279 if ((unsigned)*p > 255) {
1280 spin_unlock_bh(&sl->lock);
1281 return -EINVAL;
1283 sl->keepalive = (unchar) *p;
1284 if (sl->keepalive != 0) {
1285 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1286 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1287 set_bit(SLF_KEEPTEST, &sl->flags);
1288 } else {
1289 del_timer(&sl->keepalive_timer);
1291 break;
1293 case SIOCGKEEPALIVE:
1294 *p = sl->keepalive;
1295 break;
1297 case SIOCSOUTFILL:
1298 if ((unsigned)*p > 255) { /* max for unchar */
1299 spin_unlock_bh(&sl->lock);
1300 return -EINVAL;
1302 if ((sl->outfill = (unchar)*p) != 0){
1303 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1304 set_bit(SLF_OUTWAIT, &sl->flags);
1305 } else {
1306 del_timer (&sl->outfill_timer);
1308 break;
1310 case SIOCGOUTFILL:
1311 *p = sl->outfill;
1312 break;
1314 case SIOCSLEASE:
1315 /* Resolve race condition, when ioctl'ing hanged up
1316 and opened by another process device.
1318 if (sl->tty != current->signal->tty && sl->pid != current->pid) {
1319 spin_unlock_bh(&sl->lock);
1320 return -EPERM;
1322 sl->leased = 0;
1323 if (*p)
1324 sl->leased = 1;
1325 break;
1327 case SIOCGLEASE:
1328 *p = sl->leased;
1330 spin_unlock_bh(&sl->lock);
1331 return 0;
1333 #endif
1334 /* VSV changes end */
1336 static struct tty_ldisc sl_ldisc = {
1337 .owner = THIS_MODULE,
1338 .magic = TTY_LDISC_MAGIC,
1339 .name = "slip",
1340 .open = slip_open,
1341 .close = slip_close,
1342 .ioctl = slip_ioctl,
1343 .receive_buf = slip_receive_buf,
1344 .receive_room = slip_receive_room,
1345 .write_wakeup = slip_write_wakeup,
1348 static int __init slip_init(void)
1350 int status;
1352 if (slip_maxdev < 4)
1353 slip_maxdev = 4; /* Sanity */
1355 printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1356 #ifdef CONFIG_SLIP_MODE_SLIP6
1357 " (6 bit encapsulation enabled)"
1358 #endif
1359 ".\n",
1360 SLIP_VERSION, slip_maxdev );
1361 #if defined(SL_INCLUDE_CSLIP)
1362 printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
1363 #endif
1364 #ifdef CONFIG_SLIP_SMART
1365 printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1366 #endif
1368 slip_devs = kmalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL);
1369 if (!slip_devs) {
1370 printk(KERN_ERR "SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n");
1371 return -ENOMEM;
1374 /* Clear the pointer array, we allocate devices when we need them */
1375 memset(slip_devs, 0, sizeof(struct net_device *)*slip_maxdev);
1377 /* Fill in our line protocol discipline, and register it */
1378 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1379 printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
1380 kfree(slip_devs);
1382 return status;
1385 static void __exit slip_exit(void)
1387 int i;
1388 struct net_device *dev;
1389 struct slip *sl;
1390 unsigned long timeout = jiffies + HZ;
1391 int busy = 0;
1393 if (slip_devs == NULL)
1394 return;
1396 /* First of all: check for active disciplines and hangup them.
1398 do {
1399 if (busy) {
1400 set_current_state(TASK_INTERRUPTIBLE);
1401 schedule_timeout(HZ / 10);
1404 busy = 0;
1405 for (i = 0; i < slip_maxdev; i++) {
1406 dev = slip_devs[i];
1407 if (!dev)
1408 continue;
1409 sl = dev->priv;
1410 spin_lock_bh(&sl->lock);
1411 if (sl->tty) {
1412 busy++;
1413 tty_hangup(sl->tty);
1415 spin_unlock_bh(&sl->lock);
1417 } while (busy && time_before(jiffies, timeout));
1420 for (i = 0; i < slip_maxdev; i++) {
1421 dev = slip_devs[i];
1422 if (!dev)
1423 continue;
1424 slip_devs[i] = NULL;
1426 sl = dev->priv;
1427 if (sl->tty) {
1428 printk(KERN_ERR "%s: tty discipline still running\n",
1429 dev->name);
1430 /* Intentionally leak the control block. */
1431 dev->destructor = NULL;
1434 unregister_netdev(dev);
1437 kfree(slip_devs);
1438 slip_devs = NULL;
1440 if ((i = tty_register_ldisc(N_SLIP, NULL)))
1442 printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
1446 module_init(slip_init);
1447 module_exit(slip_exit);
1449 #ifdef CONFIG_SLIP_SMART
1451 * This is start of the code for multislip style line checking
1452 * added by Stanislav Voronyi. All changes before marked VSV
1455 static void sl_outfill(unsigned long sls)
1457 struct slip *sl=(struct slip *)sls;
1459 spin_lock(&sl->lock);
1461 if (sl->tty == NULL)
1462 goto out;
1464 if(sl->outfill)
1466 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1468 /* no packets were transmitted, do outfill */
1469 #ifdef CONFIG_SLIP_MODE_SLIP6
1470 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1471 #else
1472 unsigned char s = END;
1473 #endif
1474 /* put END into tty queue. Is it right ??? */
1475 if (!netif_queue_stopped(sl->dev))
1477 /* if device busy no outfill */
1478 sl->tty->driver->write(sl->tty, 0, &s, 1);
1481 else
1482 set_bit(SLF_OUTWAIT, &sl->flags);
1484 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1486 out:
1487 spin_unlock(&sl->lock);
1490 static void sl_keepalive(unsigned long sls)
1492 struct slip *sl=(struct slip *)sls;
1494 spin_lock(&sl->lock);
1496 if (sl->tty == NULL)
1497 goto out;
1499 if( sl->keepalive)
1501 if(test_bit(SLF_KEEPTEST, &sl->flags))
1503 /* keepalive still high :(, we must hangup */
1504 if( sl->outfill ) /* outfill timer must be deleted too */
1505 (void)del_timer(&sl->outfill_timer);
1506 printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1507 tty_hangup(sl->tty); /* this must hangup tty & close slip */
1508 /* I think we need not something else */
1509 goto out;
1511 else
1512 set_bit(SLF_KEEPTEST, &sl->flags);
1514 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1517 out:
1518 spin_unlock(&sl->lock);
1521 #endif
1522 MODULE_LICENSE("GPL");
1523 MODULE_ALIAS_LDISC(N_SLIP);