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