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