xref: /openbmc/linux/drivers/net/ppp/ppp_async.c (revision 44f57d78)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PPP async serial channel driver for Linux.
4  *
5  * Copyright 1999 Paul Mackerras.
6  *
7  * This driver provides the encapsulation and framing for sending
8  * and receiving PPP frames over async serial lines.  It relies on
9  * the generic PPP layer to give it frames to send and to process
10  * received frames.  It implements the PPP line discipline.
11  *
12  * Part of the code in this driver was inspired by the old async-only
13  * PPP driver, written by Michael Callahan and Al Longyear, and
14  * subsequently hacked by Paul Mackerras.
15  */
16 
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/skbuff.h>
20 #include <linux/tty.h>
21 #include <linux/netdevice.h>
22 #include <linux/poll.h>
23 #include <linux/crc-ccitt.h>
24 #include <linux/ppp_defs.h>
25 #include <linux/ppp-ioctl.h>
26 #include <linux/ppp_channel.h>
27 #include <linux/spinlock.h>
28 #include <linux/init.h>
29 #include <linux/interrupt.h>
30 #include <linux/jiffies.h>
31 #include <linux/slab.h>
32 #include <asm/unaligned.h>
33 #include <linux/uaccess.h>
34 #include <asm/string.h>
35 
36 #define PPP_VERSION	"2.4.2"
37 
38 #define OBUFSIZE	4096
39 
40 /* Structure for storing local state. */
41 struct asyncppp {
42 	struct tty_struct *tty;
43 	unsigned int	flags;
44 	unsigned int	state;
45 	unsigned int	rbits;
46 	int		mru;
47 	spinlock_t	xmit_lock;
48 	spinlock_t	recv_lock;
49 	unsigned long	xmit_flags;
50 	u32		xaccm[8];
51 	u32		raccm;
52 	unsigned int	bytes_sent;
53 	unsigned int	bytes_rcvd;
54 
55 	struct sk_buff	*tpkt;
56 	int		tpkt_pos;
57 	u16		tfcs;
58 	unsigned char	*optr;
59 	unsigned char	*olim;
60 	unsigned long	last_xmit;
61 
62 	struct sk_buff	*rpkt;
63 	int		lcp_fcs;
64 	struct sk_buff_head rqueue;
65 
66 	struct tasklet_struct tsk;
67 
68 	refcount_t	refcnt;
69 	struct completion dead;
70 	struct ppp_channel chan;	/* interface to generic ppp layer */
71 	unsigned char	obuf[OBUFSIZE];
72 };
73 
74 /* Bit numbers in xmit_flags */
75 #define XMIT_WAKEUP	0
76 #define XMIT_FULL	1
77 #define XMIT_BUSY	2
78 
79 /* State bits */
80 #define SC_TOSS		1
81 #define SC_ESCAPE	2
82 #define SC_PREV_ERROR	4
83 
84 /* Bits in rbits */
85 #define SC_RCV_BITS	(SC_RCV_B7_1|SC_RCV_B7_0|SC_RCV_ODDP|SC_RCV_EVNP)
86 
87 static int flag_time = HZ;
88 module_param(flag_time, int, 0);
89 MODULE_PARM_DESC(flag_time, "ppp_async: interval between flagged packets (in clock ticks)");
90 MODULE_LICENSE("GPL");
91 MODULE_ALIAS_LDISC(N_PPP);
92 
93 /*
94  * Prototypes.
95  */
96 static int ppp_async_encode(struct asyncppp *ap);
97 static int ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb);
98 static int ppp_async_push(struct asyncppp *ap);
99 static void ppp_async_flush_output(struct asyncppp *ap);
100 static void ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
101 			    char *flags, int count);
102 static int ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd,
103 			   unsigned long arg);
104 static void ppp_async_process(unsigned long arg);
105 
106 static void async_lcp_peek(struct asyncppp *ap, unsigned char *data,
107 			   int len, int inbound);
108 
109 static const struct ppp_channel_ops async_ops = {
110 	.start_xmit = ppp_async_send,
111 	.ioctl      = ppp_async_ioctl,
112 };
113 
114 /*
115  * Routines implementing the PPP line discipline.
116  */
117 
118 /*
119  * We have a potential race on dereferencing tty->disc_data,
120  * because the tty layer provides no locking at all - thus one
121  * cpu could be running ppp_asynctty_receive while another
122  * calls ppp_asynctty_close, which zeroes tty->disc_data and
123  * frees the memory that ppp_asynctty_receive is using.  The best
124  * way to fix this is to use a rwlock in the tty struct, but for now
125  * we use a single global rwlock for all ttys in ppp line discipline.
126  *
127  * FIXME: this is no longer true. The _close path for the ldisc is
128  * now guaranteed to be sane.
129  */
130 static DEFINE_RWLOCK(disc_data_lock);
131 
132 static struct asyncppp *ap_get(struct tty_struct *tty)
133 {
134 	struct asyncppp *ap;
135 
136 	read_lock(&disc_data_lock);
137 	ap = tty->disc_data;
138 	if (ap != NULL)
139 		refcount_inc(&ap->refcnt);
140 	read_unlock(&disc_data_lock);
141 	return ap;
142 }
143 
144 static void ap_put(struct asyncppp *ap)
145 {
146 	if (refcount_dec_and_test(&ap->refcnt))
147 		complete(&ap->dead);
148 }
149 
150 /*
151  * Called when a tty is put into PPP line discipline. Called in process
152  * context.
153  */
154 static int
155 ppp_asynctty_open(struct tty_struct *tty)
156 {
157 	struct asyncppp *ap;
158 	int err;
159 	int speed;
160 
161 	if (tty->ops->write == NULL)
162 		return -EOPNOTSUPP;
163 
164 	err = -ENOMEM;
165 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
166 	if (!ap)
167 		goto out;
168 
169 	/* initialize the asyncppp structure */
170 	ap->tty = tty;
171 	ap->mru = PPP_MRU;
172 	spin_lock_init(&ap->xmit_lock);
173 	spin_lock_init(&ap->recv_lock);
174 	ap->xaccm[0] = ~0U;
175 	ap->xaccm[3] = 0x60000000U;
176 	ap->raccm = ~0U;
177 	ap->optr = ap->obuf;
178 	ap->olim = ap->obuf;
179 	ap->lcp_fcs = -1;
180 
181 	skb_queue_head_init(&ap->rqueue);
182 	tasklet_init(&ap->tsk, ppp_async_process, (unsigned long) ap);
183 
184 	refcount_set(&ap->refcnt, 1);
185 	init_completion(&ap->dead);
186 
187 	ap->chan.private = ap;
188 	ap->chan.ops = &async_ops;
189 	ap->chan.mtu = PPP_MRU;
190 	speed = tty_get_baud_rate(tty);
191 	ap->chan.speed = speed;
192 	err = ppp_register_channel(&ap->chan);
193 	if (err)
194 		goto out_free;
195 
196 	tty->disc_data = ap;
197 	tty->receive_room = 65536;
198 	return 0;
199 
200  out_free:
201 	kfree(ap);
202  out:
203 	return err;
204 }
205 
206 /*
207  * Called when the tty is put into another line discipline
208  * or it hangs up.  We have to wait for any cpu currently
209  * executing in any of the other ppp_asynctty_* routines to
210  * finish before we can call ppp_unregister_channel and free
211  * the asyncppp struct.  This routine must be called from
212  * process context, not interrupt or softirq context.
213  */
214 static void
215 ppp_asynctty_close(struct tty_struct *tty)
216 {
217 	struct asyncppp *ap;
218 
219 	write_lock_irq(&disc_data_lock);
220 	ap = tty->disc_data;
221 	tty->disc_data = NULL;
222 	write_unlock_irq(&disc_data_lock);
223 	if (!ap)
224 		return;
225 
226 	/*
227 	 * We have now ensured that nobody can start using ap from now
228 	 * on, but we have to wait for all existing users to finish.
229 	 * Note that ppp_unregister_channel ensures that no calls to
230 	 * our channel ops (i.e. ppp_async_send/ioctl) are in progress
231 	 * by the time it returns.
232 	 */
233 	if (!refcount_dec_and_test(&ap->refcnt))
234 		wait_for_completion(&ap->dead);
235 	tasklet_kill(&ap->tsk);
236 
237 	ppp_unregister_channel(&ap->chan);
238 	kfree_skb(ap->rpkt);
239 	skb_queue_purge(&ap->rqueue);
240 	kfree_skb(ap->tpkt);
241 	kfree(ap);
242 }
243 
244 /*
245  * Called on tty hangup in process context.
246  *
247  * Wait for I/O to driver to complete and unregister PPP channel.
248  * This is already done by the close routine, so just call that.
249  */
250 static int ppp_asynctty_hangup(struct tty_struct *tty)
251 {
252 	ppp_asynctty_close(tty);
253 	return 0;
254 }
255 
256 /*
257  * Read does nothing - no data is ever available this way.
258  * Pppd reads and writes packets via /dev/ppp instead.
259  */
260 static ssize_t
261 ppp_asynctty_read(struct tty_struct *tty, struct file *file,
262 		  unsigned char __user *buf, size_t count)
263 {
264 	return -EAGAIN;
265 }
266 
267 /*
268  * Write on the tty does nothing, the packets all come in
269  * from the ppp generic stuff.
270  */
271 static ssize_t
272 ppp_asynctty_write(struct tty_struct *tty, struct file *file,
273 		   const unsigned char *buf, size_t count)
274 {
275 	return -EAGAIN;
276 }
277 
278 /*
279  * Called in process context only. May be re-entered by multiple
280  * ioctl calling threads.
281  */
282 
283 static int
284 ppp_asynctty_ioctl(struct tty_struct *tty, struct file *file,
285 		   unsigned int cmd, unsigned long arg)
286 {
287 	struct asyncppp *ap = ap_get(tty);
288 	int err, val;
289 	int __user *p = (int __user *)arg;
290 
291 	if (!ap)
292 		return -ENXIO;
293 	err = -EFAULT;
294 	switch (cmd) {
295 	case PPPIOCGCHAN:
296 		err = -EFAULT;
297 		if (put_user(ppp_channel_index(&ap->chan), p))
298 			break;
299 		err = 0;
300 		break;
301 
302 	case PPPIOCGUNIT:
303 		err = -EFAULT;
304 		if (put_user(ppp_unit_number(&ap->chan), p))
305 			break;
306 		err = 0;
307 		break;
308 
309 	case TCFLSH:
310 		/* flush our buffers and the serial port's buffer */
311 		if (arg == TCIOFLUSH || arg == TCOFLUSH)
312 			ppp_async_flush_output(ap);
313 		err = n_tty_ioctl_helper(tty, file, cmd, arg);
314 		break;
315 
316 	case FIONREAD:
317 		val = 0;
318 		if (put_user(val, p))
319 			break;
320 		err = 0;
321 		break;
322 
323 	default:
324 		/* Try the various mode ioctls */
325 		err = tty_mode_ioctl(tty, file, cmd, arg);
326 	}
327 
328 	ap_put(ap);
329 	return err;
330 }
331 
332 /* No kernel lock - fine */
333 static __poll_t
334 ppp_asynctty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
335 {
336 	return 0;
337 }
338 
339 /* May sleep, don't call from interrupt level or with interrupts disabled */
340 static void
341 ppp_asynctty_receive(struct tty_struct *tty, const unsigned char *buf,
342 		  char *cflags, int count)
343 {
344 	struct asyncppp *ap = ap_get(tty);
345 	unsigned long flags;
346 
347 	if (!ap)
348 		return;
349 	spin_lock_irqsave(&ap->recv_lock, flags);
350 	ppp_async_input(ap, buf, cflags, count);
351 	spin_unlock_irqrestore(&ap->recv_lock, flags);
352 	if (!skb_queue_empty(&ap->rqueue))
353 		tasklet_schedule(&ap->tsk);
354 	ap_put(ap);
355 	tty_unthrottle(tty);
356 }
357 
358 static void
359 ppp_asynctty_wakeup(struct tty_struct *tty)
360 {
361 	struct asyncppp *ap = ap_get(tty);
362 
363 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
364 	if (!ap)
365 		return;
366 	set_bit(XMIT_WAKEUP, &ap->xmit_flags);
367 	tasklet_schedule(&ap->tsk);
368 	ap_put(ap);
369 }
370 
371 
372 static struct tty_ldisc_ops ppp_ldisc = {
373 	.owner  = THIS_MODULE,
374 	.magic	= TTY_LDISC_MAGIC,
375 	.name	= "ppp",
376 	.open	= ppp_asynctty_open,
377 	.close	= ppp_asynctty_close,
378 	.hangup	= ppp_asynctty_hangup,
379 	.read	= ppp_asynctty_read,
380 	.write	= ppp_asynctty_write,
381 	.ioctl	= ppp_asynctty_ioctl,
382 	.poll	= ppp_asynctty_poll,
383 	.receive_buf = ppp_asynctty_receive,
384 	.write_wakeup = ppp_asynctty_wakeup,
385 };
386 
387 static int __init
388 ppp_async_init(void)
389 {
390 	int err;
391 
392 	err = tty_register_ldisc(N_PPP, &ppp_ldisc);
393 	if (err != 0)
394 		printk(KERN_ERR "PPP_async: error %d registering line disc.\n",
395 		       err);
396 	return err;
397 }
398 
399 /*
400  * The following routines provide the PPP channel interface.
401  */
402 static int
403 ppp_async_ioctl(struct ppp_channel *chan, unsigned int cmd, unsigned long arg)
404 {
405 	struct asyncppp *ap = chan->private;
406 	void __user *argp = (void __user *)arg;
407 	int __user *p = argp;
408 	int err, val;
409 	u32 accm[8];
410 
411 	err = -EFAULT;
412 	switch (cmd) {
413 	case PPPIOCGFLAGS:
414 		val = ap->flags | ap->rbits;
415 		if (put_user(val, p))
416 			break;
417 		err = 0;
418 		break;
419 	case PPPIOCSFLAGS:
420 		if (get_user(val, p))
421 			break;
422 		ap->flags = val & ~SC_RCV_BITS;
423 		spin_lock_irq(&ap->recv_lock);
424 		ap->rbits = val & SC_RCV_BITS;
425 		spin_unlock_irq(&ap->recv_lock);
426 		err = 0;
427 		break;
428 
429 	case PPPIOCGASYNCMAP:
430 		if (put_user(ap->xaccm[0], (u32 __user *)argp))
431 			break;
432 		err = 0;
433 		break;
434 	case PPPIOCSASYNCMAP:
435 		if (get_user(ap->xaccm[0], (u32 __user *)argp))
436 			break;
437 		err = 0;
438 		break;
439 
440 	case PPPIOCGRASYNCMAP:
441 		if (put_user(ap->raccm, (u32 __user *)argp))
442 			break;
443 		err = 0;
444 		break;
445 	case PPPIOCSRASYNCMAP:
446 		if (get_user(ap->raccm, (u32 __user *)argp))
447 			break;
448 		err = 0;
449 		break;
450 
451 	case PPPIOCGXASYNCMAP:
452 		if (copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
453 			break;
454 		err = 0;
455 		break;
456 	case PPPIOCSXASYNCMAP:
457 		if (copy_from_user(accm, argp, sizeof(accm)))
458 			break;
459 		accm[2] &= ~0x40000000U;	/* can't escape 0x5e */
460 		accm[3] |= 0x60000000U;		/* must escape 0x7d, 0x7e */
461 		memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
462 		err = 0;
463 		break;
464 
465 	case PPPIOCGMRU:
466 		if (put_user(ap->mru, p))
467 			break;
468 		err = 0;
469 		break;
470 	case PPPIOCSMRU:
471 		if (get_user(val, p))
472 			break;
473 		if (val < PPP_MRU)
474 			val = PPP_MRU;
475 		ap->mru = val;
476 		err = 0;
477 		break;
478 
479 	default:
480 		err = -ENOTTY;
481 	}
482 
483 	return err;
484 }
485 
486 /*
487  * This is called at softirq level to deliver received packets
488  * to the ppp_generic code, and to tell the ppp_generic code
489  * if we can accept more output now.
490  */
491 static void ppp_async_process(unsigned long arg)
492 {
493 	struct asyncppp *ap = (struct asyncppp *) arg;
494 	struct sk_buff *skb;
495 
496 	/* process received packets */
497 	while ((skb = skb_dequeue(&ap->rqueue)) != NULL) {
498 		if (skb->cb[0])
499 			ppp_input_error(&ap->chan, 0);
500 		ppp_input(&ap->chan, skb);
501 	}
502 
503 	/* try to push more stuff out */
504 	if (test_bit(XMIT_WAKEUP, &ap->xmit_flags) && ppp_async_push(ap))
505 		ppp_output_wakeup(&ap->chan);
506 }
507 
508 /*
509  * Procedures for encapsulation and framing.
510  */
511 
512 /*
513  * Procedure to encode the data for async serial transmission.
514  * Does octet stuffing (escaping), puts the address/control bytes
515  * on if A/C compression is disabled, and does protocol compression.
516  * Assumes ap->tpkt != 0 on entry.
517  * Returns 1 if we finished the current frame, 0 otherwise.
518  */
519 
520 #define PUT_BYTE(ap, buf, c, islcp)	do {		\
521 	if ((islcp && c < 0x20) || (ap->xaccm[c >> 5] & (1 << (c & 0x1f)))) {\
522 		*buf++ = PPP_ESCAPE;			\
523 		*buf++ = c ^ PPP_TRANS;			\
524 	} else						\
525 		*buf++ = c;				\
526 } while (0)
527 
528 static int
529 ppp_async_encode(struct asyncppp *ap)
530 {
531 	int fcs, i, count, c, proto;
532 	unsigned char *buf, *buflim;
533 	unsigned char *data;
534 	int islcp;
535 
536 	buf = ap->obuf;
537 	ap->olim = buf;
538 	ap->optr = buf;
539 	i = ap->tpkt_pos;
540 	data = ap->tpkt->data;
541 	count = ap->tpkt->len;
542 	fcs = ap->tfcs;
543 	proto = get_unaligned_be16(data);
544 
545 	/*
546 	 * LCP packets with code values between 1 (configure-reqest)
547 	 * and 7 (code-reject) must be sent as though no options
548 	 * had been negotiated.
549 	 */
550 	islcp = proto == PPP_LCP && 1 <= data[2] && data[2] <= 7;
551 
552 	if (i == 0) {
553 		if (islcp)
554 			async_lcp_peek(ap, data, count, 0);
555 
556 		/*
557 		 * Start of a new packet - insert the leading FLAG
558 		 * character if necessary.
559 		 */
560 		if (islcp || flag_time == 0 ||
561 		    time_after_eq(jiffies, ap->last_xmit + flag_time))
562 			*buf++ = PPP_FLAG;
563 		ap->last_xmit = jiffies;
564 		fcs = PPP_INITFCS;
565 
566 		/*
567 		 * Put in the address/control bytes if necessary
568 		 */
569 		if ((ap->flags & SC_COMP_AC) == 0 || islcp) {
570 			PUT_BYTE(ap, buf, 0xff, islcp);
571 			fcs = PPP_FCS(fcs, 0xff);
572 			PUT_BYTE(ap, buf, 0x03, islcp);
573 			fcs = PPP_FCS(fcs, 0x03);
574 		}
575 	}
576 
577 	/*
578 	 * Once we put in the last byte, we need to put in the FCS
579 	 * and closing flag, so make sure there is at least 7 bytes
580 	 * of free space in the output buffer.
581 	 */
582 	buflim = ap->obuf + OBUFSIZE - 6;
583 	while (i < count && buf < buflim) {
584 		c = data[i++];
585 		if (i == 1 && c == 0 && (ap->flags & SC_COMP_PROT))
586 			continue;	/* compress protocol field */
587 		fcs = PPP_FCS(fcs, c);
588 		PUT_BYTE(ap, buf, c, islcp);
589 	}
590 
591 	if (i < count) {
592 		/*
593 		 * Remember where we are up to in this packet.
594 		 */
595 		ap->olim = buf;
596 		ap->tpkt_pos = i;
597 		ap->tfcs = fcs;
598 		return 0;
599 	}
600 
601 	/*
602 	 * We have finished the packet.  Add the FCS and flag.
603 	 */
604 	fcs = ~fcs;
605 	c = fcs & 0xff;
606 	PUT_BYTE(ap, buf, c, islcp);
607 	c = (fcs >> 8) & 0xff;
608 	PUT_BYTE(ap, buf, c, islcp);
609 	*buf++ = PPP_FLAG;
610 	ap->olim = buf;
611 
612 	consume_skb(ap->tpkt);
613 	ap->tpkt = NULL;
614 	return 1;
615 }
616 
617 /*
618  * Transmit-side routines.
619  */
620 
621 /*
622  * Send a packet to the peer over an async tty line.
623  * Returns 1 iff the packet was accepted.
624  * If the packet was not accepted, we will call ppp_output_wakeup
625  * at some later time.
626  */
627 static int
628 ppp_async_send(struct ppp_channel *chan, struct sk_buff *skb)
629 {
630 	struct asyncppp *ap = chan->private;
631 
632 	ppp_async_push(ap);
633 
634 	if (test_and_set_bit(XMIT_FULL, &ap->xmit_flags))
635 		return 0;	/* already full */
636 	ap->tpkt = skb;
637 	ap->tpkt_pos = 0;
638 
639 	ppp_async_push(ap);
640 	return 1;
641 }
642 
643 /*
644  * Push as much data as possible out to the tty.
645  */
646 static int
647 ppp_async_push(struct asyncppp *ap)
648 {
649 	int avail, sent, done = 0;
650 	struct tty_struct *tty = ap->tty;
651 	int tty_stuffed = 0;
652 
653 	/*
654 	 * We can get called recursively here if the tty write
655 	 * function calls our wakeup function.  This can happen
656 	 * for example on a pty with both the master and slave
657 	 * set to PPP line discipline.
658 	 * We use the XMIT_BUSY bit to detect this and get out,
659 	 * leaving the XMIT_WAKEUP bit set to tell the other
660 	 * instance that it may now be able to write more now.
661 	 */
662 	if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags))
663 		return 0;
664 	spin_lock_bh(&ap->xmit_lock);
665 	for (;;) {
666 		if (test_and_clear_bit(XMIT_WAKEUP, &ap->xmit_flags))
667 			tty_stuffed = 0;
668 		if (!tty_stuffed && ap->optr < ap->olim) {
669 			avail = ap->olim - ap->optr;
670 			set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
671 			sent = tty->ops->write(tty, ap->optr, avail);
672 			if (sent < 0)
673 				goto flush;	/* error, e.g. loss of CD */
674 			ap->optr += sent;
675 			if (sent < avail)
676 				tty_stuffed = 1;
677 			continue;
678 		}
679 		if (ap->optr >= ap->olim && ap->tpkt) {
680 			if (ppp_async_encode(ap)) {
681 				/* finished processing ap->tpkt */
682 				clear_bit(XMIT_FULL, &ap->xmit_flags);
683 				done = 1;
684 			}
685 			continue;
686 		}
687 		/*
688 		 * We haven't made any progress this time around.
689 		 * Clear XMIT_BUSY to let other callers in, but
690 		 * after doing so we have to check if anyone set
691 		 * XMIT_WAKEUP since we last checked it.  If they
692 		 * did, we should try again to set XMIT_BUSY and go
693 		 * around again in case XMIT_BUSY was still set when
694 		 * the other caller tried.
695 		 */
696 		clear_bit(XMIT_BUSY, &ap->xmit_flags);
697 		/* any more work to do? if not, exit the loop */
698 		if (!(test_bit(XMIT_WAKEUP, &ap->xmit_flags) ||
699 		      (!tty_stuffed && ap->tpkt)))
700 			break;
701 		/* more work to do, see if we can do it now */
702 		if (test_and_set_bit(XMIT_BUSY, &ap->xmit_flags))
703 			break;
704 	}
705 	spin_unlock_bh(&ap->xmit_lock);
706 	return done;
707 
708 flush:
709 	clear_bit(XMIT_BUSY, &ap->xmit_flags);
710 	if (ap->tpkt) {
711 		kfree_skb(ap->tpkt);
712 		ap->tpkt = NULL;
713 		clear_bit(XMIT_FULL, &ap->xmit_flags);
714 		done = 1;
715 	}
716 	ap->optr = ap->olim;
717 	spin_unlock_bh(&ap->xmit_lock);
718 	return done;
719 }
720 
721 /*
722  * Flush output from our internal buffers.
723  * Called for the TCFLSH ioctl. Can be entered in parallel
724  * but this is covered by the xmit_lock.
725  */
726 static void
727 ppp_async_flush_output(struct asyncppp *ap)
728 {
729 	int done = 0;
730 
731 	spin_lock_bh(&ap->xmit_lock);
732 	ap->optr = ap->olim;
733 	if (ap->tpkt != NULL) {
734 		kfree_skb(ap->tpkt);
735 		ap->tpkt = NULL;
736 		clear_bit(XMIT_FULL, &ap->xmit_flags);
737 		done = 1;
738 	}
739 	spin_unlock_bh(&ap->xmit_lock);
740 	if (done)
741 		ppp_output_wakeup(&ap->chan);
742 }
743 
744 /*
745  * Receive-side routines.
746  */
747 
748 /* see how many ordinary chars there are at the start of buf */
749 static inline int
750 scan_ordinary(struct asyncppp *ap, const unsigned char *buf, int count)
751 {
752 	int i, c;
753 
754 	for (i = 0; i < count; ++i) {
755 		c = buf[i];
756 		if (c == PPP_ESCAPE || c == PPP_FLAG ||
757 		    (c < 0x20 && (ap->raccm & (1 << c)) != 0))
758 			break;
759 	}
760 	return i;
761 }
762 
763 /* called when a flag is seen - do end-of-packet processing */
764 static void
765 process_input_packet(struct asyncppp *ap)
766 {
767 	struct sk_buff *skb;
768 	unsigned char *p;
769 	unsigned int len, fcs;
770 
771 	skb = ap->rpkt;
772 	if (ap->state & (SC_TOSS | SC_ESCAPE))
773 		goto err;
774 
775 	if (skb == NULL)
776 		return;		/* 0-length packet */
777 
778 	/* check the FCS */
779 	p = skb->data;
780 	len = skb->len;
781 	if (len < 3)
782 		goto err;	/* too short */
783 	fcs = PPP_INITFCS;
784 	for (; len > 0; --len)
785 		fcs = PPP_FCS(fcs, *p++);
786 	if (fcs != PPP_GOODFCS)
787 		goto err;	/* bad FCS */
788 	skb_trim(skb, skb->len - 2);
789 
790 	/* check for address/control and protocol compression */
791 	p = skb->data;
792 	if (p[0] == PPP_ALLSTATIONS) {
793 		/* chop off address/control */
794 		if (p[1] != PPP_UI || skb->len < 3)
795 			goto err;
796 		p = skb_pull(skb, 2);
797 	}
798 
799 	/* If protocol field is not compressed, it can be LCP packet */
800 	if (!(p[0] & 0x01)) {
801 		unsigned int proto;
802 
803 		if (skb->len < 2)
804 			goto err;
805 		proto = (p[0] << 8) + p[1];
806 		if (proto == PPP_LCP)
807 			async_lcp_peek(ap, p, skb->len, 1);
808 	}
809 
810 	/* queue the frame to be processed */
811 	skb->cb[0] = ap->state;
812 	skb_queue_tail(&ap->rqueue, skb);
813 	ap->rpkt = NULL;
814 	ap->state = 0;
815 	return;
816 
817  err:
818 	/* frame had an error, remember that, reset SC_TOSS & SC_ESCAPE */
819 	ap->state = SC_PREV_ERROR;
820 	if (skb) {
821 		/* make skb appear as freshly allocated */
822 		skb_trim(skb, 0);
823 		skb_reserve(skb, - skb_headroom(skb));
824 	}
825 }
826 
827 /* Called when the tty driver has data for us. Runs parallel with the
828    other ldisc functions but will not be re-entered */
829 
830 static void
831 ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
832 		char *flags, int count)
833 {
834 	struct sk_buff *skb;
835 	int c, i, j, n, s, f;
836 	unsigned char *sp;
837 
838 	/* update bits used for 8-bit cleanness detection */
839 	if (~ap->rbits & SC_RCV_BITS) {
840 		s = 0;
841 		for (i = 0; i < count; ++i) {
842 			c = buf[i];
843 			if (flags && flags[i] != 0)
844 				continue;
845 			s |= (c & 0x80)? SC_RCV_B7_1: SC_RCV_B7_0;
846 			c = ((c >> 4) ^ c) & 0xf;
847 			s |= (0x6996 & (1 << c))? SC_RCV_ODDP: SC_RCV_EVNP;
848 		}
849 		ap->rbits |= s;
850 	}
851 
852 	while (count > 0) {
853 		/* scan through and see how many chars we can do in bulk */
854 		if ((ap->state & SC_ESCAPE) && buf[0] == PPP_ESCAPE)
855 			n = 1;
856 		else
857 			n = scan_ordinary(ap, buf, count);
858 
859 		f = 0;
860 		if (flags && (ap->state & SC_TOSS) == 0) {
861 			/* check the flags to see if any char had an error */
862 			for (j = 0; j < n; ++j)
863 				if ((f = flags[j]) != 0)
864 					break;
865 		}
866 		if (f != 0) {
867 			/* start tossing */
868 			ap->state |= SC_TOSS;
869 
870 		} else if (n > 0 && (ap->state & SC_TOSS) == 0) {
871 			/* stuff the chars in the skb */
872 			skb = ap->rpkt;
873 			if (!skb) {
874 				skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
875 				if (!skb)
876 					goto nomem;
877  				ap->rpkt = skb;
878  			}
879  			if (skb->len == 0) {
880  				/* Try to get the payload 4-byte aligned.
881  				 * This should match the
882  				 * PPP_ALLSTATIONS/PPP_UI/compressed tests in
883  				 * process_input_packet, but we do not have
884  				 * enough chars here to test buf[1] and buf[2].
885  				 */
886 				if (buf[0] != PPP_ALLSTATIONS)
887 					skb_reserve(skb, 2 + (buf[0] & 1));
888 			}
889 			if (n > skb_tailroom(skb)) {
890 				/* packet overflowed MRU */
891 				ap->state |= SC_TOSS;
892 			} else {
893 				sp = skb_put_data(skb, buf, n);
894 				if (ap->state & SC_ESCAPE) {
895 					sp[0] ^= PPP_TRANS;
896 					ap->state &= ~SC_ESCAPE;
897 				}
898 			}
899 		}
900 
901 		if (n >= count)
902 			break;
903 
904 		c = buf[n];
905 		if (flags != NULL && flags[n] != 0) {
906 			ap->state |= SC_TOSS;
907 		} else if (c == PPP_FLAG) {
908 			process_input_packet(ap);
909 		} else if (c == PPP_ESCAPE) {
910 			ap->state |= SC_ESCAPE;
911 		} else if (I_IXON(ap->tty)) {
912 			if (c == START_CHAR(ap->tty))
913 				start_tty(ap->tty);
914 			else if (c == STOP_CHAR(ap->tty))
915 				stop_tty(ap->tty);
916 		}
917 		/* otherwise it's a char in the recv ACCM */
918 		++n;
919 
920 		buf += n;
921 		if (flags)
922 			flags += n;
923 		count -= n;
924 	}
925 	return;
926 
927  nomem:
928 	printk(KERN_ERR "PPPasync: no memory (input pkt)\n");
929 	ap->state |= SC_TOSS;
930 }
931 
932 /*
933  * We look at LCP frames going past so that we can notice
934  * and react to the LCP configure-ack from the peer.
935  * In the situation where the peer has been sent a configure-ack
936  * already, LCP is up once it has sent its configure-ack
937  * so the immediately following packet can be sent with the
938  * configured LCP options.  This allows us to process the following
939  * packet correctly without pppd needing to respond quickly.
940  *
941  * We only respond to the received configure-ack if we have just
942  * sent a configure-request, and the configure-ack contains the
943  * same data (this is checked using a 16-bit crc of the data).
944  */
945 #define CONFREQ		1	/* LCP code field values */
946 #define CONFACK		2
947 #define LCP_MRU		1	/* LCP option numbers */
948 #define LCP_ASYNCMAP	2
949 
950 static void async_lcp_peek(struct asyncppp *ap, unsigned char *data,
951 			   int len, int inbound)
952 {
953 	int dlen, fcs, i, code;
954 	u32 val;
955 
956 	data += 2;		/* skip protocol bytes */
957 	len -= 2;
958 	if (len < 4)		/* 4 = code, ID, length */
959 		return;
960 	code = data[0];
961 	if (code != CONFACK && code != CONFREQ)
962 		return;
963 	dlen = get_unaligned_be16(data + 2);
964 	if (len < dlen)
965 		return;		/* packet got truncated or length is bogus */
966 
967 	if (code == (inbound? CONFACK: CONFREQ)) {
968 		/*
969 		 * sent confreq or received confack:
970 		 * calculate the crc of the data from the ID field on.
971 		 */
972 		fcs = PPP_INITFCS;
973 		for (i = 1; i < dlen; ++i)
974 			fcs = PPP_FCS(fcs, data[i]);
975 
976 		if (!inbound) {
977 			/* outbound confreq - remember the crc for later */
978 			ap->lcp_fcs = fcs;
979 			return;
980 		}
981 
982 		/* received confack, check the crc */
983 		fcs ^= ap->lcp_fcs;
984 		ap->lcp_fcs = -1;
985 		if (fcs != 0)
986 			return;
987 	} else if (inbound)
988 		return;	/* not interested in received confreq */
989 
990 	/* process the options in the confack */
991 	data += 4;
992 	dlen -= 4;
993 	/* data[0] is code, data[1] is length */
994 	while (dlen >= 2 && dlen >= data[1] && data[1] >= 2) {
995 		switch (data[0]) {
996 		case LCP_MRU:
997 			val = get_unaligned_be16(data + 2);
998 			if (inbound)
999 				ap->mru = val;
1000 			else
1001 				ap->chan.mtu = val;
1002 			break;
1003 		case LCP_ASYNCMAP:
1004 			val = get_unaligned_be32(data + 2);
1005 			if (inbound)
1006 				ap->raccm = val;
1007 			else
1008 				ap->xaccm[0] = val;
1009 			break;
1010 		}
1011 		dlen -= data[1];
1012 		data += data[1];
1013 	}
1014 }
1015 
1016 static void __exit ppp_async_cleanup(void)
1017 {
1018 	if (tty_unregister_ldisc(N_PPP) != 0)
1019 		printk(KERN_ERR "failed to unregister PPP line discipline\n");
1020 }
1021 
1022 module_init(ppp_async_init);
1023 module_exit(ppp_async_cleanup);
1024