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_enqueue
  6. sl_dequeue
  7. sl_lock
  8. sl_unlock
  9. sl_bump
  10. sl_encaps
  11. slip_write_wakeup
  12. sl_xmit
  13. sl_type_trans
  14. sl_header
  15. sl_rebuild_header
  16. sl_open
  17. sl_close
  18. slip_receive_room
  19. slip_receive_buf
  20. slip_open
  21. sl_get_stats
  22. slip_close
  23. slip_esc
  24. slip_unesc
  25. slip_esc6
  26. slip_unesc6
  27. sl_set_mac_address
  28. sl_set_dev_mac_address
  29. slip_ioctl
  30. slip_init

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

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