root/drivers/net/ppp.c

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

DEFINITIONS

This source file includes following definitions.
  1. store_long
  2. ppp_first_time
  3. ppp_init_dev
  4. ppp_init_ctrl_blk
  5. ppp_init
  6. ppp_alloc_buf
  7. ppp_free_buf
  8. lock_buffer
  9. ppp_changedmtu
  10. ppp_ccp_closed
  11. ppp_release
  12. ppp_tty_close_local
  13. ppp_tty_close
  14. ppp_tty_open
  15. ppp_tty_wakeup_code
  16. ppp_tty_wakeup
  17. ppp_kick_tty
  18. ppp_tty_room
  19. ppp_tty_receive
  20. ppp_rcv_rx
  21. rcv_proto_ip
  22. rcv_proto_ipx
  23. rcv_proto_vjc_comp
  24. rcv_proto_vjc_uncomp
  25. rcv_proto_unknown
  26. ppp_proto_ccp
  27. rcv_proto_ccp
  28. rcv_proto_lqr
  29. ppp_doframe_lower
  30. ppp_doframe
  31. ppp_tty_read
  32. ppp_stuff_char
  33. ppp_dev_xmit_lower
  34. ppp_dev_xmit_frame
  35. send_revise_frame
  36. ppp_tty_write
  37. ppp_set_compression
  38. ppp_tty_ioctl
  39. ppp_tty_select
  40. ppp_dev_open
  41. ppp_dev_close
  42. ppp_dev_ioctl_version
  43. ppp_dev_ioctl_stats
  44. ppp_dev_ioctl_comp_stats
  45. ppp_dev_ioctl
  46. ppp_dev_xmit_ip
  47. ppp_dev_xmit_ipx
  48. ppp_dev_xmit
  49. ppp_dev_stats
  50. ppp_dev_header
  51. ppp_dev_rebuild
  52. ppp_find
  53. ppp_alloc
  54. ppp_print_hex
  55. ppp_print_char
  56. ppp_print_buffer
  57. find_compressor
  58. ppp_register_compressor
  59. ppp_unregister_compressor
  60. init_module
  61. cleanup_module

   1 /*  PPP for Linux
   2  *
   3  *  Michael Callahan <callahan@maths.ox.ac.uk>
   4  *  Al Longyear <longyear@netcom.com>
   5  *
   6  *  Dynamic PPP devices by Jim Freeman <jfree@caldera.com>.
   7  *  ppp_tty_receive ``noisy-raise-bug'' fixed by Ove Ewerlid <ewerlid@syscon.uu.se>
   8  *
   9  *  ==FILEVERSION 960303==
  10  *
  11  *  NOTE TO MAINTAINERS:
  12  *     If you modify this file at all, please set the number above to the
  13  *     date of the modification as YYMMDD (year month day).
  14  *     ppp.c is shipped with a PPP distribution as well as with the kernel;
  15  *     if everyone increases the FILEVERSION number above, then scripts
  16  *     can do the right thing when deciding whether to install a new ppp.c
  17  *     file.  Don't change the format of that line otherwise, so the
  18  *     installation script can recognize it.
  19  */
  20 
  21 /*
  22    Sources:
  23 
  24    slip.c
  25 
  26    RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
  27    Multi-protocol Datagrams over Point-to-Point Links
  28 
  29    RFC1332: IPCP
  30 
  31    ppp-2.0
  32 
  33    Flags for this module (any combination is acceptable for testing.):
  34 
  35    OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
  36                         character. This is normally set to ((HZ * 3) / 2).
  37                         This is 1.5 seconds. If zero then the leading
  38                         flag is always sent.
  39 
  40    CHECK_CHARACTERS   - Enable the checking on all received characters for
  41                         8 data bits, no parity. This adds a small amount of
  42                         processing for each received character.
  43 */
  44 
  45 #define OPTIMIZE_FLAG_TIME      ((HZ * 3)/2)
  46 
  47 #define CHECK_CHARACTERS        1
  48 #define PPP_COMPRESS            1
  49 
  50 #ifndef PPP_MAX_DEV
  51 #define PPP_MAX_DEV     256
  52 #endif
  53 
  54 /* $Id: ppp.c,v 1.5 1995/06/12 11:36:53 paulus Exp $
  55  * Added dynamic allocation of channels to eliminate
  56  *   compiled-in limits on the number of channels.
  57  *
  58  * Dynamic channel allocation code Copyright 1995 Caldera, Inc.,
  59  *   released under the GNU General Public License Version 2.
  60  */
  61 
  62 #include <linux/module.h>
  63 #include <endian.h>
  64 #include <linux/kernel.h>
  65 #include <linux/sched.h>
  66 #include <linux/types.h>
  67 #include <linux/fcntl.h>
  68 #include <linux/interrupt.h>
  69 #include <linux/ptrace.h>
  70 #include <linux/ioport.h>
  71 #include <linux/in.h>
  72 #include <linux/malloc.h>
  73 #include <linux/tty.h>
  74 #include <linux/errno.h>
  75 #include <linux/sched.h>        /* to get the struct task_struct */
  76 #include <linux/string.h>       /* used in new tty drivers */
  77 #include <linux/signal.h>       /* used in new tty drivers */
  78 #include <asm/system.h>
  79 #include <asm/bitops.h>
  80 #include <asm/segment.h>
  81 #include <linux/if.h>
  82 #include <linux/if_ether.h>
  83 #include <linux/netdevice.h>
  84 #include <linux/skbuff.h>
  85 #include <linux/inet.h>
  86 #include <linux/ioctl.h>
  87 
  88 typedef struct sk_buff       sk_buff;
  89 #define skb_data(skb)        ((__u8 *) (skb)->data)
  90 
  91 #include <linux/ip.h>
  92 #include <linux/tcp.h>
  93 #include <linux/if_arp.h>
  94 #include "slhc.h"
  95 
  96 #define fcstab  ppp_crc16_table         /* Name of the table in the kernel */
  97 #include <linux/ppp_defs.h>
  98 
  99 #include <linux/socket.h>
 100 #include <linux/if_ppp.h>
 101 #include <linux/if_pppvar.h>
 102 
 103 #undef   PACKETPTR
 104 #define  PACKETPTR 1
 105 #include <linux/ppp-comp.h>
 106 #undef   PACKETPTR
 107 
 108 #define bsd_decompress  (*ppp->sc_rcomp->decompress)
 109 #define bsd_compress    (*ppp->sc_xcomp->compress)
 110 
 111 #ifndef PPP_IPX
 112 #define PPP_IPX 0x2b  /* IPX protocol over PPP */
 113 #endif
 114 
 115 #ifndef PPP_LQR
 116 #define PPP_LQR 0xc025  /* Link Quality Reporting Protocol */
 117 #endif
 118 
 119 static int ppp_register_compressor (struct compressor *cp);
 120 static void ppp_unregister_compressor (struct compressor *cp);
 121 
 122 /*
 123  * Local functions
 124  */
 125 
 126 static struct compressor *find_compressor (int type);
 127 static void ppp_init_ctrl_blk (register struct ppp *);
 128 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
 129 static int ppp_doframe (struct ppp *);
 130 static struct ppp *ppp_alloc (void);
 131 static struct ppp *ppp_find (int pid_value);
 132 static void ppp_print_buffer (const __u8 *, const __u8 *, int);
 133 extern inline void ppp_stuff_char (struct ppp *ppp,
 134                                    register struct ppp_buffer *buf,
 135                                    register __u8 chr);
 136 extern inline int lock_buffer (register struct ppp_buffer *buf);
 137 
 138 static int rcv_proto_ip         (struct ppp *, __u16, __u8 *, int);
 139 static int rcv_proto_ipx        (struct ppp *, __u16, __u8 *, int);
 140 static int rcv_proto_vjc_comp   (struct ppp *, __u16, __u8 *, int);
 141 static int rcv_proto_vjc_uncomp (struct ppp *, __u16, __u8 *, int);
 142 static int rcv_proto_unknown    (struct ppp *, __u16, __u8 *, int);
 143 static int rcv_proto_lqr        (struct ppp *, __u16, __u8 *, int);
 144 static void ppp_doframe_lower   (struct ppp *, __u8 *, int);
 145 static int ppp_doframe          (struct ppp *);
 146 
 147 extern int  ppp_bsd_compressor_init(void);
 148 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd);
 149 static int  rcv_proto_ccp (struct ppp *, __u16, __u8 *, int);
 150 
 151 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
 152 
 153 #ifndef OPTIMIZE_FLAG_TIME
 154 #define OPTIMIZE_FLAG_TIME      0
 155 #endif
 156 
 157 #ifndef PPP_MAX_DEV
 158 #define PPP_MAX_DEV 256
 159 #endif
 160 
 161 /*
 162  * Parameters which may be changed via insmod.
 163  */
 164 
 165 static int  flag_time = OPTIMIZE_FLAG_TIME;
 166 static int  max_dev   = PPP_MAX_DEV;
 167 
 168 /*
 169  * The "main" procedure to the ppp device
 170  */
 171 
 172 int ppp_init (struct device *);
 173 
 174 /*
 175  * Network device driver callback routines
 176  */
 177 
 178 static int ppp_dev_open (struct device *);
 179 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
 180 static int ppp_dev_close (struct device *);
 181 static int ppp_dev_xmit (sk_buff *, struct device *);
 182 static struct enet_statistics *ppp_dev_stats (struct device *);
 183 static int ppp_dev_header (sk_buff *, struct device *, __u16,
 184                            void *, void *, unsigned int);
 185 static int ppp_dev_rebuild (void *eth, struct device *dev,
 186                             unsigned long raddr, struct sk_buff *skb);
 187 /*
 188  * TTY callbacks
 189  */
 190 
 191 static int ppp_tty_read (struct tty_struct *, struct file *, __u8 *,
 192                          unsigned int);
 193 static int ppp_tty_write (struct tty_struct *, struct file *, const __u8 *,
 194                           unsigned int);
 195 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
 196                           unsigned long);
 197 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
 198                       struct file *filp, int sel_type, select_table * wait);
 199 static int ppp_tty_open (struct tty_struct *);
 200 static void ppp_tty_close (struct tty_struct *);
 201 static int ppp_tty_room (struct tty_struct *tty);
 202 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
 203                              char *fp, int count);
 204 static void ppp_tty_wakeup (struct tty_struct *tty);
 205 
 206 #define CHECK_PPP(a)  if (!ppp->inuse) { printk (ppp_warning, __LINE__); return a;}
 207 #define CHECK_PPP_VOID()  if (!ppp->inuse) { printk (ppp_warning, __LINE__); return;}
 208 
 209 #define in_xmap(ppp,c)  (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
 210 #define in_rmap(ppp,c)  ((((unsigned int) (__u8) (c)) < 0x20) && \
 211                         ppp->recv_async_map & (1 << (c)))
 212 
 213 #define bset(p,b)       ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
 214 
 215 #define tty2ppp(tty)    ((struct ppp *) (tty->disc_data))
 216 #define dev2ppp(dev)    ((struct ppp *) (dev->priv))
 217 #define ppp2tty(ppp)    ((struct tty_struct *) ppp->tty)
 218 #define ppp2dev(ppp)    ((struct device *) ppp->dev)
 219 
 220 struct ppp_hdr {
 221         __u8 address;
 222         __u8 control;
 223         __u8 protocol[2];
 224 };
 225 
 226 #define PPP_HARD_HDR_LEN        (sizeof (struct ppp_hdr))
 227 
 228 typedef struct  ppp_ctrl {
 229         struct ppp_ctrl *next;          /* Next structure in the list   */
 230         char            name [8];       /* Name of the device           */
 231         struct ppp      ppp;            /* PPP control table            */
 232         struct device   dev;            /* Device information table     */
 233 } ppp_ctrl_t;
 234 
 235 static ppp_ctrl_t *ppp_list = NULL;
 236 
 237 #define ctl2ppp(ctl) (struct ppp *)    &ctl->ppp
 238 #define ctl2dev(ctl) (struct device *) &ctl->dev
 239 #undef  PPP_NRUNIT
 240 
 241 /* Buffer types */
 242 #define BUFFER_TYPE_DEV_RD      0  /* ppp read buffer       */
 243 #define BUFFER_TYPE_TTY_WR      1  /* tty write buffer      */
 244 #define BUFFER_TYPE_DEV_WR      2  /* ppp write buffer      */
 245 #define BUFFER_TYPE_TTY_RD      3  /* tty read buffer       */
 246 #define BUFFER_TYPE_VJ          4  /* vj compression buffer */
 247 
 248 /* Define this string only once for all macro envocations */
 249 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
 250 
 251 static char szVersion[]         = PPP_VERSION;
 252  
 253 /*
 254  * Information for the protocol decoder
 255  */
 256 
 257 typedef int (*pfn_proto)  (struct ppp *, __u16, __u8 *, int);
 258 
 259 typedef struct ppp_proto_struct {
 260         int             proto;
 261         pfn_proto       func;
 262 } ppp_proto_type;
 263 
 264 static
 265 ppp_proto_type proto_list[] = {
 266         { PPP_IP,         rcv_proto_ip         },
 267         { PPP_IPX,        rcv_proto_ipx        },
 268         { PPP_VJC_COMP,   rcv_proto_vjc_comp   },
 269         { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
 270         { PPP_LQR,        rcv_proto_lqr        },
 271         { PPP_CCP,        rcv_proto_ccp        },
 272         { 0,              rcv_proto_unknown    }  /* !!! MUST BE LAST !!! */
 273 };
 274 
 275 __u16 ppp_crc16_table[256] =
 276 {
 277         0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
 278         0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
 279         0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
 280         0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
 281         0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
 282         0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
 283         0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
 284         0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
 285         0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
 286         0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
 287         0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
 288         0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
 289         0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
 290         0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
 291         0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
 292         0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
 293         0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
 294         0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
 295         0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
 296         0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
 297         0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
 298         0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
 299         0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
 300         0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
 301         0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
 302         0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
 303         0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
 304         0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
 305         0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
 306         0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
 307         0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
 308         0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
 309 };
 310 
 311 #ifdef CHECK_CHARACTERS
 312 static __u32 paritytab[8] =
 313 {
 314         0x96696996, 0x69969669, 0x69969669, 0x96696996,
 315         0x69969669, 0x96696996, 0x96696996, 0x69969669
 316 };
 317 #endif
 318 
 319 /* local function to store a value into the LQR frame */
 320 extern inline __u8 * store_long (register __u8 *p, register int value) {
     /* [previous][next][first][last][top][bottom][index][help] */
 321         *p++ = (__u8) (value >> 24);
 322         *p++ = (__u8) (value >> 16);
 323         *p++ = (__u8) (value >>  8);
 324         *p++ = (__u8) value;
 325         return p;
 326 }
 327 
 328 /*************************************************************
 329  * INITIALIZATION
 330  *************************************************************/
 331 
 332 /* This procedure is called once and once only to define who we are to
 333  * the operating system and the various procedures that it may use in
 334  * accessing the ppp protocol.
 335  */
 336 
 337 static int
 338 ppp_first_time (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 339 {
 340         static struct tty_ldisc ppp_ldisc;
 341         int    status;
 342 
 343         printk (KERN_INFO
 344                 "PPP: version %s (dynamic channel allocation)"
 345                 "\n", szVersion);
 346 
 347 #ifndef MODULE /* slhc module logic has its own copyright announcment */
 348         printk (KERN_INFO
 349                 "TCP compression code copyright 1989 Regents of the "
 350                 "University of California\n");
 351 #endif
 352         
 353         printk (KERN_INFO
 354                 "PPP Dynamic channel allocation code copyright 1995 "
 355                 "Caldera, Inc.\n");
 356 /*
 357  * Register the tty dicipline
 358  */     
 359         (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
 360         ppp_ldisc.magic         = TTY_LDISC_MAGIC;
 361         ppp_ldisc.open          = ppp_tty_open;
 362         ppp_ldisc.close         = ppp_tty_close;
 363         ppp_ldisc.read          = ppp_tty_read;
 364         ppp_ldisc.write         = ppp_tty_write;
 365         ppp_ldisc.ioctl         = ppp_tty_ioctl;
 366         ppp_ldisc.select        = ppp_tty_select;
 367         ppp_ldisc.receive_room  = ppp_tty_room;
 368         ppp_ldisc.receive_buf   = ppp_tty_receive;
 369         ppp_ldisc.write_wakeup  = ppp_tty_wakeup;
 370         
 371         status = tty_register_ldisc (N_PPP, &ppp_ldisc);
 372         if (status == 0)
 373                 printk (KERN_INFO "PPP line discipline registered.\n");
 374         else
 375                 printk (KERN_ERR "error registering line discipline: %d\n",
 376                         status);
 377         return status;
 378 }
 379 
 380 /*************************************************************
 381  * INITIALIZATION
 382  *************************************************************/
 383 
 384 /* called when the device is actually created */
 385 
 386 static int
 387 ppp_init_dev (struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 388 {
 389         int    indx;
 390 
 391         dev->hard_header      = ppp_dev_header;
 392         dev->rebuild_header   = ppp_dev_rebuild;
 393         dev->hard_header_len  = PPP_HARD_HDR_LEN;
 394 
 395         /* device INFO */
 396         dev->mtu              = PPP_MTU;
 397         dev->hard_start_xmit  = ppp_dev_xmit;
 398         dev->open             = ppp_dev_open;
 399         dev->stop             = ppp_dev_close;
 400         dev->get_stats        = ppp_dev_stats;
 401         dev->do_ioctl         = ppp_dev_ioctl;
 402         dev->addr_len         = 0;
 403         dev->type             = ARPHRD_PPP;
 404 
 405         for (indx = 0; indx < DEV_NUMBUFFS; indx++)
 406                 skb_queue_head_init (&dev->buffs[indx]);
 407 
 408         /* New-style flags */
 409         dev->flags      = IFF_POINTOPOINT;
 410         dev->family     = AF_INET;
 411         dev->pa_addr    = 0;
 412         dev->pa_brdaddr = 0;
 413         dev->pa_mask    = 0;
 414         dev->pa_alen    = 4; /* sizeof (__u32) */
 415 
 416         return 0;
 417 }
 418 
 419 /*
 420  * Local procedure to initialize the ppp structure
 421  */
 422 
 423 static void
 424 ppp_init_ctrl_blk (register struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
 425 {
 426         ppp->magic  = PPP_MAGIC;
 427         ppp->toss   = 0xE0;
 428         ppp->escape = 0;
 429 
 430         ppp->flags  = 0;
 431         ppp->mtu    = PPP_MTU;
 432         ppp->mru    = PPP_MRU;
 433 
 434         memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
 435         ppp->xmit_async_map[0] = 0xffffffff;
 436         ppp->xmit_async_map[3] = 0x60000000;
 437         ppp->recv_async_map    = 0x00000000;
 438 
 439         ppp->rbuf       = NULL;
 440         ppp->wbuf       = NULL;
 441         ppp->ubuf       = NULL;
 442         ppp->cbuf       = NULL;
 443         ppp->slcomp     = NULL;
 444         ppp->read_wait  = NULL;
 445         ppp->write_wait = NULL;
 446         ppp->last_xmit  = jiffies - flag_time;
 447 
 448         /* clear statistics */
 449         memset (&ppp->stats, '\0', sizeof (struct pppstat));
 450 
 451         /* Reset the demand dial information */
 452         ppp->ddinfo.xmit_idle=         /* time since last NP packet sent */
 453         ppp->ddinfo.recv_idle=jiffies; /* time since last NP packet received */
 454 
 455         /* PPP compression data */
 456         ppp->sc_xc_state =
 457         ppp->sc_rc_state = NULL;
 458 }
 459 
 460 static struct symbol_table ppp_syms = {
 461 #include <linux/symtab_begin.h>
 462         X(ppp_register_compressor),
 463         X(ppp_unregister_compressor),
 464         X(ppp_crc16_table),
 465 #include <linux/symtab_end.h>
 466 };
 467 
 468 /* called at boot/load time for each ppp device defined in the kernel */
 469 
 470 #ifndef MODULE
 471 int
 472 ppp_init (struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 473 {
 474         static int first_time = 1;
 475         int    answer = 0;
 476 
 477         if (first_time) {
 478                 first_time = 0;
 479                 answer     = ppp_first_time();
 480                 if (answer == 0)
 481                         (void) register_symtab (&ppp_syms);
 482         }
 483         if (answer == 0)
 484                 answer = -ENODEV;
 485         return answer;
 486 }
 487 #endif
 488 
 489 /*
 490  * Routine to allocate a buffer for later use by the driver.
 491  */
 492 
 493 static struct ppp_buffer *
 494 ppp_alloc_buf (int size, int type)
     /* [previous][next][first][last][top][bottom][index][help] */
 495 {
 496         struct ppp_buffer *buf;
 497 
 498         buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
 499                                              GFP_ATOMIC);
 500 
 501         if (buf != NULL) {
 502                 buf->size   = size - 1; /* Mask for the buffer size */
 503                 buf->type   = type;
 504                 buf->locked = 0;
 505                 buf->count  = 0;
 506                 buf->head   = 0;
 507                 buf->tail   = 0;
 508                 buf->fcs    = PPP_INITFCS;
 509 
 510         }
 511         return (buf);
 512 }
 513 
 514 /*
 515  * Routine to release the allocated buffer.
 516  */
 517 
 518 static void
 519 ppp_free_buf (struct ppp_buffer *ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 520 {
 521         if (ptr != NULL)
 522                 kfree (ptr);
 523 }
 524 
 525 /*
 526  * Lock the indicated transmit buffer
 527  */
 528 
 529 extern inline int
 530 lock_buffer (register struct ppp_buffer *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 531 {
 532         register int state;
 533         int flags;
 534 /*
 535  * Save the current state and if free then set it to the "busy" state
 536  */
 537         save_flags (flags);
 538         cli ();
 539         state = buf->locked;
 540         if (state == 0)
 541                 buf->locked = 2;
 542 
 543         restore_flags (flags);
 544         return (state);
 545 }
 546 
 547 /*
 548  * MTU has been changed by the IP layer. Unfortunately we are not told
 549  * about this, but we spot it ourselves and fix things up. We could be
 550  * in an upcall from the tty driver, or in an ip packet queue.
 551  */
 552 
 553 static int
 554 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
     /* [previous][next][first][last][top][bottom][index][help] */
 555 {
 556         struct device *dev;
 557 
 558         struct ppp_buffer *new_rbuf;
 559         struct ppp_buffer *new_wbuf;
 560         struct ppp_buffer *new_cbuf;
 561         struct ppp_buffer *new_tbuf;
 562 
 563         struct ppp_buffer *old_rbuf;
 564         struct ppp_buffer *old_wbuf;
 565         struct ppp_buffer *old_cbuf;
 566         struct ppp_buffer *old_tbuf;
 567 
 568         int mtu, mru;
 569 /*
 570  *  Allocate the buffer from the kernel for the data
 571  */
 572         dev = ppp2dev (ppp);
 573         mru = new_mru;
 574         /* allow for possible escapement of every character */
 575         mtu = (new_mtu * 2) + 20;
 576 
 577         /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
 578         if (mru < PPP_MRU)
 579                 mru = PPP_MRU;
 580 
 581         mru += 10;
 582         
 583         if (ppp->flags & SC_DEBUG)
 584                 printk (KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
 585                         dev->name, new_mtu, new_mru);
 586 
 587         new_wbuf = ppp_alloc_buf (mtu+PPP_HARD_HDR_LEN, BUFFER_TYPE_DEV_WR);
 588         new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24,   BUFFER_TYPE_TTY_WR);
 589         new_rbuf = ppp_alloc_buf (mru + 84,             BUFFER_TYPE_DEV_RD);
 590         new_cbuf = ppp_alloc_buf (mru+PPP_HARD_HDR_LEN, BUFFER_TYPE_VJ);
 591 /*
 592  *  If the buffers failed to allocate then complain and release the partial
 593  *  allocations.
 594  */
 595         if (new_wbuf == NULL || new_tbuf == NULL ||
 596             new_rbuf == NULL || new_cbuf == NULL) {
 597                 if (ppp->flags & SC_DEBUG)
 598                         printk (KERN_ERR
 599                                 "ppp: failed to allocate new buffers\n");
 600 
 601                 ppp_free_buf (new_wbuf);
 602                 ppp_free_buf (new_tbuf);
 603                 ppp_free_buf (new_rbuf);
 604                 ppp_free_buf (new_cbuf);
 605                 return 0;
 606         }
 607 /*
 608  *  Update the pointers to the new buffer structures.
 609  */
 610         cli ();
 611         old_wbuf = ppp->wbuf;
 612         old_rbuf = ppp->rbuf;
 613         old_cbuf = ppp->cbuf;
 614         old_tbuf = ppp->tbuf;
 615 
 616         ppp->wbuf = new_wbuf;
 617         ppp->rbuf = new_rbuf;
 618         ppp->cbuf = new_cbuf;
 619         ppp->tbuf = new_tbuf;
 620 
 621         ppp->rbuf->size -= 80;  /* reserve space for vj header expansion */
 622 
 623         dev->mem_start  = (__u32) buf_base (new_wbuf);
 624         dev->mem_end    = (__u32) (dev->mem_start + mtu);
 625         dev->rmem_start = (__u32) buf_base (new_rbuf);
 626         dev->rmem_end   = (__u32) (dev->rmem_start + mru);
 627 /*
 628  *  Update the parameters for the new buffer sizes
 629  */
 630         ppp->toss   = 0xE0;     /* To ignore characters until new FLAG */
 631         ppp->escape = 0;        /* No pending escape character */
 632 
 633         dev->mtu    =
 634         ppp->mtu    = new_mtu;
 635         ppp->mru    = new_mru;
 636 
 637         ppp->s1buf  = NULL;
 638         ppp->s2buf  = NULL;
 639         ppp->xbuf   = NULL;
 640 
 641         ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 642         ppp->flags      &= ~SC_XMIT_BUSY;
 643 
 644         sti ();
 645 /*
 646  *  Release old buffer pointers
 647  */
 648         ppp_free_buf (old_rbuf);
 649         ppp_free_buf (old_wbuf);
 650         ppp_free_buf (old_cbuf);
 651         ppp_free_buf (old_tbuf);
 652         return 1;
 653 }
 654 
 655 /*
 656  * CCP is down; free (de)compressor state if necessary.
 657  */
 658 
 659 static void
 660 ppp_ccp_closed (struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
 661 {
 662         if (ppp->sc_xc_state) {
 663                 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
 664                 ppp->sc_xc_state = NULL;
 665         }
 666 
 667         if (ppp->sc_rc_state) {
 668                 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
 669                 ppp->sc_rc_state = NULL;
 670         }
 671 }
 672 
 673 /*
 674  * Called to release all of the information in the current PPP structure.
 675  *
 676  * It is called when the ppp device goes down or if it is unable to go
 677  * up.
 678  */
 679 
 680 static void
 681 ppp_release (struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
 682 {
 683         struct tty_struct *tty;
 684         struct device *dev;
 685 
 686         tty = ppp2tty (ppp);
 687         dev = ppp2dev (ppp);
 688 
 689         ppp_ccp_closed (ppp);
 690 
 691         /* Ensure that the pppd process is not hanging on select() */
 692         wake_up_interruptible (&ppp->read_wait);
 693         wake_up_interruptible (&ppp->write_wait);
 694 
 695         if (tty != NULL && tty->disc_data == ppp)
 696                 tty->disc_data = NULL;  /* Break the tty->ppp link */
 697 
 698         if (dev && dev->flags & IFF_UP) {
 699                 dev_close (dev); /* close the device properly */
 700                 dev->flags = 0;  /* prevent recursion */
 701         }
 702 
 703         ppp_free_buf (ppp->rbuf);
 704         ppp_free_buf (ppp->wbuf);
 705         ppp_free_buf (ppp->cbuf);
 706         ppp_free_buf (ppp->ubuf);
 707         ppp_free_buf (ppp->tbuf);
 708 
 709         ppp->rbuf  =
 710         ppp->wbuf  =
 711         ppp->cbuf  =
 712         ppp->tbuf  =
 713         ppp->xbuf  =
 714         ppp->s1buf =
 715         ppp->s2buf =
 716         ppp->ubuf  = NULL;
 717 
 718         if (ppp->slcomp) {
 719                 slhc_free (ppp->slcomp);
 720                 ppp->slcomp = NULL;
 721         }
 722 
 723         ppp->inuse = 0;
 724         ppp->tty   = NULL;
 725 }
 726 
 727 /*
 728  * Device callback.
 729  *
 730  * Called when the PPP device goes down in response to an ifconfig request.
 731  */
 732 
 733 static void
 734 ppp_tty_close_local (struct tty_struct *tty, int sc_xfer)
     /* [previous][next][first][last][top][bottom][index][help] */
 735 {
 736         struct ppp *ppp = tty2ppp (tty);
 737 
 738         if (ppp != NULL) {
 739                 if (ppp->magic != PPP_MAGIC) {
 740                         if (ppp->flags & SC_DEBUG)
 741                                 printk (KERN_WARNING
 742                                        "ppp: trying to close unopened tty!\n");
 743                 } else {
 744                         CHECK_PPP_VOID();
 745                         ppp->sc_xfer = sc_xfer;
 746                         if (ppp->flags & SC_DEBUG)
 747                                 printk (KERN_INFO "ppp: channel %s closing.\n",
 748                                         ppp2dev(ppp) -> name);
 749                         ppp_release (ppp);
 750                         MOD_DEC_USE_COUNT;
 751                 }
 752         }
 753 }
 754 
 755 static void
 756 ppp_tty_close (struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 757 {
 758         ppp_tty_close_local (tty, 0);
 759 }
 760 
 761 /*
 762  * TTY callback.
 763  *
 764  * Called when the tty discipline is switched to PPP.
 765  */
 766 
 767 static int
 768 ppp_tty_open (struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 769 {
 770         struct ppp *ppp = tty2ppp (tty);
 771         int indx;
 772 /*
 773  * There should not be an existing table for this slot.
 774  */
 775         if (ppp) {
 776                 if (ppp->flags & SC_DEBUG)
 777                         printk (KERN_ERR
 778                         "ppp_tty_open: gack! tty already associated to %s!\n",
 779                         ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
 780                                                 : "unknown");
 781                 return -EEXIST;
 782         }
 783 /*
 784  * Allocate the structure from the system
 785  */
 786         ppp = ppp_find(current->pid);
 787         if (ppp == NULL) {
 788                 ppp = ppp_find(0);
 789                 if (ppp == NULL)
 790                         ppp = ppp_alloc();
 791         }
 792 
 793         if (ppp == NULL) {
 794                 if (ppp->flags & SC_DEBUG)
 795                         printk (KERN_ERR
 796                         "ppp_tty_open: couldn't allocate ppp channel\n");
 797                 return -ENFILE;
 798         }
 799 /*
 800  * Initialize the control block
 801  */
 802         ppp_init_ctrl_blk (ppp);
 803         ppp->tty       = tty;
 804         tty->disc_data = ppp;
 805 /*
 806  * Flush any pending characters in the driver and discipline.
 807  */
 808         if (tty->ldisc.flush_buffer)
 809                 tty->ldisc.flush_buffer (tty);
 810 
 811         if (tty->driver.flush_buffer)
 812                 tty->driver.flush_buffer (tty);
 813 /*
 814  * Allocate space for the default VJ header compression slots
 815  */
 816         ppp->slcomp = slhc_init (16, 16);
 817         if (ppp->slcomp == NULL) {
 818                 if (ppp->flags & SC_DEBUG)
 819                         printk (KERN_ERR
 820                         "ppp_tty_open: no space for compression buffers!\n");
 821                 ppp_release (ppp);
 822                 return -ENOMEM;
 823         }
 824 /*
 825  * Allocate space for the MTU and MRU buffers
 826  */
 827         if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
 828                 ppp_release (ppp);
 829                 return -ENOMEM;
 830         }
 831 /*
 832  * Allocate space for a user level buffer
 833  */
 834         ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
 835         if (ppp->ubuf == NULL) {
 836                 if (ppp->flags & SC_DEBUG)
 837                         printk (KERN_ERR
 838                        "ppp_tty_open: no space for user receive buffer\n");
 839                 ppp_release (ppp);
 840                 return -ENOMEM;
 841         }
 842 
 843         if (ppp->flags & SC_DEBUG)
 844                 printk (KERN_INFO "ppp: channel %s open\n",
 845                         ppp2dev(ppp)->name);
 846 
 847         for (indx = 0; indx < NUM_NP; ++indx)
 848                 ppp->sc_npmode[indx] = NPMODE_PASS;
 849 
 850         MOD_INC_USE_COUNT;
 851         return (ppp->line);
 852 }
 853 
 854 /*
 855  * Local function to send the next portion of the buffer.
 856  *
 857  * Called by the tty driver's tty_wakeup function should it be entered
 858  * because the partial buffer was transmitted.
 859  *
 860  * Called by kick_tty to send the initial portion of the buffer.
 861  *
 862  * Completion processing of the buffer transmission is handled here.
 863  */
 864 
 865 static void
 866 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
     /* [previous][next][first][last][top][bottom][index][help] */
 867                      struct ppp_buffer *xbuf)
 868 {
 869         register int count, actual;
 870 /*
 871  * Prevent re-entrancy by ensuring that this routine is called only once.
 872  */
 873         cli ();
 874         if (ppp->flags & SC_XMIT_BUSY) {
 875                 sti ();
 876                 return;
 877         }
 878         ppp->flags |= SC_XMIT_BUSY;
 879         sti ();
 880 /*
 881  * Send the next block of data to the modem
 882  */
 883         count = xbuf->count - xbuf->tail;
 884         actual = tty->driver.write (tty, 0,
 885                                     buf_base (xbuf) + xbuf->tail, count);
 886 /*
 887  * Terminate transmission of any block which may have an error.
 888  * This could occur should the carrier drop.
 889  */
 890         if (actual < 0) {
 891                 ppp->stats.ppp_oerrors++;
 892                 actual = count;
 893         } else
 894                 ppp->bytes_sent += actual;
 895 /*
 896  * If the buffer has been transmitted then clear the indicators.
 897  */
 898         xbuf->tail += actual;
 899         if (actual == count) {
 900                 xbuf = NULL;
 901                 ppp->flags &= ~SC_XMIT_BUSY;
 902 /*
 903  * Complete the transmission on the current buffer.
 904  */
 905                 xbuf = ppp->xbuf;
 906                 if (xbuf != NULL) {
 907                         tty->flags  &= ~(1 << TTY_DO_WRITE_WAKEUP);
 908                         xbuf->locked = 0;
 909                         ppp->xbuf    = NULL;
 910 /*
 911  * If the completed buffer came from the device write, then complete the
 912  * transmission block.
 913  */
 914                         if (ppp2dev (ppp) -> flags & IFF_UP) {
 915                                 if (xbuf->type == BUFFER_TYPE_DEV_WR)
 916                                         ppp2dev (ppp)->tbusy = 0;
 917                                 mark_bh (NET_BH);
 918                         }
 919 /*
 920  * Wake up the transmission queue for all completion events.
 921  */
 922                         wake_up_interruptible (&ppp->write_wait);
 923 /*
 924  * Look at the priorities. Choose a daemon write over the device driver.
 925  */
 926                         cli();
 927                         xbuf = ppp->s1buf;
 928                         ppp->s1buf = NULL;
 929                         if (xbuf == NULL) {
 930                                 xbuf = ppp->s2buf;
 931                                 ppp->s2buf = NULL;
 932                         }
 933                         sti();
 934 /*
 935  * If there is a pending buffer then transmit it now.
 936  */
 937                         if (xbuf != NULL) {
 938                                 ppp->flags &= ~SC_XMIT_BUSY;
 939                                 ppp_kick_tty (ppp, xbuf);
 940                                 return;
 941                         }
 942                 }
 943         }
 944 /*
 945  * Clear the re-entry flag
 946  */
 947         ppp->flags &= ~SC_XMIT_BUSY;
 948 }
 949 
 950 /*
 951  * This function is called by the tty driver when the transmit buffer has
 952  * additional space. It is used by the ppp code to continue to transmit
 953  * the current buffer should the buffer have been partially sent.
 954  *
 955  * In addition, it is used to send the first part of the buffer since the
 956  * logic and the inter-locking would be identical.
 957  */
 958 
 959 static void
 960 ppp_tty_wakeup (struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 961 {
 962         struct ppp_buffer *xbuf;
 963         struct ppp *ppp = tty2ppp (tty);
 964 
 965         if (!ppp)
 966                 return;
 967 
 968         if (ppp->magic != PPP_MAGIC)
 969                 return;
 970 /*
 971  * Ensure that there is a transmission pending. Clear the re-entry flag if
 972  * there is no pending buffer. Otherwise, send the buffer.
 973  */
 974         xbuf = ppp->xbuf;
 975         if (xbuf == NULL)
 976                 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
 977         else
 978                 ppp_tty_wakeup_code (ppp, tty, xbuf);
 979 }
 980 
 981 /*
 982  * This function is called to transmit a buffer to the remote. The buffer
 983  * is placed on the pending queue if there is presently a buffer being
 984  * sent or it is transmitted with the aid of ppp_tty_wakeup.
 985  */
 986 
 987 static void
 988 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
     /* [previous][next][first][last][top][bottom][index][help] */
 989 {
 990         register int flags;
 991 /*
 992  * Hold interrupts.
 993  */
 994         save_flags (flags);
 995         cli ();
 996 /*
 997  * Control the flags which are best performed with the interrupts masked.
 998  */
 999         xbuf->locked     = 1;
1000         xbuf->tail       = 0;
1001 /*
1002  * If the transmitter is busy then place the buffer on the appropriate
1003  * priority queue.
1004  */
1005         if (ppp->xbuf != NULL) {
1006                 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1007                         ppp->s1buf = xbuf;
1008                 else
1009                         ppp->s2buf = xbuf;
1010                 restore_flags (flags);
1011                 return;
1012         }
1013 /*
1014  * If the transmitter is not busy then this is the highest priority frame
1015  */
1016         ppp->flags      &= ~SC_XMIT_BUSY;
1017         ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1018         ppp->xbuf        = xbuf;
1019         restore_flags (flags);
1020 /*
1021  * Do the "tty wakeup_code" to actually send this buffer.
1022  */
1023         ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1024 }
1025 
1026 /*************************************************************
1027  * TTY INPUT
1028  *    The following functions handle input that arrives from
1029  *    the TTY.  It recognizes PPP frames and either hands them
1030  *    to the network layer or queues them for delivery to a
1031  *    user process reading this TTY.
1032  *************************************************************/
1033 
1034 /*
1035  * Callback function from tty driver. Return the amount of space left
1036  * in the receiver's buffer to decide if remote transmitter is to be
1037  * throttled.
1038  */
1039 
1040 static int
1041 ppp_tty_room (struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
1042 {
1043         return 65536;       /* We can handle an infinite amount of data. :-) */
1044 }
1045 
1046 /*
1047  * Callback function when data is available at the tty driver.
1048  */
1049 
1050 static void
1051 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
     /* [previous][next][first][last][top][bottom][index][help] */
1052                  char *flags, int count)
1053 {
1054         register struct ppp *ppp = tty2ppp (tty);
1055         register struct ppp_buffer *buf = NULL;
1056         __u8 chr;
1057 /*
1058  * Fetch the pointer to the buffer. Be careful about race conditions.
1059  */
1060         if (ppp != NULL)
1061                 buf = ppp->rbuf;
1062 
1063         if (buf == NULL)
1064                 return;
1065 /*
1066  * Verify the table pointer and ensure that the line is
1067  * still in PPP discipline.
1068  */
1069         if (ppp->magic != PPP_MAGIC) {
1070                 if (ppp->flags & SC_DEBUG)
1071                         printk (KERN_DEBUG
1072                                 "PPP: handler called but couldn't find "
1073                                 "PPP struct.\n");
1074                 return;
1075         }
1076         CHECK_PPP_VOID ();
1077 /*
1078  * Print the buffer if desired
1079  */
1080         if (ppp->flags & SC_LOG_RAWIN)
1081                 ppp_print_buffer ("receive buffer", data, count);
1082 /*
1083  * Collect the character and error condition for the character. Set the toss
1084  * flag for the first character error.
1085  */
1086         while (count-- > 0) {
1087                 ppp->bytes_rcvd++;
1088                 chr = *data++;
1089                 if (flags) {
1090                         if (*flags && ppp->toss == 0)
1091                                 ppp->toss = *flags;
1092                         ++flags;
1093                 }
1094 /*
1095  * Set the flags for 8 data bits and no parity.
1096  *
1097  * Actually, it sets the flags for d7 being 0/1 and parity being even/odd
1098  * so that the normal processing would have all flags set at the end of the
1099  * session. A missing flag bit would denote an error condition.
1100  */
1101 
1102 #ifdef CHECK_CHARACTERS
1103                 if (chr & 0x80)
1104                         ppp->flags |= SC_RCV_B7_1;
1105                 else
1106                         ppp->flags |= SC_RCV_B7_0;
1107 
1108                 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1109                         ppp->flags |= SC_RCV_ODDP;
1110                 else
1111                         ppp->flags |= SC_RCV_EVNP;
1112 #endif
1113 /*
1114  * Branch on the character. Process the escape character. The sequence ESC ESC
1115  * is defined to be ESC.
1116  */
1117                 switch (chr) {
1118                 case PPP_ESCAPE: /* PPP_ESCAPE: invert bit in next character */
1119                         ppp->escape = PPP_TRANS;
1120                         break;
1121 /*
1122  * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1123  * then the block is to be ignored. In addition, characters before the very
1124  * first FLAG are also tossed by this procedure.
1125  */
1126                 case PPP_FLAG:  /* PPP_FLAG: end of frame */
1127                         ppp->stats.ppp_ibytes += ppp->rbuf->count;
1128                         if (ppp->escape)
1129                                 ppp->toss |= 0x80;
1130 /*
1131  * Process frames which are not to be ignored. If the processing failed,
1132  * then clean up the VJ tables.
1133  */
1134                         if ((ppp->toss & 0x80) != 0 ||
1135                             ppp_doframe (ppp) == 0) {
1136                                 slhc_toss (ppp->slcomp);
1137                         }
1138 /*
1139  * Reset all indicators for the new frame to follow.
1140  */
1141                         buf->count  = 0;
1142                         buf->fcs    = PPP_INITFCS;
1143                         ppp->escape = 0;
1144                         ppp->toss   = 0;
1145                         break;
1146 /*
1147  * All other characters in the data come here. If the character is in the
1148  * receive mask then ignore the character.
1149  */
1150                 default:
1151                         if (in_rmap (ppp, chr))
1152                                 break;
1153 /*
1154  * Adjust the character and if the frame is to be discarded then simply
1155  * ignore the character until the ending FLAG is received.
1156  */
1157                         chr ^= ppp->escape;
1158                         ppp->escape = 0;
1159 
1160                         if (ppp->toss != 0)
1161                                 break;
1162 /*
1163  * If the count sent is within reason then store the character, bump the
1164  * count, and update the FCS for the character.
1165  */
1166                         if (buf->count < buf->size) {
1167                                 buf_base (buf)[buf->count++] = chr;
1168                                 buf->fcs = PPP_FCS (buf->fcs, chr);
1169                                 break;
1170                         }
1171 /*
1172  * The peer sent too much data. Set the flags to discard the current frame
1173  * and wait for the re-synchronization FLAG to be sent.
1174  */
1175                         ppp->stats.ppp_ierrors++;
1176                         ppp->toss |= 0xC0;
1177                         break;
1178                 }
1179         }
1180 }
1181 
1182 /*
1183  * Put the input frame into the networking system for the indicated protocol
1184  */
1185 
1186 static int
1187 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1188 {
1189         sk_buff *skb = dev_alloc_skb (count);
1190 /*
1191  * Generate a skb buffer for the new frame.
1192  */
1193         if (skb == NULL) {
1194                 if (ppp->flags & SC_DEBUG)
1195                         printk (KERN_ERR
1196                          "ppp_do_ip: packet dropped on %s (no memory)!\n",
1197                          ppp2dev (ppp)->name);
1198                 return 0;
1199         }
1200 /*
1201  * Move the received data from the input buffer to the skb buffer.
1202  */
1203         skb->dev      = ppp2dev (ppp);  /* We are the device */
1204         skb->protocol = proto;
1205         skb->mac.raw  = skb_data(skb);
1206         memcpy (skb_put(skb,count), data, count);       /* move data */
1207 /*
1208  * Tag the frame and kick it to the proper receive routine
1209  */
1210         skb->free = 1;
1211         ppp->ddinfo.recv_idle = jiffies;
1212         netif_rx (skb);
1213         return 1;
1214 }
1215 
1216 /*
1217  * Process the receipt of an IP frame
1218  */
1219 
1220 static int
1221 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1222 {
1223         if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1224                 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1225                         return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1226         return 0;
1227 }
1228 
1229 /*
1230  * Process the receipt of an IPX frame
1231  */
1232 
1233 static int
1234 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1235 {
1236         if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1237                 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1238         return 0;
1239 }
1240 
1241 /*
1242  * Process the receipt of an VJ Compressed frame
1243  */
1244 
1245 static int
1246 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
     /* [previous][next][first][last][top][bottom][index][help] */
1247                     __u8 *data, int count)
1248 {
1249         if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1250                 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1251                 if (new_count >= 0) {
1252                         return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1253                 }
1254                 if (ppp->flags & SC_DEBUG)
1255                         printk (KERN_NOTICE
1256                                 "ppp: error in VJ decompression\n");
1257         }
1258         return 0;
1259 }
1260 
1261 /*
1262  * Process the receipt of an VJ Un-compressed frame
1263  */
1264 
1265 static int
1266 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
     /* [previous][next][first][last][top][bottom][index][help] */
1267                       __u8 *data, int count)
1268 {
1269         if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1270                 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1271                         return rcv_proto_ip (ppp, PPP_IP, data, count);
1272                 }
1273                 if (ppp->flags & SC_DEBUG)
1274                         printk (KERN_NOTICE
1275                                 "ppp: error in VJ memorizing\n");
1276         }
1277         return 0;
1278 }
1279 
1280 /*
1281  * Receive all unclassified protocols.
1282  */
1283 
1284 static int
1285 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
     /* [previous][next][first][last][top][bottom][index][help] */
1286                    __u8 *data, int len)
1287 {
1288         int totlen;
1289         register int current_idx;
1290 
1291 #define PUTC(c)                                          \
1292 {                                                        \
1293     buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1294     current_idx &= ppp->ubuf->size;                      \
1295     if (current_idx == ppp->ubuf->tail)                  \
1296             goto failure;                                \
1297 }
1298 
1299 /*
1300  * The total length includes the protocol data.
1301  * Lock the user information buffer.
1302  */
1303         if (set_bit (0, &ppp->ubuf->locked)) {
1304                 if (ppp->flags & SC_DEBUG)
1305                         printk (KERN_DEBUG
1306                                 "ppp_us_queue: can't get lock\n");
1307         } else {
1308                 current_idx = ppp->ubuf->head;
1309 /*
1310  * Insert the buffer length (not counted), the protocol, and the data
1311  */
1312                 totlen = len + 2;
1313                 PUTC (totlen >> 8);
1314                 PUTC (totlen);
1315 
1316                 PUTC (proto >> 8);
1317                 PUTC (proto);
1318 
1319                 totlen -= 2;
1320                 while (totlen-- > 0) {
1321                         PUTC (*data++);
1322                 }
1323 #undef PUTC
1324 /*
1325  * The frame is complete. Update the head pointer and wakeup the pppd
1326  * process.
1327  */
1328                 ppp->ubuf->head = current_idx;
1329 
1330                 clear_bit (0, &ppp->ubuf->locked);
1331                 wake_up_interruptible (&ppp->read_wait);
1332                 if (ppp->tty->fasync != NULL)
1333                         kill_fasync (ppp->tty->fasync, SIGIO);
1334 
1335                 if (ppp->flags & SC_DEBUG)
1336                         printk (KERN_INFO
1337                                 "ppp: successfully queued %d bytes, flags = %x\n",
1338                                 len + 2, ppp->flags);
1339 
1340                 return 1;
1341 /*
1342  * The buffer is full. Unlock the header
1343  */
1344 failure:
1345                 clear_bit (0, &ppp->ubuf->locked);
1346                 if (ppp->flags & SC_DEBUG)
1347                         printk (KERN_INFO
1348                                 "ppp_us_queue: ran out of buffer space.\n");
1349         }
1350 /*
1351  * Discard the frame. There are no takers for this protocol.
1352  */
1353         if (ppp->flags & SC_DEBUG)
1354                 printk (KERN_WARNING
1355                         "ppp: dropping packet on the floor.\n");
1356         slhc_toss (ppp->slcomp);
1357         return 0;
1358 }
1359 
1360 /*
1361  * Handle a CCP packet.
1362  *
1363  * The CCP packet is passed along to the pppd process just like any
1364  * other PPP frame. The difference is that some processing needs to be
1365  * immediate or the compressors will become confused on the peer.
1366  */
1367 
1368 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
     /* [previous][next][first][last][top][bottom][index][help] */
1369 {
1370         int slen    = CCP_LENGTH(dp);
1371         __u8 *opt = dp   + CCP_HDRLEN;
1372         int opt_len = slen - CCP_HDRLEN;
1373 
1374         if (slen > len)
1375                 return;
1376 
1377         switch (CCP_CODE(dp)) {
1378         case CCP_CONFREQ:
1379         case CCP_TERMREQ:
1380         case CCP_TERMACK:
1381 /*
1382  * CCP must be going down - disable compression
1383  */
1384                 if (ppp->flags & SC_CCP_UP) {
1385                         ppp->flags &= ~(SC_CCP_UP   |
1386                                         SC_COMP_RUN |
1387                                         SC_DECOMP_RUN);
1388                 }
1389                 break;
1390 
1391         case CCP_CONFACK:
1392                 if ((ppp->flags & SC_CCP_OPEN) == 0)
1393                         break;
1394                 if (ppp->flags & SC_CCP_UP)
1395                         break;
1396                 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1397                         break;
1398                 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1399                         break;
1400 /*
1401  * we're agreeing to send compressed packets.
1402  */
1403                 if (!rcvd) {
1404                         if (ppp->sc_xc_state == NULL)
1405                                 break;
1406 
1407                         if ((*ppp->sc_xcomp->comp_init)
1408                             (ppp->sc_xc_state,
1409                              opt,
1410                              opt_len,
1411                              ppp2dev (ppp)->base_addr,
1412                              0,
1413                              ppp->flags))
1414                                 ppp->flags |= SC_COMP_RUN;
1415                         break;
1416                 }
1417 /*
1418  * peer is agreeing to send compressed packets.
1419  */
1420                 if (ppp->sc_rc_state == NULL)
1421                         break;
1422 
1423                 if ((*ppp->sc_rcomp->decomp_init)
1424                     (ppp->sc_rc_state,
1425                      opt,
1426                      opt_len,
1427                      ppp2dev (ppp)->base_addr,
1428                      0,
1429                      ppp->mru,
1430                      ppp->flags)) {
1431                         ppp->flags |= SC_DECOMP_RUN;
1432                         ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1433                 }
1434                 break;
1435 /*
1436  * The protocol sequence is complete at this end
1437  */
1438         case CCP_RESETACK:
1439                 if ((ppp->flags & SC_CCP_UP) == 0)
1440                         break;
1441 
1442                 if (!rcvd) {
1443                         if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN))
1444                                 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1445                 } else {
1446                         if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1447                               (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1448                                 ppp->flags &= ~SC_DC_ERROR;
1449                         }
1450                 }
1451                 break;
1452         }
1453 }
1454 
1455 static int
1456 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1457 {
1458         ppp_proto_ccp (ppp, dp, len, 1);
1459         return rcv_proto_unknown (ppp, proto, dp, len);
1460 }
1461 
1462 /*
1463  * Handle a LQR packet.
1464  */
1465 
1466 static int
1467 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1468 {
1469         return rcv_proto_unknown (ppp, proto, data, len);
1470 }
1471 
1472 /* on entry, a received frame is in ppp->rbuf.bufr
1473    check it and dispose as appropriate */
1474 
1475 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1476 {
1477         __u16           proto = PPP_PROTOCOL (data);
1478         ppp_proto_type  *proto_ptr;
1479 /*
1480  * Ignore empty frames
1481  */
1482         if (count <= 4)
1483                 return;
1484 /*
1485  * Count the frame and print it
1486  */
1487         ++ppp->stats.ppp_ipackets;
1488         if (ppp->flags & SC_LOG_INPKT)
1489                 ppp_print_buffer ("receive frame", data, count);
1490 /*
1491  * Find the procedure to handle this protocol. The last one is marked
1492  * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1493  */
1494         proto_ptr = proto_list;
1495         while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1496                 ++proto_ptr;
1497 /*
1498  * Update the appropriate statistic counter.
1499  */
1500         if ((*proto_ptr->func) (ppp, proto,
1501                                 &data[PPP_HARD_HDR_LEN],
1502                                 count - PPP_HARD_HDR_LEN))
1503                 ppp->stats.ppp_ioctects += count;
1504         else
1505                 ++ppp->stats.ppp_discards;
1506 }
1507 
1508 /* on entry, a received frame is in ppp->rbuf.bufr
1509    check it and dispose as appropriate */
1510 
1511 static int
1512 ppp_doframe (struct ppp *ppp)
     /* [previous][next][first][last][top][bottom][index][help] */
1513 {
1514         __u8    *data = buf_base (ppp->rbuf);
1515         int     count = ppp->rbuf->count;
1516         int     addr, ctrl, proto;
1517         int     new_count;
1518         __u8 *new_data;
1519 /*
1520  * If there is a pending error from the receiver then log it and discard
1521  * the damaged frame.
1522  */
1523         if (ppp->toss) {
1524                 if (ppp->flags & SC_DEBUG)
1525                         printk (KERN_WARNING
1526                                 "ppp_toss: tossing frame, reason = %d\n",
1527                                 ppp->toss);
1528                 ppp->stats.ppp_ierrors++;
1529                 return 0;
1530         }
1531 /*
1532  * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1533  * follows each frame.
1534  */
1535         if (count == 0)
1536                 return 1;
1537 /*
1538  * Generate an error if the frame is too small.
1539  */
1540         if (count < PPP_HARD_HDR_LEN) {
1541                 if (ppp->flags & SC_DEBUG)
1542                         printk (KERN_WARNING
1543                                 "ppp: got runt ppp frame, %d chars\n", count);
1544                 slhc_toss (ppp->slcomp);
1545                 ppp->stats.ppp_ierrors++;
1546                 return 1;
1547         }
1548 /*
1549  * Verify the CRC of the frame and discard the CRC characters from the
1550  * end of the buffer.
1551  */
1552         if (ppp->rbuf->fcs != PPP_GOODFCS) {
1553                 if (ppp->flags & SC_DEBUG)
1554                         printk (KERN_WARNING
1555                                 "ppp: frame with bad fcs, excess = %x\n",
1556                                 ppp->rbuf->fcs ^ PPP_GOODFCS);
1557                 ppp->stats.ppp_ierrors++;
1558                 return 0;
1559         }
1560         count -= 2;             /* ignore the fcs characters */
1561 /*
1562  * Ignore the leading ADDRESS and CONTROL fields in the frame.
1563  */
1564         addr   = PPP_ALLSTATIONS;
1565         ctrl   = PPP_UI;
1566 
1567         if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1568                 data  += 2;
1569                 count -= 2;
1570         }
1571 /*
1572  * Obtain the protocol from the frame
1573  */
1574         proto = (__u16) *data++;
1575         if ((proto & 1) == 0) {
1576                 proto = (proto << 8) | (__u16) *data++;
1577                 --count;
1578         }
1579 /*
1580  * Rewrite the header with the full information. This may encroach upon
1581  * the 'filler' area in the buffer header. This is the purpose for the
1582  * filler.
1583  */
1584         *(--data) = proto;
1585         *(--data) = proto >> 8;
1586         *(--data) = ctrl;
1587         *(--data) = addr;
1588         count    += 3;
1589 /*
1590  * Process the active decompressor.
1591  */
1592         if ((ppp->sc_rc_state != (void *) 0) &&
1593             (ppp->flags & SC_DECOMP_RUN)     &&
1594             ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1595                 if (proto == PPP_COMP) {
1596 /*
1597  * If the frame is compressed then decompress it.
1598  */
1599                         new_data = kmalloc (ppp->mru + 4, GFP_ATOMIC);
1600                         if (new_data == NULL) {
1601                                 if (ppp->flags & SC_DEBUG)
1602                                         printk (KERN_ERR
1603                                                 "ppp_doframe: no memory\n");
1604                                 slhc_toss (ppp->slcomp);
1605                                 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1606                                                           data,
1607                                                           count);
1608                                 return 1;
1609                         }
1610 /*
1611  * Decompress the frame
1612  */
1613                         new_count = bsd_decompress (ppp->sc_rc_state,
1614                                                     data,
1615                                                     count,
1616                                                     new_data,
1617                                                     ppp->mru + 4);
1618                         switch (new_count) {
1619                         default:
1620                                 ppp_doframe_lower (ppp, new_data, new_count);
1621                                 kfree (new_data);
1622                                 return 1;
1623 
1624                         case DECOMP_OK:
1625                                 break;
1626 
1627                         case DECOMP_ERROR:
1628                                 ppp->flags |= SC_DC_ERROR;
1629                                 break;
1630 
1631                         case DECOMP_FATALERROR:
1632                                 ppp->flags |= SC_DC_FERROR;
1633                                 break;
1634                         }
1635 /*
1636  * Log the error condition and discard the frame.
1637  */
1638                         if (ppp->flags & SC_DEBUG)
1639                                 printk (KERN_ERR
1640                                         "ppp_proto_comp: "
1641                                         "decompress err %d\n", new_count);
1642                         kfree (new_data);
1643                         slhc_toss (ppp->slcomp);
1644                         return 1;
1645                 }
1646 /*
1647  * The frame is not special. Pass it through the compressor without
1648  * actually compressing the data
1649  */
1650                 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1651                                           data,
1652                                           count);
1653         }
1654 /*
1655  * Process the uncompressed frame.
1656  */
1657         ppp_doframe_lower (ppp, data, count);
1658         return 1;
1659 }
1660 
1661 /*************************************************************
1662  * LINE DISCIPLINE SUPPORT
1663  *    The following functions form support user programs
1664  *    which read and write data on a TTY with the PPP line
1665  *    discipline.  Reading is done from a circular queue,
1666  *    filled by the lower TTY levels.
1667  *************************************************************/
1668 
1669 /* read a PPP frame from the us_rbuff circular buffer,
1670    waiting if necessary
1671 */
1672 
1673 static int
1674 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
     /* [previous][next][first][last][top][bottom][index][help] */
