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

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