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

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