1675               unsigned int nr)
1676 {
1677         struct ppp *ppp = tty2ppp (tty);
1678         __u8 c;
1679         int len, indx;
1680 
1681 #define GETC(c)                                         \
1682 {                                                       \
1683         c = buf_base (ppp->ubuf) [ppp->ubuf->tail++];   \
1684         ppp->ubuf->tail &= ppp->ubuf->size;             \
1685 }
1686 
1687 /*
1688  * Validate the pointers
1689  */
1690         if (!ppp)
1691                 return -EIO;
1692 
1693         if (ppp->magic != PPP_MAGIC)
1694                 return -EIO;
1695 
1696         CHECK_PPP (-ENXIO);
1697 
1698         if (ppp->flags & SC_DEBUG)
1699                 printk (KERN_DEBUG
1700                         "ppp_tty_read: called buf=%p nr=%u\n",
1701                         buf, nr);
1702 /*
1703  * Acquire the read lock.
1704  */
1705         for (;;) {
1706                 ppp = tty2ppp (tty);
1707                 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse)
1708                         return 0;
1709 
1710                 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1711                         if (ppp->flags & SC_DEBUG)
1712                                 printk (KERN_DEBUG
1713                                      "ppp_tty_read: sleeping(ubuf)\n");
1714 
1715                         current->timeout = 0;
1716                         current->state   = TASK_INTERRUPTIBLE;
1717                         schedule ();
1718 
1719                         if (current->signal & ~current->blocked)
1720                                 return -EINTR;
1721                         continue;
1722                 }
1723 /*
1724  * Before we attempt to write the frame to the user, ensure that the
1725  * user has access to the pages for the total buffer length.
1726  */
1727                 indx = verify_area (VERIFY_WRITE, buf, nr);
1728                 if (indx != 0)
1729                         return (indx);
1730 /*
1731  * Fetch the length of the buffer from the first two bytes.
1732  */
1733                 if (ppp->ubuf->head == ppp->ubuf->tail)
1734                         len = 0;
1735                 else {
1736                         GETC (c);
1737                         len = c << 8;
1738                         GETC (c);
1739                         len += c;
1740                 }
1741 /*
1742  * If there is no length then wait for the data to arrive.
1743  */
1744                 if (len == 0) {
1745                         /* no data */
1746                         clear_bit (0, &ppp->ubuf->locked);
1747                         if (file->f_flags & O_NONBLOCK) {
1748                                 if (ppp->flags & SC_DEBUG)
1749                                         printk (KERN_DEBUG
1750                                                 "ppp_tty_read: no data "
1751                                                 "(EAGAIN)\n");
1752                                 return -EAGAIN;
1753                         }
1754                         current->timeout = 0;
1755 
1756                         if (ppp->flags & SC_DEBUG)
1757                                 printk (KERN_DEBUG
1758                                         "ppp_tty_read: sleeping(read_wait)\n");
1759 
1760                         interruptible_sleep_on (&ppp->read_wait);
1761                         if (current->signal & ~current->blocked)
1762                                 return -EINTR;
1763                         continue;
1764                 }
1765 /*
1766  * Reset the time of the last read operation.
1767  */
1768                 if (ppp->flags & SC_DEBUG)
1769                         printk (KERN_DEBUG "ppp_tty_read: len = %d\n", len);
1770 /*
1771  * Ensure that the frame will fit within the caller's buffer. If not, then
1772  * discard the frame from the input buffer.
1773  */
1774                 if (len + 2 > nr) {
1775                         /* Can't copy it, update us_rbuff_head */
1776 
1777                         if (ppp->flags & SC_DEBUG)
1778                                 printk (KERN_DEBUG
1779                                 "ppp: read of %u bytes too small for %d "
1780                                 "frame\n", nr, len + 2);
1781                         ppp->ubuf->tail += len;
1782                         ppp->ubuf->tail &= ppp->ubuf->size;
1783                         clear_bit (0, &ppp->ubuf->locked);
1784                         ppp->stats.ppp_ierrors++;
1785                         return -EOVERFLOW;
1786                 }
1787 /*
1788  * Before we attempt to write the frame to the user, ensure that the
1789  * page tables are proper.
1790  */
1791                 indx = verify_area (VERIFY_WRITE, buf, len + 2);
1792                 if (indx != 0) {
1793                         ppp->ubuf->tail += len;
1794                         ppp->ubuf->tail &= ppp->ubuf->size;
1795                         clear_bit (0, &ppp->ubuf->locked);
1796                         return (indx);
1797                 }
1798 /*
1799  * Fake the insertion of the ADDRESS and CONTROL information because these
1800  * were not saved in the buffer.
1801  */
1802                 put_user (PPP_ALLSTATIONS, buf++);
1803                 put_user (PPP_UI,          buf++);
1804 
1805                 indx = len;
1806 /*
1807  * Copy the received data from the buffer to the caller's area.
1808  */
1809                 while (indx-- > 0) {
1810                         GETC (c);
1811                         put_user (c, buf);
1812                         ++buf;
1813                 }
1814 
1815                 clear_bit (0, &ppp->ubuf->locked);
1816                 len += 2; /* Account for ADDRESS and CONTROL bytes */
1817                 if (ppp->flags & SC_DEBUG)
1818                         printk (KERN_DEBUG
1819                                 "ppp_tty_read: passing %d bytes up\n", len);
1820                 return len;
1821         }
1822 #undef GETC
1823 }
1824 
1825 /* stuff a character into the transmit buffer, using PPP's way of escaping
1826    special characters.
1827    also, update fcs to take account of new character */
1828 
1829 extern inline void
1830 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
1831                 register __u8 chr)
1832 {
1833 /*
1834  * The buffer should not be full.
1835  */
1836         if (ppp->flags & SC_DEBUG) {
1837                 if ((buf->count < 0) || (buf->count > 3000))
1838                         printk (KERN_DEBUG "ppp_stuff_char: %x %d\n",
1839                                 (unsigned int) buf->count,
1840                                 (unsigned int) chr);
1841         }
1842 /*
1843  * Update the FCS and if the character needs to be escaped, do it.
1844  */
1845         buf->fcs = PPP_FCS (buf->fcs, chr);
1846         if (in_xmap (ppp, chr)) {
1847                 chr ^= PPP_TRANS;
1848                 ins_char (buf, PPP_ESCAPE);
1849         }
1850 /*
1851  * Add the character to the buffer.
1852  */
1853         ins_char (buf, chr);
1854 }
1855 
1856 /*
1857  * Procedure to encode the data with the proper escapement and send the
1858  * data to the remote system.
1859  */
1860 
1861 static void
1862 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
1863                     __u8 *data, int count, int non_ip)
1864 {
1865         __u16   write_fcs;
1866         int     address, control;
1867         int     proto;
1868 /*
1869  * Insert the leading FLAG character
1870  */
1871         buf->count = 0;
1872 
1873         if (non_ip || flag_time == 0)
1874                 ins_char (buf, PPP_FLAG);
1875         else {
1876                 if (jiffies - ppp->last_xmit > flag_time)
1877                         ins_char (buf, PPP_FLAG);
1878         }
1879         ppp->last_xmit = jiffies;
1880         buf->fcs       = PPP_INITFCS;
1881 /*
1882  * Emit the address/control information if needed
1883  */
1884         address = PPP_ADDRESS  (data);
1885         control = PPP_CONTROL  (data);
1886         proto   = PPP_PROTOCOL (data);
1887 
1888         if (address != PPP_ALLSTATIONS ||
1889             control != PPP_UI ||
1890             (ppp->flags & SC_COMP_AC) == 0) {
1891                 ppp_stuff_char (ppp, buf, address);
1892                 ppp_stuff_char (ppp, buf, control);
1893         }
1894 /*
1895  * Emit the protocol (compressed if possible)
1896  */
1897         if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
1898                 ppp_stuff_char (ppp, buf, proto >> 8);
1899 
1900         ppp_stuff_char (ppp, buf, proto);
1901 /*
1902  * Insert the data
1903  */
1904         data  += 4;
1905         count -= 4;
1906 
1907         while (count-- > 0)
1908                 ppp_stuff_char (ppp, buf, *data++);
1909 /*
1910  * Add the trailing CRC and the final flag character
1911  */
1912         write_fcs = buf->fcs ^ 0xFFFF;
1913         ppp_stuff_char (ppp, buf, write_fcs);
1914         ppp_stuff_char (ppp, buf, write_fcs >> 8);
1915 
1916         if (ppp->flags & SC_DEBUG)
1917                 printk (KERN_DEBUG "ppp_dev_xmit_lower: fcs is %hx\n",
1918                         write_fcs);
1919 /*
1920  * Add the trailing flag character
1921  */
1922         ins_char (buf, PPP_FLAG);
1923 /*
1924  * Print the buffer
1925  */
1926         if (ppp->flags & SC_LOG_FLUSH)
1927                 ppp_print_buffer ("ppp flush", buf_base (buf),
1928                                   buf->count);
1929         else {
1930                 if (ppp->flags & SC_DEBUG)
1931                         printk (KERN_DEBUG
1932                                 "ppp_dev_xmit: writing %d chars\n",
1933                                 buf->count);
1934         }
1935 /*
1936  * Send the block to the tty driver.
1937  */
1938         ppp->stats.ppp_obytes += buf->count;
1939         ppp_kick_tty (ppp, buf);
1940 }
1941 
1942 /*
1943  * Send an frame to the remote with the proper bsd compression.
1944  *
1945  * Return 0 if frame was queued for transmission.
1946  *        1 if frame must be re-queued for later driver support.
1947  */
1948 
1949 static int
1950 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
     /* [previous][next][first][last][top][bottom][index][help] */
