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