3 * Michael Callahan <callahan@maths.ox.ac.uk>
4 * Al Longyear <longyear@netcom.com>
6 * Dynamic PPP devices by Jim Freeman <jfree@caldera.com>.
7 * ppp_tty_receive ``noisy-raise-bug'' fixed by Ove Ewerlid <ewerlid@syscon.uu.se>
11 * NOTE TO MAINTAINERS:
12 * If you modify this file at all, increment the number above.
13 * ppp.c is shipped with a PPP distribution as well as with the kernel;
14 * if everyone increases the FILEVERSION number above, then scripts
15 * can do the right thing when deciding whether to install a new ppp.c
16 * file. Don't change the format of that line otherwise, so the
17 * installation script can recognize it.
25 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
26 Multi-protocol Datagrams over Point-to-Point Links
32 Flags for this module (any combination is acceptable for testing.):
34 OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
35 character. This is normally set to ((HZ * 3) / 2).
36 This is 1.5 seconds. If zero then the leading
39 CHECK_CHARACTERS - Enable the checking on all received characters for
40 8 data bits, no parity. This adds a small amount of
41 processing for each received character.
43 NEW_SKBUFF - Use NET3.020 sk_buff's
45 IPX_CHANGE - Force the use of IPX support into the driver.
46 THIS IS **VERY** ALPHA LEVEL CODE!!!!
49 /* #define NEW_SKBUFF 1 */
50 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
52 #define CHECK_CHARACTERS 1
53 #define PPP_COMPRESS 1
54 #define USE_SKB_PROTOCOL 1 /* Set by the installation program! */
57 #undef USE_SKB_PROTOCOL
58 #define USE_SKB_PROTOCOL 2
62 #define PPP_MAX_DEV 256
67 #if defined(IPX_CHANGE) || defined(NEW_SKBUFF)
68 #define PPP_REQUESTED_HDR_LEN PPP_HARD_HDR_LEN
70 #define PPP_REQUESTED_HDR_LEN 0
73 /* $Id: ppp.c,v 1.6 1995/12/18 03:38:12 paulus Exp $
74 * Added dynamic allocation of channels to eliminate
75 * compiled-in limits on the number of channels.
77 * Dynamic channel allocation code Copyright 1995 Caldera, Inc.,
78 * released under the GNU General Public License Version 2.
81 #if USE_SKB_PROTOCOL == 0
82 #include <linux/config.h> /* still needed for 1.2 */
84 #include <linux/module.h>
86 #ifndef MOD_INC_USE_COUNT /* for those 1.2 kernels still out there */
87 #undef MOD_DEC_USE_COUNT
88 #define MOD_DEC_USE_COUNT do {} while (0)
89 #define MOD_INC_USE_COUNT do {} while (0)
93 #include <linux/kernel.h>
94 #include <linux/sched.h>
95 #include <linux/types.h>
96 #include <linux/fcntl.h>
97 #include <linux/interrupt.h>
98 #include <linux/ptrace.h>
99 #include <linux/ioport.h>
100 #include <linux/in.h>
101 #include <linux/malloc.h>
102 #include <linux/tty.h>
103 #include <linux/errno.h>
104 #include <linux/sched.h> /* to get the struct task_struct */
105 #include <linux/string.h> /* used in new tty drivers */
106 #include <linux/signal.h> /* used in new tty drivers */
107 #include <asm/system.h>
108 #include <asm/bitops.h>
109 #include <asm/segment.h>
111 #include <linux/if_ether.h>
112 #include <linux/netdevice.h>
113 #include <linux/skbuff.h>
114 #include <linux/inet.h>
115 #include <linux/ioctl.h>
118 #include <linux/netprotocol.h>
120 typedef struct sk_buff sk_buff;
121 #define skb_data(skb) ((unsigned char *) (skb)->data)
124 #include <netinet/ip.h>
125 #include <netinet/tcp.h>
126 #include <linux/if_arp.h>
128 #include <linux/ppp_defs.h>
129 #include <linux/socket.h>
130 #include <linux/if_ppp.h>
131 #include <linux/if_pppvar.h>
135 #include <linux/ppp-comp.h>
138 #define bsd_decompress (*ppp->sc_rcomp->decompress)
139 #define bsd_compress (*ppp->sc_xcomp->compress)
142 #define PPP_IPX 0x2b /* IPX protocol over PPP */
146 #define PPP_LQR 0xc025 /* Link Quality Reporting Protocol */
149 static int ppp_register_compressor (struct compressor *cp);
150 static void ppp_unregister_compressor (struct compressor *cp);
156 static struct compressor *find_compressor (int type);
157 static void ppp_init_ctrl_blk (register struct ppp *);
158 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
159 static int ppp_doframe (struct ppp *);
160 static struct ppp *ppp_alloc (void);
161 static void ppp_print_buffer (const u_char *, const u_char *, int);
162 extern inline void ppp_stuff_char (struct ppp *ppp,
163 register struct ppp_buffer *buf,
164 register u_char chr);
165 extern inline int lock_buffer (register struct ppp_buffer *buf);
167 static int rcv_proto_ip (struct ppp *, u_short, u_char *, int);
168 static int rcv_proto_ipx (struct ppp *, u_short, u_char *, int);
169 static int rcv_proto_vjc_comp (struct ppp *, u_short, u_char *, int);
170 static int rcv_proto_vjc_uncomp (struct ppp *, u_short, u_char *, int);
171 static int rcv_proto_unknown (struct ppp *, u_short, u_char *, int);
172 static int rcv_proto_lqr (struct ppp *, u_short, u_char *, int);
173 static void ppp_doframe_lower (struct ppp *, u_char *, int);
174 static int ppp_doframe (struct ppp *);
176 extern int ppp_bsd_compressor_init(void);
177 static void ppp_proto_ccp (struct ppp *ppp, u_char *dp, int len, int rcvd);
178 static int rcv_proto_ccp (struct ppp *, u_short, u_char *, int);
180 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (u_char)(c))
182 #ifndef OPTIMIZE_FLAG_TIME
183 #define OPTIMIZE_FLAG_TIME 0
187 #define PPP_MAX_DEV 256
191 * Parameters which may be changed via insmod.
194 static int flag_time = OPTIMIZE_FLAG_TIME;
195 static int max_dev = PPP_MAX_DEV;
198 * The "main" procedure to the ppp device
201 int ppp_init (struct device *);
204 * Network device driver callback routines
207 static int ppp_dev_open (struct device *);
208 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
209 static int ppp_dev_close (struct device *);
210 static int ppp_dev_xmit (sk_buff *, struct device *);
211 static struct enet_statistics *ppp_dev_stats (struct device *);
213 #if USE_SKB_PROTOCOL == 0 /* The 1.2.x kernel is here */
214 #define dev_alloc_skb(count) alloc_skb(count, GFP_ATOMIC)
215 #define skb_put(skb,count) skb_data(skb)
216 #define get_long_user(addr) get_user_long((void *) addr)
217 #define get_int_user(addr) ((int) get_user_long((void *) addr))
218 #define put_byte_user(val,addr) put_fs_byte(val,((u_char *) (addr)))
219 #define put_long_user(val,addr) put_fs_long((val),((void *) (addr)))
221 static unsigned short ppp_dev_type (sk_buff *, struct device *);
222 static int ppp_dev_header (unsigned char *buff, struct device *dev,
223 unsigned short type, void *daddr, void *saddr,
224 unsigned len, struct sk_buff *skb);
226 #else /* The 1.3.x kernel is here */
227 #define get_long_user(addr) get_user(((int *) addr))
228 #define get_int_user(addr) ((int) get_user(((int *) addr)))
229 #define put_byte_user(val,addr) put_user((val),((u_char *) (addr)))
230 #define put_long_user(val,addr) put_user((val),((int *) (addr)))
232 static int ppp_dev_header (sk_buff *, struct device *, unsigned short,
233 void *, void *, unsigned);
237 static int ppp_dev_input (struct protocol *self, struct protocol *lower,
238 sk_buff *skb, void *saddr, void *daddr);
239 static int ppp_dev_output (struct protocol *self, sk_buff *skb, int type,
240 int subid, void *saddr, void *daddr, void *opt);
241 static int ppp_dev_getkey(int protocol, int subid, unsigned char *key);
243 static int ppp_dev_rebuild (void *, struct device *, unsigned long,
251 static int ppp_tty_read (struct tty_struct *, struct file *, u_char *,
253 static int ppp_tty_write (struct tty_struct *, struct file *, const u_char *,
255 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
257 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
258 struct file *filp, int sel_type, select_table * wait);
259 static int ppp_tty_open (struct tty_struct *);
260 static void ppp_tty_close (struct tty_struct *);
261 static int ppp_tty_room (struct tty_struct *tty);
262 static void ppp_tty_receive (struct tty_struct *tty, const u_char * cp,
263 char *fp, int count);
264 static void ppp_tty_wakeup (struct tty_struct *tty);
266 #define CHECK_PPP(a) if (!ppp->inuse) { printk (ppp_warning, __LINE__); return a;}
267 #define CHECK_PPP_VOID() if (!ppp->inuse) { printk (ppp_warning, __LINE__); return;}
269 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
270 #define in_rmap(ppp,c) ((((unsigned int) (u_char) (c)) < 0x20) && \
271 ppp->recv_async_map & (1 << (c)))
273 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
275 #define tty2ppp(tty) ((struct ppp *) (tty->disc_data))
276 #define dev2ppp(dev) ((struct ppp *) (dev->priv))
277 #define ppp2tty(ppp) ((struct tty_struct *) ppp->tty)
278 #define ppp2dev(ppp) ((struct device *) ppp->dev)
281 unsigned char address;
282 unsigned char control;
283 unsigned char protocol[2];
286 #define PPP_HARD_HDR_LEN (sizeof (struct ppp_hdr))
288 typedef struct ppp_ctrl {
289 struct ppp_ctrl *next; /* Next structure in the list */
290 char name [8]; /* Name of the device */
291 struct ppp ppp; /* PPP control table */
292 struct device dev; /* Device information table */
295 static ppp_ctrl_t *ppp_list = NULL;
297 #define ctl2ppp(ctl) (struct ppp *) &ctl->ppp
298 #define ctl2dev(ctl) (struct device *) &ctl->dev
302 #define BUFFER_TYPE_DEV_RD 0 /* ppp read buffer */
303 #define BUFFER_TYPE_TTY_WR 1 /* tty write buffer */
304 #define BUFFER_TYPE_DEV_WR 2 /* ppp write buffer */
305 #define BUFFER_TYPE_TTY_RD 3 /* tty read buffer */
306 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
308 /* Define this string only once for all macro envocations */
309 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
311 static char szVersion[] = PPP_VERSION;
314 static struct protocol proto_ppp;
318 * Information for the protocol decoder
321 typedef int (*pfn_proto) (struct ppp *, u_short, u_char *, int);
323 typedef struct ppp_proto_struct {
329 ppp_proto_type proto_list[] = {
330 { PPP_IP, rcv_proto_ip },
331 { PPP_IPX, rcv_proto_ipx },
332 { PPP_VJC_COMP, rcv_proto_vjc_comp },
333 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
334 { PPP_LQR, rcv_proto_lqr },
335 { PPP_CCP, rcv_proto_ccp },
336 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
340 * Values for FCS calculations.
343 #define PPP_INITFCS 0xffff /* Initial FCS value */
344 #define PPP_GOODFCS 0xf0b8 /* Good final FCS value */
345 #define PPP_FCS(fcs, c) (((fcs) >> 8) ^ ppp_crc16_table[((fcs) ^ (c)) & 0xff])
347 unsigned short ppp_crc16_table[256] =
349 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
350 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
351 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
352 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
353 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
354 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
355 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
356 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
357 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
358 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
359 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
360 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
361 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
362 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
363 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
364 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
365 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
366 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
367 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
368 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
369 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
370 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
371 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
372 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
373 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
374 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
375 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
376 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
377 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
378 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
379 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
380 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
383 #ifdef CHECK_CHARACTERS
384 static unsigned paritytab[8] =
386 0x96696996, 0x69969669, 0x69969669, 0x96696996,
387 0x69969669, 0x96696996, 0x96696996, 0x69969669
391 /* local function to store a value into the LQR frame */
392 extern inline u_char * store_long (register u_char *p, register int value) {
393 *p++ = (u_char) (value >> 24);
394 *p++ = (u_char) (value >> 16);
395 *p++ = (u_char) (value >> 8);
396 *p++ = (u_char) value;
400 /*************************************************************
402 *************************************************************/
404 /* This procedure is called once and once only to define who we are to
405 * the operating system and the various procedures that it may use in
406 * accessing the ppp protocol.
410 ppp_first_time (void)
412 static struct tty_ldisc ppp_ldisc;
416 "PPP: version %s (dynamic channel allocation)"
422 #ifndef MODULE /* slhc module logic has its own copyright announcment */
424 "TCP compression code copyright 1989 Regents of the "
425 "University of California\n");
429 "PPP Dynamic channel allocation code copyright 1995 "
432 * Register the protocol for the device
436 memset (&proto_ppp, 0, sizeof (proto_ppp));
438 proto_ppp.name = "PPP";
439 proto_ppp.output = ppp_dev_output;
440 proto_ppp.input = ppp_dev_input;
441 proto_ppp.bh_input = ppp_dev_input;
442 proto_ppp.control_event = default_protocol_control;
443 proto_ppp.get_binding = ppp_dev_getkey;
444 proto_ppp.header_space = PPP_REQUESTED_HDR_LEN;
446 protocol_register(&proto_ppp);
450 * Register the tty dicipline
452 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
453 ppp_ldisc.magic = TTY_LDISC_MAGIC;
454 ppp_ldisc.open = ppp_tty_open;
455 ppp_ldisc.close = ppp_tty_close;
456 ppp_ldisc.read = ppp_tty_read;
457 ppp_ldisc.write = ppp_tty_write;
458 ppp_ldisc.ioctl = ppp_tty_ioctl;
459 ppp_ldisc.select = ppp_tty_select;
460 ppp_ldisc.receive_room = ppp_tty_room;
461 ppp_ldisc.receive_buf = ppp_tty_receive;
462 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
464 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
466 printk (KERN_INFO "PPP line discipline registered.\n");
468 printk (KERN_ERR "error registering line discipline: %d\n",
473 /*************************************************************
475 *************************************************************/
477 /* called when the device is actually created */
480 ppp_init_dev (struct device *dev)
484 dev->default_protocol = &proto_ppp; /* Our protocol layer is PPP */
486 dev->hard_header = ppp_dev_header;
487 #if USE_SKB_PROTOCOL == 0
488 dev->type_trans = ppp_dev_type;
490 dev->rebuild_header = ppp_dev_rebuild;
491 dev->hard_header_len = PPP_REQUESTED_HDR_LEN;
496 dev->hard_start_xmit = ppp_dev_xmit;
497 dev->open = ppp_dev_open;
498 dev->stop = ppp_dev_close;
499 dev->get_stats = ppp_dev_stats;
500 dev->do_ioctl = ppp_dev_ioctl;
502 dev->type = ARPHRD_PPP;
504 for (indx = 0; indx < DEV_NUMBUFFS; indx++)
505 skb_queue_head_init (&dev->buffs[indx]);
507 /* New-style flags */
508 dev->flags = IFF_POINTOPOINT;
509 dev->family = AF_INET;
513 dev->pa_alen = 4; /* sizeof (unsigned long) */
519 * Local procedure to initialize the ppp structure
523 ppp_init_ctrl_blk (register struct ppp *ppp)
525 ppp->magic = PPP_MAGIC;
533 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
534 ppp->xmit_async_map[0] = 0xffffffff;
535 ppp->xmit_async_map[3] = 0x60000000;
536 ppp->recv_async_map = 0x00000000;
543 ppp->read_wait = NULL;
544 ppp->write_wait = NULL;
545 ppp->last_xmit = jiffies - flag_time;
547 /* clear statistics */
548 memset (&ppp->stats, '\0', sizeof (struct pppstat));
550 /* Reset the demand dial information */
551 ppp->ddinfo.xmit_idle= /* time since last NP packet sent */
552 ppp->ddinfo.recv_idle=jiffies; /* time since last NP packet received */
554 /* PPP compression data */
556 ppp->sc_rc_state = NULL;
559 static struct symbol_table ppp_syms = {
560 #include <linux/symtab_begin.h>
561 X(ppp_register_compressor),
562 X(ppp_unregister_compressor),
564 #include <linux/symtab_end.h>
567 /* called at boot/load time for each ppp device defined in the kernel */
571 ppp_init (struct device *dev)
573 static int first_time = 1;
578 answer = ppp_first_time();
580 (void) register_symtab (&ppp_syms);
589 * Routine to allocate a buffer for later use by the driver.
592 static struct ppp_buffer *
593 ppp_alloc_buf (int size, int type)
595 struct ppp_buffer *buf;
597 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
601 buf->size = size - 1; /* Mask for the buffer size */
607 buf->fcs = PPP_INITFCS;
613 * Routine to release the allocated buffer.
617 ppp_free_buf (struct ppp_buffer *ptr)
624 * Lock the indicated transmit buffer
628 lock_buffer (register struct ppp_buffer *buf)
633 * Save the current state and if free then set it to the "busy" state
641 * Restore the flags and return the previous state. 0 implies success.
643 restore_flags (flags);
648 * MTU has been changed by the IP layer. Unfortunately we are not told
649 * about this, but we spot it ourselves and fix things up. We could be
650 * in an upcall from the tty driver, or in an ip packet queue.
654 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
658 struct ppp_buffer *new_rbuf;
659 struct ppp_buffer *new_wbuf;
660 struct ppp_buffer *new_cbuf;
661 struct ppp_buffer *new_tbuf;
663 struct ppp_buffer *old_rbuf;
664 struct ppp_buffer *old_wbuf;
665 struct ppp_buffer *old_cbuf;
666 struct ppp_buffer *old_tbuf;
670 * Allocate the buffer from the kernel for the data
674 /* allow for possible escapement of every character */
675 mtu = (new_mtu * 2) + 20;
677 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
683 if (ppp->flags & SC_DEBUG)
684 printk (KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
685 dev->name, new_mtu, new_mru);
687 new_wbuf = ppp_alloc_buf (mtu+PPP_HARD_HDR_LEN, BUFFER_TYPE_DEV_WR);
688 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
689 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
690 new_cbuf = ppp_alloc_buf (mru+PPP_HARD_HDR_LEN, BUFFER_TYPE_VJ);
692 * If the buffers failed to allocate then complain and release the partial
695 if (new_wbuf == NULL || new_tbuf == NULL ||
696 new_rbuf == NULL || new_cbuf == NULL) {
697 if (ppp->flags & SC_DEBUG)
699 "ppp: failed to allocate new buffers\n");
701 ppp_free_buf (new_wbuf);
702 ppp_free_buf (new_tbuf);
703 ppp_free_buf (new_rbuf);
704 ppp_free_buf (new_cbuf);
708 * Update the pointers to the new buffer structures.
711 old_wbuf = ppp->wbuf;
712 old_rbuf = ppp->rbuf;
713 old_cbuf = ppp->cbuf;
714 old_tbuf = ppp->tbuf;
716 ppp->wbuf = new_wbuf;
717 ppp->rbuf = new_rbuf;
718 ppp->cbuf = new_cbuf;
719 ppp->tbuf = new_tbuf;
721 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
723 dev->mem_start = (unsigned long) buf_base (new_wbuf);
724 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
725 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
726 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
728 * Update the parameters for the new buffer sizes
730 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
731 ppp->escape = 0; /* No pending escape character */
741 ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
742 ppp->flags &= ~SC_XMIT_BUSY;
746 * Release old buffer pointers
748 ppp_free_buf (old_rbuf);
749 ppp_free_buf (old_wbuf);
750 ppp_free_buf (old_cbuf);
751 ppp_free_buf (old_tbuf);
756 * CCP is down; free (de)compressor state if necessary.
760 ppp_ccp_closed (struct ppp *ppp)
762 if (ppp->sc_xc_state) {
763 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
764 ppp->sc_xc_state = NULL;
767 if (ppp->sc_rc_state) {
768 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
769 ppp->sc_rc_state = NULL;
774 * Called to release all of the information in the current PPP structure.
776 * It is called when the ppp device goes down or if it is unable to go
781 ppp_release (struct ppp *ppp)
783 struct tty_struct *tty;
789 ppp_ccp_closed (ppp);
791 if (tty != NULL && tty->disc_data == ppp)
792 tty->disc_data = NULL; /* Break the tty->ppp link */
794 if (dev && dev->flags & IFF_UP) {
795 dev_close (dev); /* close the device properly */
796 dev->flags = 0; /* prevent recursion */
799 ppp_free_buf (ppp->rbuf);
800 ppp_free_buf (ppp->wbuf);
801 ppp_free_buf (ppp->cbuf);
802 ppp_free_buf (ppp->ubuf);
803 ppp_free_buf (ppp->tbuf);
815 slhc_free (ppp->slcomp);
826 * Called when the PPP device goes down in response to an ifconfig request.
830 ppp_tty_close (struct tty_struct *tty)
832 struct ppp *ppp = tty2ppp (tty);
835 if (ppp->magic != PPP_MAGIC) {
836 if (ppp->flags & SC_DEBUG)
838 "ppp: trying to close unopened tty!\n");
841 if (ppp->flags & SC_DEBUG)
842 printk (KERN_INFO "ppp: channel %s closing.\n",
843 ppp2dev(ppp) -> name);
853 * Called when the tty discipline is switched to PPP.
857 ppp_tty_open (struct tty_struct *tty)
859 struct ppp *ppp = tty2ppp (tty);
861 * There should not be an existing table for this slot.
864 if (ppp->flags & SC_DEBUG)
866 "ppp_tty_open: gack! tty already associated to %s!\n",
867 ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
872 * Allocate the structure from the system
876 if (ppp->flags & SC_DEBUG)
878 "ppp_tty_open: couldn't allocate ppp channel\n");
882 * Initialize the control block
884 ppp_init_ctrl_blk (ppp);
886 tty->disc_data = ppp;
888 * Flush any pending characters in the driver and discipline.
890 if (tty->ldisc.flush_buffer)
891 tty->ldisc.flush_buffer (tty);
893 if (tty->driver.flush_buffer)
894 tty->driver.flush_buffer (tty);
896 * Allocate space for the default VJ header compression slots
898 ppp->slcomp = slhc_init (16, 16);
899 if (ppp->slcomp == NULL) {
900 if (ppp->flags & SC_DEBUG)
902 "ppp_tty_open: no space for compression buffers!\n");
907 * Allocate space for the MTU and MRU buffers
909 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
914 * Allocate space for a user level buffer
916 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
917 if (ppp->ubuf == NULL) {
918 if (ppp->flags & SC_DEBUG)
920 "ppp_tty_open: no space for user receive buffer\n");
925 if (ppp->flags & SC_DEBUG)
926 printk (KERN_INFO "ppp: channel %s open\n",
934 * Local function to send the next portion of the buffer.
936 * Called by the tty driver's tty_wakeup function should it be entered
937 * because the partial buffer was transmitted.
939 * Called by kick_tty to send the initial portion of the buffer.
941 * Completion processing of the buffer transmission is handled here.
945 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
946 struct ppp_buffer *xbuf)
948 register int count, actual;
950 * Prevent re-entrancy by ensuring that this routine is called only once.
953 if (ppp->flags & SC_XMIT_BUSY) {
957 ppp->flags |= SC_XMIT_BUSY;
960 * Send the next block of data to the modem
962 count = xbuf->count - xbuf->tail;
963 actual = tty->driver.write (tty, 0,
964 buf_base (xbuf) + xbuf->tail, count);
966 * Terminate transmission of any block which may have an error.
967 * This could occur should the carrier drop.
970 ppp->stats.ppp_oerrors++;
973 ppp->bytes_sent += actual;
975 * If the buffer has been transmitted then clear the indicators.
977 xbuf->tail += actual;
978 if (actual == count) {
980 ppp->flags &= ~SC_XMIT_BUSY;
982 * Complete the transmission on the current buffer.
986 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
990 * If the completed buffer came from the device write, then complete the
991 * transmission block.
993 if (ppp2dev (ppp) -> flags & IFF_UP) {
994 if (xbuf->type == BUFFER_TYPE_DEV_WR)
995 ppp2dev (ppp)->tbusy = 0;
999 * Wake up the transmission queue for all completion events.
1001 wake_up_interruptible (&ppp->write_wait);
1003 * Look at the priorities. Choose a daemon write over the device driver.
1014 * If there is a pending buffer then transmit it now.
1017 ppp->flags &= ~SC_XMIT_BUSY;
1018 ppp_kick_tty (ppp, xbuf);
1024 * Clear the re-entry flag
1026 ppp->flags &= ~SC_XMIT_BUSY;
1030 * This function is called by the tty driver when the transmit buffer has
1031 * additional space. It is used by the ppp code to continue to transmit
1032 * the current buffer should the buffer have been partially sent.
1034 * In addition, it is used to send the first part of the buffer since the
1035 * logic and the inter-locking would be identical.
1039 ppp_tty_wakeup (struct tty_struct *tty)
1041 struct ppp_buffer *xbuf;
1042 struct ppp *ppp = tty2ppp (tty);
1047 if (ppp->magic != PPP_MAGIC)
1050 * Ensure that there is a transmission pending. Clear the re-entry flag if
1051 * there is no pending buffer. Otherwise, send the buffer.
1055 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1057 ppp_tty_wakeup_code (ppp, tty, xbuf);
1061 * This function is called to transmit a buffer to the remote. The buffer
1062 * is placed on the pending queue if there is presently a buffer being
1063 * sent or it is transmitted with the aid of ppp_tty_wakeup.
1067 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
1076 * Control the flags which are best performed with the interrupts masked.
1081 * If the transmitter is busy then place the buffer on the appropriate
1084 if (ppp->xbuf != NULL) {
1085 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1089 restore_flags (flags);
1093 * If the transmitter is not busy then this is the highest priority frame
1095 ppp->flags &= ~SC_XMIT_BUSY;
1096 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1098 restore_flags (flags);
1100 * Do the "tty wakeup_code" to actually send this buffer.
1102 ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1105 /*************************************************************
1107 * The following functions handle input that arrives from
1108 * the TTY. It recognizes PPP frames and either hands them
1109 * to the network layer or queues them for delivery to a
1110 * user process reading this TTY.
1111 *************************************************************/
1114 * Callback function from tty driver. Return the amount of space left
1115 * in the receiver's buffer to decide if remote transmitter is to be
1120 ppp_tty_room (struct tty_struct *tty)
1122 return 65536; /* We can handle an infinite amount of data. :-) */
1126 * Callback function when data is available at the tty driver.
1130 ppp_tty_receive (struct tty_struct *tty, const u_char * data,
1131 char *flags, int count)
1133 register struct ppp *ppp = tty2ppp (tty);
1134 register struct ppp_buffer *buf = NULL;
1137 * Fetch the pointer to the buffer. Be careful about race conditions.
1145 * Verify the table pointer and ensure that the line is
1146 * still in PPP discipline.
1148 if (ppp->magic != PPP_MAGIC) {
1149 if (ppp->flags & SC_DEBUG)
1151 "PPP: handler called but couldn't find "
1157 * Print the buffer if desired
1159 if (ppp->flags & SC_LOG_RAWIN)
1160 ppp_print_buffer ("receive buffer", data, count);
1162 * Collect the character and error condition for the character. Set the toss
1163 * flag for the first character error.
1165 while (count-- > 0) {
1169 if (*flags && ppp->toss == 0)
1174 * Set the flags for 8 data bits and no parity.
1176 * Actually, it sets the flags for d7 being 0/1 and parity being even/odd
1177 * so that the normal processing would have all flags set at the end of the
1178 * session. A missing flag bit would denote an error condition.
1181 #ifdef CHECK_CHARACTERS
1183 ppp->flags |= SC_RCV_B7_1;
1185 ppp->flags |= SC_RCV_B7_0;
1187 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1188 ppp->flags |= SC_RCV_ODDP;
1190 ppp->flags |= SC_RCV_EVNP;
1193 * Branch on the character. Process the escape character. The sequence ESC ESC
1194 * is defined to be ESC.
1197 case PPP_ESCAPE: /* PPP_ESCAPE: invert bit in next character */
1198 ppp->escape = PPP_TRANS;
1201 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1202 * then the block is to be ignored. In addition, characters before the very
1203 * first FLAG are also tossed by this procedure.
1205 case PPP_FLAG: /* PPP_FLAG: end of frame */
1206 ppp->stats.ppp_ibytes += ppp->rbuf->count;
1210 * Process frames which are not to be ignored. If the processing failed,
1211 * then clean up the VJ tables.
1213 if ((ppp->toss & 0x80) != 0 ||
1214 ppp_doframe (ppp) == 0) {
1215 slhc_toss (ppp->slcomp);
1218 * Reset all indicators for the new frame to follow.
1221 buf->fcs = PPP_INITFCS;
1226 * All other characters in the data come here. If the character is in the
1227 * receive mask then ignore the character.
1230 if (in_rmap (ppp, chr))
1233 * Adjust the character and if the frame is to be discarded then simply
1234 * ignore the character until the ending FLAG is received.
1242 * If the count sent is within reason then store the character, bump the
1243 * count, and update the FCS for the character.
1245 if (buf->count < buf->size) {
1246 buf_base (buf)[buf->count++] = chr;
1247 buf->fcs = PPP_FCS (buf->fcs, chr);
1251 * The peer sent too much data. Set the flags to discard the current frame
1252 * and wait for the re-synchronization FLAG to be sent.
1254 ppp->stats.ppp_ierrors++;
1262 * Put the input frame into the networking system for the indicated protocol
1266 ppp_rcv_rx (struct ppp *ppp, unsigned short proto, u_char * data, int count)
1268 sk_buff *skb = dev_alloc_skb (count);
1270 * Generate a skb buffer for the new frame.
1273 if (ppp->flags & SC_DEBUG)
1275 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1276 ppp2dev (ppp)->name);
1280 * Move the received data from the input buffer to the skb buffer.
1282 skb->dev = ppp2dev (ppp); /* We are the device */
1283 #if USE_SKB_PROTOCOL == 0
1286 skb->protocol = proto;
1287 skb->mac.raw = skb_data(skb);
1289 memcpy (skb_put(skb,count), data, count); /* move data */
1291 * Tag the frame and kick it to the proper receive routine
1294 ppp->ddinfo.recv_idle = jiffies;
1300 * Process the receipt of an IP frame
1304 rcv_proto_ip (struct ppp *ppp, unsigned short proto, u_char * data, int count)
1306 if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1307 return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1312 * Process the receipt of an IPX frame
1316 rcv_proto_ipx (struct ppp *ppp, unsigned short proto, u_char * data, int count)
1318 #if defined(NEW_SKBUFF) || defined(IPX_CHANGE)
1319 if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1320 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1326 * Process the receipt of an VJ Compressed frame
1330 rcv_proto_vjc_comp (struct ppp *ppp, unsigned short proto,
1331 u_char *data, int count)
1333 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1334 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1335 if (new_count >= 0) {
1336 return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1338 if (ppp->flags & SC_DEBUG)
1340 "ppp: error in VJ decompression\n");
1346 * Process the receipt of an VJ Un-compressed frame
1350 rcv_proto_vjc_uncomp (struct ppp *ppp, unsigned short proto,
1351 u_char *data, int count)
1353 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1354 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1355 return rcv_proto_ip (ppp, PPP_IP, data, count);
1357 if (ppp->flags & SC_DEBUG)
1359 "ppp: error in VJ memorizing\n");
1365 * Receive all unclassified protocols.
1369 rcv_proto_unknown (struct ppp *ppp, unsigned short proto,
1370 u_char *data, int len)
1373 register int current_idx;
1377 buf_base (ppp->ubuf) [current_idx++] = (u_char) (c); \
1378 current_idx &= ppp->ubuf->size; \
1379 if (current_idx == ppp->ubuf->tail) \
1384 * The total length includes the protocol data.
1385 * Lock the user information buffer.
1387 if (set_bit (0, &ppp->ubuf->locked)) {
1388 if (ppp->flags & SC_DEBUG)
1390 "ppp_us_queue: can't get lock\n");
1392 current_idx = ppp->ubuf->head;
1394 * Insert the buffer length (not counted), the protocol, and the data
1404 while (totlen-- > 0) {
1409 * The frame is complete. Update the head pointer and wakeup the pppd
1412 ppp->ubuf->head = current_idx;
1414 clear_bit (0, &ppp->ubuf->locked);
1415 wake_up_interruptible (&ppp->read_wait);
1416 if (ppp->tty->fasync != NULL)
1417 kill_fasync (ppp->tty->fasync, SIGIO);
1419 if (ppp->flags & SC_DEBUG)
1421 "ppp: successfully queued %d bytes, flags = %x\n",
1422 len + 2, ppp->flags);
1426 * The buffer is full. Unlock the header
1429 clear_bit (0, &ppp->ubuf->locked);
1430 if (ppp->flags & SC_DEBUG)
1432 "ppp_us_queue: ran out of buffer space.\n");
1435 * Discard the frame. There are no takers for this protocol.
1437 if (ppp->flags & SC_DEBUG)
1438 printk (KERN_WARNING
1439 "ppp: dropping packet on the floor.\n");
1440 slhc_toss (ppp->slcomp);
1445 * Handle a CCP packet.
1447 * The CCP packet is passed along to the pppd process just like any
1448 * other PPP frame. The difference is that some processing needs to be
1449 * immediate or the compressors will become confused on the peer.
1452 static void ppp_proto_ccp (struct ppp *ppp, u_char *dp, int len, int rcvd)
1454 int slen = CCP_LENGTH(dp);
1455 u_char *opt = dp + CCP_HDRLEN;
1456 int opt_len = slen - CCP_HDRLEN;
1461 switch (CCP_CODE(dp)) {
1466 * CCP must be going down - disable compression
1468 if (ppp->flags & SC_CCP_UP) {
1469 ppp->flags &= ~(SC_CCP_UP |
1476 if ((ppp->flags & SC_CCP_OPEN) == 0)
1478 if (ppp->flags & SC_CCP_UP)
1480 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1482 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1485 * we're agreeing to send compressed packets.
1488 if (ppp->sc_xc_state == NULL)
1491 if ((*ppp->sc_xcomp->comp_init)
1495 ppp2dev (ppp)->base_addr,
1498 ppp->flags |= SC_COMP_RUN;
1502 * peer is agreeing to send compressed packets.
1504 if (ppp->sc_rc_state == NULL)
1507 if ((*ppp->sc_rcomp->decomp_init)
1511 ppp2dev (ppp)->base_addr,
1515 ppp->flags |= SC_DECOMP_RUN;
1516 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1520 * The protocol sequence is complete at this end
1523 if ((ppp->flags & SC_CCP_UP) == 0)
1527 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN))
1528 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1530 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1531 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1532 ppp->flags &= ~SC_DC_ERROR;
1540 rcv_proto_ccp (struct ppp *ppp, unsigned short proto, u_char *dp, int len)
1542 ppp_proto_ccp (ppp, dp, len, 1);
1543 return rcv_proto_unknown (ppp, proto, dp, len);
1547 * Handle a LQR packet.
1549 * The LQR packet is passed along to the pppd process just like any
1550 * other PPP frame. The difference is that some processing needs to be
1551 * performed to append the current data to the end of the frame.
1555 rcv_proto_lqr (struct ppp *ppp, unsigned short proto, u_char * data, int len)
1557 #if 0 /* until support is in the pppd process don't corrupt the reject. */
1561 memset (&data [len], '\0', 48 - len);
1563 * Fill in the fields from the driver data
1566 p = store_long (p, ++ppp->stats.ppp_ilqrs);
1567 p = store_long (p, ppp->stats.ppp_ipackets);
1568 p = store_long (p, ppp->stats.ppp_discards);
1569 p = store_long (p, ppp->stats.ppp_ierrors);
1570 p = store_long (p, ppp->stats.ppp_ioctects + len);
1576 * Pass the frame to the pppd daemon.
1578 return rcv_proto_unknown (ppp, proto, data, len);
1581 /* on entry, a received frame is in ppp->rbuf.bufr
1582 check it and dispose as appropriate */
1584 static void ppp_doframe_lower (struct ppp *ppp, u_char *data, int count)
1586 u_short proto = PPP_PROTOCOL (data);
1587 ppp_proto_type *proto_ptr;
1589 * Ignore empty frames
1594 * Count the frame and print it
1596 ++ppp->stats.ppp_ipackets;
1597 if (ppp->flags & SC_LOG_INPKT)
1598 ppp_print_buffer ("receive frame", data, count);
1600 * Find the procedure to handle this protocol. The last one is marked
1601 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1603 proto_ptr = proto_list;
1604 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1607 * Update the appropriate statistic counter.
1609 if ((*proto_ptr->func) (ppp, proto,
1610 &data[PPP_HARD_HDR_LEN],
1611 count - PPP_HARD_HDR_LEN))
1612 ppp->stats.ppp_ioctects += count;
1614 ++ppp->stats.ppp_discards;
1617 /* on entry, a received frame is in ppp->rbuf.bufr
1618 check it and dispose as appropriate */
1621 ppp_doframe (struct ppp *ppp)
1623 u_char *data = buf_base (ppp->rbuf);
1624 int count = ppp->rbuf->count;
1625 int addr, ctrl, proto;
1629 * If there is a pending error from the receiver then log it and discard
1630 * the damaged frame.
1633 if (ppp->flags & SC_DEBUG)
1634 printk (KERN_WARNING
1635 "ppp_toss: tossing frame, reason = %d\n",
1637 ppp->stats.ppp_ierrors++;
1641 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1642 * follows each frame.
1647 * Generate an error if the frame is too small.
1649 if (count < PPP_HARD_HDR_LEN) {
1650 if (ppp->flags & SC_DEBUG)
1651 printk (KERN_WARNING
1652 "ppp: got runt ppp frame, %d chars\n", count);
1653 slhc_toss (ppp->slcomp);
1654 ppp->stats.ppp_ierrors++;
1658 * Verify the CRC of the frame and discard the CRC characters from the
1659 * end of the buffer.
1661 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1662 if (ppp->flags & SC_DEBUG)
1663 printk (KERN_WARNING
1664 "ppp: frame with bad fcs, excess = %x\n",
1665 ppp->rbuf->fcs ^ PPP_GOODFCS);
1666 ppp->stats.ppp_ierrors++;
1669 count -= 2; /* ignore the fcs characters */
1671 * Ignore the leading ADDRESS and CONTROL fields in the frame.
1673 addr = PPP_ALLSTATIONS;
1676 if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1681 * Obtain the protocol from the frame
1683 proto = (u_short) *data++;
1684 if ((proto & 1) == 0) {
1685 proto = (proto << 8) | (u_short) *data++;
1689 * Rewrite the header with the full information. This may encroach upon
1690 * the 'filler' area in the buffer header. This is the purpose for the
1694 *(--data) = proto >> 8;
1699 * Process the active decompressor.
1701 if ((ppp->sc_rc_state != (void *) 0) &&
1702 (ppp->flags & SC_DECOMP_RUN) &&
1703 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1704 if (proto == PPP_COMP) {
1706 * If the frame is compressed then decompress it.
1708 new_data = kmalloc (ppp->mru + 4, GFP_ATOMIC);
1709 if (new_data == NULL) {
1710 if (ppp->flags & SC_DEBUG)
1712 "ppp_doframe: no memory\n");
1713 slhc_toss (ppp->slcomp);
1714 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1720 * Decompress the frame
1722 new_count = bsd_decompress (ppp->sc_rc_state,
1727 switch (new_count) {
1729 ppp_doframe_lower (ppp, new_data, new_count);
1737 ppp->flags |= SC_DC_ERROR;
1740 case DECOMP_FATALERROR:
1741 ppp->flags |= SC_DC_FERROR;
1745 * Log the error condition and discard the frame.
1747 if (ppp->flags & SC_DEBUG)
1750 "decompress err %d\n", new_count);
1752 slhc_toss (ppp->slcomp);
1756 * The frame is not special. Pass it through the compressor without
1757 * actually compressing the data
1759 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1764 * Process the uncompressed frame.
1766 ppp_doframe_lower (ppp, data, count);
1770 /*************************************************************
1771 * LINE DISCIPLINE SUPPORT
1772 * The following functions form support user programs
1773 * which read and write data on a TTY with the PPP line
1774 * discipline. Reading is done from a circular queue,
1775 * filled by the lower TTY levels.
1776 *************************************************************/
1778 /* read a PPP frame from the us_rbuff circular buffer,
1779 waiting if necessary
1783 ppp_tty_read (struct tty_struct *tty, struct file *file, u_char * buf,
1786 struct ppp *ppp = tty2ppp (tty);
1792 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1793 ppp->ubuf->tail &= ppp->ubuf->size; \
1797 * Validate the pointer to the PPP structure
1802 if (ppp->magic != PPP_MAGIC)
1807 if (ppp->flags & SC_DEBUG)
1809 "ppp_tty_read: called buf=%p nr=%u\n",
1812 * Acquire the read lock.
1815 ppp = tty2ppp (tty);
1816 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse)
1819 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1820 if (ppp->flags & SC_DEBUG)
1822 "ppp_tty_read: sleeping(ubuf)\n");
1824 current->timeout = 0;
1825 current->state = TASK_INTERRUPTIBLE;
1828 if (current->signal & ~current->blocked)
1833 * Before we attempt to write the frame to the user, ensure that the
1834 * user has access to the pages for the total buffer length.
1836 indx = verify_area (VERIFY_WRITE, buf, nr);
1840 * Fetch the length of the buffer from the first two bytes.
1842 if (ppp->ubuf->head == ppp->ubuf->tail)
1851 * If there is no length then wait for the data to arrive.
1855 clear_bit (0, &ppp->ubuf->locked);
1856 if (file->f_flags & O_NONBLOCK) {
1857 if (ppp->flags & SC_DEBUG)
1859 "ppp_tty_read: no data "
1861 return -EWOULDBLOCK;
1863 current->timeout = 0;
1865 if (ppp->flags & SC_DEBUG)
1867 "ppp_tty_read: sleeping(read_wait)\n");
1869 interruptible_sleep_on (&ppp->read_wait);
1870 if (current->signal & ~current->blocked)
1875 * Reset the time of the last read operation.
1877 if (ppp->flags & SC_DEBUG)
1878 printk (KERN_DEBUG "ppp_tty_read: len = %d\n", len);
1880 * Ensure that the frame will fit within the caller's buffer. If not, then
1881 * discard the frame from the input buffer and return an error to the caller.
1884 /* Can't copy it, update us_rbuff_head */
1886 if (ppp->flags & SC_DEBUG)
1888 "ppp: read of %u bytes too small for %d "
1889 "frame\n", nr, len + 2);
1890 ppp->ubuf->tail += len;
1891 ppp->ubuf->tail &= ppp->ubuf->size;
1892 clear_bit (0, &ppp->ubuf->locked);
1893 ppp->stats.ppp_ierrors++;
1897 * Before we attempt to write the frame to the user, ensure that the
1898 * page tables are proper.
1900 indx = verify_area (VERIFY_WRITE, buf, len + 2);
1902 ppp->ubuf->tail += len;
1903 ppp->ubuf->tail &= ppp->ubuf->size;
1904 clear_bit (0, &ppp->ubuf->locked);
1908 * Fake the insertion of the ADDRESS and CONTROL information because these
1909 * were not saved in the buffer.
1911 put_byte_user (PPP_ALLSTATIONS, buf++);
1912 put_byte_user (PPP_UI, buf++);
1916 * Copy the received data from the buffer to the caller's area.
1918 while (indx-- > 0) {
1920 put_byte_user (c, buf);
1924 * Release the lock and return the character count in the buffer area.
1926 clear_bit (0, &ppp->ubuf->locked);
1927 len += 2; /* Account for ADDRESS and CONTROL bytes */
1928 if (ppp->flags & SC_DEBUG)
1930 "ppp_tty_read: passing %d bytes up\n", len);
1936 /* stuff a character into the transmit buffer, using PPP's way of escaping
1938 also, update fcs to take account of new character */
1941 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1942 register u_char chr)
1945 * The buffer should not be full.
1947 if (ppp->flags & SC_DEBUG) {
1948 if ((buf->count < 0) || (buf->count > 3000))
1949 printk (KERN_DEBUG "ppp_stuff_char: %x %d\n",
1950 (unsigned int) buf->count,
1951 (unsigned int) chr);
1954 * Update the FCS and if the character needs to be escaped, do it.
1956 buf->fcs = PPP_FCS (buf->fcs, chr);
1957 if (in_xmap (ppp, chr)) {
1959 ins_char (buf, PPP_ESCAPE);
1962 * Add the character to the buffer.
1964 ins_char (buf, chr);
1968 * Procedure to encode the data with the proper escapement and send the
1969 * data to the remote system.
1973 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
1974 u_char *data, int count, int non_ip)
1976 unsigned short int write_fcs;
1977 int address, control;
1980 * Insert the leading FLAG character
1984 if (non_ip || flag_time == 0)
1985 ins_char (buf, PPP_FLAG);
1987 if (jiffies - ppp->last_xmit > flag_time)
1988 ins_char (buf, PPP_FLAG);
1990 ppp->last_xmit = jiffies;
1991 buf->fcs = PPP_INITFCS;
1993 * Emit the address/control information if needed
1995 address = PPP_ADDRESS (data);
1996 control = PPP_CONTROL (data);
1997 proto = PPP_PROTOCOL (data);
1999 if (address != PPP_ALLSTATIONS ||
2000 control != PPP_UI ||
2001 (ppp->flags & SC_COMP_AC) == 0) {
2002 ppp_stuff_char (ppp, buf, address);
2003 ppp_stuff_char (ppp, buf, control);
2006 * Emit the protocol (compressed if possible)
2008 if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
2009 ppp_stuff_char (ppp, buf, proto >> 8);
2011 ppp_stuff_char (ppp, buf, proto);
2019 ppp_stuff_char (ppp, buf, *data++);
2021 * Add the trailing CRC and the final flag character
2023 write_fcs = buf->fcs ^ 0xFFFF;
2024 ppp_stuff_char (ppp, buf, write_fcs);
2025 ppp_stuff_char (ppp, buf, write_fcs >> 8);
2027 if (ppp->flags & SC_DEBUG)
2028 printk (KERN_DEBUG "ppp_dev_xmit_lower: fcs is %hx\n",
2031 * Add the trailing flag character
2033 ins_char (buf, PPP_FLAG);
2037 if (ppp->flags & SC_LOG_FLUSH)
2038 ppp_print_buffer ("ppp flush", buf_base (buf),
2041 if (ppp->flags & SC_DEBUG)
2043 "ppp_dev_xmit: writing %d chars\n",
2047 * Send the block to the tty driver.
2049 ppp->stats.ppp_obytes += buf->count;
2050 ppp_kick_tty (ppp, buf);
2054 * Send an frame to the remote with the proper bsd compression.
2056 * Return 0 if frame was queued for transmission.
2057 * 1 if frame must be re-queued for later driver support.
2061 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
2062 u_char *data, int count)
2065 int address, control;
2071 if (ppp->flags & SC_LOG_OUTPKT)
2072 ppp_print_buffer ("write frame", data, count);
2074 * Determine if the frame may be compressed. Attempt to compress the
2075 * frame if possible.
2077 proto = PPP_PROTOCOL (data);
2078 address = PPP_ADDRESS (data);
2079 control = PPP_CONTROL (data);
2081 if (((ppp->flags & SC_COMP_RUN) != 0) &&
2082 (ppp->sc_xc_state != (void *) 0) &&
2083 (address == PPP_ALLSTATIONS) &&
2084 (control == PPP_UI) &&
2085 (proto != PPP_LCP) &&
2086 (proto != PPP_CCP)) {
2087 new_data = kmalloc (count, GFP_ATOMIC);
2088 if (new_data == NULL) {
2089 if (ppp->flags & SC_DEBUG)
2091 "ppp_dev_xmit_frame: no memory\n");
2095 new_count = bsd_compress (ppp->sc_xc_state,
2101 if (new_count > 0) {
2102 ++ppp->stats.ppp_opackets;
2103 ppp->stats.ppp_ooctects += new_count;
2105 ppp_dev_xmit_lower (ppp, buf, new_data,
2111 * The frame could not be compressed.
2116 * The frame may not be compressed. Update the statistics before the
2117 * count field is destroyed. The frame will be transmitted.
2119 ++ppp->stats.ppp_opackets;
2120 ppp->stats.ppp_ooctects += count;
2122 * Go to the escape encoding
2124 ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2129 * Revise the tty frame for specific protocols.
2133 send_revise_frame (register struct ppp *ppp, u_char *data, int len)
2137 switch (PPP_PROTOCOL (data)) {
2139 * Update the LQR frame with the current MIB information. This saves having
2140 * the daemon read old MIB data from the driver.
2143 len = 48; /* total size of this frame */
2144 p = (u_char *) &data [40]; /* Point to last two items. */
2145 p = store_long (p, ppp->stats.ppp_opackets + 1);
2146 p = store_long (p, ppp->stats.ppp_ooctects + len);
2149 * Outbound compression frames
2153 data + PPP_HARD_HDR_LEN,
2154 len - PPP_HARD_HDR_LEN,
2158 * All other frame types
2168 * write a frame with NR chars from BUF to TTY
2169 * we have to put the FCS field on ourselves
2173 ppp_tty_write (struct tty_struct *tty, struct file *file, const u_char * data,
2176 struct ppp *ppp = tty2ppp (tty);
2180 * Verify the pointer to the PPP data and that the tty is still in PPP mode.
2185 if (ppp->magic != PPP_MAGIC)
2190 * Ensure that the caller does not wish to send too much.
2192 if (count > PPP_MTU) {
2193 if (ppp->flags & SC_DEBUG)
2194 printk (KERN_WARNING
2195 "ppp_tty_write: truncating user packet "
2196 "from %u to mtu %d\n", count, PPP_MTU);
2200 * Allocate a buffer for the data and fetch it from the user space.
2202 new_data = kmalloc (count, GFP_KERNEL);
2203 if (new_data == NULL) {
2204 if (ppp->flags & SC_DEBUG)
2206 "ppp_tty_write: no memory\n");
2210 * lock this PPP unit so we will be the only writer;
2211 * sleep if necessary
2213 while (lock_buffer (ppp->tbuf) != 0) {
2214 current->timeout = 0;
2215 if (ppp->flags & SC_DEBUG)
2216 printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2217 interruptible_sleep_on (&ppp->write_wait);
2219 ppp = tty2ppp (tty);
2220 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse) {
2225 if (current->signal & ~current->blocked) {
2231 * Ensure that the caller's buffer is valid.
2233 status = verify_area (VERIFY_READ, data, count);
2236 ppp->tbuf->locked = 0;
2240 memcpy_fromfs (new_data, data, count);
2242 * Change the LQR frame
2244 count = send_revise_frame (ppp, new_data, count);
2248 ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2254 * Process the BSD compression IOCTL event for the tty device.
2258 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2260 struct compressor *cp;
2261 struct ppp_option_data data;
2265 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
2267 * Fetch the compression parameters
2269 error = verify_area (VERIFY_READ, odp, sizeof (data));
2271 memcpy_fromfs (&data, odp, sizeof (data));
2274 if ((unsigned long) nb >= (unsigned long)CCP_MAX_OPTION_LENGTH)
2275 nb = CCP_MAX_OPTION_LENGTH;
2277 error = verify_area (VERIFY_READ, ptr, nb);
2283 memcpy_fromfs (ccp_option, ptr, nb);
2285 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2288 cp = find_compressor ((int) (unsigned) (unsigned char) ccp_option[0]);
2289 if (cp != (struct compressor *) 0) {
2291 * Found a handler for the protocol - try to allocate
2292 * a compressor or decompressor.
2295 if (data.transmit) {
2296 if (ppp->sc_xc_state != NULL)
2297 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2300 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2302 if (ppp->sc_xc_state == NULL) {
2303 if (ppp->flags & SC_DEBUG)
2304 printk("ppp%ld: comp_alloc failed\n",
2305 ppp2dev (ppp)->base_addr);
2308 ppp->flags &= ~SC_COMP_RUN;
2310 if (ppp->sc_rc_state != NULL)
2311 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2313 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2314 if (ppp->sc_rc_state == NULL) {
2315 if (ppp->flags & SC_DEBUG)
2316 printk("ppp%ld: decomp_alloc failed\n",
2317 ppp2dev (ppp)->base_addr);
2320 ppp->flags &= ~SC_DECOMP_RUN;
2325 if (ppp->flags & SC_DEBUG)
2326 printk(KERN_DEBUG "ppp%ld: no compressor for [%x %x %x], %x\n",
2327 ppp2dev (ppp)->base_addr, ccp_option[0], ccp_option[1],
2329 return (-EINVAL); /* no handler found */
2333 * Process the IOCTL event for the tty device.
2337 ppp_tty_ioctl (struct tty_struct *tty, struct file *file, unsigned int param2,
2338 unsigned long param3)
2340 struct ppp *ppp = tty2ppp (tty);
2341 register int temp_i = 0;
2344 * Verify the status of the PPP device.
2349 if (ppp->magic != PPP_MAGIC)
2354 * The user must have an euid of root to do these requests.
2363 error = verify_area (VERIFY_READ, (void *) param3,
2366 temp_i = get_int_user ((int *) param3);
2367 if (ppp->flags & SC_DEBUG)
2369 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2371 if (ppp->mru != temp_i)
2372 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2379 error = verify_area (VERIFY_WRITE, (void *) param3,
2382 temp_i = (ppp->flags & SC_MASK);
2383 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2384 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2385 SC_RCV_ODDP | SC_RCV_EVNP;
2387 put_long_user ((long) temp_i, param3);
2388 if (ppp->flags & SC_DEBUG)
2390 "ppp_tty_ioctl: get flags: addr %lx flags "
2391 "%x\n", param3, temp_i);
2395 * Set the flags for the various options
2398 error = verify_area (VERIFY_READ, (void *) param3,
2401 temp_i = get_int_user (param3) & SC_MASK;
2402 temp_i |= (ppp->flags & ~SC_MASK);
2404 if ((ppp->flags & SC_CCP_OPEN) &&
2405 (temp_i & SC_CCP_OPEN) == 0)
2406 ppp_ccp_closed (ppp);
2408 if ((ppp->flags | temp_i) & SC_DEBUG)
2410 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2411 ppp->flags = temp_i;
2415 * Set the compression mode
2417 case PPPIOCSCOMPRESS:
2418 error = ppp_set_compression (ppp,
2419 (struct ppp_option_data *) param3);
2422 * Retrieve the transmit async map
2424 case PPPIOCGASYNCMAP:
2425 error = verify_area (VERIFY_WRITE, (void *) param3,
2428 put_long_user (ppp->xmit_async_map[0], param3);
2429 if (ppp->flags & SC_DEBUG)
2431 "ppp_tty_ioctl: get asyncmap: addr "
2432 "%lx asyncmap %lx\n",
2434 (unsigned long) ppp->xmit_async_map[0]);
2438 * Set the transmit async map
2440 case PPPIOCSASYNCMAP:
2441 error = verify_area (VERIFY_READ, (void *) param3,
2444 ppp->xmit_async_map[0] = get_long_user (param3);
2445 if (ppp->flags & SC_DEBUG)
2447 "ppp_tty_ioctl: set xmit asyncmap %lx\n",
2448 (unsigned long) ppp->xmit_async_map[0]);
2452 * Set the receive async map
2454 case PPPIOCSRASYNCMAP:
2455 error = verify_area (VERIFY_READ, (void *) param3,
2458 ppp->recv_async_map = get_long_user (param3);
2459 if (ppp->flags & SC_DEBUG)
2461 "ppp_tty_ioctl: set rcv asyncmap %lx\n",
2462 (unsigned long) ppp->recv_async_map);
2466 * Obtain the unit number for this device.
2469 error = verify_area (VERIFY_WRITE, (void *) param3,
2472 put_long_user (ppp2dev (ppp)->base_addr, param3);
2473 if (ppp->flags & SC_DEBUG)
2475 "ppp_tty_ioctl: get unit: %ld",
2476 ppp2dev (ppp)->base_addr);
2480 * Set the debug level
2483 error = verify_area (VERIFY_READ, (void *) param3,
2486 temp_i = (get_int_user (param3) & 0x1F) << 16;
2487 temp_i |= (ppp->flags & ~0x1F0000);
2489 if ((ppp->flags | temp_i) & SC_DEBUG)
2491 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2492 ppp->flags = temp_i;
2496 * Get the debug level
2499 error = verify_area (VERIFY_WRITE, (void *) param3,
2502 temp_i = (ppp->flags >> 16) & 0x1F;
2503 put_long_user ((long) temp_i, param3);
2505 if (ppp->flags & SC_DEBUG)
2507 "ppp_tty_ioctl: get debug level %d\n",
2512 * Get the times since the last send/receive frame operation
2515 error = verify_area (VERIFY_WRITE, (void *) param3,
2516 sizeof (struct ppp_idle));
2518 struct ppp_idle cur_ddinfo;
2519 unsigned long cur_jiffies = jiffies;
2521 /* change absolute times to relative times. */
2522 cur_ddinfo.xmit_idle = (cur_jiffies - ppp->ddinfo.xmit_idle) / HZ;
2523 cur_ddinfo.recv_idle = (cur_jiffies - ppp->ddinfo.recv_idle) / HZ;
2524 memcpy_tofs ((void *) param3, &cur_ddinfo,
2525 sizeof (cur_ddinfo));
2526 if (ppp->flags & SC_DEBUG)
2528 "ppp_tty_ioctl: read demand dial info\n");
2532 * Retrieve the extended async map
2534 case PPPIOCGXASYNCMAP:
2535 error = verify_area (VERIFY_WRITE,
2537 sizeof (ppp->xmit_async_map));
2539 memcpy_tofs ((void *) param3,
2540 ppp->xmit_async_map,
2541 sizeof (ppp->xmit_async_map));
2543 if (ppp->flags & SC_DEBUG)
2545 "ppp_tty_ioctl: get xasyncmap: addr %lx\n",
2550 * Set the async extended map
2552 case PPPIOCSXASYNCMAP:
2553 error = verify_area (VERIFY_READ, (void *) param3,
2554 sizeof (ppp->xmit_async_map));
2558 memcpy_fromfs (temp_tbl, (void *) param3,
2559 sizeof (ppp->xmit_async_map));
2560 temp_tbl[1] = 0x00000000;
2561 temp_tbl[2] &= ~0x40000000;
2562 temp_tbl[3] |= 0x60000000;
2564 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2565 (temp_tbl[4] & temp_tbl[5]) != 0 ||
2566 (temp_tbl[6] & temp_tbl[7]) != 0)
2569 memcpy (ppp->xmit_async_map, temp_tbl,
2570 sizeof (ppp->xmit_async_map));
2572 if (ppp->flags & SC_DEBUG)
2574 "ppp_tty_ioctl: set xasyncmap\n");
2579 * Set the maximum VJ header compression slot number.
2582 error = verify_area (VERIFY_READ, (void *) param3,
2585 temp_i = get_int_user (param3) + 1;
2586 if (ppp->flags & SC_DEBUG)
2588 "ppp_tty_ioctl: set maxcid to %d\n",
2590 if (ppp->slcomp != NULL)
2591 slhc_free (ppp->slcomp);
2592 ppp->slcomp = slhc_init (16, temp_i);
2594 if (ppp->slcomp == NULL) {
2595 if (ppp->flags & SC_DEBUG)
2597 "ppp: no space for compression buffers!\n");
2604 * Allow users to read, but not set, the serial port parameters
2608 error = n_tty_ioctl (tty, file, param2, param3);
2611 * All other ioctl() events will come here.
2614 if (ppp->flags & SC_DEBUG)
2616 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2620 error = -ENOIOCTLCMD;
2629 * Process the select() statement for the PPP device.
2633 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2634 struct file *filp, int sel_type, select_table * wait)
2636 struct ppp *ppp = tty2ppp (tty);
2639 * Verify the status of the PPP device.
2644 if (ppp->magic != PPP_MAGIC)
2649 * Branch on the type of select mode. A read request must lock the user
2654 if (set_bit (0, &ppp->ubuf->locked) == 0) {
2655 /* Test for the presence of data in the queue */
2656 if (ppp->ubuf->head != ppp->ubuf->tail) {
2657 clear_bit (0, &ppp->ubuf->locked);
2660 clear_bit (0, &ppp->ubuf->locked);
2661 } /* fall through */
2663 * Exceptions or read errors.
2666 /* Is this a pty link and the remote disconnected? */
2667 if (tty->flags & (1 << TTY_SLAVE_CLOSED))
2670 /* Is this a local link and the modem disconnected? */
2671 if (tty_hung_up_p (filp))
2674 select_wait (&ppp->read_wait, wait);
2678 * Write mode. A write is allowed if there is no current transmission.
2681 if (ppp->tbuf->locked != 0) {
2682 select_wait (&ppp->write_wait, wait);
2690 /*************************************************************
2692 * This routine accepts requests from the network layer
2693 * and attempts to deliver the packets.
2694 * It also includes various routines we are compelled to
2695 * have to make the network layer work (arp, etc...).
2696 *************************************************************/
2699 * Callback from the network layer when the device goes up.
2703 ppp_dev_open (struct device *dev)
2705 struct ppp *ppp = dev2ppp (dev);
2707 /* reset POINTOPOINT every time, since dev_close zaps it! */
2708 dev->flags |= IFF_POINTOPOINT;
2710 if (ppp2tty (ppp) == NULL) {
2711 if (ppp->flags & SC_DEBUG)
2713 "ppp: %s not connected to a TTY! can't go open!\n",
2718 if (ppp->flags & SC_DEBUG)
2720 "ppp: channel %s going up for IP packets!\n",
2728 * Callback from the network layer when the ppp device goes down.
2732 ppp_dev_close (struct device *dev)
2734 struct ppp *ppp = dev2ppp (dev);
2736 if (ppp2tty (ppp) == NULL) {
2737 if (ppp->flags & SC_DEBUG)
2739 "ppp: %s not connected to a TTY! can't go down!\n",
2744 * We don't do anything about the device going down. It is not important
2747 if (ppp->flags & SC_DEBUG)
2749 "ppp: channel %s going down for IP packets!\n",
2756 * IOCTL operation to read the version of the driver.
2760 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2766 * Must have write access to the buffer.
2768 result = (char *) ifr->ifr_ifru.ifru_data;
2769 len = strlen (szVersion) + 1;
2770 error = verify_area (VERIFY_WRITE, result, len);
2772 * Move the version data
2775 memcpy_tofs (result, szVersion, len);
2781 * IOCTL to read the statistics for the pppstats program.
2785 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2787 struct ppp_stats *result, temp;
2790 * Must have write access to the buffer.
2792 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2793 error = verify_area (VERIFY_WRITE,
2797 * Supply the information for the caller. First move the version data
2798 * then move the ppp stats; and finally the vj stats.
2800 memset (&temp, 0, sizeof(temp));
2801 if (error == 0 && dev->flags & IFF_UP) {
2802 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2803 if (ppp->slcomp != NULL) {
2804 temp.vj.vjs_packets = ppp->slcomp->sls_o_compressed+
2805 ppp->slcomp->sls_o_uncompressed;
2806 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2807 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
2808 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
2809 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
2810 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
2811 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2812 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
2816 * Move the data to the caller's buffer
2819 memcpy_tofs (result, &temp, sizeof (temp));
2824 * IOCTL to read the compression statistics for the pppstats program.
2828 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2830 struct ppp_comp_stats *result, temp;
2833 * Must have write access to the buffer.
2835 result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2836 error = verify_area (VERIFY_WRITE,
2840 * Supply the information for the caller.
2842 memset (&temp, 0, sizeof(temp));
2843 if (error == 0 && dev->flags & IFF_UP) {
2844 if (ppp->sc_xc_state != NULL)
2845 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2848 if (ppp->sc_rc_state != NULL)
2849 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2853 * Move the data to the caller's buffer
2856 memcpy_tofs (result, &temp, sizeof (temp));
2861 * Callback from the network layer to process the sockioctl functions.
2865 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2867 struct ppp *ppp = dev2ppp (dev);
2870 * Process the requests
2874 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2877 case SIOCGPPPCSTATS:
2878 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2882 error = ppp_dev_ioctl_version (ppp, ifr);
2893 * Send an IP frame to the remote with vj header compression.
2895 * Return 0 if frame was queued for transmission.
2896 * 1 if frame must be re-queued for later driver support.
2899 #if defined(IPX_CHANGE)
2900 #define ppp_dev_xmit_ip1 ppp_dev_xmit_ip
2904 ppp_dev_xmit_ip1 (struct device *dev, struct ppp *ppp, u_char *data)
2908 struct ppp_hdr *hdr;
2909 struct tty_struct *tty = ppp2tty (ppp);
2911 * Obtain the length from the IP header.
2913 len = ((struct iphdr *)data) -> tot_len;
2916 * Validate the tty interface
2919 if (ppp->flags & SC_DEBUG)
2921 "ppp_dev_xmit: %s not connected to a TTY!\n",
2926 * Ensure that the PPP device is still up
2928 if (!(dev->flags & IFF_UP)) {
2929 if (ppp->flags & SC_DEBUG)
2930 printk (KERN_WARNING
2931 "ppp_dev_xmit: packet sent on interface %s,"
2932 " which is down for IP\n",
2937 * Detect a change in the transfer size
2939 if (ppp->mtu != ppp2dev (ppp)->mtu) {
2940 ppp_changedmtu (ppp,
2945 * Acquire the lock on the transmission buffer. If the buffer was busy then
2946 * mark the device as busy and return "failure to send, try back later" error.
2948 if (lock_buffer (ppp->wbuf) != 0) {
2953 * Print the frame being sent
2955 if (ppp->flags & SC_LOG_OUTPKT)
2956 ppp_print_buffer ("ppp outpkt", data, len);
2958 * At this point, the buffer will be transmitted. There is no other exit.
2960 * Try to compress the header.
2962 if (ppp->flags & SC_COMP_TCP) {
2963 len = slhc_compress (ppp->slcomp, data, len,
2964 buf_base (ppp->cbuf) + PPP_HARD_HDR_LEN,
2966 (ppp->flags & SC_NO_TCP_CCID) == 0);
2968 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2969 proto = PPP_VJC_COMP;
2970 data[0] ^= SL_TYPE_COMPRESSED_TCP;
2972 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2973 proto = PPP_VJC_UNCOMP;
2974 data[0] = (data[0] & 0x0f) | 0x40;
2980 len += PPP_HARD_HDR_LEN;
2981 hdr = &((struct ppp_hdr *) data)[-1];
2983 hdr->address = PPP_ALLSTATIONS;
2984 hdr->control = PPP_UI;
2985 hdr->protocol[0] = 0;
2986 hdr->protocol[1] = proto;
2988 return ppp_dev_xmit_frame (ppp, ppp->wbuf, (u_char *) hdr, len);
2992 * This is just an interum solution until the 1.3 kernel's networking is
2993 * available. The 1.2 kernel has problems with device headers before the
2996 * This routine should be deleted, and the ppp_dev_xmit_ip1 routine called
3000 #if !defined(IPX_CHANGE)
3002 ppp_dev_xmit_ip (struct device *dev, struct ppp *ppp, u_char *data)
3004 struct ppp_hdr *hdr;
3008 len = ((struct iphdr *)data) -> tot_len;
3011 hdr = (struct ppp_hdr *) kmalloc (len + sizeof (struct ppp_hdr),
3017 memcpy (&hdr[1], data, len);
3018 answer = ppp_dev_xmit_ip1 (dev, ppp, (u_char *) &hdr[1]);
3024 #endif /* !defined(IPX_CHANGE) */
3027 * Send an IPX (or any other non-IP) frame to the remote.
3029 * Return 0 if frame was queued for transmission.
3030 * 1 if frame must be re-queued for later driver support.
3033 #if defined(IPX_CHANGE)
3034 #define ppp_dev_xmit_ipx1 ppp_dev_xmit_ipx
3037 #if defined(NEW_SKBUFF) || defined(IPX_CHANGE)
3039 ppp_dev_xmit_ipx1 (struct device *dev, struct ppp *ppp,
3040 u_char *data, int len, int proto)
3042 struct tty_struct *tty = ppp2tty (ppp);
3043 struct ppp_hdr *hdr;
3045 * Validate the tty interface
3048 if (ppp->flags & SC_DEBUG)
3050 "ppp_dev_xmit: %s not connected to a TTY!\n",
3055 * Ensure that the PPP device is still up
3057 if (!(dev->flags & IFF_UP)) {
3058 if (ppp->flags & SC_DEBUG)
3059 printk (KERN_WARNING
3060 "ppp_dev_xmit: packet sent on interface %s,"
3066 * Detect a change in the transfer size
3068 if (ppp->mtu != ppp2dev (ppp)->mtu) {
3069 ppp_changedmtu (ppp,
3074 * Acquire the lock on the transmission buffer. If the buffer was busy then
3075 * mark the device as busy and return "failure to send, try back later" error.
3077 if (lock_buffer (ppp->wbuf) != 0) {
3082 * Print the frame being sent
3084 if (ppp->flags & SC_LOG_OUTPKT)
3085 ppp_print_buffer ("ppp outpkt", data, len);
3089 len += PPP_HARD_HDR_LEN;
3090 hdr = &((struct ppp_hdr *) data)[-1];
3092 hdr->address = PPP_ALLSTATIONS;
3093 hdr->control = PPP_UI;
3094 hdr->protocol[0] = proto >> 8;
3095 hdr->protocol[1] = proto;
3097 return ppp_dev_xmit_frame (ppp, ppp->wbuf, (u_char *) hdr, len);
3101 * This is just an interum solution until the 1.3 kernel's networking is
3102 * available. The 1.2 kernel has problems with device headers before the
3105 * This routine should be deleted, and the ppp_dev_xmit_ipx1 routine called
3109 #if !defined(IPX_CHANGE)
3111 ppp_dev_xmit_ipx (struct device *dev, struct ppp *ppp,
3112 u_char *data, int len, int proto)
3114 struct ppp_hdr *hdr;
3117 hdr = (struct ppp_hdr *) kmalloc (len + sizeof (struct ppp_hdr),
3122 memcpy (&hdr[1], data, len);
3123 answer = ppp_dev_xmit_ipx1 (dev, ppp, (u_char *) &hdr[1],
3130 #endif /* !defined(IPX_CHANGE) */
3131 #endif /* defined(NEW_SKBUFF) || defined(IPX_CHANGE) */
3134 * Send a frame to the remote.
3138 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3142 struct ppp *ppp = dev2ppp (dev);
3143 struct tty_struct *tty = ppp2tty (ppp);
3145 * just a little sanity check.
3148 if (ppp->flags & SC_DEBUG)
3149 printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3153 * Avoid timing problem should tty hangup while data is queued to be sent
3156 dev_kfree_skb (skb, FREE_WRITE);
3161 * Validate the tty linkage
3163 if (ppp->flags & SC_DEBUG)
3164 printk (KERN_DEBUG "ppp_dev_xmit [%s]: skb %p\n",
3167 * Validate the tty interface
3170 if (ppp->flags & SC_DEBUG)
3172 "ppp_dev_xmit: %s not connected to a TTY!\n",
3174 dev_kfree_skb (skb, FREE_WRITE);
3178 * Fetch the pointer to the data
3181 data = skb_data(skb);
3183 * Look at the protocol in the skb to determine the difference between
3184 * an IP frame and an IPX frame.
3187 #if defined(NEW_SKBUFF) || defined(IPX_CHANGE)
3188 switch (ntohs (skb->protocol)) {
3190 answer = ppp_dev_xmit_ipx (dev, ppp, data, len, PPP_IPX);
3194 answer = ppp_dev_xmit_ip (dev, ppp, data);
3197 default: /* All others have no support at this time. */
3198 #if 1 /* I **REALLY** want to toss this. For the time being, I'll assume
3199 that this is IP. However, if you start to see the message below
3200 then you should fix the skb->protocol to have the proper values. */
3203 "ppp: strange protocol type %x in ppp_dev_xmit\n",
3205 answer = ppp_dev_xmit_ip (dev, ppp, data);
3207 #else /* Shortly, this is what it will be! */
3208 dev_kfree_skb (skb, FREE_WRITE);
3212 #else /* defined(NEW_SKBUFF) || defined(IPX_CHANGE) */
3213 answer = ppp_dev_xmit_ip (dev, ppp, data);
3217 * This is the end of the transmission. Release the buffer if it was sent.
3220 dev_kfree_skb (skb, FREE_WRITE);
3221 ppp->ddinfo.xmit_idle = jiffies;
3227 * Generate the statistic information for the /proc/net/dev listing.
3230 static struct enet_statistics *
3231 ppp_dev_stats (struct device *dev)
3233 struct ppp *ppp = dev2ppp (dev);
3234 static struct enet_statistics ppp_stats;
3236 ppp_stats.rx_packets = ppp->stats.ppp_ipackets;
3237 ppp_stats.rx_errors = ppp->stats.ppp_ierrors;
3238 ppp_stats.rx_dropped = ppp->stats.ppp_ierrors;
3239 ppp_stats.rx_fifo_errors = 0;
3240 ppp_stats.rx_length_errors = 0;
3241 ppp_stats.rx_over_errors = 0;
3242 ppp_stats.rx_crc_errors = 0;
3243 ppp_stats.rx_frame_errors = 0;
3244 ppp_stats.tx_packets = ppp->stats.ppp_opackets;
3245 ppp_stats.tx_errors = ppp->stats.ppp_oerrors;
3246 ppp_stats.tx_dropped = 0;
3247 ppp_stats.tx_fifo_errors = 0;
3248 ppp_stats.collisions = 0;
3249 ppp_stats.tx_carrier_errors = 0;
3250 ppp_stats.tx_aborted_errors = 0;
3251 ppp_stats.tx_window_errors = 0;
3252 ppp_stats.tx_heartbeat_errors = 0;
3254 if (ppp->flags & SC_DEBUG)
3255 printk (KERN_INFO "ppp_dev_stats called");
3259 #if defined(NEW_SKBUFF)
3261 * This defines the protocol layer which is blank since the
3262 * driver does all the cooking.
3265 static int ppp_dev_input (struct protocol *self, struct protocol *lower,
3266 sk_buff *skb, void *saddr, void *daddr)
3268 return protocol_pass_demultiplex(self, NULL, skb, NULL, NULL);
3271 static int ppp_dev_output (struct protocol *self, sk_buff *skb, int type,
3272 int subid, void *saddr, void *daddr, void *opt)
3276 printk("ppp_dev_output: No device.\n");
3277 kfree_skb(skb, FREE_WRITE);
3280 dev_queue_xmit(skb, skb->dev, skb->priority);
3284 static int ppp_dev_getkey(int protocol, int subid, unsigned char *key)
3288 case htons (ETH_P_IP):
3289 case htons (ETH_P_IPX):
3296 return -EAFNOSUPPORT;
3301 #if USE_SKB_PROTOCOL == 0
3303 * Called to enquire about the type of the frame in the buffer. Return
3304 * ETH_P_IP for an IP frame, ETH_P_IPX for an IPX frame.
3307 static unsigned short
3308 ppp_dev_type (sk_buff *skb, struct device *dev)
3310 return (htons (ETH_P_IP));
3314 #if USE_SKB_PROTOCOL == 0
3315 static int ppp_dev_header (unsigned char *buff, struct device *dev,
3316 unsigned short type, void *daddr, void *saddr,
3317 unsigned len, struct sk_buff *skb)
3319 static int ppp_dev_header (sk_buff *skb, struct device *dev,
3320 unsigned short type, void *daddr,
3321 void *saddr, unsigned len)
3328 ppp_dev_rebuild (void *buff, struct device *dev, unsigned long raddr,
3335 /*************************************************************
3337 * Miscellany called by various functions above.
3338 *************************************************************/
3340 /* allocate or create a PPP channel */
3350 /* try to find an free device */
3355 ppp = ctl2ppp (ctl);
3356 if (!set_bit(0, &ppp->inuse))
3359 if (++if_num == max_dev)
3363 * There are no available items. Allocate a device from the system pool
3365 ctl = (ppp_ctrl_t *) kmalloc (sizeof(ppp_ctrl_t), GFP_KERNEL);
3367 (void) memset(ctl, 0, sizeof(ppp_ctrl_t));
3368 ppp = ctl2ppp (ctl);
3369 dev = ctl2dev (ctl);
3371 /* initialize channel control data */
3372 set_bit(0, &ppp->inuse);
3379 dev->init = ppp_init_dev;
3380 dev->name = ctl->name;
3381 dev->base_addr = (unsigned long) if_num;
3382 dev->priv = (void *) ppp;
3384 sprintf (dev->name, "ppp%d", if_num);
3386 /* link in the new channel */
3387 ctl->next = ppp_list;
3390 /* register device so that we can be ifconfig'd */
3391 /* ppp_init_dev() will be called as a side-effect */
3393 status = register_netdev (dev);
3395 printk (KERN_INFO "registered device %s\n", dev->name);
3400 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3402 /* This one will forever be busy as it is not initialized */
3408 * Utility procedures to print a buffer in hex/ascii
3412 ppp_print_hex (register u_char * out, const u_char * in, int count)
3414 register u_char next_ch;
3415 static char hex[] = "0123456789ABCDEF";
3417 while (count-- > 0) {
3419 *out++ = hex[(next_ch >> 4) & 0x0F];
3420 *out++ = hex[next_ch & 0x0F];
3426 ppp_print_char (register u_char * out, const u_char * in, int count)
3428 register u_char next_ch;
3430 while (count-- > 0) {
3433 if (next_ch < 0x20 || next_ch > 0x7e)
3437 if (next_ch == '%') /* printk/syslogd has a bug !! */
3445 ppp_print_buffer (const u_char * name, const u_char * buf, int count)
3449 if (name != (u_char *) NULL)
3450 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3453 memset (line, 32, 44);
3454 ppp_print_hex (line, buf, 8);
3455 ppp_print_char (&line[8 * 3], buf, 8);
3456 printk (KERN_DEBUG "%s\n", line);
3462 memset (line, 32, 44);
3463 ppp_print_hex (line, buf, count);
3464 ppp_print_char (&line[8 * 3], buf, count);
3465 printk (KERN_DEBUG "%s\n", line);
3469 /*************************************************************
3470 * Compressor module interface
3471 *************************************************************/
3473 struct compressor_link {
3474 struct compressor_link *next;
3475 struct compressor *comp;
3478 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3480 static struct compressor *find_compressor (int type)
3482 struct compressor_link *lnk;
3483 unsigned long flags;
3488 lnk = ppp_compressors;
3489 while (lnk != (struct compressor_link *) 0) {
3490 if ((int) (unsigned char) lnk->comp->compress_proto == type) {
3491 restore_flags(flags);
3497 restore_flags(flags);
3498 return (struct compressor *) 0;
3501 static int ppp_register_compressor (struct compressor *cp)
3503 struct compressor_link *new;
3504 unsigned long flags;
3506 new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3508 if (new == (struct compressor_link *) 0)
3514 if (find_compressor (cp->compress_proto)) {
3515 restore_flags(flags);
3520 new->next = ppp_compressors;
3522 ppp_compressors = new;
3524 restore_flags(flags);
3528 static void ppp_unregister_compressor (struct compressor *cp)
3530 struct compressor_link *prev = (struct compressor_link *) 0;
3531 struct compressor_link *lnk;
3532 unsigned long flags;
3537 lnk = ppp_compressors;
3538 while (lnk != (struct compressor_link *) 0) {
3539 if (lnk->comp == cp) {
3541 prev->next = lnk->next;
3543 ppp_compressors = lnk->next;
3550 restore_flags(flags);
3553 /*************************************************************
3554 * Module support routines
3555 *************************************************************/
3563 /* register our line disciplines */
3564 status = ppp_first_time();
3567 "PPP: ppp_init() failure %d\n", status);
3569 (void) register_symtab (&ppp_syms);
3574 cleanup_module(void)
3577 ppp_ctrl_t *ctl, *next_ctl;
3582 * Ensure that the devices are not in operation.
3586 ppp = ctl2ppp (ctl);
3587 if (ppp->inuse && ppp->tty != NULL) {
3592 dev = ctl2dev (ctl);
3593 if (dev->start || dev->flags & IFF_UP) {
3600 * Ensure that there are no compressor modules registered
3602 if (ppp_compressors != NULL)
3607 "PPP: device busy, remove delayed\n");
3611 * Release the tty registration of the line dicipline so that no new entries
3614 status = tty_register_ldisc (N_PPP, NULL);
3617 "PPP: Unable to unregister ppp line discipline "
3618 "(err = %d)\n", status);
3621 "PPP: ppp line discipline successfully unregistered\n");
3623 * De-register the devices so that there is no problem with them
3625 next_ctl = ppp_list;
3628 next_ctl = ctl->next;
3629 ppp = ctl2ppp (ctl);
3630 dev = ctl2dev (ctl);
3633 unregister_netdev (dev);