root/drivers/net/ppp.c

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

DEFINITIONS

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

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

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