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

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