1951                     __u8 *data, int count)
1952 {
1953         int     proto;
1954         int     address, control;
1955         __u8 *new_data;
1956         int     new_count;
1957 /*
1958  * Print the buffer
1959  */
1960         if (ppp->flags & SC_LOG_OUTPKT)
1961                 ppp_print_buffer ("write frame", data, count);
1962 /*
1963  * Determine if the frame may be compressed. Attempt to compress the
1964  * frame if possible.
1965  */
1966         proto   = PPP_PROTOCOL (data);
1967         address = PPP_ADDRESS  (data);
1968         control = PPP_CONTROL  (data);
1969 
1970         if (((ppp->flags & SC_COMP_RUN) != 0)   &&
1971             (ppp->sc_xc_state != (void *) 0)    &&
1972             (address == PPP_ALLSTATIONS)        &&
1973             (control == PPP_UI)                 &&
1974             (proto != PPP_LCP)                  &&
1975             (proto != PPP_CCP)) {
1976                 new_data = kmalloc (count, GFP_ATOMIC);
1977                 if (new_data == NULL) {
1978                         if (ppp->flags & SC_DEBUG)
1979                                 printk (KERN_ERR
1980                                         "ppp_dev_xmit_frame: no memory\n");
1981                         return 1;
1982                 }
1983 
1984                 new_count = bsd_compress (ppp->sc_xc_state,
1985                                           data,
1986                                           new_data,
1987                                           count,
1988                                           count);
1989 
1990                 if (new_count > 0) {
1991                         ++ppp->stats.ppp_opackets;
1992                         ppp->stats.ppp_ooctects += new_count;
1993 
1994                         ppp_dev_xmit_lower (ppp, buf, new_data,
1995                                             new_count, 0);
1996                         kfree (new_data);
1997                         return 0;
1998                 }
1999 /*
2000  * The frame could not be compressed.
2001  */
2002                 kfree (new_data);
2003         }
2004 /*
2005  * The frame may not be compressed. Update the statistics before the
2006  * count field is destroyed. The frame will be transmitted.
2007  */
2008         ++ppp->stats.ppp_opackets;
2009         ppp->stats.ppp_ooctects += count;
2010 /*
2011  * Go to the escape encoding
2012  */
2013         ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2014         return 0;
2015 }
2016 
2017 /*
2018  * Revise the tty frame for specific protocols.
2019  */
2020 
2021 static int
2022 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2023 {
2024         __u8 *p;
2025 
2026         switch (PPP_PROTOCOL (data)) {
2027 /*
2028  * Update the LQR frame with the current MIB information. This saves having
2029  * the daemon read old MIB data from the driver.
2030  */
2031         case PPP_LQR:
2032                 len = 48;                       /* total size of this frame */
2033                 p   = (__u8 *) &data [40];      /* Point to last two items. */
2034                 p   = store_long (p, ppp->stats.ppp_opackets + 1);
2035                 p   = store_long (p, ppp->stats.ppp_ooctects + len);
2036                 break;
2037 /*
2038  * Outbound compression frames
2039  */
2040         case PPP_CCP:
2041                 ppp_proto_ccp (ppp,
2042                                data + PPP_HARD_HDR_LEN,
2043                                len  - PPP_HARD_HDR_LEN,
2044                                0);
2045                 break;
2046 
2047         default:
2048                 break;
2049         }
2050 
2051         return len;
2052 }
2053 
2054 /*
2055  * write a frame with NR chars from BUF to TTY
2056  * we have to put the FCS field on ourselves
2057  */
2058 
2059 static int
2060 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
     /* [previous][next][first][last][top][bottom][index][help] */
