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                 if (test_bit(0, (void *) &sl->dev->tbusy)) /* add by VSV */
 475                         sl_unlock(sl);
 476                 mark_bh(NET_BH);
 477                 return;
 478         }
 479 
 480         actual = tty->driver.write(tty, 0, sl->xhead, sl->xleft);
 481         sl->xleft -= actual;
 482         sl->xhead += actual;
 483 }
 484 
 485 /* Encapsulate an IP datagram and kick it into a TTY queue. */
 486 static int
 487 sl_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 488 {
 489         struct slip *sl = (struct slip*)(dev->priv);
 490 
 491         if (!dev->start)  {
 492                 printk("%s: xmit call when iface is down\n", dev->name);
 493                 return 1;
 494         }
 495         /*
 496          * If we are busy already- too bad.  We ought to be able
 497          * to queue things at this point, to allow for a little
 498          * frame buffer.  Oh well...
 499          * -----------------------------------------------------
 500          * I hate queues in SLIP driver. May be it's efficient,
 501          * but for me latency is more important. ;)
 502          * So, no queues !
 503          *        14 Oct 1994  Dmitry Gorodchanin.
 504          */
 505         if (dev->tbusy) {
 506                 /* May be we must check transmitter timeout here ?
 507                  *      14 Oct 1994 Dmitry Gorodchanin.
 508                  */
 509 #ifdef SL_CHECK_TRANSMIT
 510                 if (jiffies - dev->trans_start  < 20 * HZ)  {
 511                         /* 20 sec timeout not reached */
 512                         return 1;
 513                 }
 514                 printk("%s: transmit timed out, %s?\n", dev->name,
 515                        (sl->tty->driver.chars_in_buffer(sl->tty) || sl->xleft) ?
 516                        "bad line quality" : "driver error");
 517                 sl->xleft = 0;
 518                 sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 519                 sl_unlock(sl);
 520 #else
 521                 return 1;
 522 #endif
 523         }
 524 
 525         /* We were not busy, so we are now... :-) */
 526         if (skb != NULL) {
 527                 sl_lock(sl);
 528                 sl_encaps(sl, skb->data, skb->len);
 529                 dev_kfree_skb(skb, FREE_WRITE);
 530         }
 531         return 0;
 532 }
 533 
 534 
 535 /* Return the frame type ID.  This is normally IP but maybe be AX.25. */
 536 
 537 /* Fill in the MAC-level header. Not used by SLIP. */
 538 static int
 539 sl_header(struct sk_buff *skb, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
 540           void *daddr, void *saddr, unsigned len)
 541 {
 542 #ifdef CONFIG_AX25
 543 #ifdef CONFIG_INET
 544         struct slip *sl = (struct slip*)(dev->priv);
 545 
 546         if (sl->mode & SL_MODE_AX25 && type != htons(ETH_P_AX25))  {
 547                 return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
 548         }
 549 #endif
 550 #endif
 551         return 0;
 552 }
 553 
 554 
 555 /* Rebuild the MAC-level header.  Not used by SLIP. */
 556 static int
 557 sl_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
     /* [previous][next][first][last][top][bottom][index][help] */
 558                   struct sk_buff *skb)
 559 {
 560 #ifdef CONFIG_AX25
 561 #ifdef CONFIG_INET
 562         struct slip *sl = (struct slip*)(dev->priv);
 563 
 564         if (sl->mode & SL_MODE_AX25) {
 565                 return ax25_rebuild_header(buff, dev, raddr, skb);
 566         }
 567 #endif
 568 #endif
 569         return 0;
 570 }
 571 
 572 
 573 /* Open the low-level part of the SLIP channel. Easy! */
 574 static int
 575 sl_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 576 {
 577         struct slip *sl = (struct slip*)(dev->priv);
 578         unsigned long len;
 579 
 580         if (sl->tty == NULL) {
 581                 return -ENODEV;
 582         }
 583 
 584         /*
 585          * Allocate the SLIP frame buffers:
 586          *
 587          * rbuff        Receive buffer.
 588          * xbuff        Transmit buffer.
 589          * cbuff        Temporary compression buffer.
 590          */
 591         len = dev->mtu * 2;
 592         /*
 593          * allow for arrival of larger UDP packets, even if we say not to
 594          * also fixes a bug in which SunOS sends 512-byte packets even with
 595          * an MSS of 128
 596          */
 597         if (len < 576 * 2)  {
 598                 len = 576 * 2;
 599         }
 600         sl->rbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
 601         if (sl->rbuff == NULL)   {
 602                 goto norbuff;
 603         }
 604         sl->xbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
 605         if (sl->xbuff == NULL)   {
 606                 goto noxbuff;
 607         }
 608 #ifdef SL_INCLUDE_CSLIP
 609         sl->cbuff = (unsigned char *) kmalloc(len + 4, GFP_KERNEL);
 610         if (sl->cbuff == NULL)   {
 611                 goto nocbuff;
 612         }
 613         sl->slcomp = slhc_init(16, 16);
 614         if (sl->slcomp == NULL)  {
 615                 goto noslcomp;
 616         }
 617 #endif
 618 
 619 #ifdef CONFIG_AX25
 620         sl->mtu      = dev->mtu + 73;
 621 #else
 622         sl->mtu      = dev->mtu;
 623 #endif
 624         sl->buffsize = len;
 625         sl->rcount   = 0;
 626         sl->xleft    = 0;
 627 #ifdef CONFIG_SLIP_MODE_SLIP6
 628         sl->xdata    = 0;
 629         sl->xbits    = 0;
 630 #endif
 631         sl->flags   &= (1 << SLF_INUSE);      /* Clear ESCAPE & ERROR flags */
 632 #ifdef CONFIG_SLIP_SMART        
 633         sl->keepalive=0;                /* no keepalive by default = VSV */
 634         init_timer(&sl->keepalive_timer);       /* initialize timer_list struct */
 635         sl->keepalive_timer.data=(unsigned long)sl;
 636         sl->keepalive_timer.function=sl_keepalive;      
 637         sl->outfill=0;                  /* & outfill too */
 638         init_timer(&sl->outfill_timer);
 639         sl->outfill_timer.data=(unsigned long)sl;
 640         sl->outfill_timer.function=sl_outfill;
 641 #endif
 642         /* Needed because address '0' is special */
 643         if (dev->pa_addr == 0)  {
 644                 dev->pa_addr=ntohl(0xC0A80001);
 645         }
 646         dev->tbusy  = 0;
 647 /*      dev->flags |= IFF_UP; */
 648         dev->start  = 1;
 649 
 650         return 0;
 651 
 652         /* Cleanup */
 653 #ifdef SL_INCLUDE_CSLIP
 654 noslcomp:
 655         kfree(sl->cbuff);
 656 nocbuff:
 657 #endif
 658         kfree(sl->xbuff);
 659 noxbuff:
 660         kfree(sl->rbuff);
 661 norbuff:
 662         return -ENOMEM;
 663 }
 664 
 665 
 666 /* Close the low-level part of the SLIP channel. Easy! */
 667 static int
 668 sl_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 669 {
 670         struct slip *sl = (struct slip*)(dev->priv);
 671 
 672         if (sl->tty == NULL) {
 673                 return -EBUSY;
 674         }
 675         sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 676         dev->tbusy = 1;
 677         dev->start = 0;
 678         
 679 /*      dev->flags &= ~IFF_UP; */
 680 
 681         return 0;
 682 }
 683 
 684 static int
 685 slip_receive_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 686 {
 687         return 65536;  /* We can handle an infinite amount of data. :-) */
 688 }
 689 
 690 /*
 691  * Handle the 'receiver data ready' interrupt.
 692  * This function is called by the 'tty_io' module in the kernel when
 693  * a block of SLIP data has been received, which can now be decapsulated
 694  * and sent on to some IP layer for further processing.
 695  */
 696 static void
 697 slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 698 {
 699         struct slip *sl = (struct slip *) tty->disc_data;
 700 
 701         if (!sl || sl->magic != SLIP_MAGIC || !sl->dev->start)
 702                 return;
 703 
 704         /*
 705          * Argh! mtu change time! - costs us the packet part received
 706          * at the change
 707          */
 708 #ifdef CONFIG_AX25
 709         if (sl->mtu != sl->dev->mtu + 73)  {
 710 #else
 711         if (sl->mtu != sl->dev->mtu)  {
 712 #endif
 713                 sl_changedmtu(sl);
 714         }
 715 
 716         /* Read the characters out of the buffer */
 717         while (count--) {
 718                 if (fp && *fp++) {
 719                         if (!set_bit(SLF_ERROR, &sl->flags))  {
 720                                 sl->rx_errors++;
 721                         }
 722                         cp++;
 723                         continue;
 724                 }
 725 #ifdef CONFIG_SLIP_MODE_SLIP6
 726                 if (sl->mode & SL_MODE_SLIP6)
 727                         slip_unesc6(sl, *cp++);
 728                 else
 729 #endif
 730                         slip_unesc(sl, *cp++);
 731         }
 732 }
 733 
 734 /*
 735  * Open the high-level part of the SLIP channel.
 736  * This function is called by the TTY module when the
 737  * SLIP line discipline is called for.  Because we are
 738  * sure the tty line exists, we only have to link it to
 739  * a free SLIP channel...
 740  */
 741 static int
 742 slip_open(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 743 {
 744         struct slip *sl = (struct slip *) tty->disc_data;
 745         int err;
 746 
 747         /* First make sure we're not already connected. */
 748         if (sl && sl->magic == SLIP_MAGIC) {
 749                 return -EEXIST;
 750         }
 751 
 752         /* OK.  Find a free SLIP channel to use. */
 753         if ((sl = sl_alloc()) == NULL) {
 754                 return -ENFILE;
 755         }
 756 
 757         sl->tty = tty;
 758         tty->disc_data = sl;
 759         if (tty->driver.flush_buffer)  {
 760                 tty->driver.flush_buffer(tty);
 761         }
 762         if (tty->ldisc.flush_buffer)  {
 763                 tty->ldisc.flush_buffer(tty);
 764         }
 765 
 766         /* Restore default settings */
 767         sl->mode      = SL_MODE_DEFAULT;
 768         sl->dev->type = ARPHRD_SLIP + sl->mode;
 769 #ifdef CONFIG_AX25      
 770         if (sl->dev->type == 260) {             /* KISS */
 771                 sl->dev->type = ARPHRD_AX25;
 772         }
 773 #endif  
 774         /* Perform the low-level SLIP initialization. */
 775         if ((err = sl_open(sl->dev)))  {
 776                 return err;
 777         }
 778         
 779         MOD_INC_USE_COUNT;
 780 
 781         /* Done.  We have linked the TTY line to a channel. */
 782         return sl->dev->base_addr;
 783 }
 784 
 785 
 786 /*
 787  * Close down a SLIP channel.
 788  * This means flushing out any pending queues, and then restoring the
 789  * TTY line discipline to what it was before it got hooked to SLIP
 790  * (which usually is TTY again).
 791  */
 792 static void
 793 slip_close(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 794 {
 795         struct slip *sl = (struct slip *) tty->disc_data;
 796 
 797         /* First make sure we're connected. */
 798         if (!sl || sl->magic != SLIP_MAGIC) {
 799                 return;
 800         }
 801 
 802         (void) dev_close(sl->dev);
 803         
 804         tty->disc_data = 0;
 805         sl->tty = NULL;
 806         sl_free(sl);
 807         unregister_netdev(sl->dev);
 808         MOD_DEC_USE_COUNT;
 809 }
 810 
 811 
 812 static struct enet_statistics *
 813 sl_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 814 {
 815         static struct enet_statistics stats;
 816         struct slip *sl = (struct slip*)(dev->priv);
 817 #ifdef SL_INCLUDE_CSLIP
 818         struct slcompress *comp;
 819 #endif
 820 
 821         memset(&stats, 0, sizeof(struct enet_statistics));
 822 
 823         stats.rx_packets     = sl->rx_packets;
 824         stats.tx_packets     = sl->tx_packets;
 825         stats.rx_dropped     = sl->rx_dropped;
 826         stats.tx_dropped     = sl->tx_dropped;
 827         stats.tx_errors      = sl->tx_errors;
 828         stats.rx_errors      = sl->rx_errors;
 829         stats.rx_over_errors = sl->rx_over_errors;
 830 #ifdef SL_INCLUDE_CSLIP
 831         stats.rx_fifo_errors = sl->rx_compressed;
 832         stats.tx_fifo_errors = sl->tx_compressed;
 833         stats.collisions     = sl->tx_misses;
 834         comp = sl->slcomp;
 835         if (comp) {
 836                 stats.rx_fifo_errors += comp->sls_i_compressed;
 837                 stats.rx_dropped     += comp->sls_i_tossed;
 838                 stats.tx_fifo_errors += comp->sls_o_compressed;
 839                 stats.collisions     += comp->sls_o_misses;
 840         }
 841 #endif /* CONFIG_INET */
 842         return (&stats);
 843 }
 844 
 845 
 846  /************************************************************************
 847   *                     STANDARD SLIP ENCAPSULATION                      *
 848   ************************************************************************/
 849 
 850 int
 851 slip_esc(unsigned char *s, unsigned char *d, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 852 {
 853         unsigned char *ptr = d;
 854         unsigned char c;
 855 
 856         /*
 857          * Send an initial END character to flush out any
 858          * data that may have accumulated in the receiver
 859          * due to line noise.
 860          */
 861 
 862         *ptr++ = END;
 863 
 864         /*
 865          * For each byte in the packet, send the appropriate
 866          * character sequence, according to the SLIP protocol.
 867          */
 868 
 869         while (len-- > 0) {
 870                 switch(c = *s++) {
 871                  case END:
 872                         *ptr++ = ESC;
 873                         *ptr++ = ESC_END;
 874                         break;
 875                  case ESC:
 876                         *ptr++ = ESC;
 877                         *ptr++ = ESC_ESC;
 878                         break;
 879                  default:
 880                         *ptr++ = c;
 881                         break;
 882                 }
 883         }
 884         *ptr++ = END;
 885         return (ptr - d);
 886 }
 887 
 888 static void
 889 slip_unesc(struct slip *sl, unsigned char s)
     /* [previous][next][first][last][top][bottom][index][help] */
 890 {
 891 
 892         switch(s) {
 893          case END:
 894                 /* drop keeptest bit = VSV */
 895                 if (test_bit(SLF_KEEPTEST, &sl->flags))
 896                         clear_bit(SLF_KEEPTEST, &sl->flags);
 897 
 898                 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
 899                         sl_bump(sl);
 900                 }
 901                 clear_bit(SLF_ESCAPE, &sl->flags);
 902                 sl->rcount = 0;
 903                 return;
 904 
 905          case ESC:
 906                 set_bit(SLF_ESCAPE, &sl->flags);
 907                 return;
 908          case ESC_ESC:
 909                 if (clear_bit(SLF_ESCAPE, &sl->flags))  {
 910                         s = ESC;
 911                 }
 912                 break;
 913          case ESC_END:
 914                 if (clear_bit(SLF_ESCAPE, &sl->flags))  {
 915                         s = END;
 916                 }
 917                 break;
 918         }
 919         if (!test_bit(SLF_ERROR, &sl->flags))  {
 920                 if (sl->rcount < sl->buffsize)  {
 921                         sl->rbuff[sl->rcount++] = s;
 922                         return;
 923                 }
 924                 sl->rx_over_errors++;
 925                 set_bit(SLF_ERROR, &sl->flags);
 926         }
 927 }
 928 
 929 
 930 #ifdef CONFIG_SLIP_MODE_SLIP6
 931 /************************************************************************
 932  *                       6 BIT SLIP ENCAPSULATION                       *
 933  ************************************************************************/
 934 
 935 int
 936 slip_esc6(unsigned char *s, unsigned char *d, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
 937 {
 938         unsigned char *ptr = d;
 939         unsigned char c;
 940         int i;
 941         unsigned short v = 0;
 942         short bits = 0;
 943 
 944         /*
 945          * Send an initial END character to flush out any
 946          * data that may have accumulated in the receiver
 947          * due to line noise.
 948          */
 949 
 950         *ptr++ = 0x70;
 951 
 952         /*
 953          * Encode the packet into printable ascii characters
 954          */
 955 
 956         for (i = 0; i < len; ++i) {
 957                 v = (v << 8) | s[i];
 958                 bits += 8;
 959                 while (bits >= 6) {
 960                         bits -= 6;
 961                         c = 0x30 + ((v >> bits) & 0x3F);
 962                         *ptr++ = c;
 963                 }
 964         }
 965         if (bits) {
 966                 c = 0x30 + ((v << (6 - bits)) & 0x3F);
 967                 *ptr++ = c;
 968         }
 969         *ptr++ = 0x70;
 970         return ptr - d;
 971 }
 972 
 973 void
 974 slip_unesc6(struct slip *sl, unsigned char s)
     /* [previous][next][first][last][top][bottom][index][help] */
 975 {
 976         unsigned char c;
 977 
 978         if (s == 0x70) {
 979                 /* drop keeptest bit = VSV */
 980                 if (test_bit(SLF_KEEPTEST, &sl->flags))
 981                         clear_bit(SLF_KEEPTEST, &sl->flags);
 982 
 983                 if (!clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2))  {
 984                         sl_bump(sl);
 985                 }
 986                 sl->rcount = 0;
 987                 sl->xbits = 0;
 988                 sl->xdata = 0;
 989         } else if (s >= 0x30 && s < 0x70) {
 990                 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
 991                 sl->xbits += 6;
 992                 if (sl->xbits >= 8) {
 993                         sl->xbits -= 8;
 994                         c = (unsigned char)(sl->xdata >> sl->xbits);
 995                         if (!test_bit(SLF_ERROR, &sl->flags))  {
 996                                 if (sl->rcount < sl->buffsize)  {
 997                                         sl->rbuff[sl->rcount++] = c;
 998                                         return;
 999                                 }
1000                                 sl->rx_over_errors++;
1001                                 set_bit(SLF_ERROR, &sl->flags);
1002                         }
1003                 }
1004         }
1005 }
1006 #endif /* CONFIG_SLIP_MODE_SLIP6 */
1007 
1008 #ifdef CONFIG_AX25
1009 int
1010 sl_set_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1011 {
1012         int err;
1013 
1014         err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN);
1015         if (err)  {
1016                 return err;
1017         }
1018 
1019         memcpy_fromfs(dev->dev_addr, addr, AX25_ADDR_LEN);      /* addr is an AX.25 shifted ASCII mac address */
1020 
1021         return 0;
1022 }
1023 
1024 static int
1025 sl_set_dev_mac_address(struct device *dev, void *addr)
     /* [previous][next][first][last][top][bottom][index][help] */
1026 {
1027         struct sockaddr *sa=addr;
1028         memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
1029         return 0;
1030 }
1031 #endif /* CONFIG_AX25 */
1032 
1033 
1034 /* Perform I/O control on an active SLIP channel. */
1035 static int
1036 slip_ioctl(struct tty_struct *tty, void *file, int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1037 {
1038         struct slip *sl = (struct slip *) tty->disc_data;
1039         int err;
1040         unsigned int tmp;
1041 
1042         /* First make sure we're connected. */
1043         if (!sl || sl->magic != SLIP_MAGIC) {
1044                 return -EINVAL;
1045         }
1046 
1047         switch(cmd) {
1048          case SIOCGIFNAME:
1049                 err = verify_area(VERIFY_WRITE, arg, strlen(sl->dev->name) + 1);
1050                 if (err)  {
1051                         return err;
1052                 }
1053                 memcpy_tofs(arg, sl->dev->name, strlen(sl->dev->name) + 1);
1054                 return 0;
1055 
1056         case SIOCGIFENCAP:
1057                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1058                 if (err)  {
1059                         return err;
1060                 }
1061                 put_user(sl->mode, (int *)arg);
1062                 return 0;
1063 
1064         case SIOCSIFENCAP:
1065                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1066                 if (err)  {
1067                         return err;
1068                 }
1069                 tmp = get_user((int *)arg);
1070 #ifndef SL_INCLUDE_CSLIP
1071                 if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE))  {
1072                         return -EINVAL;
1073                 }
1074 #else
1075                 if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) ==
1076                     (SL_MODE_ADAPTIVE | SL_MODE_CSLIP))  {
1077                         /* return -EINVAL; */
1078                         tmp &= ~SL_MODE_ADAPTIVE;
1079                 }
1080 #endif
1081 #ifndef CONFIG_SLIP_MODE_SLIP6
1082                 if (tmp & SL_MODE_SLIP6)  {
1083                         return -EINVAL;
1084                 }
1085 #endif
1086 #ifndef CONFIG_AX25
1087                 if (tmp & SL_MODE_AX25) {
1088                         return -EINVAL;
1089                 }
1090 #else
1091                 if (tmp & SL_MODE_AX25) {
1092                         sl->dev->addr_len=AX25_ADDR_LEN;          /* sizeof an AX.25 addr */
1093                         sl->dev->hard_header_len=AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
1094                 } else  {
1095                         sl->dev->addr_len=0;    /* No mac addr in slip mode */
1096                         sl->dev->hard_header_len=0;
1097                 }
1098 #endif
1099                 sl->mode = tmp;
1100                 sl->dev->type = ARPHRD_SLIP+sl->mode;
1101 #ifdef CONFIG_AX25              
1102                 if (sl->dev->type == 260)  {
1103                         sl->dev->type = ARPHRD_AX25;
1104                 }
1105 #endif          
1106                 return 0;
1107 
1108          case SIOCSIFHWADDR:
1109 #ifdef CONFIG_AX25
1110                 return sl_set_mac_address(sl->dev, arg);
1111 #else
1112                 return -EINVAL;
1113 #endif
1114 
1115 #ifdef CONFIG_SLIP_SMART
1116         /* VSV changes start here */
1117         case SIOCSKEEPALIVE:
1118                 if (sl->keepalive)
1119                         del_timer (&sl->keepalive_timer);
1120                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1121                 if (err)  {
1122                         return -err;
1123                 }
1124                 tmp = get_user((int *)arg);
1125                 if (tmp > 255) /* max for unchar */
1126                         return -EINVAL; 
1127                 if ((sl->keepalive = (unchar) tmp) != 0) {
1128                         sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1129                         add_timer(&sl->keepalive_timer);
1130                         set_bit(SLF_KEEPTEST, &sl->flags);
1131                 }
1132                 return 0;
1133 
1134         case SIOCGKEEPALIVE:
1135                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1136                 if (err)  {
1137                         return -err;
1138                 }
1139                 put_user(sl->keepalive, (int *)arg);
1140                 return 0;
1141 
1142         case SIOCSOUTFILL:
1143                 if (sl->outfill)
1144                          (void)del_timer (&sl->outfill_timer);
1145                 err = verify_area(VERIFY_READ, arg, sizeof(int));
1146                 if (err)  {
1147                         return -err;
1148                 }
1149                 tmp = get_user((int *)arg);
1150                 if (tmp > 255) /* max for unchar */
1151                         return -EINVAL; 
1152                 if ((sl->outfill = (unchar) tmp) != 0){
1153                         sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1154                         add_timer(&sl->outfill_timer);
1155                         set_bit(SLF_OUTWAIT, &sl->flags);
1156                 }
1157                 return 0;
1158 
1159         case SIOCGOUTFILL:
1160                 err = verify_area(VERIFY_WRITE, arg, sizeof(int));
1161                 if (err)  {
1162                         return -err;
1163                 }
1164                 put_user(sl->outfill, (int *)arg);
1165                 return 0;
1166         /* VSV changes end */
1167 #endif  
1168 
1169         /* Allow stty to read, but not set, the serial port */
1170         case TCGETS:
1171         case TCGETA:
1172                 return n_tty_ioctl(tty, (struct file *) file, cmd, (unsigned long) arg);
1173 
1174         default:
1175                 return -ENOIOCTLCMD;
1176         }
1177 }
1178 
1179 static int sl_open_dev(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1180 {
1181         struct slip *sl = (struct slip*)(dev->priv);
1182         if(sl->tty==NULL)
1183                 return -ENODEV;
1184         return 0;
1185 }
1186 
1187 /* Initialize SLIP control device -- register SLIP line discipline */
1188 #ifdef MODULE
1189 static int slip_init_ctrl_dev(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1190 #else   /* !MODULE */
1191 int slip_init_ctrl_dev(struct device *dummy)
1192 #endif  /* !MODULE */
1193 {
1194         int status;
1195 
1196         if (slip_maxdev < 4) slip_maxdev = 4; /* Sanity */
1197 
1198         printk("SLIP: version %s (dynamic channels, max=%d)"
1199 #ifdef CONFIG_SLIP_MODE_SLIP6
1200                " (6 bit encapsulation enabled)"
1201 #endif
1202                ".\n",
1203                SLIP_VERSION, slip_maxdev );
1204 #if defined(SL_INCLUDE_CSLIP) && !defined(MODULE)
1205         printk("CSLIP: code copyright 1989 Regents of the University of California.\n");
1206 #endif
1207 #ifdef CONFIG_AX25
1208         printk("AX25: KISS encapsulation enabled.\n");
1209 #endif
1210 #ifdef CONFIG_SLIP_SMART
1211         printk("SLIP linefill/keepalive option.\n");
1212 #endif  
1213 
1214         slip_ctrls = (slip_ctrl_t **) kmalloc(sizeof(void*)*slip_maxdev, GFP_KERNEL);
1215         if (slip_ctrls == NULL) 
1216         {
1217                 printk("SLIP: Can't allocate slip_ctrls[] array!  Uaargh! (-> No SLIP available)\n");
1218                 return -ENOMEM;
1219         }
1220         
1221         /* Clear the pointer array, we allocate devices when we need them */
1222         memset(slip_ctrls, 0, sizeof(void*)*slip_maxdev); /* Pointers */
1223 
1224         /* Fill in our line protocol discipline, and register it */
1225         memset(&sl_ldisc, 0, sizeof(sl_ldisc));
1226         sl_ldisc.magic  = TTY_LDISC_MAGIC;
1227         sl_ldisc.flags  = 0;
1228         sl_ldisc.open   = slip_open;
1229         sl_ldisc.close  = slip_close;
1230         sl_ldisc.read   = NULL;
1231         sl_ldisc.write  = NULL;
1232         sl_ldisc.ioctl  = (int (*)(struct tty_struct *, struct file *,
1233                                    unsigned int, unsigned long)) slip_ioctl;
1234         sl_ldisc.select = NULL;
1235         sl_ldisc.receive_buf = slip_receive_buf;
1236         sl_ldisc.receive_room = slip_receive_room;
1237         sl_ldisc.write_wakeup = slip_write_wakeup;
1238         if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0)  {
1239                 printk("SLIP: can't register line discipline (err = %d)\n", status);
1240         }
1241 
1242 
1243 #ifdef MODULE
1244         return status;
1245 #else
1246         /* Return "not found", so that dev_init() will unlink
1247          * the placeholder device entry for us.
1248          */
1249         return ENODEV;
1250 #endif
1251       }
1252 
1253 
1254 /* Initialize the SLIP driver.  Called by DDI. */
1255 int
1256 slip_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1257 {
1258         struct slip *sl = (struct slip*)(dev->priv);
1259         int i;
1260 #ifdef CONFIG_AX25
1261         static char ax25_bcast[AX25_ADDR_LEN] =
1262                 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
1263         static char ax25_test[AX25_ADDR_LEN] =
1264                 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
1265 #endif
1266 
1267         if (sl == NULL)         /* Allocation failed ?? */
1268           return -ENODEV;
1269 
1270         /* Set up the "SLIP Control Block". (And clear statistics) */
1271         
1272         memset(sl, 0, sizeof (struct slip));
1273         sl->magic  = SLIP_MAGIC;
1274         sl->dev    = dev;
1275         
1276         /* Finish setting up the DEVICE info. */
1277         dev->mtu                = SL_MTU;
1278         dev->hard_start_xmit    = sl_xmit;
1279         dev->open               = sl_open_dev;
1280         dev->stop               = sl_close;
1281         dev->hard_header        = sl_header;
1282         dev->get_stats          = sl_get_stats;
1283 #ifdef HAVE_SET_MAC_ADDR
1284 #ifdef CONFIG_AX25
1285         dev->set_mac_address    = sl_set_dev_mac_address;
1286 #endif
1287 #endif
1288         dev->hard_header_len    = 0;
1289         dev->addr_len           = 0;
1290         dev->type               = ARPHRD_SLIP + SL_MODE_DEFAULT;
1291         dev->tx_queue_len       = 10;
1292 #ifdef CONFIG_AX25
1293         if (sl->dev->type == 260) {
1294                 sl->dev->type = ARPHRD_AX25;
1295         }
1296         memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);      /* Only activated in AX.25 mode */
1297         memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);        /*    ""      ""       ""    "" */
1298 #endif
1299         dev->rebuild_header     = sl_rebuild_header;
1300 
1301         for (i = 0; i < DEV_NUMBUFFS; i++)  {
1302                 skb_queue_head_init(&dev->buffs[i]);
1303         }
1304 
1305         /* New-style flags. */
1306         dev->flags              = 0;
1307         dev->family             = AF_INET;
1308         dev->pa_addr            = 0;
1309         dev->pa_brdaddr         = 0;
1310         dev->pa_mask            = 0;
1311         dev->pa_alen            = 4;
1312 
1313         return 0;
1314 }
1315 #ifdef MODULE
1316 
1317 int
1318 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1319 {
1320         return slip_init_ctrl_dev();
1321 }
1322 
1323 void
1324 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1325 {
1326         int i;
1327 
1328         if (slip_ctrls != NULL) 
1329         {
1330                 for (i = 0; i < slip_maxdev; i++)  
1331                 {
1332                         if (slip_ctrls[i] != NULL) 
1333                         {
1334                                 unregister_netdev(&(slip_ctrls[i]->dev));
1335                                 kfree(slip_ctrls[i]);
1336                         }
1337                 }
1338                 kfree(slip_ctrls);
1339                 slip_ctrls = NULL;
1340         }
1341         if ((i = tty_register_ldisc(N_SLIP, NULL)))  
1342         {
1343                 printk("SLIP: can't unregister line discipline (err = %d)\n", i);
1344         }
1345 }
1346 #endif /* MODULE */
1347 
1348 #ifdef CONFIG_SLIP_SMART
1349 /*
1350  * This is start of the code for multislip style line checking
1351  * added by Stanislav Voronyi. All changes before marked VSV
1352  */
1353  
1354 static void sl_outfill(unsigned long sls)
     /* [previous][next][first][last][top][bottom][index][help] */
