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