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

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