- Andries Brouwer: final isofs pieces.
[davej-history.git] / drivers / net / slip.c
blob6e8427a3b367dd22411b6295b02a4efe53c5fe5f
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 #ifdef MODULE
85 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY-MODULAR"
86 #else
87 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
88 #endif
91 typedef struct slip_ctrl {
92 struct slip ctrl; /* SLIP things */
93 struct net_device dev; /* the device */
94 } slip_ctrl_t;
95 static slip_ctrl_t **slip_ctrls = NULL;
97 int slip_maxdev = SL_NRUNIT; /* Can be overridden with insmod! */
98 MODULE_PARM(slip_maxdev, "i");
100 static struct tty_ldisc sl_ldisc;
102 static int slip_esc(unsigned char *p, unsigned char *d, int len);
103 static void slip_unesc(struct slip *sl, unsigned char c);
104 #ifdef CONFIG_SLIP_MODE_SLIP6
105 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
106 static void slip_unesc6(struct slip *sl, unsigned char c);
107 #endif
108 #ifdef CONFIG_SLIP_SMART
109 static void sl_keepalive(unsigned long sls);
110 static void sl_outfill(unsigned long sls);
111 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd);
112 #endif
114 /********************************
115 * Buffer administration routines:
116 * sl_alloc_bufs()
117 * sl_free_bufs()
118 * sl_realloc_bufs()
120 * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
121 * sl_realloc_bufs provides strong atomicity and reallocation
122 * on actively running device.
123 *********************************/
126 Allocate channel buffers.
129 static int
130 sl_alloc_bufs(struct slip *sl, int mtu)
132 int err = -ENOBUFS;
133 unsigned long len;
134 char * rbuff = NULL;
135 char * xbuff = NULL;
136 #ifdef SL_INCLUDE_CSLIP
137 char * cbuff = NULL;
138 struct slcompress *slcomp = NULL;
139 #endif
142 * Allocate the SLIP frame buffers:
144 * rbuff Receive buffer.
145 * xbuff Transmit buffer.
146 * cbuff Temporary compression buffer.
148 len = mtu * 2;
151 * allow for arrival of larger UDP packets, even if we say not to
152 * also fixes a bug in which SunOS sends 512-byte packets even with
153 * an MSS of 128
155 if (len < 576 * 2)
156 len = 576 * 2;
157 rbuff = kmalloc(len + 4, GFP_KERNEL);
158 if (rbuff == NULL)
159 goto err_exit;
160 xbuff = kmalloc(len + 4, GFP_KERNEL);
161 if (xbuff == NULL)
162 goto err_exit;
163 #ifdef SL_INCLUDE_CSLIP
164 cbuff = kmalloc(len + 4, GFP_KERNEL);
165 if (cbuff == NULL)
166 goto err_exit;
167 slcomp = slhc_init(16, 16);
168 if (slcomp == NULL)
169 goto err_exit;
170 #endif
171 spin_lock_bh(&sl->lock);
172 if (sl->tty == NULL) {
173 spin_unlock_bh(&sl->lock);
174 err = -ENODEV;
175 goto err_exit;
177 sl->mtu = mtu;
178 sl->buffsize = len;
179 sl->rcount = 0;
180 sl->xleft = 0;
181 rbuff = xchg(&sl->rbuff, rbuff);
182 xbuff = xchg(&sl->xbuff, xbuff);
183 #ifdef SL_INCLUDE_CSLIP
184 cbuff = xchg(&sl->cbuff, cbuff);
185 slcomp = xchg(&sl->slcomp, slcomp);
186 #ifdef CONFIG_SLIP_MODE_SLIP6
187 sl->xdata = 0;
188 sl->xbits = 0;
189 #endif
190 #endif
191 spin_unlock_bh(&sl->lock);
192 err = 0;
194 /* Cleanup */
195 err_exit:
196 #ifdef SL_INCLUDE_CSLIP
197 if (cbuff)
198 kfree(cbuff);
199 if (slcomp)
200 slhc_free(slcomp);
201 #endif
202 if (xbuff)
203 kfree(xbuff);
204 if (rbuff)
205 kfree(rbuff);
206 return err;
209 /* Free a SLIP channel buffers. */
210 static void
211 sl_free_bufs(struct slip *sl)
213 void * tmp;
215 /* Free all SLIP frame buffers. */
216 if ((tmp = xchg(&sl->rbuff, NULL)) != NULL)
217 kfree(tmp);
218 if ((tmp = xchg(&sl->xbuff, NULL)) != NULL)
219 kfree(tmp);
220 #ifdef SL_INCLUDE_CSLIP
221 if ((tmp = xchg(&sl->cbuff, NULL)) != NULL)
222 kfree(tmp);
223 if ((tmp = xchg(&sl->slcomp, NULL)) != NULL)
224 slhc_free(tmp);
225 #endif
229 Reallocate slip channel buffers.
232 static int sl_realloc_bufs(struct slip *sl, int mtu)
234 int err = 0;
235 struct net_device *dev = sl->dev;
236 unsigned char *xbuff, *rbuff;
237 #ifdef SL_INCLUDE_CSLIP
238 unsigned char *cbuff;
239 #endif
240 int len = mtu * 2;
243 * allow for arrival of larger UDP packets, even if we say not to
244 * also fixes a bug in which SunOS sends 512-byte packets even with
245 * an MSS of 128
247 if (len < 576 * 2)
248 len = 576 * 2;
250 xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
251 rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
252 #ifdef SL_INCLUDE_CSLIP
253 cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
254 #endif
257 #ifdef SL_INCLUDE_CSLIP
258 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
259 #else
260 if (xbuff == NULL || rbuff == NULL) {
261 #endif
262 if (mtu >= sl->mtu) {
263 printk("%s: unable to grow slip buffers, MTU change cancelled.\n",
264 dev->name);
265 err = -ENOBUFS;
267 goto done;
270 spin_lock_bh(&sl->lock);
272 err = -ENODEV;
273 if (sl->tty == NULL)
274 goto done_on_bh;
276 xbuff = xchg(&sl->xbuff, xbuff);
277 rbuff = xchg(&sl->rbuff, rbuff);
278 #ifdef SL_INCLUDE_CSLIP
279 cbuff = xchg(&sl->cbuff, cbuff);
280 #endif
281 if (sl->xleft) {
282 if (sl->xleft <= len) {
283 memcpy(sl->xbuff, sl->xhead, sl->xleft);
284 } else {
285 sl->xleft = 0;
286 sl->tx_dropped++;
289 sl->xhead = sl->xbuff;
291 if (sl->rcount) {
292 if (sl->rcount <= len) {
293 memcpy(sl->rbuff, rbuff, sl->rcount);
294 } else {
295 sl->rcount = 0;
296 sl->rx_over_errors++;
297 set_bit(SLF_ERROR, &sl->flags);
300 sl->mtu = mtu;
301 dev->mtu = mtu;
302 sl->buffsize = len;
303 err = 0;
305 done_on_bh:
306 spin_unlock_bh(&sl->lock);
308 done:
309 if (xbuff)
310 kfree(xbuff);
311 if (rbuff)
312 kfree(rbuff);
313 #ifdef SL_INCLUDE_CSLIP
314 if (cbuff)
315 kfree(cbuff);
316 #endif
317 return err;
321 /* Set the "sending" flag. This must be atomic hence the set_bit. */
322 static inline void
323 sl_lock(struct slip *sl)
325 netif_stop_queue(sl->dev);
329 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
330 static inline void
331 sl_unlock(struct slip *sl)
333 netif_wake_queue(sl->dev);
336 /* Send one completely decapsulated IP datagram to the IP layer. */
337 static void
338 sl_bump(struct slip *sl)
340 struct sk_buff *skb;
341 int count;
343 count = sl->rcount;
344 #ifdef SL_INCLUDE_CSLIP
345 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
346 unsigned char c;
347 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
348 /* ignore compressed packets when CSLIP is off */
349 if (!(sl->mode & SL_MODE_CSLIP)) {
350 printk("%s: compressed packet ignored\n", sl->dev->name);
351 return;
353 /* make sure we've reserved enough space for uncompress to use */
354 if (count + 80 > sl->buffsize) {
355 sl->rx_over_errors++;
356 return;
358 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
359 if (count <= 0) {
360 return;
362 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
363 if (!(sl->mode & SL_MODE_CSLIP)) {
364 /* turn on header compression */
365 sl->mode |= SL_MODE_CSLIP;
366 sl->mode &= ~SL_MODE_ADAPTIVE;
367 printk("%s: header compression turned on\n", sl->dev->name);
369 sl->rbuff[0] &= 0x4f;
370 if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
371 return;
375 #endif /* SL_INCLUDE_CSLIP */
377 sl->rx_bytes+=count;
379 skb = dev_alloc_skb(count);
380 if (skb == NULL) {
381 printk("%s: memory squeeze, dropping packet.\n", sl->dev->name);
382 sl->rx_dropped++;
383 return;
385 skb->dev = sl->dev;
386 memcpy(skb_put(skb,count), sl->rbuff, count);
387 skb->mac.raw=skb->data;
388 skb->protocol=htons(ETH_P_IP);
389 netif_rx(skb);
390 sl->rx_packets++;
393 /* Encapsulate one IP datagram and stuff into a TTY queue. */
394 static void
395 sl_encaps(struct slip *sl, unsigned char *icp, int len)
397 unsigned char *p;
398 int actual, count;
400 if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */
401 printk ("%s: truncating oversized transmit packet!\n", sl->dev->name);
402 sl->tx_dropped++;
403 sl_unlock(sl);
404 return;
407 p = icp;
408 #ifdef SL_INCLUDE_CSLIP
409 if (sl->mode & SL_MODE_CSLIP) {
410 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
412 #endif
413 #ifdef CONFIG_SLIP_MODE_SLIP6
414 if(sl->mode & SL_MODE_SLIP6)
415 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
416 else
417 #endif
418 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
420 /* Order of next two lines is *very* important.
421 * When we are sending a little amount of data,
422 * the transfer may be completed inside driver.write()
423 * routine, because it's running with interrupts enabled.
424 * In this case we *never* got WRITE_WAKEUP event,
425 * if we did not request it before write operation.
426 * 14 Oct 1994 Dmitry Gorodchanin.
428 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
429 actual = sl->tty->driver.write(sl->tty, 0, sl->xbuff, count);
430 #ifdef SL_CHECK_TRANSMIT
431 sl->dev->trans_start = jiffies;
432 #endif
433 sl->xleft = count - actual;
434 sl->xhead = sl->xbuff + actual;
435 #ifdef CONFIG_SLIP_SMART
436 /* VSV */
437 clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
438 #endif
442 * Called by the driver when there's room for more data. If we have
443 * more packets to send, we send them here.
445 static void slip_write_wakeup(struct tty_struct *tty)
447 int actual;
448 struct slip *sl = (struct slip *) tty->disc_data;
450 /* First make sure we're connected. */
451 if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
452 return;
454 if (sl->xleft <= 0) {
455 /* Now serial buffer is almost free & we can start
456 * transmission of another packet */
457 sl->tx_packets++;
458 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
459 sl_unlock(sl);
460 return;
463 actual = tty->driver.write(tty, 0, sl->xhead, sl->xleft);
464 sl->xleft -= actual;
465 sl->xhead += actual;
468 static void sl_tx_timeout(struct net_device *dev)
470 struct slip *sl = (struct slip*)(dev->priv);
472 spin_lock(&sl->lock);
474 if (netif_queue_stopped(dev)) {
475 struct slip *sl = (struct slip*)(dev->priv);
477 if (!netif_running(dev))
478 goto out;
480 /* May be we must check transmitter timeout here ?
481 * 14 Oct 1994 Dmitry Gorodchanin.
483 #ifdef SL_CHECK_TRANSMIT
484 if (jiffies - dev->trans_start < 20 * HZ) {
485 /* 20 sec timeout not reached */
486 goto out;
488 printk("%s: transmit timed out, %s?\n", dev->name,
489 (sl->tty->driver.chars_in_buffer(sl->tty) || sl->xleft) ?
490 "bad line quality" : "driver error");
491 sl->xleft = 0;
492 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
493 sl_unlock(sl);
494 #endif
497 out:
498 spin_unlock(&sl->lock);
502 /* Encapsulate an IP datagram and kick it into a TTY queue. */
503 static int
504 sl_xmit(struct sk_buff *skb, struct net_device *dev)
506 struct slip *sl = (struct slip*)(dev->priv);
508 spin_lock(&sl->lock);
509 if (!netif_running(dev)) {
510 spin_unlock(&sl->lock);
511 printk("%s: xmit call when iface is down\n", dev->name);
512 dev_kfree_skb(skb);
513 return 0;
515 if (sl->tty == NULL) {
516 spin_unlock(&sl->lock);
517 dev_kfree_skb(skb);
518 return 0;
521 sl_lock(sl);
522 sl->tx_bytes+=skb->len;
523 sl_encaps(sl, skb->data, skb->len);
524 spin_unlock(&sl->lock);
526 dev_kfree_skb(skb);
527 return 0;
531 /******************************************
532 * Routines looking at netdevice side.
533 ******************************************/
535 /* Netdevice UP -> DOWN routine */
537 static int
538 sl_close(struct net_device *dev)
540 struct slip *sl = (struct slip*)(dev->priv);
542 spin_lock_bh(&sl->lock);
543 if (sl->tty) {
544 /* TTY discipline is running. */
545 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
547 netif_stop_queue(dev);
548 sl->rcount = 0;
549 sl->xleft = 0;
550 spin_unlock_bh(&sl->lock);
552 return 0;
555 /* Netdevice DOWN -> UP routine */
557 static int sl_open(struct net_device *dev)
559 struct slip *sl = (struct slip*)(dev->priv);
561 if (sl->tty==NULL)
562 return -ENODEV;
564 sl->flags &= (1 << SLF_INUSE);
565 netif_start_queue(dev);
566 return 0;
569 /* Netdevice change MTU request */
571 static int sl_change_mtu(struct net_device *dev, int new_mtu)
573 struct slip *sl = (struct slip*)(dev->priv);
575 if (new_mtu < 68 || new_mtu > 65534)
576 return -EINVAL;
578 if (new_mtu != dev->mtu)
579 return sl_realloc_bufs(sl, new_mtu);
580 return 0;
583 /* Netdevice get statistics request */
585 static struct net_device_stats *
586 sl_get_stats(struct net_device *dev)
588 static struct net_device_stats stats;
589 struct slip *sl = (struct slip*)(dev->priv);
590 #ifdef SL_INCLUDE_CSLIP
591 struct slcompress *comp;
592 #endif
594 memset(&stats, 0, sizeof(struct net_device_stats));
596 stats.rx_packets = sl->rx_packets;
597 stats.tx_packets = sl->tx_packets;
598 stats.rx_bytes = sl->rx_bytes;
599 stats.tx_bytes = sl->tx_bytes;
600 stats.rx_dropped = sl->rx_dropped;
601 stats.tx_dropped = sl->tx_dropped;
602 stats.tx_errors = sl->tx_errors;
603 stats.rx_errors = sl->rx_errors;
604 stats.rx_over_errors = sl->rx_over_errors;
605 #ifdef SL_INCLUDE_CSLIP
606 stats.rx_fifo_errors = sl->rx_compressed;
607 stats.tx_fifo_errors = sl->tx_compressed;
608 stats.collisions = sl->tx_misses;
609 comp = sl->slcomp;
610 if (comp) {
611 stats.rx_fifo_errors += comp->sls_i_compressed;
612 stats.rx_dropped += comp->sls_i_tossed;
613 stats.tx_fifo_errors += comp->sls_o_compressed;
614 stats.collisions += comp->sls_o_misses;
616 #endif /* CONFIG_INET */
617 return (&stats);
620 /* Netdevice register callback */
622 static int sl_init(struct net_device *dev)
624 struct slip *sl = (struct slip*)(dev->priv);
627 * Finish setting up the DEVICE info.
630 dev->mtu = sl->mtu;
631 dev->hard_start_xmit = sl_xmit;
632 #ifdef SL_CHECK_TRANSMIT
633 dev->tx_timeout = sl_tx_timeout;
634 dev->watchdog_timeo = 20*HZ;
635 #endif
636 dev->open = sl_open;
637 dev->stop = sl_close;
638 dev->get_stats = sl_get_stats;
639 dev->change_mtu = sl_change_mtu;
640 #ifdef CONFIG_SLIP_SMART
641 dev->do_ioctl = sl_ioctl;
642 #endif
643 dev->hard_header_len = 0;
644 dev->addr_len = 0;
645 dev->type = ARPHRD_SLIP + sl->mode;
646 dev->tx_queue_len = 10;
648 SET_MODULE_OWNER(dev);
650 dev_init_buffers(dev);
652 /* New-style flags. */
653 dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
655 return 0;
659 /******************************************
660 Routines looking at TTY side.
661 ******************************************/
664 static int slip_receive_room(struct tty_struct *tty)
666 return 65536; /* We can handle an infinite amount of data. :-) */
670 * Handle the 'receiver data ready' interrupt.
671 * This function is called by the 'tty_io' module in the kernel when
672 * a block of SLIP data has been received, which can now be decapsulated
673 * and sent on to some IP layer for further processing.
676 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
678 struct slip *sl = (struct slip *) tty->disc_data;
680 if (!sl || sl->magic != SLIP_MAGIC ||
681 !netif_running(sl->dev))
682 return;
684 /* Read the characters out of the buffer */
685 while (count--) {
686 if (fp && *fp++) {
687 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
688 sl->rx_errors++;
690 cp++;
691 continue;
693 #ifdef CONFIG_SLIP_MODE_SLIP6
694 if (sl->mode & SL_MODE_SLIP6)
695 slip_unesc6(sl, *cp++);
696 else
697 #endif
698 slip_unesc(sl, *cp++);
702 /************************************
703 * slip_open helper routines.
704 ************************************/
706 /* Collect hanged up channels */
708 static void sl_sync(void)
710 int i;
712 for (i = 0; i < slip_maxdev; i++) {
713 slip_ctrl_t *slp = slip_ctrls[i];
714 if (slp == NULL)
715 break;
716 if (slp->ctrl.tty || slp->ctrl.leased)
717 continue;
718 if (slp->dev.flags&IFF_UP)
719 dev_close(&slp->dev);
723 /* Find a free SLIP channel, and link in this `tty' line. */
724 static struct slip *
725 sl_alloc(kdev_t line)
727 struct slip *sl;
728 slip_ctrl_t *slp = NULL;
729 int i;
730 int sel = -1;
731 int score = -1;
733 if (slip_ctrls == NULL)
734 return NULL; /* Master array missing ! */
736 for (i = 0; i < slip_maxdev; i++) {
737 slp = slip_ctrls[i];
738 if (slp == NULL)
739 break;
741 if (slp->ctrl.leased) {
742 if (slp->ctrl.line != line)
743 continue;
744 if (slp->ctrl.tty)
745 return NULL;
747 /* Clear ESCAPE & ERROR flags */
748 slp->ctrl.flags &= (1 << SLF_INUSE);
749 return &slp->ctrl;
752 if (slp->ctrl.tty)
753 continue;
755 if (current->pid == slp->ctrl.pid) {
756 if (slp->ctrl.line == line && score < 3) {
757 sel = i;
758 score = 3;
759 continue;
761 if (score < 2) {
762 sel = i;
763 score = 2;
765 continue;
767 if (slp->ctrl.line == line && score < 1) {
768 sel = i;
769 score = 1;
770 continue;
772 if (score < 0) {
773 sel = i;
774 score = 0;
778 if (sel >= 0) {
779 i = sel;
780 slp = slip_ctrls[i];
781 if (score > 1) {
782 slp->ctrl.flags &= (1 << SLF_INUSE);
783 return &slp->ctrl;
787 /* Sorry, too many, all slots in use */
788 if (i >= slip_maxdev)
789 return NULL;
791 if (slp) {
792 if (test_bit(SLF_INUSE, &slp->ctrl.flags)) {
793 unregister_netdevice(&slp->dev);
794 sl_free_bufs(&slp->ctrl);
796 } else if ((slp = (slip_ctrl_t *)kmalloc(sizeof(slip_ctrl_t),GFP_KERNEL)) == NULL)
797 return NULL;
799 memset(slp, 0, sizeof(slip_ctrl_t));
801 sl = &slp->ctrl;
802 /* Initialize channel control data */
803 sl->magic = SLIP_MAGIC;
804 sl->dev = &slp->dev;
805 spin_lock_init(&sl->lock);
806 sl->mode = SL_MODE_DEFAULT;
807 sprintf(slp->dev.name, "sl%d", i);
808 slp->dev.base_addr = i;
809 slp->dev.priv = (void*)sl;
810 slp->dev.init = sl_init;
811 #ifdef CONFIG_SLIP_SMART
812 init_timer(&sl->keepalive_timer); /* initialize timer_list struct */
813 sl->keepalive_timer.data=(unsigned long)sl;
814 sl->keepalive_timer.function=sl_keepalive;
815 init_timer(&sl->outfill_timer);
816 sl->outfill_timer.data=(unsigned long)sl;
817 sl->outfill_timer.function=sl_outfill;
818 #endif
819 slip_ctrls[i] = slp;
820 return &slp->ctrl;
824 * Open the high-level part of the SLIP channel.
825 * This function is called by the TTY module when the
826 * SLIP line discipline is called for. Because we are
827 * sure the tty line exists, we only have to link it to
828 * a free SLIP channel...
830 static int
831 slip_open(struct tty_struct *tty)
833 struct slip *sl;
834 int err;
836 if(!capable(CAP_NET_ADMIN))
837 return -EPERM;
839 MOD_INC_USE_COUNT;
841 /* RTnetlink lock is misused here to serialize concurrent
842 opens of slip channels. There are better ways, but it is
843 the simplest one.
845 rtnl_lock();
847 /* Collect hanged up channels. */
848 sl_sync();
850 sl = (struct slip *) tty->disc_data;
852 err = -EEXIST;
853 /* First make sure we're not already connected. */
854 if (sl && sl->magic == SLIP_MAGIC)
855 goto err_exit;
857 /* OK. Find a free SLIP channel to use. */
858 err = -ENFILE;
859 if ((sl = sl_alloc(tty->device)) == NULL)
860 goto err_exit;
862 sl->tty = tty;
863 tty->disc_data = sl;
864 sl->line = tty->device;
865 sl->pid = current->pid;
866 if (tty->driver.flush_buffer)
867 tty->driver.flush_buffer(tty);
868 if (tty->ldisc.flush_buffer)
869 tty->ldisc.flush_buffer(tty);
871 if (!test_bit(SLF_INUSE, &sl->flags)) {
872 /* Perform the low-level SLIP initialization. */
873 if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
874 goto err_free_chan;
876 if (register_netdevice(sl->dev)) {
877 sl_free_bufs(sl);
878 goto err_free_chan;
881 set_bit(SLF_INUSE, &sl->flags);
884 #ifdef CONFIG_SLIP_SMART
885 if (sl->keepalive) {
886 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
887 add_timer (&sl->keepalive_timer);
889 if (sl->outfill) {
890 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
891 add_timer (&sl->outfill_timer);
893 #endif
895 /* Done. We have linked the TTY line to a channel. */
896 rtnl_unlock();
897 return sl->dev->base_addr;
899 err_free_chan:
900 sl->tty = NULL;
901 tty->disc_data = NULL;
902 clear_bit(SLF_INUSE, &sl->flags);
904 err_exit:
905 rtnl_unlock();
907 /* Count references from TTY module */
908 MOD_DEC_USE_COUNT;
909 return err;
913 Let me to blame a bit.
914 1. TTY module calls this funstion on soft interrupt.
915 2. TTY module calls this function WITH MASKED INTERRUPTS!
916 3. TTY module does not notify us about line discipline
917 shutdown,
919 Seems, now it is clean. The solution is to consider netdevice and
920 line discipline sides as two independent threads.
922 By-product (not desired): sl? does not feel hangups and remains open.
923 It is supposed, that user level program (dip, diald, slattach...)
924 will catch SIGHUP and make the rest of work.
926 I see no way to make more with current tty code. --ANK
930 * Close down a SLIP channel.
931 * This means flushing out any pending queues, and then restoring the
932 * TTY line discipline to what it was before it got hooked to SLIP
933 * (which usually is TTY again).
935 static void
936 slip_close(struct tty_struct *tty)
938 struct slip *sl = (struct slip *) tty->disc_data;
940 /* First make sure we're connected. */
941 if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
942 return;
944 tty->disc_data = 0;
945 sl->tty = NULL;
946 if (!sl->leased)
947 sl->line = 0;
949 /* VSV = very important to remove timers */
950 #ifdef CONFIG_SLIP_SMART
951 del_timer_sync(&sl->keepalive_timer);
952 del_timer_sync(&sl->outfill_timer);
953 #endif
955 /* Count references from TTY module */
956 MOD_DEC_USE_COUNT;
959 /************************************************************************
960 * STANDARD SLIP ENCAPSULATION *
961 ************************************************************************/
964 slip_esc(unsigned char *s, unsigned char *d, int len)
966 unsigned char *ptr = d;
967 unsigned char c;
970 * Send an initial END character to flush out any
971 * data that may have accumulated in the receiver
972 * due to line noise.
975 *ptr++ = END;
978 * For each byte in the packet, send the appropriate
979 * character sequence, according to the SLIP protocol.
982 while (len-- > 0) {
983 switch(c = *s++) {
984 case END:
985 *ptr++ = ESC;
986 *ptr++ = ESC_END;
987 break;
988 case ESC:
989 *ptr++ = ESC;
990 *ptr++ = ESC_ESC;
991 break;
992 default:
993 *ptr++ = c;
994 break;
997 *ptr++ = END;
998 return (ptr - d);
1001 static void slip_unesc(struct slip *sl, unsigned char s)
1004 switch(s) {
1005 case END:
1006 #ifdef CONFIG_SLIP_SMART
1007 /* drop keeptest bit = VSV */
1008 if (test_bit(SLF_KEEPTEST, &sl->flags))
1009 clear_bit(SLF_KEEPTEST, &sl->flags);
1010 #endif
1012 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1013 sl_bump(sl);
1015 clear_bit(SLF_ESCAPE, &sl->flags);
1016 sl->rcount = 0;
1017 return;
1019 case ESC:
1020 set_bit(SLF_ESCAPE, &sl->flags);
1021 return;
1022 case ESC_ESC:
1023 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1024 s = ESC;
1026 break;
1027 case ESC_END:
1028 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) {
1029 s = END;
1031 break;
1033 if (!test_bit(SLF_ERROR, &sl->flags)) {
1034 if (sl->rcount < sl->buffsize) {
1035 sl->rbuff[sl->rcount++] = s;
1036 return;
1038 sl->rx_over_errors++;
1039 set_bit(SLF_ERROR, &sl->flags);
1044 #ifdef CONFIG_SLIP_MODE_SLIP6
1045 /************************************************************************
1046 * 6 BIT SLIP ENCAPSULATION *
1047 ************************************************************************/
1050 slip_esc6(unsigned char *s, unsigned char *d, int len)
1052 unsigned char *ptr = d;
1053 unsigned char c;
1054 int i;
1055 unsigned short v = 0;
1056 short bits = 0;
1059 * Send an initial END character to flush out any
1060 * data that may have accumulated in the receiver
1061 * due to line noise.
1064 *ptr++ = 0x70;
1067 * Encode the packet into printable ascii characters
1070 for (i = 0; i < len; ++i) {
1071 v = (v << 8) | s[i];
1072 bits += 8;
1073 while (bits >= 6) {
1074 bits -= 6;
1075 c = 0x30 + ((v >> bits) & 0x3F);
1076 *ptr++ = c;
1079 if (bits) {
1080 c = 0x30 + ((v << (6 - bits)) & 0x3F);
1081 *ptr++ = c;
1083 *ptr++ = 0x70;
1084 return ptr - d;
1087 void
1088 slip_unesc6(struct slip *sl, unsigned char s)
1090 unsigned char c;
1092 if (s == 0x70) {
1093 #ifdef CONFIG_SLIP_SMART
1094 /* drop keeptest bit = VSV */
1095 if (test_bit(SLF_KEEPTEST, &sl->flags))
1096 clear_bit(SLF_KEEPTEST, &sl->flags);
1097 #endif
1099 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) {
1100 sl_bump(sl);
1102 sl->rcount = 0;
1103 sl->xbits = 0;
1104 sl->xdata = 0;
1105 } else if (s >= 0x30 && s < 0x70) {
1106 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1107 sl->xbits += 6;
1108 if (sl->xbits >= 8) {
1109 sl->xbits -= 8;
1110 c = (unsigned char)(sl->xdata >> sl->xbits);
1111 if (!test_bit(SLF_ERROR, &sl->flags)) {
1112 if (sl->rcount < sl->buffsize) {
1113 sl->rbuff[sl->rcount++] = c;
1114 return;
1116 sl->rx_over_errors++;
1117 set_bit(SLF_ERROR, &sl->flags);
1122 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1124 /* Perform I/O control on an active SLIP channel. */
1125 static int
1126 slip_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
1128 struct slip *sl = (struct slip *) tty->disc_data;
1129 unsigned int tmp;
1131 /* First make sure we're connected. */
1132 if (!sl || sl->magic != SLIP_MAGIC) {
1133 return -EINVAL;
1136 switch(cmd) {
1137 case SIOCGIFNAME:
1138 /* Please, do not put this line under copy_to_user,
1139 it breaks my old poor gcc on alpha --ANK
1141 tmp = strlen(sl->dev->name) + 1;
1142 if (copy_to_user(arg, sl->dev->name, tmp))
1143 return -EFAULT;
1144 return 0;
1146 case SIOCGIFENCAP:
1147 if (put_user(sl->mode, (int *)arg))
1148 return -EFAULT;
1149 return 0;
1151 case SIOCSIFENCAP:
1152 if (get_user(tmp,(int *)arg))
1153 return -EFAULT;
1154 #ifndef SL_INCLUDE_CSLIP
1155 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) {
1156 return -EINVAL;
1158 #else
1159 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1160 (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
1161 /* return -EINVAL; */
1162 tmp &= ~SL_MODE_ADAPTIVE;
1164 #endif
1165 #ifndef CONFIG_SLIP_MODE_SLIP6
1166 if (tmp & SL_MODE_SLIP6) {
1167 return -EINVAL;
1169 #endif
1170 sl->mode = tmp;
1171 sl->dev->type = ARPHRD_SLIP+sl->mode;
1172 return 0;
1174 case SIOCSIFHWADDR:
1175 return -EINVAL;
1177 #ifdef CONFIG_SLIP_SMART
1178 /* VSV changes start here */
1179 case SIOCSKEEPALIVE:
1180 if (get_user(tmp,(int *)arg))
1181 return -EFAULT;
1182 if (tmp > 255) /* max for unchar */
1183 return -EINVAL;
1185 spin_lock_bh(&sl->lock);
1186 if (!sl->tty) {
1187 spin_unlock_bh(&sl->lock);
1188 return -ENODEV;
1190 if ((sl->keepalive = (unchar) tmp) != 0) {
1191 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1192 set_bit(SLF_KEEPTEST, &sl->flags);
1193 } else {
1194 del_timer (&sl->keepalive_timer);
1196 spin_unlock_bh(&sl->lock);
1197 return 0;
1199 case SIOCGKEEPALIVE:
1200 if (put_user(sl->keepalive, (int *)arg))
1201 return -EFAULT;
1202 return 0;
1204 case SIOCSOUTFILL:
1205 if (get_user(tmp,(int *)arg))
1206 return -EFAULT;
1207 if (tmp > 255) /* max for unchar */
1208 return -EINVAL;
1209 spin_lock_bh(&sl->lock);
1210 if (!sl->tty) {
1211 spin_unlock_bh(&sl->lock);
1212 return -ENODEV;
1214 if ((sl->outfill = (unchar) tmp) != 0){
1215 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1216 set_bit(SLF_OUTWAIT, &sl->flags);
1217 } else {
1218 del_timer (&sl->outfill_timer);
1220 spin_unlock_bh(&sl->lock);
1221 return 0;
1223 case SIOCGOUTFILL:
1224 if (put_user(sl->outfill, (int *)arg))
1225 return -EFAULT;
1226 return 0;
1227 /* VSV changes end */
1228 #endif
1230 /* Allow stty to read, but not set, the serial port */
1231 case TCGETS:
1232 case TCGETA:
1233 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1235 default:
1236 return -ENOIOCTLCMD;
1240 /* VSV changes start here */
1241 #ifdef CONFIG_SLIP_SMART
1242 /* function do_ioctl called from net/core/dev.c
1243 to allow get/set outfill/keepalive parameter
1244 by ifconfig */
1246 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
1248 struct slip *sl = (struct slip*)(dev->priv);
1250 if (sl == NULL) /* Allocation failed ?? */
1251 return -ENODEV;
1253 spin_lock_bh(&sl->lock);
1255 if (!sl->tty) {
1256 spin_unlock_bh(&sl->lock);
1257 return -ENODEV;
1260 switch(cmd){
1261 case SIOCSKEEPALIVE:
1262 /* max for unchar */
1263 if (((unsigned int)((unsigned long)rq->ifr_data)) > 255) {
1264 spin_unlock_bh(&sl->lock);
1265 return -EINVAL;
1267 sl->keepalive = (unchar) ((unsigned long)rq->ifr_data);
1268 if (sl->keepalive != 0) {
1269 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1270 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1271 set_bit(SLF_KEEPTEST, &sl->flags);
1272 } else {
1273 del_timer(&sl->keepalive_timer);
1275 break;
1277 case SIOCGKEEPALIVE:
1278 rq->ifr_data=(caddr_t)((unsigned long)sl->keepalive);
1279 break;
1281 case SIOCSOUTFILL:
1282 if (((unsigned)((unsigned long)rq->ifr_data)) > 255) { /* max for unchar */
1283 spin_unlock_bh(&sl->lock);
1284 return -EINVAL;
1286 if ((sl->outfill = (unchar)((unsigned long) rq->ifr_data)) != 0){
1287 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1288 set_bit(SLF_OUTWAIT, &sl->flags);
1289 } else {
1290 del_timer (&sl->outfill_timer);
1292 break;
1294 case SIOCGOUTFILL:
1295 rq->ifr_data=(caddr_t)((unsigned long)sl->outfill);
1296 break;
1298 case SIOCSLEASE:
1299 /* Resolve race condition, when ioctl'ing hanged up
1300 and opened by another process device.
1302 if (sl->tty != current->tty && sl->pid != current->pid) {
1303 spin_unlock_bh(&sl->lock);
1304 return -EPERM;
1306 sl->leased = 0;
1307 if ((unsigned long)rq->ifr_data)
1308 sl->leased = 1;
1309 break;
1311 case SIOCGLEASE:
1312 rq->ifr_data=(caddr_t)((unsigned long)sl->leased);
1314 spin_unlock_bh(&sl->lock);
1315 return 0;
1317 #endif
1318 /* VSV changes end */
1320 /* Initialize SLIP control device -- register SLIP line discipline */
1322 int __init slip_init_ctrl_dev(void)
1324 int status;
1326 if (slip_maxdev < 4) slip_maxdev = 4; /* Sanity */
1328 printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1329 #ifdef CONFIG_SLIP_MODE_SLIP6
1330 " (6 bit encapsulation enabled)"
1331 #endif
1332 ".\n",
1333 SLIP_VERSION, slip_maxdev );
1334 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1335 printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1336 #endif
1337 #ifdef CONFIG_SLIP_SMART
1338 printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1339 #endif
1341 slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1342 if (slip_ctrls == NULL)
1344 printk("SLIP: Can't allocate slip_ctrls[] array! Uaargh! (-> No SLIP available)\n");
1345 return -ENOMEM;
1348 /* Clear the pointer array, we allocate devices when we need them */
1349 memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev); /* Pointers */
1351 /* Fill in our line protocol discipline, and register it */
1352 memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1353 sl_ldisc.magic = TTY_LDISC_MAGIC;
1354 sl_ldisc.name = "slip";
1355 sl_ldisc.flags = 0;
1356 sl_ldisc.open = slip_open;
1357 sl_ldisc.close = slip_close;
1358 sl_ldisc.read = NULL;
1359 sl_ldisc.write = NULL;
1360 sl_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
1361 unsigned int, unsigned long)) slip_ioctl;
1362 sl_ldisc.poll = NULL;
1363 sl_ldisc.receive_buf = slip_receive_buf;
1364 sl_ldisc.receive_room = slip_receive_room;
1365 sl_ldisc.write_wakeup = slip_write_wakeup;
1366 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1367 printk("SLIP: can't register line discipline (err = %d)\n", status);
1371 return status;
1376 #ifdef MODULE
1379 init_module(void)
1381 return slip_init_ctrl_dev();
1384 void
1385 cleanup_module(void)
1387 int i;
1389 if (slip_ctrls != NULL) {
1390 unsigned long start = jiffies;
1391 int busy = 0;
1393 /* First of all: check for active disciplines and hangup them.
1395 do {
1396 if (busy) {
1397 current->counter = 0;
1398 schedule();
1401 busy = 0;
1402 local_bh_disable();
1403 for (i = 0; i < slip_maxdev; i++) {
1404 struct slip_ctrl *slc = slip_ctrls[i];
1405 if (!slc)
1406 continue;
1407 spin_lock(&slc->ctrl.lock);
1408 if (slc->ctrl.tty) {
1409 busy++;
1410 tty_hangup(slc->ctrl.tty);
1412 spin_unlock(&slc->ctrl.lock);
1414 local_bh_enable();
1415 } while (busy && jiffies - start < 1*HZ);
1417 busy = 0;
1418 for (i = 0; i < slip_maxdev; i++) {
1419 struct slip_ctrl *slc = slip_ctrls[i];
1420 if (slc) {
1421 unregister_netdev(&slc->dev);
1422 if (slc->ctrl.tty) {
1423 printk("%s: tty discipline is still running\n", slc->dev.name);
1424 /* Pin module forever */
1425 MOD_INC_USE_COUNT;
1426 busy++;
1427 continue;
1429 sl_free_bufs(&slc->ctrl);
1430 kfree(slc);
1431 slip_ctrls[i] = NULL;
1434 if (!busy) {
1435 kfree(slip_ctrls);
1436 slip_ctrls = NULL;
1439 if ((i = tty_register_ldisc(N_SLIP, NULL)))
1441 printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1444 #endif /* MODULE */
1446 #ifdef CONFIG_SLIP_SMART
1448 * This is start of the code for multislip style line checking
1449 * added by Stanislav Voronyi. All changes before marked VSV
1452 static void sl_outfill(unsigned long sls)
1454 struct slip *sl=(struct slip *)sls;
1456 spin_lock(&sl->lock);
1458 if (sl->tty == NULL)
1459 goto out;
1461 if(sl->outfill)
1463 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1465 /* no packets were transmitted, do outfill */
1466 #ifdef CONFIG_SLIP_MODE_SLIP6
1467 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1468 #else
1469 unsigned char s = END;
1470 #endif
1471 /* put END into tty queue. Is it right ??? */
1472 if (!netif_queue_stopped(sl->dev))
1474 /* if device busy no outfill */
1475 sl->tty->driver.write(sl->tty, 0, &s, 1);
1478 else
1479 set_bit(SLF_OUTWAIT, &sl->flags);
1481 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1483 out:
1484 spin_unlock(&sl->lock);
1487 static void sl_keepalive(unsigned long sls)
1489 struct slip *sl=(struct slip *)sls;
1491 spin_lock(&sl->lock);
1493 if (sl->tty == NULL)
1494 goto out;
1496 if( sl->keepalive)
1498 if(test_bit(SLF_KEEPTEST, &sl->flags))
1500 /* keepalive still high :(, we must hangup */
1501 if( sl->outfill ) /* outfill timer must be deleted too */
1502 (void)del_timer(&sl->outfill_timer);
1503 printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1504 tty_hangup(sl->tty); /* this must hangup tty & close slip */
1505 /* I think we need not something else */
1506 goto out;
1508 else
1509 set_bit(SLF_KEEPTEST, &sl->flags);
1511 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1514 out:
1515 spin_unlock(&sl->lock);
1518 #endif