root/drivers/net/ppp.c

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

DEFINITIONS

This source file includes following definitions.
  1. ppp_init
  2. ppp_init_ctrl_blk
  3. ppp_changedmtu
  4. ppp_release
  5. ppp_close
  6. ppp_open
  7. ppp_dev_open
  8. ppp_dev_close
  9. ppp_dev_ioctl
  10. ppp_output_done
  11. ppp_kick_tty
  12. ppp_kick_tty
  13. ppp_write_wakeup
  14. ppp_enqueue
  15. ppp_dump_inqueue
  16. ppp_tty_input_ready
  17. ppp_unesc
  18. ppp_receive_room
  19. ppp_receive_buf
  20. ppp_doframe
  21. ppp_do_ip
  22. ppp_us_queue
  23. ppp_read
  24. ppp_stuff_char
  25. ppp_write
  26. ppp_ioctl
  27. ppp_select
  28. ppp_xmit
  29. ppp_type_trans
  30. ppp_header
  31. ppp_rebuild_header
  32. ppp_add_arp
  33. ppp_header
  34. ppp_rebuild_header
  35. ppp_get_stats
  36. ppp_find
  37. ppp_alloc
  38. ppp_lock
  39. ppp_unlock
  40. ppp_add_fcs
  41. ppp_check_fcs
  42. ppp_print_hex
  43. ppp_print_char
  44. ppp_print_buffer
  45. init_module
  46. cleanup_module

   1 /*
   2    PPP for Linux
   3 */
   4 
   5 /*
   6    Sources:
   7 
   8    slip.c
   9 
  10    RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
  11    Multi-protocol Datagrams over Point-to-Point Links
  12 
  13    RFC1332: IPCP
  14 
  15    ppp-2.0
  16 
  17    Flags for this module (any combination is acceptable for testing.):
  18 
  19    NET02D             - Define if using Net-2-Debugged in kernels earlier
  20                         than v1.1.4.
  21 
  22    NEW_TTY_DRIVERS    - Define if using new Ted Ts'o's alpha TTY drivers
  23                         from tsx-11.mit.edu. From Ted Ts'o.
  24 
  25    OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
  26                         character. This is normally set to ((HZ * 3) / 2).
  27                         This is 1.5 seconds. If not defined then the leading
  28                         flag is always sent.  
  29 */
  30 
  31 /* #define NET02D                               -* */
  32 #define NEW_TTY_DRIVERS                         /* */
  33 #define OPTIMIZE_FLAG_TIME  ((HZ * 3)/2)        /* */
  34 #define CHECK_CHARACTERS
  35 
  36 #ifdef MODULE
  37 #include <linux/module.h>
  38 #include <linux/version.h>
  39 #endif
  40 
  41 #include <linux/kernel.h>
  42 #include <linux/sched.h>
  43 #include <linux/types.h>
  44 #include <linux/fcntl.h>
  45 #include <linux/interrupt.h>
  46 #include <linux/ptrace.h>
  47 #include <linux/ioport.h>
  48 #include <linux/in.h>
  49 #include <linux/malloc.h>
  50 #include <linux/tty.h>
  51 #include <linux/errno.h>
  52 #include <linux/sched.h>   /* to get the struct task_struct */
  53 #include <linux/string.h>  /* used in new tty drivers */
  54 #include <linux/signal.h>  /* used in new tty drivers */
  55 #include <asm/system.h>
  56 #include <asm/bitops.h>
  57 #include <asm/segment.h>
  58 
  59 #ifdef NET02D                           /* v1.1.4 net code and earlier */
  60 #include <dev.h>
  61 #include <skbuff.h>
  62 #include <inet.h>
  63 #define skb_queue_head_init(buf)        *(buf) = NULL
  64 #else                                   /* v1.1.5 and later */
  65 #include <linux/netdevice.h>
  66 #include <linux/skbuff.h>
  67 #include <linux/inet.h>
  68 #endif
  69 
  70 #include <linux/ppp.h>
  71 
  72 #include <linux/ip.h>
  73 #include <linux/tcp.h>
  74 
  75 #include "slhc.h"
  76 
  77 #include <linux/if_arp.h>
  78 #ifndef ARPHRD_PPP
  79 #define ARPHRD_PPP 0
  80 #endif
  81 
  82 #define PRINTK(p) printk p ;
  83 #define ASSERT(p) if (!p) PRINTK ((KERN_CRIT "assertion failed: " # p))
  84 #define PRINTKN(n,p) {if (ppp_debug >= n) PRINTK (p)}
  85 #define CHECK_PPP(a)  if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return a;}
  86 #define CHECK_PPP_VOID()  if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return;}
  87 
  88 #define in_xmap(ppp,c)  (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
  89 #define in_rmap(ppp,c)  ((((unsigned int) (unsigned char) (c)) < 0x20) && \
  90                         ppp->recv_async_map & (1 << (c)))
  91 
  92 #define bset(p,b)       ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
  93 
  94 int ppp_debug = 2;
  95 int ppp_debug_netpackets = 0;
  96 
  97 /* Define this string only once for all macro invocations */
  98 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
  99 
 100 int ppp_init(struct device *);
 101 static void ppp_init_ctrl_blk(struct ppp *);
 102 static int ppp_dev_open(struct device *);
 103 static int ppp_dev_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
 104 static int ppp_dev_close(struct device *);
 105 static void ppp_kick_tty(struct ppp *);
 106 
 107 #ifdef NEW_TTY_DRIVERS
 108 #define ppp_find(tty) ((struct ppp *) tty->disc_data)
 109 #else
 110 static void ppp_output_done(void *);
 111 static void ppp_unesc(struct ppp *ppp, unsigned char *c, int n);
 112 static struct ppp *ppp_find(struct tty_struct *);
 113 #endif
 114 
 115 static void ppp_doframe(struct ppp *);
 116 static int ppp_do_ip(struct ppp *, unsigned short, unsigned char *, int);
 117 static int ppp_us_queue(struct ppp *, unsigned short, unsigned char *, int);
 118 static int ppp_xmit(struct sk_buff *, struct device *);
 119 static unsigned short ppp_type_trans(struct sk_buff *, struct device *);
 120 
 121 #ifdef NET02D
 122 static int ppp_header(unsigned char *buff, struct device *dev,
 123                       unsigned short type, unsigned long daddr,
 124                       unsigned long saddr, unsigned len);
 125 static int ppp_rebuild_header(void *buff, struct device *dev);
 126 static void ppp_add_arp(unsigned long addr, struct sk_buff *skb,
 127                         struct device *dev);
 128 #else
 129 static int ppp_header(unsigned char *, struct device *, unsigned short,
 130                       void *, void *, unsigned, struct sk_buff *);
 131 static int ppp_rebuild_header(void *, struct device *, unsigned long,
 132                               struct sk_buff *);
 133 #endif
 134 
 135 static struct enet_statistics *ppp_get_stats (struct device *);
 136 static struct ppp *ppp_alloc(void);
 137 static int ppp_lock(struct ppp *);
 138 static void ppp_unlock(struct ppp *);
 139 static void ppp_add_fcs(struct ppp *);
 140 static int ppp_check_fcs(struct ppp *);
 141 static void ppp_print_buffer(const char *,char *,int,int);
 142 
 143 static int ppp_read(struct tty_struct *, struct file *, unsigned char *,
 144                     unsigned int);
 145 static int ppp_write(struct tty_struct *, struct file *, unsigned char *,
 146                      unsigned int);
 147 static int ppp_ioctl(struct tty_struct *, struct file *, unsigned int,
 148                      unsigned long);
 149 static int ppp_select(struct tty_struct *tty, struct inode * inode,
 150                       struct file * filp, int sel_type, select_table * wait);
 151 static int ppp_open(struct tty_struct *);
 152 static void ppp_close(struct tty_struct *);
 153 
 154 #ifdef NEW_TTY_DRIVERS
 155 static int ppp_receive_room(struct tty_struct *tty);
 156 static void ppp_receive_buf(struct tty_struct *tty, unsigned char *cp,
 157                             char *fp, int count);
 158 static void ppp_write_wakeup(struct tty_struct *tty);
 159 #else
 160 static void ppp_tty_input_ready(struct tty_struct *);
 161 #endif
 162 
 163 /* FCS table from RFC1331 */
 164 
 165 static unsigned short fcstab[256] = {
 166   0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
 167   0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
 168   0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
 169   0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
 170   0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
 171   0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
 172   0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
 173   0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
 174   0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
 175   0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
 176   0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
 177   0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
 178   0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
 179   0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
 180   0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
 181   0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
 182   0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
 183   0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
 184   0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
 185   0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
 186   0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
 187   0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
 188   0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
 189   0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
 190   0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
 191   0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
 192   0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
 193   0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
 194   0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
 195   0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
 196   0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
 197   0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
 198   };
 199 
 200 struct tty_ldisc ppp_ldisc;
 201 
 202 static struct ppp ppp_ctrl[PPP_NRUNIT];
 203 
 204 /*************************************************************
 205  * INITIALIZATION
 206  *************************************************************/
 207 
 208 static int first_time = 1;
 209 
 210 /* called at boot time for each ppp device */
 211 
 212 int
 213 ppp_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 214 {
 215   struct ppp *ppp;
 216   int i;
 217 
 218   ppp = &ppp_ctrl[dev->base_addr];
 219 
 220   if (first_time) {
 221     first_time = 0;
 222 
 223     printk (KERN_INFO "PPP: version %s (%d channels)"
 224 #ifdef NET02D
 225            " NET02D"
 226 #endif
 227 #ifdef NEW_TTY_DRIVERS
 228            " NEW_TTY_DRIVERS"
 229 #endif
 230 #ifdef OPTIMIZE_FLAG_TIME
 231            " OPTIMIZE_FLAGS"
 232 #endif
 233            "\n", PPP_VERSION, PPP_NRUNIT);
 234 
 235     printk (KERN_INFO
 236            "TCP compression code copyright 1989 Regents of the "
 237            "University of California\n");
 238 
 239     (void) memset(&ppp_ldisc, 0, sizeof(ppp_ldisc));
 240     ppp_ldisc.open    = ppp_open;
 241     ppp_ldisc.close   = ppp_close;
 242     ppp_ldisc.read    = ppp_read;
 243     ppp_ldisc.write   = ppp_write;
 244     ppp_ldisc.ioctl   = ppp_ioctl;
 245     ppp_ldisc.select  = ppp_select;
 246 
 247 #ifdef NEW_TTY_DRIVERS
 248     ppp_ldisc.magic       = TTY_LDISC_MAGIC;
 249     ppp_ldisc.receive_room = ppp_receive_room;
 250     ppp_ldisc.receive_buf = ppp_receive_buf;
 251     ppp_ldisc.write_wakeup = ppp_write_wakeup;
 252 #else
 253     ppp_ldisc.handler     = ppp_tty_input_ready;
 254 #endif
 255 
 256     if ((i = tty_register_ldisc(N_PPP, &ppp_ldisc)) == 0)
 257       printk(KERN_INFO "PPP line discipline registered.\n");
 258     else
 259       printk(KERN_ERR "error registering line discipline: %d\n", i);
 260   }
 261 
 262   /* initialize PPP control block */
 263   ppp_init_ctrl_blk (ppp);
 264   ppp->inuse = 0;
 265   ppp->line  = dev->base_addr;
 266   ppp->tty   = NULL;
 267   ppp->dev   = dev;
 268 
 269   /* clear statistics */
 270   memset (&ppp->stats, '\0', sizeof (struct ppp_stats));
 271 
 272   /* device INFO */
 273   dev->mtu             = PPP_MTU;
 274   dev->hard_start_xmit = ppp_xmit;
 275   dev->open            = ppp_dev_open;
 276   dev->stop            = ppp_dev_close;
 277   dev->get_stats       = ppp_get_stats;
 278   dev->hard_header     = ppp_header;
 279   dev->type_trans      = ppp_type_trans;
 280   dev->rebuild_header  = ppp_rebuild_header;
 281   dev->hard_header_len = 0;
 282   dev->addr_len        = 0;
 283   dev->type            = ARPHRD_PPP;
 284 
 285 #ifdef NET02D
 286   dev->add_arp         = ppp_add_arp;
 287   dev->queue_xmit      = dev_queue_xmit;
 288 #else
 289   dev->do_ioctl        = ppp_dev_ioctl;
 290 #endif
 291 
 292   for (i = 0; i < DEV_NUMBUFFS; i++)
 293     skb_queue_head_init(&dev->buffs[i]);  /* = NULL if NET02D */
 294 
 295   /* New-style flags */
 296   dev->flags      = IFF_POINTOPOINT;
 297   dev->family     = AF_INET;
 298   dev->pa_addr    = 0;
 299   dev->pa_brdaddr = 0;
 300   dev->pa_mask    = 0;
 301   dev->pa_alen    = sizeof(unsigned long);
 302 
 303   return 0;
 304 }
 305 
 306 static void
 307 ppp_init_ctrl_blk(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
 308 {
 309   ppp->magic            = PPP_MAGIC;
 310   ppp->sending          = 0;
 311   ppp->toss             = 0xFE;
 312   ppp->escape           = 0;
 313 
 314   ppp->flags            = 0;
 315   ppp->mtu              = PPP_MTU;
 316   ppp->mru              = PPP_MRU;
 317   ppp->fcs              = 0;
 318 
 319   memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
 320   ppp->xmit_async_map[0] = 0xffffffff;
 321   ppp->xmit_async_map[3] = 0x60000000;
 322   ppp->recv_async_map    = 0x00000000;
 323 
 324   ppp->slcomp           = NULL;
 325   ppp->rbuff            = NULL;
 326   ppp->xbuff            = NULL;
 327   ppp->cbuff            = NULL;
 328 
 329   ppp->rhead            = NULL;
 330   ppp->rend             = NULL;
 331   ppp->rcount           = 0;
 332   ppp->xhead            = NULL;
 333   ppp->xtail            = NULL;
 334 
 335   ppp->us_rbuff         = NULL;
 336   ppp->us_rbuff_end     = NULL;
 337   ppp->us_rbuff_head    = NULL;
 338   ppp->us_rbuff_tail    = NULL;
 339   ppp->read_wait        = NULL;
 340   ppp->write_wait       = NULL;
 341   ppp->us_rbuff_lock    = 0;
 342   ppp->inp_sig          = 0;
 343   ppp->inp_sig_pid      = 0;
 344 
 345 #ifdef OPTIMIZE_FLAG_TIME /* ensure flag will always be sent first time */
 346   ppp->last_xmit        = jiffies - OPTIMIZE_FLAG_TIME;
 347 #else
 348   ppp->last_xmit        = 0;
 349 #endif
 350 
 351   /* clear statistics */
 352   memset (&ppp->stats, '\0', sizeof (struct ppp_stats));
 353 
 354   /* Reset the demand dial information */
 355   ppp->ddinfo.ip_sjiffies  =
 356   ppp->ddinfo.ip_rjiffies  =
 357   ppp->ddinfo.nip_sjiffies =
 358   ppp->ddinfo.nip_rjiffies = jiffies;
 359 }
 360 
 361 /*
 362  * MTU has been changed by the IP layer. Unfortunately we are not told
 363  * about this, but we spot it ourselves and fix things up. We could be
 364  * in an upcall from the tty driver, or in an ip packet queue.
 365  */
 366    
 367 static void
 368 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
     /* [previous][next][first][last][top][bottom][index][help] */
 369 {
 370   struct device *dev;
 371   unsigned char *new_rbuff, *new_xbuff, *new_cbuff;
 372   unsigned char *old_rbuff, *old_xbuff, *old_cbuff;
 373   int mtu, mru;
 374 /*
 375  *  Allocate the buffer from the kernel for the data
 376  */
 377   dev = ppp->dev;
 378   mru = new_mru;
 379   mtu = new_mtu;
 380 
 381   /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
 382   if (mru < PPP_MRU)
 383     mru = PPP_MRU;
 384 
 385   mtu = (mtu * 2) + 20;
 386   mru = (mru * 2) + 20;
 387 
 388   PRINTKN (2,(KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
 389               dev->name, new_mtu, new_mru));
 390         
 391   new_xbuff = (unsigned char *) kmalloc(mtu + 4, GFP_ATOMIC);
 392   new_rbuff = (unsigned char *) kmalloc(mru + 4, GFP_ATOMIC);
 393   new_cbuff = (unsigned char *) kmalloc(mru + 4, GFP_ATOMIC);
 394 /*
 395  *  If the buffers failed to allocate then complain.
 396  */
 397   if (new_xbuff == NULL || new_rbuff == NULL || new_cbuff == NULL)
 398     {
 399       PRINTKN (2,(KERN_ERR "ppp: failed to allocate new buffers\n"));
 400 /*
 401  *  Release new buffer pointers if the updates were not performed
 402  */
 403       if (new_rbuff != NULL)
 404         kfree (new_rbuff);
 405 
 406       if (new_xbuff != NULL)
 407         kfree (new_xbuff);
 408 
 409       if (new_cbuff != NULL)
 410         kfree (new_cbuff);
 411     }
 412 /*
 413  *  Update the pointers to the new buffer structures.
 414  */
 415   else
 416     {
 417       cli();
 418       old_xbuff       = ppp->xbuff;
 419       old_rbuff       = ppp->rbuff;
 420       old_cbuff       = ppp->cbuff;
 421 
 422       ppp->xbuff      = new_xbuff;
 423       ppp->rbuff      = new_rbuff;
 424       ppp->cbuff      = new_cbuff;
 425 
 426       dev->mem_start  = (unsigned long) new_xbuff;
 427       dev->mem_end    = (unsigned long) (dev->mem_start + mtu);
 428 
 429       dev->rmem_start = (unsigned long) new_rbuff;
 430       ppp->rend       = (unsigned char *)
 431       dev->rmem_end   = (unsigned long) (dev->rmem_start + mru);
 432 
 433       ppp->rhead      = new_rbuff;
 434 /*
 435  *  Update the parameters for the new buffer sizes
 436  */
 437       ppp->toss         = 0xFE;
 438       ppp->escape       = 0;
 439       ppp->sending      = 0;
 440       ppp->rcount       = 0;
 441 
 442       ppp->mru          = new_mru;
 443 
 444       ppp->mtu          =
 445       dev->mtu          = new_mtu;
 446 
 447       sti();
 448 /*
 449  *  Release old buffer pointers
 450  */
 451       if (old_rbuff != NULL)
 452         kfree (old_rbuff);
 453 
 454       if (old_xbuff != NULL)
 455         kfree (old_xbuff);
 456 
 457       if (old_cbuff != NULL)
 458         kfree (old_cbuff);
 459     }
 460 }
 461 
 462 /* called when we abandon the PPP line discipline */
 463 
 464 static void
 465 ppp_release(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467 #ifdef NEW_TTY_DRIVERS
 468   if (ppp->tty != NULL && ppp->tty->disc_data == ppp)
 469     ppp->tty->disc_data = NULL; /* Break the tty->ppp link */
 470 #endif
 471 
 472   if (ppp->dev) {
 473     dev_close (ppp->dev);
 474     ppp->dev->flags = 0;
 475   }
 476 
 477   kfree (ppp->xbuff);
 478   kfree (ppp->cbuff);
 479   kfree (ppp->rbuff);
 480   kfree (ppp->us_rbuff);
 481 
 482   ppp->xbuff    =
 483   ppp->cbuff    =
 484   ppp->rbuff    =
 485   ppp->us_rbuff = NULL;
 486 
 487   if (ppp->slcomp) {
 488     slhc_free(ppp->slcomp);
 489     ppp->slcomp = NULL;
 490   }
 491 
 492   ppp->inuse = 0;
 493   ppp->tty   = NULL;
 494 }
 495 
 496 static void
 497 ppp_close(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 498 {
 499   struct ppp *ppp = ppp_find(tty);
 500 
 501   if (ppp == NULL || ppp->magic != PPP_MAGIC) {
 502     PRINTKN (1,(KERN_WARNING "ppp: trying to close unopened tty!\n"));
 503   } else {
 504     CHECK_PPP_VOID();
 505     ppp_release (ppp);
 506 
 507     PRINTKN (2,(KERN_INFO "ppp: channel %s closing.\n", ppp->dev->name));
 508   }
 509 }
 510 
 511 /* called when PPP line discipline is selected on a tty */
 512 static int
 513 ppp_open(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 514 {
 515   struct ppp *ppp = ppp_find(tty);
 516 
 517   if (ppp) {
 518     PRINTKN (1,(KERN_ERR "ppp_open: gack! tty already associated to %s!\n",
 519                 ppp->magic == PPP_MAGIC ? ppp->dev->name : "unknown"));
 520     return -EEXIST;
 521   }
 522 
 523   ppp = ppp_alloc();
 524   if (ppp == NULL) {
 525     PRINTKN (1,(KERN_ERR "ppp_open: couldn't allocate ppp channel\n"));
 526     return -ENFILE;
 527   }
 528 
 529   /* make sure the channel is actually open */
 530   ppp_init_ctrl_blk (ppp);
 531 
 532   ppp->tty = tty;
 533 
 534 #ifdef NEW_TTY_DRIVERS
 535   tty->disc_data = ppp;
 536   if (tty->driver.flush_buffer)
 537     tty->driver.flush_buffer(tty);
 538   if (tty->ldisc.flush_buffer)
 539     tty->ldisc.flush_buffer(tty);
 540 #else
 541   tty_read_flush (tty);
 542   tty_write_flush (tty);
 543 #endif
 544 
 545   if ((ppp->slcomp = slhc_init(16, 16)) == NULL) {
 546     PRINTKN (1,(KERN_ERR "ppp: no space for compression buffers!\n"));
 547     ppp_release (ppp);
 548     return -ENOMEM;
 549   }
 550 
 551   /* Define the buffers for operation */
 552   ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru);
 553   if (ppp->rbuff == NULL) {
 554     ppp_release (ppp);
 555     return -ENOMEM;
 556   }
 557 
 558   /* Allocate a user-level receive buffer */
 559   ppp->us_rbuff = (unsigned char *) kmalloc (RBUFSIZE, GFP_KERNEL);
 560   if (ppp->us_rbuff == NULL) {
 561     PRINTKN (1,(KERN_ERR "ppp: no space for user receive buffer\n"));
 562     ppp_release (ppp);
 563     return -ENOMEM;
 564   }
 565 
 566   ppp->us_rbuff_head =
 567   ppp->us_rbuff_tail = ppp->us_rbuff;
 568   ppp->us_rbuff_end  = ppp->us_rbuff + RBUFSIZE;
 569 
 570   PRINTKN (2,(KERN_INFO "ppp: channel %s open\n", ppp->dev->name));
 571 
 572 #ifdef MODULE
 573   MOD_INC_USE_COUNT;
 574 #endif
 575 
 576   return (ppp->line);
 577 }
 578 
 579 /* called when ppp interface goes "up".  here this just means we start
 580    passing IP packets */
 581 static int
 582 ppp_dev_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 583 {
 584   struct ppp *ppp = &ppp_ctrl[dev->base_addr];
 585 
 586   /* reset POINTOPOINT every time, since dev_close zaps it! */
 587   dev->flags |= IFF_POINTOPOINT;
 588 
 589   if (ppp->tty == NULL) {
 590     PRINTKN (1,(KERN_ERR "ppp: %s not connected to a TTY! can't go open!\n",
 591                 dev->name));
 592     return -ENXIO;
 593   }
 594 
 595   PRINTKN (2,(KERN_INFO "ppp: channel %s going up for IP packets!\n",
 596               dev->name));
 597 
 598   CHECK_PPP(-ENXIO);
 599   return 0;
 600 }
 601 
 602 static int
 603 ppp_dev_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 604 {
 605   struct ppp *ppp = &ppp_ctrl[dev->base_addr];
 606 
 607   if (ppp->tty == NULL) {
 608     PRINTKN (1,(KERN_ERR "ppp: %s not connected to a TTY! can't go down!\n",
 609                 dev->name));
 610     return -ENXIO;
 611   }
 612 
 613   PRINTKN (2,(KERN_INFO "ppp: channel %s going down for IP packets!\n",
 614               dev->name));
 615   CHECK_PPP(-ENXIO);
 616 #ifdef MODULE
 617   MOD_DEC_USE_COUNT;
 618 #endif
 619   return 0;
 620 }
 621 
 622 #ifndef NET02D
 623 static int ppp_dev_ioctl(struct device *dev, struct ifreq *ifr, int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 624 {
 625   struct ppp *ppp = &ppp_ctrl[dev->base_addr];
 626   int    error;
 627 
 628   struct stats
 629   {
 630     struct ppp_stats  ppp_stats;
 631     struct slcompress slhc;
 632   } *result;
 633 
 634   error = verify_area (VERIFY_READ,
 635                        ifr->ifr_ifru.ifru_data,
 636                        sizeof (struct stats));
 637 
 638   if (error == 0) {
 639     result = (struct stats *) ifr->ifr_ifru.ifru_data;
 640 
 641     memcpy_tofs (&result->ppp_stats, &ppp->stats, sizeof (struct ppp_stats));
 642     if (ppp->slcomp)
 643       memcpy_tofs (&result->slhc,    ppp->slcomp, sizeof (struct slcompress));
 644   }
 645 
 646   return error;
 647 }
 648 #endif
 649 
 650 /*************************************************************
 651  * TTY OUTPUT
 652  *    The following function delivers a fully-formed PPP
 653  *    frame in ppp->xbuff to the TTY for output.
 654  *************************************************************/
 655 
 656 #ifdef NEW_TTY_DRIVERS
 657 static inline void
 658 #else
 659 static void
 660 #endif
 661 ppp_output_done (void *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
 662 {
 663   /* unlock the transmitter queue */
 664   ppp_unlock ((struct ppp *) ppp);
 665 
 666   /* If the device is still up then enable the transmitter of the
 667      next frame. */
 668   if (((struct ppp *) ppp)->dev->flags & IFF_UP)
 669 #ifndef NET02D
 670     mark_bh (NET_BH);
 671 #else
 672     dev_tint (((struct ppp *) ppp)->dev);
 673 #endif
 674 
 675   /* enable any blocked process pending transmission */
 676   wake_up_interruptible (&((struct ppp *) ppp)->write_wait);
 677 }
 678 
 679 #ifndef NEW_TTY_DRIVERS
 680 static void
 681 ppp_kick_tty (struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
 682 {
 683   register int count = ppp->xhead - ppp->xbuff;
 684   register int answer;
 685 
 686   ppp->stats.sbytes += count;
 687 
 688   answer = tty_write_data (ppp->tty,
 689                            ppp->xbuff,
 690                            count,
 691                            ppp_output_done,
 692                            (void *) ppp);
 693 
 694   if (answer == 0)
 695     ppp_output_done (ppp);   /* Should not happen */
 696   else
 697     if (answer < 0) {
 698       ppp->stats.serrors++;
 699       ppp_output_done (ppp); /* unlock the transmitter */
 700     }
 701 }
 702 
 703 #else
 704 
 705 static void
 706 ppp_kick_tty (struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
 707 {
 708         register int count, actual;
 709         
 710         count = ppp->xhead - ppp->xbuff;
 711         
 712         actual = ppp->tty->driver.write(ppp->tty, 0, ppp->xbuff, count);
 713         ppp->stats.sbytes += actual;
 714         if (actual == count) {
 715                 ppp_output_done(ppp);
 716         } else {
 717                 ppp->xtail = ppp->xbuff + actual;
 718                 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
 719         }
 720 }
 721 
 722 static void ppp_write_wakeup(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 723 {
 724         register int count, actual;
 725         struct ppp *ppp = ppp_find(tty);
 726 
 727         if (!ppp || ppp->magic != PPP_MAGIC) {
 728                 PRINTKN (1,
 729                          (KERN_ERR "PPP: write_wakeup called but couldn't "
 730                           "find PPP struct.\n"));
 731                 return;
 732         }
 733 
 734         if (!ppp->xtail)
 735                 return;
 736 
 737         cli();
 738         if (ppp->flags & SC_XMIT_BUSY) {
 739                 sti();
 740                 return;
 741         }
 742         ppp->flags |= SC_XMIT_BUSY;
 743         sti();
 744         
 745         count = ppp->xhead - ppp->xtail;
 746         
 747         actual = tty->driver.write(tty, 0, ppp->xtail, count);
 748         ppp->stats.sbytes += actual;
 749         if (actual == count) {
 750                 ppp->xtail = 0;
 751                 tty->flags &= ~TTY_DO_WRITE_WAKEUP;
 752 
 753                 ppp_output_done(ppp);
 754         } else {
 755                 ppp->xtail += actual;
 756         }
 757         ppp->flags &= ~SC_XMIT_BUSY;
 758 }
 759 #endif
 760 
 761 /*************************************************************
 762  * TTY INPUT
 763  *    The following functions handle input that arrives from
 764  *    the TTY.  It recognizes PPP frames and either hands them
 765  *    to the network layer or queues them for delivery to a
 766  *    user process reading this TTY.
 767  *************************************************************/
 768 
 769 /* stuff a single character into the receive buffer */
 770 
 771 static inline void
 772 ppp_enqueue(struct ppp *ppp, unsigned char c)
     /* [previous][next][first][last][top][bottom][index][help] */
 773 {
 774   unsigned long flags;
 775 
 776   save_flags(flags);
 777   cli();
 778   if (ppp->rhead < ppp->rend) {
 779     *ppp->rhead = c;
 780     ppp->rhead++;
 781     ppp->rcount++;
 782   } else
 783     ppp->stats.roverrun++;
 784   restore_flags(flags);
 785 }
 786 
 787 #ifdef CHECK_CHARACTERS
 788 static unsigned paritytab[8] = {
 789     0x96696996, 0x69969669, 0x69969669, 0x96696996,
 790     0x69969669, 0x96696996, 0x96696996, 0x69969669
 791 };
 792 #endif
 793 
 794 #ifndef NEW_TTY_DRIVERS
 795 static void
 796 ppp_dump_inqueue(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 797 {
 798   int  head = tty->read_q.head,
 799        tail = tty->read_q.tail,
 800        i, count;
 801   char buffer[8];
 802 
 803   PRINTK ((KERN_DEBUG "INQUEUE: head %d tail %d imode %x:\n", head, tail, 
 804            (unsigned int) tty->termios->c_iflag))
 805 
 806   i     = tail;
 807   count = 0;
 808 
 809   while (i != head) {
 810     buffer [count] = tty->read_q.buf[i];
 811     if (++count == 8) {
 812       ppp_print_buffer (NULL, buffer, 8, KERNEL_DS);
 813       count = 0;
 814     }
 815     i = (i + 1) & (TTY_BUF_SIZE - 1);
 816   }
 817   ppp_print_buffer (NULL, buffer, count, KERNEL_DS);
 818 }
 819 
 820 /* called by lower levels of TTY driver when data becomes available.
 821    all incoming data comes through this function. */
 822 
 823 void ppp_tty_input_ready(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 824 {
 825   struct ppp *ppp = ppp_find(tty);
 826   int n, error;
 827   unsigned char buff[128];
 828 
 829 /*  PRINTK( (KERN_DEBUG "PPP: handler called.\n") ) */
 830   if (!ppp || ppp->magic != PPP_MAGIC) {
 831     PRINTKN (1,
 832              (KERN_ERR "PPP: handler called but couldn't find PPP struct.\n"));
 833     return;
 834   }
 835 
 836   CHECK_PPP_VOID();
 837 
 838   /* ZZZ */
 839   if (ppp_debug >= 5)
 840     ppp_dump_inqueue(ppp->tty);
 841 
 842   do {
 843     n = tty_read_raw_data(tty, buff, 128);
 844     if ( n == 0 )               /* nothing there */
 845       break;
 846 
 847     if (ppp_debug >= 5)
 848       ppp_print_buffer ("receive buffer", buff, n > 0 ? n : -n, KERNEL_DS);
 849 
 850     if ( n < 0 ) {
 851       /* Last character is error flag.
 852          Process the previous characters, then set toss flag. */
 853       n = (-n) - 1;
 854       error = buff[n];
 855     } else error = 0;
 856     ppp->stats.rbytes += n;
 857     ppp_unesc(ppp,buff,n);
 858     if (error)
 859       ppp->toss = error;
 860   } while (1);
 861 }
 862 
 863 /* recover frame by undoing PPP escape mechanism;
 864    copies N chars of input data from C into PPP->rbuff
 865    calls ppp_doframe to dispose of any frames it finds
 866 */
 867 
 868 static void
 869 ppp_unesc(struct ppp *ppp, unsigned char *c, int n)
     /* [previous][next][first][last][top][bottom][index][help] */
 870 {
 871   int i;
 872 
 873   for (i = 0; i < n; i++, c++) {
 874     PRINTKN (6,(KERN_DEBUG "(%x)", (unsigned int) *c));
 875 
 876 #ifdef CHECK_CHARACTERS
 877     if (*c & 0x80)
 878         sc->sc_flags |= SC_RCV_B7_1;
 879     else
 880         sc->sc_flags |= SC_RCV_B7_0;
 881 
 882     if (paritytab[*c >> 5] & (1 << (*c & 0x1F)))
 883         sc->sc_flags |= SC_RCV_ODDP;
 884     else
 885         sc->sc_flags |= SC_RCV_EVNP;
 886 #endif
 887 
 888     switch (*c) {
 889     case PPP_ESC:               /* PPP_ESC: invert 0x20 in next character */
 890       ppp->escape = PPP_TRANS;
 891       break;
 892 
 893     case PPP_FLAG:              /* PPP_FLAG: end of frame */
 894       if (ppp->escape)          /* PPP_ESC just before PPP_FLAG is illegal */
 895         ppp->toss = 0xFF;
 896 
 897       if ((ppp->toss & 0x80) == 0)
 898         ppp_doframe(ppp);       /* pass frame on to next layers */
 899 
 900       ppp->rcount = 0;
 901       ppp->rhead  = ppp->rbuff;
 902       ppp->escape = 0;
 903       ppp->toss   = 0;
 904       break;
 905 
 906     default:                    /* regular character */
 907       if (!in_rmap (ppp, *c)) {
 908         if (ppp->toss == 0)
 909           ppp_enqueue (ppp, *c ^ ppp->escape);
 910         ppp->escape = 0;
 911       }
 912       break;
 913     }
 914   }
 915 }
 916 
 917 #else
 918 static int ppp_receive_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 919 {
 920         return 65536;  /* We can handle an infinite amount of data. :-) */
 921 }
 922 
 923 
 924 static void ppp_receive_buf(struct tty_struct *tty, unsigned char *cp,
     /* [previous][next][first][last][top][bottom][index][help] */
 925                             char *fp, int count)
 926 {
 927   register struct ppp *ppp = ppp_find (tty);
 928   unsigned char c;
 929  
 930 /*  PRINTK( ("PPP: handler called.\n") ); */
 931 
 932   if (!ppp || ppp->magic != PPP_MAGIC) {
 933     PRINTKN (1,("PPP: handler called but couldn't find "
 934                 "PPP struct.\n"));
 935     return;
 936   }
 937 
 938   CHECK_PPP_VOID();
 939  
 940   if (ppp_debug >= 5) {
 941     ppp_print_buffer ("receive buffer", cp, count, KERNEL_DS);
 942   }
 943 
 944   ppp->stats.rbytes += count;
 945  
 946   while (count-- > 0) {
 947     c = *cp++;
 948 
 949     if (fp) {
 950       if (*fp && ppp->toss == 0)
 951         ppp->toss = *fp;
 952       fp++;
 953     }
 954 
 955 #ifdef CHECK_CHARACTERS
 956     if (c & 0x80)
 957         ppp->flags |= SC_RCV_B7_1;
 958     else
 959         ppp->flags |= SC_RCV_B7_0;
 960 
 961     if (paritytab[c >> 5] & (1 << (c & 0x1F)))
 962         ppp->flags |= SC_RCV_ODDP;
 963     else
 964         ppp->flags |= SC_RCV_EVNP;
 965 #endif
 966 
 967     switch (c) {
 968     case PPP_ESC:               /* PPP_ESC: invert 0x20 in next character */
 969       ppp->escape = PPP_TRANS;
 970       break;
 971 
 972     case PPP_FLAG:              /* PPP_FLAG: end of frame */
 973       if (ppp->escape)          /* PPP_ESC just before PPP_FLAG is "cancel"*/
 974         ppp->toss = 0xFF;
 975 
 976       if ((ppp->toss & 0x80) == 0)
 977         ppp_doframe(ppp);       /* pass frame on to next layers */
 978 
 979       ppp->rcount = 0;
 980       ppp->rhead  = ppp->rbuff;
 981       ppp->escape = 0;
 982       ppp->toss   = 0;
 983       break;
 984 
 985     default:                    /* regular character */
 986       if (!in_rmap (ppp, c)) {
 987         if (ppp->toss == 0)
 988           ppp_enqueue (ppp, c ^ ppp->escape);
 989         ppp->escape = 0;
 990       }
 991     }
 992   }
 993 }
 994 #endif
 995 
 996 /* on entry, a received frame is in ppp->rbuff
 997    check it and dispose as appropriate */
 998 static void
 999 ppp_doframe(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1000 {
1001   u_char *c = ppp->rbuff;
1002   u_short proto;
1003   int count = ppp->rcount;
1004 
1005   /* forget it if we've already noticed an error */
1006   if (ppp->toss) {
1007     PRINTKN (1, (KERN_WARNING "ppp_toss: tossing frame, reason = %d\n",
1008                  ppp->toss));
1009     slhc_toss (ppp->slcomp);
1010     ppp->stats.rerrors++;
1011     return;
1012   }
1013 
1014   /* do this before printing buffer to avoid generating copious output */
1015   if (count == 0)
1016     return;
1017 
1018   if (ppp_debug >= 3)
1019     ppp_print_buffer ("receive frame", c, count, KERNEL_DS);
1020 
1021   if (count < 4) {
1022     PRINTKN (1,(KERN_WARNING "ppp: got runt ppp frame, %d chars\n", count));
1023     slhc_toss (ppp->slcomp);
1024     ppp->stats.runts++;
1025     return;
1026   }
1027 
1028   /* check PPP error detection field */
1029   if (!ppp_check_fcs(ppp)) {
1030     PRINTKN (1,(KERN_WARNING "ppp: frame with bad fcs\n"));
1031     slhc_toss (ppp->slcomp);
1032     ppp->stats.rerrors++;
1033     return;
1034   }
1035 
1036   count -= 2;                   /* ignore last two characters */
1037 
1038   /* now we have a good frame */
1039   /* figure out the protocol field */
1040   if ((c[0] == PPP_ADDRESS) && (c[1] == PPP_CONTROL)) {
1041     c = c + 2;                  /* ADDR/CTRL not compressed, so skip */
1042     count -= 2;
1043   }
1044 
1045   proto = (u_short) *c++;               /* PROTO compressed */
1046   if (proto & 1) {
1047     count--;
1048   } else {
1049     proto = (proto << 8) | (u_short) *c++; /* PROTO uncompressed */
1050     count -= 2;
1051   }
1052 
1053   /* Send the frame to the network if the ppp device is up */
1054   if ((ppp->dev->flags & IFF_UP) && ppp_do_ip(ppp, proto, c, count)) {
1055     ppp->ddinfo.ip_rjiffies = jiffies;
1056     return;
1057   }
1058 
1059   /* If we got here, it has to go to a user process doing a read,
1060      so queue it.
1061 
1062      User process expects to get whole frame (for some reason), so
1063      use count+2 so as to include FCS field. */
1064 
1065   if (ppp_us_queue (ppp, proto, c, count+2)) {
1066     ppp->ddinfo.nip_rjiffies = jiffies;
1067     ppp->stats.rothers++;
1068     return;
1069   }
1070 
1071   /* couldn't cope. */
1072   PRINTKN (1,(KERN_WARNING
1073               "ppp: dropping packet on the floor: nobody could take it.\n"));
1074   slhc_toss (ppp->slcomp);
1075   ppp->stats.tossed++;
1076 }
1077 
1078 /* Examine packet at C, attempt to pass up to net layer. 
1079    PROTO is the protocol field from the PPP frame.
1080    Return 1 if could handle it, 0 otherwise.  */
1081 
1082 static int
1083 ppp_do_ip (struct ppp *ppp, unsigned short proto, unsigned char *c,
     /* [previous][next][first][last][top][bottom][index][help] */
1084           int count)
1085 {
1086   int flags, done;
1087 
1088   PRINTKN (4,(KERN_DEBUG "ppp_do_ip: proto %x len %d first byte %x\n",
1089               (int) proto, count, c[0]));
1090 
1091   if (ppp_debug_netpackets) {
1092     PRINTK (("KERN_DEBUG %s <-- proto %x len %d\n", ppp->dev->name,
1093              (int) proto, count));
1094   }
1095     
1096   if (proto == PROTO_IP) {
1097     ppp->stats.runcomp++;
1098     goto sendit;
1099   }
1100 
1101   if ((proto == PROTO_VJCOMP) && !(ppp->flags & SC_REJ_COMP_TCP)) {
1102     /* get space for uncompressing the header */
1103     done = 0;
1104     save_flags (flags);
1105     cli();
1106     if ((ppp->rhead + 80) < ppp->rend) {
1107       ppp->rhead += 80;
1108       ppp->rcount += 80;
1109       done = 1;
1110     }
1111     restore_flags(flags);
1112 
1113     if (! done) {
1114       PRINTKN (1,(KERN_NOTICE
1115                   "ppp: no space to decompress VJ compressed TCP header.\n"));
1116       ppp->stats.roverrun++;
1117       slhc_toss (ppp->slcomp);
1118       return 1;
1119     }
1120 
1121     count = slhc_uncompress(ppp->slcomp, c, count);
1122     if (count <= 0) {
1123       ppp->stats.rerrors++;
1124       PRINTKN (1,(KERN_NOTICE "ppp: error in VJ decompression\n"));
1125       slhc_toss (ppp->slcomp);
1126       return 1;
1127     }
1128     ppp->stats.rcomp++;
1129     goto sendit;
1130   }
1131   
1132   if ((proto == PROTO_VJUNCOMP) && !(ppp->flags & SC_REJ_COMP_TCP)) {
1133     if (slhc_remember(ppp->slcomp, c, count) <= 0) {
1134       ppp->stats.rerrors++;
1135       PRINTKN (1,(KERN_NOTICE "ppp: error in VJ memorizing\n"));
1136       slhc_toss (ppp->slcomp);
1137       return 1;
1138     }
1139     ppp->stats.runcomp++;
1140     goto sendit;
1141   }
1142 
1143   /* not ours */
1144   return 0;
1145 
1146  sendit:
1147   if (ppp_debug_netpackets) {
1148     struct iphdr *iph = (struct iphdr *) c;
1149     PRINTK ((KERN_INFO "%s <--    src %lx dst %lx len %d\n", ppp->dev->name, 
1150              iph->saddr, iph->daddr, count))
1151   }
1152 
1153   /* receive the frame through the network software */
1154   (void) dev_rint (c, count, 0, ppp->dev);
1155   return 1;
1156 }
1157 
1158 /* stuff packet at BUF, length LEN, into the us_rbuff buffer
1159    prepend PROTO information */
1160 
1161 #define PUTC(c,label) *ppp->us_rbuff_head++ = c; \
1162                 if (ppp->us_rbuff_head == ppp->us_rbuff_end) \
1163                      ppp->us_rbuff_head = ppp->us_rbuff; \
1164                 if (ppp->us_rbuff_head == ppp->us_rbuff_tail) \
1165                      goto label;
1166 #define GETC(c) c = *ppp->us_rbuff_tail++; \
1167                 if (ppp->us_rbuff_tail == ppp->us_rbuff_end) \
1168                      ppp->us_rbuff_tail = ppp->us_rbuff;
1169 
1170 static int
1171 ppp_us_queue(struct ppp *ppp, unsigned short proto, 
     /* [previous][next][first][last][top][bottom][index][help] */
1172              unsigned char *buf, int len)
1173 {
1174   int totlen;
1175   unsigned char *saved_head;
1176 
1177   totlen = len+2;               /* including protocol */
1178 
1179   if (set_bit(1, &ppp->us_rbuff_lock)) {
1180     PRINTKN (1, (KERN_NOTICE "ppp_us_queue: can't get lock\n"));
1181     return 0;
1182   }
1183   saved_head = ppp->us_rbuff_head;
1184 
1185   PUTC((totlen & 0xff00) >> 8, failure);
1186   PUTC(totlen & 0x00ff, failure);
1187   PUTC((proto & 0xff00) >> 8, failure);
1188   PUTC(proto & 0x00ff, failure);
1189 
1190   while (len-- > 0) {
1191     PUTC(*buf++, failure);
1192   }
1193 
1194   PRINTKN (3, (KERN_INFO "ppp: successfully queued %d bytes\n", totlen));
1195   clear_bit(1, &ppp->us_rbuff_lock);
1196   wake_up_interruptible (&ppp->read_wait);
1197 
1198 #ifdef NEW_TTY_DRIVERS
1199   kill_fasync(ppp->tty->fasync, SIGIO);
1200 #endif
1201 
1202   if (ppp->inp_sig && ppp->inp_sig_pid)
1203     if (kill_proc (ppp->inp_sig_pid, ppp->inp_sig, 1) != 0) {
1204       /* process is gone */
1205       PRINTKN (2,(KERN_NOTICE
1206                   "ppp: process that requested notification is gone\n"));
1207       ppp->inp_sig = 0;
1208       ppp->inp_sig_pid = 0;
1209     }
1210   return 1;
1211 
1212  failure:
1213   ppp->us_rbuff_head = saved_head;
1214   clear_bit(1, &ppp->us_rbuff_lock);
1215 
1216   PRINTKN (1, (KERN_NOTICE "ppp_us_queue: ran out of buffer space.\n"));
1217 
1218   return 0;
1219 }
1220 
1221 /*************************************************************
1222  * LINE DISCIPLINE SUPPORT
1223  *    The following functions form support user programs
1224  *    which read and write data on a TTY with the PPP line
1225  *    discipline.  Reading is done from a circular queue,
1226  *    filled by the lower TTY levels.
1227  *************************************************************/
1228 
1229 /* read a PPP frame from the us_rbuff circular buffer, 
1230    waiting if necessary
1231 */
1232 
1233 static int
1234 ppp_read(struct tty_struct *tty, struct file *file, unsigned char *buf, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
1235 {
1236   struct ppp *ppp = ppp_find(tty);
1237   unsigned char c;
1238   int len, i;
1239 
1240   if (!ppp || ppp->magic != PPP_MAGIC) {
1241     PRINTKN (1,(KERN_ERR "ppp_read: cannot find ppp channel\n"));
1242     return -EIO;
1243   }
1244 
1245   CHECK_PPP(-ENXIO);
1246 
1247   PRINTKN (4,(KERN_DEBUG "ppp_read: called %x num %u\n",
1248               (unsigned int) buf,
1249               nr));
1250 
1251   do {
1252     /* try to acquire read lock */
1253     if (set_bit(0, &ppp->us_rbuff_lock) == 0) {
1254       /* got lock */
1255       if (ppp->us_rbuff_head == ppp->us_rbuff_tail) {
1256         /* no data */
1257         PRINTKN (4,(KERN_DEBUG "ppp_read: no data\n"));
1258         clear_bit(0, &ppp->us_rbuff_lock);
1259         if (ppp->inp_sig) {
1260           PRINTKN (4,(KERN_DEBUG "ppp_read: EWOULDBLOCK\n"));
1261           return -EWOULDBLOCK;
1262         } else goto wait;
1263       }
1264 
1265       /* reset the time of the last read operation */
1266       ppp->ddinfo.nip_rjiffies = jiffies;
1267 
1268       GETC (c); len = c << 8; GETC (c); len += c;
1269 
1270       PRINTKN (4,(KERN_DEBUG "ppp_read: len = %d\n", len));
1271 
1272       if (len + 2 > nr) {
1273         /* frame too big; can't copy it, but do update us_rbuff_head */
1274         PRINTKN (1,(KERN_DEBUG
1275                     "ppp: read of %u bytes too small for %d frame\n",
1276                     nr, len+2));
1277         ppp->us_rbuff_head += len;
1278         if (ppp->us_rbuff_head > ppp->us_rbuff_end)
1279           ppp->us_rbuff_head += - (ppp->us_rbuff_end - ppp->us_rbuff);
1280         clear_bit(0, &ppp->us_rbuff_lock);
1281         wake_up_interruptible (&ppp->read_wait);
1282         ppp->stats.rgiants++;
1283         return -EOVERFLOW;              /* ZZZ; HACK! */
1284       } else {
1285         /* have the space: copy the packet, faking the first two bytes */
1286         put_fs_byte (PPP_ADDRESS, buf++);
1287         put_fs_byte (PPP_CONTROL, buf++);
1288         i = len;
1289         while (i-- > 0) {
1290           GETC (c);
1291           put_fs_byte (c, buf++);
1292         }
1293       }
1294 
1295       clear_bit(0, &ppp->us_rbuff_lock);
1296       PRINTKN (3,(KERN_DEBUG "ppp_read: passing %d bytes up\n", len + 2));
1297       ppp->stats.rothers++;
1298       return len + 2;
1299     }
1300 
1301     /* need to wait */
1302   wait:
1303     current->timeout = 0;
1304     PRINTKN (3,(KERN_DEBUG "ppp_read: sleeping\n"));
1305     interruptible_sleep_on (&ppp->read_wait);
1306     if (current->signal & ~current->blocked)
1307       return -EINTR;
1308   } while (1);
1309 }
1310 
1311 /* stuff a character into the transmit buffer, using PPP's way of escaping
1312    special characters.
1313    also, update ppp->fcs to take account of new character */
1314 static inline void
1315 ppp_stuff_char(struct ppp *ppp, unsigned char c)
     /* [previous][next][first][last][top][bottom][index][help] */
1316 {
1317   int curpt = ppp->xhead - ppp->xbuff;
1318   if ((curpt < 0) || (curpt > 3000)) {
1319     PRINTK ((KERN_DEBUG "ppp_stuff_char: %x %x %d\n",
1320              (unsigned int) ppp->xbuff, (unsigned int) ppp->xhead, curpt))
1321   }
1322   if (in_xmap (ppp, c)) {
1323     *ppp->xhead++ = PPP_ESC;
1324     *ppp->xhead++ = c ^ PPP_TRANS;
1325   } else
1326     *ppp->xhead++ = c;
1327   ppp->fcs = (ppp->fcs >> 8) ^ fcstab[(ppp->fcs ^ c) & 0xff];
1328 }
1329 
1330 /* write a frame with NR chars from BUF to TTY
1331    we have to put the FCS field on ourselves
1332 */
1333 
1334 static int
1335 ppp_write(struct tty_struct *tty, struct file *file, unsigned char *buf, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
1336 {
1337   struct ppp *ppp = ppp_find(tty);
1338   int i;
1339 
1340   if (!ppp || ppp->magic != PPP_MAGIC) {
1341     PRINTKN (1,(KERN_ERR "ppp_write: cannot find ppp unit\n"));
1342     return -EIO;
1343   }
1344 
1345   CHECK_PPP(-ENXIO);
1346   
1347   if (ppp->mtu != ppp->dev->mtu)        /* Someone has been ifconfigging */
1348     ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru);
1349 
1350   if (nr > ppp->mtu) {
1351     PRINTKN (1,(KERN_WARNING
1352                 "ppp_write: truncating user packet from %u to mtu %d\n",
1353                 nr, ppp->mtu));
1354     nr = ppp->mtu;
1355   }
1356 
1357   if (ppp_debug >= 3)
1358     ppp_print_buffer ("write frame", buf, nr, USER_DS);
1359 
1360   /* lock this PPP unit so we will be the only writer;
1361      sleep if necessary */
1362   while ((ppp->sending == 1) || !ppp_lock(ppp)) {
1363     current->timeout = 0;
1364     PRINTKN (3,(KERN_DEBUG "ppp_write: sleeping\n"));
1365     interruptible_sleep_on(&ppp->write_wait);
1366     if (current->signal & ~current->blocked)
1367       return -EINTR;
1368   }
1369 
1370   /* OK, locked.  Stuff the given bytes into the buffer. */
1371 
1372   PRINTKN(4,(KERN_DEBUG "ppp_write: acquired write lock\n"));
1373   ppp->xhead = ppp->xbuff;
1374 
1375 #ifdef OPTIMIZE_FLAG_TIME
1376   if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1377     *ppp->xhead++ = PPP_FLAG;
1378   ppp->last_xmit = jiffies;
1379 #else      
1380   *ppp->xhead++ = PPP_FLAG;
1381 #endif
1382 
1383   ppp->fcs = PPP_FCS_INIT;
1384   i = nr;
1385   while (i-- > 0)
1386     ppp_stuff_char(ppp,get_fs_byte(buf++));
1387 
1388   ppp_add_fcs(ppp);             /* concatenate FCS at end */
1389 
1390   *ppp->xhead++ = PPP_FLAG;
1391   
1392   /* reset the time of the last write operation */
1393   ppp->ddinfo.nip_sjiffies = jiffies;
1394 
1395   if (ppp_debug >= 6)
1396     ppp_print_buffer ("xmit buffer", ppp->xbuff, ppp->xhead - ppp->xbuff, KERNEL_DS);
1397   else {
1398     PRINTKN (4,(KERN_DEBUG
1399                 "ppp_write: writing %d chars\n", ppp->xhead - ppp->xbuff));
1400   }
1401 
1402   /* packet is ready-to-go */
1403   ++ppp->stats.sothers;
1404   ppp_kick_tty(ppp);
1405 
1406   return((int)nr);
1407 }
1408  
1409 static int
1410 ppp_ioctl(struct tty_struct *tty, struct file *file, unsigned int i,
     /* [previous][next][first][last][top][bottom][index][help] */
1411           unsigned long l)
1412 {
1413   struct ppp *ppp = ppp_find(tty);
1414   register int temp_i = 0;
1415   int error;
1416 
1417   if (!ppp || ppp->magic != PPP_MAGIC) {
1418     PRINTK ((KERN_ERR "ppp_ioctl: can't find PPP block from tty!\n"))
1419     return -EBADF;
1420   }
1421 
1422   CHECK_PPP(-ENXIO);
1423 
1424   /* This must be root user */
1425   if (!suser())
1426     return -EPERM;
1427 
1428   switch (i) {
1429   case PPPIOCSMRU:
1430     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1431     if (error == 0) {
1432       temp_i = (int) get_fs_long (l);
1433       PRINTKN (3,(KERN_INFO "ppp_ioctl: set mru to %d\n", temp_i));
1434       if (ppp->mru != temp_i)
1435         ppp_changedmtu (ppp, ppp->dev->mtu, temp_i);
1436     }
1437     break;
1438 
1439   case PPPIOCGFLAGS:
1440     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1441     if (error == 0) {
1442       temp_i = (ppp->flags & SC_MASK);
1443 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
1444       temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP;
1445 #endif
1446       put_fs_long ((long) temp_i, l);
1447       PRINTKN (3,(KERN_DEBUG "ppp_ioctl: get flags: addr %lx flags %x\n",
1448                   l,
1449                   temp_i));
1450     }
1451     break;
1452 
1453   case PPPIOCSFLAGS:
1454     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1455     if (error == 0) {
1456       temp_i      = (int) get_fs_long (l);
1457       ppp->flags ^= ((ppp->flags ^ temp_i) & SC_MASK);
1458       PRINTKN (3,(KERN_INFO "ppp_ioctl: set flags to %x\n", temp_i));
1459     }
1460     break;
1461 
1462   case PPPIOCGASYNCMAP:
1463     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1464     if (error == 0) {
1465       put_fs_long (ppp->xmit_async_map[0], l);
1466       PRINTKN (3,(KERN_INFO "ppp_ioctl: get asyncmap: addr %lx asyncmap %lx\n",
1467                   l, ppp->xmit_async_map[0]));
1468     }
1469     break;
1470 
1471   case PPPIOCSASYNCMAP:
1472     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1473     if (error == 0) {
1474       ppp->xmit_async_map[0] = get_fs_long (l);
1475       bset (ppp->xmit_async_map, PPP_FLAG);
1476       bset (ppp->xmit_async_map, PPP_ESC);
1477       PRINTKN (3,(KERN_INFO "ppp_ioctl: set xmit asyncmap %lx\n",
1478                   ppp->xmit_async_map[0]));
1479     }
1480     break;
1481 
1482   case PPPIOCRASYNCMAP:
1483     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1484     if (error == 0) {
1485       ppp->recv_async_map = get_fs_long (l);
1486       PRINTKN (3,(KERN_INFO "ppp_ioctl: set recv asyncmap %lx\n",
1487                   ppp->recv_async_map));
1488     }
1489     break;
1490 
1491   case PPPIOCGUNIT:
1492     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1493     if (error == 0) {
1494       put_fs_long (ppp->dev->base_addr, l);
1495       PRINTKN (3,(KERN_INFO "ppp_ioctl: get unit: %ld", ppp->dev->base_addr));
1496     }
1497     break;
1498 
1499   case PPPIOCSINPSIG:
1500     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1501     if (error == 0) {
1502       ppp->inp_sig     = (int) get_fs_long (l);
1503       ppp->inp_sig_pid = current->pid;
1504       PRINTKN (3,(KERN_INFO "ppp_ioctl: set input signal %d\n", ppp->inp_sig));
1505     }
1506     break;
1507 
1508   case PPPIOCSDEBUG:
1509     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1510     if (error == 0) {
1511       ppp_debug = (int) get_fs_long (l);
1512       ppp_debug_netpackets = (ppp_debug & 0xff00) >> 8;
1513       ppp_debug &= 0xff;
1514       PRINTKN (1, (KERN_INFO "ppp_ioctl: set debug level %d, netpacket %d\n", 
1515                    ppp_debug, ppp_debug_netpackets));
1516     }
1517     break;
1518 
1519   case PPPIOCGDEBUG:
1520     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1521     if (error == 0) {
1522       put_fs_long ((long) (ppp_debug | (ppp_debug_netpackets << 8)), l);
1523       PRINTKN (3,(KERN_INFO "ppp_ioctl: get debug level %d\n", 
1524                   ppp_debug | (ppp_debug_netpackets << 8)));
1525     }
1526     break;
1527 
1528   case PPPIOCGSTAT:
1529     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (struct ppp_stats));
1530     if (error == 0) {
1531       memcpy_tofs ((void *) l, &ppp->stats, sizeof (struct ppp_stats));
1532       PRINTKN (3,(KERN_INFO "ppp_ioctl: read statistics\n"));
1533     }
1534     break;
1535 
1536   case PPPIOCGTIME:
1537     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (struct ppp_ddinfo));
1538     if (error == 0) {
1539       struct ppp_ddinfo cur_ddinfo;
1540       unsigned long cur_jiffies = jiffies;
1541 
1542       /* change absolute times to relative times. */
1543       cur_ddinfo.ip_sjiffies  = cur_jiffies - ppp->ddinfo.ip_sjiffies;
1544       cur_ddinfo.ip_rjiffies  = cur_jiffies - ppp->ddinfo.ip_rjiffies;
1545       cur_ddinfo.nip_sjiffies = cur_jiffies - ppp->ddinfo.nip_sjiffies;
1546       cur_ddinfo.nip_rjiffies = cur_jiffies - ppp->ddinfo.nip_rjiffies;
1547       
1548       memcpy_tofs ((void *) l, &cur_ddinfo, sizeof (struct ppp_ddinfo));
1549       PRINTKN (3,(KERN_INFO "ppp_ioctl: read demand dial info\n"));
1550     }
1551     break;
1552 
1553   case PPPIOCGXASYNCMAP:
1554     error = verify_area (VERIFY_WRITE,
1555                          (void *) l,
1556                          sizeof (ppp->xmit_async_map));
1557     if (error == 0) {
1558       memcpy_tofs ((void *) l,
1559                    ppp->xmit_async_map,
1560                    sizeof (ppp->xmit_async_map));
1561       PRINTKN (3,(KERN_INFO "ppp_ioctl: get xasyncmap: addr %lx\n", l));
1562     }
1563     break;
1564 
1565   case PPPIOCSXASYNCMAP:
1566     error = verify_area (VERIFY_READ, (void *) l,
1567                          sizeof (ppp->xmit_async_map));
1568     if (error == 0) {
1569       unsigned long temp_tbl [8];
1570 
1571       memcpy_fromfs (temp_tbl, (void *) l, sizeof (ppp->xmit_async_map));
1572       temp_tbl[1]  =  0x00000000; /* must not escape 0x20 - 0x3f */
1573       temp_tbl[2] &= ~0x40000000; /* must not escape 0x5e        */
1574       temp_tbl[3] |=  0x60000000; /* must escape 0x7d and 0x7e   */
1575 
1576       if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
1577           (temp_tbl[4] & temp_tbl[5]) != 0 ||
1578           (temp_tbl[6] & temp_tbl[7]) != 0)
1579         error = -EINVAL;
1580       else {
1581         memcpy (ppp->xmit_async_map, temp_tbl, sizeof (ppp->xmit_async_map));
1582         PRINTKN (3,(KERN_INFO "ppp_ioctl: set xasyncmap\n"));
1583       }
1584     }
1585     break;
1586 
1587   case PPPIOCSMAXCID:
1588     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1589     if (error == 0) {
1590       temp_i = (int) get_fs_long (l) + 1;
1591       PRINTKN (3,(KERN_INFO "ppp_ioctl: set maxcid to %d\n", temp_i));
1592       if (ppp->slcomp != NULL)
1593         slhc_free (ppp->slcomp);
1594 
1595       ppp->slcomp = slhc_init (temp_i, temp_i);
1596 
1597       if (ppp->slcomp == NULL) {
1598         PRINTKN (1,(KERN_ERR "ppp: no space for compression buffers!\n"));
1599         ppp_release (ppp);
1600         error = -ENOMEM;
1601       }
1602     }
1603     break;
1604 
1605 #ifdef NEW_TTY_DRIVERS
1606     /* Allow stty to read, but not set, the serial port */
1607   case TCGETS:
1608   case TCGETA:
1609     error = n_tty_ioctl(tty, file, i, l);
1610     break;
1611 #endif
1612 
1613 /*
1614  *  All other ioctl() events will come here.
1615  */
1616 
1617   default:
1618     PRINTKN (1,(KERN_ERR "ppp_ioctl: invalid ioctl: %x, addr %lx\n",
1619                 i,
1620                 l));
1621 #ifdef NEW_TTY_DRIVERS
1622     error = -ENOIOCTLCMD;
1623 #else
1624     error = -EINVAL;
1625 #endif
1626     break;
1627   }
1628   return error;
1629 }
1630 
1631 static int
1632 ppp_select (struct tty_struct *tty, struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1633             struct file * filp, int sel_type, select_table * wait)
1634 {
1635   struct ppp *ppp = ppp_find (tty);
1636   
1637   if (!ppp || ppp->magic != PPP_MAGIC) {
1638     PRINTK ((KERN_ERR "ppp_select: can't find PPP block from tty!\n"))
1639     return -EBADF;
1640   }
1641   
1642   /* If the PPP protocol is no longer active, return false */
1643   CHECK_PPP (0);
1644   
1645   /* Process the request based upon the type desired */
1646   switch (sel_type) {
1647   case SEL_IN:
1648     if (set_bit(0, &ppp->us_rbuff_lock) == 0) {
1649       /* Test for the presence of data in the queue */
1650       if (ppp->us_rbuff_head != ppp->us_rbuff_tail) {
1651         clear_bit (0, &ppp->us_rbuff_lock);
1652         return 1;
1653       }
1654       clear_bit (0, &ppp->us_rbuff_lock);
1655     } /* fall through */
1656 
1657   case SEL_EX:
1658     /* Is there a pending error condition? */
1659     if (tty->packet && tty->link->ctrl_status)
1660       return 1;
1661     
1662     /* closed? */
1663     if (tty->flags & (1 << TTY_SLAVE_CLOSED))
1664       return 1;
1665     
1666     /* If the tty is disconnected, then this is an exception too */
1667     if (tty_hung_up_p(filp))
1668       return 1;
1669 
1670     select_wait (&ppp->read_wait, wait);
1671     break;
1672     
1673   case SEL_OUT:
1674     if (ppp_lock (ppp)) {
1675       if (ppp->sending == 0) {
1676         ppp_unlock (ppp);
1677         return 1;
1678       }
1679       ppp_unlock (ppp);
1680     }
1681     select_wait (&ppp->write_wait, wait);
1682     break;
1683   }
1684   return 0;
1685 }
1686 
1687 /*************************************************************
1688  * NETWORK OUTPUT
1689  *    This routine accepts requests from the network layer
1690  *    and attempts to deliver the packets.
1691  *    It also includes various routines we are compelled to
1692  *    have to make the network layer work (arp, etc...).
1693  *************************************************************/
1694 
1695 int
1696 ppp_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1697 {
1698   struct tty_struct *tty;
1699   struct ppp *ppp;
1700   unsigned char *p;
1701   unsigned short proto;
1702   int len;
1703 
1704   /* just a little sanity check. */
1705   if (skb == NULL) {
1706     PRINTKN(3,(KERN_WARNING "ppp_xmit: null packet!\n"));
1707     return 0;
1708   }
1709 
1710   /* Get pointers to the various components */
1711   ppp   = &ppp_ctrl[dev->base_addr];
1712   tty   = ppp->tty;
1713   p     = (unsigned char *) (skb + 1);
1714   len   = skb->len;
1715   proto = PROTO_IP;
1716 
1717   PRINTKN(4,(KERN_DEBUG "ppp_xmit [%s]: skb %lX busy %d\n", dev->name, 
1718              (unsigned long int) skb, ppp->sending));
1719 
1720   /* avoid race conditions when the link fails */
1721   if (!ppp->inuse) {
1722     dev_kfree_skb(skb, FREE_WRITE);
1723     dev_close (dev);
1724     return 0;
1725   }
1726 
1727   if (tty == NULL) {
1728     PRINTKN(1,(KERN_ERR "ppp_xmit: %s not connected to a TTY!\n", dev->name));
1729     goto done;
1730   }
1731 
1732   if (!(dev->flags & IFF_UP)) {
1733     PRINTKN(1,(KERN_WARNING
1734                "ppp_xmit: packet sent on interface %s, which is down for IP\n",
1735                dev->name));
1736     goto done;
1737   }
1738 
1739   /* get length from IP header as per Alan Cox bugfix for slip.c */
1740   if (len < sizeof(struct iphdr)) {
1741     PRINTKN(0,(KERN_ERR "ppp_xmit: given runt packet, ignoring\n"));
1742     goto done;
1743   }
1744   len = ntohs( ((struct iphdr *)(skb->data)) -> tot_len );
1745 
1746   /* If doing demand dial then divert the first frame to pppd. */
1747   if (ppp->flags & SC_IP_DOWN) {
1748     if (ppp->flags & SC_IP_FLUSH == 0) {
1749       if (ppp_us_queue (ppp, proto, p, len))
1750         ppp->flags |= SC_IP_FLUSH;
1751     }
1752     goto done;
1753   }
1754 
1755   /* Attempt to acquire send lock */
1756   if (ppp->sending || !ppp_lock(ppp)) {
1757     PRINTKN(3,(KERN_WARNING "ppp_xmit: busy\n"));
1758     ppp->stats.sbusy++;
1759     return 1;
1760   }
1761 
1762   ppp->xhead = ppp->xbuff;
1763 
1764   /* try to compress, if VJ compression mode is on */
1765   if (ppp->flags & SC_COMP_TCP) {
1766     len = slhc_compress(ppp->slcomp, p, len, ppp->cbuff, &p, 
1767                         !(ppp->flags & SC_NO_TCP_CCID));
1768     if (p[0] & SL_TYPE_COMPRESSED_TCP)
1769       proto = PROTO_VJCOMP;
1770     else {
1771       if (p[0] >= SL_TYPE_UNCOMPRESSED_TCP) {
1772         proto = PROTO_VJUNCOMP;
1773         p[0] = (p[0] & 0x0f) | 0x40; 
1774       }
1775     }
1776   }
1777 
1778   /* increment appropriate counter */
1779   if (proto == PROTO_VJCOMP)
1780     ++ppp->stats.scomp;
1781   else
1782     ++ppp->stats.suncomp;
1783       
1784   if (ppp_debug_netpackets) {
1785     struct iphdr *iph = (struct iphdr *) (skb + 1);
1786     PRINTK ((KERN_DEBUG "%s ==> proto %x len %d src %x dst %x proto %d\n",
1787             dev->name, (int) proto, (int) len, (int) iph->saddr,
1788             (int) iph->daddr, (int) iph->protocol))
1789   }
1790 
1791   /* start of frame:   FLAG  ALL_STATIONS  CONTROL  <protohi> <protolo> */
1792 #ifdef OPTIMIZE_FLAG_TIME
1793   if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1794     *ppp->xhead++ = PPP_FLAG;
1795   ppp->last_xmit = jiffies;
1796 #else      
1797   *ppp->xhead++ = PPP_FLAG;
1798 #endif
1799 
1800   ppp->fcs = PPP_FCS_INIT;
1801   if (!(ppp->flags & SC_COMP_AC)) { 
1802     ppp_stuff_char(ppp, PPP_ADDRESS);
1803     ppp_stuff_char(ppp, PPP_CONTROL);
1804   }
1805 
1806   if (!(ppp->flags & SC_COMP_PROT) || (proto & 0xff00))
1807     ppp_stuff_char(ppp, proto>>8);
1808   ppp_stuff_char(ppp, proto&0xff);
1809 
1810   /* data part */
1811   while (len-- > 0)
1812     ppp_stuff_char(ppp, *p++);
1813 
1814   /* fcs and flag */
1815   ppp_add_fcs(ppp);
1816   *ppp->xhead++ = PPP_FLAG;
1817 
1818   /* update the time for demand dial function */
1819   ppp->ddinfo.ip_sjiffies = jiffies;
1820 
1821   /* send it! */
1822   if (ppp_debug >= 6)
1823     ppp_print_buffer ("xmit buffer", ppp->xbuff, ppp->xhead - ppp->xbuff, KERNEL_DS);
1824   else {
1825     PRINTKN (4,(KERN_DEBUG
1826                 "ppp_write: writing %d chars\n", ppp->xhead - ppp->xbuff));
1827   }
1828 
1829   ppp_kick_tty(ppp);
1830 
1831  done:
1832   dev_kfree_skb(skb, FREE_WRITE);
1833   return 0;
1834 }
1835   
1836 static unsigned short
1837 ppp_type_trans (struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1838 {
1839   return(htons(ETH_P_IP));
1840 }
1841 
1842 #ifdef NET02D
1843 static int
1844 ppp_header(unsigned char *buff, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
1845            unsigned long daddr, unsigned long saddr, unsigned len)
1846 {
1847   return(0);
1848 }
1849 
1850 static int
1851 ppp_rebuild_header(void *buff, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1852 {
1853   return(0);
1854 }
1855 
1856 static void
1857 ppp_add_arp(unsigned long addr, struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1858 {
1859 }
1860 
1861 #else
1862 
1863 static int
1864 ppp_header(unsigned char *buff, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
1865            void *daddr, void *saddr, unsigned len, struct sk_buff *skb)
1866 {
1867   return(0);
1868 }
1869 
1870 static int
1871 ppp_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1872                    struct sk_buff *skb)
1873 {
1874   return(0);
1875 }
1876 #endif
1877 
1878 static struct enet_statistics *
1879 ppp_get_stats (struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1880 {
1881   struct ppp *ppp = &ppp_ctrl[dev->base_addr];
1882   static struct enet_statistics ppp_stats;
1883 
1884   ppp_stats.rx_packets = ppp->stats.rcomp + ppp->stats.runcomp;
1885   ppp_stats.rx_errors = ppp->stats.rerrors;
1886   ppp_stats.rx_dropped = ppp->stats.tossed;
1887   ppp_stats.rx_fifo_errors = 0;
1888   ppp_stats.rx_length_errors = ppp->stats.runts;
1889   ppp_stats.rx_over_errors = ppp->stats.roverrun;
1890   ppp_stats.rx_crc_errors = 0;
1891   ppp_stats.rx_frame_errors = 0;
1892   ppp_stats.tx_packets = ppp->stats.scomp + ppp->stats.suncomp;
1893   ppp_stats.tx_errors = ppp->stats.serrors;
1894   ppp_stats.tx_dropped = 0;
1895   ppp_stats.tx_fifo_errors = 0;
1896   ppp_stats.collisions = ppp->stats.sbusy;
1897   ppp_stats.tx_carrier_errors = 0;
1898   ppp_stats.tx_aborted_errors = 0;
1899   ppp_stats.tx_window_errors = 0;
1900   ppp_stats.tx_heartbeat_errors = 0;
1901 
1902   PRINTKN (3, (KERN_INFO "ppp_get_stats called"));
1903   return &ppp_stats;
1904 }
1905 
1906 /*************************************************************
1907  * UTILITIES
1908  *    Miscellany called by various functions above.
1909  *************************************************************/
1910 
1911 #ifndef NEW_TTY_DRIVERS
1912 /* find a PPP channel given a TTY */
1913 struct ppp *
1914 ppp_find(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1915 {
1916   int i;
1917   for (i = 0; i < PPP_NRUNIT; i++)
1918     if (ppp_ctrl[i].inuse && (ppp_ctrl[i].tty == tty)) return &ppp_ctrl[i];
1919 
1920   return NULL;
1921 }
1922 #endif
1923 
1924 /* allocate a PPP channel */
1925 static struct ppp *
1926 ppp_alloc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1927 {
1928   int i;
1929   for (i = 0; i < PPP_NRUNIT; i++)
1930     if (!set_bit(0, &ppp_ctrl[i].inuse)) return &ppp_ctrl[i];
1931 
1932   return NULL;
1933 }
1934 
1935 /* marks a PPP interface 'busy'.  user processes will wait, if
1936    they try to write, and the network code will refrain from sending
1937    return nonzero if succeeded in acquiring lock
1938 */
1939 
1940 static int
1941 ppp_lock(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1942 {
1943   int flags, locked;
1944   save_flags(flags);
1945   cli();
1946   locked = ppp->sending;
1947   ppp->sending = 1;
1948   if (ppp->dev->flags & IFF_UP)
1949     ppp->dev->tbusy = 1;
1950   restore_flags(flags);
1951   return locked == 0;
1952 }
1953 
1954 static void
1955 ppp_unlock(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1956 {
1957   int flags;
1958   save_flags(flags);
1959   cli();
1960   ppp->sending = 0;
1961   if (ppp->dev->flags & IFF_UP)
1962     ppp->dev->tbusy = 0;
1963   restore_flags(flags);
1964 }
1965 
1966 /* FCS support functions */
1967 
1968 static void
1969 ppp_add_fcs(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1970 {
1971   unsigned short fcs = ppp->fcs;
1972 
1973   fcs ^= 0xffff;
1974   ppp_stuff_char(ppp, fcs & 0x00ff);
1975   ppp_stuff_char(ppp, (fcs & 0xff00) >> 8);
1976   ASSERT (ppp->fcs == PPP_FCS_GOOD);
1977   PRINTKN (4,(KERN_DEBUG "ppp_add_fcs: fcs is %lx\n",
1978               (long) (unsigned long) fcs));
1979 }
1980 
1981 static int
1982 ppp_check_fcs(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1983 {
1984   unsigned short fcs = PPP_FCS_INIT, msgfcs;
1985   unsigned char *c = ppp->rbuff;
1986   int i;
1987 
1988   for (i = 0; i < ppp->rcount - 2; i++, c++)
1989     fcs = (fcs >> 8) ^ fcstab[(fcs ^ *c) & 0xff];
1990 
1991   fcs ^= 0xffff;
1992   msgfcs = (c[1] << 8) + c[0];
1993   PRINTKN (4,(KERN_INFO "ppp_check_fcs: got %lx want %lx\n",
1994               (unsigned long) msgfcs, (unsigned long) fcs));
1995   return fcs == msgfcs;
1996 }
1997 
1998 static char hex[] = "0123456789ABCDEF";
1999 
2000 static inline void ppp_print_hex (register char *out, char *in, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
2001 {
2002   register unsigned char next_ch;
2003 
2004   while (count-- > 0) {
2005     next_ch = (unsigned char) get_fs_byte (in);
2006 
2007     *out++  = hex[(next_ch >> 4) & 0x0F];
2008     *out++  = hex[next_ch        & 0x0F];
2009     ++out;
2010     ++in;
2011   }
2012 }
2013 
2014 static inline void ppp_print_char (register char *out, char *in, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
2015 {
2016   register unsigned char next_ch;
2017 
2018   while (count-- > 0) {
2019     next_ch = (unsigned char) get_fs_byte (in);
2020 
2021     if (next_ch < 0x20 || next_ch > 0x7e)
2022       *out++ = '.';
2023     else {
2024       *out++ = next_ch;
2025       if (next_ch == '%')       /* printk/syslogd has a bug !! */
2026         *out++ = '%';
2027     }
2028     ++in;
2029   }
2030   *out = '\0';
2031 }
2032 
2033 static void ppp_print_buffer(const char *name, char *buf, int count, int seg)
     /* [previous][next][first][last][top][bottom][index][help] */
2034 {
2035   char line [44];
2036   int  old_fs = get_fs();
2037 
2038   set_fs (seg);
2039 
2040   if (name != NULL)
2041     PRINTK ((KERN_DEBUG "ppp: %s, count = %d\n", name, count));
2042 
2043   while (count > 8) {
2044     memset         (line, ' ', sizeof (line));
2045     ppp_print_hex  (line, buf, 8);
2046     ppp_print_char (&line[8 * 3], buf, 8);
2047     PRINTK ((KERN_DEBUG "%s\n", line));
2048     count -= 8;
2049     buf   += 8;
2050   }
2051 
2052   if (count > 0) {
2053     memset         (line, ' ', sizeof (line));
2054     ppp_print_hex  (line, buf, count);
2055     ppp_print_char (&line[8 * 3], buf, count);
2056     PRINTK ((KERN_DEBUG "%s\n", line));
2057   }
2058 
2059   set_fs (old_fs);
2060 }
2061 
2062 #ifdef MODULE
2063 char kernel_version[] = UTS_RELEASE;
2064 
2065 static struct device dev_ppp[PPP_NRUNIT] = {
2066         {
2067                 "ppp0",         /* ppp */
2068                 0, 0, 0, 0,     /* memory */
2069                 0, 0,           /* base, irq */
2070                 0, 0, 0, NULL, ppp_init,
2071         }
2072         , { "ppp1" , 0, 0, 0, 0,  1, 0, 0, 0, 0, NULL, ppp_init }
2073         , { "ppp2" , 0, 0, 0, 0,  2, 0, 0, 0, 0, NULL, ppp_init }
2074         , { "ppp3" , 0, 0, 0, 0,  3, 0, 0, 0, 0, NULL, ppp_init }
2075 
2076 #ifdef PPP_PPP_LOTS
2077         , { "ppp4" , 0, 0, 0, 0,  4, 0, 0, 0, 0, NULL, ppp_init }
2078         , { "ppp5" , 0, 0, 0, 0,  5, 0, 0, 0, 0, NULL, ppp_init }
2079         , { "ppp6" , 0, 0, 0, 0,  6, 0, 0, 0, 0, NULL, ppp_init }
2080         , { "ppp7" , 0, 0, 0, 0,  7, 0, 0, 0, 0, NULL, ppp_init }
2081         , { "ppp8" , 0, 0, 0, 0,  8, 0, 0, 0, 0, NULL, ppp_init }
2082         , { "ppp9" , 0, 0, 0, 0,  9, 0, 0, 0, 0, NULL, ppp_init }
2083         , { "ppp10" , 0, 0, 0, 0, 10, 0, 0, 0, 0, NULL, ppp_init }
2084         , { "ppp11" , 0, 0, 0, 0, 11, 0, 0, 0, 0, NULL, ppp_init }
2085         , { "ppp12" , 0, 0, 0, 0, 12, 0, 0, 0, 0, NULL, ppp_init }
2086         , { "ppp13" , 0, 0, 0, 0, 13, 0, 0, 0, 0, NULL, ppp_init }
2087         , { "ppp14" , 0, 0, 0, 0, 14, 0, 0, 0, 0, NULL, ppp_init }
2088         , { "ppp15" , 0, 0, 0, 0, 15, 0, 0, 0, 0, NULL, ppp_init }
2089 #endif
2090 };
2091 
2092 int
2093 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2094 {
2095         int err;
2096         int i;
2097 
2098         for (i = 0; i < PPP_NRUNIT; i++)  {
2099                 if ((err = register_netdev(&dev_ppp[i])))  {
2100                         if (err == -EEXIST)  {
2101                                 printk("PPP: devices already present. Module not loaded.\n");
2102                         }
2103                         return err;
2104                 }
2105         }
2106         return 0;
2107 }
2108 
2109 void
2110 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2111 {
2112         int i;
2113 
2114         if (MOD_IN_USE)  {
2115                 printk("PPP: device busy, remove delayed\n");
2116                 return;
2117         }
2118         for (i = 0; i < PPP_NRUNIT; i++)  {
2119                 unregister_netdev(&dev_ppp[i]);
2120         }
2121         if ((i = tty_register_ldisc(N_PPP, NULL)))  {
2122                 printk("PPP: can't unregister line discipline (err = %d)\n", i);
2123         }
2124 }
2125 
2126 #endif

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