root/drivers/net/slip.c

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

DEFINITIONS

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

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