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

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