2061                unsigned int count)
2062 {
2063         struct ppp *ppp = tty2ppp (tty);
2064         __u8 *new_data;
2065         int status;
2066 /*
2067  * Verify the pointers.
2068  */
2069         if (!ppp)
2070                 return -EIO;
2071 
2072         if (ppp->magic != PPP_MAGIC)
2073                 return -EIO;
2074 
2075         CHECK_PPP (-ENXIO);
2076 /*
2077  * Ensure that the caller does not wish to send too much.
2078  */
2079         if (count > PPP_MTU) {
2080                 if (ppp->flags & SC_DEBUG)
2081                         printk (KERN_WARNING
2082                                 "ppp_tty_write: truncating user packet "
2083                                 "from %u to mtu %d\n", count, PPP_MTU);
2084                 count = PPP_MTU;
2085         }
2086 /*
2087  * Allocate a buffer for the data and fetch it from the user space.
2088  */
2089         new_data = kmalloc (count, GFP_KERNEL);
2090         if (new_data == NULL) {
2091                 if (ppp->flags & SC_DEBUG)
2092                         printk (KERN_ERR
2093                                 "ppp_tty_write: no memory\n");
2094                 return 0;
2095         }
2096 /*
2097  * lock this PPP unit so we will be the only writer;
2098  * sleep if necessary
2099  */
2100         while (lock_buffer (ppp->tbuf) != 0) {
2101                 current->timeout = 0;
2102                 if (ppp->flags & SC_DEBUG)
2103                         printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2104                 interruptible_sleep_on (&ppp->write_wait);
2105 
2106                 ppp = tty2ppp (tty);
2107                 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse) {
2108                         kfree (new_data);
2109                         return 0;
2110                 }
2111 
2112                 if (current->signal & ~current->blocked) {
2113                         kfree (new_data);
2114                         return -EINTR;
2115                 }
2116         }
2117 /*
2118  * Ensure that the caller's buffer is valid.
2119  */
2120         status = verify_area (VERIFY_READ, data, count);
2121         if (status != 0) {
2122                 kfree (new_data);
2123                 ppp->tbuf->locked = 0;
2124                 return status;
2125         }
2126 
2127         memcpy_fromfs (new_data, data, count);
2128 /*
2129  * Change the LQR frame
2130  */
2131         count = send_revise_frame (ppp, new_data, count);
2132 /*
2133  * Send the data
2134  */
2135         ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2136         kfree (new_data);
2137         return (int) count;
2138 }
2139 
2140 /*
2141  * Process the BSD compression IOCTL event for the tty device.
2142  */
2143 
2144 static int
2145 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
     /* [previous][next][first][last][top][bottom][index][help] */
