Import 2.3.18pre1
[davej-history.git] / drivers / net / ppp.c
blob13f0b88d991ced589108870243f0410f5266838e
1 /* PPP for Linux
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>
7 * ==FILEVERSION 990510==
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.
20 Sources:
22 slip.c
24 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
25 Multi-protocol Datagrams over Point-to-Point Links
27 RFC1332: IPCP
29 ppp-2.0
31 Flags for this module (any combination is acceptable for testing.):
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.
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.
43 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
44 #define CHECK_CHARACTERS 1
46 #define PPP_MAX_RCV_QLEN 32 /* max # frames we queue up for pppd */
48 /* $Id: ppp.c,v 1.24 1999/03/31 06:07:57 paulus Exp $ */
50 #include <linux/config.h>
51 #include <linux/module.h>
52 #include <linux/kernel.h>
53 #include <linux/sched.h>
54 #include <linux/types.h>
55 #include <linux/fcntl.h>
56 #include <linux/interrupt.h>
57 #include <linux/ptrace.h>
58 #include <linux/poll.h>
59 #include <linux/in.h>
60 #include <linux/malloc.h>
61 #include <linux/tty.h>
62 #include <linux/errno.h>
63 #include <linux/string.h> /* used in new tty drivers */
64 #include <linux/signal.h> /* used in new tty drivers */
65 #include <asm/system.h>
66 #include <asm/bitops.h>
67 #include <asm/uaccess.h>
69 #include <linux/if.h>
70 #include <linux/if_ether.h>
71 #include <linux/netdevice.h>
72 #include <linux/skbuff.h>
73 #include <linux/inet.h>
74 #include <linux/ioctl.h>
75 #include <linux/ip.h>
76 #include <linux/tcp.h>
77 #include <linux/if_arp.h>
78 #include <net/slhc_vj.h>
80 #define fcstab ppp_crc16_table /* Name of the table in the kernel */
81 #include <linux/ppp_defs.h>
83 #include <linux/socket.h>
84 #include <linux/if_ppp.h>
85 #include <linux/if_pppvar.h>
86 #include <linux/ppp-comp.h>
88 #ifdef CONFIG_KMOD
89 #include <linux/kmod.h>
90 #endif
93 * Local functions
96 #ifdef CONFIG_MODULES
97 static int ppp_register_compressor (struct compressor *cp);
98 static void ppp_unregister_compressor (struct compressor *cp);
99 #endif
101 static void ppp_async_init(struct ppp *ppp);
102 static void ppp_async_release(struct ppp *ppp);
103 static int ppp_tty_sync_push(struct ppp *ppp);
104 static int ppp_tty_push(struct ppp *ppp);
105 static int ppp_async_encode(struct ppp *ppp);
106 static int ppp_async_send(struct ppp *, struct sk_buff *);
107 static int ppp_sync_send(struct ppp *, struct sk_buff *);
108 static void ppp_tty_flush_output(struct ppp *);
110 static int ppp_ioctl(struct ppp *, unsigned int, unsigned long);
111 static int ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp);
112 static void ppp_proto_ccp(struct ppp *ppp, __u8 *dp, int len, int rcvd);
113 static void ppp_ccp_closed(struct ppp *ppp);
114 static int ppp_receive_frame(struct ppp *, struct sk_buff *);
115 static void ppp_receive_error(struct ppp *ppp);
116 static void ppp_output_wakeup(struct ppp *ppp);
117 static void ppp_send_ctrl(struct ppp *ppp, struct sk_buff *skb);
118 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
119 static void ppp_send_frames(struct ppp *ppp);
120 static struct sk_buff *ppp_vj_compress(struct ppp *ppp, struct sk_buff *skb);
122 static struct ppp *ppp_find (int pid_value);
123 static struct ppp *ppp_alloc (void);
124 static void ppp_generic_init(struct ppp *ppp);
125 static void ppp_release(struct ppp *ppp);
126 static void ppp_print_buffer (const char *, const __u8 *, int);
127 static struct compressor *find_compressor (int type);
129 #ifndef OPTIMIZE_FLAG_TIME
130 #define OPTIMIZE_FLAG_TIME 0
131 #endif
134 * Parameters which may be changed via insmod.
137 static int flag_time = OPTIMIZE_FLAG_TIME;
138 MODULE_PARM(flag_time, "i");
140 #define CHECK_PPP_MAGIC(ppp) do { \
141 if (ppp->magic != PPP_MAGIC) { \
142 printk(ppp_magic_warn, ppp, __FILE__, __LINE__); \
144 } while (0)
145 #define CHECK_PPP(a) do { \
146 CHECK_PPP_MAGIC(ppp); \
147 if (!ppp->inuse) { \
148 printk(ppp_warning, __LINE__); \
149 return a; \
151 } while (0)
152 #define CHECK_PPP_VOID() do { \
153 CHECK_PPP_MAGIC(ppp); \
154 if (!ppp->inuse) { \
155 printk(ppp_warning, __LINE__); \
156 return; \
158 } while (0)
160 #define tty2ppp(tty) ((struct ppp *) ((tty)->disc_data))
161 #define dev2ppp(dev) ((struct ppp *) ((dev)->priv))
162 #define ppp2tty(ppp) ((ppp)->tty)
163 #define ppp2dev(ppp) (&(ppp)->dev)
165 static struct ppp *ppp_list = NULL;
166 static struct ppp *ppp_last = NULL;
168 /* Define these strings only once for all macro invocations */
169 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
170 static char ppp_magic_warn[] = KERN_WARNING "bad magic for ppp %p at %s:%d\n";
172 static char szVersion[] = PPP_VERSION;
174 EXPORT_SYMBOL(ppp_register_compressor);
175 EXPORT_SYMBOL(ppp_unregister_compressor);
177 /*************************************************************
178 * LINE DISCIPLINE SUPPORT
179 * The following code implements the PPP line discipline
180 * and supports using PPP on an async serial line.
181 *************************************************************/
183 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
184 #define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \
185 ppp->recv_async_map & (1 << (c)))
188 * TTY callbacks
191 static ssize_t ppp_tty_read(struct tty_struct *, struct file *, __u8 *,
192 size_t);
193 static ssize_t ppp_tty_write(struct tty_struct *, struct file *, const __u8 *,
194 size_t);
195 static int ppp_tty_ioctl(struct tty_struct *, struct file *, unsigned int,
196 unsigned long);
197 static unsigned int ppp_tty_poll(struct tty_struct *tty, struct file *filp,
198 poll_table * wait);
199 static int ppp_tty_open (struct tty_struct *);
200 static void ppp_tty_close (struct tty_struct *);
201 static int ppp_tty_room (struct tty_struct *tty);
202 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
203 char *fp, int count);
204 static void ppp_tty_wakeup (struct tty_struct *tty);
206 __u16 ppp_crc16_table[256] =
208 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
209 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
210 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
211 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
212 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
213 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
214 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
215 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
216 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
217 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
218 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
219 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
220 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
221 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
222 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
223 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
224 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
225 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
226 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
227 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
228 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
229 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
230 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
231 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
232 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
233 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
234 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
235 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
236 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
237 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
238 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
239 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
241 EXPORT_SYMBOL(ppp_crc16_table);
243 #ifdef CHECK_CHARACTERS
244 static __u32 paritytab[8] =
246 0x96696996, 0x69969669, 0x69969669, 0x96696996,
247 0x69969669, 0x96696996, 0x96696996, 0x69969669
249 #endif
252 * This procedure is called at initialization time to register
253 * the PPP line discipline.
255 static int
256 ppp_first_time(void)
258 static struct tty_ldisc ppp_ldisc;
259 int status;
261 printk(KERN_INFO
262 "PPP: version %s (demand dialling)"
263 "\n", szVersion);
265 #ifndef MODULE /* slhc module logic has its own copyright announcement */
266 printk(KERN_INFO
267 "TCP compression code copyright 1989 Regents of the "
268 "University of California\n");
269 #endif
272 * Register the tty discipline
274 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
275 ppp_ldisc.magic = TTY_LDISC_MAGIC;
276 ppp_ldisc.name = "ppp";
277 ppp_ldisc.open = ppp_tty_open;
278 ppp_ldisc.close = ppp_tty_close;
279 ppp_ldisc.read = ppp_tty_read;
280 ppp_ldisc.write = ppp_tty_write;
281 ppp_ldisc.ioctl = ppp_tty_ioctl;
282 ppp_ldisc.poll = ppp_tty_poll;
283 ppp_ldisc.receive_room = ppp_tty_room;
284 ppp_ldisc.receive_buf = ppp_tty_receive;
285 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
287 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
288 if (status == 0)
289 printk(KERN_INFO "PPP line discipline registered.\n");
290 else
291 printk(KERN_ERR "error registering line discipline: %d\n",
292 status);
293 return status;
297 #ifndef MODULE
299 * Called at boot time if the PPP driver is compiled into the kernel.
302 ppp_init(struct net_device *dev)
304 static int first_time = 1;
305 int answer = 0;
307 if (first_time) {
308 first_time = 0;
309 answer = ppp_first_time();
311 if (answer == 0)
312 answer = -ENODEV;
313 return answer;
315 #endif
318 * Initialize the async-specific parts of the ppp structure.
320 static void
321 ppp_async_init(struct ppp *ppp)
323 ppp->escape = 0;
324 ppp->toss = 0xE0;
325 ppp->tty_pushing = 0;
327 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
328 ppp->xmit_async_map[0] = 0xffffffff;
329 ppp->xmit_async_map[3] = 0x60000000;
330 ppp->recv_async_map = 0xffffffff;
332 ppp->tpkt = NULL;
333 ppp->tfcs = PPP_INITFCS;
334 ppp->optr = ppp->obuf;
335 ppp->olim = ppp->obuf;
337 ppp->rpkt = NULL;
338 ppp->rfcs = PPP_INITFCS;
340 ppp->tty = NULL;
341 ppp->backup_tty = NULL;
343 ppp->bytes_sent = 0;
344 ppp->bytes_rcvd = 0;
348 * Clean up the async-specific parts of the ppp structure.
350 static void
351 ppp_async_release(struct ppp *ppp)
353 struct sk_buff *skb;
355 if ((skb = ppp->rpkt) != NULL)
356 kfree_skb(skb);
357 ppp->rpkt = NULL;
358 if ((skb = ppp->tpkt) != NULL)
359 kfree_skb(skb);
360 ppp->tpkt = NULL;
364 * TTY callback.
366 * Called when the tty discipline is switched to PPP.
369 static int
370 ppp_tty_open (struct tty_struct *tty)
372 struct ppp *ppp;
375 * Allocate a ppp structure to use.
377 tty->disc_data = NULL;
378 ppp = ppp_find(current->pid);
379 if (ppp != NULL) {
381 * If we are taking over a ppp unit which is currently
382 * connected to a loopback pty, there's not much to do.
384 CHECK_PPP(-EINVAL);
386 } else {
387 ppp = ppp_alloc();
388 if (ppp == NULL) {
389 printk(KERN_ERR "ppp_alloc failed\n");
390 return -ENFILE;
394 * Initialize the control block
396 ppp_generic_init(ppp);
397 ppp_async_init(ppp);
399 MOD_INC_USE_COUNT;
402 tty->disc_data = ppp;
403 ppp->tty = tty;
406 * Flush any pending characters in the driver
408 if (tty->driver.flush_buffer)
409 tty->driver.flush_buffer (tty);
411 return ppp->line;
415 * TTY callback.
417 * Called when the line discipline is changed to something
418 * else, the tty is closed, or the tty detects a hangup.
421 static void
422 ppp_tty_close (struct tty_struct *tty)
424 struct ppp *ppp = tty2ppp(tty);
426 if (ppp == NULL)
427 return;
428 tty->disc_data = NULL;
429 if (ppp->magic != PPP_MAGIC) {
430 printk(KERN_WARNING "ppp_tty_close: bogus\n");
431 return;
433 if (!ppp->inuse) {
434 printk(KERN_WARNING "ppp_tty_close: not inuse\n");
435 ppp->tty = ppp->backup_tty = 0;
436 return;
438 if (tty == ppp->backup_tty)
439 ppp->backup_tty = 0;
440 if (tty != ppp->tty)
441 return;
442 if (ppp->backup_tty) {
443 ppp->tty = ppp->backup_tty;
444 if (ppp_tty_push(ppp))
445 ppp_output_wakeup(ppp);
446 wake_up_interruptible(&ppp->read_wait);
447 } else {
448 ppp->tty = 0;
449 ppp->sc_xfer = 0;
450 if (ppp->flags & SC_DEBUG)
451 printk(KERN_INFO "ppp: channel %s closing.\n",
452 ppp2dev(ppp)->name);
454 ppp_async_release(ppp);
455 ppp_release(ppp);
456 MOD_DEC_USE_COUNT;
461 * Read a PPP frame from the rcv_q list,
462 * waiting if necessary
464 static ssize_t
465 ppp_tty_read(struct tty_struct *tty, struct file *file, __u8 * buf,
466 size_t nr)
468 struct ppp *ppp = tty2ppp (tty);
469 struct sk_buff *skb;
470 ssize_t len, err;
473 * Validate the pointers
475 if (!ppp)
476 return -EIO;
477 CHECK_PPP(-ENXIO);
480 * Before we attempt to write the frame to the user, ensure that the
481 * user has access to the pages for the total buffer length.
483 err = verify_area(VERIFY_WRITE, buf, nr);
484 if (err != 0)
485 return (err);
488 * Wait for a frame to arrive if necessary.
489 * We increment the module use count so that the module
490 * can't go away while we're sleeping.
492 MOD_INC_USE_COUNT;
493 skb = NULL;
494 for (;;) {
495 ppp = tty2ppp(tty);
496 err = 0;
497 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
498 || tty != ppp->tty)
499 break;
501 skb = skb_dequeue(&ppp->rcv_q);
502 if (skb != 0)
503 break;
506 * If no frame is available, return -EAGAIN or wait.
508 err = -EAGAIN;
509 if (file->f_flags & O_NONBLOCK)
510 break;
512 interruptible_sleep_on(&ppp->read_wait);
513 err = -EINTR;
514 if (signal_pending(current))
515 break;
517 MOD_DEC_USE_COUNT;
518 if (skb == 0)
519 return err;
522 * Ensure that the frame will fit within the caller's buffer.
523 * If not, just discard the frame.
525 len = skb->len;
526 if (len > nr) {
527 if (ppp->flags & SC_DEBUG)
528 printk(KERN_DEBUG
529 "ppp: read of %lu bytes too small for %ld "
530 "frame\n", (unsigned long) nr, (long) len);
531 ppp->stats.ppp_ierrors++;
532 err = -EOVERFLOW;
533 goto out;
537 * Copy the received data from the buffer to the caller's area.
539 err = len;
540 if (copy_to_user(buf, skb->data, len))
541 err = -EFAULT;
543 out:
544 kfree_skb(skb);
545 return err;
549 * Writing to a tty in ppp line discipline sends a PPP frame.
550 * Used by pppd to send control packets (LCP, etc.).
552 static ssize_t
553 ppp_tty_write(struct tty_struct *tty, struct file *file, const __u8 * data,
554 size_t count)
556 struct ppp *ppp = tty2ppp (tty);
557 __u8 *new_data;
558 struct sk_buff *skb;
561 * Verify the pointers.
563 if (!ppp)
564 return -EIO;
566 if (ppp->magic != PPP_MAGIC)
567 return -EIO;
569 CHECK_PPP(-ENXIO);
572 * Ensure that the caller does not wish to send too much.
574 if (count > PPP_MTU + PPP_HDRLEN) {
575 if (ppp->flags & SC_DEBUG)
576 printk(KERN_WARNING
577 "ppp_tty_write: truncating user packet "
578 "from %lu to mtu %d\n", (unsigned long) count,
579 PPP_MTU + PPP_HDRLEN);
580 count = PPP_MTU + PPP_HDRLEN;
584 * Allocate a buffer for the data and fetch it from the user space.
586 skb = alloc_skb(count, GFP_KERNEL);
587 if (skb == NULL) {
588 printk(KERN_ERR "ppp_tty_write: no memory\n");
589 return 0;
591 new_data = skb_put(skb, count);
594 * Retrieve the user's buffer
596 if (copy_from_user(new_data, data, count)) {
597 kfree_skb(skb);
598 return -EFAULT;
602 * Send the frame
604 ppp_send_ctrl(ppp, skb);
606 return (ssize_t) count;
610 * Process the IOCTL call for the tty device.
611 * Only the ioctls that relate to using ppp on async serial lines
612 * are processed here; the rest are handled by ppp_ioctl.
614 static int
615 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
616 unsigned int param2, unsigned long param3)
618 struct ppp *ppp = tty2ppp (tty);
619 register int temp_i = 0;
620 int error = -EFAULT;
623 * Verify the status of the PPP device.
625 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse)
626 return -ENXIO;
629 * The user must have an euid of root to do these requests.
631 if (!capable(CAP_NET_ADMIN))
632 return -EPERM;
634 switch (param2) {
635 case PPPIOCGASYNCMAP:
637 * Retrieve the transmit async map
639 if (put_user(ppp->xmit_async_map[0], (int *) param3))
640 break;
641 error = 0;
642 break;
644 case PPPIOCSASYNCMAP:
646 * Set the transmit async map
648 if (get_user(temp_i, (int *) param3))
649 break;
650 ppp->xmit_async_map[0] = temp_i;
651 if (ppp->flags & SC_DEBUG)
652 printk(KERN_INFO
653 "ppp_tty_ioctl: set xmit asyncmap %x\n",
654 ppp->xmit_async_map[0]);
655 error = 0;
656 break;
658 case PPPIOCSRASYNCMAP:
660 * Set the receive async map
662 if (get_user(temp_i, (int *) param3))
663 break;
664 ppp->recv_async_map = temp_i;
665 if (ppp->flags & SC_DEBUG)
666 printk(KERN_INFO
667 "ppp_tty_ioctl: set rcv asyncmap %x\n",
668 ppp->recv_async_map);
669 error = 0;
670 break;
672 case PPPIOCGXASYNCMAP:
674 * Get the map of characters to be escaped on transmission.
676 if (copy_to_user((void *) param3, ppp->xmit_async_map,
677 sizeof (ppp->xmit_async_map)))
678 break;
679 error = 0;
680 break;
682 case PPPIOCSXASYNCMAP:
684 * Set the map of characters to be escaped on transmission.
687 __u32 temp_tbl[8];
689 if (copy_from_user(temp_tbl, (void *) param3,
690 sizeof (temp_tbl)))
691 break;
693 temp_tbl[1] = 0x00000000;
694 temp_tbl[2] &= ~0x40000000;
695 temp_tbl[3] |= 0x60000000;
697 memcpy(ppp->xmit_async_map, temp_tbl,
698 sizeof (ppp->xmit_async_map));
700 if (ppp->flags & SC_DEBUG)
701 printk(KERN_INFO
702 "ppp_tty_ioctl: set xasyncmap\n");
703 error = 0;
705 break;
707 case PPPIOCXFERUNIT:
709 * Set up this PPP unit to be used next time this
710 * process sets a tty to PPP line discipline.
712 ppp->backup_tty = tty;
713 ppp->sc_xfer = current->pid;
714 error = 0;
715 break;
717 case TCGETS:
718 case TCGETA:
720 * Allow users to read, but not set, the serial port parameters
722 error = n_tty_ioctl (tty, file, param2, param3);
723 break;
725 case TCFLSH:
727 * Flush our buffers, then call the generic code to
728 * flush the serial port's buffer.
730 if (param3 == TCIFLUSH || param3 == TCIOFLUSH) {
731 struct sk_buff *skb;
732 while ((skb = skb_dequeue(&ppp->rcv_q)) != NULL)
733 kfree_skb(skb);
735 if (param3 == TCIOFLUSH || param3 == TCOFLUSH)
736 ppp_tty_flush_output(ppp);
737 error = n_tty_ioctl (tty, file, param2, param3);
738 break;
740 case FIONREAD:
742 * Returns how many bytes are available for a read().
745 unsigned long flags;
746 struct sk_buff *skb;
747 int count = 0;
749 save_flags(flags);
750 cli();
751 skb = skb_peek(&ppp->rcv_q);
752 if (skb != 0)
753 count = skb->len;
754 restore_flags(flags);
755 if (put_user(count, (int *) param3))
756 break;
757 error = 0;
759 break;
761 default:
763 * All other ioctl() events will come here.
765 error = ppp_ioctl(ppp, param2, param3);
766 break;
768 return error;
772 * TTY callback.
774 * Process the poll() statement for the PPP device.
777 static unsigned int
778 ppp_tty_poll(struct tty_struct *tty, struct file *filp, poll_table * wait)
780 struct ppp *ppp = tty2ppp(tty);
781 unsigned int mask = 0;
783 if (ppp && ppp->magic == PPP_MAGIC && tty == ppp->tty) {
784 CHECK_PPP(0);
786 poll_wait(filp, &ppp->read_wait, wait);
788 if (skb_peek(&ppp->rcv_q) != NULL)
789 mask |= POLLIN | POLLRDNORM;
790 if (tty->flags & (1 << TTY_OTHER_CLOSED)
791 || tty_hung_up_p(filp))
792 mask |= POLLHUP;
793 mask |= POLLOUT | POLLWRNORM;
795 return mask;
799 * This function is called by the tty driver when the transmit buffer has
800 * additional space. It is used by the ppp code to continue to transmit
801 * the current buffer should the buffer have been partially sent.
803 static void
804 ppp_tty_wakeup (struct tty_struct *tty)
806 struct ppp *ppp = tty2ppp (tty);
808 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
809 if (!ppp)
810 return;
811 CHECK_PPP_VOID();
812 if (tty != ppp->tty)
813 return;
815 if (ppp_tty_push(ppp))
816 ppp_output_wakeup(ppp);
820 * Send a packet to the peer over a synchronous tty line.
821 * All encoding and FCS are handled by hardware.
822 * Addr/Ctrl and Protocol field compression implemented.
823 * Returns -1 iff the packet could not be accepted at present,
824 * 0 if the packet was accepted but we can't accept another yet, or
825 * 1 if we can accept another packet immediately.
826 * If this procedure returns 0, ppp_output_wakeup will be called
827 * exactly once.
829 static int
830 ppp_sync_send(struct ppp *ppp, struct sk_buff *skb)
832 unsigned char *data;
833 int islcp;
835 CHECK_PPP(0);
837 if (ppp->tpkt != NULL)
838 return -1;
839 ppp->tpkt = skb;
841 data = ppp->tpkt->data;
844 * LCP packets with code values between 1 (configure-reqest)
845 * and 7 (code-reject) must be sent as though no options
846 * had been negotiated.
848 islcp = PPP_PROTOCOL(data) == PPP_LCP
849 && 1 <= data[PPP_HDRLEN] && data[PPP_HDRLEN] <= 7;
851 /* only reset idle time for data packets */
852 if (PPP_PROTOCOL(data) < 0x8000)
853 ppp->last_xmit = jiffies;
854 ++ppp->stats.ppp_opackets;
855 ppp->stats.ppp_ooctects += ppp->tpkt->len;
857 if ( !(data[2]) && (ppp->flags & SC_COMP_PROT) ) {
858 /* compress protocol field */
859 data[2] = data[1];
860 data[1] = data[0];
861 skb_pull(ppp->tpkt,1);
862 data = ppp->tpkt->data;
866 * Do address/control compression
868 if ((ppp->flags & SC_COMP_AC) && !islcp
869 && PPP_ADDRESS(data) == PPP_ALLSTATIONS
870 && PPP_CONTROL(data) == PPP_UI) {
871 /* strip addr and control field */
872 skb_pull(ppp->tpkt,2);
875 return ppp_tty_sync_push(ppp);
879 * Push a synchronous frame out to the tty.
880 * Returns 1 if frame accepted (or discarded), 0 otherwise.
882 static int
883 ppp_tty_sync_push(struct ppp *ppp)
885 int sent;
886 struct tty_struct *tty = ppp2tty(ppp);
887 unsigned long flags;
889 CHECK_PPP(0);
891 if (ppp->tpkt == NULL)
892 return 0;
894 /* prevent reentrancy with tty_pushing flag */
895 save_flags(flags);
896 cli();
897 if (ppp->tty_pushing) {
898 /* record wakeup attempt so we don't lose */
899 /* a wakeup call while doing push processing */
900 ppp->woke_up=1;
901 restore_flags(flags);
902 return 0;
904 ppp->tty_pushing = 1;
905 restore_flags(flags);
907 if (tty == NULL || tty->disc_data != (void *) ppp)
908 goto flush;
910 for(;;){
911 ppp->woke_up=0;
913 /* Note: Sync driver accepts complete frame or nothing */
914 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
915 sent = tty->driver.write(tty, 0, ppp->tpkt->data, ppp->tpkt->len);
916 if (sent < 0) {
917 /* write error (possible loss of CD) */
918 /* record error and discard current packet */
919 ppp->stats.ppp_oerrors++;
920 break;
922 ppp->stats.ppp_obytes += sent;
923 if (sent < ppp->tpkt->len) {
924 /* driver unable to accept frame just yet */
925 save_flags(flags);
926 cli();
927 if (ppp->woke_up) {
928 /* wake up called while processing */
929 /* try to send the frame again */
930 restore_flags(flags);
931 continue;
933 /* wait for wakeup callback to try send again */
934 ppp->tty_pushing = 0;
935 restore_flags(flags);
936 return 0;
938 break;
940 flush:
941 /* done with current packet (sent or discarded) */
942 kfree_skb(ppp->tpkt);
943 ppp->tpkt = 0;
944 ppp->tty_pushing = 0;
945 return 1;
949 * Send a packet to the peer over an async tty line.
950 * Returns -1 iff the packet could not be accepted at present,
951 * 0 if the packet was accepted but we can't accept another yet, or
952 * 1 if we can accept another packet immediately.
953 * If this procedure returns 0, ppp_output_wakeup will be called
954 * exactly once.
956 static int
957 ppp_async_send(struct ppp *ppp, struct sk_buff *skb)
959 CHECK_PPP(0);
961 ppp_tty_push(ppp);
963 if (ppp->tpkt != NULL)
964 return -1;
965 ppp->tpkt = skb;
966 ppp->tpkt_pos = 0;
968 return ppp_tty_push(ppp);
972 * Push as much data as possible out to the tty.
973 * Returns 1 if we finished encoding the current frame, 0 otherwise.
975 static int
976 ppp_tty_push(struct ppp *ppp)
978 int avail, sent, done = 0;
979 struct tty_struct *tty = ppp2tty(ppp);
981 if (ppp->flags & SC_SYNC)
982 return ppp_tty_sync_push(ppp);
984 CHECK_PPP(0);
985 if (ppp->tty_pushing) {
986 ppp->woke_up = 1;
987 return 0;
989 if (tty == NULL || tty->disc_data != (void *) ppp)
990 goto flush;
991 while (ppp->optr < ppp->olim || ppp->tpkt != 0) {
992 ppp->tty_pushing = 1;
993 mb();
994 ppp->woke_up = 0;
995 avail = ppp->olim - ppp->optr;
996 if (avail > 0) {
997 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
998 sent = tty->driver.write(tty, 0, ppp->optr, avail);
999 if (sent < 0)
1000 goto flush; /* error, e.g. loss of CD */
1001 ppp->stats.ppp_obytes += sent;
1002 ppp->optr += sent;
1003 if (sent < avail) {
1004 wmb();
1005 ppp->tty_pushing = 0;
1006 mb();
1007 if (ppp->woke_up)
1008 continue;
1009 return done;
1012 if (ppp->tpkt != 0)
1013 done = ppp_async_encode(ppp);
1014 wmb();
1015 ppp->tty_pushing = 0;
1017 return done;
1019 flush:
1020 ppp->tty_pushing = 1;
1021 mb();
1022 ppp->stats.ppp_oerrors++;
1023 if (ppp->tpkt != 0) {
1024 kfree_skb(ppp->tpkt);
1025 ppp->tpkt = 0;
1026 done = 1;
1028 ppp->optr = ppp->olim;
1029 wmb();
1030 ppp->tty_pushing = 0;
1031 return done;
1035 * Procedure to encode the data for async serial transmission.
1036 * Does octet stuffing (escaping) and address/control
1037 * and protocol compression.
1038 * Assumes ppp->opkt != 0 on entry.
1039 * Returns 1 if we finished the current frame, 0 otherwise.
1041 static int
1042 ppp_async_encode(struct ppp *ppp)
1044 int fcs, i, count, c;
1045 unsigned char *buf, *buflim;
1046 unsigned char *data;
1047 int islcp;
1049 CHECK_PPP(0);
1051 buf = ppp->obuf;
1052 ppp->olim = buf;
1053 ppp->optr = buf;
1054 i = ppp->tpkt_pos;
1055 data = ppp->tpkt->data;
1056 count = ppp->tpkt->len;
1057 fcs = ppp->tfcs;
1060 * LCP packets with code values between 1 (configure-reqest)
1061 * and 7 (code-reject) must be sent as though no options
1062 * had been negotiated.
1064 islcp = PPP_PROTOCOL(data) == PPP_LCP
1065 && 1 <= data[PPP_HDRLEN] && data[PPP_HDRLEN] <= 7;
1067 if (i == 0) {
1069 * Start of a new packet - insert the leading FLAG
1070 * character if necessary.
1072 if (islcp || flag_time == 0
1073 || jiffies - ppp->last_xmit >= flag_time)
1074 *buf++ = PPP_FLAG;
1075 /* only reset idle time for data packets */
1076 if (PPP_PROTOCOL(data) < 0x8000)
1077 ppp->last_xmit = jiffies;
1078 fcs = PPP_INITFCS;
1079 ++ppp->stats.ppp_opackets;
1080 ppp->stats.ppp_ooctects += count;
1083 * Do address/control compression
1085 if ((ppp->flags & SC_COMP_AC) != 0 && !islcp
1086 && PPP_ADDRESS(data) == PPP_ALLSTATIONS
1087 && PPP_CONTROL(data) == PPP_UI)
1088 i += 2;
1092 * Once we put in the last byte, we need to put in the FCS
1093 * and closing flag, so make sure there is at least 7 bytes
1094 * of free space in the output buffer.
1096 buflim = buf + OBUFSIZE - 6;
1097 while (i < count && buf < buflim) {
1098 c = data[i++];
1099 if (i == 3 && c == 0 && (ppp->flags & SC_COMP_PROT))
1100 continue; /* compress protocol field */
1101 fcs = PPP_FCS(fcs, c);
1102 if (in_xmap(ppp, c) || (islcp && c < 0x20)) {
1103 *buf++ = PPP_ESCAPE;
1104 c ^= 0x20;
1106 *buf++ = c;
1109 if (i == count) {
1111 * We have finished the packet. Add the FCS and flag.
1113 fcs = ~fcs;
1114 c = fcs & 0xff;
1115 if (in_xmap(ppp, c) || (islcp && c < 0x20)) {
1116 *buf++ = PPP_ESCAPE;
1117 c ^= 0x20;
1119 *buf++ = c;
1120 c = (fcs >> 8) & 0xff;
1121 if (in_xmap(ppp, c) || (islcp && c < 0x20)) {
1122 *buf++ = PPP_ESCAPE;
1123 c ^= 0x20;
1125 *buf++ = c;
1126 *buf++ = PPP_FLAG;
1127 ppp->olim = buf;
1129 kfree_skb(ppp->tpkt);
1130 ppp->tpkt = 0;
1131 return 1;
1135 * Remember where we are up to in this packet.
1137 ppp->olim = buf;
1138 ppp->tpkt_pos = i;
1139 ppp->tfcs = fcs;
1140 return 0;
1144 * Flush output from our internal buffers.
1145 * Called for the TCFLSH ioctl.
1147 static void
1148 ppp_tty_flush_output(struct ppp *ppp)
1150 struct sk_buff *skb;
1151 int done = 0;
1153 while ((skb = skb_dequeue(&ppp->xmt_q)) != NULL)
1154 kfree_skb(skb);
1155 ppp->tty_pushing = 1;
1156 mb();
1157 ppp->optr = ppp->olim;
1158 if (ppp->tpkt != NULL) {
1159 kfree_skb(ppp->tpkt);
1160 ppp->tpkt = 0;
1161 done = 1;
1163 wmb();
1164 ppp->tty_pushing = 0;
1165 if (done)
1166 ppp_output_wakeup(ppp);
1170 * Callback function from tty driver. Return the amount of space left
1171 * in the receiver's buffer to decide if remote transmitter is to be
1172 * throttled.
1174 static int
1175 ppp_tty_room (struct tty_struct *tty)
1177 return 65536; /* We can handle an infinite amount of data. :-) */
1181 * Callback function when data is available at the tty driver.
1183 static void
1184 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1185 char *flags, int count)
1187 register struct ppp *ppp = tty2ppp (tty);
1188 struct sk_buff *skb;
1189 int chr, flg;
1190 unsigned char *p;
1192 if (ppp != 0)
1193 CHECK_PPP_VOID();
1195 * This can happen if stuff comes in on the backup tty.
1197 if (ppp == 0 || tty != ppp->tty)
1198 return;
1200 * Verify the table pointer and ensure that the line is
1201 * still in PPP discipline.
1203 if (ppp->magic != PPP_MAGIC) {
1204 if (ppp->flags & SC_DEBUG)
1205 printk(KERN_DEBUG
1206 "PPP: tty_receive called but couldn't find "
1207 "PPP struct.\n");
1208 return;
1211 * Print the buffer if desired
1213 if (ppp->flags & SC_LOG_RAWIN)
1214 ppp_print_buffer ("receive buffer", data, count);
1216 ppp->stats.ppp_ibytes += count;
1217 skb = ppp->rpkt;
1219 if ( ppp->flags & SC_SYNC ) {
1220 /* synchronous mode */
1222 if (ppp->toss==0xE0) {
1223 /* this is the 1st frame, reset vj comp */
1224 ppp_receive_error(ppp);
1225 ppp->toss = 0;
1229 * Allocate an skbuff for frame.
1230 * The 128 is room for VJ header expansion.
1233 if (skb == NULL)
1234 skb = dev_alloc_skb(ppp->mru + 128 + PPP_HDRLEN);
1236 if (skb == NULL) {
1237 if (ppp->flags & SC_DEBUG)
1238 printk(KERN_DEBUG "couldn't "
1239 "alloc skb for recv\n");
1240 } else {
1242 * Decompress A/C and protocol compression here.
1244 p = skb_put(skb, 2);
1245 p[0] = PPP_ALLSTATIONS;
1246 p[1] = PPP_UI;
1247 if (*data == PPP_ALLSTATIONS) {
1248 data += 2;
1249 count -= 2;
1251 if ((*data & 1) != 0) {
1252 p = skb_put(skb, 1);
1253 p[0] = 0;
1256 /* copy frame to socket buffer */
1257 p = skb_put(skb, count);
1258 memcpy(p,data,count);
1261 * Check if we've overflowed the MRU
1263 if (skb->len >= ppp->mru + PPP_HDRLEN + 2
1264 || skb_tailroom(skb) <= 0) {
1265 ++ppp->estats.rx_length_errors;
1266 if (ppp->flags & SC_DEBUG)
1267 printk(KERN_DEBUG "rcv frame too long: "
1268 "len=%d mru=%d hroom=%d troom=%d\n",
1269 skb->len, ppp->mru, skb_headroom(skb),
1270 skb_tailroom(skb));
1271 } else {
1272 if (!ppp_receive_frame(ppp, skb)) {
1273 kfree_skb(skb);
1274 ppp_receive_error(ppp);
1278 /* Reset for the next frame */
1279 skb = NULL;
1281 ppp->rpkt = skb;
1282 return;
1285 while (count-- > 0) {
1287 * Collect the character and error condition for the character.
1288 * Set the toss flag for the first character error.
1290 chr = *data++;
1291 if (flags) {
1292 flg = *flags++;
1293 if (flg) {
1294 if (ppp->toss == 0)
1295 ppp->toss = flg;
1296 switch (flg) {
1297 case TTY_OVERRUN:
1298 ++ppp->estats.rx_fifo_errors;
1299 break;
1300 case TTY_FRAME:
1301 case TTY_BREAK:
1302 ++ppp->estats.rx_frame_errors;
1303 break;
1305 continue;
1310 * Set the flags for d7 being 0/1 and parity being
1311 * even/odd so that the normal processing would have
1312 * all flags set at the end of the session. A
1313 * missing flag bit indicates an error condition.
1316 #ifdef CHECK_CHARACTERS
1317 if (chr & 0x80)
1318 ppp->flags |= SC_RCV_B7_1;
1319 else
1320 ppp->flags |= SC_RCV_B7_0;
1322 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1323 ppp->flags |= SC_RCV_ODDP;
1324 else
1325 ppp->flags |= SC_RCV_EVNP;
1326 #endif
1328 if (chr == PPP_FLAG) {
1330 * FLAG. This is the end of the block. If the block
1331 * ends with ESC FLAG, then the block is to be ignored.
1333 if (ppp->escape)
1334 ppp->toss |= 0x80;
1336 * Process the frame if it was received correctly.
1337 * If there was an error, let the VJ decompressor know.
1338 * There are 4 cases here:
1339 * skb != NULL, toss != 0: error in frame
1340 * skb != NULL, toss == 0: frame ok
1341 * skb == NULL, toss != 0: very first frame,
1342 * error on 1st char, or alloc_skb failed
1343 * skb == NULL, toss == 0: empty frame (~~)
1345 if (ppp->toss || !ppp_receive_frame(ppp, skb)) {
1346 if (ppp->toss && (ppp->flags & SC_DEBUG))
1347 printk(KERN_DEBUG
1348 "ppp: tossing frame (%x)\n",
1349 ppp->toss);
1350 if (skb != NULL)
1351 kfree_skb(skb);
1352 if (!(ppp->toss == 0xE0 || ppp->toss == 0x80))
1353 ++ppp->stats.ppp_ierrors;
1354 ppp_receive_error(ppp);
1357 * Reset for the next frame.
1359 skb = NULL;
1360 ppp->rfcs = PPP_INITFCS;
1361 ppp->escape = 0;
1362 ppp->toss = 0;
1363 continue;
1366 /* If we're tossing, look no further. */
1367 if (ppp->toss != 0)
1368 continue;
1370 /* If this is a control char to be ignored, do so */
1371 if (in_rmap(ppp, chr))
1372 continue;
1375 * Modify the next character if preceded by escape.
1376 * The escape character (0x7d) could be an escaped
1377 * 0x5d, if it follows an escape :-)
1379 if (ppp->escape) {
1380 chr ^= PPP_TRANS;
1381 ppp->escape = 0;
1382 } else if (chr == PPP_ESCAPE) {
1383 ppp->escape = PPP_TRANS;
1384 continue;
1388 * Allocate an skbuff on the first character received.
1389 * The 128 is room for VJ header expansion and FCS.
1391 if (skb == NULL) {
1392 skb = dev_alloc_skb(ppp->mru + 128 + PPP_HDRLEN);
1393 if (skb == NULL) {
1394 if (ppp->flags & SC_DEBUG)
1395 printk(KERN_DEBUG "couldn't "
1396 "alloc skb for recv\n");
1397 ppp->toss = 1;
1398 continue;
1403 * Decompress A/C and protocol compression here.
1405 if (skb->len == 0 && chr != PPP_ALLSTATIONS) {
1406 p = skb_put(skb, 2);
1407 p[0] = PPP_ALLSTATIONS;
1408 p[1] = PPP_UI;
1410 if (skb->len == 2 && (chr & 1) != 0) {
1411 p = skb_put(skb, 1);
1412 p[0] = 0;
1416 * Check if we've overflowed the MRU
1418 if (skb->len >= ppp->mru + PPP_HDRLEN + 2
1419 || skb_tailroom(skb) <= 0) {
1420 ++ppp->estats.rx_length_errors;
1421 ppp->toss = 0xC0;
1422 if (ppp->flags & SC_DEBUG)
1423 printk(KERN_DEBUG "rcv frame too long: "
1424 "len=%d mru=%d hroom=%d troom=%d\n",
1425 skb->len, ppp->mru, skb_headroom(skb),
1426 skb_tailroom(skb));
1427 continue;
1431 * Store the character and update the FCS.
1433 p = skb_put(skb, 1);
1434 *p = chr;
1435 ppp->rfcs = PPP_FCS(ppp->rfcs, chr);
1437 ppp->rpkt = skb;
1440 /*************************************************************
1441 * PPP NETWORK INTERFACE SUPPORT
1442 * The following code implements the PPP network
1443 * interface device and handles those parts of
1444 * the PPP processing which are independent of the
1445 * type of hardware link being used, including
1446 * VJ and packet compression.
1447 *************************************************************/
1450 * Network device driver callback routines
1453 static int ppp_init_dev(struct net_device *dev);
1454 static int ppp_dev_open(struct net_device *);
1455 static int ppp_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
1456 static int ppp_dev_close(struct net_device *);
1457 static int ppp_dev_xmit(struct sk_buff *, struct net_device *);
1458 static struct net_device_stats *ppp_dev_stats (struct net_device *);
1461 * Information for the protocol decoder
1464 typedef int (*pfn_proto) (struct ppp *, struct sk_buff *);
1466 typedef struct ppp_proto_struct {
1467 int proto;
1468 pfn_proto func;
1469 } ppp_proto_type;
1471 static int rcv_proto_ip (struct ppp *, struct sk_buff *);
1472 static int rcv_proto_ipv6 (struct ppp *, struct sk_buff *);
1473 static int rcv_proto_ipx (struct ppp *, struct sk_buff *);
1474 static int rcv_proto_at (struct ppp *, struct sk_buff *);
1475 static int rcv_proto_vjc_comp (struct ppp *, struct sk_buff *);
1476 static int rcv_proto_vjc_uncomp (struct ppp *, struct sk_buff *);
1477 static int rcv_proto_ccp (struct ppp *, struct sk_buff *);
1478 static int rcv_proto_unknown (struct ppp *, struct sk_buff *);
1480 static
1481 ppp_proto_type proto_list[] = {
1482 { PPP_IP, rcv_proto_ip },
1483 { PPP_IPV6, rcv_proto_ipv6 },
1484 { PPP_IPX, rcv_proto_ipx },
1485 { PPP_AT, rcv_proto_at },
1486 { PPP_VJC_COMP, rcv_proto_vjc_comp },
1487 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
1488 { PPP_CCP, rcv_proto_ccp },
1489 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
1493 * Called when the PPP network interface device is actually created.
1495 static int
1496 ppp_init_dev (struct net_device *dev)
1498 dev->hard_header_len = PPP_HDRLEN;
1500 /* device INFO */
1501 dev->mtu = PPP_MTU;
1502 dev->hard_start_xmit = ppp_dev_xmit;
1503 dev->open = ppp_dev_open;
1504 dev->stop = ppp_dev_close;
1505 dev->get_stats = ppp_dev_stats;
1506 dev->do_ioctl = ppp_dev_ioctl;
1507 dev->addr_len = 0;
1508 dev->tx_queue_len = 10;
1509 dev->type = ARPHRD_PPP;
1511 dev_init_buffers(dev);
1513 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1515 return 0;
1519 * Callback from the network layer when the device goes up.
1522 static int
1523 ppp_dev_open (struct net_device *dev)
1525 struct ppp *ppp = dev2ppp(dev);
1527 if (!ppp->inuse || ppp2tty(ppp) == NULL) {
1528 printk(KERN_ERR "ppp: %s not active\n", dev->name);
1529 return -ENXIO;
1532 MOD_INC_USE_COUNT;
1534 return 0;
1538 * Callback from the network layer when the ppp device goes down.
1541 static int
1542 ppp_dev_close (struct net_device *dev)
1544 struct ppp *ppp = dev2ppp (dev);
1546 CHECK_PPP_MAGIC(ppp);
1548 MOD_DEC_USE_COUNT;
1550 return 0;
1553 static inline void
1554 get_vj_stats(struct vjstat *vj, struct slcompress *slc)
1556 vj->vjs_packets = slc->sls_o_compressed + slc->sls_o_uncompressed;
1557 vj->vjs_compressed = slc->sls_o_compressed;
1558 vj->vjs_searches = slc->sls_o_searches;
1559 vj->vjs_misses = slc->sls_o_misses;
1560 vj->vjs_errorin = slc->sls_i_error;
1561 vj->vjs_tossed = slc->sls_i_tossed;
1562 vj->vjs_uncompressedin = slc->sls_i_uncompressed;
1563 vj->vjs_compressedin = slc->sls_i_compressed;
1567 * Callback from the network layer to process the sockioctl functions.
1569 static int
1570 ppp_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
1572 struct ppp *ppp = dev2ppp(dev);
1573 int nb;
1574 union {
1575 struct ppp_stats stats;
1576 struct ppp_comp_stats cstats;
1577 char vers[32];
1578 } u;
1580 CHECK_PPP_MAGIC(ppp);
1582 memset(&u, 0, sizeof(u));
1583 switch (cmd) {
1584 case SIOCGPPPSTATS:
1585 u.stats.p = ppp->stats;
1586 if (ppp->slcomp != NULL)
1587 get_vj_stats(&u.stats.vj, ppp->slcomp);
1588 nb = sizeof(u.stats);
1589 break;
1591 case SIOCGPPPCSTATS:
1592 if (ppp->sc_xc_state != NULL)
1593 (*ppp->sc_xcomp->comp_stat)
1594 (ppp->sc_xc_state, &u.cstats.c);
1595 if (ppp->sc_rc_state != NULL)
1596 (*ppp->sc_rcomp->decomp_stat)
1597 (ppp->sc_rc_state, &u.cstats.d);
1598 nb = sizeof(u.cstats);
1599 break;
1601 case SIOCGPPPVER:
1602 strcpy(u.vers, szVersion);
1603 nb = strlen(u.vers) + 1;
1604 break;
1606 default:
1607 return -EINVAL;
1610 if (copy_to_user((void *) ifr->ifr_ifru.ifru_data, &u, nb))
1611 return -EFAULT;
1612 return 0;
1616 * Process the generic PPP ioctls, i.e. those which are not specific
1617 * to any particular type of hardware link.
1619 static int
1620 ppp_ioctl(struct ppp *ppp, unsigned int param2, unsigned long param3)
1622 register int temp_i = 0, oldflags;
1623 int error = -EFAULT;
1624 unsigned long flags;
1625 struct ppp_idle cur_ddinfo;
1626 struct npioctl npi;
1628 CHECK_PPP(-ENXIO);
1631 * The user must have an euid of root to do these requests.
1633 if (!capable(CAP_NET_ADMIN))
1634 return -EPERM;
1636 switch (param2) {
1637 case PPPIOCSMRU:
1639 * Set the MRU value
1641 if (get_user(temp_i, (int *) param3))
1642 break;
1643 if (temp_i < PPP_MRU)
1644 temp_i = PPP_MRU;
1645 ppp->mru = temp_i;
1646 if (ppp->flags & SC_DEBUG)
1647 printk(KERN_INFO
1648 "ppp_ioctl: set mru to %x\n", temp_i);
1649 error = 0;
1650 break;
1652 case PPPIOCGFLAGS:
1654 * Fetch the current flags
1656 temp_i = ppp->flags & SC_MASK;
1657 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
1658 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
1659 SC_RCV_ODDP | SC_RCV_EVNP;
1660 #endif
1661 if (put_user(temp_i, (int *) param3))
1662 break;
1663 error = 0;
1664 break;
1666 case PPPIOCSFLAGS:
1668 * Set the flags for the various options
1670 if (get_user(temp_i, (int *) param3))
1671 break;
1673 if (ppp->flags & ~temp_i & SC_CCP_OPEN)
1674 ppp_ccp_closed(ppp);
1676 save_flags(flags);
1677 cli();
1678 oldflags = ppp->flags;
1679 temp_i = (temp_i & SC_MASK) | (oldflags & ~SC_MASK);
1680 ppp->flags = temp_i;
1681 restore_flags(flags);
1683 if ((oldflags | temp_i) & SC_DEBUG)
1684 printk(KERN_INFO
1685 "ppp_ioctl: set flags to %x\n", temp_i);
1686 error = 0;
1687 break;
1689 case PPPIOCSCOMPRESS:
1691 * Set the compression mode
1693 error = ppp_set_compression
1694 (ppp, (struct ppp_option_data *) param3);
1695 break;
1697 case PPPIOCGUNIT:
1699 * Obtain the unit number for this device.
1701 if (put_user(ppp->line, (int *) param3))
1702 break;
1703 if (ppp->flags & SC_DEBUG)
1704 printk(KERN_INFO
1705 "ppp_ioctl: get unit: %d\n", ppp->line);
1706 error = 0;
1707 break;
1709 case PPPIOCSDEBUG:
1711 * Set the debug level
1713 if (get_user(temp_i, (int *) param3))
1714 break;
1715 temp_i = (temp_i & 0x1F) << 16;
1717 if ((ppp->flags | temp_i) & SC_DEBUG)
1718 printk(KERN_INFO
1719 "ppp_ioctl: set dbg flags to %x\n", temp_i);
1721 save_flags(flags);
1722 cli();
1723 ppp->flags = (ppp->flags & ~0x1F0000) | temp_i;
1724 restore_flags(flags);
1725 error = 0;
1726 break;
1728 case PPPIOCGDEBUG:
1730 * Get the debug level
1732 temp_i = (ppp->flags >> 16) & 0x1F;
1733 if (put_user(temp_i, (int *) param3))
1734 break;
1735 error = 0;
1736 break;
1738 case PPPIOCGIDLE:
1740 * Get the times since the last send/receive frame operation
1742 /* change absolute times to relative times. */
1743 cur_ddinfo.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
1744 cur_ddinfo.recv_idle = (jiffies - ppp->last_recv) / HZ;
1745 if (copy_to_user((void *) param3, &cur_ddinfo,
1746 sizeof (cur_ddinfo)))
1747 break;
1748 error = 0;
1749 break;
1751 case PPPIOCSMAXCID:
1753 * Set the maximum VJ header compression slot number.
1755 if (get_user(temp_i, (int *) param3))
1756 break;
1757 error = -EINVAL;
1758 if (temp_i < 2 || temp_i > 255)
1759 break;
1760 ++temp_i;
1761 if (ppp->flags & SC_DEBUG)
1762 printk(KERN_INFO "ppp_ioctl: set maxcid to %d\n",
1763 temp_i);
1764 if (ppp->slcomp != NULL)
1765 slhc_free(ppp->slcomp);
1766 ppp->slcomp = slhc_init(16, temp_i);
1768 error = -ENOMEM;
1769 if (ppp->slcomp == NULL) {
1770 printk(KERN_ERR "ppp: no memory for VJ compression\n");
1771 break;
1773 error = 0;
1774 break;
1776 case PPPIOCGNPMODE:
1777 case PPPIOCSNPMODE:
1778 if (copy_from_user(&npi, (void *) param3, sizeof(npi)))
1779 break;
1781 switch (npi.protocol) {
1782 case PPP_IP:
1783 npi.protocol = NP_IP;
1784 break;
1785 case PPP_IPX:
1786 npi.protocol = NP_IPX;
1787 break;
1788 case PPP_AT:
1789 npi.protocol = NP_AT;
1790 break;
1791 default:
1792 if (ppp->flags & SC_DEBUG)
1793 printk(KERN_DEBUG "pppioc[gs]npmode: "
1794 "invalid proto %d\n", npi.protocol);
1795 error = -EINVAL;
1796 goto out;
1799 if (param2 == PPPIOCGNPMODE) {
1800 npi.mode = ppp->sc_npmode[npi.protocol];
1801 if (copy_to_user((void *) param3, &npi, sizeof(npi)))
1802 break;
1803 } else {
1804 ppp->sc_npmode[npi.protocol] = npi.mode;
1805 if (ppp->flags & SC_DEBUG)
1806 printk(KERN_DEBUG "ppp: set np %d to %d\n",
1807 npi.protocol, npi.mode);
1808 mark_bh(NET_BH);
1810 error = 0;
1811 break;
1813 default:
1815 * All other ioctl() events will come here.
1817 if (ppp->flags & SC_DEBUG)
1818 printk(KERN_ERR
1819 "ppp_ioctl: invalid ioctl: %x, addr %lx\n",
1820 param2, param3);
1822 error = -ENOIOCTLCMD;
1823 break;
1825 out:
1826 return error;
1830 * Process the set-compression ioctl.
1832 static int
1833 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
1835 struct compressor *cp;
1836 int error, nb;
1837 unsigned long flags;
1838 __u8 *ptr;
1839 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
1840 struct ppp_option_data data;
1843 * Fetch the compression parameters
1845 error = -EFAULT;
1846 if (copy_from_user(&data, odp, sizeof (data)))
1847 goto out;
1849 nb = data.length;
1850 ptr = data.ptr;
1851 if ((unsigned) nb >= CCP_MAX_OPTION_LENGTH)
1852 nb = CCP_MAX_OPTION_LENGTH;
1854 if (copy_from_user(ccp_option, ptr, nb))
1855 goto out;
1857 error = -EINVAL;
1858 if (ccp_option[1] < 2) /* preliminary check on the length byte */
1859 goto out;
1861 save_flags(flags);
1862 cli();
1863 ppp->flags &= ~(data.transmit? SC_COMP_RUN: SC_DECOMP_RUN);
1864 restore_flags(flags);
1866 cp = find_compressor (ccp_option[0]);
1867 #ifdef CONFIG_KMOD
1868 if (cp == NULL) {
1869 char modname[32];
1870 sprintf(modname, "ppp-compress-%d", ccp_option[0]);
1871 request_module(modname);
1872 cp = find_compressor(ccp_option[0]);
1874 #endif /* CONFIG_KMOD */
1876 if (cp == NULL) {
1877 if (ppp->flags & SC_DEBUG)
1878 printk(KERN_DEBUG
1879 "%s: no compressor for [%x %x %x], %x\n",
1880 ppp->name, ccp_option[0], ccp_option[1],
1881 ccp_option[2], nb);
1882 goto out; /* compressor not loaded */
1886 * Found a handler for the protocol - try to allocate
1887 * a compressor or decompressor.
1889 error = 0;
1890 if (data.transmit) {
1891 if (ppp->sc_xc_state != NULL)
1892 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
1893 ppp->sc_xc_state = NULL;
1895 ppp->sc_xcomp = cp;
1896 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
1897 if (ppp->sc_xc_state == NULL) {
1898 if (ppp->flags & SC_DEBUG)
1899 printk(KERN_DEBUG "%s: comp_alloc failed\n",
1900 ppp->name);
1901 error = -ENOBUFS;
1903 } else {
1904 if (ppp->sc_rc_state != NULL)
1905 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
1906 ppp->sc_rc_state = NULL;
1908 ppp->sc_rcomp = cp;
1909 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
1910 if (ppp->sc_rc_state == NULL) {
1911 if (ppp->flags & SC_DEBUG)
1912 printk(KERN_DEBUG "%s: decomp_alloc failed\n",
1913 ppp->name);
1914 error = -ENOBUFS;
1917 out:
1918 return error;
1922 * Handle a CCP packet.
1924 * The CCP packet is passed along to the pppd process just like any
1925 * other PPP frame. The difference is that some processing needs to be
1926 * immediate or the compressors will become confused on the peer.
1929 static void ppp_proto_ccp(struct ppp *ppp, __u8 *dp, int len, int rcvd)
1931 int slen = CCP_LENGTH(dp);
1932 __u8 *opt = dp + CCP_HDRLEN;
1933 int opt_len = slen - CCP_HDRLEN;
1934 unsigned long flags;
1936 if (slen > len)
1937 return;
1939 if (ppp->flags & SC_DEBUG)
1940 printk(KERN_DEBUG "ppp_proto_ccp rcvd=%d code=%x flags=%x\n",
1941 rcvd, CCP_CODE(dp), ppp->flags);
1942 save_flags(flags);
1943 switch (CCP_CODE(dp)) {
1944 case CCP_CONFREQ:
1945 case CCP_TERMREQ:
1946 case CCP_TERMACK:
1948 * CCP must be going down - disable compression
1950 if (ppp->flags & SC_CCP_UP) {
1951 cli();
1952 ppp->flags &= ~(SC_CCP_UP |
1953 SC_COMP_RUN |
1954 SC_DECOMP_RUN);
1956 break;
1958 case CCP_CONFACK:
1959 if ((ppp->flags & SC_CCP_OPEN) == 0)
1960 break;
1961 if (ppp->flags & SC_CCP_UP)
1962 break;
1963 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1964 break;
1965 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1966 break;
1967 if (!rcvd) {
1969 * we're agreeing to send compressed packets.
1971 if (ppp->sc_xc_state == NULL)
1972 break;
1974 if ((*ppp->sc_xcomp->comp_init)
1975 (ppp->sc_xc_state,
1976 opt, opt_len,
1977 ppp->line, 0, ppp->flags & SC_DEBUG)) {
1978 if (ppp->flags & SC_DEBUG)
1979 printk(KERN_DEBUG "%s: comp running\n",
1980 ppp->name);
1981 cli();
1982 ppp->flags |= SC_COMP_RUN;
1984 break;
1988 * peer is agreeing to send compressed packets.
1990 if (ppp->sc_rc_state == NULL)
1991 break;
1993 if ((*ppp->sc_rcomp->decomp_init)
1994 (ppp->sc_rc_state,
1995 opt, opt_len,
1996 ppp->line, 0, ppp->mru, ppp->flags & SC_DEBUG)) {
1997 if (ppp->flags & SC_DEBUG)
1998 printk(KERN_DEBUG "%s: decomp running\n",
1999 ppp->name);
2000 cli();
2001 ppp->flags |= SC_DECOMP_RUN;
2002 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
2004 break;
2006 case CCP_RESETACK:
2008 * CCP Reset-ack resets compressors and decompressors
2009 * as it passes through.
2011 if ((ppp->flags & SC_CCP_UP) == 0)
2012 break;
2014 if (!rcvd) {
2015 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN)) {
2016 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
2017 if (ppp->flags & SC_DEBUG)
2018 printk(KERN_DEBUG "%s: comp reset\n",
2019 ppp->name);
2021 } else {
2022 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
2023 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
2024 if (ppp->flags & SC_DEBUG)
2025 printk(KERN_DEBUG "%s: decomp reset\n",
2026 ppp->name);
2027 cli();
2028 ppp->flags &= ~SC_DC_ERROR;
2031 break;
2033 restore_flags(flags);
2037 * CCP is down; free (de)compressor state if necessary.
2040 static void
2041 ppp_ccp_closed(struct ppp *ppp)
2043 unsigned long flags;
2045 save_flags(flags);
2046 cli();
2047 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
2048 restore_flags(flags);
2049 if (ppp->flags & SC_DEBUG)
2050 printk(KERN_DEBUG "%s: ccp closed\n", ppp->name);
2051 if (ppp->sc_xc_state) {
2052 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
2053 ppp->sc_xc_state = NULL;
2056 if (ppp->sc_rc_state) {
2057 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
2058 ppp->sc_rc_state = NULL;
2062 /*************************************************************
2063 * RECEIVE-SIDE ROUTINES
2064 *************************************************************/
2067 * On entry, a received frame is in skb.
2068 * Check it and dispose as appropriate.
2070 static int
2071 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb)
2073 __u8 *data;
2074 int count;
2075 int proto;
2076 int new_count;
2077 struct sk_buff *new_skb;
2078 ppp_proto_type *proto_ptr;
2081 * An empty frame is ignored. This occurs if the FLAG sequence
2082 * precedes and follows each frame.
2084 if (skb == NULL)
2085 return 1;
2086 if (skb->len == 0) {
2087 kfree_skb(skb);
2088 return 1;
2090 data = skb->data;
2091 count = skb->len;
2094 * Generate an error if the frame is too small.
2096 if (count < PPP_HDRLEN + 2) {
2097 if (ppp->flags & SC_DEBUG)
2098 printk(KERN_DEBUG
2099 "ppp: got runt ppp frame, %d chars\n", count);
2100 ++ppp->estats.rx_length_errors;
2101 return 0;
2104 if ( !(ppp->flags & SC_SYNC) ) {
2106 * Verify the FCS of the frame and discard the FCS characters
2107 * from the end of the buffer.
2109 if (ppp->rfcs != PPP_GOODFCS) {
2110 if (ppp->flags & SC_DEBUG) {
2111 printk(KERN_DEBUG
2112 "ppp: frame with bad fcs, length = %d\n",
2113 count);
2114 ppp_print_buffer("bad frame", data, count);
2116 ++ppp->estats.rx_crc_errors;
2117 return 0;
2119 count -= 2; /* ignore the fcs characters */
2120 skb_trim(skb, count);
2124 * Process the active decompressor.
2126 if (ppp->sc_rc_state != NULL &&
2127 (ppp->flags & SC_DECOMP_RUN) &&
2128 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
2129 if (PPP_PROTOCOL(data) == PPP_COMP) {
2131 * If the frame is compressed then decompress it.
2133 new_skb = dev_alloc_skb(ppp->mru + 128 + PPP_HDRLEN);
2134 if (new_skb == NULL) {
2135 printk(KERN_ERR "ppp_recv_frame: no memory\n");
2136 new_count = DECOMP_ERROR;
2137 } else {
2138 new_count = (*ppp->sc_rcomp->decompress)
2139 (ppp->sc_rc_state, data, count,
2140 new_skb->data, ppp->mru + PPP_HDRLEN);
2142 if (new_count > 0) {
2143 /* Frame was decompressed OK */
2144 kfree_skb(skb);
2145 skb = new_skb;
2146 count = new_count;
2147 data = skb_put(skb, count);
2149 } else {
2151 * On a decompression error, we pass the
2152 * compressed frame up to pppd as an
2153 * error indication.
2155 if (ppp->flags & SC_DEBUG)
2156 printk(KERN_INFO "%s: decomp err %d\n",
2157 ppp->name, new_count);
2158 if (new_skb != 0)
2159 kfree_skb(new_skb);
2160 if (ppp->slcomp != 0)
2161 slhc_toss(ppp->slcomp);
2162 ++ppp->stats.ppp_ierrors;
2163 if (new_count == DECOMP_FATALERROR) {
2164 ppp->flags |= SC_DC_FERROR;
2165 } else {
2166 ppp->flags |= SC_DC_ERROR;
2171 } else {
2173 * The frame is not compressed. Pass it to the
2174 * decompression code so it can update its
2175 * dictionary if necessary.
2177 (*ppp->sc_rcomp->incomp)(ppp->sc_rc_state,
2178 data, count);
2181 else if (PPP_PROTOCOL(data) == PPP_COMP && (ppp->flags & SC_DEBUG))
2182 printk(KERN_INFO "%s: not decomp, rc_state=%p flags=%x\n",
2183 ppp->name, ppp->sc_rc_state, ppp->flags);
2186 * Count the frame and print it
2188 ++ppp->stats.ppp_ipackets;
2189 ppp->stats.ppp_ioctects += count;
2190 if (ppp->flags & SC_LOG_INPKT)
2191 ppp_print_buffer ("receive frame", data, count);
2194 * Find the procedure to handle this protocol.
2195 * The last one is marked as protocol 0 which is the 'catch-all'
2196 * to feed it to the pppd daemon.
2198 proto = PPP_PROTOCOL(data);
2199 proto_ptr = proto_list;
2200 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
2201 ++proto_ptr;
2204 * Update the appropriate statistic counter.
2206 if (!(*proto_ptr->func)(ppp, skb)) {
2207 kfree_skb(skb);
2208 ++ppp->stats.ppp_discards;
2211 return 1;
2215 * An input error has been detected, so we need to inform
2216 * the VJ decompressor.
2218 static void
2219 ppp_receive_error(struct ppp *ppp)
2221 CHECK_PPP_VOID();
2223 if (ppp->slcomp != 0)
2224 slhc_toss(ppp->slcomp);
2228 * Put the input frame into the networking system for the indicated protocol
2230 static int
2231 ppp_rcv_rx(struct ppp *ppp, __u16 proto, struct sk_buff *skb)
2235 * Fill in a few fields of the skb and give it to netif_rx().
2237 skb->dev = ppp2dev(ppp); /* We are the device */
2238 skb->protocol = htons(proto);
2239 skb_pull(skb, PPP_HDRLEN); /* pull off ppp header */
2240 skb->mac.raw = skb->data;
2241 ppp->last_recv = jiffies;
2242 netif_rx (skb);
2243 return 1;
2247 * Process the receipt of an IP frame
2249 static int
2250 rcv_proto_ip(struct ppp *ppp, struct sk_buff *skb)
2252 CHECK_PPP(0);
2253 if ((ppp2dev(ppp)->flags & IFF_UP) && (skb->len > 0)
2254 && ppp->sc_npmode[NP_IP] == NPMODE_PASS)
2255 return ppp_rcv_rx(ppp, ETH_P_IP, skb);
2256 return 0;
2260 * Process the receipt of an IPv6 frame
2262 static int
2263 rcv_proto_ipv6(struct ppp *ppp, struct sk_buff *skb)
2265 CHECK_PPP(0);
2266 if ((ppp2dev(ppp)->flags & IFF_UP) && (skb->len > 0)
2267 && ppp->sc_npmode[NP_IPV6] == NPMODE_PASS)
2268 return ppp_rcv_rx(ppp, ETH_P_IPV6, skb);
2269 return 0;
2273 * Process the receipt of an IPX frame
2275 static int
2276 rcv_proto_ipx(struct ppp *ppp, struct sk_buff *skb)
2278 CHECK_PPP(0);
2279 if (((ppp2dev(ppp)->flags & IFF_UP) != 0) && (skb->len > 0)
2280 && ppp->sc_npmode[NP_IPX] == NPMODE_PASS)
2281 return ppp_rcv_rx(ppp, ETH_P_IPX, skb);
2282 return 0;
2286 * Process the receipt of an Appletalk frame
2288 static int
2289 rcv_proto_at(struct ppp *ppp, struct sk_buff *skb)
2291 CHECK_PPP(0);
2292 if ((ppp2dev(ppp)->flags & IFF_UP) && (skb->len > 0)
2293 && ppp->sc_npmode[NP_AT] == NPMODE_PASS)
2294 return ppp_rcv_rx(ppp, ETH_P_PPPTALK, skb);
2295 return 0;
2299 * Process the receipt of an VJ Compressed frame
2301 static int
2302 rcv_proto_vjc_comp(struct ppp *ppp, struct sk_buff *skb)
2304 int new_count;
2306 CHECK_PPP(0);
2307 if ((ppp->flags & SC_REJ_COMP_TCP) || ppp->slcomp == NULL)
2308 return 0;
2309 new_count = slhc_uncompress(ppp->slcomp, skb->data + PPP_HDRLEN,
2310 skb->len - PPP_HDRLEN);
2311 if (new_count <= 0) {
2312 if (ppp->flags & SC_DEBUG)
2313 printk(KERN_NOTICE
2314 "ppp: error in VJ decompression\n");
2315 return 0;
2317 new_count += PPP_HDRLEN;
2318 if (new_count > skb->len)
2319 skb_put(skb, new_count - skb->len);
2320 else
2321 skb_trim(skb, new_count);
2322 return rcv_proto_ip(ppp, skb);
2326 * Process the receipt of an VJ Un-compressed frame
2328 static int
2329 rcv_proto_vjc_uncomp(struct ppp *ppp, struct sk_buff *skb)
2331 CHECK_PPP(0);
2332 if ((ppp->flags & SC_REJ_COMP_TCP) || ppp->slcomp == NULL)
2333 return 0;
2334 if (slhc_remember(ppp->slcomp, skb->data + PPP_HDRLEN,
2335 skb->len - PPP_HDRLEN) <= 0) {
2336 if (ppp->flags & SC_DEBUG)
2337 printk(KERN_NOTICE "ppp: error in VJ memorizing\n");
2338 return 0;
2340 return rcv_proto_ip(ppp, skb);
2343 static int
2344 rcv_proto_ccp(struct ppp *ppp, struct sk_buff *skb)
2346 CHECK_PPP(0);
2347 ppp_proto_ccp (ppp, skb->data + PPP_HDRLEN, skb->len - PPP_HDRLEN, 1);
2348 return rcv_proto_unknown(ppp, skb);
2352 * Receive all unclassified protocols.
2354 static int
2355 rcv_proto_unknown(struct ppp *ppp, struct sk_buff *skb)
2357 CHECK_PPP(0);
2360 * Limit queue length by dropping old frames.
2362 skb_queue_tail(&ppp->rcv_q, skb);
2363 while (ppp->rcv_q.qlen > PPP_MAX_RCV_QLEN) {
2364 struct sk_buff *skb = skb_dequeue(&ppp->rcv_q);
2365 if (skb)
2366 kfree_skb(skb);
2369 wake_up_interruptible (&ppp->read_wait);
2370 if (ppp->tty->fasync != NULL)
2371 kill_fasync (ppp->tty->fasync, SIGIO);
2373 return 1;
2376 /*************************************************************
2377 * TRANSMIT-SIDE ROUTINES
2378 *************************************************************/
2380 /* local function to store a value into the LQR frame */
2381 extern inline __u8 * store_long (register __u8 *p, register int value) {
2382 *p++ = (__u8) (value >> 24);
2383 *p++ = (__u8) (value >> 16);
2384 *p++ = (__u8) (value >> 8);
2385 *p++ = (__u8) value;
2386 return p;
2390 * Compress and send an frame to the peer.
2391 * Should be called with xmit_busy == 1, having been set by the caller.
2392 * That is, we use xmit_busy as a lock to prevent reentry of this
2393 * procedure.
2395 static void
2396 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
2398 int proto;
2399 __u8 *data;
2400 int count;
2401 __u8 *p;
2402 int ret;
2404 CHECK_PPP_VOID();
2405 data = skb->data;
2406 count = skb->len;
2408 /* dump the buffer */
2409 if (ppp->flags & SC_LOG_OUTPKT)
2410 ppp_print_buffer ("write frame", data, count);
2413 * Handle various types of protocol-specific compression
2414 * and other processing, including:
2415 * - VJ TCP header compression
2416 * - updating LQR packets
2417 * - updating CCP state on CCP packets
2419 proto = PPP_PROTOCOL(data);
2420 switch (proto) {
2421 case PPP_IP:
2422 if ((ppp->flags & SC_COMP_TCP) && ppp->slcomp != NULL)
2423 skb = ppp_vj_compress(ppp, skb);
2424 break;
2426 case PPP_LQR:
2428 * Update the LQR frame with the current MIB information.
2429 * This way the information is accurate and up-to-date.
2431 if (count < 48)
2432 break;
2433 p = data + 40; /* Point to last two items. */
2434 p = store_long(p, ppp->stats.ppp_opackets + 1);
2435 p = store_long(p, ppp->stats.ppp_ooctects + count);
2436 ++ppp->stats.ppp_olqrs;
2437 break;
2439 case PPP_CCP:
2441 * Outbound compression control frames
2443 ppp_proto_ccp(ppp, data + PPP_HDRLEN, count - PPP_HDRLEN, 0);
2444 break;
2446 data = skb->data;
2447 count = skb->len;
2450 * Compress the whole frame if possible.
2452 if (((ppp->flags & SC_COMP_RUN) != 0) &&
2453 (ppp->sc_xc_state != (void *) 0) &&
2454 (proto != PPP_LCP) &&
2455 (proto != PPP_CCP)) {
2456 struct sk_buff *new_skb;
2457 int new_count;
2459 /* Allocate an skb for the compressed frame. */
2460 new_skb = alloc_skb(ppp->mtu + PPP_HDRLEN, GFP_ATOMIC);
2461 if (new_skb == NULL) {
2462 printk(KERN_ERR "ppp_send_frame: no memory\n");
2463 kfree_skb(skb);
2464 ppp->xmit_busy = 0;
2465 return;
2468 /* Compress the frame. */
2469 new_count = (*ppp->sc_xcomp->compress)
2470 (ppp->sc_xc_state, data, new_skb->data,
2471 count, ppp->mtu + PPP_HDRLEN);
2473 /* Did it compress? */
2474 if (new_count > 0 && (ppp->flags & SC_CCP_UP)) {
2475 skb_put(new_skb, new_count);
2476 kfree_skb(skb);
2477 skb = new_skb;
2478 } else {
2480 * The frame could not be compressed, or it could not
2481 * be sent in compressed form because CCP is down.
2483 kfree_skb(new_skb);
2488 * Send the frame
2490 if ( ppp->flags & SC_SYNC )
2491 ret = ppp_sync_send(ppp, skb);
2492 else
2493 ret = ppp_async_send(ppp, skb);
2494 if (ret > 0) {
2495 /* we can release the lock */
2496 ppp->xmit_busy = 0;
2497 } else if (ret < 0) {
2498 /* can't happen, since the caller got the xmit_busy lock */
2499 printk(KERN_ERR "ppp: ppp_async_send didn't accept pkt\n");
2504 * Apply VJ TCP header compression to a packet.
2506 static struct sk_buff *
2507 ppp_vj_compress(struct ppp *ppp, struct sk_buff *skb)
2509 __u8 *orig_data, *data;
2510 struct sk_buff *new_skb;
2511 int len, proto;
2513 new_skb = alloc_skb(skb->len, GFP_ATOMIC);
2514 if (new_skb == NULL) {
2515 printk(KERN_ERR "ppp: no memory for vj compression\n");
2516 return skb;
2519 orig_data = data = skb->data + PPP_HDRLEN;
2520 len = slhc_compress(ppp->slcomp, data, skb->len - PPP_HDRLEN,
2521 new_skb->data + PPP_HDRLEN, &data,
2522 (ppp->flags & SC_NO_TCP_CCID) == 0);
2524 if (data == orig_data) {
2525 /* Couldn't compress the data */
2526 kfree_skb(new_skb);
2527 return skb;
2530 /* The data has been changed */
2531 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2532 proto = PPP_VJC_COMP;
2533 data[0] ^= SL_TYPE_COMPRESSED_TCP;
2534 } else {
2535 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2536 proto = PPP_VJC_UNCOMP;
2537 else
2538 proto = PPP_IP;
2539 data[0] = orig_data[0];
2542 data = skb_put(new_skb, len + PPP_HDRLEN);
2543 data[0] = PPP_ALLSTATIONS;
2544 data[1] = PPP_UI;
2545 data[2] = 0;
2546 data[3] = proto;
2548 kfree_skb(skb);
2549 return new_skb;
2552 static inline void
2553 ppp_send_frames(struct ppp *ppp)
2555 struct sk_buff *skb;
2557 while (!test_and_set_bit(0, &ppp->xmit_busy)) {
2558 skb = skb_dequeue(&ppp->xmt_q);
2559 if (skb == NULL) {
2560 ppp->xmit_busy = 0;
2561 break;
2563 ppp_send_frame(ppp, skb);
2565 if (!ppp->xmit_busy && ppp->dev.tbusy) {
2566 ppp->dev.tbusy = 0;
2567 mark_bh(NET_BH);
2572 * Called from the hardware (tty) layer when it can accept
2573 * another packet.
2575 static void
2576 ppp_output_wakeup(struct ppp *ppp)
2578 CHECK_PPP_VOID();
2580 if (!ppp->xmit_busy) {
2581 printk(KERN_ERR "ppp_output_wakeup called but xmit_busy==0\n");
2582 return;
2584 ppp->xmit_busy = 0;
2585 ppp_send_frames(ppp);
2589 * Send a control frame (from pppd).
2591 static void
2592 ppp_send_ctrl(struct ppp *ppp, struct sk_buff *skb)
2594 CHECK_PPP_VOID();
2597 * Put the packet on the queue, then send as many as we can.
2599 skb_queue_tail(&ppp->xmt_q, skb);
2600 ppp_send_frames(ppp);
2604 /*************************************************************
2605 * NETWORK OUTPUT
2606 * This routine accepts requests from the network layer
2607 * and attempts to deliver the packets.
2608 *************************************************************/
2610 * Send a frame to the peer.
2611 * Returns 1 iff the frame was not accepted.
2613 static int
2614 ppp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
2616 struct ppp *ppp = dev2ppp(dev);
2617 struct tty_struct *tty = ppp2tty(ppp);
2618 enum NPmode npmode;
2619 int proto;
2620 unsigned char *hdr;
2622 /* just a little sanity check. */
2623 if (skb == NULL)
2624 return 0;
2625 if (skb->data == NULL) {
2626 kfree_skb(skb);
2627 return 0;
2631 * Avoid timing problem should tty hangup while data is
2632 * queued to be sent.
2634 if (!ppp->inuse) {
2635 dev_kfree_skb(skb);
2636 return 0;
2640 * Validate the tty interface
2642 if (tty == NULL) {
2643 if (ppp->flags & SC_DEBUG)
2644 printk(KERN_ERR
2645 "ppp_dev_xmit: %s not connected to a TTY!\n",
2646 dev->name);
2647 dev_kfree_skb(skb);
2648 return 0;
2652 * Work out the appropriate network-protocol mode for this packet.
2654 npmode = NPMODE_PASS; /* default */
2655 switch (ntohs(skb->protocol)) {
2656 case ETH_P_IP:
2657 proto = PPP_IP;
2658 npmode = ppp->sc_npmode[NP_IP];
2659 break;
2660 case ETH_P_IPV6:
2661 proto = PPP_IPV6;
2662 npmode = ppp->sc_npmode[NP_IPV6];
2663 break;
2664 case ETH_P_IPX:
2665 proto = PPP_IPX;
2666 npmode = ppp->sc_npmode[NP_IPX];
2667 break;
2668 case ETH_P_PPPTALK:
2669 case ETH_P_ATALK:
2670 proto = PPP_AT;
2671 npmode = ppp->sc_npmode[NP_AT];
2672 break;
2673 default:
2674 if (ppp->flags & SC_DEBUG)
2675 printk(KERN_INFO "%s: packet for unknown proto %x\n",
2676 ppp->name, ntohs(skb->protocol));
2677 dev_kfree_skb(skb);
2678 return 0;
2682 * Drop, accept or reject the packet depending on the mode.
2684 switch (npmode) {
2685 case NPMODE_PASS:
2686 break;
2688 case NPMODE_QUEUE:
2690 * We may not send the packet now, so drop it.
2691 * XXX It would be nice to be able to return it to the
2692 * network system to be queued and retransmitted later.
2694 if (ppp->flags & SC_DEBUG)
2695 printk(KERN_DEBUG "%s: returning frame\n", ppp->name);
2696 dev_kfree_skb(skb);
2697 return 0;
2699 case NPMODE_ERROR:
2700 case NPMODE_DROP:
2701 if (ppp->flags & SC_DEBUG)
2702 printk(KERN_DEBUG
2703 "ppp_dev_xmit: dropping (npmode = %d) on %s\n",
2704 npmode, ppp->name);
2705 dev_kfree_skb(skb);
2706 return 0;
2710 * The dev->tbusy field acts as a lock to allow only
2711 * one packet to be processed at a time. If we can't
2712 * get the lock, try again later.
2713 * We deliberately queue as little as possible inside
2714 * the ppp driver in order to minimize the latency
2715 * for high-priority packets.
2717 if (test_and_set_bit(0, &ppp->xmit_busy)) {
2718 dev->tbusy = 1; /* can't take it now */
2719 return 1;
2721 dev->tbusy = 0;
2724 * Put the 4-byte PPP header on the packet.
2725 * If there isn't room for it, we have to copy the packet.
2727 if (skb_headroom(skb) < PPP_HDRLEN) {
2728 struct sk_buff *new_skb;
2730 new_skb = alloc_skb(skb->len + PPP_HDRLEN, GFP_ATOMIC);
2731 if (new_skb == NULL) {
2732 printk(KERN_ERR "%s: skb hdr alloc failed\n",
2733 ppp->name);
2734 dev_kfree_skb(skb);
2735 ppp->xmit_busy = 0;
2736 ppp_send_frames(ppp);
2737 return 0;
2739 skb_reserve(new_skb, PPP_HDRLEN);
2740 memcpy(skb_put(new_skb, skb->len), skb->data, skb->len);
2741 dev_kfree_skb(skb);
2742 skb = new_skb;
2745 hdr = skb_push(skb, PPP_HDRLEN);
2746 hdr[0] = PPP_ALLSTATIONS;
2747 hdr[1] = PPP_UI;
2748 hdr[2] = proto >> 8;
2749 hdr[3] = proto;
2751 ppp_send_frame(ppp, skb);
2752 if (!ppp->xmit_busy)
2753 ppp_send_frames(ppp);
2754 return 0;
2758 * Generate the statistic information for the /proc/net/dev listing.
2760 static struct net_device_stats *
2761 ppp_dev_stats (struct net_device *dev)
2763 struct ppp *ppp = dev2ppp (dev);
2765 ppp->estats.rx_packets = ppp->stats.ppp_ipackets;
2766 ppp->estats.rx_errors = ppp->stats.ppp_ierrors;
2767 ppp->estats.tx_packets = ppp->stats.ppp_opackets;
2768 ppp->estats.tx_errors = ppp->stats.ppp_oerrors;
2769 ppp->estats.rx_bytes = ppp->stats.ppp_ibytes;
2770 ppp->estats.tx_bytes = ppp->stats.ppp_obytes;
2772 return &ppp->estats;
2775 /*************************************************************
2776 * UTILITIES
2777 * Miscellany called by various functions above.
2778 *************************************************************/
2780 /* Locate the previous instance of the PPP channel */
2781 static struct ppp *
2782 ppp_find(int pid_value)
2784 struct ppp *ppp;
2786 /* try to find the device which this pid is already using */
2787 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
2788 if (ppp->inuse && ppp->sc_xfer == pid_value) {
2789 ppp->sc_xfer = 0;
2790 break;
2793 return ppp;
2796 /* allocate or create a PPP channel */
2797 static struct ppp *
2798 ppp_alloc(void)
2800 int if_num;
2801 int status;
2802 struct net_device *dev;
2803 struct ppp *ppp;
2805 /* try to find an free device */
2806 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
2807 if (!test_and_set_bit(0, &ppp->inuse)) {
2808 dev = ppp2dev(ppp);
2809 if (dev->flags & IFF_UP) {
2810 clear_bit(0, &ppp->inuse);
2811 continue;
2813 /* Reregister device */
2814 unregister_netdev(dev);
2815 if (register_netdev(dev) == 0)
2816 return ppp;
2817 printk(KERN_DEBUG "could not reregister ppp device\n");
2818 /* leave inuse set in this case */
2823 * There are no available units, so make a new one.
2825 ppp = (struct ppp *) kmalloc(sizeof(struct ppp), GFP_KERNEL);
2826 if (ppp == 0) {
2827 printk(KERN_ERR "ppp: struct ppp allocation failed\n");
2828 return 0;
2830 memset(ppp, 0, sizeof(*ppp));
2832 /* initialize channel control data */
2833 ppp->magic = PPP_MAGIC;
2834 ppp->next = NULL;
2835 ppp->inuse = 1;
2836 init_waitqueue_head(&ppp->read_wait);
2839 * Make up a suitable name for this device
2841 dev = ppp2dev(ppp);
2842 dev->name = ppp->name;
2843 if_num = dev_alloc_name(dev, "ppp%d");
2844 if (if_num < 0) {
2845 printk(KERN_ERR "ppp: dev_alloc_name failed (%d)\n", if_num);
2846 kfree(ppp);
2847 return 0;
2849 ppp->line = if_num;
2850 ppp->slcomp = NULL;
2852 dev->next = NULL;
2853 dev->init = ppp_init_dev;
2854 dev->name = ppp->name;
2855 dev->priv = (void *) ppp;
2857 /* register device so that we can be ifconfig'd */
2858 /* ppp_init_dev() will be called as a side-effect */
2859 status = register_netdev (dev);
2860 if (status == 0) {
2861 printk(KERN_INFO "registered device %s\n", dev->name);
2862 } else {
2863 printk(KERN_ERR
2864 "ppp_alloc - register_netdev(%s) = %d failure.\n",
2865 dev->name, status);
2866 kfree(ppp);
2867 ppp = NULL;
2870 /* link this unit into our list */
2871 if (ppp_list == 0)
2872 ppp_list = ppp;
2873 else
2874 ppp_last->next = ppp;
2875 ppp_last = ppp;
2877 return ppp;
2881 * Initialize the generic parts of the ppp structure.
2883 static void
2884 ppp_generic_init(struct ppp *ppp)
2886 int indx;
2888 ppp->flags = 0;
2889 ppp->mtu = PPP_MTU;
2890 ppp->mru = PPP_MRU;
2892 skb_queue_head_init(&ppp->xmt_q);
2893 skb_queue_head_init(&ppp->rcv_q);
2895 ppp->last_xmit = jiffies;
2896 ppp->last_recv = jiffies;
2897 ppp->xmit_busy = 0;
2899 /* clear statistics */
2900 memset(&ppp->stats, 0, sizeof (struct pppstat));
2901 memset(&ppp->estats, 0, sizeof(struct net_device_stats));
2903 /* PPP compression data */
2904 ppp->sc_xc_state = NULL;
2905 ppp->sc_rc_state = NULL;
2907 for (indx = 0; indx < NUM_NP; ++indx)
2908 ppp->sc_npmode[indx] = NPMODE_PASS;
2912 * Called to clean up the generic parts of the ppp structure.
2914 static void
2915 ppp_release(struct ppp *ppp)
2917 struct sk_buff *skb;
2919 CHECK_PPP_MAGIC(ppp);
2921 if (ppp->flags & SC_DEBUG)
2922 printk(KERN_DEBUG "%s released\n", ppp->name);
2924 ppp_ccp_closed(ppp);
2926 /* Ensure that the pppd process is not hanging on select()/poll() */
2927 wake_up_interruptible(&ppp->read_wait);
2929 if (ppp->slcomp) {
2930 slhc_free(ppp->slcomp);
2931 ppp->slcomp = NULL;
2934 while ((skb = skb_dequeue(&ppp->rcv_q)) != NULL)
2935 kfree_skb(skb);
2936 while ((skb = skb_dequeue(&ppp->xmt_q)) != NULL)
2937 kfree_skb(skb);
2939 ppp->inuse = 0;
2940 if (ppp->dev.tbusy) {
2941 ppp->dev.tbusy = 0;
2942 mark_bh(NET_BH);
2947 * Utility procedures to print a buffer in hex/ascii
2949 static void
2950 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
2952 register __u8 next_ch;
2953 static char hex[] = "0123456789ABCDEF";
2955 while (count-- > 0) {
2956 next_ch = *in++;
2957 *out++ = hex[(next_ch >> 4) & 0x0F];
2958 *out++ = hex[next_ch & 0x0F];
2959 ++out;
2963 static void
2964 ppp_print_char (register __u8 * out, const __u8 * in, int count)
2966 register __u8 next_ch;
2968 while (count-- > 0) {
2969 next_ch = *in++;
2971 if (next_ch < 0x20 || next_ch > 0x7e)
2972 *out++ = '.';
2973 else {
2974 *out++ = next_ch;
2975 if (next_ch == '%') /* printk/syslogd has a bug !! */
2976 *out++ = '%';
2979 *out = '\0';
2982 static void
2983 ppp_print_buffer (const char *name, const __u8 *buf, int count)
2985 __u8 line[44];
2987 if (name != NULL)
2988 printk(KERN_DEBUG "ppp: %s, count = %d\n", name, count);
2990 while (count > 8) {
2991 memset (line, 32, 44);
2992 ppp_print_hex (line, buf, 8);
2993 ppp_print_char (&line[8 * 3], buf, 8);
2994 printk(KERN_DEBUG "%s\n", line);
2995 count -= 8;
2996 buf += 8;
2999 if (count > 0) {
3000 memset (line, 32, 44);
3001 ppp_print_hex (line, buf, count);
3002 ppp_print_char (&line[8 * 3], buf, count);
3003 printk(KERN_DEBUG "%s\n", line);
3007 /*************************************************************
3008 * Compressor module interface
3009 *************************************************************/
3011 struct compressor_link {
3012 struct compressor_link *next;
3013 struct compressor *comp;
3016 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3018 static struct compressor *find_compressor (int type)
3020 struct compressor_link *lnk;
3021 unsigned long flags;
3023 save_flags(flags);
3024 cli();
3026 lnk = ppp_compressors;
3027 while (lnk != (struct compressor_link *) 0) {
3028 if ((int) (__u8) lnk->comp->compress_proto == type) {
3029 restore_flags(flags);
3030 return lnk->comp;
3032 lnk = lnk->next;
3035 restore_flags(flags);
3036 return (struct compressor *) 0;
3039 #ifdef CONFIG_MODULES
3040 static int ppp_register_compressor (struct compressor *cp)
3042 struct compressor_link *new;
3043 unsigned long flags;
3045 new = (struct compressor_link *)
3046 kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3048 if (new == (struct compressor_link *) 0)
3049 return 1;
3051 save_flags(flags);
3052 cli();
3054 if (find_compressor (cp->compress_proto)) {
3055 restore_flags(flags);
3056 kfree (new);
3057 return 0;
3060 new->next = ppp_compressors;
3061 new->comp = cp;
3062 ppp_compressors = new;
3064 restore_flags(flags);
3065 return 0;
3068 static void ppp_unregister_compressor (struct compressor *cp)
3070 struct compressor_link *prev = (struct compressor_link *) 0;
3071 struct compressor_link *lnk;
3072 unsigned long flags;
3074 save_flags(flags);
3075 cli();
3077 lnk = ppp_compressors;
3078 while (lnk != (struct compressor_link *) 0) {
3079 if (lnk->comp == cp) {
3080 if (prev)
3081 prev->next = lnk->next;
3082 else
3083 ppp_compressors = lnk->next;
3084 kfree (lnk);
3085 break;
3087 prev = lnk;
3088 lnk = lnk->next;
3090 restore_flags(flags);
3092 #endif
3094 /*************************************************************
3095 * Module support routines
3096 *************************************************************/
3098 #ifdef MODULE
3100 init_module(void)
3102 int status;
3104 /* register our line disciplines */
3105 status = ppp_first_time();
3106 if (status != 0)
3107 printk(KERN_INFO "PPP: ppp_init() failure %d\n", status);
3109 return status;
3112 void
3113 cleanup_module(void)
3115 int status;
3116 struct ppp *ppp, *next_ppp;
3117 int busy = 0;
3120 * Ensure that the devices are not in operation.
3122 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3123 CHECK_PPP_MAGIC(ppp);
3124 if (ppp->inuse || (ppp->dev.flags & IFF_UP))
3125 ++busy;
3127 if (busy)
3128 printk(KERN_CRIT "PPP: removing despite %d units in use!\n",
3129 busy);
3132 * Release the tty registration of the line discipline so that
3133 * ttys can no longer be put into PPP line discipline.
3135 status = tty_register_ldisc (N_PPP, NULL);
3136 if (status != 0)
3137 printk(KERN_ERR
3138 "PPP: Unable to unregister ppp line discipline "
3139 "(err = %d)\n", status);
3140 else
3141 printk(KERN_INFO
3142 "PPP: ppp line discipline successfully unregistered\n");
3145 * De-register the devices so that there is no problem with them
3147 for (ppp = ppp_list; ppp != 0; ppp = next_ppp) {
3148 next_ppp = ppp->next;
3149 unregister_netdev(&ppp->dev);
3150 kfree (ppp);
3153 #endif