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.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>
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>
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>
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>
89 #include <linux/kmod.h>
97 static int ppp_register_compressor (struct compressor
*cp
);
98 static void ppp_unregister_compressor (struct compressor
*cp
);
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
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__); \
145 #define CHECK_PPP(a) do { \
146 CHECK_PPP_MAGIC(ppp); \
148 printk(ppp_warning, __LINE__); \
152 #define CHECK_PPP_VOID() do { \
153 CHECK_PPP_MAGIC(ppp); \
155 printk(ppp_warning, __LINE__); \
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)))
191 static ssize_t
ppp_tty_read(struct tty_struct
*, struct file
*, __u8
*,
193 static ssize_t
ppp_tty_write(struct tty_struct
*, struct file
*, const __u8
*,
195 static int ppp_tty_ioctl(struct tty_struct
*, struct file
*, unsigned int,
197 static unsigned int ppp_tty_poll(struct tty_struct
*tty
, struct file
*filp
,
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
252 * This procedure is called at initialization time to register
253 * the PPP line discipline.
258 static struct tty_ldisc ppp_ldisc
;
262 "PPP: version %s (demand dialling)"
265 #ifndef MODULE /* slhc module logic has its own copyright announcement */
267 "TCP compression code copyright 1989 Regents of the "
268 "University of California\n");
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
);
289 printk(KERN_INFO
"PPP line discipline registered.\n");
291 printk(KERN_ERR
"error registering line discipline: %d\n",
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;
309 answer
= ppp_first_time();
318 * Initialize the async-specific parts of the ppp structure.
321 ppp_async_init(struct ppp
*ppp
)
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;
333 ppp
->tfcs
= PPP_INITFCS
;
334 ppp
->optr
= ppp
->obuf
;
335 ppp
->olim
= ppp
->obuf
;
338 ppp
->rfcs
= PPP_INITFCS
;
341 ppp
->backup_tty
= NULL
;
348 * Clean up the async-specific parts of the ppp structure.
351 ppp_async_release(struct ppp
*ppp
)
355 if ((skb
= ppp
->rpkt
) != NULL
)
358 if ((skb
= ppp
->tpkt
) != NULL
)
366 * Called when the tty discipline is switched to PPP.
370 ppp_tty_open (struct tty_struct
*tty
)
375 * Allocate a ppp structure to use.
377 tty
->disc_data
= NULL
;
378 ppp
= ppp_find(current
->pid
);
381 * If we are taking over a ppp unit which is currently
382 * connected to a loopback pty, there's not much to do.
389 printk(KERN_ERR
"ppp_alloc failed\n");
394 * Initialize the control block
396 ppp_generic_init(ppp
);
402 tty
->disc_data
= ppp
;
406 * Flush any pending characters in the driver
408 if (tty
->driver
.flush_buffer
)
409 tty
->driver
.flush_buffer (tty
);
417 * Called when the line discipline is changed to something
418 * else, the tty is closed, or the tty detects a hangup.
422 ppp_tty_close (struct tty_struct
*tty
)
424 struct ppp
*ppp
= tty2ppp(tty
);
428 tty
->disc_data
= NULL
;
429 if (ppp
->magic
!= PPP_MAGIC
) {
430 printk(KERN_WARNING
"ppp_tty_close: bogus\n");
434 printk(KERN_WARNING
"ppp_tty_close: not inuse\n");
435 ppp
->tty
= ppp
->backup_tty
= 0;
438 if (tty
== ppp
->backup_tty
)
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
);
450 if (ppp
->flags
& SC_DEBUG
)
451 printk(KERN_INFO
"ppp: channel %s closing.\n",
454 ppp_async_release(ppp
);
461 * Read a PPP frame from the rcv_q list,
462 * waiting if necessary
465 ppp_tty_read(struct tty_struct
*tty
, struct file
*file
, __u8
* buf
,
468 struct ppp
*ppp
= tty2ppp (tty
);
473 * Validate the pointers
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
);
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.
497 if (!ppp
|| ppp
->magic
!= PPP_MAGIC
|| !ppp
->inuse
501 skb
= skb_dequeue(&ppp
->rcv_q
);
506 * If no frame is available, return -EAGAIN or wait.
509 if (file
->f_flags
& O_NONBLOCK
)
512 interruptible_sleep_on(&ppp
->read_wait
);
514 if (signal_pending(current
))
522 * Ensure that the frame will fit within the caller's buffer.
523 * If not, just discard the frame.
527 if (ppp
->flags
& SC_DEBUG
)
529 "ppp: read of %lu bytes too small for %ld "
530 "frame\n", (unsigned long) nr
, (long) len
);
531 ppp
->stats
.ppp_ierrors
++;
537 * Copy the received data from the buffer to the caller's area.
540 if (copy_to_user(buf
, skb
->data
, len
))
549 * Writing to a tty in ppp line discipline sends a PPP frame.
550 * Used by pppd to send control packets (LCP, etc.).
553 ppp_tty_write(struct tty_struct
*tty
, struct file
*file
, const __u8
* data
,
556 struct ppp
*ppp
= tty2ppp (tty
);
561 * Verify the pointers.
566 if (ppp
->magic
!= PPP_MAGIC
)
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
)
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
);
588 printk(KERN_ERR
"ppp_tty_write: no memory\n");
591 new_data
= skb_put(skb
, count
);
594 * Retrieve the user's buffer
596 if (copy_from_user(new_data
, data
, count
)) {
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.
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;
623 * Verify the status of the PPP device.
625 if (!ppp
|| ppp
->magic
!= PPP_MAGIC
|| !ppp
->inuse
)
629 * The user must have an euid of root to do these requests.
631 if (!capable(CAP_NET_ADMIN
))
635 case PPPIOCGASYNCMAP
:
637 * Retrieve the transmit async map
639 if (put_user(ppp
->xmit_async_map
[0], (int *) param3
))
644 case PPPIOCSASYNCMAP
:
646 * Set the transmit async map
648 if (get_user(temp_i
, (int *) param3
))
650 ppp
->xmit_async_map
[0] = temp_i
;
651 if (ppp
->flags
& SC_DEBUG
)
653 "ppp_tty_ioctl: set xmit asyncmap %x\n",
654 ppp
->xmit_async_map
[0]);
658 case PPPIOCSRASYNCMAP
:
660 * Set the receive async map
662 if (get_user(temp_i
, (int *) param3
))
664 ppp
->recv_async_map
= temp_i
;
665 if (ppp
->flags
& SC_DEBUG
)
667 "ppp_tty_ioctl: set rcv asyncmap %x\n",
668 ppp
->recv_async_map
);
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
)))
682 case PPPIOCSXASYNCMAP
:
684 * Set the map of characters to be escaped on transmission.
689 if (copy_from_user(temp_tbl
, (void *) param3
,
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
)
702 "ppp_tty_ioctl: set xasyncmap\n");
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
;
720 * Allow users to read, but not set, the serial port parameters
722 error
= n_tty_ioctl (tty
, file
, param2
, param3
);
727 * Flush our buffers, then call the generic code to
728 * flush the serial port's buffer.
730 if (param3
== TCIFLUSH
|| param3
== TCIOFLUSH
) {
732 while ((skb
= skb_dequeue(&ppp
->rcv_q
)) != NULL
)
735 if (param3
== TCIOFLUSH
|| param3
== TCOFLUSH
)
736 ppp_tty_flush_output(ppp
);
737 error
= n_tty_ioctl (tty
, file
, param2
, param3
);
742 * Returns how many bytes are available for a read().
751 skb
= skb_peek(&ppp
->rcv_q
);
754 restore_flags(flags
);
755 if (put_user(count
, (int *) param3
))
763 * All other ioctl() events will come here.
765 error
= ppp_ioctl(ppp
, param2
, param3
);
774 * Process the poll() statement for the PPP device.
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
) {
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
))
793 mask
|= POLLOUT
| POLLWRNORM
;
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.
804 ppp_tty_wakeup (struct tty_struct
*tty
)
806 struct ppp
*ppp
= tty2ppp (tty
);
808 tty
->flags
&= ~(1 << TTY_DO_WRITE_WAKEUP
);
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
830 ppp_sync_send(struct ppp
*ppp
, struct sk_buff
*skb
)
837 if (ppp
->tpkt
!= NULL
)
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 */
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.
883 ppp_tty_sync_push(struct ppp
*ppp
)
886 struct tty_struct
*tty
= ppp2tty(ppp
);
891 if (ppp
->tpkt
== NULL
)
894 /* prevent reentrancy with tty_pushing flag */
897 if (ppp
->tty_pushing
) {
898 /* record wakeup attempt so we don't lose */
899 /* a wakeup call while doing push processing */
901 restore_flags(flags
);
904 ppp
->tty_pushing
= 1;
905 restore_flags(flags
);
907 if (tty
== NULL
|| tty
->disc_data
!= (void *) ppp
)
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
);
917 /* write error (possible loss of CD) */
918 /* record error and discard current packet */
919 ppp
->stats
.ppp_oerrors
++;
922 ppp
->stats
.ppp_obytes
+= sent
;
923 if (sent
< ppp
->tpkt
->len
) {
924 /* driver unable to accept frame just yet */
928 /* wake up called while processing */
929 /* try to send the frame again */
930 restore_flags(flags
);
933 /* wait for wakeup callback to try send again */
934 ppp
->tty_pushing
= 0;
935 restore_flags(flags
);
941 /* done with current packet (sent or discarded) */
942 kfree_skb(ppp
->tpkt
);
944 ppp
->tty_pushing
= 0;
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
957 ppp_async_send(struct ppp
*ppp
, struct sk_buff
*skb
)
963 if (ppp
->tpkt
!= NULL
)
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.
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
);
985 if (ppp
->tty_pushing
) {
989 if (tty
== NULL
|| tty
->disc_data
!= (void *) ppp
)
991 while (ppp
->optr
< ppp
->olim
|| ppp
->tpkt
!= 0) {
992 ppp
->tty_pushing
= 1;
995 avail
= ppp
->olim
- ppp
->optr
;
997 tty
->flags
|= (1 << TTY_DO_WRITE_WAKEUP
);
998 sent
= tty
->driver
.write(tty
, 0, ppp
->optr
, avail
);
1000 goto flush
; /* error, e.g. loss of CD */
1001 ppp
->stats
.ppp_obytes
+= sent
;
1005 ppp
->tty_pushing
= 0;
1013 done
= ppp_async_encode(ppp
);
1015 ppp
->tty_pushing
= 0;
1020 ppp
->tty_pushing
= 1;
1022 ppp
->stats
.ppp_oerrors
++;
1023 if (ppp
->tpkt
!= 0) {
1024 kfree_skb(ppp
->tpkt
);
1028 ppp
->optr
= ppp
->olim
;
1030 ppp
->tty_pushing
= 0;
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.
1042 ppp_async_encode(struct ppp
*ppp
)
1044 int fcs
, i
, count
, c
;
1045 unsigned char *buf
, *buflim
;
1046 unsigned char *data
;
1055 data
= ppp
->tpkt
->data
;
1056 count
= ppp
->tpkt
->len
;
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;
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
)
1075 /* only reset idle time for data packets */
1076 if (PPP_PROTOCOL(data
) < 0x8000)
1077 ppp
->last_xmit
= jiffies
;
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
)
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
) {
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
;
1111 * We have finished the packet. Add the FCS and flag.
1115 if (in_xmap(ppp
, c
) || (islcp
&& c
< 0x20)) {
1116 *buf
++ = PPP_ESCAPE
;
1120 c
= (fcs
>> 8) & 0xff;
1121 if (in_xmap(ppp
, c
) || (islcp
&& c
< 0x20)) {
1122 *buf
++ = PPP_ESCAPE
;
1129 kfree_skb(ppp
->tpkt
);
1135 * Remember where we are up to in this packet.
1144 * Flush output from our internal buffers.
1145 * Called for the TCFLSH ioctl.
1148 ppp_tty_flush_output(struct ppp
*ppp
)
1150 struct sk_buff
*skb
;
1153 while ((skb
= skb_dequeue(&ppp
->xmt_q
)) != NULL
)
1155 ppp
->tty_pushing
= 1;
1157 ppp
->optr
= ppp
->olim
;
1158 if (ppp
->tpkt
!= NULL
) {
1159 kfree_skb(ppp
->tpkt
);
1164 ppp
->tty_pushing
= 0;
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
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.
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
;
1195 * This can happen if stuff comes in on the backup tty.
1197 if (ppp
== 0 || tty
!= ppp
->tty
)
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
)
1206 "PPP: tty_receive called but couldn't find "
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
;
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
);
1229 * Allocate an skbuff for frame.
1230 * The 128 is room for VJ header expansion.
1234 skb
= dev_alloc_skb(ppp
->mru
+ 128 + PPP_HDRLEN
);
1237 if (ppp
->flags
& SC_DEBUG
)
1238 printk(KERN_DEBUG
"couldn't "
1239 "alloc skb for recv\n");
1242 * Decompress A/C and protocol compression here.
1244 p
= skb_put(skb
, 2);
1245 p
[0] = PPP_ALLSTATIONS
;
1247 if (*data
== PPP_ALLSTATIONS
) {
1251 if ((*data
& 1) != 0) {
1252 p
= skb_put(skb
, 1);
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
),
1272 if (!ppp_receive_frame(ppp
, skb
)) {
1274 ppp_receive_error(ppp
);
1278 /* Reset for the next frame */
1285 while (count
-- > 0) {
1287 * Collect the character and error condition for the character.
1288 * Set the toss flag for the first character error.
1298 ++ppp
->estats
.rx_fifo_errors
;
1302 ++ppp
->estats
.rx_frame_errors
;
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
1318 ppp
->flags
|= SC_RCV_B7_1
;
1320 ppp
->flags
|= SC_RCV_B7_0
;
1322 if (paritytab
[chr
>> 5] & (1 << (chr
& 0x1F)))
1323 ppp
->flags
|= SC_RCV_ODDP
;
1325 ppp
->flags
|= SC_RCV_EVNP
;
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.
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
))
1348 "ppp: tossing frame (%x)\n",
1352 if (!(ppp
->toss
== 0xE0 || ppp
->toss
== 0x80))
1353 ++ppp
->stats
.ppp_ierrors
;
1354 ppp_receive_error(ppp
);
1357 * Reset for the next frame.
1360 ppp
->rfcs
= PPP_INITFCS
;
1366 /* If we're tossing, look no further. */
1370 /* If this is a control char to be ignored, do so */
1371 if (in_rmap(ppp
, chr
))
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 :-)
1382 } else if (chr
== PPP_ESCAPE
) {
1383 ppp
->escape
= PPP_TRANS
;
1388 * Allocate an skbuff on the first character received.
1389 * The 128 is room for VJ header expansion and FCS.
1392 skb
= dev_alloc_skb(ppp
->mru
+ 128 + PPP_HDRLEN
);
1394 if (ppp
->flags
& SC_DEBUG
)
1395 printk(KERN_DEBUG
"couldn't "
1396 "alloc skb for recv\n");
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
;
1410 if (skb
->len
== 2 && (chr
& 1) != 0) {
1411 p
= skb_put(skb
, 1);
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
;
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
),
1431 * Store the character and update the FCS.
1433 p
= skb_put(skb
, 1);
1435 ppp
->rfcs
= PPP_FCS(ppp
->rfcs
, chr
);
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
{
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
*);
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.
1496 ppp_init_dev (struct net_device
*dev
)
1498 dev
->hard_header_len
= PPP_HDRLEN
;
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
;
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
;
1519 * Callback from the network layer when the device goes up.
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
);
1538 * Callback from the network layer when the ppp device goes down.
1542 ppp_dev_close (struct net_device
*dev
)
1544 struct ppp
*ppp
= dev2ppp (dev
);
1546 CHECK_PPP_MAGIC(ppp
);
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.
1570 ppp_dev_ioctl (struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1572 struct ppp
*ppp
= dev2ppp(dev
);
1575 struct ppp_stats stats
;
1576 struct ppp_comp_stats cstats
;
1580 CHECK_PPP_MAGIC(ppp
);
1582 memset(&u
, 0, sizeof(u
));
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
);
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
);
1602 strcpy(u
.vers
, szVersion
);
1603 nb
= strlen(u
.vers
) + 1;
1610 if (copy_to_user((void *) ifr
->ifr_ifru
.ifru_data
, &u
, nb
))
1616 * Process the generic PPP ioctls, i.e. those which are not specific
1617 * to any particular type of hardware link.
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
;
1631 * The user must have an euid of root to do these requests.
1633 if (!capable(CAP_NET_ADMIN
))
1641 if (get_user(temp_i
, (int *) param3
))
1643 if (temp_i
< PPP_MRU
)
1646 if (ppp
->flags
& SC_DEBUG
)
1648 "ppp_ioctl: set mru to %x\n", temp_i
);
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
;
1661 if (put_user(temp_i
, (int *) param3
))
1668 * Set the flags for the various options
1670 if (get_user(temp_i
, (int *) param3
))
1673 if (ppp
->flags
& ~temp_i
& SC_CCP_OPEN
)
1674 ppp_ccp_closed(ppp
);
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
)
1685 "ppp_ioctl: set flags to %x\n", temp_i
);
1689 case PPPIOCSCOMPRESS
:
1691 * Set the compression mode
1693 error
= ppp_set_compression
1694 (ppp
, (struct ppp_option_data
*) param3
);
1699 * Obtain the unit number for this device.
1701 if (put_user(ppp
->line
, (int *) param3
))
1703 if (ppp
->flags
& SC_DEBUG
)
1705 "ppp_ioctl: get unit: %d\n", ppp
->line
);
1711 * Set the debug level
1713 if (get_user(temp_i
, (int *) param3
))
1715 temp_i
= (temp_i
& 0x1F) << 16;
1717 if ((ppp
->flags
| temp_i
) & SC_DEBUG
)
1719 "ppp_ioctl: set dbg flags to %x\n", temp_i
);
1723 ppp
->flags
= (ppp
->flags
& ~0x1F0000) | temp_i
;
1724 restore_flags(flags
);
1730 * Get the debug level
1732 temp_i
= (ppp
->flags
>> 16) & 0x1F;
1733 if (put_user(temp_i
, (int *) param3
))
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
)))
1753 * Set the maximum VJ header compression slot number.
1755 if (get_user(temp_i
, (int *) param3
))
1758 if (temp_i
< 2 || temp_i
> 255)
1761 if (ppp
->flags
& SC_DEBUG
)
1762 printk(KERN_INFO
"ppp_ioctl: set maxcid to %d\n",
1764 if (ppp
->slcomp
!= NULL
)
1765 slhc_free(ppp
->slcomp
);
1766 ppp
->slcomp
= slhc_init(16, temp_i
);
1769 if (ppp
->slcomp
== NULL
) {
1770 printk(KERN_ERR
"ppp: no memory for VJ compression\n");
1778 if (copy_from_user(&npi
, (void *) param3
, sizeof(npi
)))
1781 switch (npi
.protocol
) {
1783 npi
.protocol
= NP_IP
;
1786 npi
.protocol
= NP_IPX
;
1789 npi
.protocol
= NP_AT
;
1792 if (ppp
->flags
& SC_DEBUG
)
1793 printk(KERN_DEBUG
"pppioc[gs]npmode: "
1794 "invalid proto %d\n", npi
.protocol
);
1799 if (param2
== PPPIOCGNPMODE
) {
1800 npi
.mode
= ppp
->sc_npmode
[npi
.protocol
];
1801 if (copy_to_user((void *) param3
, &npi
, sizeof(npi
)))
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
);
1815 * All other ioctl() events will come here.
1817 if (ppp
->flags
& SC_DEBUG
)
1819 "ppp_ioctl: invalid ioctl: %x, addr %lx\n",
1822 error
= -ENOIOCTLCMD
;
1830 * Process the set-compression ioctl.
1833 ppp_set_compression (struct ppp
*ppp
, struct ppp_option_data
*odp
)
1835 struct compressor
*cp
;
1837 unsigned long flags
;
1839 __u8 ccp_option
[CCP_MAX_OPTION_LENGTH
];
1840 struct ppp_option_data data
;
1843 * Fetch the compression parameters
1846 if (copy_from_user(&data
, odp
, sizeof (data
)))
1851 if ((unsigned) nb
>= CCP_MAX_OPTION_LENGTH
)
1852 nb
= CCP_MAX_OPTION_LENGTH
;
1854 if (copy_from_user(ccp_option
, ptr
, nb
))
1858 if (ccp_option
[1] < 2) /* preliminary check on the length byte */
1863 ppp
->flags
&= ~(data
.transmit
? SC_COMP_RUN
: SC_DECOMP_RUN
);
1864 restore_flags(flags
);
1866 cp
= find_compressor (ccp_option
[0]);
1870 sprintf(modname
, "ppp-compress-%d", ccp_option
[0]);
1871 request_module(modname
);
1872 cp
= find_compressor(ccp_option
[0]);
1874 #endif /* CONFIG_KMOD */
1877 if (ppp
->flags
& SC_DEBUG
)
1879 "%s: no compressor for [%x %x %x], %x\n",
1880 ppp
->name
, ccp_option
[0], ccp_option
[1],
1882 goto out
; /* compressor not loaded */
1886 * Found a handler for the protocol - try to allocate
1887 * a compressor or decompressor.
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
;
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",
1904 if (ppp
->sc_rc_state
!= NULL
)
1905 (*ppp
->sc_rcomp
->decomp_free
)(ppp
->sc_rc_state
);
1906 ppp
->sc_rc_state
= NULL
;
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",
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
;
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
);
1943 switch (CCP_CODE(dp
)) {
1948 * CCP must be going down - disable compression
1950 if (ppp
->flags
& SC_CCP_UP
) {
1952 ppp
->flags
&= ~(SC_CCP_UP
|
1959 if ((ppp
->flags
& SC_CCP_OPEN
) == 0)
1961 if (ppp
->flags
& SC_CCP_UP
)
1963 if (slen
< (CCP_HDRLEN
+ CCP_OPT_MINLEN
))
1965 if (slen
< (CCP_OPT_LENGTH (opt
) + CCP_HDRLEN
))
1969 * we're agreeing to send compressed packets.
1971 if (ppp
->sc_xc_state
== NULL
)
1974 if ((*ppp
->sc_xcomp
->comp_init
)
1977 ppp
->line
, 0, ppp
->flags
& SC_DEBUG
)) {
1978 if (ppp
->flags
& SC_DEBUG
)
1979 printk(KERN_DEBUG
"%s: comp running\n",
1982 ppp
->flags
|= SC_COMP_RUN
;
1988 * peer is agreeing to send compressed packets.
1990 if (ppp
->sc_rc_state
== NULL
)
1993 if ((*ppp
->sc_rcomp
->decomp_init
)
1996 ppp
->line
, 0, ppp
->mru
, ppp
->flags
& SC_DEBUG
)) {
1997 if (ppp
->flags
& SC_DEBUG
)
1998 printk(KERN_DEBUG
"%s: decomp running\n",
2001 ppp
->flags
|= SC_DECOMP_RUN
;
2002 ppp
->flags
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
2008 * CCP Reset-ack resets compressors and decompressors
2009 * as it passes through.
2011 if ((ppp
->flags
& SC_CCP_UP
) == 0)
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",
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",
2028 ppp
->flags
&= ~SC_DC_ERROR
;
2033 restore_flags(flags
);
2037 * CCP is down; free (de)compressor state if necessary.
2041 ppp_ccp_closed(struct ppp
*ppp
)
2043 unsigned long flags
;
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.
2071 ppp_receive_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
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.
2086 if (skb
->len
== 0) {
2094 * Generate an error if the frame is too small.
2096 if (count
< PPP_HDRLEN
+ 2) {
2097 if (ppp
->flags
& SC_DEBUG
)
2099 "ppp: got runt ppp frame, %d chars\n", count
);
2100 ++ppp
->estats
.rx_length_errors
;
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
) {
2112 "ppp: frame with bad fcs, length = %d\n",
2114 ppp_print_buffer("bad frame", data
, count
);
2116 ++ppp
->estats
.rx_crc_errors
;
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
;
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 */
2147 data
= skb_put(skb
, count
);
2151 * On a decompression error, we pass the
2152 * compressed frame up to pppd as an
2155 if (ppp
->flags
& SC_DEBUG
)
2156 printk(KERN_INFO
"%s: decomp err %d\n",
2157 ppp
->name
, new_count
);
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
;
2166 ppp
->flags
|= SC_DC_ERROR
;
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
,
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
)
2204 * Update the appropriate statistic counter.
2206 if (!(*proto_ptr
->func
)(ppp
, skb
)) {
2208 ++ppp
->stats
.ppp_discards
;
2215 * An input error has been detected, so we need to inform
2216 * the VJ decompressor.
2219 ppp_receive_error(struct ppp
*ppp
)
2223 if (ppp
->slcomp
!= 0)
2224 slhc_toss(ppp
->slcomp
);
2228 * Put the input frame into the networking system for the indicated protocol
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
;
2247 * Process the receipt of an IP frame
2250 rcv_proto_ip(struct ppp
*ppp
, struct sk_buff
*skb
)
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
);
2260 * Process the receipt of an IPv6 frame
2263 rcv_proto_ipv6(struct ppp
*ppp
, struct sk_buff
*skb
)
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
);
2273 * Process the receipt of an IPX frame
2276 rcv_proto_ipx(struct ppp
*ppp
, struct sk_buff
*skb
)
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
);
2286 * Process the receipt of an Appletalk frame
2289 rcv_proto_at(struct ppp
*ppp
, struct sk_buff
*skb
)
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
);
2299 * Process the receipt of an VJ Compressed frame
2302 rcv_proto_vjc_comp(struct ppp
*ppp
, struct sk_buff
*skb
)
2307 if ((ppp
->flags
& SC_REJ_COMP_TCP
) || ppp
->slcomp
== NULL
)
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
)
2314 "ppp: error in VJ decompression\n");
2317 new_count
+= PPP_HDRLEN
;
2318 if (new_count
> skb
->len
)
2319 skb_put(skb
, new_count
- skb
->len
);
2321 skb_trim(skb
, new_count
);
2322 return rcv_proto_ip(ppp
, skb
);
2326 * Process the receipt of an VJ Un-compressed frame
2329 rcv_proto_vjc_uncomp(struct ppp
*ppp
, struct sk_buff
*skb
)
2332 if ((ppp
->flags
& SC_REJ_COMP_TCP
) || ppp
->slcomp
== NULL
)
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");
2340 return rcv_proto_ip(ppp
, skb
);
2344 rcv_proto_ccp(struct ppp
*ppp
, struct sk_buff
*skb
)
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.
2355 rcv_proto_unknown(struct ppp
*ppp
, struct sk_buff
*skb
)
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
);
2369 wake_up_interruptible (&ppp
->read_wait
);
2370 if (ppp
->tty
->fasync
!= NULL
)
2371 kill_fasync (ppp
->tty
->fasync
, SIGIO
);
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
;
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
2396 ppp_send_frame(struct ppp
*ppp
, struct sk_buff
*skb
)
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
);
2422 if ((ppp
->flags
& SC_COMP_TCP
) && ppp
->slcomp
!= NULL
)
2423 skb
= ppp_vj_compress(ppp
, skb
);
2428 * Update the LQR frame with the current MIB information.
2429 * This way the information is accurate and up-to-date.
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
;
2441 * Outbound compression control frames
2443 ppp_proto_ccp(ppp
, data
+ PPP_HDRLEN
, count
- PPP_HDRLEN
, 0);
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
;
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");
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
);
2480 * The frame could not be compressed, or it could not
2481 * be sent in compressed form because CCP is down.
2490 if ( ppp
->flags
& SC_SYNC
)
2491 ret
= ppp_sync_send(ppp
, skb
);
2493 ret
= ppp_async_send(ppp
, skb
);
2495 /* we can release the lock */
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
;
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");
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 */
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
;
2535 if (data
[0] >= SL_TYPE_UNCOMPRESSED_TCP
)
2536 proto
= PPP_VJC_UNCOMP
;
2539 data
[0] = orig_data
[0];
2542 data
= skb_put(new_skb
, len
+ PPP_HDRLEN
);
2543 data
[0] = PPP_ALLSTATIONS
;
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
);
2563 ppp_send_frame(ppp
, skb
);
2565 if (!ppp
->xmit_busy
&& ppp
->dev
.tbusy
) {
2572 * Called from the hardware (tty) layer when it can accept
2576 ppp_output_wakeup(struct ppp
*ppp
)
2580 if (!ppp
->xmit_busy
) {
2581 printk(KERN_ERR
"ppp_output_wakeup called but xmit_busy==0\n");
2585 ppp_send_frames(ppp
);
2589 * Send a control frame (from pppd).
2592 ppp_send_ctrl(struct ppp
*ppp
, struct sk_buff
*skb
)
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 /*************************************************************
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.
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
);
2622 /* just a little sanity check. */
2625 if (skb
->data
== NULL
) {
2631 * Avoid timing problem should tty hangup while data is
2632 * queued to be sent.
2640 * Validate the tty interface
2643 if (ppp
->flags
& SC_DEBUG
)
2645 "ppp_dev_xmit: %s not connected to a TTY!\n",
2652 * Work out the appropriate network-protocol mode for this packet.
2654 npmode
= NPMODE_PASS
; /* default */
2655 switch (ntohs(skb
->protocol
)) {
2658 npmode
= ppp
->sc_npmode
[NP_IP
];
2662 npmode
= ppp
->sc_npmode
[NP_IPV6
];
2666 npmode
= ppp
->sc_npmode
[NP_IPX
];
2671 npmode
= ppp
->sc_npmode
[NP_AT
];
2674 if (ppp
->flags
& SC_DEBUG
)
2675 printk(KERN_INFO
"%s: packet for unknown proto %x\n",
2676 ppp
->name
, ntohs(skb
->protocol
));
2682 * Drop, accept or reject the packet depending on the mode.
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
);
2701 if (ppp
->flags
& SC_DEBUG
)
2703 "ppp_dev_xmit: dropping (npmode = %d) on %s\n",
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 */
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",
2736 ppp_send_frames(ppp
);
2739 skb_reserve(new_skb
, PPP_HDRLEN
);
2740 memcpy(skb_put(new_skb
, skb
->len
), skb
->data
, skb
->len
);
2745 hdr
= skb_push(skb
, PPP_HDRLEN
);
2746 hdr
[0] = PPP_ALLSTATIONS
;
2748 hdr
[2] = proto
>> 8;
2751 ppp_send_frame(ppp
, skb
);
2752 if (!ppp
->xmit_busy
)
2753 ppp_send_frames(ppp
);
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 /*************************************************************
2777 * Miscellany called by various functions above.
2778 *************************************************************/
2780 /* Locate the previous instance of the PPP channel */
2782 ppp_find(int pid_value
)
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
) {
2796 /* allocate or create a PPP channel */
2802 struct net_device
*dev
;
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
)) {
2809 if (dev
->flags
& IFF_UP
) {
2810 clear_bit(0, &ppp
->inuse
);
2813 /* Reregister device */
2814 unregister_netdev(dev
);
2815 if (register_netdev(dev
) == 0)
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
);
2827 printk(KERN_ERR
"ppp: struct ppp allocation failed\n");
2830 memset(ppp
, 0, sizeof(*ppp
));
2832 /* initialize channel control data */
2833 ppp
->magic
= PPP_MAGIC
;
2836 init_waitqueue_head(&ppp
->read_wait
);
2839 * Make up a suitable name for this device
2842 dev
->name
= ppp
->name
;
2843 if_num
= dev_alloc_name(dev
, "ppp%d");
2845 printk(KERN_ERR
"ppp: dev_alloc_name failed (%d)\n", if_num
);
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
);
2861 printk(KERN_INFO
"registered device %s\n", dev
->name
);
2864 "ppp_alloc - register_netdev(%s) = %d failure.\n",
2870 /* link this unit into our list */
2874 ppp_last
->next
= ppp
;
2881 * Initialize the generic parts of the ppp structure.
2884 ppp_generic_init(struct ppp
*ppp
)
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
;
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.
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
);
2930 slhc_free(ppp
->slcomp
);
2934 while ((skb
= skb_dequeue(&ppp
->rcv_q
)) != NULL
)
2936 while ((skb
= skb_dequeue(&ppp
->xmt_q
)) != NULL
)
2940 if (ppp
->dev
.tbusy
) {
2947 * Utility procedures to print a buffer in hex/ascii
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) {
2957 *out
++ = hex
[(next_ch
>> 4) & 0x0F];
2958 *out
++ = hex
[next_ch
& 0x0F];
2964 ppp_print_char (register __u8
* out
, const __u8
* in
, int count
)
2966 register __u8 next_ch
;
2968 while (count
-- > 0) {
2971 if (next_ch
< 0x20 || next_ch
> 0x7e)
2975 if (next_ch
== '%') /* printk/syslogd has a bug !! */
2983 ppp_print_buffer (const char *name
, const __u8
*buf
, int count
)
2988 printk(KERN_DEBUG
"ppp: %s, count = %d\n", name
, count
);
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
);
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
;
3026 lnk
= ppp_compressors
;
3027 while (lnk
!= (struct compressor_link
*) 0) {
3028 if ((int) (__u8
) lnk
->comp
->compress_proto
== type
) {
3029 restore_flags(flags
);
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)
3054 if (find_compressor (cp
->compress_proto
)) {
3055 restore_flags(flags
);
3060 new->next
= ppp_compressors
;
3062 ppp_compressors
= new;
3064 restore_flags(flags
);
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
;
3077 lnk
= ppp_compressors
;
3078 while (lnk
!= (struct compressor_link
*) 0) {
3079 if (lnk
->comp
== cp
) {
3081 prev
->next
= lnk
->next
;
3083 ppp_compressors
= lnk
->next
;
3090 restore_flags(flags
);
3094 /*************************************************************
3095 * Module support routines
3096 *************************************************************/
3104 /* register our line disciplines */
3105 status
= ppp_first_time();
3107 printk(KERN_INFO
"PPP: ppp_init() failure %d\n", status
);
3113 cleanup_module(void)
3116 struct ppp
*ppp
, *next_ppp
;
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
))
3128 printk(KERN_CRIT
"PPP: removing despite %d units in use!\n",
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
);
3138 "PPP: Unable to unregister ppp line discipline "
3139 "(err = %d)\n", status
);
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
);