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