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

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