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_buf
  19. ppp_doframe
  20. ppp_do_ip
  21. ppp_us_queue
  22. ppp_read
  23. ppp_stuff_char
  24. ppp_write
  25. ppp_ioctl
  26. ppp_select
  27. ppp_xmit
  28. ppp_type_trans
  29. ppp_header
  30. ppp_rebuild_header
  31. ppp_add_arp
  32. ppp_header
  33. ppp_rebuild_header
  34. ppp_get_stats
  35. ppp_find
  36. ppp_alloc
  37. ppp_lock
  38. ppp_unlock
  39. ppp_add_fcs
  40. ppp_check_fcs
  41. ppp_print_hex
  42. ppp_print_char
  43. ppp_print_buffer

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

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