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