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