* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / slip.c
blob4fa976736c5e5d93ae7191c1691b720772aaeeaf
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 char if_name[16]; /* "sl0\0" .. "sl99999\0" */
93 struct slip ctrl; /* SLIP things */
94 struct net_device dev; /* the device */
95 } slip_ctrl_t;
96 static slip_ctrl_t **slip_ctrls = NULL;
98 int slip_maxdev = SL_NRUNIT; /* Can be overridden with insmod! */
99 MODULE_PARM(slip_maxdev, "i");
101 static struct tty_ldisc sl_ldisc;
103 static int slip_esc(unsigned char *p, unsigned char *d, int len);
104 static void slip_unesc(struct slip *sl, unsigned char c);
105 #ifdef CONFIG_SLIP_MODE_SLIP6
106 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
107 static void slip_unesc6(struct slip *sl, unsigned char c);
108 #endif
109 #ifdef CONFIG_SLIP_SMART
110 static void sl_keepalive(unsigned long sls);
111 static void sl_outfill(unsigned long sls);
112 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd);
113 #endif
115 /********************************
116 * Buffer administration routines:
117 * sl_alloc_bufs()
118 * sl_free_bufs()
119 * sl_realloc_bufs()
121 * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
122 * sl_realloc_bufs provides strong atomicity and reallocation
123 * on actively running device.
124 *********************************/
127 Allocate channel buffers.
130 static int
131 sl_alloc_bufs(struct slip *sl, int mtu)
133 int err = -ENOBUFS;
134 unsigned long len;
135 char * rbuff = NULL;
136 char * xbuff = NULL;
137 #ifdef SL_INCLUDE_CSLIP
138 char * cbuff = NULL;
139 struct slcompress *slcomp = NULL;
140 #endif
143 * Allocate the SLIP frame buffers:
145 * rbuff Receive buffer.
146 * xbuff Transmit buffer.
147 * cbuff Temporary compression buffer.
149 len = mtu * 2;
152 * allow for arrival of larger UDP packets, even if we say not to
153 * also fixes a bug in which SunOS sends 512-byte packets even with
154 * an MSS of 128
156 if (len < 576 * 2)
157 len = 576 * 2;
158 rbuff = kmalloc(len + 4, GFP_KERNEL);
159 if (rbuff == NULL)
160 goto err_exit;
161 xbuff = kmalloc(len + 4, GFP_KERNEL);
162 if (xbuff == NULL)
163 goto err_exit;
164 #ifdef SL_INCLUDE_CSLIP
165 cbuff = kmalloc(len + 4, GFP_KERNEL);
166 if (cbuff == NULL)
167 goto err_exit;
168 slcomp = slhc_init(16, 16);
169 if (slcomp == NULL)
170 goto err_exit;
171 #endif
172 start_bh_atomic();
173 if (sl->tty == NULL) {
174 end_bh_atomic();
175 err = -ENODEV;
176 goto err_exit;
178 sl->mtu = mtu;
179 sl->buffsize = len;
180 sl->rcount = 0;
181 sl->xleft = 0;
182 rbuff = xchg(&sl->rbuff, rbuff);
183 xbuff = xchg(&sl->xbuff, xbuff);
184 #ifdef SL_INCLUDE_CSLIP
185 cbuff = xchg(&sl->cbuff, cbuff);
186 slcomp = xchg(&sl->slcomp, slcomp);
187 #ifdef CONFIG_SLIP_MODE_SLIP6
188 sl->xdata = 0;
189 sl->xbits = 0;
190 #endif
191 #endif
192 end_bh_atomic();
193 err = 0;
195 /* Cleanup */
196 err_exit:
197 #ifdef SL_INCLUDE_CSLIP
198 if (cbuff)
199 kfree(cbuff);
200 if (slcomp)
201 slhc_free(slcomp);
202 #endif
203 if (xbuff)
204 kfree(xbuff);
205 if (rbuff)
206 kfree(rbuff);
207 return err;
210 /* Free a SLIP channel buffers. */
211 static void
212 sl_free_bufs(struct slip *sl)
214 void * tmp;
216 /* Free all SLIP frame buffers. */
217 if ((tmp = xchg(&sl->rbuff, NULL)) != NULL)
218 kfree(tmp);
219 if ((tmp = xchg(&sl->xbuff, NULL)) != NULL)
220 kfree(tmp);
221 #ifdef SL_INCLUDE_CSLIP
222 if ((tmp = xchg(&sl->cbuff, NULL)) != NULL)
223 kfree(tmp);
224 if ((tmp = xchg(&sl->slcomp, NULL)) != NULL)
225 slhc_free(tmp);
226 #endif
230 Reallocate slip channel buffers.
233 static int sl_realloc_bufs(struct slip *sl, int mtu)
235 int err = 0;
236 struct net_device *dev = sl->dev;
237 unsigned char *xbuff, *rbuff;
238 #ifdef SL_INCLUDE_CSLIP
239 unsigned char *cbuff;
240 #endif
241 int len = mtu * 2;
244 * allow for arrival of larger UDP packets, even if we say not to
245 * also fixes a bug in which SunOS sends 512-byte packets even with
246 * an MSS of 128
248 if (len < 576 * 2)
249 len = 576 * 2;
251 xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
252 rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
253 #ifdef SL_INCLUDE_CSLIP
254 cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
255 #endif
258 #ifdef SL_INCLUDE_CSLIP
259 if (xbuff == NULL || rbuff == NULL || cbuff == NULL) {
260 #else
261 if (xbuff == NULL || rbuff == NULL) {
262 #endif
263 if (mtu >= sl->mtu) {
264 printk("%s: unable to grow slip buffers, MTU change cancelled.\n",
265 dev->name);
266 err = -ENOBUFS;
268 goto done;
271 start_bh_atomic();
273 err = -ENODEV;
274 if (sl->tty == NULL)
275 goto done_on_bh;
277 xbuff = xchg(&sl->xbuff, xbuff);
278 rbuff = xchg(&sl->rbuff, rbuff);
279 #ifdef SL_INCLUDE_CSLIP
280 cbuff = xchg(&sl->cbuff, cbuff);
281 #endif
282 if (sl->xleft) {
283 if (sl->xleft <= len) {
284 memcpy(sl->xbuff, sl->xhead, sl->xleft);
285 } else {
286 sl->xleft = 0;
287 sl->tx_dropped++;
290 sl->xhead = sl->xbuff;
292 if (sl->rcount) {
293 if (sl->rcount <= len) {
294 memcpy(sl->rbuff, rbuff, sl->rcount);
295 } else {
296 sl->rcount = 0;
297 sl->rx_over_errors++;
298 set_bit(SLF_ERROR, &sl->flags);
301 sl->mtu = mtu;
302 dev->mtu = mtu;
303 sl->buffsize = len;
304 err = 0;
306 done_on_bh:
307 end_bh_atomic();
309 done:
310 if (xbuff)
311 kfree(xbuff);
312 if (rbuff)
313 kfree(rbuff);
314 #ifdef SL_INCLUDE_CSLIP
315 if (cbuff)
316 kfree(cbuff);
317 #endif
318 return err;
322 /* Set the "sending" flag. This must be atomic hence the set_bit. */
323 static inline void
324 sl_lock(struct slip *sl)
326 if (test_and_set_bit(0, (void *) &sl->dev->tbusy)) {
327 printk("%s: trying to lock already locked device!\n", sl->dev->name);
332 /* Clear the "sending" flag. This must be atomic, hence the ASM. */
333 static inline void
334 sl_unlock(struct slip *sl)
336 if (!test_and_clear_bit(0, (void *)&sl->dev->tbusy)) {
337 printk("%s: trying to unlock already unlocked device!\n", sl->dev->name);
341 /* Send one completely decapsulated IP datagram to the IP layer. */
342 static void
343 sl_bump(struct slip *sl)
345 struct sk_buff *skb;
346 int count;
348 count = sl->rcount;
349 #ifdef SL_INCLUDE_CSLIP
350 if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
351 unsigned char c;
352 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
353 /* ignore compressed packets when CSLIP is off */
354 if (!(sl->mode & SL_MODE_CSLIP)) {
355 printk("%s: compressed packet ignored\n", sl->dev->name);
356 return;
358 /* make sure we've reserved enough space for uncompress to use */
359 if (count + 80 > sl->buffsize) {
360 sl->rx_over_errors++;
361 return;
363 count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
364 if (count <= 0) {
365 return;
367 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
368 if (!(sl->mode & SL_MODE_CSLIP)) {
369 /* turn on header compression */
370 sl->mode |= SL_MODE_CSLIP;
371 sl->mode &= ~SL_MODE_ADAPTIVE;
372 printk("%s: header compression turned on\n", sl->dev->name);
374 sl->rbuff[0] &= 0x4f;
375 if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
376 return;
380 #endif /* SL_INCLUDE_CSLIP */
382 sl->rx_bytes+=count;
384 skb = dev_alloc_skb(count);
385 if (skb == NULL) {
386 printk("%s: memory squeeze, dropping packet.\n", sl->dev->name);
387 sl->rx_dropped++;
388 return;
390 skb->dev = sl->dev;
391 memcpy(skb_put(skb,count), sl->rbuff, count);
392 skb->mac.raw=skb->data;
393 skb->protocol=htons(ETH_P_IP);
394 netif_rx(skb);
395 sl->rx_packets++;
398 /* Encapsulate one IP datagram and stuff into a TTY queue. */
399 static void
400 sl_encaps(struct slip *sl, unsigned char *icp, int len)
402 unsigned char *p;
403 int actual, count;
405 if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */
406 printk ("%s: truncating oversized transmit packet!\n", sl->dev->name);
407 sl->tx_dropped++;
408 sl_unlock(sl);
409 return;
412 p = icp;
413 #ifdef SL_INCLUDE_CSLIP
414 if (sl->mode & SL_MODE_CSLIP) {
415 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
417 #endif
418 #ifdef CONFIG_SLIP_MODE_SLIP6
419 if(sl->mode & SL_MODE_SLIP6)
420 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
421 else
422 #endif
423 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
425 /* Order of next two lines is *very* important.
426 * When we are sending a little amount of data,
427 * the transfer may be completed inside driver.write()
428 * routine, because it's running with interrupts enabled.
429 * In this case we *never* got WRITE_WAKEUP event,
430 * if we did not request it before write operation.
431 * 14 Oct 1994 Dmitry Gorodchanin.
433 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
434 actual = sl->tty->driver.write(sl->tty, 0, sl->xbuff, count);
435 #ifdef SL_CHECK_TRANSMIT
436 sl->dev->trans_start = jiffies;
437 #endif
438 sl->xleft = count - actual;
439 sl->xhead = sl->xbuff + actual;
440 #ifdef CONFIG_SLIP_SMART
441 /* VSV */
442 clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */
443 #endif
447 * Called by the driver when there's room for more data. If we have
448 * more packets to send, we send them here.
450 static void slip_write_wakeup(struct tty_struct *tty)
452 int actual;
453 struct slip *sl = (struct slip *) tty->disc_data;
455 /* First make sure we're connected. */
456 if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start) {
457 return;
459 if (sl->xleft <= 0) {
460 /* Now serial buffer is almost free & we can start
461 * transmission of another packet */
462 sl->tx_packets++;
463 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
464 sl_unlock(sl);
465 mark_bh(NET_BH);
466 return;
469 actual = tty->driver.write(tty, 0, sl->xhead, sl->xleft);
470 sl->xleft -= actual;
471 sl->xhead += actual;
474 /* Encapsulate an IP datagram and kick it into a TTY queue. */
475 static int
476 sl_xmit(struct sk_buff *skb, struct net_device *dev)
478 struct slip *sl = (struct slip*)(dev->priv);
480 if (!dev->start) {
481 printk("%s: xmit call when iface is down\n", dev->name);
482 dev_kfree_skb(skb);
483 return 0;
485 if (sl->tty == NULL) {
486 dev_kfree_skb(skb);
487 return 0;
491 * If we are busy already- too bad. We ought to be able
492 * to queue things at this point, to allow for a little
493 * frame buffer. Oh well...
494 * -----------------------------------------------------
495 * I hate queues in SLIP driver. May be it's efficient,
496 * but for me latency is more important. ;)
497 * So, no queues !
498 * 14 Oct 1994 Dmitry Gorodchanin.
500 if (dev->tbusy) {
501 /* May be we must check transmitter timeout here ?
502 * 14 Oct 1994 Dmitry Gorodchanin.
504 #ifdef SL_CHECK_TRANSMIT
505 if (jiffies - dev->trans_start < 20 * HZ) {
506 /* 20 sec timeout not reached */
507 return 1;
509 printk("%s: transmit timed out, %s?\n", dev->name,
510 (sl->tty->driver.chars_in_buffer(sl->tty) || sl->xleft) ?
511 "bad line quality" : "driver error");
512 sl->xleft = 0;
513 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
514 sl_unlock(sl);
515 #else
516 return 1;
517 #endif
520 /* We were not busy, so we are now... :-) */
521 if (skb != NULL)
523 sl_lock(sl);
524 sl->tx_bytes+=skb->len;
525 sl_encaps(sl, skb->data, skb->len);
526 dev_kfree_skb(skb);
528 return 0;
532 /******************************************
533 * Routines looking at netdevice side.
534 ******************************************/
536 /* Netdevice UP -> DOWN routine */
538 static int
539 sl_close(struct net_device *dev)
541 struct slip *sl = (struct slip*)(dev->priv);
543 start_bh_atomic();
544 if (sl->tty) {
545 /* TTY discipline is running. */
546 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
548 dev->tbusy = 1;
549 dev->start = 0;
550 sl->rcount = 0;
551 sl->xleft = 0;
552 end_bh_atomic();
554 MOD_DEC_USE_COUNT;
555 return 0;
558 /* Netdevice DOWN -> UP routine */
560 static int sl_open(struct net_device *dev)
562 struct slip *sl = (struct slip*)(dev->priv);
564 if (sl->tty==NULL)
565 return -ENODEV;
567 sl->flags &= (1 << SLF_INUSE);
568 dev->start = 1;
569 dev->tbusy = 0;
570 MOD_INC_USE_COUNT;
571 return 0;
574 /* Netdevice change MTU request */
576 static int sl_change_mtu(struct net_device *dev, int new_mtu)
578 struct slip *sl = (struct slip*)(dev->priv);
580 if (new_mtu < 68 || new_mtu > 65534)
581 return -EINVAL;
583 if (new_mtu != dev->mtu)
584 return sl_realloc_bufs(sl, new_mtu);
585 return 0;
588 /* Netdevice get statistics request */
590 static struct net_device_stats *
591 sl_get_stats(struct net_device *dev)
593 static struct net_device_stats stats;
594 struct slip *sl = (struct slip*)(dev->priv);
595 #ifdef SL_INCLUDE_CSLIP
596 struct slcompress *comp;
597 #endif
599 memset(&stats, 0, sizeof(struct net_device_stats));
601 stats.rx_packets = sl->rx_packets;
602 stats.tx_packets = sl->tx_packets;
603 stats.rx_bytes = sl->rx_bytes;
604 stats.tx_bytes = sl->tx_bytes;
605 stats.rx_dropped = sl->rx_dropped;
606 stats.tx_dropped = sl->tx_dropped;
607 stats.tx_errors = sl->tx_errors;
608 stats.rx_errors = sl->rx_errors;
609 stats.rx_over_errors = sl->rx_over_errors;
610 #ifdef SL_INCLUDE_CSLIP
611 stats.rx_fifo_errors = sl->rx_compressed;
612 stats.tx_fifo_errors = sl->tx_compressed;
613 stats.collisions = sl->tx_misses;
614 comp = sl->slcomp;
615 if (comp) {
616 stats.rx_fifo_errors += comp->sls_i_compressed;
617 stats.rx_dropped += comp->sls_i_tossed;
618 stats.tx_fifo_errors += comp->sls_o_compressed;
619 stats.collisions += comp->sls_o_misses;
621 #endif /* CONFIG_INET */
622 return (&stats);
625 /* Netdevice register callback */
627 static int sl_init(struct net_device *dev)
629 struct slip *sl = (struct slip*)(dev->priv);
632 * Finish setting up the DEVICE info.
635 dev->mtu = sl->mtu;
636 dev->hard_start_xmit = sl_xmit;
637 dev->open = sl_open;
638 dev->stop = sl_close;
639 dev->get_stats = sl_get_stats;
640 dev->change_mtu = sl_change_mtu;
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->type = ARPHRD_SLIP + sl->mode;
647 dev->tx_queue_len = 10;
649 dev_init_buffers(dev);
651 /* New-style flags. */
652 dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
654 return 0;
658 /******************************************
659 Routines looking at TTY side.
660 ******************************************/
663 static int slip_receive_room(struct tty_struct *tty)
665 return 65536; /* We can handle an infinite amount of data. :-) */
669 * Handle the 'receiver data ready' interrupt.
670 * This function is called by the 'tty_io' module in the kernel when
671 * a block of SLIP data has been received, which can now be decapsulated
672 * and sent on to some IP layer for further processing.
675 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
677 struct slip *sl = (struct slip *) tty->disc_data;
679 if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start)
680 return;
682 /* Read the characters out of the buffer */
683 while (count--) {
684 if (fp && *fp++) {
685 if (!test_and_set_bit(SLF_ERROR, &sl->flags)) {
686 sl->rx_errors++;
688 cp++;
689 continue;
691 #ifdef CONFIG_SLIP_MODE_SLIP6
692 if (sl->mode & SL_MODE_SLIP6)
693 slip_unesc6(sl, *cp++);
694 else
695 #endif
696 slip_unesc(sl, *cp++);
700 /************************************
701 * slip_open helper routines.
702 ************************************/
704 /* Collect hanged up channels */
706 static void sl_sync(void)
708 int i;
710 for (i = 0; i < slip_maxdev; i++) {
711 slip_ctrl_t *slp = slip_ctrls[i];
712 if (slp == NULL)
713 break;
714 if (slp->ctrl.tty || slp->ctrl.leased)
715 continue;
716 if (slp->dev.flags&IFF_UP)
717 dev_close(&slp->dev);
721 /* Find a free SLIP channel, and link in this `tty' line. */
722 static struct slip *
723 sl_alloc(kdev_t line)
725 struct slip *sl;
726 slip_ctrl_t *slp = NULL;
727 int i;
728 int sel = -1;
729 int score = -1;
731 if (slip_ctrls == NULL)
732 return NULL; /* Master array missing ! */
734 for (i = 0; i < slip_maxdev; i++) {
735 slp = slip_ctrls[i];
736 if (slp == NULL)
737 break;
739 if (slp->ctrl.leased) {
740 if (slp->ctrl.line != line)
741 continue;
742 if (slp->ctrl.tty)
743 return NULL;
745 /* Clear ESCAPE & ERROR flags */
746 slp->ctrl.flags &= (1 << SLF_INUSE);
747 return &slp->ctrl;
750 if (slp->ctrl.tty)
751 continue;
753 if (current->pid == slp->ctrl.pid) {
754 if (slp->ctrl.line == line && score < 3) {
755 sel = i;
756 score = 3;
757 continue;
759 if (score < 2) {
760 sel = i;
761 score = 2;
763 continue;
765 if (slp->ctrl.line == line && score < 1) {
766 sel = i;
767 score = 1;
768 continue;
770 if (score < 0) {
771 sel = i;
772 score = 0;
776 if (sel >= 0) {
777 i = sel;
778 slp = slip_ctrls[i];
779 if (score > 1) {
780 slp->ctrl.flags &= (1 << SLF_INUSE);
781 return &slp->ctrl;
785 /* Sorry, too many, all slots in use */
786 if (i >= slip_maxdev)
787 return NULL;
789 if (slp) {
790 if (test_bit(SLF_INUSE, &slp->ctrl.flags)) {
791 unregister_netdevice(&slp->dev);
792 sl_free_bufs(&slp->ctrl);
794 } else if ((slp = (slip_ctrl_t *)kmalloc(sizeof(slip_ctrl_t),GFP_KERNEL)) == NULL)
795 return NULL;
797 memset(slp, 0, sizeof(slip_ctrl_t));
799 sl = &slp->ctrl;
800 /* Initialize channel control data */
801 sl->magic = SLIP_MAGIC;
802 sl->dev = &slp->dev;
803 sl->mode = SL_MODE_DEFAULT;
804 sprintf(slp->if_name, "sl%d", i);
805 slp->dev.name = slp->if_name;
806 slp->dev.base_addr = i;
807 slp->dev.priv = (void*)sl;
808 slp->dev.init = sl_init;
809 #ifdef CONFIG_SLIP_SMART
810 init_timer(&sl->keepalive_timer); /* initialize timer_list struct */
811 sl->keepalive_timer.data=(unsigned long)sl;
812 sl->keepalive_timer.function=sl_keepalive;
813 init_timer(&sl->outfill_timer);
814 sl->outfill_timer.data=(unsigned long)sl;
815 sl->outfill_timer.function=sl_outfill;
816 #endif
817 slip_ctrls[i] = slp;
818 return &slp->ctrl;
822 * Open the high-level part of the SLIP channel.
823 * This function is called by the TTY module when the
824 * SLIP line discipline is called for. Because we are
825 * sure the tty line exists, we only have to link it to
826 * a free SLIP channel...
828 static int
829 slip_open(struct tty_struct *tty)
831 struct slip *sl;
832 int err;
834 if(!capable(CAP_NET_ADMIN))
835 return -EPERM;
837 MOD_INC_USE_COUNT;
839 /* RTnetlink lock is misused here to serialize concurrent
840 opens of slip channels. There are better ways, but it is
841 the simplest one.
843 rtnl_lock();
845 /* Collect hanged up channels. */
846 sl_sync();
848 sl = (struct slip *) tty->disc_data;
850 err = -EEXIST;
851 /* First make sure we're not already connected. */
852 if (sl && sl->magic == SLIP_MAGIC)
853 goto err_exit;
855 /* OK. Find a free SLIP channel to use. */
856 err = -ENFILE;
857 if ((sl = sl_alloc(tty->device)) == NULL)
858 goto err_exit;
860 sl->tty = tty;
861 tty->disc_data = sl;
862 sl->line = tty->device;
863 sl->pid = current->pid;
864 if (tty->driver.flush_buffer)
865 tty->driver.flush_buffer(tty);
866 if (tty->ldisc.flush_buffer)
867 tty->ldisc.flush_buffer(tty);
869 if (!test_bit(SLF_INUSE, &sl->flags)) {
870 /* Perform the low-level SLIP initialization. */
871 if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
872 goto err_free_chan;
874 if (register_netdevice(sl->dev)) {
875 sl_free_bufs(sl);
876 goto err_free_chan;
879 set_bit(SLF_INUSE, &sl->flags);
882 #ifdef CONFIG_SLIP_SMART
883 if (sl->keepalive) {
884 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
885 add_timer (&sl->keepalive_timer);
887 if (sl->outfill) {
888 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
889 add_timer (&sl->outfill_timer);
891 #endif
893 /* Done. We have linked the TTY line to a channel. */
894 rtnl_unlock();
895 return sl->dev->base_addr;
897 err_free_chan:
898 sl->tty = NULL;
899 tty->disc_data = NULL;
900 clear_bit(SLF_INUSE, &sl->flags);
902 err_exit:
903 rtnl_unlock();
905 /* Count references from TTY module */
906 MOD_DEC_USE_COUNT;
907 return err;
911 Let me to blame a bit.
912 1. TTY module calls this funstion on soft interrupt.
913 2. TTY module calls this function WITH MASKED INTERRUPTS!
914 3. TTY module does not notify us about line discipline
915 shutdown,
917 Seems, now it is clean. The solution is to consider netdevice and
918 line discipline sides as two independent threads.
920 By-product (not desired): sl? does not feel hangups and remains open.
921 It is supposed, that user level program (dip, diald, slattach...)
922 will catch SIGHUP and make the rest of work.
924 I see no way to make more with current tty code. --ANK
928 * Close down a SLIP channel.
929 * This means flushing out any pending queues, and then restoring the
930 * TTY line discipline to what it was before it got hooked to SLIP
931 * (which usually is TTY again).
933 static void
934 slip_close(struct tty_struct *tty)
936 struct slip *sl = (struct slip *) tty->disc_data;
938 /* First make sure we're connected. */
939 if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
940 return;
942 tty->disc_data = 0;
943 sl->tty = NULL;
944 if (!sl->leased)
945 sl->line = 0;
947 /* VSV = very important to remove timers */
948 #ifdef CONFIG_SLIP_SMART
949 if (sl->keepalive)
950 del_timer (&sl->keepalive_timer);
951 if (sl->outfill)
952 del_timer (&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 start_bh_atomic();
1186 if (!sl->tty) {
1187 end_bh_atomic();
1188 return -ENODEV;
1190 if (sl->keepalive)
1191 (void)del_timer (&sl->keepalive_timer);
1192 if ((sl->keepalive = (unchar) tmp) != 0) {
1193 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1194 add_timer(&sl->keepalive_timer);
1195 set_bit(SLF_KEEPTEST, &sl->flags);
1197 end_bh_atomic();
1199 return 0;
1201 case SIOCGKEEPALIVE:
1202 if (put_user(sl->keepalive, (int *)arg))
1203 return -EFAULT;
1204 return 0;
1206 case SIOCSOUTFILL:
1207 if (get_user(tmp,(int *)arg))
1208 return -EFAULT;
1209 if (tmp > 255) /* max for unchar */
1210 return -EINVAL;
1211 start_bh_atomic();
1212 if (!sl->tty) {
1213 end_bh_atomic();
1214 return -ENODEV;
1216 if (sl->outfill)
1217 (void)del_timer (&sl->outfill_timer);
1218 if ((sl->outfill = (unchar) tmp) != 0){
1219 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1220 add_timer(&sl->outfill_timer);
1221 set_bit(SLF_OUTWAIT, &sl->flags);
1223 end_bh_atomic();
1224 return 0;
1226 case SIOCGOUTFILL:
1227 if (put_user(sl->outfill, (int *)arg))
1228 return -EFAULT;
1229 return 0;
1230 /* VSV changes end */
1231 #endif
1233 /* Allow stty to read, but not set, the serial port */
1234 case TCGETS:
1235 case TCGETA:
1236 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1238 default:
1239 return -ENOIOCTLCMD;
1243 /* VSV changes start here */
1244 #ifdef CONFIG_SLIP_SMART
1245 /* function do_ioctl called from net/core/dev.c
1246 to allow get/set outfill/keepalive parameter
1247 by ifconfig */
1249 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
1251 struct slip *sl = (struct slip*)(dev->priv);
1253 if (sl == NULL) /* Allocation failed ?? */
1254 return -ENODEV;
1256 start_bh_atomic(); /* Hangup would kill us */
1258 if (!sl->tty) {
1259 end_bh_atomic();
1260 return -ENODEV;
1263 switch(cmd){
1264 case SIOCSKEEPALIVE:
1265 /* max for unchar */
1266 if (((unsigned int)((unsigned long)rq->ifr_data)) > 255)
1267 return -EINVAL;
1268 if (sl->keepalive)
1269 (void)del_timer (&sl->keepalive_timer);
1270 sl->keepalive = (unchar) ((unsigned long)rq->ifr_data);
1271 if (sl->keepalive != 0) {
1272 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1273 add_timer(&sl->keepalive_timer);
1274 set_bit(SLF_KEEPTEST, &sl->flags);
1276 break;
1278 case SIOCGKEEPALIVE:
1279 rq->ifr_data=(caddr_t)((unsigned long)sl->keepalive);
1280 break;
1282 case SIOCSOUTFILL:
1283 if (((unsigned)((unsigned long)rq->ifr_data)) > 255) /* max for unchar */
1284 return -EINVAL;
1285 if (sl->outfill)
1286 del_timer (&sl->outfill_timer);
1287 if ((sl->outfill = (unchar)((unsigned long) rq->ifr_data)) != 0){
1288 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1289 add_timer(&sl->outfill_timer);
1290 set_bit(SLF_OUTWAIT, &sl->flags);
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 end_bh_atomic();
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 end_bh_atomic();
1315 return 0;
1317 #endif
1318 /* VSV changes end */
1320 /* Initialize SLIP control device -- register SLIP line discipline */
1321 #ifdef MODULE
1322 static int slip_init_ctrl_dev(void)
1323 #else /* !MODULE */
1324 int __init slip_init_ctrl_dev(struct net_device *dummy)
1325 #endif /* !MODULE */
1327 int status;
1329 if (slip_maxdev < 4) slip_maxdev = 4; /* Sanity */
1331 printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1332 #ifdef CONFIG_SLIP_MODE_SLIP6
1333 " (6 bit encapsulation enabled)"
1334 #endif
1335 ".\n",
1336 SLIP_VERSION, slip_maxdev );
1337 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1338 printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1339 #endif
1340 #ifdef CONFIG_SLIP_SMART
1341 printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1342 #endif
1344 slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1345 if (slip_ctrls == NULL)
1347 printk("SLIP: Can't allocate slip_ctrls[] array! Uaargh! (-> No SLIP available)\n");
1348 return -ENOMEM;
1351 /* Clear the pointer array, we allocate devices when we need them */
1352 memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev); /* Pointers */
1354 /* Fill in our line protocol discipline, and register it */
1355 memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1356 sl_ldisc.magic = TTY_LDISC_MAGIC;
1357 sl_ldisc.name = "slip";
1358 sl_ldisc.flags = 0;
1359 sl_ldisc.open = slip_open;
1360 sl_ldisc.close = slip_close;
1361 sl_ldisc.read = NULL;
1362 sl_ldisc.write = NULL;
1363 sl_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
1364 unsigned int, unsigned long)) slip_ioctl;
1365 sl_ldisc.poll = NULL;
1366 sl_ldisc.receive_buf = slip_receive_buf;
1367 sl_ldisc.receive_room = slip_receive_room;
1368 sl_ldisc.write_wakeup = slip_write_wakeup;
1369 if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) {
1370 printk("SLIP: can't register line discipline (err = %d)\n", status);
1374 #ifdef MODULE
1375 return status;
1376 #else
1377 /* Return "not found", so that dev_init() will unlink
1378 * the placeholder device entry for us.
1380 return ENODEV;
1381 #endif
1386 #ifdef MODULE
1389 init_module(void)
1391 return slip_init_ctrl_dev();
1394 void
1395 cleanup_module(void)
1397 int i;
1399 if (slip_ctrls != NULL) {
1400 unsigned long start = jiffies;
1401 int busy = 0;
1403 /* First of all: check for active disciplines and hangup them.
1405 do {
1406 if (busy) {
1407 current->counter = 0;
1408 schedule();
1411 busy = 0;
1412 start_bh_atomic();
1413 for (i = 0; i < slip_maxdev; i++) {
1414 struct slip_ctrl *slc = slip_ctrls[i];
1415 if (slc && slc->ctrl.tty) {
1416 busy++;
1417 tty_hangup(slc->ctrl.tty);
1420 end_bh_atomic();
1421 } while (busy && jiffies - start < 1*HZ);
1423 busy = 0;
1424 for (i = 0; i < slip_maxdev; i++) {
1425 struct slip_ctrl *slc = slip_ctrls[i];
1426 if (slc) {
1427 unregister_netdev(&slc->dev);
1428 if (slc->ctrl.tty) {
1429 printk("%s: tty discipline is still running\n", slc->dev.name);
1430 /* Pin module forever */
1431 MOD_INC_USE_COUNT;
1432 busy++;
1433 continue;
1435 sl_free_bufs(&slc->ctrl);
1436 kfree(slc);
1437 slip_ctrls[i] = NULL;
1440 if (!busy) {
1441 kfree(slip_ctrls);
1442 slip_ctrls = NULL;
1445 if ((i = tty_register_ldisc(N_SLIP, NULL)))
1447 printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1450 #endif /* MODULE */
1452 #ifdef CONFIG_SLIP_SMART
1454 * This is start of the code for multislip style line checking
1455 * added by Stanislav Voronyi. All changes before marked VSV
1458 static void sl_outfill(unsigned long sls)
1460 struct slip *sl=(struct slip *)sls;
1462 if (sl==NULL || sl->tty == NULL)
1463 return;
1465 if(sl->outfill)
1467 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1469 /* no packets were transmitted, do outfill */
1470 #ifdef CONFIG_SLIP_MODE_SLIP6
1471 unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1472 #else
1473 unsigned char s = END;
1474 #endif
1475 /* put END into tty queue. Is it right ??? */
1476 if (!test_bit(0, (void *) &sl->dev->tbusy))
1478 /* if device busy no outfill */
1479 sl->tty->driver.write(sl->tty, 0, &s, 1);
1482 else
1483 set_bit(SLF_OUTWAIT, &sl->flags);
1484 (void)del_timer(&sl->outfill_timer);
1485 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1486 add_timer(&sl->outfill_timer);
1490 static void sl_keepalive(unsigned long sls)
1492 struct slip *sl=(struct slip *)sls;
1494 if (sl == NULL || sl->tty == NULL)
1495 return;
1497 if( sl->keepalive)
1499 if(test_bit(SLF_KEEPTEST, &sl->flags))
1501 /* keepalive still high :(, we must hangup */
1502 if( sl->outfill ) /* outfill timer must be deleted too */
1503 (void)del_timer(&sl->outfill_timer);
1504 printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1505 tty_hangup(sl->tty); /* this must hangup tty & close slip */
1506 /* I think we need not something else */
1507 return;
1509 else
1510 set_bit(SLF_KEEPTEST, &sl->flags);
1511 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1512 add_timer(&sl->keepalive_timer);
1516 #endif