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 980319==
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.18 1998/05/13 06:00:59 paulus Exp $ */
53 #include <linux/version.h>
54 #include <linux/config.h> /* for CONFIG_KERNELD/KMOD */
55 #include <linux/module.h>
56 #include <linux/kernel.h>
57 #include <linux/sched.h>
58 #include <linux/types.h>
59 #include <linux/fcntl.h>
60 #include <linux/interrupt.h>
61 #include <linux/ptrace.h>
64 /* a nice define to generate linux version numbers */
65 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
67 #if LINUX_VERSION_CODE < VERSION(2,1,14)
68 #include <linux/ioport.h>
71 #if LINUX_VERSION_CODE >= VERSION(2,1,23)
72 #include <linux/poll.h>
76 #include <linux/malloc.h>
77 #include <linux/tty.h>
78 #include <linux/errno.h>
79 #include <linux/sched.h> /* to get the struct task_struct */
80 #include <linux/string.h> /* used in new tty drivers */
81 #include <linux/signal.h> /* used in new tty drivers */
82 #include <asm/system.h>
83 #include <asm/bitops.h>
85 #include <linux/if_ether.h>
86 #include <linux/netdevice.h>
87 #include <linux/skbuff.h>
89 #if LINUX_VERSION_CODE >= VERSION(2,1,68)
90 #include <linux/rtnetlink.h>
93 #include <linux/inet.h>
94 #include <linux/ioctl.h>
96 typedef struct sk_buff sk_buff;
97 #define skb_data(skb) ((__u8 *) (skb)->data)
100 #include <linux/tcp.h>
101 #include <linux/if_arp.h>
102 #include <net/slhc_vj.h>
104 #define fcstab ppp_crc16_table /* Name of the table in the kernel */
105 #include <linux/ppp_defs.h>
107 #include <linux/socket.h>
108 #include <linux/if_ppp.h>
109 #include <linux/if_pppvar.h>
110 #include <linux/ppp-comp.h>
112 #ifdef CONFIG_KERNELD
113 #include <linux/kerneld.h>
116 #include <linux/kmod.h>
120 #define PPP_IPX 0x2b /* IPX protocol over PPP */
124 #define PPP_LQR 0xc025 /* Link Quality Reporting Protocol */
127 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
128 #include <asm/segment.h>
129 #define GET_USER(error,value,addr) error = get_user(value,addr)
130 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
131 #define PUT_USER(error,value,addr) error = put_user(value,addr)
132 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
134 #if LINUX_VERSION_CODE >= VERSION(2,1,5)
135 #include <asm/uaccess.h>
138 #else /* 2.0.x and 2.1.x before 2.1.4 */
140 #define GET_USER(error,value,addr) \
142 error = verify_area (VERIFY_READ, (void *) addr, sizeof (value)); \
144 value = get_user(addr); \
147 #define COPY_FROM_USER(error,dest,src,size) \
149 error = verify_area (VERIFY_READ, (void *) src, size); \
151 memcpy_fromfs (dest, src, size); \
154 #define PUT_USER(error,value,addr) \
156 error = verify_area (VERIFY_WRITE, (void *) addr, sizeof (value)); \
158 put_user (value, addr); \
161 #define COPY_TO_USER(error,dest,src,size) \
163 error = verify_area (VERIFY_WRITE, (void *) dest, size); \
165 memcpy_tofs (dest, src, size); \
170 #if LINUX_VERSION_CODE < VERSION(2,1,37)
171 #define test_and_set_bit(nr, addr) set_bit(nr, addr)
174 #if LINUX_VERSION_CODE < VERSION(2,1,57)
175 #define signal_pending(p) ((p)->signal & ~(p)->blocked)
178 #if LINUX_VERSION_CODE < VERSION(2,1,25)
179 #define net_device_stats enet_statistics
182 #if LINUX_VERSION_CODE < VERSION(2,1,60)
183 typedef int rw_ret_t;
184 typedef unsigned int rw_count_t;
186 typedef ssize_t rw_ret_t;
187 typedef size_t rw_count_t;
190 static int ppp_register_compressor (struct compressor *cp);
191 static void ppp_unregister_compressor (struct compressor *cp);
197 static struct compressor *find_compressor (int type);
198 static void ppp_init_ctrl_blk (register struct ppp *);
199 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
200 static struct ppp *ppp_alloc (void);
201 static struct ppp *ppp_find (int pid_value);
202 static void ppp_print_buffer (const __u8 *, const __u8 *, int);
203 extern inline void ppp_stuff_char (struct ppp *ppp,
204 register struct ppp_buffer *buf,
206 extern inline int lock_buffer (register struct ppp_buffer *buf);
207 static int ppp_dev_xmit_ip (struct ppp *ppp, struct ppp_buffer *buf,
208 __u8 *data, int len, enum NPmode npmode);
210 static int rcv_proto_ip (struct ppp *, __u16, __u8 *, int);
211 static int rcv_proto_ipx (struct ppp *, __u16, __u8 *, int);
212 static int rcv_proto_vjc_comp (struct ppp *, __u16, __u8 *, int);
213 static int rcv_proto_vjc_uncomp (struct ppp *, __u16, __u8 *, int);
214 static int rcv_proto_unknown (struct ppp *, __u16, __u8 *, int);
215 static int rcv_proto_lqr (struct ppp *, __u16, __u8 *, int);
216 static void ppp_doframe_lower (struct ppp *, __u8 *, int);
217 static int ppp_doframe (struct ppp *);
219 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd);
220 static int rcv_proto_ccp (struct ppp *, __u16, __u8 *, int);
222 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
224 #ifndef OPTIMIZE_FLAG_TIME
225 #define OPTIMIZE_FLAG_TIME 0
229 * Parameters which may be changed via insmod.
232 static int flag_time = OPTIMIZE_FLAG_TIME;
234 #if LINUX_VERSION_CODE >= VERSION(2,1,19)
235 MODULE_PARM(flag_time, "i");
239 * The "main" procedure to the ppp device
242 int ppp_init (struct device *);
245 * Network device driver callback routines
248 static int ppp_dev_open (struct device *);
249 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
250 static int ppp_dev_close (struct device *);
251 static int ppp_dev_xmit (sk_buff *, struct device *);
252 static struct net_device_stats *ppp_dev_stats (struct device *);
254 #if LINUX_VERSION_CODE < VERSION(2,1,15)
255 static int ppp_dev_header (sk_buff *, struct device *, __u16,
256 void *, void *, unsigned int);
257 static int ppp_dev_rebuild (void *eth, struct device *dev,
258 unsigned long raddr, struct sk_buff *skb);
265 static rw_ret_t ppp_tty_read(struct tty_struct *, struct file *, __u8 *,
267 static rw_ret_t ppp_tty_write(struct tty_struct *, struct file *, const __u8 *,
269 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
271 #if LINUX_VERSION_CODE < VERSION(2,1,23)
272 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
273 struct file *filp, int sel_type, select_table * wait);
275 static unsigned int ppp_tty_poll (struct tty_struct *tty, struct file *filp,
278 static int ppp_tty_open (struct tty_struct *);
279 static void ppp_tty_close (struct tty_struct *);
280 static int ppp_tty_room (struct tty_struct *tty);
281 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
282 char *fp, int count);
283 static void ppp_tty_wakeup (struct tty_struct *tty);
285 #define CHECK_PPP_MAGIC(ppp) do { \
286 if (ppp->magic != PPP_MAGIC) { \
287 printk(KERN_WARNING "bad magic for ppp %p at %s:%d\n", \
288 ppp, __FILE__, __LINE__); \
291 #define CHECK_PPP(a) do { \
292 CHECK_PPP_MAGIC(ppp); \
294 printk (ppp_warning, __LINE__); \
298 #define CHECK_PPP_VOID() do { \
299 CHECK_PPP_MAGIC(ppp); \
301 printk (ppp_warning, __LINE__); \
305 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
306 #define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \
307 ppp->recv_async_map & (1 << (c)))
309 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
311 #define tty2ppp(tty) ((struct ppp *) ((tty)->disc_data))
312 #define dev2ppp(dev) ((struct ppp *) ((dev)->priv))
313 #define ppp2tty(ppp) ((ppp)->tty)
314 #define ppp2dev(ppp) (&(ppp)->dev)
316 static struct ppp *ppp_list = NULL;
317 static struct ppp *ppp_last = NULL;
320 #define BUFFER_TYPE_DEV_RD 0 /* ppp read buffer */
321 #define BUFFER_TYPE_TTY_WR 1 /* tty write buffer */
322 #define BUFFER_TYPE_DEV_WR 2 /* ppp write buffer */
323 #define BUFFER_TYPE_TTY_RD 3 /* tty read buffer */
324 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
326 /* Define this string only once for all macro invocations */
327 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
329 static char szVersion[] = PPP_VERSION;
332 * Information for the protocol decoder
335 typedef int (*pfn_proto) (struct ppp *, __u16, __u8 *, int);
337 typedef struct ppp_proto_struct {
343 ppp_proto_type proto_list[] = {
344 { PPP_IP, rcv_proto_ip },
345 { PPP_IPX, rcv_proto_ipx },
346 { PPP_VJC_COMP, rcv_proto_vjc_comp },
347 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
348 { PPP_LQR, rcv_proto_lqr },
349 { PPP_CCP, rcv_proto_ccp },
350 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
353 __u16 ppp_crc16_table[256] =
355 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
356 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
357 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
358 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
359 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
360 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
361 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
362 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
363 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
364 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
365 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
366 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
367 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
368 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
369 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
370 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
371 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
372 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
373 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
374 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
375 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
376 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
377 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
378 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
379 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
380 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
381 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
382 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
383 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
384 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
385 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
386 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
389 #ifdef CHECK_CHARACTERS
390 static __u32 paritytab[8] =
392 0x96696996, 0x69969669, 0x69969669, 0x96696996,
393 0x69969669, 0x96696996, 0x96696996, 0x69969669
397 /* local function to store a value into the LQR frame */
398 extern inline __u8 * store_long (register __u8 *p, register int value) {
399 *p++ = (__u8) (value >> 24);
400 *p++ = (__u8) (value >> 16);
401 *p++ = (__u8) (value >> 8);
406 /*************************************************************
408 *************************************************************/
410 /* This procedure is called once and once only to define who we are to
411 * the operating system and the various procedures that it may use in
412 * accessing the ppp protocol.
416 ppp_first_time (void)
418 static struct tty_ldisc ppp_ldisc;
422 "PPP: version %s (demand dialling)"
425 #ifndef MODULE /* slhc module logic has its own copyright announcement */
427 "TCP compression code copyright 1989 Regents of the "
428 "University of California\n");
432 * Register the tty discipline
434 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
435 ppp_ldisc.magic = TTY_LDISC_MAGIC;
436 #if LINUX_VERSION_CODE >= VERSION(2,1,28)
437 ppp_ldisc.name = "ppp";
439 ppp_ldisc.open = ppp_tty_open;
440 ppp_ldisc.close = ppp_tty_close;
441 ppp_ldisc.read = ppp_tty_read;
442 ppp_ldisc.write = ppp_tty_write;
443 ppp_ldisc.ioctl = ppp_tty_ioctl;
444 #if LINUX_VERSION_CODE < VERSION(2,1,23)
445 ppp_ldisc.select = ppp_tty_select;
447 ppp_ldisc.poll = ppp_tty_poll;
449 ppp_ldisc.receive_room = ppp_tty_room;
450 ppp_ldisc.receive_buf = ppp_tty_receive;
451 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
453 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
455 printk (KERN_INFO "PPP line discipline registered.\n");
457 printk (KERN_ERR "error registering line discipline: %d\n",
462 /*************************************************************
464 *************************************************************/
466 /* called when the device is actually created */
469 ppp_init_dev (struct device *dev)
471 #if LINUX_VERSION_CODE < VERSION(2,1,15)
472 dev->hard_header = ppp_dev_header;
473 dev->rebuild_header = ppp_dev_rebuild;
476 dev->hard_header_len = PPP_HDRLEN;
480 dev->hard_start_xmit = ppp_dev_xmit;
481 dev->open = ppp_dev_open;
482 dev->stop = ppp_dev_close;
483 dev->get_stats = ppp_dev_stats;
484 dev->do_ioctl = ppp_dev_ioctl;
486 dev->tx_queue_len = 10;
487 dev->type = ARPHRD_PPP;
489 #if LINUX_VERSION_CODE < VERSION(2,1,20)
493 for (indx = 0; indx < DEV_NUMBUFFS; indx++)
494 skb_queue_head_init (&dev->buffs[indx]);
497 dev_init_buffers(dev);
500 /* New-style flags */
501 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
502 #if LINUX_VERSION_CODE < VERSION(2,1,67)
503 dev->family = AF_INET;
507 dev->pa_alen = 4; /* sizeof (__u32) */
514 * Local procedure to initialize the ppp structure
518 ppp_init_ctrl_blk (register struct ppp *ppp)
520 ppp->magic = PPP_MAGIC;
528 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
529 ppp->xmit_async_map[0] = 0xffffffff;
530 ppp->xmit_async_map[3] = 0x60000000;
531 ppp->recv_async_map = 0x00000000;
538 ppp->read_wait = NULL;
539 ppp->write_wait = NULL;
540 ppp->last_xmit = jiffies - flag_time;
541 ppp->last_recv = jiffies;
543 /* clear statistics */
544 memset(&ppp->stats, 0, sizeof (struct pppstat));
545 memset(&ppp->estats, 0, sizeof(ppp->estats));
547 /* PPP compression data */
549 ppp->sc_rc_state = NULL;
552 #if LINUX_VERSION_CODE < VERSION(2,1,18)
553 static struct symbol_table ppp_syms = {
554 #include <linux/symtab_begin.h>
555 X(ppp_register_compressor),
556 X(ppp_unregister_compressor),
558 #include <linux/symtab_end.h>
561 EXPORT_SYMBOL(ppp_register_compressor);
562 EXPORT_SYMBOL(ppp_unregister_compressor);
563 EXPORT_SYMBOL(ppp_crc16_table);
566 /* called at boot/load time for each ppp device defined in the kernel */
570 ppp_init (struct device *dev)
572 static int first_time = 1;
577 answer = ppp_first_time();
578 #if LINUX_VERSION_CODE < VERSION(2,1,18)
580 (void) register_symtab (&ppp_syms);
589 #define BUFFER_MAGIC 0x1d10
590 #define CHECK_BUF_MAGIC(buf) do { \
591 if (buf->magic != BUFFER_MAGIC) { \
592 printk(KERN_WARNING "bad magic for ppp buffer %p at %s:%d\n", \
593 buf, __FILE__, __LINE__); \
598 * Routine to allocate a buffer for later use by the driver.
601 static struct ppp_buffer *
602 ppp_alloc_buf (int size, int type)
604 struct ppp_buffer *buf;
606 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
610 buf->size = size - 1; /* Mask for the buffer size */
616 buf->fcs = PPP_INITFCS;
617 buf->magic = BUFFER_MAGIC;
623 * Routine to release the allocated buffer.
627 ppp_free_buf (struct ppp_buffer *ptr)
630 CHECK_BUF_MAGIC(ptr);
636 * Lock the indicated transmit buffer
640 lock_buffer (register struct ppp_buffer *buf)
645 * Save the current state and if free then set it to the "busy" state
647 CHECK_BUF_MAGIC(buf);
654 restore_flags (flags);
659 * MTU has been changed by the IP layer. Unfortunately we are not told
660 * about this, but we spot it ourselves and fix things up. We could be
661 * in an upcall from the tty driver, or in an ip packet queue.
665 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
670 struct ppp_buffer *new_rbuf;
671 struct ppp_buffer *new_wbuf;
672 struct ppp_buffer *new_cbuf;
673 struct ppp_buffer *new_tbuf;
675 struct ppp_buffer *old_rbuf;
676 struct ppp_buffer *old_wbuf;
677 struct ppp_buffer *old_cbuf;
678 struct ppp_buffer *old_tbuf;
682 * Allocate the buffer from the kernel for the data
686 if (ppp->flags & SC_DEBUG)
687 printk(KERN_DEBUG "%s: changedmtu %d %d\n", ppp->name,
690 /* allow for possible escaping of every character */
691 mtu = (new_mtu * 2) + 20;
693 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
699 new_wbuf = ppp_alloc_buf (mtu+PPP_HDRLEN, BUFFER_TYPE_DEV_WR);
700 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
701 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
702 new_cbuf = ppp_alloc_buf (mru+PPP_HDRLEN, BUFFER_TYPE_VJ);
704 * If the buffers failed to allocate then complain and release the partial
707 if (new_wbuf == NULL || new_tbuf == NULL ||
708 new_rbuf == NULL || new_cbuf == NULL) {
709 printk (KERN_ERR "ppp: failed to allocate new buffers\n");
711 ppp_free_buf (new_wbuf);
712 ppp_free_buf (new_tbuf);
713 ppp_free_buf (new_rbuf);
714 ppp_free_buf (new_cbuf);
718 * Update the pointers to the new buffer structures.
722 old_wbuf = ppp->wbuf;
723 old_rbuf = ppp->rbuf;
724 old_cbuf = ppp->cbuf;
725 old_tbuf = ppp->tbuf;
727 ppp->wbuf = new_wbuf;
728 ppp->rbuf = new_rbuf;
729 ppp->cbuf = new_cbuf;
730 ppp->tbuf = new_tbuf;
733 new_wbuf->locked = old_wbuf->locked;
735 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
737 dev->mem_start = (unsigned long) buf_base (new_wbuf);
738 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
739 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
740 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
742 * Update the parameters for the new buffer sizes
744 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
745 ppp->escape = 0; /* No pending escape character */
755 ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
756 ppp->flags &= ~SC_XMIT_BUSY;
758 restore_flags(flags);
760 * Release old buffer pointers
762 ppp_free_buf (old_rbuf);
763 ppp_free_buf (old_wbuf);
764 ppp_free_buf (old_cbuf);
765 ppp_free_buf (old_tbuf);
770 * CCP is down; free (de)compressor state if necessary.
774 ppp_ccp_closed (struct ppp *ppp)
780 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
781 restore_flags(flags);
782 if (ppp->flags & SC_DEBUG)
783 printk(KERN_DEBUG "%s: ccp closed\n", ppp->name);
784 if (ppp->sc_xc_state) {
785 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
786 ppp->sc_xc_state = NULL;
789 if (ppp->sc_rc_state) {
790 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
791 ppp->sc_rc_state = NULL;
796 * Called to release all of the information in the current PPP structure.
798 * It is called when the ppp device goes down or if it is unable to go
803 ppp_release (struct ppp *ppp)
805 struct tty_struct *tty;
808 CHECK_PPP_MAGIC(ppp);
812 if (ppp->flags & SC_DEBUG)
813 printk(KERN_DEBUG "%s released\n", ppp->name);
815 ppp_ccp_closed (ppp);
817 /* Ensure that the pppd process is not hanging on select()/poll() */
818 wake_up_interruptible (&ppp->read_wait);
819 wake_up_interruptible (&ppp->write_wait);
821 if (tty != NULL && tty->disc_data == ppp)
822 tty->disc_data = NULL; /* Break the tty->ppp link */
824 ppp_free_buf (ppp->rbuf);
825 ppp_free_buf (ppp->wbuf);
826 ppp_free_buf (ppp->cbuf);
827 ppp_free_buf (ppp->ubuf);
828 ppp_free_buf (ppp->tbuf);
840 slhc_free (ppp->slcomp);
846 ppp->backup_tty = NULL;
852 * Called when the line discipline is changed to something
853 * else, the tty is closed, or the tty detects a hangup.
857 ppp_tty_close (struct tty_struct *tty)
859 struct ppp *ppp = tty2ppp (tty);
862 if (ppp->magic != PPP_MAGIC) {
863 if (ppp->flags & SC_DEBUG)
865 "ppp: trying to close unopened tty!\n");
869 tty->disc_data = NULL;
870 if (tty == ppp->backup_tty)
874 if (ppp->backup_tty) {
875 ppp->tty = ppp->backup_tty;
878 if (ppp->flags & SC_DEBUG)
879 printk (KERN_INFO "ppp: channel %s closing.\n",
890 * Called when the tty discipline is switched to PPP.
894 ppp_tty_open (struct tty_struct *tty)
896 struct ppp *ppp = tty2ppp (tty);
899 * There should not be an existing table for this slot.
903 "ppp_tty_open: gack! tty already associated to %s!\n",
904 ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
909 * Allocate the structure from the system
911 ppp = ppp_find(current->pid);
914 * If we are taking over a ppp unit which is currently
915 * connected to a loopback pty, there's not much to do.
918 tty->disc_data = ppp;
924 printk (KERN_ERR "ppp_alloc failed\n");
928 * Initialize the control block
930 ppp_init_ctrl_blk (ppp);
931 tty->disc_data = ppp;
934 * Allocate space for the default VJ header compression slots
936 ppp->slcomp = slhc_init (16, 16);
937 if (ppp->slcomp == NULL) {
938 printk (KERN_ERR "ppp_tty_open: "
939 "no space for compression buffers!\n");
944 * Allocate space for the MTU and MRU buffers
946 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
951 * Allocate space for a user level buffer
953 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
954 if (ppp->ubuf == NULL) {
955 printk (KERN_ERR "ppp_tty_open: "
956 "no space for user receive buffer\n");
961 if (ppp->flags & SC_DEBUG)
962 printk (KERN_INFO "ppp: channel %s open\n",
965 for (indx = 0; indx < NUM_NP; ++indx)
966 ppp->sc_npmode[indx] = NPMODE_PASS;
971 * Flush any pending characters in the driver and discipline.
973 if (tty->ldisc.flush_buffer)
974 tty->ldisc.flush_buffer (tty);
976 if (tty->driver.flush_buffer)
977 tty->driver.flush_buffer (tty);
982 * Local function to send the next portion of the buffer.
984 * Called by the tty driver's tty_wakeup function should it be entered
985 * because the partial buffer was transmitted.
987 * Called by kick_tty to send the initial portion of the buffer.
989 * Completion processing of the buffer transmission is handled here.
993 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
994 struct ppp_buffer *xbuf)
996 register int count, actual;
1000 CHECK_BUF_MAGIC(xbuf);
1002 * Prevent re-entrancy by ensuring that this routine is called only once.
1006 if (ppp->flags & SC_XMIT_BUSY) {
1007 restore_flags(flags);
1010 ppp->flags |= SC_XMIT_BUSY;
1011 restore_flags(flags);
1013 * Send the next block of data to the modem
1015 count = xbuf->count - xbuf->tail;
1016 actual = tty->driver.write (tty, 0,
1017 buf_base (xbuf) + xbuf->tail, count);
1019 * Terminate transmission of any block which may have an error.
1020 * This could occur should the carrier drop.
1023 ppp->stats.ppp_oerrors++;
1026 ppp->bytes_sent += actual;
1028 * If the buffer has been transmitted then clear the indicators.
1030 xbuf->tail += actual;
1031 if (actual == count) {
1033 ppp->flags &= ~SC_XMIT_BUSY;
1035 * Complete the transmission on the current buffer.
1039 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1043 * If the completed buffer came from the device write, then complete the
1044 * transmission block.
1046 ppp2dev (ppp)->tbusy = 0;
1049 * Wake up the transmission queue for all completion events.
1051 wake_up_interruptible (&ppp->write_wait);
1053 * Look at the priorities. Choose a daemon write over the device driver.
1064 * If there is a pending buffer then transmit it now.
1067 ppp->flags &= ~SC_XMIT_BUSY;
1068 ppp_kick_tty (ppp, xbuf);
1069 restore_flags(flags);
1072 restore_flags(flags);
1076 * Clear the re-entry flag
1078 save_flags(flags); /* &=~ may not be atomic */
1080 ppp->flags &= ~SC_XMIT_BUSY;
1081 restore_flags(flags);
1085 * This function is called by the tty driver when the transmit buffer has
1086 * additional space. It is used by the ppp code to continue to transmit
1087 * the current buffer should the buffer have been partially sent.
1089 * In addition, it is used to send the first part of the buffer since the
1090 * logic and the inter-locking would be identical.
1094 ppp_tty_wakeup (struct tty_struct *tty)
1096 struct ppp_buffer *xbuf;
1097 struct ppp *ppp = tty2ppp (tty);
1103 if (tty != ppp->tty) {
1104 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1108 * Ensure that there is a transmission pending. Clear the re-entry flag if
1109 * there is no pending buffer. Otherwise, send the buffer.
1113 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1115 ppp_tty_wakeup_code (ppp, tty, xbuf);
1119 * This function is called to transmit a buffer to the remote. The buffer
1120 * is placed on the pending queue if there is presently a buffer being
1121 * sent or it is transmitted with the aid of ppp_tty_wakeup.
1125 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
1127 unsigned long flags;
1130 CHECK_BUF_MAGIC(xbuf);
1137 * Control the flags which are best performed with the interrupts masked.
1142 * If the transmitter is busy then place the buffer on the appropriate
1145 if (ppp->xbuf != NULL) {
1146 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1150 restore_flags (flags);
1154 * If the transmitter is not busy then this is the highest priority frame
1156 ppp->flags &= ~SC_XMIT_BUSY;
1157 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1159 restore_flags (flags);
1161 * Do the "tty wakeup_code" to actually send this buffer.
1163 ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1166 /*************************************************************
1168 * The following functions handle input that arrives from
1169 * the TTY. It recognizes PPP frames and either hands them
1170 * to the network layer or queues them for delivery to a
1171 * user process reading this TTY.
1172 *************************************************************/
1175 * Callback function from tty driver. Return the amount of space left
1176 * in the receiver's buffer to decide if remote transmitter is to be
1181 ppp_tty_room (struct tty_struct *tty)
1183 return 65536; /* We can handle an infinite amount of data. :-) */
1187 * Callback function when data is available at the tty driver.
1190 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1191 char *flags, int count)
1193 register struct ppp *ppp = tty2ppp (tty);
1194 register struct ppp_buffer *buf = NULL;
1200 * This can happen if stuff comes in on the backup tty.
1202 if (ppp == 0 || tty != ppp->tty)
1205 * Fetch the pointer to the buffer. Be careful about race conditions.
1211 * Verify the table pointer and ensure that the line is
1212 * still in PPP discipline.
1214 if (ppp->magic != PPP_MAGIC) {
1215 if (ppp->flags & SC_DEBUG)
1217 "PPP: tty_receive called but couldn't find "
1223 * Print the buffer if desired
1225 if (ppp->flags & SC_LOG_RAWIN)
1226 ppp_print_buffer ("receive buffer", data, count);
1229 * Collect the character and error condition for the character. Set the toss
1230 * flag for the first character error.
1232 while (count-- > 0) {
1236 if (*flags && ppp->toss == 0) {
1238 switch (ppp->toss) {
1240 ++ppp->estats.rx_fifo_errors;
1244 ++ppp->estats.rx_frame_errors;
1252 * Set the flags for d7 being 0/1 and parity being even/odd so that
1253 * the normal processing would have all flags set at the end of the
1254 * session. A missing flag bit indicates an error condition.
1257 #ifdef CHECK_CHARACTERS
1259 ppp->flags |= SC_RCV_B7_1;
1261 ppp->flags |= SC_RCV_B7_0;
1263 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1264 ppp->flags |= SC_RCV_ODDP;
1266 ppp->flags |= SC_RCV_EVNP;
1269 * Branch on the character.
1273 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1274 * then the block is to be ignored. In addition, characters before the very
1275 * first FLAG are also tossed by this procedure.
1277 case PPP_FLAG: /* PPP_FLAG: end of frame */
1278 ppp->stats.ppp_ibytes += ppp->rbuf->count;
1282 * Process frames which are not to be ignored. If the processing failed,
1283 * then clean up the VJ tables.
1285 if (ppp_doframe (ppp) == 0) {
1286 ++ppp->stats.ppp_ierrors;
1287 slhc_toss (ppp->slcomp);
1290 * Reset all indicators for the new frame to follow.
1293 buf->fcs = PPP_INITFCS;
1298 * All other characters in the data come here. If the character is in the
1299 * receive mask then ignore the character.
1302 /* If we're tossing, look no further. */
1306 /* If this is a control char to be ignored, do so */
1307 if (in_rmap (ppp, chr))
1311 * Modify the next character if preceded by escape.
1312 * The escape character (0x7d) could be an escaped
1313 * 0x5d, if it follows an escape :-)
1318 } else if (chr == PPP_ESCAPE) {
1319 ppp->escape = PPP_TRANS;
1324 * Decompress A/C and protocol compression here.
1326 if (buf->count == 0 && chr != PPP_ALLSTATIONS) {
1327 buf_base(buf)[0] = PPP_ALLSTATIONS;
1328 buf_base(buf)[1] = PPP_UI;
1331 if (buf->count == 2 && (chr & 1) != 0) {
1332 buf_base(buf)[2] = 0;
1336 * If the count sent is within reason then store the character, bump the
1337 * count, and update the FCS for the character.
1339 if (buf->count < buf->size) {
1340 buf_base (buf)[buf->count++] = chr;
1341 buf->fcs = PPP_FCS (buf->fcs, chr);
1345 * The peer sent too much data. Set the flags to discard the current frame
1346 * and wait for the re-synchronization FLAG to be sent.
1348 ++ppp->estats.rx_length_errors;
1355 /* on entry, a received frame is in ppp->rbuf.bufr
1356 check it and dispose as appropriate */
1359 ppp_doframe (struct ppp *ppp)
1361 __u8 *data = buf_base (ppp->rbuf);
1362 int count = ppp->rbuf->count;
1368 CHECK_BUF_MAGIC(ppp->rbuf);
1371 * If there is a pending error from the receiver then log it and discard
1372 * the damaged frame.
1375 if ((ppp->flags & SC_DEBUG) && count > 0)
1377 "ppp_toss: tossing frame, reason = %x\n",
1382 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1383 * follows each frame.
1388 * Generate an error if the frame is too small.
1390 if (count < PPP_HDRLEN + 2) {
1391 if (ppp->flags & SC_DEBUG)
1393 "ppp: got runt ppp frame, %d chars\n", count);
1394 ++ppp->estats.rx_length_errors;
1398 * Verify the CRC of the frame and discard the CRC characters from the
1399 * end of the buffer.
1401 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1402 if (ppp->flags & SC_DEBUG) {
1404 "ppp: frame with bad fcs, length = %d\n",
1406 ppp_print_buffer("bad frame", data, count);
1408 ++ppp->estats.rx_crc_errors;
1411 count -= 2; /* ignore the fcs characters */
1413 * Obtain the protocol from the frame
1415 proto = PPP_PROTOCOL(data);
1417 * Process the active decompressor.
1419 if ((ppp->sc_rc_state != (void *) 0) &&
1420 (ppp->flags & SC_DECOMP_RUN) &&
1421 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1422 if (proto == PPP_COMP) {
1424 * If the frame is compressed then decompress it.
1426 new_data = kmalloc (ppp->mru + PPP_HDRLEN, GFP_ATOMIC);
1427 if (new_data == NULL) {
1428 printk (KERN_ERR "ppp_doframe: no memory\n");
1429 new_count = DECOMP_ERROR;
1431 new_count = (*ppp->sc_rcomp->decompress)
1432 (ppp->sc_rc_state, data, count,
1433 new_data, ppp->mru + PPP_HDRLEN);
1435 switch (new_count) {
1437 ppp_doframe_lower (ppp, new_data, new_count);
1442 ppp->flags |= SC_DC_ERROR;
1445 case DECOMP_FATALERROR:
1446 ppp->flags |= SC_DC_FERROR;
1447 printk(KERN_ERR "ppp: fatal decomp error\n");
1451 * Log the error condition and discard the frame.
1455 slhc_toss (ppp->slcomp);
1456 ++ppp->stats.ppp_ierrors;
1459 * The frame is not special. Pass it through the compressor without
1460 * actually compressing the data
1462 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1465 } else if (proto == PPP_COMP && (ppp->flags & SC_DEBUG)) {
1466 printk(KERN_DEBUG "ppp: frame not decompressed: "
1467 "flags=%x, count=%d, sc_rc_state=%p\n",
1468 ppp->flags, count, ppp->sc_rc_state);
1471 * Process the uncompressed frame.
1473 ppp_doframe_lower (ppp, data, count);
1477 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
1479 __u16 proto = PPP_PROTOCOL (data);
1480 ppp_proto_type *proto_ptr;
1484 * Ignore empty frames
1486 if (count <= PPP_HDRLEN)
1489 * Count the frame and print it
1491 ++ppp->stats.ppp_ipackets;
1492 if (ppp->flags & SC_LOG_INPKT)
1493 ppp_print_buffer ("receive frame", data, count);
1495 * Find the procedure to handle this protocol. The last one is marked
1496 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1498 proto_ptr = proto_list;
1499 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1502 * Update the appropriate statistic counter.
1504 if ((*proto_ptr->func) (ppp, proto,
1506 count - PPP_HDRLEN))
1507 ppp->stats.ppp_ioctects += count;
1509 ++ppp->stats.ppp_discards;
1513 * Put the input frame into the networking system for the indicated protocol
1517 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1519 sk_buff *skb = dev_alloc_skb (count);
1521 * Generate a skb buffer for the new frame.
1524 if (ppp->flags & SC_DEBUG)
1526 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1527 ppp2dev (ppp)->name);
1531 * Move the received data from the input buffer to the skb buffer.
1533 skb->dev = ppp2dev (ppp); /* We are the device */
1534 skb->protocol = proto;
1535 skb->mac.raw = skb_data(skb);
1536 memcpy (skb_put(skb,count), data, count); /* move data */
1538 * Tag the frame and kick it to the proper receive routine
1540 #if LINUX_VERSION_CODE < VERSION(2,1,15)
1544 ppp->last_recv = jiffies;
1550 * Process the receipt of an IP frame
1554 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1557 if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1558 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1559 return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1564 * Process the receipt of an IPX frame
1568 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1571 if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1572 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1577 * Process the receipt of an VJ Compressed frame
1581 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
1582 __u8 *data, int count)
1585 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1586 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1587 if (new_count >= 0) {
1588 return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1590 if (ppp->flags & SC_DEBUG)
1592 "ppp: error in VJ decompression\n");
1598 * Process the receipt of an VJ Un-compressed frame
1602 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
1603 __u8 *data, int count)
1606 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1607 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1608 return rcv_proto_ip (ppp, PPP_IP, data, count);
1610 if (ppp->flags & SC_DEBUG)
1612 "ppp: error in VJ memorizing\n");
1618 * Receive all unclassified protocols.
1622 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
1623 __u8 *data, int len)
1626 register int current_idx;
1630 buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1631 current_idx &= ppp->ubuf->size; \
1632 if (current_idx == ppp->ubuf->tail) \
1638 * The total length includes the protocol data.
1639 * Lock the user information buffer.
1641 if (test_and_set_bit (0, &ppp->ubuf->locked)) {
1642 if (ppp->flags & SC_DEBUG)
1644 "ppp: rcv_proto_unknown: can't get lock\n");
1646 CHECK_BUF_MAGIC(ppp->ubuf);
1647 current_idx = ppp->ubuf->head;
1649 * Insert the buffer length (not counted), the protocol, and the data
1659 while (totlen-- > 0) {
1664 * The frame is complete. Update the head pointer and wakeup the pppd
1667 ppp->ubuf->head = current_idx;
1669 clear_bit (0, &ppp->ubuf->locked);
1670 wake_up_interruptible (&ppp->read_wait);
1671 if (ppp->tty->fasync != NULL)
1672 kill_fasync (ppp->tty->fasync, SIGIO);
1676 * The buffer is full. Unlock the header
1679 clear_bit (0, &ppp->ubuf->locked);
1680 if (ppp->flags & SC_DEBUG)
1682 "ppp: rcv_proto_unknown: buffer overflow\n");
1685 * Discard the frame. There are no takers for this protocol.
1687 if (ppp->flags & SC_DEBUG)
1689 "ppp: rcv_proto_unknown: dropping packet\n");
1694 * Handle a CCP packet.
1696 * The CCP packet is passed along to the pppd process just like any
1697 * other PPP frame. The difference is that some processing needs to be
1698 * immediate or the compressors will become confused on the peer.
1701 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
1703 int slen = CCP_LENGTH(dp);
1704 __u8 *opt = dp + CCP_HDRLEN;
1705 int opt_len = slen - CCP_HDRLEN;
1706 unsigned long flags;
1712 switch (CCP_CODE(dp)) {
1717 * CCP must be going down - disable compression
1719 if (ppp->flags & SC_CCP_UP) {
1721 ppp->flags &= ~(SC_CCP_UP |
1728 if ((ppp->flags & SC_CCP_OPEN) == 0)
1730 if (ppp->flags & SC_CCP_UP)
1732 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1734 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1737 * we're agreeing to send compressed packets.
1740 if (ppp->sc_xc_state == NULL)
1743 if ((*ppp->sc_xcomp->comp_init)
1747 ppp2dev (ppp)->base_addr,
1749 ppp->flags & SC_DEBUG)) {
1750 if (ppp->flags & SC_DEBUG)
1751 printk(KERN_DEBUG "%s: comp running\n",
1754 ppp->flags |= SC_COMP_RUN;
1759 * peer is agreeing to send compressed packets.
1761 if (ppp->sc_rc_state == NULL)
1764 if ((*ppp->sc_rcomp->decomp_init)
1768 ppp2dev (ppp)->base_addr,
1771 ppp->flags & SC_DEBUG)) {
1772 if (ppp->flags & SC_DEBUG)
1773 printk(KERN_DEBUG "%s: decomp running\n",
1776 ppp->flags |= SC_DECOMP_RUN;
1777 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1781 * CCP Reset-ack resets compressors and decompressors as it passes through.
1784 if ((ppp->flags & SC_CCP_UP) == 0)
1788 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN)) {
1789 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1790 if (ppp->flags & SC_DEBUG)
1791 printk(KERN_DEBUG "%s: comp reset\n",
1795 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1796 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1797 if (ppp->flags & SC_DEBUG)
1798 printk(KERN_DEBUG "%s: decomp reset\n",
1801 ppp->flags &= ~SC_DC_ERROR;
1806 if (ppp->flags & SC_DEBUG)
1807 printk(KERN_DEBUG "ppp_proto_ccp: %s code %d, flags=%x\n",
1808 (rcvd? "rcvd": "sent"), CCP_CODE(dp), ppp->flags);
1809 restore_flags(flags);
1813 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
1816 ppp_proto_ccp (ppp, dp, len, 1);
1817 return rcv_proto_unknown (ppp, proto, dp, len);
1821 * Handle a LQR packet.
1825 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
1827 return rcv_proto_unknown (ppp, proto, data, len);
1830 /*************************************************************
1831 * LINE DISCIPLINE SUPPORT
1832 * The following functions form support user programs
1833 * which read and write data on a TTY with the PPP line
1834 * discipline. Reading is done from a circular queue,
1835 * filled by the lower TTY levels.
1836 *************************************************************/
1838 /* read a PPP frame from the us_rbuff circular buffer,
1839 waiting if necessary
1843 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
1846 struct ppp *ppp = tty2ppp (tty);
1853 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1854 ppp->ubuf->tail &= ppp->ubuf->size; \
1858 * Validate the pointers
1863 /* if (ppp->magic != PPP_MAGIC)
1869 * Before we attempt to write the frame to the user, ensure that the
1870 * user has access to the pages for the total buffer length.
1872 error = verify_area (VERIFY_WRITE, buf, nr);
1877 * Acquire the read lock.
1880 ppp = tty2ppp (tty);
1881 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
1885 if (test_and_set_bit (0, &ppp->ubuf->locked) != 0) {
1887 if (ppp->flags & SC_DEBUG)
1889 "ppp_tty_read: sleeping(ubuf)\n");
1891 current->timeout = 0;
1892 current->state = TASK_INTERRUPTIBLE;
1895 if (signal_pending(current))
1901 * Fetch the length of the buffer from the first two bytes.
1903 if (ppp->ubuf->head == ppp->ubuf->tail)
1915 * If there is no length then wait for the data to arrive.
1918 clear_bit (0, &ppp->ubuf->locked);
1919 if (file->f_flags & O_NONBLOCK)
1921 current->timeout = 0;
1923 if (ppp->flags & SC_DEBUG)
1925 "ppp_tty_read: sleeping(read_wait)\n");
1927 interruptible_sleep_on (&ppp->read_wait);
1928 if (signal_pending(current))
1933 * Ensure that the frame will fit within the caller's buffer. If not, then
1934 * discard the frame from the input buffer.
1937 /* Can't copy it, update us_rbuff_head */
1939 if (ppp->flags & SC_DEBUG)
1941 "ppp: read of %lu bytes too small for %ld "
1942 "frame\n", (unsigned long) nr, (long) len + 2);
1943 ppp->stats.ppp_ierrors++;
1949 * Fake the insertion of the ADDRESS and CONTROL information because these
1950 * were not saved in the buffer.
1952 PUT_USER (error, (u_char) PPP_ALLSTATIONS, buf);
1956 PUT_USER (error, (u_char) PPP_UI, buf);
1962 * Copy the received data from the buffer to the caller's area.
1964 ret = len + 2; /* Account for ADDRESS and CONTROL bytes */
1967 PUT_USER (error, c, buf);
1973 clear_bit (0, &ppp->ubuf->locked);
1977 ppp->ubuf->tail += len;
1978 ppp->ubuf->tail &= ppp->ubuf->size;
1979 clear_bit (0, &ppp->ubuf->locked);
1984 /* stuff a character into the transmit buffer, using PPP's way of escaping
1986 also, update fcs to take account of new character */
1989 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1993 * The buffer should not be full.
1995 if (ppp->flags & SC_DEBUG) {
1996 if ((buf->count < 0) || (buf->count > 3000))
1997 printk (KERN_DEBUG "ppp_stuff_char: %d %x\n",
1998 (unsigned int) buf->count,
1999 (unsigned int) chr);
2002 * Update the FCS and if the character needs to be escaped, do it.
2004 buf->fcs = PPP_FCS (buf->fcs, chr);
2005 if (in_xmap (ppp, chr)) {
2007 ins_char (buf, PPP_ESCAPE);
2010 * Add the character to the buffer.
2012 ins_char (buf, chr);
2016 * Procedure to encode the data with the proper escaping and send the
2017 * data to the remote system.
2021 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
2022 __u8 *data, int count, int non_ip)
2025 int address, control;
2029 CHECK_BUF_MAGIC(buf);
2030 ++ppp->stats.ppp_opackets;
2031 ppp->stats.ppp_ooctects += count;
2034 * Insert the leading FLAG character
2038 if (non_ip || flag_time == 0
2039 || jiffies - ppp->last_xmit >= flag_time)
2040 ins_char (buf, PPP_FLAG);
2042 ppp->last_xmit = jiffies;
2043 buf->fcs = PPP_INITFCS;
2045 * Emit the address/control information if needed
2047 address = PPP_ADDRESS (data);
2048 control = PPP_CONTROL (data);
2049 proto = PPP_PROTOCOL (data);
2051 if (address != PPP_ALLSTATIONS ||
2052 control != PPP_UI ||
2053 (ppp->flags & SC_COMP_AC) == 0) {
2054 ppp_stuff_char (ppp, buf, address);
2055 ppp_stuff_char (ppp, buf, control);
2058 * Emit the protocol (compressed if possible)
2060 if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
2061 ppp_stuff_char (ppp, buf, proto >> 8);
2063 ppp_stuff_char (ppp, buf, proto);
2071 ppp_stuff_char (ppp, buf, *data++);
2073 * Add the trailing CRC and the final flag character
2075 write_fcs = buf->fcs ^ 0xFFFF;
2076 ppp_stuff_char (ppp, buf, write_fcs);
2077 ppp_stuff_char (ppp, buf, write_fcs >> 8);
2079 * Add the trailing flag character
2081 ins_char (buf, PPP_FLAG);
2083 * Send the block to the tty driver.
2085 ppp->stats.ppp_obytes += buf->count;
2086 ppp_kick_tty (ppp, buf);
2090 * Compress and send an frame to the peer.
2092 * Return 0 if frame was queued for transmission.
2093 * 1 if frame must be re-queued for later driver support.
2097 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
2098 __u8 *data, int count)
2101 int address, control;
2106 CHECK_BUF_MAGIC(buf);
2110 if (ppp->flags & SC_LOG_OUTPKT)
2111 ppp_print_buffer ("write frame", data, count);
2113 * Determine if the frame may be compressed. Attempt to compress the
2114 * frame if possible.
2116 proto = PPP_PROTOCOL (data);
2117 address = PPP_ADDRESS (data);
2118 control = PPP_CONTROL (data);
2120 if (((ppp->flags & SC_COMP_RUN) != 0) &&
2121 (ppp->sc_xc_state != (void *) 0) &&
2122 (address == PPP_ALLSTATIONS) &&
2123 (control == PPP_UI) &&
2124 (proto != PPP_LCP) &&
2125 (proto != PPP_CCP)) {
2126 new_data = kmalloc (ppp->mtu + PPP_HDRLEN, GFP_ATOMIC);
2127 if (new_data == NULL) {
2128 printk (KERN_ERR "ppp_dev_xmit_frame: no memory\n");
2132 new_count = (*ppp->sc_xcomp->compress)
2133 (ppp->sc_xc_state, data, new_data, count,
2134 ppp->mtu + PPP_HDRLEN);
2136 if (new_count > 0 && (ppp->flags & SC_CCP_UP)) {
2137 ppp_dev_xmit_lower (ppp, buf, new_data, new_count, 0);
2142 * The frame could not be compressed, or it could not be sent in
2143 * compressed form because CCP is not yet up.
2148 * Go to the escape encoding
2150 ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2155 * Revise the tty frame for specific protocols.
2159 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
2163 switch (PPP_PROTOCOL (data)) {
2165 * Update the LQR frame with the current MIB information. This saves having
2166 * the daemon read old MIB data from the driver.
2169 len = 48; /* total size of this frame */
2170 p = (__u8 *) &data [40]; /* Point to last two items. */
2171 p = store_long (p, ppp->stats.ppp_opackets + 1);
2172 p = store_long (p, ppp->stats.ppp_ooctects + len);
2175 * Outbound compression frames
2192 * write a frame with NR chars from BUF to TTY
2193 * we have to put the FCS field on ourselves
2197 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
2200 struct ppp *ppp = tty2ppp (tty);
2203 struct wait_queue wait = {current, NULL};
2206 * Verify the pointers.
2211 if (ppp->magic != PPP_MAGIC)
2216 * Ensure that the caller does not wish to send too much.
2218 if (count > PPP_MTU + PPP_HDRLEN) {
2219 if (ppp->flags & SC_DEBUG)
2220 printk (KERN_WARNING
2221 "ppp_tty_write: truncating user packet "
2222 "from %lu to mtu %d\n", (unsigned long) count,
2223 PPP_MTU + PPP_HDRLEN);
2224 count = PPP_MTU + PPP_HDRLEN;
2227 * Allocate a buffer for the data and fetch it from the user space.
2229 new_data = kmalloc (count, GFP_KERNEL);
2230 if (new_data == NULL) {
2231 printk (KERN_ERR "ppp_tty_write: no memory\n");
2235 * Retrieve the user's buffer
2237 COPY_FROM_USER (error, new_data, data, count);
2241 * Lock this PPP unit so we will be the only writer,
2242 * sleeping if necessary.
2244 * Note that we add our task to the wait queue before
2245 * attempting to lock, as the lock flag may be cleared
2246 * from an interrupt.
2248 add_wait_queue(&ppp->write_wait, &wait);
2251 current->timeout = 0;
2252 current->state = TASK_INTERRUPTIBLE;
2253 if (lock_buffer(ppp->tbuf) == 0)
2258 ppp = tty2ppp (tty);
2259 if (!ppp || ppp->magic != PPP_MAGIC ||
2260 !ppp->inuse || tty != ppp->tty) {
2261 printk("ppp_tty_write: %p invalid after wait!\n", ppp);
2265 if (signal_pending(current))
2268 current->state = TASK_RUNNING;
2269 remove_wait_queue(&ppp->write_wait, &wait);
2274 * Change the LQR frame
2276 count = send_revise_frame (ppp, new_data, count);
2280 if (PPP_PROTOCOL(new_data) == PPP_IP) {
2282 * IP frames can be sent by pppd when we're doing
2283 * demand-dialling. We send them via ppp_dev_xmit_ip
2284 * to make sure that VJ compression happens properly.
2286 ppp_dev_xmit_ip(ppp, ppp->tbuf, new_data + PPP_HDRLEN,
2287 count - PPP_HDRLEN, NPMODE_PASS);
2290 ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2300 * Process the set-compression ioctl.
2304 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2306 struct compressor *cp;
2308 unsigned long flags;
2310 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2311 struct ppp_option_data data;
2314 * Fetch the compression parameters
2316 COPY_FROM_USER (error, &data, odp, sizeof (data));
2322 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2323 nb = CCP_MAX_OPTION_LENGTH;
2325 COPY_FROM_USER (error, ccp_option, ptr, nb);
2329 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2334 ppp->flags &= ~(data.transmit? SC_COMP_RUN: SC_DECOMP_RUN);
2335 restore_flags(flags);
2337 cp = find_compressor (ccp_option[0]);
2338 #if defined(CONFIG_KMOD) || defined(CONFIG_KERNELD)
2341 sprintf(modname, "ppp-compress-%d", ccp_option[0]);
2342 request_module(modname);
2343 cp = find_compressor(ccp_option[0]);
2345 #endif /* CONFIG_KERNELD */
2350 * Found a handler for the protocol - try to allocate
2351 * a compressor or decompressor.
2354 if (data.transmit) {
2355 if (ppp->sc_xc_state != NULL)
2356 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2357 ppp->sc_xc_state = NULL;
2360 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2361 if (ppp->sc_xc_state == NULL) {
2362 printk(KERN_WARNING "%s: comp_alloc failed\n",
2365 } else if (ppp->flags & SC_DEBUG)
2366 printk(KERN_DEBUG "%s: comp_alloc -> %p\n",
2367 ppp->name, ppp->sc_xc_state);
2369 if (ppp->sc_rc_state != NULL)
2370 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2371 ppp->sc_rc_state = NULL;
2374 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2375 if (ppp->sc_rc_state == NULL) {
2376 printk(KERN_WARNING "%s: decomp_alloc failed\n",
2379 } else if (ppp->flags & SC_DEBUG)
2380 printk(KERN_DEBUG "%s: decomp_alloc -> %p\n",
2381 ppp->name, ppp->sc_rc_state);
2386 if (ppp->flags & SC_DEBUG)
2387 printk(KERN_DEBUG "%s: no compressor for [%x %x %x], %x\n",
2388 ppp->name, ccp_option[0], ccp_option[1],
2390 return (-EINVAL); /* no handler found */
2394 * Process the IOCTL event for the tty device.
2398 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
2399 unsigned int param2, unsigned long param3)
2401 struct ppp *ppp = tty2ppp (tty);
2402 register int temp_i = 0, oldflags;
2404 unsigned long flags;
2406 * Verify the status of the PPP device.
2411 if (ppp->magic != PPP_MAGIC)
2416 * The user must have an euid of root to do these requests.
2425 GET_USER (error, temp_i, (int *) param3);
2428 if (ppp->flags & SC_DEBUG)
2430 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2432 if (ppp->mru != temp_i)
2433 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2439 temp_i = (ppp->flags & SC_MASK);
2440 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2441 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2442 SC_RCV_ODDP | SC_RCV_EVNP;
2444 PUT_USER (error, temp_i, (int *) param3);
2447 * Set the flags for the various options
2450 GET_USER (error, temp_i, (int *) param3);
2455 if ((ppp->flags & SC_CCP_OPEN) && (temp_i & SC_CCP_OPEN) == 0)
2456 ppp_ccp_closed(ppp);
2460 oldflags = ppp->flags;
2461 ppp->flags = temp_i |= (ppp->flags & ~SC_MASK);
2462 restore_flags(flags);
2463 if ((oldflags | temp_i) & SC_DEBUG)
2465 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2468 * Set the compression mode
2470 case PPPIOCSCOMPRESS:
2471 error = ppp_set_compression (ppp,
2472 (struct ppp_option_data *) param3);
2475 * Retrieve the transmit async map
2477 case PPPIOCGASYNCMAP:
2478 PUT_USER (error, ppp->xmit_async_map[0], (int *) param3);
2481 * Set the transmit async map
2483 case PPPIOCSASYNCMAP:
2484 GET_USER (error, temp_i, (int *) param3);
2487 ppp->xmit_async_map[0] = temp_i;
2488 if (ppp->flags & SC_DEBUG)
2490 "ppp_tty_ioctl: set xmit asyncmap %x\n",
2491 ppp->xmit_async_map[0]);
2494 * Set the receive async map
2496 case PPPIOCSRASYNCMAP:
2497 GET_USER (error, temp_i, (int *) param3);
2500 ppp->recv_async_map = temp_i;
2501 if (ppp->flags & SC_DEBUG)
2503 "ppp_tty_ioctl: set rcv asyncmap %x\n",
2504 ppp->recv_async_map);
2507 * Obtain the unit number for this device.
2510 PUT_USER (error, ppp2dev (ppp)->base_addr, (int *) param3);
2513 if (ppp->flags & SC_DEBUG)
2515 "ppp_tty_ioctl: get unit: %ld\n",
2516 ppp2dev (ppp)->base_addr);
2519 * Set the debug level
2522 GET_USER (error, temp_i, (int *) param3);
2525 temp_i = (temp_i & 0x1F) << 16;
2526 temp_i |= (ppp->flags & ~0x1F0000);
2528 if ((ppp->flags | temp_i) & SC_DEBUG)
2530 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2531 ppp->flags = temp_i;
2534 * Get the debug level
2537 temp_i = (ppp->flags >> 16) & 0x1F;
2538 PUT_USER (error, temp_i, (int *) param3);
2541 * Get the times since the last send/receive frame operation
2545 struct ppp_idle cur_ddinfo;
2547 /* change absolute times to relative times. */
2548 cur_ddinfo.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
2549 cur_ddinfo.recv_idle = (jiffies - ppp->last_recv) / HZ;
2550 COPY_TO_USER (error, (void *) param3, &cur_ddinfo,
2551 sizeof (cur_ddinfo));
2555 * Retrieve the extended async map
2557 case PPPIOCGXASYNCMAP:
2558 COPY_TO_USER (error, (void *) param3, ppp->xmit_async_map,
2559 sizeof (ppp->xmit_async_map));
2562 * Set the async extended map
2564 case PPPIOCSXASYNCMAP:
2568 COPY_FROM_USER (error, temp_tbl, (void *) param3,
2573 temp_tbl[1] = 0x00000000;
2574 temp_tbl[2] &= ~0x40000000;
2575 temp_tbl[3] |= 0x60000000;
2577 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2578 (temp_tbl[4] & temp_tbl[5]) != 0 ||
2579 (temp_tbl[6] & temp_tbl[7]) != 0)
2582 memcpy (ppp->xmit_async_map,
2584 sizeof (ppp->xmit_async_map));
2586 if (ppp->flags & SC_DEBUG)
2588 "ppp_tty_ioctl: set xasyncmap\n");
2593 * Set the maximum VJ header compression slot number.
2596 GET_USER (error, temp_i, (int *) param3);
2599 temp_i = (temp_i & 255) + 1;
2600 if (ppp->flags & SC_DEBUG)
2602 "ppp_tty_ioctl: set maxcid to %d\n", temp_i);
2603 if (ppp->slcomp != NULL)
2604 slhc_free (ppp->slcomp);
2607 ppp->slcomp = slhc_init (16, temp_i);
2608 if (ppp->slcomp == NULL) {
2609 printk (KERN_ERR "ppp_tty_ioctl: "
2610 "no space for compression buffers!\n");
2616 case PPPIOCXFERUNIT:
2617 ppp->backup_tty = tty;
2618 ppp->sc_xfer = current->pid;
2626 COPY_FROM_USER (error, &npi, (void *) param3,
2631 switch (npi.protocol) {
2633 npi.protocol = NP_IP;
2636 if (ppp->flags & SC_DEBUG)
2637 printk(KERN_DEBUG "pppioc[gs]npmode: "
2638 "invalid protocol %d\n",
2646 if (param2 == PPPIOCGNPMODE) {
2647 npi.mode = ppp->sc_npmode[npi.protocol];
2649 COPY_TO_USER (error, (void *) param3, &npi,
2654 ppp->sc_npmode[npi.protocol] = npi.mode;
2655 if (ppp->flags & SC_DEBUG)
2656 printk(KERN_DEBUG "ppp: set np %d to %d\n",
2657 npi.protocol, npi.mode);
2658 ppp2dev(ppp)->tbusy = 0;
2663 * Allow users to read, but not set, the serial port parameters
2667 error = n_tty_ioctl (tty, file, param2, param3);
2672 int count = ppp->ubuf->tail - ppp->ubuf->head;
2674 count += (ppp->ubuf->size + 1);
2675 PUT_USER (error, count, (int *) param3);
2679 * All other ioctl() events will come here.
2682 if (ppp->flags & SC_DEBUG)
2683 printk (KERN_WARNING "ppp_tty_ioctl: "
2684 "invalid ioctl=%x, addr=%lx\n", param2, param3);
2685 error = -ENOIOCTLCMD;
2694 * Process the select() (or poll()) statement for the PPP device.
2697 #if LINUX_VERSION_CODE < VERSION(2,1,23)
2699 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2700 struct file *filp, int sel_type, select_table * wait)
2702 struct ppp *ppp = tty2ppp (tty);
2705 * Verify the status of the PPP device.
2710 if (ppp->magic != PPP_MAGIC || tty != ppp->tty)
2715 * Branch on the type of select mode. A read request must lock the user
2720 if (test_and_set_bit (0, &ppp->ubuf->locked) == 0) {
2721 /* Test for the presence of data in the queue */
2722 if (ppp->ubuf->head != ppp->ubuf->tail) {
2723 clear_bit (0, &ppp->ubuf->locked);
2726 clear_bit (0, &ppp->ubuf->locked);
2727 } /* fall through */
2729 * Exceptions or read errors.
2732 /* Is this a pty link and the remote disconnected? */
2733 if (tty->flags & (1 << TTY_OTHER_CLOSED))
2736 /* Is this a local link and the modem disconnected? */
2737 if (tty_hung_up_p (filp))
2740 select_wait (&ppp->read_wait, wait);
2744 * Write mode. A write is allowed if there is no current transmission.
2747 if (ppp->tbuf->locked != 0) {
2748 select_wait (&ppp->write_wait, wait);
2756 #else /* 2.1.23 or later */
2759 ppp_tty_poll (struct tty_struct *tty, struct file *filp, poll_table * wait)
2761 struct ppp *ppp = tty2ppp (tty);
2762 unsigned int mask = 0;
2764 if (ppp && ppp->magic == PPP_MAGIC && tty == ppp->tty) {
2767 #if LINUX_VERSION_CODE < VERSION(2,1,89)
2768 poll_wait(&ppp->read_wait, wait);
2769 poll_wait(&ppp->write_wait, wait);
2771 poll_wait(filp, &ppp->read_wait, wait);
2772 poll_wait(filp, &ppp->write_wait, wait);
2775 /* Must lock the user buffer area while checking. */
2776 CHECK_BUF_MAGIC(ppp->ubuf);
2777 if(test_and_set_bit(0, &ppp->ubuf->locked) == 0) {
2778 if(ppp->ubuf->head != ppp->ubuf->tail)
2779 mask |= POLLIN | POLLRDNORM;
2780 clear_bit(0, &ppp->ubuf->locked);
2782 if(tty->flags & (1 << TTY_OTHER_CLOSED))
2784 if(tty_hung_up_p(filp))
2786 if(ppp->tbuf->locked == 0)
2787 mask |= POLLOUT | POLLWRNORM;
2794 /*************************************************************
2796 * This routine accepts requests from the network layer
2797 * and attempts to deliver the packets.
2798 * It also includes various routines we are compelled to
2799 * have to make the network layer work (arp, etc...).
2800 *************************************************************/
2803 * Callback from the network layer when the device goes up.
2807 ppp_dev_open (struct device *dev)
2809 struct ppp *ppp = dev2ppp (dev);
2811 if (ppp2tty (ppp) == NULL) {
2813 "ppp: %s not connected to a TTY! can't go open!\n",
2818 if (ppp->flags & SC_DEBUG)
2820 "ppp: channel %s going up for IP packets!\n",
2828 * Callback from the network layer when the ppp device goes down.
2832 ppp_dev_close (struct device *dev)
2834 struct ppp *ppp = dev2ppp (dev);
2836 if (ppp2tty (ppp) == NULL) {
2840 * We don't do anything about the device going down. It is not important
2843 if (ppp->flags & SC_DEBUG)
2845 "ppp: channel %s going down for IP packets!\n",
2852 * IOCTL operation to read the version of the driver.
2856 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2859 char *result = (char *) ifr->ifr_ifru.ifru_data;
2860 int len = strlen (szVersion) + 1;
2862 * Move the version data
2864 COPY_TO_USER (error, result, szVersion, len);
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, result, &temp, sizeof (temp));
2906 * IOCTL to read the compression statistics for the pppstats program.
2910 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2912 struct ppp_comp_stats *result, temp;
2915 * Supply the information for the caller.
2917 memset (&temp, 0, sizeof(temp));
2918 if (dev->flags & IFF_UP) {
2919 if (ppp->sc_xc_state != NULL)
2920 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2923 if (ppp->sc_rc_state != NULL)
2924 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2928 * Move the data to the caller's buffer
2930 result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2932 COPY_TO_USER (error, result, &temp, sizeof (temp));
2938 * Callback from the network layer to process the sockioctl functions.
2942 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2944 struct ppp *ppp = dev2ppp (dev);
2947 CHECK_PPP_MAGIC(ppp);
2949 * Process the requests
2953 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2956 case SIOCGPPPCSTATS:
2957 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2961 error = ppp_dev_ioctl_version (ppp, ifr);
2972 * Send an IP frame to the remote with vj header compression.
2974 * Return 0 if frame was queued for transmission.
2975 * 1 if frame must be re-queued for later driver support.
2976 * -1 if frame should be dropped.
2980 ppp_dev_xmit_ip (struct ppp *ppp, struct ppp_buffer *buf,
2981 __u8 *data, int len, enum NPmode npmode)
2986 * Branch on the type of processing for the IP frame.
2994 * We may not send the packet now, so drop it.
2995 * XXX It would be nice to be able to return it to the
2996 * network system to be queued and retransmitted later.
2998 if (ppp->flags & SC_DEBUG)
2999 printk(KERN_DEBUG "%s: returning frame\n",
3005 if (ppp->flags & SC_DEBUG)
3007 "ppp_dev_xmit: npmode = %d on %s\n",
3008 ppp->sc_npmode[NP_IP], ppp->name);
3012 if (ppp->flags & SC_DEBUG)
3013 printk (KERN_WARNING
3014 "ppp_dev_xmit: unknown npmode %d on %s\n",
3015 ppp->sc_npmode[NP_IP], ppp->name);
3019 * At this point, the buffer will be transmitted. There is no other exit.
3021 * Try to compress the header.
3023 if (ppp->flags & SC_COMP_TCP) {
3024 len = slhc_compress (ppp->slcomp, data, len,
3025 buf_base (ppp->cbuf) + PPP_HDRLEN,
3027 (ppp->flags & SC_NO_TCP_CCID) == 0);
3029 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
3030 proto = PPP_VJC_COMP;
3031 data[0] ^= SL_TYPE_COMPRESSED_TCP;
3033 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
3034 proto = PPP_VJC_UNCOMP;
3035 data[0] = (data[0] & 0x0f) | 0x40;
3042 hdr = data - PPP_HDRLEN;
3044 hdr[0] = PPP_ALLSTATIONS;
3049 return ppp_dev_xmit_frame (ppp, buf, hdr, len);
3053 * Send a non-IP data frame (such as an IPX frame) to the remote.
3055 * Return 0 if frame was queued for transmission.
3056 * 1 if frame must be re-queued for later driver support.
3059 ppp_dev_xmit_other (struct device *dev, struct ppp *ppp,
3060 __u8 *data, int len, int proto)
3067 hdr = data - PPP_HDRLEN;
3069 hdr[0] = PPP_ALLSTATIONS;
3071 hdr[2] = proto >> 8;
3074 return ppp_dev_xmit_frame (ppp, ppp->wbuf, hdr, len);
3078 * Send a frame to the remote.
3080 #if LINUX_VERSION_CODE >= VERSION(2,1,86)
3081 #define FREE_SKB(skb) dev_kfree_skb(skb)
3083 #define FREE_SKB(skb) dev_kfree_skb(skb, FREE_WRITE)
3087 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3091 struct ppp *ppp = dev2ppp (dev);
3092 struct tty_struct *tty = ppp2tty (ppp);
3094 * just a little sanity check.
3097 if (ppp->flags & SC_DEBUG)
3098 printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3102 * Avoid timing problem should tty hangup while data is queued to be sent
3109 * Validate the tty interface
3112 if (ppp->flags & SC_DEBUG)
3114 "ppp_dev_xmit: %s not connected to a TTY!\n",
3120 * Fetch the pointer to the data
3123 data = skb_data(skb);
3125 if (data == (__u8 *) 0) {
3126 if (ppp->flags & SC_DEBUG)
3127 printk (KERN_CRIT "ppp_dev_xmit: %s Null skb data\n",
3133 * Detect a change in the transfer size
3135 if (ppp->mtu != ppp2dev (ppp)->mtu) {
3136 ppp_changedmtu (ppp,
3141 * Acquire the lock on the transmission buffer. If the buffer was busy then
3142 * mark the device as busy.
3143 * We also require that ppp->tbuf be unlocked, in order to serialize
3144 * calls to ppp_dev_xmit_frame (which does compression) and the output
3145 * of frames w.r.t. tty writes from pppd.
3147 CHECK_BUF_MAGIC(ppp->wbuf);
3148 if (ppp->tbuf->locked || lock_buffer (ppp->wbuf) != 0) {
3150 if (ppp->flags & SC_DEBUG)
3151 printk(KERN_DEBUG "dev_xmit blocked, t=%lu w=%lu\n",
3152 ppp->tbuf->locked, ppp->wbuf->locked);
3156 * Look at the protocol in the skb to determine the difference between
3157 * an IP frame and an IPX frame.
3159 switch (ntohs (skb->protocol)) {
3161 answer = ppp_dev_xmit_other (dev, ppp, data, len, PPP_IPX);
3165 answer = ppp_dev_xmit_ip (ppp, ppp->wbuf, data, len,
3166 ppp->sc_npmode[NP_IP]);
3169 default: /* All others have no support at this time. */
3174 * This is the end of the transmission. Release the buffer if it was sent.
3177 /* packet queued OK */
3180 ppp->wbuf->locked = 0;
3182 /* packet should be dropped */
3186 /* packet should be queued for later */
3194 * Generate the statistic information for the /proc/net/dev listing.
3197 static struct net_device_stats *
3198 ppp_dev_stats (struct device *dev)
3200 struct ppp *ppp = dev2ppp (dev);
3202 ppp->estats.rx_packets = ppp->stats.ppp_ipackets;
3203 ppp->estats.rx_errors = ppp->stats.ppp_ierrors;
3204 ppp->estats.tx_packets = ppp->stats.ppp_opackets;
3205 ppp->estats.tx_errors = ppp->stats.ppp_oerrors;
3206 #if LINUX_VERSION_CODE >= VERSION(2,1,25)
3207 ppp->estats.rx_bytes = ppp->stats.ppp_ibytes;
3208 ppp->estats.tx_bytes = ppp->stats.ppp_obytes;
3211 return &ppp->estats;
3214 #if LINUX_VERSION_CODE < VERSION(2,1,15)
3215 static int ppp_dev_header (sk_buff *skb, struct device *dev,
3216 __u16 type, void *daddr,
3217 void *saddr, unsigned int len)
3223 ppp_dev_rebuild (void *eth, struct device *dev,
3224 unsigned long raddr, struct sk_buff *skb)
3230 /*************************************************************
3232 * Miscellany called by various functions above.
3233 *************************************************************/
3235 /* Locate the previous instance of the PPP channel */
3237 ppp_find (int pid_value)
3241 /* try to find the device which this pid is already using */
3242 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3243 if (ppp->inuse && ppp->sc_xfer == pid_value) {
3251 /* Collect hung up channels */
3253 static void ppp_sync(void)
3258 #if LINUX_VERSION_CODE >= VERSION(2,1,68)
3261 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3264 if (dev->flags & IFF_UP)
3268 #if LINUX_VERSION_CODE >= VERSION(2,1,68)
3274 /* allocate or create a PPP channel */
3285 /* try to find an free device */
3287 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3288 if (!test_and_set_bit(0, &ppp->inuse)) {
3290 /* Reregister device */
3293 unregister_netdev (dev);
3295 if (register_netdev (dev)) {
3296 printk(KERN_DEBUG "cannot reregister ppp device\n");
3304 * There are no available units, so make a new one.
3306 ppp = (struct ppp *) kmalloc (sizeof(struct ppp), GFP_KERNEL);
3309 memset(ppp, 0, sizeof(*ppp));
3311 /* initialize channel control data */
3312 set_bit(0, &ppp->inuse);
3316 ppp->backup_tty = NULL;
3320 ppp_last->next = ppp;
3326 dev->init = ppp_init_dev;
3327 dev->name = ppp->name;
3328 sprintf(dev->name, "ppp%d", if_num);
3329 dev->base_addr = (__u32) if_num;
3330 dev->priv = (void *) ppp;
3332 /* register device so that we can be ifconfig'd */
3333 /* ppp_init_dev() will be called as a side-effect */
3334 status = register_netdev (dev);
3336 printk (KERN_INFO "registered device %s\n", dev->name);
3339 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3342 /* This one will forever be busy as it is not initialized */
3348 * Utility procedures to print a buffer in hex/ascii
3352 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3354 register __u8 next_ch;
3355 static char hex[] = "0123456789ABCDEF";
3357 while (count-- > 0) {
3359 *out++ = hex[(next_ch >> 4) & 0x0F];
3360 *out++ = hex[next_ch & 0x0F];
3366 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3368 register __u8 next_ch;
3370 while (count-- > 0) {
3373 if (next_ch < 0x20 || next_ch > 0x7e)
3377 if (next_ch == '%') /* printk/syslogd has a bug !! */
3385 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
3389 if (name != (__u8 *) NULL)
3390 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3393 memset (line, 32, 44);
3394 ppp_print_hex (line, buf, 8);
3395 ppp_print_char (&line[8 * 3], buf, 8);
3396 printk (KERN_DEBUG "%s\n", line);
3402 memset (line, 32, 44);
3403 ppp_print_hex (line, buf, count);
3404 ppp_print_char (&line[8 * 3], buf, count);
3405 printk (KERN_DEBUG "%s\n", line);
3409 /*************************************************************
3410 * Compressor module interface
3411 *************************************************************/
3413 struct compressor_link {
3414 struct compressor_link *next;
3415 struct compressor *comp;
3418 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3420 static struct compressor *find_compressor (int type)
3422 struct compressor_link *lnk;
3423 unsigned long flags;
3428 lnk = ppp_compressors;
3429 while (lnk != (struct compressor_link *) 0) {
3430 if ((int) (__u8) lnk->comp->compress_proto == type) {
3431 restore_flags(flags);
3437 restore_flags(flags);
3438 return (struct compressor *) 0;
3441 static int ppp_register_compressor (struct compressor *cp)
3443 struct compressor_link *new;
3444 unsigned long flags;
3446 new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3448 if (new == (struct compressor_link *) 0)
3454 if (find_compressor (cp->compress_proto)) {
3455 restore_flags(flags);
3460 new->next = ppp_compressors;
3462 ppp_compressors = new;
3464 restore_flags(flags);
3468 static void ppp_unregister_compressor (struct compressor *cp)
3470 struct compressor_link *prev = (struct compressor_link *) 0;
3471 struct compressor_link *lnk;
3472 unsigned long flags;
3477 lnk = ppp_compressors;
3478 while (lnk != (struct compressor_link *) 0) {
3479 if (lnk->comp == cp) {
3481 prev->next = lnk->next;
3483 ppp_compressors = lnk->next;
3490 restore_flags(flags);
3493 /*************************************************************
3494 * Module support routines
3495 *************************************************************/
3503 /* register our line disciplines */
3504 status = ppp_first_time();
3507 "PPP: ppp_init() failure %d\n", status);
3508 #if LINUX_VERSION_CODE < VERSION(2,1,18)
3510 (void) register_symtab (&ppp_syms);
3516 cleanup_module(void)
3520 struct ppp *ppp, *next_ppp;
3523 * Ensure that the devices are not in operation.
3525 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3526 if (ppp->inuse && ppp->tty != NULL) {
3531 dev = ppp2dev (ppp);
3532 if (dev->start || dev->flags & IFF_UP) {
3538 * Ensure that there are no compressor modules registered
3540 if (ppp_compressors != NULL)
3545 "PPP: device busy, remove delayed\n");
3549 * Release the tty registration of the line discipline so that no new entries
3552 status = tty_register_ldisc (N_PPP, NULL);
3555 "PPP: Unable to unregister ppp line discipline "
3556 "(err = %d)\n", status);
3559 "PPP: ppp line discipline successfully unregistered\n");
3561 * De-register the devices so that there is no problem with them
3563 for (ppp = ppp_list; ppp != 0; ppp = next_ppp) {
3564 next_ppp = ppp->next;
3566 unregister_netdev (&ppp->dev);