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==
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.
24 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
25 Multi-protocol Datagrams over Point-to-Point Links
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
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.28 1999/05/14 01:07:57 paulus Exp $ */
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>
60 /* a macro to generate linux version number codes */
61 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
63 #if LINUX_VERSION_CODE < VERSION(2,1,14)
64 #include <linux/ioport.h>
67 #if LINUX_VERSION_CODE >= VERSION(2,1,23)
68 #include <linux/poll.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>
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>
87 #include <linux/tcp.h>
88 #include <linux/if_arp.h>
89 #include <net/slhc_vj.h>
91 #define fcstab ppp_crc16_table /* Name of the table in the kernel */
92 #include <linux/ppp_defs.h>
94 #include <linux/socket.h>
95 #include <linux/if_ppp.h>
96 #include <linux/if_pppvar.h>
97 #include <linux/ppp-comp.h>
100 #include <linux/kmod.h>
102 #ifdef CONFIG_KERNELD
103 #include <linux/kerneld.h>
107 #define PPP_VERSION "2.3.8"
109 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
111 #if LINUX_VERSION_CODE >= VERSION(2,1,5)
112 #include <asm/uaccess.h>
114 #include <asm/segment.h>
117 #define GET_USER get_user
118 #define PUT_USER put_user
119 #define COPY_FROM_USER copy_from_user
120 #define COPY_TO_USER copy_to_user
122 #else /* 2.0.x and 2.1.x before 2.1.4 */
124 #define GET_USER(val, src) \
125 (verify_area(VERIFY_READ, src, sizeof(*src))? -EFAULT: \
126 ((val) = get_user(src), 0))
127 #define PUT_USER(val, dst) \
128 (verify_area(VERIFY_WRITE, dst, sizeof(*dst))? -EFAULT: \
129 (put_user(val, dst), 0))
130 #define COPY_FROM_USER(dst, src, size) \
131 (verify_area(VERIFY_READ, src, size)? -EFAULT: \
132 (memcpy_fromfs(dst, src, size), 0))
133 #define COPY_TO_USER(dst, src, size) \
134 (verify_area(VERIFY_WRITE, dst, size)? -EFAULT: \
135 (memcpy_tofs(dst, src, size), 0))
139 #if LINUX_VERSION_CODE < VERSION(2,1,37)
140 #define test_and_set_bit(nr, addr) set_bit(nr, addr)
143 #if LINUX_VERSION_CODE < VERSION(2,1,25)
144 #define net_device_stats enet_statistics
147 #if LINUX_VERSION_CODE < VERSION(2,1,57)
148 #define signal_pending(tsk) ((tsk)->signal & ~(tsk)->blocked)
151 #if LINUX_VERSION_CODE < VERSION(2,1,60)
152 typedef int rw_ret_t;
153 typedef unsigned int rw_count_t;
155 typedef ssize_t rw_ret_t;
156 typedef size_t rw_count_t;
159 #if LINUX_VERSION_CODE < VERSION(2,1,86)
160 #define KFREE_SKB(s) dev_kfree_skb((s), FREE_WRITE)
162 #define KFREE_SKB(s) kfree_skb(s)
165 #if LINUX_VERSION_CODE < VERSION(2,1,15)
166 #define LIBERATE_SKB(s) ((s)->free = 1)
168 #define LIBERATE_SKB(s) do { } while (0)
171 #if LINUX_VERSION_CODE < VERSION(2,1,95)
172 #define SUSER() suser()
174 #define SUSER() capable(CAP_NET_ADMIN)
177 #if LINUX_VERSION_CODE < VERSION(2,2,0)
185 #ifdef CONFIG_MODULES
186 static int ppp_register_compressor (struct compressor *cp);
187 static void ppp_unregister_compressor (struct compressor *cp);
190 static void ppp_async_init(struct ppp *ppp);
191 static void ppp_async_release(struct ppp *ppp);
192 static int ppp_tty_sync_push(struct ppp *ppp);
193 static int ppp_tty_push(struct ppp *ppp);
194 static int ppp_async_encode(struct ppp *ppp);
195 static int ppp_async_send(struct ppp *, struct sk_buff *);
196 static int ppp_sync_send(struct ppp *, struct sk_buff *);
197 static void ppp_tty_flush_output(struct ppp *);
199 static int ppp_ioctl(struct ppp *, unsigned int, unsigned long);
200 static int ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp);
201 static void ppp_proto_ccp(struct ppp *ppp, __u8 *dp, int len, int rcvd);
202 static void ppp_ccp_closed(struct ppp *ppp);
203 static int ppp_receive_frame(struct ppp *, struct sk_buff *);
204 static void ppp_receive_error(struct ppp *ppp);
205 static void ppp_output_wakeup(struct ppp *ppp);
206 static void ppp_send_ctrl(struct ppp *ppp, struct sk_buff *skb);
207 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
208 static void ppp_send_frames(struct ppp *ppp);
209 static struct sk_buff *ppp_vj_compress(struct ppp *ppp, struct sk_buff *skb);
211 static struct ppp *ppp_find (int pid_value);
212 static struct ppp *ppp_alloc (void);
213 static void ppp_generic_init(struct ppp *ppp);
214 static void ppp_release(struct ppp *ppp);
215 static void ppp_print_buffer (const char *, const __u8 *, int);
216 static struct compressor *find_compressor (int type);
218 #ifndef OPTIMIZE_FLAG_TIME
219 #define OPTIMIZE_FLAG_TIME 0
223 * Parameters which may be changed via insmod.
226 static int flag_time = OPTIMIZE_FLAG_TIME;
227 #if LINUX_VERSION_CODE >= VERSION(2,1,19)
228 MODULE_PARM(flag_time, "i");
231 #define CHECK_PPP_MAGIC(ppp) do { \
232 if (ppp->magic != PPP_MAGIC) { \
233 printk(ppp_magic_warn, ppp, __FILE__, __LINE__); \
236 #define CHECK_PPP(a) do { \
237 CHECK_PPP_MAGIC(ppp); \
239 printk(ppp_warning, __LINE__); \
243 #define CHECK_PPP_VOID() do { \
244 CHECK_PPP_MAGIC(ppp); \
246 printk(ppp_warning, __LINE__); \
251 #define tty2ppp(tty) ((struct ppp *) ((tty)->disc_data))
252 #define dev2ppp(dev) ((struct ppp *) ((dev)->priv))
253 #define ppp2tty(ppp) ((ppp)->tty)
254 #define ppp2dev(ppp) (&(ppp)->dev)
256 static struct ppp *ppp_list = NULL;
257 static struct ppp *ppp_last = NULL;
259 /* Define these strings only once for all macro invocations */
260 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
261 static char ppp_magic_warn[] = KERN_WARNING "bad magic for ppp %p at %s:%d\n";
263 static char szVersion[] = PPP_VERSION;
265 #if LINUX_VERSION_CODE < VERSION(2,1,18)
266 static struct symbol_table ppp_syms = {
267 #include <linux/symtab_begin.h>
268 X(ppp_register_compressor),
269 X(ppp_unregister_compressor),
270 #include <linux/symtab_end.h>
273 EXPORT_SYMBOL(ppp_register_compressor);
274 EXPORT_SYMBOL(ppp_unregister_compressor);
277 /*************************************************************
278 * LINE DISCIPLINE SUPPORT
279 * The following code implements the PPP line discipline
280 * and supports using PPP on an async serial line.
281 *************************************************************/
283 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
284 #define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \
285 ppp->recv_async_map & (1 << (c)))
291 static rw_ret_t ppp_tty_read(struct tty_struct *, struct file *, __u8 *,
293 static rw_ret_t ppp_tty_write(struct tty_struct *, struct file *, const __u8 *,
295 static int ppp_tty_ioctl(struct tty_struct *, struct file *, unsigned int,
297 #if LINUX_VERSION_CODE < VERSION(2,1,23)
298 static int ppp_tty_select(struct tty_struct *tty, struct inode *inode,
299 struct file *filp, int sel_type, select_table * wait);
301 static unsigned int ppp_tty_poll(struct tty_struct *tty, struct file *filp,
304 static int ppp_tty_open (struct tty_struct *);
305 static void ppp_tty_close (struct tty_struct *);
306 static int ppp_tty_room (struct tty_struct *tty);
307 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
308 char *fp, int count);
309 static void ppp_tty_wakeup (struct tty_struct *tty);
311 __u16 ppp_crc16_table[256] =
313 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
314 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
315 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
316 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
317 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
318 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
319 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
320 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
321 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
322 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
323 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
324 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
325 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
326 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
327 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
328 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
329 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
330 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
331 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
332 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
333 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
334 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
335 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
336 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
337 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
338 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
339 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
340 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
341 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
342 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
343 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
344 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
346 #if LINUX_VERSION_CODE >= VERSION(2,1,18)
347 EXPORT_SYMBOL(ppp_crc16_table);
350 #ifdef CHECK_CHARACTERS
351 static __u32 paritytab[8] =
353 0x96696996, 0x69969669, 0x69969669, 0x96696996,
354 0x69969669, 0x96696996, 0x96696996, 0x69969669
359 * This procedure is called at initialization time to register
360 * the PPP line discipline.
365 static struct tty_ldisc ppp_ldisc;
369 "PPP: version %s (demand dialling)"
372 #ifndef MODULE /* slhc module logic has its own copyright announcement */
374 "TCP compression code copyright 1989 Regents of the "
375 "University of California\n");
379 * Register the tty discipline
381 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
382 ppp_ldisc.magic = TTY_LDISC_MAGIC;
383 #if LINUX_VERSION_CODE >= VERSION(2,1,28)
384 ppp_ldisc.name = "ppp";
386 ppp_ldisc.open = ppp_tty_open;
387 ppp_ldisc.close = ppp_tty_close;
388 ppp_ldisc.read = ppp_tty_read;
389 ppp_ldisc.write = ppp_tty_write;
390 ppp_ldisc.ioctl = ppp_tty_ioctl;
391 #if LINUX_VERSION_CODE < VERSION(2,1,23)
392 ppp_ldisc.select = ppp_tty_select;
394 ppp_ldisc.poll = ppp_tty_poll;
396 ppp_ldisc.receive_room = ppp_tty_room;
397 ppp_ldisc.receive_buf = ppp_tty_receive;
398 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
400 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
402 printk(KERN_INFO "PPP line discipline registered.\n");
404 printk(KERN_ERR "error registering line discipline: %d\n",
412 * Called at boot time if the PPP driver is compiled into the kernel.
415 ppp_init(struct device *dev)
417 static int first_time = 1;
422 answer = ppp_first_time();
423 #if LINUX_VERSION_CODE < VERSION(2,1,18)
425 (void) register_symtab(&ppp_syms);
435 * Initialize the async-specific parts of the ppp structure.
438 ppp_async_init(struct ppp *ppp)
442 ppp->tty_pushing = 0;
444 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
445 ppp->xmit_async_map[0] = 0xffffffff;
446 ppp->xmit_async_map[3] = 0x60000000;
447 ppp->recv_async_map = 0xffffffff;
450 ppp->tfcs = PPP_INITFCS;
451 ppp->optr = ppp->obuf;
452 ppp->olim = ppp->obuf;
455 ppp->rfcs = PPP_INITFCS;
458 ppp->backup_tty = NULL;
465 * Clean up the async-specific parts of the ppp structure.
468 ppp_async_release(struct ppp *ppp)
472 if ((skb = ppp->rpkt) != NULL)
475 if ((skb = ppp->tpkt) != NULL)
483 * Called when the tty discipline is switched to PPP.
487 ppp_tty_open (struct tty_struct *tty)
492 * Allocate a ppp structure to use.
494 tty->disc_data = NULL;
495 ppp = ppp_find(current->pid);
498 * If we are taking over a ppp unit which is currently
499 * connected to a loopback pty, there's not much to do.
506 printk(KERN_ERR "ppp_alloc failed\n");
511 * Initialize the control block
513 ppp_generic_init(ppp);
519 tty->disc_data = ppp;
523 * Flush any pending characters in the driver
525 if (tty->driver.flush_buffer)
526 tty->driver.flush_buffer (tty);
534 * Called when the line discipline is changed to something
535 * else, the tty is closed, or the tty detects a hangup.
539 ppp_tty_close (struct tty_struct *tty)
541 struct ppp *ppp = tty2ppp(tty);
545 tty->disc_data = NULL;
546 if (ppp->magic != PPP_MAGIC) {
547 printk(KERN_WARNING "ppp_tty_close: bogus\n");
551 printk(KERN_WARNING "ppp_tty_close: not inuse\n");
552 ppp->tty = ppp->backup_tty = 0;
555 if (tty == ppp->backup_tty)
559 if (ppp->backup_tty) {
560 ppp->tty = ppp->backup_tty;
561 if (ppp_tty_push(ppp))
562 ppp_output_wakeup(ppp);
563 wake_up_interruptible(&ppp->read_wait);
567 if (ppp->flags & SC_DEBUG)
568 printk(KERN_INFO "ppp: channel %s closing.\n",
571 ppp_async_release(ppp);
578 * Read a PPP frame from the rcv_q list,
579 * waiting if necessary
582 ppp_tty_read(struct tty_struct *tty, struct file *file, __u8 * buf,
585 struct ppp *ppp = tty2ppp (tty);
590 * Validate the pointers
597 * Before we attempt to write the frame to the user, ensure that the
598 * user has access to the pages for the total buffer length.
600 err = verify_area(VERIFY_WRITE, buf, nr);
605 * Wait for a frame to arrive if necessary.
606 * We increment the module use count so that the module
607 * can't go away while we're sleeping.
614 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
618 skb = skb_dequeue(&ppp->rcv_q);
623 * If no frame is available, return -EAGAIN or wait.
626 if (file->f_flags & O_NONBLOCK)
629 interruptible_sleep_on(&ppp->read_wait);
631 if (signal_pending(current))
639 * Ensure that the frame will fit within the caller's buffer.
640 * If not, just discard the frame.
644 if (ppp->flags & SC_DEBUG)
646 "ppp: read of %lu bytes too small for %ld "
647 "frame\n", (unsigned long) nr, (long) len);
648 ppp->stats.ppp_ierrors++;
654 * Copy the received data from the buffer to the caller's area.
657 if (COPY_TO_USER(buf, skb->data, len))
666 * Writing to a tty in ppp line discipline sends a PPP frame.
667 * Used by pppd to send control packets (LCP, etc.).
670 ppp_tty_write(struct tty_struct *tty, struct file *file, const __u8 * data,
673 struct ppp *ppp = tty2ppp (tty);
678 * Verify the pointers.
683 if (ppp->magic != PPP_MAGIC)
689 * Ensure that the caller does not wish to send too much.
691 if (count > PPP_MTU + PPP_HDRLEN) {
692 if (ppp->flags & SC_DEBUG)
694 "ppp_tty_write: truncating user packet "
695 "from %lu to mtu %d\n", (unsigned long) count,
696 PPP_MTU + PPP_HDRLEN);
697 count = PPP_MTU + PPP_HDRLEN;
701 * Allocate a buffer for the data and fetch it from the user space.
703 skb = alloc_skb(count, GFP_KERNEL);
705 printk(KERN_ERR "ppp_tty_write: no memory\n");
709 new_data = skb_put(skb, count);
712 * Retrieve the user's buffer
714 if (COPY_FROM_USER(new_data, data, count)) {
722 ppp_send_ctrl(ppp, skb);
724 return (rw_ret_t) count;
728 * Process the IOCTL call for the tty device.
729 * Only the ioctls that relate to using ppp on async serial lines
730 * are processed here; the rest are handled by ppp_ioctl.
733 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
734 unsigned int param2, unsigned long param3)
736 struct ppp *ppp = tty2ppp (tty);
737 register int temp_i = 0;
741 * Verify the status of the PPP device.
743 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse)
747 * The user must have an euid of root to do these requests.
753 case PPPIOCGASYNCMAP:
755 * Retrieve the transmit async map
757 if (PUT_USER(ppp->xmit_async_map[0], (int *) param3))
762 case PPPIOCSASYNCMAP:
764 * Set the transmit async map
766 if (GET_USER(temp_i, (int *) param3))
768 ppp->xmit_async_map[0] = temp_i;
769 if (ppp->flags & SC_DEBUG)
771 "ppp_tty_ioctl: set xmit asyncmap %x\n",
772 ppp->xmit_async_map[0]);
776 case PPPIOCSRASYNCMAP:
778 * Set the receive async map
780 if (GET_USER(temp_i, (int *) param3))
782 ppp->recv_async_map = temp_i;
783 if (ppp->flags & SC_DEBUG)
785 "ppp_tty_ioctl: set rcv asyncmap %x\n",
786 ppp->recv_async_map);
790 case PPPIOCGXASYNCMAP:
792 * Get the map of characters to be escaped on transmission.
794 if (COPY_TO_USER((void *) param3, ppp->xmit_async_map,
795 sizeof (ppp->xmit_async_map)))
800 case PPPIOCSXASYNCMAP:
802 * Set the map of characters to be escaped on transmission.
807 if (COPY_FROM_USER(temp_tbl, (void *) param3,
811 temp_tbl[1] = 0x00000000;
812 temp_tbl[2] &= ~0x40000000;
813 temp_tbl[3] |= 0x60000000;
815 memcpy(ppp->xmit_async_map, temp_tbl,
816 sizeof (ppp->xmit_async_map));
818 if (ppp->flags & SC_DEBUG)
820 "ppp_tty_ioctl: set xasyncmap\n");
827 * Set up this PPP unit to be used next time this
828 * process sets a tty to PPP line discipline.
830 ppp->backup_tty = tty;
831 ppp->sc_xfer = current->pid;
838 * Allow users to read, but not set, the serial port parameters
840 error = n_tty_ioctl (tty, file, param2, param3);
845 * Flush our buffers, then call the generic code to
846 * flush the serial port's buffer.
848 if (param3 == TCIFLUSH || param3 == TCIOFLUSH) {
850 while ((skb = skb_dequeue(&ppp->rcv_q)) != NULL)
853 if (param3 == TCIOFLUSH || param3 == TCOFLUSH)
854 ppp_tty_flush_output(ppp);
855 error = n_tty_ioctl (tty, file, param2, param3);
860 * Returns how many bytes are available for a read().
869 skb = skb_peek(&ppp->rcv_q);
872 restore_flags(flags);
873 if (PUT_USER(count, (int *) param3))
881 * All other ioctl() events will come here.
883 error = ppp_ioctl(ppp, param2, param3);
892 * Process the select() or poll() statement for the PPP device.
895 #if LINUX_VERSION_CODE < VERSION(2,1,23)
897 ppp_tty_select(struct tty_struct *tty, struct inode *inode,
898 struct file *filp, int sel_type, select_table * wait)
900 struct ppp *ppp = tty2ppp(tty);
904 * Verify the status of the PPP device.
906 if (!ppp || tty != ppp->tty)
913 /* The fd is readable if the receive queue isn't empty. */
914 if (skb_peek(&ppp->rcv_q) != NULL)
918 /* Check for exceptions or read errors. */
919 /* Is this a pty link and the remote disconnected? */
920 if (tty->flags & (1 << TTY_OTHER_CLOSED))
923 /* Is this a local link and the modem disconnected? */
924 if (tty_hung_up_p (filp))
927 select_wait(&ppp->read_wait, wait);
932 /* The fd is always writable. */
938 #else /* 2.1.23 or later */
941 ppp_tty_poll(struct tty_struct *tty, struct file *filp, poll_table * wait)
943 struct ppp *ppp = tty2ppp(tty);
944 unsigned int mask = 0;
946 if (ppp && ppp->magic == PPP_MAGIC && tty == ppp->tty) {
949 poll_wait(filp, &ppp->read_wait, wait);
951 if (skb_peek(&ppp->rcv_q) != NULL)
952 mask |= POLLIN | POLLRDNORM;
953 if (tty->flags & (1 << TTY_OTHER_CLOSED)
954 || tty_hung_up_p(filp))
956 mask |= POLLOUT | POLLWRNORM;
960 #endif /* >= 2.1.23 */
963 * This function is called by the tty driver when the transmit buffer has
964 * additional space. It is used by the ppp code to continue to transmit
965 * the current buffer should the buffer have been partially sent.
968 ppp_tty_wakeup (struct tty_struct *tty)
970 struct ppp *ppp = tty2ppp (tty);
972 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
979 if (ppp_tty_push(ppp))
980 ppp_output_wakeup(ppp);
984 * Send a packet to the peer over a synchronous tty line.
985 * All encoding and FCS are handled by hardware.
986 * Addr/Ctrl and Protocol field compression implemented.
987 * Returns -1 iff the packet could not be accepted at present,
988 * 0 if the packet was accepted but we can't accept another yet, or
989 * 1 if we can accept another packet immediately.
990 * If this procedure returns 0, ppp_output_wakeup will be called
994 ppp_sync_send(struct ppp *ppp, struct sk_buff *skb)
1001 if (ppp->tpkt != NULL)
1005 data = ppp->tpkt->data;
1008 * LCP packets with code values between 1 (configure-reqest)
1009 * and 7 (code-reject) must be sent as though no options
1010 * had been negotiated.
1012 islcp = PPP_PROTOCOL(data) == PPP_LCP
1013 && 1 <= data[PPP_HDRLEN] && data[PPP_HDRLEN] <= 7;
1015 /* only reset idle time for data packets */
1016 if (PPP_PROTOCOL(data) < 0x8000)
1017 ppp->last_xmit = jiffies;
1018 ++ppp->stats.ppp_opackets;
1019 ppp->stats.ppp_ooctects += ppp->tpkt->len;
1021 if ( !(data[2]) && (ppp->flags & SC_COMP_PROT) ) {
1022 /* compress protocol field */
1025 skb_pull(ppp->tpkt,1);
1026 data = ppp->tpkt->data;
1030 * Do address/control compression
1032 if ((ppp->flags & SC_COMP_AC) && !islcp
1033 && PPP_ADDRESS(data) == PPP_ALLSTATIONS
1034 && PPP_CONTROL(data) == PPP_UI) {
1035 /* strip addr and control field */
1036 skb_pull(ppp->tpkt,2);
1039 return ppp_tty_sync_push(ppp);
1043 * Push a synchronous frame out to the tty.
1044 * Returns 1 if frame accepted (or discarded), 0 otherwise.
1047 ppp_tty_sync_push(struct ppp *ppp)
1050 struct tty_struct *tty = ppp2tty(ppp);
1051 unsigned long flags;
1055 if (ppp->tpkt == NULL)
1058 /* prevent reentrancy with tty_pushing flag */
1061 if (ppp->tty_pushing) {
1062 /* record wakeup attempt so we don't lose */
1063 /* a wakeup call while doing push processing */
1065 restore_flags(flags);
1068 ppp->tty_pushing = 1;
1069 restore_flags(flags);
1071 if (tty == NULL || tty->disc_data != (void *) ppp)
1077 /* Note: Sync driver accepts complete frame or nothing */
1078 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1079 sent = tty->driver.write(tty, 0, ppp->tpkt->data, ppp->tpkt->len);
1081 /* write error (possible loss of CD) */
1082 /* record error and discard current packet */
1083 ppp->stats.ppp_oerrors++;
1086 ppp->stats.ppp_obytes += sent;
1087 if (sent < ppp->tpkt->len) {
1088 /* driver unable to accept frame just yet */
1092 /* wake up called while processing */
1093 /* try to send the frame again */
1094 restore_flags(flags);
1097 /* wait for wakeup callback to try send again */
1098 ppp->tty_pushing = 0;
1099 restore_flags(flags);
1105 /* done with current packet (sent or discarded) */
1106 KFREE_SKB(ppp->tpkt);
1108 ppp->tty_pushing = 0;
1113 * Send a packet to the peer over an async tty line.
1114 * Returns -1 iff the packet could not be accepted at present,
1115 * 0 if the packet was accepted but we can't accept another yet, or
1116 * 1 if we can accept another packet immediately.
1117 * If this procedure returns 0, ppp_output_wakeup will be called
1121 ppp_async_send(struct ppp *ppp, struct sk_buff *skb)
1127 if (ppp->tpkt != NULL)
1132 return ppp_tty_push(ppp);
1136 * Push as much data as possible out to the tty.
1137 * Returns 1 if we finished encoding the current frame, 0 otherwise.
1140 ppp_tty_push(struct ppp *ppp)
1142 int avail, sent, done = 0;
1143 struct tty_struct *tty = ppp2tty(ppp);
1145 if (ppp->flags & SC_SYNC)
1146 return ppp_tty_sync_push(ppp);
1149 if (ppp->tty_pushing) {
1153 if (tty == NULL || tty->disc_data != (void *) ppp)
1155 while (ppp->optr < ppp->olim || ppp->tpkt != 0) {
1156 ppp->tty_pushing = 1;
1159 avail = ppp->olim - ppp->optr;
1161 tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1162 sent = tty->driver.write(tty, 0, ppp->optr, avail);
1164 goto flush; /* error, e.g. loss of CD */
1165 ppp->stats.ppp_obytes += sent;
1169 ppp->tty_pushing = 0;
1177 done = ppp_async_encode(ppp);
1179 ppp->tty_pushing = 0;
1184 ppp->tty_pushing = 1;
1186 ppp->stats.ppp_oerrors++;
1187 if (ppp->tpkt != 0) {
1188 KFREE_SKB(ppp->tpkt);
1192 ppp->optr = ppp->olim;
1194 ppp->tty_pushing = 0;
1199 * Procedure to encode the data for async serial transmission.
1200 * Does octet stuffing (escaping) and address/control
1201 * and protocol compression.
1202 * Assumes ppp->opkt != 0 on entry.
1203 * Returns 1 if we finished the current frame, 0 otherwise.
1206 ppp_async_encode(struct ppp *ppp)
1208 int fcs, i, count, c;
1209 unsigned char *buf, *buflim;
1210 unsigned char *data;
1219 data = ppp->tpkt->data;
1220 count = ppp->tpkt->len;
1224 * LCP packets with code values between 1 (configure-reqest)
1225 * and 7 (code-reject) must be sent as though no options
1226 * had been negotiated.
1228 islcp = PPP_PROTOCOL(data) == PPP_LCP
1229 && 1 <= data[PPP_HDRLEN] && data[PPP_HDRLEN] <= 7;
1233 * Start of a new packet - insert the leading FLAG
1234 * character if necessary.
1236 if (islcp || flag_time == 0
1237 || jiffies - ppp->last_xmit >= flag_time)
1239 /* only reset idle time for data packets */
1240 if (PPP_PROTOCOL(data) < 0x8000)
1241 ppp->last_xmit = jiffies;
1243 ++ppp->stats.ppp_opackets;
1244 ppp->stats.ppp_ooctects += count;
1247 * Do address/control compression
1249 if ((ppp->flags & SC_COMP_AC) != 0 && !islcp
1250 && PPP_ADDRESS(data) == PPP_ALLSTATIONS
1251 && PPP_CONTROL(data) == PPP_UI)
1256 * Once we put in the last byte, we need to put in the FCS
1257 * and closing flag, so make sure there is at least 7 bytes
1258 * of free space in the output buffer.
1260 buflim = buf + OBUFSIZE - 6;
1261 while (i < count && buf < buflim) {
1263 if (i == 3 && c == 0 && (ppp->flags & SC_COMP_PROT))
1264 continue; /* compress protocol field */
1265 fcs = PPP_FCS(fcs, c);
1266 if (in_xmap(ppp, c) || (islcp && c < 0x20)) {
1267 *buf++ = PPP_ESCAPE;
1275 * We have finished the packet. Add the FCS and flag.
1279 if (in_xmap(ppp, c) || (islcp && c < 0x20)) {
1280 *buf++ = PPP_ESCAPE;
1284 c = (fcs >> 8) & 0xff;
1285 if (in_xmap(ppp, c) || (islcp && c < 0x20)) {
1286 *buf++ = PPP_ESCAPE;
1293 KFREE_SKB(ppp->tpkt);
1299 * Remember where we are up to in this packet.
1308 * Flush output from our internal buffers.
1309 * Called for the TCFLSH ioctl.
1312 ppp_tty_flush_output(struct ppp *ppp)
1314 struct sk_buff *skb;
1317 while ((skb = skb_dequeue(&ppp->xmt_q)) != NULL)
1319 ppp->tty_pushing = 1;
1321 ppp->optr = ppp->olim;
1322 if (ppp->tpkt != NULL) {
1323 KFREE_SKB(ppp->tpkt);
1328 ppp->tty_pushing = 0;
1330 ppp_output_wakeup(ppp);
1334 * Callback function from tty driver. Return the amount of space left
1335 * in the receiver's buffer to decide if remote transmitter is to be
1339 ppp_tty_room (struct tty_struct *tty)
1341 return 65536; /* We can handle an infinite amount of data. :-) */
1345 * Callback function when data is available at the tty driver.
1348 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1349 char *flags, int count)
1351 register struct ppp *ppp = tty2ppp (tty);
1352 struct sk_buff *skb;
1359 * This can happen if stuff comes in on the backup tty.
1361 if (ppp == 0 || tty != ppp->tty)
1364 * Verify the table pointer and ensure that the line is
1365 * still in PPP discipline.
1367 if (ppp->magic != PPP_MAGIC) {
1368 if (ppp->flags & SC_DEBUG)
1370 "PPP: tty_receive called but couldn't find "
1375 * Print the buffer if desired
1377 if (ppp->flags & SC_LOG_RAWIN)
1378 ppp_print_buffer ("receive buffer", data, count);
1380 ppp->stats.ppp_ibytes += count;
1383 if ( ppp->flags & SC_SYNC ) {
1384 /* synchronous mode */
1386 if (ppp->toss==0xE0) {
1387 /* this is the 1st frame, reset vj comp */
1388 ppp_receive_error(ppp);
1393 * Allocate an skbuff for frame.
1394 * The 128 is room for VJ header expansion.
1398 skb = dev_alloc_skb(ppp->mru + 128 + PPP_HDRLEN);
1401 if (ppp->flags & SC_DEBUG)
1402 printk(KERN_DEBUG "couldn't "
1403 "alloc skb for recv\n");
1407 * Decompress A/C and protocol compression here.
1409 p = skb_put(skb, 2);
1410 p[0] = PPP_ALLSTATIONS;
1412 if (*data == PPP_ALLSTATIONS) {
1416 if ((*data & 1) != 0) {
1417 p = skb_put(skb, 1);
1421 /* copy frame to socket buffer */
1422 p = skb_put(skb, count);
1423 memcpy(p,data,count);
1426 * Check if we've overflowed the MRU
1428 if (skb->len >= ppp->mru + PPP_HDRLEN + 2
1429 || skb_tailroom(skb) <= 0) {
1430 ++ppp->estats.rx_length_errors;
1431 if (ppp->flags & SC_DEBUG)
1432 printk(KERN_DEBUG "rcv frame too long: "
1433 "len=%d mru=%d hroom=%d troom=%d\n",
1434 skb->len, ppp->mru, skb_headroom(skb),
1437 if (!ppp_receive_frame(ppp, skb)) {
1439 ppp_receive_error(ppp);
1443 /* Reset for the next frame */
1450 while (count-- > 0) {
1452 * Collect the character and error condition for the character.
1453 * Set the toss flag for the first character error.
1463 ++ppp->estats.rx_fifo_errors;
1467 ++ppp->estats.rx_frame_errors;
1475 * Set the flags for d7 being 0/1 and parity being
1476 * even/odd so that the normal processing would have
1477 * all flags set at the end of the session. A
1478 * missing flag bit indicates an error condition.
1481 #ifdef CHECK_CHARACTERS
1483 ppp->flags |= SC_RCV_B7_1;
1485 ppp->flags |= SC_RCV_B7_0;
1487 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1488 ppp->flags |= SC_RCV_ODDP;
1490 ppp->flags |= SC_RCV_EVNP;
1493 if (chr == PPP_FLAG) {
1495 * FLAG. This is the end of the block. If the block
1496 * ends with ESC FLAG, then the block is to be ignored.
1501 * Process the frame if it was received correctly.
1502 * If there was an error, let the VJ decompressor know.
1503 * There are 4 cases here:
1504 * skb != NULL, toss != 0: error in frame
1505 * skb != NULL, toss == 0: frame ok
1506 * skb == NULL, toss != 0: very first frame,
1507 * error on 1st char, or alloc_skb failed
1508 * skb == NULL, toss == 0: empty frame (~~)
1510 if (ppp->toss || !ppp_receive_frame(ppp, skb)) {
1511 if (ppp->toss && (ppp->flags & SC_DEBUG))
1513 "ppp: tossing frame (%x)\n",
1517 if (!(ppp->toss == 0xE0 || ppp->toss == 0x80))
1518 ++ppp->stats.ppp_ierrors;
1519 ppp_receive_error(ppp);
1522 * Reset for the next frame.
1525 ppp->rfcs = PPP_INITFCS;
1531 /* If we're tossing, look no further. */
1535 /* If this is a control char to be ignored, do so */
1536 if (in_rmap(ppp, chr))
1540 * Modify the next character if preceded by escape.
1541 * The escape character (0x7d) could be an escaped
1542 * 0x5d, if it follows an escape :-)
1547 } else if (chr == PPP_ESCAPE) {
1548 ppp->escape = PPP_TRANS;
1553 * Allocate an skbuff on the first character received.
1554 * The 128 is room for VJ header expansion and FCS.
1557 skb = dev_alloc_skb(ppp->mru + 128 + PPP_HDRLEN);
1559 if (ppp->flags & SC_DEBUG)
1560 printk(KERN_DEBUG "couldn't "
1561 "alloc skb for recv\n");
1569 * Decompress A/C and protocol compression here.
1571 if (skb->len == 0 && chr != PPP_ALLSTATIONS) {
1572 p = skb_put(skb, 2);
1573 p[0] = PPP_ALLSTATIONS;
1576 if (skb->len == 2 && (chr & 1) != 0) {
1577 p = skb_put(skb, 1);
1582 * Check if we've overflowed the MRU
1584 if (skb->len >= ppp->mru + PPP_HDRLEN + 2
1585 || skb_tailroom(skb) <= 0) {
1586 ++ppp->estats.rx_length_errors;
1588 if (ppp->flags & SC_DEBUG)
1589 printk(KERN_DEBUG "rcv frame too long: "
1590 "len=%d mru=%d hroom=%d troom=%d\n",
1591 skb->len, ppp->mru, skb_headroom(skb),
1597 * Store the character and update the FCS.
1599 p = skb_put(skb, 1);
1601 ppp->rfcs = PPP_FCS(ppp->rfcs, chr);
1606 /*************************************************************
1607 * PPP NETWORK INTERFACE SUPPORT
1608 * The following code implements the PPP network
1609 * interface device and handles those parts of
1610 * the PPP processing which are independent of the
1611 * type of hardware link being used, including
1612 * VJ and packet compression.
1613 *************************************************************/
1616 * Network device driver callback routines
1619 static int ppp_init_dev(struct device *dev);
1620 static int ppp_dev_open(struct device *);
1621 static int ppp_dev_ioctl(struct device *dev, struct ifreq *ifr, int cmd);
1622 static int ppp_dev_close(struct device *);
1623 static int ppp_dev_xmit(struct sk_buff *, struct device *);
1624 static struct net_device_stats *ppp_dev_stats (struct device *);
1626 #if LINUX_VERSION_CODE < VERSION(2,1,15)
1627 static int ppp_dev_header(struct sk_buff *, struct device *, __u16,
1628 void *, void *, unsigned int);
1629 static int ppp_dev_rebuild(void *eth, struct device *dev,
1630 unsigned long raddr, struct sk_buff *skb);
1634 * Information for the protocol decoder
1637 typedef int (*pfn_proto) (struct ppp *, struct sk_buff *);
1639 typedef struct ppp_proto_struct {
1644 static int rcv_proto_ip (struct ppp *, struct sk_buff *);
1645 static int rcv_proto_ipv6 (struct ppp *, struct sk_buff *);
1646 static int rcv_proto_ipx (struct ppp *, struct sk_buff *);
1647 static int rcv_proto_at (struct ppp *, struct sk_buff *);
1648 static int rcv_proto_vjc_comp (struct ppp *, struct sk_buff *);
1649 static int rcv_proto_vjc_uncomp (struct ppp *, struct sk_buff *);
1650 static int rcv_proto_ccp (struct ppp *, struct sk_buff *);
1651 static int rcv_proto_unknown (struct ppp *, struct sk_buff *);
1654 ppp_proto_type proto_list[] = {
1655 { PPP_IP, rcv_proto_ip },
1656 { PPP_IPV6, rcv_proto_ipv6 },
1657 { PPP_IPX, rcv_proto_ipx },
1658 { PPP_AT, rcv_proto_at },
1659 { PPP_VJC_COMP, rcv_proto_vjc_comp },
1660 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
1661 { PPP_CCP, rcv_proto_ccp },
1662 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
1666 * Called when the PPP network interface device is actually created.
1669 ppp_init_dev (struct device *dev)
1671 dev->hard_header_len = PPP_HDRLEN;
1672 #if LINUX_VERSION_CODE < VERSION(2,1,15)
1673 dev->hard_header = ppp_dev_header;
1674 dev->rebuild_header = ppp_dev_rebuild;
1679 dev->hard_start_xmit = ppp_dev_xmit;
1680 dev->open = ppp_dev_open;
1681 dev->stop = ppp_dev_close;
1682 dev->get_stats = ppp_dev_stats;
1683 dev->do_ioctl = ppp_dev_ioctl;
1685 dev->tx_queue_len = 10;
1686 dev->type = ARPHRD_PPP;
1688 #if LINUX_VERSION_CODE < VERSION(2,1,20)
1692 for (indx = 0; indx < DEV_NUMBUFFS; indx++)
1693 skb_queue_head_init (&dev->buffs[indx]);
1696 dev_init_buffers(dev);
1699 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1705 * Callback from the network layer when the device goes up.
1709 ppp_dev_open (struct device *dev)
1711 struct ppp *ppp = dev2ppp(dev);
1713 if (!ppp->inuse || ppp2tty(ppp) == NULL) {
1714 printk(KERN_ERR "ppp: %s not active\n", dev->name);
1724 * Callback from the network layer when the ppp device goes down.
1728 ppp_dev_close (struct device *dev)
1730 struct ppp *ppp = dev2ppp (dev);
1732 CHECK_PPP_MAGIC(ppp);
1740 get_vj_stats(struct vjstat *vj, struct slcompress *slc)
1742 vj->vjs_packets = slc->sls_o_compressed + slc->sls_o_uncompressed;
1743 vj->vjs_compressed = slc->sls_o_compressed;
1744 vj->vjs_searches = slc->sls_o_searches;
1745 vj->vjs_misses = slc->sls_o_misses;
1746 vj->vjs_errorin = slc->sls_i_error;
1747 vj->vjs_tossed = slc->sls_i_tossed;
1748 vj->vjs_uncompressedin = slc->sls_i_uncompressed;
1749 vj->vjs_compressedin = slc->sls_i_compressed;
1753 * Callback from the network layer to process the sockioctl functions.
1756 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
1758 struct ppp *ppp = dev2ppp(dev);
1761 struct ppp_stats stats;
1762 struct ppp_comp_stats cstats;
1766 CHECK_PPP_MAGIC(ppp);
1768 memset(&u, 0, sizeof(u));
1771 u.stats.p = ppp->stats;
1772 if (ppp->slcomp != NULL)
1773 get_vj_stats(&u.stats.vj, ppp->slcomp);
1774 nb = sizeof(u.stats);
1777 case SIOCGPPPCSTATS:
1778 if (ppp->sc_xc_state != NULL)
1779 (*ppp->sc_xcomp->comp_stat)
1780 (ppp->sc_xc_state, &u.cstats.c);
1781 if (ppp->sc_rc_state != NULL)
1782 (*ppp->sc_rcomp->decomp_stat)
1783 (ppp->sc_rc_state, &u.cstats.d);
1784 nb = sizeof(u.cstats);
1788 strcpy(u.vers, szVersion);
1789 nb = strlen(u.vers) + 1;
1796 if (COPY_TO_USER((void *) ifr->ifr_ifru.ifru_data, &u, nb))
1802 * Process the generic PPP ioctls, i.e. those which are not specific
1803 * to any particular type of hardware link.
1806 ppp_ioctl(struct ppp *ppp, unsigned int param2, unsigned long param3)
1808 register int temp_i = 0, oldflags;
1809 int error = -EFAULT;
1810 unsigned long flags;
1811 struct ppp_idle cur_ddinfo;
1817 * The user must have an euid of root to do these requests.
1827 if (GET_USER(temp_i, (int *) param3))
1829 if (temp_i < PPP_MRU)
1832 if (ppp->flags & SC_DEBUG)
1834 "ppp_ioctl: set mru to %x\n", temp_i);
1840 * Fetch the current flags
1842 temp_i = ppp->flags & SC_MASK;
1843 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
1844 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
1845 SC_RCV_ODDP | SC_RCV_EVNP;
1847 if (PUT_USER(temp_i, (int *) param3))
1854 * Set the flags for the various options
1856 if (GET_USER(temp_i, (int *) param3))
1859 if (ppp->flags & ~temp_i & SC_CCP_OPEN)
1860 ppp_ccp_closed(ppp);
1864 oldflags = ppp->flags;
1865 temp_i = (temp_i & SC_MASK) | (oldflags & ~SC_MASK);
1866 ppp->flags = temp_i;
1867 restore_flags(flags);
1869 if ((oldflags | temp_i) & SC_DEBUG)
1871 "ppp_ioctl: set flags to %x\n", temp_i);
1875 case PPPIOCSCOMPRESS:
1877 * Set the compression mode
1879 error = ppp_set_compression
1880 (ppp, (struct ppp_option_data *) param3);
1885 * Obtain the unit number for this device.
1887 if (PUT_USER(ppp->line, (int *) param3))
1889 if (ppp->flags & SC_DEBUG)
1891 "ppp_ioctl: get unit: %d\n", ppp->line);
1897 * Set the debug level
1899 if (GET_USER(temp_i, (int *) param3))
1901 temp_i = (temp_i & 0x1F) << 16;
1903 if ((ppp->flags | temp_i) & SC_DEBUG)
1905 "ppp_ioctl: set dbg flags to %x\n", temp_i);
1909 ppp->flags = (ppp->flags & ~0x1F0000) | temp_i;
1910 restore_flags(flags);
1916 * Get the debug level
1918 temp_i = (ppp->flags >> 16) & 0x1F;
1919 if (PUT_USER(temp_i, (int *) param3))
1926 * Get the times since the last send/receive frame operation
1928 /* change absolute times to relative times. */
1929 cur_ddinfo.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
1930 cur_ddinfo.recv_idle = (jiffies - ppp->last_recv) / HZ;
1931 if (COPY_TO_USER((void *) param3, &cur_ddinfo,
1932 sizeof (cur_ddinfo)))
1939 * Set the maximum VJ header compression slot number.
1941 if (GET_USER(temp_i, (int *) param3))
1944 if (temp_i < 2 || temp_i > 255)
1947 if (ppp->flags & SC_DEBUG)
1948 printk(KERN_INFO "ppp_ioctl: set maxcid to %d\n",
1950 if (ppp->slcomp != NULL)
1951 slhc_free(ppp->slcomp);
1952 ppp->slcomp = slhc_init(16, temp_i);
1955 if (ppp->slcomp == NULL) {
1956 printk(KERN_ERR "ppp: no memory for VJ compression\n");
1964 if (COPY_FROM_USER(&npi, (void *) param3, sizeof(npi)))
1967 switch (npi.protocol) {
1969 npi.protocol = NP_IP;
1972 npi.protocol = NP_IPX;
1975 npi.protocol = NP_AT;
1978 if (ppp->flags & SC_DEBUG)
1979 printk(KERN_DEBUG "pppioc[gs]npmode: "
1980 "invalid proto %d\n", npi.protocol);
1985 if (param2 == PPPIOCGNPMODE) {
1986 npi.mode = ppp->sc_npmode[npi.protocol];
1987 if (COPY_TO_USER((void *) param3, &npi, sizeof(npi)))
1990 ppp->sc_npmode[npi.protocol] = npi.mode;
1991 if (ppp->flags & SC_DEBUG)
1992 printk(KERN_DEBUG "ppp: set np %d to %d\n",
1993 npi.protocol, npi.mode);
2001 * All other ioctl() events will come here.
2003 if (ppp->flags & SC_DEBUG)
2005 "ppp_ioctl: invalid ioctl: %x, addr %lx\n",
2008 error = -ENOIOCTLCMD;
2016 * Process the set-compression ioctl.
2019 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2021 struct compressor *cp;
2023 unsigned long flags;
2025 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2026 struct ppp_option_data data;
2029 * Fetch the compression parameters
2032 if (COPY_FROM_USER(&data, odp, sizeof (data)))
2037 if ((unsigned) nb >= CCP_MAX_OPTION_LENGTH)
2038 nb = CCP_MAX_OPTION_LENGTH;
2040 if (COPY_FROM_USER(ccp_option, ptr, nb))
2044 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2049 ppp->flags &= ~(data.transmit? SC_COMP_RUN: SC_DECOMP_RUN);
2050 restore_flags(flags);
2052 cp = find_compressor (ccp_option[0]);
2053 #if defined(CONFIG_KMOD) || defined(CONFIG_KERNELD)
2056 sprintf(modname, "ppp-compress-%d", ccp_option[0]);
2057 request_module(modname);
2058 cp = find_compressor(ccp_option[0]);
2060 #endif /* CONFIG_KMOD */
2063 if (ppp->flags & SC_DEBUG)
2065 "%s: no compressor for [%x %x %x], %x\n",
2066 ppp->name, ccp_option[0], ccp_option[1],
2068 goto out; /* compressor not loaded */
2072 * Found a handler for the protocol - try to allocate
2073 * a compressor or decompressor.
2076 if (data.transmit) {
2077 if (ppp->sc_xc_state != NULL)
2078 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2079 ppp->sc_xc_state = NULL;
2082 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2083 if (ppp->sc_xc_state == NULL) {
2084 if (ppp->flags & SC_DEBUG)
2085 printk(KERN_DEBUG "%s: comp_alloc failed\n",
2090 if (ppp->sc_rc_state != NULL)
2091 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2092 ppp->sc_rc_state = NULL;
2095 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2096 if (ppp->sc_rc_state == NULL) {
2097 if (ppp->flags & SC_DEBUG)
2098 printk(KERN_DEBUG "%s: decomp_alloc failed\n",
2108 * Handle a CCP packet.
2110 * The CCP packet is passed along to the pppd process just like any
2111 * other PPP frame. The difference is that some processing needs to be
2112 * immediate or the compressors will become confused on the peer.
2115 static void ppp_proto_ccp(struct ppp *ppp, __u8 *dp, int len, int rcvd)
2117 int slen = CCP_LENGTH(dp);
2118 __u8 *opt = dp + CCP_HDRLEN;
2119 int opt_len = slen - CCP_HDRLEN;
2120 unsigned long flags;
2125 if (ppp->flags & SC_DEBUG)
2126 printk(KERN_DEBUG "ppp_proto_ccp rcvd=%d code=%x flags=%x\n",
2127 rcvd, CCP_CODE(dp), ppp->flags);
2129 switch (CCP_CODE(dp)) {
2134 * CCP must be going down - disable compression
2136 if (ppp->flags & SC_CCP_UP) {
2138 ppp->flags &= ~(SC_CCP_UP |
2145 if ((ppp->flags & SC_CCP_OPEN) == 0)
2147 if (ppp->flags & SC_CCP_UP)
2149 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
2151 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
2155 * we're agreeing to send compressed packets.
2157 if (ppp->sc_xc_state == NULL)
2160 if ((*ppp->sc_xcomp->comp_init)
2163 ppp->line, 0, ppp->flags & SC_DEBUG)) {
2164 if (ppp->flags & SC_DEBUG)
2165 printk(KERN_DEBUG "%s: comp running\n",
2168 ppp->flags |= SC_COMP_RUN;
2174 * peer is agreeing to send compressed packets.
2176 if (ppp->sc_rc_state == NULL)
2179 if ((*ppp->sc_rcomp->decomp_init)
2182 ppp->line, 0, ppp->mru, ppp->flags & SC_DEBUG)) {
2183 if (ppp->flags & SC_DEBUG)
2184 printk(KERN_DEBUG "%s: decomp running\n",
2187 ppp->flags |= SC_DECOMP_RUN;
2188 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
2194 * CCP Reset-ack resets compressors and decompressors
2195 * as it passes through.
2197 if ((ppp->flags & SC_CCP_UP) == 0)
2201 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN)) {
2202 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
2203 if (ppp->flags & SC_DEBUG)
2204 printk(KERN_DEBUG "%s: comp reset\n",
2208 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
2209 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
2210 if (ppp->flags & SC_DEBUG)
2211 printk(KERN_DEBUG "%s: decomp reset\n",
2214 ppp->flags &= ~SC_DC_ERROR;
2219 restore_flags(flags);
2223 * CCP is down; free (de)compressor state if necessary.
2227 ppp_ccp_closed(struct ppp *ppp)
2229 unsigned long flags;
2233 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
2234 restore_flags(flags);
2235 if (ppp->flags & SC_DEBUG)
2236 printk(KERN_DEBUG "%s: ccp closed\n", ppp->name);
2237 if (ppp->sc_xc_state) {
2238 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
2239 ppp->sc_xc_state = NULL;
2242 if (ppp->sc_rc_state) {
2243 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
2244 ppp->sc_rc_state = NULL;
2248 /*************************************************************
2249 * RECEIVE-SIDE ROUTINES
2250 *************************************************************/
2253 * On entry, a received frame is in skb.
2254 * Check it and dispose as appropriate.
2257 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb)
2263 struct sk_buff *new_skb;
2264 ppp_proto_type *proto_ptr;
2267 * An empty frame is ignored. This occurs if the FLAG sequence
2268 * precedes and follows each frame.
2272 if (skb->len == 0) {
2280 * Generate an error if the frame is too small.
2282 if (count < PPP_HDRLEN + 2) {
2283 if (ppp->flags & SC_DEBUG)
2285 "ppp: got runt ppp frame, %d chars\n", count);
2286 ++ppp->estats.rx_length_errors;
2290 if ( !(ppp->flags & SC_SYNC) ) {
2292 * Verify the FCS of the frame and discard the FCS characters
2293 * from the end of the buffer.
2295 if (ppp->rfcs != PPP_GOODFCS) {
2296 if (ppp->flags & SC_DEBUG) {
2298 "ppp: frame with bad fcs, length = %d\n",
2300 ppp_print_buffer("bad frame", data, count);
2302 ++ppp->estats.rx_crc_errors;
2305 count -= 2; /* ignore the fcs characters */
2306 skb_trim(skb, count);
2310 * Process the active decompressor.
2312 if (ppp->sc_rc_state != NULL &&
2313 (ppp->flags & SC_DECOMP_RUN) &&
2314 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
2315 if (PPP_PROTOCOL(data) == PPP_COMP) {
2317 * If the frame is compressed then decompress it.
2319 new_skb = dev_alloc_skb(ppp->mru + 128 + PPP_HDRLEN);
2320 if (new_skb == NULL) {
2321 printk(KERN_ERR "ppp_recv_frame: no memory\n");
2322 new_count = DECOMP_ERROR;
2324 LIBERATE_SKB(new_skb);
2325 new_count = (*ppp->sc_rcomp->decompress)
2326 (ppp->sc_rc_state, data, count,
2327 new_skb->data, ppp->mru + PPP_HDRLEN);
2329 if (new_count > 0) {
2330 /* Frame was decompressed OK */
2334 data = skb_put(skb, count);
2338 * On a decompression error, we pass the
2339 * compressed frame up to pppd as an
2342 if (ppp->flags & SC_DEBUG)
2343 printk(KERN_INFO "%s: decomp err %d\n",
2344 ppp->name, new_count);
2347 if (ppp->slcomp != 0)
2348 slhc_toss(ppp->slcomp);
2349 ++ppp->stats.ppp_ierrors;
2350 if (new_count == DECOMP_FATALERROR) {
2351 ppp->flags |= SC_DC_FERROR;
2353 ppp->flags |= SC_DC_ERROR;
2360 * The frame is not compressed. Pass it to the
2361 * decompression code so it can update its
2362 * dictionary if necessary.
2364 (*ppp->sc_rcomp->incomp)(ppp->sc_rc_state,
2368 else if (PPP_PROTOCOL(data) == PPP_COMP && (ppp->flags & SC_DEBUG))
2369 printk(KERN_INFO "%s: not decomp, rc_state=%p flags=%x\n",
2370 ppp->name, ppp->sc_rc_state, ppp->flags);
2373 * Count the frame and print it
2375 ++ppp->stats.ppp_ipackets;
2376 ppp->stats.ppp_ioctects += count;
2377 if (ppp->flags & SC_LOG_INPKT)
2378 ppp_print_buffer ("receive frame", data, count);
2381 * Find the procedure to handle this protocol.
2382 * The last one is marked as protocol 0 which is the 'catch-all'
2383 * to feed it to the pppd daemon.
2385 proto = PPP_PROTOCOL(data);
2386 proto_ptr = proto_list;
2387 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
2391 * Update the appropriate statistic counter.
2393 if (!(*proto_ptr->func)(ppp, skb)) {
2395 ++ppp->stats.ppp_discards;
2402 * An input error has been detected, so we need to inform
2403 * the VJ decompressor.
2406 ppp_receive_error(struct ppp *ppp)
2410 if (ppp->slcomp != 0)
2411 slhc_toss(ppp->slcomp);
2415 * Put the input frame into the networking system for the indicated protocol
2418 ppp_rcv_rx(struct ppp *ppp, __u16 proto, struct sk_buff *skb)
2422 * Fill in a few fields of the skb and give it to netif_rx().
2424 skb->dev = ppp2dev(ppp); /* We are the device */
2425 skb->protocol = htons(proto);
2426 skb_pull(skb, PPP_HDRLEN); /* pull off ppp header */
2427 skb->mac.raw = skb->data;
2428 ppp->last_recv = jiffies;
2434 * Process the receipt of an IP frame
2437 rcv_proto_ip(struct ppp *ppp, struct sk_buff *skb)
2440 if ((ppp2dev(ppp)->flags & IFF_UP) && (skb->len > 0)
2441 && ppp->sc_npmode[NP_IP] == NPMODE_PASS)
2442 return ppp_rcv_rx(ppp, ETH_P_IP, skb);
2447 * Process the receipt of an IPv6 frame
2450 rcv_proto_ipv6(struct ppp *ppp, struct sk_buff *skb)
2453 if ((ppp2dev(ppp)->flags & IFF_UP) && (skb->len > 0)
2454 && ppp->sc_npmode[NP_IPV6] == NPMODE_PASS)
2455 return ppp_rcv_rx(ppp, ETH_P_IPV6, skb);
2460 * Process the receipt of an IPX frame
2463 rcv_proto_ipx(struct ppp *ppp, struct sk_buff *skb)
2466 if (((ppp2dev(ppp)->flags & IFF_UP) != 0) && (skb->len > 0)
2467 && ppp->sc_npmode[NP_IPX] == NPMODE_PASS)
2468 return ppp_rcv_rx(ppp, ETH_P_IPX, skb);
2473 * Process the receipt of an Appletalk frame
2476 rcv_proto_at(struct ppp *ppp, struct sk_buff *skb)
2479 if ((ppp2dev(ppp)->flags & IFF_UP) && (skb->len > 0)
2480 && ppp->sc_npmode[NP_AT] == NPMODE_PASS)
2481 return ppp_rcv_rx(ppp, ETH_P_PPPTALK, skb);
2486 * Process the receipt of an VJ Compressed frame
2489 rcv_proto_vjc_comp(struct ppp *ppp, struct sk_buff *skb)
2494 if ((ppp->flags & SC_REJ_COMP_TCP) || ppp->slcomp == NULL)
2496 new_count = slhc_uncompress(ppp->slcomp, skb->data + PPP_HDRLEN,
2497 skb->len - PPP_HDRLEN);
2498 if (new_count <= 0) {
2499 if (ppp->flags & SC_DEBUG)
2501 "ppp: error in VJ decompression\n");
2504 new_count += PPP_HDRLEN;
2505 if (new_count > skb->len)
2506 skb_put(skb, new_count - skb->len);
2508 skb_trim(skb, new_count);
2509 return rcv_proto_ip(ppp, skb);
2513 * Process the receipt of an VJ Un-compressed frame
2516 rcv_proto_vjc_uncomp(struct ppp *ppp, struct sk_buff *skb)
2519 if ((ppp->flags & SC_REJ_COMP_TCP) || ppp->slcomp == NULL)
2521 if (slhc_remember(ppp->slcomp, skb->data + PPP_HDRLEN,
2522 skb->len - PPP_HDRLEN) <= 0) {
2523 if (ppp->flags & SC_DEBUG)
2524 printk(KERN_NOTICE "ppp: error in VJ memorizing\n");
2527 return rcv_proto_ip(ppp, skb);
2531 rcv_proto_ccp(struct ppp *ppp, struct sk_buff *skb)
2534 ppp_proto_ccp (ppp, skb->data + PPP_HDRLEN, skb->len - PPP_HDRLEN, 1);
2535 return rcv_proto_unknown(ppp, skb);
2539 * Receive all unclassified protocols.
2542 rcv_proto_unknown(struct ppp *ppp, struct sk_buff *skb)
2547 * Limit queue length by dropping old frames.
2549 skb_queue_tail(&ppp->rcv_q, skb);
2550 while (ppp->rcv_q.qlen > PPP_MAX_RCV_QLEN) {
2551 struct sk_buff *skb = skb_dequeue(&ppp->rcv_q);
2556 wake_up_interruptible (&ppp->read_wait);
2557 if (ppp->tty->fasync != NULL)
2558 kill_fasync (ppp->tty->fasync, SIGIO);
2563 /*************************************************************
2564 * TRANSMIT-SIDE ROUTINES
2565 *************************************************************/
2567 /* local function to store a value into the LQR frame */
2568 extern inline __u8 * store_long (register __u8 *p, register int value) {
2569 *p++ = (__u8) (value >> 24);
2570 *p++ = (__u8) (value >> 16);
2571 *p++ = (__u8) (value >> 8);
2572 *p++ = (__u8) value;
2577 * Compress and send an frame to the peer.
2578 * Should be called with xmit_busy == 1, having been set by the caller.
2579 * That is, we use xmit_busy as a lock to prevent reentry of this
2583 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
2595 /* dump the buffer */
2596 if (ppp->flags & SC_LOG_OUTPKT)
2597 ppp_print_buffer ("write frame", data, count);
2600 * Handle various types of protocol-specific compression
2601 * and other processing, including:
2602 * - VJ TCP header compression
2603 * - updating LQR packets
2604 * - updating CCP state on CCP packets
2606 proto = PPP_PROTOCOL(data);
2609 if ((ppp->flags & SC_COMP_TCP) && ppp->slcomp != NULL)
2610 skb = ppp_vj_compress(ppp, skb);
2615 * Update the LQR frame with the current MIB information.
2616 * This way the information is accurate and up-to-date.
2620 p = data + 40; /* Point to last two items. */
2621 p = store_long(p, ppp->stats.ppp_opackets + 1);
2622 p = store_long(p, ppp->stats.ppp_ooctects + count);
2623 ++ppp->stats.ppp_olqrs;
2628 * Outbound compression control frames
2630 ppp_proto_ccp(ppp, data + PPP_HDRLEN, count - PPP_HDRLEN, 0);
2637 * Compress the whole frame if possible.
2639 if (((ppp->flags & SC_COMP_RUN) != 0) &&
2640 (ppp->sc_xc_state != (void *) 0) &&
2641 (proto != PPP_LCP) &&
2642 (proto != PPP_CCP)) {
2643 struct sk_buff *new_skb;
2646 /* Allocate an skb for the compressed frame. */
2647 new_skb = alloc_skb(ppp->mtu + PPP_HDRLEN, GFP_ATOMIC);
2648 if (new_skb == NULL) {
2649 printk(KERN_ERR "ppp_send_frame: no memory\n");
2654 LIBERATE_SKB(new_skb);
2656 /* Compress the frame. */
2657 new_count = (*ppp->sc_xcomp->compress)
2658 (ppp->sc_xc_state, data, new_skb->data,
2659 count, ppp->mtu + PPP_HDRLEN);
2661 /* Did it compress? */
2662 if (new_count > 0 && (ppp->flags & SC_CCP_UP)) {
2663 skb_put(new_skb, new_count);
2668 * The frame could not be compressed, or it could not
2669 * be sent in compressed form because CCP is down.
2678 if ( ppp->flags & SC_SYNC )
2679 ret = ppp_sync_send(ppp, skb);
2681 ret = ppp_async_send(ppp, skb);
2683 /* we can release the lock */
2685 } else if (ret < 0) {
2686 /* can't happen, since the caller got the xmit_busy lock */
2687 printk(KERN_ERR "ppp: ppp_async_send didn't accept pkt\n");
2692 * Apply VJ TCP header compression to a packet.
2694 static struct sk_buff *
2695 ppp_vj_compress(struct ppp *ppp, struct sk_buff *skb)
2697 __u8 *orig_data, *data;
2698 struct sk_buff *new_skb;
2701 new_skb = alloc_skb(skb->len, GFP_ATOMIC);
2702 if (new_skb == NULL) {
2703 printk(KERN_ERR "ppp: no memory for vj compression\n");
2706 LIBERATE_SKB(new_skb);
2708 orig_data = data = skb->data + PPP_HDRLEN;
2709 len = slhc_compress(ppp->slcomp, data, skb->len - PPP_HDRLEN,
2710 new_skb->data + PPP_HDRLEN, &data,
2711 (ppp->flags & SC_NO_TCP_CCID) == 0);
2713 if (data == orig_data) {
2714 /* Couldn't compress the data */
2719 /* The data has been changed */
2720 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2721 proto = PPP_VJC_COMP;
2722 data[0] ^= SL_TYPE_COMPRESSED_TCP;
2724 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2725 proto = PPP_VJC_UNCOMP;
2728 data[0] = orig_data[0];
2731 data = skb_put(new_skb, len + PPP_HDRLEN);
2732 data[0] = PPP_ALLSTATIONS;
2742 ppp_send_frames(struct ppp *ppp)
2744 struct sk_buff *skb;
2746 while (!test_and_set_bit(0, &ppp->xmit_busy)) {
2747 skb = skb_dequeue(&ppp->xmt_q);
2752 ppp_send_frame(ppp, skb);
2754 if (!ppp->xmit_busy && ppp->dev.tbusy) {
2761 * Called from the hardware (tty) layer when it can accept
2765 ppp_output_wakeup(struct ppp *ppp)
2769 if (!ppp->xmit_busy) {
2770 printk(KERN_ERR "ppp_output_wakeup called but xmit_busy==0\n");
2774 ppp_send_frames(ppp);
2778 * Send a control frame (from pppd).
2781 ppp_send_ctrl(struct ppp *ppp, struct sk_buff *skb)
2786 * Put the packet on the queue, then send as many as we can.
2788 skb_queue_tail(&ppp->xmt_q, skb);
2789 ppp_send_frames(ppp);
2793 /*************************************************************
2795 * This routine accepts requests from the network layer
2796 * and attempts to deliver the packets.
2797 *************************************************************/
2799 * Send a frame to the peer.
2800 * Returns 1 iff the frame was not accepted.
2803 ppp_dev_xmit(struct sk_buff *skb, struct device *dev)
2805 struct ppp *ppp = dev2ppp(dev);
2806 struct tty_struct *tty = ppp2tty(ppp);
2811 /* just a little sanity check. */
2814 if (skb->data == NULL) {
2820 * Avoid timing problem should tty hangup while data is
2821 * queued to be sent.
2829 * Validate the tty interface
2832 if (ppp->flags & SC_DEBUG)
2834 "ppp_dev_xmit: %s not connected to a TTY!\n",
2841 * Work out the appropriate network-protocol mode for this packet.
2843 npmode = NPMODE_PASS; /* default */
2844 switch (ntohs(skb->protocol)) {
2847 npmode = ppp->sc_npmode[NP_IP];
2851 npmode = ppp->sc_npmode[NP_IPV6];
2855 npmode = ppp->sc_npmode[NP_IPX];
2860 npmode = ppp->sc_npmode[NP_AT];
2863 if (ppp->flags & SC_DEBUG)
2864 printk(KERN_INFO "%s: packet for unknown proto %x\n",
2865 ppp->name, ntohs(skb->protocol));
2871 * Drop, accept or reject the packet depending on the mode.
2879 * We may not send the packet now, so drop it.
2880 * XXX It would be nice to be able to return it to the
2881 * network system to be queued and retransmitted later.
2883 if (ppp->flags & SC_DEBUG)
2884 printk(KERN_DEBUG "%s: returning frame\n", ppp->name);
2890 if (ppp->flags & SC_DEBUG)
2892 "ppp_dev_xmit: dropping (npmode = %d) on %s\n",
2899 * The dev->tbusy field acts as a lock to allow only
2900 * one packet to be processed at a time. If we can't
2901 * get the lock, try again later.
2902 * We deliberately queue as little as possible inside
2903 * the ppp driver in order to minimize the latency
2904 * for high-priority packets.
2906 if (test_and_set_bit(0, &ppp->xmit_busy)) {
2907 dev->tbusy = 1; /* can't take it now */
2913 * Put the 4-byte PPP header on the packet.
2914 * If there isn't room for it, we have to copy the packet.
2916 if (skb_headroom(skb) < PPP_HDRLEN) {
2917 struct sk_buff *new_skb;
2919 new_skb = alloc_skb(skb->len + PPP_HDRLEN, GFP_ATOMIC);
2920 if (new_skb == NULL) {
2921 printk(KERN_ERR "%s: skb hdr alloc failed\n",
2925 ppp_send_frames(ppp);
2928 LIBERATE_SKB(new_skb);
2929 skb_reserve(new_skb, PPP_HDRLEN);
2930 memcpy(skb_put(new_skb, skb->len), skb->data, skb->len);
2935 hdr = skb_push(skb, PPP_HDRLEN);
2936 hdr[0] = PPP_ALLSTATIONS;
2938 hdr[2] = proto >> 8;
2941 ppp_send_frame(ppp, skb);
2942 if (!ppp->xmit_busy)
2943 ppp_send_frames(ppp);
2947 #if LINUX_VERSION_CODE < VERSION(2,1,15)
2949 * Null hard_header and header_rebuild routines.
2951 static int ppp_dev_header(struct sk_buff *skb, struct device *dev,
2952 unsigned short type, void *daddr,
2953 void *saddr, unsigned int len)
2958 static int ppp_dev_rebuild(void *eth, struct device *dev,
2959 unsigned long raddr, struct sk_buff *skb)
2963 #endif /* < 2.1.15 */
2966 * Generate the statistic information for the /proc/net/dev listing.
2968 static struct net_device_stats *
2969 ppp_dev_stats (struct device *dev)
2971 struct ppp *ppp = dev2ppp (dev);
2973 ppp->estats.rx_packets = ppp->stats.ppp_ipackets;
2974 ppp->estats.rx_errors = ppp->stats.ppp_ierrors;
2975 ppp->estats.tx_packets = ppp->stats.ppp_opackets;
2976 ppp->estats.tx_errors = ppp->stats.ppp_oerrors;
2977 #if LINUX_VERSION_CODE >= VERSION(2,1,25)
2978 ppp->estats.rx_bytes = ppp->stats.ppp_ibytes;
2979 ppp->estats.tx_bytes = ppp->stats.ppp_obytes;
2982 return &ppp->estats;
2985 /*************************************************************
2987 * Miscellany called by various functions above.
2988 *************************************************************/
2990 /* Locate the previous instance of the PPP channel */
2992 ppp_find(int pid_value)
2996 /* try to find the device which this pid is already using */
2997 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
2998 if (ppp->inuse && ppp->sc_xfer == pid_value) {
3006 /* allocate or create a PPP channel */
3015 /* try to find an free device */
3016 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3017 if (!test_and_set_bit(0, &ppp->inuse)) {
3019 if (dev->flags & IFF_UP) {
3020 clear_bit(0, &ppp->inuse);
3023 /* Reregister device */
3024 unregister_netdev(dev);
3025 if (register_netdev(dev) == 0)
3027 printk(KERN_DEBUG "could not reregister ppp device\n");
3028 /* leave inuse set in this case */
3033 * There are no available units, so make a new one.
3035 ppp = (struct ppp *) kmalloc(sizeof(struct ppp), GFP_KERNEL);
3037 printk(KERN_ERR "ppp: struct ppp allocation failed\n");
3040 memset(ppp, 0, sizeof(*ppp));
3042 /* initialize channel control data */
3043 ppp->magic = PPP_MAGIC;
3046 ppp->read_wait = NULL;
3049 * Make up a suitable name for this device
3052 dev->name = ppp->name;
3053 #if LINUX_VERSION_CODE < VERSION(2,1,31)
3054 if_num = (ppp_list == 0)? 0: ppp_last->line + 1;
3055 sprintf(ppp->name, "ppp%d", if_num);
3057 if_num = dev_alloc_name(dev, "ppp%d");
3060 printk(KERN_ERR "ppp: dev_alloc_name failed (%d)\n", if_num);
3068 dev->init = ppp_init_dev;
3069 dev->name = ppp->name;
3070 dev->priv = (void *) ppp;
3072 /* register device so that we can be ifconfig'd */
3073 /* ppp_init_dev() will be called as a side-effect */
3074 status = register_netdev (dev);
3076 printk(KERN_INFO "registered device %s\n", dev->name);
3079 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3085 /* link this unit into our list */
3089 ppp_last->next = ppp;
3096 * Initialize the generic parts of the ppp structure.
3099 ppp_generic_init(struct ppp *ppp)
3107 skb_queue_head_init(&ppp->xmt_q);
3108 skb_queue_head_init(&ppp->rcv_q);
3110 ppp->last_xmit = jiffies;
3111 ppp->last_recv = jiffies;
3114 /* clear statistics */
3115 memset(&ppp->stats, 0, sizeof (struct pppstat));
3116 memset(&ppp->estats, 0, sizeof(struct net_device_stats));
3118 /* PPP compression data */
3119 ppp->sc_xc_state = NULL;
3120 ppp->sc_rc_state = NULL;
3122 for (indx = 0; indx < NUM_NP; ++indx)
3123 ppp->sc_npmode[indx] = NPMODE_PASS;
3127 * Called to clean up the generic parts of the ppp structure.
3130 ppp_release(struct ppp *ppp)
3132 struct sk_buff *skb;
3134 CHECK_PPP_MAGIC(ppp);
3136 if (ppp->flags & SC_DEBUG)
3137 printk(KERN_DEBUG "%s released\n", ppp->name);
3139 ppp_ccp_closed(ppp);
3141 /* Ensure that the pppd process is not hanging on select()/poll() */
3142 wake_up_interruptible(&ppp->read_wait);
3145 slhc_free(ppp->slcomp);
3149 while ((skb = skb_dequeue(&ppp->rcv_q)) != NULL)
3151 while ((skb = skb_dequeue(&ppp->xmt_q)) != NULL)
3155 if (ppp->dev.tbusy) {
3162 * Utility procedures to print a buffer in hex/ascii
3165 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3167 register __u8 next_ch;
3168 static char hex[] = "0123456789ABCDEF";
3170 while (count-- > 0) {
3172 *out++ = hex[(next_ch >> 4) & 0x0F];
3173 *out++ = hex[next_ch & 0x0F];
3179 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3181 register __u8 next_ch;
3183 while (count-- > 0) {
3186 if (next_ch < 0x20 || next_ch > 0x7e)
3190 if (next_ch == '%') /* printk/syslogd has a bug !! */
3198 ppp_print_buffer (const char *name, const __u8 *buf, int count)
3203 printk(KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3206 memset (line, 32, 44);
3207 ppp_print_hex (line, buf, 8);
3208 ppp_print_char (&line[8 * 3], buf, 8);
3209 printk(KERN_DEBUG "%s\n", line);
3215 memset (line, 32, 44);
3216 ppp_print_hex (line, buf, count);
3217 ppp_print_char (&line[8 * 3], buf, count);
3218 printk(KERN_DEBUG "%s\n", line);
3222 /*************************************************************
3223 * Compressor module interface
3224 *************************************************************/
3226 struct compressor_link {
3227 struct compressor_link *next;
3228 struct compressor *comp;
3231 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3233 static struct compressor *find_compressor (int type)
3235 struct compressor_link *lnk;
3236 unsigned long flags;
3241 lnk = ppp_compressors;
3242 while (lnk != (struct compressor_link *) 0) {
3243 if ((int) (__u8) lnk->comp->compress_proto == type) {
3244 restore_flags(flags);
3250 restore_flags(flags);
3251 return (struct compressor *) 0;
3254 #ifdef CONFIG_MODULES
3255 static int ppp_register_compressor (struct compressor *cp)
3257 struct compressor_link *new;
3258 unsigned long flags;
3260 new = (struct compressor_link *)
3261 kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3263 if (new == (struct compressor_link *) 0)
3269 if (find_compressor (cp->compress_proto)) {
3270 restore_flags(flags);
3275 new->next = ppp_compressors;
3277 ppp_compressors = new;
3279 restore_flags(flags);
3283 static void ppp_unregister_compressor (struct compressor *cp)
3285 struct compressor_link *prev = (struct compressor_link *) 0;
3286 struct compressor_link *lnk;
3287 unsigned long flags;
3292 lnk = ppp_compressors;
3293 while (lnk != (struct compressor_link *) 0) {
3294 if (lnk->comp == cp) {
3296 prev->next = lnk->next;
3298 ppp_compressors = lnk->next;
3305 restore_flags(flags);
3309 /*************************************************************
3310 * Module support routines
3311 *************************************************************/
3319 /* register our line disciplines */
3320 status = ppp_first_time();
3322 printk(KERN_INFO "PPP: ppp_init() failure %d\n", status);
3323 #if LINUX_VERSION_CODE < VERSION(2,1,18)
3325 (void) register_symtab (&ppp_syms);
3332 cleanup_module(void)
3335 struct ppp *ppp, *next_ppp;
3339 * Ensure that the devices are not in operation.
3341 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3342 CHECK_PPP_MAGIC(ppp);
3343 if (ppp->inuse || (ppp->dev.flags & IFF_UP))
3347 printk(KERN_CRIT "PPP: removing despite %d units in use!\n",
3351 * Release the tty registration of the line discipline so that
3352 * ttys can no longer be put into PPP line discipline.
3354 status = tty_register_ldisc (N_PPP, NULL);
3357 "PPP: Unable to unregister ppp line discipline "
3358 "(err = %d)\n", status);
3361 "PPP: ppp line discipline successfully unregistered\n");
3364 * De-register the devices so that there is no problem with them
3366 for (ppp = ppp_list; ppp != 0; ppp = next_ppp) {
3367 next_ppp = ppp->next;
3368 unregister_netdev(&ppp->dev);