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