blob: f27a07a3c546ee3adeabd51460ed4331eddbb309 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */
2
3/* Copyright 1999-2000 by Mitchell Blank Jr */
4/* Based on clip.c; 1995-1999 by Werner Almesberger, EPFL LRC/ICA */
5/* And on ppp_async.c; Copyright 1999 Paul Mackerras */
6/* And help from Jens Axboe */
7
8/*
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 * This driver provides the encapsulation and framing for sending
15 * and receiving PPP frames in ATM AAL5 PDUs.
16 */
17
18/*
19 * One shortcoming of this driver is that it does not comply with
20 * section 8 of RFC2364 - we are supposed to detect a change
21 * in encapsulation and immediately abort the connection (in order
22 * to avoid a black-hole being created if our peer loses state
23 * and changes encapsulation unilaterally. However, since the
24 * ppp_generic layer actually does the decapsulation, we need
25 * a way of notifying it when we _think_ there might be a problem)
26 * There's two cases:
27 * 1. LLC-encapsulation was missing when it was enabled. In
28 * this case, we should tell the upper layer "tear down
29 * this session if this skb looks ok to you"
30 * 2. LLC-encapsulation was present when it was disabled. Then
31 * we need to tell the upper layer "this packet may be
32 * ok, but if its in error tear down the session"
33 * These hooks are not yet available in ppp_generic
34 */
35
Joe Perches99824462010-01-26 11:40:00 +000036#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000040#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/atm.h>
44#include <linux/atmdev.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080045#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/ppp_defs.h>
Paul Mackerras4b32da2b2012-03-04 12:56:55 +000047#include <linux/ppp-ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/ppp_channel.h>
49#include <linux/atmppp.h>
50
51#include "common.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053enum pppoatm_encaps {
54 e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
55 e_vc = PPPOATM_ENCAPS_VC,
56 e_llc = PPPOATM_ENCAPS_LLC,
57};
58
59struct pppoatm_vcc {
60 struct atm_vcc *atmvcc; /* VCC descriptor */
61 void (*old_push)(struct atm_vcc *, struct sk_buff *);
62 void (*old_pop)(struct atm_vcc *, struct sk_buff *);
63 /* keep old push/pop for detaching */
64 enum pppoatm_encaps encaps;
David Woodhouse9d02daf752012-04-08 09:55:43 +000065 atomic_t inflight;
66 unsigned long blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int flags; /* SC_COMP_PROT - compress protocol */
68 struct ppp_channel chan; /* interface to generic ppp layer */
69 struct tasklet_struct wakeup_tasklet;
70};
71
72/*
David Woodhouse9d02daf752012-04-08 09:55:43 +000073 * We want to allow two packets in the queue. The one that's currently in
74 * flight, and *one* queued up ready for the ATM device to send immediately
75 * from its TX done IRQ. We want to be able to use atomic_inc_not_zero(), so
76 * inflight == -2 represents an empty queue, -1 one packet, and zero means
77 * there are two packets in the queue.
78 */
79#define NONE_INFLIGHT -2
80
81#define BLOCKED 0
82
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
85 * ID (0xC021) used in autodetection
86 */
87static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
88#define LLC_LEN (4)
89
90static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
91{
92 return (struct pppoatm_vcc *) (atmvcc->user_back);
93}
94
95static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
96{
97 return (struct pppoatm_vcc *) (chan->private);
98}
99
100/*
101 * We can't do this directly from our _pop handler, since the ppp code
102 * doesn't want to be called in interrupt context, so we do it from
103 * a tasklet
104 */
105static void pppoatm_wakeup_sender(unsigned long arg)
106{
107 ppp_output_wakeup((struct ppp_channel *) arg);
108}
109
110/*
111 * This gets called every time the ATM card has finished sending our
112 * skb. The ->old_pop will take care up normal atm flow control,
113 * but we also need to wake up the device if we blocked it
114 */
115static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
116{
117 struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
David Woodhouse9d02daf752012-04-08 09:55:43 +0000118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 pvcc->old_pop(atmvcc, skb);
David Woodhouse9d02daf752012-04-08 09:55:43 +0000120 atomic_dec(&pvcc->inflight);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /*
David Woodhouse9d02daf752012-04-08 09:55:43 +0000123 * We always used to run the wakeup tasklet unconditionally here, for
124 * fear of race conditions where we clear the BLOCKED flag just as we
125 * refuse another packet in pppoatm_send(). This was quite inefficient.
126 *
127 * In fact it's OK. The PPP core will only ever call pppoatm_send()
128 * while holding the channel->downl lock. And ppp_output_wakeup() as
129 * called by the tasklet will *also* grab that lock. So even if another
130 * CPU is in pppoatm_send() right now, the tasklet isn't going to race
131 * with it. The wakeup *will* happen after the other CPU is safely out
132 * of pppoatm_send() again.
133 *
134 * So if the CPU in pppoatm_send() has already set the BLOCKED bit and
135 * it about to return, that's fine. We trigger a wakeup which will
136 * happen later. And if the CPU in pppoatm_send() *hasn't* set the
137 * BLOCKED bit yet, that's fine too because of the double check in
138 * pppoatm_may_send() which is commented there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 */
David Woodhouse9d02daf752012-04-08 09:55:43 +0000140 if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
141 tasklet_schedule(&pvcc->wakeup_tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144/*
145 * Unbind from PPP - currently we only do this when closing the socket,
146 * but we could put this into an ioctl if need be
147 */
148static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
149{
150 struct pppoatm_vcc *pvcc;
151 pvcc = atmvcc_to_pvcc(atmvcc);
152 atmvcc->push = pvcc->old_push;
153 atmvcc->pop = pvcc->old_pop;
154 tasklet_kill(&pvcc->wakeup_tasklet);
155 ppp_unregister_channel(&pvcc->chan);
156 atmvcc->user_back = NULL;
157 kfree(pvcc);
158 /* Gee, I hope we have the big kernel lock here... */
159 module_put(THIS_MODULE);
160}
161
162/* Called when an AAL5 PDU comes in */
163static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
164{
165 struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
Joe Perches99824462010-01-26 11:40:00 +0000166 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (skb == NULL) { /* VCC was closed */
Stephen Hemminger52240062007-08-28 15:22:09 -0700168 pr_debug("removing ATMPPP VCC %p\n", pvcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 pppoatm_unassign_vcc(atmvcc);
170 atmvcc->push(atmvcc, NULL); /* Pass along bad news */
171 return;
172 }
173 atm_return(atmvcc, skb->truesize);
174 switch (pvcc->encaps) {
175 case e_llc:
176 if (skb->len < LLC_LEN ||
177 memcmp(skb->data, pppllc, LLC_LEN))
178 goto error;
179 skb_pull(skb, LLC_LEN);
180 break;
181 case e_autodetect:
182 if (pvcc->chan.ppp == NULL) { /* Not bound yet! */
183 kfree_skb(skb);
184 return;
185 }
186 if (skb->len >= sizeof(pppllc) &&
187 !memcmp(skb->data, pppllc, sizeof(pppllc))) {
188 pvcc->encaps = e_llc;
189 skb_pull(skb, LLC_LEN);
190 break;
191 }
192 if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
193 !memcmp(skb->data, &pppllc[LLC_LEN],
194 sizeof(pppllc) - LLC_LEN)) {
195 pvcc->encaps = e_vc;
196 pvcc->chan.mtu += LLC_LEN;
197 break;
198 }
Joe Perches99824462010-01-26 11:40:00 +0000199 pr_debug("Couldn't autodetect yet (skb: %02X %02X %02X %02X %02X %02X)\n",
200 skb->data[0], skb->data[1], skb->data[2],
201 skb->data[3], skb->data[4], skb->data[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto error;
203 case e_vc:
204 break;
205 }
206 ppp_input(&pvcc->chan, skb);
207 return;
Joe Perchesd81219d2010-01-26 11:40:12 +0000208
209error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 kfree_skb(skb);
211 ppp_input_error(&pvcc->chan, 0);
212}
213
David Woodhouse9d02daf752012-04-08 09:55:43 +0000214static inline int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
215{
216 /*
217 * It's not clear that we need to bother with using atm_may_send()
218 * to check we don't exceed sk->sk_sndbuf. If userspace sets a
219 * value of sk_sndbuf which is lower than the MTU, we're going to
220 * block for ever. But the code always did that before we introduced
221 * the packet count limit, so...
222 */
223 if (atm_may_send(pvcc->atmvcc, size) &&
224 atomic_inc_not_zero_hint(&pvcc->inflight, NONE_INFLIGHT))
225 return 1;
226
227 /*
228 * We use test_and_set_bit() rather than set_bit() here because
229 * we need to ensure there's a memory barrier after it. The bit
230 * *must* be set before we do the atomic_inc() on pvcc->inflight.
231 * There's no smp_mb__after_set_bit(), so it's this or abuse
232 * smp_mb__after_clear_bit().
233 */
234 test_and_set_bit(BLOCKED, &pvcc->blocked);
235
236 /*
237 * We may have raced with pppoatm_pop(). If it ran for the
238 * last packet in the queue, *just* before we set the BLOCKED
239 * bit, then it might never run again and the channel could
240 * remain permanently blocked. Cope with that race by checking
241 * *again*. If it did run in that window, we'll have space on
242 * the queue now and can return success. It's harmless to leave
243 * the BLOCKED flag set, since it's only used as a trigger to
244 * run the wakeup tasklet. Another wakeup will never hurt.
245 * If pppoatm_pop() is running but hasn't got as far as making
246 * space on the queue yet, then it hasn't checked the BLOCKED
247 * flag yet either, so we're safe in that case too. It'll issue
248 * an "immediate" wakeup... where "immediate" actually involves
249 * taking the PPP channel's ->downl lock, which is held by the
250 * code path that calls pppoatm_send(), and is thus going to
251 * wait for us to finish.
252 */
253 if (atm_may_send(pvcc->atmvcc, size) &&
254 atomic_inc_not_zero(&pvcc->inflight))
255 return 1;
256
257 return 0;
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * Called by the ppp_generic.c to send a packet - returns true if packet
261 * was accepted. If we return false, then it's our job to call
262 * ppp_output_wakeup(chan) when we're feeling more up to it.
263 * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
264 * we should really drop the packet, but the generic layer doesn't
265 * support this yet. We just return 'DROP_PACKET' which we actually define
266 * as success, just to be clear what we're really doing.
267 */
268#define DROP_PACKET 1
269static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
270{
271 struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
272 ATM_SKB(skb)->vcc = pvcc->atmvcc;
Joe Perches99824462010-01-26 11:40:00 +0000273 pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
275 (void) skb_pull(skb, 1);
276 switch (pvcc->encaps) { /* LLC encapsulation needed */
277 case e_llc:
278 if (skb_headroom(skb) < LLC_LEN) {
279 struct sk_buff *n;
280 n = skb_realloc_headroom(skb, LLC_LEN);
281 if (n != NULL &&
David Woodhouse9d02daf752012-04-08 09:55:43 +0000282 !pppoatm_may_send(pvcc, n->truesize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 kfree_skb(n);
284 goto nospace;
285 }
Eric Dumazet5d0ba552012-06-04 01:17:19 +0000286 consume_skb(skb);
Joe Perchesd81219d2010-01-26 11:40:12 +0000287 skb = n;
288 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return DROP_PACKET;
David Woodhouse9d02daf752012-04-08 09:55:43 +0000290 } else if (!pppoatm_may_send(pvcc, skb->truesize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 goto nospace;
292 memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
293 break;
294 case e_vc:
David Woodhouse9d02daf752012-04-08 09:55:43 +0000295 if (!pppoatm_may_send(pvcc, skb->truesize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 goto nospace;
297 break;
298 case e_autodetect:
Stephen Hemminger52240062007-08-28 15:22:09 -0700299 pr_debug("Trying to send without setting encaps!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 kfree_skb(skb);
301 return 1;
302 }
303
304 atomic_add(skb->truesize, &sk_atm(ATM_SKB(skb)->vcc)->sk_wmem_alloc);
305 ATM_SKB(skb)->atm_options = ATM_SKB(skb)->vcc->atm_options;
Joe Perches99824462010-01-26 11:40:00 +0000306 pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
307 skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
309 ? DROP_PACKET : 1;
Joe Perchesd81219d2010-01-26 11:40:12 +0000310nospace:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
312 * We don't have space to send this SKB now, but we might have
313 * already applied SC_COMP_PROT compression, so may need to undo
314 */
315 if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
316 skb->data[-1] == '\0')
317 (void) skb_push(skb, 1);
318 return 0;
319}
320
321/* This handles ioctls sent to the /dev/ppp interface */
322static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
323 unsigned long arg)
324{
325 switch (cmd) {
326 case PPPIOCGFLAGS:
327 return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
328 ? -EFAULT : 0;
329 case PPPIOCSFLAGS:
330 return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
331 ? -EFAULT : 0;
332 }
333 return -ENOTTY;
334}
335
stephen hemmingerd7100da2010-08-04 07:34:36 +0000336static const struct ppp_channel_ops pppoatm_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .start_xmit = pppoatm_send,
338 .ioctl = pppoatm_devppp_ioctl,
339};
340
341static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
342{
343 struct atm_backend_ppp be;
344 struct pppoatm_vcc *pvcc;
345 int err;
346 /*
347 * Each PPPoATM instance has its own tasklet - this is just a
348 * prototypical one used to initialize them
349 */
350 static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0);
351 if (copy_from_user(&be, arg, sizeof be))
352 return -EFAULT;
353 if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
354 be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
355 return -EINVAL;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700356 pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (pvcc == NULL)
358 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pvcc->atmvcc = atmvcc;
David Woodhouse9d02daf752012-04-08 09:55:43 +0000360
361 /* Maximum is zero, so that we can use atomic_inc_not_zero() */
362 atomic_set(&pvcc->inflight, NONE_INFLIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 pvcc->old_push = atmvcc->push;
364 pvcc->old_pop = atmvcc->pop;
365 pvcc->encaps = (enum pppoatm_encaps) be.encaps;
366 pvcc->chan.private = pvcc;
367 pvcc->chan.ops = &pppoatm_ops;
368 pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
369 (be.encaps == e_vc ? 0 : LLC_LEN);
370 pvcc->wakeup_tasklet = tasklet_proto;
371 pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
Joe Perchesd81219d2010-01-26 11:40:12 +0000372 err = ppp_register_channel(&pvcc->chan);
373 if (err != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 kfree(pvcc);
375 return err;
376 }
377 atmvcc->user_back = pvcc;
378 atmvcc->push = pppoatm_push;
379 atmvcc->pop = pppoatm_pop;
380 __module_get(THIS_MODULE);
Jorge Boncompte [DTI2]4e55f572011-11-21 10:25:57 +0000381
382 /* re-process everything received between connection setup and
383 backend setup */
384 vcc_process_recv_queue(atmvcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
386}
387
388/*
389 * This handles ioctls actually performed on our vcc - we must return
390 * -ENOIOCTLCMD for any unrecognized ioctl
391 */
392static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
393 unsigned long arg)
394{
395 struct atm_vcc *atmvcc = ATM_SD(sock);
396 void __user *argp = (void __user *)arg;
397
398 if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
399 return -ENOIOCTLCMD;
400 switch (cmd) {
401 case ATM_SETBACKEND: {
402 atm_backend_t b;
403 if (get_user(b, (atm_backend_t __user *) argp))
404 return -EFAULT;
405 if (b != ATM_BACKEND_PPP)
406 return -ENOIOCTLCMD;
407 if (!capable(CAP_NET_ADMIN))
408 return -EPERM;
Krzysztof Mazur3b1a9142012-11-06 23:16:58 +0100409 if (sock->state != SS_CONNECTED)
410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return pppoatm_assign_vcc(atmvcc, argp);
412 }
413 case PPPIOCGCHAN:
414 return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
415 chan), (int __user *) argp) ? -EFAULT : 0;
416 case PPPIOCGUNIT:
417 return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
418 chan), (int __user *) argp) ? -EFAULT : 0;
419 }
420 return -ENOIOCTLCMD;
421}
422
423static struct atm_ioctl pppoatm_ioctl_ops = {
424 .owner = THIS_MODULE,
425 .ioctl = pppoatm_ioctl,
426};
427
428static int __init pppoatm_init(void)
429{
430 register_atm_ioctl(&pppoatm_ioctl_ops);
431 return 0;
432}
433
434static void __exit pppoatm_exit(void)
435{
436 deregister_atm_ioctl(&pppoatm_ioctl_ops);
437}
438
439module_init(pppoatm_init);
440module_exit(pppoatm_exit);
441
442MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
443MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
444MODULE_LICENSE("GPL");