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.16 1998/03/19 05:02:17 paulus Exp $ */
53 #include <linux/version.h>
54 #include <linux/config.h> /* for CONFIG_KERNELD */
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>
117 #define PPP_IPX 0x2b /* IPX protocol over PPP */
121 #define PPP_LQR 0xc025 /* Link Quality Reporting Protocol */
124 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
125 #include <asm/segment.h>
126 #define GET_USER(error,value,addr) error = get_user(value,addr)
127 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
128 #define PUT_USER(error,value,addr) error = put_user(value,addr)
129 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
131 #if LINUX_VERSION_CODE >= VERSION(2,1,5)
132 #include <asm/uaccess.h>
135 #else /* 2.0.x and 2.1.x before 2.1.4 */
137 #define GET_USER(error,value,addr) \
139 error = verify_area (VERIFY_READ, (void *) addr, sizeof (value)); \
141 value = get_user(addr); \
144 #define COPY_FROM_USER(error,dest,src,size) \
146 error = verify_area (VERIFY_READ, (void *) src, size); \
148 memcpy_fromfs (dest, src, size); \
151 #define PUT_USER(error,value,addr) \
153 error = verify_area (VERIFY_WRITE, (void *) addr, sizeof (value)); \
155 put_user (value, addr); \
158 #define COPY_TO_USER(error,dest,src,size) \
160 error = verify_area (VERIFY_WRITE, (void *) dest, size); \
162 memcpy_tofs (dest, src, size); \
167 #if LINUX_VERSION_CODE < VERSION(2,1,37)
168 #define test_and_set_bit(nr, addr) set_bit(nr, addr)
171 #if LINUX_VERSION_CODE < VERSION(2,1,57)
172 #define signal_pending(p) ((p)->signal & ~(p)->blocked)
175 #if LINUX_VERSION_CODE < VERSION(2,1,25)
176 #define net_device_stats enet_statistics
179 #if LINUX_VERSION_CODE < VERSION(2,1,60)
180 typedef int rw_ret_t;
181 typedef unsigned int rw_count_t;
183 typedef ssize_t rw_ret_t;
184 typedef size_t rw_count_t;
187 static int ppp_register_compressor (struct compressor *cp);
188 static void ppp_unregister_compressor (struct compressor *cp);
194 static struct compressor *find_compressor (int type);
195 static void ppp_init_ctrl_blk (register struct ppp *);
196 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
197 static struct ppp *ppp_alloc (void);
198 static struct ppp *ppp_find (int pid_value);
199 static void ppp_print_buffer (const __u8 *, const __u8 *, int);
200 extern inline void ppp_stuff_char (struct ppp *ppp,
201 register struct ppp_buffer *buf,
203 extern inline int lock_buffer (register struct ppp_buffer *buf);
204 static int ppp_dev_xmit_ip (struct ppp *ppp, struct ppp_buffer *buf,
205 __u8 *data, int len, enum NPmode npmode);
207 static int rcv_proto_ip (struct ppp *, __u16, __u8 *, int);
208 static int rcv_proto_ipx (struct ppp *, __u16, __u8 *, int);
209 static int rcv_proto_vjc_comp (struct ppp *, __u16, __u8 *, int);
210 static int rcv_proto_vjc_uncomp (struct ppp *, __u16, __u8 *, int);
211 static int rcv_proto_unknown (struct ppp *, __u16, __u8 *, int);
212 static int rcv_proto_lqr (struct ppp *, __u16, __u8 *, int);
213 static void ppp_doframe_lower (struct ppp *, __u8 *, int);
214 static int ppp_doframe (struct ppp *);
216 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd);
217 static int rcv_proto_ccp (struct ppp *, __u16, __u8 *, int);
219 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
221 #ifndef OPTIMIZE_FLAG_TIME
222 #define OPTIMIZE_FLAG_TIME 0
226 * Parameters which may be changed via insmod.
229 static int flag_time = OPTIMIZE_FLAG_TIME;
231 #if LINUX_VERSION_CODE >= VERSION(2,1,19)
232 MODULE_PARM(flag_time, "i");
236 * The "main" procedure to the ppp device
239 int ppp_init (struct device *);
242 * Network device driver callback routines
245 static int ppp_dev_open (struct device *);
246 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
247 static int ppp_dev_close (struct device *);
248 static int ppp_dev_xmit (sk_buff *, struct device *);
249 static struct net_device_stats *ppp_dev_stats (struct device *);
251 #if LINUX_VERSION_CODE < VERSION(2,1,15)
252 static int ppp_dev_header (sk_buff *, struct device *, __u16,
253 void *, void *, unsigned int);
254 static int ppp_dev_rebuild (void *eth, struct device *dev,
255 unsigned long raddr, struct sk_buff *skb);
262 static rw_ret_t ppp_tty_read(struct tty_struct *, struct file *, __u8 *,
264 static rw_ret_t ppp_tty_write(struct tty_struct *, struct file *, const __u8 *,
266 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
268 #if LINUX_VERSION_CODE < VERSION(2,1,23)
269 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
270 struct file *filp, int sel_type, select_table * wait);
272 static unsigned int ppp_tty_poll (struct tty_struct *tty, struct file *filp,
275 static int ppp_tty_open (struct tty_struct *);
276 static void ppp_tty_close (struct tty_struct *);
277 static int ppp_tty_room (struct tty_struct *tty);
278 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
279 char *fp, int count);
280 static void ppp_tty_wakeup (struct tty_struct *tty);
282 #define CHECK_PPP_MAGIC(ppp) do { \
283 if (ppp->magic != PPP_MAGIC) { \
284 printk(KERN_WARNING "bad magic for ppp %p at %s:%d\n", \
285 ppp, __FILE__, __LINE__); \
288 #define CHECK_PPP(a) do { \
289 CHECK_PPP_MAGIC(ppp); \
291 printk (ppp_warning, __LINE__); \
295 #define CHECK_PPP_VOID() do { \
296 CHECK_PPP_MAGIC(ppp); \
298 printk (ppp_warning, __LINE__); \
302 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
303 #define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \
304 ppp->recv_async_map & (1 << (c)))
306 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
308 #define tty2ppp(tty) ((struct ppp *) ((tty)->disc_data))
309 #define dev2ppp(dev) ((struct ppp *) ((dev)->priv))
310 #define ppp2tty(ppp) ((ppp)->tty)
311 #define ppp2dev(ppp) (&(ppp)->dev)
313 static struct ppp *ppp_list = NULL;
314 static struct ppp *ppp_last = NULL;
317 #define BUFFER_TYPE_DEV_RD 0 /* ppp read buffer */
318 #define BUFFER_TYPE_TTY_WR 1 /* tty write buffer */
319 #define BUFFER_TYPE_DEV_WR 2 /* ppp write buffer */
320 #define BUFFER_TYPE_TTY_RD 3 /* tty read buffer */
321 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
323 /* Define this string only once for all macro invocations */
324 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
326 static char szVersion[] = PPP_VERSION;
329 * Information for the protocol decoder
332 typedef int (*pfn_proto) (struct ppp *, __u16, __u8 *, int);
334 typedef struct ppp_proto_struct {
340 ppp_proto_type proto_list[] = {
341 { PPP_IP, rcv_proto_ip },
342 { PPP_IPX, rcv_proto_ipx },
343 { PPP_VJC_COMP, rcv_proto_vjc_comp },
344 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
345 { PPP_LQR, rcv_proto_lqr },
346 { PPP_CCP, rcv_proto_ccp },
347 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
350 __u16 ppp_crc16_table[256] =
352 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
353 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
354 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
355 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
356 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
357 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
358 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
359 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
360 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
361 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
362 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
363 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
364 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
365 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
366 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
367 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
368 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
369 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
370 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
371 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
372 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
373 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
374 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
375 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
376 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
377 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
378 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
379 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
380 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
381 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
382 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
383 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
386 #ifdef CHECK_CHARACTERS
387 static __u32 paritytab[8] =
389 0x96696996, 0x69969669, 0x69969669, 0x96696996,
390 0x69969669, 0x96696996, 0x96696996, 0x69969669
394 /* local function to store a value into the LQR frame */
395 extern inline __u8 * store_long (register __u8 *p, register int value) {
396 *p++ = (__u8) (value >> 24);
397 *p++ = (__u8) (value >> 16);
398 *p++ = (__u8) (value >> 8);
403 /*************************************************************
405 *************************************************************/
407 /* This procedure is called once and once only to define who we are to
408 * the operating system and the various procedures that it may use in
409 * accessing the ppp protocol.
413 ppp_first_time (void)
415 static struct tty_ldisc ppp_ldisc;
419 "PPP: version %s (demand dialling)"
422 #ifndef MODULE /* slhc module logic has its own copyright announcement */
424 "TCP compression code copyright 1989 Regents of the "
425 "University of California\n");
429 * Register the tty discipline
431 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
432 ppp_ldisc.magic = TTY_LDISC_MAGIC;
433 #if LINUX_VERSION_CODE >= VERSION(2,1,28)
434 ppp_ldisc.name = "ppp";
436 ppp_ldisc.open = ppp_tty_open;
437 ppp_ldisc.close = ppp_tty_close;
438 ppp_ldisc.read = ppp_tty_read;
439 ppp_ldisc.write = ppp_tty_write;
440 ppp_ldisc.ioctl = ppp_tty_ioctl;
441 #if LINUX_VERSION_CODE < VERSION(2,1,23)
442 ppp_ldisc.select = ppp_tty_select;
444 ppp_ldisc.poll = ppp_tty_poll;
446 ppp_ldisc.receive_room = ppp_tty_room;
447 ppp_ldisc.receive_buf = ppp_tty_receive;
448 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
450 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
452 printk (KERN_INFO "PPP line discipline registered.\n");
454 printk (KERN_ERR "error registering line discipline: %d\n",
459 /*************************************************************
461 *************************************************************/
463 /* called when the device is actually created */
466 ppp_init_dev (struct device *dev)
468 #if LINUX_VERSION_CODE < VERSION(2,1,15)
469 dev->hard_header = ppp_dev_header;
470 dev->rebuild_header = ppp_dev_rebuild;
473 dev->hard_header_len = PPP_HDRLEN;
477 dev->hard_start_xmit = ppp_dev_xmit;
478 dev->open = ppp_dev_open;
479 dev->stop = ppp_dev_close;
480 dev->get_stats = ppp_dev_stats;
481 dev->do_ioctl = ppp_dev_ioctl;
483 dev->tx_queue_len = 10;
484 dev->type = ARPHRD_PPP;
486 #if LINUX_VERSION_CODE < VERSION(2,1,20)
490 for (indx = 0; indx < DEV_NUMBUFFS; indx++)
491 skb_queue_head_init (&dev->buffs[indx]);
494 dev_init_buffers(dev);
497 /* New-style flags */
498 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
499 #if LINUX_VERSION_CODE < VERSION(2,1,67)
500 dev->family = AF_INET;
504 dev->pa_alen = 4; /* sizeof (__u32) */
511 * Local procedure to initialize the ppp structure
515 ppp_init_ctrl_blk (register struct ppp *ppp)
517 ppp->magic = PPP_MAGIC;
525 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
526 ppp->xmit_async_map[0] = 0xffffffff;
527 ppp->xmit_async_map[3] = 0x60000000;
528 ppp->recv_async_map = 0x00000000;
535 ppp->read_wait = NULL;
536 ppp->write_wait = NULL;
537 ppp->last_xmit = jiffies - flag_time;
538 ppp->last_recv = jiffies;
540 /* clear statistics */
541 memset(&ppp->stats, 0, sizeof (struct pppstat));
542 memset(&ppp->estats, 0, sizeof(ppp->estats));
544 /* PPP compression data */
546 ppp->sc_rc_state = NULL;
549 #if LINUX_VERSION_CODE < VERSION(2,1,18)
550 static struct symbol_table ppp_syms = {
551 #include <linux/symtab_begin.h>
552 X(ppp_register_compressor),
553 X(ppp_unregister_compressor),
555 #include <linux/symtab_end.h>
558 EXPORT_SYMBOL(ppp_register_compressor);
559 EXPORT_SYMBOL(ppp_unregister_compressor);
560 EXPORT_SYMBOL(ppp_crc16_table);
563 /* called at boot/load time for each ppp device defined in the kernel */
567 ppp_init (struct device *dev)
569 static int first_time = 1;
574 answer = ppp_first_time();
575 #if LINUX_VERSION_CODE < VERSION(2,1,18)
577 (void) register_symtab (&ppp_syms);
586 #define BUFFER_MAGIC 0x1d10
587 #define CHECK_BUF_MAGIC(buf) do { \
588 if (buf->magic != BUFFER_MAGIC) { \
589 printk(KERN_WARNING "bad magic for ppp buffer %p at %s:%d\n", \
590 buf, __FILE__, __LINE__); \
595 * Routine to allocate a buffer for later use by the driver.
598 static struct ppp_buffer *
599 ppp_alloc_buf (int size, int type)
601 struct ppp_buffer *buf;
603 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
607 buf->size = size - 1; /* Mask for the buffer size */
613 buf->fcs = PPP_INITFCS;
614 buf->magic = BUFFER_MAGIC;
620 * Routine to release the allocated buffer.
624 ppp_free_buf (struct ppp_buffer *ptr)
627 CHECK_BUF_MAGIC(ptr);
633 * Lock the indicated transmit buffer
637 lock_buffer (register struct ppp_buffer *buf)
642 * Save the current state and if free then set it to the "busy" state
644 CHECK_BUF_MAGIC(buf);
651 restore_flags (flags);
656 * MTU has been changed by the IP layer. Unfortunately we are not told
657 * about this, but we spot it ourselves and fix things up. We could be
658 * in an upcall from the tty driver, or in an ip packet queue.
662 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
667 struct ppp_buffer *new_rbuf;
668 struct ppp_buffer *new_wbuf;
669 struct ppp_buffer *new_cbuf;
670 struct ppp_buffer *new_tbuf;
672 struct ppp_buffer *old_rbuf;
673 struct ppp_buffer *old_wbuf;
674 struct ppp_buffer *old_cbuf;
675 struct ppp_buffer *old_tbuf;
679 * Allocate the buffer from the kernel for the data
683 if (ppp->flags & SC_DEBUG)
684 printk(KERN_DEBUG "%s: changedmtu %d %d\n", ppp->name,
687 /* allow for possible escaping of every character */
688 mtu = (new_mtu * 2) + 20;
690 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
696 new_wbuf = ppp_alloc_buf (mtu+PPP_HDRLEN, BUFFER_TYPE_DEV_WR);
697 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
698 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
699 new_cbuf = ppp_alloc_buf (mru+PPP_HDRLEN, BUFFER_TYPE_VJ);
701 * If the buffers failed to allocate then complain and release the partial
704 if (new_wbuf == NULL || new_tbuf == NULL ||
705 new_rbuf == NULL || new_cbuf == NULL) {
706 printk (KERN_ERR "ppp: failed to allocate new buffers\n");
708 ppp_free_buf (new_wbuf);
709 ppp_free_buf (new_tbuf);
710 ppp_free_buf (new_rbuf);
711 ppp_free_buf (new_cbuf);
715 * Update the pointers to the new buffer structures.
719 old_wbuf = ppp->wbuf;
720 old_rbuf = ppp->rbuf;
721 old_cbuf = ppp->cbuf;
722 old_tbuf = ppp->tbuf;
724 ppp->wbuf = new_wbuf;
725 ppp->rbuf = new_rbuf;
726 ppp->cbuf = new_cbuf;
727 ppp->tbuf = new_tbuf;
730 new_wbuf->locked = old_wbuf->locked;
732 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
734 dev->mem_start = (unsigned long) buf_base (new_wbuf);
735 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
736 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
737 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
739 * Update the parameters for the new buffer sizes
741 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
742 ppp->escape = 0; /* No pending escape character */
752 ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
753 ppp->flags &= ~SC_XMIT_BUSY;
755 restore_flags(flags);
757 * Release old buffer pointers
759 ppp_free_buf (old_rbuf);
760 ppp_free_buf (old_wbuf);
761 ppp_free_buf (old_cbuf);
762 ppp_free_buf (old_tbuf);
767 * CCP is down; free (de)compressor state if necessary.
771 ppp_ccp_closed (struct ppp *ppp)
777 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
778 restore_flags(flags);
779 if (ppp->flags & SC_DEBUG)
780 printk(KERN_DEBUG "%s: ccp closed\n", ppp->name);
781 if (ppp->sc_xc_state) {
782 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
783 ppp->sc_xc_state = NULL;
786 if (ppp->sc_rc_state) {
787 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
788 ppp->sc_rc_state = NULL;
793 * Called to release all of the information in the current PPP structure.
795 * It is called when the ppp device goes down or if it is unable to go
800 ppp_release (struct ppp *ppp)
802 struct tty_struct *tty;
805 CHECK_PPP_MAGIC(ppp);
809 if (ppp->flags & SC_DEBUG)
810 printk(KERN_DEBUG "%s released\n", ppp->name);
812 ppp_ccp_closed (ppp);
814 /* Ensure that the pppd process is not hanging on select()/poll() */
815 wake_up_interruptible (&ppp->read_wait);
816 wake_up_interruptible (&ppp->write_wait);
818 if (tty != NULL && tty->disc_data == ppp)
819 tty->disc_data = NULL; /* Break the tty->ppp link */
821 ppp_free_buf (ppp->rbuf);
822 ppp_free_buf (ppp->wbuf);
823 ppp_free_buf (ppp->cbuf);
824 ppp_free_buf (ppp->ubuf);
825 ppp_free_buf (ppp->tbuf);
837 slhc_free (ppp->slcomp);
843 ppp->backup_tty = NULL;
849 * Called when the line discipline is changed to something
850 * else, the tty is closed, or the tty detects a hangup.
854 ppp_tty_close (struct tty_struct *tty)
856 struct ppp *ppp = tty2ppp (tty);
859 if (ppp->magic != PPP_MAGIC) {
860 if (ppp->flags & SC_DEBUG)
862 "ppp: trying to close unopened tty!\n");
866 tty->disc_data = NULL;
867 if (tty == ppp->backup_tty)
871 if (ppp->backup_tty) {
872 ppp->tty = ppp->backup_tty;
875 if (ppp->flags & SC_DEBUG)
876 printk (KERN_INFO "ppp: channel %s closing.\n",
887 * Called when the tty discipline is switched to PPP.
891 ppp_tty_open (struct tty_struct *tty)
893 struct ppp *ppp = tty2ppp (tty);
896 * There should not be an existing table for this slot.
900 "ppp_tty_open: gack! tty already associated to %s!\n",
901 ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
906 * Allocate the structure from the system
908 ppp = ppp_find(current->pid);
911 * If we are taking over a ppp unit which is currently
912 * connected to a loopback pty, there's not much to do.
915 tty->disc_data = ppp;
921 printk (KERN_ERR "ppp_alloc failed\n");
925 * Initialize the control block
927 ppp_init_ctrl_blk (ppp);
928 tty->disc_data = ppp;
931 * Allocate space for the default VJ header compression slots
933 ppp->slcomp = slhc_init (16, 16);
934 if (ppp->slcomp == NULL) {
935 printk (KERN_ERR "ppp_tty_open: "
936 "no space for compression buffers!\n");
941 * Allocate space for the MTU and MRU buffers
943 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
948 * Allocate space for a user level buffer
950 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
951 if (ppp->ubuf == NULL) {
952 printk (KERN_ERR "ppp_tty_open: "
953 "no space for user receive buffer\n");
958 if (ppp->flags & SC_DEBUG)
959 printk (KERN_INFO "ppp: channel %s open\n",
962 for (indx = 0; indx < NUM_NP; ++indx)
963 ppp->sc_npmode[indx] = NPMODE_PASS;
968 * Flush any pending characters in the driver and discipline.
970 if (tty->ldisc.flush_buffer)
971 tty->ldisc.flush_buffer (tty);
973 if (tty->driver.flush_buffer)
974 tty->driver.flush_buffer (tty);
979 * Local function to send the next portion of the buffer.
981 * Called by the tty driver's tty_wakeup function should it be entered
982 * because the partial buffer was transmitted.
984 * Called by kick_tty to send the initial portion of the buffer.
986 * Completion processing of the buffer transmission is handled here.
990 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
991 struct ppp_buffer *xbuf)
993 register int count, actual;
997 CHECK_BUF_MAGIC(xbuf);
999 * Prevent re-entrancy by ensuring that this routine is called only once.
1003 if (ppp->flags & SC_XMIT_BUSY) {
1004 restore_flags(flags);
1007 ppp->flags |= SC_XMIT_BUSY;
1008 restore_flags(flags);
1010 * Send the next block of data to the modem
1012 count = xbuf->count - xbuf->tail;
1013 actual = tty->driver.write (tty, 0,
1014 buf_base (xbuf) + xbuf->tail, count);
1016 * Terminate transmission of any block which may have an error.
1017 * This could occur should the carrier drop.
1020 ppp->stats.ppp_oerrors++;
1023 ppp->bytes_sent += actual;
1025 * If the buffer has been transmitted then clear the indicators.
1027 xbuf->tail += actual;
1028 if (actual == count) {
1030 ppp->flags &= ~SC_XMIT_BUSY;
1032 * Complete the transmission on the current buffer.
1036 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1040 * If the completed buffer came from the device write, then complete the
1041 * transmission block.
1043 ppp2dev (ppp)->tbusy = 0;
1046 * Wake up the transmission queue for all completion events.
1048 wake_up_interruptible (&ppp->write_wait);
1050 * Look at the priorities. Choose a daemon write over the device driver.
1061 * If there is a pending buffer then transmit it now.
1064 ppp->flags &= ~SC_XMIT_BUSY;
1065 ppp_kick_tty (ppp, xbuf);
1066 restore_flags(flags);
1069 restore_flags(flags);
1073 * Clear the re-entry flag
1075 save_flags(flags); /* &=~ may not be atomic */
1077 ppp->flags &= ~SC_XMIT_BUSY;
1078 restore_flags(flags);
1082 * This function is called by the tty driver when the transmit buffer has
1083 * additional space. It is used by the ppp code to continue to transmit
1084 * the current buffer should the buffer have been partially sent.
1086 * In addition, it is used to send the first part of the buffer since the
1087 * logic and the inter-locking would be identical.
1091 ppp_tty_wakeup (struct tty_struct *tty)
1093 struct ppp_buffer *xbuf;
1094 struct ppp *ppp = tty2ppp (tty);
1100 if (tty != ppp->tty) {
1101 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1105 * Ensure that there is a transmission pending. Clear the re-entry flag if
1106 * there is no pending buffer. Otherwise, send the buffer.
1110 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1112 ppp_tty_wakeup_code (ppp, tty, xbuf);
1116 * This function is called to transmit a buffer to the remote. The buffer
1117 * is placed on the pending queue if there is presently a buffer being
1118 * sent or it is transmitted with the aid of ppp_tty_wakeup.
1122 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
1124 unsigned long flags;
1127 CHECK_BUF_MAGIC(xbuf);
1134 * Control the flags which are best performed with the interrupts masked.
1139 * If the transmitter is busy then place the buffer on the appropriate
1142 if (ppp->xbuf != NULL) {
1143 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1147 restore_flags (flags);
1151 * If the transmitter is not busy then this is the highest priority frame
1153 ppp->flags &= ~SC_XMIT_BUSY;
1154 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1156 restore_flags (flags);
1158 * Do the "tty wakeup_code" to actually send this buffer.
1160 ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1163 /*************************************************************
1165 * The following functions handle input that arrives from
1166 * the TTY. It recognizes PPP frames and either hands them
1167 * to the network layer or queues them for delivery to a
1168 * user process reading this TTY.
1169 *************************************************************/
1172 * Callback function from tty driver. Return the amount of space left
1173 * in the receiver's buffer to decide if remote transmitter is to be
1178 ppp_tty_room (struct tty_struct *tty)
1180 return 65536; /* We can handle an infinite amount of data. :-) */
1184 * Callback function when data is available at the tty driver.
1187 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1188 char *flags, int count)
1190 register struct ppp *ppp = tty2ppp (tty);
1191 register struct ppp_buffer *buf = NULL;
1197 * This can happen if stuff comes in on the backup tty.
1199 if (ppp == 0 || tty != ppp->tty)
1202 * Fetch the pointer to the buffer. Be careful about race conditions.
1208 * Verify the table pointer and ensure that the line is
1209 * still in PPP discipline.
1211 if (ppp->magic != PPP_MAGIC) {
1212 if (ppp->flags & SC_DEBUG)
1214 "PPP: tty_receive called but couldn't find "
1220 * Print the buffer if desired
1222 if (ppp->flags & SC_LOG_RAWIN)
1223 ppp_print_buffer ("receive buffer", data, count);
1226 * Collect the character and error condition for the character. Set the toss
1227 * flag for the first character error.
1229 while (count-- > 0) {
1233 if (*flags && ppp->toss == 0) {
1235 switch (ppp->toss) {
1237 ++ppp->estats.rx_fifo_errors;
1241 ++ppp->estats.rx_frame_errors;
1249 * Set the flags for d7 being 0/1 and parity being even/odd so that
1250 * the normal processing would have all flags set at the end of the
1251 * session. A missing flag bit indicates an error condition.
1254 #ifdef CHECK_CHARACTERS
1256 ppp->flags |= SC_RCV_B7_1;
1258 ppp->flags |= SC_RCV_B7_0;
1260 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1261 ppp->flags |= SC_RCV_ODDP;
1263 ppp->flags |= SC_RCV_EVNP;
1266 * Branch on the character.
1270 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1271 * then the block is to be ignored. In addition, characters before the very
1272 * first FLAG are also tossed by this procedure.
1274 case PPP_FLAG: /* PPP_FLAG: end of frame */
1275 ppp->stats.ppp_ibytes += ppp->rbuf->count;
1279 * Process frames which are not to be ignored. If the processing failed,
1280 * then clean up the VJ tables.
1282 if (ppp_doframe (ppp) == 0) {
1283 ++ppp->stats.ppp_ierrors;
1284 slhc_toss (ppp->slcomp);
1287 * Reset all indicators for the new frame to follow.
1290 buf->fcs = PPP_INITFCS;
1295 * All other characters in the data come here. If the character is in the
1296 * receive mask then ignore the character.
1299 /* If we're tossing, look no further. */
1303 /* If this is a control char to be ignored, do so */
1304 if (in_rmap (ppp, chr))
1308 * Modify the next character if preceded by escape.
1309 * The escape character (0x7d) could be an escaped
1310 * 0x5d, if it follows an escape :-)
1315 } else if (chr == PPP_ESCAPE) {
1316 ppp->escape = PPP_TRANS;
1321 * Decompress A/C and protocol compression here.
1323 if (buf->count == 0 && chr != PPP_ALLSTATIONS) {
1324 buf_base(buf)[0] = PPP_ALLSTATIONS;
1325 buf_base(buf)[1] = PPP_UI;
1328 if (buf->count == 2 && (chr & 1) != 0) {
1329 buf_base(buf)[2] = 0;
1333 * If the count sent is within reason then store the character, bump the
1334 * count, and update the FCS for the character.
1336 if (buf->count < buf->size) {
1337 buf_base (buf)[buf->count++] = chr;
1338 buf->fcs = PPP_FCS (buf->fcs, chr);
1342 * The peer sent too much data. Set the flags to discard the current frame
1343 * and wait for the re-synchronization FLAG to be sent.
1345 ++ppp->estats.rx_length_errors;
1352 /* on entry, a received frame is in ppp->rbuf.bufr
1353 check it and dispose as appropriate */
1356 ppp_doframe (struct ppp *ppp)
1358 __u8 *data = buf_base (ppp->rbuf);
1359 int count = ppp->rbuf->count;
1365 CHECK_BUF_MAGIC(ppp->rbuf);
1368 * If there is a pending error from the receiver then log it and discard
1369 * the damaged frame.
1372 if ((ppp->flags & SC_DEBUG) && count > 0)
1374 "ppp_toss: tossing frame, reason = %x\n",
1379 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1380 * follows each frame.
1385 * Generate an error if the frame is too small.
1387 if (count < PPP_HDRLEN + 2) {
1388 if (ppp->flags & SC_DEBUG)
1390 "ppp: got runt ppp frame, %d chars\n", count);
1391 ++ppp->estats.rx_length_errors;
1395 * Verify the CRC of the frame and discard the CRC characters from the
1396 * end of the buffer.
1398 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1399 if (ppp->flags & SC_DEBUG) {
1401 "ppp: frame with bad fcs, length = %d\n",
1403 ppp_print_buffer("bad frame", data, count);
1405 ++ppp->estats.rx_crc_errors;
1408 count -= 2; /* ignore the fcs characters */
1410 * Obtain the protocol from the frame
1412 proto = PPP_PROTOCOL(data);
1414 * Process the active decompressor.
1416 if ((ppp->sc_rc_state != (void *) 0) &&
1417 (ppp->flags & SC_DECOMP_RUN) &&
1418 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1419 if (proto == PPP_COMP) {
1421 * If the frame is compressed then decompress it.
1423 new_data = kmalloc (ppp->mru + PPP_HDRLEN, GFP_ATOMIC);
1424 if (new_data == NULL) {
1425 printk (KERN_ERR "ppp_doframe: no memory\n");
1426 new_count = DECOMP_ERROR;
1428 new_count = (*ppp->sc_rcomp->decompress)
1429 (ppp->sc_rc_state, data, count,
1430 new_data, ppp->mru + PPP_HDRLEN);
1432 switch (new_count) {
1434 ppp_doframe_lower (ppp, new_data, new_count);
1439 ppp->flags |= SC_DC_ERROR;
1442 case DECOMP_FATALERROR:
1443 ppp->flags |= SC_DC_FERROR;
1444 printk(KERN_ERR "ppp: fatal decomp error\n");
1448 * Log the error condition and discard the frame.
1452 slhc_toss (ppp->slcomp);
1453 ++ppp->stats.ppp_ierrors;
1456 * The frame is not special. Pass it through the compressor without
1457 * actually compressing the data
1459 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1464 * Process the uncompressed frame.
1466 ppp_doframe_lower (ppp, data, count);
1470 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
1472 __u16 proto = PPP_PROTOCOL (data);
1473 ppp_proto_type *proto_ptr;
1477 * Ignore empty frames
1479 if (count <= PPP_HDRLEN)
1482 * Count the frame and print it
1484 ++ppp->stats.ppp_ipackets;
1485 if (ppp->flags & SC_LOG_INPKT)
1486 ppp_print_buffer ("receive frame", data, count);
1488 * Find the procedure to handle this protocol. The last one is marked
1489 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1491 proto_ptr = proto_list;
1492 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1495 * Update the appropriate statistic counter.
1497 if ((*proto_ptr->func) (ppp, proto,
1499 count - PPP_HDRLEN))
1500 ppp->stats.ppp_ioctects += count;
1502 ++ppp->stats.ppp_discards;
1506 * Put the input frame into the networking system for the indicated protocol
1510 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1512 sk_buff *skb = dev_alloc_skb (count);
1514 * Generate a skb buffer for the new frame.
1517 if (ppp->flags & SC_DEBUG)
1519 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1520 ppp2dev (ppp)->name);
1524 * Move the received data from the input buffer to the skb buffer.
1526 skb->dev = ppp2dev (ppp); /* We are the device */
1527 skb->protocol = proto;
1528 skb->mac.raw = skb_data(skb);
1529 memcpy (skb_put(skb,count), data, count); /* move data */
1531 * Tag the frame and kick it to the proper receive routine
1533 #if LINUX_VERSION_CODE < VERSION(2,1,15)
1537 ppp->last_recv = jiffies;
1543 * Process the receipt of an IP frame
1547 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1550 if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1551 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1552 return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1557 * Process the receipt of an IPX frame
1561 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1564 if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1565 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1570 * Process the receipt of an VJ Compressed frame
1574 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
1575 __u8 *data, int count)
1578 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1579 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1580 if (new_count >= 0) {
1581 return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1583 if (ppp->flags & SC_DEBUG)
1585 "ppp: error in VJ decompression\n");
1591 * Process the receipt of an VJ Un-compressed frame
1595 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
1596 __u8 *data, int count)
1599 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1600 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1601 return rcv_proto_ip (ppp, PPP_IP, data, count);
1603 if (ppp->flags & SC_DEBUG)
1605 "ppp: error in VJ memorizing\n");
1611 * Receive all unclassified protocols.
1615 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
1616 __u8 *data, int len)
1619 register int current_idx;
1623 buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1624 current_idx &= ppp->ubuf->size; \
1625 if (current_idx == ppp->ubuf->tail) \
1631 * The total length includes the protocol data.
1632 * Lock the user information buffer.
1634 if (test_and_set_bit (0, &ppp->ubuf->locked)) {
1635 if (ppp->flags & SC_DEBUG)
1637 "ppp: rcv_proto_unknown: can't get lock\n");
1639 CHECK_BUF_MAGIC(ppp->ubuf);
1640 current_idx = ppp->ubuf->head;
1642 * Insert the buffer length (not counted), the protocol, and the data
1652 while (totlen-- > 0) {
1657 * The frame is complete. Update the head pointer and wakeup the pppd
1660 ppp->ubuf->head = current_idx;
1662 clear_bit (0, &ppp->ubuf->locked);
1663 wake_up_interruptible (&ppp->read_wait);
1664 if (ppp->tty->fasync != NULL)
1665 kill_fasync (ppp->tty->fasync, SIGIO);
1669 * The buffer is full. Unlock the header
1672 clear_bit (0, &ppp->ubuf->locked);
1673 if (ppp->flags & SC_DEBUG)
1675 "ppp: rcv_proto_unknown: buffer overflow\n");
1678 * Discard the frame. There are no takers for this protocol.
1680 if (ppp->flags & SC_DEBUG)
1682 "ppp: rcv_proto_unknown: dropping packet\n");
1687 * Handle a CCP packet.
1689 * The CCP packet is passed along to the pppd process just like any
1690 * other PPP frame. The difference is that some processing needs to be
1691 * immediate or the compressors will become confused on the peer.
1694 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
1696 int slen = CCP_LENGTH(dp);
1697 __u8 *opt = dp + CCP_HDRLEN;
1698 int opt_len = slen - CCP_HDRLEN;
1699 unsigned long flags;
1705 switch (CCP_CODE(dp)) {
1710 * CCP must be going down - disable compression
1712 if (ppp->flags & SC_CCP_UP) {
1714 ppp->flags &= ~(SC_CCP_UP |
1721 if ((ppp->flags & SC_CCP_OPEN) == 0)
1723 if (ppp->flags & SC_CCP_UP)
1725 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1727 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1730 * we're agreeing to send compressed packets.
1733 if (ppp->sc_xc_state == NULL)
1736 if ((*ppp->sc_xcomp->comp_init)
1740 ppp2dev (ppp)->base_addr,
1742 ppp->flags & SC_DEBUG)) {
1743 if (ppp->flags & SC_DEBUG)
1744 printk(KERN_DEBUG "%s: comp running\n",
1747 ppp->flags |= SC_COMP_RUN;
1752 * peer is agreeing to send compressed packets.
1754 if (ppp->sc_rc_state == NULL)
1757 if ((*ppp->sc_rcomp->decomp_init)
1761 ppp2dev (ppp)->base_addr,
1764 ppp->flags & SC_DEBUG)) {
1765 if (ppp->flags & SC_DEBUG)
1766 printk(KERN_DEBUG "%s: decomp running\n",
1769 ppp->flags |= SC_DECOMP_RUN;
1770 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1774 * CCP Reset-ack resets compressors and decompressors as it passes through.
1777 if ((ppp->flags & SC_CCP_UP) == 0)
1781 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN)) {
1782 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1783 if (ppp->flags & SC_DEBUG)
1784 printk(KERN_DEBUG "%s: comp reset\n",
1788 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1789 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1790 if (ppp->flags & SC_DEBUG)
1791 printk(KERN_DEBUG "%s: decomp reset\n",
1794 ppp->flags &= ~SC_DC_ERROR;
1799 restore_flags(flags);
1803 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
1806 ppp_proto_ccp (ppp, dp, len, 1);
1807 return rcv_proto_unknown (ppp, proto, dp, len);
1811 * Handle a LQR packet.
1815 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
1817 return rcv_proto_unknown (ppp, proto, data, len);
1820 /*************************************************************
1821 * LINE DISCIPLINE SUPPORT
1822 * The following functions form support user programs
1823 * which read and write data on a TTY with the PPP line
1824 * discipline. Reading is done from a circular queue,
1825 * filled by the lower TTY levels.
1826 *************************************************************/
1828 /* read a PPP frame from the us_rbuff circular buffer,
1829 waiting if necessary
1833 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
1836 struct ppp *ppp = tty2ppp (tty);
1843 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1844 ppp->ubuf->tail &= ppp->ubuf->size; \
1848 * Validate the pointers
1853 /* if (ppp->magic != PPP_MAGIC)
1859 * Before we attempt to write the frame to the user, ensure that the
1860 * user has access to the pages for the total buffer length.
1862 error = verify_area (VERIFY_WRITE, buf, nr);
1867 * Acquire the read lock.
1870 ppp = tty2ppp (tty);
1871 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
1875 if (test_and_set_bit (0, &ppp->ubuf->locked) != 0) {
1877 if (ppp->flags & SC_DEBUG)
1879 "ppp_tty_read: sleeping(ubuf)\n");
1881 current->timeout = 0;
1882 current->state = TASK_INTERRUPTIBLE;
1885 if (signal_pending(current))
1891 * Fetch the length of the buffer from the first two bytes.
1893 if (ppp->ubuf->head == ppp->ubuf->tail)
1905 * If there is no length then wait for the data to arrive.
1908 clear_bit (0, &ppp->ubuf->locked);
1909 if (file->f_flags & O_NONBLOCK)
1911 current->timeout = 0;
1913 if (ppp->flags & SC_DEBUG)
1915 "ppp_tty_read: sleeping(read_wait)\n");
1917 interruptible_sleep_on (&ppp->read_wait);
1918 if (signal_pending(current))
1923 * Ensure that the frame will fit within the caller's buffer. If not, then
1924 * discard the frame from the input buffer.
1927 /* Can't copy it, update us_rbuff_head */
1929 if (ppp->flags & SC_DEBUG)
1931 "ppp: read of %lu bytes too small for %ld "
1932 "frame\n", (unsigned long) nr, (long) len + 2);
1933 ppp->stats.ppp_ierrors++;
1939 * Fake the insertion of the ADDRESS and CONTROL information because these
1940 * were not saved in the buffer.
1942 PUT_USER (error, (u_char) PPP_ALLSTATIONS, buf);
1946 PUT_USER (error, (u_char) PPP_UI, buf);
1952 * Copy the received data from the buffer to the caller's area.
1954 ret = len + 2; /* Account for ADDRESS and CONTROL bytes */
1957 PUT_USER (error, c, buf);
1963 clear_bit (0, &ppp->ubuf->locked);
1967 ppp->ubuf->tail += len;
1968 ppp->ubuf->tail &= ppp->ubuf->size;
1969 clear_bit (0, &ppp->ubuf->locked);
1974 /* stuff a character into the transmit buffer, using PPP's way of escaping
1976 also, update fcs to take account of new character */
1979 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1983 * The buffer should not be full.
1985 if (ppp->flags & SC_DEBUG) {
1986 if ((buf->count < 0) || (buf->count > 3000))
1987 printk (KERN_DEBUG "ppp_stuff_char: %d %x\n",
1988 (unsigned int) buf->count,
1989 (unsigned int) chr);
1992 * Update the FCS and if the character needs to be escaped, do it.
1994 buf->fcs = PPP_FCS (buf->fcs, chr);
1995 if (in_xmap (ppp, chr)) {
1997 ins_char (buf, PPP_ESCAPE);
2000 * Add the character to the buffer.
2002 ins_char (buf, chr);
2006 * Procedure to encode the data with the proper escaping and send the
2007 * data to the remote system.
2011 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
2012 __u8 *data, int count, int non_ip)
2015 int address, control;
2019 CHECK_BUF_MAGIC(buf);
2020 ++ppp->stats.ppp_opackets;
2021 ppp->stats.ppp_ooctects += count;
2024 * Insert the leading FLAG character
2028 if (non_ip || flag_time == 0)
2029 ins_char (buf, PPP_FLAG);
2031 if (jiffies - ppp->last_xmit >= flag_time)
2032 ins_char (buf, PPP_FLAG);
2034 ppp->last_xmit = jiffies;
2035 buf->fcs = PPP_INITFCS;
2037 * Emit the address/control information if needed
2039 address = PPP_ADDRESS (data);
2040 control = PPP_CONTROL (data);
2041 proto = PPP_PROTOCOL (data);
2043 if (address != PPP_ALLSTATIONS ||
2044 control != PPP_UI ||
2045 (ppp->flags & SC_COMP_AC) == 0) {
2046 ppp_stuff_char (ppp, buf, address);
2047 ppp_stuff_char (ppp, buf, control);
2050 * Emit the protocol (compressed if possible)
2052 if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
2053 ppp_stuff_char (ppp, buf, proto >> 8);
2055 ppp_stuff_char (ppp, buf, proto);
2063 ppp_stuff_char (ppp, buf, *data++);
2065 * Add the trailing CRC and the final flag character
2067 write_fcs = buf->fcs ^ 0xFFFF;
2068 ppp_stuff_char (ppp, buf, write_fcs);
2069 ppp_stuff_char (ppp, buf, write_fcs >> 8);
2071 * Add the trailing flag character
2073 ins_char (buf, PPP_FLAG);
2075 * Send the block to the tty driver.
2077 ppp->stats.ppp_obytes += buf->count;
2078 ppp_kick_tty (ppp, buf);
2082 * Compress and send an frame to the peer.
2084 * Return 0 if frame was queued for transmission.
2085 * 1 if frame must be re-queued for later driver support.
2089 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
2090 __u8 *data, int count)
2093 int address, control;
2098 CHECK_BUF_MAGIC(buf);
2102 if (ppp->flags & SC_LOG_OUTPKT)
2103 ppp_print_buffer ("write frame", data, count);
2105 * Determine if the frame may be compressed. Attempt to compress the
2106 * frame if possible.
2108 proto = PPP_PROTOCOL (data);
2109 address = PPP_ADDRESS (data);
2110 control = PPP_CONTROL (data);
2112 if (((ppp->flags & SC_COMP_RUN) != 0) &&
2113 (ppp->sc_xc_state != (void *) 0) &&
2114 (address == PPP_ALLSTATIONS) &&
2115 (control == PPP_UI) &&
2116 (proto != PPP_LCP) &&
2117 (proto != PPP_CCP)) {
2118 new_data = kmalloc (ppp->mtu + PPP_HDRLEN, GFP_ATOMIC);
2119 if (new_data == NULL) {
2120 printk (KERN_ERR "ppp_dev_xmit_frame: no memory\n");
2124 new_count = (*ppp->sc_xcomp->compress)
2125 (ppp->sc_xc_state, data, new_data, count,
2126 ppp->mtu + PPP_HDRLEN);
2128 if (new_count > 0 && (ppp->flags & SC_CCP_UP)) {
2129 ppp_dev_xmit_lower (ppp, buf, new_data, new_count, 0);
2134 * The frame could not be compressed, or it could not be sent in
2135 * compressed form because CCP is not yet up.
2140 * Go to the escape encoding
2142 ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2147 * Revise the tty frame for specific protocols.
2151 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
2155 switch (PPP_PROTOCOL (data)) {
2157 * Update the LQR frame with the current MIB information. This saves having
2158 * the daemon read old MIB data from the driver.
2161 len = 48; /* total size of this frame */
2162 p = (__u8 *) &data [40]; /* Point to last two items. */
2163 p = store_long (p, ppp->stats.ppp_opackets + 1);
2164 p = store_long (p, ppp->stats.ppp_ooctects + len);
2167 * Outbound compression frames
2184 * write a frame with NR chars from BUF to TTY
2185 * we have to put the FCS field on ourselves
2189 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
2192 struct ppp *ppp = tty2ppp (tty);
2195 struct wait_queue wait = {current, NULL};
2198 * Verify the pointers.
2203 if (ppp->magic != PPP_MAGIC)
2208 * Ensure that the caller does not wish to send too much.
2210 if (count > PPP_MTU + PPP_HDRLEN) {
2211 if (ppp->flags & SC_DEBUG)
2212 printk (KERN_WARNING
2213 "ppp_tty_write: truncating user packet "
2214 "from %lu to mtu %d\n", (unsigned long) count,
2215 PPP_MTU + PPP_HDRLEN);
2216 count = PPP_MTU + PPP_HDRLEN;
2219 * Allocate a buffer for the data and fetch it from the user space.
2221 new_data = kmalloc (count, GFP_KERNEL);
2222 if (new_data == NULL) {
2223 printk (KERN_ERR "ppp_tty_write: no memory\n");
2227 * Retrieve the user's buffer
2229 COPY_FROM_USER (error, new_data, data, count);
2233 * Lock this PPP unit so we will be the only writer,
2234 * sleeping if necessary.
2236 * Note that we add our task to the wait queue before
2237 * attempting to lock, as the lock flag may be cleared
2238 * from an interrupt.
2240 add_wait_queue(&ppp->write_wait, &wait);
2243 current->timeout = 0;
2244 current->state = TASK_INTERRUPTIBLE;
2245 if (lock_buffer(ppp->tbuf) == 0)
2250 ppp = tty2ppp (tty);
2251 if (!ppp || ppp->magic != PPP_MAGIC ||
2252 !ppp->inuse || tty != ppp->tty) {
2253 printk("ppp_tty_write: %p invalid after wait!\n", ppp);
2257 if (signal_pending(current))
2260 current->state = TASK_RUNNING;
2261 remove_wait_queue(&ppp->write_wait, &wait);
2266 * Change the LQR frame
2268 count = send_revise_frame (ppp, new_data, count);
2272 if (PPP_PROTOCOL(new_data) == PPP_IP) {
2274 * IP frames can be sent by pppd when we're doing
2275 * demand-dialling. We send them via ppp_dev_xmit_ip
2276 * to make sure that VJ compression happens properly.
2278 ppp_dev_xmit_ip(ppp, ppp->tbuf, new_data + PPP_HDRLEN,
2279 count - PPP_HDRLEN, NPMODE_PASS);
2282 ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2292 * Process the BSD compression IOCTL event for the tty device.
2296 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2298 struct compressor *cp;
2300 unsigned long flags;
2302 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2303 struct ppp_option_data data;
2306 * Fetch the compression parameters
2308 COPY_FROM_USER (error, &data, odp, sizeof (data));
2314 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2315 nb = CCP_MAX_OPTION_LENGTH;
2317 COPY_FROM_USER (error, ccp_option, ptr, nb);
2321 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2326 ppp->flags &= ~(SC_COMP_RUN | SC_DECOMP_RUN);
2327 restore_flags(flags);
2329 cp = find_compressor (ccp_option[0]);
2330 #ifdef CONFIG_KERNELD
2333 sprintf(modname, "ppp-compress-%d", ccp_option[0]);
2334 request_module(modname);
2335 cp = find_compressor(ccp_option[0]);
2337 #endif /* CONFIG_KERNELD */
2342 * Found a handler for the protocol - try to allocate
2343 * a compressor or decompressor.
2346 if (data.transmit) {
2347 if (ppp->sc_xc_state != NULL)
2348 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2349 ppp->sc_xc_state = NULL;
2352 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2353 if (ppp->sc_xc_state == NULL) {
2354 printk(KERN_WARNING "%s: comp_alloc failed\n",
2357 } else if (ppp->flags & SC_DEBUG)
2358 printk(KERN_DEBUG "%s: comp_alloc -> %p\n",
2359 ppp->name, ppp->sc_xc_state);
2361 if (ppp->sc_rc_state != NULL)
2362 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2363 ppp->sc_rc_state = NULL;
2366 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2367 if (ppp->sc_rc_state == NULL) {
2368 printk(KERN_WARNING "%s: decomp_alloc failed\n",
2371 } else if (ppp->flags & SC_DEBUG)
2372 printk(KERN_DEBUG "%s: decomp_alloc -> %p\n",
2373 ppp->name, ppp->sc_rc_state);
2378 if (ppp->flags & SC_DEBUG)
2379 printk(KERN_DEBUG "%s: no compressor for [%x %x %x], %x\n",
2380 ppp->name, ccp_option[0], ccp_option[1],
2382 return (-EINVAL); /* no handler found */
2386 * Process the IOCTL event for the tty device.
2390 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
2391 unsigned int param2, unsigned long param3)
2393 struct ppp *ppp = tty2ppp (tty);
2394 register int temp_i = 0;
2397 * Verify the status of the PPP device.
2402 if (ppp->magic != PPP_MAGIC)
2407 * The user must have an euid of root to do these requests.
2416 GET_USER (error, temp_i, (int *) param3);
2419 if (ppp->flags & SC_DEBUG)
2421 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2423 if (ppp->mru != temp_i)
2424 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2430 temp_i = (ppp->flags & SC_MASK);
2431 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2432 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2433 SC_RCV_ODDP | SC_RCV_EVNP;
2435 PUT_USER (error, temp_i, (int *) param3);
2438 * Set the flags for the various options
2441 GET_USER (error, temp_i, (int *) param3);
2445 temp_i |= (ppp->flags & ~SC_MASK);
2447 if ((ppp->flags & SC_CCP_OPEN) &&
2448 (temp_i & SC_CCP_OPEN) == 0)
2449 ppp_ccp_closed (ppp);
2451 if ((ppp->flags | temp_i) & SC_DEBUG)
2453 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2454 ppp->flags = temp_i;
2457 * Set the compression mode
2459 case PPPIOCSCOMPRESS:
2460 error = ppp_set_compression (ppp,
2461 (struct ppp_option_data *) param3);
2464 * Retrieve the transmit async map
2466 case PPPIOCGASYNCMAP:
2467 PUT_USER (error, ppp->xmit_async_map[0], (int *) param3);
2470 * Set the transmit async map
2472 case PPPIOCSASYNCMAP:
2473 GET_USER (error, temp_i, (int *) param3);
2476 ppp->xmit_async_map[0] = temp_i;
2477 if (ppp->flags & SC_DEBUG)
2479 "ppp_tty_ioctl: set xmit asyncmap %x\n",
2480 ppp->xmit_async_map[0]);
2483 * Set the receive async map
2485 case PPPIOCSRASYNCMAP:
2486 GET_USER (error, temp_i, (int *) param3);
2489 ppp->recv_async_map = temp_i;
2490 if (ppp->flags & SC_DEBUG)
2492 "ppp_tty_ioctl: set rcv asyncmap %x\n",
2493 ppp->recv_async_map);
2496 * Obtain the unit number for this device.
2499 PUT_USER (error, ppp2dev (ppp)->base_addr, (int *) param3);
2502 if (ppp->flags & SC_DEBUG)
2504 "ppp_tty_ioctl: get unit: %ld\n",
2505 ppp2dev (ppp)->base_addr);
2508 * Set the debug level
2511 GET_USER (error, temp_i, (int *) param3);
2514 temp_i = (temp_i & 0x1F) << 16;
2515 temp_i |= (ppp->flags & ~0x1F0000);
2517 if ((ppp->flags | temp_i) & SC_DEBUG)
2519 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2520 ppp->flags = temp_i;
2523 * Get the debug level
2526 temp_i = (ppp->flags >> 16) & 0x1F;
2527 PUT_USER (error, temp_i, (int *) param3);
2530 * Get the times since the last send/receive frame operation
2534 struct ppp_idle cur_ddinfo;
2536 /* change absolute times to relative times. */
2537 cur_ddinfo.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
2538 cur_ddinfo.recv_idle = (jiffies - ppp->last_recv) / HZ;
2539 COPY_TO_USER (error, (void *) param3, &cur_ddinfo,
2540 sizeof (cur_ddinfo));
2544 * Retrieve the extended async map
2546 case PPPIOCGXASYNCMAP:
2547 COPY_TO_USER (error, (void *) param3, ppp->xmit_async_map,
2548 sizeof (ppp->xmit_async_map));
2551 * Set the async extended map
2553 case PPPIOCSXASYNCMAP:
2557 COPY_FROM_USER (error, temp_tbl, (void *) param3,
2562 temp_tbl[1] = 0x00000000;
2563 temp_tbl[2] &= ~0x40000000;
2564 temp_tbl[3] |= 0x60000000;
2566 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2567 (temp_tbl[4] & temp_tbl[5]) != 0 ||
2568 (temp_tbl[6] & temp_tbl[7]) != 0)
2571 memcpy (ppp->xmit_async_map,
2573 sizeof (ppp->xmit_async_map));
2575 if (ppp->flags & SC_DEBUG)
2577 "ppp_tty_ioctl: set xasyncmap\n");
2582 * Set the maximum VJ header compression slot number.
2585 GET_USER (error, temp_i, (int *) param3);
2588 temp_i = (temp_i & 255) + 1;
2589 if (ppp->flags & SC_DEBUG)
2591 "ppp_tty_ioctl: set maxcid to %d\n", temp_i);
2592 if (ppp->slcomp != NULL)
2593 slhc_free (ppp->slcomp);
2596 ppp->slcomp = slhc_init (16, temp_i);
2597 if (ppp->slcomp == NULL) {
2598 printk (KERN_ERR "ppp_tty_ioctl: "
2599 "no space for compression buffers!\n");
2605 case PPPIOCXFERUNIT:
2606 ppp->backup_tty = tty;
2607 ppp->sc_xfer = current->pid;
2615 COPY_FROM_USER (error, &npi, (void *) param3,
2620 switch (npi.protocol) {
2622 npi.protocol = NP_IP;
2625 if (ppp->flags & SC_DEBUG)
2626 printk(KERN_DEBUG "pppioc[gs]npmode: "
2627 "invalid protocol %d\n",
2635 if (param2 == PPPIOCGNPMODE) {
2636 npi.mode = ppp->sc_npmode[npi.protocol];
2638 COPY_TO_USER (error, (void *) param3, &npi,
2643 ppp->sc_npmode[npi.protocol] = npi.mode;
2644 if (ppp->flags & SC_DEBUG)
2645 printk(KERN_DEBUG "ppp: set np %d to %d\n",
2646 npi.protocol, npi.mode);
2647 ppp2dev(ppp)->tbusy = 0;
2652 * Allow users to read, but not set, the serial port parameters
2656 error = n_tty_ioctl (tty, file, param2, param3);
2661 int count = ppp->ubuf->tail - ppp->ubuf->head;
2663 count += (ppp->ubuf->size + 1);
2664 PUT_USER (error, count, (int *) param3);
2668 * All other ioctl() events will come here.
2671 if (ppp->flags & SC_DEBUG)
2672 printk (KERN_WARNING "ppp_tty_ioctl: "
2673 "invalid ioctl=%x, addr=%lx\n", param2, param3);
2674 error = -ENOIOCTLCMD;
2683 * Process the select() (or poll()) statement for the PPP device.
2686 #if LINUX_VERSION_CODE < VERSION(2,1,23)
2688 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2689 struct file *filp, int sel_type, select_table * wait)
2691 struct ppp *ppp = tty2ppp (tty);
2694 * Verify the status of the PPP device.
2699 if (ppp->magic != PPP_MAGIC || tty != ppp->tty)
2704 * Branch on the type of select mode. A read request must lock the user
2709 if (test_and_set_bit (0, &ppp->ubuf->locked) == 0) {
2710 /* Test for the presence of data in the queue */
2711 if (ppp->ubuf->head != ppp->ubuf->tail) {
2712 clear_bit (0, &ppp->ubuf->locked);
2715 clear_bit (0, &ppp->ubuf->locked);
2716 } /* fall through */
2718 * Exceptions or read errors.
2721 /* Is this a pty link and the remote disconnected? */
2722 if (tty->flags & (1 << TTY_OTHER_CLOSED))
2725 /* Is this a local link and the modem disconnected? */
2726 if (tty_hung_up_p (filp))
2729 select_wait (&ppp->read_wait, wait);
2733 * Write mode. A write is allowed if there is no current transmission.
2736 if (ppp->tbuf->locked != 0) {
2737 select_wait (&ppp->write_wait, wait);
2745 #else /* 2.1.23 or later */
2748 ppp_tty_poll (struct tty_struct *tty, struct file *filp, poll_table * wait)
2750 struct ppp *ppp = tty2ppp (tty);
2751 unsigned int mask = 0;
2753 if (ppp && ppp->magic == PPP_MAGIC && tty == ppp->tty) {
2756 #if LINUX_VERSION_CODE < VERSION(2,1,89)
2757 poll_wait(&ppp->read_wait, wait);
2758 poll_wait(&ppp->write_wait, wait);
2760 poll_wait(filp, &ppp->read_wait, wait);
2761 poll_wait(filp, &ppp->write_wait, wait);
2764 /* Must lock the user buffer area while checking. */
2765 CHECK_BUF_MAGIC(ppp->ubuf);
2766 if(test_and_set_bit(0, &ppp->ubuf->locked) == 0) {
2767 if(ppp->ubuf->head != ppp->ubuf->tail)
2768 mask |= POLLIN | POLLRDNORM;
2769 clear_bit(0, &ppp->ubuf->locked);
2771 if(tty->flags & (1 << TTY_OTHER_CLOSED))
2773 if(tty_hung_up_p(filp))
2775 if(ppp->tbuf->locked == 0)
2776 mask |= POLLOUT | POLLWRNORM;
2783 /*************************************************************
2785 * This routine accepts requests from the network layer
2786 * and attempts to deliver the packets.
2787 * It also includes various routines we are compelled to
2788 * have to make the network layer work (arp, etc...).
2789 *************************************************************/
2792 * Callback from the network layer when the device goes up.
2796 ppp_dev_open (struct device *dev)
2798 struct ppp *ppp = dev2ppp (dev);
2800 if (ppp2tty (ppp) == NULL) {
2802 "ppp: %s not connected to a TTY! can't go open!\n",
2807 if (ppp->flags & SC_DEBUG)
2809 "ppp: channel %s going up for IP packets!\n",
2817 * Callback from the network layer when the ppp device goes down.
2821 ppp_dev_close (struct device *dev)
2823 struct ppp *ppp = dev2ppp (dev);
2825 if (ppp2tty (ppp) == NULL) {
2829 * We don't do anything about the device going down. It is not important
2832 if (ppp->flags & SC_DEBUG)
2834 "ppp: channel %s going down for IP packets!\n",
2841 * IOCTL operation to read the version of the driver.
2845 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2848 char *result = (char *) ifr->ifr_ifru.ifru_data;
2849 int len = strlen (szVersion) + 1;
2851 * Move the version data
2853 COPY_TO_USER (error, result, szVersion, len);
2859 * IOCTL to read the statistics for the pppstats program.
2863 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2865 struct ppp_stats *result, temp;
2868 * Supply the information for the caller. First move the version data
2869 * then move the ppp stats; and finally the vj stats.
2871 memset (&temp, 0, sizeof(temp));
2872 if (dev->flags & IFF_UP) {
2873 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2874 if (ppp->slcomp != NULL) {
2875 temp.vj.vjs_packets = ppp->slcomp->sls_o_compressed+
2876 ppp->slcomp->sls_o_uncompressed;
2877 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2878 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
2879 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
2880 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
2881 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
2882 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2883 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
2887 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2889 COPY_TO_USER (error, result, &temp, sizeof (temp));
2895 * IOCTL to read the compression statistics for the pppstats program.
2899 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2901 struct ppp_comp_stats *result, temp;
2904 * Supply the information for the caller.
2906 memset (&temp, 0, sizeof(temp));
2907 if (dev->flags & IFF_UP) {
2908 if (ppp->sc_xc_state != NULL)
2909 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2912 if (ppp->sc_rc_state != NULL)
2913 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2917 * Move the data to the caller's buffer
2919 result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2921 COPY_TO_USER (error, result, &temp, sizeof (temp));
2927 * Callback from the network layer to process the sockioctl functions.
2931 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2933 struct ppp *ppp = dev2ppp (dev);
2936 CHECK_PPP_MAGIC(ppp);
2938 * Process the requests
2942 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2945 case SIOCGPPPCSTATS:
2946 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2950 error = ppp_dev_ioctl_version (ppp, ifr);
2961 * Send an IP frame to the remote with vj header compression.
2963 * Return 0 if frame was queued for transmission.
2964 * 1 if frame must be re-queued for later driver support.
2965 * -1 if frame should be dropped.
2969 ppp_dev_xmit_ip (struct ppp *ppp, struct ppp_buffer *buf,
2970 __u8 *data, int len, enum NPmode npmode)
2975 * Branch on the type of processing for the IP frame.
2983 * We may not send the packet now, so drop it.
2984 * XXX It would be nice to be able to return it to the
2985 * network system to be queued and retransmitted later.
2987 if (ppp->flags & SC_DEBUG)
2988 printk(KERN_DEBUG "%s: returning frame\n",
2994 if (ppp->flags & SC_DEBUG)
2996 "ppp_dev_xmit: npmode = %d on %s\n",
2997 ppp->sc_npmode[NP_IP], ppp->name);
3001 if (ppp->flags & SC_DEBUG)
3002 printk (KERN_WARNING
3003 "ppp_dev_xmit: unknown npmode %d on %s\n",
3004 ppp->sc_npmode[NP_IP], ppp->name);
3008 * At this point, the buffer will be transmitted. There is no other exit.
3010 * Try to compress the header.
3012 if (ppp->flags & SC_COMP_TCP) {
3013 len = slhc_compress (ppp->slcomp, data, len,
3014 buf_base (ppp->cbuf) + PPP_HDRLEN,
3016 (ppp->flags & SC_NO_TCP_CCID) == 0);
3018 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
3019 proto = PPP_VJC_COMP;
3020 data[0] ^= SL_TYPE_COMPRESSED_TCP;
3022 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
3023 proto = PPP_VJC_UNCOMP;
3024 data[0] = (data[0] & 0x0f) | 0x40;
3031 hdr = data - PPP_HDRLEN;
3033 hdr[0] = PPP_ALLSTATIONS;
3038 return ppp_dev_xmit_frame (ppp, buf, hdr, len);
3042 * Send a non-IP data frame (such as an IPX frame) to the remote.
3044 * Return 0 if frame was queued for transmission.
3045 * 1 if frame must be re-queued for later driver support.
3048 ppp_dev_xmit_other (struct device *dev, struct ppp *ppp,
3049 __u8 *data, int len, int proto)
3056 hdr = data - PPP_HDRLEN;
3058 hdr[0] = PPP_ALLSTATIONS;
3060 hdr[2] = proto >> 8;
3063 return ppp_dev_xmit_frame (ppp, ppp->wbuf, hdr, len);
3067 * Send a frame to the remote.
3069 #if LINUX_VERSION_CODE < VERSION(2,1,86)
3070 #define FREE_SKB(skb) dev_kfree_skb(skb)
3072 #define FREE_SKB(skb) dev_kfree_skb(skb, FREE_WRITE)
3076 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3080 struct ppp *ppp = dev2ppp (dev);
3081 struct tty_struct *tty = ppp2tty (ppp);
3083 * just a little sanity check.
3086 if (ppp->flags & SC_DEBUG)
3087 printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3091 * Avoid timing problem should tty hangup while data is queued to be sent
3098 * Validate the tty interface
3101 if (ppp->flags & SC_DEBUG)
3103 "ppp_dev_xmit: %s not connected to a TTY!\n",
3109 * Fetch the pointer to the data
3112 data = skb_data(skb);
3114 if (data == (__u8 *) 0) {
3115 if (ppp->flags & SC_DEBUG)
3116 printk (KERN_CRIT "ppp_dev_xmit: %s Null skb data\n",
3122 * Detect a change in the transfer size
3124 if (ppp->mtu != ppp2dev (ppp)->mtu) {
3125 ppp_changedmtu (ppp,
3130 * Acquire the lock on the transmission buffer. If the buffer was busy then
3131 * mark the device as busy.
3132 * We also require that ppp->tbuf be unlocked, in order to serialize
3133 * calls to ppp_dev_xmit_frame (which does compression) and the output
3134 * of frames w.r.t. tty writes from pppd.
3136 CHECK_BUF_MAGIC(ppp->wbuf);
3137 if (ppp->tbuf->locked || lock_buffer (ppp->wbuf) != 0) {
3139 if (ppp->flags & SC_DEBUG)
3140 printk(KERN_DEBUG "dev_xmit blocked, t=%lu w=%lu\n",
3141 ppp->tbuf->locked, ppp->wbuf->locked);
3145 * Look at the protocol in the skb to determine the difference between
3146 * an IP frame and an IPX frame.
3148 switch (ntohs (skb->protocol)) {
3150 answer = ppp_dev_xmit_other (dev, ppp, data, len, PPP_IPX);
3154 answer = ppp_dev_xmit_ip (ppp, ppp->wbuf, data, len,
3155 ppp->sc_npmode[NP_IP]);
3158 default: /* All others have no support at this time. */
3163 * This is the end of the transmission. Release the buffer if it was sent.
3166 /* packet queued OK */
3169 ppp->wbuf->locked = 0;
3171 /* packet should be dropped */
3175 /* packet should be queued for later */
3183 * Generate the statistic information for the /proc/net/dev listing.
3186 static struct net_device_stats *
3187 ppp_dev_stats (struct device *dev)
3189 struct ppp *ppp = dev2ppp (dev);
3191 ppp->estats.rx_packets = ppp->stats.ppp_ipackets;
3192 ppp->estats.rx_errors = ppp->stats.ppp_ierrors;
3193 ppp->estats.tx_packets = ppp->stats.ppp_opackets;
3194 ppp->estats.tx_errors = ppp->stats.ppp_oerrors;
3195 #if LINUX_VERSION_CODE >= VERSION(2,1,25)
3196 ppp->estats.rx_bytes = ppp->stats.ppp_ibytes;
3197 ppp->estats.tx_bytes = ppp->stats.ppp_obytes;
3200 return &ppp->estats;
3203 #if LINUX_VERSION_CODE < VERSION(2,1,15)
3204 static int ppp_dev_header (sk_buff *skb, struct device *dev,
3205 __u16 type, void *daddr,
3206 void *saddr, unsigned int len)
3212 ppp_dev_rebuild (void *eth, struct device *dev,
3213 unsigned long raddr, struct sk_buff *skb)
3219 /*************************************************************
3221 * Miscellany called by various functions above.
3222 *************************************************************/
3224 /* Locate the previous instance of the PPP channel */
3226 ppp_find (int pid_value)
3230 /* try to find the device which this pid is already using */
3231 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3232 if (ppp->inuse && ppp->sc_xfer == pid_value) {
3240 /* Collect hung up channels */
3242 static void ppp_sync(void)
3247 #if LINUX_VERSION_CODE >= VERSION(2,1,68)
3250 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3253 if (dev->flags & IFF_UP)
3257 #if LINUX_VERSION_CODE >= VERSION(2,1,68)
3263 /* allocate or create a PPP channel */
3274 /* try to find an free device */
3276 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3277 if (!test_and_set_bit(0, &ppp->inuse)) {
3279 /* Reregister device */
3282 unregister_netdev (dev);
3284 if (register_netdev (dev)) {
3285 printk(KERN_DEBUG "cannot reregister ppp device\n");
3293 * There are no available units, so make a new one.
3295 ppp = (struct ppp *) kmalloc (sizeof(struct ppp), GFP_KERNEL);
3298 memset(ppp, 0, sizeof(*ppp));
3300 /* initialize channel control data */
3301 set_bit(0, &ppp->inuse);
3305 ppp->backup_tty = NULL;
3309 ppp_last->next = ppp;
3315 dev->init = ppp_init_dev;
3316 dev->name = ppp->name;
3317 sprintf(dev->name, "ppp%d", if_num);
3318 dev->base_addr = (__u32) if_num;
3319 dev->priv = (void *) ppp;
3321 /* register device so that we can be ifconfig'd */
3322 /* ppp_init_dev() will be called as a side-effect */
3323 status = register_netdev (dev);
3325 printk (KERN_INFO "registered device %s\n", dev->name);
3328 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3331 /* This one will forever be busy as it is not initialized */
3337 * Utility procedures to print a buffer in hex/ascii
3341 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3343 register __u8 next_ch;
3344 static char hex[] = "0123456789ABCDEF";
3346 while (count-- > 0) {
3348 *out++ = hex[(next_ch >> 4) & 0x0F];
3349 *out++ = hex[next_ch & 0x0F];
3355 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3357 register __u8 next_ch;
3359 while (count-- > 0) {
3362 if (next_ch < 0x20 || next_ch > 0x7e)
3366 if (next_ch == '%') /* printk/syslogd has a bug !! */
3374 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
3378 if (name != (__u8 *) NULL)
3379 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3382 memset (line, 32, 44);
3383 ppp_print_hex (line, buf, 8);
3384 ppp_print_char (&line[8 * 3], buf, 8);
3385 printk (KERN_DEBUG "%s\n", line);
3391 memset (line, 32, 44);
3392 ppp_print_hex (line, buf, count);
3393 ppp_print_char (&line[8 * 3], buf, count);
3394 printk (KERN_DEBUG "%s\n", line);
3398 /*************************************************************
3399 * Compressor module interface
3400 *************************************************************/
3402 struct compressor_link {
3403 struct compressor_link *next;
3404 struct compressor *comp;
3407 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3409 static struct compressor *find_compressor (int type)
3411 struct compressor_link *lnk;
3412 unsigned long flags;
3417 lnk = ppp_compressors;
3418 while (lnk != (struct compressor_link *) 0) {
3419 if ((int) (__u8) lnk->comp->compress_proto == type) {
3420 restore_flags(flags);
3426 restore_flags(flags);
3427 return (struct compressor *) 0;
3430 static int ppp_register_compressor (struct compressor *cp)
3432 struct compressor_link *new;
3433 unsigned long flags;
3435 new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3437 if (new == (struct compressor_link *) 0)
3443 if (find_compressor (cp->compress_proto)) {
3444 restore_flags(flags);
3449 new->next = ppp_compressors;
3451 ppp_compressors = new;
3453 restore_flags(flags);
3457 static void ppp_unregister_compressor (struct compressor *cp)
3459 struct compressor_link *prev = (struct compressor_link *) 0;
3460 struct compressor_link *lnk;
3461 unsigned long flags;
3466 lnk = ppp_compressors;
3467 while (lnk != (struct compressor_link *) 0) {
3468 if (lnk->comp == cp) {
3470 prev->next = lnk->next;
3472 ppp_compressors = lnk->next;
3479 restore_flags(flags);
3482 /*************************************************************
3483 * Module support routines
3484 *************************************************************/
3492 /* register our line disciplines */
3493 status = ppp_first_time();
3496 "PPP: ppp_init() failure %d\n", status);
3497 #if LINUX_VERSION_CODE < VERSION(2,1,18)
3499 (void) register_symtab (&ppp_syms);
3505 cleanup_module(void)
3509 struct ppp *ppp, *next_ppp;
3512 * Ensure that the devices are not in operation.
3514 for (ppp = ppp_list; ppp != 0; ppp = ppp->next) {
3515 if (ppp->inuse && ppp->tty != NULL) {
3520 dev = ppp2dev (ppp);
3521 if (dev->start || dev->flags & IFF_UP) {
3527 * Ensure that there are no compressor modules registered
3529 if (ppp_compressors != NULL)
3534 "PPP: device busy, remove delayed\n");
3538 * Release the tty registration of the line discipline so that no new entries
3541 status = tty_register_ldisc (N_PPP, NULL);
3544 "PPP: Unable to unregister ppp line discipline "
3545 "(err = %d)\n", status);
3548 "PPP: ppp line discipline successfully unregistered\n");
3550 * De-register the devices so that there is no problem with them
3552 for (ppp = ppp_list; ppp != 0; ppp = next_ppp) {
3553 next_ppp = ppp->next;
3555 unregister_netdev (&ppp->dev);