xref: /openbmc/linux/drivers/net/ppp/pppoe.c (revision 1acea4f6)
1224cf5adSJeff Kirsher /** -*- linux-c -*- ***********************************************************
2224cf5adSJeff Kirsher  * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
3224cf5adSJeff Kirsher  *
4224cf5adSJeff Kirsher  * PPPoX --- Generic PPP encapsulation socket family
5224cf5adSJeff Kirsher  * PPPoE --- PPP over Ethernet (RFC 2516)
6224cf5adSJeff Kirsher  *
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.
28224cf5adSJeff Kirsher  * 050101 :	Fix PADT procesing.
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  *		This program is free software; you can redistribute it and/or
54224cf5adSJeff Kirsher  *		modify it under the terms of the GNU General Public License
55224cf5adSJeff Kirsher  *		as published by the Free Software Foundation; either version
56224cf5adSJeff Kirsher  *		2 of the License, or (at your option) any later version.
57224cf5adSJeff Kirsher  *
58224cf5adSJeff Kirsher  */
59224cf5adSJeff Kirsher 
60224cf5adSJeff Kirsher #include <linux/string.h>
61224cf5adSJeff Kirsher #include <linux/module.h>
62224cf5adSJeff Kirsher #include <linux/kernel.h>
63224cf5adSJeff Kirsher #include <linux/slab.h>
64224cf5adSJeff Kirsher #include <linux/errno.h>
65224cf5adSJeff Kirsher #include <linux/netdevice.h>
66224cf5adSJeff Kirsher #include <linux/net.h>
67224cf5adSJeff Kirsher #include <linux/inetdevice.h>
68224cf5adSJeff Kirsher #include <linux/etherdevice.h>
69224cf5adSJeff Kirsher #include <linux/skbuff.h>
70224cf5adSJeff Kirsher #include <linux/init.h>
71224cf5adSJeff Kirsher #include <linux/if_ether.h>
72224cf5adSJeff Kirsher #include <linux/if_pppox.h>
73224cf5adSJeff Kirsher #include <linux/ppp_channel.h>
74224cf5adSJeff Kirsher #include <linux/ppp_defs.h>
754b32da2bSPaul Mackerras #include <linux/ppp-ioctl.h>
76224cf5adSJeff Kirsher #include <linux/notifier.h>
77224cf5adSJeff Kirsher #include <linux/file.h>
78224cf5adSJeff Kirsher #include <linux/proc_fs.h>
79224cf5adSJeff Kirsher #include <linux/seq_file.h>
80224cf5adSJeff Kirsher 
81224cf5adSJeff Kirsher #include <linux/nsproxy.h>
82224cf5adSJeff Kirsher #include <net/net_namespace.h>
83224cf5adSJeff Kirsher #include <net/netns/generic.h>
84224cf5adSJeff Kirsher #include <net/sock.h>
85224cf5adSJeff Kirsher 
86224cf5adSJeff Kirsher #include <asm/uaccess.h>
87224cf5adSJeff Kirsher 
88224cf5adSJeff Kirsher #define PPPOE_HASH_BITS 4
89224cf5adSJeff Kirsher #define PPPOE_HASH_SIZE (1 << PPPOE_HASH_BITS)
90224cf5adSJeff Kirsher #define PPPOE_HASH_MASK	(PPPOE_HASH_SIZE - 1)
91224cf5adSJeff Kirsher 
92224cf5adSJeff Kirsher static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
93224cf5adSJeff Kirsher 
94224cf5adSJeff Kirsher static const struct proto_ops pppoe_ops;
95224cf5adSJeff Kirsher static const struct ppp_channel_ops pppoe_chan_ops;
96224cf5adSJeff Kirsher 
97224cf5adSJeff Kirsher /* per-net private data for this module */
98224cf5adSJeff Kirsher static int pppoe_net_id __read_mostly;
99224cf5adSJeff Kirsher struct pppoe_net {
100224cf5adSJeff Kirsher 	/*
101224cf5adSJeff Kirsher 	 * we could use _single_ hash table for all
102224cf5adSJeff Kirsher 	 * nets by injecting net id into the hash but
103224cf5adSJeff Kirsher 	 * it would increase hash chains and add
104224cf5adSJeff Kirsher 	 * a few additional math comparations messy
105224cf5adSJeff Kirsher 	 * as well, moreover in case of SMP less locking
106224cf5adSJeff Kirsher 	 * controversy here
107224cf5adSJeff Kirsher 	 */
108224cf5adSJeff Kirsher 	struct pppox_sock *hash_table[PPPOE_HASH_SIZE];
109224cf5adSJeff Kirsher 	rwlock_t hash_lock;
110224cf5adSJeff Kirsher };
111224cf5adSJeff Kirsher 
112224cf5adSJeff Kirsher /*
113224cf5adSJeff Kirsher  * PPPoE could be in the following stages:
114224cf5adSJeff Kirsher  * 1) Discovery stage (to obtain remote MAC and Session ID)
115224cf5adSJeff Kirsher  * 2) Session stage (MAC and SID are known)
116224cf5adSJeff Kirsher  *
117224cf5adSJeff Kirsher  * Ethernet frames have a special tag for this but
118224cf5adSJeff Kirsher  * we use simpler approach based on session id
119224cf5adSJeff Kirsher  */
120224cf5adSJeff Kirsher static inline bool stage_session(__be16 sid)
121224cf5adSJeff Kirsher {
122224cf5adSJeff Kirsher 	return sid != 0;
123224cf5adSJeff Kirsher }
124224cf5adSJeff Kirsher 
125224cf5adSJeff Kirsher static inline struct pppoe_net *pppoe_pernet(struct net *net)
126224cf5adSJeff Kirsher {
127224cf5adSJeff Kirsher 	BUG_ON(!net);
128224cf5adSJeff Kirsher 
129224cf5adSJeff Kirsher 	return net_generic(net, pppoe_net_id);
130224cf5adSJeff Kirsher }
131224cf5adSJeff Kirsher 
132224cf5adSJeff Kirsher static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
133224cf5adSJeff Kirsher {
1347ed8ca5bSdingtianhong 	return a->sid == b->sid && ether_addr_equal(a->remote, b->remote);
135224cf5adSJeff Kirsher }
136224cf5adSJeff Kirsher 
137224cf5adSJeff Kirsher static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr)
138224cf5adSJeff Kirsher {
1397ed8ca5bSdingtianhong 	return a->sid == sid && ether_addr_equal(a->remote, addr);
140224cf5adSJeff Kirsher }
141224cf5adSJeff Kirsher 
142224cf5adSJeff Kirsher #if 8 % PPPOE_HASH_BITS
143224cf5adSJeff Kirsher #error 8 must be a multiple of PPPOE_HASH_BITS
144224cf5adSJeff Kirsher #endif
145224cf5adSJeff Kirsher 
146224cf5adSJeff Kirsher static int hash_item(__be16 sid, unsigned char *addr)
147224cf5adSJeff Kirsher {
148224cf5adSJeff Kirsher 	unsigned char hash = 0;
149224cf5adSJeff Kirsher 	unsigned int i;
150224cf5adSJeff Kirsher 
151224cf5adSJeff Kirsher 	for (i = 0; i < ETH_ALEN; i++)
152224cf5adSJeff Kirsher 		hash ^= addr[i];
153224cf5adSJeff Kirsher 	for (i = 0; i < sizeof(sid_t) * 8; i += 8)
154224cf5adSJeff Kirsher 		hash ^= (__force __u32)sid >> i;
155224cf5adSJeff Kirsher 	for (i = 8; (i >>= 1) >= PPPOE_HASH_BITS;)
156224cf5adSJeff Kirsher 		hash ^= hash >> i;
157224cf5adSJeff Kirsher 
158224cf5adSJeff Kirsher 	return hash & PPPOE_HASH_MASK;
159224cf5adSJeff Kirsher }
160224cf5adSJeff Kirsher 
161224cf5adSJeff Kirsher /**********************************************************************
162224cf5adSJeff Kirsher  *
163224cf5adSJeff Kirsher  *  Set/get/delete/rehash items  (internal versions)
164224cf5adSJeff Kirsher  *
165224cf5adSJeff Kirsher  **********************************************************************/
166224cf5adSJeff Kirsher static struct pppox_sock *__get_item(struct pppoe_net *pn, __be16 sid,
167224cf5adSJeff Kirsher 				unsigned char *addr, int ifindex)
168224cf5adSJeff Kirsher {
169224cf5adSJeff Kirsher 	int hash = hash_item(sid, addr);
170224cf5adSJeff Kirsher 	struct pppox_sock *ret;
171224cf5adSJeff Kirsher 
172224cf5adSJeff Kirsher 	ret = pn->hash_table[hash];
173224cf5adSJeff Kirsher 	while (ret) {
174224cf5adSJeff Kirsher 		if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
175224cf5adSJeff Kirsher 		    ret->pppoe_ifindex == ifindex)
176224cf5adSJeff Kirsher 			return ret;
177224cf5adSJeff Kirsher 
178224cf5adSJeff Kirsher 		ret = ret->next;
179224cf5adSJeff Kirsher 	}
180224cf5adSJeff Kirsher 
181224cf5adSJeff Kirsher 	return NULL;
182224cf5adSJeff Kirsher }
183224cf5adSJeff Kirsher 
184224cf5adSJeff Kirsher static int __set_item(struct pppoe_net *pn, struct pppox_sock *po)
185224cf5adSJeff Kirsher {
186224cf5adSJeff Kirsher 	int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
187224cf5adSJeff Kirsher 	struct pppox_sock *ret;
188224cf5adSJeff Kirsher 
189224cf5adSJeff Kirsher 	ret = pn->hash_table[hash];
190224cf5adSJeff Kirsher 	while (ret) {
191224cf5adSJeff Kirsher 		if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) &&
192224cf5adSJeff Kirsher 		    ret->pppoe_ifindex == po->pppoe_ifindex)
193224cf5adSJeff Kirsher 			return -EALREADY;
194224cf5adSJeff Kirsher 
195224cf5adSJeff Kirsher 		ret = ret->next;
196224cf5adSJeff Kirsher 	}
197224cf5adSJeff Kirsher 
198224cf5adSJeff Kirsher 	po->next = pn->hash_table[hash];
199224cf5adSJeff Kirsher 	pn->hash_table[hash] = po;
200224cf5adSJeff Kirsher 
201224cf5adSJeff Kirsher 	return 0;
202224cf5adSJeff Kirsher }
203224cf5adSJeff Kirsher 
2043b12bb60SRami Rosen static void __delete_item(struct pppoe_net *pn, __be16 sid,
205224cf5adSJeff Kirsher 					char *addr, int ifindex)
206224cf5adSJeff Kirsher {
207224cf5adSJeff Kirsher 	int hash = hash_item(sid, addr);
208224cf5adSJeff Kirsher 	struct pppox_sock *ret, **src;
209224cf5adSJeff Kirsher 
210224cf5adSJeff Kirsher 	ret = pn->hash_table[hash];
211224cf5adSJeff Kirsher 	src = &pn->hash_table[hash];
212224cf5adSJeff Kirsher 
213224cf5adSJeff Kirsher 	while (ret) {
214224cf5adSJeff Kirsher 		if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
215224cf5adSJeff Kirsher 		    ret->pppoe_ifindex == ifindex) {
216224cf5adSJeff Kirsher 			*src = ret->next;
217224cf5adSJeff Kirsher 			break;
218224cf5adSJeff Kirsher 		}
219224cf5adSJeff Kirsher 
220224cf5adSJeff Kirsher 		src = &ret->next;
221224cf5adSJeff Kirsher 		ret = ret->next;
222224cf5adSJeff Kirsher 	}
223224cf5adSJeff Kirsher }
224224cf5adSJeff Kirsher 
225224cf5adSJeff Kirsher /**********************************************************************
226224cf5adSJeff Kirsher  *
227224cf5adSJeff Kirsher  *  Set/get/delete/rehash items
228224cf5adSJeff Kirsher  *
229224cf5adSJeff Kirsher  **********************************************************************/
230224cf5adSJeff Kirsher static inline struct pppox_sock *get_item(struct pppoe_net *pn, __be16 sid,
231224cf5adSJeff Kirsher 					unsigned char *addr, int ifindex)
232224cf5adSJeff Kirsher {
233224cf5adSJeff Kirsher 	struct pppox_sock *po;
234224cf5adSJeff Kirsher 
235224cf5adSJeff Kirsher 	read_lock_bh(&pn->hash_lock);
236224cf5adSJeff Kirsher 	po = __get_item(pn, sid, addr, ifindex);
237224cf5adSJeff Kirsher 	if (po)
238224cf5adSJeff Kirsher 		sock_hold(sk_pppox(po));
239224cf5adSJeff Kirsher 	read_unlock_bh(&pn->hash_lock);
240224cf5adSJeff Kirsher 
241224cf5adSJeff Kirsher 	return po;
242224cf5adSJeff Kirsher }
243224cf5adSJeff Kirsher 
244224cf5adSJeff Kirsher static inline struct pppox_sock *get_item_by_addr(struct net *net,
245224cf5adSJeff Kirsher 						struct sockaddr_pppox *sp)
246224cf5adSJeff Kirsher {
247224cf5adSJeff Kirsher 	struct net_device *dev;
248224cf5adSJeff Kirsher 	struct pppoe_net *pn;
249224cf5adSJeff Kirsher 	struct pppox_sock *pppox_sock = NULL;
250224cf5adSJeff Kirsher 
251224cf5adSJeff Kirsher 	int ifindex;
252224cf5adSJeff Kirsher 
253224cf5adSJeff Kirsher 	rcu_read_lock();
254224cf5adSJeff Kirsher 	dev = dev_get_by_name_rcu(net, sp->sa_addr.pppoe.dev);
255224cf5adSJeff Kirsher 	if (dev) {
256224cf5adSJeff Kirsher 		ifindex = dev->ifindex;
257224cf5adSJeff Kirsher 		pn = pppoe_pernet(net);
258224cf5adSJeff Kirsher 		pppox_sock = get_item(pn, sp->sa_addr.pppoe.sid,
259224cf5adSJeff Kirsher 				sp->sa_addr.pppoe.remote, ifindex);
260224cf5adSJeff Kirsher 	}
261224cf5adSJeff Kirsher 	rcu_read_unlock();
262224cf5adSJeff Kirsher 	return pppox_sock;
263224cf5adSJeff Kirsher }
264224cf5adSJeff Kirsher 
2653b12bb60SRami Rosen static inline void delete_item(struct pppoe_net *pn, __be16 sid,
266224cf5adSJeff Kirsher 					char *addr, int ifindex)
267224cf5adSJeff Kirsher {
268224cf5adSJeff Kirsher 	write_lock_bh(&pn->hash_lock);
2693b12bb60SRami Rosen 	__delete_item(pn, sid, addr, ifindex);
270224cf5adSJeff Kirsher 	write_unlock_bh(&pn->hash_lock);
271224cf5adSJeff Kirsher }
272224cf5adSJeff Kirsher 
273224cf5adSJeff Kirsher /***************************************************************************
274224cf5adSJeff Kirsher  *
275224cf5adSJeff Kirsher  *  Handler for device events.
276224cf5adSJeff Kirsher  *  Certain device events require that sockets be unconnected.
277224cf5adSJeff Kirsher  *
278224cf5adSJeff Kirsher  **************************************************************************/
279224cf5adSJeff Kirsher 
280224cf5adSJeff Kirsher static void pppoe_flush_dev(struct net_device *dev)
281224cf5adSJeff Kirsher {
282224cf5adSJeff Kirsher 	struct pppoe_net *pn;
283224cf5adSJeff Kirsher 	int i;
284224cf5adSJeff Kirsher 
285224cf5adSJeff Kirsher 	pn = pppoe_pernet(dev_net(dev));
286224cf5adSJeff Kirsher 	write_lock_bh(&pn->hash_lock);
287224cf5adSJeff Kirsher 	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
288224cf5adSJeff Kirsher 		struct pppox_sock *po = pn->hash_table[i];
289224cf5adSJeff Kirsher 		struct sock *sk;
290224cf5adSJeff Kirsher 
291224cf5adSJeff Kirsher 		while (po) {
292224cf5adSJeff Kirsher 			while (po && po->pppoe_dev != dev) {
293224cf5adSJeff Kirsher 				po = po->next;
294224cf5adSJeff Kirsher 			}
295224cf5adSJeff Kirsher 
296224cf5adSJeff Kirsher 			if (!po)
297224cf5adSJeff Kirsher 				break;
298224cf5adSJeff Kirsher 
299224cf5adSJeff Kirsher 			sk = sk_pppox(po);
300224cf5adSJeff Kirsher 
301224cf5adSJeff Kirsher 			/* We always grab the socket lock, followed by the
302224cf5adSJeff Kirsher 			 * hash_lock, in that order.  Since we should hold the
303224cf5adSJeff Kirsher 			 * sock lock while doing any unbinding, we need to
304224cf5adSJeff Kirsher 			 * release the lock we're holding.  Hold a reference to
305224cf5adSJeff Kirsher 			 * the sock so it doesn't disappear as we're jumping
306224cf5adSJeff Kirsher 			 * between locks.
307224cf5adSJeff Kirsher 			 */
308224cf5adSJeff Kirsher 
309224cf5adSJeff Kirsher 			sock_hold(sk);
310224cf5adSJeff Kirsher 			write_unlock_bh(&pn->hash_lock);
311224cf5adSJeff Kirsher 			lock_sock(sk);
312224cf5adSJeff Kirsher 
313224cf5adSJeff Kirsher 			if (po->pppoe_dev == dev &&
314224cf5adSJeff Kirsher 			    sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND | PPPOX_ZOMBIE)) {
315224cf5adSJeff Kirsher 				pppox_unbind_sock(sk);
316224cf5adSJeff Kirsher 				sk->sk_state_change(sk);
317224cf5adSJeff Kirsher 				po->pppoe_dev = NULL;
318224cf5adSJeff Kirsher 				dev_put(dev);
319224cf5adSJeff Kirsher 			}
320224cf5adSJeff Kirsher 
321224cf5adSJeff Kirsher 			release_sock(sk);
322224cf5adSJeff Kirsher 			sock_put(sk);
323224cf5adSJeff Kirsher 
324224cf5adSJeff Kirsher 			/* Restart the process from the start of the current
325224cf5adSJeff Kirsher 			 * hash chain. We dropped locks so the world may have
326224cf5adSJeff Kirsher 			 * change from underneath us.
327224cf5adSJeff Kirsher 			 */
328224cf5adSJeff Kirsher 
329224cf5adSJeff Kirsher 			BUG_ON(pppoe_pernet(dev_net(dev)) == NULL);
330224cf5adSJeff Kirsher 			write_lock_bh(&pn->hash_lock);
331224cf5adSJeff Kirsher 			po = pn->hash_table[i];
332224cf5adSJeff Kirsher 		}
333224cf5adSJeff Kirsher 	}
334224cf5adSJeff Kirsher 	write_unlock_bh(&pn->hash_lock);
335224cf5adSJeff Kirsher }
336224cf5adSJeff Kirsher 
337224cf5adSJeff Kirsher static int pppoe_device_event(struct notifier_block *this,
338224cf5adSJeff Kirsher 			      unsigned long event, void *ptr)
339224cf5adSJeff Kirsher {
340351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
341224cf5adSJeff Kirsher 
342224cf5adSJeff Kirsher 	/* Only look at sockets that are using this specific device. */
343224cf5adSJeff Kirsher 	switch (event) {
344224cf5adSJeff Kirsher 	case NETDEV_CHANGEADDR:
345224cf5adSJeff Kirsher 	case NETDEV_CHANGEMTU:
346224cf5adSJeff Kirsher 		/* A change in mtu or address is a bad thing, requiring
347224cf5adSJeff Kirsher 		 * LCP re-negotiation.
348224cf5adSJeff Kirsher 		 */
349224cf5adSJeff Kirsher 
350224cf5adSJeff Kirsher 	case NETDEV_GOING_DOWN:
351224cf5adSJeff Kirsher 	case NETDEV_DOWN:
352224cf5adSJeff Kirsher 		/* Find every socket on this device and kill it. */
353224cf5adSJeff Kirsher 		pppoe_flush_dev(dev);
354224cf5adSJeff Kirsher 		break;
355224cf5adSJeff Kirsher 
356224cf5adSJeff Kirsher 	default:
357224cf5adSJeff Kirsher 		break;
358224cf5adSJeff Kirsher 	}
359224cf5adSJeff Kirsher 
360224cf5adSJeff Kirsher 	return NOTIFY_DONE;
361224cf5adSJeff Kirsher }
362224cf5adSJeff Kirsher 
363224cf5adSJeff Kirsher static struct notifier_block pppoe_notifier = {
364224cf5adSJeff Kirsher 	.notifier_call = pppoe_device_event,
365224cf5adSJeff Kirsher };
366224cf5adSJeff Kirsher 
367224cf5adSJeff Kirsher /************************************************************************
368224cf5adSJeff Kirsher  *
369224cf5adSJeff Kirsher  * Do the real work of receiving a PPPoE Session frame.
370224cf5adSJeff Kirsher  *
371224cf5adSJeff Kirsher  ***********************************************************************/
372224cf5adSJeff Kirsher static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
373224cf5adSJeff Kirsher {
374224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
375224cf5adSJeff Kirsher 	struct pppox_sock *relay_po;
376224cf5adSJeff Kirsher 
377224cf5adSJeff Kirsher 	/* Backlog receive. Semantics of backlog rcv preclude any code from
378224cf5adSJeff Kirsher 	 * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state
379224cf5adSJeff Kirsher 	 * can't change.
380224cf5adSJeff Kirsher 	 */
381224cf5adSJeff Kirsher 
382a068833bSJoakim Tjernlund 	if (skb->pkt_type == PACKET_OTHERHOST)
383a068833bSJoakim Tjernlund 		goto abort_kfree;
384a068833bSJoakim Tjernlund 
385224cf5adSJeff Kirsher 	if (sk->sk_state & PPPOX_BOUND) {
386224cf5adSJeff Kirsher 		ppp_input(&po->chan, skb);
387224cf5adSJeff Kirsher 	} else if (sk->sk_state & PPPOX_RELAY) {
388224cf5adSJeff Kirsher 		relay_po = get_item_by_addr(sock_net(sk),
389224cf5adSJeff Kirsher 					    &po->pppoe_relay);
390224cf5adSJeff Kirsher 		if (relay_po == NULL)
391224cf5adSJeff Kirsher 			goto abort_kfree;
392224cf5adSJeff Kirsher 
393224cf5adSJeff Kirsher 		if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
394224cf5adSJeff Kirsher 			goto abort_put;
395224cf5adSJeff Kirsher 
396224cf5adSJeff Kirsher 		if (!__pppoe_xmit(sk_pppox(relay_po), skb))
397224cf5adSJeff Kirsher 			goto abort_put;
398224cf5adSJeff Kirsher 	} else {
399224cf5adSJeff Kirsher 		if (sock_queue_rcv_skb(sk, skb))
400224cf5adSJeff Kirsher 			goto abort_kfree;
401224cf5adSJeff Kirsher 	}
402224cf5adSJeff Kirsher 
403224cf5adSJeff Kirsher 	return NET_RX_SUCCESS;
404224cf5adSJeff Kirsher 
405224cf5adSJeff Kirsher abort_put:
406224cf5adSJeff Kirsher 	sock_put(sk_pppox(relay_po));
407224cf5adSJeff Kirsher 
408224cf5adSJeff Kirsher abort_kfree:
409224cf5adSJeff Kirsher 	kfree_skb(skb);
410224cf5adSJeff Kirsher 	return NET_RX_DROP;
411224cf5adSJeff Kirsher }
412224cf5adSJeff Kirsher 
413224cf5adSJeff Kirsher /************************************************************************
414224cf5adSJeff Kirsher  *
415224cf5adSJeff Kirsher  * Receive wrapper called in BH context.
416224cf5adSJeff Kirsher  *
417224cf5adSJeff Kirsher  ***********************************************************************/
418224cf5adSJeff Kirsher static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev,
419224cf5adSJeff Kirsher 		     struct packet_type *pt, struct net_device *orig_dev)
420224cf5adSJeff Kirsher {
421224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
422224cf5adSJeff Kirsher 	struct pppox_sock *po;
423224cf5adSJeff Kirsher 	struct pppoe_net *pn;
424224cf5adSJeff Kirsher 	int len;
425224cf5adSJeff Kirsher 
426224cf5adSJeff Kirsher 	skb = skb_share_check(skb, GFP_ATOMIC);
427224cf5adSJeff Kirsher 	if (!skb)
428224cf5adSJeff Kirsher 		goto out;
429224cf5adSJeff Kirsher 
430224cf5adSJeff Kirsher 	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
431224cf5adSJeff Kirsher 		goto drop;
432224cf5adSJeff Kirsher 
433224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
434224cf5adSJeff Kirsher 	len = ntohs(ph->length);
435224cf5adSJeff Kirsher 
436224cf5adSJeff Kirsher 	skb_pull_rcsum(skb, sizeof(*ph));
437224cf5adSJeff Kirsher 	if (skb->len < len)
438224cf5adSJeff Kirsher 		goto drop;
439224cf5adSJeff Kirsher 
440224cf5adSJeff Kirsher 	if (pskb_trim_rcsum(skb, len))
441224cf5adSJeff Kirsher 		goto drop;
442224cf5adSJeff Kirsher 
443224cf5adSJeff Kirsher 	pn = pppoe_pernet(dev_net(dev));
444224cf5adSJeff Kirsher 
445224cf5adSJeff Kirsher 	/* Note that get_item does a sock_hold(), so sk_pppox(po)
446224cf5adSJeff Kirsher 	 * is known to be safe.
447224cf5adSJeff Kirsher 	 */
448224cf5adSJeff Kirsher 	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
449224cf5adSJeff Kirsher 	if (!po)
450224cf5adSJeff Kirsher 		goto drop;
451224cf5adSJeff Kirsher 
452224cf5adSJeff Kirsher 	return sk_receive_skb(sk_pppox(po), skb, 0);
453224cf5adSJeff Kirsher 
454224cf5adSJeff Kirsher drop:
455224cf5adSJeff Kirsher 	kfree_skb(skb);
456224cf5adSJeff Kirsher out:
457224cf5adSJeff Kirsher 	return NET_RX_DROP;
458224cf5adSJeff Kirsher }
459224cf5adSJeff Kirsher 
460287f3a94SSimon Farnsworth static void pppoe_unbind_sock_work(struct work_struct *work)
461287f3a94SSimon Farnsworth {
462287f3a94SSimon Farnsworth 	struct pppox_sock *po = container_of(work, struct pppox_sock,
463287f3a94SSimon Farnsworth 					     proto.pppoe.padt_work);
464287f3a94SSimon Farnsworth 	struct sock *sk = sk_pppox(po);
465287f3a94SSimon Farnsworth 
466287f3a94SSimon Farnsworth 	lock_sock(sk);
467665a6cd8SFelix Fietkau 	if (po->pppoe_dev) {
468665a6cd8SFelix Fietkau 		dev_put(po->pppoe_dev);
469665a6cd8SFelix Fietkau 		po->pppoe_dev = NULL;
470665a6cd8SFelix Fietkau 	}
471287f3a94SSimon Farnsworth 	pppox_unbind_sock(sk);
472287f3a94SSimon Farnsworth 	release_sock(sk);
473287f3a94SSimon Farnsworth 	sock_put(sk);
474287f3a94SSimon Farnsworth }
475287f3a94SSimon Farnsworth 
476224cf5adSJeff Kirsher /************************************************************************
477224cf5adSJeff Kirsher  *
478224cf5adSJeff Kirsher  * Receive a PPPoE Discovery frame.
479224cf5adSJeff Kirsher  * This is solely for detection of PADT frames
480224cf5adSJeff Kirsher  *
481224cf5adSJeff Kirsher  ***********************************************************************/
482224cf5adSJeff Kirsher static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev,
483224cf5adSJeff Kirsher 			  struct packet_type *pt, struct net_device *orig_dev)
484224cf5adSJeff Kirsher 
485224cf5adSJeff Kirsher {
486224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
487224cf5adSJeff Kirsher 	struct pppox_sock *po;
488224cf5adSJeff Kirsher 	struct pppoe_net *pn;
489224cf5adSJeff Kirsher 
490224cf5adSJeff Kirsher 	skb = skb_share_check(skb, GFP_ATOMIC);
491224cf5adSJeff Kirsher 	if (!skb)
492224cf5adSJeff Kirsher 		goto out;
493224cf5adSJeff Kirsher 
494224cf5adSJeff Kirsher 	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
495224cf5adSJeff Kirsher 		goto abort;
496224cf5adSJeff Kirsher 
497224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
498224cf5adSJeff Kirsher 	if (ph->code != PADT_CODE)
499224cf5adSJeff Kirsher 		goto abort;
500224cf5adSJeff Kirsher 
501224cf5adSJeff Kirsher 	pn = pppoe_pernet(dev_net(dev));
502224cf5adSJeff Kirsher 	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
503224cf5adSJeff Kirsher 	if (po) {
504224cf5adSJeff Kirsher 		struct sock *sk = sk_pppox(po);
505224cf5adSJeff Kirsher 
506224cf5adSJeff Kirsher 		bh_lock_sock(sk);
507224cf5adSJeff Kirsher 
508224cf5adSJeff Kirsher 		/* If the user has locked the socket, just ignore
509224cf5adSJeff Kirsher 		 * the packet.  With the way two rcv protocols hook into
510224cf5adSJeff Kirsher 		 * one socket family type, we cannot (easily) distinguish
511224cf5adSJeff Kirsher 		 * what kind of SKB it is during backlog rcv.
512224cf5adSJeff Kirsher 		 */
513224cf5adSJeff Kirsher 		if (sock_owned_by_user(sk) == 0) {
514224cf5adSJeff Kirsher 			/* We're no longer connect at the PPPOE layer,
515224cf5adSJeff Kirsher 			 * and must wait for ppp channel to disconnect us.
516224cf5adSJeff Kirsher 			 */
517224cf5adSJeff Kirsher 			sk->sk_state = PPPOX_ZOMBIE;
518224cf5adSJeff Kirsher 		}
519224cf5adSJeff Kirsher 
520224cf5adSJeff Kirsher 		bh_unlock_sock(sk);
521287f3a94SSimon Farnsworth 		if (!schedule_work(&po->proto.pppoe.padt_work))
522224cf5adSJeff Kirsher 			sock_put(sk);
523224cf5adSJeff Kirsher 	}
524224cf5adSJeff Kirsher 
525224cf5adSJeff Kirsher abort:
526224cf5adSJeff Kirsher 	kfree_skb(skb);
527224cf5adSJeff Kirsher out:
528224cf5adSJeff Kirsher 	return NET_RX_SUCCESS; /* Lies... :-) */
529224cf5adSJeff Kirsher }
530224cf5adSJeff Kirsher 
531224cf5adSJeff Kirsher static struct packet_type pppoes_ptype __read_mostly = {
532224cf5adSJeff Kirsher 	.type	= cpu_to_be16(ETH_P_PPP_SES),
533224cf5adSJeff Kirsher 	.func	= pppoe_rcv,
534224cf5adSJeff Kirsher };
535224cf5adSJeff Kirsher 
536224cf5adSJeff Kirsher static struct packet_type pppoed_ptype __read_mostly = {
537224cf5adSJeff Kirsher 	.type	= cpu_to_be16(ETH_P_PPP_DISC),
538224cf5adSJeff Kirsher 	.func	= pppoe_disc_rcv,
539224cf5adSJeff Kirsher };
540224cf5adSJeff Kirsher 
541224cf5adSJeff Kirsher static struct proto pppoe_sk_proto __read_mostly = {
542224cf5adSJeff Kirsher 	.name	  = "PPPOE",
543224cf5adSJeff Kirsher 	.owner	  = THIS_MODULE,
544224cf5adSJeff Kirsher 	.obj_size = sizeof(struct pppox_sock),
545224cf5adSJeff Kirsher };
546224cf5adSJeff Kirsher 
547224cf5adSJeff Kirsher /***********************************************************************
548224cf5adSJeff Kirsher  *
549224cf5adSJeff Kirsher  * Initialize a new struct sock.
550224cf5adSJeff Kirsher  *
551224cf5adSJeff Kirsher  **********************************************************************/
55211aa9c28SEric W. Biederman static int pppoe_create(struct net *net, struct socket *sock, int kern)
553224cf5adSJeff Kirsher {
554224cf5adSJeff Kirsher 	struct sock *sk;
555224cf5adSJeff Kirsher 
55611aa9c28SEric W. Biederman 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, kern);
557224cf5adSJeff Kirsher 	if (!sk)
558224cf5adSJeff Kirsher 		return -ENOMEM;
559224cf5adSJeff Kirsher 
560224cf5adSJeff Kirsher 	sock_init_data(sock, sk);
561224cf5adSJeff Kirsher 
562224cf5adSJeff Kirsher 	sock->state	= SS_UNCONNECTED;
563224cf5adSJeff Kirsher 	sock->ops	= &pppoe_ops;
564224cf5adSJeff Kirsher 
565224cf5adSJeff Kirsher 	sk->sk_backlog_rcv	= pppoe_rcv_core;
566224cf5adSJeff Kirsher 	sk->sk_state		= PPPOX_NONE;
567224cf5adSJeff Kirsher 	sk->sk_type		= SOCK_STREAM;
568224cf5adSJeff Kirsher 	sk->sk_family		= PF_PPPOX;
569224cf5adSJeff Kirsher 	sk->sk_protocol		= PX_PROTO_OE;
570224cf5adSJeff Kirsher 
571224cf5adSJeff Kirsher 	return 0;
572224cf5adSJeff Kirsher }
573224cf5adSJeff Kirsher 
574224cf5adSJeff Kirsher static int pppoe_release(struct socket *sock)
575224cf5adSJeff Kirsher {
576224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
577224cf5adSJeff Kirsher 	struct pppox_sock *po;
578224cf5adSJeff Kirsher 	struct pppoe_net *pn;
579224cf5adSJeff Kirsher 	struct net *net = NULL;
580224cf5adSJeff Kirsher 
581224cf5adSJeff Kirsher 	if (!sk)
582224cf5adSJeff Kirsher 		return 0;
583224cf5adSJeff Kirsher 
584224cf5adSJeff Kirsher 	lock_sock(sk);
585224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD)) {
586224cf5adSJeff Kirsher 		release_sock(sk);
587224cf5adSJeff Kirsher 		return -EBADF;
588224cf5adSJeff Kirsher 	}
589224cf5adSJeff Kirsher 
590224cf5adSJeff Kirsher 	po = pppox_sk(sk);
591224cf5adSJeff Kirsher 
5921acea4f6SGuillaume Nault 	if (po->pppoe_dev) {
593224cf5adSJeff Kirsher 		dev_put(po->pppoe_dev);
594224cf5adSJeff Kirsher 		po->pppoe_dev = NULL;
595224cf5adSJeff Kirsher 	}
596224cf5adSJeff Kirsher 
597224cf5adSJeff Kirsher 	pppox_unbind_sock(sk);
598224cf5adSJeff Kirsher 
599224cf5adSJeff Kirsher 	/* Signal the death of the socket. */
600224cf5adSJeff Kirsher 	sk->sk_state = PPPOX_DEAD;
601224cf5adSJeff Kirsher 
602224cf5adSJeff Kirsher 	net = sock_net(sk);
603224cf5adSJeff Kirsher 	pn = pppoe_pernet(net);
604224cf5adSJeff Kirsher 
605224cf5adSJeff Kirsher 	/*
606224cf5adSJeff Kirsher 	 * protect "po" from concurrent updates
607224cf5adSJeff Kirsher 	 * on pppoe_flush_dev
608224cf5adSJeff Kirsher 	 */
609224cf5adSJeff Kirsher 	delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
610224cf5adSJeff Kirsher 		    po->pppoe_ifindex);
611224cf5adSJeff Kirsher 
612224cf5adSJeff Kirsher 	sock_orphan(sk);
613224cf5adSJeff Kirsher 	sock->sk = NULL;
614224cf5adSJeff Kirsher 
615224cf5adSJeff Kirsher 	skb_queue_purge(&sk->sk_receive_queue);
616224cf5adSJeff Kirsher 	release_sock(sk);
617224cf5adSJeff Kirsher 	sock_put(sk);
618224cf5adSJeff Kirsher 
619224cf5adSJeff Kirsher 	return 0;
620224cf5adSJeff Kirsher }
621224cf5adSJeff Kirsher 
622224cf5adSJeff Kirsher static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
623224cf5adSJeff Kirsher 		  int sockaddr_len, int flags)
624224cf5adSJeff Kirsher {
625224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
626224cf5adSJeff Kirsher 	struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr;
627224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
628224cf5adSJeff Kirsher 	struct net_device *dev = NULL;
629224cf5adSJeff Kirsher 	struct pppoe_net *pn;
630224cf5adSJeff Kirsher 	struct net *net = NULL;
631224cf5adSJeff Kirsher 	int error;
632224cf5adSJeff Kirsher 
633224cf5adSJeff Kirsher 	lock_sock(sk);
634224cf5adSJeff Kirsher 
635287f3a94SSimon Farnsworth 	INIT_WORK(&po->proto.pppoe.padt_work, pppoe_unbind_sock_work);
636287f3a94SSimon Farnsworth 
637224cf5adSJeff Kirsher 	error = -EINVAL;
638224cf5adSJeff Kirsher 	if (sp->sa_protocol != PX_PROTO_OE)
639224cf5adSJeff Kirsher 		goto end;
640224cf5adSJeff Kirsher 
641224cf5adSJeff Kirsher 	/* Check for already bound sockets */
642224cf5adSJeff Kirsher 	error = -EBUSY;
643224cf5adSJeff Kirsher 	if ((sk->sk_state & PPPOX_CONNECTED) &&
644224cf5adSJeff Kirsher 	     stage_session(sp->sa_addr.pppoe.sid))
645224cf5adSJeff Kirsher 		goto end;
646224cf5adSJeff Kirsher 
647224cf5adSJeff Kirsher 	/* Check for already disconnected sockets, on attempts to disconnect */
648224cf5adSJeff Kirsher 	error = -EALREADY;
649224cf5adSJeff Kirsher 	if ((sk->sk_state & PPPOX_DEAD) &&
650224cf5adSJeff Kirsher 	     !stage_session(sp->sa_addr.pppoe.sid))
651224cf5adSJeff Kirsher 		goto end;
652224cf5adSJeff Kirsher 
653224cf5adSJeff Kirsher 	error = 0;
654224cf5adSJeff Kirsher 
655224cf5adSJeff Kirsher 	/* Delete the old binding */
656224cf5adSJeff Kirsher 	if (stage_session(po->pppoe_pa.sid)) {
657224cf5adSJeff Kirsher 		pppox_unbind_sock(sk);
658224cf5adSJeff Kirsher 		pn = pppoe_pernet(sock_net(sk));
659224cf5adSJeff Kirsher 		delete_item(pn, po->pppoe_pa.sid,
660224cf5adSJeff Kirsher 			    po->pppoe_pa.remote, po->pppoe_ifindex);
661224cf5adSJeff Kirsher 		if (po->pppoe_dev) {
662224cf5adSJeff Kirsher 			dev_put(po->pppoe_dev);
663224cf5adSJeff Kirsher 			po->pppoe_dev = NULL;
664224cf5adSJeff Kirsher 		}
665224cf5adSJeff Kirsher 
666224cf5adSJeff Kirsher 		memset(sk_pppox(po) + 1, 0,
667224cf5adSJeff Kirsher 		       sizeof(struct pppox_sock) - sizeof(struct sock));
668224cf5adSJeff Kirsher 		sk->sk_state = PPPOX_NONE;
669224cf5adSJeff Kirsher 	}
670224cf5adSJeff Kirsher 
671224cf5adSJeff Kirsher 	/* Re-bind in session stage only */
672224cf5adSJeff Kirsher 	if (stage_session(sp->sa_addr.pppoe.sid)) {
673224cf5adSJeff Kirsher 		error = -ENODEV;
674224cf5adSJeff Kirsher 		net = sock_net(sk);
675224cf5adSJeff Kirsher 		dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev);
676224cf5adSJeff Kirsher 		if (!dev)
677224cf5adSJeff Kirsher 			goto err_put;
678224cf5adSJeff Kirsher 
679224cf5adSJeff Kirsher 		po->pppoe_dev = dev;
680224cf5adSJeff Kirsher 		po->pppoe_ifindex = dev->ifindex;
681224cf5adSJeff Kirsher 		pn = pppoe_pernet(net);
682224cf5adSJeff Kirsher 		if (!(dev->flags & IFF_UP)) {
683224cf5adSJeff Kirsher 			goto err_put;
684224cf5adSJeff Kirsher 		}
685224cf5adSJeff Kirsher 
686224cf5adSJeff Kirsher 		memcpy(&po->pppoe_pa,
687224cf5adSJeff Kirsher 		       &sp->sa_addr.pppoe,
688224cf5adSJeff Kirsher 		       sizeof(struct pppoe_addr));
689224cf5adSJeff Kirsher 
690224cf5adSJeff Kirsher 		write_lock_bh(&pn->hash_lock);
691224cf5adSJeff Kirsher 		error = __set_item(pn, po);
692224cf5adSJeff Kirsher 		write_unlock_bh(&pn->hash_lock);
693224cf5adSJeff Kirsher 		if (error < 0)
694224cf5adSJeff Kirsher 			goto err_put;
695224cf5adSJeff Kirsher 
696224cf5adSJeff Kirsher 		po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
697224cf5adSJeff Kirsher 				   dev->hard_header_len);
698224cf5adSJeff Kirsher 
699a8a3e41cSChristoph Schulz 		po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr) - 2;
700224cf5adSJeff Kirsher 		po->chan.private = sk;
701224cf5adSJeff Kirsher 		po->chan.ops = &pppoe_chan_ops;
702224cf5adSJeff Kirsher 
703224cf5adSJeff Kirsher 		error = ppp_register_net_channel(dev_net(dev), &po->chan);
704224cf5adSJeff Kirsher 		if (error) {
705224cf5adSJeff Kirsher 			delete_item(pn, po->pppoe_pa.sid,
706224cf5adSJeff Kirsher 				    po->pppoe_pa.remote, po->pppoe_ifindex);
707224cf5adSJeff Kirsher 			goto err_put;
708224cf5adSJeff Kirsher 		}
709224cf5adSJeff Kirsher 
710224cf5adSJeff Kirsher 		sk->sk_state = PPPOX_CONNECTED;
711224cf5adSJeff Kirsher 	}
712224cf5adSJeff Kirsher 
713224cf5adSJeff Kirsher 	po->num = sp->sa_addr.pppoe.sid;
714224cf5adSJeff Kirsher 
715224cf5adSJeff Kirsher end:
716224cf5adSJeff Kirsher 	release_sock(sk);
717224cf5adSJeff Kirsher 	return error;
718224cf5adSJeff Kirsher err_put:
719224cf5adSJeff Kirsher 	if (po->pppoe_dev) {
720224cf5adSJeff Kirsher 		dev_put(po->pppoe_dev);
721224cf5adSJeff Kirsher 		po->pppoe_dev = NULL;
722224cf5adSJeff Kirsher 	}
723224cf5adSJeff Kirsher 	goto end;
724224cf5adSJeff Kirsher }
725224cf5adSJeff Kirsher 
726224cf5adSJeff Kirsher static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
727224cf5adSJeff Kirsher 		  int *usockaddr_len, int peer)
728224cf5adSJeff Kirsher {
729224cf5adSJeff Kirsher 	int len = sizeof(struct sockaddr_pppox);
730224cf5adSJeff Kirsher 	struct sockaddr_pppox sp;
731224cf5adSJeff Kirsher 
732224cf5adSJeff Kirsher 	sp.sa_family	= AF_PPPOX;
733224cf5adSJeff Kirsher 	sp.sa_protocol	= PX_PROTO_OE;
734224cf5adSJeff Kirsher 	memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
735224cf5adSJeff Kirsher 	       sizeof(struct pppoe_addr));
736224cf5adSJeff Kirsher 
737224cf5adSJeff Kirsher 	memcpy(uaddr, &sp, len);
738224cf5adSJeff Kirsher 
739224cf5adSJeff Kirsher 	*usockaddr_len = len;
740224cf5adSJeff Kirsher 
741224cf5adSJeff Kirsher 	return 0;
742224cf5adSJeff Kirsher }
743224cf5adSJeff Kirsher 
744224cf5adSJeff Kirsher static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
745224cf5adSJeff Kirsher 		unsigned long arg)
746224cf5adSJeff Kirsher {
747224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
748224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
749224cf5adSJeff Kirsher 	int val;
750224cf5adSJeff Kirsher 	int err;
751224cf5adSJeff Kirsher 
752224cf5adSJeff Kirsher 	switch (cmd) {
753224cf5adSJeff Kirsher 	case PPPIOCGMRU:
754224cf5adSJeff Kirsher 		err = -ENXIO;
755224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
756224cf5adSJeff Kirsher 			break;
757224cf5adSJeff Kirsher 
758224cf5adSJeff Kirsher 		err = -EFAULT;
759224cf5adSJeff Kirsher 		if (put_user(po->pppoe_dev->mtu -
760224cf5adSJeff Kirsher 			     sizeof(struct pppoe_hdr) -
761224cf5adSJeff Kirsher 			     PPP_HDRLEN,
762224cf5adSJeff Kirsher 			     (int __user *)arg))
763224cf5adSJeff Kirsher 			break;
764224cf5adSJeff Kirsher 		err = 0;
765224cf5adSJeff Kirsher 		break;
766224cf5adSJeff Kirsher 
767224cf5adSJeff Kirsher 	case PPPIOCSMRU:
768224cf5adSJeff Kirsher 		err = -ENXIO;
769224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
770224cf5adSJeff Kirsher 			break;
771224cf5adSJeff Kirsher 
772224cf5adSJeff Kirsher 		err = -EFAULT;
773224cf5adSJeff Kirsher 		if (get_user(val, (int __user *)arg))
774224cf5adSJeff Kirsher 			break;
775224cf5adSJeff Kirsher 
776224cf5adSJeff Kirsher 		if (val < (po->pppoe_dev->mtu
777224cf5adSJeff Kirsher 			   - sizeof(struct pppoe_hdr)
778224cf5adSJeff Kirsher 			   - PPP_HDRLEN))
779224cf5adSJeff Kirsher 			err = 0;
780224cf5adSJeff Kirsher 		else
781224cf5adSJeff Kirsher 			err = -EINVAL;
782224cf5adSJeff Kirsher 		break;
783224cf5adSJeff Kirsher 
784224cf5adSJeff Kirsher 	case PPPIOCSFLAGS:
785224cf5adSJeff Kirsher 		err = -EFAULT;
786224cf5adSJeff Kirsher 		if (get_user(val, (int __user *)arg))
787224cf5adSJeff Kirsher 			break;
788224cf5adSJeff Kirsher 		err = 0;
789224cf5adSJeff Kirsher 		break;
790224cf5adSJeff Kirsher 
791224cf5adSJeff Kirsher 	case PPPOEIOCSFWD:
792224cf5adSJeff Kirsher 	{
793224cf5adSJeff Kirsher 		struct pppox_sock *relay_po;
794224cf5adSJeff Kirsher 
795224cf5adSJeff Kirsher 		err = -EBUSY;
796224cf5adSJeff Kirsher 		if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
797224cf5adSJeff Kirsher 			break;
798224cf5adSJeff Kirsher 
799224cf5adSJeff Kirsher 		err = -ENOTCONN;
800224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
801224cf5adSJeff Kirsher 			break;
802224cf5adSJeff Kirsher 
803224cf5adSJeff Kirsher 		/* PPPoE address from the user specifies an outbound
804224cf5adSJeff Kirsher 		   PPPoE address which frames are forwarded to */
805224cf5adSJeff Kirsher 		err = -EFAULT;
806224cf5adSJeff Kirsher 		if (copy_from_user(&po->pppoe_relay,
807224cf5adSJeff Kirsher 				   (void __user *)arg,
808224cf5adSJeff Kirsher 				   sizeof(struct sockaddr_pppox)))
809224cf5adSJeff Kirsher 			break;
810224cf5adSJeff Kirsher 
811224cf5adSJeff Kirsher 		err = -EINVAL;
812224cf5adSJeff Kirsher 		if (po->pppoe_relay.sa_family != AF_PPPOX ||
813224cf5adSJeff Kirsher 		    po->pppoe_relay.sa_protocol != PX_PROTO_OE)
814224cf5adSJeff Kirsher 			break;
815224cf5adSJeff Kirsher 
816224cf5adSJeff Kirsher 		/* Check that the socket referenced by the address
817224cf5adSJeff Kirsher 		   actually exists. */
818224cf5adSJeff Kirsher 		relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
819224cf5adSJeff Kirsher 		if (!relay_po)
820224cf5adSJeff Kirsher 			break;
821224cf5adSJeff Kirsher 
822224cf5adSJeff Kirsher 		sock_put(sk_pppox(relay_po));
823224cf5adSJeff Kirsher 		sk->sk_state |= PPPOX_RELAY;
824224cf5adSJeff Kirsher 		err = 0;
825224cf5adSJeff Kirsher 		break;
826224cf5adSJeff Kirsher 	}
827224cf5adSJeff Kirsher 
828224cf5adSJeff Kirsher 	case PPPOEIOCDFWD:
829224cf5adSJeff Kirsher 		err = -EALREADY;
830224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_RELAY))
831224cf5adSJeff Kirsher 			break;
832224cf5adSJeff Kirsher 
833224cf5adSJeff Kirsher 		sk->sk_state &= ~PPPOX_RELAY;
834224cf5adSJeff Kirsher 		err = 0;
835224cf5adSJeff Kirsher 		break;
836224cf5adSJeff Kirsher 
837224cf5adSJeff Kirsher 	default:
838224cf5adSJeff Kirsher 		err = -ENOTTY;
839224cf5adSJeff Kirsher 	}
840224cf5adSJeff Kirsher 
841224cf5adSJeff Kirsher 	return err;
842224cf5adSJeff Kirsher }
843224cf5adSJeff Kirsher 
8441b784140SYing Xue static int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
8451b784140SYing Xue 			 size_t total_len)
846224cf5adSJeff Kirsher {
847224cf5adSJeff Kirsher 	struct sk_buff *skb;
848224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
849224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
850224cf5adSJeff Kirsher 	int error;
851224cf5adSJeff Kirsher 	struct pppoe_hdr hdr;
852224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
853224cf5adSJeff Kirsher 	struct net_device *dev;
854224cf5adSJeff Kirsher 	char *start;
855224cf5adSJeff Kirsher 
856224cf5adSJeff Kirsher 	lock_sock(sk);
857224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
858224cf5adSJeff Kirsher 		error = -ENOTCONN;
859224cf5adSJeff Kirsher 		goto end;
860224cf5adSJeff Kirsher 	}
861224cf5adSJeff Kirsher 
862224cf5adSJeff Kirsher 	hdr.ver = 1;
863224cf5adSJeff Kirsher 	hdr.type = 1;
864224cf5adSJeff Kirsher 	hdr.code = 0;
865224cf5adSJeff Kirsher 	hdr.sid = po->num;
866224cf5adSJeff Kirsher 
867224cf5adSJeff Kirsher 	dev = po->pppoe_dev;
868224cf5adSJeff Kirsher 
869224cf5adSJeff Kirsher 	error = -EMSGSIZE;
870224cf5adSJeff Kirsher 	if (total_len > (dev->mtu + dev->hard_header_len))
871224cf5adSJeff Kirsher 		goto end;
872224cf5adSJeff Kirsher 
873224cf5adSJeff Kirsher 
874224cf5adSJeff Kirsher 	skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
875224cf5adSJeff Kirsher 			   0, GFP_KERNEL);
876224cf5adSJeff Kirsher 	if (!skb) {
877224cf5adSJeff Kirsher 		error = -ENOMEM;
878224cf5adSJeff Kirsher 		goto end;
879224cf5adSJeff Kirsher 	}
880224cf5adSJeff Kirsher 
881224cf5adSJeff Kirsher 	/* Reserve space for headers. */
882224cf5adSJeff Kirsher 	skb_reserve(skb, dev->hard_header_len);
883224cf5adSJeff Kirsher 	skb_reset_network_header(skb);
884224cf5adSJeff Kirsher 
885224cf5adSJeff Kirsher 	skb->dev = dev;
886224cf5adSJeff Kirsher 
887224cf5adSJeff Kirsher 	skb->priority = sk->sk_priority;
888224cf5adSJeff Kirsher 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
889224cf5adSJeff Kirsher 
890224cf5adSJeff Kirsher 	ph = (struct pppoe_hdr *)skb_put(skb, total_len + sizeof(struct pppoe_hdr));
891224cf5adSJeff Kirsher 	start = (char *)&ph->tag[0];
892224cf5adSJeff Kirsher 
8936ce8e9ceSAl Viro 	error = memcpy_from_msg(start, m, total_len);
894224cf5adSJeff Kirsher 	if (error < 0) {
895224cf5adSJeff Kirsher 		kfree_skb(skb);
896224cf5adSJeff Kirsher 		goto end;
897224cf5adSJeff Kirsher 	}
898224cf5adSJeff Kirsher 
899224cf5adSJeff Kirsher 	error = total_len;
900224cf5adSJeff Kirsher 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
901224cf5adSJeff Kirsher 			po->pppoe_pa.remote, NULL, total_len);
902224cf5adSJeff Kirsher 
903224cf5adSJeff Kirsher 	memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
904224cf5adSJeff Kirsher 
905224cf5adSJeff Kirsher 	ph->length = htons(total_len);
906224cf5adSJeff Kirsher 
907224cf5adSJeff Kirsher 	dev_queue_xmit(skb);
908224cf5adSJeff Kirsher 
909224cf5adSJeff Kirsher end:
910224cf5adSJeff Kirsher 	release_sock(sk);
911224cf5adSJeff Kirsher 	return error;
912224cf5adSJeff Kirsher }
913224cf5adSJeff Kirsher 
914224cf5adSJeff Kirsher /************************************************************************
915224cf5adSJeff Kirsher  *
916224cf5adSJeff Kirsher  * xmit function for internal use.
917224cf5adSJeff Kirsher  *
918224cf5adSJeff Kirsher  ***********************************************************************/
919224cf5adSJeff Kirsher static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
920224cf5adSJeff Kirsher {
921224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
922224cf5adSJeff Kirsher 	struct net_device *dev = po->pppoe_dev;
923224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
924224cf5adSJeff Kirsher 	int data_len = skb->len;
925224cf5adSJeff Kirsher 
926224cf5adSJeff Kirsher 	/* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
927224cf5adSJeff Kirsher 	 * xmit operations conclude prior to an unregistration call.  Thus
928224cf5adSJeff Kirsher 	 * sk->sk_state cannot change, so we don't need to do lock_sock().
929224cf5adSJeff Kirsher 	 * But, we also can't do a lock_sock since that introduces a potential
930224cf5adSJeff Kirsher 	 * deadlock as we'd reverse the lock ordering used when calling
931224cf5adSJeff Kirsher 	 * ppp_unregister_channel().
932224cf5adSJeff Kirsher 	 */
933224cf5adSJeff Kirsher 
934224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
935224cf5adSJeff Kirsher 		goto abort;
936224cf5adSJeff Kirsher 
937224cf5adSJeff Kirsher 	if (!dev)
938224cf5adSJeff Kirsher 		goto abort;
939224cf5adSJeff Kirsher 
940224cf5adSJeff Kirsher 	/* Copy the data if there is no space for the header or if it's
941224cf5adSJeff Kirsher 	 * read-only.
942224cf5adSJeff Kirsher 	 */
943224cf5adSJeff Kirsher 	if (skb_cow_head(skb, sizeof(*ph) + dev->hard_header_len))
944224cf5adSJeff Kirsher 		goto abort;
945224cf5adSJeff Kirsher 
946224cf5adSJeff Kirsher 	__skb_push(skb, sizeof(*ph));
947224cf5adSJeff Kirsher 	skb_reset_network_header(skb);
948224cf5adSJeff Kirsher 
949224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
950224cf5adSJeff Kirsher 	ph->ver	= 1;
951224cf5adSJeff Kirsher 	ph->type = 1;
952224cf5adSJeff Kirsher 	ph->code = 0;
953224cf5adSJeff Kirsher 	ph->sid	= po->num;
954224cf5adSJeff Kirsher 	ph->length = htons(data_len);
955224cf5adSJeff Kirsher 
956224cf5adSJeff Kirsher 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
957224cf5adSJeff Kirsher 	skb->dev = dev;
958224cf5adSJeff Kirsher 
959224cf5adSJeff Kirsher 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
960224cf5adSJeff Kirsher 			po->pppoe_pa.remote, NULL, data_len);
961224cf5adSJeff Kirsher 
962224cf5adSJeff Kirsher 	dev_queue_xmit(skb);
963224cf5adSJeff Kirsher 	return 1;
964224cf5adSJeff Kirsher 
965224cf5adSJeff Kirsher abort:
966224cf5adSJeff Kirsher 	kfree_skb(skb);
967224cf5adSJeff Kirsher 	return 1;
968224cf5adSJeff Kirsher }
969224cf5adSJeff Kirsher 
970224cf5adSJeff Kirsher /************************************************************************
971224cf5adSJeff Kirsher  *
972224cf5adSJeff Kirsher  * xmit function called by generic PPP driver
973224cf5adSJeff Kirsher  * sends PPP frame over PPPoE socket
974224cf5adSJeff Kirsher  *
975224cf5adSJeff Kirsher  ***********************************************************************/
976224cf5adSJeff Kirsher static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
977224cf5adSJeff Kirsher {
978224cf5adSJeff Kirsher 	struct sock *sk = (struct sock *)chan->private;
979224cf5adSJeff Kirsher 	return __pppoe_xmit(sk, skb);
980224cf5adSJeff Kirsher }
981224cf5adSJeff Kirsher 
982224cf5adSJeff Kirsher static const struct ppp_channel_ops pppoe_chan_ops = {
983224cf5adSJeff Kirsher 	.start_xmit = pppoe_xmit,
984224cf5adSJeff Kirsher };
985224cf5adSJeff Kirsher 
9861b784140SYing Xue static int pppoe_recvmsg(struct socket *sock, struct msghdr *m,
9871b784140SYing Xue 			 size_t total_len, int flags)
988224cf5adSJeff Kirsher {
989224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
990224cf5adSJeff Kirsher 	struct sk_buff *skb;
991224cf5adSJeff Kirsher 	int error = 0;
992224cf5adSJeff Kirsher 
993224cf5adSJeff Kirsher 	if (sk->sk_state & PPPOX_BOUND) {
994224cf5adSJeff Kirsher 		error = -EIO;
995224cf5adSJeff Kirsher 		goto end;
996224cf5adSJeff Kirsher 	}
997224cf5adSJeff Kirsher 
998224cf5adSJeff Kirsher 	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
999224cf5adSJeff Kirsher 				flags & MSG_DONTWAIT, &error);
1000224cf5adSJeff Kirsher 	if (error < 0)
1001224cf5adSJeff Kirsher 		goto end;
1002224cf5adSJeff Kirsher 
1003224cf5adSJeff Kirsher 	if (skb) {
1004224cf5adSJeff Kirsher 		total_len = min_t(size_t, total_len, skb->len);
100551f3d02bSDavid S. Miller 		error = skb_copy_datagram_msg(skb, 0, m, total_len);
1006968d7018SEric Dumazet 		if (error == 0) {
1007968d7018SEric Dumazet 			consume_skb(skb);
1008968d7018SEric Dumazet 			return total_len;
1009968d7018SEric Dumazet 		}
1010224cf5adSJeff Kirsher 	}
1011224cf5adSJeff Kirsher 
1012224cf5adSJeff Kirsher 	kfree_skb(skb);
1013224cf5adSJeff Kirsher end:
1014224cf5adSJeff Kirsher 	return error;
1015224cf5adSJeff Kirsher }
1016224cf5adSJeff Kirsher 
1017224cf5adSJeff Kirsher #ifdef CONFIG_PROC_FS
1018224cf5adSJeff Kirsher static int pppoe_seq_show(struct seq_file *seq, void *v)
1019224cf5adSJeff Kirsher {
1020224cf5adSJeff Kirsher 	struct pppox_sock *po;
1021224cf5adSJeff Kirsher 	char *dev_name;
1022224cf5adSJeff Kirsher 
1023224cf5adSJeff Kirsher 	if (v == SEQ_START_TOKEN) {
1024224cf5adSJeff Kirsher 		seq_puts(seq, "Id       Address              Device\n");
1025224cf5adSJeff Kirsher 		goto out;
1026224cf5adSJeff Kirsher 	}
1027224cf5adSJeff Kirsher 
1028224cf5adSJeff Kirsher 	po = v;
1029224cf5adSJeff Kirsher 	dev_name = po->pppoe_pa.dev;
1030224cf5adSJeff Kirsher 
1031224cf5adSJeff Kirsher 	seq_printf(seq, "%08X %pM %8s\n",
1032224cf5adSJeff Kirsher 		po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
1033224cf5adSJeff Kirsher out:
1034224cf5adSJeff Kirsher 	return 0;
1035224cf5adSJeff Kirsher }
1036224cf5adSJeff Kirsher 
1037224cf5adSJeff Kirsher static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
1038224cf5adSJeff Kirsher {
1039224cf5adSJeff Kirsher 	struct pppox_sock *po;
1040224cf5adSJeff Kirsher 	int i;
1041224cf5adSJeff Kirsher 
1042224cf5adSJeff Kirsher 	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
1043224cf5adSJeff Kirsher 		po = pn->hash_table[i];
1044224cf5adSJeff Kirsher 		while (po) {
1045224cf5adSJeff Kirsher 			if (!pos--)
1046224cf5adSJeff Kirsher 				goto out;
1047224cf5adSJeff Kirsher 			po = po->next;
1048224cf5adSJeff Kirsher 		}
1049224cf5adSJeff Kirsher 	}
1050224cf5adSJeff Kirsher 
1051224cf5adSJeff Kirsher out:
1052224cf5adSJeff Kirsher 	return po;
1053224cf5adSJeff Kirsher }
1054224cf5adSJeff Kirsher 
1055224cf5adSJeff Kirsher static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
1056224cf5adSJeff Kirsher 	__acquires(pn->hash_lock)
1057224cf5adSJeff Kirsher {
1058224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1059224cf5adSJeff Kirsher 	loff_t l = *pos;
1060224cf5adSJeff Kirsher 
1061224cf5adSJeff Kirsher 	read_lock_bh(&pn->hash_lock);
1062224cf5adSJeff Kirsher 	return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
1063224cf5adSJeff Kirsher }
1064224cf5adSJeff Kirsher 
1065224cf5adSJeff Kirsher static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1066224cf5adSJeff Kirsher {
1067224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1068224cf5adSJeff Kirsher 	struct pppox_sock *po;
1069224cf5adSJeff Kirsher 
1070224cf5adSJeff Kirsher 	++*pos;
1071224cf5adSJeff Kirsher 	if (v == SEQ_START_TOKEN) {
1072224cf5adSJeff Kirsher 		po = pppoe_get_idx(pn, 0);
1073224cf5adSJeff Kirsher 		goto out;
1074224cf5adSJeff Kirsher 	}
1075224cf5adSJeff Kirsher 	po = v;
1076224cf5adSJeff Kirsher 	if (po->next)
1077224cf5adSJeff Kirsher 		po = po->next;
1078224cf5adSJeff Kirsher 	else {
1079224cf5adSJeff Kirsher 		int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1080224cf5adSJeff Kirsher 
1081224cf5adSJeff Kirsher 		po = NULL;
1082224cf5adSJeff Kirsher 		while (++hash < PPPOE_HASH_SIZE) {
1083224cf5adSJeff Kirsher 			po = pn->hash_table[hash];
1084224cf5adSJeff Kirsher 			if (po)
1085224cf5adSJeff Kirsher 				break;
1086224cf5adSJeff Kirsher 		}
1087224cf5adSJeff Kirsher 	}
1088224cf5adSJeff Kirsher 
1089224cf5adSJeff Kirsher out:
1090224cf5adSJeff Kirsher 	return po;
1091224cf5adSJeff Kirsher }
1092224cf5adSJeff Kirsher 
1093224cf5adSJeff Kirsher static void pppoe_seq_stop(struct seq_file *seq, void *v)
1094224cf5adSJeff Kirsher 	__releases(pn->hash_lock)
1095224cf5adSJeff Kirsher {
1096224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1097224cf5adSJeff Kirsher 	read_unlock_bh(&pn->hash_lock);
1098224cf5adSJeff Kirsher }
1099224cf5adSJeff Kirsher 
1100224cf5adSJeff Kirsher static const struct seq_operations pppoe_seq_ops = {
1101224cf5adSJeff Kirsher 	.start		= pppoe_seq_start,
1102224cf5adSJeff Kirsher 	.next		= pppoe_seq_next,
1103224cf5adSJeff Kirsher 	.stop		= pppoe_seq_stop,
1104224cf5adSJeff Kirsher 	.show		= pppoe_seq_show,
1105224cf5adSJeff Kirsher };
1106224cf5adSJeff Kirsher 
1107224cf5adSJeff Kirsher static int pppoe_seq_open(struct inode *inode, struct file *file)
1108224cf5adSJeff Kirsher {
1109224cf5adSJeff Kirsher 	return seq_open_net(inode, file, &pppoe_seq_ops,
1110224cf5adSJeff Kirsher 			sizeof(struct seq_net_private));
1111224cf5adSJeff Kirsher }
1112224cf5adSJeff Kirsher 
1113224cf5adSJeff Kirsher static const struct file_operations pppoe_seq_fops = {
1114224cf5adSJeff Kirsher 	.owner		= THIS_MODULE,
1115224cf5adSJeff Kirsher 	.open		= pppoe_seq_open,
1116224cf5adSJeff Kirsher 	.read		= seq_read,
1117224cf5adSJeff Kirsher 	.llseek		= seq_lseek,
1118224cf5adSJeff Kirsher 	.release	= seq_release_net,
1119224cf5adSJeff Kirsher };
1120224cf5adSJeff Kirsher 
1121224cf5adSJeff Kirsher #endif /* CONFIG_PROC_FS */
1122224cf5adSJeff Kirsher 
1123224cf5adSJeff Kirsher static const struct proto_ops pppoe_ops = {
1124224cf5adSJeff Kirsher 	.family		= AF_PPPOX,
1125224cf5adSJeff Kirsher 	.owner		= THIS_MODULE,
1126224cf5adSJeff Kirsher 	.release	= pppoe_release,
1127224cf5adSJeff Kirsher 	.bind		= sock_no_bind,
1128224cf5adSJeff Kirsher 	.connect	= pppoe_connect,
1129224cf5adSJeff Kirsher 	.socketpair	= sock_no_socketpair,
1130224cf5adSJeff Kirsher 	.accept		= sock_no_accept,
1131224cf5adSJeff Kirsher 	.getname	= pppoe_getname,
1132224cf5adSJeff Kirsher 	.poll		= datagram_poll,
1133224cf5adSJeff Kirsher 	.listen		= sock_no_listen,
1134224cf5adSJeff Kirsher 	.shutdown	= sock_no_shutdown,
1135224cf5adSJeff Kirsher 	.setsockopt	= sock_no_setsockopt,
1136224cf5adSJeff Kirsher 	.getsockopt	= sock_no_getsockopt,
1137224cf5adSJeff Kirsher 	.sendmsg	= pppoe_sendmsg,
1138224cf5adSJeff Kirsher 	.recvmsg	= pppoe_recvmsg,
1139224cf5adSJeff Kirsher 	.mmap		= sock_no_mmap,
1140224cf5adSJeff Kirsher 	.ioctl		= pppox_ioctl,
1141224cf5adSJeff Kirsher };
1142224cf5adSJeff Kirsher 
1143224cf5adSJeff Kirsher static const struct pppox_proto pppoe_proto = {
1144224cf5adSJeff Kirsher 	.create	= pppoe_create,
1145224cf5adSJeff Kirsher 	.ioctl	= pppoe_ioctl,
1146224cf5adSJeff Kirsher 	.owner	= THIS_MODULE,
1147224cf5adSJeff Kirsher };
1148224cf5adSJeff Kirsher 
1149224cf5adSJeff Kirsher static __net_init int pppoe_init_net(struct net *net)
1150224cf5adSJeff Kirsher {
1151224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(net);
1152224cf5adSJeff Kirsher 	struct proc_dir_entry *pde;
1153224cf5adSJeff Kirsher 
1154224cf5adSJeff Kirsher 	rwlock_init(&pn->hash_lock);
1155224cf5adSJeff Kirsher 
1156d4beaa66SGao feng 	pde = proc_create("pppoe", S_IRUGO, net->proc_net, &pppoe_seq_fops);
1157224cf5adSJeff Kirsher #ifdef CONFIG_PROC_FS
1158224cf5adSJeff Kirsher 	if (!pde)
1159224cf5adSJeff Kirsher 		return -ENOMEM;
1160224cf5adSJeff Kirsher #endif
1161224cf5adSJeff Kirsher 
1162224cf5adSJeff Kirsher 	return 0;
1163224cf5adSJeff Kirsher }
1164224cf5adSJeff Kirsher 
1165224cf5adSJeff Kirsher static __net_exit void pppoe_exit_net(struct net *net)
1166224cf5adSJeff Kirsher {
1167ece31ffdSGao feng 	remove_proc_entry("pppoe", net->proc_net);
1168224cf5adSJeff Kirsher }
1169224cf5adSJeff Kirsher 
1170224cf5adSJeff Kirsher static struct pernet_operations pppoe_net_ops = {
1171224cf5adSJeff Kirsher 	.init = pppoe_init_net,
1172224cf5adSJeff Kirsher 	.exit = pppoe_exit_net,
1173224cf5adSJeff Kirsher 	.id   = &pppoe_net_id,
1174224cf5adSJeff Kirsher 	.size = sizeof(struct pppoe_net),
1175224cf5adSJeff Kirsher };
1176224cf5adSJeff Kirsher 
1177224cf5adSJeff Kirsher static int __init pppoe_init(void)
1178224cf5adSJeff Kirsher {
1179224cf5adSJeff Kirsher 	int err;
1180224cf5adSJeff Kirsher 
1181224cf5adSJeff Kirsher 	err = register_pernet_device(&pppoe_net_ops);
1182224cf5adSJeff Kirsher 	if (err)
1183224cf5adSJeff Kirsher 		goto out;
1184224cf5adSJeff Kirsher 
1185224cf5adSJeff Kirsher 	err = proto_register(&pppoe_sk_proto, 0);
1186224cf5adSJeff Kirsher 	if (err)
1187224cf5adSJeff Kirsher 		goto out_unregister_net_ops;
1188224cf5adSJeff Kirsher 
1189224cf5adSJeff Kirsher 	err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1190224cf5adSJeff Kirsher 	if (err)
1191224cf5adSJeff Kirsher 		goto out_unregister_pppoe_proto;
1192224cf5adSJeff Kirsher 
1193224cf5adSJeff Kirsher 	dev_add_pack(&pppoes_ptype);
1194224cf5adSJeff Kirsher 	dev_add_pack(&pppoed_ptype);
1195224cf5adSJeff Kirsher 	register_netdevice_notifier(&pppoe_notifier);
1196224cf5adSJeff Kirsher 
1197224cf5adSJeff Kirsher 	return 0;
1198224cf5adSJeff Kirsher 
1199224cf5adSJeff Kirsher out_unregister_pppoe_proto:
1200224cf5adSJeff Kirsher 	proto_unregister(&pppoe_sk_proto);
1201224cf5adSJeff Kirsher out_unregister_net_ops:
1202224cf5adSJeff Kirsher 	unregister_pernet_device(&pppoe_net_ops);
1203224cf5adSJeff Kirsher out:
1204224cf5adSJeff Kirsher 	return err;
1205224cf5adSJeff Kirsher }
1206224cf5adSJeff Kirsher 
1207224cf5adSJeff Kirsher static void __exit pppoe_exit(void)
1208224cf5adSJeff Kirsher {
1209224cf5adSJeff Kirsher 	unregister_netdevice_notifier(&pppoe_notifier);
1210224cf5adSJeff Kirsher 	dev_remove_pack(&pppoed_ptype);
1211224cf5adSJeff Kirsher 	dev_remove_pack(&pppoes_ptype);
1212224cf5adSJeff Kirsher 	unregister_pppox_proto(PX_PROTO_OE);
1213224cf5adSJeff Kirsher 	proto_unregister(&pppoe_sk_proto);
1214224cf5adSJeff Kirsher 	unregister_pernet_device(&pppoe_net_ops);
1215224cf5adSJeff Kirsher }
1216224cf5adSJeff Kirsher 
1217224cf5adSJeff Kirsher module_init(pppoe_init);
1218224cf5adSJeff Kirsher module_exit(pppoe_exit);
1219224cf5adSJeff Kirsher 
1220224cf5adSJeff Kirsher MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1221224cf5adSJeff Kirsher MODULE_DESCRIPTION("PPP over Ethernet driver");
1222224cf5adSJeff Kirsher MODULE_LICENSE("GPL");
1223224cf5adSJeff Kirsher MODULE_ALIAS_NETPROTO(PF_PPPOX);
1224