3 * Michael Callahan <callahan@maths.ox.ac.uk>
4 * Al Longyear <longyear@netcom.com>
5 * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
7 * Dynamic PPP devices by Jim Freeman <jfree@caldera.com>.
8 * ppp_tty_receive ``noisy-raise-bug'' fixed by Ove Ewerlid <ewerlid@syscon.uu.se>
10 * ==FILEVERSION 971016==
12 * NOTE TO MAINTAINERS:
13 * If you modify this file at all, please set the number above to the
14 * date of the modification as YYMMDD (year month day).
15 * ppp.c is shipped with a PPP distribution as well as with the kernel;
16 * if everyone increases the FILEVERSION number above, then scripts
17 * can do the right thing when deciding whether to install a new ppp.c
18 * file. Don't change the format of that line otherwise, so the
19 * installation script can recognize it.
27 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
28 Multi-protocol Datagrams over Point-to-Point Links
34 Flags for this module (any combination is acceptable for testing.):
36 OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
37 character. This is normally set to ((HZ * 3) / 2).
38 This is 1.5 seconds. If zero then the leading
41 CHECK_CHARACTERS - Enable the checking on all received characters for
42 8 data bits, no parity. This adds a small amount of
43 processing for each received character.
46 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
48 #define CHECK_CHARACTERS 1
49 #define PPP_COMPRESS 1
51 /* $Id: ppp.c,v 1.14 1997/11/27 06:04:45 paulus Exp $ */
53 #include <linux/version.h>
54 #include <linux/module.h>
55 #include <linux/kernel.h>
56 #include <linux/sched.h>
57 #include <linux/types.h>
58 #include <linux/fcntl.h>
59 #include <linux/interrupt.h>
60 #include <linux/ptrace.h>
63 /* a nice define to generate linux version numbers */
64 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
66 #if LINUX_VERSION_CODE < VERSION(2,1,14)
67 #include <linux/ioport.h>
70 #if LINUX_VERSION_CODE >= VERSION(2,1,23)
71 #include <linux/poll.h>
75 #include <linux/malloc.h>
76 #include <linux/tty.h>
77 #include <linux/errno.h>
78 #include <linux/sched.h> /* to get the struct task_struct */
79 #include <linux/string.h> /* used in new tty drivers */
80 #include <linux/signal.h> /* used in new tty drivers */
81 #include <asm/system.h>
82 #include <asm/bitops.h>
84 #include <linux/if_ether.h>
85 #include <linux/netdevice.h>
86 #include <linux/skbuff.h>
87 #include <linux/inet.h>
88 #include <linux/ioctl.h>
90 typedef struct sk_buff sk_buff;
91 #define skb_data(skb) ((__u8 *) (skb)->data)
94 #include <linux/tcp.h>
95 #include <linux/if_arp.h>
96 #include <net/slhc_vj.h>
98 #define fcstab ppp_crc16_table /* Name of the table in the kernel */
99 #include <linux/ppp_defs.h>
101 #include <linux/socket.h>
102 #include <linux/if_ppp.h>
103 #include <linux/if_pppvar.h>
104 #include <linux/ppp-comp.h>
107 #define PPP_IPX 0x2b /* IPX protocol over PPP */
111 #define PPP_LQR 0xc025 /* Link Quality Reporting Protocol */
114 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
115 #include <asm/segment.h>
116 #define GET_USER(error,value,addr) error = get_user(value,addr)
117 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
118 #define PUT_USER(error,value,addr) error = put_user(value,addr)
119 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
121 #if LINUX_VERSION_CODE >= VERSION(2,1,5)
122 #include <asm/uaccess.h>
125 #else /* 2.0.x and 2.1.x before 2.1.4 */
127 #define GET_USER(error,value,addr) \
129 error = verify_area (VERIFY_READ, (void *) addr, sizeof (value)); \
131 value = get_user(addr); \
134 #define COPY_FROM_USER(error,dest,src,size) \
136 error = verify_area (VERIFY_READ, (void *) src, size); \
138 memcpy_fromfs (dest, src, size); \
141 #define PUT_USER(error,value,addr) \
143 error = verify_area (VERIFY_WRITE, (void *) addr, sizeof (value)); \
145 put_user (value, addr); \
148 #define COPY_TO_USER(error,dest,src,size) \
150 error = verify_area (VERIFY_WRITE, (void *) dest, size); \
152 memcpy_tofs (dest, src, size); \
157 #if LINUX_VERSION_CODE < VERSION(2,1,37)
158 #define test_and_set_bit(nr, addr) set_bit(nr, addr)
161 static int ppp_register_compressor (struct compressor *cp);
162 static void ppp_unregister_compressor (struct compressor *cp);
168 static struct compressor *find_compressor (int type);
169 static void ppp_init_ctrl_blk (register struct ppp *);
170 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
171 static struct ppp *ppp_alloc (void);
172 static struct ppp *ppp_find (int pid_value);
173 static void ppp_print_buffer (const __u8 *, const __u8 *, int);
174 extern inline void ppp_stuff_char (struct ppp *ppp,
175 register struct ppp_buffer *buf,
177 extern inline int lock_buffer (register struct ppp_buffer *buf);
178 static int ppp_dev_xmit_ip (struct ppp *ppp, struct ppp_buffer *buf,
179 __u8 *data, int len, enum NPmode npmode);
181 static int rcv_proto_ip (struct ppp *, __u16, __u8 *, int);
182 static int rcv_proto_ipx (struct ppp *, __u16, __u8 *, int);
183 static int rcv_proto_vjc_comp (struct ppp *, __u16, __u8 *, int);
184 static int rcv_proto_vjc_uncomp (struct ppp *, __u16, __u8 *, int);
185 static int rcv_proto_unknown (struct ppp *, __u16, __u8 *, int);
186 static int rcv_proto_lqr (struct ppp *, __u16, __u8 *, int);
187 static void ppp_doframe_lower (struct ppp *, __u8 *, int);
188 static int ppp_doframe (struct ppp *);
190 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd);
191 static int rcv_proto_ccp (struct ppp *, __u16, __u8 *, int);
193 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
195 #ifndef OPTIMIZE_FLAG_TIME
196 #define OPTIMIZE_FLAG_TIME 0
200 * Parameters which may be changed via insmod.
203 static int flag_time = OPTIMIZE_FLAG_TIME;
205 #if LINUX_VERSION_CODE >= VERSION(2,1,19)
206 MODULE_PARM(flag_time, "i");
210 * The "main" procedure to the ppp device
213 int ppp_init (struct device *);
216 * Network device driver callback routines
219 static int ppp_dev_open (struct device *);
220 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
221 static int ppp_dev_close (struct device *);
222 static int ppp_dev_xmit (sk_buff *, struct device *);
223 static struct enet_statistics *ppp_dev_stats (struct device *);
225 #if LINUX_VERSION_CODE < VERSION(2,1,15)
226 static int ppp_dev_header (sk_buff *, struct device *, __u16,
227 void *, void *, unsigned int);
228 static int ppp_dev_rebuild (void *eth, struct device *dev,
229 unsigned long raddr, struct sk_buff *skb);
236 static int ppp_tty_read (struct tty_struct *, struct file *, __u8 *,
238 static int ppp_tty_write (struct tty_struct *, struct file *, const __u8 *,
240 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
242 #if LINUX_VERSION_CODE < VERSION(2,1,23)
243 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
244 struct file *filp, int sel_type, select_table * wait);
246 static unsigned int ppp_tty_poll (struct tty_struct *tty, struct file *filp, poll_table * wait);
248 static int ppp_tty_open (struct tty_struct *);
249 static void ppp_tty_close (struct tty_struct *);
250 static int ppp_tty_room (struct tty_struct *tty);
251 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
252 char *fp, int count);
253 static void ppp_tty_wakeup (struct tty_struct *tty);
255 #define CHECK_PPP_MAGIC(ppp) do { \
256 if (ppp->magic != PPP_MAGIC) { \
257 printk(KERN_WARNING "bad magic for ppp %p at %s:%d\n", \
258 ppp, __FILE__, __LINE__); \
261 #define CHECK_PPP(a) do { \
262 CHECK_PPP_MAGIC(ppp); \
264 printk (ppp_warning, __LINE__); \
268 #define CHECK_PPP_VOID() do { \
269 CHECK_PPP_MAGIC(ppp); \
271 printk (ppp_warning, __LINE__); \
275 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
276 #define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \
277 ppp->recv_async_map & (1 << (c)))
279 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
281 #define tty2ppp(tty) ((struct ppp *) ((tty)->disc_data))
282 #define dev2ppp(dev) ((struct ppp *) ((dev)->priv))
283 #define ppp2tty(ppp) ((ppp)->tty)
284 #define ppp2dev(ppp) (&(ppp)->dev)
286 static struct ppp *ppp_list = NULL;
287 static struct ppp *ppp_last = NULL;
290 #define BUFFER_TYPE_DEV_RD 0 /* ppp read buffer */
291 #define BUFFER_TYPE_TTY_WR 1 /* tty write buffer */
292 #define BUFFER_TYPE_DEV_WR 2 /* ppp write buffer */
293 #define BUFFER_TYPE_TTY_RD 3 /* tty read buffer */
294 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
296 /* Define this string only once for all macro invocations */
297 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
299 static char szVersion[] = PPP_VERSION;
302 * Information for the protocol decoder
305 typedef int (*pfn_proto) (struct ppp *, __u16, __u8 *, int);
307 typedef struct ppp_proto_struct {
313 ppp_proto_type proto_list[] = {
314 { PPP_IP, rcv_proto_ip },
315 { PPP_IPX, rcv_proto_ipx },
316 { PPP_VJC_COMP, rcv_proto_vjc_comp },
317 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
318 { PPP_LQR, rcv_proto_lqr },
319 { PPP_CCP, rcv_proto_ccp },
320 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
323 __u16 ppp_crc16_table[256] =
325 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
326 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
327 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
328 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
329 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
330 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
331 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
332 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
333 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
334 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
335 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
336 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
337 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
338 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
339 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
340 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
341 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
342 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
343 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
344 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
345 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
346 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
347 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
348 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
349 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
350 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
351 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
352 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
353 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
354 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
355 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
356 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
359 #ifdef CHECK_CHARACTERS
360 static __u32 paritytab[8] =
362 0x96696996, 0x69969669, 0x69969669, 0x96696996,
363 0x69969669, 0x96696996, 0x96696996, 0x69969669
367 /* local function to store a value into the LQR frame */
368 extern inline __u8 * store_long (register __u8 *p, register int value) {
369 *p++ = (__u8) (value >> 24);
370 *p++ = (__u8) (value >> 16);
371 *p++ = (__u8) (value >> 8);
376 /*************************************************************
378 *************************************************************/
380 /* This procedure is called once and once only to define who we are to
381 * the operating system and the various procedures that it may use in
382 * accessing the ppp protocol.
386 ppp_first_time (void)
388 static struct tty_ldisc ppp_ldisc;
392 "PPP: version %s (demand dialling)"
395 #ifndef MODULE /* slhc module logic has its own copyright announcement */
397 "TCP compression code copyright 1989 Regents of the "
398 "University of California\n");
402 * Register the tty discipline
404 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
405 ppp_ldisc.magic = TTY_LDISC_MAGIC;
406 ppp_ldisc.open = ppp_tty_open;
407 ppp_ldisc.close = ppp_tty_close;
408 ppp_ldisc.read = ppp_tty_read;
409 ppp_ldisc.write = ppp_tty_write;
410 ppp_ldisc.ioctl = ppp_tty_ioctl;
411 #if LINUX_VERSION_CODE < VERSION(2,1,23)
412 ppp_ldisc.select = ppp_tty_select;
414 ppp_ldisc.poll = ppp_tty_poll;
416 ppp_ldisc.receive_room = ppp_tty_room;
417 ppp_ldisc.receive_buf = ppp_tty_receive;
418 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
420 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
422 printk (KERN_INFO "PPP line discipline registered.\n");
424 printk (KERN_ERR "error registering line discipline: %d\n",
429 /*************************************************************
431 *************************************************************/
433 /* called when the device is actually created */
436 ppp_init_dev (struct device *dev)
438 #if LINUX_VERSION_CODE < VERSION(2,1,15)
439 dev->hard_header = ppp_dev_header;
440 dev->rebuild_header = ppp_dev_rebuild;
443 dev->hard_header_len = PPP_HDRLEN;
447 dev->hard_start_xmit = ppp_dev_xmit;
448 dev->open = ppp_dev_open;
449 dev->stop = ppp_dev_close;
450 dev->get_stats = ppp_dev_stats;
451 dev->do_ioctl = ppp_dev_ioctl;
453 dev->tx_queue_len = 10;
454 dev->type = ARPHRD_PPP;
456 #if LINUX_VERSION_CODE < VERSION(2,1,20)
460 for (indx = 0; indx < DEV_NUMBUFFS; indx++)
461 skb_queue_head_init (&dev->buffs[indx]);
464 dev_init_buffers(dev);
467 /* New-style flags */
468 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
469 dev->family = AF_INET;
473 dev->pa_alen = 4; /* sizeof (__u32) */
479 * Local procedure to initialize the ppp structure
483 ppp_init_ctrl_blk (register struct ppp *ppp)
485 ppp->magic = PPP_MAGIC;
493 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
494 ppp->xmit_async_map[0] = 0xffffffff;
495 ppp->xmit_async_map[3] = 0x60000000;
496 ppp->recv_async_map = 0x00000000;
503 ppp->read_wait = NULL;
504 ppp->write_wait = NULL;
505 ppp->last_xmit = jiffies - flag_time;
506 ppp->last_recv = jiffies;
508 /* clear statistics */
509 memset(&ppp->stats, 0, sizeof (struct pppstat));
510 memset(&ppp->estats, 0, sizeof(ppp->estats));
512 /* PPP compression data */
514 ppp->sc_rc_state = NULL;
517 #if LINUX_VERSION_CODE < VERSION(2,1,18)
518 static struct symbol_table ppp_syms = {
519 #include <linux/symtab_begin.h>
520 X(ppp_register_compressor),
521 X(ppp_unregister_compressor),
523 #include <linux/symtab_end.h>
526 EXPORT_SYMBOL(ppp_register_compressor);
527 EXPORT_SYMBOL(ppp_unregister_compressor);
528 EXPORT_SYMBOL(ppp_crc16_table);
531 /* called at boot/load time for each ppp device defined in the kernel */
535 ppp_init (struct device *dev)
537 static int first_time = 1;
542 answer = ppp_first_time();
543 #if LINUX_VERSION_CODE < VERSION(2,1,18)
545 (void) register_symtab (&ppp_syms);
554 #define BUFFER_MAGIC 0x1d10
555 #define CHECK_BUF_MAGIC(buf) do { \
556 if (buf->magic != BUFFER_MAGIC) { \
557 printk(KERN_WARNING "bad magic for ppp buffer %p at %s:%d\n", \
558 buf, __FILE__, __LINE__); \
563 * Routine to allocate a buffer for later use by the driver.
566 static struct ppp_buffer *
567 ppp_alloc_buf (int size, int type)
569 struct ppp_buffer *buf;
571 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
575 buf->size = size - 1; /* Mask for the buffer size */
581 buf->fcs = PPP_INITFCS;
582 buf->magic = BUFFER_MAGIC;
588 * Routine to release the allocated buffer.
592 ppp_free_buf (struct ppp_buffer *ptr)
595 CHECK_BUF_MAGIC(ptr);
601 * Lock the indicated transmit buffer
605 lock_buffer (register struct ppp_buffer *buf)
610 * Save the current state and if free then set it to the "busy" state
612 CHECK_BUF_MAGIC(buf);
619 restore_flags (flags);
624 * MTU has been changed by the IP layer. Unfortunately we are not told
625 * about this, but we spot it ourselves and fix things up. We could be
626 * in an upcall from the tty driver, or in an ip packet queue.
630 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
635 struct ppp_buffer *new_rbuf;
636 struct ppp_buffer *new_wbuf;
637 struct ppp_buffer *new_cbuf;
638 struct ppp_buffer *new_tbuf;
640 struct ppp_buffer *old_rbuf;
641 struct ppp_buffer *old_wbuf;
642 struct ppp_buffer *old_cbuf;
643 struct ppp_buffer *old_tbuf;
647 * Allocate the buffer from the kernel for the data
651 if (ppp->flags & SC_DEBUG)
652 printk(KERN_DEBUG "%s: changedmtu %d %d\n", ppp->name,
655 /* allow for possible escaping of every character */
656 mtu = (new_mtu * 2) + 20;
658 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
664 new_wbuf = ppp_alloc_buf (mtu+PPP_HDRLEN, BUFFER_TYPE_DEV_WR);
665 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
666 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
667 new_cbuf = ppp_alloc_buf (mru+PPP_HDRLEN, BUFFER_TYPE_VJ);
669 * If the buffers failed to allocate then complain and release the partial
672 if (new_wbuf == NULL || new_tbuf == NULL ||
673 new_rbuf == NULL || new_cbuf == NULL) {
674 if (ppp->flags & SC_DEBUG)
676 "ppp: failed to allocate new buffers\n");
678 ppp_free_buf (new_wbuf);
679 ppp_free_buf (new_tbuf);
680 ppp_free_buf (new_rbuf);
681 ppp_free_buf (new_cbuf);
685 * Update the pointers to the new buffer structures.
689 old_wbuf = ppp->wbuf;
690 old_rbuf = ppp->rbuf;
691 old_cbuf = ppp->cbuf;
692 old_tbuf = ppp->tbuf;
694 ppp->wbuf = new_wbuf;
695 ppp->rbuf = new_rbuf;
696 ppp->cbuf = new_cbuf;
697 ppp->tbuf = new_tbuf;
700 new_wbuf->locked = old_wbuf->locked;
702 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
704 dev->mem_start = (unsigned long) buf_base (new_wbuf);
705 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
706 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
707 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
709 * Update the parameters for the new buffer sizes
711 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
712 ppp->escape = 0; /* No pending escape character */
722 ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
723 ppp->flags &= ~SC_XMIT_BUSY;
725 restore_flags(flags);
727 * Release old buffer pointers
729 ppp_free_buf (old_rbuf);
730 ppp_free_buf (old_wbuf);
731 ppp_free_buf (old_cbuf);
732 ppp_free_buf (old_tbuf);
737 * CCP is down; free (de)compressor state if necessary.
741 ppp_ccp_closed (struct ppp *ppp)
747 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
748 restore_flags(flags);
749 if (ppp->flags & SC_DEBUG)
750 printk(KERN_DEBUG "%s: ccp closed\n", ppp->name);
751 if (ppp->sc_xc_state) {
752 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
753 ppp->sc_xc_state = NULL;
756 if (ppp->sc_rc_state) {
757 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
758 ppp->sc_rc_state = NULL;
763 * Called to release all of the information in the current PPP structure.
765 * It is called when the ppp device goes down or if it is unable to go
770 ppp_release (struct ppp *ppp)
772 struct tty_struct *tty;
775 CHECK_PPP_MAGIC(ppp);
779 if (ppp->flags & SC_DEBUG)
780 printk(KERN_DEBUG "%s released\n", ppp->name);
782 ppp_ccp_closed (ppp);
784 /* Ensure that the pppd process is not hanging on select()/poll() */
785 wake_up_interruptible (&ppp->read_wait);
786 wake_up_interruptible (&ppp->write_wait);
788 if (tty != NULL && tty->disc_data == ppp)
789 tty->disc_data = NULL; /* Break the tty->ppp link */
791 if (dev && dev->flags & IFF_UP) {
792 dev->flags &= ~IFF_UP; /* prevent recursion */
793 dev_close (dev); /* close the device properly */
796 ppp_free_buf (ppp->rbuf);
797 ppp_free_buf (ppp->wbuf);
798 ppp_free_buf (ppp->cbuf);
799 ppp_free_buf (ppp->ubuf);
800 ppp_free_buf (ppp->tbuf);
812 slhc_free (ppp->slcomp);
818 ppp->backup_tty = NULL;
824 * Called when the line discipline is changed to something
825 * else, the tty is closed, or the tty detects a hangup.
829 ppp_tty_close (struct tty_struct *tty)
831 struct ppp *ppp = tty2ppp (tty);
834 if (ppp->magic != PPP_MAGIC) {
835 if (ppp->flags & SC_DEBUG)
837 "ppp: trying to close unopened tty!\n");
841 tty->disc_data = NULL;
842 if (tty == ppp->backup_tty)
846 if (ppp->backup_tty) {
847 ppp->tty = ppp->backup_tty;
850 if (ppp->flags & SC_DEBUG)
851 printk (KERN_INFO "ppp: channel %s closing.\n",
862 * Called when the tty discipline is switched to PPP.
866 ppp_tty_open (struct tty_struct *tty)
868 struct ppp *ppp = tty2ppp (tty);
871 * There should not be an existing table for this slot.
874 if (ppp->flags & SC_DEBUG)
876 "ppp_tty_open: gack! tty already associated to %s!\n",
877 ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
882 * Allocate the structure from the system
884 ppp = ppp_find(current->pid);
887 * If we are taking over a ppp unit which is currently
888 * connected to a loopback pty, there's not much to do.
891 tty->disc_data = ppp;
897 if (ppp->flags & SC_DEBUG)
898 printk (KERN_ERR "ppp_alloc failed\n");
902 * Initialize the control block
904 ppp_init_ctrl_blk (ppp);
905 tty->disc_data = ppp;
908 * Allocate space for the default VJ header compression slots
910 ppp->slcomp = slhc_init (16, 16);
911 if (ppp->slcomp == NULL) {
912 if (ppp->flags & SC_DEBUG)
913 printk (KERN_ERR "ppp_tty_open: "
914 "no space for compression buffers!\n");
919 * Allocate space for the MTU and MRU buffers
921 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
926 * Allocate space for a user level buffer
928 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
929 if (ppp->ubuf == NULL) {
930 if (ppp->flags & SC_DEBUG)
931 printk (KERN_ERR "ppp_tty_open: "
932 "no space for user receive buffer\n");
937 if (ppp->flags & SC_DEBUG)
938 printk (KERN_INFO "ppp: channel %s open\n",
941 for (indx = 0; indx < NUM_NP; ++indx)
942 ppp->sc_npmode[indx] = NPMODE_PASS;
947 * Flush any pending characters in the driver and discipline.
949 if (tty->ldisc.flush_buffer)
950 tty->ldisc.flush_buffer (tty);
952 if (tty->driver.flush_buffer)
953 tty->driver.flush_buffer (tty);
958 * Local function to send the next portion of the buffer.
960 * Called by the tty driver's tty_wakeup function should it be entered
961 * because the partial buffer was transmitted.
963 * Called by kick_tty to send the initial portion of the buffer.
965 * Completion processing of the buffer transmission is handled here.
969 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
970 struct ppp_buffer *xbuf)
972 register int count, actual;
976 CHECK_BUF_MAGIC(xbuf);
978 * Prevent re-entrancy by ensuring that this routine is called only once.
982 if (ppp->flags & SC_XMIT_BUSY) {
983 restore_flags(flags);
986 ppp->flags |= SC_XMIT_BUSY;
987 restore_flags(flags);
989 * Send the next block of data to the modem
991 count = xbuf->count - xbuf->tail;
992 actual = tty->driver.write (tty, 0,
993 buf_base (xbuf) + xbuf->tail, count);
995 * Terminate transmission of any block which may have an error.
996 * This could occur should the carrier drop.
999 ppp->stats.ppp_oerrors++;
1002 ppp->bytes_sent += actual;
1004 * If the buffer has been transmitted then clear the indicators.
1006 xbuf->tail += actual;
1007 if (actual == count) {
1009 ppp->flags &= ~SC_XMIT_BUSY;
1011 * Complete the transmission on the current buffer.
1015 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1019 * If the completed buffer came from the device write, then complete the
1020 * transmission block.
1022 ppp2dev (ppp)->tbusy = 0;
1025 * Wake up the transmission queue for all completion events.
1027 wake_up_interruptible (&ppp->write_wait);
1029 * Look at the priorities. Choose a daemon write over the device driver.
1040 * If there is a pending buffer then transmit it now.
1043 ppp->flags &= ~SC_XMIT_BUSY;
1044 ppp_kick_tty (ppp, xbuf);
1045 restore_flags(flags);
1048 restore_flags(flags);
1052 * Clear the re-entry flag
1054 save_flags(flags); /* &=~ may not be atomic */
1056 ppp->flags &= ~SC_XMIT_BUSY;
1057 restore_flags(flags);
1061 * This function is called by the tty driver when the transmit buffer has
1062 * additional space. It is used by the ppp code to continue to transmit
1063 * the current buffer should the buffer have been partially sent.
1065 * In addition, it is used to send the first part of the buffer since the
1066 * logic and the inter-locking would be identical.
1070 ppp_tty_wakeup (struct tty_struct *tty)
1072 struct ppp_buffer *xbuf;
1073 struct ppp *ppp = tty2ppp (tty);
1079 if (tty != ppp->tty) {
1080 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1084 * Ensure that there is a transmission pending. Clear the re-entry flag if
1085 * there is no pending buffer. Otherwise, send the buffer.
1089 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1091 ppp_tty_wakeup_code (ppp, tty, xbuf);
1095 * This function is called to transmit a buffer to the remote. The buffer
1096 * is placed on the pending queue if there is presently a buffer being
1097 * sent or it is transmitted with the aid of ppp_tty_wakeup.
1101 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
1103 unsigned long flags;
1106 CHECK_BUF_MAGIC(xbuf);
1113 * Control the flags which are best performed with the interrupts masked.
1118 * If the transmitter is busy then place the buffer on the appropriate
1121 if (ppp->xbuf != NULL) {
1122 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1126 restore_flags (flags);
1130 * If the transmitter is not busy then this is the highest priority frame
1132 ppp->flags &= ~SC_XMIT_BUSY;
1133 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1135 restore_flags (flags);
1137 * Do the "tty wakeup_code" to actually send this buffer.
1139 ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1142 /*************************************************************
1144 * The following functions handle input that arrives from
1145 * the TTY. It recognizes PPP frames and either hands them
1146 * to the network layer or queues them for delivery to a
1147 * user process reading this TTY.
1148 *************************************************************/
1151 * Callback function from tty driver. Return the amount of space left
1152 * in the receiver's buffer to decide if remote transmitter is to be
1157 ppp_tty_room (struct tty_struct *tty)
1159 return 65536; /* We can handle an infinite amount of data. :-) */
1163 * Callback function when data is available at the tty driver.
1166 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1167 char *flags, int count)
1169 register struct ppp *ppp = tty2ppp (tty);
1170 register struct ppp_buffer *buf = NULL;
1176 * This can happen if stuff comes in on the backup tty.
1178 if (ppp == 0 || tty != ppp->tty)
1181 * Fetch the pointer to the buffer. Be careful about race conditions.
1187 * Verify the table pointer and ensure that the line is
1188 * still in PPP discipline.
1190 if (ppp->magic != PPP_MAGIC) {
1191 if (ppp->flags & SC_DEBUG)
1193 "PPP: tty_receive called but couldn't find "
1199 * Print the buffer if desired
1201 if (ppp->flags & SC_LOG_RAWIN)
1202 ppp_print_buffer ("receive buffer", data, count);
1205 * Collect the character and error condition for the character. Set the toss
1206 * flag for the first character error.
1208 while (count-- > 0) {
1212 if (*flags && ppp->toss == 0) {
1214 switch (ppp->toss) {
1216 ++ppp->estats.rx_fifo_errors;
1220 ++ppp->estats.rx_frame_errors;
1228 * Set the flags for d7 being 0/1 and parity being even/odd so that
1229 * the normal processing would have all flags set at the end of the
1230 * session. A missing flag bit indicates an error condition.
1233 #ifdef CHECK_CHARACTERS
1235 ppp->flags |= SC_RCV_B7_1;
1237 ppp->flags |= SC_RCV_B7_0;
1239 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1240 ppp->flags |= SC_RCV_ODDP;
1242 ppp->flags |= SC_RCV_EVNP;
1245 * Branch on the character.
1249 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1250 * then the block is to be ignored. In addition, characters before the very
1251 * first FLAG are also tossed by this procedure.
1253 case PPP_FLAG: /* PPP_FLAG: end of frame */
1254 ppp->stats.ppp_ibytes += ppp->rbuf->count;
1258 * Process frames which are not to be ignored. If the processing failed,
1259 * then clean up the VJ tables.
1261 if (ppp_doframe (ppp) == 0) {
1262 ++ppp->stats.ppp_ierrors;
1263 slhc_toss (ppp->slcomp);
1266 * Reset all indicators for the new frame to follow.
1269 buf->fcs = PPP_INITFCS;
1274 * All other characters in the data come here. If the character is in the
1275 * receive mask then ignore the character.
1278 /* If we're tossing, look no further. */
1282 /* If this is a control char to be ignored, do so */
1283 if (in_rmap (ppp, chr))
1287 * Modify the next character if preceded by escape.
1288 * The escape character (0x7d) could be an escaped
1289 * 0x5d, if it follows an escape :-)
1294 } else if (chr == PPP_ESCAPE) {
1295 ppp->escape = PPP_TRANS;
1300 * Decompress A/C and protocol compression here.
1302 if (buf->count == 0 && chr != PPP_ALLSTATIONS) {
1303 buf_base(buf)[0] = PPP_ALLSTATIONS;
1304 buf_base(buf)[1] = PPP_UI;
1307 if (buf->count == 2 && (chr & 1) != 0) {
1308 buf_base(buf)[2] = 0;
1312 * If the count sent is within reason then store the character, bump the
1313 * count, and update the FCS for the character.
1315 if (buf->count < buf->size) {
1316 buf_base (buf)[buf->count++] = chr;
1317 buf->fcs = PPP_FCS (buf->fcs, chr);
1321 * The peer sent too much data. Set the flags to discard the current frame
1322 * and wait for the re-synchronization FLAG to be sent.
1324 ++ppp->estats.rx_length_errors;
1331 /* on entry, a received frame is in ppp->rbuf.bufr
1332 check it and dispose as appropriate */
1335 ppp_doframe (struct ppp *ppp)
1337 __u8 *data = buf_base (ppp->rbuf);
1338 int count = ppp->rbuf->count;
1344 CHECK_BUF_MAGIC(ppp->rbuf);
1347 * If there is a pending error from the receiver then log it and discard
1348 * the damaged frame.
1351 if ((ppp->flags & SC_DEBUG) && count > 0)
1353 "ppp_toss: tossing frame, reason = %x\n",
1358 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1359 * follows each frame.
1364 * Generate an error if the frame is too small.
1366 if (count < PPP_HDRLEN + 2) {
1367 if (ppp->flags & SC_DEBUG)
1369 "ppp: got runt ppp frame, %d chars\n", count);
1370 ++ppp->estats.rx_length_errors;
1374 * Verify the CRC of the frame and discard the CRC characters from the
1375 * end of the buffer.
1377 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1378 if (ppp->flags & SC_DEBUG) {
1380 "ppp: frame with bad fcs, length = %d\n",
1382 ppp_print_buffer("bad frame", data, count);
1384 ++ppp->estats.rx_crc_errors;
1387 count -= 2; /* ignore the fcs characters */
1389 * Obtain the protocol from the frame
1391 proto = PPP_PROTOCOL(data);
1393 * Process the active decompressor.
1395 if ((ppp->sc_rc_state != (void *) 0) &&
1396 (ppp->flags & SC_DECOMP_RUN) &&
1397 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1398 if (proto == PPP_COMP) {
1400 * If the frame is compressed then decompress it.
1402 new_data = kmalloc (ppp->mru + PPP_HDRLEN, GFP_ATOMIC);
1403 if (new_data == NULL) {
1404 if (ppp->flags & SC_DEBUG)
1406 "ppp_doframe: no memory\n");
1407 new_count = DECOMP_ERROR;
1409 new_count = (*ppp->sc_rcomp->decompress)
1410 (ppp->sc_rc_state, data, count,
1411 new_data, ppp->mru + PPP_HDRLEN);
1413 switch (new_count) {
1415 ppp_doframe_lower (ppp, new_data, new_count);
1420 ppp->flags |= SC_DC_ERROR;
1423 case DECOMP_FATALERROR:
1424 ppp->flags |= SC_DC_FERROR;
1425 if (ppp->flags & SC_DEBUG)
1426 printk(KERN_ERR "ppp: fatal decomp error\n");
1430 * Log the error condition and discard the frame.
1434 slhc_toss (ppp->slcomp);
1435 ++ppp->stats.ppp_ierrors;
1438 * The frame is not special. Pass it through the compressor without
1439 * actually compressing the data
1441 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1446 * Process the uncompressed frame.
1448 ppp_doframe_lower (ppp, data, count);
1452 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
1454 __u16 proto = PPP_PROTOCOL (data);
1455 ppp_proto_type *proto_ptr;
1459 * Ignore empty frames
1461 if (count <= PPP_HDRLEN)
1464 * Count the frame and print it
1466 ++ppp->stats.ppp_ipackets;
1467 if (ppp->flags & SC_LOG_INPKT)
1468 ppp_print_buffer ("receive frame", data, count);
1470 * Find the procedure to handle this protocol. The last one is marked
1471 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1473 proto_ptr = proto_list;
1474 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1477 * Update the appropriate statistic counter.
1479 if ((*proto_ptr->func) (ppp, proto,
1481 count - PPP_HDRLEN))
1482 ppp->stats.ppp_ioctects += count;
1484 ++ppp->stats.ppp_discards;
1488 * Put the input frame into the networking system for the indicated protocol
1492 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1494 sk_buff *skb = dev_alloc_skb (count);
1496 * Generate a skb buffer for the new frame.
1499 if (ppp->flags & SC_DEBUG)
1501 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1502 ppp2dev (ppp)->name);
1506 * Move the received data from the input buffer to the skb buffer.
1508 skb->dev = ppp2dev (ppp); /* We are the device */
1509 skb->protocol = proto;
1510 skb->mac.raw = skb_data(skb);
1511 memcpy (skb_put(skb,count), data, count); /* move data */
1513 * Tag the frame and kick it to the proper receive routine
1515 #if LINUX_VERSION_CODE < VERSION(2,1,15)
1519 ppp->last_recv = jiffies;
1525 * Process the receipt of an IP frame
1529 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1532 if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1533 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1534 return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1539 * Process the receipt of an IPX frame
1543 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1546 if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1547 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1552 * Process the receipt of an VJ Compressed frame
1556 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
1557 __u8 *data, int count)
1560 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1561 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1562 if (new_count >= 0) {
1563 return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1565 if (ppp->flags & SC_DEBUG)
1567 "ppp: error in VJ decompression\n");
1573 * Process the receipt of an VJ Un-compressed frame
1577 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
1578 __u8 *data, int count)
1581 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1582 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1583 return rcv_proto_ip (ppp, PPP_IP, data, count);
1585 if (ppp->flags & SC_DEBUG)
1587 "ppp: error in VJ memorizing\n");
1593 * Receive all unclassified protocols.
1597 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
1598 __u8 *data, int len)
1601 register int current_idx;
1605 buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1606 current_idx &= ppp->ubuf->size; \
1607 if (current_idx == ppp->ubuf->tail) \
1613 * The total length includes the protocol data.
1614 * Lock the user information buffer.
1616 if (test_and_set_bit (0, &ppp->ubuf->locked)) {
1617 if (ppp->flags & SC_DEBUG)
1619 "ppp: rcv_proto_unknown: can't get lock\n");
1621 CHECK_BUF_MAGIC(ppp->ubuf);
1622 current_idx = ppp->ubuf->head;
1624 * Insert the buffer length (not counted), the protocol, and the data
1634 while (totlen-- > 0) {
1639 * The frame is complete. Update the head pointer and wakeup the pppd
1642 ppp->ubuf->head = current_idx;
1644 clear_bit (0, &ppp->ubuf->locked);
1645 wake_up_interruptible (&ppp->read_wait);
1646 if (ppp->tty->fasync != NULL)
1647 kill_fasync (ppp->tty->fasync, SIGIO);
1651 * The buffer is full. Unlock the header
1654 clear_bit (0, &ppp->ubuf->locked);
1655 if (ppp->flags & SC_DEBUG)
1657 "ppp: rcv_proto_unknown: buffer overflow\n");
1660 * Discard the frame. There are no takers for this protocol.
1662 if (ppp->flags & SC_DEBUG)
1664 "ppp: rcv_proto_unknown: dropping packet\n");
1669 * Handle a CCP packet.
1671 * The CCP packet is passed along to the pppd process just like any
1672 * other PPP frame. The difference is that some processing needs to be
1673 * immediate or the compressors will become confused on the peer.
1676 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
1678 int slen = CCP_LENGTH(dp);
1679 __u8 *opt = dp + CCP_HDRLEN;
1680 int opt_len = slen - CCP_HDRLEN;
1681 unsigned long flags;
1687 switch (CCP_CODE(dp)) {
1692 * CCP must be going down - disable compression
1694 if (ppp->flags & SC_CCP_UP) {
1696 ppp->flags &= ~(SC_CCP_UP |
1703 if ((ppp->flags & SC_CCP_OPEN) == 0)
1705 if (ppp->flags & SC_CCP_UP)
1707 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1709 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1712 * we're agreeing to send compressed packets.
1715 if (ppp->sc_xc_state == NULL)
1718 if ((*ppp->sc_xcomp->comp_init)
1722 ppp2dev (ppp)->base_addr,
1724 ppp->flags & SC_DEBUG)) {
1725 if (ppp->flags & SC_DEBUG)
1726 printk(KERN_DEBUG "%s: comp running\n",
1729 ppp->flags |= SC_COMP_RUN;
1734 * peer is agreeing to send compressed packets.
1736 if (ppp->sc_rc_state == NULL)
1739 if ((*ppp->sc_rcomp->decomp_init)
1743 ppp2dev (ppp)->base_addr,
1746 ppp->flags & SC_DEBUG)) {
1747 if (ppp->flags & SC_DEBUG)
1748 printk(KERN_DEBUG "%s: decomp running\n",
1751 ppp->flags |= SC_DECOMP_RUN;
1752 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1756 * CCP Reset-ack resets compressors and decompressors as it passes through.
1759 if ((ppp->flags & SC_CCP_UP) == 0)
1763 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN)) {
1764 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1765 if (ppp->flags & SC_DEBUG)
1766 printk(KERN_DEBUG "%s: comp reset\n",
1770 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1771 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1772 if (ppp->flags & SC_DEBUG)
1773 printk(KERN_DEBUG "%s: decomp reset\n",
1776 ppp->flags &= ~SC_DC_ERROR;
1781 restore_flags(flags);
1785 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
1788 ppp_proto_ccp (ppp, dp, len, 1);
1789 return rcv_proto_unknown (ppp, proto, dp, len);
1793 * Handle a LQR packet.
1797 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
1799 return rcv_proto_unknown (ppp, proto, data, len);
1802 /*************************************************************
1803 * LINE DISCIPLINE SUPPORT
1804 * The following functions form support user programs
1805 * which read and write data on a TTY with the PPP line
1806 * discipline. Reading is done from a circular queue,
1807 * filled by the lower TTY levels.
1808 *************************************************************/
1810 /* read a PPP frame from the us_rbuff circular buffer,
1811 waiting if necessary
1815 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
1818 struct ppp *ppp = tty2ppp (tty);
1825 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1826 ppp->ubuf->tail &= ppp->ubuf->size; \
1830 * Validate the pointers
1835 /* if (ppp->magic != PPP_MAGIC)
1841 * Before we attempt to write the frame to the user, ensure that the
1842 * user has access to the pages for the total buffer length.
1844 error = verify_area (VERIFY_WRITE, buf, nr);
1849 * Acquire the read lock.
1852 ppp = tty2ppp (tty);
1853 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
1857 if (test_and_set_bit (0, &ppp->ubuf->locked) != 0) {
1859 if (ppp->flags & SC_DEBUG)
1861 "ppp_tty_read: sleeping(ubuf)\n");
1863 current->timeout = 0;
1864 current->state = TASK_INTERRUPTIBLE;
1867 if (current->signal & ~current->blocked)
1873 * Fetch the length of the buffer from the first two bytes.
1875 if (ppp->ubuf->head == ppp->ubuf->tail)
1887 * If there is no length then wait for the data to arrive.
1890 clear_bit (0, &ppp->ubuf->locked);
1891 if (file->f_flags & O_NONBLOCK)
1893 current->timeout = 0;
1895 if (ppp->flags & SC_DEBUG)
1897 "ppp_tty_read: sleeping(read_wait)\n");
1899 interruptible_sleep_on (&ppp->read_wait);
1900 if (current->signal & ~current->blocked)
1905 * Ensure that the frame will fit within the caller's buffer. If not, then
1906 * discard the frame from the input buffer.
1909 /* Can't copy it, update us_rbuff_head */
1911 if (ppp->flags & SC_DEBUG)
1913 "ppp: read of %u bytes too small for %d "
1914 "frame\n", nr, len + 2);
1915 ppp->stats.ppp_ierrors++;
1921 * Fake the insertion of the ADDRESS and CONTROL information because these
1922 * were not saved in the buffer.
1924 PUT_USER (error, (u_char) PPP_ALLSTATIONS, buf);
1928 PUT_USER (error, (u_char) PPP_UI, buf);
1934 * Copy the received data from the buffer to the caller's area.
1936 ret = len + 2; /* Account for ADDRESS and CONTROL bytes */
1939 PUT_USER (error, c, buf);
1945 clear_bit (0, &ppp->ubuf->locked);
1947 if (ppp->flags & SC_DEBUG)
1948 printk (KERN_DEBUG "ppp_tty_read: passing %d bytes up\n", ret);
1953 ppp->ubuf->tail += len;
1954 ppp->ubuf->tail &= ppp->ubuf->size;
1955 clear_bit (0, &ppp->ubuf->locked);
1960 /* stuff a character into the transmit buffer, using PPP's way of escaping
1962 also, update fcs to take account of new character */
1965 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1969 * The buffer should not be full.
1971 if (ppp->flags & SC_DEBUG) {
1972 if ((buf->count < 0) || (buf->count > 3000))
1973 printk (KERN_DEBUG "ppp_stuff_char: %d %x\n",
1974 (unsigned int) buf->count,
1975 (unsigned int) chr);
1978 * Update the FCS and if the character needs to be escaped, do it.
1980 buf->fcs = PPP_FCS (buf->fcs, chr);
1981 if (in_xmap (ppp, chr)) {
1983 ins_char (buf, PPP_ESCAPE);
1986 * Add the character to the buffer.
1988 ins_char (buf, chr);
1992 * Procedure to encode the data with the proper escaping and send the
1993 * data to the remote system.
1997 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
1998 __u8 *data, int count, int non_ip)
2001 int address, control;
2005 CHECK_BUF_MAGIC(buf);
2006 ++ppp->stats.ppp_opackets;
2007 ppp->stats.ppp_ooctects += count;
2010 * Insert the leading FLAG character
2014 if (non_ip || flag_time == 0)
2015 ins_char (buf, PPP_FLAG);
2017 if (jiffies - ppp->last_xmit >= flag_time)
2018 ins_char (buf, PPP_FLAG);
2020 ppp->last_xmit = jiffies;
2021 buf->fcs = PPP_INITFCS;
2023 * Emit the address/control information if needed
2025 address = PPP_ADDRESS (data);
2026 control = PPP_CONTROL (data);
2027 proto = PPP_PROTOCOL (data);
2029 if (address != PPP_ALLSTATIONS ||
2030 control != PPP_UI ||
2031 (ppp->flags & SC_COMP_AC) == 0) {
2032 ppp_stuff_char (ppp, buf, address);
2033 ppp_stuff_char (ppp, buf, control);
2036 * Emit the protocol (compressed if possible)
2038 if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
2039 ppp_stuff_char (ppp, buf, proto >> 8);
2041 ppp_stuff_char (ppp, buf, proto);
2049 ppp_stuff_char (ppp, buf, *data++);
2051 * Add the trailing CRC and the final flag character
2053 write_fcs = buf->fcs ^ 0xFFFF;
2054 ppp_stuff_char (ppp, buf, write_fcs);
2055 ppp_stuff_char (ppp, buf, write_fcs >> 8);
2057 * Add the trailing flag character
2059 ins_char (buf, PPP_FLAG);
2061 * Send the block to the tty driver.
2063 ppp->stats.ppp_obytes += buf->count;
2064 ppp_kick_tty (ppp, buf);
2068 * Compress and send an frame to the peer.
2070 * Return 0 if frame was queued for transmission.
2071 * 1 if frame must be re-queued for later driver support.
2075 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
2076 __u8 *data, int count)
2079 int address, control;
2084 CHECK_BUF_MAGIC(buf);
2088 if (ppp->flags & SC_LOG_OUTPKT)
2089 ppp_print_buffer ("write frame", data, count);
2091 * Determine if the frame may be compressed. Attempt to compress the
2092 * frame if possible.
2094 proto = PPP_PROTOCOL (data);
2095 address = PPP_ADDRESS (data);
2096 control = PPP_CONTROL (data);
2098 if (((ppp->flags & SC_COMP_RUN) != 0) &&
2099 (ppp->sc_xc_state != (void *) 0) &&
2100 (address == PPP_ALLSTATIONS) &&
2101 (control == PPP_UI) &&
2102 (proto != PPP_LCP) &&
2103 (proto != PPP_CCP)) {
2104 new_data = kmalloc (ppp->mtu, GFP_ATOMIC);
2105 if (new_data == NULL) {
2106 if (ppp->flags & SC_DEBUG)
2108 "ppp_dev_xmit_frame: no memory\n");
2112 new_count = (*ppp->sc_xcomp->compress)
2113 (ppp->sc_xc_state, data, new_data, count, ppp->mtu);
2115 if (new_count > 0 && (ppp->flags & SC_CCP_UP)) {
2116 ppp_dev_xmit_lower (ppp, buf, new_data, new_count, 0);
2121 * The frame could not be compressed, or it could not be sent in
2122 * compressed form because CCP is not yet up.
2127 * Go to the escape encoding
2129 ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2134 * Revise the tty frame for specific protocols.
2138 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
2142 switch (PPP_PROTOCOL (data)) {
2144 * Update the LQR frame with the current MIB information. This saves having
2145 * the daemon read old MIB data from the driver.
2148 len = 48; /* total size of this frame */
2149 p = (__u8 *) &data [40]; /* Point to last two items. */
2150 p = store_long (p, ppp->stats.ppp_opackets + 1);
2151 p = store_long (p, ppp->stats.ppp_ooctects + len);
2154 * Outbound compression frames
2171 * write a frame with NR chars from BUF to TTY
2172 * we have to put the FCS field on ourselves
2176 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
2179 struct ppp *ppp = tty2ppp (tty);
2184 * Verify the pointers.
2189 if (ppp->magic != PPP_MAGIC)
2194 * Ensure that the caller does not wish to send too much.
2196 if (count > PPP_MTU + PPP_HDRLEN) {
2197 if (ppp->flags & SC_DEBUG)
2198 printk (KERN_WARNING
2199 "ppp_tty_write: truncating user packet "
2200 "from %u to mtu %d\n", count,
2201 PPP_MTU + PPP_HDRLEN);
2202 count = PPP_MTU + PPP_HDRLEN;
2205 * Allocate a buffer for the data and fetch it from the user space.
2207 new_data = kmalloc (count, GFP_KERNEL);
2208 if (new_data == NULL) {
2209 if (ppp->flags & SC_DEBUG)
2211 "ppp_tty_write: no memory\n");
2215 * Retrieve the user's buffer
2217 COPY_FROM_USER (error, new_data, data, count);
2223 * lock this PPP unit so we will be the only writer;
2224 * sleep if necessary
2226 while (lock_buffer (ppp->tbuf) != 0) {
2227 current->timeout = 0;
2229 if (ppp->flags & SC_DEBUG)
2230 printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2232 interruptible_sleep_on (&ppp->write_wait);
2234 ppp = tty2ppp (tty);
2235 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
2236 || tty != ppp->tty) {
2241 if (current->signal & ~current->blocked) {
2247 * Change the LQR frame
2249 count = send_revise_frame (ppp, new_data, count);
2253 if (PPP_PROTOCOL(new_data) == PPP_IP) {
2255 * IP frames can be sent by pppd when we're doing
2256 * demand-dialling. We send them via ppp_dev_xmit_ip
2257 * to make sure that VJ compression happens properly.
2259 ppp_dev_xmit_ip(ppp, ppp->tbuf, new_data + PPP_HDRLEN,
2260 count - PPP_HDRLEN, NPMODE_PASS);
2263 ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2271 * Process the BSD compression IOCTL event for the tty device.
2275 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2277 struct compressor *cp;
2278 struct ppp_option_data data;
2282 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2283 unsigned long flags;
2286 * Fetch the compression parameters
2288 COPY_FROM_USER (error,
2298 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2299 nb = CCP_MAX_OPTION_LENGTH;
2301 COPY_FROM_USER (error,
2309 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2314 ppp->flags &= ~(SC_COMP_RUN | SC_DECOMP_RUN);
2315 restore_flags(flags);
2317 cp = find_compressor (ccp_option[0]);
2318 if (cp != (struct compressor *) 0) {
2320 * Found a handler for the protocol - try to allocate
2321 * a compressor or decompressor.
2324 if (data.transmit) {
2325 if (ppp->sc_xc_state != NULL)
2326 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2329 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2331 if (ppp->sc_xc_state == NULL) {
2332 if (ppp->flags & SC_DEBUG)
2333 printk(KERN_DEBUG "%s: comp_alloc failed\n",
2337 if (ppp->flags & SC_DEBUG)
2338 printk(KERN_DEBUG "%s: comp_alloc -> %p\n",
2339 ppp->name, ppp->sc_xc_state);
2342 if (ppp->sc_rc_state != NULL)
2343 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2345 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2346 if (ppp->sc_rc_state == NULL) {
2347 if (ppp->flags & SC_DEBUG)
2348 printk(KERN_DEBUG "%s: decomp_alloc failed\n",
2352 if (ppp->flags & SC_DEBUG)
2353 printk(KERN_DEBUG "%s: decomp_alloc -> %p\n",
2354 ppp->name, ppp->sc_rc_state);
2360 if (ppp->flags & SC_DEBUG)
2361 printk(KERN_DEBUG "%s: no compressor for [%x %x %x], %x\n",
2362 ppp->name, ccp_option[0], ccp_option[1],
2364 return (-EINVAL); /* no handler found */
2368 * Process the IOCTL event for the tty device.
2372 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
2373 unsigned int param2, unsigned long param3)
2375 struct ppp *ppp = tty2ppp (tty);
2376 register int temp_i = 0;
2379 * Verify the status of the PPP device.
2384 if (ppp->magic != PPP_MAGIC)
2389 * The user must have an euid of root to do these requests.
2398 GET_USER (error, temp_i, (int *) param3);
2401 if (ppp->flags & SC_DEBUG)
2403 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2405 if (ppp->mru != temp_i)
2406 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2412 temp_i = (ppp->flags & SC_MASK);
2413 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2414 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2415 SC_RCV_ODDP | SC_RCV_EVNP;
2417 PUT_USER (error, temp_i, (int *) param3);
2420 * Set the flags for the various options
2423 GET_USER (error, temp_i, (int *) param3);
2427 temp_i |= (ppp->flags & ~SC_MASK);
2429 if ((ppp->flags & SC_CCP_OPEN) &&
2430 (temp_i & SC_CCP_OPEN) == 0)
2431 ppp_ccp_closed (ppp);
2433 if ((ppp->flags | temp_i) & SC_DEBUG)
2435 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2436 ppp->flags = temp_i;
2439 * Set the compression mode
2441 case PPPIOCSCOMPRESS:
2442 error = ppp_set_compression (ppp,
2443 (struct ppp_option_data *) param3);
2446 * Retrieve the transmit async map
2448 case PPPIOCGASYNCMAP:
2449 PUT_USER (error, ppp->xmit_async_map[0], (int *) param3);
2452 * Set the transmit async map
2454 case PPPIOCSASYNCMAP:
2455 GET_USER (error, temp_i, (int *) param3);
2458 ppp->xmit_async_map[0] = temp_i;
2459 if (ppp->flags & SC_DEBUG)
2461 "ppp_tty_ioctl: set xmit asyncmap %x\n",
2462 ppp->xmit_async_map[0]);
2465 * Set the receive async map
2467 case PPPIOCSRASYNCMAP:
2468 GET_USER (error, temp_i, (int *) param3);
2471 ppp->recv_async_map = temp_i;
2472 if (ppp->flags & SC_DEBUG)
2474 "ppp_tty_ioctl: set rcv asyncmap %x\n",
2475 ppp->recv_async_map);
2478 * Obtain the unit number for this device.
2481 PUT_USER (error, ppp2dev (ppp)->base_addr, (int *) param3);
2484 if (ppp->flags & SC_DEBUG)
2486 "ppp_tty_ioctl: get unit: %ld\n",
2487 ppp2dev (ppp)->base_addr);
2490 * Set the debug level
2493 GET_USER (error, temp_i, (int *) param3);
2496 temp_i = (temp_i & 0x1F) << 16;
2497 temp_i |= (ppp->flags & ~0x1F0000);
2499 if ((ppp->flags | temp_i) & SC_DEBUG)
2501 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2502 ppp->flags = temp_i;
2505 * Get the debug level
2508 temp_i = (ppp->flags >> 16) & 0x1F;
2509 PUT_USER (error, temp_i, (int *) param3);
2512 * Get the times since the last send/receive frame operation
2516 struct ppp_idle cur_ddinfo;
2518 /* change absolute times to relative times. */
2519 cur_ddinfo.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
2520 cur_ddinfo.recv_idle = (jiffies - ppp->last_recv) / HZ;
2521 COPY_TO_USER (error,
2524 sizeof (cur_ddinfo));
2528 * Retrieve the extended async map
2530 case PPPIOCGXASYNCMAP:
2531 COPY_TO_USER (error,
2533 ppp->xmit_async_map,
2534 sizeof (ppp->xmit_async_map));
2537 * Set the async extended map
2539 case PPPIOCSXASYNCMAP:
2543 COPY_FROM_USER (error,
2550 temp_tbl[1] = 0x00000000;
2551 temp_tbl[2] &= ~0x40000000;
2552 temp_tbl[3] |= 0x60000000;
2554 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2555 (temp_tbl[4] & temp_tbl[5]) != 0 ||
2556 (temp_tbl[6] & temp_tbl[7]) != 0)
2559 memcpy (ppp->xmit_async_map,
2561 sizeof (ppp->xmit_async_map));
2563 if (ppp->flags & SC_DEBUG)
2565 "ppp_tty_ioctl: set xasyncmap\n");
2570 * Set the maximum VJ header compression slot number.
2573 GET_USER (error, temp_i, (int *) param3);
2576 temp_i = (temp_i & 255) + 1;
2577 if (ppp->flags & SC_DEBUG)
2579 "ppp_tty_ioctl: set maxcid to %d\n",
2581 if (ppp->slcomp != NULL)
2582 slhc_free (ppp->slcomp);
2583 ppp->slcomp = slhc_init (16, temp_i);
2585 if (ppp->slcomp == NULL) {
2586 if (ppp->flags & SC_DEBUG)
2588 "ppp: no space for compression buffers!\n");
2594 case PPPIOCXFERUNIT:
2595 ppp->backup_tty = tty;
2596 ppp->sc_xfer = current->pid;
2604 COPY_FROM_USER (error,
2611 switch (npi.protocol) {
2613 npi.protocol = NP_IP;
2616 if (ppp->flags & SC_DEBUG)
2617 printk(KERN_DEBUG "pppioc[gs]npmode: "
2618 "invalid proto %d\n", npi.protocol);
2625 if (param2 == PPPIOCGNPMODE) {
2626 npi.mode = ppp->sc_npmode[npi.protocol];
2628 COPY_TO_USER (error,
2635 ppp->sc_npmode[npi.protocol] = npi.mode;
2636 if (ppp->flags & SC_DEBUG)
2637 printk(KERN_DEBUG "ppp: set np %d to %d\n",
2638 npi.protocol, npi.mode);
2639 ppp2dev(ppp)->tbusy = 0;
2644 * Allow users to read, but not set, the serial port parameters
2648 error = n_tty_ioctl (tty, file, param2, param3);
2653 int count = ppp->ubuf->tail - ppp->ubuf->head;
2655 count += (ppp->ubuf->size + 1);
2656 PUT_USER (error, count, (int *) param3);
2660 * All other ioctl() events will come here.
2663 if (ppp->flags & SC_DEBUG)
2665 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2669 error = -ENOIOCTLCMD;
2678 * Process the select() (or poll()) statement for the PPP device.
2681 #if LINUX_VERSION_CODE < VERSION(2,1,23)
2683 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2684 struct file *filp, int sel_type, select_table * wait)
2686 struct ppp *ppp = tty2ppp (tty);
2689 * Verify the status of the PPP device.
2694 if (ppp->magic != PPP_MAGIC || tty != ppp->tty)
2699 * Branch on the type of select mode. A read request must lock the user
2704 if (test_and_set_bit (0, &ppp->ubuf->locked) == 0) {
2705 /* Test for the presence of data in the queue */
2706 if (ppp->ubuf->head != ppp->ubuf->tail) {
2707 clear_bit (0, &ppp->ubuf->locked);
2710 clear_bit (0, &ppp->ubuf->locked);
2711 } /* fall through */
2713 * Exceptions or read errors.
2716 /* Is this a pty link and the remote disconnected? */
2717 if (tty->flags & (1 << TTY_OTHER_CLOSED))
2720 /* Is this a local link and the modem disconnected? */
2721 if (tty_hung_up_p (filp))
2724 select_wait (&ppp->read_wait, wait);
2728 * Write mode. A write is allowed if there is no current transmission.
2731 if (ppp->tbuf->locked != 0) {
2732 select_wait (&ppp->write_wait, wait);
2740 #else /* 2.1.23 or later */
2743 ppp_tty_poll (struct tty_struct *tty, struct file *filp, poll_table * wait)
2745 struct ppp *ppp = tty2ppp (tty);
2746 unsigned int mask = 0;
2748 if (ppp && ppp->magic == PPP_MAGIC && tty == ppp->tty) {
2751 poll_wait(&ppp->read_wait, wait);
2752 poll_wait(&ppp->write_wait, wait);
2754 /* Must lock the user buffer area while checking. */
2755 CHECK_BUF_MAGIC(ppp->ubuf);
2756 if(test_and_set_bit(0, &ppp->ubuf->locked) == 0) {
2757 if(ppp->ubuf->head != ppp->ubuf->tail)
2758 mask |= POLLIN | POLLRDNORM;
2759 clear_bit(0, &ppp->ubuf->locked);
2761 if(tty->flags & (1 << TTY_OTHER_CLOSED))
2763 if(tty_hung_up_p(filp))
2765 if(ppp->tbuf->locked == 0)
2766 mask |= POLLOUT | POLLWRNORM;
2773 /*************************************************************
2775 * This routine accepts requests from the network layer
2776 * and attempts to deliver the packets.
2777 * It also includes various routines we are compelled to
2778 * have to make the network layer work (arp, etc...).
2779 *************************************************************/
2782 * Callback from the network layer when the device goes up.
2786 ppp_dev_open (struct device *dev)
2788 struct ppp *ppp = dev2ppp (dev);
2791 /* reset POINTOPOINT every time, since dev_close zaps it! */
2792 dev->flags |= IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2795 if (ppp2tty (ppp) == NULL) {
2796 if (ppp->flags & SC_DEBUG)
2798 "ppp: %s not connected to a TTY! can't go open!\n",
2803 if (ppp->flags & SC_DEBUG)
2805 "ppp: channel %s going up for IP packets!\n",
2813 * Callback from the network layer when the ppp device goes down.
2817 ppp_dev_close (struct device *dev)
2819 struct ppp *ppp = dev2ppp (dev);
2821 if (ppp2tty (ppp) == NULL) {
2825 * We don't do anything about the device going down. It is not important
2828 if (ppp->flags & SC_DEBUG)
2830 "ppp: channel %s going down for IP packets!\n",
2837 * IOCTL operation to read the version of the driver.
2841 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2844 char *result = (char *) ifr->ifr_ifru.ifru_data;
2845 int len = strlen (szVersion) + 1;
2847 * Move the version data
2849 COPY_TO_USER (error,
2858 * IOCTL to read the statistics for the pppstats program.
2862 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2864 struct ppp_stats *result, temp;
2867 * Supply the information for the caller. First move the version data
2868 * then move the ppp stats; and finally the vj stats.
2870 memset (&temp, 0, sizeof(temp));
2871 if (dev->flags & IFF_UP) {
2872 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2873 if (ppp->slcomp != NULL) {
2874 temp.vj.vjs_packets = ppp->slcomp->sls_o_compressed+
2875 ppp->slcomp->sls_o_uncompressed;
2876 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2877 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
2878 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
2879 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
2880 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
2881 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2882 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
2886 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2888 COPY_TO_USER (error,
2897 * IOCTL to read the compression statistics for the pppstats program.
2901 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2903 struct ppp_comp_stats *result, temp;
2906 * Supply the information for the caller.
2908 memset (&temp, 0, sizeof(temp));
2909 if (dev->flags & IFF_UP) {
2910 if (ppp->sc_xc_state != NULL)
2911 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2914 if (ppp->sc_rc_state != NULL)
2915 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2919 * Move the data to the caller's buffer
2921 result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2923 COPY_TO_USER (error,
2932 * Callback from the network layer to process the sockioctl functions.
2936 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2938 struct ppp *ppp = dev2ppp (dev);
2941 CHECK_PPP_MAGIC(ppp);
2943 * Process the requests
2947 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2950 case SIOCGPPPCSTATS:
2951 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2955 error = ppp_dev_ioctl_version (ppp, ifr);
2966 * Send an IP frame to the remote with vj header compression.
2968 * Return 0 if frame was queued for transmission.
2969 * 1 if frame must be re-queued for later driver support.
2970 * -1 if frame should be dropped.
2974 ppp_dev_xmit_ip (struct ppp *ppp, struct ppp_buffer *buf,
2975 __u8 *data, int len, enum NPmode npmode)
2980 * Branch on the type of processing for the IP frame.
2988 * We may not send the packet now, so drop it.
2989 * XXX It would be nice to be able to return it to the
2990 * network system to be queued and retransmitted later.
2992 if (ppp->flags & SC_DEBUG)
2993 printk(KERN_DEBUG "%s: returning frame\n",
2999 if (ppp->flags & SC_DEBUG)
3001 "ppp_dev_xmit: npmode = %d on %s\n",
3002 ppp->sc_npmode[NP_IP], ppp->name);
3006 if (ppp->flags & SC_DEBUG)
3007 printk (KERN_WARNING
3008 "ppp_dev_xmit: unknown npmode %d on %s\n",
3009 ppp->sc_npmode[NP_IP], ppp->name);
3013 * At this point, the buffer will be transmitted. There is no other exit.
3015 * Try to compress the header.
3017 if (ppp->flags & SC_COMP_TCP) {
3018 len = slhc_compress (ppp->slcomp, data, len,
3019 buf_base (ppp->cbuf) + PPP_HDRLEN,
3021 (ppp->flags & SC_NO_TCP_CCID) == 0);
3023 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
3024 proto = PPP_VJC_COMP;
3025 data[0] ^= SL_TYPE_COMPRESSED_TCP;
3027 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
3028 proto = PPP_VJC_UNCOMP;
3029 data[0] = (data[0] & 0x0f) | 0x40;
3036 hdr = data - PPP_HDRLEN;
3038 hdr[0] = PPP_ALLSTATIONS;
3043 return ppp_dev_xmit_frame (ppp, buf, hdr, len);
3047 * Send a non-IP data frame (such as an IPX frame) to the remote.
3049 * Return 0 if frame was queued for transmission.
3050 * 1 if frame must be re-queued for later driver support.
3053 ppp_dev_xmit_other (struct device *dev, struct ppp *ppp,
3054 __u8 *data, int len, int proto)
3061 hdr = data - PPP_HDRLEN;
3063 hdr[0] = PPP_ALLSTATIONS;
3065 hdr[2] = proto >> 8;
3068 return ppp_dev_xmit_frame (ppp, ppp->wbuf, hdr, len);
3072 * Send a frame to the remote.
3076 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3080 struct ppp *ppp = dev2ppp (dev);
3081 struct tty_struct *tty = ppp2tty (ppp);
3083 * just a little sanity check.
3086 if (ppp->flags & SC_DEBUG)
3087 printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3091 * Avoid timing problem should tty hangup while data is queued to be sent
3094 dev_kfree_skb (skb, FREE_WRITE);
3098 * Validate the tty interface
3101 if (ppp->flags & SC_DEBUG)
3103 "ppp_dev_xmit: %s not connected to a TTY!\n",
3105 dev_kfree_skb (skb, FREE_WRITE);
3109 * Fetch the pointer to the data
3112 data = skb_data(skb);
3114 if (data == (__u8 *) 0) {
3115 if (ppp->flags & SC_DEBUG)
3116 printk (KERN_CRIT "ppp_dev_xmit: %s Null skb data\n",
3118 dev_kfree_skb (skb, FREE_WRITE);
3122 * Detect a change in the transfer size
3124 if (ppp->mtu != ppp2dev (ppp)->mtu) {
3125 ppp_changedmtu (ppp,
3130 * Acquire the lock on the transmission buffer. If the buffer was busy then
3131 * mark the device as busy.
3132 * We also require that ppp->tbuf be unlocked, in order to serialize
3133 * calls to ppp_dev_xmit_frame (which does compression) and the output
3134 * of frames w.r.t. tty writes from pppd.
3136 CHECK_BUF_MAGIC(ppp->wbuf);
3137 if (ppp->tbuf->locked || lock_buffer (ppp->wbuf) != 0) {
3139 if (ppp->flags & SC_DEBUG)
3140 printk(KERN_DEBUG "dev_xmit blocked, t=%lu w=%lu\n",
3141 ppp->tbuf->locked, ppp->wbuf->locked);
3145 * Look at the protocol in the skb to determine the difference between
3146 * an IP frame and an IPX frame.
3148 switch (ntohs (skb->protocol)) {
3150 answer = ppp_dev_xmit_other (dev, ppp, data, len, PPP_IPX);
3154 answer = ppp_dev_xmit_ip (ppp, ppp->wbuf, data, len,
3155 ppp->sc_npmode[NP_IP]);
3158 default: /* All others have no support at this time. */
3159 dev_kfree_skb (skb, FREE_WRITE);
3163 * This is the end of the transmission. Release the buffer if it was sent.
3166 /* packet queued OK */
3167 dev_kfree_skb (skb, FREE_WRITE);
3169 ppp->wbuf->locked = 0;
3171 /* packet should be dropped */
3172 dev_kfree_skb (skb, FREE_WRITE);
3175 /* packet should be queued for later */
3183 * Generate the statistic information for the /proc/net/dev listing.
3186 static struct enet_statistics *
3187 ppp_dev_stats (struct device *dev)
3189 struct ppp *ppp = dev2ppp (dev);
3191 ppp->estats.rx_packets = ppp->stats.ppp_ipackets;
3192 ppp->estats.rx_errors = ppp->stats.ppp_ierrors;
3193 ppp->estats.tx_packets = ppp->stats.ppp_opackets;
3194 ppp->estats.tx_errors = ppp->stats.ppp_oerrors;
3195 #if LINUX_VERSION_CODE >= VERSION(2,1,25)
3196 ppp->estats.rx_bytes = ppp->stats.ppp_ibytes;
3197 ppp->estats.tx_bytes = ppp->stats.ppp_obytes;
3200 return &ppp->estats;
3203 #if LINUX_VERSION_CODE < VERSION(2,1,15)
3204 static int ppp_dev_header (sk_buff *skb, struct device *dev,
3205 __u16 type, void *daddr,
3206 void *saddr, unsigned int len)
3212 ppp_dev_rebuild (void *eth, struct device *dev,
3213 unsigned long raddr, struct sk_buff *skb)
3219 /*************************************************************
3221 * Miscellany called by various functions above.
3222 *************************************************************/
3224 /* Locate the previous instance of the PPP channel */
3226 ppp_find (int pid_value)
3230 /* try to find the device which this pid is already using */
3231 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3232 if (ppp->inuse && ppp->sc_xfer == pid_value) {
3240 /* allocate or create a PPP channel */
3249 /* try to find an free device */
3251 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3252 if (!test_and_set_bit(0, &ppp->inuse))
3257 * There are no available units, so make a new one.
3259 ppp = (struct ppp *) kmalloc (sizeof(struct ppp), GFP_KERNEL);
3262 memset(ppp, 0, sizeof(*ppp));
3264 /* initialize channel control data */
3265 set_bit(0, &ppp->inuse);
3269 ppp->backup_tty = NULL;
3273 ppp_last->next = ppp;
3279 dev->init = ppp_init_dev;
3280 dev->name = ppp->name;
3281 sprintf(dev->name, "ppp%d", if_num);
3282 dev->base_addr = (__u32) if_num;
3283 dev->priv = (void *) ppp;
3285 /* register device so that we can be ifconfig'd */
3286 /* ppp_init_dev() will be called as a side-effect */
3287 status = register_netdev (dev);
3289 printk (KERN_INFO "registered device %s\n", dev->name);
3292 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3295 /* This one will forever be busy as it is not initialized */
3301 * Utility procedures to print a buffer in hex/ascii
3305 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3307 register __u8 next_ch;
3308 static char hex[] = "0123456789ABCDEF";
3310 while (count-- > 0) {
3312 *out++ = hex[(next_ch >> 4) & 0x0F];
3313 *out++ = hex[next_ch & 0x0F];
3319 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3321 register __u8 next_ch;
3323 while (count-- > 0) {
3326 if (next_ch < 0x20 || next_ch > 0x7e)
3330 if (next_ch == '%') /* printk/syslogd has a bug !! */
3338 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
3342 if (name != (__u8 *) NULL)
3343 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3346 memset (line, 32, 44);
3347 ppp_print_hex (line, buf, 8);
3348 ppp_print_char (&line[8 * 3], buf, 8);
3349 printk (KERN_DEBUG "%s\n", line);
3355 memset (line, 32, 44);
3356 ppp_print_hex (line, buf, count);
3357 ppp_print_char (&line[8 * 3], buf, count);
3358 printk (KERN_DEBUG "%s\n", line);
3362 /*************************************************************
3363 * Compressor module interface
3364 *************************************************************/
3366 struct compressor_link {
3367 struct compressor_link *next;
3368 struct compressor *comp;
3371 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3373 static struct compressor *find_compressor (int type)
3375 struct compressor_link *lnk;
3376 unsigned long flags;
3381 lnk = ppp_compressors;
3382 while (lnk != (struct compressor_link *) 0) {
3383 if ((int) (__u8) lnk->comp->compress_proto == type) {
3384 restore_flags(flags);
3390 restore_flags(flags);
3391 return (struct compressor *) 0;
3394 static int ppp_register_compressor (struct compressor *cp)
3396 struct compressor_link *new;
3397 unsigned long flags;
3399 new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3401 if (new == (struct compressor_link *) 0)
3407 if (find_compressor (cp->compress_proto)) {
3408 restore_flags(flags);
3413 new->next = ppp_compressors;
3415 ppp_compressors = new;
3417 restore_flags(flags);
3421 static void ppp_unregister_compressor (struct compressor *cp)
3423 struct compressor_link *prev = (struct compressor_link *) 0;
3424 struct compressor_link *lnk;
3425 unsigned long flags;
3430 lnk = ppp_compressors;
3431 while (lnk != (struct compressor_link *) 0) {
3432 if (lnk->comp == cp) {
3434 prev->next = lnk->next;
3436 ppp_compressors = lnk->next;
3443 restore_flags(flags);
3446 /*************************************************************
3447 * Module support routines
3448 *************************************************************/
3456 /* register our line disciplines */
3457 status = ppp_first_time();
3460 "PPP: ppp_init() failure %d\n", status);
3461 #if LINUX_VERSION_CODE < VERSION(2,1,18)
3463 (void) register_symtab (&ppp_syms);
3469 cleanup_module(void)
3473 struct ppp *ppp, *next_ppp;
3476 * Ensure that the devices are not in operation.
3478 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3479 if (ppp->inuse && ppp->tty != NULL) {
3484 dev = ppp2dev (ppp);
3485 if (dev->start || dev->flags & IFF_UP) {
3491 * Ensure that there are no compressor modules registered
3493 if (ppp_compressors != NULL)
3498 "PPP: device busy, remove delayed\n");
3502 * Release the tty registration of the line discipline so that no new entries
3505 status = tty_register_ldisc (N_PPP, NULL);
3508 "PPP: Unable to unregister ppp line discipline "
3509 "(err = %d)\n", status);
3512 "PPP: ppp line discipline successfully unregistered\n");
3514 * De-register the devices so that there is no problem with them
3516 for (ppp = ppp_list; ppp != 0; ppp = next_ppp) {
3517 next_ppp = ppp->next;
3519 unregister_netdev (&ppp->dev);