root/drivers/net/slip.c

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

DEFINITIONS

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

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