12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */
31da177e4SLinus Torvalds
41da177e4SLinus Torvalds /* Copyright 1999-2000 by Mitchell Blank Jr */
51da177e4SLinus Torvalds /* Based on clip.c; 1995-1999 by Werner Almesberger, EPFL LRC/ICA */
61da177e4SLinus Torvalds /* And on ppp_async.c; Copyright 1999 Paul Mackerras */
71da177e4SLinus Torvalds /* And help from Jens Axboe */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * This driver provides the encapsulation and framing for sending
121da177e4SLinus Torvalds * and receiving PPP frames in ATM AAL5 PDUs.
131da177e4SLinus Torvalds */
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds * One shortcoming of this driver is that it does not comply with
171da177e4SLinus Torvalds * section 8 of RFC2364 - we are supposed to detect a change
181da177e4SLinus Torvalds * in encapsulation and immediately abort the connection (in order
191da177e4SLinus Torvalds * to avoid a black-hole being created if our peer loses state
201da177e4SLinus Torvalds * and changes encapsulation unilaterally. However, since the
211da177e4SLinus Torvalds * ppp_generic layer actually does the decapsulation, we need
221da177e4SLinus Torvalds * a way of notifying it when we _think_ there might be a problem)
231da177e4SLinus Torvalds * There's two cases:
241da177e4SLinus Torvalds * 1. LLC-encapsulation was missing when it was enabled. In
251da177e4SLinus Torvalds * this case, we should tell the upper layer "tear down
261da177e4SLinus Torvalds * this session if this skb looks ok to you"
271da177e4SLinus Torvalds * 2. LLC-encapsulation was present when it was disabled. Then
281da177e4SLinus Torvalds * we need to tell the upper layer "this packet may be
291da177e4SLinus Torvalds * ok, but if its in error tear down the session"
301da177e4SLinus Torvalds * These hooks are not yet available in ppp_generic
311da177e4SLinus Torvalds */
321da177e4SLinus Torvalds
3399824461SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
3499824461SJoe Perches
351da177e4SLinus Torvalds #include <linux/module.h>
361da177e4SLinus Torvalds #include <linux/init.h>
37a6b7a407SAlexey Dobriyan #include <linux/interrupt.h>
381da177e4SLinus Torvalds #include <linux/skbuff.h>
395a0e3ad6STejun Heo #include <linux/slab.h>
401da177e4SLinus Torvalds #include <linux/atm.h>
411da177e4SLinus Torvalds #include <linux/atmdev.h>
424fc268d2SRandy Dunlap #include <linux/capability.h>
431da177e4SLinus Torvalds #include <linux/ppp_defs.h>
444b32da2bSPaul Mackerras #include <linux/ppp-ioctl.h>
451da177e4SLinus Torvalds #include <linux/ppp_channel.h>
461da177e4SLinus Torvalds #include <linux/atmppp.h>
471da177e4SLinus Torvalds
481da177e4SLinus Torvalds #include "common.h"
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds enum pppoatm_encaps {
511da177e4SLinus Torvalds e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
521da177e4SLinus Torvalds e_vc = PPPOATM_ENCAPS_VC,
531da177e4SLinus Torvalds e_llc = PPPOATM_ENCAPS_LLC,
541da177e4SLinus Torvalds };
551da177e4SLinus Torvalds
561da177e4SLinus Torvalds struct pppoatm_vcc {
571da177e4SLinus Torvalds struct atm_vcc *atmvcc; /* VCC descriptor */
581da177e4SLinus Torvalds void (*old_push)(struct atm_vcc *, struct sk_buff *);
591da177e4SLinus Torvalds void (*old_pop)(struct atm_vcc *, struct sk_buff *);
600e56d99aSDavid Woodhouse void (*old_release_cb)(struct atm_vcc *);
61e41faed9SKrzysztof Mazur struct module *old_owner;
621da177e4SLinus Torvalds /* keep old push/pop for detaching */
631da177e4SLinus Torvalds enum pppoatm_encaps encaps;
649d02daf7SDavid Woodhouse atomic_t inflight;
659d02daf7SDavid Woodhouse unsigned long blocked;
661da177e4SLinus Torvalds int flags; /* SC_COMP_PROT - compress protocol */
671da177e4SLinus Torvalds struct ppp_channel chan; /* interface to generic ppp layer */
681da177e4SLinus Torvalds struct tasklet_struct wakeup_tasklet;
691da177e4SLinus Torvalds };
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds /*
729d02daf7SDavid Woodhouse * We want to allow two packets in the queue. The one that's currently in
739d02daf7SDavid Woodhouse * flight, and *one* queued up ready for the ATM device to send immediately
749d02daf7SDavid Woodhouse * from its TX done IRQ. We want to be able to use atomic_inc_not_zero(), so
759d02daf7SDavid Woodhouse * inflight == -2 represents an empty queue, -1 one packet, and zero means
769d02daf7SDavid Woodhouse * there are two packets in the queue.
779d02daf7SDavid Woodhouse */
789d02daf7SDavid Woodhouse #define NONE_INFLIGHT -2
799d02daf7SDavid Woodhouse
809d02daf7SDavid Woodhouse #define BLOCKED 0
819d02daf7SDavid Woodhouse
829d02daf7SDavid Woodhouse /*
831da177e4SLinus Torvalds * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
841da177e4SLinus Torvalds * ID (0xC021) used in autodetection
851da177e4SLinus Torvalds */
861da177e4SLinus Torvalds static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
871da177e4SLinus Torvalds #define LLC_LEN (4)
881da177e4SLinus Torvalds
atmvcc_to_pvcc(const struct atm_vcc * atmvcc)891da177e4SLinus Torvalds static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds return (struct pppoatm_vcc *) (atmvcc->user_back);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds
chan_to_pvcc(const struct ppp_channel * chan)941da177e4SLinus Torvalds static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds return (struct pppoatm_vcc *) (chan->private);
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds * We can't do this directly from our _pop handler, since the ppp code
1011da177e4SLinus Torvalds * doesn't want to be called in interrupt context, so we do it from
1021da177e4SLinus Torvalds * a tasklet
1031da177e4SLinus Torvalds */
pppoatm_wakeup_sender(struct tasklet_struct * t)104*a5874597SEmil Renner Berthing static void pppoatm_wakeup_sender(struct tasklet_struct *t)
1051da177e4SLinus Torvalds {
106*a5874597SEmil Renner Berthing struct pppoatm_vcc *pvcc = from_tasklet(pvcc, t, wakeup_tasklet);
107*a5874597SEmil Renner Berthing
108*a5874597SEmil Renner Berthing ppp_output_wakeup(&pvcc->chan);
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds
pppoatm_release_cb(struct atm_vcc * atmvcc)1110e56d99aSDavid Woodhouse static void pppoatm_release_cb(struct atm_vcc *atmvcc)
1120e56d99aSDavid Woodhouse {
1130e56d99aSDavid Woodhouse struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
1140e56d99aSDavid Woodhouse
1155b4d7208SDavid Woodhouse /*
1165b4d7208SDavid Woodhouse * As in pppoatm_pop(), it's safe to clear the BLOCKED bit here because
1175b4d7208SDavid Woodhouse * the wakeup *can't* race with pppoatm_send(). They both hold the PPP
1185b4d7208SDavid Woodhouse * channel's ->downl lock. And the potential race with *setting* it,
1195b4d7208SDavid Woodhouse * which leads to the double-check dance in pppoatm_may_send(), doesn't
1205b4d7208SDavid Woodhouse * exist here. In the sock_owned_by_user() case in pppoatm_send(), we
1215b4d7208SDavid Woodhouse * set the BLOCKED bit while the socket is still locked. We know that
1225b4d7208SDavid Woodhouse * ->release_cb() can't be called until that's done.
1235b4d7208SDavid Woodhouse */
1245b4d7208SDavid Woodhouse if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
1250e56d99aSDavid Woodhouse tasklet_schedule(&pvcc->wakeup_tasklet);
1260e56d99aSDavid Woodhouse if (pvcc->old_release_cb)
1270e56d99aSDavid Woodhouse pvcc->old_release_cb(atmvcc);
1280e56d99aSDavid Woodhouse }
1291da177e4SLinus Torvalds /*
1301da177e4SLinus Torvalds * This gets called every time the ATM card has finished sending our
1311da177e4SLinus Torvalds * skb. The ->old_pop will take care up normal atm flow control,
1321da177e4SLinus Torvalds * but we also need to wake up the device if we blocked it
1331da177e4SLinus Torvalds */
pppoatm_pop(struct atm_vcc * atmvcc,struct sk_buff * skb)1341da177e4SLinus Torvalds static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
1379d02daf7SDavid Woodhouse
1381da177e4SLinus Torvalds pvcc->old_pop(atmvcc, skb);
1399d02daf7SDavid Woodhouse atomic_dec(&pvcc->inflight);
1409d02daf7SDavid Woodhouse
1411da177e4SLinus Torvalds /*
1429d02daf7SDavid Woodhouse * We always used to run the wakeup tasklet unconditionally here, for
1439d02daf7SDavid Woodhouse * fear of race conditions where we clear the BLOCKED flag just as we
1449d02daf7SDavid Woodhouse * refuse another packet in pppoatm_send(). This was quite inefficient.
1459d02daf7SDavid Woodhouse *
1469d02daf7SDavid Woodhouse * In fact it's OK. The PPP core will only ever call pppoatm_send()
1479d02daf7SDavid Woodhouse * while holding the channel->downl lock. And ppp_output_wakeup() as
1489d02daf7SDavid Woodhouse * called by the tasklet will *also* grab that lock. So even if another
1499d02daf7SDavid Woodhouse * CPU is in pppoatm_send() right now, the tasklet isn't going to race
1509d02daf7SDavid Woodhouse * with it. The wakeup *will* happen after the other CPU is safely out
1519d02daf7SDavid Woodhouse * of pppoatm_send() again.
1529d02daf7SDavid Woodhouse *
1539d02daf7SDavid Woodhouse * So if the CPU in pppoatm_send() has already set the BLOCKED bit and
1549d02daf7SDavid Woodhouse * it about to return, that's fine. We trigger a wakeup which will
1559d02daf7SDavid Woodhouse * happen later. And if the CPU in pppoatm_send() *hasn't* set the
1569d02daf7SDavid Woodhouse * BLOCKED bit yet, that's fine too because of the double check in
1579d02daf7SDavid Woodhouse * pppoatm_may_send() which is commented there.
1581da177e4SLinus Torvalds */
1599d02daf7SDavid Woodhouse if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
1601da177e4SLinus Torvalds tasklet_schedule(&pvcc->wakeup_tasklet);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds
1631da177e4SLinus Torvalds /*
1641da177e4SLinus Torvalds * Unbind from PPP - currently we only do this when closing the socket,
1651da177e4SLinus Torvalds * but we could put this into an ioctl if need be
1661da177e4SLinus Torvalds */
pppoatm_unassign_vcc(struct atm_vcc * atmvcc)1671da177e4SLinus Torvalds static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds struct pppoatm_vcc *pvcc;
1701da177e4SLinus Torvalds pvcc = atmvcc_to_pvcc(atmvcc);
1711da177e4SLinus Torvalds atmvcc->push = pvcc->old_push;
1721da177e4SLinus Torvalds atmvcc->pop = pvcc->old_pop;
1730e56d99aSDavid Woodhouse atmvcc->release_cb = pvcc->old_release_cb;
1741da177e4SLinus Torvalds tasklet_kill(&pvcc->wakeup_tasklet);
1751da177e4SLinus Torvalds ppp_unregister_channel(&pvcc->chan);
1761da177e4SLinus Torvalds atmvcc->user_back = NULL;
1771da177e4SLinus Torvalds kfree(pvcc);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds
1801da177e4SLinus Torvalds /* Called when an AAL5 PDU comes in */
pppoatm_push(struct atm_vcc * atmvcc,struct sk_buff * skb)1811da177e4SLinus Torvalds static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
18499824461SJoe Perches pr_debug("\n");
1851da177e4SLinus Torvalds if (skb == NULL) { /* VCC was closed */
186e41faed9SKrzysztof Mazur struct module *module;
187e41faed9SKrzysztof Mazur
18852240062SStephen Hemminger pr_debug("removing ATMPPP VCC %p\n", pvcc);
189e41faed9SKrzysztof Mazur module = pvcc->old_owner;
1901da177e4SLinus Torvalds pppoatm_unassign_vcc(atmvcc);
1911da177e4SLinus Torvalds atmvcc->push(atmvcc, NULL); /* Pass along bad news */
192e41faed9SKrzysztof Mazur module_put(module);
1931da177e4SLinus Torvalds return;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds atm_return(atmvcc, skb->truesize);
1961da177e4SLinus Torvalds switch (pvcc->encaps) {
1971da177e4SLinus Torvalds case e_llc:
1981da177e4SLinus Torvalds if (skb->len < LLC_LEN ||
1991da177e4SLinus Torvalds memcmp(skb->data, pppllc, LLC_LEN))
2001da177e4SLinus Torvalds goto error;
2011da177e4SLinus Torvalds skb_pull(skb, LLC_LEN);
2021da177e4SLinus Torvalds break;
2031da177e4SLinus Torvalds case e_autodetect:
2041da177e4SLinus Torvalds if (pvcc->chan.ppp == NULL) { /* Not bound yet! */
2051da177e4SLinus Torvalds kfree_skb(skb);
2061da177e4SLinus Torvalds return;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds if (skb->len >= sizeof(pppllc) &&
2091da177e4SLinus Torvalds !memcmp(skb->data, pppllc, sizeof(pppllc))) {
2101da177e4SLinus Torvalds pvcc->encaps = e_llc;
2111da177e4SLinus Torvalds skb_pull(skb, LLC_LEN);
2121da177e4SLinus Torvalds break;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
2151da177e4SLinus Torvalds !memcmp(skb->data, &pppllc[LLC_LEN],
2161da177e4SLinus Torvalds sizeof(pppllc) - LLC_LEN)) {
2171da177e4SLinus Torvalds pvcc->encaps = e_vc;
2181da177e4SLinus Torvalds pvcc->chan.mtu += LLC_LEN;
2191da177e4SLinus Torvalds break;
2201da177e4SLinus Torvalds }
221a8a213cbSAndy Shevchenko pr_debug("Couldn't autodetect yet (skb: %6ph)\n", skb->data);
2221da177e4SLinus Torvalds goto error;
2231da177e4SLinus Torvalds case e_vc:
2241da177e4SLinus Torvalds break;
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds ppp_input(&pvcc->chan, skb);
2271da177e4SLinus Torvalds return;
228d81219dbSJoe Perches
2291da177e4SLinus Torvalds error:
2301da177e4SLinus Torvalds kfree_skb(skb);
2311da177e4SLinus Torvalds ppp_input_error(&pvcc->chan, 0);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds
pppoatm_may_send(struct pppoatm_vcc * pvcc,int size)234397ff16dSKrzysztof Mazur static int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
2359d02daf7SDavid Woodhouse {
2369d02daf7SDavid Woodhouse /*
2379d02daf7SDavid Woodhouse * It's not clear that we need to bother with using atm_may_send()
2389d02daf7SDavid Woodhouse * to check we don't exceed sk->sk_sndbuf. If userspace sets a
2399d02daf7SDavid Woodhouse * value of sk_sndbuf which is lower than the MTU, we're going to
2409d02daf7SDavid Woodhouse * block for ever. But the code always did that before we introduced
2419d02daf7SDavid Woodhouse * the packet count limit, so...
2429d02daf7SDavid Woodhouse */
2439d02daf7SDavid Woodhouse if (atm_may_send(pvcc->atmvcc, size) &&
244f74445b6SMark Rutland atomic_inc_not_zero(&pvcc->inflight))
2459d02daf7SDavid Woodhouse return 1;
2469d02daf7SDavid Woodhouse
2479d02daf7SDavid Woodhouse /*
2489d02daf7SDavid Woodhouse * We use test_and_set_bit() rather than set_bit() here because
2499d02daf7SDavid Woodhouse * we need to ensure there's a memory barrier after it. The bit
2509d02daf7SDavid Woodhouse * *must* be set before we do the atomic_inc() on pvcc->inflight.
2519d02daf7SDavid Woodhouse * There's no smp_mb__after_set_bit(), so it's this or abuse
2524e857c58SPeter Zijlstra * smp_mb__after_atomic().
2539d02daf7SDavid Woodhouse */
2549d02daf7SDavid Woodhouse test_and_set_bit(BLOCKED, &pvcc->blocked);
2559d02daf7SDavid Woodhouse
2569d02daf7SDavid Woodhouse /*
2579d02daf7SDavid Woodhouse * We may have raced with pppoatm_pop(). If it ran for the
2589d02daf7SDavid Woodhouse * last packet in the queue, *just* before we set the BLOCKED
2599d02daf7SDavid Woodhouse * bit, then it might never run again and the channel could
2609d02daf7SDavid Woodhouse * remain permanently blocked. Cope with that race by checking
2619d02daf7SDavid Woodhouse * *again*. If it did run in that window, we'll have space on
2629d02daf7SDavid Woodhouse * the queue now and can return success. It's harmless to leave
2639d02daf7SDavid Woodhouse * the BLOCKED flag set, since it's only used as a trigger to
2649d02daf7SDavid Woodhouse * run the wakeup tasklet. Another wakeup will never hurt.
2659d02daf7SDavid Woodhouse * If pppoatm_pop() is running but hasn't got as far as making
2669d02daf7SDavid Woodhouse * space on the queue yet, then it hasn't checked the BLOCKED
2679d02daf7SDavid Woodhouse * flag yet either, so we're safe in that case too. It'll issue
2689d02daf7SDavid Woodhouse * an "immediate" wakeup... where "immediate" actually involves
2699d02daf7SDavid Woodhouse * taking the PPP channel's ->downl lock, which is held by the
2709d02daf7SDavid Woodhouse * code path that calls pppoatm_send(), and is thus going to
2719d02daf7SDavid Woodhouse * wait for us to finish.
2729d02daf7SDavid Woodhouse */
2739d02daf7SDavid Woodhouse if (atm_may_send(pvcc->atmvcc, size) &&
2749d02daf7SDavid Woodhouse atomic_inc_not_zero(&pvcc->inflight))
2759d02daf7SDavid Woodhouse return 1;
2769d02daf7SDavid Woodhouse
2779d02daf7SDavid Woodhouse return 0;
2789d02daf7SDavid Woodhouse }
2791da177e4SLinus Torvalds /*
2801da177e4SLinus Torvalds * Called by the ppp_generic.c to send a packet - returns true if packet
2811da177e4SLinus Torvalds * was accepted. If we return false, then it's our job to call
2821da177e4SLinus Torvalds * ppp_output_wakeup(chan) when we're feeling more up to it.
2831da177e4SLinus Torvalds * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
2841da177e4SLinus Torvalds * we should really drop the packet, but the generic layer doesn't
2851da177e4SLinus Torvalds * support this yet. We just return 'DROP_PACKET' which we actually define
2861da177e4SLinus Torvalds * as success, just to be clear what we're really doing.
2871da177e4SLinus Torvalds */
2881da177e4SLinus Torvalds #define DROP_PACKET 1
pppoatm_send(struct ppp_channel * chan,struct sk_buff * skb)2891da177e4SLinus Torvalds static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
2923ac10800SKrzysztof Mazur struct atm_vcc *vcc;
2933ac10800SKrzysztof Mazur int ret;
2943ac10800SKrzysztof Mazur
2951da177e4SLinus Torvalds ATM_SKB(skb)->vcc = pvcc->atmvcc;
29699824461SJoe Perches pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
2971da177e4SLinus Torvalds if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
2981da177e4SLinus Torvalds (void) skb_pull(skb, 1);
2993ac10800SKrzysztof Mazur
3003ac10800SKrzysztof Mazur vcc = ATM_SKB(skb)->vcc;
3013ac10800SKrzysztof Mazur bh_lock_sock(sk_atm(vcc));
3025b4d7208SDavid Woodhouse if (sock_owned_by_user(sk_atm(vcc))) {
3035b4d7208SDavid Woodhouse /*
3045b4d7208SDavid Woodhouse * Needs to happen (and be flushed, hence test_and_) before we unlock
3055b4d7208SDavid Woodhouse * the socket. It needs to be seen by the time our ->release_cb gets
3065b4d7208SDavid Woodhouse * called.
3075b4d7208SDavid Woodhouse */
3085b4d7208SDavid Woodhouse test_and_set_bit(BLOCKED, &pvcc->blocked);
3093ac10800SKrzysztof Mazur goto nospace;
3105b4d7208SDavid Woodhouse }
311071d9393SKrzysztof Mazur if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
312071d9393SKrzysztof Mazur test_bit(ATM_VF_CLOSE, &vcc->flags) ||
313071d9393SKrzysztof Mazur !test_bit(ATM_VF_READY, &vcc->flags)) {
314071d9393SKrzysztof Mazur bh_unlock_sock(sk_atm(vcc));
315071d9393SKrzysztof Mazur kfree_skb(skb);
316071d9393SKrzysztof Mazur return DROP_PACKET;
317071d9393SKrzysztof Mazur }
3183ac10800SKrzysztof Mazur
3191da177e4SLinus Torvalds switch (pvcc->encaps) { /* LLC encapsulation needed */
3201da177e4SLinus Torvalds case e_llc:
3211da177e4SLinus Torvalds if (skb_headroom(skb) < LLC_LEN) {
3221da177e4SLinus Torvalds struct sk_buff *n;
3231da177e4SLinus Torvalds n = skb_realloc_headroom(skb, LLC_LEN);
3241da177e4SLinus Torvalds if (n != NULL &&
3259d02daf7SDavid Woodhouse !pppoatm_may_send(pvcc, n->truesize)) {
3261da177e4SLinus Torvalds kfree_skb(n);
3271da177e4SLinus Torvalds goto nospace;
3281da177e4SLinus Torvalds }
3295d0ba55bSEric Dumazet consume_skb(skb);
330d81219dbSJoe Perches skb = n;
3313ac10800SKrzysztof Mazur if (skb == NULL) {
3323ac10800SKrzysztof Mazur bh_unlock_sock(sk_atm(vcc));
3331da177e4SLinus Torvalds return DROP_PACKET;
3343ac10800SKrzysztof Mazur }
3359d02daf7SDavid Woodhouse } else if (!pppoatm_may_send(pvcc, skb->truesize))
3361da177e4SLinus Torvalds goto nospace;
3371da177e4SLinus Torvalds memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
3381da177e4SLinus Torvalds break;
3391da177e4SLinus Torvalds case e_vc:
3409d02daf7SDavid Woodhouse if (!pppoatm_may_send(pvcc, skb->truesize))
3411da177e4SLinus Torvalds goto nospace;
3421da177e4SLinus Torvalds break;
3431da177e4SLinus Torvalds case e_autodetect:
3443ac10800SKrzysztof Mazur bh_unlock_sock(sk_atm(vcc));
34552240062SStephen Hemminger pr_debug("Trying to send without setting encaps!\n");
3461da177e4SLinus Torvalds kfree_skb(skb);
3471da177e4SLinus Torvalds return 1;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds
3509bbe60a6SDavid Woodhouse atm_account_tx(vcc, skb);
35199824461SJoe Perches pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
35299824461SJoe Perches skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
3533ac10800SKrzysztof Mazur ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
3541da177e4SLinus Torvalds ? DROP_PACKET : 1;
3553ac10800SKrzysztof Mazur bh_unlock_sock(sk_atm(vcc));
3563ac10800SKrzysztof Mazur return ret;
3571da177e4SLinus Torvalds nospace:
3583ac10800SKrzysztof Mazur bh_unlock_sock(sk_atm(vcc));
3591da177e4SLinus Torvalds /*
3601da177e4SLinus Torvalds * We don't have space to send this SKB now, but we might have
3611da177e4SLinus Torvalds * already applied SC_COMP_PROT compression, so may need to undo
3621da177e4SLinus Torvalds */
3631da177e4SLinus Torvalds if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
3641da177e4SLinus Torvalds skb->data[-1] == '\0')
3651da177e4SLinus Torvalds (void) skb_push(skb, 1);
3661da177e4SLinus Torvalds return 0;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds
3691da177e4SLinus Torvalds /* This handles ioctls sent to the /dev/ppp interface */
pppoatm_devppp_ioctl(struct ppp_channel * chan,unsigned int cmd,unsigned long arg)3701da177e4SLinus Torvalds static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
3711da177e4SLinus Torvalds unsigned long arg)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds switch (cmd) {
3741da177e4SLinus Torvalds case PPPIOCGFLAGS:
3751da177e4SLinus Torvalds return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
3761da177e4SLinus Torvalds ? -EFAULT : 0;
3771da177e4SLinus Torvalds case PPPIOCSFLAGS:
3781da177e4SLinus Torvalds return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
3791da177e4SLinus Torvalds ? -EFAULT : 0;
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds return -ENOTTY;
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds
384d7100da0Sstephen hemminger static const struct ppp_channel_ops pppoatm_ops = {
3851da177e4SLinus Torvalds .start_xmit = pppoatm_send,
3861da177e4SLinus Torvalds .ioctl = pppoatm_devppp_ioctl,
3871da177e4SLinus Torvalds };
3881da177e4SLinus Torvalds
pppoatm_assign_vcc(struct atm_vcc * atmvcc,void __user * arg)3891da177e4SLinus Torvalds static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds struct atm_backend_ppp be;
3921da177e4SLinus Torvalds struct pppoatm_vcc *pvcc;
3931da177e4SLinus Torvalds int err;
394a5b88632SEmil Renner Berthing
3951da177e4SLinus Torvalds if (copy_from_user(&be, arg, sizeof be))
3961da177e4SLinus Torvalds return -EFAULT;
3971da177e4SLinus Torvalds if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
3981da177e4SLinus Torvalds be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
3991da177e4SLinus Torvalds return -EINVAL;
4000da974f4SPanagiotis Issaris pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
4011da177e4SLinus Torvalds if (pvcc == NULL)
4021da177e4SLinus Torvalds return -ENOMEM;
4031da177e4SLinus Torvalds pvcc->atmvcc = atmvcc;
4049d02daf7SDavid Woodhouse
4059d02daf7SDavid Woodhouse /* Maximum is zero, so that we can use atomic_inc_not_zero() */
4069d02daf7SDavid Woodhouse atomic_set(&pvcc->inflight, NONE_INFLIGHT);
4071da177e4SLinus Torvalds pvcc->old_push = atmvcc->push;
4081da177e4SLinus Torvalds pvcc->old_pop = atmvcc->pop;
409e41faed9SKrzysztof Mazur pvcc->old_owner = atmvcc->owner;
4100e56d99aSDavid Woodhouse pvcc->old_release_cb = atmvcc->release_cb;
4111da177e4SLinus Torvalds pvcc->encaps = (enum pppoatm_encaps) be.encaps;
4121da177e4SLinus Torvalds pvcc->chan.private = pvcc;
4131da177e4SLinus Torvalds pvcc->chan.ops = &pppoatm_ops;
4141da177e4SLinus Torvalds pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
4151da177e4SLinus Torvalds (be.encaps == e_vc ? 0 : LLC_LEN);
416*a5874597SEmil Renner Berthing tasklet_setup(&pvcc->wakeup_tasklet, pppoatm_wakeup_sender);
417d81219dbSJoe Perches err = ppp_register_channel(&pvcc->chan);
418d81219dbSJoe Perches if (err != 0) {
4191da177e4SLinus Torvalds kfree(pvcc);
4201da177e4SLinus Torvalds return err;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds atmvcc->user_back = pvcc;
4231da177e4SLinus Torvalds atmvcc->push = pppoatm_push;
4241da177e4SLinus Torvalds atmvcc->pop = pppoatm_pop;
4250e56d99aSDavid Woodhouse atmvcc->release_cb = pppoatm_release_cb;
4261da177e4SLinus Torvalds __module_get(THIS_MODULE);
427e41faed9SKrzysztof Mazur atmvcc->owner = THIS_MODULE;
4284e55f578SJorge Boncompte [DTI2]
4294e55f578SJorge Boncompte [DTI2] /* re-process everything received between connection setup and
4304e55f578SJorge Boncompte [DTI2] backend setup */
4314e55f578SJorge Boncompte [DTI2] vcc_process_recv_queue(atmvcc);
4321da177e4SLinus Torvalds return 0;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds
4351da177e4SLinus Torvalds /*
4361da177e4SLinus Torvalds * This handles ioctls actually performed on our vcc - we must return
4371da177e4SLinus Torvalds * -ENOIOCTLCMD for any unrecognized ioctl
4381da177e4SLinus Torvalds */
pppoatm_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)4391da177e4SLinus Torvalds static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
4401da177e4SLinus Torvalds unsigned long arg)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds struct atm_vcc *atmvcc = ATM_SD(sock);
4431da177e4SLinus Torvalds void __user *argp = (void __user *)arg;
4441da177e4SLinus Torvalds
4451da177e4SLinus Torvalds if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
4461da177e4SLinus Torvalds return -ENOIOCTLCMD;
4471da177e4SLinus Torvalds switch (cmd) {
4481da177e4SLinus Torvalds case ATM_SETBACKEND: {
4491da177e4SLinus Torvalds atm_backend_t b;
4501da177e4SLinus Torvalds if (get_user(b, (atm_backend_t __user *) argp))
4511da177e4SLinus Torvalds return -EFAULT;
4521da177e4SLinus Torvalds if (b != ATM_BACKEND_PPP)
4531da177e4SLinus Torvalds return -ENOIOCTLCMD;
4541da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
4551da177e4SLinus Torvalds return -EPERM;
4563b1a9145SKrzysztof Mazur if (sock->state != SS_CONNECTED)
4573b1a9145SKrzysztof Mazur return -EINVAL;
4581da177e4SLinus Torvalds return pppoatm_assign_vcc(atmvcc, argp);
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds case PPPIOCGCHAN:
4611da177e4SLinus Torvalds return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
4621da177e4SLinus Torvalds chan), (int __user *) argp) ? -EFAULT : 0;
4631da177e4SLinus Torvalds case PPPIOCGUNIT:
4641da177e4SLinus Torvalds return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
4651da177e4SLinus Torvalds chan), (int __user *) argp) ? -EFAULT : 0;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds return -ENOIOCTLCMD;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds
4701da177e4SLinus Torvalds static struct atm_ioctl pppoatm_ioctl_ops = {
4711da177e4SLinus Torvalds .owner = THIS_MODULE,
4721da177e4SLinus Torvalds .ioctl = pppoatm_ioctl,
4731da177e4SLinus Torvalds };
4741da177e4SLinus Torvalds
pppoatm_init(void)4751da177e4SLinus Torvalds static int __init pppoatm_init(void)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds register_atm_ioctl(&pppoatm_ioctl_ops);
4781da177e4SLinus Torvalds return 0;
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds
pppoatm_exit(void)4811da177e4SLinus Torvalds static void __exit pppoatm_exit(void)
4821da177e4SLinus Torvalds {
4831da177e4SLinus Torvalds deregister_atm_ioctl(&pppoatm_ioctl_ops);
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds
4861da177e4SLinus Torvalds module_init(pppoatm_init);
4871da177e4SLinus Torvalds module_exit(pppoatm_exit);
4881da177e4SLinus Torvalds
4891da177e4SLinus Torvalds MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
4901da177e4SLinus Torvalds MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
4911da177e4SLinus Torvalds MODULE_LICENSE("GPL");
492