Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */
0003 
0004 /* Copyright 1999-2000 by Mitchell Blank Jr */
0005 /* Based on clip.c; 1995-1999 by Werner Almesberger, EPFL LRC/ICA */
0006 /* And on ppp_async.c; Copyright 1999 Paul Mackerras */
0007 /* And help from Jens Axboe */
0008 
0009 /*
0010  *
0011  * This driver provides the encapsulation and framing for sending
0012  * and receiving PPP frames in ATM AAL5 PDUs.
0013  */
0014 
0015 /*
0016  * One shortcoming of this driver is that it does not comply with
0017  * section 8 of RFC2364 - we are supposed to detect a change
0018  * in encapsulation and immediately abort the connection (in order
0019  * to avoid a black-hole being created if our peer loses state
0020  * and changes encapsulation unilaterally.  However, since the
0021  * ppp_generic layer actually does the decapsulation, we need
0022  * a way of notifying it when we _think_ there might be a problem)
0023  * There's two cases:
0024  *   1. LLC-encapsulation was missing when it was enabled.  In
0025  *  this case, we should tell the upper layer "tear down
0026  *  this session if this skb looks ok to you"
0027  *   2. LLC-encapsulation was present when it was disabled.  Then
0028  *  we need to tell the upper layer "this packet may be
0029  *  ok, but if its in error tear down the session"
0030  * These hooks are not yet available in ppp_generic
0031  */
0032 
0033 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
0034 
0035 #include <linux/module.h>
0036 #include <linux/init.h>
0037 #include <linux/interrupt.h>
0038 #include <linux/skbuff.h>
0039 #include <linux/slab.h>
0040 #include <linux/atm.h>
0041 #include <linux/atmdev.h>
0042 #include <linux/capability.h>
0043 #include <linux/ppp_defs.h>
0044 #include <linux/ppp-ioctl.h>
0045 #include <linux/ppp_channel.h>
0046 #include <linux/atmppp.h>
0047 
0048 #include "common.h"
0049 
0050 enum pppoatm_encaps {
0051     e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
0052     e_vc = PPPOATM_ENCAPS_VC,
0053     e_llc = PPPOATM_ENCAPS_LLC,
0054 };
0055 
0056 struct pppoatm_vcc {
0057     struct atm_vcc  *atmvcc;    /* VCC descriptor */
0058     void (*old_push)(struct atm_vcc *, struct sk_buff *);
0059     void (*old_pop)(struct atm_vcc *, struct sk_buff *);
0060     void (*old_release_cb)(struct atm_vcc *);
0061     struct module *old_owner;
0062                     /* keep old push/pop for detaching */
0063     enum pppoatm_encaps encaps;
0064     atomic_t inflight;
0065     unsigned long blocked;
0066     int flags;          /* SC_COMP_PROT - compress protocol */
0067     struct ppp_channel chan;    /* interface to generic ppp layer */
0068     struct tasklet_struct wakeup_tasklet;
0069 };
0070 
0071 /*
0072  * We want to allow two packets in the queue. The one that's currently in
0073  * flight, and *one* queued up ready for the ATM device to send immediately
0074  * from its TX done IRQ. We want to be able to use atomic_inc_not_zero(), so
0075  * inflight == -2 represents an empty queue, -1 one packet, and zero means
0076  * there are two packets in the queue.
0077  */
0078 #define NONE_INFLIGHT -2
0079 
0080 #define BLOCKED 0
0081 
0082 /*
0083  * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
0084  * ID (0xC021) used in autodetection
0085  */
0086 static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
0087 #define LLC_LEN     (4)
0088 
0089 static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
0090 {
0091     return (struct pppoatm_vcc *) (atmvcc->user_back);
0092 }
0093 
0094 static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
0095 {
0096     return (struct pppoatm_vcc *) (chan->private);
0097 }
0098 
0099 /*
0100  * We can't do this directly from our _pop handler, since the ppp code
0101  * doesn't want to be called in interrupt context, so we do it from
0102  * a tasklet
0103  */
0104 static void pppoatm_wakeup_sender(struct tasklet_struct *t)
0105 {
0106     struct pppoatm_vcc *pvcc = from_tasklet(pvcc, t, wakeup_tasklet);
0107 
0108     ppp_output_wakeup(&pvcc->chan);
0109 }
0110 
0111 static void pppoatm_release_cb(struct atm_vcc *atmvcc)
0112 {
0113     struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
0114 
0115     /*
0116      * As in pppoatm_pop(), it's safe to clear the BLOCKED bit here because
0117      * the wakeup *can't* race with pppoatm_send(). They both hold the PPP
0118      * channel's ->downl lock. And the potential race with *setting* it,
0119      * which leads to the double-check dance in pppoatm_may_send(), doesn't
0120      * exist here. In the sock_owned_by_user() case in pppoatm_send(), we
0121      * set the BLOCKED bit while the socket is still locked. We know that
0122      * ->release_cb() can't be called until that's done.
0123      */
0124     if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
0125         tasklet_schedule(&pvcc->wakeup_tasklet);
0126     if (pvcc->old_release_cb)
0127         pvcc->old_release_cb(atmvcc);
0128 }
0129 /*
0130  * This gets called every time the ATM card has finished sending our
0131  * skb.  The ->old_pop will take care up normal atm flow control,
0132  * but we also need to wake up the device if we blocked it
0133  */
0134 static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
0135 {
0136     struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
0137 
0138     pvcc->old_pop(atmvcc, skb);
0139     atomic_dec(&pvcc->inflight);
0140 
0141     /*
0142      * We always used to run the wakeup tasklet unconditionally here, for
0143      * fear of race conditions where we clear the BLOCKED flag just as we
0144      * refuse another packet in pppoatm_send(). This was quite inefficient.
0145      *
0146      * In fact it's OK. The PPP core will only ever call pppoatm_send()
0147      * while holding the channel->downl lock. And ppp_output_wakeup() as
0148      * called by the tasklet will *also* grab that lock. So even if another
0149      * CPU is in pppoatm_send() right now, the tasklet isn't going to race
0150      * with it. The wakeup *will* happen after the other CPU is safely out
0151      * of pppoatm_send() again.
0152      *
0153      * So if the CPU in pppoatm_send() has already set the BLOCKED bit and
0154      * it about to return, that's fine. We trigger a wakeup which will
0155      * happen later. And if the CPU in pppoatm_send() *hasn't* set the
0156      * BLOCKED bit yet, that's fine too because of the double check in
0157      * pppoatm_may_send() which is commented there.
0158      */
0159     if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
0160         tasklet_schedule(&pvcc->wakeup_tasklet);
0161 }
0162 
0163 /*
0164  * Unbind from PPP - currently we only do this when closing the socket,
0165  * but we could put this into an ioctl if need be
0166  */
0167 static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
0168 {
0169     struct pppoatm_vcc *pvcc;
0170     pvcc = atmvcc_to_pvcc(atmvcc);
0171     atmvcc->push = pvcc->old_push;
0172     atmvcc->pop = pvcc->old_pop;
0173     atmvcc->release_cb = pvcc->old_release_cb;
0174     tasklet_kill(&pvcc->wakeup_tasklet);
0175     ppp_unregister_channel(&pvcc->chan);
0176     atmvcc->user_back = NULL;
0177     kfree(pvcc);
0178 }
0179 
0180 /* Called when an AAL5 PDU comes in */
0181 static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
0182 {
0183     struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
0184     pr_debug("\n");
0185     if (skb == NULL) {          /* VCC was closed */
0186         struct module *module;
0187 
0188         pr_debug("removing ATMPPP VCC %p\n", pvcc);
0189         module = pvcc->old_owner;
0190         pppoatm_unassign_vcc(atmvcc);
0191         atmvcc->push(atmvcc, NULL); /* Pass along bad news */
0192         module_put(module);
0193         return;
0194     }
0195     atm_return(atmvcc, skb->truesize);
0196     switch (pvcc->encaps) {
0197     case e_llc:
0198         if (skb->len < LLC_LEN ||
0199             memcmp(skb->data, pppllc, LLC_LEN))
0200             goto error;
0201         skb_pull(skb, LLC_LEN);
0202         break;
0203     case e_autodetect:
0204         if (pvcc->chan.ppp == NULL) {   /* Not bound yet! */
0205             kfree_skb(skb);
0206             return;
0207         }
0208         if (skb->len >= sizeof(pppllc) &&
0209             !memcmp(skb->data, pppllc, sizeof(pppllc))) {
0210             pvcc->encaps = e_llc;
0211             skb_pull(skb, LLC_LEN);
0212             break;
0213         }
0214         if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
0215             !memcmp(skb->data, &pppllc[LLC_LEN],
0216             sizeof(pppllc) - LLC_LEN)) {
0217             pvcc->encaps = e_vc;
0218             pvcc->chan.mtu += LLC_LEN;
0219             break;
0220         }
0221         pr_debug("Couldn't autodetect yet (skb: %6ph)\n", skb->data);
0222         goto error;
0223     case e_vc:
0224         break;
0225     }
0226     ppp_input(&pvcc->chan, skb);
0227     return;
0228 
0229 error:
0230     kfree_skb(skb);
0231     ppp_input_error(&pvcc->chan, 0);
0232 }
0233 
0234 static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
0235 {
0236     /*
0237      * It's not clear that we need to bother with using atm_may_send()
0238      * to check we don't exceed sk->sk_sndbuf. If userspace sets a
0239      * value of sk_sndbuf which is lower than the MTU, we're going to
0240      * block for ever. But the code always did that before we introduced
0241      * the packet count limit, so...
0242      */
0243     if (atm_may_send(pvcc->atmvcc, size) &&
0244         atomic_inc_not_zero(&pvcc->inflight))
0245         return 1;
0246 
0247     /*
0248      * We use test_and_set_bit() rather than set_bit() here because
0249      * we need to ensure there's a memory barrier after it. The bit
0250      * *must* be set before we do the atomic_inc() on pvcc->inflight.
0251      * There's no smp_mb__after_set_bit(), so it's this or abuse
0252      * smp_mb__after_atomic().
0253      */
0254     test_and_set_bit(BLOCKED, &pvcc->blocked);
0255 
0256     /*
0257      * We may have raced with pppoatm_pop(). If it ran for the
0258      * last packet in the queue, *just* before we set the BLOCKED
0259      * bit, then it might never run again and the channel could
0260      * remain permanently blocked. Cope with that race by checking
0261      * *again*. If it did run in that window, we'll have space on
0262      * the queue now and can return success. It's harmless to leave
0263      * the BLOCKED flag set, since it's only used as a trigger to
0264      * run the wakeup tasklet. Another wakeup will never hurt.
0265      * If pppoatm_pop() is running but hasn't got as far as making
0266      * space on the queue yet, then it hasn't checked the BLOCKED
0267      * flag yet either, so we're safe in that case too. It'll issue
0268      * an "immediate" wakeup... where "immediate" actually involves
0269      * taking the PPP channel's ->downl lock, which is held by the
0270      * code path that calls pppoatm_send(), and is thus going to
0271      * wait for us to finish.
0272      */
0273     if (atm_may_send(pvcc->atmvcc, size) &&
0274         atomic_inc_not_zero(&pvcc->inflight))
0275         return 1;
0276 
0277     return 0;
0278 }
0279 /*
0280  * Called by the ppp_generic.c to send a packet - returns true if packet
0281  * was accepted.  If we return false, then it's our job to call
0282  * ppp_output_wakeup(chan) when we're feeling more up to it.
0283  * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
0284  * we should really drop the packet, but the generic layer doesn't
0285  * support this yet.  We just return 'DROP_PACKET' which we actually define
0286  * as success, just to be clear what we're really doing.
0287  */
0288 #define DROP_PACKET 1
0289 static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
0290 {
0291     struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
0292     struct atm_vcc *vcc;
0293     int ret;
0294 
0295     ATM_SKB(skb)->vcc = pvcc->atmvcc;
0296     pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
0297     if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
0298         (void) skb_pull(skb, 1);
0299 
0300     vcc = ATM_SKB(skb)->vcc;
0301     bh_lock_sock(sk_atm(vcc));
0302     if (sock_owned_by_user(sk_atm(vcc))) {
0303         /*
0304          * Needs to happen (and be flushed, hence test_and_) before we unlock
0305          * the socket. It needs to be seen by the time our ->release_cb gets
0306          * called.
0307          */
0308         test_and_set_bit(BLOCKED, &pvcc->blocked);
0309         goto nospace;
0310     }
0311     if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
0312         test_bit(ATM_VF_CLOSE, &vcc->flags) ||
0313         !test_bit(ATM_VF_READY, &vcc->flags)) {
0314         bh_unlock_sock(sk_atm(vcc));
0315         kfree_skb(skb);
0316         return DROP_PACKET;
0317     }
0318 
0319     switch (pvcc->encaps) {     /* LLC encapsulation needed */
0320     case e_llc:
0321         if (skb_headroom(skb) < LLC_LEN) {
0322             struct sk_buff *n;
0323             n = skb_realloc_headroom(skb, LLC_LEN);
0324             if (n != NULL &&
0325                 !pppoatm_may_send(pvcc, n->truesize)) {
0326                 kfree_skb(n);
0327                 goto nospace;
0328             }
0329             consume_skb(skb);
0330             skb = n;
0331             if (skb == NULL) {
0332                 bh_unlock_sock(sk_atm(vcc));
0333                 return DROP_PACKET;
0334             }
0335         } else if (!pppoatm_may_send(pvcc, skb->truesize))
0336             goto nospace;
0337         memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
0338         break;
0339     case e_vc:
0340         if (!pppoatm_may_send(pvcc, skb->truesize))
0341             goto nospace;
0342         break;
0343     case e_autodetect:
0344         bh_unlock_sock(sk_atm(vcc));
0345         pr_debug("Trying to send without setting encaps!\n");
0346         kfree_skb(skb);
0347         return 1;
0348     }
0349 
0350     atm_account_tx(vcc, skb);
0351     pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
0352          skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
0353     ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
0354         ? DROP_PACKET : 1;
0355     bh_unlock_sock(sk_atm(vcc));
0356     return ret;
0357 nospace:
0358     bh_unlock_sock(sk_atm(vcc));
0359     /*
0360      * We don't have space to send this SKB now, but we might have
0361      * already applied SC_COMP_PROT compression, so may need to undo
0362      */
0363     if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
0364         skb->data[-1] == '\0')
0365         (void) skb_push(skb, 1);
0366     return 0;
0367 }
0368 
0369 /* This handles ioctls sent to the /dev/ppp interface */
0370 static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
0371     unsigned long arg)
0372 {
0373     switch (cmd) {
0374     case PPPIOCGFLAGS:
0375         return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
0376             ? -EFAULT : 0;
0377     case PPPIOCSFLAGS:
0378         return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
0379             ? -EFAULT : 0;
0380     }
0381     return -ENOTTY;
0382 }
0383 
0384 static const struct ppp_channel_ops pppoatm_ops = {
0385     .start_xmit = pppoatm_send,
0386     .ioctl = pppoatm_devppp_ioctl,
0387 };
0388 
0389 static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
0390 {
0391     struct atm_backend_ppp be;
0392     struct pppoatm_vcc *pvcc;
0393     int err;
0394 
0395     if (copy_from_user(&be, arg, sizeof be))
0396         return -EFAULT;
0397     if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
0398         be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
0399         return -EINVAL;
0400     pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
0401     if (pvcc == NULL)
0402         return -ENOMEM;
0403     pvcc->atmvcc = atmvcc;
0404 
0405     /* Maximum is zero, so that we can use atomic_inc_not_zero() */
0406     atomic_set(&pvcc->inflight, NONE_INFLIGHT);
0407     pvcc->old_push = atmvcc->push;
0408     pvcc->old_pop = atmvcc->pop;
0409     pvcc->old_owner = atmvcc->owner;
0410     pvcc->old_release_cb = atmvcc->release_cb;
0411     pvcc->encaps = (enum pppoatm_encaps) be.encaps;
0412     pvcc->chan.private = pvcc;
0413     pvcc->chan.ops = &pppoatm_ops;
0414     pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
0415         (be.encaps == e_vc ? 0 : LLC_LEN);
0416     tasklet_setup(&pvcc->wakeup_tasklet, pppoatm_wakeup_sender);
0417     err = ppp_register_channel(&pvcc->chan);
0418     if (err != 0) {
0419         kfree(pvcc);
0420         return err;
0421     }
0422     atmvcc->user_back = pvcc;
0423     atmvcc->push = pppoatm_push;
0424     atmvcc->pop = pppoatm_pop;
0425     atmvcc->release_cb = pppoatm_release_cb;
0426     __module_get(THIS_MODULE);
0427     atmvcc->owner = THIS_MODULE;
0428 
0429     /* re-process everything received between connection setup and
0430        backend setup */
0431     vcc_process_recv_queue(atmvcc);
0432     return 0;
0433 }
0434 
0435 /*
0436  * This handles ioctls actually performed on our vcc - we must return
0437  * -ENOIOCTLCMD for any unrecognized ioctl
0438  */
0439 static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
0440     unsigned long arg)
0441 {
0442     struct atm_vcc *atmvcc = ATM_SD(sock);
0443     void __user *argp = (void __user *)arg;
0444 
0445     if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
0446         return -ENOIOCTLCMD;
0447     switch (cmd) {
0448     case ATM_SETBACKEND: {
0449         atm_backend_t b;
0450         if (get_user(b, (atm_backend_t __user *) argp))
0451             return -EFAULT;
0452         if (b != ATM_BACKEND_PPP)
0453             return -ENOIOCTLCMD;
0454         if (!capable(CAP_NET_ADMIN))
0455             return -EPERM;
0456         if (sock->state != SS_CONNECTED)
0457             return -EINVAL;
0458         return pppoatm_assign_vcc(atmvcc, argp);
0459         }
0460     case PPPIOCGCHAN:
0461         return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
0462             chan), (int __user *) argp) ? -EFAULT : 0;
0463     case PPPIOCGUNIT:
0464         return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
0465             chan), (int __user *) argp) ? -EFAULT : 0;
0466     }
0467     return -ENOIOCTLCMD;
0468 }
0469 
0470 static struct atm_ioctl pppoatm_ioctl_ops = {
0471     .owner  = THIS_MODULE,
0472     .ioctl  = pppoatm_ioctl,
0473 };
0474 
0475 static int __init pppoatm_init(void)
0476 {
0477     register_atm_ioctl(&pppoatm_ioctl_ops);
0478     return 0;
0479 }
0480 
0481 static void __exit pppoatm_exit(void)
0482 {
0483     deregister_atm_ioctl(&pppoatm_ioctl_ops);
0484 }
0485 
0486 module_init(pppoatm_init);
0487 module_exit(pppoatm_exit);
0488 
0489 MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
0490 MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
0491 MODULE_LICENSE("GPL");