xref: /openbmc/linux/net/atm/pppoatm.c (revision 071d93931a75dc1f82f0baa9959613af81c5a032)
11da177e4SLinus Torvalds /* net/atm/pppoatm.c - RFC2364 PPP over ATM/AAL5 */
21da177e4SLinus Torvalds 
31da177e4SLinus Torvalds /* Copyright 1999-2000 by Mitchell Blank Jr */
41da177e4SLinus Torvalds /* Based on clip.c; 1995-1999 by Werner Almesberger, EPFL LRC/ICA */
51da177e4SLinus Torvalds /* And on ppp_async.c; Copyright 1999 Paul Mackerras */
61da177e4SLinus Torvalds /* And help from Jens Axboe */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or
101da177e4SLinus Torvalds  *  modify it under the terms of the GNU General Public License
111da177e4SLinus Torvalds  *  as published by the Free Software Foundation; either version
121da177e4SLinus Torvalds  *  2 of the License, or (at your option) any later version.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * This driver provides the encapsulation and framing for sending
151da177e4SLinus Torvalds  * and receiving PPP frames in ATM AAL5 PDUs.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /*
191da177e4SLinus Torvalds  * One shortcoming of this driver is that it does not comply with
201da177e4SLinus Torvalds  * section 8 of RFC2364 - we are supposed to detect a change
211da177e4SLinus Torvalds  * in encapsulation and immediately abort the connection (in order
221da177e4SLinus Torvalds  * to avoid a black-hole being created if our peer loses state
231da177e4SLinus Torvalds  * and changes encapsulation unilaterally.  However, since the
241da177e4SLinus Torvalds  * ppp_generic layer actually does the decapsulation, we need
251da177e4SLinus Torvalds  * a way of notifying it when we _think_ there might be a problem)
261da177e4SLinus Torvalds  * There's two cases:
271da177e4SLinus Torvalds  *   1.	LLC-encapsulation was missing when it was enabled.  In
281da177e4SLinus Torvalds  *	this case, we should tell the upper layer "tear down
291da177e4SLinus Torvalds  *	this session if this skb looks ok to you"
301da177e4SLinus Torvalds  *   2.	LLC-encapsulation was present when it was disabled.  Then
311da177e4SLinus Torvalds  *	we need to tell the upper layer "this packet may be
321da177e4SLinus Torvalds  *	ok, but if its in error tear down the session"
331da177e4SLinus Torvalds  * These hooks are not yet available in ppp_generic
341da177e4SLinus Torvalds  */
351da177e4SLinus Torvalds 
3699824461SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
3799824461SJoe Perches 
381da177e4SLinus Torvalds #include <linux/module.h>
391da177e4SLinus Torvalds #include <linux/init.h>
40a6b7a407SAlexey Dobriyan #include <linux/interrupt.h>
411da177e4SLinus Torvalds #include <linux/skbuff.h>
425a0e3ad6STejun Heo #include <linux/slab.h>
431da177e4SLinus Torvalds #include <linux/atm.h>
441da177e4SLinus Torvalds #include <linux/atmdev.h>
454fc268d2SRandy Dunlap #include <linux/capability.h>
461da177e4SLinus Torvalds #include <linux/ppp_defs.h>
474b32da2bSPaul Mackerras #include <linux/ppp-ioctl.h>
481da177e4SLinus Torvalds #include <linux/ppp_channel.h>
491da177e4SLinus Torvalds #include <linux/atmppp.h>
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #include "common.h"
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds enum pppoatm_encaps {
541da177e4SLinus Torvalds 	e_autodetect = PPPOATM_ENCAPS_AUTODETECT,
551da177e4SLinus Torvalds 	e_vc = PPPOATM_ENCAPS_VC,
561da177e4SLinus Torvalds 	e_llc = PPPOATM_ENCAPS_LLC,
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds struct pppoatm_vcc {
601da177e4SLinus Torvalds 	struct atm_vcc	*atmvcc;	/* VCC descriptor */
611da177e4SLinus Torvalds 	void (*old_push)(struct atm_vcc *, struct sk_buff *);
621da177e4SLinus Torvalds 	void (*old_pop)(struct atm_vcc *, struct sk_buff *);
63e41faed9SKrzysztof Mazur 	struct module *old_owner;
641da177e4SLinus Torvalds 					/* keep old push/pop for detaching */
651da177e4SLinus Torvalds 	enum pppoatm_encaps encaps;
669d02daf7SDavid Woodhouse 	atomic_t inflight;
679d02daf7SDavid Woodhouse 	unsigned long blocked;
681da177e4SLinus Torvalds 	int flags;			/* SC_COMP_PROT - compress protocol */
691da177e4SLinus Torvalds 	struct ppp_channel chan;	/* interface to generic ppp layer */
701da177e4SLinus Torvalds 	struct tasklet_struct wakeup_tasklet;
711da177e4SLinus Torvalds };
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
749d02daf7SDavid Woodhouse  * We want to allow two packets in the queue. The one that's currently in
759d02daf7SDavid Woodhouse  * flight, and *one* queued up ready for the ATM device to send immediately
769d02daf7SDavid Woodhouse  * from its TX done IRQ. We want to be able to use atomic_inc_not_zero(), so
779d02daf7SDavid Woodhouse  * inflight == -2 represents an empty queue, -1 one packet, and zero means
789d02daf7SDavid Woodhouse  * there are two packets in the queue.
799d02daf7SDavid Woodhouse  */
809d02daf7SDavid Woodhouse #define NONE_INFLIGHT -2
819d02daf7SDavid Woodhouse 
829d02daf7SDavid Woodhouse #define BLOCKED 0
839d02daf7SDavid Woodhouse 
849d02daf7SDavid Woodhouse /*
851da177e4SLinus Torvalds  * Header used for LLC Encapsulated PPP (4 bytes) followed by the LCP protocol
861da177e4SLinus Torvalds  * ID (0xC021) used in autodetection
871da177e4SLinus Torvalds  */
881da177e4SLinus Torvalds static const unsigned char pppllc[6] = { 0xFE, 0xFE, 0x03, 0xCF, 0xC0, 0x21 };
891da177e4SLinus Torvalds #define LLC_LEN		(4)
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds static inline struct pppoatm_vcc *atmvcc_to_pvcc(const struct atm_vcc *atmvcc)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds 	return (struct pppoatm_vcc *) (atmvcc->user_back);
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static inline struct pppoatm_vcc *chan_to_pvcc(const struct ppp_channel *chan)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	return (struct pppoatm_vcc *) (chan->private);
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds  * We can't do this directly from our _pop handler, since the ppp code
1031da177e4SLinus Torvalds  * doesn't want to be called in interrupt context, so we do it from
1041da177e4SLinus Torvalds  * a tasklet
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds static void pppoatm_wakeup_sender(unsigned long arg)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	ppp_output_wakeup((struct ppp_channel *) arg);
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /*
1121da177e4SLinus Torvalds  * This gets called every time the ATM card has finished sending our
1131da177e4SLinus Torvalds  * skb.  The ->old_pop will take care up normal atm flow control,
1141da177e4SLinus Torvalds  * but we also need to wake up the device if we blocked it
1151da177e4SLinus Torvalds  */
1161da177e4SLinus Torvalds static void pppoatm_pop(struct atm_vcc *atmvcc, struct sk_buff *skb)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
1199d02daf7SDavid Woodhouse 
1201da177e4SLinus Torvalds 	pvcc->old_pop(atmvcc, skb);
1219d02daf7SDavid Woodhouse 	atomic_dec(&pvcc->inflight);
1229d02daf7SDavid Woodhouse 
1231da177e4SLinus Torvalds 	/*
1249d02daf7SDavid Woodhouse 	 * We always used to run the wakeup tasklet unconditionally here, for
1259d02daf7SDavid Woodhouse 	 * fear of race conditions where we clear the BLOCKED flag just as we
1269d02daf7SDavid Woodhouse 	 * refuse another packet in pppoatm_send(). This was quite inefficient.
1279d02daf7SDavid Woodhouse 	 *
1289d02daf7SDavid Woodhouse 	 * In fact it's OK. The PPP core will only ever call pppoatm_send()
1299d02daf7SDavid Woodhouse 	 * while holding the channel->downl lock. And ppp_output_wakeup() as
1309d02daf7SDavid Woodhouse 	 * called by the tasklet will *also* grab that lock. So even if another
1319d02daf7SDavid Woodhouse 	 * CPU is in pppoatm_send() right now, the tasklet isn't going to race
1329d02daf7SDavid Woodhouse 	 * with it. The wakeup *will* happen after the other CPU is safely out
1339d02daf7SDavid Woodhouse 	 * of pppoatm_send() again.
1349d02daf7SDavid Woodhouse 	 *
1359d02daf7SDavid Woodhouse 	 * So if the CPU in pppoatm_send() has already set the BLOCKED bit and
1369d02daf7SDavid Woodhouse 	 * it about to return, that's fine. We trigger a wakeup which will
1379d02daf7SDavid Woodhouse 	 * happen later. And if the CPU in pppoatm_send() *hasn't* set the
1389d02daf7SDavid Woodhouse 	 * BLOCKED bit yet, that's fine too because of the double check in
1399d02daf7SDavid Woodhouse 	 * pppoatm_may_send() which is commented there.
1401da177e4SLinus Torvalds 	 */
1419d02daf7SDavid Woodhouse 	if (test_and_clear_bit(BLOCKED, &pvcc->blocked))
1421da177e4SLinus Torvalds 		tasklet_schedule(&pvcc->wakeup_tasklet);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /*
1461da177e4SLinus Torvalds  * Unbind from PPP - currently we only do this when closing the socket,
1471da177e4SLinus Torvalds  * but we could put this into an ioctl if need be
1481da177e4SLinus Torvalds  */
1491da177e4SLinus Torvalds static void pppoatm_unassign_vcc(struct atm_vcc *atmvcc)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	struct pppoatm_vcc *pvcc;
1521da177e4SLinus Torvalds 	pvcc = atmvcc_to_pvcc(atmvcc);
1531da177e4SLinus Torvalds 	atmvcc->push = pvcc->old_push;
1541da177e4SLinus Torvalds 	atmvcc->pop = pvcc->old_pop;
1551da177e4SLinus Torvalds 	tasklet_kill(&pvcc->wakeup_tasklet);
1561da177e4SLinus Torvalds 	ppp_unregister_channel(&pvcc->chan);
1571da177e4SLinus Torvalds 	atmvcc->user_back = NULL;
1581da177e4SLinus Torvalds 	kfree(pvcc);
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /* Called when an AAL5 PDU comes in */
1621da177e4SLinus Torvalds static void pppoatm_push(struct atm_vcc *atmvcc, struct sk_buff *skb)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	struct pppoatm_vcc *pvcc = atmvcc_to_pvcc(atmvcc);
16599824461SJoe Perches 	pr_debug("\n");
1661da177e4SLinus Torvalds 	if (skb == NULL) {			/* VCC was closed */
167e41faed9SKrzysztof Mazur 		struct module *module;
168e41faed9SKrzysztof Mazur 
16952240062SStephen Hemminger 		pr_debug("removing ATMPPP VCC %p\n", pvcc);
170e41faed9SKrzysztof Mazur 		module = pvcc->old_owner;
1711da177e4SLinus Torvalds 		pppoatm_unassign_vcc(atmvcc);
1721da177e4SLinus Torvalds 		atmvcc->push(atmvcc, NULL);	/* Pass along bad news */
173e41faed9SKrzysztof Mazur 		module_put(module);
1741da177e4SLinus Torvalds 		return;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 	atm_return(atmvcc, skb->truesize);
1771da177e4SLinus Torvalds 	switch (pvcc->encaps) {
1781da177e4SLinus Torvalds 	case e_llc:
1791da177e4SLinus Torvalds 		if (skb->len < LLC_LEN ||
1801da177e4SLinus Torvalds 		    memcmp(skb->data, pppllc, LLC_LEN))
1811da177e4SLinus Torvalds 			goto error;
1821da177e4SLinus Torvalds 		skb_pull(skb, LLC_LEN);
1831da177e4SLinus Torvalds 		break;
1841da177e4SLinus Torvalds 	case e_autodetect:
1851da177e4SLinus Torvalds 		if (pvcc->chan.ppp == NULL) {	/* Not bound yet! */
1861da177e4SLinus Torvalds 			kfree_skb(skb);
1871da177e4SLinus Torvalds 			return;
1881da177e4SLinus Torvalds 		}
1891da177e4SLinus Torvalds 		if (skb->len >= sizeof(pppllc) &&
1901da177e4SLinus Torvalds 		    !memcmp(skb->data, pppllc, sizeof(pppllc))) {
1911da177e4SLinus Torvalds 			pvcc->encaps = e_llc;
1921da177e4SLinus Torvalds 			skb_pull(skb, LLC_LEN);
1931da177e4SLinus Torvalds 			break;
1941da177e4SLinus Torvalds 		}
1951da177e4SLinus Torvalds 		if (skb->len >= (sizeof(pppllc) - LLC_LEN) &&
1961da177e4SLinus Torvalds 		    !memcmp(skb->data, &pppllc[LLC_LEN],
1971da177e4SLinus Torvalds 		    sizeof(pppllc) - LLC_LEN)) {
1981da177e4SLinus Torvalds 			pvcc->encaps = e_vc;
1991da177e4SLinus Torvalds 			pvcc->chan.mtu += LLC_LEN;
2001da177e4SLinus Torvalds 			break;
2011da177e4SLinus Torvalds 		}
20299824461SJoe Perches 		pr_debug("Couldn't autodetect yet (skb: %02X %02X %02X %02X %02X %02X)\n",
2031da177e4SLinus Torvalds 			 skb->data[0], skb->data[1], skb->data[2],
2041da177e4SLinus Torvalds 			 skb->data[3], skb->data[4], skb->data[5]);
2051da177e4SLinus Torvalds 		goto error;
2061da177e4SLinus Torvalds 	case e_vc:
2071da177e4SLinus Torvalds 		break;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 	ppp_input(&pvcc->chan, skb);
2101da177e4SLinus Torvalds 	return;
211d81219dbSJoe Perches 
2121da177e4SLinus Torvalds error:
2131da177e4SLinus Torvalds 	kfree_skb(skb);
2141da177e4SLinus Torvalds 	ppp_input_error(&pvcc->chan, 0);
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2179d02daf7SDavid Woodhouse static inline int pppoatm_may_send(struct pppoatm_vcc *pvcc, int size)
2189d02daf7SDavid Woodhouse {
2199d02daf7SDavid Woodhouse 	/*
2209d02daf7SDavid Woodhouse 	 * It's not clear that we need to bother with using atm_may_send()
2219d02daf7SDavid Woodhouse 	 * to check we don't exceed sk->sk_sndbuf. If userspace sets a
2229d02daf7SDavid Woodhouse 	 * value of sk_sndbuf which is lower than the MTU, we're going to
2239d02daf7SDavid Woodhouse 	 * block for ever. But the code always did that before we introduced
2249d02daf7SDavid Woodhouse 	 * the packet count limit, so...
2259d02daf7SDavid Woodhouse 	 */
2269d02daf7SDavid Woodhouse 	if (atm_may_send(pvcc->atmvcc, size) &&
2279d02daf7SDavid Woodhouse 	    atomic_inc_not_zero_hint(&pvcc->inflight, NONE_INFLIGHT))
2289d02daf7SDavid Woodhouse 		return 1;
2299d02daf7SDavid Woodhouse 
2309d02daf7SDavid Woodhouse 	/*
2319d02daf7SDavid Woodhouse 	 * We use test_and_set_bit() rather than set_bit() here because
2329d02daf7SDavid Woodhouse 	 * we need to ensure there's a memory barrier after it. The bit
2339d02daf7SDavid Woodhouse 	 * *must* be set before we do the atomic_inc() on pvcc->inflight.
2349d02daf7SDavid Woodhouse 	 * There's no smp_mb__after_set_bit(), so it's this or abuse
2359d02daf7SDavid Woodhouse 	 * smp_mb__after_clear_bit().
2369d02daf7SDavid Woodhouse 	 */
2379d02daf7SDavid Woodhouse 	test_and_set_bit(BLOCKED, &pvcc->blocked);
2389d02daf7SDavid Woodhouse 
2399d02daf7SDavid Woodhouse 	/*
2409d02daf7SDavid Woodhouse 	 * We may have raced with pppoatm_pop(). If it ran for the
2419d02daf7SDavid Woodhouse 	 * last packet in the queue, *just* before we set the BLOCKED
2429d02daf7SDavid Woodhouse 	 * bit, then it might never run again and the channel could
2439d02daf7SDavid Woodhouse 	 * remain permanently blocked. Cope with that race by checking
2449d02daf7SDavid Woodhouse 	 * *again*. If it did run in that window, we'll have space on
2459d02daf7SDavid Woodhouse 	 * the queue now and can return success. It's harmless to leave
2469d02daf7SDavid Woodhouse 	 * the BLOCKED flag set, since it's only used as a trigger to
2479d02daf7SDavid Woodhouse 	 * run the wakeup tasklet. Another wakeup will never hurt.
2489d02daf7SDavid Woodhouse 	 * If pppoatm_pop() is running but hasn't got as far as making
2499d02daf7SDavid Woodhouse 	 * space on the queue yet, then it hasn't checked the BLOCKED
2509d02daf7SDavid Woodhouse 	 * flag yet either, so we're safe in that case too. It'll issue
2519d02daf7SDavid Woodhouse 	 * an "immediate" wakeup... where "immediate" actually involves
2529d02daf7SDavid Woodhouse 	 * taking the PPP channel's ->downl lock, which is held by the
2539d02daf7SDavid Woodhouse 	 * code path that calls pppoatm_send(), and is thus going to
2549d02daf7SDavid Woodhouse 	 * wait for us to finish.
2559d02daf7SDavid Woodhouse 	 */
2569d02daf7SDavid Woodhouse 	if (atm_may_send(pvcc->atmvcc, size) &&
2579d02daf7SDavid Woodhouse 	    atomic_inc_not_zero(&pvcc->inflight))
2589d02daf7SDavid Woodhouse 		return 1;
2599d02daf7SDavid Woodhouse 
2609d02daf7SDavid Woodhouse 	return 0;
2619d02daf7SDavid Woodhouse }
2621da177e4SLinus Torvalds /*
2631da177e4SLinus Torvalds  * Called by the ppp_generic.c to send a packet - returns true if packet
2641da177e4SLinus Torvalds  * was accepted.  If we return false, then it's our job to call
2651da177e4SLinus Torvalds  * ppp_output_wakeup(chan) when we're feeling more up to it.
2661da177e4SLinus Torvalds  * Note that in the ENOMEM case (as opposed to the !atm_may_send case)
2671da177e4SLinus Torvalds  * we should really drop the packet, but the generic layer doesn't
2681da177e4SLinus Torvalds  * support this yet.  We just return 'DROP_PACKET' which we actually define
2691da177e4SLinus Torvalds  * as success, just to be clear what we're really doing.
2701da177e4SLinus Torvalds  */
2711da177e4SLinus Torvalds #define DROP_PACKET 1
2721da177e4SLinus Torvalds static int pppoatm_send(struct ppp_channel *chan, struct sk_buff *skb)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct pppoatm_vcc *pvcc = chan_to_pvcc(chan);
2753ac10800SKrzysztof Mazur 	struct atm_vcc *vcc;
2763ac10800SKrzysztof Mazur 	int ret;
2773ac10800SKrzysztof Mazur 
2781da177e4SLinus Torvalds 	ATM_SKB(skb)->vcc = pvcc->atmvcc;
27999824461SJoe Perches 	pr_debug("(skb=0x%p, vcc=0x%p)\n", skb, pvcc->atmvcc);
2801da177e4SLinus Torvalds 	if (skb->data[0] == '\0' && (pvcc->flags & SC_COMP_PROT))
2811da177e4SLinus Torvalds 		(void) skb_pull(skb, 1);
2823ac10800SKrzysztof Mazur 
2833ac10800SKrzysztof Mazur 	vcc = ATM_SKB(skb)->vcc;
2843ac10800SKrzysztof Mazur 	bh_lock_sock(sk_atm(vcc));
2853ac10800SKrzysztof Mazur 	if (sock_owned_by_user(sk_atm(vcc)))
2863ac10800SKrzysztof Mazur 		goto nospace;
287*071d9393SKrzysztof Mazur 	if (test_bit(ATM_VF_RELEASED, &vcc->flags) ||
288*071d9393SKrzysztof Mazur 	    test_bit(ATM_VF_CLOSE, &vcc->flags) ||
289*071d9393SKrzysztof Mazur 	    !test_bit(ATM_VF_READY, &vcc->flags)) {
290*071d9393SKrzysztof Mazur 		bh_unlock_sock(sk_atm(vcc));
291*071d9393SKrzysztof Mazur 		kfree_skb(skb);
292*071d9393SKrzysztof Mazur 		return DROP_PACKET;
293*071d9393SKrzysztof Mazur 	}
2943ac10800SKrzysztof Mazur 
2951da177e4SLinus Torvalds 	switch (pvcc->encaps) {		/* LLC encapsulation needed */
2961da177e4SLinus Torvalds 	case e_llc:
2971da177e4SLinus Torvalds 		if (skb_headroom(skb) < LLC_LEN) {
2981da177e4SLinus Torvalds 			struct sk_buff *n;
2991da177e4SLinus Torvalds 			n = skb_realloc_headroom(skb, LLC_LEN);
3001da177e4SLinus Torvalds 			if (n != NULL &&
3019d02daf7SDavid Woodhouse 			    !pppoatm_may_send(pvcc, n->truesize)) {
3021da177e4SLinus Torvalds 				kfree_skb(n);
3031da177e4SLinus Torvalds 				goto nospace;
3041da177e4SLinus Torvalds 			}
3055d0ba55bSEric Dumazet 			consume_skb(skb);
306d81219dbSJoe Perches 			skb = n;
3073ac10800SKrzysztof Mazur 			if (skb == NULL) {
3083ac10800SKrzysztof Mazur 				bh_unlock_sock(sk_atm(vcc));
3091da177e4SLinus Torvalds 				return DROP_PACKET;
3103ac10800SKrzysztof Mazur 			}
3119d02daf7SDavid Woodhouse 		} else if (!pppoatm_may_send(pvcc, skb->truesize))
3121da177e4SLinus Torvalds 			goto nospace;
3131da177e4SLinus Torvalds 		memcpy(skb_push(skb, LLC_LEN), pppllc, LLC_LEN);
3141da177e4SLinus Torvalds 		break;
3151da177e4SLinus Torvalds 	case e_vc:
3169d02daf7SDavid Woodhouse 		if (!pppoatm_may_send(pvcc, skb->truesize))
3171da177e4SLinus Torvalds 			goto nospace;
3181da177e4SLinus Torvalds 		break;
3191da177e4SLinus Torvalds 	case e_autodetect:
3203ac10800SKrzysztof Mazur 		bh_unlock_sock(sk_atm(vcc));
32152240062SStephen Hemminger 		pr_debug("Trying to send without setting encaps!\n");
3221da177e4SLinus Torvalds 		kfree_skb(skb);
3231da177e4SLinus Torvalds 		return 1;
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	atomic_add(skb->truesize, &sk_atm(ATM_SKB(skb)->vcc)->sk_wmem_alloc);
3271da177e4SLinus Torvalds 	ATM_SKB(skb)->atm_options = ATM_SKB(skb)->vcc->atm_options;
32899824461SJoe Perches 	pr_debug("atm_skb(%p)->vcc(%p)->dev(%p)\n",
32999824461SJoe Perches 		 skb, ATM_SKB(skb)->vcc, ATM_SKB(skb)->vcc->dev);
3303ac10800SKrzysztof Mazur 	ret = ATM_SKB(skb)->vcc->send(ATM_SKB(skb)->vcc, skb)
3311da177e4SLinus Torvalds 	    ? DROP_PACKET : 1;
3323ac10800SKrzysztof Mazur 	bh_unlock_sock(sk_atm(vcc));
3333ac10800SKrzysztof Mazur 	return ret;
3341da177e4SLinus Torvalds nospace:
3353ac10800SKrzysztof Mazur 	bh_unlock_sock(sk_atm(vcc));
3361da177e4SLinus Torvalds 	/*
3371da177e4SLinus Torvalds 	 * We don't have space to send this SKB now, but we might have
3381da177e4SLinus Torvalds 	 * already applied SC_COMP_PROT compression, so may need to undo
3391da177e4SLinus Torvalds 	 */
3401da177e4SLinus Torvalds 	if ((pvcc->flags & SC_COMP_PROT) && skb_headroom(skb) > 0 &&
3411da177e4SLinus Torvalds 	    skb->data[-1] == '\0')
3421da177e4SLinus Torvalds 		(void) skb_push(skb, 1);
3431da177e4SLinus Torvalds 	return 0;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /* This handles ioctls sent to the /dev/ppp interface */
3471da177e4SLinus Torvalds static int pppoatm_devppp_ioctl(struct ppp_channel *chan, unsigned int cmd,
3481da177e4SLinus Torvalds 	unsigned long arg)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds 	switch (cmd) {
3511da177e4SLinus Torvalds 	case PPPIOCGFLAGS:
3521da177e4SLinus Torvalds 		return put_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
3531da177e4SLinus Torvalds 		    ? -EFAULT : 0;
3541da177e4SLinus Torvalds 	case PPPIOCSFLAGS:
3551da177e4SLinus Torvalds 		return get_user(chan_to_pvcc(chan)->flags, (int __user *) arg)
3561da177e4SLinus Torvalds 		    ? -EFAULT : 0;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 	return -ENOTTY;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
361d7100da0Sstephen hemminger static const struct ppp_channel_ops pppoatm_ops = {
3621da177e4SLinus Torvalds 	.start_xmit = pppoatm_send,
3631da177e4SLinus Torvalds 	.ioctl = pppoatm_devppp_ioctl,
3641da177e4SLinus Torvalds };
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds static int pppoatm_assign_vcc(struct atm_vcc *atmvcc, void __user *arg)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct atm_backend_ppp be;
3691da177e4SLinus Torvalds 	struct pppoatm_vcc *pvcc;
3701da177e4SLinus Torvalds 	int err;
3711da177e4SLinus Torvalds 	/*
3721da177e4SLinus Torvalds 	 * Each PPPoATM instance has its own tasklet - this is just a
3731da177e4SLinus Torvalds 	 * prototypical one used to initialize them
3741da177e4SLinus Torvalds 	 */
3751da177e4SLinus Torvalds 	static const DECLARE_TASKLET(tasklet_proto, pppoatm_wakeup_sender, 0);
3761da177e4SLinus Torvalds 	if (copy_from_user(&be, arg, sizeof be))
3771da177e4SLinus Torvalds 		return -EFAULT;
3781da177e4SLinus Torvalds 	if (be.encaps != PPPOATM_ENCAPS_AUTODETECT &&
3791da177e4SLinus Torvalds 	    be.encaps != PPPOATM_ENCAPS_VC && be.encaps != PPPOATM_ENCAPS_LLC)
3801da177e4SLinus Torvalds 		return -EINVAL;
3810da974f4SPanagiotis Issaris 	pvcc = kzalloc(sizeof(*pvcc), GFP_KERNEL);
3821da177e4SLinus Torvalds 	if (pvcc == NULL)
3831da177e4SLinus Torvalds 		return -ENOMEM;
3841da177e4SLinus Torvalds 	pvcc->atmvcc = atmvcc;
3859d02daf7SDavid Woodhouse 
3869d02daf7SDavid Woodhouse 	/* Maximum is zero, so that we can use atomic_inc_not_zero() */
3879d02daf7SDavid Woodhouse 	atomic_set(&pvcc->inflight, NONE_INFLIGHT);
3881da177e4SLinus Torvalds 	pvcc->old_push = atmvcc->push;
3891da177e4SLinus Torvalds 	pvcc->old_pop = atmvcc->pop;
390e41faed9SKrzysztof Mazur 	pvcc->old_owner = atmvcc->owner;
3911da177e4SLinus Torvalds 	pvcc->encaps = (enum pppoatm_encaps) be.encaps;
3921da177e4SLinus Torvalds 	pvcc->chan.private = pvcc;
3931da177e4SLinus Torvalds 	pvcc->chan.ops = &pppoatm_ops;
3941da177e4SLinus Torvalds 	pvcc->chan.mtu = atmvcc->qos.txtp.max_sdu - PPP_HDRLEN -
3951da177e4SLinus Torvalds 	    (be.encaps == e_vc ? 0 : LLC_LEN);
3961da177e4SLinus Torvalds 	pvcc->wakeup_tasklet = tasklet_proto;
3971da177e4SLinus Torvalds 	pvcc->wakeup_tasklet.data = (unsigned long) &pvcc->chan;
398d81219dbSJoe Perches 	err = ppp_register_channel(&pvcc->chan);
399d81219dbSJoe Perches 	if (err != 0) {
4001da177e4SLinus Torvalds 		kfree(pvcc);
4011da177e4SLinus Torvalds 		return err;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	atmvcc->user_back = pvcc;
4041da177e4SLinus Torvalds 	atmvcc->push = pppoatm_push;
4051da177e4SLinus Torvalds 	atmvcc->pop = pppoatm_pop;
4061da177e4SLinus Torvalds 	__module_get(THIS_MODULE);
407e41faed9SKrzysztof Mazur 	atmvcc->owner = THIS_MODULE;
4084e55f578SJorge Boncompte [DTI2] 
4094e55f578SJorge Boncompte [DTI2] 	/* re-process everything received between connection setup and
4104e55f578SJorge Boncompte [DTI2] 	   backend setup */
4114e55f578SJorge Boncompte [DTI2] 	vcc_process_recv_queue(atmvcc);
4121da177e4SLinus Torvalds 	return 0;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds /*
4161da177e4SLinus Torvalds  * This handles ioctls actually performed on our vcc - we must return
4171da177e4SLinus Torvalds  * -ENOIOCTLCMD for any unrecognized ioctl
4181da177e4SLinus Torvalds  */
4191da177e4SLinus Torvalds static int pppoatm_ioctl(struct socket *sock, unsigned int cmd,
4201da177e4SLinus Torvalds 	unsigned long arg)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	struct atm_vcc *atmvcc = ATM_SD(sock);
4231da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	if (cmd != ATM_SETBACKEND && atmvcc->push != pppoatm_push)
4261da177e4SLinus Torvalds 		return -ENOIOCTLCMD;
4271da177e4SLinus Torvalds 	switch (cmd) {
4281da177e4SLinus Torvalds 	case ATM_SETBACKEND: {
4291da177e4SLinus Torvalds 		atm_backend_t b;
4301da177e4SLinus Torvalds 		if (get_user(b, (atm_backend_t __user *) argp))
4311da177e4SLinus Torvalds 			return -EFAULT;
4321da177e4SLinus Torvalds 		if (b != ATM_BACKEND_PPP)
4331da177e4SLinus Torvalds 			return -ENOIOCTLCMD;
4341da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
4351da177e4SLinus Torvalds 			return -EPERM;
4363b1a9145SKrzysztof Mazur 		if (sock->state != SS_CONNECTED)
4373b1a9145SKrzysztof Mazur 			return -EINVAL;
4381da177e4SLinus Torvalds 		return pppoatm_assign_vcc(atmvcc, argp);
4391da177e4SLinus Torvalds 		}
4401da177e4SLinus Torvalds 	case PPPIOCGCHAN:
4411da177e4SLinus Torvalds 		return put_user(ppp_channel_index(&atmvcc_to_pvcc(atmvcc)->
4421da177e4SLinus Torvalds 		    chan), (int __user *) argp) ? -EFAULT : 0;
4431da177e4SLinus Torvalds 	case PPPIOCGUNIT:
4441da177e4SLinus Torvalds 		return put_user(ppp_unit_number(&atmvcc_to_pvcc(atmvcc)->
4451da177e4SLinus Torvalds 		    chan), (int __user *) argp) ? -EFAULT : 0;
4461da177e4SLinus Torvalds 	}
4471da177e4SLinus Torvalds 	return -ENOIOCTLCMD;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds static struct atm_ioctl pppoatm_ioctl_ops = {
4511da177e4SLinus Torvalds 	.owner	= THIS_MODULE,
4521da177e4SLinus Torvalds 	.ioctl	= pppoatm_ioctl,
4531da177e4SLinus Torvalds };
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds static int __init pppoatm_init(void)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	register_atm_ioctl(&pppoatm_ioctl_ops);
4581da177e4SLinus Torvalds 	return 0;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds static void __exit pppoatm_exit(void)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	deregister_atm_ioctl(&pppoatm_ioctl_ops);
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds module_init(pppoatm_init);
4671da177e4SLinus Torvalds module_exit(pppoatm_exit);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds MODULE_AUTHOR("Mitchell Blank Jr <mitch@sfgoth.com>");
4701da177e4SLinus Torvalds MODULE_DESCRIPTION("RFC2364 PPP over ATM/AAL5");
4711da177e4SLinus Torvalds MODULE_LICENSE("GPL");
472