xref: /openbmc/linux/drivers/net/ppp/pppoe.c (revision 89c04d6c)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2224cf5adSJeff Kirsher /** -*- linux-c -*- ***********************************************************
3224cf5adSJeff Kirsher  * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
4224cf5adSJeff Kirsher  *
5224cf5adSJeff Kirsher  * PPPoX --- Generic PPP encapsulation socket family
6224cf5adSJeff Kirsher  * PPPoE --- PPP over Ethernet (RFC 2516)
7224cf5adSJeff Kirsher  *
8224cf5adSJeff Kirsher  * Version:	0.7.0
9224cf5adSJeff Kirsher  *
10224cf5adSJeff Kirsher  * 070228 :	Fix to allow multiple sessions with same remote MAC and same
11224cf5adSJeff Kirsher  *		session id by including the local device ifindex in the
12224cf5adSJeff Kirsher  *		tuple identifying a session. This also ensures packets can't
13224cf5adSJeff Kirsher  *		be injected into a session from interfaces other than the one
14224cf5adSJeff Kirsher  *		specified by userspace. Florian Zumbiehl <florz@florz.de>
15224cf5adSJeff Kirsher  *		(Oh, BTW, this one is YYMMDD, in case you were wondering ...)
16224cf5adSJeff Kirsher  * 220102 :	Fix module use count on failure in pppoe_create, pppox_sk -acme
17224cf5adSJeff Kirsher  * 030700 :	Fixed connect logic to allow for disconnect.
18224cf5adSJeff Kirsher  * 270700 :	Fixed potential SMP problems; we must protect against
19224cf5adSJeff Kirsher  *		simultaneous invocation of ppp_input
20224cf5adSJeff Kirsher  *		and ppp_unregister_channel.
21224cf5adSJeff Kirsher  * 040800 :	Respect reference count mechanisms on net-devices.
22224cf5adSJeff Kirsher  * 200800 :	fix kfree(skb) in pppoe_rcv (acme)
23224cf5adSJeff Kirsher  *		Module reference count is decremented in the right spot now,
24224cf5adSJeff Kirsher  *		guards against sock_put not actually freeing the sk
25224cf5adSJeff Kirsher  *		in pppoe_release.
26224cf5adSJeff Kirsher  * 051000 :	Initialization cleanup.
27224cf5adSJeff Kirsher  * 111100 :	Fix recvmsg.
2873a2218cSBhaskar Chowdhury  * 050101 :	Fix PADT processing.
29224cf5adSJeff Kirsher  * 140501 :	Use pppoe_rcv_core to handle all backlog. (Alexey)
30224cf5adSJeff Kirsher  * 170701 :	Do not lock_sock with rwlock held. (DaveM)
31224cf5adSJeff Kirsher  *		Ignore discovery frames if user has socket
32224cf5adSJeff Kirsher  *		locked. (DaveM)
33224cf5adSJeff Kirsher  *		Ignore return value of dev_queue_xmit in __pppoe_xmit
34224cf5adSJeff Kirsher  *		or else we may kfree an SKB twice. (DaveM)
35224cf5adSJeff Kirsher  * 190701 :	When doing copies of skb's in __pppoe_xmit, always delete
36224cf5adSJeff Kirsher  *		the original skb that was passed in on success, never on
37224cf5adSJeff Kirsher  *		failure.  Delete the copy of the skb on failure to avoid
38224cf5adSJeff Kirsher  *		a memory leak.
39224cf5adSJeff Kirsher  * 081001 :	Misc. cleanup (licence string, non-blocking, prevent
40224cf5adSJeff Kirsher  *		reference of device on close).
41224cf5adSJeff Kirsher  * 121301 :	New ppp channels interface; cannot unregister a channel
42224cf5adSJeff Kirsher  *		from interrupts.  Thus, we mark the socket as a ZOMBIE
43224cf5adSJeff Kirsher  *		and do the unregistration later.
44224cf5adSJeff Kirsher  * 081002 :	seq_file support for proc stuff -acme
45224cf5adSJeff Kirsher  * 111602 :	Merge all 2.4 fixes into 2.5/2.6 tree.  Label 2.5/2.6
46224cf5adSJeff Kirsher  *		as version 0.7.  Spacing cleanup.
47224cf5adSJeff Kirsher  * Author:	Michal Ostrowski <mostrows@speakeasy.net>
48224cf5adSJeff Kirsher  * Contributors:
49224cf5adSJeff Kirsher  * 		Arnaldo Carvalho de Melo <acme@conectiva.com.br>
50224cf5adSJeff Kirsher  *		David S. Miller (davem@redhat.com)
51224cf5adSJeff Kirsher  *
52224cf5adSJeff Kirsher  * License:
53224cf5adSJeff Kirsher  */
54224cf5adSJeff Kirsher 
55224cf5adSJeff Kirsher #include <linux/string.h>
56224cf5adSJeff Kirsher #include <linux/module.h>
57224cf5adSJeff Kirsher #include <linux/kernel.h>
58224cf5adSJeff Kirsher #include <linux/slab.h>
59224cf5adSJeff Kirsher #include <linux/errno.h>
60224cf5adSJeff Kirsher #include <linux/netdevice.h>
61224cf5adSJeff Kirsher #include <linux/net.h>
62224cf5adSJeff Kirsher #include <linux/inetdevice.h>
63224cf5adSJeff Kirsher #include <linux/etherdevice.h>
64224cf5adSJeff Kirsher #include <linux/skbuff.h>
65224cf5adSJeff Kirsher #include <linux/init.h>
66224cf5adSJeff Kirsher #include <linux/if_ether.h>
67224cf5adSJeff Kirsher #include <linux/if_pppox.h>
68224cf5adSJeff Kirsher #include <linux/ppp_channel.h>
69224cf5adSJeff Kirsher #include <linux/ppp_defs.h>
704b32da2bSPaul Mackerras #include <linux/ppp-ioctl.h>
71224cf5adSJeff Kirsher #include <linux/notifier.h>
72224cf5adSJeff Kirsher #include <linux/file.h>
73224cf5adSJeff Kirsher #include <linux/proc_fs.h>
74224cf5adSJeff Kirsher #include <linux/seq_file.h>
75224cf5adSJeff Kirsher 
76224cf5adSJeff Kirsher #include <linux/nsproxy.h>
77224cf5adSJeff Kirsher #include <net/net_namespace.h>
78224cf5adSJeff Kirsher #include <net/netns/generic.h>
79224cf5adSJeff Kirsher #include <net/sock.h>
80224cf5adSJeff Kirsher 
817c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
82224cf5adSJeff Kirsher 
8396ba44c6SJaco Kroon #define PPPOE_HASH_BITS CONFIG_PPPOE_HASH_BITS
84224cf5adSJeff Kirsher #define PPPOE_HASH_SIZE (1 << PPPOE_HASH_BITS)
85224cf5adSJeff Kirsher #define PPPOE_HASH_MASK	(PPPOE_HASH_SIZE - 1)
86224cf5adSJeff Kirsher 
87224cf5adSJeff Kirsher static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
88224cf5adSJeff Kirsher 
89224cf5adSJeff Kirsher static const struct proto_ops pppoe_ops;
90224cf5adSJeff Kirsher static const struct ppp_channel_ops pppoe_chan_ops;
91224cf5adSJeff Kirsher 
92224cf5adSJeff Kirsher /* per-net private data for this module */
93c7d03a00SAlexey Dobriyan static unsigned int pppoe_net_id __read_mostly;
94224cf5adSJeff Kirsher struct pppoe_net {
95224cf5adSJeff Kirsher 	/*
96224cf5adSJeff Kirsher 	 * we could use _single_ hash table for all
97224cf5adSJeff Kirsher 	 * nets by injecting net id into the hash but
98224cf5adSJeff Kirsher 	 * it would increase hash chains and add
9973a2218cSBhaskar Chowdhury 	 * a few additional math comparisons messy
100224cf5adSJeff Kirsher 	 * as well, moreover in case of SMP less locking
101224cf5adSJeff Kirsher 	 * controversy here
102224cf5adSJeff Kirsher 	 */
103224cf5adSJeff Kirsher 	struct pppox_sock *hash_table[PPPOE_HASH_SIZE];
104224cf5adSJeff Kirsher 	rwlock_t hash_lock;
105224cf5adSJeff Kirsher };
106224cf5adSJeff Kirsher 
107224cf5adSJeff Kirsher /*
108224cf5adSJeff Kirsher  * PPPoE could be in the following stages:
109224cf5adSJeff Kirsher  * 1) Discovery stage (to obtain remote MAC and Session ID)
110224cf5adSJeff Kirsher  * 2) Session stage (MAC and SID are known)
111224cf5adSJeff Kirsher  *
112224cf5adSJeff Kirsher  * Ethernet frames have a special tag for this but
113224cf5adSJeff Kirsher  * we use simpler approach based on session id
114224cf5adSJeff Kirsher  */
stage_session(__be16 sid)115224cf5adSJeff Kirsher static inline bool stage_session(__be16 sid)
116224cf5adSJeff Kirsher {
117224cf5adSJeff Kirsher 	return sid != 0;
118224cf5adSJeff Kirsher }
119224cf5adSJeff Kirsher 
pppoe_pernet(struct net * net)120224cf5adSJeff Kirsher static inline struct pppoe_net *pppoe_pernet(struct net *net)
121224cf5adSJeff Kirsher {
122224cf5adSJeff Kirsher 	return net_generic(net, pppoe_net_id);
123224cf5adSJeff Kirsher }
124224cf5adSJeff Kirsher 
cmp_2_addr(struct pppoe_addr * a,struct pppoe_addr * b)125224cf5adSJeff Kirsher static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
126224cf5adSJeff Kirsher {
1277ed8ca5bSdingtianhong 	return a->sid == b->sid && ether_addr_equal(a->remote, b->remote);
128224cf5adSJeff Kirsher }
129224cf5adSJeff Kirsher 
cmp_addr(struct pppoe_addr * a,__be16 sid,char * addr)130224cf5adSJeff Kirsher static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr)
131224cf5adSJeff Kirsher {
1327ed8ca5bSdingtianhong 	return a->sid == sid && ether_addr_equal(a->remote, addr);
133224cf5adSJeff Kirsher }
134224cf5adSJeff Kirsher 
135224cf5adSJeff Kirsher #if 8 % PPPOE_HASH_BITS
136224cf5adSJeff Kirsher #error 8 must be a multiple of PPPOE_HASH_BITS
137224cf5adSJeff Kirsher #endif
138224cf5adSJeff Kirsher 
hash_item(__be16 sid,unsigned char * addr)139224cf5adSJeff Kirsher static int hash_item(__be16 sid, unsigned char *addr)
140224cf5adSJeff Kirsher {
141224cf5adSJeff Kirsher 	unsigned char hash = 0;
142224cf5adSJeff Kirsher 	unsigned int i;
143224cf5adSJeff Kirsher 
144224cf5adSJeff Kirsher 	for (i = 0; i < ETH_ALEN; i++)
145224cf5adSJeff Kirsher 		hash ^= addr[i];
146224cf5adSJeff Kirsher 	for (i = 0; i < sizeof(sid_t) * 8; i += 8)
147224cf5adSJeff Kirsher 		hash ^= (__force __u32)sid >> i;
148224cf5adSJeff Kirsher 	for (i = 8; (i >>= 1) >= PPPOE_HASH_BITS;)
149224cf5adSJeff Kirsher 		hash ^= hash >> i;
150224cf5adSJeff Kirsher 
151224cf5adSJeff Kirsher 	return hash & PPPOE_HASH_MASK;
152224cf5adSJeff Kirsher }
153224cf5adSJeff Kirsher 
154224cf5adSJeff Kirsher /**********************************************************************
155224cf5adSJeff Kirsher  *
156224cf5adSJeff Kirsher  *  Set/get/delete/rehash items  (internal versions)
157224cf5adSJeff Kirsher  *
158224cf5adSJeff Kirsher  **********************************************************************/
__get_item(struct pppoe_net * pn,__be16 sid,unsigned char * addr,int ifindex)159224cf5adSJeff Kirsher static struct pppox_sock *__get_item(struct pppoe_net *pn, __be16 sid,
160224cf5adSJeff Kirsher 				unsigned char *addr, int ifindex)
161224cf5adSJeff Kirsher {
162224cf5adSJeff Kirsher 	int hash = hash_item(sid, addr);
163224cf5adSJeff Kirsher 	struct pppox_sock *ret;
164224cf5adSJeff Kirsher 
165224cf5adSJeff Kirsher 	ret = pn->hash_table[hash];
166224cf5adSJeff Kirsher 	while (ret) {
167224cf5adSJeff Kirsher 		if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
168224cf5adSJeff Kirsher 		    ret->pppoe_ifindex == ifindex)
169224cf5adSJeff Kirsher 			return ret;
170224cf5adSJeff Kirsher 
171224cf5adSJeff Kirsher 		ret = ret->next;
172224cf5adSJeff Kirsher 	}
173224cf5adSJeff Kirsher 
174224cf5adSJeff Kirsher 	return NULL;
175224cf5adSJeff Kirsher }
176224cf5adSJeff Kirsher 
__set_item(struct pppoe_net * pn,struct pppox_sock * po)177224cf5adSJeff Kirsher static int __set_item(struct pppoe_net *pn, struct pppox_sock *po)
178224cf5adSJeff Kirsher {
179224cf5adSJeff Kirsher 	int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
180224cf5adSJeff Kirsher 	struct pppox_sock *ret;
181224cf5adSJeff Kirsher 
182224cf5adSJeff Kirsher 	ret = pn->hash_table[hash];
183224cf5adSJeff Kirsher 	while (ret) {
184224cf5adSJeff Kirsher 		if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) &&
185224cf5adSJeff Kirsher 		    ret->pppoe_ifindex == po->pppoe_ifindex)
186224cf5adSJeff Kirsher 			return -EALREADY;
187224cf5adSJeff Kirsher 
188224cf5adSJeff Kirsher 		ret = ret->next;
189224cf5adSJeff Kirsher 	}
190224cf5adSJeff Kirsher 
191224cf5adSJeff Kirsher 	po->next = pn->hash_table[hash];
192224cf5adSJeff Kirsher 	pn->hash_table[hash] = po;
193224cf5adSJeff Kirsher 
194224cf5adSJeff Kirsher 	return 0;
195224cf5adSJeff Kirsher }
196224cf5adSJeff Kirsher 
__delete_item(struct pppoe_net * pn,__be16 sid,char * addr,int ifindex)1973b12bb60SRami Rosen static void __delete_item(struct pppoe_net *pn, __be16 sid,
198224cf5adSJeff Kirsher 					char *addr, int ifindex)
199224cf5adSJeff Kirsher {
200224cf5adSJeff Kirsher 	int hash = hash_item(sid, addr);
201224cf5adSJeff Kirsher 	struct pppox_sock *ret, **src;
202224cf5adSJeff Kirsher 
203224cf5adSJeff Kirsher 	ret = pn->hash_table[hash];
204224cf5adSJeff Kirsher 	src = &pn->hash_table[hash];
205224cf5adSJeff Kirsher 
206224cf5adSJeff Kirsher 	while (ret) {
207224cf5adSJeff Kirsher 		if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
208224cf5adSJeff Kirsher 		    ret->pppoe_ifindex == ifindex) {
209224cf5adSJeff Kirsher 			*src = ret->next;
210224cf5adSJeff Kirsher 			break;
211224cf5adSJeff Kirsher 		}
212224cf5adSJeff Kirsher 
213224cf5adSJeff Kirsher 		src = &ret->next;
214224cf5adSJeff Kirsher 		ret = ret->next;
215224cf5adSJeff Kirsher 	}
216224cf5adSJeff Kirsher }
217224cf5adSJeff Kirsher 
218224cf5adSJeff Kirsher /**********************************************************************
219224cf5adSJeff Kirsher  *
220224cf5adSJeff Kirsher  *  Set/get/delete/rehash items
221224cf5adSJeff Kirsher  *
222224cf5adSJeff Kirsher  **********************************************************************/
get_item(struct pppoe_net * pn,__be16 sid,unsigned char * addr,int ifindex)223224cf5adSJeff Kirsher static inline struct pppox_sock *get_item(struct pppoe_net *pn, __be16 sid,
224224cf5adSJeff Kirsher 					unsigned char *addr, int ifindex)
225224cf5adSJeff Kirsher {
226224cf5adSJeff Kirsher 	struct pppox_sock *po;
227224cf5adSJeff Kirsher 
228224cf5adSJeff Kirsher 	read_lock_bh(&pn->hash_lock);
229224cf5adSJeff Kirsher 	po = __get_item(pn, sid, addr, ifindex);
230224cf5adSJeff Kirsher 	if (po)
231224cf5adSJeff Kirsher 		sock_hold(sk_pppox(po));
232224cf5adSJeff Kirsher 	read_unlock_bh(&pn->hash_lock);
233224cf5adSJeff Kirsher 
234224cf5adSJeff Kirsher 	return po;
235224cf5adSJeff Kirsher }
236224cf5adSJeff Kirsher 
get_item_by_addr(struct net * net,struct sockaddr_pppox * sp)237224cf5adSJeff Kirsher static inline struct pppox_sock *get_item_by_addr(struct net *net,
238224cf5adSJeff Kirsher 						struct sockaddr_pppox *sp)
239224cf5adSJeff Kirsher {
240224cf5adSJeff Kirsher 	struct net_device *dev;
241224cf5adSJeff Kirsher 	struct pppoe_net *pn;
242224cf5adSJeff Kirsher 	struct pppox_sock *pppox_sock = NULL;
243224cf5adSJeff Kirsher 
244224cf5adSJeff Kirsher 	int ifindex;
245224cf5adSJeff Kirsher 
246224cf5adSJeff Kirsher 	rcu_read_lock();
247224cf5adSJeff Kirsher 	dev = dev_get_by_name_rcu(net, sp->sa_addr.pppoe.dev);
248224cf5adSJeff Kirsher 	if (dev) {
249224cf5adSJeff Kirsher 		ifindex = dev->ifindex;
250224cf5adSJeff Kirsher 		pn = pppoe_pernet(net);
251224cf5adSJeff Kirsher 		pppox_sock = get_item(pn, sp->sa_addr.pppoe.sid,
252224cf5adSJeff Kirsher 				sp->sa_addr.pppoe.remote, ifindex);
253224cf5adSJeff Kirsher 	}
254224cf5adSJeff Kirsher 	rcu_read_unlock();
255224cf5adSJeff Kirsher 	return pppox_sock;
256224cf5adSJeff Kirsher }
257224cf5adSJeff Kirsher 
delete_item(struct pppoe_net * pn,__be16 sid,char * addr,int ifindex)2583b12bb60SRami Rosen static inline void delete_item(struct pppoe_net *pn, __be16 sid,
259224cf5adSJeff Kirsher 					char *addr, int ifindex)
260224cf5adSJeff Kirsher {
261224cf5adSJeff Kirsher 	write_lock_bh(&pn->hash_lock);
2623b12bb60SRami Rosen 	__delete_item(pn, sid, addr, ifindex);
263224cf5adSJeff Kirsher 	write_unlock_bh(&pn->hash_lock);
264224cf5adSJeff Kirsher }
265224cf5adSJeff Kirsher 
266224cf5adSJeff Kirsher /***************************************************************************
267224cf5adSJeff Kirsher  *
268224cf5adSJeff Kirsher  *  Handler for device events.
269224cf5adSJeff Kirsher  *  Certain device events require that sockets be unconnected.
270224cf5adSJeff Kirsher  *
271224cf5adSJeff Kirsher  **************************************************************************/
272224cf5adSJeff Kirsher 
pppoe_flush_dev(struct net_device * dev)273224cf5adSJeff Kirsher static void pppoe_flush_dev(struct net_device *dev)
274224cf5adSJeff Kirsher {
275224cf5adSJeff Kirsher 	struct pppoe_net *pn;
276224cf5adSJeff Kirsher 	int i;
277224cf5adSJeff Kirsher 
278224cf5adSJeff Kirsher 	pn = pppoe_pernet(dev_net(dev));
279224cf5adSJeff Kirsher 	write_lock_bh(&pn->hash_lock);
280224cf5adSJeff Kirsher 	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
281224cf5adSJeff Kirsher 		struct pppox_sock *po = pn->hash_table[i];
282224cf5adSJeff Kirsher 		struct sock *sk;
283224cf5adSJeff Kirsher 
284224cf5adSJeff Kirsher 		while (po) {
285224cf5adSJeff Kirsher 			while (po && po->pppoe_dev != dev) {
286224cf5adSJeff Kirsher 				po = po->next;
287224cf5adSJeff Kirsher 			}
288224cf5adSJeff Kirsher 
289224cf5adSJeff Kirsher 			if (!po)
290224cf5adSJeff Kirsher 				break;
291224cf5adSJeff Kirsher 
292224cf5adSJeff Kirsher 			sk = sk_pppox(po);
293224cf5adSJeff Kirsher 
294224cf5adSJeff Kirsher 			/* We always grab the socket lock, followed by the
295224cf5adSJeff Kirsher 			 * hash_lock, in that order.  Since we should hold the
296224cf5adSJeff Kirsher 			 * sock lock while doing any unbinding, we need to
297224cf5adSJeff Kirsher 			 * release the lock we're holding.  Hold a reference to
298224cf5adSJeff Kirsher 			 * the sock so it doesn't disappear as we're jumping
299224cf5adSJeff Kirsher 			 * between locks.
300224cf5adSJeff Kirsher 			 */
301224cf5adSJeff Kirsher 
302224cf5adSJeff Kirsher 			sock_hold(sk);
303224cf5adSJeff Kirsher 			write_unlock_bh(&pn->hash_lock);
304224cf5adSJeff Kirsher 			lock_sock(sk);
305224cf5adSJeff Kirsher 
306224cf5adSJeff Kirsher 			if (po->pppoe_dev == dev &&
307a8acce6aSGuillaume Nault 			    sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
308224cf5adSJeff Kirsher 				pppox_unbind_sock(sk);
309224cf5adSJeff Kirsher 				sk->sk_state_change(sk);
310224cf5adSJeff Kirsher 				po->pppoe_dev = NULL;
311224cf5adSJeff Kirsher 				dev_put(dev);
312224cf5adSJeff Kirsher 			}
313224cf5adSJeff Kirsher 
314224cf5adSJeff Kirsher 			release_sock(sk);
315224cf5adSJeff Kirsher 			sock_put(sk);
316224cf5adSJeff Kirsher 
317224cf5adSJeff Kirsher 			/* Restart the process from the start of the current
318224cf5adSJeff Kirsher 			 * hash chain. We dropped locks so the world may have
319224cf5adSJeff Kirsher 			 * change from underneath us.
320224cf5adSJeff Kirsher 			 */
321224cf5adSJeff Kirsher 
322224cf5adSJeff Kirsher 			BUG_ON(pppoe_pernet(dev_net(dev)) == NULL);
323224cf5adSJeff Kirsher 			write_lock_bh(&pn->hash_lock);
324224cf5adSJeff Kirsher 			po = pn->hash_table[i];
325224cf5adSJeff Kirsher 		}
326224cf5adSJeff Kirsher 	}
327224cf5adSJeff Kirsher 	write_unlock_bh(&pn->hash_lock);
328224cf5adSJeff Kirsher }
329224cf5adSJeff Kirsher 
pppoe_device_event(struct notifier_block * this,unsigned long event,void * ptr)330224cf5adSJeff Kirsher static int pppoe_device_event(struct notifier_block *this,
331224cf5adSJeff Kirsher 			      unsigned long event, void *ptr)
332224cf5adSJeff Kirsher {
333351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
334224cf5adSJeff Kirsher 
335224cf5adSJeff Kirsher 	/* Only look at sockets that are using this specific device. */
336224cf5adSJeff Kirsher 	switch (event) {
337224cf5adSJeff Kirsher 	case NETDEV_CHANGEADDR:
338224cf5adSJeff Kirsher 	case NETDEV_CHANGEMTU:
339224cf5adSJeff Kirsher 		/* A change in mtu or address is a bad thing, requiring
340224cf5adSJeff Kirsher 		 * LCP re-negotiation.
341224cf5adSJeff Kirsher 		 */
342224cf5adSJeff Kirsher 
343224cf5adSJeff Kirsher 	case NETDEV_GOING_DOWN:
344224cf5adSJeff Kirsher 	case NETDEV_DOWN:
345224cf5adSJeff Kirsher 		/* Find every socket on this device and kill it. */
346224cf5adSJeff Kirsher 		pppoe_flush_dev(dev);
347224cf5adSJeff Kirsher 		break;
348224cf5adSJeff Kirsher 
349224cf5adSJeff Kirsher 	default:
350224cf5adSJeff Kirsher 		break;
351224cf5adSJeff Kirsher 	}
352224cf5adSJeff Kirsher 
353224cf5adSJeff Kirsher 	return NOTIFY_DONE;
354224cf5adSJeff Kirsher }
355224cf5adSJeff Kirsher 
356224cf5adSJeff Kirsher static struct notifier_block pppoe_notifier = {
357224cf5adSJeff Kirsher 	.notifier_call = pppoe_device_event,
358224cf5adSJeff Kirsher };
359224cf5adSJeff Kirsher 
360224cf5adSJeff Kirsher /************************************************************************
361224cf5adSJeff Kirsher  *
362224cf5adSJeff Kirsher  * Do the real work of receiving a PPPoE Session frame.
363224cf5adSJeff Kirsher  *
364224cf5adSJeff Kirsher  ***********************************************************************/
pppoe_rcv_core(struct sock * sk,struct sk_buff * skb)365224cf5adSJeff Kirsher static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
366224cf5adSJeff Kirsher {
367224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
368224cf5adSJeff Kirsher 	struct pppox_sock *relay_po;
369224cf5adSJeff Kirsher 
370224cf5adSJeff Kirsher 	/* Backlog receive. Semantics of backlog rcv preclude any code from
371224cf5adSJeff Kirsher 	 * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state
372224cf5adSJeff Kirsher 	 * can't change.
373224cf5adSJeff Kirsher 	 */
374224cf5adSJeff Kirsher 
375a068833bSJoakim Tjernlund 	if (skb->pkt_type == PACKET_OTHERHOST)
376a068833bSJoakim Tjernlund 		goto abort_kfree;
377a068833bSJoakim Tjernlund 
378224cf5adSJeff Kirsher 	if (sk->sk_state & PPPOX_BOUND) {
379224cf5adSJeff Kirsher 		ppp_input(&po->chan, skb);
380224cf5adSJeff Kirsher 	} else if (sk->sk_state & PPPOX_RELAY) {
381224cf5adSJeff Kirsher 		relay_po = get_item_by_addr(sock_net(sk),
382224cf5adSJeff Kirsher 					    &po->pppoe_relay);
383224cf5adSJeff Kirsher 		if (relay_po == NULL)
384224cf5adSJeff Kirsher 			goto abort_kfree;
385224cf5adSJeff Kirsher 
386224cf5adSJeff Kirsher 		if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
387224cf5adSJeff Kirsher 			goto abort_put;
388224cf5adSJeff Kirsher 
389224cf5adSJeff Kirsher 		if (!__pppoe_xmit(sk_pppox(relay_po), skb))
390224cf5adSJeff Kirsher 			goto abort_put;
39129e73269SGuillaume Nault 
39229e73269SGuillaume Nault 		sock_put(sk_pppox(relay_po));
393224cf5adSJeff Kirsher 	} else {
394224cf5adSJeff Kirsher 		if (sock_queue_rcv_skb(sk, skb))
395224cf5adSJeff Kirsher 			goto abort_kfree;
396224cf5adSJeff Kirsher 	}
397224cf5adSJeff Kirsher 
398224cf5adSJeff Kirsher 	return NET_RX_SUCCESS;
399224cf5adSJeff Kirsher 
400224cf5adSJeff Kirsher abort_put:
401224cf5adSJeff Kirsher 	sock_put(sk_pppox(relay_po));
402224cf5adSJeff Kirsher 
403224cf5adSJeff Kirsher abort_kfree:
404224cf5adSJeff Kirsher 	kfree_skb(skb);
405224cf5adSJeff Kirsher 	return NET_RX_DROP;
406224cf5adSJeff Kirsher }
407224cf5adSJeff Kirsher 
408224cf5adSJeff Kirsher /************************************************************************
409224cf5adSJeff Kirsher  *
410224cf5adSJeff Kirsher  * Receive wrapper called in BH context.
411224cf5adSJeff Kirsher  *
412224cf5adSJeff Kirsher  ***********************************************************************/
pppoe_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)413224cf5adSJeff Kirsher static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev,
414224cf5adSJeff Kirsher 		     struct packet_type *pt, struct net_device *orig_dev)
415224cf5adSJeff Kirsher {
416224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
417224cf5adSJeff Kirsher 	struct pppox_sock *po;
418224cf5adSJeff Kirsher 	struct pppoe_net *pn;
419224cf5adSJeff Kirsher 	int len;
420224cf5adSJeff Kirsher 
421224cf5adSJeff Kirsher 	skb = skb_share_check(skb, GFP_ATOMIC);
422224cf5adSJeff Kirsher 	if (!skb)
423224cf5adSJeff Kirsher 		goto out;
424224cf5adSJeff Kirsher 
4258540827eSGuillaume Nault 	if (skb_mac_header_len(skb) < ETH_HLEN)
4268540827eSGuillaume Nault 		goto drop;
4278540827eSGuillaume Nault 
428224cf5adSJeff Kirsher 	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
429224cf5adSJeff Kirsher 		goto drop;
430224cf5adSJeff Kirsher 
431224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
432224cf5adSJeff Kirsher 	len = ntohs(ph->length);
433224cf5adSJeff Kirsher 
434224cf5adSJeff Kirsher 	skb_pull_rcsum(skb, sizeof(*ph));
435224cf5adSJeff Kirsher 	if (skb->len < len)
436224cf5adSJeff Kirsher 		goto drop;
437224cf5adSJeff Kirsher 
438224cf5adSJeff Kirsher 	if (pskb_trim_rcsum(skb, len))
439224cf5adSJeff Kirsher 		goto drop;
440224cf5adSJeff Kirsher 
4416c57f045SRoss Lagerwall 	ph = pppoe_hdr(skb);
442224cf5adSJeff Kirsher 	pn = pppoe_pernet(dev_net(dev));
443224cf5adSJeff Kirsher 
444224cf5adSJeff Kirsher 	/* Note that get_item does a sock_hold(), so sk_pppox(po)
445224cf5adSJeff Kirsher 	 * is known to be safe.
446224cf5adSJeff Kirsher 	 */
447224cf5adSJeff Kirsher 	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
448224cf5adSJeff Kirsher 	if (!po)
449224cf5adSJeff Kirsher 		goto drop;
450224cf5adSJeff Kirsher 
451224cf5adSJeff Kirsher 	return sk_receive_skb(sk_pppox(po), skb, 0);
452224cf5adSJeff Kirsher 
453224cf5adSJeff Kirsher drop:
454224cf5adSJeff Kirsher 	kfree_skb(skb);
455224cf5adSJeff Kirsher out:
456224cf5adSJeff Kirsher 	return NET_RX_DROP;
457224cf5adSJeff Kirsher }
458224cf5adSJeff Kirsher 
pppoe_unbind_sock_work(struct work_struct * work)459287f3a94SSimon Farnsworth static void pppoe_unbind_sock_work(struct work_struct *work)
460287f3a94SSimon Farnsworth {
461287f3a94SSimon Farnsworth 	struct pppox_sock *po = container_of(work, struct pppox_sock,
462287f3a94SSimon Farnsworth 					     proto.pppoe.padt_work);
463287f3a94SSimon Farnsworth 	struct sock *sk = sk_pppox(po);
464287f3a94SSimon Farnsworth 
465287f3a94SSimon Farnsworth 	lock_sock(sk);
466665a6cd8SFelix Fietkau 	if (po->pppoe_dev) {
467665a6cd8SFelix Fietkau 		dev_put(po->pppoe_dev);
468665a6cd8SFelix Fietkau 		po->pppoe_dev = NULL;
469665a6cd8SFelix Fietkau 	}
470287f3a94SSimon Farnsworth 	pppox_unbind_sock(sk);
471287f3a94SSimon Farnsworth 	release_sock(sk);
472287f3a94SSimon Farnsworth 	sock_put(sk);
473287f3a94SSimon Farnsworth }
474287f3a94SSimon Farnsworth 
475224cf5adSJeff Kirsher /************************************************************************
476224cf5adSJeff Kirsher  *
477224cf5adSJeff Kirsher  * Receive a PPPoE Discovery frame.
478224cf5adSJeff Kirsher  * This is solely for detection of PADT frames
479224cf5adSJeff Kirsher  *
480224cf5adSJeff Kirsher  ***********************************************************************/
pppoe_disc_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)481224cf5adSJeff Kirsher static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev,
482224cf5adSJeff Kirsher 			  struct packet_type *pt, struct net_device *orig_dev)
483224cf5adSJeff Kirsher 
484224cf5adSJeff Kirsher {
485224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
486224cf5adSJeff Kirsher 	struct pppox_sock *po;
487224cf5adSJeff Kirsher 	struct pppoe_net *pn;
488224cf5adSJeff Kirsher 
489224cf5adSJeff Kirsher 	skb = skb_share_check(skb, GFP_ATOMIC);
490224cf5adSJeff Kirsher 	if (!skb)
491224cf5adSJeff Kirsher 		goto out;
492224cf5adSJeff Kirsher 
493b8c15839SGuillaume Nault 	if (skb->pkt_type != PACKET_HOST)
494b8c15839SGuillaume Nault 		goto abort;
495b8c15839SGuillaume Nault 
496224cf5adSJeff Kirsher 	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
497224cf5adSJeff Kirsher 		goto abort;
498224cf5adSJeff Kirsher 
499224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
500224cf5adSJeff Kirsher 	if (ph->code != PADT_CODE)
501224cf5adSJeff Kirsher 		goto abort;
502224cf5adSJeff Kirsher 
503224cf5adSJeff Kirsher 	pn = pppoe_pernet(dev_net(dev));
504224cf5adSJeff Kirsher 	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
5058734e485SGuillaume Nault 	if (po)
506287f3a94SSimon Farnsworth 		if (!schedule_work(&po->proto.pppoe.padt_work))
5078734e485SGuillaume Nault 			sock_put(sk_pppox(po));
508224cf5adSJeff Kirsher 
509224cf5adSJeff Kirsher abort:
510224cf5adSJeff Kirsher 	kfree_skb(skb);
511224cf5adSJeff Kirsher out:
512224cf5adSJeff Kirsher 	return NET_RX_SUCCESS; /* Lies... :-) */
513224cf5adSJeff Kirsher }
514224cf5adSJeff Kirsher 
515224cf5adSJeff Kirsher static struct packet_type pppoes_ptype __read_mostly = {
516224cf5adSJeff Kirsher 	.type	= cpu_to_be16(ETH_P_PPP_SES),
517224cf5adSJeff Kirsher 	.func	= pppoe_rcv,
518224cf5adSJeff Kirsher };
519224cf5adSJeff Kirsher 
520224cf5adSJeff Kirsher static struct packet_type pppoed_ptype __read_mostly = {
521224cf5adSJeff Kirsher 	.type	= cpu_to_be16(ETH_P_PPP_DISC),
522224cf5adSJeff Kirsher 	.func	= pppoe_disc_rcv,
523224cf5adSJeff Kirsher };
524224cf5adSJeff Kirsher 
525224cf5adSJeff Kirsher static struct proto pppoe_sk_proto __read_mostly = {
526224cf5adSJeff Kirsher 	.name	  = "PPPOE",
527224cf5adSJeff Kirsher 	.owner	  = THIS_MODULE,
528224cf5adSJeff Kirsher 	.obj_size = sizeof(struct pppox_sock),
529224cf5adSJeff Kirsher };
530224cf5adSJeff Kirsher 
531224cf5adSJeff Kirsher /***********************************************************************
532224cf5adSJeff Kirsher  *
533224cf5adSJeff Kirsher  * Initialize a new struct sock.
534224cf5adSJeff Kirsher  *
535224cf5adSJeff Kirsher  **********************************************************************/
pppoe_create(struct net * net,struct socket * sock,int kern)53611aa9c28SEric W. Biederman static int pppoe_create(struct net *net, struct socket *sock, int kern)
537224cf5adSJeff Kirsher {
538224cf5adSJeff Kirsher 	struct sock *sk;
539224cf5adSJeff Kirsher 
54011aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, kern);
541224cf5adSJeff Kirsher 	if (!sk)
542224cf5adSJeff Kirsher 		return -ENOMEM;
543224cf5adSJeff Kirsher 
544224cf5adSJeff Kirsher 	sock_init_data(sock, sk);
545224cf5adSJeff Kirsher 
546224cf5adSJeff Kirsher 	sock->state	= SS_UNCONNECTED;
547224cf5adSJeff Kirsher 	sock->ops	= &pppoe_ops;
548224cf5adSJeff Kirsher 
549224cf5adSJeff Kirsher 	sk->sk_backlog_rcv	= pppoe_rcv_core;
550224cf5adSJeff Kirsher 	sk->sk_state		= PPPOX_NONE;
551224cf5adSJeff Kirsher 	sk->sk_type		= SOCK_STREAM;
552224cf5adSJeff Kirsher 	sk->sk_family		= PF_PPPOX;
553224cf5adSJeff Kirsher 	sk->sk_protocol		= PX_PROTO_OE;
554224cf5adSJeff Kirsher 
555fe53985aSGuillaume Nault 	INIT_WORK(&pppox_sk(sk)->proto.pppoe.padt_work,
556fe53985aSGuillaume Nault 		  pppoe_unbind_sock_work);
557fe53985aSGuillaume Nault 
558224cf5adSJeff Kirsher 	return 0;
559224cf5adSJeff Kirsher }
560224cf5adSJeff Kirsher 
pppoe_release(struct socket * sock)561224cf5adSJeff Kirsher static int pppoe_release(struct socket *sock)
562224cf5adSJeff Kirsher {
563224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
564224cf5adSJeff Kirsher 	struct pppox_sock *po;
565224cf5adSJeff Kirsher 	struct pppoe_net *pn;
566224cf5adSJeff Kirsher 	struct net *net = NULL;
567224cf5adSJeff Kirsher 
568224cf5adSJeff Kirsher 	if (!sk)
569224cf5adSJeff Kirsher 		return 0;
570224cf5adSJeff Kirsher 
571224cf5adSJeff Kirsher 	lock_sock(sk);
572224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD)) {
573224cf5adSJeff Kirsher 		release_sock(sk);
574224cf5adSJeff Kirsher 		return -EBADF;
575224cf5adSJeff Kirsher 	}
576224cf5adSJeff Kirsher 
577224cf5adSJeff Kirsher 	po = pppox_sk(sk);
578224cf5adSJeff Kirsher 
5791acea4f6SGuillaume Nault 	if (po->pppoe_dev) {
580224cf5adSJeff Kirsher 		dev_put(po->pppoe_dev);
581224cf5adSJeff Kirsher 		po->pppoe_dev = NULL;
582224cf5adSJeff Kirsher 	}
583224cf5adSJeff Kirsher 
584224cf5adSJeff Kirsher 	pppox_unbind_sock(sk);
585224cf5adSJeff Kirsher 
586224cf5adSJeff Kirsher 	/* Signal the death of the socket. */
587224cf5adSJeff Kirsher 	sk->sk_state = PPPOX_DEAD;
588224cf5adSJeff Kirsher 
589224cf5adSJeff Kirsher 	net = sock_net(sk);
590224cf5adSJeff Kirsher 	pn = pppoe_pernet(net);
591224cf5adSJeff Kirsher 
592224cf5adSJeff Kirsher 	/*
593224cf5adSJeff Kirsher 	 * protect "po" from concurrent updates
594224cf5adSJeff Kirsher 	 * on pppoe_flush_dev
595224cf5adSJeff Kirsher 	 */
596224cf5adSJeff Kirsher 	delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
597224cf5adSJeff Kirsher 		    po->pppoe_ifindex);
598224cf5adSJeff Kirsher 
599224cf5adSJeff Kirsher 	sock_orphan(sk);
600224cf5adSJeff Kirsher 	sock->sk = NULL;
601224cf5adSJeff Kirsher 
602224cf5adSJeff Kirsher 	skb_queue_purge(&sk->sk_receive_queue);
603224cf5adSJeff Kirsher 	release_sock(sk);
604224cf5adSJeff Kirsher 	sock_put(sk);
605224cf5adSJeff Kirsher 
606224cf5adSJeff Kirsher 	return 0;
607224cf5adSJeff Kirsher }
608224cf5adSJeff Kirsher 
pppoe_connect(struct socket * sock,struct sockaddr * uservaddr,int sockaddr_len,int flags)609224cf5adSJeff Kirsher static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
610224cf5adSJeff Kirsher 		  int sockaddr_len, int flags)
611224cf5adSJeff Kirsher {
612224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
613224cf5adSJeff Kirsher 	struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr;
614224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
615224cf5adSJeff Kirsher 	struct net_device *dev = NULL;
616224cf5adSJeff Kirsher 	struct pppoe_net *pn;
617224cf5adSJeff Kirsher 	struct net *net = NULL;
618224cf5adSJeff Kirsher 	int error;
619224cf5adSJeff Kirsher 
620224cf5adSJeff Kirsher 	lock_sock(sk);
621224cf5adSJeff Kirsher 
622224cf5adSJeff Kirsher 	error = -EINVAL;
623a49e2f5dSGuillaume Nault 
624a49e2f5dSGuillaume Nault 	if (sockaddr_len != sizeof(struct sockaddr_pppox))
625a49e2f5dSGuillaume Nault 		goto end;
626a49e2f5dSGuillaume Nault 
627224cf5adSJeff Kirsher 	if (sp->sa_protocol != PX_PROTO_OE)
628224cf5adSJeff Kirsher 		goto end;
629224cf5adSJeff Kirsher 
630224cf5adSJeff Kirsher 	/* Check for already bound sockets */
631224cf5adSJeff Kirsher 	error = -EBUSY;
632224cf5adSJeff Kirsher 	if ((sk->sk_state & PPPOX_CONNECTED) &&
633224cf5adSJeff Kirsher 	     stage_session(sp->sa_addr.pppoe.sid))
634224cf5adSJeff Kirsher 		goto end;
635224cf5adSJeff Kirsher 
636224cf5adSJeff Kirsher 	/* Check for already disconnected sockets, on attempts to disconnect */
637224cf5adSJeff Kirsher 	error = -EALREADY;
638224cf5adSJeff Kirsher 	if ((sk->sk_state & PPPOX_DEAD) &&
639224cf5adSJeff Kirsher 	     !stage_session(sp->sa_addr.pppoe.sid))
640224cf5adSJeff Kirsher 		goto end;
641224cf5adSJeff Kirsher 
642224cf5adSJeff Kirsher 	error = 0;
643224cf5adSJeff Kirsher 
644224cf5adSJeff Kirsher 	/* Delete the old binding */
645224cf5adSJeff Kirsher 	if (stage_session(po->pppoe_pa.sid)) {
646224cf5adSJeff Kirsher 		pppox_unbind_sock(sk);
647224cf5adSJeff Kirsher 		pn = pppoe_pernet(sock_net(sk));
648224cf5adSJeff Kirsher 		delete_item(pn, po->pppoe_pa.sid,
649224cf5adSJeff Kirsher 			    po->pppoe_pa.remote, po->pppoe_ifindex);
650224cf5adSJeff Kirsher 		if (po->pppoe_dev) {
651224cf5adSJeff Kirsher 			dev_put(po->pppoe_dev);
652224cf5adSJeff Kirsher 			po->pppoe_dev = NULL;
653224cf5adSJeff Kirsher 		}
654224cf5adSJeff Kirsher 
655fe53985aSGuillaume Nault 		po->pppoe_ifindex = 0;
656fe53985aSGuillaume Nault 		memset(&po->pppoe_pa, 0, sizeof(po->pppoe_pa));
657fe53985aSGuillaume Nault 		memset(&po->pppoe_relay, 0, sizeof(po->pppoe_relay));
658fe53985aSGuillaume Nault 		memset(&po->chan, 0, sizeof(po->chan));
659fe53985aSGuillaume Nault 		po->next = NULL;
660fe53985aSGuillaume Nault 		po->num = 0;
661fe53985aSGuillaume Nault 
662224cf5adSJeff Kirsher 		sk->sk_state = PPPOX_NONE;
663224cf5adSJeff Kirsher 	}
664224cf5adSJeff Kirsher 
665224cf5adSJeff Kirsher 	/* Re-bind in session stage only */
666224cf5adSJeff Kirsher 	if (stage_session(sp->sa_addr.pppoe.sid)) {
667224cf5adSJeff Kirsher 		error = -ENODEV;
668224cf5adSJeff Kirsher 		net = sock_net(sk);
669224cf5adSJeff Kirsher 		dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev);
670224cf5adSJeff Kirsher 		if (!dev)
671224cf5adSJeff Kirsher 			goto err_put;
672224cf5adSJeff Kirsher 
673224cf5adSJeff Kirsher 		po->pppoe_dev = dev;
674224cf5adSJeff Kirsher 		po->pppoe_ifindex = dev->ifindex;
675224cf5adSJeff Kirsher 		pn = pppoe_pernet(net);
676224cf5adSJeff Kirsher 		if (!(dev->flags & IFF_UP)) {
677224cf5adSJeff Kirsher 			goto err_put;
678224cf5adSJeff Kirsher 		}
679224cf5adSJeff Kirsher 
680224cf5adSJeff Kirsher 		memcpy(&po->pppoe_pa,
681224cf5adSJeff Kirsher 		       &sp->sa_addr.pppoe,
682224cf5adSJeff Kirsher 		       sizeof(struct pppoe_addr));
683224cf5adSJeff Kirsher 
684224cf5adSJeff Kirsher 		write_lock_bh(&pn->hash_lock);
685224cf5adSJeff Kirsher 		error = __set_item(pn, po);
686224cf5adSJeff Kirsher 		write_unlock_bh(&pn->hash_lock);
687224cf5adSJeff Kirsher 		if (error < 0)
688224cf5adSJeff Kirsher 			goto err_put;
689224cf5adSJeff Kirsher 
690224cf5adSJeff Kirsher 		po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
691224cf5adSJeff Kirsher 				   dev->hard_header_len);
692224cf5adSJeff Kirsher 
693a8a3e41cSChristoph Schulz 		po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr) - 2;
694224cf5adSJeff Kirsher 		po->chan.private = sk;
695224cf5adSJeff Kirsher 		po->chan.ops = &pppoe_chan_ops;
696224cf5adSJeff Kirsher 
697224cf5adSJeff Kirsher 		error = ppp_register_net_channel(dev_net(dev), &po->chan);
698224cf5adSJeff Kirsher 		if (error) {
699224cf5adSJeff Kirsher 			delete_item(pn, po->pppoe_pa.sid,
700224cf5adSJeff Kirsher 				    po->pppoe_pa.remote, po->pppoe_ifindex);
701224cf5adSJeff Kirsher 			goto err_put;
702224cf5adSJeff Kirsher 		}
703224cf5adSJeff Kirsher 
704224cf5adSJeff Kirsher 		sk->sk_state = PPPOX_CONNECTED;
705224cf5adSJeff Kirsher 	}
706224cf5adSJeff Kirsher 
707224cf5adSJeff Kirsher 	po->num = sp->sa_addr.pppoe.sid;
708224cf5adSJeff Kirsher 
709224cf5adSJeff Kirsher end:
710224cf5adSJeff Kirsher 	release_sock(sk);
711224cf5adSJeff Kirsher 	return error;
712224cf5adSJeff Kirsher err_put:
713224cf5adSJeff Kirsher 	if (po->pppoe_dev) {
714224cf5adSJeff Kirsher 		dev_put(po->pppoe_dev);
715224cf5adSJeff Kirsher 		po->pppoe_dev = NULL;
716224cf5adSJeff Kirsher 	}
717224cf5adSJeff Kirsher 	goto end;
718224cf5adSJeff Kirsher }
719224cf5adSJeff Kirsher 
pppoe_getname(struct socket * sock,struct sockaddr * uaddr,int peer)720224cf5adSJeff Kirsher static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
7219b2c45d4SDenys Vlasenko 		  int peer)
722224cf5adSJeff Kirsher {
723224cf5adSJeff Kirsher 	int len = sizeof(struct sockaddr_pppox);
724224cf5adSJeff Kirsher 	struct sockaddr_pppox sp;
725224cf5adSJeff Kirsher 
726224cf5adSJeff Kirsher 	sp.sa_family	= AF_PPPOX;
727224cf5adSJeff Kirsher 	sp.sa_protocol	= PX_PROTO_OE;
728224cf5adSJeff Kirsher 	memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
729224cf5adSJeff Kirsher 	       sizeof(struct pppoe_addr));
730224cf5adSJeff Kirsher 
731224cf5adSJeff Kirsher 	memcpy(uaddr, &sp, len);
732224cf5adSJeff Kirsher 
7339b2c45d4SDenys Vlasenko 	return len;
734224cf5adSJeff Kirsher }
735224cf5adSJeff Kirsher 
pppoe_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)736224cf5adSJeff Kirsher static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
737224cf5adSJeff Kirsher 		unsigned long arg)
738224cf5adSJeff Kirsher {
739224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
740224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
741224cf5adSJeff Kirsher 	int val;
742224cf5adSJeff Kirsher 	int err;
743224cf5adSJeff Kirsher 
744224cf5adSJeff Kirsher 	switch (cmd) {
745224cf5adSJeff Kirsher 	case PPPIOCGMRU:
746224cf5adSJeff Kirsher 		err = -ENXIO;
747224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
748224cf5adSJeff Kirsher 			break;
749224cf5adSJeff Kirsher 
750224cf5adSJeff Kirsher 		err = -EFAULT;
751224cf5adSJeff Kirsher 		if (put_user(po->pppoe_dev->mtu -
752224cf5adSJeff Kirsher 			     sizeof(struct pppoe_hdr) -
753224cf5adSJeff Kirsher 			     PPP_HDRLEN,
754224cf5adSJeff Kirsher 			     (int __user *)arg))
755224cf5adSJeff Kirsher 			break;
756224cf5adSJeff Kirsher 		err = 0;
757224cf5adSJeff Kirsher 		break;
758224cf5adSJeff Kirsher 
759224cf5adSJeff Kirsher 	case PPPIOCSMRU:
760224cf5adSJeff Kirsher 		err = -ENXIO;
761224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
762224cf5adSJeff Kirsher 			break;
763224cf5adSJeff Kirsher 
764224cf5adSJeff Kirsher 		err = -EFAULT;
765224cf5adSJeff Kirsher 		if (get_user(val, (int __user *)arg))
766224cf5adSJeff Kirsher 			break;
767224cf5adSJeff Kirsher 
768224cf5adSJeff Kirsher 		if (val < (po->pppoe_dev->mtu
769224cf5adSJeff Kirsher 			   - sizeof(struct pppoe_hdr)
770224cf5adSJeff Kirsher 			   - PPP_HDRLEN))
771224cf5adSJeff Kirsher 			err = 0;
772224cf5adSJeff Kirsher 		else
773224cf5adSJeff Kirsher 			err = -EINVAL;
774224cf5adSJeff Kirsher 		break;
775224cf5adSJeff Kirsher 
776224cf5adSJeff Kirsher 	case PPPIOCSFLAGS:
777224cf5adSJeff Kirsher 		err = -EFAULT;
778224cf5adSJeff Kirsher 		if (get_user(val, (int __user *)arg))
779224cf5adSJeff Kirsher 			break;
780224cf5adSJeff Kirsher 		err = 0;
781224cf5adSJeff Kirsher 		break;
782224cf5adSJeff Kirsher 
783224cf5adSJeff Kirsher 	case PPPOEIOCSFWD:
784224cf5adSJeff Kirsher 	{
785224cf5adSJeff Kirsher 		struct pppox_sock *relay_po;
786224cf5adSJeff Kirsher 
787224cf5adSJeff Kirsher 		err = -EBUSY;
788a8acce6aSGuillaume Nault 		if (sk->sk_state & (PPPOX_BOUND | PPPOX_DEAD))
789224cf5adSJeff Kirsher 			break;
790224cf5adSJeff Kirsher 
791224cf5adSJeff Kirsher 		err = -ENOTCONN;
792224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
793224cf5adSJeff Kirsher 			break;
794224cf5adSJeff Kirsher 
795224cf5adSJeff Kirsher 		/* PPPoE address from the user specifies an outbound
796224cf5adSJeff Kirsher 		   PPPoE address which frames are forwarded to */
797224cf5adSJeff Kirsher 		err = -EFAULT;
798224cf5adSJeff Kirsher 		if (copy_from_user(&po->pppoe_relay,
799224cf5adSJeff Kirsher 				   (void __user *)arg,
800224cf5adSJeff Kirsher 				   sizeof(struct sockaddr_pppox)))
801224cf5adSJeff Kirsher 			break;
802224cf5adSJeff Kirsher 
803224cf5adSJeff Kirsher 		err = -EINVAL;
804224cf5adSJeff Kirsher 		if (po->pppoe_relay.sa_family != AF_PPPOX ||
805224cf5adSJeff Kirsher 		    po->pppoe_relay.sa_protocol != PX_PROTO_OE)
806224cf5adSJeff Kirsher 			break;
807224cf5adSJeff Kirsher 
808224cf5adSJeff Kirsher 		/* Check that the socket referenced by the address
809224cf5adSJeff Kirsher 		   actually exists. */
810224cf5adSJeff Kirsher 		relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
811224cf5adSJeff Kirsher 		if (!relay_po)
812224cf5adSJeff Kirsher 			break;
813224cf5adSJeff Kirsher 
814224cf5adSJeff Kirsher 		sock_put(sk_pppox(relay_po));
815224cf5adSJeff Kirsher 		sk->sk_state |= PPPOX_RELAY;
816224cf5adSJeff Kirsher 		err = 0;
817224cf5adSJeff Kirsher 		break;
818224cf5adSJeff Kirsher 	}
819224cf5adSJeff Kirsher 
820224cf5adSJeff Kirsher 	case PPPOEIOCDFWD:
821224cf5adSJeff Kirsher 		err = -EALREADY;
822224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_RELAY))
823224cf5adSJeff Kirsher 			break;
824224cf5adSJeff Kirsher 
825224cf5adSJeff Kirsher 		sk->sk_state &= ~PPPOX_RELAY;
826224cf5adSJeff Kirsher 		err = 0;
827224cf5adSJeff Kirsher 		break;
828224cf5adSJeff Kirsher 
829224cf5adSJeff Kirsher 	default:
830224cf5adSJeff Kirsher 		err = -ENOTTY;
831224cf5adSJeff Kirsher 	}
832224cf5adSJeff Kirsher 
833224cf5adSJeff Kirsher 	return err;
834224cf5adSJeff Kirsher }
835224cf5adSJeff Kirsher 
pppoe_sendmsg(struct socket * sock,struct msghdr * m,size_t total_len)8361b784140SYing Xue static int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
8371b784140SYing Xue 			 size_t total_len)
838224cf5adSJeff Kirsher {
839224cf5adSJeff Kirsher 	struct sk_buff *skb;
840224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
841224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
842224cf5adSJeff Kirsher 	int error;
843224cf5adSJeff Kirsher 	struct pppoe_hdr hdr;
844224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
845224cf5adSJeff Kirsher 	struct net_device *dev;
846224cf5adSJeff Kirsher 	char *start;
84702612bb0SGuillaume Nault 	int hlen;
848224cf5adSJeff Kirsher 
849224cf5adSJeff Kirsher 	lock_sock(sk);
850224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
851224cf5adSJeff Kirsher 		error = -ENOTCONN;
852224cf5adSJeff Kirsher 		goto end;
853224cf5adSJeff Kirsher 	}
854224cf5adSJeff Kirsher 
855224cf5adSJeff Kirsher 	hdr.ver = 1;
856224cf5adSJeff Kirsher 	hdr.type = 1;
857224cf5adSJeff Kirsher 	hdr.code = 0;
858224cf5adSJeff Kirsher 	hdr.sid = po->num;
859224cf5adSJeff Kirsher 
860224cf5adSJeff Kirsher 	dev = po->pppoe_dev;
861224cf5adSJeff Kirsher 
862224cf5adSJeff Kirsher 	error = -EMSGSIZE;
863224cf5adSJeff Kirsher 	if (total_len > (dev->mtu + dev->hard_header_len))
864224cf5adSJeff Kirsher 		goto end;
865224cf5adSJeff Kirsher 
86602612bb0SGuillaume Nault 	hlen = LL_RESERVED_SPACE(dev);
86702612bb0SGuillaume Nault 	skb = sock_wmalloc(sk, hlen + sizeof(*ph) + total_len +
86802612bb0SGuillaume Nault 			   dev->needed_tailroom, 0, GFP_KERNEL);
869224cf5adSJeff Kirsher 	if (!skb) {
870224cf5adSJeff Kirsher 		error = -ENOMEM;
871224cf5adSJeff Kirsher 		goto end;
872224cf5adSJeff Kirsher 	}
873224cf5adSJeff Kirsher 
874224cf5adSJeff Kirsher 	/* Reserve space for headers. */
87502612bb0SGuillaume Nault 	skb_reserve(skb, hlen);
876224cf5adSJeff Kirsher 	skb_reset_network_header(skb);
877224cf5adSJeff Kirsher 
878224cf5adSJeff Kirsher 	skb->dev = dev;
879224cf5adSJeff Kirsher 
880224cf5adSJeff Kirsher 	skb->priority = sk->sk_priority;
881224cf5adSJeff Kirsher 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
882224cf5adSJeff Kirsher 
8834df864c1SJohannes Berg 	ph = skb_put(skb, total_len + sizeof(struct pppoe_hdr));
884224cf5adSJeff Kirsher 	start = (char *)&ph->tag[0];
885224cf5adSJeff Kirsher 
8866ce8e9ceSAl Viro 	error = memcpy_from_msg(start, m, total_len);
887224cf5adSJeff Kirsher 	if (error < 0) {
888224cf5adSJeff Kirsher 		kfree_skb(skb);
889224cf5adSJeff Kirsher 		goto end;
890224cf5adSJeff Kirsher 	}
891224cf5adSJeff Kirsher 
892224cf5adSJeff Kirsher 	error = total_len;
893224cf5adSJeff Kirsher 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
894224cf5adSJeff Kirsher 			po->pppoe_pa.remote, NULL, total_len);
895224cf5adSJeff Kirsher 
896224cf5adSJeff Kirsher 	memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
897224cf5adSJeff Kirsher 
898224cf5adSJeff Kirsher 	ph->length = htons(total_len);
899224cf5adSJeff Kirsher 
900224cf5adSJeff Kirsher 	dev_queue_xmit(skb);
901224cf5adSJeff Kirsher 
902224cf5adSJeff Kirsher end:
903224cf5adSJeff Kirsher 	release_sock(sk);
904224cf5adSJeff Kirsher 	return error;
905224cf5adSJeff Kirsher }
906224cf5adSJeff Kirsher 
907224cf5adSJeff Kirsher /************************************************************************
908224cf5adSJeff Kirsher  *
909224cf5adSJeff Kirsher  * xmit function for internal use.
910224cf5adSJeff Kirsher  *
911224cf5adSJeff Kirsher  ***********************************************************************/
__pppoe_xmit(struct sock * sk,struct sk_buff * skb)912224cf5adSJeff Kirsher static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
913224cf5adSJeff Kirsher {
914224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
915224cf5adSJeff Kirsher 	struct net_device *dev = po->pppoe_dev;
916224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
917224cf5adSJeff Kirsher 	int data_len = skb->len;
918224cf5adSJeff Kirsher 
919224cf5adSJeff Kirsher 	/* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
920224cf5adSJeff Kirsher 	 * xmit operations conclude prior to an unregistration call.  Thus
921224cf5adSJeff Kirsher 	 * sk->sk_state cannot change, so we don't need to do lock_sock().
922224cf5adSJeff Kirsher 	 * But, we also can't do a lock_sock since that introduces a potential
923224cf5adSJeff Kirsher 	 * deadlock as we'd reverse the lock ordering used when calling
924224cf5adSJeff Kirsher 	 * ppp_unregister_channel().
925224cf5adSJeff Kirsher 	 */
926224cf5adSJeff Kirsher 
927224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
928224cf5adSJeff Kirsher 		goto abort;
929224cf5adSJeff Kirsher 
930224cf5adSJeff Kirsher 	if (!dev)
931224cf5adSJeff Kirsher 		goto abort;
932224cf5adSJeff Kirsher 
933224cf5adSJeff Kirsher 	/* Copy the data if there is no space for the header or if it's
934224cf5adSJeff Kirsher 	 * read-only.
935224cf5adSJeff Kirsher 	 */
93602612bb0SGuillaume Nault 	if (skb_cow_head(skb, LL_RESERVED_SPACE(dev) + sizeof(*ph)))
937224cf5adSJeff Kirsher 		goto abort;
938224cf5adSJeff Kirsher 
939224cf5adSJeff Kirsher 	__skb_push(skb, sizeof(*ph));
940224cf5adSJeff Kirsher 	skb_reset_network_header(skb);
941224cf5adSJeff Kirsher 
942224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
943224cf5adSJeff Kirsher 	ph->ver	= 1;
944224cf5adSJeff Kirsher 	ph->type = 1;
945224cf5adSJeff Kirsher 	ph->code = 0;
946224cf5adSJeff Kirsher 	ph->sid	= po->num;
947224cf5adSJeff Kirsher 	ph->length = htons(data_len);
948224cf5adSJeff Kirsher 
949224cf5adSJeff Kirsher 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
950224cf5adSJeff Kirsher 	skb->dev = dev;
951224cf5adSJeff Kirsher 
952224cf5adSJeff Kirsher 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
953224cf5adSJeff Kirsher 			po->pppoe_pa.remote, NULL, data_len);
954224cf5adSJeff Kirsher 
955224cf5adSJeff Kirsher 	dev_queue_xmit(skb);
956224cf5adSJeff Kirsher 	return 1;
957224cf5adSJeff Kirsher 
958224cf5adSJeff Kirsher abort:
959224cf5adSJeff Kirsher 	kfree_skb(skb);
960224cf5adSJeff Kirsher 	return 1;
961224cf5adSJeff Kirsher }
962224cf5adSJeff Kirsher 
963224cf5adSJeff Kirsher /************************************************************************
964224cf5adSJeff Kirsher  *
965224cf5adSJeff Kirsher  * xmit function called by generic PPP driver
966224cf5adSJeff Kirsher  * sends PPP frame over PPPoE socket
967224cf5adSJeff Kirsher  *
968224cf5adSJeff Kirsher  ***********************************************************************/
pppoe_xmit(struct ppp_channel * chan,struct sk_buff * skb)969224cf5adSJeff Kirsher static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
970224cf5adSJeff Kirsher {
971*89c04d6cSWu Yunchuan 	struct sock *sk = chan->private;
972224cf5adSJeff Kirsher 	return __pppoe_xmit(sk, skb);
973224cf5adSJeff Kirsher }
974224cf5adSJeff Kirsher 
pppoe_fill_forward_path(struct net_device_path_ctx * ctx,struct net_device_path * path,const struct ppp_channel * chan)975f6efc675SFelix Fietkau static int pppoe_fill_forward_path(struct net_device_path_ctx *ctx,
976f6efc675SFelix Fietkau 				   struct net_device_path *path,
977f6efc675SFelix Fietkau 				   const struct ppp_channel *chan)
978f6efc675SFelix Fietkau {
979*89c04d6cSWu Yunchuan 	struct sock *sk = chan->private;
980f6efc675SFelix Fietkau 	struct pppox_sock *po = pppox_sk(sk);
981f6efc675SFelix Fietkau 	struct net_device *dev = po->pppoe_dev;
982f6efc675SFelix Fietkau 
983f6efc675SFelix Fietkau 	if (sock_flag(sk, SOCK_DEAD) ||
984f6efc675SFelix Fietkau 	    !(sk->sk_state & PPPOX_CONNECTED) || !dev)
985f6efc675SFelix Fietkau 		return -1;
986f6efc675SFelix Fietkau 
987f6efc675SFelix Fietkau 	path->type = DEV_PATH_PPPOE;
988f6efc675SFelix Fietkau 	path->encap.proto = htons(ETH_P_PPP_SES);
989f6efc675SFelix Fietkau 	path->encap.id = be16_to_cpu(po->num);
990f6efc675SFelix Fietkau 	memcpy(path->encap.h_dest, po->pppoe_pa.remote, ETH_ALEN);
991cf2df74eSFelix Fietkau 	memcpy(ctx->daddr, po->pppoe_pa.remote, ETH_ALEN);
992f6efc675SFelix Fietkau 	path->dev = ctx->dev;
993f6efc675SFelix Fietkau 	ctx->dev = dev;
994f6efc675SFelix Fietkau 
995f6efc675SFelix Fietkau 	return 0;
996f6efc675SFelix Fietkau }
997f6efc675SFelix Fietkau 
998224cf5adSJeff Kirsher static const struct ppp_channel_ops pppoe_chan_ops = {
999224cf5adSJeff Kirsher 	.start_xmit = pppoe_xmit,
1000f6efc675SFelix Fietkau 	.fill_forward_path = pppoe_fill_forward_path,
1001224cf5adSJeff Kirsher };
1002224cf5adSJeff Kirsher 
pppoe_recvmsg(struct socket * sock,struct msghdr * m,size_t total_len,int flags)10031b784140SYing Xue static int pppoe_recvmsg(struct socket *sock, struct msghdr *m,
10041b784140SYing Xue 			 size_t total_len, int flags)
1005224cf5adSJeff Kirsher {
1006224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
1007224cf5adSJeff Kirsher 	struct sk_buff *skb;
1008224cf5adSJeff Kirsher 	int error = 0;
1009224cf5adSJeff Kirsher 
1010224cf5adSJeff Kirsher 	if (sk->sk_state & PPPOX_BOUND) {
1011224cf5adSJeff Kirsher 		error = -EIO;
1012224cf5adSJeff Kirsher 		goto end;
1013224cf5adSJeff Kirsher 	}
1014224cf5adSJeff Kirsher 
1015f4b41f06SOliver Hartkopp 	skb = skb_recv_datagram(sk, flags, &error);
1016224cf5adSJeff Kirsher 	if (error < 0)
1017224cf5adSJeff Kirsher 		goto end;
1018224cf5adSJeff Kirsher 
1019224cf5adSJeff Kirsher 	if (skb) {
1020224cf5adSJeff Kirsher 		total_len = min_t(size_t, total_len, skb->len);
102151f3d02bSDavid S. Miller 		error = skb_copy_datagram_msg(skb, 0, m, total_len);
1022968d7018SEric Dumazet 		if (error == 0) {
1023968d7018SEric Dumazet 			consume_skb(skb);
1024968d7018SEric Dumazet 			return total_len;
1025968d7018SEric Dumazet 		}
1026224cf5adSJeff Kirsher 	}
1027224cf5adSJeff Kirsher 
1028224cf5adSJeff Kirsher 	kfree_skb(skb);
1029224cf5adSJeff Kirsher end:
1030224cf5adSJeff Kirsher 	return error;
1031224cf5adSJeff Kirsher }
1032224cf5adSJeff Kirsher 
1033224cf5adSJeff Kirsher #ifdef CONFIG_PROC_FS
pppoe_seq_show(struct seq_file * seq,void * v)1034224cf5adSJeff Kirsher static int pppoe_seq_show(struct seq_file *seq, void *v)
1035224cf5adSJeff Kirsher {
1036224cf5adSJeff Kirsher 	struct pppox_sock *po;
1037224cf5adSJeff Kirsher 	char *dev_name;
1038224cf5adSJeff Kirsher 
1039224cf5adSJeff Kirsher 	if (v == SEQ_START_TOKEN) {
1040224cf5adSJeff Kirsher 		seq_puts(seq, "Id       Address              Device\n");
1041224cf5adSJeff Kirsher 		goto out;
1042224cf5adSJeff Kirsher 	}
1043224cf5adSJeff Kirsher 
1044224cf5adSJeff Kirsher 	po = v;
1045224cf5adSJeff Kirsher 	dev_name = po->pppoe_pa.dev;
1046224cf5adSJeff Kirsher 
1047224cf5adSJeff Kirsher 	seq_printf(seq, "%08X %pM %8s\n",
1048224cf5adSJeff Kirsher 		po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
1049224cf5adSJeff Kirsher out:
1050224cf5adSJeff Kirsher 	return 0;
1051224cf5adSJeff Kirsher }
1052224cf5adSJeff Kirsher 
pppoe_get_idx(struct pppoe_net * pn,loff_t pos)1053224cf5adSJeff Kirsher static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
1054224cf5adSJeff Kirsher {
1055224cf5adSJeff Kirsher 	struct pppox_sock *po;
1056224cf5adSJeff Kirsher 	int i;
1057224cf5adSJeff Kirsher 
1058224cf5adSJeff Kirsher 	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
1059224cf5adSJeff Kirsher 		po = pn->hash_table[i];
1060224cf5adSJeff Kirsher 		while (po) {
1061224cf5adSJeff Kirsher 			if (!pos--)
1062224cf5adSJeff Kirsher 				goto out;
1063224cf5adSJeff Kirsher 			po = po->next;
1064224cf5adSJeff Kirsher 		}
1065224cf5adSJeff Kirsher 	}
1066224cf5adSJeff Kirsher 
1067224cf5adSJeff Kirsher out:
1068224cf5adSJeff Kirsher 	return po;
1069224cf5adSJeff Kirsher }
1070224cf5adSJeff Kirsher 
pppoe_seq_start(struct seq_file * seq,loff_t * pos)1071224cf5adSJeff Kirsher static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
1072224cf5adSJeff Kirsher 	__acquires(pn->hash_lock)
1073224cf5adSJeff Kirsher {
1074224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1075224cf5adSJeff Kirsher 	loff_t l = *pos;
1076224cf5adSJeff Kirsher 
1077224cf5adSJeff Kirsher 	read_lock_bh(&pn->hash_lock);
1078224cf5adSJeff Kirsher 	return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
1079224cf5adSJeff Kirsher }
1080224cf5adSJeff Kirsher 
pppoe_seq_next(struct seq_file * seq,void * v,loff_t * pos)1081224cf5adSJeff Kirsher static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1082224cf5adSJeff Kirsher {
1083224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1084224cf5adSJeff Kirsher 	struct pppox_sock *po;
1085224cf5adSJeff Kirsher 
1086224cf5adSJeff Kirsher 	++*pos;
1087224cf5adSJeff Kirsher 	if (v == SEQ_START_TOKEN) {
1088224cf5adSJeff Kirsher 		po = pppoe_get_idx(pn, 0);
1089224cf5adSJeff Kirsher 		goto out;
1090224cf5adSJeff Kirsher 	}
1091224cf5adSJeff Kirsher 	po = v;
1092224cf5adSJeff Kirsher 	if (po->next)
1093224cf5adSJeff Kirsher 		po = po->next;
1094224cf5adSJeff Kirsher 	else {
1095224cf5adSJeff Kirsher 		int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1096224cf5adSJeff Kirsher 
1097224cf5adSJeff Kirsher 		po = NULL;
1098224cf5adSJeff Kirsher 		while (++hash < PPPOE_HASH_SIZE) {
1099224cf5adSJeff Kirsher 			po = pn->hash_table[hash];
1100224cf5adSJeff Kirsher 			if (po)
1101224cf5adSJeff Kirsher 				break;
1102224cf5adSJeff Kirsher 		}
1103224cf5adSJeff Kirsher 	}
1104224cf5adSJeff Kirsher 
1105224cf5adSJeff Kirsher out:
1106224cf5adSJeff Kirsher 	return po;
1107224cf5adSJeff Kirsher }
1108224cf5adSJeff Kirsher 
pppoe_seq_stop(struct seq_file * seq,void * v)1109224cf5adSJeff Kirsher static void pppoe_seq_stop(struct seq_file *seq, void *v)
1110224cf5adSJeff Kirsher 	__releases(pn->hash_lock)
1111224cf5adSJeff Kirsher {
1112224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1113224cf5adSJeff Kirsher 	read_unlock_bh(&pn->hash_lock);
1114224cf5adSJeff Kirsher }
1115224cf5adSJeff Kirsher 
1116224cf5adSJeff Kirsher static const struct seq_operations pppoe_seq_ops = {
1117224cf5adSJeff Kirsher 	.start		= pppoe_seq_start,
1118224cf5adSJeff Kirsher 	.next		= pppoe_seq_next,
1119224cf5adSJeff Kirsher 	.stop		= pppoe_seq_stop,
1120224cf5adSJeff Kirsher 	.show		= pppoe_seq_show,
1121224cf5adSJeff Kirsher };
1122224cf5adSJeff Kirsher #endif /* CONFIG_PROC_FS */
1123224cf5adSJeff Kirsher 
1124224cf5adSJeff Kirsher static const struct proto_ops pppoe_ops = {
1125224cf5adSJeff Kirsher 	.family		= AF_PPPOX,
1126224cf5adSJeff Kirsher 	.owner		= THIS_MODULE,
1127224cf5adSJeff Kirsher 	.release	= pppoe_release,
1128224cf5adSJeff Kirsher 	.bind		= sock_no_bind,
1129224cf5adSJeff Kirsher 	.connect	= pppoe_connect,
1130224cf5adSJeff Kirsher 	.socketpair	= sock_no_socketpair,
1131224cf5adSJeff Kirsher 	.accept		= sock_no_accept,
1132224cf5adSJeff Kirsher 	.getname	= pppoe_getname,
1133a11e1d43SLinus Torvalds 	.poll		= datagram_poll,
1134224cf5adSJeff Kirsher 	.listen		= sock_no_listen,
1135224cf5adSJeff Kirsher 	.shutdown	= sock_no_shutdown,
1136224cf5adSJeff Kirsher 	.sendmsg	= pppoe_sendmsg,
1137224cf5adSJeff Kirsher 	.recvmsg	= pppoe_recvmsg,
1138224cf5adSJeff Kirsher 	.mmap		= sock_no_mmap,
1139224cf5adSJeff Kirsher 	.ioctl		= pppox_ioctl,
1140055d8824SArnd Bergmann #ifdef CONFIG_COMPAT
1141055d8824SArnd Bergmann 	.compat_ioctl	= pppox_compat_ioctl,
1142055d8824SArnd Bergmann #endif
1143224cf5adSJeff Kirsher };
1144224cf5adSJeff Kirsher 
1145224cf5adSJeff Kirsher static const struct pppox_proto pppoe_proto = {
1146224cf5adSJeff Kirsher 	.create	= pppoe_create,
1147224cf5adSJeff Kirsher 	.ioctl	= pppoe_ioctl,
1148224cf5adSJeff Kirsher 	.owner	= THIS_MODULE,
1149224cf5adSJeff Kirsher };
1150224cf5adSJeff Kirsher 
pppoe_init_net(struct net * net)1151224cf5adSJeff Kirsher static __net_init int pppoe_init_net(struct net *net)
1152224cf5adSJeff Kirsher {
1153224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(net);
1154224cf5adSJeff Kirsher 	struct proc_dir_entry *pde;
1155224cf5adSJeff Kirsher 
1156224cf5adSJeff Kirsher 	rwlock_init(&pn->hash_lock);
1157224cf5adSJeff Kirsher 
1158c3506372SChristoph Hellwig 	pde = proc_create_net("pppoe", 0444, net->proc_net,
1159c3506372SChristoph Hellwig 			&pppoe_seq_ops, sizeof(struct seq_net_private));
1160224cf5adSJeff Kirsher #ifdef CONFIG_PROC_FS
1161224cf5adSJeff Kirsher 	if (!pde)
1162224cf5adSJeff Kirsher 		return -ENOMEM;
1163224cf5adSJeff Kirsher #endif
1164224cf5adSJeff Kirsher 
1165224cf5adSJeff Kirsher 	return 0;
1166224cf5adSJeff Kirsher }
1167224cf5adSJeff Kirsher 
pppoe_exit_net(struct net * net)1168224cf5adSJeff Kirsher static __net_exit void pppoe_exit_net(struct net *net)
1169224cf5adSJeff Kirsher {
1170ece31ffdSGao feng 	remove_proc_entry("pppoe", net->proc_net);
1171224cf5adSJeff Kirsher }
1172224cf5adSJeff Kirsher 
1173224cf5adSJeff Kirsher static struct pernet_operations pppoe_net_ops = {
1174224cf5adSJeff Kirsher 	.init = pppoe_init_net,
1175224cf5adSJeff Kirsher 	.exit = pppoe_exit_net,
1176224cf5adSJeff Kirsher 	.id   = &pppoe_net_id,
1177224cf5adSJeff Kirsher 	.size = sizeof(struct pppoe_net),
1178224cf5adSJeff Kirsher };
1179224cf5adSJeff Kirsher 
pppoe_init(void)1180224cf5adSJeff Kirsher static int __init pppoe_init(void)
1181224cf5adSJeff Kirsher {
1182224cf5adSJeff Kirsher 	int err;
1183224cf5adSJeff Kirsher 
1184224cf5adSJeff Kirsher 	err = register_pernet_device(&pppoe_net_ops);
1185224cf5adSJeff Kirsher 	if (err)
1186224cf5adSJeff Kirsher 		goto out;
1187224cf5adSJeff Kirsher 
1188224cf5adSJeff Kirsher 	err = proto_register(&pppoe_sk_proto, 0);
1189224cf5adSJeff Kirsher 	if (err)
1190224cf5adSJeff Kirsher 		goto out_unregister_net_ops;
1191224cf5adSJeff Kirsher 
1192224cf5adSJeff Kirsher 	err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1193224cf5adSJeff Kirsher 	if (err)
1194224cf5adSJeff Kirsher 		goto out_unregister_pppoe_proto;
1195224cf5adSJeff Kirsher 
1196224cf5adSJeff Kirsher 	dev_add_pack(&pppoes_ptype);
1197224cf5adSJeff Kirsher 	dev_add_pack(&pppoed_ptype);
1198224cf5adSJeff Kirsher 	register_netdevice_notifier(&pppoe_notifier);
1199224cf5adSJeff Kirsher 
1200224cf5adSJeff Kirsher 	return 0;
1201224cf5adSJeff Kirsher 
1202224cf5adSJeff Kirsher out_unregister_pppoe_proto:
1203224cf5adSJeff Kirsher 	proto_unregister(&pppoe_sk_proto);
1204224cf5adSJeff Kirsher out_unregister_net_ops:
1205224cf5adSJeff Kirsher 	unregister_pernet_device(&pppoe_net_ops);
1206224cf5adSJeff Kirsher out:
1207224cf5adSJeff Kirsher 	return err;
1208224cf5adSJeff Kirsher }
1209224cf5adSJeff Kirsher 
pppoe_exit(void)1210224cf5adSJeff Kirsher static void __exit pppoe_exit(void)
1211224cf5adSJeff Kirsher {
1212224cf5adSJeff Kirsher 	unregister_netdevice_notifier(&pppoe_notifier);
1213224cf5adSJeff Kirsher 	dev_remove_pack(&pppoed_ptype);
1214224cf5adSJeff Kirsher 	dev_remove_pack(&pppoes_ptype);
1215224cf5adSJeff Kirsher 	unregister_pppox_proto(PX_PROTO_OE);
1216224cf5adSJeff Kirsher 	proto_unregister(&pppoe_sk_proto);
1217224cf5adSJeff Kirsher 	unregister_pernet_device(&pppoe_net_ops);
1218224cf5adSJeff Kirsher }
1219224cf5adSJeff Kirsher 
1220224cf5adSJeff Kirsher module_init(pppoe_init);
1221224cf5adSJeff Kirsher module_exit(pppoe_exit);
1222224cf5adSJeff Kirsher 
1223224cf5adSJeff Kirsher MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1224224cf5adSJeff Kirsher MODULE_DESCRIPTION("PPP over Ethernet driver");
1225224cf5adSJeff Kirsher MODULE_LICENSE("GPL");
1226681b4d88SGuillaume Nault MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OE);
1227