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