3 * Michael Callahan <callahan@maths.ox.ac.uk>
4 * Al Longyear <longyear@netcom.com>
6 * Dynamic PPP devices by Jim Freeman <jfree@caldera.com>.
7 * ppp_tty_receive ``noisy-raise-bug'' fixed by Ove Ewerlid <ewerlid@syscon.uu.se>
9 * ==FILEVERSION 970626==
11 * NOTE TO MAINTAINERS:
12 * If you modify this file at all, please set the number above to the
13 * date of the modification as YYMMDD (year month day).
14 * ppp.c is shipped with a PPP distribution as well as with the kernel;
15 * if everyone increases the FILEVERSION number above, then scripts
16 * can do the right thing when deciding whether to install a new ppp.c
17 * file. Don't change the format of that line otherwise, so the
18 * installation script can recognize it.
26 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
27 Multi-protocol Datagrams over Point-to-Point Links
33 Flags for this module (any combination is acceptable for testing.):
35 OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
36 character. This is normally set to ((HZ * 3) / 2).
37 This is 1.5 seconds. If zero then the leading
40 CHECK_CHARACTERS - Enable the checking on all received characters for
41 8 data bits, no parity. This adds a small amount of
42 processing for each received character.
45 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
47 #define CHECK_CHARACTERS 1
48 #define PPP_COMPRESS 1
51 #define PPP_MAX_DEV 256
54 /* $Id: ppp.c,v 1.13 1997/07/14 03:50:50 paulus Exp $
55 * Added dynamic allocation of channels to eliminate
56 * compiled-in limits on the number of channels.
58 * Dynamic channel allocation code Copyright 1995 Caldera, Inc.,
59 * released under the GNU General Public License Version 2.
62 #include <linux/version.h>
63 #include <linux/module.h>
64 #include <linux/kernel.h>
65 #include <linux/sched.h>
66 #include <linux/types.h>
67 #include <linux/fcntl.h>
68 #include <linux/interrupt.h>
69 #include <linux/ptrace.h>
72 /* a nice define to generate linux version numbers */
73 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
75 #if LINUX_VERSION_CODE < VERSION(2,1,14)
76 #include <linux/ioport.h>
79 #if LINUX_VERSION_CODE >= VERSION(2,1,23)
80 #include <linux/poll.h>
84 #include <linux/malloc.h>
85 #include <linux/tty.h>
86 #include <linux/errno.h>
87 #include <linux/sched.h> /* to get the struct task_struct */
88 #include <linux/string.h> /* used in new tty drivers */
89 #include <linux/signal.h> /* used in new tty drivers */
90 #include <asm/system.h>
91 #include <asm/bitops.h>
93 #include <linux/if_ether.h>
94 #include <linux/netdevice.h>
95 #include <linux/skbuff.h>
96 #include <linux/inet.h>
97 #include <linux/ioctl.h>
99 typedef struct sk_buff sk_buff;
100 #define skb_data(skb) ((__u8 *) (skb)->data)
102 #include <linux/ip.h>
103 #include <linux/tcp.h>
104 #include <linux/if_arp.h>
105 #include <net/slhc_vj.h>
107 #define fcstab ppp_crc16_table /* Name of the table in the kernel */
108 #include <linux/ppp_defs.h>
110 #include <linux/socket.h>
111 #include <linux/if_ppp.h>
112 #include <linux/if_pppvar.h>
113 #include <linux/ppp-comp.h>
116 #define PPP_IPX 0x2b /* IPX protocol over PPP */
120 #define PPP_LQR 0xc025 /* Link Quality Reporting Protocol */
123 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
124 #include <asm/segment.h>
125 #define GET_USER(error,value,addr) error = get_user(value,addr)
126 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
127 #define PUT_USER(error,value,addr) error = put_user(value,addr)
128 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
130 #if LINUX_VERSION_CODE >= VERSION(2,1,5)
131 #include <asm/uaccess.h>
134 #else /* 2.0.x and 2.1.x before 2.1.4 */
136 #define GET_USER(error,value,addr) \
138 error = verify_area (VERIFY_READ, (void *) addr, sizeof (value)); \
140 value = get_user(addr); \
143 #define COPY_FROM_USER(error,dest,src,size) \
145 error = verify_area (VERIFY_READ, (void *) src, size); \
147 memcpy_fromfs (dest, src, size); \
150 #define PUT_USER(error,value,addr) \
152 error = verify_area (VERIFY_WRITE, (void *) addr, sizeof (value)); \
154 put_user (value, addr); \
157 #define COPY_TO_USER(error,dest,src,size) \
159 error = verify_area (VERIFY_WRITE, (void *) dest, size); \
161 memcpy_tofs (dest, src, size); \
166 #if LINUX_VERSION_CODE < VERSION(2,1,37)
167 #define test_and_set_bit(nr, addr) set_bit(nr, addr)
170 static int ppp_register_compressor (struct compressor *cp);
171 static void ppp_unregister_compressor (struct compressor *cp);
177 static struct compressor *find_compressor (int type);
178 static void ppp_init_ctrl_blk (register struct ppp *);
179 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
180 static struct ppp *ppp_alloc (void);
181 static struct ppp *ppp_find (int pid_value);
182 static void ppp_print_buffer (const __u8 *, const __u8 *, int);
183 extern inline void ppp_stuff_char (struct ppp *ppp,
184 register struct ppp_buffer *buf,
186 extern inline int lock_buffer (register struct ppp_buffer *buf);
187 static int ppp_dev_xmit_ip (struct ppp *ppp, struct ppp_buffer *buf,
188 __u8 *data, int len);
190 static int rcv_proto_ip (struct ppp *, __u16, __u8 *, int);
191 static int rcv_proto_ipx (struct ppp *, __u16, __u8 *, int);
192 static int rcv_proto_vjc_comp (struct ppp *, __u16, __u8 *, int);
193 static int rcv_proto_vjc_uncomp (struct ppp *, __u16, __u8 *, int);
194 static int rcv_proto_unknown (struct ppp *, __u16, __u8 *, int);
195 static int rcv_proto_lqr (struct ppp *, __u16, __u8 *, int);
196 static void ppp_doframe_lower (struct ppp *, __u8 *, int);
197 static int ppp_doframe (struct ppp *);
199 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd);
200 static int rcv_proto_ccp (struct ppp *, __u16, __u8 *, int);
202 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
204 #ifndef OPTIMIZE_FLAG_TIME
205 #define OPTIMIZE_FLAG_TIME 0
209 #define PPP_MAX_DEV 256
213 * Parameters which may be changed via insmod.
216 static int flag_time = OPTIMIZE_FLAG_TIME;
217 static int max_dev = PPP_MAX_DEV;
219 #if LINUX_VERSION_CODE >= VERSION(2,1,19)
220 MODULE_PARM(flag_time, "i");
221 MODULE_PARM(max_dev, "i");
225 * The "main" procedure to the ppp device
228 int ppp_init (struct device *);
231 * Network device driver callback routines
234 static int ppp_dev_open (struct device *);
235 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
236 static int ppp_dev_close (struct device *);
237 static int ppp_dev_xmit (sk_buff *, struct device *);
238 static struct enet_statistics *ppp_dev_stats (struct device *);
240 #if LINUX_VERSION_CODE < VERSION(2,1,15)
241 static int ppp_dev_header (sk_buff *, struct device *, __u16,
242 void *, void *, unsigned int);
243 static int ppp_dev_rebuild (void *eth, struct device *dev,
244 unsigned long raddr, struct sk_buff *skb);
251 static int ppp_tty_read (struct tty_struct *, struct file *, __u8 *,
253 static int ppp_tty_write (struct tty_struct *, struct file *, const __u8 *,
255 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
257 #if LINUX_VERSION_CODE < VERSION(2,1,23)
258 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
259 struct file *filp, int sel_type, select_table * wait);
261 static unsigned int ppp_tty_poll (struct tty_struct *tty, struct file *filp, poll_table * wait);
263 static int ppp_tty_open (struct tty_struct *);
264 static void ppp_tty_close (struct tty_struct *);
265 static int ppp_tty_room (struct tty_struct *tty);
266 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
267 char *fp, int count);
268 static void ppp_tty_wakeup (struct tty_struct *tty);
270 #define CHECK_PPP_MAGIC(ppp) do { \
271 if (ppp->magic != PPP_MAGIC) { \
272 printk(KERN_WARNING "bad magic for ppp %p at %s:%d\n", \
273 ppp, __FILE__, __LINE__); \
276 #define CHECK_PPP(a) do { \
277 CHECK_PPP_MAGIC(ppp); \
279 printk (ppp_warning, __LINE__); \
283 #define CHECK_PPP_VOID() do { \
284 CHECK_PPP_MAGIC(ppp); \
286 printk (ppp_warning, __LINE__); \
290 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
291 #define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \
292 ppp->recv_async_map & (1 << (c)))
294 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
296 #define tty2ppp(tty) ((struct ppp *) ((tty)->disc_data))
297 #define dev2ppp(dev) ((struct ppp *) ((dev)->priv))
298 #define ppp2tty(ppp) ((ppp)->tty)
299 #define ppp2dev(ppp) (&(ppp)->dev)
301 static struct ppp *ppp_list = NULL;
302 static struct ppp *ppp_last = NULL;
305 #define BUFFER_TYPE_DEV_RD 0 /* ppp read buffer */
306 #define BUFFER_TYPE_TTY_WR 1 /* tty write buffer */
307 #define BUFFER_TYPE_DEV_WR 2 /* ppp write buffer */
308 #define BUFFER_TYPE_TTY_RD 3 /* tty read buffer */
309 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
311 /* Define this string only once for all macro invocations */
312 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
314 static char szVersion[] = PPP_VERSION;
317 * Information for the protocol decoder
320 typedef int (*pfn_proto) (struct ppp *, __u16, __u8 *, int);
322 typedef struct ppp_proto_struct {
328 ppp_proto_type proto_list[] = {
329 { PPP_IP, rcv_proto_ip },
330 { PPP_IPX, rcv_proto_ipx },
331 { PPP_VJC_COMP, rcv_proto_vjc_comp },
332 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
333 { PPP_LQR, rcv_proto_lqr },
334 { PPP_CCP, rcv_proto_ccp },
335 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
338 __u16 ppp_crc16_table[256] =
340 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
341 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
342 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
343 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
344 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
345 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
346 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
347 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
348 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
349 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
350 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
351 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
352 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
353 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
354 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
355 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
356 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
357 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
358 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
359 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
360 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
361 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
362 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
363 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
364 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
365 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
366 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
367 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
368 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
369 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
370 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
371 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
374 #ifdef CHECK_CHARACTERS
375 static __u32 paritytab[8] =
377 0x96696996, 0x69969669, 0x69969669, 0x96696996,
378 0x69969669, 0x96696996, 0x96696996, 0x69969669
382 /* local function to store a value into the LQR frame */
383 extern inline __u8 * store_long (register __u8 *p, register int value) {
384 *p++ = (__u8) (value >> 24);
385 *p++ = (__u8) (value >> 16);
386 *p++ = (__u8) (value >> 8);
391 /*************************************************************
393 *************************************************************/
395 /* This procedure is called once and once only to define who we are to
396 * the operating system and the various procedures that it may use in
397 * accessing the ppp protocol.
401 ppp_first_time (void)
403 static struct tty_ldisc ppp_ldisc;
407 "PPP: version %s (demand dialling)"
410 #ifndef MODULE /* slhc module logic has its own copyright announcement */
412 "TCP compression code copyright 1989 Regents of the "
413 "University of California\n");
417 * Register the tty discipline
419 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
420 ppp_ldisc.magic = TTY_LDISC_MAGIC;
421 ppp_ldisc.open = ppp_tty_open;
422 ppp_ldisc.close = ppp_tty_close;
423 ppp_ldisc.read = ppp_tty_read;
424 ppp_ldisc.write = ppp_tty_write;
425 ppp_ldisc.ioctl = ppp_tty_ioctl;
426 #if LINUX_VERSION_CODE < VERSION(2,1,23)
427 ppp_ldisc.select = ppp_tty_select;
429 ppp_ldisc.poll = ppp_tty_poll;
431 ppp_ldisc.receive_room = ppp_tty_room;
432 ppp_ldisc.receive_buf = ppp_tty_receive;
433 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
435 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
437 printk (KERN_INFO "PPP line discipline registered.\n");
439 printk (KERN_ERR "error registering line discipline: %d\n",
444 /*************************************************************
446 *************************************************************/
448 /* called when the device is actually created */
451 ppp_init_dev (struct device *dev)
453 #if LINUX_VERSION_CODE < VERSION(2,1,15)
454 dev->hard_header = ppp_dev_header;
455 dev->rebuild_header = ppp_dev_rebuild;
458 dev->hard_header_len = PPP_HARD_HDR_LEN;
462 dev->hard_start_xmit = ppp_dev_xmit;
463 dev->open = ppp_dev_open;
464 dev->stop = ppp_dev_close;
465 dev->get_stats = ppp_dev_stats;
466 dev->do_ioctl = ppp_dev_ioctl;
468 dev->tx_queue_len = 10;
469 dev->type = ARPHRD_PPP;
471 #if LINUX_VERSION_CODE < VERSION(2,1,20)
475 for (indx = 0; indx < DEV_NUMBUFFS; indx++)
476 skb_queue_head_init (&dev->buffs[indx]);
479 dev_init_buffers(dev);
482 /* New-style flags */
483 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
484 dev->family = AF_INET;
488 dev->pa_alen = 4; /* sizeof (__u32) */
494 * Local procedure to initialize the ppp structure
498 ppp_init_ctrl_blk (register struct ppp *ppp)
500 ppp->magic = PPP_MAGIC;
508 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
509 ppp->xmit_async_map[0] = 0xffffffff;
510 ppp->xmit_async_map[3] = 0x60000000;
511 ppp->recv_async_map = 0x00000000;
518 ppp->read_wait = NULL;
519 ppp->write_wait = NULL;
520 ppp->last_xmit = jiffies - flag_time;
522 /* clear statistics */
523 memset(&ppp->stats, 0, sizeof (struct pppstat));
524 memset(&ppp->estats, 0, sizeof(struct enet_statistics));
526 /* Reset the demand dial information */
527 ppp->ddinfo.xmit_idle= /* time since last NP packet sent */
528 ppp->ddinfo.recv_idle=jiffies; /* time since last NP packet received */
530 /* PPP compression data */
532 ppp->sc_rc_state = NULL;
535 #if LINUX_VERSION_CODE < VERSION(2,1,18)
536 static struct symbol_table ppp_syms = {
537 #include <linux/symtab_begin.h>
538 X(ppp_register_compressor),
539 X(ppp_unregister_compressor),
541 #include <linux/symtab_end.h>
544 EXPORT_SYMBOL(ppp_register_compressor);
545 EXPORT_SYMBOL(ppp_unregister_compressor);
546 EXPORT_SYMBOL(ppp_crc16_table);
549 /* called at boot/load time for each ppp device defined in the kernel */
553 ppp_init (struct device *dev)
555 static int first_time = 1;
560 answer = ppp_first_time();
561 #if LINUX_VERSION_CODE < VERSION(2,1,18)
563 (void) register_symtab (&ppp_syms);
572 #define BUFFER_MAGIC 0x1d10
573 #define CHECK_BUF_MAGIC(buf) do { \
574 if (buf->magic != BUFFER_MAGIC) { \
575 printk(KERN_WARNING "bad magic for ppp buffer %p at %s:%d\n", \
576 buf, __FILE__, __LINE__); \
581 * Routine to allocate a buffer for later use by the driver.
584 static struct ppp_buffer *
585 ppp_alloc_buf (int size, int type)
587 struct ppp_buffer *buf;
589 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
593 buf->size = size - 1; /* Mask for the buffer size */
599 buf->fcs = PPP_INITFCS;
600 buf->magic = BUFFER_MAGIC;
606 * Routine to release the allocated buffer.
610 ppp_free_buf (struct ppp_buffer *ptr)
613 CHECK_BUF_MAGIC(ptr);
619 * Lock the indicated transmit buffer
623 lock_buffer (register struct ppp_buffer *buf)
628 * Save the current state and if free then set it to the "busy" state
630 CHECK_BUF_MAGIC(buf);
637 restore_flags (flags);
642 * MTU has been changed by the IP layer. Unfortunately we are not told
643 * about this, but we spot it ourselves and fix things up. We could be
644 * in an upcall from the tty driver, or in an ip packet queue.
648 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
653 struct ppp_buffer *new_rbuf;
654 struct ppp_buffer *new_wbuf;
655 struct ppp_buffer *new_cbuf;
656 struct ppp_buffer *new_tbuf;
658 struct ppp_buffer *old_rbuf;
659 struct ppp_buffer *old_wbuf;
660 struct ppp_buffer *old_cbuf;
661 struct ppp_buffer *old_tbuf;
665 * Allocate the buffer from the kernel for the data
669 if (ppp->flags & SC_DEBUG)
670 printk(KERN_DEBUG "%s: changedmtu %d %d\n", ppp->name,
673 /* allow for possible escaping of every character */
674 mtu = (new_mtu * 2) + 20;
676 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
682 new_wbuf = ppp_alloc_buf (mtu+PPP_HDRLEN, BUFFER_TYPE_DEV_WR);
683 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
684 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
685 new_cbuf = ppp_alloc_buf (mru+PPP_HDRLEN, BUFFER_TYPE_VJ);
687 * If the buffers failed to allocate then complain and release the partial
690 if (new_wbuf == NULL || new_tbuf == NULL ||
691 new_rbuf == NULL || new_cbuf == NULL) {
692 if (ppp->flags & SC_DEBUG)
694 "ppp: failed to allocate new buffers\n");
696 ppp_free_buf (new_wbuf);
697 ppp_free_buf (new_tbuf);
698 ppp_free_buf (new_rbuf);
699 ppp_free_buf (new_cbuf);
703 * Update the pointers to the new buffer structures.
707 old_wbuf = ppp->wbuf;
708 old_rbuf = ppp->rbuf;
709 old_cbuf = ppp->cbuf;
710 old_tbuf = ppp->tbuf;
712 ppp->wbuf = new_wbuf;
713 ppp->rbuf = new_rbuf;
714 ppp->cbuf = new_cbuf;
715 ppp->tbuf = new_tbuf;
718 new_wbuf->locked = old_wbuf->locked;
720 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
722 dev->mem_start = (unsigned long) buf_base (new_wbuf);
723 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
724 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
725 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
727 * Update the parameters for the new buffer sizes
729 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
730 ppp->escape = 0; /* No pending escape character */
740 ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
741 ppp->flags &= ~SC_XMIT_BUSY;
743 restore_flags(flags);
745 * Release old buffer pointers
747 ppp_free_buf (old_rbuf);
748 ppp_free_buf (old_wbuf);
749 ppp_free_buf (old_cbuf);
750 ppp_free_buf (old_tbuf);
755 * CCP is down; free (de)compressor state if necessary.
759 ppp_ccp_closed (struct ppp *ppp)
765 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
766 restore_flags(flags);
767 if (ppp->flags & SC_DEBUG)
768 printk(KERN_DEBUG "%s: ccp closed\n", ppp->name);
769 if (ppp->sc_xc_state) {
770 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
771 ppp->sc_xc_state = NULL;
774 if (ppp->sc_rc_state) {
775 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
776 ppp->sc_rc_state = NULL;
781 * Called to release all of the information in the current PPP structure.
783 * It is called when the ppp device goes down or if it is unable to go
788 ppp_release (struct ppp *ppp)
790 struct tty_struct *tty;
793 CHECK_PPP_MAGIC(ppp);
797 if (ppp->flags & SC_DEBUG)
798 printk(KERN_DEBUG "ppp%s released\n", ppp->name);
800 ppp_ccp_closed (ppp);
802 /* Ensure that the pppd process is not hanging on select()/poll() */
803 wake_up_interruptible (&ppp->read_wait);
804 wake_up_interruptible (&ppp->write_wait);
806 if (tty != NULL && tty->disc_data == ppp)
807 tty->disc_data = NULL; /* Break the tty->ppp link */
809 if (dev && dev->flags & IFF_UP) {
810 dev->flags &= ~IFF_UP; /* prevent recursion */
811 dev_close (dev); /* close the device properly */
814 ppp_free_buf (ppp->rbuf);
815 ppp_free_buf (ppp->wbuf);
816 ppp_free_buf (ppp->cbuf);
817 ppp_free_buf (ppp->ubuf);
818 ppp_free_buf (ppp->tbuf);
830 slhc_free (ppp->slcomp);
836 ppp->backup_tty = NULL;
842 * Called when the line discipline is changed to something
843 * else, the tty is closed, or the tty detects a hangup.
847 ppp_tty_close (struct tty_struct *tty)
849 struct ppp *ppp = tty2ppp (tty);
852 if (ppp->magic != PPP_MAGIC) {
853 if (ppp->flags & SC_DEBUG)
855 "ppp: trying to close unopened tty!\n");
859 tty->disc_data = NULL;
860 if (tty == ppp->backup_tty)
864 if (ppp->backup_tty) {
865 ppp->tty = ppp->backup_tty;
868 if (ppp->flags & SC_DEBUG)
869 printk (KERN_INFO "ppp: channel %s closing.\n",
880 * Called when the tty discipline is switched to PPP.
884 ppp_tty_open (struct tty_struct *tty)
886 struct ppp *ppp = tty2ppp (tty);
889 * There should not be an existing table for this slot.
892 if (ppp->flags & SC_DEBUG)
894 "ppp_tty_open: gack! tty already associated to %s!\n",
895 ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
900 * Allocate the structure from the system
902 ppp = ppp_find(current->pid);
905 * If we are taking over a ppp unit which is currently
906 * connected to a loopback pty, there's not much to do.
909 tty->disc_data = ppp;
915 if (ppp->flags & SC_DEBUG)
916 printk (KERN_ERR "ppp_alloc failed\n");
920 * Initialize the control block
922 ppp_init_ctrl_blk (ppp);
923 tty->disc_data = ppp;
926 * Allocate space for the default VJ header compression slots
928 ppp->slcomp = slhc_init (16, 16);
929 if (ppp->slcomp == NULL) {
930 if (ppp->flags & SC_DEBUG)
931 printk (KERN_ERR "ppp_tty_open: "
932 "no space for compression buffers!\n");
937 * Allocate space for the MTU and MRU buffers
939 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
944 * Allocate space for a user level buffer
946 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
947 if (ppp->ubuf == NULL) {
948 if (ppp->flags & SC_DEBUG)
949 printk (KERN_ERR "ppp_tty_open: "
950 "no space for user receive buffer\n");
955 if (ppp->flags & SC_DEBUG)
956 printk (KERN_INFO "ppp: channel %s open\n",
959 for (indx = 0; indx < NUM_NP; ++indx)
960 ppp->sc_npmode[indx] = NPMODE_PASS;
965 * Flush any pending characters in the driver and discipline.
967 if (tty->ldisc.flush_buffer)
968 tty->ldisc.flush_buffer (tty);
970 if (tty->driver.flush_buffer)
971 tty->driver.flush_buffer (tty);
976 * Local function to send the next portion of the buffer.
978 * Called by the tty driver's tty_wakeup function should it be entered
979 * because the partial buffer was transmitted.
981 * Called by kick_tty to send the initial portion of the buffer.
983 * Completion processing of the buffer transmission is handled here.
987 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
988 struct ppp_buffer *xbuf)
990 register int count, actual;
994 CHECK_BUF_MAGIC(xbuf);
996 * Prevent re-entrancy by ensuring that this routine is called only once.
1000 if (ppp->flags & SC_XMIT_BUSY) {
1001 restore_flags(flags);
1004 ppp->flags |= SC_XMIT_BUSY;
1005 restore_flags(flags);
1007 * Send the next block of data to the modem
1009 count = xbuf->count - xbuf->tail;
1010 actual = tty->driver.write (tty, 0,
1011 buf_base (xbuf) + xbuf->tail, count);
1013 * Terminate transmission of any block which may have an error.
1014 * This could occur should the carrier drop.
1017 ppp->stats.ppp_oerrors++;
1020 ppp->bytes_sent += actual;
1022 * If the buffer has been transmitted then clear the indicators.
1024 xbuf->tail += actual;
1025 if (actual == count) {
1027 ppp->flags &= ~SC_XMIT_BUSY;
1029 * Complete the transmission on the current buffer.
1033 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1037 * If the completed buffer came from the device write, then complete the
1038 * transmission block.
1040 ppp2dev (ppp)->tbusy = 0;
1041 if (ppp2dev (ppp) -> flags & IFF_UP) {
1045 * Wake up the transmission queue for all completion events.
1047 wake_up_interruptible (&ppp->write_wait);
1049 * Look at the priorities. Choose a daemon write over the device driver.
1060 * If there is a pending buffer then transmit it now.
1063 ppp->flags &= ~SC_XMIT_BUSY;
1064 ppp_kick_tty (ppp, xbuf);
1065 restore_flags(flags);
1068 restore_flags(flags);
1072 * Clear the re-entry flag
1074 save_flags(flags); /* &=~ may not be atomic */
1076 ppp->flags &= ~SC_XMIT_BUSY;
1077 restore_flags(flags);
1081 * This function is called by the tty driver when the transmit buffer has
1082 * additional space. It is used by the ppp code to continue to transmit
1083 * the current buffer should the buffer have been partially sent.
1085 * In addition, it is used to send the first part of the buffer since the
1086 * logic and the inter-locking would be identical.
1090 ppp_tty_wakeup (struct tty_struct *tty)
1092 struct ppp_buffer *xbuf;
1093 struct ppp *ppp = tty2ppp (tty);
1099 if (tty != ppp->tty) {
1100 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1104 * Ensure that there is a transmission pending. Clear the re-entry flag if
1105 * there is no pending buffer. Otherwise, send the buffer.
1109 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1111 ppp_tty_wakeup_code (ppp, tty, xbuf);
1115 * This function is called to transmit a buffer to the remote. The buffer
1116 * is placed on the pending queue if there is presently a buffer being
1117 * sent or it is transmitted with the aid of ppp_tty_wakeup.
1121 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
1123 unsigned long flags;
1126 CHECK_BUF_MAGIC(xbuf);
1133 * Control the flags which are best performed with the interrupts masked.
1138 * If the transmitter is busy then place the buffer on the appropriate
1141 if (ppp->xbuf != NULL) {
1142 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1146 restore_flags (flags);
1150 * If the transmitter is not busy then this is the highest priority frame
1152 ppp->flags &= ~SC_XMIT_BUSY;
1153 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1155 restore_flags (flags);
1157 * Do the "tty wakeup_code" to actually send this buffer.
1159 ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1162 /*************************************************************
1164 * The following functions handle input that arrives from
1165 * the TTY. It recognizes PPP frames and either hands them
1166 * to the network layer or queues them for delivery to a
1167 * user process reading this TTY.
1168 *************************************************************/
1171 * Callback function from tty driver. Return the amount of space left
1172 * in the receiver's buffer to decide if remote transmitter is to be
1177 ppp_tty_room (struct tty_struct *tty)
1179 return 65536; /* We can handle an infinite amount of data. :-) */
1183 * Callback function when data is available at the tty driver.
1186 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1187 char *flags, int count)
1189 register struct ppp *ppp = tty2ppp (tty);
1190 register struct ppp_buffer *buf = NULL;
1196 * This can happen if stuff comes in on the backup tty.
1198 if (ppp == 0 || tty != ppp->tty)
1201 * Fetch the pointer to the buffer. Be careful about race conditions.
1207 * Verify the table pointer and ensure that the line is
1208 * still in PPP discipline.
1210 if (ppp->magic != PPP_MAGIC) {
1211 if (ppp->flags & SC_DEBUG)
1213 "PPP: tty_receive called but couldn't find "
1219 * Print the buffer if desired
1221 if (ppp->flags & SC_LOG_RAWIN)
1222 ppp_print_buffer ("receive buffer", data, count);
1224 * Collect the character and error condition for the character. Set the toss
1225 * flag for the first character error.
1227 while (count-- > 0) {
1231 if (*flags && ppp->toss == 0) {
1233 switch (ppp->toss) {
1235 ++ppp->estats.rx_fifo_errors;
1239 ++ppp->estats.rx_frame_errors;
1246 * Set the flags for d7 being 0/1 and parity being even/odd so that
1247 * the normal processing would have all flags set at the end of the
1248 * session. A missing flag bit indicates an error condition.
1251 #ifdef CHECK_CHARACTERS
1253 ppp->flags |= SC_RCV_B7_1;
1255 ppp->flags |= SC_RCV_B7_0;
1257 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1258 ppp->flags |= SC_RCV_ODDP;
1260 ppp->flags |= SC_RCV_EVNP;
1263 * Branch on the character.
1267 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1268 * then the block is to be ignored. In addition, characters before the very
1269 * first FLAG are also tossed by this procedure.
1271 case PPP_FLAG: /* PPP_FLAG: end of frame */
1272 ppp->stats.ppp_ibytes += ppp->rbuf->count;
1276 * Process frames which are not to be ignored. If the processing failed,
1277 * then clean up the VJ tables.
1279 if (ppp_doframe (ppp) == 0) {
1280 ++ppp->stats.ppp_ierrors;
1281 slhc_toss (ppp->slcomp);
1284 * Reset all indicators for the new frame to follow.
1287 buf->fcs = PPP_INITFCS;
1292 * All other characters in the data come here. If the character is in the
1293 * receive mask then ignore the character.
1296 /* If we're tossing, look no further. */
1300 /* If this is a control char to be ignored, do so */
1301 if (in_rmap (ppp, chr))
1305 * Modify the next character if preceded by escape.
1306 * The escape character (0x7d) could be an escaped
1307 * 0x5d, if it follows an escape :-)
1312 } else if (chr == PPP_ESCAPE) {
1313 ppp->escape = PPP_TRANS;
1318 * Decompress A/C and protocol compression here.
1320 if (buf->count == 0 && chr != PPP_ALLSTATIONS) {
1321 buf_base(buf)[0] = PPP_ALLSTATIONS;
1322 buf_base(buf)[1] = PPP_UI;
1325 if (buf->count == 2 && (chr & 1) != 0) {
1326 buf_base(buf)[2] = 0;
1330 * If the count sent is within reason then store the character, bump the
1331 * count, and update the FCS for the character.
1333 if (buf->count < buf->size) {
1334 buf_base (buf)[buf->count++] = chr;
1335 buf->fcs = PPP_FCS (buf->fcs, chr);
1339 * The peer sent too much data. Set the flags to discard the current frame
1340 * and wait for the re-synchronization FLAG to be sent.
1342 ++ppp->estats.rx_length_errors;
1349 /* on entry, a received frame is in ppp->rbuf.bufr
1350 check it and dispose as appropriate */
1353 ppp_doframe (struct ppp *ppp)
1355 __u8 *data = buf_base (ppp->rbuf);
1356 int count = ppp->rbuf->count;
1362 CHECK_BUF_MAGIC(ppp->rbuf);
1364 * If there is a pending error from the receiver then log it and discard
1365 * the damaged frame.
1368 if ((ppp->flags & SC_DEBUG) && count > 0)
1370 "ppp_toss: tossing frame, reason = %x\n",
1375 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1376 * follows each frame.
1381 * Generate an error if the frame is too small.
1383 if (count < PPP_HDRLEN + 2) {
1384 if (ppp->flags & SC_DEBUG)
1386 "ppp: got runt ppp frame, %d chars\n", count);
1387 ++ppp->estats.rx_length_errors;
1391 * Verify the CRC of the frame and discard the CRC characters from the
1392 * end of the buffer.
1394 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1395 if (ppp->flags & SC_DEBUG) {
1397 "ppp: frame with bad fcs, length = %d\n",
1399 ppp_print_buffer("bad frame", data, count);
1401 ++ppp->estats.rx_crc_errors;
1404 count -= 2; /* ignore the fcs characters */
1406 * Obtain the protocol from the frame
1408 proto = PPP_PROTOCOL(data);
1410 * Process the active decompressor.
1412 if ((ppp->sc_rc_state != (void *) 0) &&
1413 (ppp->flags & SC_DECOMP_RUN) &&
1414 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1415 if (proto == PPP_COMP) {
1417 * If the frame is compressed then decompress it.
1419 new_data = kmalloc (ppp->mru + PPP_HDRLEN, GFP_ATOMIC);
1420 if (new_data == NULL) {
1421 if (ppp->flags & SC_DEBUG)
1423 "ppp_doframe: no memory\n");
1424 new_count = DECOMP_ERROR;
1426 new_count = (*ppp->sc_rcomp->decompress)
1427 (ppp->sc_rc_state, data, count,
1428 new_data, ppp->mru + PPP_HDRLEN);
1430 switch (new_count) {
1432 ppp_doframe_lower (ppp, new_data, new_count);
1437 ppp->flags |= SC_DC_ERROR;
1440 case DECOMP_FATALERROR:
1441 ppp->flags |= SC_DC_FERROR;
1442 if (ppp->flags & SC_DEBUG)
1443 printk(KERN_ERR "ppp: fatal decomp error\n");
1447 * Log the error condition and discard the frame.
1451 slhc_toss (ppp->slcomp);
1452 ++ppp->stats.ppp_ierrors;
1455 * The frame is not special. Pass it through the compressor without
1456 * actually compressing the data
1458 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1463 * Process the uncompressed frame.
1465 ppp_doframe_lower (ppp, data, count);
1469 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
1471 __u16 proto = PPP_PROTOCOL (data);
1472 ppp_proto_type *proto_ptr;
1476 * Ignore empty frames
1478 if (count <= PPP_HDRLEN)
1481 * Count the frame and print it
1483 ++ppp->stats.ppp_ipackets;
1484 if (ppp->flags & SC_LOG_INPKT)
1485 ppp_print_buffer ("receive frame", data, count);
1487 * Find the procedure to handle this protocol. The last one is marked
1488 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1490 proto_ptr = proto_list;
1491 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1494 * Update the appropriate statistic counter.
1496 if ((*proto_ptr->func) (ppp, proto,
1498 count - PPP_HDRLEN))
1499 ppp->stats.ppp_ioctects += count;
1501 ++ppp->stats.ppp_discards;
1505 * Put the input frame into the networking system for the indicated protocol
1509 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1511 sk_buff *skb = dev_alloc_skb (count);
1513 * Generate a skb buffer for the new frame.
1516 if (ppp->flags & SC_DEBUG)
1518 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1519 ppp2dev (ppp)->name);
1523 * Move the received data from the input buffer to the skb buffer.
1525 skb->dev = ppp2dev (ppp); /* We are the device */
1526 skb->protocol = proto;
1527 skb->mac.raw = skb_data(skb);
1528 memcpy (skb_put(skb,count), data, count); /* move data */
1530 * Tag the frame and kick it to the proper receive routine
1532 #if LINUX_VERSION_CODE < VERSION(2,1,15)
1536 ppp->ddinfo.recv_idle = jiffies;
1542 * Process the receipt of an IP frame
1546 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1549 if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1550 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1551 return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1556 * Process the receipt of an IPX frame
1560 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1563 if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1564 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1569 * Process the receipt of an VJ Compressed frame
1573 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
1574 __u8 *data, int count)
1577 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1578 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1579 if (new_count >= 0) {
1580 return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1582 if (ppp->flags & SC_DEBUG)
1584 "ppp: error in VJ decompression\n");
1590 * Process the receipt of an VJ Un-compressed frame
1594 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
1595 __u8 *data, int count)
1598 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1599 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1600 return rcv_proto_ip (ppp, PPP_IP, data, count);
1602 if (ppp->flags & SC_DEBUG)
1604 "ppp: error in VJ memorizing\n");
1610 * Receive all unclassified protocols.
1614 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
1615 __u8 *data, int len)
1618 register int current_idx;
1622 buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1623 current_idx &= ppp->ubuf->size; \
1624 if (current_idx == ppp->ubuf->tail) \
1630 * The total length includes the protocol data.
1631 * Lock the user information buffer.
1633 if (test_and_set_bit (0, &ppp->ubuf->locked)) {
1634 if (ppp->flags & SC_DEBUG)
1636 "ppp: rcv_proto_unknown: can't get lock\n");
1638 CHECK_BUF_MAGIC(ppp->ubuf);
1639 current_idx = ppp->ubuf->head;
1641 * Insert the buffer length (not counted), the protocol, and the data
1651 while (totlen-- > 0) {
1656 * The frame is complete. Update the head pointer and wakeup the pppd
1659 ppp->ubuf->head = current_idx;
1661 clear_bit (0, &ppp->ubuf->locked);
1662 wake_up_interruptible (&ppp->read_wait);
1663 if (ppp->tty->fasync != NULL)
1664 kill_fasync (ppp->tty->fasync, SIGIO);
1668 * The buffer is full. Unlock the header
1671 clear_bit (0, &ppp->ubuf->locked);
1672 if (ppp->flags & SC_DEBUG)
1674 "ppp: rcv_proto_unknown: buffer overflow\n");
1677 * Discard the frame. There are no takers for this protocol.
1679 if (ppp->flags & SC_DEBUG)
1681 "ppp: rcv_proto_unknown: dropping packet\n");
1686 * Handle a CCP packet.
1688 * The CCP packet is passed along to the pppd process just like any
1689 * other PPP frame. The difference is that some processing needs to be
1690 * immediate or the compressors will become confused on the peer.
1693 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
1695 int slen = CCP_LENGTH(dp);
1696 __u8 *opt = dp + CCP_HDRLEN;
1697 int opt_len = slen - CCP_HDRLEN;
1698 unsigned long flags;
1704 switch (CCP_CODE(dp)) {
1709 * CCP must be going down - disable compression
1711 if (ppp->flags & SC_CCP_UP) {
1713 ppp->flags &= ~(SC_CCP_UP |
1720 if ((ppp->flags & SC_CCP_OPEN) == 0)
1722 if (ppp->flags & SC_CCP_UP)
1724 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1726 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1729 * we're agreeing to send compressed packets.
1732 if (ppp->sc_xc_state == NULL)
1735 if ((*ppp->sc_xcomp->comp_init)
1739 ppp2dev (ppp)->base_addr,
1741 ppp->flags & SC_DEBUG)) {
1742 if (ppp->flags & SC_DEBUG)
1743 printk(KERN_DEBUG "%s: comp running\n",
1746 ppp->flags |= SC_COMP_RUN;
1751 * peer is agreeing to send compressed packets.
1753 if (ppp->sc_rc_state == NULL)
1756 if ((*ppp->sc_rcomp->decomp_init)
1760 ppp2dev (ppp)->base_addr,
1763 ppp->flags & SC_DEBUG)) {
1764 if (ppp->flags & SC_DEBUG)
1765 printk(KERN_DEBUG "%s: decomp running\n",
1768 ppp->flags |= SC_DECOMP_RUN;
1769 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1773 * CCP Reset-ack resets compressors and decompressors as it passes through.
1776 if ((ppp->flags & SC_CCP_UP) == 0)
1780 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN)) {
1781 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1782 if (ppp->flags & SC_DEBUG)
1783 printk(KERN_DEBUG "%s: comp reset\n",
1787 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1788 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1789 if (ppp->flags & SC_DEBUG)
1790 printk(KERN_DEBUG "%s: decomp reset\n",
1793 ppp->flags &= ~SC_DC_ERROR;
1798 restore_flags(flags);
1802 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
1805 ppp_proto_ccp (ppp, dp, len, 1);
1806 return rcv_proto_unknown (ppp, proto, dp, len);
1810 * Handle a LQR packet.
1814 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
1816 return rcv_proto_unknown (ppp, proto, data, len);
1819 /*************************************************************
1820 * LINE DISCIPLINE SUPPORT
1821 * The following functions form support user programs
1822 * which read and write data on a TTY with the PPP line
1823 * discipline. Reading is done from a circular queue,
1824 * filled by the lower TTY levels.
1825 *************************************************************/
1827 /* read a PPP frame from the us_rbuff circular buffer,
1828 waiting if necessary
1832 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
1835 struct ppp *ppp = tty2ppp (tty);
1842 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1843 ppp->ubuf->tail &= ppp->ubuf->size; \
1847 * Validate the pointers
1852 if (ppp->magic != PPP_MAGIC)
1858 * Before we attempt to write the frame to the user, ensure that the
1859 * user has access to the pages for the total buffer length.
1861 error = verify_area (VERIFY_WRITE, buf, nr);
1866 * Acquire the read lock.
1869 ppp = tty2ppp (tty);
1870 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
1874 if (test_and_set_bit (0, &ppp->ubuf->locked) != 0) {
1876 if (ppp->flags & SC_DEBUG)
1878 "ppp_tty_read: sleeping(ubuf)\n");
1880 current->timeout = 0;
1881 current->state = TASK_INTERRUPTIBLE;
1884 if (current->signal & ~current->blocked)
1890 * Fetch the length of the buffer from the first two bytes.
1892 if (ppp->ubuf->head == ppp->ubuf->tail)
1904 * If there is no length then wait for the data to arrive.
1907 clear_bit (0, &ppp->ubuf->locked);
1908 if (file->f_flags & O_NONBLOCK)
1910 current->timeout = 0;
1912 if (ppp->flags & SC_DEBUG)
1914 "ppp_tty_read: sleeping(read_wait)\n");
1916 interruptible_sleep_on (&ppp->read_wait);
1917 if (current->signal & ~current->blocked)
1922 * Ensure that the frame will fit within the caller's buffer. If not, then
1923 * discard the frame from the input buffer.
1926 /* Can't copy it, update us_rbuff_head */
1928 if (ppp->flags & SC_DEBUG)
1930 "ppp: read of %u bytes too small for %d "
1931 "frame\n", nr, len + 2);
1932 ppp->stats.ppp_ierrors++;
1938 * Fake the insertion of the ADDRESS and CONTROL information because these
1939 * were not saved in the buffer.
1941 PUT_USER (error, (u_char) PPP_ALLSTATIONS, buf);
1945 PUT_USER (error, (u_char) PPP_UI, buf);
1951 * Copy the received data from the buffer to the caller's area.
1953 ret = len + 2; /* Account for ADDRESS and CONTROL bytes */
1956 PUT_USER (error, c, buf);
1962 clear_bit (0, &ppp->ubuf->locked);
1964 if (ppp->flags & SC_DEBUG)
1965 printk (KERN_DEBUG "ppp_tty_read: passing %d bytes up\n", ret);
1970 ppp->ubuf->tail += len;
1971 ppp->ubuf->tail &= ppp->ubuf->size;
1972 clear_bit (0, &ppp->ubuf->locked);
1977 /* stuff a character into the transmit buffer, using PPP's way of escaping
1979 also, update fcs to take account of new character */
1982 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1986 * The buffer should not be full.
1988 if (ppp->flags & SC_DEBUG) {
1989 if ((buf->count < 0) || (buf->count > 3000))
1990 printk (KERN_DEBUG "ppp_stuff_char: %d %x\n",
1991 (unsigned int) buf->count,
1992 (unsigned int) chr);
1995 * Update the FCS and if the character needs to be escaped, do it.
1997 buf->fcs = PPP_FCS (buf->fcs, chr);
1998 if (in_xmap (ppp, chr)) {
2000 ins_char (buf, PPP_ESCAPE);
2003 * Add the character to the buffer.
2005 ins_char (buf, chr);
2009 * Procedure to encode the data with the proper escaping and send the
2010 * data to the remote system.
2014 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
2015 __u8 *data, int count, int non_ip)
2018 int address, control;
2022 CHECK_BUF_MAGIC(buf);
2023 ++ppp->stats.ppp_opackets;
2024 ppp->stats.ppp_ooctects += count;
2027 * Insert the leading FLAG character
2031 if (non_ip || flag_time == 0)
2032 ins_char (buf, PPP_FLAG);
2034 if (jiffies - ppp->last_xmit > flag_time)
2035 ins_char (buf, PPP_FLAG);
2037 ppp->last_xmit = jiffies;
2038 buf->fcs = PPP_INITFCS;
2040 * Emit the address/control information if needed
2042 address = PPP_ADDRESS (data);
2043 control = PPP_CONTROL (data);
2044 proto = PPP_PROTOCOL (data);
2046 if (address != PPP_ALLSTATIONS ||
2047 control != PPP_UI ||
2048 (ppp->flags & SC_COMP_AC) == 0) {
2049 ppp_stuff_char (ppp, buf, address);
2050 ppp_stuff_char (ppp, buf, control);
2053 * Emit the protocol (compressed if possible)
2055 if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
2056 ppp_stuff_char (ppp, buf, proto >> 8);
2058 ppp_stuff_char (ppp, buf, proto);
2066 ppp_stuff_char (ppp, buf, *data++);
2068 * Add the trailing CRC and the final flag character
2070 write_fcs = buf->fcs ^ 0xFFFF;
2071 ppp_stuff_char (ppp, buf, write_fcs);
2072 ppp_stuff_char (ppp, buf, write_fcs >> 8);
2074 * Add the trailing flag character
2076 ins_char (buf, PPP_FLAG);
2078 * Send the block to the tty driver.
2080 ppp->stats.ppp_obytes += buf->count;
2081 ppp_kick_tty (ppp, buf);
2085 * Compress and send an frame to the peer.
2087 * Return 0 if frame was queued for transmission.
2088 * 1 if frame must be re-queued for later driver support.
2092 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
2093 __u8 *data, int count)
2096 int address, control;
2101 CHECK_BUF_MAGIC(buf);
2105 if (ppp->flags & SC_LOG_OUTPKT)
2106 ppp_print_buffer ("write frame", data, count);
2108 * Determine if the frame may be compressed. Attempt to compress the
2109 * frame if possible.
2111 proto = PPP_PROTOCOL (data);
2112 address = PPP_ADDRESS (data);
2113 control = PPP_CONTROL (data);
2115 if (((ppp->flags & SC_COMP_RUN) != 0) &&
2116 (ppp->sc_xc_state != (void *) 0) &&
2117 (address == PPP_ALLSTATIONS) &&
2118 (control == PPP_UI) &&
2119 (proto != PPP_LCP) &&
2120 (proto != PPP_CCP)) {
2121 new_data = kmalloc (ppp->mtu, GFP_ATOMIC);
2122 if (new_data == NULL) {
2123 if (ppp->flags & SC_DEBUG)
2125 "ppp_dev_xmit_frame: no memory\n");
2129 new_count = (*ppp->sc_xcomp->compress)
2130 (ppp->sc_xc_state, data, new_data, count, ppp->mtu);
2132 if (new_count > 0 && (ppp->flags & SC_CCP_UP)) {
2133 ppp_dev_xmit_lower (ppp, buf, new_data, new_count, 0);
2138 * The frame could not be compressed, or it could not be sent in
2139 * compressed form because CCP is not yet up.
2144 * Go to the escape encoding
2146 ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2151 * Revise the tty frame for specific protocols.
2155 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
2159 switch (PPP_PROTOCOL (data)) {
2161 * Update the LQR frame with the current MIB information. This saves having
2162 * the daemon read old MIB data from the driver.
2165 len = 48; /* total size of this frame */
2166 p = (__u8 *) &data [40]; /* Point to last two items. */
2167 p = store_long (p, ppp->stats.ppp_opackets + 1);
2168 p = store_long (p, ppp->stats.ppp_ooctects + len);
2171 * Outbound compression frames
2188 * write a frame with NR chars from BUF to TTY
2189 * we have to put the FCS field on ourselves
2193 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
2196 struct ppp *ppp = tty2ppp (tty);
2201 * Verify the pointers.
2206 if (ppp->magic != PPP_MAGIC)
2211 * Ensure that the caller does not wish to send too much.
2213 if (count > PPP_MTU + PPP_HDRLEN) {
2214 if (ppp->flags & SC_DEBUG)
2215 printk (KERN_WARNING
2216 "ppp_tty_write: truncating user packet "
2217 "from %u to mtu %d\n", count,
2218 PPP_MTU + PPP_HDRLEN);
2219 count = PPP_MTU + PPP_HDRLEN;
2222 * Allocate a buffer for the data and fetch it from the user space.
2224 new_data = kmalloc (count, GFP_KERNEL);
2225 if (new_data == NULL) {
2226 if (ppp->flags & SC_DEBUG)
2228 "ppp_tty_write: no memory\n");
2232 * Retrieve the user's buffer
2234 if (copy_from_user (new_data, data, count)) {
2239 * lock this PPP unit so we will be the only writer;
2240 * sleep if necessary
2242 while (lock_buffer (ppp->tbuf) != 0) {
2243 current->timeout = 0;
2245 if (ppp->flags & SC_DEBUG)
2246 printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2248 interruptible_sleep_on (&ppp->write_wait);
2250 ppp = tty2ppp (tty);
2251 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
2252 || tty != ppp->tty) {
2257 if (current->signal & ~current->blocked) {
2263 * Change the LQR frame
2265 count = send_revise_frame (ppp, new_data, count);
2269 if (proto == PPP_IP) {
2271 * IP frames can be sent by pppd when we're doing
2272 * demand-dialling. We send them via ppp_dev_xmit_ip
2273 * to make sure that VJ compression happens properly.
2275 ppp_dev_xmit_ip(ppp, ppp->tbuf, new_data + PPP_HDRLEN,
2276 count - PPP_HDRLEN);
2279 ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2287 * Process the BSD compression IOCTL event for the tty device.
2291 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2293 struct compressor *cp;
2294 struct ppp_option_data data;
2298 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2299 unsigned long flags;
2302 * Fetch the compression parameters
2304 COPY_FROM_USER (error,
2314 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2315 nb = CCP_MAX_OPTION_LENGTH;
2317 COPY_FROM_USER (error,
2325 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2330 ppp->flags &= ~(SC_COMP_RUN | SC_DECOMP_RUN);
2331 restore_flags(flags);
2333 cp = find_compressor (ccp_option[0]);
2334 if (cp != (struct compressor *) 0) {
2336 * Found a handler for the protocol - try to allocate
2337 * a compressor or decompressor.
2340 if (data.transmit) {
2341 if (ppp->sc_xc_state != NULL)
2342 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2345 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2347 if (ppp->sc_xc_state == NULL) {
2348 if (ppp->flags & SC_DEBUG)
2349 printk(KERN_DEBUG "%s: comp_alloc failed\n",
2353 if (ppp->flags & SC_DEBUG)
2354 printk(KERN_DEBUG "%s: comp_alloc -> %p\n",
2355 ppp->name, ppp->sc_xc_state);
2358 if (ppp->sc_rc_state != NULL)
2359 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2361 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2362 if (ppp->sc_rc_state == NULL) {
2363 if (ppp->flags & SC_DEBUG)
2364 printk(KERN_DEBUG "%s: decomp_alloc failed\n",
2368 if (ppp->flags & SC_DEBUG)
2369 printk(KERN_DEBUG "%s: decomp_alloc -> %p\n",
2370 ppp->name, ppp->sc_rc_state);
2376 if (ppp->flags & SC_DEBUG)
2377 printk(KERN_DEBUG "%s: no compressor for [%x %x %x], %x\n",
2378 ppp->name, ccp_option[0], ccp_option[1],
2380 return (-EINVAL); /* no handler found */
2384 * Process the IOCTL event for the tty device.
2388 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
2389 unsigned int param2, unsigned long param3)
2391 struct ppp *ppp = tty2ppp (tty);
2392 register int temp_i = 0;
2395 * Verify the status of the PPP device.
2400 if (ppp->magic != PPP_MAGIC)
2405 * The user must have an euid of root to do these requests.
2414 GET_USER (error, temp_i, (int *) param3);
2417 if (ppp->flags & SC_DEBUG)
2419 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2421 if (ppp->mru != temp_i)
2422 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2428 temp_i = (ppp->flags & SC_MASK);
2429 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2430 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2431 SC_RCV_ODDP | SC_RCV_EVNP;
2433 PUT_USER (error, temp_i, (int *) param3);
2436 * Set the flags for the various options
2439 GET_USER (error, temp_i, (int *) param3);
2443 temp_i |= (ppp->flags & ~SC_MASK);
2445 if ((ppp->flags & SC_CCP_OPEN) &&
2446 (temp_i & SC_CCP_OPEN) == 0)
2447 ppp_ccp_closed (ppp);
2449 if ((ppp->flags | temp_i) & SC_DEBUG)
2451 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2452 ppp->flags = temp_i;
2455 * Set the compression mode
2457 case PPPIOCSCOMPRESS:
2458 error = ppp_set_compression (ppp,
2459 (struct ppp_option_data *) param3);
2462 * Retrieve the transmit async map
2464 case PPPIOCGASYNCMAP:
2465 PUT_USER (error, ppp->xmit_async_map[0], (int *) param3);
2468 * Set the transmit async map
2470 case PPPIOCSASYNCMAP:
2471 GET_USER (error, temp_i, (int *) param3);
2474 ppp->xmit_async_map[0] = temp_i;
2475 if (ppp->flags & SC_DEBUG)
2477 "ppp_tty_ioctl: set xmit asyncmap %x\n",
2478 ppp->xmit_async_map[0]);
2481 * Set the receive async map
2483 case PPPIOCSRASYNCMAP:
2484 GET_USER (error, temp_i, (int *) param3);
2487 ppp->recv_async_map = temp_i;
2488 if (ppp->flags & SC_DEBUG)
2490 "ppp_tty_ioctl: set rcv asyncmap %x\n",
2491 ppp->recv_async_map);
2494 * Obtain the unit number for this device.
2497 PUT_USER (error, ppp2dev (ppp)->base_addr, (int *) param3);
2500 if (ppp->flags & SC_DEBUG)
2502 "ppp_tty_ioctl: get unit: %ld\n",
2503 ppp2dev (ppp)->base_addr);
2506 * Set the debug level
2509 GET_USER (error, temp_i, (int *) param3);
2512 temp_i = (temp_i & 0x1F) << 16;
2513 temp_i |= (ppp->flags & ~0x1F0000);
2515 if ((ppp->flags | temp_i) & SC_DEBUG)
2517 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2518 ppp->flags = temp_i;
2521 * Get the debug level
2524 temp_i = (ppp->flags >> 16) & 0x1F;
2525 PUT_USER (error, temp_i, (int *) param3);
2528 * Get the times since the last send/receive frame operation
2532 struct ppp_idle cur_ddinfo;
2533 __u32 cur_jiffies = jiffies;
2535 /* change absolute times to relative times. */
2536 cur_ddinfo.xmit_idle = (cur_jiffies - ppp->ddinfo.xmit_idle) / HZ;
2537 cur_ddinfo.recv_idle = (cur_jiffies - ppp->ddinfo.recv_idle) / HZ;
2538 COPY_TO_USER (error,
2541 sizeof (cur_ddinfo));
2545 * Retrieve the extended async map
2547 case PPPIOCGXASYNCMAP:
2548 COPY_TO_USER (error,
2550 ppp->xmit_async_map,
2551 sizeof (ppp->xmit_async_map));
2554 * Set the async extended map
2556 case PPPIOCSXASYNCMAP:
2560 COPY_FROM_USER (error,
2567 temp_tbl[1] = 0x00000000;
2568 temp_tbl[2] &= ~0x40000000;
2569 temp_tbl[3] |= 0x60000000;
2571 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2572 (temp_tbl[4] & temp_tbl[5]) != 0 ||
2573 (temp_tbl[6] & temp_tbl[7]) != 0)
2576 memcpy (ppp->xmit_async_map,
2578 sizeof (ppp->xmit_async_map));
2580 if (ppp->flags & SC_DEBUG)
2582 "ppp_tty_ioctl: set xasyncmap\n");
2587 * Set the maximum VJ header compression slot number.
2590 GET_USER (error, temp_i, (int *) param3);
2593 temp_i = (temp_i & 255) + 1;
2594 if (ppp->flags & SC_DEBUG)
2596 "ppp_tty_ioctl: set maxcid to %d\n",
2598 if (ppp->slcomp != NULL)
2599 slhc_free (ppp->slcomp);
2600 ppp->slcomp = slhc_init (16, temp_i);
2602 if (ppp->slcomp == NULL) {
2603 if (ppp->flags & SC_DEBUG)
2605 "ppp: no space for compression buffers!\n");
2611 case PPPIOCXFERUNIT:
2612 ppp->backup_tty = tty;
2613 ppp->sc_xfer = current->pid;
2621 COPY_FROM_USER (error,
2628 switch (npi.protocol) {
2630 npi.protocol = NP_IP;
2633 if (ppp->flags & SC_DEBUG)
2634 printk(KERN_DEBUG "pppioc[gs]npmode: "
2635 "invalid proto %d\n", npi.protocol);
2642 if (param2 == PPPIOCGNPMODE) {
2643 npi.mode = ppp->sc_npmode[npi.protocol];
2645 COPY_TO_USER (error,
2652 ppp->sc_npmode[npi.protocol] = npi.mode;
2653 if (ppp->flags & SC_DEBUG)
2654 printk(KERN_DEBUG "ppp: set np %d to %d\n",
2655 npi.protocol, npi.mode);
2659 * Allow users to read, but not set, the serial port parameters
2663 error = n_tty_ioctl (tty, file, param2, param3);
2668 int count = ppp->ubuf->tail - ppp->ubuf->head;
2670 count += (ppp->ubuf->size + 1);
2671 PUT_USER (error, count, (int *) param3);
2675 * All other ioctl() events will come here.
2678 if (ppp->flags & SC_DEBUG)
2680 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2684 error = -ENOIOCTLCMD;
2693 * Process the select() (or poll()) statement for the PPP device.
2696 #if LINUX_VERSION_CODE < VERSION(2,1,23)
2698 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2699 struct file *filp, int sel_type, select_table * wait)
2701 struct ppp *ppp = tty2ppp (tty);
2704 * Verify the status of the PPP device.
2709 if (ppp->magic != PPP_MAGIC || tty != ppp->tty)
2714 * Branch on the type of select mode. A read request must lock the user
2719 if (test_and_set_bit (0, &ppp->ubuf->locked) == 0) {
2720 /* Test for the presence of data in the queue */
2721 if (ppp->ubuf->head != ppp->ubuf->tail) {
2722 clear_bit (0, &ppp->ubuf->locked);
2725 clear_bit (0, &ppp->ubuf->locked);
2726 } /* fall through */
2728 * Exceptions or read errors.
2731 /* Is this a pty link and the remote disconnected? */
2732 if (tty->flags & (1 << TTY_OTHER_CLOSED))
2735 /* Is this a local link and the modem disconnected? */
2736 if (tty_hung_up_p (filp))
2739 select_wait (&ppp->read_wait, wait);
2743 * Write mode. A write is allowed if there is no current transmission.
2746 if (ppp->tbuf->locked != 0) {
2747 select_wait (&ppp->write_wait, wait);
2755 #else /* 2.1.23 or later */
2758 ppp_tty_poll (struct tty_struct *tty, struct file *filp, poll_table * wait)
2760 struct ppp *ppp = tty2ppp (tty);
2761 unsigned int mask = 0;
2763 if (ppp && ppp->magic == PPP_MAGIC && tty == ppp->tty) {
2766 poll_wait(&ppp->read_wait, wait);
2767 poll_wait(&ppp->write_wait, wait);
2769 /* Must lock the user buffer area while checking. */
2770 if(test_and_set_bit(0, &ppp->ubuf->locked) == 0) {
2771 if(ppp->ubuf->head != ppp->ubuf->tail)
2772 mask |= POLLIN | POLLRDNORM;
2773 clear_bit(0, &ppp->ubuf->locked);
2775 if(tty->flags & (1 << TTY_OTHER_CLOSED))
2777 if(tty_hung_up_p(filp))
2779 if(ppp->tbuf->locked == 0)
2780 mask |= POLLOUT | POLLWRNORM;
2787 /*************************************************************
2789 * This routine accepts requests from the network layer
2790 * and attempts to deliver the packets.
2791 * It also includes various routines we are compelled to
2792 * have to make the network layer work (arp, etc...).
2793 *************************************************************/
2796 * Callback from the network layer when the device goes up.
2800 ppp_dev_open (struct device *dev)
2802 struct ppp *ppp = dev2ppp (dev);
2804 /* reset POINTOPOINT every time, since dev_close zaps it! */
2805 dev->flags |= IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2807 if (ppp2tty (ppp) == NULL) {
2808 if (ppp->flags & SC_DEBUG)
2810 "ppp: %s not connected to a TTY! can't go open!\n",
2815 if (ppp->flags & SC_DEBUG)
2817 "ppp: channel %s going up for IP packets!\n",
2825 * Callback from the network layer when the ppp device goes down.
2829 ppp_dev_close (struct device *dev)
2831 struct ppp *ppp = dev2ppp (dev);
2833 if (ppp2tty (ppp) == NULL) {
2837 * We don't do anything about the device going down. It is not important
2840 if (ppp->flags & SC_DEBUG)
2842 "ppp: channel %s going down for IP packets!\n",
2849 * IOCTL operation to read the version of the driver.
2853 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2856 char *result = (char *) ifr->ifr_ifru.ifru_data;
2857 int len = strlen (szVersion) + 1;
2859 * Move the version data
2861 COPY_TO_USER (error,
2870 * IOCTL to read the statistics for the pppstats program.
2874 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2876 struct ppp_stats *result, temp;
2879 * Supply the information for the caller. First move the version data
2880 * then move the ppp stats; and finally the vj stats.
2882 memset (&temp, 0, sizeof(temp));
2883 if (dev->flags & IFF_UP) {
2884 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2885 if (ppp->slcomp != NULL) {
2886 temp.vj.vjs_packets = ppp->slcomp->sls_o_compressed+
2887 ppp->slcomp->sls_o_uncompressed;
2888 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2889 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
2890 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
2891 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
2892 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
2893 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2894 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
2898 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2900 COPY_TO_USER (error,
2909 * IOCTL to read the compression statistics for the pppstats program.
2913 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2915 struct ppp_comp_stats *result, temp;
2918 * Supply the information for the caller.
2920 memset (&temp, 0, sizeof(temp));
2921 if (dev->flags & IFF_UP) {
2922 if (ppp->sc_xc_state != NULL)
2923 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2926 if (ppp->sc_rc_state != NULL)
2927 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2931 * Move the data to the caller's buffer
2933 result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2935 COPY_TO_USER (error,
2944 * Callback from the network layer to process the sockioctl functions.
2948 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2950 struct ppp *ppp = dev2ppp (dev);
2953 CHECK_PPP_MAGIC(ppp);
2955 * Process the requests
2959 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2962 case SIOCGPPPCSTATS:
2963 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2967 error = ppp_dev_ioctl_version (ppp, ifr);
2978 * Send an IP frame to the remote with vj header compression.
2980 * Return 0 if frame was queued for transmission.
2981 * 1 if frame must be re-queued for later driver support.
2982 * -1 if frame should be dropped.
2986 ppp_dev_xmit_ip (struct ppp *ppp, struct ppp_buffer *buf,
2987 __u8 *data, int len)
2992 * Branch on the type of processing for the IP frame.
2994 switch (ppp->sc_npmode[NP_IP]) {
3000 * We may not send the packet now, so drop it.
3001 * XXX It would be nice to be able to return it to the
3002 * network system to be queued and retransmitted later.
3004 if (ppp->flags & SC_DEBUG)
3005 printk(KERN_DEBUG "%s: returning frame\n",
3011 if (ppp->flags & SC_DEBUG)
3013 "ppp_dev_xmit: npmode = %d on %s\n",
3014 ppp->sc_npmode[NP_IP], ppp->name);
3018 if (ppp->flags & SC_DEBUG)
3019 printk (KERN_WARNING
3020 "ppp_dev_xmit: unknown npmode %d on %s\n",
3021 ppp->sc_npmode[NP_IP], ppp->name);
3025 * At this point, the buffer will be transmitted. There is no other exit.
3027 * Try to compress the header.
3029 if (ppp->flags & SC_COMP_TCP) {
3030 len = slhc_compress (ppp->slcomp, data, len,
3031 buf_base (ppp->cbuf) + PPP_HDRLEN,
3033 (ppp->flags & SC_NO_TCP_CCID) == 0);
3035 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
3036 proto = PPP_VJC_COMP;
3037 data[0] ^= SL_TYPE_COMPRESSED_TCP;
3039 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
3040 proto = PPP_VJC_UNCOMP;
3041 data[0] = (data[0] & 0x0f) | 0x40;
3048 hdr = data - PPP_HDRLEN;
3050 hdr[0] = PPP_ALLSTATIONS;
3055 return ppp_dev_xmit_frame (ppp, ppp->wbuf, hdr, len);
3059 * Send a non-IP data frame (such as an IPX frame) to the remote.
3061 * Return 0 if frame was queued for transmission.
3062 * 1 if frame must be re-queued for later driver support.
3065 ppp_dev_xmit_other (struct device *dev, struct ppp *ppp,
3066 __u8 *data, int len, int proto)
3073 hdr = data - PPP_HDRLEN;
3075 hdr[0] = PPP_ALLSTATIONS;
3077 hdr[2] = proto >> 8;
3080 return ppp_dev_xmit_frame (ppp, ppp->wbuf, hdr, len);
3084 * Send a frame to the remote.
3088 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3092 struct ppp *ppp = dev2ppp (dev);
3093 struct tty_struct *tty = ppp2tty (ppp);
3095 * just a little sanity check.
3098 if (ppp->flags & SC_DEBUG)
3099 printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3103 * Avoid timing problem should tty hangup while data is queued to be sent
3106 dev_kfree_skb (skb, FREE_WRITE);
3110 * Validate the tty interface
3113 if (ppp->flags & SC_DEBUG)
3115 "ppp_dev_xmit: %s not connected to a TTY!\n",
3117 dev_kfree_skb (skb, FREE_WRITE);
3121 * Fetch the pointer to the data
3124 data = skb_data(skb);
3126 if (data == (__u8 *) 0) {
3127 if (ppp->flags & SC_DEBUG)
3128 printk (KERN_CRIT "ppp_dev_xmit: %s Null skb data\n",
3130 dev_kfree_skb (skb, FREE_WRITE);
3134 * Detect a change in the transfer size
3136 if (ppp->mtu != ppp2dev (ppp)->mtu) {
3137 ppp_changedmtu (ppp,
3142 * Acquire the lock on the transmission buffer. If the buffer was busy then
3143 * mark the device as busy.
3144 * We also require that ppp->tbuf be unlocked, in order to serialize
3145 * calls to ppp_dev_xmit_frame (which does compression) and the output
3146 * of frames w.r.t. tty writes from pppd.
3148 CHECK_BUF_MAGIC(ppp->wbuf);
3149 if (ppp->tbuf->locked || lock_buffer (ppp->wbuf) != 0) {
3151 if (ppp->flags & SC_DEBUG)
3152 printk(KERN_DEBUG "dev_xmit blocked, t=%d w=%d\n",
3153 ppp->tbuf->locked, ppp->wbuf->locked);
3157 * Look at the protocol in the skb to determine the difference between
3158 * an IP frame and an IPX frame.
3160 switch (ntohs (skb->protocol)) {
3162 answer = ppp_dev_xmit_other (dev, ppp, data, len, PPP_IPX);
3166 answer = ppp_dev_xmit_ip (ppp, ppp->wbuf, data, len);
3169 default: /* All others have no support at this time. */
3170 dev_kfree_skb (skb, FREE_WRITE);
3174 * This is the end of the transmission. Release the buffer if it was sent.
3177 /* packet queued OK */
3178 dev_kfree_skb (skb, FREE_WRITE);
3179 ppp->ddinfo.xmit_idle = jiffies;
3181 ppp->wbuf->locked = 0;
3183 /* packet should be dropped */
3184 dev_kfree_skb (skb, FREE_WRITE);
3187 /* packet should be queued for later */
3195 * Generate the statistic information for the /proc/net/dev listing.
3198 static struct enet_statistics *
3199 ppp_dev_stats (struct device *dev)
3201 struct ppp *ppp = dev2ppp (dev);
3203 ppp->estats.rx_packets = ppp->stats.ppp_ipackets;
3204 ppp->estats.rx_errors = ppp->stats.ppp_ierrors;
3205 ppp->estats.tx_packets = ppp->stats.ppp_opackets;
3206 ppp->estats.tx_errors = ppp->stats.ppp_oerrors;
3208 return &ppp->estats;
3211 #if LINUX_VERSION_CODE < VERSION(2,1,15)
3212 static int ppp_dev_header (sk_buff *skb, struct device *dev,
3213 __u16 type, void *daddr,
3214 void *saddr, unsigned int len)
3220 ppp_dev_rebuild (void *eth, struct device *dev,
3221 unsigned long raddr, struct sk_buff *skb)
3227 /*************************************************************
3229 * Miscellany called by various functions above.
3230 *************************************************************/
3232 /* Locate the previous instance of the PPP channel */
3234 ppp_find (int pid_value)
3238 /* try to find the device which this pid is already using */
3239 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3240 if (ppp->inuse && ppp->sc_xfer == pid_value) {
3248 /* allocate or create a PPP channel */
3257 /* try to find an free device */
3259 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3260 if (!set_bit(0, &ppp->inuse))
3265 * There are no available units, so make a new one.
3267 ppp = (struct ppp *) kmalloc (sizeof(struct ppp), GFP_KERNEL);
3270 memset(ppp, 0, sizeof(*ppp));
3272 /* initialize channel control data */
3273 set_bit(0, &ppp->inuse);
3277 ppp->backup_tty = NULL;
3281 ppp_last->next = ppp;
3287 dev->init = ppp_init_dev;
3288 dev->name = ppp->name;
3289 sprintf(dev->name, "ppp%d", if_num);
3290 dev->base_addr = (__u32) if_num;
3291 dev->priv = (void *) ppp;
3293 /* register device so that we can be ifconfig'd */
3294 /* ppp_init_dev() will be called as a side-effect */
3295 status = register_netdev (dev);
3297 printk (KERN_INFO "registered device %s\n", dev->name);
3300 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3303 /* This one will forever be busy as it is not initialized */
3309 * Utility procedures to print a buffer in hex/ascii
3313 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3315 register __u8 next_ch;
3316 static char hex[] = "0123456789ABCDEF";
3318 while (count-- > 0) {
3320 *out++ = hex[(next_ch >> 4) & 0x0F];
3321 *out++ = hex[next_ch & 0x0F];
3327 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3329 register __u8 next_ch;
3331 while (count-- > 0) {
3334 if (next_ch < 0x20 || next_ch > 0x7e)
3338 if (next_ch == '%') /* printk/syslogd has a bug !! */
3346 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
3350 if (name != (__u8 *) NULL)
3351 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3354 memset (line, 32, 44);
3355 ppp_print_hex (line, buf, 8);
3356 ppp_print_char (&line[8 * 3], buf, 8);
3357 printk (KERN_DEBUG "%s\n", line);
3363 memset (line, 32, 44);
3364 ppp_print_hex (line, buf, count);
3365 ppp_print_char (&line[8 * 3], buf, count);
3366 printk (KERN_DEBUG "%s\n", line);
3370 /*************************************************************
3371 * Compressor module interface
3372 *************************************************************/
3374 struct compressor_link {
3375 struct compressor_link *next;
3376 struct compressor *comp;
3379 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3381 static struct compressor *find_compressor (int type)
3383 struct compressor_link *lnk;
3384 unsigned long flags;
3389 lnk = ppp_compressors;
3390 while (lnk != (struct compressor_link *) 0) {
3391 if ((int) (__u8) lnk->comp->compress_proto == type) {
3392 restore_flags(flags);
3398 restore_flags(flags);
3399 return (struct compressor *) 0;
3402 static int ppp_register_compressor (struct compressor *cp)
3404 struct compressor_link *new;
3405 unsigned long flags;
3407 new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3409 if (new == (struct compressor_link *) 0)
3415 if (find_compressor (cp->compress_proto)) {
3416 restore_flags(flags);
3421 new->next = ppp_compressors;
3423 ppp_compressors = new;
3425 restore_flags(flags);
3429 static void ppp_unregister_compressor (struct compressor *cp)
3431 struct compressor_link *prev = (struct compressor_link *) 0;
3432 struct compressor_link *lnk;
3433 unsigned long flags;
3438 lnk = ppp_compressors;
3439 while (lnk != (struct compressor_link *) 0) {
3440 if (lnk->comp == cp) {
3442 prev->next = lnk->next;
3444 ppp_compressors = lnk->next;
3451 restore_flags(flags);
3454 /*************************************************************
3455 * Module support routines
3456 *************************************************************/
3464 /* register our line disciplines */
3465 status = ppp_first_time();
3468 "PPP: ppp_init() failure %d\n", status);
3469 #if LINUX_VERSION_CODE < VERSION(2,1,18)
3471 (void) register_symtab (&ppp_syms);
3477 cleanup_module(void)
3481 struct ppp *ppp, *next_ppp;
3484 * Ensure that the devices are not in operation.
3486 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3487 if (ppp->inuse && ppp->tty != NULL) {
3492 dev = ppp2dev (ppp);
3493 if (dev->start || dev->flags & IFF_UP) {
3499 * Ensure that there are no compressor modules registered
3501 if (ppp_compressors != NULL)
3506 "PPP: device busy, remove delayed\n");
3510 * Release the tty registration of the line discipline so that no new entries
3513 status = tty_register_ldisc (N_PPP, NULL);
3516 "PPP: Unable to unregister ppp line discipline "
3517 "(err = %d)\n", status);
3520 "PPP: ppp line discipline successfully unregistered\n");
3522 * De-register the devices so that there is no problem with them
3524 for (ppp = ppp_list; ppp != 0; ppp = next_ppp) {
3525 next_ppp = ppp->next;
3527 unregister_netdev (&ppp->dev);