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 "slip.h"
  80 #ifdef CONFIG_INET
  81 #include <linux/ip.h>
  82 #include <linux/tcp.h>
  83 #include "slhc.h"
  84 #endif
  85 
  86 #ifdef MODULE
  87 #define SLIP_VERSION    "0.8.4-NET3.019-NEWTTY-MODULAR"
  88 #else
  89 #define SLIP_VERSION    "0.8.4-NET3.019-NEWTTY"
  90 #endif
  91 
  92 
  93 typedef struct slip_ctrl {
  94         char            if_name[8];     /* "sl0\0" .. "sl99999\0"       */
  95         struct slip     ctrl;           /* SLIP things                  */
  96         struct device   dev;            /* the device                   */
  97 } slip_ctrl_t;
  98 static slip_ctrl_t      **slip_ctrls = NULL;
  99 int slip_maxdev = SL_NRUNIT;            /* Can be overridden with insmod! */
 100 
 101 static struct tty_ldisc sl_ldisc;
 102 
 103 static int slip_esc(unsigned char *p, unsigned char *d, int len);
 104 static void slip_unesc(struct slip *sl, unsigned char c);
 105 #ifdef CONFIG_SLIP_MODE_SLIP6
 106 static int slip_esc6(unsigned char *p, unsigned char *d, int len);
 107 static void slip_unesc6(struct slip *sl, unsigned char c);
 108 #endif
 109 #ifdef CONFIG_SLIP_SMART
 110 static void sl_keepalive(unsigned long sls);
 111 static void sl_outfill(unsigned long sls);
 112 #endif
 113 
 114 /* Find a free SLIP channel, and link in this `tty' line. */
 115 static inline struct slip *
 116 sl_alloc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 117 {
 118         slip_ctrl_t *slp;
 119         int i;
 120 
 121         if (slip_ctrls == NULL) return NULL;    /* Master array missing ! */
 122 
 123         for (i = 0; i < slip_maxdev; i++) {
 124           slp = slip_ctrls[i];
 125           /* Not allocated ? */
 126           if (slp == NULL)
 127             break;
 128           /* Not in use ? */
 129           if (!set_bit(SLF_INUSE, &slp->ctrl.flags))
 130             break;
 131         }
 132         /* SLP is set.. */
 133 
 134         /* Sorry, too many, all slots in use */
 135         if (i >= slip_maxdev) return NULL;
 136 
 137         /* If no channels are available, allocate one */
 138         if (!slp &&
 139             (slip_ctrls[i] = (slip_ctrl_t *)kmalloc(sizeof(slip_ctrl_t),
 140                                                     GFP_KERNEL)) != NULL) {
 141           slp = slip_ctrls[i];
 142           memset(slp, 0, sizeof(slip_ctrl_t));
 143 
 144           /* Initialize channel control data */
 145           set_bit(SLF_INUSE, &slp->ctrl.flags);
 146           slp->ctrl.tty         = NULL;
 147           sprintf(slp->if_name, "sl%d", i);
 148           slp->dev.name         = slp->if_name;
 149           slp->dev.base_addr    = i;
 150           slp->dev.priv         = (void*)&(slp->ctrl);
 151           slp->dev.next         = NULL;
 152           slp->dev.init         = slip_init;
 153 /* printk(KERN_INFO "slip: kmalloc()ed SLIP control node for line %s\n",
 154    slp->if_name); */
 155         }
 156         if (slp != NULL) {
 157 
 158           /* register device so that it can be ifconfig'ed       */
 159           /* slip_init() will be called as a side-effect         */
 160           /* SIDE-EFFECT WARNING: slip_init() CLEARS slp->ctrl ! */
 161 
 162           if (register_netdev(&(slp->dev)) == 0) {
 163             /* (Re-)Set the INUSE bit.   Very Important! */
 164             set_bit(SLF_INUSE, &slp->ctrl.flags);
 165             slp->ctrl.dev = &(slp->dev);
 166             slp->dev.priv = (void*)&(slp->ctrl);
 167 
 168 /* printk(KERN_INFO "slip: linked in netdev %s for active use\n",
 169    slp->if_name); */
 170 
 171             return (&(slp->ctrl));
 172 
 173           } else {
 174             clear_bit(SLF_INUSE,&(slp->ctrl.flags));
 175             printk("sl_alloc() - register_netdev() failure.\n");
 176           }
 177         }
 178 
 179         return NULL;
 180 }
 181 
 182 
 183 /* Free a SLIP channel. */
 184 static inline void
 185 sl_free(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 186 {
 187         /* Free all SLIP frame buffers. */
 188         if (sl->rbuff)  {
 189                 kfree(sl->rbuff);
 190         }
 191         sl->rbuff = NULL;
 192         if (sl->xbuff)  {
 193                 kfree(sl->xbuff);
 194         }
 195         sl->xbuff = NULL;
 196 #ifdef SL_INCLUDE_CSLIP
 197         /* Save CSLIP statistics */
 198         if (sl->slcomp)  {
 199                 sl->rx_compressed += sl->slcomp->sls_i_compressed;
 200                 sl->rx_dropped    += sl->slcomp->sls_i_tossed;
 201                 sl->tx_compressed += sl->slcomp->sls_o_compressed;
 202                 sl->tx_misses     += sl->slcomp->sls_o_misses;
 203         }
 204         if (sl->cbuff)  {
 205                 kfree(sl->cbuff);
 206         }
 207         sl->cbuff = NULL;
 208         if(sl->slcomp)
 209                 slhc_free(sl->slcomp);
 210         sl->slcomp = NULL;
 211 #endif
 212 
 213         if (!clear_bit(SLF_INUSE, &sl->flags)) {
 214                 printk("%s: sl_free for already free unit.\n", sl->dev->name);
 215         }
 216 }
 217 
 218 /* MTU has been changed by the IP layer. Unfortunately we are not told
 219    about this, but we spot it ourselves and fix things up. We could be
 220    in an upcall from the tty driver, or in an ip packet queue. */
 221 
 222 static void sl_changedmtu(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 223 {
 224         struct device *dev = sl->dev;
 225         unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
 226 #ifdef SL_INCLUDE_CSLIP
 227         unsigned char *cbuff, *ocbuff;
 228 #endif
 229         int len;
 230         unsigned long flags;
 231 
 232         len = dev->mtu * 2;
 233 /*
 234  * allow for arrival of larger UDP packets, even if we say not to
 235  * also fixes a bug in which SunOS sends 512-byte packets even with
 236  * an MSS of 128
 237  */
 238         if (len < 576 * 2)  {
 239                 len = 576 * 2;
 240         }
 241 
 242         xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 243         rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 244 #ifdef SL_INCLUDE_CSLIP
 245         cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC);
 246 #endif
 247 
 248 #ifdef SL_INCLUDE_CSLIP
 249         if (xbuff == NULL || rbuff == NULL || cbuff == NULL)  {
 250 #else
 251         if (xbuff == NULL || rbuff == NULL)  {
 252 #endif
 253                 printk("%s: unable to grow slip buffers, MTU change cancelled.\n",
 254                        sl->dev->name);
 255                 dev->mtu = sl->mtu;
 256                 if (xbuff != NULL)  {
 257                         kfree(xbuff);
 258                 }
 259                 if (rbuff != NULL)  {
 260                         kfree(rbuff);
 261                 }
 262 #ifdef SL_INCLUDE_CSLIP
 263                 if (cbuff != NULL)  {
 264                         kfree(cbuff);
 265                 }
 266 #endif
 267                 return;
 268         }
 269 
 270         save_flags(flags); cli();
 271 
 272         oxbuff    = sl->xbuff;
 273         sl->xbuff = xbuff;
 274         orbuff    = sl->rbuff;
 275         sl->rbuff = rbuff;
 276 #ifdef SL_INCLUDE_CSLIP
 277         ocbuff    = sl->cbuff;
 278         sl->cbuff = cbuff;
 279 #endif
 280         if (sl->xleft)  {
 281                 if (sl->xleft <= len)  {
 282                         memcpy(sl->xbuff, sl->xhead, sl->xleft);
 283                 } else  {
 284                         sl->xleft = 0;
 285                         sl->tx_dropped++;
 286                 }
 287         }
 288         sl->xhead = sl->xbuff;
 289 
 290         if (sl->rcount)  {
 291                 if (sl->rcount <= len) {
 292                         memcpy(sl->rbuff, orbuff, sl->rcount);
 293                 } else  {
 294                         sl->rcount = 0;
 295                         sl->rx_over_errors++;
 296                         set_bit(SLF_ERROR, &sl->flags);
 297                 }
 298         }
 299 #ifdef CONFIG_AX25
 300         sl->mtu      = dev->mtu + 73;
 301 #else
 302         sl->mtu      = dev->mtu;
 303 #endif
 304         sl->buffsize = len;
 305 
 306         restore_flags(flags);
 307 
 308         if (oxbuff != NULL)   {
 309                 kfree(oxbuff);
 310         }
 311         if (orbuff != NULL)    {
 312                 kfree(orbuff);
 313         }
 314 #ifdef SL_INCLUDE_CSLIP
 315         if (ocbuff != NULL)  {
 316                 kfree(ocbuff);
 317         }
 318 #endif
 319 }
 320 
 321 
 322 /* Set the "sending" flag.  This must be atomic, hence the ASM. */
 323 static inline void
 324 sl_lock(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 325 {
 326         if (set_bit(0, (void *) &sl->dev->tbusy))  {
 327                 printk("%s: trying to lock already locked device!\n", sl->dev->name);
 328         }
 329 }
 330 
 331 
 332 /* Clear the "sending" flag.  This must be atomic, hence the ASM. */
 333 static inline void
 334 sl_unlock(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 335 {
 336         if (!clear_bit(0, (void *)&sl->dev->tbusy))  {
 337                 printk("%s: trying to unlock already unlocked device!\n", sl->dev->name);
 338         }
 339 }
 340 
 341 /* Send one completely decapsulated IP datagram to the IP layer. */
 342 static void
 343 sl_bump(struct slip *sl)
     /* [previous][next][first][last][top][bottom][index][help] */
 344 {
 345         struct sk_buff *skb;
 346         int count;
 347 
 348         count = sl->rcount;
 349 #ifdef SL_INCLUDE_CSLIP
 350         if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) {
 351                 unsigned char c;
 352                 if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) {
 353                         /* ignore compressed packets when CSLIP is off */
 354                         if (!(sl->mode & SL_MODE_CSLIP)) {
 355                                 printk("%s: compressed packet ignored\n", sl->dev->name);
 356                                 return;
 357                         }
 358                         /* make sure we've reserved enough space for uncompress to use */
 359                         if (count + 80 > sl->buffsize) {
 360                                 sl->rx_over_errors++;
 361                                 return;
 362                         }
 363                         count = slhc_uncompress(sl->slcomp, sl->rbuff, count);
 364                         if (count <= 0) {
 365                                 return;
 366                         }
 367                 } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) {
 368                         if (!(sl->mode & SL_MODE_CSLIP)) {
 369                                 /* turn on header compression */
 370                                 sl->mode |= SL_MODE_CSLIP;
 371                                 sl->mode &= ~SL_MODE_ADAPTIVE;
 372                                 printk("%s: header compression turned on\n", sl->dev->name);
 373                         }
 374                         sl->rbuff[0] &= 0x4f;
 375                         if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) {
 376                                 return;
 377                         }
 378                 }
 379         }
 380 #endif  /* SL_INCLUDE_CSLIP */
 381 
 382         skb = dev_alloc_skb(count);
 383         if (skb == NULL)  {
 384                 printk("%s: memory squeeze, dropping packet.\n", sl->dev->name);
 385                 sl->rx_dropped++;
 386                 return;
 387         }
 388         skb->dev = sl->dev;
 389         memcpy(skb_put(skb,count), sl->rbuff, count);
 390         skb->mac.raw=skb->data;
 391         if(sl->mode & SL_MODE_AX25)
 392                 skb->protocol=htons(ETH_P_AX25);
 393         else
 394                 skb->protocol=htons(ETH_P_IP);
 395         netif_rx(skb);
 396         sl->rx_packets++;
 397 }
 398 
 399 /* Encapsulate one IP datagram and stuff into a TTY queue. */
 400 static void
 401 sl_encaps(struct slip *sl, unsigned char *icp, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 402 {
 403         unsigned char *p;
 404         int actual, count;
 405 
 406 
 407 #ifdef CONFIG_AX25
 408         if (sl->mtu != sl->dev->mtu + 73) {     /* Someone has been ifconfigging */
 409 #else
 410         if (sl->mtu != sl->dev->mtu) {  /* Someone has been ifconfigging */
 411 #endif
 412                 sl_changedmtu(sl);
 413         }
 414 
 415         if (len > sl->mtu) {            /* Sigh, shouldn't occur BUT ... */
 416                 len = sl->mtu;
 417                 printk ("%s: truncating oversized transmit packet!\n", sl->dev->name);
 418                 sl->tx_dropped++;
 419                 sl_unlock(sl);
 420                 return;
 421         }
 422 
 423         p = icp;
 424 #ifdef SL_INCLUDE_CSLIP
 425         if (sl->mode & SL_MODE_CSLIP)  {
 426                 len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1);
 427         }
 428 #endif
 429 #ifdef CONFIG_SLIP_MODE_SLIP6
 430         if(sl->mode & SL_MODE_SLIP6)
 431                 count = slip_esc6(p, (unsigned char *) sl->xbuff, len);
 432         else
 433 #endif
 434                 count = slip_esc(p, (unsigned char *) sl->xbuff, len);
 435 
 436         /* Order of next two lines is *very* important.
 437          * When we are sending a little amount of data,
 438          * the transfer may be completed inside driver.write()
 439          * routine, because it's running with interrupts enabled.
 440          * In this case we *never* got WRITE_WAKEUP event,
 441          * if we did not request it before write operation.
 442          *       14 Oct 1994  Dmitry Gorodchanin.
 443          */
 444         sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
 445         actual = sl->tty->driver.write(sl->tty, 0, sl->xbuff, count);
 446 #ifdef SL_CHECK_TRANSMIT
 447         sl->dev->trans_start = jiffies;
 448 #endif
 449         sl->xleft = count - actual;
 450         sl->xhead = sl->xbuff + actual;
 451         /* VSV */
 452         clear_bit(SLF_OUTWAIT, &sl->flags);     /* reset outfill flag */
 453 }
 454 
 455 /*
 456  * Called by the driver when there's room for more data.  If we have
 457  * more packets to send, we send them here.
 458  */
 459 static void slip_write_wakeup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 460 {
 461         int actual;
 462         struct slip *sl = (struct slip *) tty->disc_data;
 463 
 464         /* First make sure we're connected. */
 465         if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start) {
 466                 return;
 467         }
 468         if (sl->xleft <= 0)  {
 469                 /* Now serial buffer is almost free & we can start
 470                  * transmission of another packet */
 471                 sl->tx_packets++;
 472                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 473                 if (test_bit(0, (void *) &sl->dev->tbusy)) /* add by VSV */
 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         sl_free(sl);
 806         unregister_netdev(sl->dev);
 807         MOD_DEC_USE_COUNT;
 808 }
 809 
 810 
 811 static struct enet_statistics *
 812 sl_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 813 {
 814         static struct enet_statistics stats;
 815         struct slip *sl = (struct slip*)(dev->priv);
 816 #ifdef SL_INCLUDE_CSLIP
 817         struct slcompress *comp;
 818 #endif
 819 
 820         memset(&stats, 0, sizeof(struct enet_statistics));
 821 
 822         stats.rx_packets     = sl->rx_packets;
 823         stats.tx_packets     = sl->tx_packets;
 824         stats.rx_dropped     = sl->rx_dropped;
 825         stats.tx_dropped     = sl->tx_dropped;
 826         stats.tx_errors      = sl->tx_errors;
 827         stats.rx_errors      = sl->rx_errors;
 828         stats.rx_over_errors = sl->rx_over_errors;
 829 #ifdef SL_INCLUDE_CSLIP
 830         stats.rx_fifo_errors = sl->rx_compressed;
 831         stats.tx_fifo_errors = sl->tx_compressed;
 832         stats.collisions     = sl->tx_misses;
 833         comp = sl->slcomp;
 834         if (comp) {
 835                 stats.rx_fifo_errors += comp->sls_i_compressed;
 836                 stats.rx_dropped     += comp->sls_i_tossed;
 837                 stats.tx_fifo_errors += comp->sls_o_compressed;
 838                 stats.collisions     += comp->sls_o_misses;
 839         }
 840 #endif /* CONFIG_INET */
 841         return (&stats);
 842 }
 843 
 844 
 845  /************************************************************************
 846   *                     STANDARD SLIP ENCAPSULATION                      *
 847   ************************************************************************/
 848 
 849 int
 850 slip_esc(unsigned char *s, unsigned char *d, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 851 {
 852         unsigned char *ptr = d;
 853         unsigned char c;
 854 
 855         /*
 856          * Send an initial END character to flush out any
 857          * data that may have accumulated in the receiver
 858          * due to line noise.
 859          */
 860 
 861         *ptr++ = END;
 862 
 863         /*
 864          * For each byte in the packet, send the appropriate
 865          * character sequence, according to the SLIP protocol.
 866          */
 867 
 868         while (len-- > 0) {
 869                 switch(c = *s++) {
 870                  case END:
 871                         *ptr++ = ESC;
 872                         *ptr++ = ESC_END;
 873                         break;
 874                  case ESC:
 875                         *ptr++ = ESC;
 876                         *ptr++ = ESC_ESC;
 877                         break;
 878                  default:
 879                         *ptr++ = c;
 880                         break;
 881                 }
 882         }
 883         *ptr++ = END;
 884         return (ptr - d);
 885 }
 886 
 887 static void
 888 slip_unesc(struct slip *sl, unsigned char s)
     /* [previous][next][first][last][top][bottom][index][help] */
 889 {
 890 
 891         switch(s) {
 892          case END:
 893                 /* drop keeptest bit = VSV */
 894                 if (test_bit(SLF_KEEPTEST, &sl->flags))
 895                         clear_bit(SLF_KEEPTEST, &sl->flags);
 896 
 897                 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
 898                         sl_bump(sl);
 899                 }
 900                 clear_bit(SLF_ESCAPE, &sl->flags);
 901                 sl->rcount = 0;
 902                 return;
 903 
 904          case ESC:
 905                 set_bit(SLF_ESCAPE, &sl->flags);
 906                 return;
 907          case ESC_ESC:
 908                 if (clear_bit(SLF_ESCAPE, &sl->flags))  {
 909                         s = ESC;
 910                 }
 911                 break;
 912          case ESC_END:
 913                 if (clear_bit(SLF_ESCAPE, &sl->flags))  {
 914                         s = END;
 915                 }
 916                 break;
 917         }
 918         if (!test_bit(SLF_ERROR, &sl->flags))  {
 919                 if (sl->rcount < sl->buffsize)  {
 920                         sl->rbuff[sl->rcount++] = s;
 921                         return;
 922                 }
 923                 sl->rx_over_errors++;
 924                 set_bit(SLF_ERROR, &sl->flags);
 925         }
 926 }
 927 
 928 
 929 #ifdef CONFIG_SLIP_MODE_SLIP6
 930 /************************************************************************
 931  *                       6 BIT SLIP ENCAPSULATION                       *
 932  ************************************************************************/
 933 
 934 int
 935 slip_esc6(unsigned char *s, unsigned char *d, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 936 {
 937         unsigned char *ptr = d;
 938         unsigned char c;
 939         int i;
 940         unsigned short v = 0;
 941         short bits = 0;
 942 
 943         /*
 944          * Send an initial END character to flush out any
 945          * data that may have accumulated in the receiver
 946          * due to line noise.
 947          */
 948 
 949         *ptr++ = 0x70;
 950 
 951         /*
 952          * Encode the packet into printable ascii characters
 953          */
 954 
 955         for (i = 0; i < len; ++i) {
 956                 v = (v << 8) | s[i];
 957                 bits += 8;
 958                 while (bits >= 6) {
 959                         bits -= 6;
 960                         c = 0x30 + ((v >> bits) & 0x3F);
 961                         *ptr++ = c;
 962                 }
 963         }
 964         if (bits) {
 965                 c = 0x30 + ((v << (6 - bits)) & 0x3F);
 966                 *ptr++ = c;
 967         }
 968         *ptr++ = 0x70;
 969         return ptr - d;
 970 }
 971 
 972 void
 973 slip_unesc6(struct slip *sl, unsigned char s)
     /* [previous][next][first][last][top][bottom][index][help] */
 974 {
 975         unsigned char c;
 976 
 977         if (s == 0x70) {
 978                 /* drop keeptest bit = VSV */
 979                 if (test_bit(SLF_KEEPTEST, &sl->flags))
 980                         clear_bit(SLF_KEEPTEST, &sl->flags);
 981 
 982                 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
 983                         sl_bump(sl);
 984                 }
 985                 sl->rcount = 0;
 986                 sl->xbits = 0;
 987                 sl->xdata = 0;
 988         } else if (s >= 0x30 && s < 0x70) {
 989                 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
 990                 sl->xbits += 6;
 991                 if (sl->xbits >= 8) {
 992                         sl->xbits -= 8;
 993                         c = (unsigned char)(sl->xdata >> sl->xbits);
 994                         if (!test_bit(SLF_ERROR, &sl->flags))  {
 995                                 if (sl->rcount < sl->buffsize)  {
 996                                         sl->rbuff[sl->rcount++] = c;
 997                                         return;
 998                                 }
 999                                 sl->rx_over_errors++;
1000                                 set_bit(SLF_ERROR, &sl->flags);
1001                         }
1002                 }
1003         }
1004 }
1005 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1006 
1007 #ifdef CONFIG_AX25
1008 int
1009 sl_set_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1010 {
1011         int err;
1012 
1013         err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN);
1014         if (err)  {
1015                 return err;
1016         }
1017 
1018         memcpy_fromfs(dev->dev_addr, addr, AX25_ADDR_LEN);      /* addr is an AX.25 shifted ASCII mac address */
1019 
1020         return 0;
1021 }
1022 
1023 static int
1024 sl_set_dev_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1025 {
1026         struct sockaddr *sa=addr;
1027         memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
1028         return 0;
1029 }
1030 #endif /* CONFIG_AX25 */
1031 
1032 
1033 /* Perform I/O control on an active SLIP channel. */
1034 static int
1035 slip_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1036 {
1037         struct slip *sl = (struct slip *) tty->disc_data;
1038         int err;
1039         unsigned int tmp;
1040 
1041         /* First make sure we're connected. */
1042         if (!sl || sl->magic != SLIP_MAGIC) {
1043                 return -EINVAL;
1044         }
1045 
1046         switch(cmd) {
1047          case SIOCGIFNAME:
1048                 err = verify_area(VERIFY_WRITE, arg, strlen(sl->dev->name) + 1);
1049                 if (err)  {
1050                         return err;
1051                 }
1052                 memcpy_tofs(arg, sl->dev->name, strlen(sl->dev->name) + 1);
1053                 return 0;
1054 
1055         case SIOCGIFENCAP:
1056                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1057                 if (err)  {
1058                         return err;
1059                 }
1060                 put_user(sl->mode, (int *)arg);
1061                 return 0;
1062 
1063         case SIOCSIFENCAP:
1064                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1065                 if (err)  {
1066                         return err;
1067                 }
1068                 tmp = get_user((int *)arg);
1069 #ifndef SL_INCLUDE_CSLIP
1070                 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))  {
1071                         return -EINVAL;
1072                 }
1073 #else
1074                 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1075                     (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))  {
1076                         /* return -EINVAL; */
1077                         tmp &= ~SL_MODE_ADAPTIVE;
1078                 }
1079 #endif
1080 #ifndef CONFIG_SLIP_MODE_SLIP6
1081                 if (tmp & SL_MODE_SLIP6)  {
1082                         return -EINVAL;
1083                 }
1084 #endif
1085 #ifndef CONFIG_AX25
1086                 if (tmp & SL_MODE_AX25) {
1087                         return -EINVAL;
1088                 }
1089 #else
1090                 if (tmp & SL_MODE_AX25) {
1091                         sl->dev->addr_len=AX25_ADDR_LEN;          /* sizeof an AX.25 addr */
1092                         sl->dev->hard_header_len=AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
1093                 } else  {
1094                         sl->dev->addr_len=0;    /* No mac addr in slip mode */
1095                         sl->dev->hard_header_len=0;
1096                 }
1097 #endif
1098                 sl->mode = tmp;
1099                 sl->dev->type = ARPHRD_SLIP+sl->mode;
1100 #ifdef CONFIG_AX25              
1101                 if (sl->dev->type == 260)  {
1102                         sl->dev->type = ARPHRD_AX25;
1103                 }
1104 #endif          
1105                 return 0;
1106 
1107          case SIOCSIFHWADDR:
1108 #ifdef CONFIG_AX25
1109                 return sl_set_mac_address(sl->dev, arg);
1110 #else
1111                 return -EINVAL;
1112 #endif
1113 
1114 #ifdef CONFIG_SLIP_SMART
1115         /* VSV changes start here */
1116         case SIOCSKEEPALIVE:
1117                 if (sl->keepalive)
1118                         del_timer (&sl->keepalive_timer);
1119                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1120                 if (err)  {
1121                         return -err;
1122                 }
1123                 tmp = get_user((int *)arg);
1124                 if (tmp > 255) /* max for unchar */
1125                         return -EINVAL; 
1126                 if ((sl->keepalive = (unchar) tmp) != 0) {
1127                         sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1128                         add_timer(&sl->keepalive_timer);
1129                         set_bit(SLF_KEEPTEST, &sl->flags);
1130                 }
1131                 return 0;
1132 
1133         case SIOCGKEEPALIVE:
1134                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1135                 if (err)  {
1136                         return -err;
1137                 }
1138                 put_user(sl->keepalive, (int *)arg);
1139                 return 0;
1140 
1141         case SIOCSOUTFILL:
1142                 if (sl->outfill)
1143                          (void)del_timer (&sl->outfill_timer);
1144                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1145                 if (err)  {
1146                         return -err;
1147                 }
1148                 tmp = get_user((int *)arg);
1149                 if (tmp > 255) /* max for unchar */
1150                         return -EINVAL; 
1151                 if ((sl->outfill = (unchar) tmp) != 0){
1152                         sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1153                         add_timer(&sl->outfill_timer);
1154                         set_bit(SLF_OUTWAIT, &sl->flags);
1155                 }
1156                 return 0;
1157 
1158         case SIOCGOUTFILL:
1159                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1160                 if (err)  {
1161                         return -err;
1162                 }
1163                 put_user(sl->outfill, (int *)arg);
1164                 return 0;
1165         /* VSV changes end */
1166 #endif  
1167 
1168         /* Allow stty to read, but not set, the serial port */
1169         case TCGETS:
1170         case TCGETA:
1171                 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1172 
1173         default:
1174                 return -ENOIOCTLCMD;
1175         }
1176 }
1177 
1178 static int sl_open_dev(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1179 {
1180         struct slip *sl = (struct slip*)(dev->priv);
1181         if(sl->tty==NULL)
1182                 return -ENODEV;
1183         return 0;
1184 }
1185 
1186 /* Initialize SLIP control device -- register SLIP line discipline */
1187 #ifdef MODULE
1188 static int slip_init_ctrl_dev(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1189 #else   /* !MODULE */
1190 int slip_init_ctrl_dev(struct device *dummy)
1191 #endif  /* !MODULE */
1192 {
1193         int status;
1194 
1195         if (slip_maxdev < 4) slip_maxdev = 4; /* Sanity */
1196 
1197         printk("SLIP: version %s (dynamic channels, max=%d)"
1198 #ifdef CONFIG_SLIP_MODE_SLIP6
1199                " (6 bit encapsulation enabled)"
1200 #endif
1201                ".\n",
1202                SLIP_VERSION, slip_maxdev );
1203 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1204         printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1205 #endif
1206 #ifdef CONFIG_AX25
1207         printk("AX25: KISS encapsulation enabled.\n");
1208 #endif
1209 #ifdef CONFIG_SLIP_SMART
1210         printk("SLIP linefill/keepalive option.\n");
1211 #endif  
1212 
1213         slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1214         if (slip_ctrls == NULL) 
1215         {
1216                 printk("SLIP: Can't allocate slip_ctrls[] array!  Uaargh! (-> No SLIP available)\n");
1217                 return -ENOMEM;
1218         }
1219         
1220         /* Clear the pointer array, we allocate devices when we need them */
1221         memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev); /* Pointers */
1222 
1223         /* Fill in our line protocol discipline, and register it */
1224         memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1225         sl_ldisc.magic  = TTY_LDISC_MAGIC;
1226         sl_ldisc.flags  = 0;
1227         sl_ldisc.open   = slip_open;
1228         sl_ldisc.close  = slip_close;
1229         sl_ldisc.read   = NULL;
1230         sl_ldisc.write  = NULL;
1231         sl_ldisc.ioctl  = (int (*)(struct tty_struct *, struct file *,
1232                                    unsigned int, unsigned long)) slip_ioctl;
1233         sl_ldisc.select = NULL;
1234         sl_ldisc.receive_buf = slip_receive_buf;
1235         sl_ldisc.receive_room = slip_receive_room;
1236         sl_ldisc.write_wakeup = slip_write_wakeup;
1237         if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0)  {
1238                 printk("SLIP: can't register line discipline (err = %d)\n", status);
1239         }
1240 
1241 
1242 #ifdef MODULE
1243         return status;
1244 #else
1245         /* Return "not found", so that dev_init() will unlink
1246          * the placeholder device entry for us.
1247          */
1248         return ENODEV;
1249 #endif
1250       }
1251 
1252 
1253 /* Initialize the SLIP driver.  Called by DDI. */
1254 int
1255 slip_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1256 {
1257         struct slip *sl = (struct slip*)(dev->priv);
1258         int i;
1259 #ifdef CONFIG_AX25
1260         static char ax25_bcast[AX25_ADDR_LEN] =
1261                 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
1262         static char ax25_test[AX25_ADDR_LEN] =
1263                 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
1264 #endif
1265 
1266         if (sl == NULL)         /* Allocation failed ?? */
1267           return -ENODEV;
1268 
1269         /* Set up the "SLIP Control Block". (And clear statistics) */
1270         
1271         memset(sl, 0, sizeof (struct slip));
1272         sl->magic  = SLIP_MAGIC;
1273         sl->dev    = dev;
1274         
1275         /* Finish setting up the DEVICE info. */
1276         dev->mtu                = SL_MTU;
1277         dev->hard_start_xmit    = sl_xmit;
1278         dev->open               = sl_open_dev;
1279         dev->stop               = sl_close;
1280         dev->hard_header        = sl_header;
1281         dev->get_stats          = sl_get_stats;
1282 #ifdef HAVE_SET_MAC_ADDR
1283 #ifdef CONFIG_AX25
1284         dev->set_mac_address    = sl_set_dev_mac_address;
1285 #endif
1286 #endif
1287         dev->hard_header_len    = 0;
1288         dev->addr_len           = 0;
1289         dev->type               = ARPHRD_SLIP + SL_MODE_DEFAULT;
1290         dev->tx_queue_len       = 10;
1291 #ifdef CONFIG_AX25
1292         if (sl->dev->type == 260) {
1293                 sl->dev->type = ARPHRD_AX25;
1294         }
1295         memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);      /* Only activated in AX.25 mode */
1296         memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);        /*    ""      ""       ""    "" */
1297 #endif
1298         dev->rebuild_header     = sl_rebuild_header;
1299 
1300         for (i = 0; i < DEV_NUMBUFFS; i++)  {
1301                 skb_queue_head_init(&dev->buffs[i]);
1302         }
1303 
1304         /* New-style flags. */
1305         dev->flags              = 0;
1306         dev->family             = AF_INET;
1307         dev->pa_addr            = 0;
1308         dev->pa_brdaddr         = 0;
1309         dev->pa_mask            = 0;
1310         dev->pa_alen            = 4;
1311 
1312         return 0;
1313 }
1314 #ifdef MODULE
1315 
1316 int
1317 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1318 {
1319         return slip_init_ctrl_dev();
1320 }
1321 
1322 void
1323 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1324 {
1325         int i;
1326 
1327         if (slip_ctrls != NULL) 
1328         {
1329                 for (i = 0; i < slip_maxdev; i++)  
1330                 {
1331                         if (slip_ctrls[i] != NULL) 
1332                         {
1333                                 unregister_netdev(&(slip_ctrls[i]->dev));
1334                                 kfree(slip_ctrls[i]);
1335                         }
1336                 }
1337                 kfree(slip_ctrls);
1338                 slip_ctrls = NULL;
1339         }
1340         if ((i = tty_register_ldisc(N_SLIP, NULL)))  
1341         {
1342                 printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1343         }
1344 }
1345 #endif /* MODULE */
1346 
1347 #ifdef CONFIG_SLIP_SMART
1348 /*
1349  * This is start of the code for multislip style line checking
1350  * added by Stanislav Voronyi. All changes before marked VSV
1351  */
1352  
1353 static void sl_outfill(unsigned long sls)
     /* [previous][next][first][last][top][bottom][index][help] */
