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