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_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
  44. init_module
  45. cleanup_module

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

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