4 $Id: ppp.c,v 1.3 1994/12/08 02:03:55 paulus Exp $
12 RFC1331: The Point-to-Point Protocol (PPP) for the Transmission of
13 Multi-protocol Datagrams over Point-to-Point Links
19 Flags for this module (any combination is acceptable for testing.):
21 OPTIMIZE_FLAG_TIME - Number of jiffies to force sending of leading flag
22 character. This is normally set to ((HZ * 3) / 2).
23 This is 1.5 seconds. If not defined then the leading
26 CHECK_CHARACTERS - Enable the checking on all received characters for
27 8 data bits, no parity. This adds a small amount of
28 processing for each received character.
30 NEW_SKBUFF - Use NET3.020 sk_buff's
33 /* #define NEW_SKBUFF */
34 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2) /* */
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/types.h>
39 #include <linux/fcntl.h>
40 #include <linux/interrupt.h>
41 #include <linux/ptrace.h>
42 #include <linux/ioport.h>
44 #include <linux/malloc.h>
45 #include <linux/tty.h>
46 #include <linux/errno.h>
47 #include <linux/sched.h> /* to get the struct task_struct */
48 #include <linux/string.h> /* used in new tty drivers */
49 #include <linux/signal.h> /* used in new tty drivers */
50 #include <asm/system.h>
51 #include <asm/bitops.h>
52 #include <asm/segment.h>
54 #include <net/if_arp.h>
55 #include <net/if_route.h>
56 #include <linux/if_ether.h>
57 #include <linux/netdevice.h>
58 #include <linux/skbuff.h>
59 #include <linux/inet.h>
60 #include <linux/ioctl.h>
63 #include <linux/netprotocol.h>
65 #define skb_data(skb) ((skb)->data)
66 typedef struct sk_buff sk_buff;
71 #include <linux/if_arp.h>
73 #include <net/ppp_defs.h>
74 #include <linux/socket.h>
75 #include <net/if_ppp.h>
76 #include <net/if_pppvar.h>
79 #define PPP_IPX 0x2b /* IPX protocol over PPP */
86 static void ppp_init_ctrl_blk (register struct ppp *);
87 static void ppp_kick_tty (struct ppp *, struct ppp_buffer *bfr);
88 static int ppp_doframe (struct ppp *);
89 static int ppp_do_ip (struct ppp *, unsigned short, u_char *, int);
90 static int ppp_us_queue (struct ppp *, unsigned short, u_char *, int);
91 static struct ppp *ppp_alloc (void);
92 static void ppp_print_buffer (const u_char *, u_char *, int, int);
93 extern inline void ppp_stuff_char (struct ppp *ppp,
94 register struct ppp_buffer *buf,
96 extern inline int lock_buffer (register struct ppp_buffer *buf);
98 #define ins_char(pbuf,c) (buf_base(pbuf) [(pbuf)->count++] = (u_char)(c))
101 * The "main" procedure to the ppp device
104 int ppp_init (struct device *);
107 * Network device driver callback routines
110 static int ppp_dev_open (struct device *);
111 static int ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd);
112 static int ppp_dev_close (struct device *);
113 static int ppp_dev_xmit (sk_buff *, struct device *);
114 static struct enet_statistics *ppp_dev_stats (struct device *);
117 static int ppp_dev_input (struct protocol *self, struct protocol *lower,
118 sk_buff *skb, void *saddr, void *daddr);
119 static int ppp_dev_output (struct protocol *self, sk_buff *skb, int type,
120 int subid, void *saddr, void *daddr, void *opt);
121 static int ppp_dev_getkey(int protocol, int subid, unsigned char *key);
123 static int ppp_dev_header (u_char *, struct device *, unsigned short,
124 void *, void *, unsigned, sk_buff *);
125 static int ppp_dev_rebuild (void *, struct device *, unsigned long,
127 static unsigned short ppp_dev_type (sk_buff *, struct device *);
134 static int ppp_tty_read (struct tty_struct *, struct file *, u_char *,
136 static int ppp_tty_write (struct tty_struct *, struct file *, u_char *,
138 static int ppp_tty_ioctl (struct tty_struct *, struct file *, unsigned int,
140 static int ppp_tty_select (struct tty_struct *tty, struct inode *inode,
141 struct file *filp, int sel_type, select_table * wait);
142 static int ppp_tty_open (struct tty_struct *);
143 static void ppp_tty_close (struct tty_struct *);
144 static int ppp_tty_room (struct tty_struct *tty);
145 static void ppp_tty_receive (struct tty_struct *tty, u_char * cp,
146 char *fp, int count);
147 static void ppp_tty_wakeup (struct tty_struct *tty);
150 #define PRINTK(p) printk p ;
151 #define ASSERT(p) if (!p) PRINTK ((KERN_CRIT "assertion failed: " # p))
152 #define PRINTKN(n,p) {if (ppp_debug >= n) PRINTK (p)}
153 #define CHECK_PPP(a) if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return a;}
154 #define CHECK_PPP_VOID() if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return;}
156 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
157 #define in_rmap(ppp,c) ((((unsigned int) (u_char) (c)) < 0x20) && \
158 ppp->recv_async_map & (1 << (c)))
160 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
163 #define BUFFER_TYPE_DEV_RD 0 /* ppp? read buffer */
164 #define BUFFER_TYPE_TTY_WR 1 /* tty? write buffer */
165 #define BUFFER_TYPE_DEV_WR 2 /* ppp? write buffer */
166 #define BUFFER_TYPE_TTY_RD 3 /* tty? read buffer */
167 #define BUFFER_TYPE_VJ 4 /* vj compression buffer */
169 /* Define this string only once for all macro envocations */
170 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
172 static int first_time = 1;
173 static char szVersion[] = PPP_VERSION;
174 static int ppp_debug = 5;
175 static int ppp_debug_netpackets = 0;
176 static struct tty_ldisc ppp_ldisc;
177 static struct ppp ppp_ctrl [PPP_NRUNIT];
180 struct protocol proto_ppp;
183 /* FCS table from RFC1331 */
185 static unsigned short fcstab[256] =
187 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
188 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
189 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
190 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
191 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
192 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
193 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
194 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
195 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
196 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
197 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
198 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
199 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
200 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
201 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
202 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
203 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
204 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
205 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
206 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
207 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
208 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
209 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
210 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
211 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
212 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
213 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
214 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
215 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
216 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
217 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
218 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
221 /*************************************************************
223 *************************************************************/
225 /* called at boot time for each ppp device */
228 ppp_init (struct device *dev)
233 ppp = &ppp_ctrl[dev->base_addr];
238 printk (KERN_INFO "PPP: version %s (%d channels)"
242 "\n", szVersion, PPP_NRUNIT);
245 "TCP compression code copyright 1989 Regents of the "
246 "University of California\n");
248 (void) memset (&ppp_ldisc, 0, sizeof (ppp_ldisc));
249 ppp_ldisc.magic = TTY_LDISC_MAGIC;
250 ppp_ldisc.open = ppp_tty_open;
251 ppp_ldisc.close = ppp_tty_close;
252 ppp_ldisc.read = ppp_tty_read;
253 ppp_ldisc.write = ppp_tty_write;
254 ppp_ldisc.ioctl = ppp_tty_ioctl;
255 ppp_ldisc.select = ppp_tty_select;
256 ppp_ldisc.receive_room = ppp_tty_room;
257 ppp_ldisc.receive_buf = ppp_tty_receive;
258 ppp_ldisc.write_wakeup = ppp_tty_wakeup;
260 i = tty_register_ldisc (N_PPP, &ppp_ldisc);
262 printk (KERN_INFO "PPP line discipline registered.\n");
264 printk (KERN_ERR "error registering line discipline: %d\n", i);
268 memset (&proto_ppp, 0, sizeof (proto_ppp));
270 proto_ppp.name = "PPP";
271 proto_ppp.output = ppp_dev_output;
272 proto_ppp.input = ppp_dev_input;
273 proto_ppp.bh_input = ppp_dev_input;
274 proto_ppp.control_event = default_protocol_control;
275 proto_ppp.get_binding = ppp_dev_getkey;
277 protocol_register(&proto_ppp);
281 /* initialize PPP control block */
282 ppp_init_ctrl_blk (ppp);
284 ppp->line = dev->base_addr;
288 /* clear statistics */
289 memset (&ppp->p, '\0', sizeof (struct ppp_stats));
292 dev->default_protocol = &proto_ppp; /* Our protocol layer is PPP */
294 dev->hard_header = ppp_dev_header;
295 dev->type_trans = ppp_dev_type;
296 dev->rebuild_header = ppp_dev_rebuild;
297 dev->hard_header_len = 0;
302 dev->hard_start_xmit = ppp_dev_xmit;
303 dev->open = ppp_dev_open;
304 dev->stop = ppp_dev_close;
305 dev->get_stats = ppp_dev_stats;
306 dev->do_ioctl = ppp_dev_ioctl;
308 dev->type = ARPHRD_PPP;
310 for (i = 0; i < DEV_NUMBUFFS; i++) {
311 skb_queue_head_init (&dev->buffs[i]);
314 /* New-style flags */
315 dev->flags = IFF_POINTOPOINT;
316 dev->family = AF_INET;
320 dev->pa_alen = sizeof (unsigned long);
326 * Local procedure to initialize the ppp structure
330 ppp_init_ctrl_blk (register struct ppp *ppp)
332 ppp->magic = PPP_MAGIC;
340 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
341 ppp->xmit_async_map[0] = 0xffffffff;
342 ppp->xmit_async_map[3] = 0x60000000;
343 ppp->recv_async_map = 0x00000000;
350 ppp->read_wait = NULL;
351 ppp->write_wait = NULL;
353 #ifdef OPTIMIZE_FLAG_TIME /* ensure flag will always be sent first time */
354 ppp->last_xmit = jiffies - OPTIMIZE_FLAG_TIME;
359 /* clear statistics */
360 memset (&ppp->p, '\0', sizeof (struct ppp_stats));
362 /* Reset the demand dial information */
363 ppp->ddinfo.ip_sjiffies =
364 ppp->ddinfo.ip_rjiffies =
365 ppp->ddinfo.nip_sjiffies =
366 ppp->ddinfo.nip_rjiffies = jiffies;
370 * Routine to allocate a buffer for later use by the driver.
373 static struct ppp_buffer *
374 ppp_alloc_buf (int size, int type)
376 struct ppp_buffer *buf;
378 buf = (struct ppp_buffer *) kmalloc (size + sizeof (struct ppp_buffer),
382 buf->size = size - 1; /* Mask for the buffer size */
388 buf->fcs = PPP_INITFCS;
394 * Routine to release the allocated buffer.
398 ppp_free_buf (struct ppp_buffer *ptr)
406 * Lock the indicated transmit buffer
410 lock_buffer (register struct ppp_buffer *buf)
415 * Save the current state and if free then set it to the "busy" state
424 * Restore the flags and return the previous state. 0 implies success.
426 restore_flags (flags);
431 * MTU has been changed by the IP layer. Unfortunately we are not told
432 * about this, but we spot it ourselves and fix things up. We could be
433 * in an upcall from the tty driver, or in an ip packet queue.
437 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
441 struct ppp_buffer *new_rbuf;
442 struct ppp_buffer *new_wbuf;
443 struct ppp_buffer *new_cbuf;
444 struct ppp_buffer *new_tbuf;
446 struct ppp_buffer *old_rbuf;
447 struct ppp_buffer *old_wbuf;
448 struct ppp_buffer *old_cbuf;
449 struct ppp_buffer *old_tbuf;
453 * Allocate the buffer from the kernel for the data
457 mtu = (new_mtu * 2) + 20;
459 /* RFC 1331, section 7.2 says the minimum value is 1500 bytes */
465 PRINTKN (2, (KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
466 dev->name, new_mtu, new_mru));
468 new_wbuf = ppp_alloc_buf (mtu + 4, BUFFER_TYPE_DEV_WR);
469 new_tbuf = ppp_alloc_buf ((PPP_MTU * 2) + 24, BUFFER_TYPE_TTY_WR);
470 new_rbuf = ppp_alloc_buf (mru + 84, BUFFER_TYPE_DEV_RD);
471 new_cbuf = ppp_alloc_buf (mru + 4, BUFFER_TYPE_VJ);
473 * If the buffers failed to allocate then complain and release the partial
476 if (new_wbuf == NULL || new_tbuf == NULL ||
477 new_rbuf == NULL || new_cbuf == NULL) {
478 PRINTKN (2,(KERN_ERR "ppp: failed to allocate new buffers\n"));
479 ppp_free_buf (new_wbuf);
480 ppp_free_buf (new_tbuf);
481 ppp_free_buf (new_rbuf);
482 ppp_free_buf (new_cbuf);
486 * Update the pointers to the new buffer structures.
489 old_wbuf = ppp->wbuf;
490 old_rbuf = ppp->rbuf;
491 old_cbuf = ppp->cbuf;
492 old_tbuf = ppp->tbuf;
494 ppp->wbuf = new_wbuf;
495 ppp->rbuf = new_rbuf;
496 ppp->cbuf = new_cbuf;
497 ppp->tbuf = new_tbuf;
499 ppp->rbuf->size -= 80; /* reserve space for vj header expansion */
501 dev->mem_start = (unsigned long) buf_base (new_wbuf);
502 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
503 dev->rmem_start = (unsigned long) buf_base (new_rbuf);
504 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
506 * Update the parameters for the new buffer sizes
508 ppp->toss = 0xE0; /* To ignore characters until new FLAG */
509 ppp->escape = 0; /* No pending escape character */
519 ppp->tty->flags &= ~TTY_DO_WRITE_WAKEUP;
520 ppp->flags &= ~SC_XMIT_BUSY;
524 * Release old buffer pointers
526 ppp_free_buf (old_rbuf);
527 ppp_free_buf (old_wbuf);
528 ppp_free_buf (old_cbuf);
529 ppp_free_buf (old_tbuf);
534 * Called to release all of the information in the current PPP structure.
536 * It is called when the ppp device goes down or if it is unable to go
541 ppp_release (struct ppp *ppp)
543 if (ppp->tty != NULL && ppp->tty->disc_data == ppp) {
544 ppp->tty->disc_data = NULL; /* Break the tty->ppp link */
547 ppp->dev->flags &= ~IFF_UP; /* down the device */
548 ppp->dev->flags |= IFF_POINTOPOINT;
550 ppp_free_buf (ppp->rbuf);
551 ppp_free_buf (ppp->wbuf);
552 ppp_free_buf (ppp->cbuf);
553 ppp_free_buf (ppp->ubuf);
554 ppp_free_buf (ppp->tbuf);
566 slhc_free (ppp->slcomp);
576 * Called when the PPP device goes down in response to an ifconfig request.
580 ppp_tty_close (struct tty_struct *tty)
582 struct ppp *ppp = (struct ppp *) tty->disc_data;
584 if (ppp == NULL || ppp->magic != PPP_MAGIC) {
586 (KERN_WARNING "ppp: trying to close unopened tty!\n"));
590 (KERN_INFO "ppp: channel %s closing.\n", ppp->dev->name));
598 * Called when the tty dicipline is switched to PPP.
602 ppp_tty_open (struct tty_struct *tty)
604 struct ppp *ppp = (struct ppp *) tty->disc_data;
606 * There should not be an existing table for this slot.
609 PRINTKN (1, (KERN_ERR
610 "ppp_tty_open: gack! tty already associated to %s!\n",
611 ppp->magic == PPP_MAGIC ? ppp->dev->name
616 * Allocate the structure from the system
620 PRINTKN (1, (KERN_ERR
621 "ppp_tty_open: couldn't allocate ppp channel\n"));
625 * Initialize the control block
627 ppp_init_ctrl_blk (ppp);
629 tty->disc_data = ppp;
631 * Flush any pending characters in the driver and dicipline.
633 if (tty->ldisc.flush_buffer) {
634 tty->ldisc.flush_buffer (tty);
637 if (tty->driver.flush_buffer) {
638 tty->driver.flush_buffer (tty);
641 * Allocate space for the default VJ header compression slots (16)
643 ppp->slcomp = slhc_init (16, 16);
644 if (ppp->slcomp == NULL) {
645 PRINTKN (1, (KERN_ERR
646 "ppp_tty_open: no space for compression buffers!\n"));
651 * Allocate space for the MTU and MRU buffers
653 if (ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru) == 0) {
658 * Allocate space for a user level buffer
660 ppp->ubuf = ppp_alloc_buf (RBUFSIZE, BUFFER_TYPE_TTY_RD);
661 if (ppp->ubuf == NULL) {
662 PRINTKN (1, (KERN_ERR
663 "ppp_tty_open: no space for user receive buffer\n"));
668 PRINTKN (2, (KERN_INFO "ppp: channel %s open\n", ppp->dev->name));
673 * Local function to send the next portion of the buffer.
675 * Called by the tty driver's tty_wakeup function should it be entered
676 * because the partial buffer was transmitted.
678 * Called by kick_tty to send the initial portion of the buffer.
680 * Completion processing of the buffer transmission is handled here.
684 ppp_tty_wakeup_code (struct ppp *ppp, struct tty_struct *tty,
685 struct ppp_buffer *xbuf)
687 register int count, actual;
689 * Prevent re-entrancy by ensuring that this routine is called only once.
692 if (ppp->flags & SC_XMIT_BUSY) {
696 ppp->flags |= SC_XMIT_BUSY;
699 * Send the next block of data to the modem
701 count = xbuf->count - xbuf->tail;
702 actual = tty->driver.write (tty, 0,
703 buf_base (xbuf) + xbuf->tail, count);
705 * Terminate transmission of any block which may have an error.
706 * This could occur should the carrier drop.
709 ppp->p.ppp_oerrors++;
712 ppp->bytes_sent += actual;
715 * If the buffer has been transmitted then clear the indicators.
717 xbuf->tail += actual;
718 if (actual == count) {
720 ppp->flags &= ~SC_XMIT_BUSY;
722 * Complete the transmisson on the current buffer.
726 tty->flags &= ~TTY_DO_WRITE_WAKEUP;
730 * If the completed buffer came from the device write, then complete the
731 * transmission block.
733 if (ppp->dev->flags & IFF_UP) {
739 * Wake up the transmission queue for all completion events.
741 wake_up_interruptible (&ppp->write_wait);
743 * Look at the priorities. Choose a daemon write over the device driver.
752 * If there is a pending buffer then transmit it now.
755 ppp->flags &= ~SC_XMIT_BUSY;
756 ppp_kick_tty (ppp, xbuf);
761 * Clear the re-entry flag
763 ppp->flags &= ~SC_XMIT_BUSY;
767 * This function is called by the tty driver when the transmit buffer has
768 * additional space. It is used by the ppp code to continue to transmit
769 * the current buffer should the buffer have been partially sent.
771 * In addition, it is used to send the first part of the buffer since the
772 * logic and the inter-locking would be identical.
776 ppp_tty_wakeup (struct tty_struct *tty)
778 struct ppp_buffer *xbuf;
779 struct ppp *ppp = (struct ppp *) tty->disc_data;
781 if (!ppp || ppp->magic != PPP_MAGIC) {
782 PRINTKN (1, (KERN_ERR "PPP: write_wakeup called but couldn't "
783 "find PPP struct.\n"));
787 * Ensure that there is a transmission pending. Clear the re-entry flag if
788 * there is no pending buffer. Otherwise, send the buffer.
792 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
794 ppp_tty_wakeup_code (ppp, tty, xbuf);
799 * This function is called to transmit a buffer to the rmeote. The buffer
800 * is placed on the pending queue if there is presently a buffer being
801 * sent or it is transmitted with the aid of ppp_tty_wakeup.
805 ppp_kick_tty (struct ppp *ppp, struct ppp_buffer *xbuf)
814 * Control the flags which are best performed with the interrupts masked.
819 * If the transmitter is busy then place the buffer on the appropriate
822 if (ppp->xbuf != NULL) {
823 if (xbuf->type == BUFFER_TYPE_TTY_WR) {
828 restore_flags (flags);
832 * If the transmitter is not busy then this is the highest priority frame
834 ppp->flags &= ~SC_XMIT_BUSY;
835 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
837 restore_flags (flags);
839 * Do the "tty wakeup_code" to actually send this buffer.
841 ppp_tty_wakeup_code (ppp, ppp->tty, xbuf);
844 /*************************************************************
846 * The following functions handle input that arrives from
847 * the TTY. It recognizes PPP frames and either hands them
848 * to the network layer or queues them for delivery to a
849 * user process reading this TTY.
850 *************************************************************/
852 #ifdef CHECK_CHARACTERS
853 static unsigned paritytab[8] =
855 0x96696996, 0x69969669, 0x69969669, 0x96696996,
856 0x69969669, 0x96696996, 0x96696996, 0x69969669
861 * Callback function from tty driver. Return the amount of space left
862 * in the receiver's buffer to decide if remote transmitter is to be
867 ppp_tty_room (struct tty_struct *tty)
869 return 65536; /* We can handle an infinite amount of data. :-) */
873 * Callback function when data is available at the tty driver.
877 ppp_tty_receive (struct tty_struct *tty, u_char * data, char *flags, int count)
879 register struct ppp *ppp = (struct ppp *) tty->disc_data;
880 register struct ppp_buffer *buf = ppp->rbuf;
883 * Verify the table pointer and ensure that the line is still in PPP dicipline.
885 if (!ppp || ppp->magic != PPP_MAGIC) {
886 PRINTKN (1, ("PPP: handler called but couldn't find "
892 * Print the buffer if desired
894 if (ppp_debug >= 5) {
895 ppp_print_buffer ("receive buffer", data, count, KERNEL_DS);
898 * Collect the character and error condition for the character. Set the toss
899 * flag for the first character error.
901 while (count-- > 0) {
905 if (*flags && ppp->toss == 0) {
911 * Set the flags for 8 data bits and no parity.
913 * Actually, it sets the flags for d7 being 0/1 and parity being even/odd
914 * so that the normal processing would have all flags set at the end of the
915 * session. A missing flag bit would denote an error condition.
917 #ifdef CHECK_CHARACTERS
919 ppp->flags |= SC_RCV_B7_1;
921 ppp->flags |= SC_RCV_B7_0;
924 if (paritytab[chr >> 5] & (1 << (chr & 0x1F))) {
925 ppp->flags |= SC_RCV_ODDP;
927 ppp->flags |= SC_RCV_EVNP;
931 * Branch on the character. Process the escape character. The sequence ESC ESC
932 * is defined to be ESC.
935 case PPP_ESCAPE: /* PPP_ESCAPE: invert bit in next character */
936 ppp->escape = PPP_TRANS;
939 * FLAG. This is the end of the block. If the block terminated by ESC FLAG,
940 * then the block is to be ignored. In addition, characters before the very
941 * first FLAG are also tossed by this procedure.
943 case PPP_FLAG: /* PPP_FLAG: end of frame */
944 ppp->p.ppp_ibytes = ppp->bytes_rcvd;
949 * Process frames which are not to be ignored. If the processing failed,
950 * then clean up the VJ tables.
952 if ((ppp->toss & 0x80) != 0 ||
953 ppp_doframe (ppp) == 0) {
954 slhc_toss (ppp->slcomp);
957 * Reset all indicators for the new frame to follow.
960 buf->fcs = PPP_INITFCS;
965 * All other characters in the data come here. If the character is in the
966 * recieve mask then ignore the character.
969 if (in_rmap (ppp, chr)) {
973 * Adjust the character and if the frame is to be discarded then simply
974 * ignore the character until the ending FLAG is received.
979 if (ppp->toss != 0) {
983 * If the count sent is within reason then store the character, bump the
984 * count, and update the FCS for the character.
986 if (buf->count < buf->size) {
987 buf_base (buf)[buf->count++] = chr;
988 buf->fcs = PPP_FCS (buf->fcs, chr);
992 * The peer sent too much data. Set the flags to discard the current frame
993 * and wait for the re-synchronization FLAG to be sent.
995 ppp->p.ppp_ierrors++;
1002 /* on entry, a received frame is in ppp->rbuf.bufr
1003 check it and dispose as appropriate */
1006 ppp_doframe (struct ppp *ppp)
1009 u_char *data = buf_base (ppp->rbuf);
1010 int count = ppp->rbuf->count;
1012 * If there is a pending error from the receiver then log it and discard
1013 * the damaged frame.
1016 PRINTKN (1, (KERN_WARNING
1017 "ppp_toss: tossing frame, reason = %d\n",
1019 ppp->p.ppp_ierrors++;
1023 * An empty frame is ignored. This occurs if the FLAG sequence precedes and
1024 * follows each frame.
1030 * Print the received data.
1032 if (ppp_debug >= 3) {
1033 ppp_print_buffer ("receive frame", data, count, KERNEL_DS);
1036 * Generate an error if the frame is too small.
1039 PRINTKN (1, (KERN_WARNING
1040 "ppp: got runt ppp frame, %d chars\n", count));
1041 ppp->p.ppp_ierrors++;
1045 * Verify the CRC of the frame and discard the CRC characters from the
1046 * end of the buffer.
1048 if (ppp->rbuf->fcs != PPP_GOODFCS) {
1049 PRINTKN (1, (KERN_WARNING
1050 "ppp: frame with bad fcs, excess = %x\n",
1051 ppp->rbuf->fcs ^ PPP_GOODFCS));
1052 ppp->p.ppp_ierrors++;
1055 count -= 2; /* ignore the fcs characters */
1057 * Ignore the leading ADDRESS and CONTROL fields in the frame.
1059 if ((data[0] == PPP_ALLSTATIONS) && (data[1] == PPP_UI)) {
1064 proto = (u_short) * data++; /* PROTO compressed */
1068 proto = (proto << 8) | (u_short) * data++;
1072 /* Send the frame to the network if the ppp device is up */
1073 if (ppp->dev->flags & IFF_UP) {
1074 if (ppp_do_ip (ppp, proto, data, count)) {
1075 ppp->ddinfo.ip_rjiffies = jiffies;
1080 /* If we got here, it has to go to a user process doing a read,
1082 if (ppp_us_queue (ppp, proto, data, count)) {
1083 ppp->ddinfo.nip_rjiffies = jiffies;
1087 /* couldn't cope. */
1088 PRINTKN (1, (KERN_WARNING
1089 "ppp: dropping packet on the floor: nobody could take it.\n"));
1090 ppp->p.ppp_ierrors++;
1094 /* Examine packet at C, attempt to pass up to net layer.
1095 PROTO is the protocol field from the PPP frame.
1096 Return 1 if could handle it, 0 otherwise. */
1099 ppp_do_ip (struct ppp *ppp, unsigned short proto, u_char * data, int count)
1103 * Log the IP information
1105 PRINTKN (4, (KERN_DEBUG "ppp_do_ip: proto %x len %d first byte %x\n",
1106 (int) proto, count, data[0]));
1108 if (ppp_debug_netpackets) {
1109 PRINTK ((KERN_DEBUG "%s <-- proto %x len %d\n", ppp->dev->name,
1110 (int) proto, count));
1113 * If this is uncompressed IP data then receive the data
1119 * For now, reject the IPX frames. Return 1 to indicate that it has been
1120 * processed so that it is simply discarded.
1125 * Process compressed IP frame. If the remote told us to reject frames then
1126 * do so now. Otherwise ensure that there is space in the buffer.
1129 if (ppp->flags & SC_REJ_COMP_TCP) {
1133 * Uncompress the header. We now can _guarantee_ that there is room.
1135 count = slhc_uncompress (ppp->slcomp, data, count);
1137 ppp->p.ppp_ierrors++;
1138 PRINTKN (1, (KERN_NOTICE
1139 "ppp: error in VJ decompression\n"));
1145 * Process uncompressed IP frame
1147 case PPP_VJC_UNCOMP:
1148 if ((ppp->flags & SC_REJ_COMP_TCP) == 0) {
1149 if (slhc_remember (ppp->slcomp,
1150 data, count) <= 0) {
1151 ppp->p.ppp_ierrors++;
1152 PRINTKN (1, (KERN_NOTICE
1153 "ppp: error in VJ memorizing\n"));
1160 * The frame is not a valid IP frame. Ignore it.
1166 * If debugging net packets then print the information. Process the IP
1169 if (ppp_debug_netpackets && proto == PPP_IP) {
1170 struct iphdr *iph = (struct iphdr *) data;
1172 "%s <-- src %lx dst %lx len %d\n",
1179 * Generate a skb buffer for the new frame.
1181 skb = alloc_skb (count, GFP_ATOMIC);
1184 "ppp_do_ip: packet dropped on %s (no memory)!\n",
1188 * Move the received data from the input buffer to the skb buffer.
1192 skb->len = count; /* Store the length */
1193 skb->dev = ppp->dev; /* We are the device */
1194 memcpy ((u_char *) skb_data(skb), data, count); /* move data */
1196 * Tag the frame and kick it to the proper receive routine
1199 netif_rx (skb); /* Receive the buffer */
1204 /* stuff packet at BUF, length LEN, into the us_rbuff buffer
1205 prepend PROTO information */
1208 ppp_us_queue (struct ppp *ppp, unsigned short proto, u_char * data, int len)
1211 register int current_idx;
1213 * The total length includes the protocol data.
1214 * Lock the user information buffer.
1216 if (set_bit (0, &ppp->ubuf->locked)) {
1217 PRINTKN (1, (KERN_NOTICE "ppp_us_queue: can't get lock\n"));
1220 current_idx = ppp->ubuf->head;
1224 buf_base (ppp->ubuf) [current_idx++] = (u_char) (c);\
1225 current_idx &= ppp->ubuf->size; \
1226 if (current_idx == ppp->ubuf->tail) { \
1232 * Insert the buffer length (not counted), the protocol, and the data
1246 * The frame is complete. Update the head pointer and wakeup the pppd
1249 ppp->ubuf->head = current_idx;
1251 clear_bit (0, &ppp->ubuf->locked); /* Unlock the buffer header */
1252 wake_up_interruptible (&ppp->read_wait); /* select() processing */
1253 if (ppp->tty->fasync != NULL) {
1254 kill_fasync (ppp->tty->fasync, SIGIO); /* SIGIO processing */
1257 PRINTKN (3, (KERN_INFO "ppp: successfully queued %d bytes\n", totlen));
1260 * The buffer is full. Unlock the header and return the failure condition.
1263 clear_bit (0, &ppp->ubuf->locked);
1264 PRINTKN (1, (KERN_NOTICE "ppp_us_queue: ran out of buffer space.\n"));
1268 /*************************************************************
1269 * LINE DISCIPLINE SUPPORT
1270 * The following functions form support user programs
1271 * which read and write data on a TTY with the PPP line
1272 * discipline. Reading is done from a circular queue,
1273 * filled by the lower TTY levels.
1274 *************************************************************/
1276 /* read a PPP frame from the us_rbuff circular buffer,
1277 waiting if necessary
1281 ppp_tty_read (struct tty_struct *tty, struct file *file, u_char * buf,
1284 struct ppp *ppp = (struct ppp *) tty->disc_data;
1290 c = buf_base (ppp->ubuf) [ppp->ubuf->tail++]; \
1291 ppp->ubuf->tail &= ppp->ubuf->size; \
1295 * Validate the pointer to the PPP structure
1297 if (!ppp || ppp->magic != PPP_MAGIC) {
1298 PRINTKN (1, (KERN_ERR
1299 "ppp_tty_read: cannnot find ppp channel\n"));
1304 PRINTKN (4, (KERN_DEBUG "ppp_tty_read: called %x num %u\n",
1308 * Acquire the read lock.
1311 if (set_bit (0, &ppp->ubuf->locked) != 0) {
1312 PRINTKN (3, (KERN_DEBUG
1313 "ppp_tty_read: sleeping(ubuf)\n"));
1315 current->timeout = 0;
1316 current->state = TASK_INTERRUPTIBLE;
1319 if (current->signal & ~current->blocked) {
1325 * Fetch the length of the buffer from the first two bytes.
1327 if (ppp->ubuf->head == ppp->ubuf->tail) {
1336 * If there is no length then wait for the data to arrive.
1340 clear_bit (0, &ppp->ubuf->locked);
1341 if (file->f_flags & O_NONBLOCK) {
1342 PRINTKN (4, (KERN_DEBUG
1343 "ppp_tty_read: no data (EWOULDBLOCK)\n"));
1344 return -EWOULDBLOCK;
1346 current->timeout = 0;
1347 PRINTKN (3, (KERN_DEBUG
1348 "ppp_tty_read: sleeping(read_wait)\n"));
1349 interruptible_sleep_on (&ppp->read_wait);
1350 if (current->signal & ~current->blocked) {
1356 * Reset the time of the last read operation.
1358 ppp->ddinfo.nip_rjiffies = jiffies;
1359 PRINTKN (4, (KERN_DEBUG "ppp_tty_read: len = %d\n", len));
1361 * Ensure that the frame will fit within the caller's buffer. If not, then
1362 * discard the frame from the input buffer and return an error to the caller.
1365 /* Can't copy it, update us_rbuff_head */
1366 PRINTKN (1, (KERN_DEBUG
1367 "ppp: read of %u bytes too small for %d frame\n",
1369 ppp->ubuf->tail += len;
1370 ppp->ubuf->tail &= ppp->ubuf->size;
1371 clear_bit (0, &ppp->ubuf->locked);
1372 ppp->p.ppp_ierrors++;
1376 * Fake the insertion of the ADDRESS and CONTROL information because these
1377 * were not saved in the buffer.
1379 put_fs_byte (PPP_ALLSTATIONS, buf++);
1380 put_fs_byte (PPP_UI, buf++);
1383 * Copy the received data from the buffer to the caller's area.
1385 while (indx-- > 0) {
1387 put_fs_byte (c, buf++);
1390 * Release the lock and return the character count in the buffer area.
1392 clear_bit (0, &ppp->ubuf->locked);
1393 len += 2; /* Account for ADDRESS and CONTROL bytes */
1395 (KERN_DEBUG "ppp_tty_read: passing %d bytes up\n", len));
1401 /* stuff a character into the transmit buffer, using PPP's way of escaping
1403 also, update fcs to take account of new character */
1406 ppp_stuff_char (struct ppp *ppp, register struct ppp_buffer *buf,
1407 register u_char chr)
1410 * The buffer should not be full.
1412 if ((buf->count < 0) || (buf->count > 3000)) {
1413 PRINTK ((KERN_DEBUG "ppp_stuff_char: %x %d\n",
1414 (unsigned int) buf->count,
1415 (unsigned int) buf->count))
1418 * Update the FCS and if the character needs to be escaped, do it.
1420 buf->fcs = PPP_FCS (buf->fcs, chr);
1421 if (in_xmap (ppp, chr)) {
1423 ins_char (buf, PPP_ESCAPE);
1426 * Add the character to the buffer.
1428 ins_char (buf, chr);
1432 * write a frame with NR chars from BUF to TTY
1433 * we have to put the FCS field on ourselves
1437 ppp_tty_write (struct tty_struct *tty, struct file *file, u_char * buf,
1440 struct ppp *ppp = (struct ppp *) tty->disc_data;
1442 unsigned short write_fcs;
1444 * Verify the pointer to the PPP data and that the tty is still in PPP mode.
1446 if (!ppp || ppp->magic != PPP_MAGIC) {
1447 PRINTKN (1,(KERN_ERR "ppp_tty_write: cannot find ppp unit\n"));
1452 * Detech a change in the transfer size
1454 if (ppp->mtu != ppp->dev->mtu) { /* Someone has been ifconfigging */
1455 ppp_changedmtu (ppp,
1460 * Ensure that the caller does not wish to send too much.
1462 if (count > PPP_MTU) {
1463 PRINTKN (1, (KERN_WARNING
1464 "ppp_tty_write: truncating user packet from %u to mtu %d\n",
1471 if (ppp_debug >= 3) {
1472 ppp_print_buffer ("write frame", buf, count, USER_DS);
1475 * lock this PPP unit so we will be the only writer;
1476 * sleep if necessary
1478 while (lock_buffer (ppp->tbuf) != 0) {
1479 current->timeout = 0;
1480 PRINTKN (3, (KERN_DEBUG "ppp_tty_write: sleeping\n"));
1481 interruptible_sleep_on (&ppp->write_wait);
1482 if (current->signal & ~current->blocked) {
1487 * OK, locked. Add the leading FLAG character to the buffer.
1489 PRINTKN (4, (KERN_DEBUG "ppp_tty_write: acquired write lock\n"));
1490 ppp->tbuf->count = 0;
1492 #ifdef OPTIMIZE_FLAG_TIME
1493 if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME) {
1494 ins_char (ppp->tbuf, PPP_FLAG);
1496 ppp->last_xmit = jiffies;
1498 ins_char (ppp->tbuf, PPP_FLAG);
1501 * Add the data for the frame to the buffer.
1503 ppp->tbuf->fcs = PPP_INITFCS;
1505 while (indx-- > 0) {
1506 register char chr = get_fs_byte (buf++);
1507 ppp_stuff_char (ppp, ppp->tbuf, chr);
1510 * Add the trailing CRC and the final flag character
1512 write_fcs = ppp->tbuf->fcs ^ 0xFFFF;
1513 ppp_stuff_char (ppp, ppp->tbuf, write_fcs);
1514 ppp_stuff_char (ppp, ppp->tbuf, write_fcs >> 8);
1516 PRINTKN (4, (KERN_DEBUG "ppp_tty_write: fcs is %hx\n", write_fcs));
1518 * Add the trailing FLAG character
1520 ins_char (ppp->tbuf, PPP_FLAG);
1522 * Update the time and print the data to the debug log.
1524 ppp->ddinfo.nip_sjiffies = jiffies;
1526 if (ppp_debug >= 6) {
1527 ppp_print_buffer ("xmit buffer",
1528 buf_base (ppp->tbuf),
1532 PRINTKN (4, (KERN_DEBUG
1533 "ppp_tty_write: writing %d chars\n", ppp->tbuf->count));
1536 * Start the transmitter and the request is complete.
1538 ppp->p.ppp_obytes += ppp->tbuf->count;
1539 ++ppp->p.ppp_opackets;
1541 ppp_kick_tty (ppp, ppp->tbuf);
1542 return ((int) count);
1546 * Process the IOCTL event for the tty device.
1550 ppp_tty_ioctl (struct tty_struct *tty, struct file *file, unsigned int param2,
1551 unsigned long param3)
1553 struct ppp *ppp = (struct ppp *) tty->disc_data;
1554 register int temp_i = 0;
1557 * Verify the status of the PPP device.
1559 if (!ppp || ppp->magic != PPP_MAGIC) {
1561 "ppp_tty_ioctl: can't find PPP block from tty!\n"));
1566 * The user must have an euid of root to do these requests.
1576 error = verify_area (VERIFY_READ, (void *) param3,
1579 PRINTKN (3, (KERN_INFO
1580 "ppp_tty_ioctl: set mru to %x\n", temp_i));
1581 temp_i = (int) get_fs_long (param3);
1582 if (ppp->mru != temp_i) {
1583 ppp_changedmtu (ppp, ppp->dev->mtu, temp_i);
1591 error = verify_area (VERIFY_WRITE, (void *) param3,
1594 temp_i = (ppp->flags & SC_MASK);
1595 #ifndef CHECK_CHARACTERS /* Don't generate errors if we don't check chars. */
1596 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 |
1597 SC_RCV_ODDP | SC_RCV_EVNP;
1599 put_fs_long ((long) temp_i, param3);
1600 PRINTKN (3, (KERN_DEBUG
1601 "ppp_tty_ioctl: get flags: addr %lx flags %x\n",
1606 * Set the flags for the various options
1609 error = verify_area (VERIFY_READ, (void *) param3,
1612 temp_i = (int) get_fs_long (param3);
1613 ppp->flags ^= ((ppp->flags ^ temp_i) & SC_MASK);
1614 PRINTKN (3, (KERN_INFO
1615 "ppp_tty_ioctl: set flags to %x\n", temp_i));
1619 * Retrieve the transmit async map
1621 case PPPIOCGASYNCMAP:
1622 error = verify_area (VERIFY_WRITE, (void *) param3,
1625 put_fs_long (ppp->xmit_async_map[0], param3);
1626 PRINTKN (3, (KERN_INFO
1627 "ppp_tty_ioctl: get asyncmap: addr %lx asyncmap %lx\n",
1628 param3, ppp->xmit_async_map[0]));
1632 * Set the transmit async map
1634 case PPPIOCSASYNCMAP:
1635 error = verify_area (VERIFY_READ, (void *) param3,
1638 ppp->xmit_async_map[0] = get_fs_long (param3);
1639 PRINTKN (3, (KERN_INFO
1640 "ppp_tty_ioctl: set xmit asyncmap %lx\n",
1641 ppp->xmit_async_map[0]));
1645 * Set the receive async map
1647 case PPPIOCSRASYNCMAP:
1648 error = verify_area (VERIFY_READ, (void *) param3,
1651 ppp->recv_async_map = get_fs_long (param3);
1652 PRINTKN (3, (KERN_INFO
1653 "ppp_tty_ioctl: set rcv asyncmap %lx\n",
1654 ppp->recv_async_map));
1658 * Obtain the unit number for this device.
1661 error = verify_area (VERIFY_WRITE, (void *) param3,
1664 put_fs_long (ppp->dev->base_addr, param3);
1666 (KERN_INFO "ppp_tty_ioctl: get unit: %d",
1667 ppp->dev->base_addr));
1671 * Set the debug level
1674 error = verify_area (VERIFY_READ, (void *) param3,
1677 ppp_debug = (int) get_fs_long (param3);
1678 ppp_debug_netpackets = (ppp_debug & 0xff00) >> 8;
1680 PRINTKN (1, (KERN_INFO
1681 "ppp_tty_ioctl: set debug level %d, netpacket %d\n",
1682 ppp_debug, ppp_debug_netpackets));
1686 * Get the debug level
1689 error = verify_area (VERIFY_WRITE, (void *) param3,
1692 put_fs_long ((long) (ppp_debug |
1693 (ppp_debug_netpackets << 8)),
1696 PRINTKN (3, (KERN_INFO
1697 "ppp_tty_ioctl: get debug level %d\n",
1698 ppp_debug | (ppp_debug_netpackets << 8)));
1702 * Get the times since the last send/receive frame operation
1705 error = verify_area (VERIFY_WRITE, (void *) param3,
1706 sizeof (struct ppp_ddinfo));
1708 struct ppp_ddinfo cur_ddinfo;
1709 unsigned long cur_jiffies = jiffies;
1711 /* change absolute times to relative times. */
1712 cur_ddinfo.ip_sjiffies = cur_jiffies - ppp->ddinfo.ip_sjiffies;
1713 cur_ddinfo.ip_rjiffies = cur_jiffies - ppp->ddinfo.ip_rjiffies;
1714 cur_ddinfo.nip_sjiffies = cur_jiffies - ppp->ddinfo.nip_sjiffies;
1715 cur_ddinfo.nip_rjiffies = cur_jiffies - ppp->ddinfo.nip_rjiffies;
1717 memcpy_tofs ((void *) param3, &cur_ddinfo,
1718 sizeof (struct ppp_ddinfo));
1719 PRINTKN (3, (KERN_INFO
1720 "ppp_tty_ioctl: read demand dial info\n"));
1724 * Retrieve the extended async map
1726 case PPPIOCGXASYNCMAP:
1727 error = verify_area (VERIFY_WRITE,
1729 sizeof (ppp->xmit_async_map));
1731 memcpy_tofs ((void *) param3,
1732 ppp->xmit_async_map,
1733 sizeof (ppp->xmit_async_map));
1734 PRINTKN (3, (KERN_INFO
1735 "ppp_tty_ioctl: get xasyncmap: addr %lx\n",
1740 * Set the async extended map
1742 case PPPIOCSXASYNCMAP:
1743 error = verify_area (VERIFY_READ, (void *) param3,
1744 sizeof (ppp->xmit_async_map));
1746 unsigned long temp_tbl[8];
1748 memcpy_fromfs (temp_tbl, (void *) param3,
1749 sizeof (ppp->xmit_async_map));
1750 temp_tbl[1] = 0x00000000;
1751 temp_tbl[2] &= ~0x40000000;
1752 temp_tbl[3] |= 0x60000000;
1754 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
1755 (temp_tbl[4] & temp_tbl[5]) != 0 ||
1756 (temp_tbl[6] & temp_tbl[7]) != 0) {
1759 memcpy (ppp->xmit_async_map, temp_tbl,
1760 sizeof (ppp->xmit_async_map));
1761 PRINTKN (3, (KERN_INFO
1762 "ppp_tty_ioctl: set xasyncmap\n"));
1767 * Set the maximum VJ header compression slot number.
1770 error = verify_area (VERIFY_READ, (void *) param3,
1773 temp_i = (int) get_fs_long (param3) + 1;
1774 PRINTKN (3, (KERN_INFO
1775 "ppp_tty_ioctl: set maxcid to %d\n",
1777 if (ppp->slcomp != NULL) {
1778 slhc_free (ppp->slcomp);
1780 ppp->slcomp = slhc_init (temp_i, temp_i);
1782 if (ppp->slcomp == NULL) {
1783 PRINTKN (1, (KERN_ERR
1784 "ppp: no space for compression buffers!\n"));
1791 * Allow users to read, but not set, the serial port parameters
1795 error = n_tty_ioctl (tty, file, param2, param3);
1798 * All other ioctl() events will come here.
1801 PRINTKN (1, (KERN_ERR
1802 "ppp_tty_ioctl: invalid ioctl: %x, addr %lx\n",
1806 error = -ENOIOCTLCMD;
1815 * Process the select() statement for the PPP device.
1819 ppp_tty_select (struct tty_struct *tty, struct inode *inode,
1820 struct file *filp, int sel_type, select_table * wait)
1822 struct ppp *ppp = (struct ppp *) tty->disc_data;
1825 * Verify the status of the PPP device.
1827 if (!ppp || ppp->magic != PPP_MAGIC) {
1829 "ppp_tty_select: can't find PPP block from tty!\n"));
1834 * Branch on the type of select mode. A read request must lock the user
1839 if (set_bit (0, &ppp->ubuf->locked) == 0) {
1840 /* Test for the presence of data in the queue */
1841 if (ppp->ubuf->head != ppp->ubuf->tail) {
1842 clear_bit (0, &ppp->ubuf->locked);
1845 clear_bit (0, &ppp->ubuf->locked);
1846 } /* fall through */
1848 * Exceptions or read errors.
1851 /* Is this a pty link and the remote disconnected? */
1852 if (tty->flags & (1 << TTY_SLAVE_CLOSED)) {
1855 /* Is this a local link and the modem disconnected? */
1856 if (tty_hung_up_p (filp)) {
1859 select_wait (&ppp->read_wait, wait);
1863 * Write mode. A write is allowed if there is no current transmission.
1866 if (ppp->tbuf->locked != 0) {
1867 select_wait (&ppp->write_wait, wait);
1876 /*************************************************************
1878 * This routine accepts requests from the network layer
1879 * and attempts to deliver the packets.
1880 * It also includes various routines we are compelled to
1881 * have to make the network layer work (arp, etc...).
1882 *************************************************************/
1885 * Callback from the network layer when the device goes up.
1889 ppp_dev_open (struct device *dev)
1891 struct ppp *ppp = &ppp_ctrl[dev->base_addr];
1893 /* reset POINTOPOINT every time, since dev_close zaps it! */
1894 dev->flags |= IFF_POINTOPOINT;
1896 if (ppp->tty == NULL) {
1898 (KERN_ERR "ppp: %s not connected to a TTY! can't go open!\n",
1903 PRINTKN (2, (KERN_INFO "ppp: channel %s going up for IP packets!\n",
1911 * Callback from the network layer when the ppp device goes down.
1915 ppp_dev_close (struct device *dev)
1917 struct ppp *ppp = &ppp_ctrl[dev->base_addr];
1919 if (ppp->tty == NULL) {
1921 (KERN_ERR "ppp: %s not connected to a TTY! can't go down!\n",
1926 * We don't do anything about the device going down. It is not important
1929 PRINTKN (2, (KERN_INFO "ppp: channel %s going down for IP packets!\n",
1936 * IOCTL operation to read the version of the driver.
1940 ppp_dev_ioctl_version (struct ppp *ppp, struct ifreq *ifr)
1946 * Must have write access to the buffer.
1948 result = (char *) ifr->ifr_ifru.ifru_data;
1949 len = strlen (szVersion) + 1;
1950 error = verify_area (VERIFY_WRITE, result, len);
1952 * Move the version data
1955 memcpy_tofs (result, szVersion, len);
1961 * IOCTL to read the statistics for the pppstats program.
1965 ppp_dev_ioctl_stats (struct ppp *ppp, struct ifreq *ifr)
1967 struct ppp_stats *result, temp;
1970 * Must have write access to the buffer.
1972 result = (struct ppp_stats *) ifr->ifr_ifru.ifru_data;
1973 error = verify_area (VERIFY_WRITE,
1977 * Supply the information for the caller. First move the version data
1978 * then move the ppp stats; and finally the vj stats.
1981 memset (&temp, 0, sizeof(temp));
1982 memcpy (&temp.p, &ppp->p, sizeof (struct pppstat));
1984 * Header Compression statistics
1986 if (ppp->slcomp != NULL) {
1987 temp.vj.vjs_packets = ppp->slcomp->sls_o_nontcp +
1988 ppp->slcomp->sls_o_tcp;
1989 temp.vj.vjs_compressed = ppp->slcomp->sls_o_compressed;
1990 temp.vj.vjs_searches = ppp->slcomp->sls_o_searches;
1991 temp.vj.vjs_misses = ppp->slcomp->sls_o_misses;
1992 temp.vj.vjs_errorin = ppp->slcomp->sls_i_error;
1993 temp.vj.vjs_tossed = ppp->slcomp->sls_i_tossed;
1994 temp.vj.vjs_uncompressedin = ppp->slcomp->sls_i_uncompressed;
1995 temp.vj.vjs_compressedin = ppp->slcomp->sls_i_compressed;
1998 * Frame data compression statistics
2001 if (ppp->sc_xc_state != NULL)
2002 (*ppp->sc_xcomp->comp_stat) (ppp->sc_xc_state,
2005 if (ppp->sc_rc_state != NULL)
2006 (*ppp->sc_rcomp->decomp_stat) (ppp->sc_rc_state,
2008 #endif /* PPP_COMPRESS */
2011 * Move the data to the caller's buffer
2013 memcpy_tofs (result, &temp, sizeof (temp));
2019 * Callback from the network layer to process the sockioctl functions.
2023 ppp_dev_ioctl (struct device *dev, struct ifreq *ifr, int cmd)
2025 struct ppp *ppp = &ppp_ctrl[dev->base_addr];
2028 * Process the requests
2032 error = ppp_dev_ioctl_stats (ppp, ifr);
2036 error = ppp_dev_ioctl_version (ppp, ifr);
2047 * Send a frame to the remote.
2051 ppp_dev_xmit (sk_buff *skb, struct device *dev)
2053 struct tty_struct *tty;
2056 unsigned short proto;
2058 unsigned short write_fcs;
2060 * just a little sanity check.
2063 PRINTKN (3, (KERN_WARNING "ppp_dev_xmit: null packet!\n"));
2067 * Fetch the poitners to the data
2069 ppp = &ppp_ctrl[dev->base_addr];
2071 data = (u_char *) (&skb[1]);
2075 PRINTKN (4, (KERN_DEBUG "ppp_dev_xmit [%s]: skb %lX busy %d\n",
2077 (unsigned long int) skb, ppp->wbuf->locked));
2081 * Validate the tty interface
2086 (KERN_ERR "ppp_dev_xmit: %s not connected to a TTY!\n",
2091 * Ensure that the PPP device is still up
2093 if (!(dev->flags & IFF_UP)) {
2094 PRINTKN (1, (KERN_WARNING
2095 "ppp_dev_xmit: packet sent on interface %s,"
2096 " which is down for IP\n",
2101 * Detect a change in the transfer size
2103 if (ppp->mtu != ppp->dev->mtu) {
2104 ppp_changedmtu (ppp,
2109 * Fetch the length from the IP header.
2111 if (len < sizeof (struct iphdr)) {
2112 PRINTKN (0, (KERN_ERR
2113 "ppp_dev_xmit: given runt packet, ignoring\n"));
2116 len = ntohs (((struct iphdr *) skb_data(skb))->tot_len);
2118 * Acquire the lock on the transmission buffer. If the buffer was busy then
2119 * mark the device as busy and return "failure to send, try back later" error.
2121 if (lock_buffer (ppp->wbuf) != 0) {
2126 * At this point, the buffer will be transmitted. There is no other exit.
2128 * Try to compress the header.
2130 if (ppp->flags & SC_COMP_TCP) {
2131 /* last 0 argument says don't compress connection ID */
2132 len = slhc_compress (ppp->slcomp, data, len,
2133 buf_base (ppp->cbuf),
2136 if (data[0] & SL_TYPE_COMPRESSED_TCP) {
2137 proto = PPP_VJC_COMP;
2139 if (data[0] >= SL_TYPE_UNCOMPRESSED_TCP) {
2140 proto = PPP_VJC_UNCOMP;
2141 data[0] = (data[0] & 0x0f) | 0x40;
2146 if (ppp_debug_netpackets) {
2147 struct iphdr *iph = (struct iphdr *) skb_data(skb);
2148 PRINTK ((KERN_DEBUG "%s ==> proto %x len %d src %x "
2149 "dst %x proto %d\n",
2150 dev->name, (int) proto, (int) len, (int) iph->saddr,
2151 (int) iph->daddr, (int) iph->protocol))
2154 * Insert the leading FLAG character
2156 ppp->wbuf->count = 0;
2158 #ifdef OPTIMIZE_FLAG_TIME
2159 if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME) {
2160 ins_char (ppp->wbuf, PPP_FLAG);
2162 ppp->last_xmit = jiffies;
2164 ins_char (ppp->wbuf, PPP_FLAG);
2167 ppp->wbuf->fcs = PPP_INITFCS;
2169 * Insert the address and control data
2171 if (!(ppp->flags & SC_COMP_AC)) {
2172 ppp_stuff_char (ppp, ppp->wbuf, PPP_ALLSTATIONS);
2173 ppp_stuff_char (ppp, ppp->wbuf, PPP_UI);
2176 * Insert the protocol.
2178 if (!(ppp->flags & SC_COMP_PROT) || (proto & 0xff00)) {
2179 ppp_stuff_char (ppp, ppp->wbuf, proto >> 8);
2181 ppp_stuff_char (ppp, ppp->wbuf, proto);
2186 ppp_stuff_char (ppp, ppp->wbuf, *data++);
2189 * Add the trailing CRC and the final flag character
2191 write_fcs = ppp->wbuf->fcs ^ 0xFFFF;
2192 ppp_stuff_char (ppp, ppp->wbuf, write_fcs);
2193 ppp_stuff_char (ppp, ppp->wbuf, write_fcs >> 8);
2196 (KERN_DEBUG "ppp_dev_xmit: fcs is %hx\n", write_fcs));
2198 * Add the trailing flag character
2200 ins_char (ppp->wbuf, PPP_FLAG);
2202 * Update the times for the transmission.
2204 ppp->ddinfo.ip_sjiffies = jiffies;
2208 if (ppp_debug >= 6) {
2209 ppp_print_buffer ("xmit buffer", buf_base (ppp->wbuf),
2210 ppp->wbuf->count, KERNEL_DS);
2212 PRINTKN (4, (KERN_DEBUG
2213 "ppp_dev_xmit: writing %d chars\n",
2217 * Send the block to the tty driver.
2219 ppp->p.ppp_obytes += ppp->wbuf->count;
2220 ++ppp->p.ppp_opackets;
2221 ppp_kick_tty (ppp, ppp->wbuf);
2225 * This is the end of the transmission. Release the buffer.
2227 dev_kfree_skb (skb, FREE_WRITE);
2231 static struct enet_statistics *
2232 ppp_dev_stats (struct device *dev)
2234 struct ppp *ppp = &ppp_ctrl[dev->base_addr];
2235 static struct enet_statistics ppp_stats;
2237 ppp_stats.rx_packets = ppp->p.ppp_ipackets;
2238 ppp_stats.rx_errors = ppp->p.ppp_ierrors;
2239 ppp_stats.rx_dropped = ppp->p.ppp_ierrors;
2240 ppp_stats.rx_fifo_errors = 0;
2241 ppp_stats.rx_length_errors = 0;
2242 ppp_stats.rx_over_errors = 0;
2243 ppp_stats.rx_crc_errors = 0;
2244 ppp_stats.rx_frame_errors = 0;
2245 ppp_stats.tx_packets = ppp->p.ppp_opackets;
2246 ppp_stats.tx_errors = ppp->p.ppp_oerrors;
2247 ppp_stats.tx_dropped = 0;
2248 ppp_stats.tx_fifo_errors = 0;
2249 ppp_stats.collisions = 0;
2250 ppp_stats.tx_carrier_errors = 0;
2251 ppp_stats.tx_aborted_errors = 0;
2252 ppp_stats.tx_window_errors = 0;
2253 ppp_stats.tx_heartbeat_errors = 0;
2255 PRINTKN (3, (KERN_INFO "ppp_dev_stats called"));
2261 * The PPP protocol is currently pure IP (no IPX yet). This defines
2262 * the protocol layer which is blank since the driver does all the
2266 static int ppp_dev_input (struct protocol *self, struct protocol *lower,
2267 sk_buff *skb, void *saddr, void *daddr)
2269 return protocol_pass_demultiplex(self, NULL, skb, NULL, NULL);
2272 static int ppp_dev_output (struct protocol *self, sk_buff *skb, int type,
2273 int subid, void *saddr, void *daddr, void *opt)
2277 printk("ppp_dev_output: No device.\n");
2278 kfree_skb(skb, FREE_WRITE);
2281 dev_queue_xmit(skb, skb->dev, skb->priority);
2285 static int ppp_dev_getkey(int protocol, int subid, unsigned char *key)
2296 return -EAFNOSUPPORT;
2302 * Called to enquire about the type of the frame in the buffer. Return
2303 * ETH_P_IP for an IP frame, ETH_P_IPX for an IPX frame.
2306 static unsigned short
2307 ppp_dev_type (sk_buff *skb, struct device *dev)
2309 return (htons (ETH_P_IP));
2313 ppp_dev_header (u_char * buff, struct device *dev, unsigned short type,
2314 void *daddr, void *saddr, unsigned len, sk_buff *skb)
2320 ppp_dev_rebuild (void *buff, struct device *dev, unsigned long raddr,
2327 /*************************************************************
2329 * Miscellany called by various functions above.
2330 *************************************************************/
2332 /* allocate a PPP channel */
2337 for (i = 0; i < PPP_NRUNIT; i++) {
2338 if (!set_bit (0, &ppp_ctrl[i].inuse)) {
2339 return &ppp_ctrl[i];
2346 * Utility procedures to print a buffer in hex/ascii
2350 ppp_print_hex (register u_char * out, u_char * in, int count)
2352 register u_char next_ch;
2353 static char hex[] = "0123456789ABCDEF";
2355 while (count-- > 0) {
2356 next_ch = (u_char) get_fs_byte (in);
2357 *out++ = hex[(next_ch >> 4) & 0x0F];
2358 *out++ = hex[next_ch & 0x0F];
2365 ppp_print_char (register u_char * out, u_char * in, int count)
2367 register u_char next_ch;
2369 while (count-- > 0) {
2370 next_ch = (u_char) get_fs_byte (in);
2372 if (next_ch < 0x20 || next_ch > 0x7e) {
2376 if (next_ch == '%') { /* printk/syslogd has a bug !! */
2386 ppp_print_buffer (const u_char * name, u_char * buf, int count, int seg)
2389 int old_fs = get_fs ();
2393 if (name != (u_char *) NULL) {
2394 PRINTK ((KERN_DEBUG "ppp: %s, count = %d\n", name, count));
2397 memset (line, ' ', sizeof (line));
2398 ppp_print_hex (line, buf, 8);
2399 ppp_print_char (&line[8 * 3], buf, 8);
2400 PRINTK ((KERN_DEBUG "%s\n", line));
2406 memset (line, ' ', sizeof (line));
2407 ppp_print_hex (line, buf, count);
2408 ppp_print_char (&line[8 * 3], buf, count);
2409 PRINTK ((KERN_DEBUG "%s\n", line));