Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */ |
| 3 | |
| 4 | /* Copyright 1999-2000 by Mitchell Blank Jr */ |
| 5 | /* Based on clip.c; 1995-1999 by Werner Almesberger, EPFL LRC/ICA */ |
| 6 | /* And on ppp_async.c; Copyright 1999 Paul Mackerras */ |
| 7 | /* And help from Jens Axboe */ |
| 8 | |
| 9 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * This driver provides the encapsulation and framing for sending |
| 12 | * and receiving PPP frames in ATM AAL5 PDUs. |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * One shortcoming of this driver is that it does not comply with |
| 17 | * section 8 of RFC2364 - we are supposed to detect a change |
| 18 | * in encapsulation and immediately abort the connection (in order |
| 19 | * to avoid a black-hole being created if our peer loses state |
| 20 | * and changes encapsulation unilaterally. However, since the |
| 21 | * ppp_generic layer actually does the decapsulation, we need |
| 22 | * a way of notifying it when we _think_ there might be a problem) |
| 23 | * There's two cases: |
| 24 | * 1. LLC-encapsulation was missing when it was enabled. In |
| 25 | * this case, we should tell the upper layer "tear down |
| 26 | * this session if this skb looks ok to you" |
| 27 | * 2. LLC-encapsulation was present when it was disabled. Then |
| 28 | * we need to tell the upper layer "this packet may be |
| 29 | * ok, but if its in error tear down the session" |
| 30 | * These hooks are not yet available in ppp_generic |
| 31 | */ |
| 32 | |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 33 | #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/init.h> |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 37 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/skbuff.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Paul Mackerras | 4b32da2b | 2012-03-04 12:56:55 +0000 | [diff] [blame] | 44 | #include <linux/ppp-ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/ppp_channel.h> |
| 46 | #include <linux/atmppp.h> |
| 47 | |
| 48 | #include "common.h" |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | enum pppoatm_encaps { |
| 51 | e_autodetect = PPPOATM_ENCAPS_AUTODETECT, |
| 52 | e_vc = PPPOATM_ENCAPS_VC, |
| 53 | e_llc = PPPOATM_ENCAPS_LLC, |
| 54 | }; |
| 55 | |
| 56 | struct pppoatm_vcc { |
| 57 | struct atm_vcc *atmvcc; /* VCC descriptor */ |
| 58 | void (*old_push)(struct atm_vcc *, struct sk_buff *); |
| 59 | void (*old_pop)(struct atm_vcc *, struct sk_buff *); |
David Woodhouse | 0e56d99 | 2012-11-28 00:05:52 +0000 | [diff] [blame] | 60 | void (*old_release_cb)(struct atm_vcc *); |
Krzysztof Mazur | e41faed | 2012-11-06 23:16:59 +0100 | [diff] [blame] | 61 | struct module *old_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* keep old push/pop for detaching */ |
| 63 | enum pppoatm_encaps encaps; |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 64 | atomic_t inflight; |
| 65 | unsigned long blocked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | int flags; /* SC_COMP_PROT - compress protocol */ |
| 67 | struct ppp_channel chan; /* interface to generic ppp layer */ |
| 68 | struct tasklet_struct wakeup_tasklet; |
| 69 | }; |
| 70 | |
| 71 | /* |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 72 | * We want to allow two packets in the queue. The one that's currently in |
| 73 | * flight, and *one* queued up ready for the ATM device to send immediately |
| 74 | * from its TX done IRQ. We want to be able to use atomic_inc_not_zero(), so |
| 75 | * inflight == -2 represents an empty queue, -1 one packet, and zero means |
| 76 | * there are two packets in the queue. |
| 77 | */ |
| 78 | #define NONE_INFLIGHT -2 |
| 79 | |
| 80 | #define BLOCKED 0 |
| 81 | |
| 82 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol |
| 84 | * ID (0xC021) used in autodetection |
| 85 | */ |
| 86 | static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 }; |
| 87 | #define LLC_LEN (4) |
| 88 | |
| 89 | static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc) |
| 90 | { |
| 91 | return (struct pppoatm_vcc *) (atmvcc->user_back); |
| 92 | } |
| 93 | |
| 94 | static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan) |
| 95 | { |
| 96 | return (struct pppoatm_vcc *) (chan->private); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * We can't do this directly from our _pop handler, since the ppp code |
| 101 | * doesn't want to be called in interrupt context, so we do it from |
| 102 | * a tasklet |
| 103 | */ |
| 104 | static void pppoatm_wakeup_sender(unsigned long arg) |
| 105 | { |
| 106 | ppp_output_wakeup((struct ppp_channel *) arg); |
| 107 | } |
| 108 | |
David Woodhouse | 0e56d99 | 2012-11-28 00:05:52 +0000 | [diff] [blame] | 109 | static void pppoatm_release_cb(struct atm_vcc *atmvcc) |
| 110 | { |
| 111 | struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc); |
| 112 | |
David Woodhouse | 5b4d720 | 2012-11-28 10:15:05 +0000 | [diff] [blame] | 113 | /* |
| 114 | * As in pppoatm_pop(), it's safe to clear the BLOCKED bit here because |
| 115 | * the wakeup *can't* race with pppoatm_send(). They both hold the PPP |
| 116 | * channel's ->downl lock. And the potential race with *setting* it, |
| 117 | * which leads to the double-check dance in pppoatm_may_send(), doesn't |
| 118 | * exist here. In the sock_owned_by_user() case in pppoatm_send(), we |
| 119 | * set the BLOCKED bit while the socket is still locked. We know that |
| 120 | * ->release_cb() can't be called until that's done. |
| 121 | */ |
| 122 | if (test_and_clear_bit(BLOCKED, &pvcc->blocked)) |
| 123 | tasklet_schedule(&pvcc->wakeup_tasklet); |
David Woodhouse | 0e56d99 | 2012-11-28 00:05:52 +0000 | [diff] [blame] | 124 | if (pvcc->old_release_cb) |
| 125 | pvcc->old_release_cb(atmvcc); |
| 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* |
| 128 | * This gets called every time the ATM card has finished sending our |
| 129 | * skb. The ->old_pop will take care up normal atm flow control, |
| 130 | * but we also need to wake up the device if we blocked it |
| 131 | */ |
| 132 | static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb) |
| 133 | { |
| 134 | struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc); |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | pvcc->old_pop(atmvcc, skb); |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 137 | atomic_dec(&pvcc->inflight); |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | /* |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 140 | * We always used to run the wakeup tasklet unconditionally here, for |
| 141 | * fear of race conditions where we clear the BLOCKED flag just as we |
| 142 | * refuse another packet in pppoatm_send(). This was quite inefficient. |
| 143 | * |
| 144 | * In fact it's OK. The PPP core will only ever call pppoatm_send() |
| 145 | * while holding the channel->downl lock. And ppp_output_wakeup() as |
| 146 | * called by the tasklet will *also* grab that lock. So even if another |
| 147 | * CPU is in pppoatm_send() right now, the tasklet isn't going to race |
| 148 | * with it. The wakeup *will* happen after the other CPU is safely out |
| 149 | * of pppoatm_send() again. |
| 150 | * |
| 151 | * So if the CPU in pppoatm_send() has already set the BLOCKED bit and |
| 152 | * it about to return, that's fine. We trigger a wakeup which will |
| 153 | * happen later. And if the CPU in pppoatm_send() *hasn't* set the |
| 154 | * BLOCKED bit yet, that's fine too because of the double check in |
| 155 | * pppoatm_may_send() which is commented there. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | */ |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 157 | if (test_and_clear_bit(BLOCKED, &pvcc->blocked)) |
| 158 | tasklet_schedule(&pvcc->wakeup_tasklet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Unbind from PPP - currently we only do this when closing the socket, |
| 163 | * but we could put this into an ioctl if need be |
| 164 | */ |
| 165 | static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc) |
| 166 | { |
| 167 | struct pppoatm_vcc *pvcc; |
| 168 | pvcc = atmvcc_to_pvcc(atmvcc); |
| 169 | atmvcc->push = pvcc->old_push; |
| 170 | atmvcc->pop = pvcc->old_pop; |
David Woodhouse | 0e56d99 | 2012-11-28 00:05:52 +0000 | [diff] [blame] | 171 | atmvcc->release_cb = pvcc->old_release_cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | tasklet_kill(&pvcc->wakeup_tasklet); |
| 173 | ppp_unregister_channel(&pvcc->chan); |
| 174 | atmvcc->user_back = NULL; |
| 175 | kfree(pvcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* Called when an AAL5 PDU comes in */ |
| 179 | static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb) |
| 180 | { |
| 181 | struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc); |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 182 | pr_debug("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (skb == NULL) { /* VCC was closed */ |
Krzysztof Mazur | e41faed | 2012-11-06 23:16:59 +0100 | [diff] [blame] | 184 | struct module *module; |
| 185 | |
Stephen Hemminger | 5224006 | 2007-08-28 15:22:09 -0700 | [diff] [blame] | 186 | pr_debug("removing ATMPPP VCC %p\n", pvcc); |
Krzysztof Mazur | e41faed | 2012-11-06 23:16:59 +0100 | [diff] [blame] | 187 | module = pvcc->old_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | pppoatm_unassign_vcc(atmvcc); |
| 189 | atmvcc->push(atmvcc, NULL); /* Pass along bad news */ |
Krzysztof Mazur | e41faed | 2012-11-06 23:16:59 +0100 | [diff] [blame] | 190 | module_put(module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | atm_return(atmvcc, skb->truesize); |
| 194 | switch (pvcc->encaps) { |
| 195 | case e_llc: |
| 196 | if (skb->len < LLC_LEN || |
| 197 | memcmp(skb->data, pppllc, LLC_LEN)) |
| 198 | goto error; |
| 199 | skb_pull(skb, LLC_LEN); |
| 200 | break; |
| 201 | case e_autodetect: |
| 202 | if (pvcc->chan.ppp == NULL) { /* Not bound yet! */ |
| 203 | kfree_skb(skb); |
| 204 | return; |
| 205 | } |
| 206 | if (skb->len >= sizeof(pppllc) && |
| 207 | !memcmp(skb->data, pppllc, sizeof(pppllc))) { |
| 208 | pvcc->encaps = e_llc; |
| 209 | skb_pull(skb, LLC_LEN); |
| 210 | break; |
| 211 | } |
| 212 | if (skb->len >= (sizeof(pppllc) - LLC_LEN) && |
| 213 | !memcmp(skb->data, &pppllc[LLC_LEN], |
| 214 | sizeof(pppllc) - LLC_LEN)) { |
| 215 | pvcc->encaps = e_vc; |
| 216 | pvcc->chan.mtu += LLC_LEN; |
| 217 | break; |
| 218 | } |
Andy Shevchenko | a8a213c | 2019-09-04 20:44:59 +0300 | [diff] [blame] | 219 | pr_debug("Couldn't autodetect yet (skb: %6ph)\n", skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | goto error; |
| 221 | case e_vc: |
| 222 | break; |
| 223 | } |
| 224 | ppp_input(&pvcc->chan, skb); |
| 225 | return; |
Joe Perches | d81219d | 2010-01-26 11:40:12 +0000 | [diff] [blame] | 226 | |
| 227 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | kfree_skb(skb); |
| 229 | ppp_input_error(&pvcc->chan, 0); |
| 230 | } |
| 231 | |
Krzysztof Mazur | 397ff16 | 2012-11-06 23:17:02 +0100 | [diff] [blame] | 232 | static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size) |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 233 | { |
| 234 | /* |
| 235 | * It's not clear that we need to bother with using atm_may_send() |
| 236 | * to check we don't exceed sk->sk_sndbuf. If userspace sets a |
| 237 | * value of sk_sndbuf which is lower than the MTU, we're going to |
| 238 | * block for ever. But the code always did that before we introduced |
| 239 | * the packet count limit, so... |
| 240 | */ |
| 241 | if (atm_may_send(pvcc->atmvcc, size) && |
Mark Rutland | f74445b | 2018-06-21 13:13:06 +0100 | [diff] [blame] | 242 | atomic_inc_not_zero(&pvcc->inflight)) |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 243 | return 1; |
| 244 | |
| 245 | /* |
| 246 | * We use test_and_set_bit() rather than set_bit() here because |
| 247 | * we need to ensure there's a memory barrier after it. The bit |
| 248 | * *must* be set before we do the atomic_inc() on pvcc->inflight. |
| 249 | * There's no smp_mb__after_set_bit(), so it's this or abuse |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 250 | * smp_mb__after_atomic(). |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 251 | */ |
| 252 | test_and_set_bit(BLOCKED, &pvcc->blocked); |
| 253 | |
| 254 | /* |
| 255 | * We may have raced with pppoatm_pop(). If it ran for the |
| 256 | * last packet in the queue, *just* before we set the BLOCKED |
| 257 | * bit, then it might never run again and the channel could |
| 258 | * remain permanently blocked. Cope with that race by checking |
| 259 | * *again*. If it did run in that window, we'll have space on |
| 260 | * the queue now and can return success. It's harmless to leave |
| 261 | * the BLOCKED flag set, since it's only used as a trigger to |
| 262 | * run the wakeup tasklet. Another wakeup will never hurt. |
| 263 | * If pppoatm_pop() is running but hasn't got as far as making |
| 264 | * space on the queue yet, then it hasn't checked the BLOCKED |
| 265 | * flag yet either, so we're safe in that case too. It'll issue |
| 266 | * an "immediate" wakeup... where "immediate" actually involves |
| 267 | * taking the PPP channel's ->downl lock, which is held by the |
| 268 | * code path that calls pppoatm_send(), and is thus going to |
| 269 | * wait for us to finish. |
| 270 | */ |
| 271 | if (atm_may_send(pvcc->atmvcc, size) && |
| 272 | atomic_inc_not_zero(&pvcc->inflight)) |
| 273 | return 1; |
| 274 | |
| 275 | return 0; |
| 276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* |
| 278 | * Called by the ppp_generic.c to send a packet - returns true if packet |
| 279 | * was accepted. If we return false, then it's our job to call |
| 280 | * ppp_output_wakeup(chan) when we're feeling more up to it. |
| 281 | * Note that in the ENOMEM case (as opposed to the !atm_may_send case) |
| 282 | * we should really drop the packet, but the generic layer doesn't |
| 283 | * support this yet. We just return 'DROP_PACKET' which we actually define |
| 284 | * as success, just to be clear what we're really doing. |
| 285 | */ |
| 286 | #define DROP_PACKET 1 |
| 287 | static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb) |
| 288 | { |
| 289 | struct pppoatm_vcc *pvcc = chan_to_pvcc(chan); |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 290 | struct atm_vcc *vcc; |
| 291 | int ret; |
| 292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | ATM_SKB(skb)->vcc = pvcc->atmvcc; |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 294 | pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT)) |
| 296 | (void) skb_pull(skb, 1); |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 297 | |
| 298 | vcc = ATM_SKB(skb)->vcc; |
| 299 | bh_lock_sock(sk_atm(vcc)); |
David Woodhouse | 5b4d720 | 2012-11-28 10:15:05 +0000 | [diff] [blame] | 300 | if (sock_owned_by_user(sk_atm(vcc))) { |
| 301 | /* |
| 302 | * Needs to happen (and be flushed, hence test_and_) before we unlock |
| 303 | * the socket. It needs to be seen by the time our ->release_cb gets |
| 304 | * called. |
| 305 | */ |
| 306 | test_and_set_bit(BLOCKED, &pvcc->blocked); |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 307 | goto nospace; |
David Woodhouse | 5b4d720 | 2012-11-28 10:15:05 +0000 | [diff] [blame] | 308 | } |
Krzysztof Mazur | 071d939 | 2012-11-10 23:33:19 +0100 | [diff] [blame] | 309 | if (test_bit(ATM_VF_RELEASED, &vcc->flags) || |
| 310 | test_bit(ATM_VF_CLOSE, &vcc->flags) || |
| 311 | !test_bit(ATM_VF_READY, &vcc->flags)) { |
| 312 | bh_unlock_sock(sk_atm(vcc)); |
| 313 | kfree_skb(skb); |
| 314 | return DROP_PACKET; |
| 315 | } |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | switch (pvcc->encaps) { /* LLC encapsulation needed */ |
| 318 | case e_llc: |
| 319 | if (skb_headroom(skb) < LLC_LEN) { |
| 320 | struct sk_buff *n; |
| 321 | n = skb_realloc_headroom(skb, LLC_LEN); |
| 322 | if (n != NULL && |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 323 | !pppoatm_may_send(pvcc, n->truesize)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | kfree_skb(n); |
| 325 | goto nospace; |
| 326 | } |
Eric Dumazet | 5d0ba55 | 2012-06-04 01:17:19 +0000 | [diff] [blame] | 327 | consume_skb(skb); |
Joe Perches | d81219d | 2010-01-26 11:40:12 +0000 | [diff] [blame] | 328 | skb = n; |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 329 | if (skb == NULL) { |
| 330 | bh_unlock_sock(sk_atm(vcc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return DROP_PACKET; |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 332 | } |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 333 | } else if (!pppoatm_may_send(pvcc, skb->truesize)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | goto nospace; |
| 335 | memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN); |
| 336 | break; |
| 337 | case e_vc: |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 338 | if (!pppoatm_may_send(pvcc, skb->truesize)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | goto nospace; |
| 340 | break; |
| 341 | case e_autodetect: |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 342 | bh_unlock_sock(sk_atm(vcc)); |
Stephen Hemminger | 5224006 | 2007-08-28 15:22:09 -0700 | [diff] [blame] | 343 | pr_debug("Trying to send without setting encaps!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | kfree_skb(skb); |
| 345 | return 1; |
| 346 | } |
| 347 | |
David Woodhouse | 9bbe60a | 2018-06-16 11:55:44 +0100 | [diff] [blame] | 348 | atm_account_tx(vcc, skb); |
Joe Perches | 9982446 | 2010-01-26 11:40:00 +0000 | [diff] [blame] | 349 | pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n", |
| 350 | skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev); |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 351 | ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | ? DROP_PACKET : 1; |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 353 | bh_unlock_sock(sk_atm(vcc)); |
| 354 | return ret; |
Joe Perches | d81219d | 2010-01-26 11:40:12 +0000 | [diff] [blame] | 355 | nospace: |
Krzysztof Mazur | 3ac1080 | 2012-11-06 23:17:00 +0100 | [diff] [blame] | 356 | bh_unlock_sock(sk_atm(vcc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | /* |
| 358 | * We don't have space to send this SKB now, but we might have |
| 359 | * already applied SC_COMP_PROT compression, so may need to undo |
| 360 | */ |
| 361 | if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 && |
| 362 | skb->data[-1] == '\0') |
| 363 | (void) skb_push(skb, 1); |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /* This handles ioctls sent to the /dev/ppp interface */ |
| 368 | static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd, |
| 369 | unsigned long arg) |
| 370 | { |
| 371 | switch (cmd) { |
| 372 | case PPPIOCGFLAGS: |
| 373 | return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg) |
| 374 | ? -EFAULT : 0; |
| 375 | case PPPIOCSFLAGS: |
| 376 | return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg) |
| 377 | ? -EFAULT : 0; |
| 378 | } |
| 379 | return -ENOTTY; |
| 380 | } |
| 381 | |
stephen hemminger | d7100da | 2010-08-04 07:34:36 +0000 | [diff] [blame] | 382 | static const struct ppp_channel_ops pppoatm_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | .start_xmit = pppoatm_send, |
| 384 | .ioctl = pppoatm_devppp_ioctl, |
| 385 | }; |
| 386 | |
| 387 | static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg) |
| 388 | { |
| 389 | struct atm_backend_ppp be; |
| 390 | struct pppoatm_vcc *pvcc; |
| 391 | int err; |
| 392 | /* |
| 393 | * Each PPPoATM instance has its own tasklet - this is just a |
| 394 | * prototypical one used to initialize them |
| 395 | */ |
| 396 | static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0); |
| 397 | if (copy_from_user(&be, arg, sizeof be)) |
| 398 | return -EFAULT; |
| 399 | if (be.encaps != PPPOATM_ENCAPS_AUTODETECT && |
| 400 | be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC) |
| 401 | return -EINVAL; |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 402 | pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | if (pvcc == NULL) |
| 404 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | pvcc->atmvcc = atmvcc; |
David Woodhouse | 9d02daf75 | 2012-04-08 09:55:43 +0000 | [diff] [blame] | 406 | |
| 407 | /* Maximum is zero, so that we can use atomic_inc_not_zero() */ |
| 408 | atomic_set(&pvcc->inflight, NONE_INFLIGHT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | pvcc->old_push = atmvcc->push; |
| 410 | pvcc->old_pop = atmvcc->pop; |
Krzysztof Mazur | e41faed | 2012-11-06 23:16:59 +0100 | [diff] [blame] | 411 | pvcc->old_owner = atmvcc->owner; |
David Woodhouse | 0e56d99 | 2012-11-28 00:05:52 +0000 | [diff] [blame] | 412 | pvcc->old_release_cb = atmvcc->release_cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | pvcc->encaps = (enum pppoatm_encaps) be.encaps; |
| 414 | pvcc->chan.private = pvcc; |
| 415 | pvcc->chan.ops = &pppoatm_ops; |
| 416 | pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN - |
| 417 | (be.encaps == e_vc ? 0 : LLC_LEN); |
| 418 | pvcc->wakeup_tasklet = tasklet_proto; |
| 419 | pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan; |
Joe Perches | d81219d | 2010-01-26 11:40:12 +0000 | [diff] [blame] | 420 | err = ppp_register_channel(&pvcc->chan); |
| 421 | if (err != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | kfree(pvcc); |
| 423 | return err; |
| 424 | } |
| 425 | atmvcc->user_back = pvcc; |
| 426 | atmvcc->push = pppoatm_push; |
| 427 | atmvcc->pop = pppoatm_pop; |
David Woodhouse | 0e56d99 | 2012-11-28 00:05:52 +0000 | [diff] [blame] | 428 | atmvcc->release_cb = pppoatm_release_cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | __module_get(THIS_MODULE); |
Krzysztof Mazur | e41faed | 2012-11-06 23:16:59 +0100 | [diff] [blame] | 430 | atmvcc->owner = THIS_MODULE; |
Jorge Boncompte [DTI2] | 4e55f57 | 2011-11-21 10:25:57 +0000 | [diff] [blame] | 431 | |
| 432 | /* re-process everything received between connection setup and |
| 433 | backend setup */ |
| 434 | vcc_process_recv_queue(atmvcc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * This handles ioctls actually performed on our vcc - we must return |
| 440 | * -ENOIOCTLCMD for any unrecognized ioctl |
| 441 | */ |
| 442 | static int pppoatm_ioctl(struct socket *sock, unsigned int cmd, |
| 443 | unsigned long arg) |
| 444 | { |
| 445 | struct atm_vcc *atmvcc = ATM_SD(sock); |
| 446 | void __user *argp = (void __user *)arg; |
| 447 | |
| 448 | if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push) |
| 449 | return -ENOIOCTLCMD; |
| 450 | switch (cmd) { |
| 451 | case ATM_SETBACKEND: { |
| 452 | atm_backend_t b; |
| 453 | if (get_user(b, (atm_backend_t __user *) argp)) |
| 454 | return -EFAULT; |
| 455 | if (b != ATM_BACKEND_PPP) |
| 456 | return -ENOIOCTLCMD; |
| 457 | if (!capable(CAP_NET_ADMIN)) |
| 458 | return -EPERM; |
Krzysztof Mazur | 3b1a914 | 2012-11-06 23:16:58 +0100 | [diff] [blame] | 459 | if (sock->state != SS_CONNECTED) |
| 460 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return pppoatm_assign_vcc(atmvcc, argp); |
| 462 | } |
| 463 | case PPPIOCGCHAN: |
| 464 | return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)-> |
| 465 | chan), (int __user *) argp) ? -EFAULT : 0; |
| 466 | case PPPIOCGUNIT: |
| 467 | return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)-> |
| 468 | chan), (int __user *) argp) ? -EFAULT : 0; |
| 469 | } |
| 470 | return -ENOIOCTLCMD; |
| 471 | } |
| 472 | |
| 473 | static struct atm_ioctl pppoatm_ioctl_ops = { |
| 474 | .owner = THIS_MODULE, |
| 475 | .ioctl = pppoatm_ioctl, |
| 476 | }; |
| 477 | |
| 478 | static int __init pppoatm_init(void) |
| 479 | { |
| 480 | register_atm_ioctl(&pppoatm_ioctl_ops); |
| 481 | return 0; |
| 482 | } |
| 483 | |
| 484 | static void __exit pppoatm_exit(void) |
| 485 | { |
| 486 | deregister_atm_ioctl(&pppoatm_ioctl_ops); |
| 487 | } |
| 488 | |
| 489 | module_init(pppoatm_init); |
| 490 | module_exit(pppoatm_exit); |
| 491 | |
| 492 | MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>"); |
| 493 | MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5"); |
| 494 | MODULE_LICENSE("GPL"); |