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