// SPDX-License-Identifier: GPL-2.0-or-later1/* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */23/* 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 */78/*9*10* This driver provides the encapsulation and framing for sending11* and receiving PPP frames in ATM AAL5 PDUs.12*/1314/*15* One shortcoming of this driver is that it does not comply with16* section 8 of RFC2364 - we are supposed to detect a change17* in encapsulation and immediately abort the connection (in order18* to avoid a black-hole being created if our peer loses state19* and changes encapsulation unilaterally. However, since the20* ppp_generic layer actually does the decapsulation, we need21* a way of notifying it when we _think_ there might be a problem)22* There's two cases:23* 1. LLC-encapsulation was missing when it was enabled. In24* this case, we should tell the upper layer "tear down25* this session if this skb looks ok to you"26* 2. LLC-encapsulation was present when it was disabled. Then27* we need to tell the upper layer "this packet may be28* ok, but if its in error tear down the session"29* These hooks are not yet available in ppp_generic30*/3132#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__3334#include <linux/module.h>35#include <linux/init.h>36#include <linux/interrupt.h>37#include <linux/skbuff.h>38#include <linux/slab.h>39#include <linux/atm.h>40#include <linux/atmdev.h>41#include <linux/capability.h>42#include <linux/ppp_defs.h>43#include <linux/ppp-ioctl.h>44#include <linux/ppp_channel.h>45#include <linux/atmppp.h>4647#include "common.h"4849enum pppoatm_encaps {50e_autodetect = PPPOATM_ENCAPS_AUTODETECT,51e_vc = PPPOATM_ENCAPS_VC,52e_llc = PPPOATM_ENCAPS_LLC,53};5455struct pppoatm_vcc {56struct atm_vcc *atmvcc; /* VCC descriptor */57void (*old_push)(struct atm_vcc *, struct sk_buff *);58void (*old_pop)(struct atm_vcc *, struct sk_buff *);59void (*old_release_cb)(struct atm_vcc *);60struct module *old_owner;61/* keep old push/pop for detaching */62enum pppoatm_encaps encaps;63atomic_t inflight;64unsigned long blocked;65int flags; /* SC_COMP_PROT - compress protocol */66struct ppp_channel chan; /* interface to generic ppp layer */67struct tasklet_struct wakeup_tasklet;68};6970/*71* We want to allow two packets in the queue. The one that's currently in72* flight, and *one* queued up ready for the ATM device to send immediately73* from its TX done IRQ. We want to be able to use atomic_inc_not_zero(), so74* inflight == -2 represents an empty queue, -1 one packet, and zero means75* there are two packets in the queue.76*/77#define NONE_INFLIGHT -27879#define BLOCKED 08081/*82* Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol83* ID (0xC021) used in autodetection84*/85static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };86#define LLC_LEN (4)8788static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)89{90return (struct pppoatm_vcc *) (atmvcc->user_back);91}9293static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)94{95return (struct pppoatm_vcc *) (chan->private);96}9798/*99* We can't do this directly from our _pop handler, since the ppp code100* doesn't want to be called in interrupt context, so we do it from101* a tasklet102*/103static void pppoatm_wakeup_sender(struct tasklet_struct *t)104{105struct pppoatm_vcc *pvcc = from_tasklet(pvcc, t, wakeup_tasklet);106107ppp_output_wakeup(&pvcc->chan);108}109110static void pppoatm_release_cb(struct atm_vcc *atmvcc)111{112struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);113114/*115* As in pppoatm_pop(), it's safe to clear the BLOCKED bit here because116* the wakeup *can't* race with pppoatm_send(). They both hold the PPP117* channel's ->downl lock. And the potential race with *setting* it,118* which leads to the double-check dance in pppoatm_may_send(), doesn't119* exist here. In the sock_owned_by_user() case in pppoatm_send(), we120* set the BLOCKED bit while the socket is still locked. We know that121* ->release_cb() can't be called until that's done.122*/123if (test_and_clear_bit(BLOCKED, &pvcc->blocked))124tasklet_schedule(&pvcc->wakeup_tasklet);125if (pvcc->old_release_cb)126pvcc->old_release_cb(atmvcc);127}128/*129* This gets called every time the ATM card has finished sending our130* skb. The ->old_pop will take care up normal atm flow control,131* but we also need to wake up the device if we blocked it132*/133static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)134{135struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);136137pvcc->old_pop(atmvcc, skb);138atomic_dec(&pvcc->inflight);139140/*141* We always used to run the wakeup tasklet unconditionally here, for142* fear of race conditions where we clear the BLOCKED flag just as we143* refuse another packet in pppoatm_send(). This was quite inefficient.144*145* In fact it's OK. The PPP core will only ever call pppoatm_send()146* while holding the channel->downl lock. And ppp_output_wakeup() as147* called by the tasklet will *also* grab that lock. So even if another148* CPU is in pppoatm_send() right now, the tasklet isn't going to race149* with it. The wakeup *will* happen after the other CPU is safely out150* of pppoatm_send() again.151*152* So if the CPU in pppoatm_send() has already set the BLOCKED bit and153* it about to return, that's fine. We trigger a wakeup which will154* happen later. And if the CPU in pppoatm_send() *hasn't* set the155* BLOCKED bit yet, that's fine too because of the double check in156* pppoatm_may_send() which is commented there.157*/158if (test_and_clear_bit(BLOCKED, &pvcc->blocked))159tasklet_schedule(&pvcc->wakeup_tasklet);160}161162/*163* Unbind from PPP - currently we only do this when closing the socket,164* but we could put this into an ioctl if need be165*/166static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)167{168struct pppoatm_vcc *pvcc;169pvcc = atmvcc_to_pvcc(atmvcc);170atmvcc->push = pvcc->old_push;171atmvcc->pop = pvcc->old_pop;172atmvcc->release_cb = pvcc->old_release_cb;173tasklet_kill(&pvcc->wakeup_tasklet);174ppp_unregister_channel(&pvcc->chan);175atmvcc->user_back = NULL;176kfree(pvcc);177}178179/* Called when an AAL5 PDU comes in */180static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)181{182struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);183pr_debug("\n");184if (skb == NULL) { /* VCC was closed */185struct module *module;186187pr_debug("removing ATMPPP VCC %p\n", pvcc);188module = pvcc->old_owner;189pppoatm_unassign_vcc(atmvcc);190atmvcc->push(atmvcc, NULL); /* Pass along bad news */191module_put(module);192return;193}194atm_return(atmvcc, skb->truesize);195switch (pvcc->encaps) {196case e_llc:197if (skb->len < LLC_LEN ||198memcmp(skb->data, pppllc, LLC_LEN))199goto error;200skb_pull(skb, LLC_LEN);201break;202case e_autodetect:203if (pvcc->chan.ppp == NULL) { /* Not bound yet! */204kfree_skb(skb);205return;206}207if (skb->len >= sizeof(pppllc) &&208!memcmp(skb->data, pppllc, sizeof(pppllc))) {209pvcc->encaps = e_llc;210skb_pull(skb, LLC_LEN);211break;212}213if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&214!memcmp(skb->data, &pppllc[LLC_LEN],215sizeof(pppllc) - LLC_LEN)) {216pvcc->encaps = e_vc;217pvcc->chan.mtu += LLC_LEN;218break;219}220pr_debug("Couldn't autodetect yet (skb: %6ph)\n", skb->data);221goto error;222case e_vc:223break;224}225ppp_input(&pvcc->chan, skb);226return;227228error:229kfree_skb(skb);230ppp_input_error(&pvcc->chan, 0);231}232233static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)234{235/*236* It's not clear that we need to bother with using atm_may_send()237* to check we don't exceed sk->sk_sndbuf. If userspace sets a238* value of sk_sndbuf which is lower than the MTU, we're going to239* block for ever. But the code always did that before we introduced240* the packet count limit, so...241*/242if (atm_may_send(pvcc->atmvcc, size) &&243atomic_inc_not_zero(&pvcc->inflight))244return 1;245246/*247* We use test_and_set_bit() rather than set_bit() here because248* we need to ensure there's a memory barrier after it. The bit249* *must* be set before we do the atomic_inc() on pvcc->inflight.250* There's no smp_mb__after_set_bit(), so it's this or abuse251* smp_mb__after_atomic().252*/253test_and_set_bit(BLOCKED, &pvcc->blocked);254255/*256* We may have raced with pppoatm_pop(). If it ran for the257* last packet in the queue, *just* before we set the BLOCKED258* bit, then it might never run again and the channel could259* remain permanently blocked. Cope with that race by checking260* *again*. If it did run in that window, we'll have space on261* the queue now and can return success. It's harmless to leave262* the BLOCKED flag set, since it's only used as a trigger to263* run the wakeup tasklet. Another wakeup will never hurt.264* If pppoatm_pop() is running but hasn't got as far as making265* space on the queue yet, then it hasn't checked the BLOCKED266* flag yet either, so we're safe in that case too. It'll issue267* an "immediate" wakeup... where "immediate" actually involves268* taking the PPP channel's ->downl lock, which is held by the269* code path that calls pppoatm_send(), and is thus going to270* wait for us to finish.271*/272if (atm_may_send(pvcc->atmvcc, size) &&273atomic_inc_not_zero(&pvcc->inflight))274return 1;275276return 0;277}278/*279* Called by the ppp_generic.c to send a packet - returns true if packet280* was accepted. If we return false, then it's our job to call281* ppp_output_wakeup(chan) when we're feeling more up to it.282* Note that in the ENOMEM case (as opposed to the !atm_may_send case)283* we should really drop the packet, but the generic layer doesn't284* support this yet. We just return 'DROP_PACKET' which we actually define285* as success, just to be clear what we're really doing.286*/287#define DROP_PACKET 1288static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)289{290struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);291struct atm_vcc *vcc;292int ret;293294ATM_SKB(skb)->vcc = pvcc->atmvcc;295pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);296if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))297(void) skb_pull(skb, 1);298299vcc = ATM_SKB(skb)->vcc;300bh_lock_sock(sk_atm(vcc));301if (sock_owned_by_user(sk_atm(vcc))) {302/*303* Needs to happen (and be flushed, hence test_and_) before we unlock304* the socket. It needs to be seen by the time our ->release_cb gets305* called.306*/307test_and_set_bit(BLOCKED, &pvcc->blocked);308goto nospace;309}310if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||311test_bit(ATM_VF_CLOSE, &vcc->flags) ||312!test_bit(ATM_VF_READY, &vcc->flags)) {313bh_unlock_sock(sk_atm(vcc));314kfree_skb(skb);315return DROP_PACKET;316}317318switch (pvcc->encaps) { /* LLC encapsulation needed */319case e_llc:320if (skb_headroom(skb) < LLC_LEN) {321struct sk_buff *n;322n = skb_realloc_headroom(skb, LLC_LEN);323if (n != NULL &&324!pppoatm_may_send(pvcc, n->truesize)) {325kfree_skb(n);326goto nospace;327}328consume_skb(skb);329skb = n;330if (skb == NULL) {331bh_unlock_sock(sk_atm(vcc));332return DROP_PACKET;333}334} else if (!pppoatm_may_send(pvcc, skb->truesize))335goto nospace;336memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);337break;338case e_vc:339if (!pppoatm_may_send(pvcc, skb->truesize))340goto nospace;341break;342case e_autodetect:343bh_unlock_sock(sk_atm(vcc));344pr_debug("Trying to send without setting encaps!\n");345kfree_skb(skb);346return 1;347}348349atm_account_tx(vcc, skb);350pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",351skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);352ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)353? DROP_PACKET : 1;354bh_unlock_sock(sk_atm(vcc));355return ret;356nospace:357bh_unlock_sock(sk_atm(vcc));358/*359* We don't have space to send this SKB now, but we might have360* already applied SC_COMP_PROT compression, so may need to undo361*/362if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&363skb->data[-1] == '\0')364(void) skb_push(skb, 1);365return 0;366}367368/* This handles ioctls sent to the /dev/ppp interface */369static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,370unsigned long arg)371{372switch (cmd) {373case PPPIOCGFLAGS:374return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)375? -EFAULT : 0;376case PPPIOCSFLAGS:377return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)378? -EFAULT : 0;379}380return -ENOTTY;381}382383static const struct ppp_channel_ops pppoatm_ops = {384.start_xmit = pppoatm_send,385.ioctl = pppoatm_devppp_ioctl,386};387388static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)389{390struct atm_backend_ppp be;391struct pppoatm_vcc *pvcc;392int err;393394if (copy_from_user(&be, arg, sizeof be))395return -EFAULT;396if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&397be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)398return -EINVAL;399pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);400if (pvcc == NULL)401return -ENOMEM;402pvcc->atmvcc = atmvcc;403404/* Maximum is zero, so that we can use atomic_inc_not_zero() */405atomic_set(&pvcc->inflight, NONE_INFLIGHT);406pvcc->old_push = atmvcc->push;407pvcc->old_pop = atmvcc->pop;408pvcc->old_owner = atmvcc->owner;409pvcc->old_release_cb = atmvcc->release_cb;410pvcc->encaps = (enum pppoatm_encaps) be.encaps;411pvcc->chan.private = pvcc;412pvcc->chan.ops = &pppoatm_ops;413pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -414(be.encaps == e_vc ? 0 : LLC_LEN);415tasklet_setup(&pvcc->wakeup_tasklet, pppoatm_wakeup_sender);416err = ppp_register_channel(&pvcc->chan);417if (err != 0) {418kfree(pvcc);419return err;420}421atmvcc->user_back = pvcc;422atmvcc->push = pppoatm_push;423atmvcc->pop = pppoatm_pop;424atmvcc->release_cb = pppoatm_release_cb;425__module_get(THIS_MODULE);426atmvcc->owner = THIS_MODULE;427428/* re-process everything received between connection setup and429backend setup */430vcc_process_recv_queue(atmvcc);431return 0;432}433434/*435* This handles ioctls actually performed on our vcc - we must return436* -ENOIOCTLCMD for any unrecognized ioctl437*/438static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,439unsigned long arg)440{441struct atm_vcc *atmvcc = ATM_SD(sock);442void __user *argp = (void __user *)arg;443444if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)445return -ENOIOCTLCMD;446switch (cmd) {447case ATM_SETBACKEND: {448atm_backend_t b;449if (get_user(b, (atm_backend_t __user *) argp))450return -EFAULT;451if (b != ATM_BACKEND_PPP)452return -ENOIOCTLCMD;453if (!capable(CAP_NET_ADMIN))454return -EPERM;455if (sock->state != SS_CONNECTED)456return -EINVAL;457return pppoatm_assign_vcc(atmvcc, argp);458}459case PPPIOCGCHAN:460return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->461chan), (int __user *) argp) ? -EFAULT : 0;462case PPPIOCGUNIT:463return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->464chan), (int __user *) argp) ? -EFAULT : 0;465}466return -ENOIOCTLCMD;467}468469static struct atm_ioctl pppoatm_ioctl_ops = {470.owner = THIS_MODULE,471.ioctl = pppoatm_ioctl,472};473474static int __init pppoatm_init(void)475{476register_atm_ioctl(&pppoatm_ioctl_ops);477return 0;478}479480static void __exit pppoatm_exit(void)481{482deregister_atm_ioctl(&pppoatm_ioctl_ops);483}484485module_init(pppoatm_init);486module_exit(pppoatm_exit);487488MODULE_AUTHOR("Mitchell Blank Jr <[email protected]>");489MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");490MODULE_LICENSE("GPL");491492493