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

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