blob: 400839273c67503e6fa4207c2d2949a5f99e351f [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>
40#include <linux/skbuff.h>
41#include <linux/atm.h>
42#include <linux/atmdev.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080043#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/ppp_defs.h>
45#include <linux/if_ppp.h>
46#include <linux/ppp_channel.h>
47#include <linux/atmppp.h>
48
49#include "common.h"
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051enum pppoatm_encaps {
52 e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
53 e_vc = PPPOATM_ENCAPS_VC,
54 e_llc = PPPOATM_ENCAPS_LLC,
55};
56
57struct pppoatm_vcc {
58 struct atm_vcc *atmvcc; /* VCC descriptor */
59 void (*old_push)(struct atm_vcc *, struct sk_buff *);
60 void (*old_pop)(struct atm_vcc *, struct sk_buff *);
61 /* keep old push/pop for detaching */
62 enum pppoatm_encaps encaps;
63 int flags; /* SC_COMP_PROT - compress protocol */
64 struct ppp_channel chan; /* interface to generic ppp layer */
65 struct tasklet_struct wakeup_tasklet;
66};
67
68/*
69 * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
70 * ID (0xC021) used in autodetection
71 */
72static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
73#define LLC_LEN (4)
74
75static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
76{
77 return (struct pppoatm_vcc *) (atmvcc->user_back);
78}
79
80static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
81{
82 return (struct pppoatm_vcc *) (chan->private);
83}
84
85/*
86 * We can't do this directly from our _pop handler, since the ppp code
87 * doesn't want to be called in interrupt context, so we do it from
88 * a tasklet
89 */
90static void pppoatm_wakeup_sender(unsigned long arg)
91{
92 ppp_output_wakeup((struct ppp_channel *) arg);
93}
94
95/*
96 * This gets called every time the ATM card has finished sending our
97 * skb. The ->old_pop will take care up normal atm flow control,
98 * but we also need to wake up the device if we blocked it
99 */
100static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
101{
102 struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
103 pvcc->old_pop(atmvcc, skb);
104 /*
105 * We don't really always want to do this since it's
106 * really inefficient - it would be much better if we could
107 * test if we had actually throttled the generic layer.
108 * Unfortunately then there would be a nasty SMP race where
109 * we could clear that flag just as we refuse another packet.
110 * For now we do the safe thing.
111 */
112 tasklet_schedule(&pvcc->wakeup_tasklet);
113}
114
115/*
116 * Unbind from PPP - currently we only do this when closing the socket,
117 * but we could put this into an ioctl if need be
118 */
119static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
120{
121 struct pppoatm_vcc *pvcc;
122 pvcc = atmvcc_to_pvcc(atmvcc);
123 atmvcc->push = pvcc->old_push;
124 atmvcc->pop = pvcc->old_pop;
125 tasklet_kill(&pvcc->wakeup_tasklet);
126 ppp_unregister_channel(&pvcc->chan);
127 atmvcc->user_back = NULL;
128 kfree(pvcc);
129 /* Gee, I hope we have the big kernel lock here... */
130 module_put(THIS_MODULE);
131}
132
133/* Called when an AAL5 PDU comes in */
134static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
135{
136 struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
Joe Perches99824462010-01-26 11:40:00 +0000137 pr_debug("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (skb == NULL) { /* VCC was closed */
Stephen Hemminger52240062007-08-28 15:22:09 -0700139 pr_debug("removing ATMPPP VCC %p\n", pvcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 pppoatm_unassign_vcc(atmvcc);
141 atmvcc->push(atmvcc, NULL); /* Pass along bad news */
142 return;
143 }
144 atm_return(atmvcc, skb->truesize);
145 switch (pvcc->encaps) {
146 case e_llc:
147 if (skb->len < LLC_LEN ||
148 memcmp(skb->data, pppllc, LLC_LEN))
149 goto error;
150 skb_pull(skb, LLC_LEN);
151 break;
152 case e_autodetect:
153 if (pvcc->chan.ppp == NULL) { /* Not bound yet! */
154 kfree_skb(skb);
155 return;
156 }
157 if (skb->len >= sizeof(pppllc) &&
158 !memcmp(skb->data, pppllc, sizeof(pppllc))) {
159 pvcc->encaps = e_llc;
160 skb_pull(skb, LLC_LEN);
161 break;
162 }
163 if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
164 !memcmp(skb->data, &pppllc[LLC_LEN],
165 sizeof(pppllc) - LLC_LEN)) {
166 pvcc->encaps = e_vc;
167 pvcc->chan.mtu += LLC_LEN;
168 break;
169 }
Joe Perches99824462010-01-26 11:40:00 +0000170 pr_debug("Couldn't autodetect yet (skb: %02X %02X %02X %02X %02X %02X)\n",
171 skb->data[0], skb->data[1], skb->data[2],
172 skb->data[3], skb->data[4], skb->data[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 goto error;
174 case e_vc:
175 break;
176 }
177 ppp_input(&pvcc->chan, skb);
178 return;
Joe Perchesd81219d2010-01-26 11:40:12 +0000179
180error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 kfree_skb(skb);
182 ppp_input_error(&pvcc->chan, 0);
183}
184
185/*
186 * Called by the ppp_generic.c to send a packet - returns true if packet
187 * was accepted. If we return false, then it's our job to call
188 * ppp_output_wakeup(chan) when we're feeling more up to it.
189 * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
190 * we should really drop the packet, but the generic layer doesn't
191 * support this yet. We just return 'DROP_PACKET' which we actually define
192 * as success, just to be clear what we're really doing.
193 */
194#define DROP_PACKET 1
195static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
196{
197 struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
198 ATM_SKB(skb)->vcc = pvcc->atmvcc;
Joe Perches99824462010-01-26 11:40:00 +0000199 pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
201 (void) skb_pull(skb, 1);
202 switch (pvcc->encaps) { /* LLC encapsulation needed */
203 case e_llc:
204 if (skb_headroom(skb) < LLC_LEN) {
205 struct sk_buff *n;
206 n = skb_realloc_headroom(skb, LLC_LEN);
207 if (n != NULL &&
208 !atm_may_send(pvcc->atmvcc, n->truesize)) {
209 kfree_skb(n);
210 goto nospace;
211 }
212 kfree_skb(skb);
Joe Perchesd81219d2010-01-26 11:40:12 +0000213 skb = n;
214 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return DROP_PACKET;
216 } else if (!atm_may_send(pvcc->atmvcc, skb->truesize))
217 goto nospace;
218 memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
219 break;
220 case e_vc:
221 if (!atm_may_send(pvcc->atmvcc, skb->truesize))
222 goto nospace;
223 break;
224 case e_autodetect:
Stephen Hemminger52240062007-08-28 15:22:09 -0700225 pr_debug("Trying to send without setting encaps!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 kfree_skb(skb);
227 return 1;
228 }
229
230 atomic_add(skb->truesize, &sk_atm(ATM_SKB(skb)->vcc)->sk_wmem_alloc);
231 ATM_SKB(skb)->atm_options = ATM_SKB(skb)->vcc->atm_options;
Joe Perches99824462010-01-26 11:40:00 +0000232 pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
233 skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
235 ? DROP_PACKET : 1;
Joe Perchesd81219d2010-01-26 11:40:12 +0000236nospace:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /*
238 * We don't have space to send this SKB now, but we might have
239 * already applied SC_COMP_PROT compression, so may need to undo
240 */
241 if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
242 skb->data[-1] == '\0')
243 (void) skb_push(skb, 1);
244 return 0;
245}
246
247/* This handles ioctls sent to the /dev/ppp interface */
248static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
249 unsigned long arg)
250{
251 switch (cmd) {
252 case PPPIOCGFLAGS:
253 return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
254 ? -EFAULT : 0;
255 case PPPIOCSFLAGS:
256 return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
257 ? -EFAULT : 0;
258 }
259 return -ENOTTY;
260}
261
262static /*const*/ struct ppp_channel_ops pppoatm_ops = {
263 .start_xmit = pppoatm_send,
264 .ioctl = pppoatm_devppp_ioctl,
265};
266
267static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
268{
269 struct atm_backend_ppp be;
270 struct pppoatm_vcc *pvcc;
271 int err;
272 /*
273 * Each PPPoATM instance has its own tasklet - this is just a
274 * prototypical one used to initialize them
275 */
276 static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0);
277 if (copy_from_user(&be, arg, sizeof be))
278 return -EFAULT;
279 if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
280 be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
281 return -EINVAL;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700282 pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (pvcc == NULL)
284 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 pvcc->atmvcc = atmvcc;
286 pvcc->old_push = atmvcc->push;
287 pvcc->old_pop = atmvcc->pop;
288 pvcc->encaps = (enum pppoatm_encaps) be.encaps;
289 pvcc->chan.private = pvcc;
290 pvcc->chan.ops = &pppoatm_ops;
291 pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
292 (be.encaps == e_vc ? 0 : LLC_LEN);
293 pvcc->wakeup_tasklet = tasklet_proto;
294 pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
Joe Perchesd81219d2010-01-26 11:40:12 +0000295 err = ppp_register_channel(&pvcc->chan);
296 if (err != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 kfree(pvcc);
298 return err;
299 }
300 atmvcc->user_back = pvcc;
301 atmvcc->push = pppoatm_push;
302 atmvcc->pop = pppoatm_pop;
303 __module_get(THIS_MODULE);
304 return 0;
305}
306
307/*
308 * This handles ioctls actually performed on our vcc - we must return
309 * -ENOIOCTLCMD for any unrecognized ioctl
310 */
311static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
312 unsigned long arg)
313{
314 struct atm_vcc *atmvcc = ATM_SD(sock);
315 void __user *argp = (void __user *)arg;
316
317 if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
318 return -ENOIOCTLCMD;
319 switch (cmd) {
320 case ATM_SETBACKEND: {
321 atm_backend_t b;
322 if (get_user(b, (atm_backend_t __user *) argp))
323 return -EFAULT;
324 if (b != ATM_BACKEND_PPP)
325 return -ENOIOCTLCMD;
326 if (!capable(CAP_NET_ADMIN))
327 return -EPERM;
328 return pppoatm_assign_vcc(atmvcc, argp);
329 }
330 case PPPIOCGCHAN:
331 return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
332 chan), (int __user *) argp) ? -EFAULT : 0;
333 case PPPIOCGUNIT:
334 return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
335 chan), (int __user *) argp) ? -EFAULT : 0;
336 }
337 return -ENOIOCTLCMD;
338}
339
340static struct atm_ioctl pppoatm_ioctl_ops = {
341 .owner = THIS_MODULE,
342 .ioctl = pppoatm_ioctl,
343};
344
345static int __init pppoatm_init(void)
346{
347 register_atm_ioctl(&pppoatm_ioctl_ops);
348 return 0;
349}
350
351static void __exit pppoatm_exit(void)
352{
353 deregister_atm_ioctl(&pppoatm_ioctl_ops);
354}
355
356module_init(pppoatm_init);
357module_exit(pppoatm_exit);
358
359MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
360MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
361MODULE_LICENSE("GPL");