2146 {
2147         struct compressor *cp;
2148         struct ppp_option_data data;
2149         int error;
2150         int nb;
2151         __u8 *ptr;
2152         __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2153 /*
2154  * Fetch the compression parameters
2155  */
2156         error = verify_area (VERIFY_READ, odp, sizeof (data));
2157         if (error == 0) {
2158                 memcpy_fromfs (&data, odp, sizeof (data));
2159                 nb  = data.length;
2160                 ptr = data.ptr;
2161                 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2162                         nb = CCP_MAX_OPTION_LENGTH;
2163         
2164                 error = verify_area (VERIFY_READ, ptr, nb);
2165         }
2166 
2167         if (error != 0)
2168                 return error;
2169 
2170         memcpy_fromfs (ccp_option, ptr, nb);
2171 
2172         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
2173                 return (-EINVAL);
2174 
2175         cp = find_compressor ((int) (unsigned int) (__u8) ccp_option[0]);
2176         if (cp != (struct compressor *) 0) {
2177                 /*
2178                  * Found a handler for the protocol - try to allocate
2179                  * a compressor or decompressor.
2180                  */
2181                 error = 0;
2182                 if (data.transmit) {
2183                         if (ppp->sc_xc_state != NULL)
2184                                 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2185 
2186                         ppp->sc_xcomp    = cp;
2187                         ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2188 
2189                         if (ppp->sc_xc_state == NULL) {
2190                                 if (ppp->flags & SC_DEBUG)
2191                                         printk("ppp%ld: comp_alloc failed\n",
2192                                                ppp2dev (ppp)->base_addr);
2193                                 error = -ENOBUFS;
2194                         }
2195                         ppp->flags &= ~SC_COMP_RUN;
2196                 } else {
2197                         if (ppp->sc_rc_state != NULL)
2198                                 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2199                         ppp->sc_rcomp    = cp;
2200                         ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2201                         if (ppp->sc_rc_state == NULL) {
2202                                 if (ppp->flags & SC_DEBUG)
2203                                         printk("ppp%ld: decomp_alloc failed\n",
2204                                                ppp2dev (ppp)->base_addr);
2205                                 error = ENOBUFS;
2206                         }
2207                         ppp->flags &= ~SC_DECOMP_RUN;
2208                 }
2209                 return (error);
2210         }
2211 
2212         if (ppp->flags & SC_DEBUG)
2213                 printk(KERN_DEBUG "ppp%ld: no compressor for [%x %x %x], %x\n",
2214                        ppp2dev (ppp)->base_addr, ccp_option[0], ccp_option[1],
2215                        ccp_option[2], nb);
2216         return (-EINVAL);       /* no handler found */
2217 }
2218 
2219 /*
2220  * Process the IOCTL event for the tty device.
2221  */
2222 
2223 static int
2224 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
2225                unsigned int param2, unsigned long param3)
2226 {
2227         struct ppp *ppp = tty2ppp (tty);
2228         register int temp_i = 0;
2229         int error = 0;
2230 /*
2231  * Verify the status of the PPP device.
2232  */
2233         if (!ppp)
2234                 return -EBADF;
2235 
2236         if (ppp->magic != PPP_MAGIC)
2237                 return -EBADF;
2238 
2239         CHECK_PPP (-ENXIO);
2240 /*
2241  * The user must have an euid of root to do these requests.
2242  */
2243         if (!suser ())
2244                 return -EPERM;
2245 /*
2246  * Set the MRU value
2247  */
2248         switch (param2) {
2249         case PPPIOCSMRU:
2250                 error = verify_area (VERIFY_READ, (void *) param3,
2251                                      sizeof (temp_i));
2252                 if (error == 0) {
2253                         temp_i = get_user ((int *) param3);
2254                         if (ppp->flags & SC_DEBUG)
2255                                 printk (KERN_INFO
2256                                  "ppp_tty_ioctl: set mru to %x\n", temp_i);
2257 
2258                         if (ppp->mru != temp_i)
2259                                 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2260                 }
2261                 break;
2262 /*
2263  * Fetch the flags
2264  */
2265         case PPPIOCGFLAGS:
2266                 error = verify_area (VERIFY_WRITE, (void *) param3,
2267                                      sizeof (temp_i));
2268                 if (error == 0) {
2269                         temp_i = (ppp->flags & SC_MASK);
2270 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2271                         temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2272                                   SC_RCV_ODDP | SC_RCV_EVNP;
2273 #endif
2274                         put_user (temp_i, (int *) param3);
2275                         if (ppp->flags & SC_DEBUG)
2276                                 printk (KERN_DEBUG
2277                                 "ppp_tty_ioctl: get flags: addr %lx flags "
2278                                 "%x\n", param3, temp_i);
2279                 }
2280                 break;
2281 /*
2282  * Set the flags for the various options
2283  */
2284         case PPPIOCSFLAGS:
2285                 error = verify_area (VERIFY_READ, (void *) param3,
2286                                      sizeof (temp_i));
2287                 if (error == 0) {
2288                         temp_i  = get_user ((int *) param3) & SC_MASK;
2289                         temp_i |= (ppp->flags & ~SC_MASK);
2290 
2291                         if ((ppp->flags & SC_CCP_OPEN) &&
2292                             (temp_i & SC_CCP_OPEN) == 0)
2293                                 ppp_ccp_closed (ppp);
2294 
2295                         if ((ppp->flags | temp_i) & SC_DEBUG)
2296                                 printk (KERN_INFO
2297                                "ppp_tty_ioctl: set flags to %x\n", temp_i);
2298                         ppp->flags = temp_i;
2299                 }
2300                 break;
2301 /*
2302  * Set the compression mode
2303  */
2304         case PPPIOCSCOMPRESS:
2305                 error = ppp_set_compression (ppp,
2306                                             (struct ppp_option_data *) param3);
2307                 break;
2308 /*
2309  * Retrieve the transmit async map
2310  */
2311         case PPPIOCGASYNCMAP:
2312                 error = verify_area (VERIFY_WRITE, (void *) param3,
2313                                      sizeof (temp_i));
2314                 if (error == 0) {
2315                         put_user (ppp->xmit_async_map[0], (int *) param3);
2316                         if (ppp->flags & SC_DEBUG)
2317                                 printk (KERN_INFO
2318                                      "ppp_tty_ioctl: get asyncmap: addr "
2319                                      "%lx asyncmap %x\n",
2320                                      param3,
2321                                      ppp->xmit_async_map[0]);
2322                 }
2323                 break;
2324 /*
2325  * Set the transmit async map
2326  */
2327         case PPPIOCSASYNCMAP:
2328                 error = verify_area (VERIFY_READ, (void *) param3,
2329                                      sizeof (temp_i));
2330                 if (error == 0) {
2331                         ppp->xmit_async_map[0] = get_user ((int *) param3);
2332                         if (ppp->flags & SC_DEBUG)
2333                                 printk (KERN_INFO
2334                                      "ppp_tty_ioctl: set xmit asyncmap %x\n",
2335                                      ppp->xmit_async_map[0]);
2336                 }
2337                 break;
2338 /*
2339  * Set the receive async map
2340  */
2341         case PPPIOCSRASYNCMAP:
2342                 error = verify_area (VERIFY_READ, (void *) param3,
2343                                      sizeof (temp_i));
2344                 if (error == 0) {
2345                         ppp->recv_async_map = get_user ((int *) param3);
2346                         if (ppp->flags & SC_DEBUG)
2347                                 printk (KERN_INFO
2348                                      "ppp_tty_ioctl: set rcv asyncmap %x\n",
2349                                      ppp->recv_async_map);
2350                 }
2351                 break;
2352 /*
2353  * Obtain the unit number for this device.
2354  */
2355         case PPPIOCGUNIT:
2356                 error = verify_area (VERIFY_WRITE, (void *) param3,
2357                                      sizeof (temp_i));
2358                 if (error == 0) {
2359                         put_user (ppp2dev (ppp)->base_addr, (int *) param3);
2360                         if (ppp->flags & SC_DEBUG)
2361                                 printk (KERN_INFO
2362                                         "ppp_tty_ioctl: get unit: %ld",
2363                                         ppp2dev (ppp)->base_addr);
2364                 }
2365                 break;
2366 /*
2367  * Set the debug level
2368  */
2369         case PPPIOCSDEBUG:
2370                 error = verify_area (VERIFY_READ, (void *) param3,
2371                                      sizeof (temp_i));
2372                 if (error == 0) {
2373                         temp_i  = (get_user ((int *) param3) & 0x1F) << 16;
2374                         temp_i |= (ppp->flags & ~0x1F0000);
2375 
2376                         if ((ppp->flags | temp_i) & SC_DEBUG)
2377                                 printk (KERN_INFO
2378                                "ppp_tty_ioctl: set flags to %x\n", temp_i);
2379                         ppp->flags = temp_i;
2380                 }
2381                 break;
2382 /*
2383  * Get the debug level
2384  */
2385         case PPPIOCGDEBUG:
2386                 error = verify_area (VERIFY_WRITE, (void *) param3,
2387                                      sizeof (temp_i));
2388                 if (error == 0) {
2389                         temp_i = (ppp->flags >> 16) & 0x1F;
2390                         put_user (temp_i, (int *) param3);
2391 
2392                         if (ppp->flags & SC_DEBUG)
2393                                 printk (KERN_INFO
2394                                         "ppp_tty_ioctl: get debug level %d\n",
2395                                         temp_i);
2396                 }
2397                 break;
2398 /*
2399  * Get the times since the last send/receive frame operation
2400  */
2401         case PPPIOCGIDLE:
2402                 error = verify_area (VERIFY_WRITE, (void *) param3,
2403                                      sizeof (struct ppp_idle));
2404                 if (error == 0) {
2405                         struct ppp_idle cur_ddinfo;
2406                         __u32 cur_jiffies = jiffies;
2407 
2408                         /* change absolute times to relative times. */
2409                         cur_ddinfo.xmit_idle = (cur_jiffies - ppp->ddinfo.xmit_idle) / HZ;
2410                         cur_ddinfo.recv_idle = (cur_jiffies - ppp->ddinfo.recv_idle) / HZ;
2411                         memcpy_tofs ((void *) param3, &cur_ddinfo,
2412                                      sizeof (cur_ddinfo));
2413                         if (ppp->flags & SC_DEBUG)
2414                                 printk (KERN_INFO
2415                                 "ppp_tty_ioctl: read demand dial info\n");
2416                 }
2417                 break;
2418 /*
2419  * Retrieve the extended async map
2420  */
2421         case PPPIOCGXASYNCMAP:
2422                 error = verify_area (VERIFY_WRITE,
2423                                      (void *) param3,
2424                                      sizeof (ppp->xmit_async_map));
2425                 if (error == 0) {
2426                         memcpy_tofs ((void *) param3,
2427                                      ppp->xmit_async_map,
2428                                      sizeof (ppp->xmit_async_map));
2429 
2430                         if (ppp->flags & SC_DEBUG)
2431                                 printk (KERN_INFO
2432                                 "ppp_tty_ioctl: get xasyncmap: addr %lx\n",
2433                                 param3);
2434                 }
2435                 break;
2436 /*
2437  * Set the async extended map
2438  */
2439         case PPPIOCSXASYNCMAP:
2440                 error = verify_area (VERIFY_READ, (void *) param3,
2441                                      sizeof (ppp->xmit_async_map));
2442                 if (error == 0) {
2443                         __u32 temp_tbl[8];
2444 
2445                         memcpy_fromfs (temp_tbl, (void *) param3,
2446                                        sizeof (ppp->xmit_async_map));
2447                         temp_tbl[1]  =  0x00000000;
2448                         temp_tbl[2] &= ~0x40000000;
2449                         temp_tbl[3] |=  0x60000000;
2450 
2451                         if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2452                             (temp_tbl[4] & temp_tbl[5]) != 0 ||
2453                             (temp_tbl[6] & temp_tbl[7]) != 0)
2454                                 error = -EINVAL;
2455                         else {
2456                                 memcpy (ppp->xmit_async_map, temp_tbl,
2457                                         sizeof (ppp->xmit_async_map));
2458 
2459                                 if (ppp->flags & SC_DEBUG)
2460                                         printk (KERN_INFO
2461                                         "ppp_tty_ioctl: set xasyncmap\n");
2462                         }
2463                 }
2464                 break;
2465 /*
2466  * Set the maximum VJ header compression slot number.
2467  */
2468         case PPPIOCSMAXCID:
2469                 error = verify_area (VERIFY_READ, (void *) param3,
2470                                      sizeof (temp_i));
2471                 if (error == 0) {
2472                         temp_i = get_user ((int *) param3) + 1;
2473                         if (ppp->flags & SC_DEBUG)
2474                                 printk (KERN_INFO
2475                                      "ppp_tty_ioctl: set maxcid to %d\n",
2476                                      temp_i);
2477                         if (ppp->slcomp != NULL)
2478                                 slhc_free (ppp->slcomp);
2479                         ppp->slcomp = slhc_init (16, temp_i);
2480 
2481                         if (ppp->slcomp == NULL) {
2482                                 if (ppp->flags & SC_DEBUG)
2483                                         printk (KERN_ERR
2484                                         "ppp: no space for compression buffers!\n");
2485                                 ppp_release (ppp);
2486                                 error = -ENOMEM;
2487                         }
2488                 }
2489                 break;
2490 
2491     case PPPIOCXFERUNIT:
2492                 ppp_tty_close_local (tty, current->pid);
2493                 break;
2494 
2495     case PPPIOCGNPMODE:
2496     case PPPIOCSNPMODE:
2497                 error = verify_area (VERIFY_READ, (void *) param3,
2498                                      sizeof (struct npioctl));
2499                 if (error == 0) {
2500                         struct npioctl npi;
2501                         memcpy_fromfs (&npi,
2502                                        (void *) param3,
2503                                        sizeof (npi));
2504 
2505                         switch (npi.protocol) {
2506                         case PPP_IP:
2507                                 npi.protocol = NP_IP;
2508                                 break;
2509                         default:
2510                                 error = -EINVAL;
2511                         }
2512 
2513                         if (error != 0)
2514                                 break;
2515 
2516                         if (param2 == PPPIOCGNPMODE) {
2517                                 npi.mode = ppp->sc_npmode[npi.protocol];
2518                                 error = verify_area (VERIFY_WRITE,
2519                                                      (void *) param3,
2520                                                      sizeof (npi));
2521                                 if (error != 0)
2522                                         break;
2523 
2524                                 memcpy_tofs ((void *) param3,
2525                                              &npi,
2526                                              sizeof (npi));
2527                                 break;
2528                         }
2529 
2530                         if (npi.mode != ppp->sc_npmode[npi.protocol]) {
2531                                 ppp->sc_npmode[npi.protocol] = npi.mode;
2532                                 if (npi.mode != NPMODE_QUEUE) {
2533                                         /* ppp_requeue(ppp); maybe needed */
2534                                         ppp_tty_wakeup (ppp2tty(ppp));
2535                                 }
2536                         }
2537                 }
2538                 break;
2539 /*
2540  * Allow users to read, but not set, the serial port parameters
2541  */
2542         case TCGETS:
2543         case TCGETA:
2544                 error = n_tty_ioctl (tty, file, param2, param3);
2545                 break;
2546 
2547         case FIONREAD:
2548                 error = verify_area (VERIFY_WRITE,
2549                                      (void *) param3,
2550                                      sizeof (int));
2551                 if (error == 0) {
2552                         int count = ppp->ubuf->tail - ppp->ubuf->head;
2553                         if (count < 0)
2554                                 count += (ppp->ubuf->size + 1);
2555 
2556                         put_user (count, (int *) param3);
2557                 }
2558                 break;
2559 /*
2560  *  All other ioctl() events will come here.
2561  */
2562         default:
2563                 if (ppp->flags & SC_DEBUG)
2564                         printk (KERN_ERR
2565                                 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2566                                 param2,
2567                                 param3);
2568 
2569                 error = -ENOIOCTLCMD;
2570                 break;
2571         }
2572         return error;
2573 }
2574 
2575 /*
2576  * TTY callback.
2577  *
2578  * Process the select() statement for the PPP device.
2579  */
2580 
2581 static int
2582 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
     /* [previous][next][first][last][top][bottom][index][help] */
