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