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