root/drivers/net/slip.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sl_alloc
  2. sl_free
  3. sl_changedmtu
  4. sl_lock
  5. sl_unlock
  6. sl_bump
  7. sl_encaps
  8. slip_write_wakeup
  9. sl_xmit
  10. sl_header
  11. sl_rebuild_header
  12. sl_open
  13. sl_close
  14. slip_receive_room
  15. slip_receive_buf
  16. slip_open
  17. slip_close
  18. sl_get_stats
  19. slip_esc
  20. slip_unesc
  21. slip_esc6
  22. slip_unesc6
  23. sl_set_mac_address
  24. sl_set_dev_mac_address
  25. slip_ioctl
  26. sl_open_dev
  27. slip_init_ctrl_dev
  28. slip_init
  29. init_module
  30. cleanup_module
  31. sl_outfill
  32. sl_keepalive

   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.
   5  *
   6  * Version:     @(#)slip.c      0.8.3   12/24/94
   7  *
   8  * Authors:     Laurence Culhane, <loz@holmes.demon.co.uk>
   9  *              Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  10  *
  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  */
  54 
  55 #define SL_CHECK_TRANSMIT
  56 #include <linux/config.h>
  57 #include <linux/module.h>
  58 
  59 /* Undef this, if you don't need 6bit encapsulation code in the driver */
  60 #define CONFIG_SLIP_MODE_SLIP6
  61 
  62 #include <asm/system.h>
  63 #include <asm/segment.h>
  64 #include <asm/bitops.h>
  65 #include <linux/string.h>
  66 #include <linux/mm.h>
  67 #include <linux/interrupt.h>
  68 #include <linux/in.h>
  69 #include <linux/tty.h>
  70 #include <linux/errno.h>
  71 #include <linux/netdevice.h>
  72 #ifdef CONFIG_AX25
  73 #include <linux/timer.h>
  74 #include <net/ax25.h>
  75 #endif
  76 #include <linux/etherdevice.h>
  77 #include <linux/skbuff.h>
  78 #include <linux/if_arp.h>
  79 #include <linux/if_slip.h>
  80 #include "slip.h"
  81 #ifdef CONFIG_INET
  82 #include <linux/ip.h>
  83 #include <linux/tcp.h>
  84 #include "slhc.h"
  85 #endif
  86 
  87 #ifdef MODULE
  88 #define SLIP_VERSION    "0.8.4-NET3.019-NEWTTY-MODULAR"
  89 #else
  90 #define SLIP_VERSION    "0.8.4-NET3.019-NEWTTY"
  91 #endif
  92 
  93 
  94 typedef struct slip_ctrl {
  95         char            if_name[8];     /* "sl0\0" .. "sl99999\0"       */
  96         struct slip     ctrl;           /* SLIP things                  */
  97         struct device   dev;            /* the device                   */
  98 } slip_ctrl_t;
  99 static slip_ctrl_t      **slip_ctrls = NULL;
 100 int slip_maxdev = SL_NRUNIT;            /* Can be overridden with insmod! */
 101 
 102 static struct tty_ldisc sl_ldisc;
 103 
 104 static int slip_esc(unsigned char *p, unsigned char *d, int len);
 105 static void slip_unesc(struct slip *sl, unsigned char c);
 106 #ifdef CONFIG_SLIP_MODE_SLIP6
 107 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
 108 static void slip_unesc6(struct slip *sl, unsigned char c);
 109 #endif
 110 #ifdef CONFIG_SLIP_SMART
 111 static void sl_keepalive(unsigned long sls);
 112 static void sl_outfill(unsigned long sls);
 113 #endif
 114 
 115 /* Find a free SLIP channel, and link in this `tty' line. */
 116 static inline struct slip *
 117 sl_alloc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 118 {
 119         slip_ctrl_t *slp;
 120         int i;
 121 
 122         if (slip_ctrls == NULL) return NULL;    /* Master array missing ! */
 123 
 124         for (i = 0; i < slip_maxdev; i++) {
 125           slp = slip_ctrls[i];
 126           /* Not allocated ? */
 127           if (slp == NULL)
 128             break;
 129           /* Not in use ? */
 130           if (!set_bit(SLF_INUSE, &slp->ctrl.flags))
 131             break;
 132         }
 133         /* SLP is set.. */
 134 
 135         /* Sorry, too many, all slots in use */
 136         if (i >= slip_maxdev) return NULL;
 137 
 138         /* If no channels are available, allocate one */
 139         if (!slp &&
 140             (slip_ctrls[i] = (slip_ctrl_t *)kmalloc(sizeof(slip_ctrl_t),
 141                                                     GFP_KERNEL)) != NULL) {
 142           slp = slip_ctrls[i];
 143           memset(slp, 0, sizeof(slip_ctrl_t));
 144 
 145           /* Initialize channel control data */
 146           set_bit(SLF_INUSE, &slp->ctrl.flags);
 147           slp->ctrl.tty         = NULL;
 148           sprintf(slp->if_name, "sl%d", i);
 149           slp->dev.name         = slp->if_name;
 150           slp->dev.base_addr    = i;
 151           slp->dev.priv         = (void*)&(slp->ctrl);
 152           slp->dev.next         = NULL;
 153           slp->dev.init         = slip_init;
 154 /* printk(KERN_INFO "slip: kmalloc()ed SLIP control node for line %s\n",
 155    slp->if_name); */
 156         }
 157         if (slp != NULL) {
 158 
 159           /* register device so that it can be ifconfig'ed       */
 160           /* slip_init() will be called as a side-effect         */
 161           /* SIDE-EFFECT WARNING: slip_init() CLEARS slp->ctrl ! */
 162 
 163           if (register_netdev(&(slp->dev)) == 0) {
 164             /* (Re-)Set the INUSE bit.   Very Important! */
 165             set_bit(SLF_INUSE, &slp->ctrl.flags);
 166             slp->ctrl.dev = &(slp->dev);
 167             slp->dev.priv = (void*)&(slp->ctrl);
 168 
 169 /* printk(KERN_INFO "slip: linked in netdev %s for active use\n",
 170    slp->if_name); */
 171 
 172             return (&(slp->ctrl));
 173 
 174           } else {
 175             clear_bit(SLF_INUSE,&(slp->ctrl.flags));
 176             printk("sl_alloc() - register_netdev() failure.\n");
 177           }
 178         }
 179 
 180         return NULL;
 181 }
 182 
 183 
 184 /* Free a SLIP channel. */
 185 static inline void
 186 sl_free(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188         /* Free all SLIP frame buffers. */
 189         if (sl->rbuff)  {
 190                 kfree(sl->rbuff);
 191         }
 192         sl->rbuff = NULL;
 193         if (sl->xbuff)  {
 194                 kfree(sl->xbuff);
 195         }
 196         sl->xbuff = NULL;
 197 #ifdef SL_INCLUDE_CSLIP
 198         /* Save CSLIP statistics */
 199         if (sl->slcomp)  {
 200                 sl->rx_compressed += sl->slcomp->sls_i_compressed;
 201                 sl->rx_dropped    += sl->slcomp->sls_i_tossed;
 202                 sl->tx_compressed += sl->slcomp->sls_o_compressed;
 203                 sl->tx_misses     += sl->slcomp->sls_o_misses;
 204         }
 205         if (sl->cbuff)  {
 206                 kfree(sl->cbuff);
 207         }
 208         sl->cbuff = NULL;
 209         if(sl->slcomp)
 210                 slhc_free(sl->slcomp);
 211         sl->slcomp = NULL;
 212 #endif
 213 
 214         if (!clear_bit(SLF_INUSE, &sl->flags)) {
 215                 printk("%s: sl_free for already free unit.\n", sl->dev->name);
 216         }
 217 }
 218 
 219 /* MTU has been changed by the IP layer. Unfortunately we are not told
 220    about this, but we spot it ourselves and fix things up. We could be
 221    in an upcall from the tty driver, or in an ip packet queue. */
 222 
 223 static void sl_changedmtu(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 224 {
 225         struct device *dev = sl->dev;
 226         unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
 227 #ifdef SL_INCLUDE_CSLIP
 228         unsigned char *cbuff, *ocbuff;
 229 #endif
 230         int len;
 231         unsigned long flags;
 232 
 233         len = dev->mtu * 2;
 234 /*
 235  * allow for arrival of larger UDP packets, even if we say not to
 236  * also fixes a bug in which SunOS sends 512-byte packets even with
 237  * an MSS of 128
 238  */
 239         if (len < 576 * 2)  {
 240                 len = 576 * 2;
 241         }
 242 
 243         xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 244         rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 245 #ifdef SL_INCLUDE_CSLIP
 246         cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 247 #endif
 248 
 249 #ifdef SL_INCLUDE_CSLIP
 250         if (xbuff == NULL || rbuff == NULL || cbuff == NULL)  {
 251 #else
 252         if (xbuff == NULL || rbuff == NULL)  {
 253 #endif
 254                 printk("%s: unable to grow slip buffers, MTU change cancelled.\n",
 255                        sl->dev->name);
 256                 dev->mtu = sl->mtu;
 257                 if (xbuff != NULL)  {
 258                         kfree(xbuff);
 259                 }
 260                 if (rbuff != NULL)  {
 261                         kfree(rbuff);
 262                 }
 263 #ifdef SL_INCLUDE_CSLIP
 264                 if (cbuff != NULL)  {
 265                         kfree(cbuff);
 266                 }
 267 #endif
 268                 return;
 269         }
 270 
 271         save_flags(flags); cli();
 272 
 273         oxbuff    = sl->xbuff;
 274         sl->xbuff = xbuff;
 275         orbuff    = sl->rbuff;
 276         sl->rbuff = rbuff;
 277 #ifdef SL_INCLUDE_CSLIP
 278         ocbuff    = sl->cbuff;
 279         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++;
 287                 }
 288         }
 289         sl->xhead = sl->xbuff;
 290 
 291         if (sl->rcount)  {
 292                 if (sl->rcount <= len) {
 293                         memcpy(sl->rbuff, orbuff, sl->rcount);
 294                 } else  {
 295                         sl->rcount = 0;
 296                         sl->rx_over_errors++;
 297                         set_bit(SLF_ERROR, &sl->flags);
 298                 }
 299         }
 300 #ifdef CONFIG_AX25
 301         sl->mtu      = dev->mtu + 73;
 302 #else
 303         sl->mtu      = dev->mtu;
 304 #endif
 305         sl->buffsize = len;
 306 
 307         restore_flags(flags);
 308 
 309         if (oxbuff != NULL)   {
 310                 kfree(oxbuff);
 311         }
 312         if (orbuff != NULL)    {
 313                 kfree(orbuff);
 314         }
 315 #ifdef SL_INCLUDE_CSLIP
 316         if (ocbuff != NULL)  {
 317                 kfree(ocbuff);
 318         }
 319 #endif
 320 }
 321 
 322 
 323 /* Set the "sending" flag.  This must be atomic, hence the ASM. */
 324 static inline void
 325 sl_lock(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 326 {
 327         if (set_bit(0, (void *) &sl->dev->tbusy))  {
 328                 printk("%s: trying to lock already locked device!\n", sl->dev->name);
 329         }
 330 }
 331 
 332 
 333 /* Clear the "sending" flag.  This must be atomic, hence the ASM. */
 334 static inline void
 335 sl_unlock(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 336 {
 337         if (!clear_bit(0, (void *)&sl->dev->tbusy))  {
 338                 printk("%s: trying to unlock already unlocked device!\n", sl->dev->name);
 339         }
 340 }
 341 
 342 /* Send one completely decapsulated IP datagram to the IP layer. */
 343 static void
 344 sl_bump(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346         struct sk_buff *skb;
 347         int count;
 348 
 349         count = sl->rcount;
 350 #ifdef SL_INCLUDE_CSLIP
 351         if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
 352                 unsigned char c;
 353                 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
 354                         /* ignore compressed packets when CSLIP is off */
 355                         if (!(sl->mode & SL_MODE_CSLIP)) {
 356                                 printk("%s: compressed packet ignored\n", sl->dev->name);
 357                                 return;
 358                         }
 359                         /* make sure we've reserved enough space for uncompress to use */
 360                         if (count + 80 > sl->buffsize) {
 361                                 sl->rx_over_errors++;
 362                                 return;
 363                         }
 364                         count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
 365                         if (count <= 0) {
 366                                 return;
 367                         }
 368                 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
 369                         if (!(sl->mode & SL_MODE_CSLIP)) {
 370                                 /* turn on header compression */
 371                                 sl->mode |= SL_MODE_CSLIP;
 372                                 sl->mode &= ~SL_MODE_ADAPTIVE;
 373                                 printk("%s: header compression turned on\n", sl->dev->name);
 374                         }
 375                         sl->rbuff[0] &= 0x4f;
 376                         if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
 377                                 return;
 378                         }
 379                 }
 380         }
 381 #endif  /* SL_INCLUDE_CSLIP */
 382 
 383         skb = dev_alloc_skb(count);
 384         if (skb == NULL)  {
 385                 printk("%s: memory squeeze, dropping packet.\n", sl->dev->name);
 386                 sl->rx_dropped++;
 387                 return;
 388         }
 389         skb->dev = sl->dev;
 390         memcpy(skb_put(skb,count), sl->rbuff, count);
 391         skb->mac.raw=skb->data;
 392         if(sl->mode & SL_MODE_AX25)
 393                 skb->protocol=htons(ETH_P_AX25);
 394         else
 395                 skb->protocol=htons(ETH_P_IP);
 396         netif_rx(skb);
 397         sl->rx_packets++;
 398 }
 399 
 400 /* Encapsulate one IP datagram and stuff into a TTY queue. */
 401 static void
 402 sl_encaps(struct slip *sl, unsigned char *icp, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 403 {
 404         unsigned char *p;
 405         int actual, count;
 406 
 407 
 408 #ifdef CONFIG_AX25
 409         if (sl->mtu != sl->dev->mtu + 73) {     /* Someone has been ifconfigging */
 410 #else
 411         if (sl->mtu != sl->dev->mtu) {  /* Someone has been ifconfigging */
 412 #endif
 413                 sl_changedmtu(sl);
 414         }
 415 
 416         if (len > sl->mtu) {            /* Sigh, shouldn't occur BUT ... */
 417                 len = sl->mtu;
 418                 printk ("%s: truncating oversized transmit packet!\n", sl->dev->name);
 419                 sl->tx_dropped++;
 420                 sl_unlock(sl);
 421                 return;
 422         }
 423 
 424         p = icp;
 425 #ifdef SL_INCLUDE_CSLIP
 426         if (sl->mode & SL_MODE_CSLIP)  {
 427                 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
 428         }
 429 #endif
 430 #ifdef CONFIG_SLIP_MODE_SLIP6
 431         if(sl->mode & SL_MODE_SLIP6)
 432                 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
 433         else
 434 #endif
 435                 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
 436 
 437         /* Order of next two lines is *very* important.
 438          * When we are sending a little amount of data,
 439          * the transfer may be completed inside driver.write()
 440          * routine, because it's running with interrupts enabled.
 441          * In this case we *never* got WRITE_WAKEUP event,
 442          * if we did not request it before write operation.
 443          *       14 Oct 1994  Dmitry Gorodchanin.
 444          */
 445         sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
 446         actual = sl->tty->driver.write(sl->tty, 0, sl->xbuff, count);
 447 #ifdef SL_CHECK_TRANSMIT
 448         sl->dev->trans_start = jiffies;
 449 #endif
 450         sl->xleft = count - actual;
 451         sl->xhead = sl->xbuff + actual;
 452         /* VSV */
 453         clear_bit(SLF_OUTWAIT, &sl->flags);     /* reset outfill flag */
 454 }
 455 
 456 /*
 457  * Called by the driver when there's room for more data.  If we have
 458  * more packets to send, we send them here.
 459  */
 460 static void slip_write_wakeup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 461 {
 462         int actual;
 463         struct slip *sl = (struct slip *) tty->disc_data;
 464 
 465         /* First make sure we're connected. */
 466         if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start) {
 467                 return;
 468         }
 469         if (sl->xleft <= 0)  {
 470                 /* Now serial buffer is almost free & we can start
 471                  * transmission of another packet */
 472                 sl->tx_packets++;
 473                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 474                 sl_unlock(sl);
 475                 mark_bh(NET_BH);
 476                 return;
 477         }
 478 
 479         actual = tty->driver.write(tty, 0, sl->xhead, sl->xleft);
 480         sl->xleft -= actual;
 481         sl->xhead += actual;
 482 }
 483 
 484 /* Encapsulate an IP datagram and kick it into a TTY queue. */
 485 static int
 486 sl_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 487 {
 488         struct slip *sl = (struct slip*)(dev->priv);
 489 
 490         if (!dev->start)  {
 491                 printk("%s: xmit call when iface is down\n", dev->name);
 492                 return 1;
 493         }
 494         /*
 495          * If we are busy already- too bad.  We ought to be able
 496          * to queue things at this point, to allow for a little
 497          * frame buffer.  Oh well...
 498          * -----------------------------------------------------
 499          * I hate queues in SLIP driver. May be it's efficient,
 500          * but for me latency is more important. ;)
 501          * So, no queues !
 502          *        14 Oct 1994  Dmitry Gorodchanin.
 503          */
 504         if (dev->tbusy) {
 505                 /* May be we must check transmitter timeout here ?
 506                  *      14 Oct 1994 Dmitry Gorodchanin.
 507                  */
 508 #ifdef SL_CHECK_TRANSMIT
 509                 if (jiffies - dev->trans_start  < 20 * HZ)  {
 510                         /* 20 sec timeout not reached */
 511                         return 1;
 512                 }
 513                 printk("%s: transmit timed out, %s?\n", dev->name,
 514                        (sl->tty->driver.chars_in_buffer(sl->tty) || sl->xleft) ?
 515                        "bad line quality" : "driver error");
 516                 sl->xleft = 0;
 517                 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 518                 sl_unlock(sl);
 519 #else
 520                 return 1;
 521 #endif
 522         }
 523 
 524         /* We were not busy, so we are now... :-) */
 525         if (skb != NULL) {
 526                 sl_lock(sl);
 527                 sl_encaps(sl, skb->data, skb->len);
 528                 dev_kfree_skb(skb, FREE_WRITE);
 529         }
 530         return 0;
 531 }
 532 
 533 
 534 /* Return the frame type ID.  This is normally IP but maybe be AX.25. */
 535 
 536 /* Fill in the MAC-level header. Not used by SLIP. */
 537 static int
 538 sl_header(struct sk_buff *skb, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
 539           void *daddr, void *saddr, unsigned len)
 540 {
 541 #ifdef CONFIG_AX25
 542 #ifdef CONFIG_INET
 543         struct slip *sl = (struct slip*)(dev->priv);
 544 
 545         if (sl->mode & SL_MODE_AX25 && type != htons(ETH_P_AX25))  {
 546                 return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
 547         }
 548 #endif
 549 #endif
 550         return 0;
 551 }
 552 
 553 
 554 /* Rebuild the MAC-level header.  Not used by SLIP. */
 555 static int
 556 sl_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 557                   struct sk_buff *skb)
 558 {
 559 #ifdef CONFIG_AX25
 560 #ifdef CONFIG_INET
 561         struct slip *sl = (struct slip*)(dev->priv);
 562 
 563         if (sl->mode & SL_MODE_AX25) {
 564                 return ax25_rebuild_header(buff, dev, raddr, skb);
 565         }
 566 #endif
 567 #endif
 568         return 0;
 569 }
 570 
 571 
 572 /* Open the low-level part of the SLIP channel. Easy! */
 573 static int
 574 sl_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 575 {
 576         struct slip *sl = (struct slip*)(dev->priv);
 577         unsigned long len;
 578 
 579         if (sl->tty == NULL) {
 580                 return -ENODEV;
 581         }
 582 
 583         /*
 584          * Allocate the SLIP frame buffers:
 585          *
 586          * rbuff        Receive buffer.
 587          * xbuff        Transmit buffer.
 588          * cbuff        Temporary compression buffer.
 589          */
 590         len = dev->mtu * 2;
 591         /*
 592          * allow for arrival of larger UDP packets, even if we say not to
 593          * also fixes a bug in which SunOS sends 512-byte packets even with
 594          * an MSS of 128
 595          */
 596         if (len < 576 * 2)  {
 597                 len = 576 * 2;
 598         }
 599         sl->rbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
 600         if (sl->rbuff == NULL)   {
 601                 goto norbuff;
 602         }
 603         sl->xbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
 604         if (sl->xbuff == NULL)   {
 605                 goto noxbuff;
 606         }
 607 #ifdef SL_INCLUDE_CSLIP
 608         sl->cbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
 609         if (sl->cbuff == NULL)   {
 610                 goto nocbuff;
 611         }
 612         sl->slcomp = slhc_init(16, 16);
 613         if (sl->slcomp == NULL)  {
 614                 goto noslcomp;
 615         }
 616 #endif
 617 
 618 #ifdef CONFIG_AX25
 619         sl->mtu      = dev->mtu + 73;
 620 #else
 621         sl->mtu      = dev->mtu;
 622 #endif
 623         sl->buffsize = len;
 624         sl->rcount   = 0;
 625         sl->xleft    = 0;
 626 #ifdef CONFIG_SLIP_MODE_SLIP6
 627         sl->xdata    = 0;
 628         sl->xbits    = 0;
 629 #endif
 630         sl->flags   &= (1 << SLF_INUSE);      /* Clear ESCAPE & ERROR flags */
 631 #ifdef CONFIG_SLIP_SMART        
 632         sl->keepalive=0;                /* no keepalive by default = VSV */
 633         init_timer(&sl->keepalive_timer);       /* initialize timer_list struct */
 634         sl->keepalive_timer.data=(unsigned long)sl;
 635         sl->keepalive_timer.function=sl_keepalive;      
 636         sl->outfill=0;                  /* & outfill too */
 637         init_timer(&sl->outfill_timer);
 638         sl->outfill_timer.data=(unsigned long)sl;
 639         sl->outfill_timer.function=sl_outfill;
 640 #endif
 641         /* Needed because address '0' is special */
 642         if (dev->pa_addr == 0)  {
 643                 dev->pa_addr=ntohl(0xC0A80001);
 644         }
 645         dev->tbusy  = 0;
 646 /*      dev->flags |= IFF_UP; */
 647         dev->start  = 1;
 648 
 649         return 0;
 650 
 651         /* Cleanup */
 652 #ifdef SL_INCLUDE_CSLIP
 653 noslcomp:
 654         kfree(sl->cbuff);
 655 nocbuff:
 656 #endif
 657         kfree(sl->xbuff);
 658 noxbuff:
 659         kfree(sl->rbuff);
 660 norbuff:
 661         return -ENOMEM;
 662 }
 663 
 664 
 665 /* Close the low-level part of the SLIP channel. Easy! */
 666 static int
 667 sl_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 668 {
 669         struct slip *sl = (struct slip*)(dev->priv);
 670 
 671         if (sl->tty == NULL) {
 672                 return -EBUSY;
 673         }
 674         sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 675         dev->tbusy = 1;
 676         dev->start = 0;
 677         
 678 /*      dev->flags &= ~IFF_UP; */
 679 
 680         return 0;
 681 }
 682 
 683 static int
 684 slip_receive_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 685 {
 686         return 65536;  /* We can handle an infinite amount of data. :-) */
 687 }
 688 
 689 /*
 690  * Handle the 'receiver data ready' interrupt.
 691  * This function is called by the 'tty_io' module in the kernel when
 692  * a block of SLIP data has been received, which can now be decapsulated
 693  * and sent on to some IP layer for further processing.
 694  */
 695 static void
 696 slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 697 {
 698         struct slip *sl = (struct slip *) tty->disc_data;
 699 
 700         if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start)
 701                 return;
 702 
 703         /*
 704          * Argh! mtu change time! - costs us the packet part received
 705          * at the change
 706          */
 707 #ifdef CONFIG_AX25
 708         if (sl->mtu != sl->dev->mtu + 73)  {
 709 #else
 710         if (sl->mtu != sl->dev->mtu)  {
 711 #endif
 712                 sl_changedmtu(sl);
 713         }
 714 
 715         /* Read the characters out of the buffer */
 716         while (count--) {
 717                 if (fp && *fp++) {
 718                         if (!set_bit(SLF_ERROR, &sl->flags))  {
 719                                 sl->rx_errors++;
 720                         }
 721                         cp++;
 722                         continue;
 723                 }
 724 #ifdef CONFIG_SLIP_MODE_SLIP6
 725                 if (sl->mode & SL_MODE_SLIP6)
 726                         slip_unesc6(sl, *cp++);
 727                 else
 728 #endif
 729                         slip_unesc(sl, *cp++);
 730         }
 731 }
 732 
 733 /*
 734  * Open the high-level part of the SLIP channel.
 735  * This function is called by the TTY module when the
 736  * SLIP line discipline is called for.  Because we are
 737  * sure the tty line exists, we only have to link it to
 738  * a free SLIP channel...
 739  */
 740 static int
 741 slip_open(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 742 {
 743         struct slip *sl = (struct slip *) tty->disc_data;
 744         int err;
 745 
 746         /* First make sure we're not already connected. */
 747         if (sl && sl->magic == SLIP_MAGIC) {
 748                 return -EEXIST;
 749         }
 750 
 751         /* OK.  Find a free SLIP channel to use. */
 752         if ((sl = sl_alloc()) == NULL) {
 753                 return -ENFILE;
 754         }
 755 
 756         sl->tty = tty;
 757         tty->disc_data = sl;
 758         if (tty->driver.flush_buffer)  {
 759                 tty->driver.flush_buffer(tty);
 760         }
 761         if (tty->ldisc.flush_buffer)  {
 762                 tty->ldisc.flush_buffer(tty);
 763         }
 764 
 765         /* Restore default settings */
 766         sl->mode      = SL_MODE_DEFAULT;
 767         sl->dev->type = ARPHRD_SLIP + sl->mode;
 768 #ifdef CONFIG_AX25      
 769         if (sl->dev->type == 260) {             /* KISS */
 770                 sl->dev->type = ARPHRD_AX25;
 771         }
 772 #endif  
 773         /* Perform the low-level SLIP initialization. */
 774         if ((err = sl_open(sl->dev)))  {
 775                 return err;
 776         }
 777         
 778         MOD_INC_USE_COUNT;
 779 
 780         /* Done.  We have linked the TTY line to a channel. */
 781         return sl->dev->base_addr;
 782 }
 783 
 784 
 785 /*
 786  * Close down a SLIP channel.
 787  * This means flushing out any pending queues, and then restoring the
 788  * TTY line discipline to what it was before it got hooked to SLIP
 789  * (which usually is TTY again).
 790  */
 791 static void
 792 slip_close(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 793 {
 794         struct slip *sl = (struct slip *) tty->disc_data;
 795 
 796         /* First make sure we're connected. */
 797         if (!sl || sl->magic != SLIP_MAGIC) {
 798                 return;
 799         }
 800 
 801         (void) dev_close(sl->dev);
 802         
 803         tty->disc_data = 0;
 804         sl->tty = NULL;
 805         /* VSV = very important to remove timers */
 806 #ifdef CONFIG_SLIP_SMART
 807         if (sl->keepalive)
 808                 (void)del_timer (&sl->keepalive_timer);
 809         if (sl->outfill)
 810                 (void)del_timer (&sl->outfill_timer);
 811 #endif
 812         sl_free(sl);
 813         unregister_netdev(sl->dev);
 814         MOD_DEC_USE_COUNT;
 815 }
 816 
 817 
 818 static struct enet_statistics *
 819 sl_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 820 {
 821         static struct enet_statistics stats;
 822         struct slip *sl = (struct slip*)(dev->priv);
 823 #ifdef SL_INCLUDE_CSLIP
 824         struct slcompress *comp;
 825 #endif
 826 
 827         memset(&stats, 0, sizeof(struct enet_statistics));
 828 
 829         stats.rx_packets     = sl->rx_packets;
 830         stats.tx_packets     = sl->tx_packets;
 831         stats.rx_dropped     = sl->rx_dropped;
 832         stats.tx_dropped     = sl->tx_dropped;
 833         stats.tx_errors      = sl->tx_errors;
 834         stats.rx_errors      = sl->rx_errors;
 835         stats.rx_over_errors = sl->rx_over_errors;
 836 #ifdef SL_INCLUDE_CSLIP
 837         stats.rx_fifo_errors = sl->rx_compressed;
 838         stats.tx_fifo_errors = sl->tx_compressed;
 839         stats.collisions     = sl->tx_misses;
 840         comp = sl->slcomp;
 841         if (comp) {
 842                 stats.rx_fifo_errors += comp->sls_i_compressed;
 843                 stats.rx_dropped     += comp->sls_i_tossed;
 844                 stats.tx_fifo_errors += comp->sls_o_compressed;
 845                 stats.collisions     += comp->sls_o_misses;
 846         }
 847 #endif /* CONFIG_INET */
 848         return (&stats);
 849 }
 850 
 851 
 852  /************************************************************************
 853   *                     STANDARD SLIP ENCAPSULATION                      *
 854   ************************************************************************/
 855 
 856 int
 857 slip_esc(unsigned char *s, unsigned char *d, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 858 {
 859         unsigned char *ptr = d;
 860         unsigned char c;
 861 
 862         /*
 863          * Send an initial END character to flush out any
 864          * data that may have accumulated in the receiver
 865          * due to line noise.
 866          */
 867 
 868         *ptr++ = END;
 869 
 870         /*
 871          * For each byte in the packet, send the appropriate
 872          * character sequence, according to the SLIP protocol.
 873          */
 874 
 875         while (len-- > 0) {
 876                 switch(c = *s++) {
 877                  case END:
 878                         *ptr++ = ESC;
 879                         *ptr++ = ESC_END;
 880                         break;
 881                  case ESC:
 882                         *ptr++ = ESC;
 883                         *ptr++ = ESC_ESC;
 884                         break;
 885                  default:
 886                         *ptr++ = c;
 887                         break;
 888                 }
 889         }
 890         *ptr++ = END;
 891         return (ptr - d);
 892 }
 893 
 894 static void
 895 slip_unesc(struct slip *sl, unsigned char s)
     /* [previous][next][first][last][top][bottom][index][help] */
 896 {
 897 
 898         switch(s) {
 899          case END:
 900                 /* drop keeptest bit = VSV */
 901                 if (test_bit(SLF_KEEPTEST, &sl->flags))
 902                         clear_bit(SLF_KEEPTEST, &sl->flags);
 903 
 904                 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
 905                         sl_bump(sl);
 906                 }
 907                 clear_bit(SLF_ESCAPE, &sl->flags);
 908                 sl->rcount = 0;
 909                 return;
 910 
 911          case ESC:
 912                 set_bit(SLF_ESCAPE, &sl->flags);
 913                 return;
 914          case ESC_ESC:
 915                 if (clear_bit(SLF_ESCAPE, &sl->flags))  {
 916                         s = ESC;
 917                 }
 918                 break;
 919          case ESC_END:
 920                 if (clear_bit(SLF_ESCAPE, &sl->flags))  {
 921                         s = END;
 922                 }
 923                 break;
 924         }
 925         if (!test_bit(SLF_ERROR, &sl->flags))  {
 926                 if (sl->rcount < sl->buffsize)  {
 927                         sl->rbuff[sl->rcount++] = s;
 928                         return;
 929                 }
 930                 sl->rx_over_errors++;
 931                 set_bit(SLF_ERROR, &sl->flags);
 932         }
 933 }
 934 
 935 
 936 #ifdef CONFIG_SLIP_MODE_SLIP6
 937 /************************************************************************
 938  *                       6 BIT SLIP ENCAPSULATION                       *
 939  ************************************************************************/
 940 
 941 int
 942 slip_esc6(unsigned char *s, unsigned char *d, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 943 {
 944         unsigned char *ptr = d;
 945         unsigned char c;
 946         int i;
 947         unsigned short v = 0;
 948         short bits = 0;
 949 
 950         /*
 951          * Send an initial END character to flush out any
 952          * data that may have accumulated in the receiver
 953          * due to line noise.
 954          */
 955 
 956         *ptr++ = 0x70;
 957 
 958         /*
 959          * Encode the packet into printable ascii characters
 960          */
 961 
 962         for (i = 0; i < len; ++i) {
 963                 v = (v << 8) | s[i];
 964                 bits += 8;
 965                 while (bits >= 6) {
 966                         bits -= 6;
 967                         c = 0x30 + ((v >> bits) & 0x3F);
 968                         *ptr++ = c;
 969                 }
 970         }
 971         if (bits) {
 972                 c = 0x30 + ((v << (6 - bits)) & 0x3F);
 973                 *ptr++ = c;
 974         }
 975         *ptr++ = 0x70;
 976         return ptr - d;
 977 }
 978 
 979 void
 980 slip_unesc6(struct slip *sl, unsigned char s)
     /* [previous][next][first][last][top][bottom][index][help] */
 981 {
 982         unsigned char c;
 983 
 984         if (s == 0x70) {
 985                 /* drop keeptest bit = VSV */
 986                 if (test_bit(SLF_KEEPTEST, &sl->flags))
 987                         clear_bit(SLF_KEEPTEST, &sl->flags);
 988 
 989                 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
 990                         sl_bump(sl);
 991                 }
 992                 sl->rcount = 0;
 993                 sl->xbits = 0;
 994                 sl->xdata = 0;
 995         } else if (s >= 0x30 && s < 0x70) {
 996                 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
 997                 sl->xbits += 6;
 998                 if (sl->xbits >= 8) {
 999                         sl->xbits -= 8;
1000                         c = (unsigned char)(sl->xdata >> sl->xbits);
1001                         if (!test_bit(SLF_ERROR, &sl->flags))  {
1002                                 if (sl->rcount < sl->buffsize)  {
1003                                         sl->rbuff[sl->rcount++] = c;
1004                                         return;
1005                                 }
1006                                 sl->rx_over_errors++;
1007                                 set_bit(SLF_ERROR, &sl->flags);
1008                         }
1009                 }
1010         }
1011 }
1012 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1013 
1014 #ifdef CONFIG_AX25
1015 int
1016 sl_set_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1017 {
1018         int err;
1019 
1020         err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN);
1021         if (err)  {
1022                 return err;
1023         }
1024 
1025         memcpy_fromfs(dev->dev_addr, addr, AX25_ADDR_LEN);      /* addr is an AX.25 shifted ASCII mac address */
1026 
1027         return 0;
1028 }
1029 
1030 static int
1031 sl_set_dev_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1032 {
1033         struct sockaddr *sa=addr;
1034         memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
1035         return 0;
1036 }
1037 #endif /* CONFIG_AX25 */
1038 
1039 
1040 /* Perform I/O control on an active SLIP channel. */
1041 static int
1042 slip_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1043 {
1044         struct slip *sl = (struct slip *) tty->disc_data;
1045         int err;
1046         unsigned int tmp;
1047 
1048         /* First make sure we're connected. */
1049         if (!sl || sl->magic != SLIP_MAGIC) {
1050                 return -EINVAL;
1051         }
1052 
1053         switch(cmd) {
1054          case SIOCGIFNAME:
1055                 err = verify_area(VERIFY_WRITE, arg, strlen(sl->dev->name) + 1);
1056                 if (err)  {
1057                         return err;
1058                 }
1059                 memcpy_tofs(arg, sl->dev->name, strlen(sl->dev->name) + 1);
1060                 return 0;
1061 
1062         case SIOCGIFENCAP:
1063                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1064                 if (err)  {
1065                         return err;
1066                 }
1067                 put_user(sl->mode, (int *)arg);
1068                 return 0;
1069 
1070         case SIOCSIFENCAP:
1071                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1072                 if (err)  {
1073                         return err;
1074                 }
1075                 tmp = get_user((int *)arg);
1076 #ifndef SL_INCLUDE_CSLIP
1077                 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))  {
1078                         return -EINVAL;
1079                 }
1080 #else
1081                 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1082                     (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))  {
1083                         /* return -EINVAL; */
1084                         tmp &= ~SL_MODE_ADAPTIVE;
1085                 }
1086 #endif
1087 #ifndef CONFIG_SLIP_MODE_SLIP6
1088                 if (tmp & SL_MODE_SLIP6)  {
1089                         return -EINVAL;
1090                 }
1091 #endif
1092 #ifndef CONFIG_AX25
1093                 if (tmp & SL_MODE_AX25) {
1094                         return -EINVAL;
1095                 }
1096 #else
1097                 if (tmp & SL_MODE_AX25) {
1098                         sl->dev->addr_len=AX25_ADDR_LEN;          /* sizeof an AX.25 addr */
1099                         sl->dev->hard_header_len=AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
1100                 } else  {
1101                         sl->dev->addr_len=0;    /* No mac addr in slip mode */
1102                         sl->dev->hard_header_len=0;
1103                 }
1104 #endif
1105                 sl->mode = tmp;
1106                 sl->dev->type = ARPHRD_SLIP+sl->mode;
1107 #ifdef CONFIG_AX25              
1108                 if (sl->dev->type == 260)  {
1109                         sl->dev->type = ARPHRD_AX25;
1110                 }
1111 #endif          
1112                 return 0;
1113 
1114          case SIOCSIFHWADDR:
1115 #ifdef CONFIG_AX25
1116                 return sl_set_mac_address(sl->dev, arg);
1117 #else
1118                 return -EINVAL;
1119 #endif
1120 
1121 #ifdef CONFIG_SLIP_SMART
1122         /* VSV changes start here */
1123         case SIOCSKEEPALIVE:
1124                 if (sl->keepalive)
1125                         (void)del_timer (&sl->keepalive_timer);
1126                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1127                 if (err)  {
1128                         return -err;
1129                 }
1130                 tmp = get_user((int *)arg);
1131                 if (tmp > 255) /* max for unchar */
1132                         return -EINVAL; 
1133                 if ((sl->keepalive = (unchar) tmp) != 0) {
1134                         sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1135                         add_timer(&sl->keepalive_timer);
1136                         set_bit(SLF_KEEPTEST, &sl->flags);
1137                 }
1138                 return 0;
1139 
1140         case SIOCGKEEPALIVE:
1141                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1142                 if (err)  {
1143                         return -err;
1144                 }
1145                 put_user(sl->keepalive, (int *)arg);
1146                 return 0;
1147 
1148         case SIOCSOUTFILL:
1149                 if (sl->outfill)
1150                          (void)del_timer (&sl->outfill_timer);
1151                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1152                 if (err)  {
1153                         return -err;
1154                 }
1155                 tmp = get_user((int *)arg);
1156                 if (tmp > 255) /* max for unchar */
1157                         return -EINVAL; 
1158                 if ((sl->outfill = (unchar) tmp) != 0){
1159                         sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1160                         add_timer(&sl->outfill_timer);
1161                         set_bit(SLF_OUTWAIT, &sl->flags);
1162                 }
1163                 return 0;
1164 
1165         case SIOCGOUTFILL:
1166                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1167                 if (err)  {
1168                         return -err;
1169                 }
1170                 put_user(sl->outfill, (int *)arg);
1171                 return 0;
1172         /* VSV changes end */
1173 #endif  
1174 
1175         /* Allow stty to read, but not set, the serial port */
1176         case TCGETS:
1177         case TCGETA:
1178                 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1179 
1180         default:
1181                 return -ENOIOCTLCMD;
1182         }
1183 }
1184 
1185 static int sl_open_dev(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1186 {
1187         struct slip *sl = (struct slip*)(dev->priv);
1188         if(sl->tty==NULL)
1189                 return -ENODEV;
1190         return 0;
1191 }
1192 
1193 /* Initialize SLIP control device -- register SLIP line discipline */
1194 #ifdef MODULE
1195 static int slip_init_ctrl_dev(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1196 #else   /* !MODULE */
1197 int slip_init_ctrl_dev(struct device *dummy)
1198 #endif  /* !MODULE */
1199 {
1200         int status;
1201 
1202         if (slip_maxdev < 4) slip_maxdev = 4; /* Sanity */
1203 
1204         printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)"
1205 #ifdef CONFIG_SLIP_MODE_SLIP6
1206                " (6 bit encapsulation enabled)"
1207 #endif
1208                ".\n",
1209                SLIP_VERSION, slip_maxdev );
1210 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1211         printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1212 #endif
1213 #ifdef CONFIG_AX25
1214         printk(KERN_INFO "AX25: KISS encapsulation enabled.\n");
1215 #endif
1216 #ifdef CONFIG_SLIP_SMART
1217         printk(KERN_INFO "SLIP linefill/keepalive option.\n");
1218 #endif  
1219 
1220         slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1221         if (slip_ctrls == NULL) 
1222         {
1223                 printk("SLIP: Can't allocate slip_ctrls[] array!  Uaargh! (-> No SLIP available)\n");
1224                 return -ENOMEM;
1225         }
1226         
1227         /* Clear the pointer array, we allocate devices when we need them */
1228         memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev); /* Pointers */
1229 
1230         /* Fill in our line protocol discipline, and register it */
1231         memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1232         sl_ldisc.magic  = TTY_LDISC_MAGIC;
1233         sl_ldisc.flags  = 0;
1234         sl_ldisc.open   = slip_open;
1235         sl_ldisc.close  = slip_close;
1236         sl_ldisc.read   = NULL;
1237         sl_ldisc.write  = NULL;
1238         sl_ldisc.ioctl  = (int (*)(struct tty_struct *, struct file *,
1239                                    unsigned int, unsigned long)) slip_ioctl;
1240         sl_ldisc.select = NULL;
1241         sl_ldisc.receive_buf = slip_receive_buf;
1242         sl_ldisc.receive_room = slip_receive_room;
1243         sl_ldisc.write_wakeup = slip_write_wakeup;
1244         if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0)  {
1245                 printk("SLIP: can't register line discipline (err = %d)\n", status);
1246         }
1247 
1248 
1249 #ifdef MODULE
1250         return status;
1251 #else
1252         /* Return "not found", so that dev_init() will unlink
1253          * the placeholder device entry for us.
1254          */
1255         return ENODEV;
1256 #endif
1257       }
1258 
1259 
1260 /* Initialize the SLIP driver.  Called by DDI. */
1261 int
1262 slip_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1263 {
1264         struct slip *sl = (struct slip*)(dev->priv);
1265         int i;
1266 #ifdef CONFIG_AX25
1267         static char ax25_bcast[AX25_ADDR_LEN] =
1268                 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
1269         static char ax25_test[AX25_ADDR_LEN] =
1270                 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
1271 #endif
1272 
1273         if (sl == NULL)         /* Allocation failed ?? */
1274           return -ENODEV;
1275 
1276         /* Set up the "SLIP Control Block". (And clear statistics) */
1277         
1278         memset(sl, 0, sizeof (struct slip));
1279         sl->magic  = SLIP_MAGIC;
1280         sl->dev    = dev;
1281         
1282         /* Finish setting up the DEVICE info. */
1283         dev->mtu                = SL_MTU;
1284         dev->hard_start_xmit    = sl_xmit;
1285         dev->open               = sl_open_dev;
1286         dev->stop               = sl_close;
1287         dev->hard_header        = sl_header;
1288         dev->get_stats          = sl_get_stats;
1289 #ifdef HAVE_SET_MAC_ADDR
1290 #ifdef CONFIG_AX25
1291         dev->set_mac_address    = sl_set_dev_mac_address;
1292 #endif
1293 #endif
1294         dev->hard_header_len    = 0;
1295         dev->addr_len           = 0;
1296         dev->type               = ARPHRD_SLIP + SL_MODE_DEFAULT;
1297         dev->tx_queue_len       = 10;
1298 #ifdef CONFIG_AX25
1299         if (sl->dev->type == 260) {
1300                 sl->dev->type = ARPHRD_AX25;
1301         }
1302         memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);      /* Only activated in AX.25 mode */
1303         memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);        /*    ""      ""       ""    "" */
1304 #endif
1305         dev->rebuild_header     = sl_rebuild_header;
1306 
1307         for (i = 0; i < DEV_NUMBUFFS; i++)  {
1308                 skb_queue_head_init(&dev->buffs[i]);
1309         }
1310 
1311         /* New-style flags. */
1312         dev->flags              = 0;
1313         dev->family             = AF_INET;
1314         dev->pa_addr            = 0;
1315         dev->pa_brdaddr         = 0;
1316         dev->pa_mask            = 0;
1317         dev->pa_alen            = 4;
1318 
1319         return 0;
1320 }
1321 #ifdef MODULE
1322 
1323 int
1324 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1325 {
1326         return slip_init_ctrl_dev();
1327 }
1328 
1329 void
1330 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1331 {
1332         int i;
1333 
1334         if (slip_ctrls != NULL) 
1335         {
1336                 for (i = 0; i < slip_maxdev; i++)  
1337                 {
1338                         if (slip_ctrls[i])
1339                         {
1340                                 /*
1341                                  * VSV = if dev->start==0, then device
1342                                  * unregistred while close proc.
1343                                  */ 
1344                                 if (slip_ctrls[i]->dev.start)
1345                                         unregister_netdev(&(slip_ctrls[i]->dev));
1346 
1347                                 kfree(slip_ctrls[i]);
1348                                 slip_ctrls[i] = NULL;
1349                         }
1350                 }
1351                 kfree(slip_ctrls);
1352                 slip_ctrls = NULL;
1353         }
1354         if ((i = tty_register_ldisc(N_SLIP, NULL)))  
1355         {
1356                 printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1357         }
1358 }
1359 #endif /* MODULE */
1360 
1361 #ifdef CONFIG_SLIP_SMART
1362 /*
1363  * This is start of the code for multislip style line checking
1364  * added by Stanislav Voronyi. All changes before marked VSV
1365  */
1366  
1367 static void sl_outfill(unsigned long sls)
     /* [previous][next][first][last][top][bottom][index][help] */
