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