7 * If you modify this file at all, increment the last number above.
8 * ppp.c is shipped with a PPP distribution as well as with the kernel;
9 * if everyone increases the PPPVERSION number above, then scripts
10 * can do the right thing when deciding whether to install a new ppp.c
11 * file. Don't change the format of that line otherwise, so the
12 * installation script can recognize it.
21 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
22 Multi-protocol Datagrams over Point-to-Point Links
28 Flags for this module (any combination is acceptable for testing.):
30 OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
31 character. This is normally set to ((HZ * 3) / 2).
32 This is 1.5 seconds. If not defined then the leading
35 CHECK_CHARACTERS - Enable the checking on all received characters for
36 8 data bits, no parity. This adds a small amount of
37 processing for each received character.
39 PPP_COMPRESS - Enable the PPP compression protocol. This protocol
40 is under contention with Motorolla's patent, so use
43 NEW_SKBUFF - Use NET3.020 sk_buff's
46 /* #define NEW_SKBUFF 1 */
47 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
48 #define CHECK_CHARACTERS 1
49 #define PPP_COMPRESS 1
51 /* $Id: ppp.c,v 1.5 1995/06/12 11:36:53 paulus Exp $
52 * Added dynamic allocation of channels to eliminate
53 * compiled-in limits on the number of channels.
55 * Dynamic channel allocation code Copyright 1995 Caldera, Inc.,
56 * released under the GNU General Public License Version 2.
59 #include <linux/kernel.h>
60 #include <linux/sched.h>
61 #include <linux/types.h>
62 #include <linux/fcntl.h>
63 #include <linux/interrupt.h>
64 #include <linux/ptrace.h>
65 #include <linux/ioport.h>
67 #include <linux/malloc.h>
68 #include <linux/tty.h>
69 #include <linux/errno.h>
70 #include <linux/sched.h> /* to get the struct task_struct */
71 #include <linux/string.h> /* used in new tty drivers */
72 #include <linux/signal.h> /* used in new tty drivers */
73 #include <asm/system.h>
74 #include <asm/bitops.h>
75 #include <asm/segment.h>
77 #include <net/if_arp.h>
78 #include <net/if_route.h>
79 #include <linux/if_ether.h>
80 #include <linux/netdevice.h>
81 #include <linux/skbuff.h>
82 #include <linux/inet.h>
83 #include <linux/ioctl.h>
86 #include <linux/netprotocol.h>
88 #define skb_data(skb) ((unsigned char *) (skb)->data)
89 typedef struct sk_buff sk_buff;
94 #include <linux/if_arp.h>
96 #include <net/ppp_defs.h>
97 #include <linux/socket.h>
98 #include <net/if_ppp.h>
99 #include <net/if_pppvar.h>
102 #include <linux/module.h>
103 #include <linux/version.h>
104 #define STATIC static
106 #define MOD_INC_USE_COUNT while(0) { }
107 #define MOD_DEC_USE_COUNT while(0) { }
108 #endif /* def MODULE */
113 #include <net/ppp-comp.h>
116 #define bsd_decompress (*ppp->sc_rcomp->decompress)
117 #define bsd_compress (*ppp->sc_xcomp->compress)
121 #define PPP_IPX 0x2b /* IPX protocol over PPP */
125 #define PPP_LQR 0xc025 /* Link Quality Reporting Protocol */
132 static void ppp_init_ctrl_blk (register struct ppp *);
133 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
134 static int ppp_doframe (struct ppp *);
135 static struct ppp *ppp_alloc (void);
136 static void ppp_print_buffer (const u_char *, u_char *, int);
137 extern inline void ppp_stuff_char (struct ppp *ppp,
138 register struct ppp_buffer *buf,
139 register u_char chr);
140 extern inline int lock_buffer (register struct ppp_buffer *buf);
141 static void ppp_proto_ccp (struct ppp *ppp, u_char *dp, int len, int rcvd);
143 static int rcv_proto_ip (struct ppp *, u_short, u_char *, int);
144 static int rcv_proto_ipx (struct ppp *, u_short, u_char *, int);
145 static int rcv_proto_vjc_comp (struct ppp *, u_short, u_char *, int);
146 static int rcv_proto_vjc_uncomp (struct ppp *, u_short, u_char *, int);
147 static int rcv_proto_unknown (struct ppp *, u_short, u_char *, int);
148 static int rcv_proto_ccp (struct ppp *, u_short, u_char *, int);
149 static int rcv_proto_lqr (struct ppp *, u_short, u_char *, int);
150 static void ppp_doframe_lower (struct ppp *, u_char *, int);
151 static int ppp_doframe (struct ppp *);
154 * List of compressors we know about.
155 * We leave some space so maybe we can modload compressors.
159 extern struct compressor ppp_bsd_compress;
161 struct compressor *ppp_compressors[8] = {
165 #endif /* PPP_COMPRESS */
167 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (u_char)(c))
170 * The "main" procedure to the ppp device
173 int ppp_init (struct device *);
176 * Network device driver callback routines
179 static int ppp_dev_open (struct device *);
180 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
181 static int ppp_dev_close (struct device *);
182 static int ppp_dev_xmit (sk_buff *, struct device *);
183 static struct enet_statistics *ppp_dev_stats (struct device *);
186 static int ppp_dev_input (struct protocol *self, struct protocol *lower,
187 sk_buff *skb, void *saddr, void *daddr);
188 static int ppp_dev_output (struct protocol *self, sk_buff *skb, int type,
189 int subid, void *saddr, void *daddr, void *opt);
190 static int ppp_dev_getkey(int protocol, int subid, unsigned char *key);
192 static int ppp_dev_header (u_char *, struct device *, unsigned short,
193 void *, void *, unsigned, sk_buff *);
194 static int ppp_dev_rebuild (void *, struct device *, unsigned long,
196 static unsigned short ppp_dev_type (sk_buff *, struct device *);
203 static int ppp_tty_read (struct tty_struct *, struct file *, u_char *,
205 static int ppp_tty_write (struct tty_struct *, struct file *, u_char *,
207 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
209 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
210 struct file *filp, int sel_type, select_table * wait);
211 static int ppp_tty_open (struct tty_struct *);
212 static void ppp_tty_close (struct tty_struct *);
213 static int ppp_tty_room (struct tty_struct *tty);
214 static void ppp_tty_receive (struct tty_struct *tty, u_char * cp,
215 char *fp, int count);
216 static void ppp_tty_wakeup (struct tty_struct *tty);
218 #define PRINTK(p) printk p ;
219 #define CHECK_PPP(a) if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return a;}
220 #define CHECK_PPP_VOID() if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return;}
222 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
223 #define in_rmap(ppp,c) ((((unsigned int) (u_char) (c)) < 0x20) && \
224 ppp->recv_async_map & (1 << (c)))
226 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
228 #define tty2ppp(tty) ((struct ppp *) (tty->disc_data))
229 #define dev2ppp(dev) ((struct ppp *) (dev->priv))
230 #define ppp2tty(ppp) ((struct tty_struct *) ppp->tty)
231 #define ppp2dev(ppp) ((struct device *) ppp->dev)
234 unsigned char address;
235 unsigned char control;
236 unsigned char protocol[2];
239 #define PPP_HARD_HDR_LEN (sizeof (struct ppp_hdr))
242 typedef struct ppp_ctrl {
243 struct ppp_ctrl *next; /* Next structure in the list */
244 char name [8]; /* Name of the device */
245 struct ppp ppp; /* PPP control table */
246 struct device dev; /* Device information table */
249 static ppp_ctrl_t *ppp_list = NULL;
251 #define ctl2ppp(ctl) (struct ppp *) &ctl->ppp
252 #define ctl2dev(ctl) (struct device *) &ctl->dev
258 static struct ppp ppp_ctrl[PPP_NRUNIT];
260 #define dev2ppp(dev) ((struct ppp *) &ppp_ctrl[dev->base_addr])
264 #define BUFFER_TYPE_DEV_RD 0 /* ppp read buffer */
265 #define BUFFER_TYPE_TTY_WR 1 /* tty write buffer */
266 #define BUFFER_TYPE_DEV_WR 2 /* ppp write buffer */
267 #define BUFFER_TYPE_TTY_RD 3 /* tty read buffer */
268 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
270 /* Define this string only once for all macro envocations */
271 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
273 static char szVersion[] = PPP_VERSION;
276 static struct protocol proto_ppp;
280 * Information for the protocol decoder
283 typedef int (*pfn_proto) (struct ppp *, u_short, u_char *, int);
285 typedef struct ppp_proto_struct {
290 ppp_proto_type proto_list[] = {
291 { PPP_IP, rcv_proto_ip },
292 { PPP_IPX, rcv_proto_ipx },
293 { PPP_VJC_COMP, rcv_proto_vjc_comp },
294 { PPP_VJC_UNCOMP, rcv_proto_vjc_uncomp },
295 { PPP_LQR, rcv_proto_lqr },
297 { PPP_CCP, rcv_proto_ccp },
299 { 0, rcv_proto_unknown } /* !!! MUST BE LAST !!! */
302 /* FCS table from RFC1331 */
304 static unsigned short fcstab[256] =
306 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
307 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
308 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
309 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
310 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
311 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
312 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
313 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
314 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
315 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
316 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
317 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
318 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
319 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
320 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
321 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
322 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
323 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
324 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
325 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
326 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
327 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
328 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
329 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
330 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
331 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
332 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
333 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
334 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
335 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
336 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
337 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
340 #ifdef CHECK_CHARACTERS
341 static unsigned paritytab[8] =
343 0x96696996, 0x69969669, 0x69969669, 0x96696996,
344 0x69969669, 0x96696996, 0x96696996, 0x69969669
348 /* local function to store a value into the LQR frame */
349 extern inline u_char * store_long (register u_char *p, register int value) {
350 *p++ = (u_char) (value >> 24);
351 *p++ = (u_char) (value >> 16);
352 *p++ = (u_char) (value >> 8);
353 *p++ = (u_char) value;
357 /*************************************************************
359 *************************************************************/
361 /* This procedure is called once and once only to define who we are to
362 * the operating system and the various procedures that it may use in
363 * accessing the ppp protocol.
367 ppp_first_time (void)
369 static struct tty_ldisc ppp_ldisc;
373 #define PPP_UNITS3(x) #x
374 #define PPP_UNITS2(x) PPP_UNITS3(x)
375 #define PPP_UNITS1(x) PPP_UNITS2(x)
376 #define PPP_UNITS "(" PPP_UNITS1(PPP_NRUNIT) " devices)"
378 #define PPP_UNITS "(dynamic channel allocation)"
381 "PPP: version %s " PPP_UNITS
392 "TCP compression code copyright 1989 Regents of the "
393 "University of California\n");
397 "PPP Dynamic channel allocation code copyright 1995 "
402 * Register the protocol for the device
406 memset (&proto_ppp, 0, sizeof (proto_ppp));
408 proto_ppp.name = "PPP";
409 proto_ppp.output = ppp_dev_output;
410 proto_ppp.input = ppp_dev_input;
411 proto_ppp.bh_input = ppp_dev_input;
412 proto_ppp.control_event = default_protocol_control;
413 proto_ppp.get_binding = ppp_dev_getkey;
414 proto_ppp.header_space = 0; /* PPP_HARD_HDR_LEN; */
416 protocol_register(&proto_ppp);
420 * Register the tty dicipline
422 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
423 ppp_ldisc.magic = TTY_LDISC_MAGIC;
424 ppp_ldisc.open = ppp_tty_open;
425 ppp_ldisc.close = ppp_tty_close;
426 ppp_ldisc.read = ppp_tty_read;
427 ppp_ldisc.write = ppp_tty_write;
428 ppp_ldisc.ioctl = ppp_tty_ioctl;
429 ppp_ldisc.select = ppp_tty_select;
430 ppp_ldisc.receive_room = ppp_tty_room;
431 ppp_ldisc.receive_buf = ppp_tty_receive;
432 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
434 status = tty_register_ldisc (N_PPP, &ppp_ldisc);
436 printk (KERN_INFO "PPP line discipline registered.\n");
438 printk (KERN_ERR "error registering line discipline: %d\n",
443 /*************************************************************
445 *************************************************************/
447 /* called when the device is actually created */
450 ppp_init_dev (struct device *dev)
454 dev->default_protocol = &proto_ppp; /* Our protocol layer is PPP */
456 dev->hard_header = ppp_dev_header;
457 dev->type_trans = ppp_dev_type;
458 dev->rebuild_header = ppp_dev_rebuild;
459 dev->hard_header_len = 0; /* PPP_HARD_HDR_LEN; */
464 dev->hard_start_xmit = ppp_dev_xmit;
465 dev->open = ppp_dev_open;
466 dev->stop = ppp_dev_close;
467 dev->get_stats = ppp_dev_stats;
468 dev->do_ioctl = ppp_dev_ioctl;
470 dev->type = ARPHRD_PPP;
472 for (indx = 0; indx < DEV_NUMBUFFS; indx++)
473 skb_queue_head_init (&dev->buffs[indx]);
475 /* New-style flags */
476 dev->flags = IFF_POINTOPOINT;
477 dev->family = AF_INET;
481 dev->pa_alen = sizeof (unsigned long);
487 * Local procedure to initialize the ppp structure
491 ppp_init_ctrl_blk (register struct ppp *ppp)
493 ppp->magic = PPP_MAGIC;
501 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
502 ppp->xmit_async_map[0] = 0xffffffff;
503 ppp->xmit_async_map[3] = 0x60000000;
504 ppp->recv_async_map = 0x00000000;
511 ppp->read_wait = NULL;
512 ppp->write_wait = NULL;
514 #ifdef OPTIMIZE_FLAG_TIME /* ensure flag will always be sent first time */
515 ppp->last_xmit = jiffies - OPTIMIZE_FLAG_TIME;
520 /* clear statistics */
521 memset (&ppp->stats, '\0', sizeof (struct pppstat));
523 /* Reset the demand dial information */
524 ppp->ddinfo.ip_sjiffies =
525 ppp->ddinfo.ip_rjiffies =
526 ppp->ddinfo.nip_sjiffies =
527 ppp->ddinfo.nip_rjiffies = jiffies;
531 ppp->sc_rc_state = NULL;
532 #endif /* PPP_COMPRESS */
535 /* called at boot/load time for each ppp device defined in the kernel */
539 ppp_init (struct device *dev)
541 static int first_time = 1;
546 answer = ppp_first_time();
549 * Un-register the devices defined at the start of the system. They will
550 * be added when they are needed again. The first device just gets us into
551 * this code to register the handlers.
554 unregister_netdev (dev);
557 ppp_init_ctrl_blk (dev2ppp (dev));
558 dev2ppp (dev) -> inuse = 0;
559 dev2ppp (dev) -> dev = dev;
566 * Routine to allocate a buffer for later use by the driver.
569 static struct ppp_buffer *
570 ppp_alloc_buf (int size, int type)
572 struct ppp_buffer *buf;
574 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
578 buf->size = size - 1; /* Mask for the buffer size */
584 buf->fcs = PPP_INITFCS;
590 * Routine to release the allocated buffer.
594 ppp_free_buf (struct ppp_buffer *ptr)
601 * Lock the indicated transmit buffer
605 lock_buffer (register struct ppp_buffer *buf)
610 * Save the current state and if free then set it to the "busy" state
618 * Restore the flags and return the previous state. 0 implies success.
620 restore_flags (flags);
625 * MTU has been changed by the IP layer. Unfortunately we are not told
626 * about this, but we spot it ourselves and fix things up. We could be
627 * in an upcall from the tty driver, or in an ip packet queue.
631 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
635 struct ppp_buffer *new_rbuf;
636 struct ppp_buffer *new_wbuf;
637 struct ppp_buffer *new_cbuf;
638 struct ppp_buffer *new_tbuf;
640 struct ppp_buffer *old_rbuf;
641 struct ppp_buffer *old_wbuf;
642 struct ppp_buffer *old_cbuf;
643 struct ppp_buffer *old_tbuf;
647 * Allocate the buffer from the kernel for the data
651 /* allow for possible escapement of every character */
652 mtu = (new_mtu * 2) + 20;
654 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
660 if (ppp->flags & SC_DEBUG)
661 printk (KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
662 dev->name, new_mtu, new_mru);
664 new_wbuf = ppp_alloc_buf (mtu+PPP_HARD_HDR_LEN, BUFFER_TYPE_DEV_WR);
665 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
666 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
667 new_cbuf = ppp_alloc_buf (mru+PPP_HARD_HDR_LEN, BUFFER_TYPE_VJ);
669 * If the buffers failed to allocate then complain and release the partial
672 if (new_wbuf == NULL || new_tbuf == NULL ||
673 new_rbuf == NULL || new_cbuf == NULL) {
674 if (ppp->flags & SC_DEBUG)
676 "ppp: failed to allocate new buffers\n");
678 ppp_free_buf (new_wbuf);
679 ppp_free_buf (new_tbuf);
680 ppp_free_buf (new_rbuf);
681 ppp_free_buf (new_cbuf);
685 * Update the pointers to the new buffer structures.
688 old_wbuf = ppp->wbuf;
689 old_rbuf = ppp->rbuf;
690 old_cbuf = ppp->cbuf;
691 old_tbuf = ppp->tbuf;
693 ppp->wbuf = new_wbuf;
694 ppp->rbuf = new_rbuf;
695 ppp->cbuf = new_cbuf;
696 ppp->tbuf = new_tbuf;
698 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
700 dev->mem_start = (unsigned long) buf_base (new_wbuf);
701 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
702 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
703 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
705 * Update the parameters for the new buffer sizes
707 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
708 ppp->escape = 0; /* No pending escape character */
718 ppp->tty->flags &= ~TTY_DO_WRITE_WAKEUP;
719 ppp->flags &= ~SC_XMIT_BUSY;
723 * Release old buffer pointers
725 ppp_free_buf (old_rbuf);
726 ppp_free_buf (old_wbuf);
727 ppp_free_buf (old_cbuf);
728 ppp_free_buf (old_tbuf);
733 * CCP is down; free (de)compressor state if necessary.
738 ppp_ccp_closed (struct ppp *ppp)
740 if (ppp->sc_xc_state) {
741 (*ppp->sc_xcomp->comp_free) (ppp->sc_xc_state);
742 ppp->sc_xc_state = NULL;
745 if (ppp->sc_rc_state) {
746 (*ppp->sc_rcomp->decomp_free) (ppp->sc_rc_state);
747 ppp->sc_rc_state = NULL;
750 #endif /* PPP_COMPRESS */
753 * Called to release all of the information in the current PPP structure.
755 * It is called when the ppp device goes down or if it is unable to go
760 ppp_release (struct ppp *ppp)
762 struct tty_struct *tty;
769 ppp_ccp_closed (ppp);
771 ppp->sc_rc_state = NULL;
772 #endif /* PPP_COMPRESS */
774 if (tty != NULL && tty->disc_data == ppp)
775 tty->disc_data = NULL; /* Break the tty->ppp link */
777 if (dev && dev->flags & IFF_UP) {
778 dev_close (dev); /* close the device properly */
779 dev->flags = 0; /* prevent recursion */
782 ppp_free_buf (ppp->rbuf);
783 ppp_free_buf (ppp->wbuf);
784 ppp_free_buf (ppp->cbuf);
785 ppp_free_buf (ppp->ubuf);
786 ppp_free_buf (ppp->tbuf);
798 slhc_free (ppp->slcomp);
809 * Called when the PPP device goes down in response to an ifconfig request.
813 ppp_tty_close (struct tty_struct *tty)
815 struct ppp *ppp = tty2ppp (tty);
817 if (ppp == NULL || ppp->magic != PPP_MAGIC) {
818 if (ppp->flags & SC_DEBUG)
820 "ppp: trying to close unopened tty!\n");
823 if (ppp->flags & SC_DEBUG)
824 printk (KERN_INFO "ppp: channel %s closing.\n",
825 ppp2dev(ppp) -> name);
834 * Called when the tty discipline is switched to PPP.
838 ppp_tty_open (struct tty_struct *tty)
840 struct ppp *ppp = tty2ppp (tty);
842 * There should not be an existing table for this slot.
845 if (ppp->flags & SC_DEBUG)
847 "ppp_tty_open: gack! tty already associated to %s!\n",
848 ppp->magic == PPP_MAGIC ? ppp2dev(ppp)->name
853 * Allocate the structure from the system
857 if (ppp->flags & SC_DEBUG)
859 "ppp_tty_open: couldn't allocate ppp channel\n");
863 * Initialize the control block
865 ppp_init_ctrl_blk (ppp);
867 tty->disc_data = ppp;
869 * Flush any pending characters in the driver and discipline.
871 if (tty->ldisc.flush_buffer)
872 tty->ldisc.flush_buffer (tty);
874 if (tty->driver.flush_buffer)
875 tty->driver.flush_buffer (tty);
877 * Allocate space for the default VJ header compression slots (16)
879 ppp->slcomp = slhc_init (16, 16);
880 if (ppp->slcomp == NULL) {
881 if (ppp->flags & SC_DEBUG)
883 "ppp_tty_open: no space for compression buffers!\n");
888 * Allocate space for the MTU and MRU buffers
890 if (ppp_changedmtu (ppp, ppp2dev(ppp)->mtu, ppp->mru) == 0) {
895 * Allocate space for a user level buffer
897 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
898 if (ppp->ubuf == NULL) {
899 if (ppp->flags & SC_DEBUG)
901 "ppp_tty_open: no space for user receive buffer\n");
906 if (ppp->flags & SC_DEBUG)
907 printk (KERN_INFO "ppp: channel %s open\n",
915 * Local function to send the next portion of the buffer.
917 * Called by the tty driver's tty_wakeup function should it be entered
918 * because the partial buffer was transmitted.
920 * Called by kick_tty to send the initial portion of the buffer.
922 * Completion processing of the buffer transmission is handled here.
926 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
927 struct ppp_buffer *xbuf)
929 register int count, actual;
931 * Prevent re-entrancy by ensuring that this routine is called only once.
934 if (ppp->flags & SC_XMIT_BUSY) {
938 ppp->flags |= SC_XMIT_BUSY;
941 * Send the next block of data to the modem
943 count = xbuf->count - xbuf->tail;
944 actual = tty->driver.write (tty, 0,
945 buf_base (xbuf) + xbuf->tail, count);
947 * Terminate transmission of any block which may have an error.
948 * This could occur should the carrier drop.
951 ppp->stats.ppp_oerrors++;
954 ppp->bytes_sent += actual;
956 * If the buffer has been transmitted then clear the indicators.
958 xbuf->tail += actual;
959 if (actual == count) {
961 ppp->flags &= ~SC_XMIT_BUSY;
963 * Complete the transmission on the current buffer.
967 tty->flags &= ~TTY_DO_WRITE_WAKEUP;
971 * If the completed buffer came from the device write, then complete the
972 * transmission block.
974 if (ppp2dev (ppp) -> flags & IFF_UP) {
975 ppp2dev (ppp)->tbusy = 0;
977 dev_tint (ppp2dev (ppp));
980 * Wake up the transmission queue for all completion events.
982 wake_up_interruptible (&ppp->write_wait);
984 * Look at the priorities. Choose a daemon write over the device driver.
993 * If there is a pending buffer then transmit it now.
996 ppp->flags &= ~SC_XMIT_BUSY;
997 ppp_kick_tty (ppp, xbuf);
1002 * Clear the re-entry flag
1004 ppp->flags &= ~SC_XMIT_BUSY;
1008 * This function is called by the tty driver when the transmit buffer has
1009 * additional space. It is used by the ppp code to continue to transmit
1010 * the current buffer should the buffer have been partially sent.
1012 * In addition, it is used to send the first part of the buffer since the
1013 * logic and the inter-locking would be identical.
1017 ppp_tty_wakeup (struct tty_struct *tty)
1019 struct ppp_buffer *xbuf;
1020 struct ppp *ppp = tty2ppp (tty);
1022 if (!ppp || ppp->magic != PPP_MAGIC) {
1023 if (ppp->flags & SC_DEBUG)
1024 printk (KERN_DEBUG "PPP: write_wakeup called but "
1025 "couldn't find PPP struct.\n");
1029 * Ensure that there is a transmission pending. Clear the re-entry flag if
1030 * there is no pending buffer. Otherwise, send the buffer.
1034 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
1036 ppp_tty_wakeup_code (ppp, tty, xbuf);
1040 * This function is called to transmit a buffer to the remote. The buffer
1041 * is placed on the pending queue if there is presently a buffer being
1042 * sent or it is transmitted with the aid of ppp_tty_wakeup.
1046 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
1055 * Control the flags which are best performed with the interrupts masked.
1060 * If the transmitter is busy then place the buffer on the appropriate
1063 if (ppp->xbuf != NULL) {
1064 if (xbuf->type == BUFFER_TYPE_TTY_WR)
1068 restore_flags (flags);
1072 * If the transmitter is not busy then this is the highest priority frame
1074 ppp->flags &= ~SC_XMIT_BUSY;
1075 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
1077 restore_flags (flags);
1079 * Do the "tty wakeup_code" to actually send this buffer.
1081 ppp_tty_wakeup_code (ppp, ppp2tty (ppp), xbuf);
1084 /*************************************************************
1086 * The following functions handle input that arrives from
1087 * the TTY. It recognizes PPP frames and either hands them
1088 * to the network layer or queues them for delivery to a
1089 * user process reading this TTY.
1090 *************************************************************/
1093 * Callback function from tty driver. Return the amount of space left
1094 * in the receiver's buffer to decide if remote transmitter is to be
1099 ppp_tty_room (struct tty_struct *tty)
1101 return 65536; /* We can handle an infinite amount of data. :-) */
1105 * Callback function when data is available at the tty driver.
1109 ppp_tty_receive (struct tty_struct *tty, u_char * data, char *flags, int count)
1111 register struct ppp *ppp = tty2ppp (tty);
1112 register struct ppp_buffer *buf = ppp->rbuf;
1115 * Verify the table pointer and ensure that the line is
1116 * still in PPP discipline.
1118 if (!ppp || ppp->magic != PPP_MAGIC) {
1119 if (ppp->flags & SC_DEBUG)
1121 "PPP: handler called but couldn't find "
1127 * Print the buffer if desired
1129 if (ppp->flags & SC_LOG_RAWIN)
1130 ppp_print_buffer ("receive buffer", data, count);
1132 * Collect the character and error condition for the character. Set the toss
1133 * flag for the first character error.
1135 while (count-- > 0) {
1139 if (*flags && ppp->toss == 0)
1144 * Set the flags for 8 data bits and no parity.
1146 * Actually, it sets the flags for d7 being 0/1 and parity being even/odd
1147 * so that the normal processing would have all flags set at the end of the
1148 * session. A missing flag bit would denote an error condition.
1151 #ifdef CHECK_CHARACTERS
1153 ppp->flags |= SC_RCV_B7_1;
1155 ppp->flags |= SC_RCV_B7_0;
1157 if (paritytab[chr >> 5] & (1 << (chr & 0x1F)))
1158 ppp->flags |= SC_RCV_ODDP;
1160 ppp->flags |= SC_RCV_EVNP;
1163 * Branch on the character. Process the escape character. The sequence ESC ESC
1164 * is defined to be ESC.
1167 case PPP_ESCAPE: /* PPP_ESCAPE: invert bit in next character */
1168 ppp->escape = PPP_TRANS;
1171 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
1172 * then the block is to be ignored. In addition, characters before the very
1173 * first FLAG are also tossed by this procedure.
1175 case PPP_FLAG: /* PPP_FLAG: end of frame */
1176 ppp->stats.ppp_ibytes = ppp->bytes_rcvd;
1180 * Process frames which are not to be ignored. If the processing failed,
1181 * then clean up the VJ tables.
1183 if ((ppp->toss & 0x80) != 0 ||
1184 ppp_doframe (ppp) == 0) {
1185 slhc_toss (ppp->slcomp);
1188 * Reset all indicators for the new frame to follow.
1191 buf->fcs = PPP_INITFCS;
1196 * All other characters in the data come here. If the character is in the
1197 * receive mask then ignore the character.
1200 if (in_rmap (ppp, chr))
1203 * Adjust the character and if the frame is to be discarded then simply
1204 * ignore the character until the ending FLAG is received.
1212 * If the count sent is within reason then store the character, bump the
1213 * count, and update the FCS for the character.
1215 if (buf->count < buf->size) {
1216 buf_base (buf)[buf->count++] = chr;
1217 buf->fcs = PPP_FCS (buf->fcs, chr);
1221 * The peer sent too much data. Set the flags to discard the current frame
1222 * and wait for the re-synchronization FLAG to be sent.
1224 ppp->stats.ppp_ierrors++;
1232 * Put the input frame into the networking system for the indicated protocol
1236 ppp_rcv_rx (struct ppp *ppp, unsigned short proto, u_char * data, int count)
1238 sk_buff *skb = alloc_skb (count, GFP_ATOMIC);
1240 * Generate a skb buffer for the new frame.
1243 if (ppp->flags & SC_DEBUG)
1245 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1246 ppp2dev (ppp)->name);
1250 * Move the received data from the input buffer to the skb buffer.
1252 skb->len = count; /* Store the length */
1253 skb->dev = ppp2dev (ppp); /* We are the device */
1255 skb->protocol = proto;
1257 memcpy ((u_char *) skb_data(skb), data, count); /* move data */
1259 * Tag the frame and kick it to the proper receive routine
1262 ppp->ddinfo.ip_rjiffies = jiffies;
1268 * Process the receipt of an IP frame
1272 rcv_proto_ip (struct ppp *ppp, unsigned short proto, u_char * data, int count)
1274 if (ppp2dev (ppp)->flags & IFF_UP) {
1276 return ppp_rcv_rx (ppp, htons (ETH_P_IP), data, count);
1282 * Process the receipt of an IPX frame
1286 rcv_proto_ipx (struct ppp *ppp, unsigned short proto, u_char * data, int count)
1289 if (ppp2dev (ppp)->flags & IFF_UP) {
1291 return ppp_rcv_rx (ppp, htons (ETH_P_IPX), data, count);
1298 * Process the receipt of an VJ Compressed frame
1302 rcv_proto_vjc_comp (struct ppp *ppp, unsigned short proto,
1303 u_char *data, int count)
1305 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1306 int new_count = slhc_uncompress (ppp->slcomp, data, count);
1307 if (new_count >= 0) {
1308 return rcv_proto_ip (ppp, PPP_IP, data, new_count);
1310 if (ppp->flags & SC_DEBUG)
1312 "ppp: error in VJ decompression\n");
1318 * Process the receipt of an VJ Un-compressed frame
1322 rcv_proto_vjc_uncomp (struct ppp *ppp, unsigned short proto,
1323 u_char *data, int count)
1325 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1326 if (slhc_remember (ppp->slcomp, data, count) > 0) {
1327 return rcv_proto_ip (ppp, PPP_IP, data, count);
1329 if (ppp->flags & SC_DEBUG)
1331 "ppp: error in VJ memorizing\n");
1337 * Receive all unclassified protocols.
1341 rcv_proto_unknown (struct ppp *ppp, unsigned short proto,
1342 u_char *data, int len)
1345 register int current_idx;
1349 buf_base (ppp->ubuf) [current_idx++] = (u_char) (c); \
1350 current_idx &= ppp->ubuf->size; \
1351 if (current_idx == ppp->ubuf->tail) \
1356 * The total length includes the protocol data.
1357 * Lock the user information buffer.
1359 if (set_bit (0, &ppp->ubuf->locked)) {
1360 if (ppp->flags & SC_DEBUG)
1362 "ppp_us_queue: can't get lock\n");
1364 current_idx = ppp->ubuf->head;
1366 * Insert the buffer length (not counted), the protocol, and the data
1376 while (totlen-- > 0) {
1381 * The frame is complete. Update the head pointer and wakeup the pppd
1384 ppp->ubuf->head = current_idx;
1386 clear_bit (0, &ppp->ubuf->locked);
1387 wake_up_interruptible (&ppp->read_wait);
1388 if (ppp->tty->fasync != NULL)
1389 kill_fasync (ppp->tty->fasync, SIGIO);
1391 if (ppp->flags & SC_DEBUG)
1393 "ppp: successfully queued %d bytes\n",
1396 ppp->ddinfo.nip_rjiffies = jiffies;
1399 * The buffer is full. Unlock the header
1402 clear_bit (0, &ppp->ubuf->locked);
1403 if (ppp->flags & SC_DEBUG)
1405 "ppp_us_queue: ran out of buffer space.\n");
1408 * Discard the frame. There are no takers for this protocol.
1410 if (ppp->flags & SC_DEBUG)
1411 printk (KERN_WARNING
1412 "ppp: dropping packet on the floor.\n");
1413 slhc_toss (ppp->slcomp);
1418 * Handle a CCP packet.
1420 * The CCP packet is passed along to the pppd process just like any
1421 * other PPP frame. The difference is that some processing needs to be
1422 * immediate or the compressors will become confused on the peer.
1426 static void ppp_proto_ccp (struct ppp *ppp, u_char *dp, int len, int rcvd)
1428 int slen = CCP_LENGTH(dp);
1431 switch (CCP_CODE(dp)) {
1436 * CCP must be going down - disable compression
1438 if (ppp->flags & SC_CCP_UP) {
1439 ppp->flags &= ~(SC_CCP_UP |
1446 if (ppp->flags & SC_CCP_OPEN == 0)
1448 if (ppp->flags & SC_CCP_UP)
1450 if (slen < CCP_HDRLEN + CCP_OPT_MINLEN)
1452 if (slen < CCP_OPT_LENGTH (dp + CCP_HDRLEN) +
1456 * we're agreeing to send compressed packets.
1459 if (ppp->sc_xc_state == NULL)
1462 if ((*ppp->sc_xcomp->comp_init)
1466 ppp2dev (ppp)->base_addr,
1467 ppp->flags & SC_DEBUG))
1468 ppp->flags |= SC_COMP_RUN;
1472 * peer is agreeing to send compressed packets.
1474 if (ppp->sc_rc_state == NULL)
1477 if ((*ppp->sc_rcomp->decomp_init)
1481 ppp2dev (ppp)->base_addr,
1483 ppp->flags & SC_DEBUG)) {
1484 ppp->flags |= SC_DECOMP_RUN;
1485 ppp->flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1489 * The protocol sequence is complete at this end
1492 if (ppp->flags & SC_CCP_UP == 0)
1496 if (ppp->sc_xc_state &&
1497 (ppp->flags & SC_COMP_RUN))
1498 (*ppp->sc_xcomp->comp_reset)
1503 if (ppp->sc_rc_state && (ppp->flags & SC_DECOMP_RUN)) {
1504 (*ppp->sc_rcomp->decomp_reset)
1506 ppp->flags &= ~SC_DC_ERROR;
1514 rcv_proto_ccp (struct ppp *ppp, unsigned short proto, u_char *dp, int len)
1516 ppp_proto_ccp (ppp, dp, len, 1);
1517 return rcv_proto_unknown (ppp, proto, dp, len);
1519 #endif /* PPP_COMPRESS */
1522 * Handle a LQR packet.
1524 * The LQR packet is passed along to the pppd process just like any
1525 * other PPP frame. The difference is that some processing needs to be
1526 * performed to append the current data to the end of the frame.
1530 rcv_proto_lqr (struct ppp *ppp, unsigned short proto, u_char * data, int len)
1536 memset (&data [len], '\0', 48 - len);
1538 * Fill in the fields from the driver data
1541 p = store_long (p, ++ppp->stats.ppp_ilqrs);
1542 p = store_long (p, ppp->stats.ppp_ipackets);
1543 p = store_long (p, ppp->stats.ppp_discards);
1544 p = store_long (p, ppp->stats.ppp_ierrors);
1545 p = store_long (p, ppp->stats.ppp_ioctects + len);
1550 * Pass the frame to the pppd daemon.
1552 return rcv_proto_unknown (ppp, proto, data, len);
1555 /* on entry, a received frame is in ppp->rbuf.bufr
1556 check it and dispose as appropriate */
1558 static void ppp_doframe_lower (struct ppp *ppp, u_char *data, int len)
1562 ppp_proto_type *proto_ptr;
1564 * Ignore empty frames
1569 * Count the frame and print it
1571 ++ppp->stats.ppp_ipackets;
1572 if (ppp->flags & SC_LOG_INPKT)
1573 ppp_print_buffer ("receive frame", data, count);
1575 * Ignore the leading ADDRESS and CONTROL fields in the frame.
1577 if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1582 * Obtain the protocol from the frame
1584 proto = (u_short) *data++;
1588 proto = (proto << 8) | (u_short) *data++;
1592 * Find the procedure to handle this protocol. The last one is marked
1593 * as a protocol 0 which is the 'catch-all' to feed it to the pppd daemon.
1595 proto_ptr = proto_list;
1596 while (proto_ptr->proto != 0 && proto_ptr->proto != proto)
1599 * Update the appropriate statistic counter.
1601 if ((*proto_ptr->func) (ppp, proto, data, count))
1602 ppp->stats.ppp_ioctects += len;
1604 ++ppp->stats.ppp_discards;
1607 /* on entry, a received frame is in ppp->rbuf.bufr
1608 check it and dispose as appropriate */
1611 ppp_doframe (struct ppp *ppp)
1613 u_char *data = buf_base (ppp->rbuf);
1614 int count = ppp->rbuf->count;
1621 * If there is a pending error from the receiver then log it and discard
1622 * the damaged frame.
1625 if (ppp->flags & SC_DEBUG)
1626 printk (KERN_WARNING
1627 "ppp_toss: tossing frame, reason = %d\n",
1629 ppp->stats.ppp_ierrors++;
1633 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1634 * follows each frame.
1639 * Generate an error if the frame is too small.
1641 if (count < PPP_HARD_HDR_LEN) {
1642 if (ppp->flags & SC_DEBUG)
1643 printk (KERN_WARNING
1644 "ppp: got runt ppp frame, %d chars\n", count);
1645 slhc_toss (ppp->slcomp);
1646 ppp->stats.ppp_ierrors++;
1650 * Verify the CRC of the frame and discard the CRC characters from the
1651 * end of the buffer.
1653 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1654 if (ppp->flags & SC_DEBUG)
1655 printk (KERN_WARNING
1656 "ppp: frame with bad fcs, excess = %x\n",
1657 ppp->rbuf->fcs ^ PPP_GOODFCS);
1658 ppp->stats.ppp_ierrors++;
1661 count -= 2; /* ignore the fcs characters */
1664 proto = PPP_PROTOCOL (data);
1666 * Process the active decompressor.
1668 if ((ppp->sc_rc_state != (void *) 0) &&
1669 ((ppp->flags & SC_DECOMP_RUN) == 0)) {
1671 * If the frame is compressed then decompress it.
1673 if (((ppp->flags & (SC_DC_FERROR | SC_DC_ERROR)) == 0) &&
1674 (proto == PPP_COMP)) {
1675 new_data = kmalloc (ppp->mru + 4, GFP_ATOMIC);
1676 if (new_data == NULL) {
1677 if (ppp->flags & SC_DEBUG)
1679 "ppp_doframe: no memory\n");
1680 slhc_toss (ppp->slcomp);
1681 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1687 * Decompress the frame
1689 new_count = bsd_decompress (ppp->sc_rc_state,
1695 switch (new_count) {
1697 ppp_doframe_lower (ppp, new_data, new_count);
1705 ppp->flags |= SC_DC_ERROR;
1708 case DECOMP_FATALERROR:
1709 ppp->flags |= SC_DC_FERROR;
1713 * Log the error condition and discard the frame.
1715 if (ppp->flags & SC_DEBUG)
1718 "decompress err %d\n", new_count);
1720 slhc_toss (ppp->slcomp);
1724 * The frame is not special. Pass it through the decompressor without
1725 * actually decompressing the data
1727 (*ppp->sc_rcomp->incomp) (ppp->sc_rc_state,
1733 * Process the uncompressed frame.
1735 ppp_doframe_lower (ppp, data, count);
1739 /*************************************************************
1740 * LINE DISCIPLINE SUPPORT
1741 * The following functions form support user programs
1742 * which read and write data on a TTY with the PPP line
1743 * discipline. Reading is done from a circular queue,
1744 * filled by the lower TTY levels.
1745 *************************************************************/
1747 /* read a PPP frame from the us_rbuff circular buffer,
1748 waiting if necessary
1752 ppp_tty_read (struct tty_struct *tty, struct file *file, u_char * buf,
1755 struct ppp *ppp = tty2ppp (tty);
1761 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1762 ppp->ubuf->tail &= ppp->ubuf->size; \
1766 * Validate the pointer to the PPP structure
1768 if (!ppp || ppp->magic != PPP_MAGIC) {
1769 if (ppp->flags & SC_DEBUG)
1771 "ppp_tty_read: cannot find ppp channel\n");
1776 if (ppp->flags & SC_DEBUG)
1778 "ppp_tty_read: called %x num %u\n",
1782 * Acquire the read lock.
1785 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1786 if (ppp->flags & SC_DEBUG)
1788 "ppp_tty_read: sleeping(ubuf)\n");
1790 current->timeout = 0;
1791 current->state = TASK_INTERRUPTIBLE;
1794 if (current->signal & ~current->blocked)
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) {
1816 if (ppp->flags & SC_DEBUG)
1818 "ppp_tty_read: no data "
1820 return -EWOULDBLOCK;
1822 current->timeout = 0;
1824 if (ppp->flags & SC_DEBUG)
1826 "ppp_tty_read: sleeping(read_wait)\n");
1828 interruptible_sleep_on (&ppp->read_wait);
1829 if (current->signal & ~current->blocked)
1834 * Reset the time of the last read operation.
1836 ppp->ddinfo.nip_rjiffies = jiffies;
1837 if (ppp->flags & SC_DEBUG)
1838 printk (KERN_DEBUG "ppp_tty_read: len = %d\n", len);
1840 * Ensure that the frame will fit within the caller's buffer. If not, then
1841 * discard the frame from the input buffer and return an error to the caller.
1844 /* Can't copy it, update us_rbuff_head */
1846 if (ppp->flags & SC_DEBUG)
1848 "ppp: read of %u bytes too small for %d "
1849 "frame\n", nr, len + 2);
1850 ppp->ubuf->tail += len;
1851 ppp->ubuf->tail &= ppp->ubuf->size;
1852 clear_bit (0, &ppp->ubuf->locked);
1853 ppp->stats.ppp_ierrors++;
1857 * Fake the insertion of the ADDRESS and CONTROL information because these
1858 * were not saved in the buffer.
1860 put_fs_byte (PPP_ALLSTATIONS, buf++);
1861 put_fs_byte (PPP_UI, buf++);
1865 * Copy the received data from the buffer to the caller's area.
1867 while (indx-- > 0) {
1869 put_fs_byte (c, buf++);
1872 * Release the lock and return the character count in the buffer area.
1874 clear_bit (0, &ppp->ubuf->locked);
1875 len += 2; /* Account for ADDRESS and CONTROL bytes */
1876 if (ppp->flags & SC_DEBUG)
1878 "ppp_tty_read: passing %d bytes up\n", len);
1884 /* stuff a character into the transmit buffer, using PPP's way of escaping
1886 also, update fcs to take account of new character */
1889 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1890 register u_char chr)
1893 * The buffer should not be full.
1895 if (ppp->flags & SC_DEBUG) {
1896 if ((buf->count < 0) || (buf->count > 3000))
1897 printk (KERN_DEBUG "ppp_stuff_char: %x %d\n",
1898 (unsigned int) buf->count,
1899 (unsigned int) buf->count);
1902 * Update the FCS and if the character needs to be escaped, do it.
1904 buf->fcs = PPP_FCS (buf->fcs, chr);
1905 if (in_xmap (ppp, chr)) {
1907 ins_char (buf, PPP_ESCAPE);
1910 * Add the character to the buffer.
1912 ins_char (buf, chr);
1916 * Procedure to encode the data with the proper escapement and send the
1917 * data to the remote system.
1921 ppp_dev_xmit_lower (struct ppp *ppp, struct ppp_buffer *buf,
1922 u_char *data, int count, int non_ip)
1924 unsigned short int write_fcs;
1926 * Insert the leading FLAG character
1930 #ifdef OPTIMIZE_FLAG_TIME
1932 ins_char (buf, PPP_FLAG);
1934 if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1935 ins_char (buf, PPP_FLAG);
1937 ppp->last_xmit = jiffies;
1939 ins_char (buf, PPP_FLAG);
1942 buf->fcs = PPP_INITFCS;
1947 ppp_stuff_char (ppp, buf, *data++);
1949 * Add the trailing CRC and the final flag character
1951 write_fcs = buf->fcs ^ 0xFFFF;
1952 ppp_stuff_char (ppp, buf, write_fcs);
1953 ppp_stuff_char (ppp, buf, write_fcs >> 8);
1955 if (ppp->flags & SC_DEBUG)
1956 printk (KERN_DEBUG "ppp_dev_xmit_lower: fcs is %hx\n",
1959 * Add the trailing flag character
1961 ins_char (buf, PPP_FLAG);
1965 if (ppp->flags & SC_LOG_FLUSH)
1966 ppp_print_buffer ("ppp flush", buf_base (buf),
1969 if (ppp->flags & SC_DEBUG)
1971 "ppp_dev_xmit: writing %d chars\n",
1975 * Send the block to the tty driver.
1977 ppp->stats.ppp_obytes += buf->count;
1978 ppp_kick_tty (ppp, buf);
1982 * Send an frame to the remote with the proper bsd compression.
1984 * Return 0 if frame was queued for transmission.
1985 * 1 if frame must be re-queued for later driver support.
1989 ppp_dev_xmit_frame (struct ppp *ppp, struct ppp_buffer *buf,
1990 u_char *data, int count)
1992 int address, control;
1999 if (ppp->flags & SC_LOG_OUTPKT)
2000 ppp_print_buffer ("write frame", data, count);
2002 * Determine if the frame may be compressed. Attempt to compress the
2003 * frame if possible.
2005 address = PPP_ADDRESS (data);
2006 control = PPP_CONTROL (data);
2007 proto = PPP_PROTOCOL (data);
2010 if ((ppp->flags & SC_COMP_RUN != 0) &&
2011 (ppp->sc_xc_state != (void *) 0) &&
2012 (address == PPP_ALLSTATIONS) &&
2013 (control == PPP_UI) &&
2014 (proto != PPP_LCP) &&
2015 (proto != PPP_CCP)) {
2016 new_data = kmalloc (count, GFP_ATOMIC);
2017 if (new_data == NULL) {
2018 if (ppp->flags & SC_DEBUG)
2020 "ppp_dev_xmit_frame: no memory\n");
2024 new_count = bsd_compress (ppp->sc_xc_state,
2030 if (new_count > 0) {
2031 ++ppp->stats.ppp_opackets;
2032 ppp->stats.ppp_ooctects += count;
2034 ppp_dev_xmit_lower (ppp, buf, new_data,
2040 * The frame could not be compressed.
2046 * The frame may not be compressed. Update the statistics before the
2047 * count field is destroyed. The frame will be transmitted.
2049 ++ppp->stats.ppp_opackets;
2050 ppp->stats.ppp_ooctects += count;
2052 * Do not compress the protocol id if not possible
2054 if (!(ppp->flags & SC_COMP_PROT) || (proto & 0xFF00)) {
2062 * Do not compress the address/control if not possible.
2064 if (address != PPP_ALLSTATIONS ||
2065 control != PPP_UI ||
2066 !(ppp->flags & SC_COMP_AC)) {
2072 * Go to the escape encoding
2074 ppp_dev_xmit_lower (ppp, buf, data, count, !!(proto & 0xFF00));
2079 * Revise the tty frame for specific protocols.
2083 send_revise_frame (register struct ppp *ppp, u_char *data, int len)
2087 switch (PPP_PROTOCOL (data)) {
2089 * Update the LQR frame with the current MIB information. This saves having
2090 * the daemon read old MIB data from the driver.
2093 len = 48; /* total size of this frame */
2094 p = (u_char *) &data [40]; /* Point to last two items. */
2095 p = store_long (p, ppp->stats.ppp_opackets + 1);
2096 p = store_long (p, ppp->stats.ppp_ooctects + len);
2099 * Outbound compression frames
2103 ppp_proto_ccp (ppp, data, len, 0);
2108 * All other frame types
2118 * write a frame with NR chars from BUF to TTY
2119 * we have to put the FCS field on ourselves
2123 ppp_tty_write (struct tty_struct *tty, struct file *file, u_char * data,
2126 struct ppp *ppp = tty2ppp (tty);
2129 * Verify the pointer to the PPP data and that the tty is still in PPP mode.
2131 if (!ppp || ppp->magic != PPP_MAGIC) {
2132 if (ppp->flags & SC_DEBUG)
2134 "ppp_tty_write: cannot find ppp unit\n");
2140 * Ensure that the caller does not wish to send too much.
2142 if (count > PPP_MTU) {
2143 if (ppp->flags & SC_DEBUG)
2144 printk (KERN_WARNING
2145 "ppp_tty_write: truncating user packet "
2146 "from %u to mtu %d\n", count, PPP_MTU);
2150 * lock this PPP unit so we will be the only writer;
2151 * sleep if necessary
2153 while (lock_buffer (ppp->tbuf) != 0) {
2154 current->timeout = 0;
2155 if (ppp->flags & SC_DEBUG)
2156 printk (KERN_DEBUG "ppp_tty_write: sleeping\n");
2157 interruptible_sleep_on (&ppp->write_wait);
2158 if (current->signal & ~current->blocked)
2162 * Allocate a buffer for the data and fetch it from the user space.
2164 new_data = kmalloc (count, GFP_ATOMIC);
2165 if (new_data == NULL) {
2166 if (ppp->flags & SC_DEBUG)
2168 "ppp_tty_write: no memory\n");
2169 ppp->tbuf->locked = 0;
2173 memcpy_fromfs (new_data, data, count);
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.
2192 ppp_set_compression (struct ppp *ppp, struct ppp_option_data *odp)
2196 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
2197 struct compressor **cp;
2199 * Validate the range of the ioctl
2201 error = verify_area (VERIFY_READ, odp,
2202 (unsigned long) (&((struct ppp_option_data *) 0)->length)
2203 + sizeof (odp->length));
2206 nb = (int) get_fs_long (odp->length);
2207 if ((unsigned long) nb-1 >= (unsigned long) CCP_MAX_OPTION_LENGTH)
2208 nb = CCP_MAX_OPTION_LENGTH;
2210 error = verify_area (VERIFY_READ, odp, nb);
2216 memcpy_fromfs (ccp_option, odp, nb);
2217 if (ccp_option[1] < 2) /* preliminary check on the length byte */
2220 for (cp = ppp_compressors; *cp != NULL; ++cp)
2221 if ((*cp)->compress_proto == ccp_option[0]) {
2223 * Found a handler for the protocol - try to allocate
2224 * a compressor or decompressor.
2227 if (odp->transmit) {
2228 if (ppp->sc_xc_state != NULL)
2229 (*ppp->sc_xcomp->comp_free)(ppp->sc_xc_state);
2231 ppp->sc_xcomp = *cp;
2232 ppp->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
2234 if (ppp->sc_xc_state == NULL) {
2235 if (ppp->flags & SC_DEBUG)
2236 printk("ppp%ld: comp_alloc failed\n",
2237 ppp2dev (ppp)->base_addr);
2240 ppp->flags &= ~SC_COMP_RUN;
2242 if (ppp->sc_rc_state != NULL)
2243 (*ppp->sc_rcomp->decomp_free)(ppp->sc_rc_state);
2244 ppp->sc_rcomp = *cp;
2245 ppp->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
2246 if (ppp->sc_rc_state == NULL) {
2247 if (ppp->flags & SC_DEBUG)
2248 printk("ppp%ld: decomp_alloc failed\n",
2249 ppp2dev (ppp)->base_addr);
2252 ppp->flags &= ~SC_DECOMP_RUN;
2257 if (ppp->flags & SC_DEBUG)
2258 printk (KERN_DEBUG "ppp%ld: no compressor for [%x %x %x], %x\n",
2259 ppp2dev (ppp)->base_addr, ccp_option[0], ccp_option[1],
2261 return (-EINVAL); /* no handler found */
2263 #endif /* PPP_COMPRESS */
2266 * Process the IOCTL event for the tty device.
2270 ppp_tty_ioctl (struct tty_struct *tty, struct file *file, unsigned int param2,
2271 unsigned long param3)
2273 struct ppp *ppp = tty2ppp (tty);
2274 register int temp_i = 0;
2277 * Verify the status of the PPP device.
2279 if (!ppp || ppp->magic != PPP_MAGIC) {
2280 if (ppp->flags & SC_DEBUG)
2282 "ppp_tty_ioctl: can't find PPP block from tty!\n");
2287 * The user must have an euid of root to do these requests.
2296 error = verify_area (VERIFY_READ, (void *) param3,
2299 temp_i = (int) get_fs_long (param3);
2300 if (ppp->flags & SC_DEBUG)
2302 "ppp_tty_ioctl: set mru to %x\n", temp_i);
2304 if (ppp->mru != temp_i)
2305 ppp_changedmtu (ppp, ppp2dev (ppp)->mtu, temp_i);
2312 error = verify_area (VERIFY_WRITE, (void *) param3,
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_fs_long ((long) temp_i, param3);
2321 if (ppp->flags & SC_DEBUG)
2323 "ppp_tty_ioctl: get flags: addr %lx flags "
2324 "%x\n", param3, temp_i);
2328 * Set the flags for the various options
2331 error = verify_area (VERIFY_READ, (void *) param3,
2334 temp_i = (int) get_fs_long (param3) & SC_MASK;
2335 temp_i |= (ppp->flags & ~SC_MASK);
2337 if ((ppp->flags & SC_CCP_OPEN) &&
2338 (temp_i & SC_CCP_OPEN) == 0)
2339 ppp_ccp_closed (ppp);
2341 temp_i &= ~SC_CCP_OPEN;
2343 if ((ppp->flags | temp_i) & SC_DEBUG)
2345 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2346 ppp->flags = temp_i;
2350 * Set the compression mode
2353 case PPPIOCSCOMPRESS:
2354 error = ppp_set_compression (ppp,
2355 (struct ppp_option_data *) param3);
2359 * Retrieve the transmit async map
2361 case PPPIOCGASYNCMAP:
2362 error = verify_area (VERIFY_WRITE, (void *) param3,
2365 put_fs_long (ppp->xmit_async_map[0], param3);
2366 if (ppp->flags & SC_DEBUG)
2368 "ppp_tty_ioctl: get asyncmap: addr "
2369 "%lx asyncmap %lx\n",
2370 param3, ppp->xmit_async_map[0]);
2374 * Set the transmit async map
2376 case PPPIOCSASYNCMAP:
2377 error = verify_area (VERIFY_READ, (void *) param3,
2380 ppp->xmit_async_map[0] = get_fs_long (param3);
2381 if (ppp->flags & SC_DEBUG)
2383 "ppp_tty_ioctl: set xmit asyncmap %lx\n",
2384 ppp->xmit_async_map[0]);
2388 * Set the receive async map
2390 case PPPIOCSRASYNCMAP:
2391 error = verify_area (VERIFY_READ, (void *) param3,
2394 ppp->recv_async_map = get_fs_long (param3);
2395 if (ppp->flags & SC_DEBUG)
2397 "ppp_tty_ioctl: set rcv asyncmap %lx\n",
2398 ppp->recv_async_map);
2402 * Obtain the unit number for this device.
2405 error = verify_area (VERIFY_WRITE, (void *) param3,
2408 put_fs_long (ppp2dev (ppp)->base_addr, param3);
2409 if (ppp->flags & SC_DEBUG)
2411 "ppp_tty_ioctl: get unit: %ld",
2412 ppp2dev (ppp)->base_addr);
2416 * Set the debug level
2419 error = verify_area (VERIFY_READ, (void *) param3,
2422 temp_i = (int) (get_fs_long (param3) & 0x1F) << 16;
2423 temp_i |= (ppp->flags & ~0x1F0000);
2425 if ((ppp->flags | temp_i) & SC_DEBUG)
2427 "ppp_tty_ioctl: set flags to %x\n", temp_i);
2428 ppp->flags = temp_i;
2432 * Get the debug level
2435 error = verify_area (VERIFY_WRITE, (void *) param3,
2438 temp_i = (ppp->flags >> 16) & 0x1F;
2439 put_fs_long ((long) temp_i, param3);
2441 if (ppp->flags & SC_DEBUG)
2443 "ppp_tty_ioctl: get debug level %d\n",
2448 * Get the times since the last send/receive frame operation
2451 error = verify_area (VERIFY_WRITE, (void *) param3,
2452 sizeof (struct ppp_ddinfo));
2454 struct ppp_ddinfo cur_ddinfo;
2455 unsigned long cur_jiffies = jiffies;
2457 /* change absolute times to relative times. */
2458 cur_ddinfo.ip_sjiffies = cur_jiffies - ppp->ddinfo.ip_sjiffies;
2459 cur_ddinfo.ip_rjiffies = cur_jiffies - ppp->ddinfo.ip_rjiffies;
2460 cur_ddinfo.nip_sjiffies = cur_jiffies - ppp->ddinfo.nip_sjiffies;
2461 cur_ddinfo.nip_rjiffies = cur_jiffies - ppp->ddinfo.nip_rjiffies;
2463 memcpy_tofs ((void *) param3, &cur_ddinfo,
2464 sizeof (struct ppp_ddinfo));
2465 if (ppp->flags & SC_DEBUG)
2467 "ppp_tty_ioctl: read demand dial info\n");
2471 * Retrieve the extended async map
2473 case PPPIOCGXASYNCMAP:
2474 error = verify_area (VERIFY_WRITE,
2476 sizeof (ppp->xmit_async_map));
2478 memcpy_tofs ((void *) param3,
2479 ppp->xmit_async_map,
2480 sizeof (ppp->xmit_async_map));
2482 if (ppp->flags & SC_DEBUG)
2484 "ppp_tty_ioctl: get xasyncmap: addr %lx\n",
2489 * Set the async extended map
2491 case PPPIOCSXASYNCMAP:
2492 error = verify_area (VERIFY_READ, (void *) param3,
2493 sizeof (ppp->xmit_async_map));
2495 unsigned long temp_tbl[8];
2497 memcpy_fromfs (temp_tbl, (void *) param3,
2498 sizeof (ppp->xmit_async_map));
2499 temp_tbl[1] = 0x00000000;
2500 temp_tbl[2] &= ~0x40000000;
2501 temp_tbl[3] |= 0x60000000;
2503 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
2504 (temp_tbl[4] & temp_tbl[5]) != 0 ||
2505 (temp_tbl[6] & temp_tbl[7]) != 0)
2508 memcpy (ppp->xmit_async_map, temp_tbl,
2509 sizeof (ppp->xmit_async_map));
2511 if (ppp->flags & SC_DEBUG)
2513 "ppp_tty_ioctl: set xasyncmap\n");
2518 * Set the maximum VJ header compression slot number.
2521 error = verify_area (VERIFY_READ, (void *) param3,
2524 temp_i = (int) get_fs_long (param3) + 1;
2525 if (ppp->flags & SC_DEBUG)
2527 "ppp_tty_ioctl: set maxcid to %d\n",
2529 if (ppp->slcomp != NULL)
2530 slhc_free (ppp->slcomp);
2531 ppp->slcomp = slhc_init (16, temp_i);
2533 if (ppp->slcomp == NULL) {
2534 if (ppp->flags & SC_DEBUG)
2536 "ppp: no space for compression buffers!\n");
2543 * Allow users to read, but not set, the serial port parameters
2547 error = n_tty_ioctl (tty, file, param2, param3);
2550 * All other ioctl() events will come here.
2553 if (ppp->flags & SC_DEBUG)
2555 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
2559 error = -ENOIOCTLCMD;
2568 * Process the select() statement for the PPP device.
2572 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
2573 struct file *filp, int sel_type, select_table * wait)
2575 struct ppp *ppp = tty2ppp (tty);
2578 * Verify the status of the PPP device.
2580 if (!ppp || ppp->magic != PPP_MAGIC) {
2581 if (ppp->flags & SC_DEBUG)
2583 "ppp_tty_select: can't find PPP block from tty!\n");
2588 * Branch on the type of select mode. A read request must lock the user
2593 if (set_bit (0, &ppp->ubuf->locked) == 0) {
2594 /* Test for the presence of data in the queue */
2595 if (ppp->ubuf->head != ppp->ubuf->tail) {
2596 clear_bit (0, &ppp->ubuf->locked);
2599 clear_bit (0, &ppp->ubuf->locked);
2600 } /* fall through */
2602 * Exceptions or read errors.
2605 /* Is this a pty link and the remote disconnected? */
2606 if (tty->flags & (1 << TTY_SLAVE_CLOSED))
2609 /* Is this a local link and the modem disconnected? */
2610 if (tty_hung_up_p (filp))
2613 select_wait (&ppp->read_wait, wait);
2617 * Write mode. A write is allowed if there is no current transmission.
2620 if (ppp->tbuf->locked != 0) {
2621 select_wait (&ppp->write_wait, wait);
2629 /*************************************************************
2631 * This routine accepts requests from the network layer
2632 * and attempts to deliver the packets.
2633 * It also includes various routines we are compelled to
2634 * have to make the network layer work (arp, etc...).
2635 *************************************************************/
2638 * Callback from the network layer when the device goes up.
2642 ppp_dev_open (struct device *dev)
2644 struct ppp *ppp = dev2ppp (dev);
2646 /* reset POINTOPOINT every time, since dev_close zaps it! */
2647 dev->flags |= IFF_POINTOPOINT;
2649 if (ppp2tty (ppp) == NULL) {
2650 if (ppp->flags & SC_DEBUG)
2652 "ppp: %s not connected to a TTY! can't go open!\n",
2657 if (ppp->flags & SC_DEBUG)
2659 "ppp: channel %s going up for IP packets!\n",
2667 * Callback from the network layer when the ppp device goes down.
2671 ppp_dev_close (struct device *dev)
2673 struct ppp *ppp = dev2ppp (dev);
2675 if (ppp2tty (ppp) == NULL) {
2676 if (ppp->flags & SC_DEBUG)
2678 "ppp: %s not connected to a TTY! can't go down!\n",
2683 * We don't do anything about the device going down. It is not important
2686 if (ppp->flags & SC_DEBUG)
2688 "ppp: channel %s going down for IP packets!\n",
2695 * IOCTL operation to read the version of the driver.
2699 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
2705 * Must have write access to the buffer.
2707 result = (char *) ifr->ifr_ifru.ifru_data;
2708 len = strlen (szVersion) + 1;
2709 error = verify_area (VERIFY_WRITE, result, len);
2711 * Move the version data
2714 memcpy_tofs (result, szVersion, len);
2720 * IOCTL to read the statistics for the pppstats program.
2724 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr)
2726 struct ppp_stats *result, temp;
2729 * Must have write access to the buffer.
2731 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
2732 error = verify_area (VERIFY_WRITE,
2736 * Supply the information for the caller. First move the version data
2737 * then move the ppp stats; and finally the vj stats.
2740 memset (&temp, 0, sizeof(temp));
2741 memcpy (&temp.p, &ppp->stats, sizeof (struct pppstat));
2743 * Header Compression statistics
2745 if (ppp->slcomp != NULL) {
2746 temp.vj.vjs_packets = ppp->slcomp->sls_o_nontcp +
2747 ppp->slcomp->sls_o_tcp;
2748 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
2749 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
2750 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
2751 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
2752 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
2753 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
2754 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
2757 * Frame data compression statistics
2760 if (ppp->sc_xc_state != NULL)
2761 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2764 if (ppp->sc_rc_state != NULL)
2765 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2767 #endif /* PPP_COMPRESS */
2770 * Move the data to the caller's buffer
2772 memcpy_tofs (result, &temp, sizeof (temp));
2778 * Callback from the network layer to process the sockioctl functions.
2782 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2784 struct ppp *ppp = dev2ppp (dev);
2787 * Process the requests
2791 error = ppp_dev_ioctl_stats (ppp, ifr);
2795 error = ppp_dev_ioctl_version (ppp, ifr);
2806 * Send an IP frame to the remote with vj header compression.
2808 * Return 0 if frame was queued for transmission.
2809 * 1 if frame must be re-queued for later driver support.
2813 ppp_dev_xmit_ip1 (struct device *dev, struct ppp *ppp, u_char *data)
2817 struct ppp_hdr *hdr;
2818 struct tty_struct *tty = ppp2tty (ppp);
2820 * Obtain the length from the IP header.
2822 len = ((struct iphdr *)data) -> tot_len;
2825 * Validate the tty interface
2828 if (ppp->flags & SC_DEBUG)
2830 "ppp_dev_xmit: %s not connected to a TTY!\n",
2835 * Ensure that the PPP device is still up
2837 if (!(dev->flags & IFF_UP)) {
2838 if (ppp->flags & SC_DEBUG)
2839 printk (KERN_WARNING
2840 "ppp_dev_xmit: packet sent on interface %s,"
2841 " which is down for IP\n",
2846 * Detect a change in the transfer size
2848 if (ppp->mtu != ppp2dev (ppp)->mtu) {
2849 ppp_changedmtu (ppp,
2854 * Acquire the lock on the transmission buffer. If the buffer was busy then
2855 * mark the device as busy and return "failure to send, try back later" error.
2857 if (lock_buffer (ppp->wbuf) != 0) {
2862 * Print the frame being sent
2864 if (ppp->flags & SC_LOG_OUTPKT)
2865 ppp_print_buffer ("ppp outpkt", data, len);
2867 * At this point, the buffer will be transmitted. There is no other exit.
2869 * Try to compress the header.
2871 if (ppp->flags & SC_COMP_TCP) {
2872 len = slhc_compress (ppp->slcomp, data, len,
2873 buf_base (ppp->cbuf) + PPP_HARD_HDR_LEN,
2875 !(ppp->flags & SC_NO_TCP_CCID));
2877 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2878 proto = PPP_VJC_COMP;
2879 data[0] ^= SL_TYPE_COMPRESSED_TCP;
2881 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP)
2882 proto = PPP_VJC_UNCOMP;
2883 data[0] = (data[0] & 0x0f) | 0x40;
2889 len += PPP_HARD_HDR_LEN;
2890 hdr = &((struct ppp_hdr *) data)[-1];
2892 hdr->address = PPP_ALLSTATIONS;
2893 hdr->control = PPP_UI;
2894 hdr->protocol[0] = 0;
2895 hdr->protocol[1] = proto;
2897 return ppp_dev_xmit_frame (ppp, ppp->wbuf, (u_char *) hdr, len);
2901 * This is just an interum solution until the 1.3 kernel's networking is
2902 * available. The 1.2 kernel has problems with device headers before the
2905 * This routine should be deleted, and the ppp_dev_xmit_ip1 routine called
2910 ppp_dev_xmit_ip (struct device *dev, struct ppp *ppp, u_char *data)
2912 struct ppp_hdr *hdr;
2916 len = ((struct iphdr *)data) -> tot_len;
2919 hdr = (struct ppp_hdr *) kmalloc (len + sizeof (struct ppp_hdr),
2925 memcpy (&hdr[1], data, len);
2926 answer = ppp_dev_xmit_ip1 (dev, ppp, (u_char *) &hdr[1]);
2934 * Send an IPX (or any other non-IP) frame to the remote.
2936 * Return 0 if frame was queued for transmission.
2937 * 1 if frame must be re-queued for later driver support.
2941 ppp_dev_xmit_ipx1 (struct device *dev, struct ppp *ppp,
2942 u_char *data, int len, int proto)
2944 struct tty_struct *tty = ppp2tty (ppp);
2945 struct ppp_hdr *hdr;
2947 * Validate the tty interface
2950 if (ppp->flags & SC_DEBUG)
2952 "ppp_dev_xmit: %s not connected to a TTY!\n",
2957 * Ensure that the PPP device is still up
2959 if (!(dev->flags & IFF_UP)) {
2960 if (ppp->flags & SC_DEBUG)
2961 printk (KERN_WARNING
2962 "ppp_dev_xmit: packet sent on interface %s,"
2968 * Detect a change in the transfer size
2970 if (ppp->mtu != ppp2dev (ppp)->mtu) {
2971 ppp_changedmtu (ppp,
2976 * Acquire the lock on the transmission buffer. If the buffer was busy then
2977 * mark the device as busy and return "failure to send, try back later" error.
2979 if (lock_buffer (ppp->wbuf) != 0) {
2984 * Print the frame being sent
2986 if (ppp->flags & SC_LOG_OUTPKT)
2987 ppp_print_buffer ("ppp outpkt", data, len);
2991 len += PPP_HARD_HDR_LEN;
2992 hdr = &((struct ppp_hdr *) data)[-1];
2994 hdr->address = PPP_ALLSTATIONS;
2995 hdr->control = PPP_UI;
2996 hdr->protocol[0] = proto >> 8;
2997 hdr->protocol[1] = proto;
2999 return ppp_dev_xmit_frame (ppp, ppp->wbuf, (u_char *) hdr, len);
3003 * This is just an interum solution until the 1.3 kernel's networking is
3004 * available. The 1.2 kernel has problems with device headers before the
3007 * This routine should be deleted, and the ppp_dev_xmit_ipx1 routine called
3012 ppp_dev_xmit_ipx (struct device *dev, struct ppp *ppp,
3013 u_char *data, int len, int proto)
3015 struct ppp_hdr *hdr;
3018 hdr = (struct ppp_hdr *) kmalloc (len + sizeof (struct ppp_hdr),
3023 memcpy (&hdr[1], data, len);
3024 answer = (dev, ppp, (u_char *) &hdr[1], len, proto);
3032 * Send a frame to the remote.
3036 ppp_dev_xmit (sk_buff *skb, struct device *dev)
3040 struct ppp *ppp = dev2ppp (dev);
3041 struct tty_struct *tty = ppp2tty (ppp);
3043 * just a little sanity check.
3046 if (ppp->flags & SC_DEBUG)
3047 printk (KERN_WARNING "ppp_dev_xmit: null packet!\n");
3051 * Avoid timing problem should tty hangup while data is queued to be sent
3054 dev_kfree_skb (skb, FREE_WRITE);
3059 * Validate the tty linkage
3061 if (ppp->flags & SC_DEBUG)
3062 printk (KERN_DEBUG "ppp_dev_xmit [%s]: skb %X\n",
3063 dev->name, (int) skb);
3065 * Validate the tty interface
3068 if (ppp->flags & SC_DEBUG)
3070 "ppp_dev_xmit: %s not connected to a TTY!\n",
3072 dev_kfree_skb (skb, FREE_WRITE);
3076 * Fetch the pointer to the data
3078 data = skb_data (skb);
3080 * Look at the protocol in the skb to determine the difference between
3081 * an IP frame and an IPX frame.
3085 switch (skb->protocol) {
3086 case htons (ETH_P_IPX):
3087 answer = ppp_dev_xmit_ipx (dev, ppp, data, skb->len, PPP_IPX);
3090 case htons (ETH_P_IP):
3091 answer = ppp_dev_xmit_ip (dev, ppp, data);
3094 default: /* All others have no support at this time. */
3095 dev_kfree_skb (skb, FREE_WRITE);
3099 answer = ppp_dev_xmit_ip (dev, ppp, data);
3103 * This is the end of the transmission. Release the buffer if it was sent.
3106 dev_kfree_skb (skb, FREE_WRITE);
3111 * Generate the statistic information for the /proc/net/dev listing.
3114 static struct enet_statistics *
3115 ppp_dev_stats (struct device *dev)
3117 struct ppp *ppp = dev2ppp (dev);
3118 static struct enet_statistics ppp_stats;
3120 ppp_stats.rx_packets = ppp->stats.ppp_ipackets;
3121 ppp_stats.rx_errors = ppp->stats.ppp_ierrors;
3122 ppp_stats.rx_dropped = ppp->stats.ppp_ierrors;
3123 ppp_stats.rx_fifo_errors = 0;
3124 ppp_stats.rx_length_errors = 0;
3125 ppp_stats.rx_over_errors = 0;
3126 ppp_stats.rx_crc_errors = 0;
3127 ppp_stats.rx_frame_errors = 0;
3128 ppp_stats.tx_packets = ppp->stats.ppp_opackets;
3129 ppp_stats.tx_errors = ppp->stats.ppp_oerrors;
3130 ppp_stats.tx_dropped = 0;
3131 ppp_stats.tx_fifo_errors = 0;
3132 ppp_stats.collisions = 0;
3133 ppp_stats.tx_carrier_errors = 0;
3134 ppp_stats.tx_aborted_errors = 0;
3135 ppp_stats.tx_window_errors = 0;
3136 ppp_stats.tx_heartbeat_errors = 0;
3138 if (ppp->flags & SC_DEBUG)
3139 printk (KERN_INFO "ppp_dev_stats called");
3145 * The PPP protocol is currently pure IP (no IPX yet). This defines
3146 * the protocol layer which is blank since the driver does all the
3150 static int ppp_dev_input (struct protocol *self, struct protocol *lower,
3151 sk_buff *skb, void *saddr, void *daddr)
3153 return protocol_pass_demultiplex(self, NULL, skb, NULL, NULL);
3156 static int ppp_dev_output (struct protocol *self, sk_buff *skb, int type,
3157 int subid, void *saddr, void *daddr, void *opt)
3161 printk("ppp_dev_output: No device.\n");
3162 kfree_skb(skb, FREE_WRITE);
3165 dev_queue_xmit(skb, skb->dev, skb->priority);
3169 static int ppp_dev_getkey(int protocol, int subid, unsigned char *key)
3173 case htons (ETH_P_IP):
3174 case htons (ETH_P_IPX):
3181 return -EAFNOSUPPORT;
3187 * Called to enquire about the type of the frame in the buffer. Return
3188 * ETH_P_IP for an IP frame, ETH_P_IPX for an IPX frame.
3191 static unsigned short
3192 ppp_dev_type (sk_buff *skb, struct device *dev)
3194 return (htons (ETH_P_IP));
3198 ppp_dev_header (u_char * buff, struct device *dev, unsigned short type,
3199 void *daddr, void *saddr, unsigned len, sk_buff *skb)
3205 ppp_dev_rebuild (void *buff, struct device *dev, unsigned long raddr,
3212 /*************************************************************
3214 * Miscellany called by various functions above.
3215 *************************************************************/
3217 /* allocate or create a PPP channel */
3228 /* try to find an free device */
3233 ppp = ctl2ppp (ctl);
3234 if (!set_bit(0, &ppp->inuse))
3237 if (++if_num == INT_MAX)
3241 * There are no available items. Allocate a device from the system pool
3243 ctl = (ppp_ctrl_t *) kmalloc (sizeof(ppp_ctrl_t), GFP_KERNEL);
3245 (void) memset(ctl, 0, sizeof(ppp_ctrl_t));
3246 ppp = ctl2ppp (ctl);
3247 dev = ctl2dev (ctl);
3249 /* initialize channel control data */
3250 set_bit(0, &ppp->inuse);
3257 dev->init = ppp_init_dev;
3258 dev->name = ctl->name;
3259 dev->base_addr = (unsigned long) if_num;
3260 dev->priv = (void *) ppp;
3262 sprintf (dev->name, "ppp%d", if_num);
3264 /* link in the new channel */
3265 ctl->next = ppp_list;
3268 /* register device so that we can be ifconfig'd */
3269 /* ppp_init_dev() will be called as a side-effect */
3271 status = register_netdev (dev);
3273 printk ("registered device %s\n", dev->name);
3278 "ppp_alloc - register_netdev(%s) = %d failure.\n",
3280 /* This one will forever be busy as it is not initialized */
3285 for (i = 0; i < PPP_NRUNIT; i++)
3286 if (!set_bit(0, &ppp_ctrl[i].inuse)) return &ppp_ctrl[i];
3293 * Utility procedures to print a buffer in hex/ascii
3297 ppp_print_hex (register u_char * out, u_char * in, int count)
3299 register u_char next_ch;
3300 static char hex[] = "0123456789ABCDEF";
3302 while (count-- > 0) {
3304 *out++ = hex[(next_ch >> 4) & 0x0F];
3305 *out++ = hex[next_ch & 0x0F];
3311 ppp_print_char (register u_char * out, u_char * in, int count)
3313 register u_char next_ch;
3315 while (count-- > 0) {
3318 if (next_ch < 0x20 || next_ch > 0x7e)
3322 if (next_ch == '%') /* printk/syslogd has a bug !! */
3330 ppp_print_buffer (const u_char * name, u_char * buf, int count)
3334 if (name != (u_char *) NULL)
3335 printk (KERN_DEBUG "ppp: %s, count = %d\n", name, count);
3338 memset (line, 32, 44);
3339 ppp_print_hex (line, buf, 8);
3340 ppp_print_char (&line[8 * 3], buf, 8);
3341 printk (KERN_DEBUG "%s\n", line);
3347 memset (line, 32, 44);
3348 ppp_print_hex (line, buf, count);
3349 ppp_print_char (&line[8 * 3], buf, count);
3350 printk (KERN_DEBUG "%s\n", line);
3354 /*************************************************************
3355 * Module support routines
3356 *************************************************************/
3359 char kernel_version[] = UTS_RELEASE;
3366 /* register our line disciplines */
3367 status = ppp_first_time();
3370 "PPP: ppp_init() failure %d\n", status);
3376 cleanup_module(void)
3379 ppp_ctrl_t *ctl, *next_ctl;
3382 int busy_flag = MOD_IN_USE;
3384 * Ensure that the devices are not in operation.
3389 ppp = ctl2ppp (ctl);
3390 if (ppp->inuse && ppp->tty != NULL) {
3395 dev = ctl2dev (ctl);
3396 if (dev->start || dev->flags & IFF_UP) {
3406 "PPP: device busy, remove delayed\n");
3410 * Release the tty registration of the line dicipline so that no new entries
3413 status = tty_register_ldisc (N_PPP, NULL);
3416 "PPP: Unable to unregister ppp line discipline "
3417 "(err = %d)\n", status);
3420 "PPP: ppp line discipline successfully unregistered\n");
3422 * De-register the devices so that there is no problem with them
3427 ppp = ctl2ppp (ctl);
3428 dev = ctl2dev (ctl);
3431 unregister_netdev (dev);
3433 * Release the storage occupied by the control structures