root/drivers/net/ppp.c

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

DEFINITIONS

This source file includes following definitions.
  1. ppp_init
  2. ppp_init_ctrl_blk
  3. ppp_changedmtu
  4. ppp_release
  5. ppp_close
  6. ppp_open
  7. ppp_dev_open
  8. ppp_dev_close
  9. ppp_dev_ioctl
  10. ppp_output_done
  11. ppp_kick_tty
  12. ppp_kick_tty
  13. ppp_write_wakeup
  14. ppp_enqueue
  15. ppp_dump_inqueue
  16. ppp_tty_input_ready
  17. ppp_unesc
  18. ppp_receive_room
  19. ppp_receive_buf
  20. ppp_doframe
  21. ppp_do_ip
  22. ppp_us_queue
  23. ppp_read
  24. ppp_stuff_char
  25. ppp_write
  26. ppp_ioctl
  27. ppp_select
  28. ppp_xmit
  29. ppp_type_trans
  30. ppp_header
  31. ppp_rebuild_header
  32. ppp_add_arp
  33. ppp_header
  34. ppp_rebuild_header
  35. ppp_get_stats
  36. ppp_find
  37. ppp_alloc
  38. ppp_lock
  39. ppp_unlock
  40. ppp_add_fcs
  41. ppp_check_fcs
  42. ppp_print_hex
  43. ppp_print_char
  44. ppp_print_buffer

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

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