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

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