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

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

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