1368 {
1369         struct slip *sl=(struct slip *)sls;
1370 
1371         if(sls==0L)
1372                 return;
1373 
1374         if(sl->outfill)
1375         {
1376                 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1377                 {
1378                         /* no packets was transmited, do outfill */
1379 #ifdef CONFIG_SLIP_MODE_SLIP6
1380                         unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1381 #else
1382                         unsigned char s = END;
1383 #endif
1384                         /* put END into tty queue. Is it right ??? */
1385                         if (!test_bit(0, (void *) &sl->dev->tbusy))
1386                         { 
1387                                 /* if device busy no outfill */
1388                                 sl->tty->driver.write(sl->tty, 0, &s, 1);
1389                         }
1390                 }
1391                 else
1392                         set_bit(SLF_OUTWAIT, &sl->flags);
1393                 (void)del_timer(&sl->outfill_timer);
1394                 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1395                 add_timer(&sl->outfill_timer);
1396         }
1397         else
1398                 del_timer(&sl->outfill_timer);
1399 }
1400 
1401 static void sl_keepalive(unsigned long sls)
     /* [previous][next][first][last][top][bottom][index][help] */
1402 {
1403         struct slip *sl=(struct slip *)sls;
1404 
1405         if(sl == NULL)
1406                 return;
1407 
1408         if( sl->keepalive)
1409         {
1410                 if(test_bit(SLF_KEEPTEST, &sl->flags))
1411                 {
1412                         /* keepalive still high :(, we must hangup */
1413                         (void)del_timer(&sl->keepalive_timer);
1414                         if( sl->outfill ) /* outfill timer must be deleted too */
1415                                 (void)del_timer(&sl->outfill_timer);
1416                         printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1417                         tty_hangup(sl->tty); /* this must hangup tty & close slip */
1418                         /* I think we need not something else */
1419                         return;
1420                 }
1421                 else
1422                         set_bit(SLF_KEEPTEST, &sl->flags);
1423                 (void)del_timer(&sl->keepalive_timer);
1424                  sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1425                 add_timer(&sl->keepalive_timer);
1426         }
1427         else
1428                 (void)del_timer(&sl->keepalive_timer);
1429 }
1430 
1431 #endif

/* [previous][next][first][last][top][bottom][index][help] */