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