]> git.ozlabs.org Git - ppp.git/blob - linux/ppp.c
avoid compiler warning
[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 /* $Id: ppp.c,v 1.8 1996/09/14 05:37:07 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/if.h>
82 #include <linux/if_ether.h>
83 #include <linux/netdevice.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         unsigned long 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         unsigned long 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 static void
1051 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1052                  char *flags, int count)
1053 {
1054         register struct ppp *ppp = tty2ppp (tty);
1055         register struct ppp_buffer *buf = NULL;
1056         __u8 chr;
1057
1058         if (count >= TTY_FLIPBUF_SIZE)
1059                 printk(KERN_WARNING "ppp_tty_receive: got %d chars\n", count);
1060
1061 /*
1062  * Fetch the pointer to the buffer. Be careful about race conditions.
1063  */
1064         if (ppp != NULL)
1065                 buf = ppp->rbuf;
1066
1067         if (buf == NULL)
1068                 return;
1069 /*
1070  * Verify the table pointer and ensure that the line is
1071  * still in PPP discipline.
1072  */
1073         if (ppp->magic != PPP_MAGIC) {
1074                 if (ppp->flags & SC_DEBUG)
1075                         printk (KERN_DEBUG
1076                                 "PPP: handler called but couldn't find "
1077                                 "PPP struct.\n");
1078                 return;
1079         }
1080         CHECK_PPP_VOID ();
1081 /*
1082  * Print the buffer if desired
1083  */
1084         if (ppp->flags & SC_LOG_RAWIN)
1085                 ppp_print_buffer ("receive buffer", data, count);
1086 /*
1087  * Collect the character and error condition for the character. Set the toss
1088  * flag for the first character error.
1089  */
1090         while (count-- > 0) {
1091                 ppp->bytes_rcvd++;
1092                 chr = *data++;
1093                 if (flags) {
1094                         if (*flags && ppp->toss == 0)
1095                                 ppp->toss = *flags;
1096                         ++flags;
1097                 }
1098 /*
1099  * Set the flags for 8 data bits and no parity.
1100  *
1101  * Actually, it sets the flags for d7 being 0/1 and parity being even/odd
1102  * so that the normal processing would have all flags set at the end of the
1103  * session. A missing flag bit would denote an error condition.
1104  */
1105
1106 #ifdef CHECK_CHARACTERS
1107                 if (chr & 0x80)
1108                         ppp->flags |= SC_RCV_B7_1;
1109                 else
1110                         ppp->flags |= SC_RCV_B7_0;
1111
1112                 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1113                         ppp->flags |= SC_RCV_ODDP;
1114                 else
1115                         ppp->flags |= SC_RCV_EVNP;
1116 #endif
1117 /*
1118  * Branch on the character.
1119  */
1120                 switch (chr) {
1121 /*
1122  * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1123  * then the block is to be ignored. In addition, characters before the very
1124  * first FLAG are also tossed by this procedure.
1125  */
1126                 case PPP_FLAG:  /* PPP_FLAG: end of frame */
1127                         ppp->stats.ppp_ibytes += ppp->rbuf->count;
1128                         if (ppp->escape)
1129                                 ppp->toss |= 0x80;
1130 /*
1131  * Process frames which are not to be ignored. If the processing failed,
1132  * then clean up the VJ tables.
1133  */
1134                         if (ppp_doframe (ppp) == 0) {
1135                                 slhc_toss (ppp->slcomp);
1136                         }
1137 /*
1138  * Reset all indicators for the new frame to follow.
1139  */
1140                         buf->count  = 0;
1141                         buf->fcs    = PPP_INITFCS;
1142                         ppp->escape = 0;
1143                         ppp->toss   = 0;
1144                         break;
1145 /*
1146  * All other characters in the data come here. If the character is in the
1147  * receive mask then ignore the character.
1148  */
1149                 default:
1150                         /* If we're tossing, look no further. */
1151                         if (ppp->toss != 0)
1152                                 break;
1153
1154                         /* If this is a control char to be ignored, do so */
1155                         if (in_rmap (ppp, chr)) {
1156                                 if (ppp->flags & SC_DEBUG)
1157                                         printk(KERN_DEBUG "ignoring control char %x\n", chr);
1158                                 break;
1159                         }
1160
1161                         /*
1162                          * Modify the next character if preceded by escape.
1163                          * The escape character (0x7d) could be an escaped
1164                          * 0x5d, if it follows an escape :-)
1165                          */
1166                         if (ppp->escape) {
1167                                 chr ^= ppp->escape;
1168                                 ppp->escape = 0;
1169                         } else if (chr == PPP_ESCAPE) {
1170                                 ppp->escape = PPP_TRANS;
1171                                 break;
1172                         }
1173
1174 /*
1175  * If the count sent is within reason then store the character, bump the
1176  * count, and update the FCS for the character.
1177  */
1178                         if (buf->count < buf->size) {
1179                                 buf_base (buf)[buf->count++] = chr;
1180                                 buf->fcs = PPP_FCS (buf->fcs, chr);
1181                                 break;
1182                         }
1183 /*
1184  * The peer sent too much data. Set the flags to discard the current frame
1185  * and wait for the re-synchronization FLAG to be sent.
1186  */
1187                         ppp->stats.ppp_ierrors++;
1188                         ppp->toss |= 0xC0;
1189                         break;
1190                 }
1191         }
1192 }
1193
1194 /*
1195  * Put the input frame into the networking system for the indicated protocol
1196  */
1197
1198 static int
1199 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1200 {
1201         sk_buff *skb = dev_alloc_skb (count);
1202 /*
1203  * Generate a skb buffer for the new frame.
1204  */
1205         if (skb == NULL) {
1206                 if (ppp->flags & SC_DEBUG)
1207                         printk (KERN_ERR
1208                          "ppp_do_ip: packet dropped on %s (no memory)!\n",
1209                          ppp2dev (ppp)->name);
1210                 return 0;
1211         }
1212 /*
1213  * Move the received data from the input buffer to the skb buffer.
1214  */
1215         skb->dev      = ppp2dev (ppp);  /* We are the device */
1216         skb->protocol = proto;
1217         skb->mac.raw  = skb_data(skb);
1218         memcpy (skb_put(skb,count), data, count);       /* move data */
1219 /*
1220  * Tag the frame and kick it to the proper receive routine
1221  */
1222         skb->free = 1;
1223         ppp->ddinfo.recv_idle = jiffies;
1224         netif_rx (skb);
1225         return 1;
1226 }
1227
1228 /*
1229  * Process the receipt of an IP frame
1230  */
1231
1232 static int
1233 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1234 {
1235         if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1236                 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1237                         return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1238         return 0;
1239 }
1240
1241 /*
1242  * Process the receipt of an IPX frame
1243  */
1244
1245 static int
1246 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1247 {
1248         if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1249                 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1250         return 0;
1251 }
1252
1253 /*
1254  * Process the receipt of an VJ Compressed frame
1255  */
1256
1257 static int
1258 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
1259                     __u8 *data, int count)
1260 {
1261         if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1262                 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1263                 if (new_count >= 0) {
1264                         return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1265                 }
1266                 if (ppp->flags & SC_DEBUG)
1267                         printk (KERN_NOTICE
1268                                 "ppp: error in VJ decompression\n");
1269         }
1270         return 0;
1271 }
1272
1273 /*
1274  * Process the receipt of an VJ Un-compressed frame
1275  */
1276
1277 static int
1278 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
1279                       __u8 *data, int count)
1280 {
1281         if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1282                 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1283                         return rcv_proto_ip (ppp, PPP_IP, data, count);
1284                 }
1285                 if (ppp->flags & SC_DEBUG)
1286                         printk (KERN_NOTICE
1287                                 "ppp: error in VJ memorizing\n");
1288         }
1289         return 0;
1290 }
1291
1292 /*
1293  * Receive all unclassified protocols.
1294  */
1295
1296 static int
1297 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
1298                    __u8 *data, int len)
1299 {
1300         int totlen;
1301         register int current_idx;
1302
1303 #define PUTC(c)                                          \
1304 {                                                        \
1305     buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1306     current_idx &= ppp->ubuf->size;                      \
1307     if (current_idx == ppp->ubuf->tail)                  \
1308             goto failure;                                \
1309 }
1310
1311 /*
1312  * The total length includes the protocol data.
1313  * Lock the user information buffer.
1314  */
1315         if (set_bit (0, &ppp->ubuf->locked)) {
1316                 if (ppp->flags & SC_DEBUG)
1317                         printk (KERN_DEBUG
1318                                 "ppp_us_queue: can't get lock\n");
1319         } else {
1320                 current_idx = ppp->ubuf->head;
1321 /*
1322  * Insert the buffer length (not counted), the protocol, and the data
1323  */
1324                 totlen = len + 2;
1325                 PUTC (totlen >> 8);
1326                 PUTC (totlen);
1327
1328                 PUTC (proto >> 8);
1329                 PUTC (proto);
1330
1331                 totlen -= 2;
1332                 while (totlen-- > 0) {
1333                         PUTC (*data++);
1334                 }
1335 #undef PUTC
1336 /*
1337  * The frame is complete. Update the head pointer and wakeup the pppd
1338  * process.
1339  */
1340                 ppp->ubuf->head = current_idx;
1341
1342                 clear_bit (0, &ppp->ubuf->locked);
1343                 wake_up_interruptible (&ppp->read_wait);
1344                 if (ppp->tty->fasync != NULL)
1345                         kill_fasync (ppp->tty->fasync, SIGIO);
1346
1347                 if (ppp->flags & SC_DEBUG)
1348                         printk (KERN_INFO
1349                                 "ppp: successfully queued %d bytes, flags = %x\n",
1350                                 len + 2, ppp->flags);
1351
1352                 return 1;
1353 /*
1354  * The buffer is full. Unlock the header
1355  */
1356 failure:
1357                 clear_bit (0, &ppp->ubuf->locked);
1358                 if (ppp->flags & SC_DEBUG)
1359                         printk (KERN_INFO
1360                                 "ppp_us_queue: ran out of buffer space.\n");
1361         }
1362 /*
1363  * Discard the frame. There are no takers for this protocol.
1364  */
1365         if (ppp->flags & SC_DEBUG)
1366                 printk (KERN_WARNING
1367                         "ppp: dropping packet on the floor.\n");
1368         slhc_toss (ppp->slcomp);
1369         return 0;
1370 }
1371
1372 /*
1373  * Handle a CCP packet.
1374  *
1375  * The CCP packet is passed along to the pppd process just like any
1376  * other PPP frame. The difference is that some processing needs to be
1377  * immediate or the compressors will become confused on the peer.
1378  */
1379
1380 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
1381 {
1382         int slen    = CCP_LENGTH(dp);
1383         __u8 *opt = dp   + CCP_HDRLEN;
1384         int opt_len = slen - CCP_HDRLEN;
1385
1386         if (slen > len)
1387                 return;
1388
1389         switch (CCP_CODE(dp)) {
1390         case CCP_CONFREQ:
1391         case CCP_TERMREQ:
1392         case CCP_TERMACK:
1393 /*
1394  * CCP must be going down - disable compression
1395  */
1396                 if (ppp->flags & SC_CCP_UP) {
1397                         ppp->flags &= ~(SC_CCP_UP   |
1398                                         SC_COMP_RUN |
1399                                         SC_DECOMP_RUN);
1400                 }
1401                 break;
1402
1403         case CCP_CONFACK:
1404                 if ((ppp->flags & SC_CCP_OPEN) == 0)
1405                         break;
1406                 if (ppp->flags & SC_CCP_UP)
1407                         break;
1408                 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1409                         break;
1410                 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1411                         break;
1412 /*
1413  * we're agreeing to send compressed packets.
1414  */
1415                 if (!rcvd) {
1416                         if (ppp->sc_xc_state == NULL)
1417                                 break;
1418
1419                         if ((*ppp->sc_xcomp->comp_init)
1420                             (ppp->sc_xc_state,
1421                              opt,
1422                              opt_len,
1423                              ppp2dev (ppp)->base_addr,
1424                              0,
1425                              ppp->flags))
1426                                 ppp->flags |= SC_COMP_RUN;
1427                         break;
1428                 }
1429 /*
1430  * peer is agreeing to send compressed packets.
1431  */
1432                 if (ppp->sc_rc_state == NULL)
1433                         break;
1434
1435                 if ((*ppp->sc_rcomp->decomp_init)
1436                     (ppp->sc_rc_state,
1437                      opt,
1438                      opt_len,
1439                      ppp2dev (ppp)->base_addr,
1440                      0,
1441                      ppp->mru,
1442                      ppp->flags)) {
1443                         ppp->flags |= SC_DECOMP_RUN;
1444                         ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1445                 }
1446                 break;
1447 /*
1448  * The protocol sequence is complete at this end
1449  */
1450         case CCP_RESETACK:
1451                 if ((ppp->flags & SC_CCP_UP) == 0)
1452                         break;
1453
1454                 if (!rcvd) {
1455                         if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN))
1456                                 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1457                 } else {
1458                         if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1459                               (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1460                                 ppp->flags &= ~SC_DC_ERROR;
1461                         }
1462                 }
1463                 break;
1464         }
1465 }
1466
1467 static int
1468 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
1469 {
1470         ppp_proto_ccp (ppp, dp, len, 1);
1471         return rcv_proto_unknown (ppp, proto, dp, len);
1472 }
1473
1474 /*
1475  * Handle a LQR packet.
1476  */
1477
1478 static int
1479 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
1480 {
1481         return rcv_proto_unknown (ppp, proto, data, len);
1482 }
1483
1484 /* on entry, a received frame is in ppp->rbuf.bufr
1485    check it and dispose as appropriate */
1486
1487 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
1488 {
1489         __u16           proto = PPP_PROTOCOL (data);
1490         ppp_proto_type  *proto_ptr;
1491 /*
1492  * Ignore empty frames
1493  */
1494         if (count <= 4)
1495                 return;
1496 /*
1497  * Count the frame and print it
1498  */
1499         ++ppp->stats.ppp_ipackets;
1500         if (ppp->flags & SC_LOG_INPKT)
1501                 ppp_print_buffer ("receive frame", data, count);
1502 /*
1503  * Find the procedure to handle this protocol. The last one is marked
1504  * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1505  */
1506         proto_ptr = proto_list;
1507         while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1508                 ++proto_ptr;
1509 /*
1510  * Update the appropriate statistic counter.
1511  */
1512         if ((*proto_ptr->func) (ppp, proto,
1513                                 &data[PPP_HARD_HDR_LEN],
1514                                 count - PPP_HARD_HDR_LEN))
1515                 ppp->stats.ppp_ioctects += count;
1516         else
1517                 ++ppp->stats.ppp_discards;
1518 }
1519
1520 /* on entry, a received frame is in ppp->rbuf.bufr
1521    check it and dispose as appropriate */
1522
1523 static int
1524 ppp_doframe (struct ppp *ppp)
1525 {
1526         __u8    *data = buf_base (ppp->rbuf);
1527         int     count = ppp->rbuf->count;
1528         int     addr, ctrl, proto;
1529         int     new_count;
1530         __u8 *new_data;
1531 /*
1532  * If there is a pending error from the receiver then log it and discard
1533  * the damaged frame.
1534  */
1535         if (ppp->toss) {
1536                 if (ppp->flags & SC_DEBUG)
1537                         printk (KERN_DEBUG
1538                                 "ppp_toss: tossing frame, reason = %x\n",
1539                                 ppp->toss);
1540                 ppp->stats.ppp_ierrors++;
1541                 return 0;
1542         }
1543 /*
1544  * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1545  * follows each frame.
1546  */
1547         if (count == 0)
1548                 return 1;
1549 /*
1550  * Generate an error if the frame is too small.
1551  */
1552         if (count < PPP_HARD_HDR_LEN) {
1553                 if (ppp->flags & SC_DEBUG)
1554                         printk (KERN_WARNING
1555                                 "ppp: got runt ppp frame, %d chars\n", count);
1556                 slhc_toss (ppp->slcomp);
1557                 ppp->stats.ppp_ierrors++;
1558                 return 1;
1559         }
1560 /*
1561  * Verify the CRC of the frame and discard the CRC characters from the
1562  * end of the buffer.
1563  */
1564         if (ppp->rbuf->fcs != PPP_GOODFCS) {
1565                 if (ppp->flags & SC_DEBUG) {
1566                         printk (KERN_WARNING
1567                                 "ppp: frame with bad fcs, excess = %x, length = %d\n",
1568                                 ppp->rbuf->fcs ^ PPP_GOODFCS, count);
1569                         ppp_print_buffer("bad frame", data, count);
1570                 }
1571                 ppp->stats.ppp_ierrors++;
1572                 return 0;
1573         }
1574         count -= 2;             /* ignore the fcs characters */
1575 /*
1576  * Ignore the leading ADDRESS and CONTROL fields in the frame.
1577  */
1578         addr   = PPP_ALLSTATIONS;
1579         ctrl   = PPP_UI;
1580
1581         if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1582                 data  += 2;
1583                 count -= 2;
1584         }
1585 /*
1586  * Obtain the protocol from the frame
1587  */
1588         proto = (__u16) *data++;
1589         if ((proto & 1) == 0) {
1590                 proto = (proto << 8) | (__u16) *data++;
1591                 --count;
1592         }
1593 /*
1594  * Rewrite the header with the full information. This may encroach upon
1595  * the 'filler' area in the buffer header. This is the purpose for the
1596  * filler.
1597  */
1598         *(--data) = proto;
1599         *(--data) = proto >> 8;
1600         *(--data) = ctrl;
1601         *(--data) = addr;
1602         count    += 3;
1603 /*
1604  * Process the active decompressor.
1605  */
1606         if ((ppp->sc_rc_state != (void *) 0) &&
1607             (ppp->flags & SC_DECOMP_RUN)     &&
1608             ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1609                 if (proto == PPP_COMP) {
1610 /*
1611  * If the frame is compressed then decompress it.
1612  */
1613                         new_data = kmalloc (ppp->mru + 4, GFP_ATOMIC);
1614                         if (new_data == NULL) {
1615                                 if (ppp->flags & SC_DEBUG)
1616                                         printk (KERN_ERR
1617                                                 "ppp_doframe: no memory\n");
1618                                 slhc_toss (ppp->slcomp);
1619                                 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1620                                                           data,
1621                                                           count);
1622                                 return 1;
1623                         }
1624 /*
1625  * Decompress the frame
1626  */
1627                         new_count = bsd_decompress (ppp->sc_rc_state,
1628                                                     data,
1629                                                     count,
1630                                                     new_data,
1631                                                     ppp->mru + 4);
1632                         switch (new_count) {
1633                         default:
1634                                 ppp_doframe_lower (ppp, new_data, new_count);
1635                                 kfree (new_data);
1636                                 return 1;
1637
1638                         case DECOMP_OK:
1639                                 break;
1640
1641                         case DECOMP_ERROR:
1642                                 ppp->flags |= SC_DC_ERROR;
1643                                 break;
1644
1645                         case DECOMP_FATALERROR:
1646                                 ppp->flags |= SC_DC_FERROR;
1647                                 break;
1648                         }
1649 /*
1650  * Log the error condition and discard the frame.
1651  */
1652                         if (ppp->flags & SC_DEBUG)
1653                                 printk (KERN_ERR
1654                                         "ppp_proto_comp: "
1655                                         "decompress err %d\n", new_count);
1656                         kfree (new_data);
1657                         slhc_toss (ppp->slcomp);
1658                         return 1;
1659                 }
1660 /*
1661  * The frame is not special. Pass it through the compressor without
1662  * actually compressing the data
1663  */
1664                 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1665                                           data,
1666                                           count);
1667         }
1668 /*
1669  * Process the uncompressed frame.
1670  */
1671         ppp_doframe_lower (ppp, data, count);
1672         return 1;
1673 }
1674
1675 /*************************************************************
1676  * LINE DISCIPLINE SUPPORT
1677  *    The following functions form support user programs
1678  *    which read and write data on a TTY with the PPP line
1679  *    discipline.  Reading is done from a circular queue,
1680  *    filled by the lower TTY levels.
1681  *************************************************************/
1682
1683 /* read a PPP frame from the us_rbuff circular buffer,
1684    waiting if necessary
1685 */
1686
1687 static int
1688 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
1689               unsigned int nr)
1690 {
1691         struct ppp *ppp = tty2ppp (tty);
1692         __u8 c;
1693         int len, indx;
1694
1695 #define GETC(c)                                         \
1696 {                                                       \
1697         c = buf_base (ppp->ubuf) [ppp->ubuf->tail++];   \
1698         ppp->ubuf->tail &= ppp->ubuf->size;             \
1699 }
1700
1701 /*
1702  * Validate the pointers
1703  */
1704         if (!ppp)
1705                 return -EIO;
1706
1707         if (ppp->magic != PPP_MAGIC)
1708                 return -EIO;
1709
1710         CHECK_PPP (-ENXIO);
1711
1712         if (ppp->flags & SC_DEBUG)
1713                 printk (KERN_DEBUG
1714                         "ppp_tty_read: called buf=%p nr=%u\n",
1715                         buf, nr);
1716 /*
1717  * Acquire the read lock.
1718  */
1719         for (;;) {
1720                 ppp = tty2ppp (tty);
1721                 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse)
1722                         return 0;
1723
1724                 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1725                         if (ppp->flags & SC_DEBUG)
1726                                 printk (KERN_DEBUG
1727                                      "ppp_tty_read: sleeping(ubuf)\n");
1728
1729                         current->timeout = 0;
1730                         current->state   = TASK_INTERRUPTIBLE;
1731                         schedule ();
1732
1733                         if (current->signal & ~current->blocked)
1734                                 return -EINTR;
1735                         continue;
1736                 }
1737 /*
1738  * Before we attempt to write the frame to the user, ensure that the
1739  * user has access to the pages for the total buffer length.
1740  */
1741                 indx = verify_area (VERIFY_WRITE, buf, nr);
1742                 if (indx != 0)
1743                         return (indx);
1744 /*
1745  * Fetch the length of the buffer from the first two bytes.
1746  */
1747                 if (ppp->ubuf->head == ppp->ubuf->tail)
1748                         len = 0;
1749                 else {
1750                         GETC (c);
1751                         len = c << 8;
1752                         GETC (c);
1753                         len += c;
1754                 }
1755 /*
1756  * If there is no length then wait for the data to arrive.
1757  */
1758                 if (len == 0) {
1759                         /* no data */
1760                         clear_bit (0, &ppp->ubuf->locked);
1761                         if (file->f_flags & O_NONBLOCK) {
1762                                 if (ppp->flags & SC_DEBUG)
1763                                         printk (KERN_DEBUG
1764                                                 "ppp_tty_read: no data "
1765                                                 "(EAGAIN)\n");
1766                                 return -EAGAIN;
1767                         }
1768                         current->timeout = 0;
1769
1770                         if (ppp->flags & SC_DEBUG)
1771                                 printk (KERN_DEBUG
1772                                         "ppp_tty_read: sleeping(read_wait)\n");
1773
1774                         interruptible_sleep_on (&ppp->read_wait);
1775                         if (current->signal & ~current->blocked)
1776                                 return -EINTR;
1777                         continue;
1778                 }
1779 /*
1780  * Reset the time of the last read operation.
1781  */
1782                 if (ppp->flags & SC_DEBUG)
1783                         printk (KERN_DEBUG "ppp_tty_read: len = %d\n", len);
1784 /*
1785  * Ensure that the frame will fit within the caller's buffer. If not, then
1786  * discard the frame from the input buffer.
1787  */
1788                 if (len + 2 > nr) {
1789                         /* Can't copy it, update us_rbuff_head */
1790
1791                         if (ppp->flags & SC_DEBUG)
1792                                 printk (KERN_DEBUG
1793                                 "ppp: read of %u bytes too small for %d "
1794                                 "frame\n", nr, len + 2);
1795                         ppp->ubuf->tail += len;
1796                         ppp->ubuf->tail &= ppp->ubuf->size;
1797                         clear_bit (0, &ppp->ubuf->locked);
1798                         ppp->stats.ppp_ierrors++;
1799                         return -EOVERFLOW;
1800                 }
1801 /*
1802  * Before we attempt to write the frame to the user, ensure that the
1803  * page tables are proper.
1804  */
1805                 indx = verify_area (VERIFY_WRITE, buf, len + 2);
1806                 if (indx != 0) {
1807                         ppp->ubuf->tail += len;
1808                         ppp->ubuf->tail &= ppp->ubuf->size;
1809                         clear_bit (0, &ppp->ubuf->locked);
1810                         return (indx);
1811                 }
1812 /*
1813  * Fake the insertion of the ADDRESS and CONTROL information because these
1814  * were not saved in the buffer.
1815  */
1816                 put_user (PPP_ALLSTATIONS, buf++);
1817                 put_user (PPP_UI,          buf++);
1818
1819                 indx = len;
1820 /*
1821  * Copy the received data from the buffer to the caller's area.
1822  */
1823                 while (indx-- > 0) {
1824                         GETC (c);
1825                         put_user (c, buf);
1826                         ++buf;
1827                 }
1828
1829                 clear_bit (0, &ppp->ubuf->locked);
1830                 len += 2; /* Account for ADDRESS and CONTROL bytes */
1831                 if (ppp->flags & SC_DEBUG)
1832                         printk (KERN_DEBUG
1833                                 "ppp_tty_read: passing %d bytes up\n", len);
1834                 return len;
1835         }
1836 #undef GETC
1837 }
1838
1839 /* stuff a character into the transmit buffer, using PPP's way of escaping
1840    special characters.
1841    also, update fcs to take account of new character */
1842
1843 extern inline void
1844 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1845                 register __u8 chr)
1846 {
1847 /*
1848  * The buffer should not be full.
1849  */
1850         if (ppp->flags & SC_DEBUG) {
1851                 if ((buf->count < 0) || (buf->count > 3000))
1852                         printk (KERN_DEBUG "ppp_stuff_char: %x %d\n",
1853                                 (unsigned int) buf->count,
1854                                 (unsigned int) chr);
1855         }
1856 /*
1857  * Update the FCS and if the character needs to be escaped, do it.
1858  */
1859         buf->fcs = PPP_FCS (buf->fcs, chr);
1860         if (in_xmap (ppp, chr)) {
1861                 chr ^= PPP_TRANS;
1862                 ins_char (buf, PPP_ESCAPE);
1863         }
1864 /*
1865  * Add the character to the buffer.
1866  */
1867         ins_char (buf, chr);
1868 }
1869
1870 /*
1871  * Procedure to encode the data with the proper escaping and send the
1872  * data to the remote system.
1873  */
1874
1875 static void
1876 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
1877                     __u8 *data, int count, int non_ip)
1878 {
1879         __u16   write_fcs;
1880         int     address, control;
1881         int     proto;
1882 /*
1883  * Insert the leading FLAG character
1884  */
1885         buf->count = 0;
1886
1887         if (non_ip || flag_time == 0)
1888                 ins_char (buf, PPP_FLAG);
1889         else {
1890                 if (jiffies - ppp->last_xmit > flag_time)
1891                         ins_char (buf, PPP_FLAG);
1892         }
1893         ppp->last_xmit = jiffies;
1894         buf->fcs       = PPP_INITFCS;
1895 /*
1896  * Emit the address/control information if needed
1897  */
1898         address = PPP_ADDRESS  (data);
1899         control = PPP_CONTROL  (data);
1900         proto   = PPP_PROTOCOL (data);
1901
1902         if (address != PPP_ALLSTATIONS ||
1903             control != PPP_UI ||
1904             (ppp->flags & SC_COMP_AC) == 0) {
1905                 ppp_stuff_char (ppp, buf, address);
1906                 ppp_stuff_char (ppp, buf, control);
1907         }
1908 /*
1909  * Emit the protocol (compressed if possible)
1910  */
1911         if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
1912                 ppp_stuff_char (ppp, buf, proto >> 8);
1913
1914         ppp_stuff_char (ppp, buf, proto);
1915 /*
1916  * Insert the data
1917  */
1918         data  += 4;
1919         count -= 4;
1920
1921         while (count-- > 0)
1922                 ppp_stuff_char (ppp, buf, *data++);
1923 /*
1924  * Add the trailing CRC and the final flag character
1925  */
1926         write_fcs = buf->fcs ^ 0xFFFF;
1927         ppp_stuff_char (ppp, buf, write_fcs);
1928         ppp_stuff_char (ppp, buf, write_fcs >> 8);
1929 /*
1930  * Add the trailing flag character
1931  */
1932         ins_char (buf, PPP_FLAG);
1933 /*
1934  * Print the buffer
1935  */
1936         if (ppp->flags & SC_LOG_FLUSH)
1937                 ppp_print_buffer ("ppp flush", buf_base (buf),
1938                                   buf->count);
1939 /*
1940  * Send the block to the tty driver.
1941  */
1942         ppp->stats.ppp_obytes += buf->count;
1943         ppp_kick_tty (ppp, buf);
1944 }
1945
1946 /*
1947  * Send an frame to the remote with the proper bsd compression.
1948  *
1949  * Return 0 if frame was queued for transmission.
1950  *        1 if frame must be re-queued for later driver support.
1951  */
1952
1953 static int
1954 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
1955                     __u8 *data, int count)
1956 {
1957         int     proto;
1958         int     address, control;
1959         __u8 *new_data;
1960         int     new_count;
1961 /*
1962  * Print the buffer
1963  */
1964         if (ppp->flags & SC_LOG_OUTPKT)
1965                 ppp_print_buffer ("write frame", data, count);
1966 /*
1967  * Determine if the frame may be compressed. Attempt to compress the
1968  * frame if possible.
1969  */
1970         proto   = PPP_PROTOCOL (data);
1971         address = PPP_ADDRESS  (data);
1972         control = PPP_CONTROL  (data);
1973
1974         if (((ppp->flags & SC_COMP_RUN) != 0)   &&
1975             (ppp->sc_xc_state != (void *) 0)    &&
1976             (address == PPP_ALLSTATIONS)        &&
1977             (control == PPP_UI)                 &&
1978             (proto != PPP_LCP)                  &&
1979             (proto != PPP_CCP)) {
1980                 new_data = kmalloc (count, GFP_ATOMIC);
1981                 if (new_data == NULL) {
1982                         if (ppp->flags & SC_DEBUG)
1983                                 printk (KERN_ERR
1984                                         "ppp_dev_xmit_frame: no memory\n");
1985                         return 1;
1986                 }
1987
1988                 new_count = bsd_compress (ppp->sc_xc_state,
1989                                           data,
1990                                           new_data,
1991                                           count,
1992                                           count);
1993
1994                 if (new_count > 0) {
1995                         ++ppp->stats.ppp_opackets;
1996                         ppp->stats.ppp_ooctects += new_count;
1997
1998                         ppp_dev_xmit_lower (ppp, buf, new_data,
1999                                             new_count, 0);
2000                         kfree (new_data);
2001                         return 0;
2002                 }
2003 /*
2004  * The frame could not be compressed.
2005  */
2006                 kfree (new_data);
2007         }
2008 /*
2009  * The frame may not be compressed. Update the statistics before the
2010  * count field is destroyed. The frame will be transmitted.
2011  */
2012         ++ppp->stats.ppp_opackets;
2013         ppp->stats.ppp_ooctects += count;
2014 /*
2015  * Go to the escape encoding
2016  */
2017         ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2018         return 0;
2019 }
2020
2021 /*
2022  * Revise the tty frame for specific protocols.
2023  */
2024
2025 static int
2026 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
2027 {
2028         __u8 *p;
2029
2030         switch (PPP_PROTOCOL (data)) {
2031 /*
2032  * Update the LQR frame with the current MIB information. This saves having
2033  * the daemon read old MIB data from the driver.
2034  */
2035         case PPP_LQR:
2036                 len = 48;                       /* total size of this frame */
2037                 p   = (__u8 *) &data [40];      /* Point to last two items. */
2038                 p   = store_long (p, ppp->stats.ppp_opackets + 1);
2039                 p   = store_long (p, ppp->stats.ppp_ooctects + len);
2040                 break;
2041 /*
2042  * Outbound compression frames
2043  */
2044         case PPP_CCP:
2045                 ppp_proto_ccp (ppp,
2046                                data + PPP_HARD_HDR_LEN,
2047                                len  - PPP_HARD_HDR_LEN,
2048                                0);
2049                 break;
2050
2051         default:
2052                 break;
2053         }
2054
2055         return len;
2056 }
2057
2058 /*
2059  * write a frame with NR chars from BUF to TTY
2060  * we have to put the FCS field on ourselves
2061  */
2062
2063 static int
2064 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
2065                unsigned int count)
2066 {
2067         struct ppp *ppp = tty2ppp (tty);
2068         __u8 *new_data;
2069         int status;
2070 /*
2071  * Verify the pointers.
2072  */
2073         if (!ppp)
2074                 return -EIO;
2075
2076         if (ppp->magic != PPP_MAGIC)
2077                 return -EIO;
2078
2079         CHECK_PPP (-ENXIO);
2080 /*
2081  * Ensure that the caller does not wish to send too much.
2082  */
2083         if (count > PPP_MTU) {
2084                 if (ppp->flags & SC_DEBUG)
2085                         printk (KERN_WARNING
2086                                 "ppp_tty_write: truncating user packet "
2087                                 "from %u to mtu %d\n", count, PPP_MTU);
2088                 count = PPP_MTU;
2089         }
2090 /*
2091  * Allocate a buffer for the data and fetch it from the user space.
2092  */
2093         new_data = kmalloc (count, GFP_KERNEL);
2094         if (new_data == NULL) {
2095                 if (ppp->flags & SC_DEBUG)
2096                         printk (KERN_ERR
2097                                 "ppp_tty_write: no memory\n");
2098                 return 0;
2099         }
2100 /*
2101  * lock this PPP unit so we will be the only writer;
2102  * sleep if necessary
2103  */
2104         while (lock_buffer (ppp->tbuf) != 0) {
2105                 current->timeout = 0;
2106                 if (ppp->flags & SC_DEBUG)
2107                         printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2108                 interruptible_sleep_on (&ppp->write_wait);
2109
2110                 ppp = tty2ppp (tty);
2111                 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse) {
2112                         kfree (new_data);
2113                         return 0;
2114                 }
2115
2116                 if (current->signal & ~current->blocked) {
2117                         kfree (new_data);
2118                         return -EINTR;
2119                 }
2120         }
2121 /*
2122  * Ensure that the caller's buffer is valid.
2123  */
2124         status = verify_area (VERIFY_READ, data, count);
2125         if (status != 0) {
2126                 kfree (new_data);
2127                 ppp->tbuf->locked = 0;
2128                 return status;
2129         }
2130
2131         memcpy_fromfs (new_data, data, count);
2132 /*
2133  * Change the LQR frame
2134  */
2135         count = send_revise_frame (ppp, new_data, count);
2136 /*
2137  * Send the data
2138  */
2139         ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2140         kfree (new_data);
2141         return (int) count;
2142 }
2143
2144 /*
2145  * Process the BSD compression IOCTL event for the tty device.
2146  */
2147
2148 static int
2149 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2150 {
2151         struct compressor *cp;
2152         struct ppp_option_data data;
2153         int error;
2154         int nb;
2155         __u8 *ptr;
2156         __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2157 /*
2158  * Fetch the compression parameters
2159  */
2160         error = verify_area (VERIFY_READ, odp, sizeof (data));
2161         if (error == 0) {
2162                 memcpy_fromfs (&data, odp, sizeof (data));
2163                 nb  = data.length;
2164                 ptr = data.ptr;
2165                 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2166                         nb = CCP_MAX_OPTION_LENGTH;
2167         
2168                 error = verify_area (VERIFY_READ, ptr, nb);
2169         }
2170
2171         if (error != 0)
2172                 return error;
2173
2174         memcpy_fromfs (ccp_option, ptr, nb);
2175
2176         if (ccp_option[1] < 2)  /* preliminary check on the length byte */
2177                 return (-EINVAL);
2178
2179         cp = find_compressor ((int) (unsigned int) (__u8) ccp_option[0]);
2180         if (cp != (struct compressor *) 0) {
2181                 /*
2182                  * Found a handler for the protocol - try to allocate
2183                  * a compressor or decompressor.
2184                  */
2185                 error = 0;
2186                 if (data.transmit) {
2187                         if (ppp->sc_xc_state != NULL)
2188                                 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2189
2190                         ppp->sc_xcomp    = cp;
2191                         ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2192
2193                         if (ppp->sc_xc_state == NULL) {
2194                                 if (ppp->flags & SC_DEBUG)
2195                                         printk("ppp%ld: comp_alloc failed\n",
2196                                                ppp2dev (ppp)->base_addr);
2197                                 error = -ENOBUFS;
2198                         }
2199                         ppp->flags &= ~SC_COMP_RUN;
2200                 } else {
2201                         if (ppp->sc_rc_state != NULL)
2202                                 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2203                         ppp->sc_rcomp    = cp;
2204                         ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2205                         if (ppp->sc_rc_state == NULL) {
2206                                 if (ppp->flags & SC_DEBUG)
2207                                         printk("ppp%ld: decomp_alloc failed\n",
2208                                                ppp2dev (ppp)->base_addr);
2209                                 error = ENOBUFS;
2210                         }
2211                         ppp->flags &= ~SC_DECOMP_RUN;
2212                 }
2213                 return (error);
2214         }
2215
2216         if (ppp->flags & SC_DEBUG)
2217                 printk(KERN_DEBUG "ppp%ld: no compressor for [%x %x %x], %x\n",
2218                        ppp2dev (ppp)->base_addr, ccp_option[0], ccp_option[1],
2219                        ccp_option[2], nb);
2220         return (-EINVAL);       /* no handler found */
2221 }
2222
2223 /*
2224  * Process the IOCTL event for the tty device.
2225  */
2226
2227 static int
2228 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
2229                unsigned int param2, unsigned long param3)
2230 {
2231         struct ppp *ppp = tty2ppp (tty);
2232         register int temp_i = 0;
2233         int error = 0;
2234 /*
2235  * Verify the status of the PPP device.
2236  */
2237         if (!ppp)
2238                 return -EBADF;
2239
2240         if (ppp->magic != PPP_MAGIC)
2241                 return -EBADF;
2242
2243         CHECK_PPP (-ENXIO);
2244 /*
2245  * The user must have an euid of root to do these requests.
2246  */
2247         if (!suser ())
2248                 return -EPERM;
2249 /*
2250  * Set the MRU value
2251  */
2252         switch (param2) {
2253         case PPPIOCSMRU:
2254                 error = verify_area (VERIFY_READ, (void *) param3,
2255                                      sizeof (temp_i));
2256                 if (error == 0) {
2257                         temp_i = get_user ((int *) param3);
2258                         if (ppp->flags & SC_DEBUG)
2259                                 printk (KERN_INFO
2260                                  "ppp_tty_ioctl: set mru to %x\n", temp_i);
2261
2262                         if (ppp->mru != temp_i)
2263                                 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2264                 }
2265                 break;
2266 /*
2267  * Fetch the flags
2268  */
2269         case PPPIOCGFLAGS:
2270                 error = verify_area (VERIFY_WRITE, (void *) param3,
2271                                      sizeof (temp_i));
2272                 if (error == 0) {
2273                         temp_i = (ppp->flags & SC_MASK);
2274 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2275                         temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2276                                   SC_RCV_ODDP | SC_RCV_EVNP;
2277 #endif
2278                         put_user (temp_i, (int *) param3);
2279                         if (ppp->flags & SC_DEBUG)
2280                                 printk (KERN_DEBUG
2281                                 "ppp_tty_ioctl: get flags: addr %lx flags "
2282                                 "%x\n", param3, temp_i);
2283                 }
2284                 break;
2285 /*
2286  * Set the flags for the various options
2287  */
2288         case PPPIOCSFLAGS:
2289                 error = verify_area (VERIFY_READ, (void *) param3,
2290                                      sizeof (temp_i));
2291                 if (error == 0) {
2292                         temp_i  = get_user ((int *) param3) & SC_MASK;
2293                         temp_i |= (ppp->flags & ~SC_MASK);
2294
2295                         if ((ppp->flags & SC_CCP_OPEN) &&
2296                             (temp_i & SC_CCP_OPEN) == 0)
2297                                 ppp_ccp_closed (ppp);
2298
2299                         if ((ppp->flags | temp_i) & SC_DEBUG)
2300                                 printk (KERN_INFO
2301                                "ppp_tty_ioctl: set flags to %x\n", temp_i);
2302                         ppp->flags = temp_i;
2303                 }
2304                 break;
2305 /*
2306  * Set the compression mode
2307  */
2308         case PPPIOCSCOMPRESS:
2309                 error = ppp_set_compression (ppp,
2310                                             (struct ppp_option_data *) param3);
2311                 break;
2312 /*
2313  * Retrieve the transmit async map
2314  */
2315         case PPPIOCGASYNCMAP:
2316                 error = verify_area (VERIFY_WRITE, (void *) param3,
2317                                      sizeof (temp_i));
2318                 if (error == 0) {
2319                         put_user (ppp->xmit_async_map[0], (int *) param3);
2320                         if (ppp->flags & SC_DEBUG)
2321                                 printk (KERN_INFO
2322                                      "ppp_tty_ioctl: get asyncmap: addr "
2323                                      "%lx asyncmap %x\n",
2324                                      param3,
2325                                      ppp->xmit_async_map[0]);
2326                 }
2327                 break;
2328 /*
2329  * Set the transmit async map
2330  */
2331         case PPPIOCSASYNCMAP:
2332                 error = verify_area (VERIFY_READ, (void *) param3,
2333                                      sizeof (temp_i));
2334                 if (error == 0) {
2335                         ppp->xmit_async_map[0] = get_user ((int *) param3);
2336                         if (ppp->flags & SC_DEBUG)
2337                                 printk (KERN_INFO
2338                                      "ppp_tty_ioctl: set xmit asyncmap %x\n",
2339                                      ppp->xmit_async_map[0]);
2340                 }
2341                 break;
2342 /*
2343  * Set the receive async map
2344  */
2345         case PPPIOCSRASYNCMAP:
2346                 error = verify_area (VERIFY_READ, (void *) param3,
2347                                      sizeof (temp_i));
2348                 if (error == 0) {
2349                         ppp->recv_async_map = get_user ((int *) param3);
2350                         if (ppp->flags & SC_DEBUG)
2351                                 printk (KERN_INFO
2352                                      "ppp_tty_ioctl: set rcv asyncmap %x\n",
2353                                      ppp->recv_async_map);
2354                 }
2355                 break;
2356 /*
2357  * Obtain the unit number for this device.
2358  */
2359         case PPPIOCGUNIT:
2360                 error = verify_area (VERIFY_WRITE, (void *) param3,
2361                                      sizeof (temp_i));
2362                 if (error == 0) {
2363                         put_user (ppp2dev (ppp)->base_addr, (int *) param3);
2364                         if (ppp->flags & SC_DEBUG)
2365                                 printk (KERN_INFO
2366                                         "ppp_tty_ioctl: get unit: %ld",
2367                                         ppp2dev (ppp)->base_addr);
2368                 }
2369                 break;
2370 /*
2371  * Set the debug level
2372  */
2373         case PPPIOCSDEBUG:
2374                 error = verify_area (VERIFY_READ, (void *) param3,
2375                                      sizeof (temp_i));
2376                 if (error == 0) {
2377                         temp_i  = (get_user ((int *) param3) & 0x1F) << 16;
2378                         temp_i |= (ppp->flags & ~0x1F0000);
2379
2380                         if ((ppp->flags | temp_i) & SC_DEBUG)
2381                                 printk (KERN_INFO
2382                                "ppp_tty_ioctl: set flags to %x\n", temp_i);
2383                         ppp->flags = temp_i;
2384                 }
2385                 break;
2386 /*
2387  * Get the debug level
2388  */
2389         case PPPIOCGDEBUG:
2390                 error = verify_area (VERIFY_WRITE, (void *) param3,
2391                                      sizeof (temp_i));
2392                 if (error == 0) {
2393                         temp_i = (ppp->flags >> 16) & 0x1F;
2394                         put_user (temp_i, (int *) param3);
2395
2396                         if (ppp->flags & SC_DEBUG)
2397                                 printk (KERN_INFO
2398                                         "ppp_tty_ioctl: get debug level %d\n",
2399                                         temp_i);
2400                 }
2401                 break;
2402 /*
2403  * Get the times since the last send/receive frame operation
2404  */
2405         case PPPIOCGIDLE:
2406                 error = verify_area (VERIFY_WRITE, (void *) param3,
2407                                      sizeof (struct ppp_idle));
2408                 if (error == 0) {
2409                         struct ppp_idle cur_ddinfo;
2410                         __u32 cur_jiffies = jiffies;
2411
2412                         /* change absolute times to relative times. */
2413                         cur_ddinfo.xmit_idle = (cur_jiffies - ppp->ddinfo.xmit_idle) / HZ;
2414                         cur_ddinfo.recv_idle = (cur_jiffies - ppp->ddinfo.recv_idle) / HZ;
2415                         memcpy_tofs ((void *) param3, &cur_ddinfo,
2416                                      sizeof (cur_ddinfo));
2417                         if (ppp->flags & SC_DEBUG)
2418                                 printk (KERN_INFO
2419                                 "ppp_tty_ioctl: read demand dial info\n");
2420                 }
2421                 break;
2422 /*
2423  * Retrieve the extended async map
2424  */
2425         case PPPIOCGXASYNCMAP:
2426                 error = verify_area (VERIFY_WRITE,
2427                                      (void *) param3,
2428                                      sizeof (ppp->xmit_async_map));
2429                 if (error == 0) {
2430                         memcpy_tofs ((void *) param3,
2431                                      ppp->xmit_async_map,
2432                                      sizeof (ppp->xmit_async_map));
2433
2434                         if (ppp->flags & SC_DEBUG)
2435                                 printk (KERN_INFO
2436                                 "ppp_tty_ioctl: get xasyncmap: addr %lx\n",
2437                                 param3);
2438                 }
2439                 break;
2440 /*
2441  * Set the async extended map
2442  */
2443         case PPPIOCSXASYNCMAP:
2444                 error = verify_area (VERIFY_READ, (void *) param3,
2445                                      sizeof (ppp->xmit_async_map));
2446                 if (error == 0) {
2447                         __u32 temp_tbl[8];
2448
2449                         memcpy_fromfs (temp_tbl, (void *) param3,
2450                                        sizeof (ppp->xmit_async_map));
2451                         temp_tbl[1]  =  0x00000000;
2452                         temp_tbl[2] &= ~0x40000000;
2453                         temp_tbl[3] |=  0x60000000;
2454
2455                         if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2456                             (temp_tbl[4] & temp_tbl[5]) != 0 ||
2457                             (temp_tbl[6] & temp_tbl[7]) != 0)
2458                                 error = -EINVAL;
2459                         else {
2460                                 memcpy (ppp->xmit_async_map, temp_tbl,
2461                                         sizeof (ppp->xmit_async_map));
2462
2463                                 if (ppp->flags & SC_DEBUG)
2464                                         printk (KERN_INFO
2465                                         "ppp_tty_ioctl: set xasyncmap\n");
2466                         }
2467                 }
2468                 break;
2469 /*
2470  * Set the maximum VJ header compression slot number.
2471  */
2472         case PPPIOCSMAXCID:
2473                 error = verify_area (VERIFY_READ, (void *) param3,
2474                                      sizeof (temp_i));
2475                 if (error == 0) {
2476                         temp_i = get_user ((int *) param3) + 1;
2477                         if (ppp->flags & SC_DEBUG)
2478                                 printk (KERN_INFO
2479                                      "ppp_tty_ioctl: set maxcid to %d\n",
2480                                      temp_i);
2481                         if (ppp->slcomp != NULL)
2482                                 slhc_free (ppp->slcomp);
2483                         ppp->slcomp = slhc_init (16, temp_i);
2484
2485                         if (ppp->slcomp == NULL) {
2486                                 if (ppp->flags & SC_DEBUG)
2487                                         printk (KERN_ERR
2488                                         "ppp: no space for compression buffers!\n");
2489                                 ppp_release (ppp);
2490                                 error = -ENOMEM;
2491                         }
2492                 }
2493                 break;
2494
2495     case PPPIOCXFERUNIT:
2496                 ppp_tty_close_local (tty, current->pid);
2497                 break;
2498
2499     case PPPIOCGNPMODE:
2500     case PPPIOCSNPMODE:
2501                 error = verify_area (VERIFY_READ, (void *) param3,
2502                                      sizeof (struct npioctl));
2503                 if (error == 0) {
2504                         struct npioctl npi;
2505                         memcpy_fromfs (&npi,
2506                                        (void *) param3,
2507                                        sizeof (npi));
2508
2509                         switch (npi.protocol) {
2510                         case PPP_IP:
2511                                 npi.protocol = NP_IP;
2512                                 break;
2513                         default:
2514                                 error = -EINVAL;
2515                         }
2516
2517                         if (error != 0)
2518                                 break;
2519
2520                         if (param2 == PPPIOCGNPMODE) {
2521                                 npi.mode = ppp->sc_npmode[npi.protocol];
2522                                 error = verify_area (VERIFY_WRITE,
2523                                                      (void *) param3,
2524                                                      sizeof (npi));
2525                                 if (error != 0)
2526                                         break;
2527
2528                                 memcpy_tofs ((void *) param3,
2529                                              &npi,
2530                                              sizeof (npi));
2531                                 break;
2532                         }
2533
2534                         if (npi.mode != ppp->sc_npmode[npi.protocol]) {
2535                                 ppp->sc_npmode[npi.protocol] = npi.mode;
2536                                 if (npi.mode != NPMODE_QUEUE) {
2537                                         /* ppp_requeue(ppp); maybe needed */
2538                                         ppp_tty_wakeup (ppp2tty(ppp));
2539                                 }
2540                         }
2541                 }
2542                 break;
2543 /*
2544  * Allow users to read, but not set, the serial port parameters
2545  */
2546         case TCGETS:
2547         case TCGETA:
2548                 error = n_tty_ioctl (tty, file, param2, param3);
2549                 break;
2550
2551         case FIONREAD:
2552                 error = verify_area (VERIFY_WRITE,
2553                                      (void *) param3,
2554                                      sizeof (int));
2555                 if (error == 0) {
2556                         int count = ppp->ubuf->tail - ppp->ubuf->head;
2557                         if (count < 0)
2558                                 count += (ppp->ubuf->size + 1);
2559
2560                         put_user (count, (int *) param3);
2561                 }
2562                 break;
2563 /*
2564  *  All other ioctl() events will come here.
2565  */
2566         default:
2567                 if (ppp->flags & SC_DEBUG)
2568                         printk (KERN_ERR
2569                                 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2570                                 param2,
2571                                 param3);
2572
2573                 error = -ENOIOCTLCMD;
2574                 break;
2575         }
2576         return error;
2577 }
2578
2579 /*
2580  * TTY callback.
2581  *
2582  * Process the select() statement for the PPP device.
2583  */
2584
2585 static int
2586 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2587                 struct file *filp, int sel_type, select_table * wait)
2588 {
2589         struct ppp *ppp = tty2ppp (tty);
2590         int result = 1;
2591 /*
2592  * Verify the status of the PPP device.
2593  */
2594         if (!ppp)
2595                 return -EBADF;
2596
2597         if (ppp->magic != PPP_MAGIC)
2598                 return -EBADF;
2599
2600         CHECK_PPP (0);
2601 /*
2602  * Branch on the type of select mode. A read request must lock the user
2603  * buffer area.
2604  */
2605         switch (sel_type) {
2606         case SEL_IN:
2607                 if (set_bit (0, &ppp->ubuf->locked) == 0) {
2608                         /* Test for the presence of data in the queue */
2609                         if (ppp->ubuf->head != ppp->ubuf->tail) {
2610                                 clear_bit (0, &ppp->ubuf->locked);
2611                                 break;
2612                         }
2613                         clear_bit (0, &ppp->ubuf->locked);
2614                 }               /* fall through */
2615 /*
2616  * Exceptions or read errors.
2617  */
2618         case SEL_EX:
2619                 /* Is this a pty link and the remote disconnected? */
2620                 if (tty->flags & (1 << TTY_OTHER_CLOSED))
2621                         break;
2622
2623                 /* Is this a local link and the modem disconnected? */
2624                 if (tty_hung_up_p (filp))
2625                         break;
2626
2627                 select_wait (&ppp->read_wait, wait);
2628                 result = 0;
2629                 break;
2630 /*
2631  * Write mode. A write is allowed if there is no current transmission.
2632  */
2633         case SEL_OUT:
2634                 if (ppp->tbuf->locked != 0) {
2635                         select_wait (&ppp->write_wait, wait);
2636                         result = 0;
2637                 }
2638                 break;
2639         }
2640         return result;
2641 }
2642
2643 /*************************************************************
2644  * NETWORK OUTPUT
2645  *    This routine accepts requests from the network layer
2646  *    and attempts to deliver the packets.
2647  *    It also includes various routines we are compelled to
2648  *    have to make the network layer work (arp, etc...).
2649  *************************************************************/
2650
2651 /*
2652  * Callback from the network layer when the device goes up.
2653  */
2654
2655 static int
2656 ppp_dev_open (struct device *dev)
2657 {
2658         struct ppp *ppp = dev2ppp (dev);
2659
2660         /* reset POINTOPOINT every time, since dev_close zaps it! */
2661         dev->flags |= IFF_POINTOPOINT;
2662
2663         if (ppp2tty (ppp) == NULL) {
2664                 if (ppp->flags & SC_DEBUG)
2665                         printk (KERN_ERR
2666                         "ppp: %s not connected to a TTY! can't go open!\n",
2667                         dev->name);
2668                 return -ENXIO;
2669         }
2670
2671         if (ppp->flags & SC_DEBUG)
2672                 printk (KERN_INFO
2673                         "ppp: channel %s going up for IP packets!\n",
2674                         dev->name);
2675
2676         CHECK_PPP (-ENXIO);
2677         return 0;
2678 }
2679
2680 /*
2681  * Callback from the network layer when the ppp device goes down.
2682  */
2683
2684 static int
2685 ppp_dev_close (struct device *dev)
2686 {
2687         struct ppp *ppp = dev2ppp (dev);
2688
2689         if (ppp2tty (ppp) == NULL) {
2690                 if (ppp->flags & SC_DEBUG)
2691                         printk (KERN_ERR
2692                         "ppp: %s not connected to a TTY! can't go down!\n",
2693                         dev->name);
2694                 return -ENXIO;
2695         }
2696 /*
2697  * We don't do anything about the device going down. It is not important
2698  * for us.
2699  */
2700         if (ppp->flags & SC_DEBUG)
2701                 printk (KERN_INFO
2702                         "ppp: channel %s going down for IP packets!\n",
2703                         dev->name);
2704         CHECK_PPP (-ENXIO);
2705         return 0;
2706 }
2707
2708 /*
2709  * IOCTL operation to read the version of the driver.
2710  */
2711
2712 static int
2713 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2714 {
2715         int error;
2716         int len;
2717         char *result;
2718 /*
2719  * Must have write access to the buffer.
2720  */
2721         result = (char *) ifr->ifr_ifru.ifru_data;
2722         len    = strlen (szVersion) + 1;
2723         error  = verify_area (VERIFY_WRITE, result, len);
2724 /*
2725  * Move the version data
2726  */
2727         if (error == 0)
2728                 memcpy_tofs (result, szVersion, len);
2729
2730         return error;
2731 }
2732
2733 /*
2734  * IOCTL to read the statistics for the pppstats program.
2735  */
2736
2737 static int
2738 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2739 {
2740         struct ppp_stats *result, temp;
2741         int    error;
2742 /*
2743  * Must have write access to the buffer.
2744  */
2745         result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2746         error = verify_area (VERIFY_WRITE,
2747                              result,
2748                              sizeof (temp));
2749 /*
2750  * Supply the information for the caller. First move the version data
2751  * then move the ppp stats; and finally the vj stats.
2752  */
2753         memset (&temp, 0, sizeof(temp));
2754         if (error == 0 && dev->flags & IFF_UP) {
2755                 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2756                 if (ppp->slcomp != NULL) {
2757                         temp.vj.vjs_packets    = ppp->slcomp->sls_o_compressed+
2758                                                  ppp->slcomp->sls_o_uncompressed;
2759                         temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2760                         temp.vj.vjs_searches   = ppp->slcomp->sls_o_searches;
2761                         temp.vj.vjs_misses     = ppp->slcomp->sls_o_misses;
2762                         temp.vj.vjs_errorin    = ppp->slcomp->sls_i_error;
2763                         temp.vj.vjs_tossed     = ppp->slcomp->sls_i_tossed;
2764                         temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2765                         temp.vj.vjs_compressedin   = ppp->slcomp->sls_i_compressed;
2766                 }
2767         }
2768
2769         if (error == 0)
2770                 memcpy_tofs (result, &temp, sizeof (temp));
2771         return error;
2772 }
2773
2774 /*
2775  * IOCTL to read the compression statistics for the pppstats program.
2776  */
2777
2778 static int
2779 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2780 {
2781         struct ppp_comp_stats *result, temp;
2782         int    error;
2783 /*
2784  * Must have write access to the buffer.
2785  */
2786         result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2787         error = verify_area (VERIFY_WRITE,
2788                              result,
2789                              sizeof (temp));
2790 /*
2791  * Supply the information for the caller.
2792  */
2793         memset (&temp, 0, sizeof(temp));
2794         if (error == 0 && dev->flags & IFF_UP) {
2795                 if (ppp->sc_xc_state != NULL)
2796                         (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2797                                                      &temp.c);
2798
2799                 if (ppp->sc_rc_state != NULL)
2800                         (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2801                                                        &temp.d);
2802         }
2803 /*
2804  * Move the data to the caller's buffer
2805  */
2806         if (error == 0)
2807                 memcpy_tofs (result, &temp, sizeof (temp));
2808         return error;
2809 }
2810
2811 /*
2812  * Callback from the network layer to process the sockioctl functions.
2813  */
2814
2815 static int
2816 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2817 {
2818         struct ppp *ppp = dev2ppp (dev);
2819         int error;
2820 /*
2821  * Process the requests
2822  */
2823         switch (cmd) {
2824         case SIOCGPPPSTATS:
2825                 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2826                 break;
2827
2828         case SIOCGPPPCSTATS:
2829                 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2830                 break;
2831
2832         case SIOCGPPPVER:
2833                 error = ppp_dev_ioctl_version (ppp, ifr);
2834                 break;
2835
2836         default:
2837                 error = -EINVAL;
2838                 break;
2839         }
2840         return error;
2841 }
2842
2843 /*
2844  * Send an IP frame to the remote with vj header compression.
2845  *
2846  * Return 0 if frame was queued for transmission.
2847  *        1 if frame must be re-queued for later driver support.
2848  */
2849
2850 static int
2851 ppp_dev_xmit_ip (struct device *dev, struct ppp *ppp, __u8 *data)
2852 {
2853         int               proto = PPP_IP;
2854         int               len;
2855         struct ppp_hdr    *hdr;
2856         struct tty_struct *tty = ppp2tty (ppp);
2857 /*
2858  * Obtain the length from the IP header.
2859  */
2860         len = ((struct iphdr *)data) -> tot_len;
2861         len = ntohs (len);
2862 /*
2863  * Validate the tty interface
2864  */
2865         if (tty == NULL) {
2866                 if (ppp->flags & SC_DEBUG)
2867                         printk (KERN_ERR
2868                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
2869                                 dev->name);
2870                 return 0;
2871         }
2872 /*
2873  * Ensure that the PPP device is still up
2874  */
2875         if (!(dev->flags & IFF_UP)) {
2876                 if (ppp->flags & SC_DEBUG)
2877                         printk (KERN_WARNING
2878                                 "ppp_dev_xmit: packet sent on interface %s,"
2879                                 " which is down for IP\n",
2880                                 dev->name);
2881                 return 0;
2882         }
2883 /*
2884  * Branch on the type of processing for the IP frame.
2885  */
2886         switch (ppp->sc_npmode[NP_IP]) {
2887         case NPMODE_PASS:
2888                 break;
2889
2890         case NPMODE_ERROR:
2891                 if (ppp->flags & SC_DEBUG)
2892                         printk (KERN_WARNING
2893                                 "ppp_dev_xmit: npmode = NPMODE_ERROR on %s\n",
2894                                 dev->name);
2895                 return 0;
2896
2897         case NPMODE_DROP:
2898                 if (ppp->flags & SC_DEBUG)
2899                         printk (KERN_WARNING
2900                                 "ppp_dev_xmit: npmode = NPMODE_DROP on %s\n",
2901                                 dev->name);
2902                 return 0;
2903
2904         case NPMODE_QUEUE:
2905                 break;
2906
2907         default:
2908                 if (ppp->flags & SC_DEBUG)
2909                         printk (KERN_WARNING
2910                                 "ppp_dev_xmit: unknown npmode %d on %s\n",
2911                                 ppp->sc_npmode[NP_IP],
2912                                 dev->name);
2913                 return 0;
2914         }
2915 /*
2916  * Detect a change in the transfer size
2917  */
2918         if (ppp->mtu != ppp2dev (ppp)->mtu) {
2919                 ppp_changedmtu (ppp,
2920                                 ppp2dev (ppp)->mtu,
2921                                 ppp->mru);
2922         }
2923 /*
2924  * Acquire the lock on the transmission buffer. If the buffer was busy then
2925  * mark the device as busy.
2926  */
2927         if (lock_buffer (ppp->wbuf) != 0) {
2928                 dev->tbusy = 1;
2929                 return 1;
2930         }
2931 /*
2932  * Print the frame being sent
2933  */
2934         if (ppp->flags & SC_LOG_OUTPKT)
2935                 ppp_print_buffer ("ppp outpkt", data, len);
2936 /*
2937  * At this point, the buffer will be transmitted. There is no other exit.
2938  *
2939  * Try to compress the header.
2940  */
2941         if (ppp->flags & SC_COMP_TCP) {
2942                 len = slhc_compress (ppp->slcomp, data, len,
2943                                      buf_base (ppp->cbuf) + PPP_HARD_HDR_LEN,
2944                                      &data,
2945                                      (ppp->flags & SC_NO_TCP_CCID) == 0);
2946
2947                 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2948                         proto    = PPP_VJC_COMP;
2949                         data[0] ^= SL_TYPE_COMPRESSED_TCP;
2950                 } else {
2951                         if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2952                                 proto = PPP_VJC_UNCOMP;
2953                         data[0] = (data[0] & 0x0f) | 0x40;
2954                 }
2955         }
2956 /*
2957  * Send the frame
2958  */
2959         len  += PPP_HARD_HDR_LEN;
2960         hdr   = &((struct ppp_hdr *) data)[-1];
2961
2962         hdr->address     = PPP_ALLSTATIONS;
2963         hdr->control     = PPP_UI;
2964         hdr->protocol[0] = 0;
2965         hdr->protocol[1] = proto;
2966
2967         return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
2968 }
2969
2970 /*
2971  * Send an IPX (or any other non-IP) frame to the remote.
2972  *
2973  * Return 0 if frame was queued for transmission.
2974  *        1 if frame must be re-queued for later driver support.
2975  */
2976 static int
2977 ppp_dev_xmit_ipx (struct device *dev, struct ppp *ppp,
2978                   __u8 *data, int len, int proto)
2979 {
2980         struct tty_struct *tty = ppp2tty (ppp);
2981         struct ppp_hdr    *hdr;
2982 /*
2983  * Validate the tty interface
2984  */
2985         if (tty == NULL) {
2986                 if (ppp->flags & SC_DEBUG)
2987                         printk (KERN_ERR
2988                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
2989                                 dev->name);
2990                 return 0;
2991         }
2992 /*
2993  * Ensure that the PPP device is still up
2994  */
2995         if (!(dev->flags & IFF_UP)) {
2996                 if (ppp->flags & SC_DEBUG)
2997                         printk (KERN_WARNING
2998                                 "ppp_dev_xmit: packet sent on interface %s,"
2999                                 " which is down\n",
3000                                 dev->name);
3001                 return 0;
3002         }
3003 /*
3004  * Detect a change in the transfer size
3005  */
3006         if (ppp->mtu != ppp2dev (ppp)->mtu) {
3007                 ppp_changedmtu (ppp,
3008                                 ppp2dev (ppp)->mtu,
3009                                 ppp->mru);
3010         }
3011 /*
3012  * Acquire the lock on the transmission buffer. If the buffer was busy then
3013  * mark the device as busy.
3014  */
3015         if (lock_buffer (ppp->wbuf) != 0) {
3016                 dev->tbusy = 1;
3017                 return 1;
3018         }
3019 /*
3020  * Print the frame being sent
3021  */
3022         if (ppp->flags & SC_LOG_OUTPKT)
3023                 ppp_print_buffer ("ppp outpkt", data, len);
3024 /*
3025  * Send the frame
3026  */
3027         len  += PPP_HARD_HDR_LEN;
3028         hdr   = &((struct ppp_hdr *) data)[-1];
3029
3030         hdr->address     = PPP_ALLSTATIONS;
3031         hdr->control     = PPP_UI;
3032         hdr->protocol[0] = proto >> 8;
3033         hdr->protocol[1] = proto;
3034
3035         return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
3036 }
3037
3038 /*
3039  * Send a frame to the remote.
3040  */
3041
3042 static int
3043 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3044 {
3045         int answer, len;
3046         __u8              *data;
3047         struct ppp        *ppp = dev2ppp (dev);
3048         struct tty_struct *tty = ppp2tty (ppp);
3049 /*
3050  * just a little sanity check.
3051  */
3052         if (skb == NULL) {
3053                 if (ppp->flags & SC_DEBUG)
3054                         printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3055                 return 0;
3056         }
3057 /*
3058  * Avoid timing problem should tty hangup while data is queued to be sent
3059  */
3060         if (!ppp->inuse) {
3061                 dev_kfree_skb (skb, FREE_WRITE);
3062                 dev_close (dev);
3063                 return 0;
3064         }
3065 /*
3066  * Validate the tty interface
3067  */
3068         if (tty == NULL) {
3069                 if (ppp->flags & SC_DEBUG)
3070                         printk (KERN_ERR
3071                                 "ppp_dev_xmit: %s not connected to a TTY!\n",
3072                                 dev->name);
3073                 dev_kfree_skb (skb, FREE_WRITE);
3074                 return 0;
3075         }
3076 /*
3077  * Fetch the pointer to the data
3078  */
3079         len   = skb->len;
3080         data  = skb_data(skb);
3081 /*
3082  * Look at the protocol in the skb to determine the difference between
3083  * an IP frame and an IPX frame.
3084  */
3085         switch (ntohs (skb->protocol)) {
3086         case ETH_P_IPX:
3087                 answer = ppp_dev_xmit_ipx (dev, ppp, data, len, PPP_IPX);
3088                 break;
3089
3090         case ETH_P_IP:
3091                 answer = ppp_dev_xmit_ip (dev, ppp, data);
3092                 break;
3093
3094         default: /* All others have no support at this time. */
3095                 dev_kfree_skb (skb, FREE_WRITE);
3096                 return 0;
3097         }
3098 /*
3099  * This is the end of the transmission. Release the buffer if it was sent.
3100  */
3101         if (answer == 0) {
3102                 dev_kfree_skb (skb, FREE_WRITE);
3103                 ppp->ddinfo.xmit_idle = jiffies;
3104         }
3105         return answer;
3106 }
3107
3108 /*
3109  * Generate the statistic information for the /proc/net/dev listing.
3110  */
3111
3112 static struct enet_statistics *
3113 ppp_dev_stats (struct device *dev)
3114 {
3115         struct ppp *ppp = dev2ppp (dev);
3116         static struct enet_statistics ppp_stats;
3117
3118         ppp_stats.rx_packets          = ppp->stats.ppp_ipackets;
3119         ppp_stats.rx_errors           = ppp->stats.ppp_ierrors;
3120         ppp_stats.rx_dropped          = ppp->stats.ppp_ierrors;
3121         ppp_stats.rx_fifo_errors      = 0;
3122         ppp_stats.rx_length_errors    = 0;
3123         ppp_stats.rx_over_errors      = 0;
3124         ppp_stats.rx_crc_errors       = 0;
3125         ppp_stats.rx_frame_errors     = 0;
3126         ppp_stats.tx_packets          = ppp->stats.ppp_opackets;
3127         ppp_stats.tx_errors           = ppp->stats.ppp_oerrors;
3128         ppp_stats.tx_dropped          = 0;
3129         ppp_stats.tx_fifo_errors      = 0;
3130         ppp_stats.collisions          = 0;
3131         ppp_stats.tx_carrier_errors   = 0;
3132         ppp_stats.tx_aborted_errors   = 0;
3133         ppp_stats.tx_window_errors    = 0;
3134         ppp_stats.tx_heartbeat_errors = 0;
3135
3136         if (ppp->flags & SC_DEBUG)
3137                 printk (KERN_INFO "ppp_dev_stats called\n");
3138         return &ppp_stats;
3139 }
3140
3141 static int ppp_dev_header (sk_buff *skb, struct device *dev,
3142                            __u16 type, void *daddr,
3143                            void *saddr, unsigned int len)
3144 {
3145         return (0);
3146 }
3147
3148 static int
3149 ppp_dev_rebuild (void *eth, struct device *dev,
3150                  unsigned long raddr, struct sk_buff *skb)
3151 {
3152         return (0);
3153 }
3154
3155 /*************************************************************
3156  * UTILITIES
3157  *    Miscellany called by various functions above.
3158  *************************************************************/
3159
3160 /* Locate the previous instance of the PPP channel */
3161 static struct ppp *
3162 ppp_find (int pid_value)
3163 {
3164         int             if_num;
3165         ppp_ctrl_t      *ctl;
3166         struct ppp      *ppp;
3167
3168         /* try to find the exact same free device which we had before */
3169         ctl      = ppp_list;
3170         if_num   = 0;
3171   
3172         while (ctl) {
3173                 ppp = ctl2ppp (ctl);
3174                 if (!set_bit(0, &ppp->inuse)) {
3175                         if (ppp->sc_xfer == pid_value) {
3176                                 ppp->sc_xfer = 0;
3177                                 return (ppp);
3178                         }
3179                         clear_bit (0, &ppp->inuse);
3180                 }
3181                 ctl = ctl->next;
3182                 if (++if_num == max_dev)
3183                         break;
3184         }
3185         return NULL;
3186 }
3187
3188 /* allocate or create a PPP channel */
3189 static struct ppp *
3190 ppp_alloc (void)
3191 {
3192         int             if_num;
3193         int             status;
3194         ppp_ctrl_t      *ctl;
3195         struct device   *dev;
3196         struct ppp      *ppp;
3197
3198         /* try to find an free device */
3199         ctl      = ppp_list;
3200         if_num   = 0;
3201   
3202         while (ctl) {
3203                 ppp = ctl2ppp (ctl);
3204                 if (!set_bit(0, &ppp->inuse))
3205                         return (ppp);
3206                 ctl = ctl->next;
3207                 if (++if_num == max_dev)
3208                         return (NULL);
3209         }
3210 /*
3211  * There are no available items. Allocate a device from the system pool
3212  */
3213         ctl = (ppp_ctrl_t *) kmalloc (sizeof(ppp_ctrl_t), GFP_KERNEL);
3214         if (ctl) {
3215                 (void) memset(ctl, 0, sizeof(ppp_ctrl_t));
3216                 ppp = ctl2ppp (ctl);
3217                 dev = ctl2dev (ctl);
3218
3219                 /* initialize channel control data */
3220                 set_bit(0, &ppp->inuse);
3221
3222                 ppp->line      = if_num;
3223                 ppp->tty       = NULL;
3224                 ppp->dev       = dev;
3225     
3226                 dev->next      = NULL;
3227                 dev->init      = ppp_init_dev;
3228                 dev->name      = ctl->name;
3229                 dev->base_addr = (__u32) if_num;
3230                 dev->priv      = (void *) ppp;
3231
3232                 sprintf (dev->name, "ppp%d", if_num);
3233     
3234                 /* link in the new channel */
3235                 ctl->next      = ppp_list;
3236                 ppp_list       = ctl;
3237
3238                 /* register device so that we can be ifconfig'd */
3239                 /* ppp_init_dev() will be called as a side-effect */
3240
3241                 status = register_netdev (dev);
3242                 if (status == 0) {
3243                         printk (KERN_INFO "registered device %s\n", dev->name);
3244                         return (ppp);
3245                 }
3246
3247                 printk (KERN_ERR
3248                        "ppp_alloc - register_netdev(%s) = %d failure.\n",
3249                         dev->name, status);
3250                 /* This one will forever be busy as it is not initialized */
3251         }
3252         return (NULL);
3253 }
3254
3255 /*
3256  * Utility procedures to print a buffer in hex/ascii
3257  */
3258
3259 static void
3260 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3261 {
3262         register __u8 next_ch;
3263         static char hex[] = "0123456789ABCDEF";
3264
3265         while (count-- > 0) {
3266                 next_ch = *in++;
3267                 *out++ = hex[(next_ch >> 4) & 0x0F];
3268                 *out++ = hex[next_ch & 0x0F];
3269                 ++out;
3270         }
3271 }
3272
3273 static void
3274 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3275 {
3276         register __u8 next_ch;
3277
3278         while (count-- > 0) {
3279                 next_ch = *in++;
3280
3281                 if (next_ch < 0x20 || next_ch > 0x7e)
3282                         *out++ = '.';
3283                 else {
3284                         *out++ = next_ch;
3285                         if (next_ch == '%')   /* printk/syslogd has a bug !! */
3286                                 *out++ = '%';
3287                 }
3288         }
3289         *out = '\0';
3290 }
3291
3292 static void
3293 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
3294 {
3295         __u8 line[44];
3296
3297         if (name != (__u8 *) NULL)
3298                 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3299
3300         while (count > 8) {
3301                 memset (line, 32, 44);
3302                 ppp_print_hex (line, buf, 8);
3303                 ppp_print_char (&line[8 * 3], buf, 8);
3304                 printk (KERN_DEBUG "%s\n", line);
3305                 count -= 8;
3306                 buf += 8;
3307         }
3308
3309         if (count > 0) {
3310                 memset (line, 32, 44);
3311                 ppp_print_hex (line, buf, count);
3312                 ppp_print_char (&line[8 * 3], buf, count);
3313                 printk (KERN_DEBUG "%s\n", line);
3314         }
3315 }
3316
3317 /*************************************************************
3318  * Compressor module interface
3319  *************************************************************/
3320
3321 struct compressor_link {
3322         struct compressor_link  *next;
3323         struct compressor       *comp;
3324 };
3325
3326 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3327
3328 static struct compressor *find_compressor (int type)
3329 {
3330         struct compressor_link *lnk;
3331         unsigned long flags;
3332
3333         save_flags(flags);
3334         cli();
3335
3336         lnk = ppp_compressors;
3337         while (lnk != (struct compressor_link *) 0) {
3338                 if ((int) (__u8) lnk->comp->compress_proto == type) {
3339                         restore_flags(flags);
3340                         return lnk->comp;
3341                 }
3342                 lnk = lnk->next;
3343         }
3344
3345         restore_flags(flags);
3346         return (struct compressor *) 0;
3347 }
3348
3349 static int ppp_register_compressor (struct compressor *cp)
3350 {
3351         struct compressor_link *new;
3352         unsigned long flags;
3353
3354         new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3355
3356         if (new == (struct compressor_link *) 0)
3357                 return 1;
3358
3359         save_flags(flags);
3360         cli();
3361
3362         if (find_compressor (cp->compress_proto)) {
3363                 restore_flags(flags);
3364                 kfree (new);
3365                 return 0;
3366         }
3367
3368         new->next       = ppp_compressors;
3369         new->comp       = cp;
3370         ppp_compressors = new;
3371
3372         restore_flags(flags);
3373         return 0;
3374 }
3375
3376 static void ppp_unregister_compressor (struct compressor *cp)
3377 {
3378         struct compressor_link *prev = (struct compressor_link *) 0;
3379         struct compressor_link *lnk;
3380         unsigned long flags;
3381
3382         save_flags(flags);
3383         cli();
3384
3385         lnk  = ppp_compressors;
3386         while (lnk != (struct compressor_link *) 0) {
3387                 if (lnk->comp == cp) {
3388                         if (prev)
3389                                 prev->next = lnk->next;
3390                         else
3391                                 ppp_compressors = lnk->next;
3392                         kfree (lnk);
3393                         break;
3394                 }
3395                 prev = lnk;
3396                 lnk  = lnk->next;
3397         }
3398         restore_flags(flags);
3399 }
3400
3401 /*************************************************************
3402  * Module support routines
3403  *************************************************************/
3404
3405 #ifdef MODULE
3406 int
3407 init_module(void)
3408 {
3409         int status;
3410
3411         /* register our line disciplines */
3412         status = ppp_first_time();
3413         if (status != 0)
3414                 printk (KERN_INFO
3415                        "PPP: ppp_init() failure %d\n", status);
3416         else
3417                 (void) register_symtab (&ppp_syms);
3418         return (status);
3419 }
3420
3421 void
3422 cleanup_module(void)
3423 {
3424         int status;
3425         ppp_ctrl_t *ctl, *next_ctl;
3426         struct device *dev;
3427         struct ppp *ppp;
3428         int busy_flag = 0;
3429 /*
3430  * Ensure that the devices are not in operation.
3431  */
3432         ctl = ppp_list;
3433         while (ctl) {
3434                 ppp = ctl2ppp (ctl);
3435                 if (ppp->inuse && ppp->tty != NULL) {
3436                         busy_flag = 1;
3437                         break;
3438                 }
3439
3440                 dev = ctl2dev (ctl);
3441                 if (dev->start || dev->flags & IFF_UP) {
3442                         busy_flag = 1;
3443                         break;
3444                 }
3445                 ctl = ctl->next;
3446         }
3447 /*
3448  * Ensure that there are no compressor modules registered
3449  */
3450         if (ppp_compressors != NULL)
3451                 busy_flag = 1;
3452
3453         if (busy_flag) {
3454                 printk (KERN_INFO
3455                         "PPP: device busy, remove delayed\n");
3456                 return;
3457         }
3458 /*
3459  * Release the tty registration of the line discipline so that no new entries
3460  * may be created.
3461  */
3462         status = tty_register_ldisc (N_PPP, NULL);
3463         if (status != 0)
3464                 printk (KERN_INFO
3465                         "PPP: Unable to unregister ppp line discipline "
3466                         "(err = %d)\n", status);
3467         else
3468                 printk (KERN_INFO
3469                        "PPP: ppp line discipline successfully unregistered\n");
3470 /*
3471  * De-register the devices so that there is no problem with them
3472  */     
3473         next_ctl = ppp_list;
3474         while (next_ctl) {
3475                 ctl      = next_ctl;
3476                 next_ctl = ctl->next;
3477                 ppp      = ctl2ppp (ctl);
3478                 dev      = ctl2dev (ctl);
3479
3480                 ppp_release       (ppp);
3481                 unregister_netdev (dev);
3482                 kfree (ctl);
3483         }
3484 }
3485 #endif