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