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