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

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