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    = 4;
 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 %x dst %x 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 %p num %u\n",
1255               buf, nr));
1256 
1257   do {
1258     /* try to acquire read lock */
1259     if (set_bit(0, &ppp->us_rbuff_lock) == 0) {
1260       /* got lock */
1261       if (ppp->us_rbuff_head == ppp->us_rbuff_tail) {
1262         /* no data */
1263         PRINTKN (4,(KERN_DEBUG "ppp_read: no data\n"));
1264         clear_bit(0, &ppp->us_rbuff_lock);
1265         if (ppp->inp_sig) {
1266           PRINTKN (4,(KERN_DEBUG "ppp_read: EWOULDBLOCK\n"));
1267           return -EWOULDBLOCK;
1268         } else goto wait;
1269       }
1270 
1271       /* reset the time of the last read operation */
1272       ppp->ddinfo.nip_rjiffies = jiffies;
1273 
1274       GETC (c); len = c << 8; GETC (c); len += c;
1275 
1276       PRINTKN (4,(KERN_DEBUG "ppp_read: len = %d\n", len));
1277 
1278       if (len + 2 > nr) {
1279         /* frame too big; can't copy it, but do update us_rbuff_head */
1280         PRINTKN (1,(KERN_DEBUG
1281                     "ppp: read of %u bytes too small for %d frame\n",
1282                     nr, len+2));
1283         ppp->us_rbuff_head += len;
1284         if (ppp->us_rbuff_head > ppp->us_rbuff_end)
1285           ppp->us_rbuff_head += - (ppp->us_rbuff_end - ppp->us_rbuff);
1286         clear_bit(0, &ppp->us_rbuff_lock);
1287         wake_up_interruptible (&ppp->read_wait);
1288         ppp->stats.rgiants++;
1289         return -EOVERFLOW;              /* ZZZ; HACK! */
1290       } else {
1291         /* have the space: copy the packet, faking the first two bytes */
1292         put_user (PPP_ADDRESS, buf++);
1293         put_user (PPP_CONTROL, buf++);
1294         i = len;
1295         while (i-- > 0) {
1296           GETC (c);
1297           put_user (c, buf++);
1298         }
1299       }
1300 
1301       clear_bit(0, &ppp->us_rbuff_lock);
1302       PRINTKN (3,(KERN_DEBUG "ppp_read: passing %d bytes up\n", len + 2));
1303       ppp->stats.rothers++;
1304       return len + 2;
1305     }
1306 
1307     /* need to wait */
1308   wait:
1309     current->timeout = 0;
1310     PRINTKN (3,(KERN_DEBUG "ppp_read: sleeping\n"));
1311     interruptible_sleep_on (&ppp->read_wait);
1312     if (current->signal & ~current->blocked)
1313       return -EINTR;
1314   } while (1);
1315 }
1316 
1317 /* stuff a character into the transmit buffer, using PPP's way of escaping
1318    special characters.
1319    also, update ppp->fcs to take account of new character */
1320 static inline void
1321 ppp_stuff_char(struct ppp *ppp, unsigned char c)
     /* [previous][next][first][last][top][bottom][index][help] */