2583                 struct file *filp, int sel_type, select_table * wait)
2584 {
2585         struct ppp *ppp = tty2ppp (tty);
2586         int result = 1;
2587 /*
2588  * Verify the status of the PPP device.
2589  */
2590         if (!ppp)
2591                 return -EBADF;
2592 
2593         if (ppp->magic != PPP_MAGIC)
2594                 return -EBADF;
2595 
2596         CHECK_PPP (0);
2597 /*
2598  * Branch on the type of select mode. A read request must lock the user
2599  * buffer area.
2600  */
2601         switch (sel_type) {
2602         case SEL_IN:
2603                 if (set_bit (0, &ppp->ubuf->locked) == 0) {
2604                         /* Test for the presence of data in the queue */
2605                         if (ppp->ubuf->head != ppp->ubuf->tail) {
2606                                 clear_bit (0, &ppp->ubuf->locked);
2607                                 break;
2608                         }
2609                         clear_bit (0, &ppp->ubuf->locked);
2610                 }               /* fall through */
2611 /*
2612  * Exceptions or read errors.
2613  */
2614         case SEL_EX:
2615                 /* Is this a pty link and the remote disconnected? */
2616                 if (tty->flags & (1 << TTY_SLAVE_CLOSED))
2617                         break;
2618 
2619                 /* Is this a local link and the modem disconnected? */
2620                 if (tty_hung_up_p (filp))
2621                         break;
2622 
2623                 select_wait (&ppp->read_wait, wait);
2624                 result = 0;
2625                 break;
2626 /*
2627  * Write mode. A write is allowed if there is no current transmission.
2628  */
2629         case SEL_OUT:
2630                 if (ppp->tbuf->locked != 0) {
2631                         select_wait (&ppp->write_wait, wait);
2632                         result = 0;
2633                 }
2634                 break;
2635         }
2636         return result;
2637 }
2638 
2639 /*************************************************************
2640  * NETWORK OUTPUT
2641  *    This routine accepts requests from the network layer
2642  *    and attempts to deliver the packets.
2643  *    It also includes various routines we are compelled to
2644  *    have to make the network layer work (arp, etc...).
2645  *************************************************************/
2646 
2647 /*
2648  * Callback from the network layer when the device goes up.
2649  */
2650 
2651 static int
2652 ppp_dev_open (struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2653 {
2654         struct ppp *ppp = dev2ppp (dev);
2655 
2656         /* reset POINTOPOINT every time, since dev_close zaps it! */
2657         dev->flags |= IFF_POINTOPOINT;
2658 
2659         if (ppp2tty (ppp) == NULL) {
2660                 if (ppp->flags & SC_DEBUG)
2661                         printk (KERN_ERR
2662                         "ppp: %s not connected to a TTY! can't go open!\n",
2663                         dev->name);
2664                 return -ENXIO;
2665         }
2666 
2667         if (ppp->flags & SC_DEBUG)
2668                 printk (KERN_INFO
2669                         "ppp: channel %s going up for IP packets!\n",
2670                         dev->name);
2671 
2672         CHECK_PPP (-ENXIO);
2673         return 0;
2674 }
2675 
2676 /*
2677  * Callback from the network layer when the ppp device goes down.
2678  */
2679 
2680 static int
2681 ppp_dev_close (struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2682 {
2683         struct ppp *ppp = dev2ppp (dev);
2684 
2685         if (ppp2tty (ppp) == NULL) {
2686                 if (ppp->flags & SC_DEBUG)
2687                         printk (KERN_ERR
2688                         "ppp: %s not connected to a TTY! can't go down!\n",
2689                         dev->name);
2690                 return -ENXIO;
2691         }
2692 /*
2693  * We don't do anything about the device going down. It is not important
2694  * for us.
2695  */
2696         if (ppp->flags & SC_DEBUG)
2697                 printk (KERN_INFO
2698                         "ppp: channel %s going down for IP packets!\n",
2699                         dev->name);
2700         CHECK_PPP (-ENXIO);
2701         return 0;
2702 }
2703 
2704 /*
2705  * IOCTL operation to read the version of the driver.
2706  */
2707 
2708 static int
2709 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
     /* [previous][next][first][last][top][bottom][index][help] */
2710 {
2711         int error;
2712         int len;
2713         char *result;
2714 /*
2715  * Must have write access to the buffer.
2716  */
2717         result = (char *) ifr->ifr_ifru.ifru_data;
2718         len    = strlen (szVersion) + 1;
2719         error  = verify_area (VERIFY_WRITE, result, len);
2720 /*
2721  * Move the version data
2722  */
2723         if (error == 0)
2724                 memcpy_tofs (result, szVersion, len);
2725 
2726         return error;
2727 }
2728 
2729 /*
2730  * IOCTL to read the statistics for the pppstats program.
2731  */
2732 
2733 static int
2734 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2735 {
2736         struct ppp_stats *result, temp;
2737         int    error;
2738 /*
2739  * Must have write access to the buffer.
2740  */
2741         result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2742         error = verify_area (VERIFY_WRITE,
2743                              result,
2744                              sizeof (temp));
2745 /*
2746  * Supply the information for the caller. First move the version data
2747  * then move the ppp stats; and finally the vj stats.
2748  */
2749         memset (&temp, 0, sizeof(temp));
2750         if (error == 0 && dev->flags & IFF_UP) {
2751                 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2752                 if (ppp->slcomp != NULL) {
2753                         temp.vj.vjs_packets    = ppp->slcomp->sls_o_compressed+
2754                                                  ppp->slcomp->sls_o_uncompressed;
2755                         temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2756                         temp.vj.vjs_searches   = ppp->slcomp->sls_o_searches;
2757                         temp.vj.vjs_misses     = ppp->slcomp->sls_o_misses;
2758                         temp.vj.vjs_errorin    = ppp->slcomp->sls_i_error;
2759                         temp.vj.vjs_tossed     = ppp->slcomp->sls_i_tossed;
2760                         temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2761                         temp.vj.vjs_compressedin   = ppp->slcomp->sls_i_compressed;
2762                 }
2763         }
2764 
2765         if (error == 0)
2766                 memcpy_tofs (result, &temp, sizeof (temp));
2767         return error;
2768 }
2769 
2770 /*
2771  * IOCTL to read the compression statistics for the pppstats program.
2772  */
2773 
2774 static int
2775 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
2776 {
2777         struct ppp_comp_stats *result, temp;
2778         int    error;
2779 /*
2780  * Must have write access to the buffer.
2781  */
2782         result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2783         error = verify_area (VERIFY_WRITE,
2784                              result,
2785                              sizeof (temp));
2786 /*
2787  * Supply the information for the caller.
2788  */
2789         memset (&temp, 0, sizeof(temp));
2790         if (error == 0 && dev->flags & IFF_UP) {
2791                 if (ppp->sc_xc_state != NULL)
2792                         (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2793                                                      &temp.c);
2794 
2795                 if (ppp->sc_rc_state != NULL)
2796                         (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2797                                                        &temp.d);
2798         }
2799 /*
2800  * Move the data to the caller's buffer
2801  */
2802         if (error == 0)
2803                 memcpy_tofs (result, &temp, sizeof (temp));
2804         return error;
2805 }
2806 
2807 /*
2808  * Callback from the network layer to process the sockioctl functions.
2809  */
2810 
2811 static int
2812 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
2813 {
2814         struct ppp *ppp = dev2ppp (dev);
2815         int error;
2816 /*
2817  * Process the requests
2818  */
2819         switch (cmd) {
2820         case SIOCGPPPSTATS:
2821                 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2822                 break;
2823 
2824         case SIOCGPPPCSTATS:
2825                 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2826                 break;
2827 
2828         case SIOCGPPPVER:
2829                 error = ppp_dev_ioctl_version (ppp, ifr);
2830                 break;
2831 
2832         default:
2833                 error = -EINVAL;
2834                 break;
2835         }
2836         return error;
2837 }
2838 
2839 /*
2840  * Send an IP frame to the remote with vj header compression.
2841  *
2842  * Return 0 if frame was queued for transmission.
2843  *        1 if frame must be re-queued for later driver support.
2844  */
2845 
2846 static int
2847 ppp_dev_xmit_ip (struct device *dev, struct ppp *ppp, __u8 *data)
     /* [previous][next][first][last][top][bottom][index][help] */
2848 {
2849         int               proto = PPP_IP;
2850         int               len;
2851         struct ppp_hdr    *hdr;
2852         struct tty_struct *tty = ppp2tty (ppp);
2853 /*
2854  * Obtain the length from the IP header.
2855  */
2856         len = ((struct iphdr *)data) -> tot_len;
2857         len = ntohs (len);
2858 /*
2859  * Validate the tty interface
2860  */
2861         if (tty == NULL) {
2862                 if (ppp->flags & SC_DEBUG)
2863                         printk (KERN_ERR
2864                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
2865                                 dev->name);
2866                 return 0;
2867         }
2868 /*
2869  * Ensure that the PPP device is still up
2870  */
2871         if (!(dev->flags & IFF_UP)) {
2872                 if (ppp->flags & SC_DEBUG)
2873                         printk (KERN_WARNING
2874                                 "ppp_dev_xmit: packet sent on interface %s,"
2875                                 " which is down for IP\n",
2876                                 dev->name);
2877                 return 0;
2878         }
2879 /*
2880  * Branch on the type of processing for the IP frame.
2881  */
2882         switch (ppp->sc_npmode[NP_IP]) {
2883         case NPMODE_PASS:
2884                 break;
2885 
2886         case NPMODE_ERROR:
2887                 if (ppp->flags & SC_DEBUG)
2888                         printk (KERN_WARNING
2889                                 "ppp_dev_xmit: npmode = NPMODE_ERROR on %s\n",
2890                                 dev->name);
2891                 return 0;
2892 
2893         case NPMODE_DROP:
2894                 if (ppp->flags & SC_DEBUG)
2895                         printk (KERN_WARNING
2896                                 "ppp_dev_xmit: npmode = NPMODE_DROP on %s\n",
2897                                 dev->name);
2898                 return 0;
2899 
2900         case NPMODE_QUEUE:
2901                 break;
2902 
2903         default:
2904                 if (ppp->flags & SC_DEBUG)
2905                         printk (KERN_WARNING
2906                                 "ppp_dev_xmit: unknown npmode %d on %s\n",
2907                                 ppp->sc_npmode[NP_IP],
2908                                 dev->name);
2909                 return 0;
2910         }
2911 /*
2912  * Detect a change in the transfer size
2913  */
2914         if (ppp->mtu != ppp2dev (ppp)->mtu) {
2915                 ppp_changedmtu (ppp,
2916                                 ppp2dev (ppp)->mtu,
2917                                 ppp->mru);
2918         }
2919 /*
2920  * Acquire the lock on the transmission buffer. If the buffer was busy then
2921  * mark the device as busy.
2922  */
2923         if (lock_buffer (ppp->wbuf) != 0) {
2924                 dev->tbusy = 1;
2925                 return 1;
2926         }
2927 /*
2928  * Print the frame being sent
2929  */
2930         if (ppp->flags & SC_LOG_OUTPKT)
2931                 ppp_print_buffer ("ppp outpkt", data, len);
2932 /*
2933  * At this point, the buffer will be transmitted. There is no other exit.
2934  *
2935  * Try to compress the header.
2936  */
2937         if (ppp->flags & SC_COMP_TCP) {
2938                 len = slhc_compress (ppp->slcomp, data, len,
2939                                      buf_base (ppp->cbuf) + PPP_HARD_HDR_LEN,
2940                                      &data,
2941                                      (ppp->flags & SC_NO_TCP_CCID) == 0);
2942 
2943                 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2944                         proto    = PPP_VJC_COMP;
2945                         data[0] ^= SL_TYPE_COMPRESSED_TCP;
2946                 } else {
2947                         if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2948                                 proto = PPP_VJC_UNCOMP;
2949                         data[0] = (data[0] & 0x0f) | 0x40;
2950                 }
2951         }
2952 /*
2953  * Send the frame
2954  */
2955         len  += PPP_HARD_HDR_LEN;
2956         hdr   = &((struct ppp_hdr *) data)[-1];
2957 
2958         hdr->address     = PPP_ALLSTATIONS;
2959         hdr->control     = PPP_UI;
2960         hdr->protocol[0] = 0;
2961         hdr->protocol[1] = proto;
2962 
2963         return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
2964 }
2965 
2966 /*
2967  * Send an IPX (or any other non-IP) frame to the remote.
2968  *
2969  * Return 0 if frame was queued for transmission.
2970  *        1 if frame must be re-queued for later driver support.
2971  */
2972 static int
2973 ppp_dev_xmit_ipx (struct device *dev, struct ppp *ppp,
     /* [previous][next][first][last][top][bottom][index][help] */
2974                   __u8 *data, int len, int proto)
2975 {
2976         struct tty_struct *tty = ppp2tty (ppp);
2977         struct ppp_hdr    *hdr;
2978 /*
2979  * Validate the tty interface
2980  */
2981         if (tty == NULL) {
2982                 if (ppp->flags & SC_DEBUG)
2983                         printk (KERN_ERR
2984                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
2985                                 dev->name);
2986                 return 0;
2987         }
2988 /*
2989  * Ensure that the PPP device is still up
2990  */
2991         if (!(dev->flags & IFF_UP)) {
2992                 if (ppp->flags & SC_DEBUG)
2993                         printk (KERN_WARNING
2994                                 "ppp_dev_xmit: packet sent on interface %s,"
2995                                 " which is down\n",
2996                                 dev->name);
2997                 return 0;
2998         }
2999 /*
3000  * Detect a change in the transfer size
3001  */
3002         if (ppp->mtu != ppp2dev (ppp)->mtu) {
3003                 ppp_changedmtu (ppp,
3004                                 ppp2dev (ppp)->mtu,
3005                                 ppp->mru);
3006         }
3007 /*
3008  * Acquire the lock on the transmission buffer. If the buffer was busy then
3009  * mark the device as busy.
3010  */
3011         if (lock_buffer (ppp->wbuf) != 0) {
3012                 dev->tbusy = 1;
3013                 return 1;
3014         }
3015 /*
3016  * Print the frame being sent
3017  */
3018         if (ppp->flags & SC_LOG_OUTPKT)
3019                 ppp_print_buffer ("ppp outpkt", data, len);
3020 /*
3021  * Send the frame
3022  */
3023         len  += PPP_HARD_HDR_LEN;
3024         hdr   = &((struct ppp_hdr *) data)[-1];
3025 
3026         hdr->address     = PPP_ALLSTATIONS;
3027         hdr->control     = PPP_UI;
3028         hdr->protocol[0] = proto >> 8;
3029         hdr->protocol[1] = proto;
3030 
3031         return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
3032 }
3033 
3034 /*
3035  * Send a frame to the remote.
3036  */
3037 
3038 static int
3039 ppp_dev_xmit (sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
3040 {
3041         int answer, len;
3042         __u8              *data;
3043         struct ppp        *ppp = dev2ppp (dev);
3044         struct tty_struct *tty = ppp2tty (ppp);
3045 /*
3046  * just a little sanity check.
3047  */
3048         if (skb == NULL) {
3049                 if (ppp->flags & SC_DEBUG)
3050                         printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3051                 return 0;
3052         }
3053 /*
3054  * Avoid timing problem should tty hangup while data is queued to be sent
3055  */
3056         if (!ppp->inuse) {
3057                 dev_kfree_skb (skb, FREE_WRITE);
3058                 dev_close (dev);
3059                 return 0;
3060         }
3061 /*
3062  * Validate the tty linkage
3063  */
3064         if (ppp->flags & SC_DEBUG)
3065                 printk (KERN_DEBUG "ppp_dev_xmit [%s]: skb %p\n",
3066                         dev->name, skb);
3067 /*
3068  * Validate the tty interface
3069  */
3070         if (tty == NULL) {
3071                 if (ppp->flags & SC_DEBUG)
3072                         printk (KERN_ERR
3073                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
3074                                 dev->name);
3075                 dev_kfree_skb (skb, FREE_WRITE);
3076                 return 0;
3077         }
3078 /*
3079  * Fetch the pointer to the data
3080  */
3081         len   = skb->len;
3082         data  = skb_data(skb);
3083 /*
3084  * Look at the protocol in the skb to determine the difference between
3085  * an IP frame and an IPX frame.
3086  */
3087         switch (ntohs (skb->protocol)) {
3088         case ETH_P_IPX:
3089                 answer = ppp_dev_xmit_ipx (dev, ppp, data, len, PPP_IPX);
3090                 break;
3091 
3092         case ETH_P_IP:
3093                 answer = ppp_dev_xmit_ip (dev, ppp, data);
3094                 break;
3095 
3096         default: /* All others have no support at this time. */
3097                 dev_kfree_skb (skb, FREE_WRITE);
3098                 return 0;
3099         }
3100 /*
3101  * This is the end of the transmission. Release the buffer if it was sent.
3102  */
3103         if (answer == 0) {
3104                 dev_kfree_skb (skb, FREE_WRITE);
3105                 ppp->ddinfo.xmit_idle = jiffies;
3106         }
3107         return answer;
3108 }
3109 
3110 /*
3111  * Generate the statistic information for the /proc/net/dev listing.
3112  */
3113 
3114 static struct enet_statistics *
3115 ppp_dev_stats (struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
3116 {
3117         struct ppp *ppp = dev2ppp (dev);
3118         static struct enet_statistics ppp_stats;
3119 
3120         ppp_stats.rx_packets          = ppp->stats.ppp_ipackets;
3121         ppp_stats.rx_errors           = ppp->stats.ppp_ierrors;
3122         ppp_stats.rx_dropped          = ppp->stats.ppp_ierrors;
3123         ppp_stats.rx_fifo_errors      = 0;
3124         ppp_stats.rx_length_errors    = 0;
3125         ppp_stats.rx_over_errors      = 0;
3126         ppp_stats.rx_crc_errors       = 0;
3127         ppp_stats.rx_frame_errors     = 0;
3128         ppp_stats.tx_packets          = ppp->stats.ppp_opackets;
3129         ppp_stats.tx_errors           = ppp->stats.ppp_oerrors;
3130         ppp_stats.tx_dropped          = 0;
3131         ppp_stats.tx_fifo_errors      = 0;
3132         ppp_stats.collisions          = 0;
3133         ppp_stats.tx_carrier_errors   = 0;
3134         ppp_stats.tx_aborted_errors   = 0;
3135         ppp_stats.tx_window_errors    = 0;
3136         ppp_stats.tx_heartbeat_errors = 0;
3137 
3138         if (ppp->flags & SC_DEBUG)
3139                 printk (KERN_INFO "ppp_dev_stats called");
3140         return &ppp_stats;
3141 }
3142 
3143 static int ppp_dev_header (sk_buff *skb, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
3144                            __u16 type, void *daddr,
3145                            void *saddr, unsigned int len)
3146 {
3147         return (0);
3148 }
3149 
3150 static int
3151 ppp_dev_rebuild (void *eth, struct device *dev,
     /* [previous][next][first][last][top][bottom][index][help] */
3152                  unsigned long raddr, struct sk_buff *skb)
3153 {
3154         return (0);
3155 }
3156 
3157 /*************************************************************
3158  * UTILITIES
3159  *    Miscellany called by various functions above.
3160  *************************************************************/
3161 
3162 /* Locate the previous instance of the PPP channel */
3163 static struct ppp *
3164 ppp_find (int pid_value)
     /* [previous][next][first][last][top][bottom][index][help] */
3165 {
3166         int             if_num;
3167         ppp_ctrl_t      *ctl;
3168         struct ppp      *ppp;
3169 
3170         /* try to find the exact same free device which we had before */
3171         ctl      = ppp_list;
3172         if_num   = 0;
3173   
3174         while (ctl) {
3175                 ppp = ctl2ppp (ctl);
3176                 if (!set_bit(0, &ppp->inuse)) {
3177                         if (ppp->sc_xfer == pid_value) {
3178                                 ppp->sc_xfer = 0;
3179                                 return (ppp);
3180                         }
3181                         clear_bit (0, &ppp->inuse);
3182                 }
3183                 ctl = ctl->next;
3184                 if (++if_num == max_dev)
3185                         break;
3186         }
3187         return NULL;
3188 }
3189 
3190 /* allocate or create a PPP channel */
3191 static struct ppp *
3192 ppp_alloc (void)
     /* [previous][next][first][last][top][bottom][index][help] */
3193 {
3194         int             if_num;
3195         int             status;
3196         ppp_ctrl_t      *ctl;
3197         struct device   *dev;
3198         struct ppp      *ppp;
3199 
3200         /* try to find an free device */
3201         ctl      = ppp_list;
3202         if_num   = 0;
3203   
3204         while (ctl) {
3205                 ppp = ctl2ppp (ctl);
3206                 if (!set_bit(0, &ppp->inuse))
3207                         return (ppp);
3208                 ctl = ctl->next;
3209                 if (++if_num == max_dev)
3210                         return (NULL);
3211         }
3212 /*
3213  * There are no available items. Allocate a device from the system pool
3214  */
3215         ctl = (ppp_ctrl_t *) kmalloc (sizeof(ppp_ctrl_t), GFP_KERNEL);
3216         if (ctl) {
3217                 (void) memset(ctl, 0, sizeof(ppp_ctrl_t));
3218                 ppp = ctl2ppp (ctl);
3219                 dev = ctl2dev (ctl);
3220 
3221                 /* initialize channel control data */
3222                 set_bit(0, &ppp->inuse);
3223 
3224                 ppp->line      = if_num;
3225                 ppp->tty       = NULL;
3226                 ppp->dev       = dev;
3227     
3228                 dev->next      = NULL;
3229                 dev->init      = ppp_init_dev;
3230                 dev->name      = ctl->name;
3231                 dev->base_addr = (__u32) if_num;
3232                 dev->priv      = (void *) ppp;
3233 
3234                 sprintf (dev->name, "ppp%d", if_num);
3235     
3236                 /* link in the new channel */
3237                 ctl->next      = ppp_list;
3238                 ppp_list       = ctl;
3239 
3240                 /* register device so that we can be ifconfig'd */
3241                 /* ppp_init_dev() will be called as a side-effect */
3242 
3243                 status = register_netdev (dev);
3244                 if (status == 0) {
3245                         printk (KERN_INFO "registered device %s\n", dev->name);
3246                         return (ppp);
3247                 }
3248 
3249                 printk (KERN_ERR
3250                        "ppp_alloc - register_netdev(%s) = %d failure.\n",
3251                         dev->name, status);
3252                 /* This one will forever be busy as it is not initialized */
3253         }
3254         return (NULL);
3255 }
3256 
3257 /*
3258  * Utility procedures to print a buffer in hex/ascii
3259  */
3260 
3261 static void
3262 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
3263 {
3264         register __u8 next_ch;
3265         static char hex[] = "0123456789ABCDEF";
3266 
3267         while (count-- > 0) {
3268                 next_ch = *in++;
3269                 *out++ = hex[(next_ch >> 4) & 0x0F];
3270                 *out++ = hex[next_ch & 0x0F];
3271                 ++out;
3272         }
3273 }
3274 
3275 static void
3276 ppp_print_char (register __u8 * out, const __u8 * in, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
3277 {
3278         register __u8 next_ch;
3279 
3280         while (count-- > 0) {
3281                 next_ch = *in++;
3282 
3283                 if (next_ch < 0x20 || next_ch > 0x7e)
3284                         *out++ = '.';
3285                 else {
3286                         *out++ = next_ch;
3287                         if (next_ch == '%')   /* printk/syslogd has a bug !! */
3288                                 *out++ = '%';
3289                 }
3290         }
3291         *out = '\0';
3292 }
3293 
3294 static void
3295 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
3296 {
3297         __u8 line[44];
3298 
3299         if (name != (__u8 *) NULL)
3300                 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3301 
3302         while (count > 8) {
3303                 memset (line, 32, 44);
3304                 ppp_print_hex (line, buf, 8);
3305                 ppp_print_char (&line[8 * 3], buf, 8);
3306                 printk (KERN_DEBUG "%s\n", line);
3307                 count -= 8;
3308                 buf += 8;
3309         }
3310 
3311         if (count > 0) {
3312                 memset (line, 32, 44);
3313                 ppp_print_hex (line, buf, count);
3314                 ppp_print_char (&line[8 * 3], buf, count);
3315                 printk (KERN_DEBUG "%s\n", line);
3316         }
3317 }
3318 
3319 /*************************************************************
3320  * Compressor module interface
3321  *************************************************************/
3322 
3323 struct compressor_link {
3324         struct compressor_link  *next;
3325         struct compressor       *comp;
3326 };
3327 
3328 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3329 
3330 static struct compressor *find_compressor (int type)
     /* [previous][next][first][last][top][bottom][index][help] */
3331 {
3332         struct compressor_link *lnk;
3333         __u32 flags;
3334 
3335         save_flags(flags);
3336         cli();
3337 
3338         lnk = ppp_compressors;
3339         while (lnk != (struct compressor_link *) 0) {
3340                 if ((int) (__u8) lnk->comp->compress_proto == type) {
3341                         restore_flags(flags);
3342                         return lnk->comp;
3343                 }
3344                 lnk = lnk->next;
3345         }
3346 
3347         restore_flags(flags);
3348         return (struct compressor *) 0;
3349 }
3350 
3351 static int ppp_register_compressor (struct compressor *cp)
     /* [previous][next][first][last][top][bottom][index][help] */
3352 {
3353         struct compressor_link *new;
3354         __u32 flags;
3355 
3356         new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3357 
3358         if (new == (struct compressor_link *) 0)
3359                 return 1;
3360 
3361         save_flags(flags);
3362         cli();
3363 
3364         if (find_compressor (cp->compress_proto)) {
3365                 restore_flags(flags);
3366                 kfree (new);
3367                 return 0;
3368         }
3369 
3370         new->next       = ppp_compressors;
3371         new->comp       = cp;
3372         ppp_compressors = new;
3373 
3374         restore_flags(flags);
3375         return 0;
3376 }
3377 
3378 static void ppp_unregister_compressor (struct compressor *cp)
     /* [previous][next][first][last][top][bottom][index][help] */
3379 {
3380         struct compressor_link *prev = (struct compressor_link *) 0;
3381         struct compressor_link *lnk;
3382         __u32 flags;
3383 
3384         save_flags(flags);
3385         cli();
3386 
3387         lnk  = ppp_compressors;
3388         while (lnk != (struct compressor_link *) 0) {
3389                 if (lnk->comp == cp) {
3390                         if (prev)
3391                                 prev->next = lnk->next;
3392                         else
3393                                 ppp_compressors = lnk->next;
3394                         kfree (lnk);
3395                         break;
3396                 }
3397                 prev = lnk;
3398                 lnk  = lnk->next;
3399         }
3400         restore_flags(flags);
3401 }
3402 
3403 /*************************************************************
3404  * Module support routines
3405  *************************************************************/
3406 
3407 #ifdef MODULE
3408 int
3409 init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3410 {
3411         int status;
3412 
3413         /* register our line disciplines */
3414         status = ppp_first_time();
3415         if (status != 0)
3416                 printk (KERN_INFO
3417                        "PPP: ppp_init() failure %d\n", status);
3418         else
3419                 (void) register_symtab (&ppp_syms);
3420         return (status);
3421 }
3422 
3423 void
3424 cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3425 {
3426         int status;
3427         ppp_ctrl_t *ctl, *next_ctl;
3428         struct device *dev;
3429         struct ppp *ppp;
3430         int busy_flag = 0;
3431 /*
3432  * Ensure that the devices are not in operation.
3433  */
3434         ctl = ppp_list;
3435         while (ctl) {
3436                 ppp = ctl2ppp (ctl);
3437                 if (ppp->inuse && ppp->tty != NULL) {
3438                         busy_flag = 1;
3439                         break;
3440                 }
3441 
3442                 dev = ctl2dev (ctl);
3443                 if (dev->start || dev->flags & IFF_UP) {
3444                         busy_flag = 1;
3445                         break;
3446                 }
3447                 ctl = ctl->next;
3448         }
3449 /*
3450  * Ensure that there are no compressor modules registered
3451  */
3452         if (ppp_compressors != NULL)
3453                 busy_flag = 1;
3454 
3455         if (busy_flag) {
3456                 printk (KERN_INFO
3457                         "PPP: device busy, remove delayed\n");
3458                 return;
3459         }
3460 /*
3461  * Release the tty registration of the line dicipline so that no new entries
3462  * may be created.
3463  */
3464         status = tty_register_ldisc (N_PPP, NULL);
3465         if (status != 0)
3466                 printk (KERN_INFO
3467                         "PPP: Unable to unregister ppp line discipline "
3468                         "(err = %d)\n", status);
3469         else
3470                 printk (KERN_INFO
3471                        "PPP: ppp line discipline successfully unregistered\n");
3472 /*
3473  * De-register the devices so that there is no problem with them
3474  */     
3475         next_ctl = ppp_list;
3476         while (next_ctl) {
3477                 ctl      = next_ctl;
3478                 next_ctl = ctl->next;
3479                 ppp      = ctl2ppp (ctl);
3480                 dev      = ctl2dev (ctl);
3481 
3482                 ppp_release       (ppp);
3483                 unregister_netdev (dev);
3484                 kfree (ctl);
3485         }
3486 }
3487 #endif

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