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

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

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