xref: /OK3568_Linux_fs/kernel/net/atm/pppoatm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun /* Copyright 1999-2000 by Mitchell Blank Jr */
5*4882a593Smuzhiyun /* Based on clip.c; 1995-1999 by Werner Almesberger, EPFL LRC/ICA */
6*4882a593Smuzhiyun /* And on ppp_async.c; Copyright 1999 Paul Mackerras */
7*4882a593Smuzhiyun /* And help from Jens Axboe */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /*
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This driver provides the encapsulation and framing for sending
12*4882a593Smuzhiyun  * and receiving PPP frames in ATM AAL5 PDUs.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * One shortcoming of this driver is that it does not comply with
17*4882a593Smuzhiyun  * section 8 of RFC2364 - we are supposed to detect a change
18*4882a593Smuzhiyun  * in encapsulation and immediately abort the connection (in order
19*4882a593Smuzhiyun  * to avoid a black-hole being created if our peer loses state
20*4882a593Smuzhiyun  * and changes encapsulation unilaterally.  However, since the
21*4882a593Smuzhiyun  * ppp_generic layer actually does the decapsulation, we need
22*4882a593Smuzhiyun  * a way of notifying it when we _think_ there might be a problem)
23*4882a593Smuzhiyun  * There's two cases:
24*4882a593Smuzhiyun  *   1.	LLC-encapsulation was missing when it was enabled.  In
25*4882a593Smuzhiyun  *	this case, we should tell the upper layer "tear down
26*4882a593Smuzhiyun  *	this session if this skb looks ok to you"
27*4882a593Smuzhiyun  *   2.	LLC-encapsulation was present when it was disabled.  Then
28*4882a593Smuzhiyun  *	we need to tell the upper layer "this packet may be
29*4882a593Smuzhiyun  *	ok, but if its in error tear down the session"
30*4882a593Smuzhiyun  * These hooks are not yet available in ppp_generic
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include <linux/module.h>
36*4882a593Smuzhiyun #include <linux/init.h>
37*4882a593Smuzhiyun #include <linux/interrupt.h>
38*4882a593Smuzhiyun #include <linux/skbuff.h>
39*4882a593Smuzhiyun #include <linux/slab.h>
40*4882a593Smuzhiyun #include <linux/atm.h>
41*4882a593Smuzhiyun #include <linux/atmdev.h>
42*4882a593Smuzhiyun #include <linux/capability.h>
43*4882a593Smuzhiyun #include <linux/ppp_defs.h>
44*4882a593Smuzhiyun #include <linux/ppp-ioctl.h>
45*4882a593Smuzhiyun #include <linux/ppp_channel.h>
46*4882a593Smuzhiyun #include <linux/atmppp.h>
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include "common.h"
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun enum pppoatm_encaps {
51*4882a593Smuzhiyun 	e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
52*4882a593Smuzhiyun 	e_vc = PPPOATM_ENCAPS_VC,
53*4882a593Smuzhiyun 	e_llc = PPPOATM_ENCAPS_LLC,
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun struct pppoatm_vcc {
57*4882a593Smuzhiyun 	struct atm_vcc	*atmvcc;	/* VCC descriptor */
58*4882a593Smuzhiyun 	void (*old_push)(struct atm_vcc *, struct sk_buff *);
59*4882a593Smuzhiyun 	void (*old_pop)(struct atm_vcc *, struct sk_buff *);
60*4882a593Smuzhiyun 	void (*old_release_cb)(struct atm_vcc *);
61*4882a593Smuzhiyun 	struct module *old_owner;
62*4882a593Smuzhiyun 					/* keep old push/pop for detaching */
63*4882a593Smuzhiyun 	enum pppoatm_encaps encaps;
64*4882a593Smuzhiyun 	atomic_t inflight;
65*4882a593Smuzhiyun 	unsigned long blocked;
66*4882a593Smuzhiyun 	int flags;			/* SC_COMP_PROT - compress protocol */
67*4882a593Smuzhiyun 	struct ppp_channel chan;	/* interface to generic ppp layer */
68*4882a593Smuzhiyun 	struct tasklet_struct wakeup_tasklet;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * We want to allow two packets in the queue. The one that's currently in
73*4882a593Smuzhiyun  * flight, and *one* queued up ready for the ATM device to send immediately
74*4882a593Smuzhiyun  * from its TX done IRQ. We want to be able to use atomic_inc_not_zero(), so
75*4882a593Smuzhiyun  * inflight == -2 represents an empty queue, -1 one packet, and zero means
76*4882a593Smuzhiyun  * there are two packets in the queue.
77*4882a593Smuzhiyun  */
78*4882a593Smuzhiyun #define NONE_INFLIGHT -2
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun #define BLOCKED 0
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /*
83*4882a593Smuzhiyun  * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
84*4882a593Smuzhiyun  * ID (0xC021) used in autodetection
85*4882a593Smuzhiyun  */
86*4882a593Smuzhiyun static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
87*4882a593Smuzhiyun #define LLC_LEN		(4)
88*4882a593Smuzhiyun 
atmvcc_to_pvcc(const struct atm_vcc * atmvcc)89*4882a593Smuzhiyun static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	return (struct pppoatm_vcc *) (atmvcc->user_back);
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
chan_to_pvcc(const struct ppp_channel * chan)94*4882a593Smuzhiyun static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun 	return (struct pppoatm_vcc *) (chan->private);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun  * We can't do this directly from our _pop handler, since the ppp code
101*4882a593Smuzhiyun  * doesn't want to be called in interrupt context, so we do it from
102*4882a593Smuzhiyun  * a tasklet
103*4882a593Smuzhiyun  */
pppoatm_wakeup_sender(unsigned long arg)104*4882a593Smuzhiyun static void pppoatm_wakeup_sender(unsigned long arg)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	ppp_output_wakeup((struct ppp_channel *) arg);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
pppoatm_release_cb(struct atm_vcc * atmvcc)109*4882a593Smuzhiyun static void pppoatm_release_cb(struct atm_vcc *atmvcc)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/*
114*4882a593Smuzhiyun 	 * As in pppoatm_pop(), it's safe to clear the BLOCKED bit here because
115*4882a593Smuzhiyun 	 * the wakeup *can't* race with pppoatm_send(). They both hold the PPP
116*4882a593Smuzhiyun 	 * channel's ->downl lock. And the potential race with *setting* it,
117*4882a593Smuzhiyun 	 * which leads to the double-check dance in pppoatm_may_send(), doesn't
118*4882a593Smuzhiyun 	 * exist here. In the sock_owned_by_user() case in pppoatm_send(), we
119*4882a593Smuzhiyun 	 * set the BLOCKED bit while the socket is still locked. We know that
120*4882a593Smuzhiyun 	 * ->release_cb() can't be called until that's done.
121*4882a593Smuzhiyun 	 */
122*4882a593Smuzhiyun 	if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
123*4882a593Smuzhiyun 		tasklet_schedule(&pvcc->wakeup_tasklet);
124*4882a593Smuzhiyun 	if (pvcc->old_release_cb)
125*4882a593Smuzhiyun 		pvcc->old_release_cb(atmvcc);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun  * This gets called every time the ATM card has finished sending our
129*4882a593Smuzhiyun  * skb.  The ->old_pop will take care up normal atm flow control,
130*4882a593Smuzhiyun  * but we also need to wake up the device if we blocked it
131*4882a593Smuzhiyun  */
pppoatm_pop(struct atm_vcc * atmvcc,struct sk_buff * skb)132*4882a593Smuzhiyun static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	pvcc->old_pop(atmvcc, skb);
137*4882a593Smuzhiyun 	atomic_dec(&pvcc->inflight);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/*
140*4882a593Smuzhiyun 	 * We always used to run the wakeup tasklet unconditionally here, for
141*4882a593Smuzhiyun 	 * fear of race conditions where we clear the BLOCKED flag just as we
142*4882a593Smuzhiyun 	 * refuse another packet in pppoatm_send(). This was quite inefficient.
143*4882a593Smuzhiyun 	 *
144*4882a593Smuzhiyun 	 * In fact it's OK. The PPP core will only ever call pppoatm_send()
145*4882a593Smuzhiyun 	 * while holding the channel->downl lock. And ppp_output_wakeup() as
146*4882a593Smuzhiyun 	 * called by the tasklet will *also* grab that lock. So even if another
147*4882a593Smuzhiyun 	 * CPU is in pppoatm_send() right now, the tasklet isn't going to race
148*4882a593Smuzhiyun 	 * with it. The wakeup *will* happen after the other CPU is safely out
149*4882a593Smuzhiyun 	 * of pppoatm_send() again.
150*4882a593Smuzhiyun 	 *
151*4882a593Smuzhiyun 	 * So if the CPU in pppoatm_send() has already set the BLOCKED bit and
152*4882a593Smuzhiyun 	 * it about to return, that's fine. We trigger a wakeup which will
153*4882a593Smuzhiyun 	 * happen later. And if the CPU in pppoatm_send() *hasn't* set the
154*4882a593Smuzhiyun 	 * BLOCKED bit yet, that's fine too because of the double check in
155*4882a593Smuzhiyun 	 * pppoatm_may_send() which is commented there.
156*4882a593Smuzhiyun 	 */
157*4882a593Smuzhiyun 	if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
158*4882a593Smuzhiyun 		tasklet_schedule(&pvcc->wakeup_tasklet);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun /*
162*4882a593Smuzhiyun  * Unbind from PPP - currently we only do this when closing the socket,
163*4882a593Smuzhiyun  * but we could put this into an ioctl if need be
164*4882a593Smuzhiyun  */
pppoatm_unassign_vcc(struct atm_vcc * atmvcc)165*4882a593Smuzhiyun static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	struct pppoatm_vcc *pvcc;
168*4882a593Smuzhiyun 	pvcc = atmvcc_to_pvcc(atmvcc);
169*4882a593Smuzhiyun 	atmvcc->push = pvcc->old_push;
170*4882a593Smuzhiyun 	atmvcc->pop = pvcc->old_pop;
171*4882a593Smuzhiyun 	atmvcc->release_cb = pvcc->old_release_cb;
172*4882a593Smuzhiyun 	tasklet_kill(&pvcc->wakeup_tasklet);
173*4882a593Smuzhiyun 	ppp_unregister_channel(&pvcc->chan);
174*4882a593Smuzhiyun 	atmvcc->user_back = NULL;
175*4882a593Smuzhiyun 	kfree(pvcc);
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun /* Called when an AAL5 PDU comes in */
pppoatm_push(struct atm_vcc * atmvcc,struct sk_buff * skb)179*4882a593Smuzhiyun static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
182*4882a593Smuzhiyun 	pr_debug("\n");
183*4882a593Smuzhiyun 	if (skb == NULL) {			/* VCC was closed */
184*4882a593Smuzhiyun 		struct module *module;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		pr_debug("removing ATMPPP VCC %p\n", pvcc);
187*4882a593Smuzhiyun 		module = pvcc->old_owner;
188*4882a593Smuzhiyun 		pppoatm_unassign_vcc(atmvcc);
189*4882a593Smuzhiyun 		atmvcc->push(atmvcc, NULL);	/* Pass along bad news */
190*4882a593Smuzhiyun 		module_put(module);
191*4882a593Smuzhiyun 		return;
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 	atm_return(atmvcc, skb->truesize);
194*4882a593Smuzhiyun 	switch (pvcc->encaps) {
195*4882a593Smuzhiyun 	case e_llc:
196*4882a593Smuzhiyun 		if (skb->len < LLC_LEN ||
197*4882a593Smuzhiyun 		    memcmp(skb->data, pppllc, LLC_LEN))
198*4882a593Smuzhiyun 			goto error;
199*4882a593Smuzhiyun 		skb_pull(skb, LLC_LEN);
200*4882a593Smuzhiyun 		break;
201*4882a593Smuzhiyun 	case e_autodetect:
202*4882a593Smuzhiyun 		if (pvcc->chan.ppp == NULL) {	/* Not bound yet! */
203*4882a593Smuzhiyun 			kfree_skb(skb);
204*4882a593Smuzhiyun 			return;
205*4882a593Smuzhiyun 		}
206*4882a593Smuzhiyun 		if (skb->len >= sizeof(pppllc) &&
207*4882a593Smuzhiyun 		    !memcmp(skb->data, pppllc, sizeof(pppllc))) {
208*4882a593Smuzhiyun 			pvcc->encaps = e_llc;
209*4882a593Smuzhiyun 			skb_pull(skb, LLC_LEN);
210*4882a593Smuzhiyun 			break;
211*4882a593Smuzhiyun 		}
212*4882a593Smuzhiyun 		if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
213*4882a593Smuzhiyun 		    !memcmp(skb->data, &pppllc[LLC_LEN],
214*4882a593Smuzhiyun 		    sizeof(pppllc) - LLC_LEN)) {
215*4882a593Smuzhiyun 			pvcc->encaps = e_vc;
216*4882a593Smuzhiyun 			pvcc->chan.mtu += LLC_LEN;
217*4882a593Smuzhiyun 			break;
218*4882a593Smuzhiyun 		}
219*4882a593Smuzhiyun 		pr_debug("Couldn't autodetect yet (skb: %6ph)\n", skb->data);
220*4882a593Smuzhiyun 		goto error;
221*4882a593Smuzhiyun 	case e_vc:
222*4882a593Smuzhiyun 		break;
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 	ppp_input(&pvcc->chan, skb);
225*4882a593Smuzhiyun 	return;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun error:
228*4882a593Smuzhiyun 	kfree_skb(skb);
229*4882a593Smuzhiyun 	ppp_input_error(&pvcc->chan, 0);
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun 
pppoatm_may_send(struct pppoatm_vcc * pvcc,int size)232*4882a593Smuzhiyun static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun 	/*
235*4882a593Smuzhiyun 	 * It's not clear that we need to bother with using atm_may_send()
236*4882a593Smuzhiyun 	 * to check we don't exceed sk->sk_sndbuf. If userspace sets a
237*4882a593Smuzhiyun 	 * value of sk_sndbuf which is lower than the MTU, we're going to
238*4882a593Smuzhiyun 	 * block for ever. But the code always did that before we introduced
239*4882a593Smuzhiyun 	 * the packet count limit, so...
240*4882a593Smuzhiyun 	 */
241*4882a593Smuzhiyun 	if (atm_may_send(pvcc->atmvcc, size) &&
242*4882a593Smuzhiyun 	    atomic_inc_not_zero(&pvcc->inflight))
243*4882a593Smuzhiyun 		return 1;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/*
246*4882a593Smuzhiyun 	 * We use test_and_set_bit() rather than set_bit() here because
247*4882a593Smuzhiyun 	 * we need to ensure there's a memory barrier after it. The bit
248*4882a593Smuzhiyun 	 * *must* be set before we do the atomic_inc() on pvcc->inflight.
249*4882a593Smuzhiyun 	 * There's no smp_mb__after_set_bit(), so it's this or abuse
250*4882a593Smuzhiyun 	 * smp_mb__after_atomic().
251*4882a593Smuzhiyun 	 */
252*4882a593Smuzhiyun 	test_and_set_bit(BLOCKED, &pvcc->blocked);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	/*
255*4882a593Smuzhiyun 	 * We may have raced with pppoatm_pop(). If it ran for the
256*4882a593Smuzhiyun 	 * last packet in the queue, *just* before we set the BLOCKED
257*4882a593Smuzhiyun 	 * bit, then it might never run again and the channel could
258*4882a593Smuzhiyun 	 * remain permanently blocked. Cope with that race by checking
259*4882a593Smuzhiyun 	 * *again*. If it did run in that window, we'll have space on
260*4882a593Smuzhiyun 	 * the queue now and can return success. It's harmless to leave
261*4882a593Smuzhiyun 	 * the BLOCKED flag set, since it's only used as a trigger to
262*4882a593Smuzhiyun 	 * run the wakeup tasklet. Another wakeup will never hurt.
263*4882a593Smuzhiyun 	 * If pppoatm_pop() is running but hasn't got as far as making
264*4882a593Smuzhiyun 	 * space on the queue yet, then it hasn't checked the BLOCKED
265*4882a593Smuzhiyun 	 * flag yet either, so we're safe in that case too. It'll issue
266*4882a593Smuzhiyun 	 * an "immediate" wakeup... where "immediate" actually involves
267*4882a593Smuzhiyun 	 * taking the PPP channel's ->downl lock, which is held by the
268*4882a593Smuzhiyun 	 * code path that calls pppoatm_send(), and is thus going to
269*4882a593Smuzhiyun 	 * wait for us to finish.
270*4882a593Smuzhiyun 	 */
271*4882a593Smuzhiyun 	if (atm_may_send(pvcc->atmvcc, size) &&
272*4882a593Smuzhiyun 	    atomic_inc_not_zero(&pvcc->inflight))
273*4882a593Smuzhiyun 		return 1;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	return 0;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun /*
278*4882a593Smuzhiyun  * Called by the ppp_generic.c to send a packet - returns true if packet
279*4882a593Smuzhiyun  * was accepted.  If we return false, then it's our job to call
280*4882a593Smuzhiyun  * ppp_output_wakeup(chan) when we're feeling more up to it.
281*4882a593Smuzhiyun  * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
282*4882a593Smuzhiyun  * we should really drop the packet, but the generic layer doesn't
283*4882a593Smuzhiyun  * support this yet.  We just return 'DROP_PACKET' which we actually define
284*4882a593Smuzhiyun  * as success, just to be clear what we're really doing.
285*4882a593Smuzhiyun  */
286*4882a593Smuzhiyun #define DROP_PACKET 1
pppoatm_send(struct ppp_channel * chan,struct sk_buff * skb)287*4882a593Smuzhiyun static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun 	struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
290*4882a593Smuzhiyun 	struct atm_vcc *vcc;
291*4882a593Smuzhiyun 	int ret;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	ATM_SKB(skb)->vcc = pvcc->atmvcc;
294*4882a593Smuzhiyun 	pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
295*4882a593Smuzhiyun 	if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
296*4882a593Smuzhiyun 		(void) skb_pull(skb, 1);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	vcc = ATM_SKB(skb)->vcc;
299*4882a593Smuzhiyun 	bh_lock_sock(sk_atm(vcc));
300*4882a593Smuzhiyun 	if (sock_owned_by_user(sk_atm(vcc))) {
301*4882a593Smuzhiyun 		/*
302*4882a593Smuzhiyun 		 * Needs to happen (and be flushed, hence test_and_) before we unlock
303*4882a593Smuzhiyun 		 * the socket. It needs to be seen by the time our ->release_cb gets
304*4882a593Smuzhiyun 		 * called.
305*4882a593Smuzhiyun 		 */
306*4882a593Smuzhiyun 		test_and_set_bit(BLOCKED, &pvcc->blocked);
307*4882a593Smuzhiyun 		goto nospace;
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
310*4882a593Smuzhiyun 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
311*4882a593Smuzhiyun 	    !test_bit(ATM_VF_READY, &vcc->flags)) {
312*4882a593Smuzhiyun 		bh_unlock_sock(sk_atm(vcc));
313*4882a593Smuzhiyun 		kfree_skb(skb);
314*4882a593Smuzhiyun 		return DROP_PACKET;
315*4882a593Smuzhiyun 	}
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	switch (pvcc->encaps) {		/* LLC encapsulation needed */
318*4882a593Smuzhiyun 	case e_llc:
319*4882a593Smuzhiyun 		if (skb_headroom(skb) < LLC_LEN) {
320*4882a593Smuzhiyun 			struct sk_buff *n;
321*4882a593Smuzhiyun 			n = skb_realloc_headroom(skb, LLC_LEN);
322*4882a593Smuzhiyun 			if (n != NULL &&
323*4882a593Smuzhiyun 			    !pppoatm_may_send(pvcc, n->truesize)) {
324*4882a593Smuzhiyun 				kfree_skb(n);
325*4882a593Smuzhiyun 				goto nospace;
326*4882a593Smuzhiyun 			}
327*4882a593Smuzhiyun 			consume_skb(skb);
328*4882a593Smuzhiyun 			skb = n;
329*4882a593Smuzhiyun 			if (skb == NULL) {
330*4882a593Smuzhiyun 				bh_unlock_sock(sk_atm(vcc));
331*4882a593Smuzhiyun 				return DROP_PACKET;
332*4882a593Smuzhiyun 			}
333*4882a593Smuzhiyun 		} else if (!pppoatm_may_send(pvcc, skb->truesize))
334*4882a593Smuzhiyun 			goto nospace;
335*4882a593Smuzhiyun 		memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
336*4882a593Smuzhiyun 		break;
337*4882a593Smuzhiyun 	case e_vc:
338*4882a593Smuzhiyun 		if (!pppoatm_may_send(pvcc, skb->truesize))
339*4882a593Smuzhiyun 			goto nospace;
340*4882a593Smuzhiyun 		break;
341*4882a593Smuzhiyun 	case e_autodetect:
342*4882a593Smuzhiyun 		bh_unlock_sock(sk_atm(vcc));
343*4882a593Smuzhiyun 		pr_debug("Trying to send without setting encaps!\n");
344*4882a593Smuzhiyun 		kfree_skb(skb);
345*4882a593Smuzhiyun 		return 1;
346*4882a593Smuzhiyun 	}
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	atm_account_tx(vcc, skb);
349*4882a593Smuzhiyun 	pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
350*4882a593Smuzhiyun 		 skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
351*4882a593Smuzhiyun 	ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
352*4882a593Smuzhiyun 	    ? DROP_PACKET : 1;
353*4882a593Smuzhiyun 	bh_unlock_sock(sk_atm(vcc));
354*4882a593Smuzhiyun 	return ret;
355*4882a593Smuzhiyun nospace:
356*4882a593Smuzhiyun 	bh_unlock_sock(sk_atm(vcc));
357*4882a593Smuzhiyun 	/*
358*4882a593Smuzhiyun 	 * We don't have space to send this SKB now, but we might have
359*4882a593Smuzhiyun 	 * already applied SC_COMP_PROT compression, so may need to undo
360*4882a593Smuzhiyun 	 */
361*4882a593Smuzhiyun 	if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
362*4882a593Smuzhiyun 	    skb->data[-1] == '\0')
363*4882a593Smuzhiyun 		(void) skb_push(skb, 1);
364*4882a593Smuzhiyun 	return 0;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /* This handles ioctls sent to the /dev/ppp interface */
pppoatm_devppp_ioctl(struct ppp_channel * chan,unsigned int cmd,unsigned long arg)368*4882a593Smuzhiyun static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
369*4882a593Smuzhiyun 	unsigned long arg)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	switch (cmd) {
372*4882a593Smuzhiyun 	case PPPIOCGFLAGS:
373*4882a593Smuzhiyun 		return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
374*4882a593Smuzhiyun 		    ? -EFAULT : 0;
375*4882a593Smuzhiyun 	case PPPIOCSFLAGS:
376*4882a593Smuzhiyun 		return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
377*4882a593Smuzhiyun 		    ? -EFAULT : 0;
378*4882a593Smuzhiyun 	}
379*4882a593Smuzhiyun 	return -ENOTTY;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun static const struct ppp_channel_ops pppoatm_ops = {
383*4882a593Smuzhiyun 	.start_xmit = pppoatm_send,
384*4882a593Smuzhiyun 	.ioctl = pppoatm_devppp_ioctl,
385*4882a593Smuzhiyun };
386*4882a593Smuzhiyun 
pppoatm_assign_vcc(struct atm_vcc * atmvcc,void __user * arg)387*4882a593Smuzhiyun static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	struct atm_backend_ppp be;
390*4882a593Smuzhiyun 	struct pppoatm_vcc *pvcc;
391*4882a593Smuzhiyun 	int err;
392*4882a593Smuzhiyun 	/*
393*4882a593Smuzhiyun 	 * Each PPPoATM instance has its own tasklet - this is just a
394*4882a593Smuzhiyun 	 * prototypical one used to initialize them
395*4882a593Smuzhiyun 	 */
396*4882a593Smuzhiyun 	static const DECLARE_TASKLET_OLD(tasklet_proto, pppoatm_wakeup_sender);
397*4882a593Smuzhiyun 	if (copy_from_user(&be, arg, sizeof be))
398*4882a593Smuzhiyun 		return -EFAULT;
399*4882a593Smuzhiyun 	if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
400*4882a593Smuzhiyun 	    be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
401*4882a593Smuzhiyun 		return -EINVAL;
402*4882a593Smuzhiyun 	pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
403*4882a593Smuzhiyun 	if (pvcc == NULL)
404*4882a593Smuzhiyun 		return -ENOMEM;
405*4882a593Smuzhiyun 	pvcc->atmvcc = atmvcc;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	/* Maximum is zero, so that we can use atomic_inc_not_zero() */
408*4882a593Smuzhiyun 	atomic_set(&pvcc->inflight, NONE_INFLIGHT);
409*4882a593Smuzhiyun 	pvcc->old_push = atmvcc->push;
410*4882a593Smuzhiyun 	pvcc->old_pop = atmvcc->pop;
411*4882a593Smuzhiyun 	pvcc->old_owner = atmvcc->owner;
412*4882a593Smuzhiyun 	pvcc->old_release_cb = atmvcc->release_cb;
413*4882a593Smuzhiyun 	pvcc->encaps = (enum pppoatm_encaps) be.encaps;
414*4882a593Smuzhiyun 	pvcc->chan.private = pvcc;
415*4882a593Smuzhiyun 	pvcc->chan.ops = &pppoatm_ops;
416*4882a593Smuzhiyun 	pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
417*4882a593Smuzhiyun 	    (be.encaps == e_vc ? 0 : LLC_LEN);
418*4882a593Smuzhiyun 	pvcc->wakeup_tasklet = tasklet_proto;
419*4882a593Smuzhiyun 	pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
420*4882a593Smuzhiyun 	err = ppp_register_channel(&pvcc->chan);
421*4882a593Smuzhiyun 	if (err != 0) {
422*4882a593Smuzhiyun 		kfree(pvcc);
423*4882a593Smuzhiyun 		return err;
424*4882a593Smuzhiyun 	}
425*4882a593Smuzhiyun 	atmvcc->user_back = pvcc;
426*4882a593Smuzhiyun 	atmvcc->push = pppoatm_push;
427*4882a593Smuzhiyun 	atmvcc->pop = pppoatm_pop;
428*4882a593Smuzhiyun 	atmvcc->release_cb = pppoatm_release_cb;
429*4882a593Smuzhiyun 	__module_get(THIS_MODULE);
430*4882a593Smuzhiyun 	atmvcc->owner = THIS_MODULE;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	/* re-process everything received between connection setup and
433*4882a593Smuzhiyun 	   backend setup */
434*4882a593Smuzhiyun 	vcc_process_recv_queue(atmvcc);
435*4882a593Smuzhiyun 	return 0;
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun /*
439*4882a593Smuzhiyun  * This handles ioctls actually performed on our vcc - we must return
440*4882a593Smuzhiyun  * -ENOIOCTLCMD for any unrecognized ioctl
441*4882a593Smuzhiyun  */
pppoatm_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)442*4882a593Smuzhiyun static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
443*4882a593Smuzhiyun 	unsigned long arg)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct atm_vcc *atmvcc = ATM_SD(sock);
446*4882a593Smuzhiyun 	void __user *argp = (void __user *)arg;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
449*4882a593Smuzhiyun 		return -ENOIOCTLCMD;
450*4882a593Smuzhiyun 	switch (cmd) {
451*4882a593Smuzhiyun 	case ATM_SETBACKEND: {
452*4882a593Smuzhiyun 		atm_backend_t b;
453*4882a593Smuzhiyun 		if (get_user(b, (atm_backend_t __user *) argp))
454*4882a593Smuzhiyun 			return -EFAULT;
455*4882a593Smuzhiyun 		if (b != ATM_BACKEND_PPP)
456*4882a593Smuzhiyun 			return -ENOIOCTLCMD;
457*4882a593Smuzhiyun 		if (!capable(CAP_NET_ADMIN))
458*4882a593Smuzhiyun 			return -EPERM;
459*4882a593Smuzhiyun 		if (sock->state != SS_CONNECTED)
460*4882a593Smuzhiyun 			return -EINVAL;
461*4882a593Smuzhiyun 		return pppoatm_assign_vcc(atmvcc, argp);
462*4882a593Smuzhiyun 		}
463*4882a593Smuzhiyun 	case PPPIOCGCHAN:
464*4882a593Smuzhiyun 		return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
465*4882a593Smuzhiyun 		    chan), (int __user *) argp) ? -EFAULT : 0;
466*4882a593Smuzhiyun 	case PPPIOCGUNIT:
467*4882a593Smuzhiyun 		return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
468*4882a593Smuzhiyun 		    chan), (int __user *) argp) ? -EFAULT : 0;
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 	return -ENOIOCTLCMD;
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun static struct atm_ioctl pppoatm_ioctl_ops = {
474*4882a593Smuzhiyun 	.owner	= THIS_MODULE,
475*4882a593Smuzhiyun 	.ioctl	= pppoatm_ioctl,
476*4882a593Smuzhiyun };
477*4882a593Smuzhiyun 
pppoatm_init(void)478*4882a593Smuzhiyun static int __init pppoatm_init(void)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun 	register_atm_ioctl(&pppoatm_ioctl_ops);
481*4882a593Smuzhiyun 	return 0;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
pppoatm_exit(void)484*4882a593Smuzhiyun static void __exit pppoatm_exit(void)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun 	deregister_atm_ioctl(&pppoatm_ioctl_ops);
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun module_init(pppoatm_init);
490*4882a593Smuzhiyun module_exit(pppoatm_exit);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
493*4882a593Smuzhiyun MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
494*4882a593Smuzhiyun MODULE_LICENSE("GPL");
495