1322 {
1323   int curpt = ppp->xhead - ppp->xbuff;
1324   if ((curpt < 0) || (curpt > 3000)) {
1325     PRINTK ((KERN_DEBUG "ppp_stuff_char: %p %p %d\n",
1326              ppp->xbuff, ppp->xhead, curpt))
1327   }
1328   if (in_xmap (ppp, c)) {
1329     *ppp->xhead++ = PPP_ESC;
1330     *ppp->xhead++ = c ^ PPP_TRANS;
1331   } else
1332     *ppp->xhead++ = c;
1333   ppp->fcs = (ppp->fcs >> 8) ^ fcstab[(ppp->fcs ^ c) & 0xff];
1334 }
1335 
1336 /* write a frame with NR chars from BUF to TTY
1337    we have to put the FCS field on ourselves
1338 */
1339 
1340 static int
1341 ppp_write(struct tty_struct *tty, struct file *file, unsigned char *buf, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
1342 {
1343   struct ppp *ppp = ppp_find(tty);
1344   int i;
1345 
1346   if (!ppp || ppp->magic != PPP_MAGIC) {
1347     PRINTKN (1,(KERN_ERR "ppp_write: cannot find ppp unit\n"));
1348     return -EIO;
1349   }
1350 
1351   CHECK_PPP(-ENXIO);
1352   
1353   if (ppp->mtu != ppp->dev->mtu)        /* Someone has been ifconfigging */
1354     ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru);
1355 
1356   if (nr > ppp->mtu) {
1357     PRINTKN (1,(KERN_WARNING
1358                 "ppp_write: truncating user packet from %u to mtu %d\n",
1359                 nr, ppp->mtu));
1360     nr = ppp->mtu;
1361   }
1362 
1363   if (ppp_debug >= 3)
1364     ppp_print_buffer ("write frame", buf, nr, USER_DS);
1365 
1366   /* lock this PPP unit so we will be the only writer;
1367      sleep if necessary */
1368   while ((ppp->sending == 1) || !ppp_lock(ppp)) {
1369     current->timeout = 0;
1370     PRINTKN (3,(KERN_DEBUG "ppp_write: sleeping\n"));
1371     interruptible_sleep_on(&ppp->write_wait);
1372     if (current->signal & ~current->blocked)
1373       return -EINTR;
1374   }
1375 
1376   /* OK, locked.  Stuff the given bytes into the buffer. */
1377 
1378   PRINTKN(4,(KERN_DEBUG "ppp_write: acquired write lock\n"));
1379   ppp->xhead = ppp->xbuff;
1380 
1381 #ifdef OPTIMIZE_FLAG_TIME
1382   if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1383     *ppp->xhead++ = PPP_FLAG;
1384   ppp->last_xmit = jiffies;
1385 #else      
1386   *ppp->xhead++ = PPP_FLAG;
1387 #endif
1388 
1389   ppp->fcs = PPP_FCS_INIT;
1390   i = nr;
1391   while (i-- > 0)
1392     ppp_stuff_char(ppp,get_user(buf++));
1393 
1394   ppp_add_fcs(ppp);             /* concatenate FCS at end */
1395 
1396   *ppp->xhead++ = PPP_FLAG;
1397   
1398   /* reset the time of the last write operation */
1399   ppp->ddinfo.nip_sjiffies = jiffies;
1400 
1401   if (ppp_debug >= 6)
1402     ppp_print_buffer ("xmit buffer", ppp->xbuff, ppp->xhead - ppp->xbuff, KERNEL_DS);
1403   else {
1404     PRINTKN (4,(KERN_DEBUG "ppp_write: writing %d chars\n",
1405                 (int) (ppp->xhead - ppp->xbuff)));
1406   }
1407 
1408   /* packet is ready-to-go */
1409   ++ppp->stats.sothers;
1410   ppp_kick_tty(ppp);
1411 
1412   return((int)nr);
1413 }
1414  
1415 static int
1416 ppp_ioctl(struct tty_struct *tty, struct file *file, unsigned int i,
     /* [previous][next][first][last][top][bottom][index][help] */
1417           unsigned long l)
1418 {
1419   struct ppp *ppp = ppp_find(tty);
1420   register int temp_i = 0;
1421   int error;
1422 
1423   if (!ppp || ppp->magic != PPP_MAGIC) {
1424     PRINTK ((KERN_ERR "ppp_ioctl: can't find PPP block from tty!\n"))
1425     return -EBADF;
1426   }
1427 
1428   CHECK_PPP(-ENXIO);
1429 
1430   /* This must be root user */
1431   if (!suser())
1432     return -EPERM;
1433 
1434   switch (i) {
1435   case PPPIOCSMRU:
1436     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1437     if (error == 0) {
1438       temp_i = get_user ((int *) l);
1439       PRINTKN (3,(KERN_INFO "ppp_ioctl: set mru to %d\n", temp_i));
1440       if (ppp->mru != temp_i)
1441         ppp_changedmtu (ppp, ppp->dev->mtu, temp_i);
1442     }
1443     break;
1444 
1445   case PPPIOCGFLAGS:
1446     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1447     if (error == 0) {
1448       temp_i = (ppp->flags & SC_MASK);
1449 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
1450       temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP;
1451 #endif
1452       put_user (temp_i, (int *) l);
1453       PRINTKN (3,(KERN_DEBUG "ppp_ioctl: get flags: addr %lx flags %x\n",
1454                   l,
1455                   temp_i));
1456     }
1457     break;
1458 
1459   case PPPIOCSFLAGS:
1460     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1461     if (error == 0) {
1462       temp_i      = get_user ((int *) l);
1463       ppp->flags ^= ((ppp->flags ^ temp_i) & SC_MASK);
1464       PRINTKN (3,(KERN_INFO "ppp_ioctl: set flags to %x\n", temp_i));
1465     }
1466     break;
1467 
1468   case PPPIOCGASYNCMAP:
1469     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1470     if (error == 0) {
1471       put_user (ppp->xmit_async_map[0], (int *) l);
1472       PRINTKN (3,(KERN_INFO "ppp_ioctl: get asyncmap: addr %lx asyncmap %lx\n",
1473                   l, (unsigned long) ppp->xmit_async_map[0]));
1474     }
1475     break;
1476 
1477   case PPPIOCSASYNCMAP:
1478     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1479     if (error == 0) {
1480       ppp->xmit_async_map[0] = get_user ((int *) l);
1481       bset (ppp->xmit_async_map, PPP_FLAG);
1482       bset (ppp->xmit_async_map, PPP_ESC);
1483       PRINTKN (3,(KERN_INFO "ppp_ioctl: set xmit asyncmap %lx\n",
1484                   (unsigned long) ppp->xmit_async_map[0]));
1485     }
1486     break;
1487 
1488   case PPPIOCRASYNCMAP:
1489     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1490     if (error == 0) {
1491       ppp->recv_async_map = get_user ((int *) l);
1492       PRINTKN (3,(KERN_INFO "ppp_ioctl: set recv asyncmap %lx\n",
1493                   (unsigned long) ppp->recv_async_map));
1494     }
1495     break;
1496 
1497   case PPPIOCGUNIT:
1498     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1499     if (error == 0) {
1500       put_user (ppp->dev->base_addr, (int *) l);
1501       PRINTKN (3,(KERN_INFO "ppp_ioctl: get unit: %ld", ppp->dev->base_addr));
1502     }
1503     break;
1504 
1505   case PPPIOCSINPSIG:
1506     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1507     if (error == 0) {
1508       ppp->inp_sig     = get_user ((int *) l);
1509       ppp->inp_sig_pid = current->pid;
1510       PRINTKN (3,(KERN_INFO "ppp_ioctl: set input signal %d\n", ppp->inp_sig));
1511     }
1512     break;
1513 
1514   case PPPIOCSDEBUG:
1515     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1516     if (error == 0) {
1517       ppp_debug = get_user ((int *) l);
1518       ppp_debug_netpackets = (ppp_debug & 0xff00) >> 8;
1519       ppp_debug &= 0xff;
1520       PRINTKN (1, (KERN_INFO "ppp_ioctl: set debug level %d, netpacket %d\n", 
1521                    ppp_debug, ppp_debug_netpackets));
1522     }
1523     break;
1524 
1525   case PPPIOCGDEBUG:
1526     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1527     if (error == 0) {
1528       put_user ((long) (ppp_debug | (ppp_debug_netpackets << 8)), (int *) l);
1529       PRINTKN (3,(KERN_INFO "ppp_ioctl: get debug level %d\n", 
1530                   ppp_debug | (ppp_debug_netpackets << 8)));
1531     }
1532     break;
1533 
1534   case PPPIOCGSTAT:
1535     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (struct ppp_stats));
1536     if (error == 0) {
1537       memcpy_tofs ((void *) l, &ppp->stats, sizeof (struct ppp_stats));
1538       PRINTKN (3,(KERN_INFO "ppp_ioctl: read statistics\n"));
1539     }
1540     break;
1541 
1542   case PPPIOCGTIME:
1543     error = verify_area (VERIFY_WRITE, (void *) l, sizeof (struct ppp_ddinfo));
1544     if (error == 0) {
1545       struct ppp_ddinfo cur_ddinfo;
1546       unsigned long cur_jiffies = jiffies;
1547 
1548       /* change absolute times to relative times. */
1549       cur_ddinfo.ip_sjiffies  = cur_jiffies - ppp->ddinfo.ip_sjiffies;
1550       cur_ddinfo.ip_rjiffies  = cur_jiffies - ppp->ddinfo.ip_rjiffies;
1551       cur_ddinfo.nip_sjiffies = cur_jiffies - ppp->ddinfo.nip_sjiffies;
1552       cur_ddinfo.nip_rjiffies = cur_jiffies - ppp->ddinfo.nip_rjiffies;
1553       
1554       memcpy_tofs ((void *) l, &cur_ddinfo, sizeof (struct ppp_ddinfo));
1555       PRINTKN (3,(KERN_INFO "ppp_ioctl: read demand dial info\n"));
1556     }
1557     break;
1558 
1559   case PPPIOCGXASYNCMAP:
1560     error = verify_area (VERIFY_WRITE,
1561                          (void *) l,
1562                          sizeof (ppp->xmit_async_map));
1563     if (error == 0) {
1564       memcpy_tofs ((void *) l,
1565                    ppp->xmit_async_map,
1566                    sizeof (ppp->xmit_async_map));
1567       PRINTKN (3,(KERN_INFO "ppp_ioctl: get xasyncmap: addr %lx\n", l));
1568     }
1569     break;
1570 
1571   case PPPIOCSXASYNCMAP:
1572     error = verify_area (VERIFY_READ, (void *) l,
1573                          sizeof (ppp->xmit_async_map));
1574     if (error == 0) {
1575       __u32 temp_tbl [8];
1576 
1577       memcpy_fromfs (temp_tbl, (void *) l, sizeof (ppp->xmit_async_map));
1578       temp_tbl[1]  =  0x00000000; /* must not escape 0x20 - 0x3f */
1579       temp_tbl[2] &= ~0x40000000; /* must not escape 0x5e        */
1580       temp_tbl[3] |=  0x60000000; /* must escape 0x7d and 0x7e   */
1581 
1582       if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
1583           (temp_tbl[4] & temp_tbl[5]) != 0 ||
1584           (temp_tbl[6] & temp_tbl[7]) != 0)
1585         error = -EINVAL;
1586       else {
1587         memcpy (ppp->xmit_async_map, temp_tbl, sizeof (ppp->xmit_async_map));
1588         PRINTKN (3,(KERN_INFO "ppp_ioctl: set xasyncmap\n"));
1589       }
1590     }
1591     break;
1592 
1593   case PPPIOCSMAXCID:
1594     error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1595     if (error == 0) {
1596       temp_i = get_user ((int *) l) + 1;
1597       PRINTKN (3,(KERN_INFO "ppp_ioctl: set maxcid to %d\n", temp_i));
1598       if (ppp->slcomp != NULL)
1599         slhc_free (ppp->slcomp);
1600 
1601       ppp->slcomp = slhc_init (temp_i, temp_i);
1602 
1603       if (ppp->slcomp == NULL) {
1604         PRINTKN (1,(KERN_ERR "ppp: no space for compression buffers!\n"));
1605         ppp_release (ppp);
1606         error = -ENOMEM;
1607       }
1608     }
1609     break;
1610 
1611 #ifdef NEW_TTY_DRIVERS
1612     /* Allow stty to read, but not set, the serial port */
1613   case TCGETS:
1614   case TCGETA:
1615     error = n_tty_ioctl(tty, file, i, l);
1616     break;
1617 #endif
1618 
1619 /*
1620  *  All other ioctl() events will come here.
1621  */
1622 
1623   default:
1624     PRINTKN (1,(KERN_ERR "ppp_ioctl: invalid ioctl: %x, addr %lx\n",
1625                 i,
1626                 l));
1627 #ifdef NEW_TTY_DRIVERS
1628     error = -ENOIOCTLCMD;
1629 #else
1630     error = -EINVAL;
1631 #endif
1632     break;
1633   }
1634   return error;
1635 }
1636 
1637 static int
1638 ppp_select (struct tty_struct *tty, struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
1639             struct file * filp, int sel_type, select_table * wait)
1640 {
1641   struct ppp *ppp = ppp_find (tty);
1642   
1643   if (!ppp || ppp->magic != PPP_MAGIC) {
1644     PRINTK ((KERN_ERR "ppp_select: can't find PPP block from tty!\n"))
1645     return -EBADF;
1646   }
1647   
1648   /* If the PPP protocol is no longer active, return false */
1649   CHECK_PPP (0);
1650   
1651   /* Process the request based upon the type desired */
1652   switch (sel_type) {
1653   case SEL_IN:
1654     if (set_bit(0, &ppp->us_rbuff_lock) == 0) {
1655       /* Test for the presence of data in the queue */
1656       if (ppp->us_rbuff_head != ppp->us_rbuff_tail) {
1657         clear_bit (0, &ppp->us_rbuff_lock);
1658         return 1;
1659       }
1660       clear_bit (0, &ppp->us_rbuff_lock);
1661     } /* fall through */
1662 
1663   case SEL_EX:
1664     /* Is there a pending error condition? */
1665     if (tty->packet && tty->link->ctrl_status)
1666       return 1;
1667     
1668     /* closed? */
1669     if (tty->flags & (1 << TTY_SLAVE_CLOSED))
1670       return 1;
1671     
1672     /* If the tty is disconnected, then this is an exception too */
1673     if (tty_hung_up_p(filp))
1674       return 1;
1675 
1676     select_wait (&ppp->read_wait, wait);
1677     break;
1678     
1679   case SEL_OUT:
1680     if (ppp_lock (ppp)) {
1681       if (ppp->sending == 0) {
1682         ppp_unlock (ppp);
1683         return 1;
1684       }
1685       ppp_unlock (ppp);
1686     }
1687     select_wait (&ppp->write_wait, wait);
1688     break;
1689   }
1690   return 0;
1691 }
1692 
1693 /*************************************************************
1694  * NETWORK OUTPUT
1695  *    This routine accepts requests from the network layer
1696  *    and attempts to deliver the packets.
1697  *    It also includes various routines we are compelled to
1698  *    have to make the network layer work (arp, etc...).
1699  *************************************************************/
1700 
1701 int
1702 ppp_xmit(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1703 {
1704   struct tty_struct *tty;
1705   struct ppp *ppp;
1706   unsigned char *p;
1707   unsigned short proto;
1708   int len;
1709 
1710   /* just a little sanity check. */
1711   if (skb == NULL) {
1712     PRINTKN(3,(KERN_WARNING "ppp_xmit: null packet!\n"));
1713     return 0;
1714   }
1715 
1716   /* Get pointers to the various components */
1717   ppp   = &ppp_ctrl[dev->base_addr];
1718   tty   = ppp->tty;
1719   p     = skb->data;
1720   len   = skb->len;
1721   proto = PROTO_IP;
1722 
1723   PRINTKN(4,(KERN_DEBUG "ppp_xmit [%s]: skb %lX busy %d\n", dev->name, 
1724              (unsigned long int) skb, ppp->sending));
1725 
1726   /* avoid race conditions when the link fails */
1727   if (!ppp->inuse) {
1728     dev_kfree_skb(skb, FREE_WRITE);
1729     dev_close (dev);
1730     return 0;
1731   }
1732 
1733   if (tty == NULL) {
1734     PRINTKN(1,(KERN_ERR "ppp_xmit: %s not connected to a TTY!\n", dev->name));
1735     goto done;
1736   }
1737 
1738   if (!(dev->flags & IFF_UP)) {
1739     PRINTKN(1,(KERN_WARNING
1740                "ppp_xmit: packet sent on interface %s, which is down for IP\n",
1741                dev->name));
1742     goto done;
1743   }
1744 
1745 #ifdef CURED_AGES_AGO
1746   /* get length from IP header as per Alan Cox bugfix for slip.c */
1747   if (len < sizeof(struct iphdr)) {
1748     PRINTKN(0,(KERN_ERR "ppp_xmit: given runt packet, ignoring\n"));
1749     goto done;
1750   }
1751   len = ntohs( ((struct iphdr *)(skb->data)) -> tot_len );
1752 #endif  
1753 
1754   /* If doing demand dial then divert the first frame to pppd. */
1755   if (ppp->flags & SC_IP_DOWN) {
1756     if (ppp->flags & SC_IP_FLUSH == 0) {
1757       if (ppp_us_queue (ppp, proto, p, len))
1758         ppp->flags |= SC_IP_FLUSH;
1759     }
1760     goto done;
1761   }
1762 
1763   /* Attempt to acquire send lock */
1764   if (ppp->sending || !ppp_lock(ppp)) {
1765     PRINTKN(3,(KERN_WARNING "ppp_xmit: busy\n"));
1766     ppp->stats.sbusy++;
1767     return 1;
1768   }
1769 
1770   ppp->xhead = ppp->xbuff;
1771 
1772   /* try to compress, if VJ compression mode is on */
1773   if (ppp->flags & SC_COMP_TCP) {
1774     len = slhc_compress(ppp->slcomp, p, len, ppp->cbuff, &p, 
1775                         !(ppp->flags & SC_NO_TCP_CCID));
1776     if (p[0] & SL_TYPE_COMPRESSED_TCP)
1777       proto = PROTO_VJCOMP;
1778     else {
1779       if (p[0] >= SL_TYPE_UNCOMPRESSED_TCP) {
1780         proto = PROTO_VJUNCOMP;
1781         p[0] = (p[0] & 0x0f) | 0x40; 
1782       }
1783     }
1784   }
1785 
1786   /* increment appropriate counter */
1787   if (proto == PROTO_VJCOMP)
1788     ++ppp->stats.scomp;
1789   else
1790     ++ppp->stats.suncomp;
1791       
1792   if (ppp_debug_netpackets) {
1793     struct iphdr *iph = (struct iphdr *)skb->data;
1794     PRINTK ((KERN_DEBUG "%s ==> proto %x len %d src %x dst %x proto %d\n",
1795             dev->name, (int) proto, (int) len, (int) iph->saddr,
1796             (int) iph->daddr, (int) iph->protocol))
1797   }
1798 
1799   /* start of frame:   FLAG  ALL_STATIONS  CONTROL  <protohi> <protolo> */
1800 #ifdef OPTIMIZE_FLAG_TIME
1801   if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1802     *ppp->xhead++ = PPP_FLAG;
1803   ppp->last_xmit = jiffies;
1804 #else      
1805   *ppp->xhead++ = PPP_FLAG;
1806 #endif
1807 
1808   ppp->fcs = PPP_FCS_INIT;
1809   if (!(ppp->flags & SC_COMP_AC)) { 
1810     ppp_stuff_char(ppp, PPP_ADDRESS);
1811     ppp_stuff_char(ppp, PPP_CONTROL);
1812   }
1813 
1814   if (!(ppp->flags & SC_COMP_PROT) || (proto & 0xff00))
1815     ppp_stuff_char(ppp, proto>>8);
1816   ppp_stuff_char(ppp, proto&0xff);
1817 
1818   /* data part */
1819   while (len-- > 0)
1820     ppp_stuff_char(ppp, *p++);
1821 
1822   /* fcs and flag */
1823   ppp_add_fcs(ppp);
1824   *ppp->xhead++ = PPP_FLAG;
1825 
1826   /* update the time for demand dial function */
1827   ppp->ddinfo.ip_sjiffies = jiffies;
1828 
1829   /* send it! */
1830   if (ppp_debug >= 6)
1831     ppp_print_buffer ("xmit buffer", ppp->xbuff, ppp->xhead - ppp->xbuff, KERNEL_DS);
1832   else {
1833     PRINTKN (4,(KERN_DEBUG "ppp_write: writing %d chars\n",
1834                 (int) (ppp->xhead - ppp->xbuff)));
1835   }
1836 
1837   ppp_kick_tty(ppp);
1838 
1839  done:
1840   dev_kfree_skb(skb, FREE_WRITE);
1841   return 0;
1842 }
1843   
1844 #ifdef NET02D
1845 static int
1846 ppp_header(unsigned char *buff, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
1847            unsigned long daddr, unsigned long saddr, unsigned len)
1848 {
1849   return(0);
1850 }
1851 
1852 static int
1853 ppp_rebuild_header(void *buff, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1854 {
1855   return(0);
1856 }
1857 
1858 static void
1859 ppp_add_arp(unsigned long addr, struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1860 {
1861 }
1862 
1863 #else
1864 
1865 static int
1866 ppp_header(struct sk_buff *skb, struct device *dev, unsigned short type,
     /* [previous][next][first][last][top][bottom][index][help] */
1867            void *daddr, void *saddr, unsigned len)
1868 {
1869   return(0);
1870 }
1871 
1872 static int
1873 ppp_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
     /* [previous][next][first][last][top][bottom][index][help] */
1874                    struct sk_buff *skb)
1875 {
1876   return(0);
1877 }
1878 #endif
1879 
1880 static struct enet_statistics *
1881 ppp_get_stats (struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
1882 {
1883   struct ppp *ppp = &ppp_ctrl[dev->base_addr];
1884   static struct enet_statistics ppp_stats;
1885 
1886   ppp_stats.rx_packets = ppp->stats.rcomp + ppp->stats.runcomp;
1887   ppp_stats.rx_errors = ppp->stats.rerrors;
1888   ppp_stats.rx_dropped = ppp->stats.tossed;
1889   ppp_stats.rx_fifo_errors = 0;
1890   ppp_stats.rx_length_errors = ppp->stats.runts;
1891   ppp_stats.rx_over_errors = ppp->stats.roverrun;
1892   ppp_stats.rx_crc_errors = 0;
1893   ppp_stats.rx_frame_errors = 0;
1894   ppp_stats.tx_packets = ppp->stats.scomp + ppp->stats.suncomp;
1895   ppp_stats.tx_errors = ppp->stats.serrors;
1896   ppp_stats.tx_dropped = 0;
1897   ppp_stats.tx_fifo_errors = 0;
1898   ppp_stats.collisions = ppp->stats.sbusy;
1899   ppp_stats.tx_carrier_errors = 0;
1900   ppp_stats.tx_aborted_errors = 0;
1901   ppp_stats.tx_window_errors = 0;
1902   ppp_stats.tx_heartbeat_errors = 0;
1903 
1904   PRINTKN (3, (KERN_INFO "ppp_get_stats called"));
1905   return &ppp_stats;
1906 }
1907 
1908 /*************************************************************
1909  * UTILITIES
1910  *    Miscellany called by various functions above.
1911  *************************************************************/
1912 
1913 #ifndef NEW_TTY_DRIVERS
1914 /* find a PPP channel given a TTY */
1915 struct ppp *
1916 ppp_find(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1917 {
1918   int i;
1919   for (i = 0; i < PPP_NRUNIT; i++)
1920     if (ppp_ctrl[i].inuse && (ppp_ctrl[i].tty == tty)) return &ppp_ctrl[i];
1921 
1922   return NULL;
1923 }
1924 #endif
1925 
1926 /* allocate a PPP channel */
1927 static struct ppp *
1928 ppp_alloc(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1929 {
1930   int i;
1931   for (i = 0; i < PPP_NRUNIT; i++)
1932     if (!set_bit(0, &ppp_ctrl[i].inuse)) return &ppp_ctrl[i];
1933 
1934   return NULL;
1935 }
1936 
1937 /* marks a PPP interface 'busy'.  user processes will wait, if
1938    they try to write, and the network code will refrain from sending
1939    return nonzero if succeeded in acquiring lock
1940 */
1941 
1942 static int
1943 ppp_lock(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1944 {
1945   int flags, locked;
1946   save_flags(flags);
1947   cli();
1948   locked = ppp->sending;
1949   ppp->sending = 1;
1950   if (ppp->dev->flags & IFF_UP)
1951     ppp->dev->tbusy = 1;
1952   restore_flags(flags);
1953   return locked == 0;
1954 }
1955 
1956 static void
1957 ppp_unlock(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1958 {
1959   int flags;
1960   save_flags(flags);
1961   cli();
1962   ppp->sending = 0;
1963   if (ppp->dev->flags & IFF_UP)
1964     ppp->dev->tbusy = 0;
1965   restore_flags(flags);
1966 }
1967 
1968 /* FCS support functions */
1969 
1970 static void
1971 ppp_add_fcs(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1972 {
1973   unsigned short fcs = ppp->fcs;
1974 
1975   fcs ^= 0xffff;
1976   ppp_stuff_char(ppp, fcs & 0x00ff);
1977   ppp_stuff_char(ppp, (fcs & 0xff00) >> 8);
1978   ASSERT (ppp->fcs == PPP_FCS_GOOD);
1979   PRINTKN (4,(KERN_DEBUG "ppp_add_fcs: fcs is %lx\n",
1980               (long) (unsigned long) fcs));
1981 }
1982 
1983 static int
1984 ppp_check_fcs(struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1985 {
1986   unsigned short fcs = PPP_FCS_INIT, msgfcs;
1987   unsigned char *c = ppp->rbuff;
1988   int i;
1989 
1990   for (i = 0; i < ppp->rcount - 2; i++, c++)
1991     fcs = (fcs >> 8) ^ fcstab[(fcs ^ *c) & 0xff];
1992 
1993   fcs ^= 0xffff;
1994   msgfcs = (c[1] << 8) + c[0];
1995   PRINTKN (4,(KERN_INFO "ppp_check_fcs: got %lx want %lx\n",
1996               (unsigned long) msgfcs, (unsigned long) fcs));
1997   return fcs == msgfcs;
1998 }
1999 
2000 static char hex[] = "0123456789ABCDEF";
2001 
2002 static inline void ppp_print_hex (register char *out, char *in, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
2003 {
2004   register unsigned char next_ch;
2005 
2006   while (count-- > 0) {
2007     next_ch = (unsigned char) get_user (in);
2008 
2009     *out++  = hex[(next_ch >> 4) & 0x0F];
2010     *out++  = hex[next_ch        & 0x0F];
2011     ++out;
2012     ++in;
2013   }
2014 }
2015 
2016 static inline void ppp_print_char (register char *out, char *in, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
2017 {
2018   register unsigned char next_ch;
2019 
2020   while (count-- > 0) {
2021     next_ch = (unsigned char) get_user (in);
2022 
2023     if (next_ch < 0x20 || next_ch > 0x7e)
2024       *out++ = '.';
2025     else {
2026       *out++ = next_ch;
2027       if (next_ch == '%')       /* printk/syslogd has a bug !! */
2028         *out++ = '%';
2029     }
2030     ++in;
2031   }
2032   *out = '\0';
2033 }
2034 
2035 static void ppp_print_buffer(const char *name, char *buf, int count, int seg)
     /* [previous][next][first][last][top][bottom][index][help] */
2036 {
2037   char line [44];
2038   int  old_fs = get_fs();
2039 
2040   set_fs (seg);
2041 
2042   if (name != NULL)
2043     PRINTK ((KERN_DEBUG "ppp: %s, count = %d\n", name, count));
2044 
2045   while (count > 8) {
2046     memset         (line, ' ', sizeof (line));
2047     ppp_print_hex  (line, buf, 8);
2048     ppp_print_char (&line[8 * 3], buf, 8);
2049     PRINTK ((KERN_DEBUG "%s\n", line));
2050     count -= 8;
2051     buf   += 8;
2052   }
2053 
2054   if (count > 0) {
2055     memset         (line, ' ', sizeof (line));
2056     ppp_print_hex  (line, buf, count);
2057     ppp_print_char (&line[8 * 3], buf, count);
2058     PRINTK ((KERN_DEBUG "%s\n", line));
2059   }
2060 
2061   set_fs (old_fs);
2062 }
2063 
2064 #ifdef MODULE
2065 char kernel_version[] = UTS_RELEASE;
2066 
2067 static struct device dev_ppp[PPP_NRUNIT] = {
2068         {
2069                 "ppp0",         /* ppp */
2070                 0, 0, 0, 0,     /* memory */
2071                 0, 0,           /* base, irq */
2072                 0, 0, 0, NULL, ppp_init,
2073         }
2074         , { "ppp1" , 0, 0, 0, 0,  1, 0, 0, 0, 0, NULL, ppp_init }
2075         , { "ppp2" , 0, 0, 0, 0,  2, 0, 0, 0, 0, NULL, ppp_init }
2076         , { "ppp3" , 0, 0, 0, 0,  3, 0, 0, 0, 0, NULL, ppp_init }
2077 
2078 #ifdef PPP_PPP_LOTS
2079         , { "ppp4" , 0, 0, 0, 0,  4, 0, 0, 0, 0, NULL, ppp_init }
2080         , { "ppp5" , 0, 0, 0, 0,  5, 0, 0, 0, 0, NULL, ppp_init }
2081         , { "ppp6" , 0, 0, 0, 0,  6, 0, 0, 0, 0, NULL, ppp_init }
2082         , { "ppp7" , 0, 0, 0, 0,  7, 0, 0, 0, 0, NULL, ppp_init }
2083         , { "ppp8" , 0, 0, 0, 0,  8, 0, 0, 0, 0, NULL, ppp_init }
2084         , { "ppp9" , 0, 0, 0, 0,  9, 0, 0, 0, 0, NULL, ppp_init }
2085         , { "ppp10" , 0, 0, 0, 0, 10, 0, 0, 0, 0, NULL, ppp_init }
2086         , { "ppp11" , 0, 0, 0, 0, 11, 0, 0, 0, 0, NULL, ppp_init }
2087         , { "ppp12" , 0, 0, 0, 0, 12, 0, 0, 0, 0, NULL, ppp_init }
2088         , { "ppp13" , 0, 0, 0, 0, 13, 0, 0, 0, 0, NULL, ppp_init }
2089         , { "ppp14" , 0, 0, 0, 0, 14, 0, 0, 0, 0, NULL, ppp_init }
2090         , { "ppp15" , 0, 0, 0, 0, 15, 0, 0, 0, 0, NULL, ppp_init }
2091 #endif
2092 };
2093 
2094 int
2095 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2096 {
2097         int err;
2098         int i;
2099 
2100         for (i = 0; i < PPP_NRUNIT; i++)  {
2101                 if ((err = register_netdev(&dev_ppp[i])))  {
2102                         if (err == -EEXIST)  {
2103                                 printk("PPP: devices already present. Module not loaded.\n");
2104                         }
2105                         return err;
2106                 }
2107         }
2108         return 0;
2109 }
2110 
2111 void
2112 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2113 {
2114         int i;
2115 
2116         if (MOD_IN_USE)  {
2117                 printk("PPP: device busy, remove delayed\n");
2118                 return;
2119         }
2120         for (i = 0; i < PPP_NRUNIT; i++)  {
2121                 unregister_netdev(&dev_ppp[i]);
2122         }
2123         if ((i = tty_register_ldisc(N_PPP, NULL)))  {
2124                 printk("PPP: can't unregister line discipline (err = %d)\n", i);
2125         }
2126 }
2127 
2128 #endif

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