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>
 
  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.
 
  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
 
  46  *                                      With MODULE-loading ``insmod'', user can
 
  47  *                                      issue parameter:   slip_maxdev=1024
 
  48  *                                      (Or how much he/she wants.. Default is 256)
 
  49  * *    Stanislav Voronyi       :       Slip line checking, with ideas taken
 
  50  *                                      from multislip BSDI driver which was written
 
  51  *                                      by Igor Chechik, RELCOM Corp. Only algorithms
 
  52  *                                      have been ported to Linux SLIP driver.
 
  53  *      Vitaly E. Lavrov        :       Sane behaviour on tty hangup.
 
  54  *      Alexey Kuznetsov        :       Cleanup interfaces to tty&netdevice modules.
 
  57 #define SL_CHECK_TRANSMIT
 
  58 #include <linux/config.h>
 
  59 #include <linux/module.h>
 
  60 #include <linux/moduleparam.h>
 
  62 #include <asm/system.h>
 
  63 #include <asm/uaccess.h>
 
  64 #include <linux/bitops.h>
 
  65 #include <linux/string.h>
 
  67 #include <linux/interrupt.h>
 
  69 #include <linux/tty.h>
 
  70 #include <linux/errno.h>
 
  71 #include <linux/netdevice.h>
 
  72 #include <linux/etherdevice.h>
 
  73 #include <linux/skbuff.h>
 
  74 #include <linux/rtnetlink.h>
 
  75 #include <linux/if_arp.h>
 
  76 #include <linux/if_slip.h>
 
  77 #include <linux/delay.h>
 
  78 #include <linux/init.h>
 
  82 #include <linux/tcp.h>
 
  83 #include <net/slhc_vj.h>
 
  86 #define SLIP_VERSION    "0.8.4-NET3.019-NEWTTY"
 
  88 static struct net_device **slip_devs;
 
  90 static int slip_maxdev = SL_NRUNIT;
 
  91 module_param(slip_maxdev, int, 0);
 
  92 MODULE_PARM_DESC(slip_maxdev, "Maximum number of slip devices");
 
  94 static int slip_esc(unsigned char *p, unsigned char *d, int len);
 
  95 static void slip_unesc(struct slip *sl, unsigned char c);
 
  96 #ifdef CONFIG_SLIP_MODE_SLIP6
 
  97 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
 
  98 static void slip_unesc6(struct slip *sl, unsigned char c);
 
 100 #ifdef CONFIG_SLIP_SMART
 
 101 static void sl_keepalive(unsigned long sls);
 
 102 static void sl_outfill(unsigned long sls);
 
 103 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd);
 
 106 /********************************
 
 107 *  Buffer administration routines:
 
 112 * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because
 
 113 *       sl_realloc_bufs provides strong atomicity and reallocation
 
 114 *       on actively running device.
 
 115 *********************************/
 
 118    Allocate channel buffers.
 
 122 sl_alloc_bufs(struct slip *sl, int mtu)
 
 128 #ifdef SL_INCLUDE_CSLIP
 
 130         struct slcompress *slcomp = NULL;
 
 134          * Allocate the SLIP frame buffers:
 
 136          * rbuff        Receive buffer.
 
 137          * xbuff        Transmit buffer.
 
 138          * cbuff        Temporary compression buffer.
 
 143          * allow for arrival of larger UDP packets, even if we say not to
 
 144          * also fixes a bug in which SunOS sends 512-byte packets even with
 
 149         rbuff = kmalloc(len + 4, GFP_KERNEL);
 
 152         xbuff = kmalloc(len + 4, GFP_KERNEL);
 
 155 #ifdef SL_INCLUDE_CSLIP
 
 156         cbuff = kmalloc(len + 4, GFP_KERNEL);
 
 159         slcomp = slhc_init(16, 16);
 
 163         spin_lock_bh(&sl->lock);
 
 164         if (sl->tty == NULL) {
 
 165                 spin_unlock_bh(&sl->lock);
 
 173         rbuff = xchg(&sl->rbuff, rbuff);
 
 174         xbuff = xchg(&sl->xbuff, xbuff);
 
 175 #ifdef SL_INCLUDE_CSLIP
 
 176         cbuff = xchg(&sl->cbuff, cbuff);
 
 177         slcomp = xchg(&sl->slcomp, slcomp);
 
 178 #ifdef CONFIG_SLIP_MODE_SLIP6
 
 183         spin_unlock_bh(&sl->lock);
 
 188 #ifdef SL_INCLUDE_CSLIP
 
 198 /* Free a SLIP channel buffers. */
 
 200 sl_free_bufs(struct slip *sl)
 
 202         /* Free all SLIP frame buffers. */
 
 203         kfree(xchg(&sl->rbuff, NULL));
 
 204         kfree(xchg(&sl->xbuff, NULL));
 
 205 #ifdef SL_INCLUDE_CSLIP
 
 206         kfree(xchg(&sl->cbuff, NULL));
 
 207         slhc_free(xchg(&sl->slcomp, NULL));
 
 212    Reallocate slip channel buffers.
 
 215 static int sl_realloc_bufs(struct slip *sl, int mtu)
 
 218         struct net_device *dev = sl->dev;
 
 219         unsigned char *xbuff, *rbuff;
 
 220 #ifdef SL_INCLUDE_CSLIP
 
 221         unsigned char *cbuff;
 
 226  * allow for arrival of larger UDP packets, even if we say not to
 
 227  * also fixes a bug in which SunOS sends 512-byte packets even with
 
 233         xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 
 234         rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 
 235 #ifdef SL_INCLUDE_CSLIP
 
 236         cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 
 240 #ifdef SL_INCLUDE_CSLIP
 
 241         if (xbuff == NULL || rbuff == NULL || cbuff == NULL)  {
 
 243         if (xbuff == NULL || rbuff == NULL)  {
 
 245                 if (mtu >= sl->mtu) {
 
 246                         printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n",
 
 253         spin_lock_bh(&sl->lock);
 
 259         xbuff    = xchg(&sl->xbuff, xbuff);
 
 260         rbuff    = xchg(&sl->rbuff, rbuff);
 
 261 #ifdef SL_INCLUDE_CSLIP
 
 262         cbuff    = xchg(&sl->cbuff, cbuff);
 
 265                 if (sl->xleft <= len)  {
 
 266                         memcpy(sl->xbuff, sl->xhead, sl->xleft);
 
 272         sl->xhead = sl->xbuff;
 
 275                 if (sl->rcount <= len) {
 
 276                         memcpy(sl->rbuff, rbuff, sl->rcount);
 
 279                         sl->rx_over_errors++;
 
 280                         set_bit(SLF_ERROR, &sl->flags);
 
 289         spin_unlock_bh(&sl->lock);
 
 294 #ifdef SL_INCLUDE_CSLIP
 
 301 /* Set the "sending" flag.  This must be atomic hence the set_bit. */
 
 303 sl_lock(struct slip *sl)
 
 305         netif_stop_queue(sl->dev);
 
 309 /* Clear the "sending" flag.  This must be atomic, hence the ASM. */
 
 311 sl_unlock(struct slip *sl)
 
 313         netif_wake_queue(sl->dev);
 
 316 /* Send one completely decapsulated IP datagram to the IP layer. */
 
 318 sl_bump(struct slip *sl)
 
 324 #ifdef SL_INCLUDE_CSLIP
 
 325         if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
 
 327                 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
 
 328                         /* ignore compressed packets when CSLIP is off */
 
 329                         if (!(sl->mode & SL_MODE_CSLIP)) {
 
 330                                 printk(KERN_WARNING "%s: compressed packet ignored\n", sl->dev->name);
 
 333                         /* make sure we've reserved enough space for uncompress to use */
 
 334                         if (count + 80 > sl->buffsize) {
 
 335                                 sl->rx_over_errors++;
 
 338                         count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
 
 342                 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
 
 343                         if (!(sl->mode & SL_MODE_CSLIP)) {
 
 344                                 /* turn on header compression */
 
 345                                 sl->mode |= SL_MODE_CSLIP;
 
 346                                 sl->mode &= ~SL_MODE_ADAPTIVE;
 
 347                                 printk(KERN_INFO "%s: header compression turned on\n", sl->dev->name);
 
 349                         sl->rbuff[0] &= 0x4f;
 
 350                         if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
 
 355 #endif  /* SL_INCLUDE_CSLIP */
 
 359         skb = dev_alloc_skb(count);
 
 361                 printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", sl->dev->name);
 
 366         memcpy(skb_put(skb,count), sl->rbuff, count);
 
 367         skb->mac.raw=skb->data;
 
 368         skb->protocol=htons(ETH_P_IP);
 
 370         sl->dev->last_rx = jiffies;
 
 374 /* Encapsulate one IP datagram and stuff into a TTY queue. */
 
 376 sl_encaps(struct slip *sl, unsigned char *icp, int len)
 
 381         if (len > sl->mtu) {            /* Sigh, shouldn't occur BUT ... */
 
 382                 printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name);
 
 389 #ifdef SL_INCLUDE_CSLIP
 
 390         if (sl->mode & SL_MODE_CSLIP)  {
 
 391                 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
 
 394 #ifdef CONFIG_SLIP_MODE_SLIP6
 
 395         if(sl->mode & SL_MODE_SLIP6)
 
 396                 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
 
 399                 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
 
 401         /* Order of next two lines is *very* important.
 
 402          * When we are sending a little amount of data,
 
 403          * the transfer may be completed inside driver.write()
 
 404          * routine, because it's running with interrupts enabled.
 
 405          * In this case we *never* got WRITE_WAKEUP event,
 
 406          * if we did not request it before write operation.
 
 407          *       14 Oct 1994  Dmitry Gorodchanin.
 
 409         sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
 
 410         actual = sl->tty->driver->write(sl->tty, sl->xbuff, count);
 
 411 #ifdef SL_CHECK_TRANSMIT
 
 412         sl->dev->trans_start = jiffies;
 
 414         sl->xleft = count - actual;
 
 415         sl->xhead = sl->xbuff + actual;
 
 416 #ifdef CONFIG_SLIP_SMART
 
 418         clear_bit(SLF_OUTWAIT, &sl->flags);     /* reset outfill flag */
 
 423  * Called by the driver when there's room for more data.  If we have
 
 424  * more packets to send, we send them here.
 
 426 static void slip_write_wakeup(struct tty_struct *tty)
 
 429         struct slip *sl = (struct slip *) tty->disc_data;
 
 431         /* First make sure we're connected. */
 
 432         if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) {
 
 435         if (sl->xleft <= 0)  {
 
 436                 /* Now serial buffer is almost free & we can start
 
 437                  * transmission of another packet */
 
 439                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 
 444         actual = tty->driver->write(tty, sl->xhead, sl->xleft);
 
 449 static void sl_tx_timeout(struct net_device *dev)
 
 451         struct slip *sl = netdev_priv(dev);
 
 453         spin_lock(&sl->lock);
 
 455         if (netif_queue_stopped(dev)) {
 
 456                 if (!netif_running(dev))
 
 459                 /* May be we must check transmitter timeout here ?
 
 460                  *      14 Oct 1994 Dmitry Gorodchanin.
 
 462 #ifdef SL_CHECK_TRANSMIT
 
 463                 if (time_before(jiffies, dev->trans_start + 20 * HZ))  {
 
 464                         /* 20 sec timeout not reached */
 
 467                 printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
 
 468                        (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft) ?
 
 469                        "bad line quality" : "driver error");
 
 471                 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 
 477         spin_unlock(&sl->lock);
 
 481 /* Encapsulate an IP datagram and kick it into a TTY queue. */
 
 483 sl_xmit(struct sk_buff *skb, struct net_device *dev)
 
 485         struct slip *sl = netdev_priv(dev);
 
 487         spin_lock(&sl->lock);
 
 488         if (!netif_running(dev))  {
 
 489                 spin_unlock(&sl->lock);
 
 490                 printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name);
 
 494         if (sl->tty == NULL) {
 
 495                 spin_unlock(&sl->lock);
 
 501         sl->tx_bytes+=skb->len;
 
 502         sl_encaps(sl, skb->data, skb->len);
 
 503         spin_unlock(&sl->lock);
 
 510 /******************************************
 
 511  *   Routines looking at netdevice side.
 
 512  ******************************************/
 
 514 /* Netdevice UP -> DOWN routine */
 
 517 sl_close(struct net_device *dev)
 
 519         struct slip *sl = netdev_priv(dev);
 
 521         spin_lock_bh(&sl->lock);
 
 523                 /* TTY discipline is running. */
 
 524                 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 
 526         netif_stop_queue(dev);
 
 529         spin_unlock_bh(&sl->lock);
 
 534 /* Netdevice DOWN -> UP routine */
 
 536 static int sl_open(struct net_device *dev)
 
 538         struct slip *sl = netdev_priv(dev);
 
 543         sl->flags &= (1 << SLF_INUSE);
 
 544         netif_start_queue(dev);
 
 548 /* Netdevice change MTU request */
 
 550 static int sl_change_mtu(struct net_device *dev, int new_mtu)
 
 552         struct slip *sl = netdev_priv(dev);
 
 554         if (new_mtu < 68 || new_mtu > 65534)
 
 557         if (new_mtu != dev->mtu)
 
 558                 return sl_realloc_bufs(sl, new_mtu);
 
 562 /* Netdevice get statistics request */
 
 564 static struct net_device_stats *
 
 565 sl_get_stats(struct net_device *dev)
 
 567         static struct net_device_stats stats;
 
 568         struct slip *sl = netdev_priv(dev);
 
 569 #ifdef SL_INCLUDE_CSLIP
 
 570         struct slcompress *comp;
 
 573         memset(&stats, 0, sizeof(struct net_device_stats));
 
 575         stats.rx_packets     = sl->rx_packets;
 
 576         stats.tx_packets     = sl->tx_packets;
 
 577         stats.rx_bytes       = sl->rx_bytes;
 
 578         stats.tx_bytes       = sl->tx_bytes;
 
 579         stats.rx_dropped     = sl->rx_dropped;
 
 580         stats.tx_dropped     = sl->tx_dropped;
 
 581         stats.tx_errors      = sl->tx_errors;
 
 582         stats.rx_errors      = sl->rx_errors;
 
 583         stats.rx_over_errors = sl->rx_over_errors;
 
 584 #ifdef SL_INCLUDE_CSLIP
 
 585         stats.rx_fifo_errors = sl->rx_compressed;
 
 586         stats.tx_fifo_errors = sl->tx_compressed;
 
 587         stats.collisions     = sl->tx_misses;
 
 590                 stats.rx_fifo_errors += comp->sls_i_compressed;
 
 591                 stats.rx_dropped     += comp->sls_i_tossed;
 
 592                 stats.tx_fifo_errors += comp->sls_o_compressed;
 
 593                 stats.collisions     += comp->sls_o_misses;
 
 595 #endif /* CONFIG_INET */
 
 599 /* Netdevice register callback */
 
 601 static int sl_init(struct net_device *dev)
 
 603         struct slip *sl = netdev_priv(dev);
 
 606          *      Finish setting up the DEVICE info. 
 
 610         dev->type               = ARPHRD_SLIP + sl->mode;
 
 611 #ifdef SL_CHECK_TRANSMIT
 
 612         dev->tx_timeout         = sl_tx_timeout;
 
 613         dev->watchdog_timeo     = 20*HZ;
 
 619 static void sl_uninit(struct net_device *dev)
 
 621         struct slip *sl = netdev_priv(dev);
 
 626 static void sl_setup(struct net_device *dev)
 
 629         dev->uninit             = sl_uninit;
 
 631         dev->destructor         = free_netdev;
 
 632         dev->stop               = sl_close;
 
 633         dev->get_stats          = sl_get_stats;
 
 634         dev->change_mtu         = sl_change_mtu;
 
 635         dev->hard_start_xmit    = sl_xmit;
 
 636 #ifdef CONFIG_SLIP_SMART
 
 637         dev->do_ioctl           = sl_ioctl;
 
 639         dev->hard_header_len    = 0;
 
 641         dev->tx_queue_len       = 10;
 
 643         SET_MODULE_OWNER(dev);
 
 645         /* New-style flags. */
 
 646         dev->flags              = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST;
 
 649 /******************************************
 
 650   Routines looking at TTY side.
 
 651  ******************************************/
 
 654 static int slip_receive_room(struct tty_struct *tty)
 
 656         return 65536;  /* We can handle an infinite amount of data. :-) */
 
 660  * Handle the 'receiver data ready' interrupt.
 
 661  * This function is called by the 'tty_io' module in the kernel when
 
 662  * a block of SLIP data has been received, which can now be decapsulated
 
 663  * and sent on to some IP layer for further processing. This will not
 
 664  * be re-entered while running but other ldisc functions may be called
 
 668 static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
 
 670         struct slip *sl = (struct slip *) tty->disc_data;
 
 672         if (!sl || sl->magic != SLIP_MAGIC ||
 
 673             !netif_running(sl->dev))
 
 676         /* Read the characters out of the buffer */
 
 679                         if (!test_and_set_bit(SLF_ERROR, &sl->flags))  {
 
 685 #ifdef CONFIG_SLIP_MODE_SLIP6
 
 686                 if (sl->mode & SL_MODE_SLIP6)
 
 687                         slip_unesc6(sl, *cp++);
 
 690                         slip_unesc(sl, *cp++);
 
 694 /************************************
 
 695  *  slip_open helper routines.
 
 696  ************************************/
 
 698 /* Collect hanged up channels */
 
 700 static void sl_sync(void)
 
 703         struct net_device *dev;
 
 706         for (i = 0; i < slip_maxdev; i++) {
 
 707                 if ((dev = slip_devs[i]) == NULL)
 
 710                 sl = netdev_priv(dev);
 
 711                 if (sl->tty || sl->leased)
 
 713                 if (dev->flags&IFF_UP)
 
 719 /* Find a free SLIP channel, and link in this `tty' line. */
 
 726         struct net_device *dev = NULL;
 
 729         if (slip_devs == NULL) 
 
 730                 return NULL;    /* Master array missing ! */
 
 732         for (i = 0; i < slip_maxdev; i++) {
 
 737                 sl = netdev_priv(dev);
 
 739                         if (sl->line != line)
 
 744                         /* Clear ESCAPE & ERROR flags */
 
 745                         sl->flags &= (1 << SLF_INUSE);
 
 752                 if (current->pid == sl->pid) {
 
 753                         if (sl->line == line && score < 3) {
 
 764                 if (sl->line == line && score < 1) {
 
 779                         sl = netdev_priv(dev);
 
 780                         sl->flags &= (1 << SLF_INUSE);
 
 785         /* Sorry, too many, all slots in use */
 
 786         if (i >= slip_maxdev)
 
 790                 sl = netdev_priv(dev);
 
 791                 if (test_bit(SLF_INUSE, &sl->flags)) {
 
 792                         unregister_netdevice(dev);
 
 800                 sprintf(name, "sl%d", i);
 
 802                 dev = alloc_netdev(sizeof(*sl), name, sl_setup);
 
 808         sl = netdev_priv(dev);
 
 810         /* Initialize channel control data */
 
 811         sl->magic       = SLIP_MAGIC;
 
 813         spin_lock_init(&sl->lock);
 
 814         sl->mode        = SL_MODE_DEFAULT;
 
 815 #ifdef CONFIG_SLIP_SMART
 
 816         init_timer(&sl->keepalive_timer);       /* initialize timer_list struct */
 
 817         sl->keepalive_timer.data=(unsigned long)sl;
 
 818         sl->keepalive_timer.function=sl_keepalive;
 
 819         init_timer(&sl->outfill_timer);
 
 820         sl->outfill_timer.data=(unsigned long)sl;
 
 821         sl->outfill_timer.function=sl_outfill;
 
 829  * Open the high-level part of the SLIP channel.
 
 830  * This function is called by the TTY module when the
 
 831  * SLIP line discipline is called for.  Because we are
 
 832  * sure the tty line exists, we only have to link it to
 
 833  * a free SLIP channel...
 
 835  * Called in process context serialized from other ldisc calls.
 
 838 static int slip_open(struct tty_struct *tty)
 
 843         if(!capable(CAP_NET_ADMIN))
 
 846         /* RTnetlink lock is misused here to serialize concurrent
 
 847            opens of slip channels. There are better ways, but it is
 
 852         /* Collect hanged up channels. */
 
 855         sl = (struct slip *) tty->disc_data;
 
 858         /* First make sure we're not already connected. */
 
 859         if (sl && sl->magic == SLIP_MAGIC)
 
 862         /* OK.  Find a free SLIP channel to use. */
 
 864         if ((sl = sl_alloc(tty_devnum(tty))) == NULL)
 
 869         sl->line = tty_devnum(tty);
 
 870         sl->pid = current->pid;
 
 872         /* FIXME: already done before we were called - seems this can go */
 
 873         if (tty->driver->flush_buffer)
 
 874                 tty->driver->flush_buffer(tty);
 
 876         if (!test_bit(SLF_INUSE, &sl->flags)) {
 
 877                 /* Perform the low-level SLIP initialization. */
 
 878                 if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0)
 
 881                 set_bit(SLF_INUSE, &sl->flags);
 
 883                 if ((err = register_netdevice(sl->dev)))
 
 887 #ifdef CONFIG_SLIP_SMART
 
 889                 sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
 
 890                 add_timer (&sl->keepalive_timer);
 
 893                 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
 
 894                 add_timer (&sl->outfill_timer);
 
 898         /* Done.  We have linked the TTY line to a channel. */
 
 900         return sl->dev->base_addr;
 
 907         tty->disc_data = NULL;
 
 908         clear_bit(SLF_INUSE, &sl->flags);
 
 913         /* Count references from TTY module */
 
 919   FIXME: 1,2 are fixed 3 was never true anyway.
 
 921    Let me to blame a bit.
 
 922    1. TTY module calls this funstion on soft interrupt.
 
 923    2. TTY module calls this function WITH MASKED INTERRUPTS!
 
 924    3. TTY module does not notify us about line discipline
 
 927    Seems, now it is clean. The solution is to consider netdevice and
 
 928    line discipline sides as two independent threads.
 
 930    By-product (not desired): sl? does not feel hangups and remains open.
 
 931    It is supposed, that user level program (dip, diald, slattach...)
 
 932    will catch SIGHUP and make the rest of work. 
 
 934    I see no way to make more with current tty code. --ANK
 
 938  * Close down a SLIP channel.
 
 939  * This means flushing out any pending queues, and then returning. This
 
 940  * call is serialized against other ldisc functions.
 
 943 slip_close(struct tty_struct *tty)
 
 945         struct slip *sl = (struct slip *) tty->disc_data;
 
 947         /* First make sure we're connected. */
 
 948         if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty)
 
 951         tty->disc_data = NULL;
 
 956         /* VSV = very important to remove timers */
 
 957 #ifdef CONFIG_SLIP_SMART
 
 958         del_timer_sync(&sl->keepalive_timer);
 
 959         del_timer_sync(&sl->outfill_timer);
 
 962         /* Count references from TTY module */
 
 965  /************************************************************************
 
 966   *                     STANDARD SLIP ENCAPSULATION                      *
 
 967   ************************************************************************/
 
 970 slip_esc(unsigned char *s, unsigned char *d, int len)
 
 972         unsigned char *ptr = d;
 
 976          * Send an initial END character to flush out any
 
 977          * data that may have accumulated in the receiver
 
 984          * For each byte in the packet, send the appropriate
 
 985          * character sequence, according to the SLIP protocol.
 
1007 static void slip_unesc(struct slip *sl, unsigned char s)
 
1012 #ifdef CONFIG_SLIP_SMART
 
1013                 /* drop keeptest bit = VSV */
 
1014                 if (test_bit(SLF_KEEPTEST, &sl->flags))
 
1015                         clear_bit(SLF_KEEPTEST, &sl->flags);
 
1018                 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
 
1021                 clear_bit(SLF_ESCAPE, &sl->flags);
 
1026                 set_bit(SLF_ESCAPE, &sl->flags);
 
1029                 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))  {
 
1034                 if (test_and_clear_bit(SLF_ESCAPE, &sl->flags))  {
 
1039         if (!test_bit(SLF_ERROR, &sl->flags))  {
 
1040                 if (sl->rcount < sl->buffsize)  {
 
1041                         sl->rbuff[sl->rcount++] = s;
 
1044                 sl->rx_over_errors++;
 
1045                 set_bit(SLF_ERROR, &sl->flags);
 
1050 #ifdef CONFIG_SLIP_MODE_SLIP6
 
1051 /************************************************************************
 
1052  *                       6 BIT SLIP ENCAPSULATION                       *
 
1053  ************************************************************************/
 
1056 slip_esc6(unsigned char *s, unsigned char *d, int len)
 
1058         unsigned char *ptr = d;
 
1061         unsigned short v = 0;
 
1065          * Send an initial END character to flush out any
 
1066          * data that may have accumulated in the receiver
 
1067          * due to line noise.
 
1073          * Encode the packet into printable ascii characters
 
1076         for (i = 0; i < len; ++i) {
 
1077                 v = (v << 8) | s[i];
 
1081                         c = 0x30 + ((v >> bits) & 0x3F);
 
1086                 c = 0x30 + ((v << (6 - bits)) & 0x3F);
 
1094 slip_unesc6(struct slip *sl, unsigned char s)
 
1099 #ifdef CONFIG_SLIP_SMART
 
1100                 /* drop keeptest bit = VSV */
 
1101                 if (test_bit(SLF_KEEPTEST, &sl->flags))
 
1102                         clear_bit(SLF_KEEPTEST, &sl->flags);
 
1105                 if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
 
1111         } else if (s >= 0x30 && s < 0x70) {
 
1112                 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
 
1114                 if (sl->xbits >= 8) {
 
1116                         c = (unsigned char)(sl->xdata >> sl->xbits);
 
1117                         if (!test_bit(SLF_ERROR, &sl->flags))  {
 
1118                                 if (sl->rcount < sl->buffsize)  {
 
1119                                         sl->rbuff[sl->rcount++] = c;
 
1122                                 sl->rx_over_errors++;
 
1123                                 set_bit(SLF_ERROR, &sl->flags);
 
1128 #endif /* CONFIG_SLIP_MODE_SLIP6 */
 
1130 /* Perform I/O control on an active SLIP channel. */
 
1131 static int slip_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
 
1133         struct slip *sl = (struct slip *) tty->disc_data;
 
1135         int __user *p = (int __user *)arg;
 
1137         /* First make sure we're connected. */
 
1138         if (!sl || sl->magic != SLIP_MAGIC) {
 
1144                 tmp = strlen(sl->dev->name) + 1;
 
1145                 if (copy_to_user((void __user *)arg, sl->dev->name, tmp))
 
1150                 if (put_user(sl->mode, p))
 
1155                 if (get_user(tmp, p))
 
1157 #ifndef SL_INCLUDE_CSLIP
 
1158                 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))  {
 
1162                 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
 
1163                     (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))  {
 
1164                         /* return -EINVAL; */
 
1165                         tmp &= ~SL_MODE_ADAPTIVE;
 
1168 #ifndef CONFIG_SLIP_MODE_SLIP6
 
1169                 if (tmp & SL_MODE_SLIP6)  {
 
1174                 sl->dev->type = ARPHRD_SLIP+sl->mode;
 
1180 #ifdef CONFIG_SLIP_SMART
 
1181         /* VSV changes start here */
 
1182         case SIOCSKEEPALIVE:
 
1183                 if (get_user(tmp, p))
 
1185                 if (tmp > 255) /* max for unchar */
 
1188                 spin_lock_bh(&sl->lock);
 
1190                         spin_unlock_bh(&sl->lock);
 
1193                 if ((sl->keepalive = (unchar) tmp) != 0) {
 
1194                         mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
 
1195                         set_bit(SLF_KEEPTEST, &sl->flags);
 
1197                         del_timer (&sl->keepalive_timer);
 
1199                 spin_unlock_bh(&sl->lock);
 
1202         case SIOCGKEEPALIVE:
 
1203                 if (put_user(sl->keepalive, p))
 
1208                 if (get_user(tmp, p))
 
1210                 if (tmp > 255) /* max for unchar */
 
1212                 spin_lock_bh(&sl->lock);
 
1214                         spin_unlock_bh(&sl->lock);
 
1217                 if ((sl->outfill = (unchar) tmp) != 0){
 
1218                         mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
 
1219                         set_bit(SLF_OUTWAIT, &sl->flags);
 
1221                         del_timer (&sl->outfill_timer);
 
1223                 spin_unlock_bh(&sl->lock);
 
1227                 if (put_user(sl->outfill, p))
 
1230         /* VSV changes end */
 
1233         /* Allow stty to read, but not set, the serial port */
 
1236                 return n_tty_ioctl(tty, file, cmd, arg);
 
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
 
1249 static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd)
 
1251         struct slip *sl = netdev_priv(dev);
 
1252         unsigned long *p = (unsigned long *)&rq->ifr_ifru;
 
1254         if (sl == NULL)         /* Allocation failed ?? */
 
1257         spin_lock_bh(&sl->lock);
 
1260                 spin_unlock_bh(&sl->lock);
 
1265         case SIOCSKEEPALIVE:
 
1266                 /* max for unchar */
 
1267                 if ((unsigned)*p > 255) {
 
1268                         spin_unlock_bh(&sl->lock);
 
1271                 sl->keepalive = (unchar) *p;
 
1272                 if (sl->keepalive != 0) {
 
1273                         sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
 
1274                         mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
 
1275                         set_bit(SLF_KEEPTEST, &sl->flags);
 
1277                         del_timer(&sl->keepalive_timer);
 
1281         case SIOCGKEEPALIVE:
 
1286                 if ((unsigned)*p > 255) { /* max for unchar */
 
1287                         spin_unlock_bh(&sl->lock);
 
1290                 if ((sl->outfill = (unchar)*p) != 0){
 
1291                         mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
 
1292                         set_bit(SLF_OUTWAIT, &sl->flags);
 
1294                         del_timer (&sl->outfill_timer);
 
1303                 /* Resolve race condition, when ioctl'ing hanged up 
 
1304                    and opened by another process device.
 
1306                 if (sl->tty != current->signal->tty && sl->pid != current->pid) {
 
1307                         spin_unlock_bh(&sl->lock);
 
1318         spin_unlock_bh(&sl->lock);
 
1322 /* VSV changes end */
 
1324 static struct tty_ldisc sl_ldisc = {
 
1325         .owner          = THIS_MODULE,
 
1326         .magic          = TTY_LDISC_MAGIC,
 
1329         .close          = slip_close,
 
1330         .ioctl          = slip_ioctl,
 
1331         .receive_buf    = slip_receive_buf,
 
1332         .receive_room   = slip_receive_room,
 
1333         .write_wakeup   = slip_write_wakeup,
 
1336 static int __init slip_init(void)
 
1340         if (slip_maxdev < 4)
 
1341                 slip_maxdev = 4; /* Sanity */
 
1343         printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
 
1344 #ifdef CONFIG_SLIP_MODE_SLIP6
 
1345                " (6 bit encapsulation enabled)"
 
1348                SLIP_VERSION, slip_maxdev );
 
1349 #if defined(SL_INCLUDE_CSLIP)
 
1350         printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
 
1352 #ifdef CONFIG_SLIP_SMART
 
1353         printk(KERN_INFO "SLIP linefill/keepalive option.\n");
 
1356         slip_devs = kmalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL);
 
1358                 printk(KERN_ERR "SLIP: Can't allocate slip devices array!  Uaargh! (-> No SLIP available)\n");
 
1362         /* Clear the pointer array, we allocate devices when we need them */
 
1363         memset(slip_devs, 0, sizeof(struct net_device *)*slip_maxdev); 
 
1365         /* Fill in our line protocol discipline, and register it */
 
1366         if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0)  {
 
1367                 printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
 
1373 static void __exit slip_exit(void)
 
1376         struct net_device *dev;
 
1378         unsigned long timeout = jiffies + HZ;
 
1381         if (slip_devs == NULL) 
 
1384         /* First of all: check for active disciplines and hangup them.
 
1388                         msleep_interruptible(100);
 
1391                 for (i = 0; i < slip_maxdev; i++) {
 
1395                         sl = netdev_priv(dev);
 
1396                         spin_lock_bh(&sl->lock);
 
1399                                 tty_hangup(sl->tty);
 
1401                         spin_unlock_bh(&sl->lock);
 
1403         } while (busy && time_before(jiffies, timeout));
 
1406         for (i = 0; i < slip_maxdev; i++) {
 
1410                 slip_devs[i] = NULL;
 
1412                 sl = netdev_priv(dev);
 
1414                         printk(KERN_ERR "%s: tty discipline still running\n",
 
1416                         /* Intentionally leak the control block. */
 
1417                         dev->destructor = NULL;
 
1420                 unregister_netdev(dev);
 
1426         if ((i = tty_unregister_ldisc(N_SLIP)))
 
1428                 printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
 
1432 module_init(slip_init);
 
1433 module_exit(slip_exit);
 
1435 #ifdef CONFIG_SLIP_SMART
 
1437  * This is start of the code for multislip style line checking
 
1438  * added by Stanislav Voronyi. All changes before marked VSV
 
1441 static void sl_outfill(unsigned long sls)
 
1443         struct slip *sl=(struct slip *)sls;
 
1445         spin_lock(&sl->lock);
 
1447         if (sl->tty == NULL)
 
1452                 if( test_bit(SLF_OUTWAIT, &sl->flags) )
 
1454                         /* no packets were transmitted, do outfill */
 
1455 #ifdef CONFIG_SLIP_MODE_SLIP6
 
1456                         unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
 
1458                         unsigned char s = END;
 
1460                         /* put END into tty queue. Is it right ??? */
 
1461                         if (!netif_queue_stopped(sl->dev))
 
1463                                 /* if device busy no outfill */
 
1464                                 sl->tty->driver->write(sl->tty, &s, 1);
 
1468                         set_bit(SLF_OUTWAIT, &sl->flags);
 
1470                 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
 
1473         spin_unlock(&sl->lock);
 
1476 static void sl_keepalive(unsigned long sls)
 
1478         struct slip *sl=(struct slip *)sls;
 
1480         spin_lock(&sl->lock);
 
1482         if (sl->tty == NULL)
 
1487                 if(test_bit(SLF_KEEPTEST, &sl->flags))
 
1489                         /* keepalive still high :(, we must hangup */
 
1490                         if( sl->outfill ) /* outfill timer must be deleted too */
 
1491                                 (void)del_timer(&sl->outfill_timer);
 
1492                         printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
 
1493                         tty_hangup(sl->tty); /* this must hangup tty & close slip */
 
1494                         /* I think we need not something else */
 
1498                         set_bit(SLF_KEEPTEST, &sl->flags);
 
1500                 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
 
1504         spin_unlock(&sl->lock);
 
1508 MODULE_LICENSE("GPL");
 
1509 MODULE_ALIAS_LDISC(N_SLIP);