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

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