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