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_type_trans
  11. sl_header
  12. sl_rebuild_header
  13. sl_open
  14. sl_close
  15. slip_receive_room
  16. slip_receive_buf
  17. slip_open
  18. slip_close
  19. sl_get_stats
  20. slip_esc
  21. slip_unesc
  22. slip_esc6
  23. slip_unesc6
  24. sl_set_mac_address
  25. sl_set_dev_mac_address
  26. slip_ioctl
  27. slip_init
  28. init_module
  29. cleanup_module

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

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