3 * Michael Callahan <callahan@maths.ox.ac.uk>
4 * Al Longyear <longyear@netcom.com>
6 * Dynamic PPP devices by Jim Freeman <jfree@caldera.com>.
7 * ppp_tty_receive ``noisy-raise-bug'' fixed by Ove Ewerlid <ewerlid@syscon.uu.se>
9 * ==FILEVERSION 970227==
11 * NOTE TO MAINTAINERS:
12 * If you modify this file at all, please set the number above to the
13 * date of the modification as YYMMDD (year month day).
14 * ppp.c is shipped with a PPP distribution as well as with the kernel;
15 * if everyone increases the FILEVERSION number above, then scripts
16 * can do the right thing when deciding whether to install a new ppp.c
17 * file. Don't change the format of that line otherwise, so the
18 * installation script can recognize it.
26 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
27 Multi-protocol Datagrams over Point-to-Point Links
33 Flags for this module (any combination is acceptable for testing.):
35 OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
36 character. This is normally set to ((HZ * 3) / 2).
37 This is 1.5 seconds. If zero then the leading
40 CHECK_CHARACTERS - Enable the checking on all received characters for
41 8 data bits, no parity. This adds a small amount of
42 processing for each received character.
45 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
47 #define CHECK_CHARACTERS 1
48 #define PPP_COMPRESS 1
51 #define PPP_MAX_DEV 256
54 /* $Id: ppp.c,v 1.10 1997/03/04 03:29:58 paulus Exp $
55 * Added dynamic allocation of channels to eliminate
56 * compiled-in limits on the number of channels.
58 * Dynamic channel allocation code Copyright 1995 Caldera, Inc.,
59 * released under the GNU General Public License Version 2.
62 #include <linux/version.h>
63 #include <linux/module.h>
64 #include <linux/kernel.h>
65 #include <linux/sched.h>
66 #include <linux/types.h>
67 #include <linux/fcntl.h>
68 #include <linux/interrupt.h>
69 #include <linux/ptrace.h>
72 /* a nice define to generate linux version numbers */
73 #define VERSION(major,minor,patch) (((((major)<<8)+(minor))<<8)+(patch))
75 #if LINUX_VERSION_CODE < VERSION(2,1,14)
76 #include <linux/ioport.h>
80 #include <linux/malloc.h>
81 #include <linux/tty.h>
82 #include <linux/errno.h>
83 #include <linux/sched.h> /* to get the struct task_struct */
84 #include <linux/string.h> /* used in new tty drivers */
85 #include <linux/signal.h> /* used in new tty drivers */
86 #include <asm/system.h>
87 #include <asm/bitops.h>
89 #include <linux/if_ether.h>
90 #include <linux/netdevice.h>
91 #include <linux/skbuff.h>
92 #include <linux/inet.h>
93 #include <linux/ioctl.h>
95 typedef struct sk_buff sk_buff;
96 #define skb_data(skb) ((__u8 *) (skb)->data)
99 #include <linux/tcp.h>
100 #include <linux/if_arp.h>
101 #include <net/slhc_vj.h>
103 #define fcstab ppp_crc16_table /* Name of the table in the kernel */
104 #include <linux/ppp_defs.h>
106 #include <linux/socket.h>
107 #include <linux/if_ppp.h>
108 #include <linux/if_pppvar.h>
109 #include <linux/ppp-comp.h>
112 #define PPP_IPX 0x2b /* IPX protocol over PPP */
116 #define PPP_LQR 0xc025 /* Link Quality Reporting Protocol */
119 #if LINUX_VERSION_CODE >= VERSION(2,1,4)
120 #include <asm/segment.h>
121 #define GET_USER(error,value,addr) error = get_user(value,addr)
122 #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
123 #define PUT_USER(error,value,addr) error = put_user(value,addr)
124 #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
126 #if LINUX_VERSION_CODE >= VERSION(2,1,5)
127 #include <asm/uaccess.h>
130 #else /* 2.0.x and 2.1.x before 2.1.4 */
132 #define GET_USER(error,value,addr) \
134 error = verify_area (VERIFY_READ, (void *) addr, sizeof (value)); \
136 value = get_user(addr); \
139 #define COPY_FROM_USER(error,dest,src,size) \
141 error = verify_area (VERIFY_READ, (void *) src, size); \
143 memcpy_fromfs (dest, src, size); \
146 #define PUT_USER(error,value,addr) \
148 error = verify_area (VERIFY_WRITE, (void *) addr, sizeof (value)); \
150 put_user (value, addr); \
153 #define COPY_TO_USER(error,dest,src,size) \
155 error = verify_area (VERIFY_WRITE, (void *) src, size); \
157 memcpy_tofs (dest, src, size); \
162 static int ppp_register_compressor (struct compressor *cp);
163 static void ppp_unregister_compressor (struct compressor *cp);
169 static struct compressor *find_compressor (int type);
170 static void ppp_init_ctrl_blk (register struct ppp *);
171 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
172 static int ppp_doframe (struct ppp *);
173 static struct ppp *ppp_alloc (void);
174 static struct ppp *ppp_find (int pid_value);
175 static void ppp_print_buffer (const __u8 *, const __u8 *, int);
176 extern inline void ppp_stuff_char (struct ppp *ppp,
177 register struct ppp_buffer *buf,
179 extern inline int lock_buffer (register struct ppp_buffer *buf);
181 static int rcv_proto_ip (struct ppp *, __u16, __u8 *, int);
182 static int rcv_proto_ipx (struct ppp *, __u16, __u8 *, int);
183 static int rcv_proto_vjc_comp (struct ppp *, __u16, __u8 *, int);
184 static int rcv_proto_vjc_uncomp (struct ppp *, __u16, __u8 *, int);
185 static int rcv_proto_unknown (struct ppp *, __u16, __u8 *, int);
186 static int rcv_proto_lqr (struct ppp *, __u16, __u8 *, int);
187 static void ppp_doframe_lower (struct ppp *, __u8 *, int);
188 static int ppp_doframe (struct ppp *);
190 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd);
191 static int rcv_proto_ccp (struct ppp *, __u16, __u8 *, int);
193 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (__u8)(c))
195 #ifndef OPTIMIZE_FLAG_TIME
196 #define OPTIMIZE_FLAG_TIME 0
200 #define PPP_MAX_DEV 256
204 * Parameters which may be changed via insmod.
207 static int flag_time = OPTIMIZE_FLAG_TIME;
208 static int max_dev = PPP_MAX_DEV;
211 * The "main" procedure to the ppp device
214 int ppp_init (struct device *);
217 * Network device driver callback routines
220 static int ppp_dev_open (struct device *);
221 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
222 static int ppp_dev_close (struct device *);
223 static int ppp_dev_xmit (sk_buff *, struct device *);
224 static struct enet_statistics *ppp_dev_stats (struct device *);
226 #if LINUX_VERSION_CODE < VERSION(2,1,15)
227 static int ppp_dev_header (sk_buff *, struct device *, __u16,
228 void *, void *, unsigned int);
229 static int ppp_dev_rebuild (void *eth, struct device *dev,
230 unsigned long raddr, struct sk_buff *skb);
237 static int ppp_tty_read (struct tty_struct *, struct file *, __u8 *,
239 static int ppp_tty_write (struct tty_struct *, struct file *, const __u8 *,
241 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
243 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
244 struct file *filp, int sel_type, select_table * wait);
245 static int ppp_tty_open (struct tty_struct *);
246 static void ppp_tty_close (struct tty_struct *);
247 static int ppp_tty_room (struct tty_struct *tty);
248 static void ppp_tty_receive (struct tty_struct *tty, const __u8 * cp,
249 char *fp, int count);
250 static void ppp_tty_wakeup (struct tty_struct *tty);
252 #define CHECK_PPP(a) if (!ppp->inuse) { printk (ppp_warning, __LINE__); return a;}
253 #define CHECK_PPP_VOID() if (!ppp->inuse) { printk (ppp_warning, __LINE__); return;}
255 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
256 #define in_rmap(ppp,c) ((((unsigned int) (__u8) (c)) < 0x20) && \
257 ppp->recv_async_map & (1 << (c)))
259 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
261 #define tty2ppp(tty) ((struct ppp *) (tty->disc_data))
262 #define dev2ppp(dev) ((struct ppp *) (dev->priv))
263 #define ppp2tty(ppp) ((struct tty_struct *) ppp->tty)
264 #define ppp2dev(ppp) ((struct device *) ppp->dev)
272 #define PPP_HARD_HDR_LEN (sizeof (struct ppp_hdr))
274 typedef struct ppp_ctrl {
275 struct ppp_ctrl *next; /* Next structure in the list */
276 char name [8]; /* Name of the device */
277 struct ppp ppp; /* PPP control table */
278 struct device dev; /* Device information table */
281 static ppp_ctrl_t *ppp_list = NULL;
283 #define ctl2ppp(ctl) (struct ppp *) &ctl->ppp
284 #define ctl2dev(ctl) (struct device *) &ctl->dev
288 #define BUFFER_TYPE_DEV_RD 0 /* ppp read buffer */
289 #define BUFFER_TYPE_TTY_WR 1 /* tty write buffer */
290 #define BUFFER_TYPE_DEV_WR 2 /* ppp write buffer */
291 #define BUFFER_TYPE_TTY_RD 3 /* tty read buffer */
292 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
294 /* Define this string only once for all macro invocations */
295 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
297 static char szVersion[] = PPP_VERSION;
300 * Information for the protocol decoder
303 typedef int (*pfn_proto) (struct ppp *, __u16, __u8 *, int);
305 typedef struct ppp_proto_struct {
311 ppp_proto_type proto_list[] = {
312 { PPP_IP, rcv_proto_ip },
313 { PPP_IPX, rcv_proto_ipx },
314 { PPP_VJC_COMP, rcv_proto_vjc_comp },
315 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
316 { PPP_LQR, rcv_proto_lqr },
317 { PPP_CCP, rcv_proto_ccp },
318 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
321 __u16 ppp_crc16_table[256] =
323 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
324 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
325 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
326 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
327 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
328 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
329 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
330 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
331 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
332 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
333 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
334 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
335 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
336 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
337 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
338 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
339 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
340 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
341 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
342 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
343 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
344 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
345 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
346 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
347 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
348 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
349 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
350 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
351 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
352 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
353 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
354 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
357 #ifdef CHECK_CHARACTERS
358 static __u32 paritytab[8] =
360 0x96696996, 0x69969669, 0x69969669, 0x96696996,
361 0x69969669, 0x96696996, 0x96696996, 0x69969669
365 /* local function to store a value into the LQR frame */
366 extern inline __u8 * store_long (register __u8 *p, register int value) {
367 *p++ = (__u8) (value >> 24);
368 *p++ = (__u8) (value >> 16);
369 *p++ = (__u8) (value >> 8);
374 /*************************************************************
376 *************************************************************/
378 /* This procedure is called once and once only to define who we are to
379 * the operating system and the various procedures that it may use in
380 * accessing the ppp protocol.
384 ppp_first_time (void)
386 static struct tty_ldisc ppp_ldisc;
390 "PPP: version %s (demand dialling)"
393 #ifndef MODULE /* slhc module logic has its own copyright announcement */
395 "TCP compression code copyright 1989 Regents of the "
396 "University of California\n");
400 "PPP Dynamic channel allocation code copyright 1995 "
403 * Register the tty discipline
405 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
406 ppp_ldisc.magic = TTY_LDISC_MAGIC;
407 ppp_ldisc.open = ppp_tty_open;
408 ppp_ldisc.close = ppp_tty_close;
409 ppp_ldisc.read = ppp_tty_read;
410 ppp_ldisc.write = ppp_tty_write;
411 ppp_ldisc.ioctl = ppp_tty_ioctl;
412 ppp_ldisc.select = ppp_tty_select;
413 ppp_ldisc.receive_room = ppp_tty_room;
414 ppp_ldisc.receive_buf = ppp_tty_receive;
415 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
417 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
419 printk (KERN_INFO "PPP line discipline registered.\n");
421 printk (KERN_ERR "error registering line discipline: %d\n",
426 /*************************************************************
428 *************************************************************/
430 /* called when the device is actually created */
433 ppp_init_dev (struct device *dev)
437 #if LINUX_VERSION_CODE < VERSION(2,1,15)
438 dev->hard_header = ppp_dev_header;
439 dev->rebuild_header = ppp_dev_rebuild;
442 dev->hard_header_len = PPP_HARD_HDR_LEN;
446 dev->hard_start_xmit = ppp_dev_xmit;
447 dev->open = ppp_dev_open;
448 dev->stop = ppp_dev_close;
449 dev->get_stats = ppp_dev_stats;
450 dev->do_ioctl = ppp_dev_ioctl;
452 dev->tx_queue_len = 10;
453 dev->type = ARPHRD_PPP;
455 for (indx = 0; indx < DEV_NUMBUFFS; indx++)
456 skb_queue_head_init (&dev->buffs[indx]);
458 /* New-style flags */
459 dev->flags = IFF_POINTOPOINT;
460 dev->family = AF_INET;
464 dev->pa_alen = 4; /* sizeof (__u32) */
470 * Local procedure to initialize the ppp structure
474 ppp_init_ctrl_blk (register struct ppp *ppp)
476 ppp->magic = PPP_MAGIC;
484 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
485 ppp->xmit_async_map[0] = 0xffffffff;
486 ppp->xmit_async_map[3] = 0x60000000;
487 ppp->recv_async_map = 0x00000000;
494 ppp->read_wait = NULL;
495 ppp->write_wait = NULL;
496 ppp->last_xmit = jiffies - flag_time;
498 /* clear statistics */
499 memset(&ppp->stats, 0, sizeof (struct pppstat));
500 memset(&ppp->estats, 0, sizeof(struct enet_statistics));
502 /* Reset the demand dial information */
503 ppp->ddinfo.xmit_idle= /* time since last NP packet sent */
504 ppp->ddinfo.recv_idle=jiffies; /* time since last NP packet received */
506 /* PPP compression data */
508 ppp->sc_rc_state = NULL;
511 #if LINUX_VERSION_CODE < VERSION(2,1,18)
512 static struct symbol_table ppp_syms = {
513 #include <linux/symtab_begin.h>
514 X(ppp_register_compressor),
515 X(ppp_unregister_compressor),
517 #include <linux/symtab_end.h>
520 EXPORT_SYMBOL(ppp_register_compressor);
521 EXPORT_SYMBOL(ppp_unregister_compressor);
522 EXPORT_SYMBOL(ppp_crc16_table);
525 /* called at boot/load time for each ppp device defined in the kernel */
529 ppp_init (struct device *dev)
531 static int first_time = 1;
536 answer = ppp_first_time();
537 #if LINUX_VERSION_CODE < VERSION(2,1,18)
539 (void) register_symtab (&ppp_syms);
549 * Routine to allocate a buffer for later use by the driver.
552 static struct ppp_buffer *
553 ppp_alloc_buf (int size, int type)
555 struct ppp_buffer *buf;
557 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
561 buf->size = size - 1; /* Mask for the buffer size */
567 buf->fcs = PPP_INITFCS;
574 * Routine to release the allocated buffer.
578 ppp_free_buf (struct ppp_buffer *ptr)
585 * Lock the indicated transmit buffer
589 lock_buffer (register struct ppp_buffer *buf)
594 * Save the current state and if free then set it to the "busy" state
602 restore_flags (flags);
607 * MTU has been changed by the IP layer. Unfortunately we are not told
608 * about this, but we spot it ourselves and fix things up. We could be
609 * in an upcall from the tty driver, or in an ip packet queue.
613 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
617 struct ppp_buffer *new_rbuf;
618 struct ppp_buffer *new_wbuf;
619 struct ppp_buffer *new_cbuf;
620 struct ppp_buffer *new_tbuf;
622 struct ppp_buffer *old_rbuf;
623 struct ppp_buffer *old_wbuf;
624 struct ppp_buffer *old_cbuf;
625 struct ppp_buffer *old_tbuf;
629 * Allocate the buffer from the kernel for the data
633 /* allow for possible escaping of every character */
634 mtu = (new_mtu * 2) + 20;
636 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
642 new_wbuf = ppp_alloc_buf (mtu+PPP_HARD_HDR_LEN, BUFFER_TYPE_DEV_WR);
643 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
644 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
645 new_cbuf = ppp_alloc_buf (mru+PPP_HARD_HDR_LEN, BUFFER_TYPE_VJ);
647 * If the buffers failed to allocate then complain and release the partial
650 if (new_wbuf == NULL || new_tbuf == NULL ||
651 new_rbuf == NULL || new_cbuf == NULL) {
652 if (ppp->flags & SC_DEBUG)
654 "ppp: failed to allocate new buffers\n");
656 ppp_free_buf (new_wbuf);
657 ppp_free_buf (new_tbuf);
658 ppp_free_buf (new_rbuf);
659 ppp_free_buf (new_cbuf);
663 * Update the pointers to the new buffer structures.
666 old_wbuf = ppp->wbuf;
667 old_rbuf = ppp->rbuf;
668 old_cbuf = ppp->cbuf;
669 old_tbuf = ppp->tbuf;
671 ppp->wbuf = new_wbuf;
672 ppp->rbuf = new_rbuf;
673 ppp->cbuf = new_cbuf;
674 ppp->tbuf = new_tbuf;
676 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
678 dev->mem_start = (unsigned long) buf_base (new_wbuf);
679 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
680 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
681 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
683 * Update the parameters for the new buffer sizes
685 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
686 ppp->escape = 0; /* No pending escape character */
696 ppp->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
697 ppp->flags &= ~SC_XMIT_BUSY;
701 * Release old buffer pointers
703 ppp_free_buf (old_rbuf);
704 ppp_free_buf (old_wbuf);
705 ppp_free_buf (old_cbuf);
706 ppp_free_buf (old_tbuf);
711 * CCP is down; free (de)compressor state if necessary.
715 ppp_ccp_closed (struct ppp *ppp)
717 if (ppp->sc_xc_state) {
718 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
719 ppp->sc_xc_state = NULL;
722 if (ppp->sc_rc_state) {
723 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
724 ppp->sc_rc_state = NULL;
729 * Called to release all of the information in the current PPP structure.
731 * It is called when the ppp device goes down or if it is unable to go
736 ppp_release (struct ppp *ppp)
738 struct tty_struct *tty;
744 ppp_ccp_closed (ppp);
746 /* Ensure that the pppd process is not hanging on select() */
747 wake_up_interruptible (&ppp->read_wait);
748 wake_up_interruptible (&ppp->write_wait);
750 if (tty != NULL && tty->disc_data == ppp)
751 tty->disc_data = NULL; /* Break the tty->ppp link */
753 if (dev && dev->flags & IFF_UP) {
754 dev_close (dev); /* close the device properly */
755 dev->flags = 0; /* prevent recursion */
758 ppp_free_buf (ppp->rbuf);
759 ppp_free_buf (ppp->wbuf);
760 ppp_free_buf (ppp->cbuf);
761 ppp_free_buf (ppp->ubuf);
762 ppp_free_buf (ppp->tbuf);
774 slhc_free (ppp->slcomp);
780 ppp->backup_tty = NULL;
786 * Called when the line discipline is changed to something
787 * else, the tty is closed, or the tty detects a hangup.
791 ppp_tty_close (struct tty_struct *tty)
793 struct ppp *ppp = tty2ppp (tty);
796 if (ppp->magic != PPP_MAGIC) {
797 if (ppp->flags & SC_DEBUG)
799 "ppp: trying to close unopened tty!\n");
803 tty->disc_data = NULL;
804 if (tty == ppp->backup_tty)
808 if (ppp->backup_tty) {
809 ppp->tty = ppp->backup_tty;
812 if (ppp->flags & SC_DEBUG)
813 printk (KERN_INFO "ppp: channel %s closing.\n",
824 * Called when the tty discipline is switched to PPP.
828 ppp_tty_open (struct tty_struct *tty)
830 struct ppp *ppp = tty2ppp (tty);
833 * There should not be an existing table for this slot.
836 if (ppp->flags & SC_DEBUG)
838 "ppp_tty_open: gack! tty already associated to %s!\n",
839 ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
844 * Allocate the structure from the system
846 ppp = ppp_find(current->pid);
849 * If we are taking over a ppp unit which is currently
850 * connected to a loopback pty, there's not much to do.
853 tty->disc_data = ppp;
858 if (ppp->flags & SC_DEBUG)
859 printk (KERN_ERR "ppp_alloc failed\n");
863 * Initialize the control block
865 ppp_init_ctrl_blk (ppp);
867 tty->disc_data = ppp;
869 * Allocate space for the default VJ header compression slots
871 ppp->slcomp = slhc_init (16, 16);
872 if (ppp->slcomp == NULL) {
873 if (ppp->flags & SC_DEBUG)
874 printk (KERN_ERR "ppp_tty_open: "
875 "no space for compression buffers!\n");
880 * Allocate space for the MTU and MRU buffers
882 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
887 * Allocate space for a user level buffer
889 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
890 if (ppp->ubuf == NULL) {
891 if (ppp->flags & SC_DEBUG)
892 printk (KERN_ERR "ppp_tty_open: "
893 "no space for user receive buffer\n");
898 if (ppp->flags & SC_DEBUG)
899 printk (KERN_INFO "ppp: channel %s open\n",
902 for (indx = 0; indx < NUM_NP; ++indx)
903 ppp->sc_npmode[indx] = NPMODE_PASS;
908 * Flush any pending characters in the driver and discipline.
910 if (tty->ldisc.flush_buffer)
911 tty->ldisc.flush_buffer (tty);
913 if (tty->driver.flush_buffer)
914 tty->driver.flush_buffer (tty);
919 * Local function to send the next portion of the buffer.
921 * Called by the tty driver's tty_wakeup function should it be entered
922 * because the partial buffer was transmitted.
924 * Called by kick_tty to send the initial portion of the buffer.
926 * Completion processing of the buffer transmission is handled here.
930 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
931 struct ppp_buffer *xbuf)
933 register int count, actual;
935 * Prevent re-entrancy by ensuring that this routine is called only once.
938 if (ppp->flags & SC_XMIT_BUSY) {
942 ppp->flags |= SC_XMIT_BUSY;
945 * Send the next block of data to the modem
947 count = xbuf->count - xbuf->tail;
948 actual = tty->driver.write (tty, 0,
949 buf_base (xbuf) + xbuf->tail, count);
951 * Terminate transmission of any block which may have an error.
952 * This could occur should the carrier drop.
955 ppp->stats.ppp_oerrors++;
958 ppp->bytes_sent += actual;
960 * If the buffer has been transmitted then clear the indicators.
962 xbuf->tail += actual;
963 if (actual == count) {
965 ppp->flags &= ~SC_XMIT_BUSY;
967 * Complete the transmission on the current buffer.
971 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
975 * If the completed buffer came from the device write, then complete the
976 * transmission block.
978 if (ppp2dev (ppp) -> flags & IFF_UP) {
979 if (xbuf->type == BUFFER_TYPE_DEV_WR)
980 ppp2dev (ppp)->tbusy = 0;
984 * Wake up the transmission queue for all completion events.
986 wake_up_interruptible (&ppp->write_wait);
988 * Look at the priorities. Choose a daemon write over the device driver.
999 * If there is a pending buffer then transmit it now.
1002 ppp->flags &= ~SC_XMIT_BUSY;
1003 ppp_kick_tty (ppp, xbuf);
1009 * Clear the re-entry flag
1011 ppp->flags &= ~SC_XMIT_BUSY;
1015 * This function is called by the tty driver when the transmit buffer has
1016 * additional space. It is used by the ppp code to continue to transmit
1017 * the current buffer should the buffer have been partially sent.
1019 * In addition, it is used to send the first part of the buffer since the
1020 * logic and the inter-locking would be identical.
1024 ppp_tty_wakeup (struct tty_struct *tty)
1026 struct ppp_buffer *xbuf;
1027 struct ppp *ppp = tty2ppp (tty);
1032 if (ppp->magic != PPP_MAGIC)
1035 if (tty != ppp->tty) {
1036 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1040 * Ensure that there is a transmission pending. Clear the re-entry flag if
1041 * there is no pending buffer. Otherwise, send the buffer.
1045 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1047 ppp_tty_wakeup_code (ppp, tty, xbuf);
1051 * This function is called to transmit a buffer to the remote. The buffer
1052 * is placed on the pending queue if there is presently a buffer being
1053 * sent or it is transmitted with the aid of ppp_tty_wakeup.
1057 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
1059 unsigned long flags;
1066 * Control the flags which are best performed with the interrupts masked.
1071 * If the transmitter is busy then place the buffer on the appropriate
1074 if (ppp->xbuf != NULL) {
1075 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1079 restore_flags (flags);
1083 * If the transmitter is not busy then this is the highest priority frame
1085 ppp->flags &= ~SC_XMIT_BUSY;
1086 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1088 restore_flags (flags);
1090 * Do the "tty wakeup_code" to actually send this buffer.
1092 ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1095 /*************************************************************
1097 * The following functions handle input that arrives from
1098 * the TTY. It recognizes PPP frames and either hands them
1099 * to the network layer or queues them for delivery to a
1100 * user process reading this TTY.
1101 *************************************************************/
1104 * Callback function from tty driver. Return the amount of space left
1105 * in the receiver's buffer to decide if remote transmitter is to be
1110 ppp_tty_room (struct tty_struct *tty)
1112 return 65536; /* We can handle an infinite amount of data. :-) */
1116 * Callback function when data is available at the tty driver.
1119 ppp_tty_receive (struct tty_struct *tty, const __u8 * data,
1120 char *flags, int count)
1122 register struct ppp *ppp = tty2ppp (tty);
1123 register struct ppp_buffer *buf = NULL;
1127 * This can happen if stuff comes in on the backup tty.
1129 if (tty != ppp->tty)
1132 * Fetch the pointer to the buffer. Be careful about race conditions.
1140 * Verify the table pointer and ensure that the line is
1141 * still in PPP discipline.
1143 if (ppp->magic != PPP_MAGIC) {
1144 if (ppp->flags & SC_DEBUG)
1146 "PPP: handler called but couldn't find "
1152 * Print the buffer if desired
1154 if (ppp->flags & SC_LOG_RAWIN)
1155 ppp_print_buffer ("receive buffer", data, count);
1157 * Collect the character and error condition for the character. Set the toss
1158 * flag for the first character error.
1160 while (count-- > 0) {
1164 if (*flags && ppp->toss == 0) {
1166 switch (ppp->toss) {
1168 ++ppp->estats.rx_fifo_errors;
1172 ++ppp->estats.rx_frame_errors;
1179 * Set the flags for d7 being 0/1 and parity being even/odd so that
1180 * the normal processing would have all flags set at the end of the
1181 * session. A missing flag bit indicates an error condition.
1184 #ifdef CHECK_CHARACTERS
1186 ppp->flags |= SC_RCV_B7_1;
1188 ppp->flags |= SC_RCV_B7_0;
1190 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1191 ppp->flags |= SC_RCV_ODDP;
1193 ppp->flags |= SC_RCV_EVNP;
1196 * Branch on the character.
1200 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1201 * then the block is to be ignored. In addition, characters before the very
1202 * first FLAG are also tossed by this procedure.
1204 case PPP_FLAG: /* PPP_FLAG: end of frame */
1205 ppp->stats.ppp_ibytes += ppp->rbuf->count;
1209 * Process frames which are not to be ignored. If the processing failed,
1210 * then clean up the VJ tables.
1212 if (ppp_doframe (ppp) == 0) {
1213 ++ppp->stats.ppp_ierrors;
1214 slhc_toss (ppp->slcomp);
1217 * Reset all indicators for the new frame to follow.
1220 buf->fcs = PPP_INITFCS;
1225 * All other characters in the data come here. If the character is in the
1226 * receive mask then ignore the character.
1229 /* If we're tossing, look no further. */
1233 /* If this is a control char to be ignored, do so */
1234 if (in_rmap (ppp, chr))
1238 * Modify the next character if preceded by escape.
1239 * The escape character (0x7d) could be an escaped
1240 * 0x5d, if it follows an escape :-)
1245 } else if (chr == PPP_ESCAPE) {
1246 ppp->escape = PPP_TRANS;
1251 * If the count sent is within reason then store the character, bump the
1252 * count, and update the FCS for the character.
1254 if (buf->count < buf->size) {
1255 buf_base (buf)[buf->count++] = chr;
1256 buf->fcs = PPP_FCS (buf->fcs, chr);
1260 * The peer sent too much data. Set the flags to discard the current frame
1261 * and wait for the re-synchronization FLAG to be sent.
1263 ++ppp->estats.rx_length_errors;
1271 * Put the input frame into the networking system for the indicated protocol
1275 ppp_rcv_rx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1277 sk_buff *skb = dev_alloc_skb (count);
1279 * Generate a skb buffer for the new frame.
1282 if (ppp->flags & SC_DEBUG)
1284 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1285 ppp2dev (ppp)->name);
1289 * Move the received data from the input buffer to the skb buffer.
1291 skb->dev = ppp2dev (ppp); /* We are the device */
1292 skb->protocol = proto;
1293 skb->mac.raw = skb_data(skb);
1294 memcpy (skb_put(skb,count), data, count); /* move data */
1296 * Tag the frame and kick it to the proper receive routine
1298 #if LINUX_VERSION_CODE < VERSION(2,1,15)
1302 ppp->ddinfo.recv_idle = jiffies;
1308 * Process the receipt of an IP frame
1312 rcv_proto_ip (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1314 if ((ppp2dev (ppp)->flags & IFF_UP) && (count > 0))
1315 if (ppp->sc_npmode[NP_IP] == NPMODE_PASS)
1316 return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1321 * Process the receipt of an IPX frame
1325 rcv_proto_ipx (struct ppp *ppp, __u16 proto, __u8 * data, int count)
1327 if (((ppp2dev (ppp)->flags & IFF_UP) != 0) && (count > 0))
1328 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1333 * Process the receipt of an VJ Compressed frame
1337 rcv_proto_vjc_comp (struct ppp *ppp, __u16 proto,
1338 __u8 *data, int count)
1340 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1341 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1342 if (new_count >= 0) {
1343 return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1345 if (ppp->flags & SC_DEBUG)
1347 "ppp: error in VJ decompression\n");
1353 * Process the receipt of an VJ Un-compressed frame
1357 rcv_proto_vjc_uncomp (struct ppp *ppp, __u16 proto,
1358 __u8 *data, int count)
1360 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1361 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1362 return rcv_proto_ip (ppp, PPP_IP, data, count);
1364 if (ppp->flags & SC_DEBUG)
1366 "ppp: error in VJ memorizing\n");
1372 * Receive all unclassified protocols.
1376 rcv_proto_unknown (struct ppp *ppp, __u16 proto,
1377 __u8 *data, int len)
1380 register int current_idx;
1384 buf_base (ppp->ubuf) [current_idx++] = (__u8) (c); \
1385 current_idx &= ppp->ubuf->size; \
1386 if (current_idx == ppp->ubuf->tail) \
1391 * The total length includes the protocol data.
1392 * Lock the user information buffer.
1394 if (set_bit (0, &ppp->ubuf->locked)) {
1395 if (ppp->flags & SC_DEBUG)
1397 "ppp_us_queue: can't get lock\n");
1399 current_idx = ppp->ubuf->head;
1401 * Insert the buffer length (not counted), the protocol, and the data
1411 while (totlen-- > 0) {
1416 * The frame is complete. Update the head pointer and wakeup the pppd
1419 ppp->ubuf->head = current_idx;
1421 clear_bit (0, &ppp->ubuf->locked);
1422 wake_up_interruptible (&ppp->read_wait);
1423 if (ppp->tty->fasync != NULL)
1424 kill_fasync (ppp->tty->fasync, SIGIO);
1428 * The buffer is full. Unlock the header
1431 clear_bit (0, &ppp->ubuf->locked);
1432 if (ppp->flags & SC_DEBUG)
1434 "ppp_us_queue: ran out of buffer space.\n");
1437 * Discard the frame. There are no takers for this protocol.
1439 if (ppp->flags & SC_DEBUG)
1441 "ppp: dropping packet on the floor.\n");
1442 slhc_toss (ppp->slcomp);
1447 * Handle a CCP packet.
1449 * The CCP packet is passed along to the pppd process just like any
1450 * other PPP frame. The difference is that some processing needs to be
1451 * immediate or the compressors will become confused on the peer.
1454 static void ppp_proto_ccp (struct ppp *ppp, __u8 *dp, int len, int rcvd)
1456 int slen = CCP_LENGTH(dp);
1457 __u8 *opt = dp + CCP_HDRLEN;
1458 int opt_len = slen - CCP_HDRLEN;
1463 switch (CCP_CODE(dp)) {
1468 * CCP must be going down - disable compression
1470 if (ppp->flags & SC_CCP_UP) {
1471 ppp->flags &= ~(SC_CCP_UP |
1478 if ((ppp->flags & SC_CCP_OPEN) == 0)
1480 if (ppp->flags & SC_CCP_UP)
1482 if (slen < (CCP_HDRLEN + CCP_OPT_MINLEN))
1484 if (slen < (CCP_OPT_LENGTH (opt) + CCP_HDRLEN))
1487 * we're agreeing to send compressed packets.
1490 if (ppp->sc_xc_state == NULL)
1493 if ((*ppp->sc_xcomp->comp_init)
1497 ppp2dev (ppp)->base_addr,
1499 ppp->flags & SC_DEBUG))
1500 ppp->flags |= SC_COMP_RUN;
1504 * peer is agreeing to send compressed packets.
1506 if (ppp->sc_rc_state == NULL)
1509 if ((*ppp->sc_rcomp->decomp_init)
1513 ppp2dev (ppp)->base_addr,
1516 ppp->flags & SC_DEBUG)) {
1517 ppp->flags |= SC_DECOMP_RUN;
1518 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1522 * The protocol sequence is complete at this end
1525 if ((ppp->flags & SC_CCP_UP) == 0)
1529 if (ppp->sc_xc_state && (ppp->flags & SC_COMP_RUN))
1530 (*ppp->sc_xcomp->comp_reset)(ppp->sc_xc_state);
1532 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1533 (*ppp->sc_rcomp->decomp_reset)(ppp->sc_rc_state);
1534 ppp->flags &= ~SC_DC_ERROR;
1542 rcv_proto_ccp (struct ppp *ppp, __u16 proto, __u8 *dp, int len)
1544 ppp_proto_ccp (ppp, dp, len, 1);
1545 return rcv_proto_unknown (ppp, proto, dp, len);
1549 * Handle a LQR packet.
1553 rcv_proto_lqr (struct ppp *ppp, __u16 proto, __u8 * data, int len)
1555 return rcv_proto_unknown (ppp, proto, data, len);
1558 static void ppp_doframe_lower (struct ppp *ppp, __u8 *data, int count)
1560 __u16 proto = PPP_PROTOCOL (data);
1561 ppp_proto_type *proto_ptr;
1563 * Ignore empty frames
1565 if (count <= PPP_HDRLEN)
1568 * Count the frame and print it
1570 ++ppp->stats.ppp_ipackets;
1571 if (ppp->flags & SC_LOG_INPKT)
1572 ppp_print_buffer ("receive frame", data, count);
1574 * Find the procedure to handle this protocol. The last one is marked
1575 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1577 proto_ptr = proto_list;
1578 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1581 * Update the appropriate statistic counter.
1583 if ((*proto_ptr->func) (ppp, proto,
1584 &data[PPP_HARD_HDR_LEN],
1585 count - PPP_HARD_HDR_LEN))
1586 ppp->stats.ppp_ioctects += count;
1588 ++ppp->stats.ppp_discards;
1591 /* on entry, a received frame is in ppp->rbuf.bufr
1592 check it and dispose as appropriate */
1595 ppp_doframe (struct ppp *ppp)
1597 __u8 *data = buf_base (ppp->rbuf);
1598 int count = ppp->rbuf->count;
1599 int addr, ctrl, proto;
1603 * If there is a pending error from the receiver then log it and discard
1604 * the damaged frame.
1607 if ((ppp->flags & SC_DEBUG) && count > 0)
1609 "ppp_toss: tossing frame, reason = %x\n",
1614 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1615 * follows each frame.
1620 * Generate an error if the frame is too small.
1622 if (count < PPP_HARD_HDR_LEN) {
1623 if (ppp->flags & SC_DEBUG)
1625 "ppp: got runt ppp frame, %d chars\n", count);
1626 ++ppp->estats.rx_length_errors;
1630 * Verify the CRC of the frame and discard the CRC characters from the
1631 * end of the buffer.
1633 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1634 if (ppp->flags & SC_DEBUG) {
1636 "ppp: frame with bad fcs, length = %d\n",
1638 ppp_print_buffer("bad frame", data, count);
1640 ++ppp->estats.rx_crc_errors;
1643 count -= 2; /* ignore the fcs characters */
1645 * Ignore the leading ADDRESS and CONTROL fields in the frame.
1647 addr = PPP_ALLSTATIONS;
1650 if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1655 * Obtain the protocol from the frame
1657 proto = (__u16) *data++;
1658 if ((proto & 1) == 0) {
1659 proto = (proto << 8) | (__u16) *data++;
1663 * Rewrite the header with the full information. This may encroach upon
1664 * the 'filler' area in the buffer header. This is the purpose for the
1668 *(--data) = proto >> 8;
1673 * Process the active decompressor.
1675 if ((ppp->sc_rc_state != (void *) 0) &&
1676 (ppp->flags & SC_DECOMP_RUN) &&
1677 ((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0)) {
1678 if (proto == PPP_COMP) {
1680 * If the frame is compressed then decompress it.
1682 new_data = kmalloc (ppp->mru + PPP_HDRLEN, GFP_ATOMIC);
1683 if (new_data == NULL) {
1684 if (ppp->flags & SC_DEBUG)
1686 "ppp_doframe: no memory\n");
1687 new_count = DECOMP_ERROR;
1689 new_count = (*ppp->sc_rcomp->decompress)
1690 (ppp->sc_rc_state, data, count,
1691 new_data, ppp->mru + PPP_HDRLEN);
1693 switch (new_count) {
1695 ppp_doframe_lower (ppp, new_data, new_count);
1700 ppp->flags |= SC_DC_ERROR;
1703 case DECOMP_FATALERROR:
1704 ppp->flags |= SC_DC_FERROR;
1705 if (ppp->flags & SC_DEBUG)
1706 printk(KERN_ERR "ppp: fatal decomp error\n");
1710 * Log the error condition and discard the frame.
1714 slhc_toss (ppp->slcomp);
1715 ++ppp->stats.ppp_ierrors;
1718 * The frame is not special. Pass it through the compressor without
1719 * actually compressing the data
1721 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1726 * Process the uncompressed frame.
1728 ppp_doframe_lower (ppp, data, count);
1732 /*************************************************************
1733 * LINE DISCIPLINE SUPPORT
1734 * The following functions form support user programs
1735 * which read and write data on a TTY with the PPP line
1736 * discipline. Reading is done from a circular queue,
1737 * filled by the lower TTY levels.
1738 *************************************************************/
1740 /* read a PPP frame from the us_rbuff circular buffer,
1741 waiting if necessary
1745 ppp_tty_read (struct tty_struct *tty, struct file *file, __u8 * buf,
1748 struct ppp *ppp = tty2ppp (tty);
1755 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1756 ppp->ubuf->tail &= ppp->ubuf->size; \
1760 * Validate the pointers
1765 if (ppp->magic != PPP_MAGIC)
1770 * Acquire the read lock.
1773 ppp = tty2ppp (tty);
1774 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
1778 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1779 if (ppp->flags & SC_DEBUG)
1781 "ppp_tty_read: sleeping(ubuf)\n");
1783 current->timeout = 0;
1784 current->state = TASK_INTERRUPTIBLE;
1787 if (current->signal & ~current->blocked)
1792 * Before we attempt to write the frame to the user, ensure that the
1793 * user has access to the pages for the total buffer length.
1795 indx = verify_area (VERIFY_WRITE, buf, nr);
1799 * Fetch the length of the buffer from the first two bytes.
1801 if (ppp->ubuf->head == ppp->ubuf->tail)
1810 * If there is no length then wait for the data to arrive.
1814 clear_bit (0, &ppp->ubuf->locked);
1815 if (file->f_flags & O_NONBLOCK)
1817 current->timeout = 0;
1819 if (ppp->flags & SC_DEBUG)
1821 "ppp_tty_read: sleeping(read_wait)\n");
1823 interruptible_sleep_on (&ppp->read_wait);
1824 if (current->signal & ~current->blocked)
1829 * Ensure that the frame will fit within the caller's buffer. If not, then
1830 * discard the frame from the input buffer.
1833 /* Can't copy it, update us_rbuff_head */
1835 if (ppp->flags & SC_DEBUG)
1837 "ppp: read of %u bytes too small for %d "
1838 "frame\n", nr, len + 2);
1839 ppp->ubuf->tail += len;
1840 ppp->ubuf->tail &= ppp->ubuf->size;
1841 clear_bit (0, &ppp->ubuf->locked);
1842 ppp->stats.ppp_ierrors++;
1846 * Before we attempt to write the frame to the user, ensure that the
1847 * page tables are proper.
1849 indx = verify_area (VERIFY_WRITE, buf, len + 2);
1851 ppp->ubuf->tail += len;
1852 ppp->ubuf->tail &= ppp->ubuf->size;
1853 clear_bit (0, &ppp->ubuf->locked);
1857 * Fake the insertion of the ADDRESS and CONTROL information because these
1858 * were not saved in the buffer.
1860 PUT_USER (error, (u_char) PPP_ALLSTATIONS, buf);
1862 PUT_USER (error, (u_char) PPP_UI, buf);
1867 * Copy the received data from the buffer to the caller's area.
1869 while (indx-- > 0) {
1871 PUT_USER (error, c, buf);
1875 clear_bit (0, &ppp->ubuf->locked);
1876 len += 2; /* Account for ADDRESS and CONTROL bytes */
1877 if (ppp->flags & SC_DEBUG)
1879 "ppp_tty_read: passing %d bytes up\n", len);
1885 /* stuff a character into the transmit buffer, using PPP's way of escaping
1887 also, update fcs to take account of new character */
1890 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1894 * The buffer should not be full.
1896 if (ppp->flags & SC_DEBUG) {
1897 if ((buf->count < 0) || (buf->count > 3000))
1898 printk (KERN_DEBUG "ppp_stuff_char: %d %x\n",
1899 (unsigned int) buf->count,
1900 (unsigned int) chr);
1903 * Update the FCS and if the character needs to be escaped, do it.
1905 buf->fcs = PPP_FCS (buf->fcs, chr);
1906 if (in_xmap (ppp, chr)) {
1908 ins_char (buf, PPP_ESCAPE);
1911 * Add the character to the buffer.
1913 ins_char (buf, chr);
1917 * Procedure to encode the data with the proper escaping and send the
1918 * data to the remote system.
1922 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
1923 __u8 *data, int count, int non_ip)
1926 int address, control;
1929 ++ppp->stats.ppp_opackets;
1930 ppp->stats.ppp_ooctects += count;
1933 * Insert the leading FLAG character
1937 if (non_ip || flag_time == 0)
1938 ins_char (buf, PPP_FLAG);
1940 if (jiffies - ppp->last_xmit > flag_time)
1941 ins_char (buf, PPP_FLAG);
1943 ppp->last_xmit = jiffies;
1944 buf->fcs = PPP_INITFCS;
1946 * Emit the address/control information if needed
1948 address = PPP_ADDRESS (data);
1949 control = PPP_CONTROL (data);
1950 proto = PPP_PROTOCOL (data);
1952 if (address != PPP_ALLSTATIONS ||
1953 control != PPP_UI ||
1954 (ppp->flags & SC_COMP_AC) == 0) {
1955 ppp_stuff_char (ppp, buf, address);
1956 ppp_stuff_char (ppp, buf, control);
1959 * Emit the protocol (compressed if possible)
1961 if ((ppp->flags & SC_COMP_PROT) == 0 || (proto & 0xFF00))
1962 ppp_stuff_char (ppp, buf, proto >> 8);
1964 ppp_stuff_char (ppp, buf, proto);
1972 ppp_stuff_char (ppp, buf, *data++);
1974 * Add the trailing CRC and the final flag character
1976 write_fcs = buf->fcs ^ 0xFFFF;
1977 ppp_stuff_char (ppp, buf, write_fcs);
1978 ppp_stuff_char (ppp, buf, write_fcs >> 8);
1980 * Add the trailing flag character
1982 ins_char (buf, PPP_FLAG);
1986 if (ppp->flags & SC_LOG_FLUSH)
1987 ppp_print_buffer ("ppp flush", buf_base (buf),
1990 * Send the block to the tty driver.
1992 ppp->stats.ppp_obytes += buf->count;
1993 ppp_kick_tty (ppp, buf);
1997 * Send an frame to the remote with the proper bsd compression.
1999 * Return 0 if frame was queued for transmission.
2000 * 1 if frame must be re-queued for later driver support.
2004 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
2005 __u8 *data, int count)
2008 int address, control;
2014 if (ppp->flags & SC_LOG_OUTPKT)
2015 ppp_print_buffer ("write frame", data, count);
2017 * Determine if the frame may be compressed. Attempt to compress the
2018 * frame if possible.
2020 proto = PPP_PROTOCOL (data);
2021 address = PPP_ADDRESS (data);
2022 control = PPP_CONTROL (data);
2024 if (((ppp->flags & SC_COMP_RUN) != 0) &&
2025 (ppp->sc_xc_state != (void *) 0) &&
2026 (address == PPP_ALLSTATIONS) &&
2027 (control == PPP_UI) &&
2028 (proto != PPP_LCP) &&
2029 (proto != PPP_CCP)) {
2030 new_data = kmalloc (count, GFP_ATOMIC);
2031 if (new_data == NULL) {
2032 if (ppp->flags & SC_DEBUG)
2034 "ppp_dev_xmit_frame: no memory\n");
2038 new_count = (*ppp->sc_xcomp->compress)
2039 (ppp->sc_xc_state, data, new_data, count, count);
2041 if (new_count > 0 && (ppp->flags & SC_CCP_UP)) {
2042 ppp_dev_xmit_lower (ppp, buf, new_data, new_count, 0);
2047 * The frame could not be compressed, or it could not be sent in
2048 * compressed form because CCP is not yet up.
2053 * Go to the escape encoding
2055 ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2060 * Revise the tty frame for specific protocols.
2064 send_revise_frame (register struct ppp *ppp, __u8 *data, int len)
2068 switch (PPP_PROTOCOL (data)) {
2070 * Update the LQR frame with the current MIB information. This saves having
2071 * the daemon read old MIB data from the driver.
2074 len = 48; /* total size of this frame */
2075 p = (__u8 *) &data [40]; /* Point to last two items. */
2076 p = store_long (p, ppp->stats.ppp_opackets + 1);
2077 p = store_long (p, ppp->stats.ppp_ooctects + len);
2080 * Outbound compression frames
2084 data + PPP_HARD_HDR_LEN,
2085 len - PPP_HARD_HDR_LEN,
2097 * write a frame with NR chars from BUF to TTY
2098 * we have to put the FCS field on ourselves
2102 ppp_tty_write (struct tty_struct *tty, struct file *file, const __u8 * data,
2105 struct ppp *ppp = tty2ppp (tty);
2109 * Verify the pointers.
2114 if (ppp->magic != PPP_MAGIC)
2119 * Ensure that the caller does not wish to send too much.
2121 if (count > PPP_MTU + PPP_HDRLEN) {
2122 if (ppp->flags & SC_DEBUG)
2123 printk (KERN_WARNING
2124 "ppp_tty_write: truncating user packet "
2125 "from %u to mtu %d\n", count,
2126 PPP_MTU + PPP_HDRLEN);
2127 count = PPP_MTU + PPP_HDRLEN;
2130 * Allocate a buffer for the data and fetch it from the user space.
2132 new_data = kmalloc (count, GFP_KERNEL);
2133 if (new_data == NULL) {
2134 if (ppp->flags & SC_DEBUG)
2136 "ppp_tty_write: no memory\n");
2140 * lock this PPP unit so we will be the only writer;
2141 * sleep if necessary
2143 while (lock_buffer (ppp->tbuf) != 0) {
2144 current->timeout = 0;
2145 if (ppp->flags & SC_DEBUG)
2146 printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2147 interruptible_sleep_on (&ppp->write_wait);
2149 ppp = tty2ppp (tty);
2150 if (!ppp || ppp->magic != PPP_MAGIC || !ppp->inuse
2151 || tty != ppp->tty) {
2156 if (current->signal & ~current->blocked) {
2162 * Retrieve the user's buffer
2164 COPY_FROM_USER (status,
2171 ppp->tbuf->locked = 0;
2175 * Change the LQR frame
2177 count = send_revise_frame (ppp, new_data, count);
2181 ppp_dev_xmit_frame (ppp, ppp->tbuf, new_data, count);
2187 * Process the BSD compression IOCTL event for the tty device.
2191 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2193 struct compressor *cp;
2194 struct ppp_option_data data;
2198 __u8 ccp_option[CCP_MAX_OPTION_LENGTH];
2200 * Fetch the compression parameters
2202 COPY_FROM_USER (error,
2212 if ((__u32) nb >= (__u32)CCP_MAX_OPTION_LENGTH)
2213 nb = CCP_MAX_OPTION_LENGTH;
2215 COPY_FROM_USER (error,
2223 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2226 cp = find_compressor ((int) (unsigned int) (__u8) ccp_option[0]);
2227 if (cp != (struct compressor *) 0) {
2229 * Found a handler for the protocol - try to allocate
2230 * a compressor or decompressor.
2233 if (data.transmit) {
2234 if (ppp->sc_xc_state != NULL)
2235 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2238 ppp->sc_xc_state = cp->comp_alloc(ccp_option, nb);
2240 if (ppp->sc_xc_state == NULL) {
2241 if (ppp->flags & SC_DEBUG)
2242 printk("ppp%ld: comp_alloc failed\n",
2243 ppp2dev (ppp)->base_addr);
2246 ppp->flags &= ~SC_COMP_RUN;
2248 if (ppp->sc_rc_state != NULL)
2249 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2251 ppp->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
2252 if (ppp->sc_rc_state == NULL) {
2253 if (ppp->flags & SC_DEBUG)
2254 printk("ppp%ld: decomp_alloc failed\n",
2255 ppp2dev (ppp)->base_addr);
2258 ppp->flags &= ~SC_DECOMP_RUN;
2263 if (ppp->flags & SC_DEBUG)
2264 printk(KERN_DEBUG "ppp%ld: no compressor for [%x %x %x], %x\n",
2265 ppp2dev (ppp)->base_addr, ccp_option[0], ccp_option[1],
2267 return (-EINVAL); /* no handler found */
2271 * Process the IOCTL event for the tty device.
2275 ppp_tty_ioctl (struct tty_struct *tty, struct file * file,
2276 unsigned int param2, unsigned long param3)
2278 struct ppp *ppp = tty2ppp (tty);
2279 register int temp_i = 0;
2282 * Verify the status of the PPP device.
2287 if (ppp->magic != PPP_MAGIC)
2292 * The user must have an euid of root to do these requests.
2301 GET_USER (error, temp_i, (int *) param3);
2303 if (ppp->flags & SC_DEBUG)
2305 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2307 if (ppp->mru != temp_i)
2308 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2315 temp_i = (ppp->flags & SC_MASK);
2316 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
2317 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
2318 SC_RCV_ODDP | SC_RCV_EVNP;
2320 PUT_USER (error, temp_i, (int *) param3);
2323 * Set the flags for the various options
2326 GET_USER (error, temp_i, (int *) param3);
2329 temp_i |= (ppp->flags & ~SC_MASK);
2331 if ((ppp->flags & SC_CCP_OPEN) &&
2332 (temp_i & SC_CCP_OPEN) == 0)
2333 ppp_ccp_closed (ppp);
2335 if ((ppp->flags | temp_i) & SC_DEBUG)
2337 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2338 ppp->flags = temp_i;
2342 * Set the compression mode
2344 case PPPIOCSCOMPRESS:
2345 error = ppp_set_compression (ppp,
2346 (struct ppp_option_data *) param3);
2349 * Retrieve the transmit async map
2351 case PPPIOCGASYNCMAP:
2352 PUT_USER (error, ppp->xmit_async_map[0], (int *) param3);
2355 * Set the transmit async map
2357 case PPPIOCSASYNCMAP:
2358 GET_USER (error, temp_i, (int *) param3);
2360 ppp->xmit_async_map[0] = temp_i;
2361 if (ppp->flags & SC_DEBUG)
2363 "ppp_tty_ioctl: set xmit asyncmap %x\n",
2364 ppp->xmit_async_map[0]);
2368 * Set the receive async map
2370 case PPPIOCSRASYNCMAP:
2371 GET_USER (error, temp_i, (int *) param3);
2373 ppp->recv_async_map = temp_i;
2374 if (ppp->flags & SC_DEBUG)
2376 "ppp_tty_ioctl: set rcv asyncmap %x\n",
2377 ppp->recv_async_map);
2381 * Obtain the unit number for this device.
2384 PUT_USER (error, ppp2dev (ppp)->base_addr, (int *) param3);
2386 if (ppp->flags & SC_DEBUG)
2388 "ppp_tty_ioctl: get unit: %ld\n",
2389 ppp2dev (ppp)->base_addr);
2393 * Set the debug level
2396 GET_USER (error, temp_i, (int *) param3);
2398 temp_i = (temp_i & 0x1F) << 16;
2399 temp_i |= (ppp->flags & ~0x1F0000);
2401 if ((ppp->flags | temp_i) & SC_DEBUG)
2403 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2404 ppp->flags = temp_i;
2408 * Get the debug level
2411 temp_i = (ppp->flags >> 16) & 0x1F;
2412 PUT_USER (error, temp_i, (int *) param3);
2415 * Get the times since the last send/receive frame operation
2419 struct ppp_idle cur_ddinfo;
2420 __u32 cur_jiffies = jiffies;
2422 /* change absolute times to relative times. */
2423 cur_ddinfo.xmit_idle = (cur_jiffies - ppp->ddinfo.xmit_idle) / HZ;
2424 cur_ddinfo.recv_idle = (cur_jiffies - ppp->ddinfo.recv_idle) / HZ;
2425 COPY_TO_USER (error,
2428 sizeof (cur_ddinfo));
2432 * Retrieve the extended async map
2434 case PPPIOCGXASYNCMAP:
2435 COPY_TO_USER (error,
2437 ppp->xmit_async_map,
2438 sizeof (ppp->xmit_async_map));
2441 * Set the async extended map
2443 case PPPIOCSXASYNCMAP:
2447 COPY_FROM_USER (error,
2453 temp_tbl[1] = 0x00000000;
2454 temp_tbl[2] &= ~0x40000000;
2455 temp_tbl[3] |= 0x60000000;
2457 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2458 (temp_tbl[4] & temp_tbl[5]) != 0 ||
2459 (temp_tbl[6] & temp_tbl[7]) != 0)
2462 memcpy (ppp->xmit_async_map,
2464 sizeof (ppp->xmit_async_map));
2466 if (ppp->flags & SC_DEBUG)
2468 "ppp_tty_ioctl: set xasyncmap\n");
2474 * Set the maximum VJ header compression slot number.
2477 GET_USER (error, temp_i, (int *) param3);
2479 temp_i = (temp_i & 255) + 1;
2480 if (ppp->flags & SC_DEBUG)
2482 "ppp_tty_ioctl: set maxcid to %d\n",
2484 if (ppp->slcomp != NULL)
2485 slhc_free (ppp->slcomp);
2486 ppp->slcomp = slhc_init (16, temp_i);
2488 if (ppp->slcomp == NULL) {
2489 if (ppp->flags & SC_DEBUG)
2491 "ppp: no space for compression buffers!\n");
2498 case PPPIOCXFERUNIT:
2499 ppp->backup_tty = tty;
2500 ppp->sc_xfer = current->pid;
2507 COPY_FROM_USER (error,
2515 switch (npi.protocol) {
2517 npi.protocol = NP_IP;
2520 if (ppp->flags & SC_DEBUG)
2521 printk(KERN_DEBUG "pppioc[gs]npmode: "
2522 "invalid proto %d\n", npi.protocol);
2529 if (param2 == PPPIOCGNPMODE) {
2530 npi.mode = ppp->sc_npmode[npi.protocol];
2532 COPY_TO_USER (error,
2539 if (npi.mode != ppp->sc_npmode[npi.protocol]) {
2540 ppp->sc_npmode[npi.protocol] = npi.mode;
2541 if (npi.mode != NPMODE_QUEUE) {
2542 /* ppp_requeue(ppp); maybe needed */
2543 ppp_tty_wakeup (ppp2tty(ppp));
2549 * Allow users to read, but not set, the serial port parameters
2553 error = n_tty_ioctl (tty, file, param2, param3);
2558 int count = ppp->ubuf->tail - ppp->ubuf->head;
2560 count += (ppp->ubuf->size + 1);
2561 PUT_USER (error, count, (int *) param3);
2565 * All other ioctl() events will come here.
2568 if (ppp->flags & SC_DEBUG)
2570 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2574 error = -ENOIOCTLCMD;
2583 * Process the select() statement for the PPP device.
2587 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2588 struct file *filp, int sel_type, select_table * wait)
2590 struct ppp *ppp = tty2ppp (tty);
2593 * Verify the status of the PPP device.
2598 if (ppp->magic != PPP_MAGIC || tty != ppp->tty)
2603 * Branch on the type of select mode. A read request must lock the user
2608 if (set_bit (0, &ppp->ubuf->locked) == 0) {
2609 /* Test for the presence of data in the queue */
2610 if (ppp->ubuf->head != ppp->ubuf->tail) {
2611 clear_bit (0, &ppp->ubuf->locked);
2614 clear_bit (0, &ppp->ubuf->locked);
2615 } /* fall through */
2617 * Exceptions or read errors.
2620 /* Is this a pty link and the remote disconnected? */
2621 if (tty->flags & (1 << TTY_OTHER_CLOSED))
2624 /* Is this a local link and the modem disconnected? */
2625 if (tty_hung_up_p (filp))
2628 select_wait (&ppp->read_wait, wait);
2632 * Write mode. A write is allowed if there is no current transmission.
2635 if (ppp->tbuf->locked != 0) {
2636 select_wait (&ppp->write_wait, wait);
2644 /*************************************************************
2646 * This routine accepts requests from the network layer
2647 * and attempts to deliver the packets.
2648 * It also includes various routines we are compelled to
2649 * have to make the network layer work (arp, etc...).
2650 *************************************************************/
2653 * Callback from the network layer when the device goes up.
2657 ppp_dev_open (struct device *dev)
2659 struct ppp *ppp = dev2ppp (dev);
2661 /* reset POINTOPOINT every time, since dev_close zaps it! */
2662 dev->flags |= IFF_POINTOPOINT;
2664 if (ppp2tty (ppp) == NULL) {
2665 if (ppp->flags & SC_DEBUG)
2667 "ppp: %s not connected to a TTY! can't go open!\n",
2672 if (ppp->flags & SC_DEBUG)
2674 "ppp: channel %s going up for IP packets!\n",
2682 * Callback from the network layer when the ppp device goes down.
2686 ppp_dev_close (struct device *dev)
2688 struct ppp *ppp = dev2ppp (dev);
2690 if (ppp2tty (ppp) == NULL) {
2694 * We don't do anything about the device going down. It is not important
2697 if (ppp->flags & SC_DEBUG)
2699 "ppp: channel %s going down for IP packets!\n",
2706 * IOCTL operation to read the version of the driver.
2710 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2713 char *result = (char *) ifr->ifr_ifru.ifru_data;
2714 int len = strlen (szVersion) + 1;
2716 * Move the version data
2718 COPY_TO_USER (error,
2727 * IOCTL to read the statistics for the pppstats program.
2731 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2733 struct ppp_stats *result, temp;
2736 * Supply the information for the caller. First move the version data
2737 * then move the ppp stats; and finally the vj stats.
2739 memset (&temp, 0, sizeof(temp));
2740 if (dev->flags & IFF_UP) {
2741 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2742 if (ppp->slcomp != NULL) {
2743 temp.vj.vjs_packets = ppp->slcomp->sls_o_compressed+
2744 ppp->slcomp->sls_o_uncompressed;
2745 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2746 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
2747 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
2748 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
2749 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
2750 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2751 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
2755 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2757 COPY_TO_USER (error,
2766 * IOCTL to read the compression statistics for the pppstats program.
2770 ppp_dev_ioctl_comp_stats (struct ppp *ppp, struct ifreq *ifr, struct device *dev)
2772 struct ppp_comp_stats *result, temp;
2775 * Supply the information for the caller.
2777 memset (&temp, 0, sizeof(temp));
2778 if (error == 0 && dev->flags & IFF_UP) {
2779 if (ppp->sc_xc_state != NULL)
2780 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2783 if (ppp->sc_rc_state != NULL)
2784 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2788 * Move the data to the caller's buffer
2790 result = (struct ppp_comp_stats *) ifr->ifr_ifru.ifru_data;
2792 COPY_TO_USER (error,
2801 * Callback from the network layer to process the sockioctl functions.
2805 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2807 struct ppp *ppp = dev2ppp (dev);
2810 * Process the requests
2814 error = ppp_dev_ioctl_stats (ppp, ifr, dev);
2817 case SIOCGPPPCSTATS:
2818 error = ppp_dev_ioctl_comp_stats (ppp, ifr, dev);
2822 error = ppp_dev_ioctl_version (ppp, ifr);
2833 * Send an IP frame to the remote with vj header compression.
2835 * Return 0 if frame was queued for transmission.
2836 * 1 if frame must be re-queued for later driver support.
2840 ppp_dev_xmit_ip (struct device *dev, struct ppp *ppp, __u8 *data)
2844 struct ppp_hdr *hdr;
2845 struct tty_struct *tty = ppp2tty (ppp);
2847 * Obtain the length from the IP header.
2849 len = ((struct iphdr *)data) -> tot_len;
2852 * Validate the tty interface
2855 if (ppp->flags & SC_DEBUG)
2857 "ppp_dev_xmit: %s not connected to a TTY!\n",
2862 * Ensure that the PPP device is still up
2864 if (!(dev->flags & IFF_UP)) {
2865 if (ppp->flags & SC_DEBUG)
2866 printk (KERN_WARNING
2867 "ppp_dev_xmit: packet sent on interface %s,"
2868 " which is down for IP\n",
2873 * Branch on the type of processing for the IP frame.
2875 switch (ppp->sc_npmode[NP_IP]) {
2881 if (ppp->flags & SC_DEBUG)
2883 "ppp_dev_xmit: npmode = %d on %s\n",
2884 ppp->sc_npmode[NP_IP], dev->name);
2891 if (ppp->flags & SC_DEBUG)
2892 printk (KERN_WARNING
2893 "ppp_dev_xmit: unknown npmode %d on %s\n",
2894 ppp->sc_npmode[NP_IP],
2899 * Detect a change in the transfer size
2901 if (ppp->mtu != ppp2dev (ppp)->mtu) {
2902 ppp_changedmtu (ppp,
2907 * Acquire the lock on the transmission buffer. If the buffer was busy then
2908 * mark the device as busy.
2910 if (lock_buffer (ppp->wbuf) != 0) {
2915 * Print the frame being sent
2917 if (ppp->flags & SC_LOG_OUTPKT)
2918 ppp_print_buffer ("ppp outpkt", data, len);
2920 * At this point, the buffer will be transmitted. There is no other exit.
2922 * Try to compress the header.
2924 if (ppp->flags & SC_COMP_TCP) {
2925 len = slhc_compress (ppp->slcomp, data, len,
2926 buf_base (ppp->cbuf) + PPP_HARD_HDR_LEN,
2928 (ppp->flags & SC_NO_TCP_CCID) == 0);
2930 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2931 proto = PPP_VJC_COMP;
2932 data[0] ^= SL_TYPE_COMPRESSED_TCP;
2934 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2935 proto = PPP_VJC_UNCOMP;
2936 data[0] = (data[0] & 0x0f) | 0x40;
2942 len += PPP_HARD_HDR_LEN;
2943 hdr = &((struct ppp_hdr *) data)[-1];
2945 hdr->address = PPP_ALLSTATIONS;
2946 hdr->control = PPP_UI;
2947 hdr->protocol[0] = 0;
2948 hdr->protocol[1] = proto;
2950 return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
2954 * Send an IPX (or any other non-IP) frame to the remote.
2956 * Return 0 if frame was queued for transmission.
2957 * 1 if frame must be re-queued for later driver support.
2960 ppp_dev_xmit_ipx (struct device *dev, struct ppp *ppp,
2961 __u8 *data, int len, int proto)
2963 struct tty_struct *tty = ppp2tty (ppp);
2964 struct ppp_hdr *hdr;
2966 * Validate the tty interface
2969 if (ppp->flags & SC_DEBUG)
2971 "ppp_dev_xmit: %s not connected to a TTY!\n",
2976 * Ensure that the PPP device is still up
2978 if (!(dev->flags & IFF_UP)) {
2979 if (ppp->flags & SC_DEBUG)
2980 printk (KERN_WARNING
2981 "ppp_dev_xmit: packet sent on interface %s,"
2987 * Detect a change in the transfer size
2989 if (ppp->mtu != ppp2dev (ppp)->mtu) {
2990 ppp_changedmtu (ppp,
2995 * Acquire the lock on the transmission buffer. If the buffer was busy then
2996 * mark the device as busy.
2998 if (lock_buffer (ppp->wbuf) != 0) {
3003 * Print the frame being sent
3005 if (ppp->flags & SC_LOG_OUTPKT)
3006 ppp_print_buffer ("ppp outpkt", data, len);
3010 len += PPP_HARD_HDR_LEN;
3011 hdr = &((struct ppp_hdr *) data)[-1];
3013 hdr->address = PPP_ALLSTATIONS;
3014 hdr->control = PPP_UI;
3015 hdr->protocol[0] = proto >> 8;
3016 hdr->protocol[1] = proto;
3018 return ppp_dev_xmit_frame (ppp, ppp->wbuf, (__u8 *) hdr, len);
3022 * Send a frame to the remote.
3026 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3030 struct ppp *ppp = dev2ppp (dev);
3031 struct tty_struct *tty = ppp2tty (ppp);
3033 * just a little sanity check.
3036 if (ppp->flags & SC_DEBUG)
3037 printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3041 * Avoid timing problem should tty hangup while data is queued to be sent
3044 dev_kfree_skb (skb, FREE_WRITE);
3049 * Validate the tty interface
3052 if (ppp->flags & SC_DEBUG)
3054 "ppp_dev_xmit: %s not connected to a TTY!\n",
3056 dev_kfree_skb (skb, FREE_WRITE);
3060 * Fetch the pointer to the data
3063 data = skb_data(skb);
3065 if (data == (__u8 *) 0) {
3066 if (ppp->flags & SC_DEBUG)
3067 printk (KERN_CRIT "ppp_dev_xmit: %s Null skb data\n",
3069 dev_kfree_skb (skb, FREE_WRITE);
3073 * Look at the protocol in the skb to determine the difference between
3074 * an IP frame and an IPX frame.
3076 switch (ntohs (skb->protocol)) {
3078 answer = ppp_dev_xmit_ipx (dev, ppp, data, len, PPP_IPX);
3082 answer = ppp_dev_xmit_ip (dev, ppp, data);
3085 default: /* All others have no support at this time. */
3086 dev_kfree_skb (skb, FREE_WRITE);
3090 * This is the end of the transmission. Release the buffer if it was sent.
3093 dev_kfree_skb (skb, FREE_WRITE);
3094 ppp->ddinfo.xmit_idle = jiffies;
3100 * Generate the statistic information for the /proc/net/dev listing.
3103 static struct enet_statistics *
3104 ppp_dev_stats (struct device *dev)
3106 struct ppp *ppp = dev2ppp (dev);
3108 ppp->estats.rx_packets = ppp->stats.ppp_ipackets;
3109 ppp->estats.rx_errors = ppp->stats.ppp_ierrors;
3110 ppp->estats.tx_packets = ppp->stats.ppp_opackets;
3111 ppp->estats.tx_errors = ppp->stats.ppp_oerrors;
3113 return &ppp->estats;
3116 #if LINUX_VERSION_CODE < VERSION(2,1,15)
3117 static int ppp_dev_header (sk_buff *skb, struct device *dev,
3118 __u16 type, void *daddr,
3119 void *saddr, unsigned int len)
3125 ppp_dev_rebuild (void *eth, struct device *dev,
3126 unsigned long raddr, struct sk_buff *skb)
3132 /*************************************************************
3134 * Miscellany called by various functions above.
3135 *************************************************************/
3137 /* Locate the previous instance of the PPP channel */
3139 ppp_find (int pid_value)
3145 /* try to find the exact same free device which we had before */
3150 ppp = ctl2ppp (ctl);
3151 if (ppp->sc_xfer == pid_value) {
3152 set_bit(0, &ppp->inuse);
3157 if (++if_num == max_dev)
3163 /* allocate or create a PPP channel */
3173 /* try to find an free device */
3178 ppp = ctl2ppp (ctl);
3179 if (!set_bit(0, &ppp->inuse))
3182 if (++if_num == max_dev)
3186 * There are no available items. Allocate a device from the system pool
3188 ctl = (ppp_ctrl_t *) kmalloc (sizeof(ppp_ctrl_t), GFP_KERNEL);
3190 (void) memset(ctl, 0, sizeof(ppp_ctrl_t));
3191 ppp = ctl2ppp (ctl);
3192 dev = ctl2dev (ctl);
3194 /* initialize channel control data */
3195 set_bit(0, &ppp->inuse);
3199 ppp->backup_tty = NULL;
3203 dev->init = ppp_init_dev;
3204 dev->name = ctl->name;
3205 dev->base_addr = (__u32) if_num;
3206 dev->priv = (void *) ppp;
3208 sprintf (dev->name, "ppp%d", if_num);
3210 /* link in the new channel */
3211 ctl->next = ppp_list;
3214 /* register device so that we can be ifconfig'd */
3215 /* ppp_init_dev() will be called as a side-effect */
3217 status = register_netdev (dev);
3219 printk (KERN_INFO "registered device %s\n", dev->name);
3224 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3226 /* This one will forever be busy as it is not initialized */
3232 * Utility procedures to print a buffer in hex/ascii
3236 ppp_print_hex (register __u8 * out, const __u8 * in, int count)
3238 register __u8 next_ch;
3239 static char hex[] = "0123456789ABCDEF";
3241 while (count-- > 0) {
3243 *out++ = hex[(next_ch >> 4) & 0x0F];
3244 *out++ = hex[next_ch & 0x0F];
3250 ppp_print_char (register __u8 * out, const __u8 * in, int count)
3252 register __u8 next_ch;
3254 while (count-- > 0) {
3257 if (next_ch < 0x20 || next_ch > 0x7e)
3261 if (next_ch == '%') /* printk/syslogd has a bug !! */
3269 ppp_print_buffer (const __u8 * name, const __u8 * buf, int count)
3273 if (name != (__u8 *) NULL)
3274 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3277 memset (line, 32, 44);
3278 ppp_print_hex (line, buf, 8);
3279 ppp_print_char (&line[8 * 3], buf, 8);
3280 printk (KERN_DEBUG "%s\n", line);
3286 memset (line, 32, 44);
3287 ppp_print_hex (line, buf, count);
3288 ppp_print_char (&line[8 * 3], buf, count);
3289 printk (KERN_DEBUG "%s\n", line);
3293 /*************************************************************
3294 * Compressor module interface
3295 *************************************************************/
3297 struct compressor_link {
3298 struct compressor_link *next;
3299 struct compressor *comp;
3302 static struct compressor_link *ppp_compressors = (struct compressor_link *) 0;
3304 static struct compressor *find_compressor (int type)
3306 struct compressor_link *lnk;
3307 unsigned long flags;
3312 lnk = ppp_compressors;
3313 while (lnk != (struct compressor_link *) 0) {
3314 if ((int) (__u8) lnk->comp->compress_proto == type) {
3315 restore_flags(flags);
3321 restore_flags(flags);
3322 return (struct compressor *) 0;
3325 static int ppp_register_compressor (struct compressor *cp)
3327 struct compressor_link *new;
3328 unsigned long flags;
3330 new = (struct compressor_link *) kmalloc (sizeof (struct compressor_link), GFP_KERNEL);
3332 if (new == (struct compressor_link *) 0)
3338 if (find_compressor (cp->compress_proto)) {
3339 restore_flags(flags);
3344 new->next = ppp_compressors;
3346 ppp_compressors = new;
3348 restore_flags(flags);
3352 static void ppp_unregister_compressor (struct compressor *cp)
3354 struct compressor_link *prev = (struct compressor_link *) 0;
3355 struct compressor_link *lnk;
3356 unsigned long flags;
3361 lnk = ppp_compressors;
3362 while (lnk != (struct compressor_link *) 0) {
3363 if (lnk->comp == cp) {
3365 prev->next = lnk->next;
3367 ppp_compressors = lnk->next;
3374 restore_flags(flags);
3377 /*************************************************************
3378 * Module support routines
3379 *************************************************************/
3387 /* register our line disciplines */
3388 status = ppp_first_time();
3391 "PPP: ppp_init() failure %d\n", status);
3392 #if LINUX_VERSION_CODE < VERSION(2,1,18)
3394 (void) register_symtab (&ppp_syms);
3400 cleanup_module(void)
3403 ppp_ctrl_t *ctl, *next_ctl;
3408 * Ensure that the devices are not in operation.
3412 ppp = ctl2ppp (ctl);
3413 if (ppp->inuse && ppp->tty != NULL) {
3418 dev = ctl2dev (ctl);
3419 if (dev->start || dev->flags & IFF_UP) {
3426 * Ensure that there are no compressor modules registered
3428 if (ppp_compressors != NULL)
3433 "PPP: device busy, remove delayed\n");
3437 * Release the tty registration of the line discipline so that no new entries
3440 status = tty_register_ldisc (N_PPP, NULL);
3443 "PPP: Unable to unregister ppp line discipline "
3444 "(err = %d)\n", status);
3447 "PPP: ppp line discipline successfully unregistered\n");
3449 * De-register the devices so that there is no problem with them
3451 next_ctl = ppp_list;
3454 next_ctl = ctl->next;
3455 ppp = ctl2ppp (ctl);
3456 dev = ctl2dev (ctl);
3459 unregister_netdev (dev);