1354 {
1355         struct slip *sl=(struct slip *)sls;
1356 
1357         if(sls==NULL)
1358                 return;
1359 
1360         if(sl->outfill)
1361         {
1362                 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1363                 {
1364                         /* no packets was transmited, do outfill */
1365 #ifdef CONFIG_SLIP_MODE_SLIP6
1366                         unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1367 #else
1368                         unsigned char s = END;
1369 #endif
1370                         /* put END into tty queue. Is it right ??? */
1371                         if (!test_bit(0, (void *) &sl->dev->tbusy))
1372                         { 
1373                                 /* if device busy no outfill */
1374                                 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1375                                 sl->tty->driver.write(sl->tty, 0, &s, 1);
1376                         }
1377                 }
1378                 else
1379                         set_bit(SLF_OUTWAIT, &sl->flags);
1380                 (void)del_timer(&sl->outfill_timer);
1381                 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1382                 add_timer(&sl->outfill_timer);
1383         }
1384         else
1385                 del_timer(&sl->outfill_timer);
1386 }
1387 
1388 static void sl_keepalive(unsigned long sls)
     /* [previous][next][first][last][top][bottom][index][help] */
1389 {
1390         struct slip *sl=(struct slip *)sls;
1391 
1392         if(sls==NULL)
1393                 return;
1394 
1395         if( sl->keepalive)
1396         {
1397                 if(test_bit(SLF_KEEPTEST, &sl->flags))
1398                 {
1399                         /* keepalive still high :(, we must hangup */
1400                         (void)del_timer(&sl->keepalive_timer);
1401                         if( sl->outfill ) /* outfill timer must be deleted too */
1402                                 (void)del_timer(&sl->outfill_timer);
1403                         printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1404                         tty_hangup(sl->tty); /* this must hangup tty & close slip */
1405                         /* I think we need not something else */
1406                         return;
1407                 }
1408                 else
1409                         set_bit(SLF_KEEPTEST, &sl->flags);
1410                 (void)del_timer(&sl->keepalive_timer);
1411                  sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1412                 add_timer(&sl->keepalive_timer);
1413         }
1414         else
1415                 (void)del_timer(&sl->keepalive_timer);
1416 }
1417 
1418 #endif

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