1355 {
1356         struct slip *sl=(struct slip *)sls;
1357 
1358         if(sls==NULL)
1359                 return;
1360 
1361         if(sl->outfill)
1362         {
1363                 if( test_bit(SLF_OUTWAIT, &sl->flags) )
1364                 {
1365                         /* no packets was transmited, do outfill */
1366 #ifdef CONFIG_SLIP_MODE_SLIP6
1367                         unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END;
1368 #else
1369                         unsigned char s = END;
1370 #endif
1371                         /* put END into tty queue. Is it right ??? */
1372                         if (!test_bit(0, (void *) &sl->dev->tbusy))
1373                         { 
1374                                 /* if device busy no outfill */
1375                                 sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1376                                 sl->tty->driver.write(sl->tty, 0, &s, 1);
1377                         }
1378                 }
1379                 else
1380                         set_bit(SLF_OUTWAIT, &sl->flags);
1381                 (void)del_timer(&sl->outfill_timer);
1382                 sl->outfill_timer.expires=jiffies+sl->outfill*HZ;
1383                 add_timer(&sl->outfill_timer);
1384         }
1385         else
1386                 del_timer(&sl->outfill_timer);
1387 }
1388 
1389 static void sl_keepalive(unsigned long sls)
     /* [previous][next][first][last][top][bottom][index][help] */
1390 {
1391         struct slip *sl=(struct slip *)sls;
1392 
1393         if(sls==NULL)
1394                 return;
1395 
1396         if( sl->keepalive)
1397         {
1398                 if(test_bit(SLF_KEEPTEST, &sl->flags))
1399                 {
1400                         /* keepalive still high :(, we must hangup */
1401                         (void)del_timer(&sl->keepalive_timer);
1402                         if( sl->outfill ) /* outfill timer must be deleted too */
1403                                 (void)del_timer(&sl->outfill_timer);
1404                         printk("%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name);
1405                         tty_hangup(sl->tty); /* this must hangup tty & close slip */
1406                         /* I think we need not something else */
1407                         return;
1408                 }
1409                 else
1410                         set_bit(SLF_KEEPTEST, &sl->flags);
1411                 (void)del_timer(&sl->keepalive_timer);
1412                  sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ;
1413                 add_timer(&sl->keepalive_timer);
1414         }
1415         else
1416                 (void)del_timer(&sl->keepalive_timer);
1417 }
1418 
1419 #endif

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