xref: /openbmc/linux/drivers/net/ppp/pppoe.c (revision 665a6cd8)
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 = PPPOX_ZOMBIE;
317224cf5adSJeff Kirsher 				sk->sk_state_change(sk);
318224cf5adSJeff Kirsher 				po->pppoe_dev = NULL;
319224cf5adSJeff Kirsher 				dev_put(dev);
320224cf5adSJeff Kirsher 			}
321224cf5adSJeff Kirsher 
322224cf5adSJeff Kirsher 			release_sock(sk);
323224cf5adSJeff Kirsher 			sock_put(sk);
324224cf5adSJeff Kirsher 
325224cf5adSJeff Kirsher 			/* Restart the process from the start of the current
326224cf5adSJeff Kirsher 			 * hash chain. We dropped locks so the world may have
327224cf5adSJeff Kirsher 			 * change from underneath us.
328224cf5adSJeff Kirsher 			 */
329224cf5adSJeff Kirsher 
330224cf5adSJeff Kirsher 			BUG_ON(pppoe_pernet(dev_net(dev)) == NULL);
331224cf5adSJeff Kirsher 			write_lock_bh(&pn->hash_lock);
332224cf5adSJeff Kirsher 			po = pn->hash_table[i];
333224cf5adSJeff Kirsher 		}
334224cf5adSJeff Kirsher 	}
335224cf5adSJeff Kirsher 	write_unlock_bh(&pn->hash_lock);
336224cf5adSJeff Kirsher }
337224cf5adSJeff Kirsher 
338224cf5adSJeff Kirsher static int pppoe_device_event(struct notifier_block *this,
339224cf5adSJeff Kirsher 			      unsigned long event, void *ptr)
340224cf5adSJeff Kirsher {
341351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
342224cf5adSJeff Kirsher 
343224cf5adSJeff Kirsher 	/* Only look at sockets that are using this specific device. */
344224cf5adSJeff Kirsher 	switch (event) {
345224cf5adSJeff Kirsher 	case NETDEV_CHANGEADDR:
346224cf5adSJeff Kirsher 	case NETDEV_CHANGEMTU:
347224cf5adSJeff Kirsher 		/* A change in mtu or address is a bad thing, requiring
348224cf5adSJeff Kirsher 		 * LCP re-negotiation.
349224cf5adSJeff Kirsher 		 */
350224cf5adSJeff Kirsher 
351224cf5adSJeff Kirsher 	case NETDEV_GOING_DOWN:
352224cf5adSJeff Kirsher 	case NETDEV_DOWN:
353224cf5adSJeff Kirsher 		/* Find every socket on this device and kill it. */
354224cf5adSJeff Kirsher 		pppoe_flush_dev(dev);
355224cf5adSJeff Kirsher 		break;
356224cf5adSJeff Kirsher 
357224cf5adSJeff Kirsher 	default:
358224cf5adSJeff Kirsher 		break;
359224cf5adSJeff Kirsher 	}
360224cf5adSJeff Kirsher 
361224cf5adSJeff Kirsher 	return NOTIFY_DONE;
362224cf5adSJeff Kirsher }
363224cf5adSJeff Kirsher 
364224cf5adSJeff Kirsher static struct notifier_block pppoe_notifier = {
365224cf5adSJeff Kirsher 	.notifier_call = pppoe_device_event,
366224cf5adSJeff Kirsher };
367224cf5adSJeff Kirsher 
368224cf5adSJeff Kirsher /************************************************************************
369224cf5adSJeff Kirsher  *
370224cf5adSJeff Kirsher  * Do the real work of receiving a PPPoE Session frame.
371224cf5adSJeff Kirsher  *
372224cf5adSJeff Kirsher  ***********************************************************************/
373224cf5adSJeff Kirsher static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
374224cf5adSJeff Kirsher {
375224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
376224cf5adSJeff Kirsher 	struct pppox_sock *relay_po;
377224cf5adSJeff Kirsher 
378224cf5adSJeff Kirsher 	/* Backlog receive. Semantics of backlog rcv preclude any code from
379224cf5adSJeff Kirsher 	 * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state
380224cf5adSJeff Kirsher 	 * can't change.
381224cf5adSJeff Kirsher 	 */
382224cf5adSJeff Kirsher 
383a068833bSJoakim Tjernlund 	if (skb->pkt_type == PACKET_OTHERHOST)
384a068833bSJoakim Tjernlund 		goto abort_kfree;
385a068833bSJoakim Tjernlund 
386224cf5adSJeff Kirsher 	if (sk->sk_state & PPPOX_BOUND) {
387224cf5adSJeff Kirsher 		ppp_input(&po->chan, skb);
388224cf5adSJeff Kirsher 	} else if (sk->sk_state & PPPOX_RELAY) {
389224cf5adSJeff Kirsher 		relay_po = get_item_by_addr(sock_net(sk),
390224cf5adSJeff Kirsher 					    &po->pppoe_relay);
391224cf5adSJeff Kirsher 		if (relay_po == NULL)
392224cf5adSJeff Kirsher 			goto abort_kfree;
393224cf5adSJeff Kirsher 
394224cf5adSJeff Kirsher 		if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
395224cf5adSJeff Kirsher 			goto abort_put;
396224cf5adSJeff Kirsher 
397224cf5adSJeff Kirsher 		if (!__pppoe_xmit(sk_pppox(relay_po), skb))
398224cf5adSJeff Kirsher 			goto abort_put;
399224cf5adSJeff Kirsher 	} else {
400224cf5adSJeff Kirsher 		if (sock_queue_rcv_skb(sk, skb))
401224cf5adSJeff Kirsher 			goto abort_kfree;
402224cf5adSJeff Kirsher 	}
403224cf5adSJeff Kirsher 
404224cf5adSJeff Kirsher 	return NET_RX_SUCCESS;
405224cf5adSJeff Kirsher 
406224cf5adSJeff Kirsher abort_put:
407224cf5adSJeff Kirsher 	sock_put(sk_pppox(relay_po));
408224cf5adSJeff Kirsher 
409224cf5adSJeff Kirsher abort_kfree:
410224cf5adSJeff Kirsher 	kfree_skb(skb);
411224cf5adSJeff Kirsher 	return NET_RX_DROP;
412224cf5adSJeff Kirsher }
413224cf5adSJeff Kirsher 
414224cf5adSJeff Kirsher /************************************************************************
415224cf5adSJeff Kirsher  *
416224cf5adSJeff Kirsher  * Receive wrapper called in BH context.
417224cf5adSJeff Kirsher  *
418224cf5adSJeff Kirsher  ***********************************************************************/
419224cf5adSJeff Kirsher static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev,
420224cf5adSJeff Kirsher 		     struct packet_type *pt, struct net_device *orig_dev)
421224cf5adSJeff Kirsher {
422224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
423224cf5adSJeff Kirsher 	struct pppox_sock *po;
424224cf5adSJeff Kirsher 	struct pppoe_net *pn;
425224cf5adSJeff Kirsher 	int len;
426224cf5adSJeff Kirsher 
427224cf5adSJeff Kirsher 	skb = skb_share_check(skb, GFP_ATOMIC);
428224cf5adSJeff Kirsher 	if (!skb)
429224cf5adSJeff Kirsher 		goto out;
430224cf5adSJeff Kirsher 
431224cf5adSJeff Kirsher 	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
432224cf5adSJeff Kirsher 		goto drop;
433224cf5adSJeff Kirsher 
434224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
435224cf5adSJeff Kirsher 	len = ntohs(ph->length);
436224cf5adSJeff Kirsher 
437224cf5adSJeff Kirsher 	skb_pull_rcsum(skb, sizeof(*ph));
438224cf5adSJeff Kirsher 	if (skb->len < len)
439224cf5adSJeff Kirsher 		goto drop;
440224cf5adSJeff Kirsher 
441224cf5adSJeff Kirsher 	if (pskb_trim_rcsum(skb, len))
442224cf5adSJeff Kirsher 		goto drop;
443224cf5adSJeff Kirsher 
444224cf5adSJeff Kirsher 	pn = pppoe_pernet(dev_net(dev));
445224cf5adSJeff Kirsher 
446224cf5adSJeff Kirsher 	/* Note that get_item does a sock_hold(), so sk_pppox(po)
447224cf5adSJeff Kirsher 	 * is known to be safe.
448224cf5adSJeff Kirsher 	 */
449224cf5adSJeff Kirsher 	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
450224cf5adSJeff Kirsher 	if (!po)
451224cf5adSJeff Kirsher 		goto drop;
452224cf5adSJeff Kirsher 
453224cf5adSJeff Kirsher 	return sk_receive_skb(sk_pppox(po), skb, 0);
454224cf5adSJeff Kirsher 
455224cf5adSJeff Kirsher drop:
456224cf5adSJeff Kirsher 	kfree_skb(skb);
457224cf5adSJeff Kirsher out:
458224cf5adSJeff Kirsher 	return NET_RX_DROP;
459224cf5adSJeff Kirsher }
460224cf5adSJeff Kirsher 
461287f3a94SSimon Farnsworth static void pppoe_unbind_sock_work(struct work_struct *work)
462287f3a94SSimon Farnsworth {
463287f3a94SSimon Farnsworth 	struct pppox_sock *po = container_of(work, struct pppox_sock,
464287f3a94SSimon Farnsworth 					     proto.pppoe.padt_work);
465287f3a94SSimon Farnsworth 	struct sock *sk = sk_pppox(po);
466287f3a94SSimon Farnsworth 
467287f3a94SSimon Farnsworth 	lock_sock(sk);
468665a6cd8SFelix Fietkau 	if (po->pppoe_dev) {
469665a6cd8SFelix Fietkau 		dev_put(po->pppoe_dev);
470665a6cd8SFelix Fietkau 		po->pppoe_dev = NULL;
471665a6cd8SFelix Fietkau 	}
472287f3a94SSimon Farnsworth 	pppox_unbind_sock(sk);
473287f3a94SSimon Farnsworth 	release_sock(sk);
474287f3a94SSimon Farnsworth 	sock_put(sk);
475287f3a94SSimon Farnsworth }
476287f3a94SSimon Farnsworth 
477224cf5adSJeff Kirsher /************************************************************************
478224cf5adSJeff Kirsher  *
479224cf5adSJeff Kirsher  * Receive a PPPoE Discovery frame.
480224cf5adSJeff Kirsher  * This is solely for detection of PADT frames
481224cf5adSJeff Kirsher  *
482224cf5adSJeff Kirsher  ***********************************************************************/
483224cf5adSJeff Kirsher static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev,
484224cf5adSJeff Kirsher 			  struct packet_type *pt, struct net_device *orig_dev)
485224cf5adSJeff Kirsher 
486224cf5adSJeff Kirsher {
487224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
488224cf5adSJeff Kirsher 	struct pppox_sock *po;
489224cf5adSJeff Kirsher 	struct pppoe_net *pn;
490224cf5adSJeff Kirsher 
491224cf5adSJeff Kirsher 	skb = skb_share_check(skb, GFP_ATOMIC);
492224cf5adSJeff Kirsher 	if (!skb)
493224cf5adSJeff Kirsher 		goto out;
494224cf5adSJeff Kirsher 
495224cf5adSJeff Kirsher 	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
496224cf5adSJeff Kirsher 		goto abort;
497224cf5adSJeff Kirsher 
498224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
499224cf5adSJeff Kirsher 	if (ph->code != PADT_CODE)
500224cf5adSJeff Kirsher 		goto abort;
501224cf5adSJeff Kirsher 
502224cf5adSJeff Kirsher 	pn = pppoe_pernet(dev_net(dev));
503224cf5adSJeff Kirsher 	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
504224cf5adSJeff Kirsher 	if (po) {
505224cf5adSJeff Kirsher 		struct sock *sk = sk_pppox(po);
506224cf5adSJeff Kirsher 
507224cf5adSJeff Kirsher 		bh_lock_sock(sk);
508224cf5adSJeff Kirsher 
509224cf5adSJeff Kirsher 		/* If the user has locked the socket, just ignore
510224cf5adSJeff Kirsher 		 * the packet.  With the way two rcv protocols hook into
511224cf5adSJeff Kirsher 		 * one socket family type, we cannot (easily) distinguish
512224cf5adSJeff Kirsher 		 * what kind of SKB it is during backlog rcv.
513224cf5adSJeff Kirsher 		 */
514224cf5adSJeff Kirsher 		if (sock_owned_by_user(sk) == 0) {
515224cf5adSJeff Kirsher 			/* We're no longer connect at the PPPOE layer,
516224cf5adSJeff Kirsher 			 * and must wait for ppp channel to disconnect us.
517224cf5adSJeff Kirsher 			 */
518224cf5adSJeff Kirsher 			sk->sk_state = PPPOX_ZOMBIE;
519224cf5adSJeff Kirsher 		}
520224cf5adSJeff Kirsher 
521224cf5adSJeff Kirsher 		bh_unlock_sock(sk);
522287f3a94SSimon Farnsworth 		if (!schedule_work(&po->proto.pppoe.padt_work))
523224cf5adSJeff Kirsher 			sock_put(sk);
524224cf5adSJeff Kirsher 	}
525224cf5adSJeff Kirsher 
526224cf5adSJeff Kirsher abort:
527224cf5adSJeff Kirsher 	kfree_skb(skb);
528224cf5adSJeff Kirsher out:
529224cf5adSJeff Kirsher 	return NET_RX_SUCCESS; /* Lies... :-) */
530224cf5adSJeff Kirsher }
531224cf5adSJeff Kirsher 
532224cf5adSJeff Kirsher static struct packet_type pppoes_ptype __read_mostly = {
533224cf5adSJeff Kirsher 	.type	= cpu_to_be16(ETH_P_PPP_SES),
534224cf5adSJeff Kirsher 	.func	= pppoe_rcv,
535224cf5adSJeff Kirsher };
536224cf5adSJeff Kirsher 
537224cf5adSJeff Kirsher static struct packet_type pppoed_ptype __read_mostly = {
538224cf5adSJeff Kirsher 	.type	= cpu_to_be16(ETH_P_PPP_DISC),
539224cf5adSJeff Kirsher 	.func	= pppoe_disc_rcv,
540224cf5adSJeff Kirsher };
541224cf5adSJeff Kirsher 
542224cf5adSJeff Kirsher static struct proto pppoe_sk_proto __read_mostly = {
543224cf5adSJeff Kirsher 	.name	  = "PPPOE",
544224cf5adSJeff Kirsher 	.owner	  = THIS_MODULE,
545224cf5adSJeff Kirsher 	.obj_size = sizeof(struct pppox_sock),
546224cf5adSJeff Kirsher };
547224cf5adSJeff Kirsher 
548224cf5adSJeff Kirsher /***********************************************************************
549224cf5adSJeff Kirsher  *
550224cf5adSJeff Kirsher  * Initialize a new struct sock.
551224cf5adSJeff Kirsher  *
552224cf5adSJeff Kirsher  **********************************************************************/
553224cf5adSJeff Kirsher static int pppoe_create(struct net *net, struct socket *sock)
554224cf5adSJeff Kirsher {
555224cf5adSJeff Kirsher 	struct sock *sk;
556224cf5adSJeff Kirsher 
557224cf5adSJeff Kirsher 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto);
558224cf5adSJeff Kirsher 	if (!sk)
559224cf5adSJeff Kirsher 		return -ENOMEM;
560224cf5adSJeff Kirsher 
561224cf5adSJeff Kirsher 	sock_init_data(sock, sk);
562224cf5adSJeff Kirsher 
563224cf5adSJeff Kirsher 	sock->state	= SS_UNCONNECTED;
564224cf5adSJeff Kirsher 	sock->ops	= &pppoe_ops;
565224cf5adSJeff Kirsher 
566224cf5adSJeff Kirsher 	sk->sk_backlog_rcv	= pppoe_rcv_core;
567224cf5adSJeff Kirsher 	sk->sk_state		= PPPOX_NONE;
568224cf5adSJeff Kirsher 	sk->sk_type		= SOCK_STREAM;
569224cf5adSJeff Kirsher 	sk->sk_family		= PF_PPPOX;
570224cf5adSJeff Kirsher 	sk->sk_protocol		= PX_PROTO_OE;
571224cf5adSJeff Kirsher 
572224cf5adSJeff Kirsher 	return 0;
573224cf5adSJeff Kirsher }
574224cf5adSJeff Kirsher 
575224cf5adSJeff Kirsher static int pppoe_release(struct socket *sock)
576224cf5adSJeff Kirsher {
577224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
578224cf5adSJeff Kirsher 	struct pppox_sock *po;
579224cf5adSJeff Kirsher 	struct pppoe_net *pn;
580224cf5adSJeff Kirsher 	struct net *net = NULL;
581224cf5adSJeff Kirsher 
582224cf5adSJeff Kirsher 	if (!sk)
583224cf5adSJeff Kirsher 		return 0;
584224cf5adSJeff Kirsher 
585224cf5adSJeff Kirsher 	lock_sock(sk);
586224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD)) {
587224cf5adSJeff Kirsher 		release_sock(sk);
588224cf5adSJeff Kirsher 		return -EBADF;
589224cf5adSJeff Kirsher 	}
590224cf5adSJeff Kirsher 
591224cf5adSJeff Kirsher 	po = pppox_sk(sk);
592224cf5adSJeff Kirsher 
5932b018d57SXiaodong Xu 	if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND | PPPOX_ZOMBIE)) {
594224cf5adSJeff Kirsher 		dev_put(po->pppoe_dev);
595224cf5adSJeff Kirsher 		po->pppoe_dev = NULL;
596224cf5adSJeff Kirsher 	}
597224cf5adSJeff Kirsher 
598224cf5adSJeff Kirsher 	pppox_unbind_sock(sk);
599224cf5adSJeff Kirsher 
600224cf5adSJeff Kirsher 	/* Signal the death of the socket. */
601224cf5adSJeff Kirsher 	sk->sk_state = PPPOX_DEAD;
602224cf5adSJeff Kirsher 
603224cf5adSJeff Kirsher 	net = sock_net(sk);
604224cf5adSJeff Kirsher 	pn = pppoe_pernet(net);
605224cf5adSJeff Kirsher 
606224cf5adSJeff Kirsher 	/*
607224cf5adSJeff Kirsher 	 * protect "po" from concurrent updates
608224cf5adSJeff Kirsher 	 * on pppoe_flush_dev
609224cf5adSJeff Kirsher 	 */
610224cf5adSJeff Kirsher 	delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
611224cf5adSJeff Kirsher 		    po->pppoe_ifindex);
612224cf5adSJeff Kirsher 
613224cf5adSJeff Kirsher 	sock_orphan(sk);
614224cf5adSJeff Kirsher 	sock->sk = NULL;
615224cf5adSJeff Kirsher 
616224cf5adSJeff Kirsher 	skb_queue_purge(&sk->sk_receive_queue);
617224cf5adSJeff Kirsher 	release_sock(sk);
618224cf5adSJeff Kirsher 	sock_put(sk);
619224cf5adSJeff Kirsher 
620224cf5adSJeff Kirsher 	return 0;
621224cf5adSJeff Kirsher }
622224cf5adSJeff Kirsher 
623224cf5adSJeff Kirsher static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
624224cf5adSJeff Kirsher 		  int sockaddr_len, int flags)
625224cf5adSJeff Kirsher {
626224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
627224cf5adSJeff Kirsher 	struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr;
628224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
629224cf5adSJeff Kirsher 	struct net_device *dev = NULL;
630224cf5adSJeff Kirsher 	struct pppoe_net *pn;
631224cf5adSJeff Kirsher 	struct net *net = NULL;
632224cf5adSJeff Kirsher 	int error;
633224cf5adSJeff Kirsher 
634224cf5adSJeff Kirsher 	lock_sock(sk);
635224cf5adSJeff Kirsher 
636287f3a94SSimon Farnsworth 	INIT_WORK(&po->proto.pppoe.padt_work, pppoe_unbind_sock_work);
637287f3a94SSimon Farnsworth 
638224cf5adSJeff Kirsher 	error = -EINVAL;
639224cf5adSJeff Kirsher 	if (sp->sa_protocol != PX_PROTO_OE)
640224cf5adSJeff Kirsher 		goto end;
641224cf5adSJeff Kirsher 
642224cf5adSJeff Kirsher 	/* Check for already bound sockets */
643224cf5adSJeff Kirsher 	error = -EBUSY;
644224cf5adSJeff Kirsher 	if ((sk->sk_state & PPPOX_CONNECTED) &&
645224cf5adSJeff Kirsher 	     stage_session(sp->sa_addr.pppoe.sid))
646224cf5adSJeff Kirsher 		goto end;
647224cf5adSJeff Kirsher 
648224cf5adSJeff Kirsher 	/* Check for already disconnected sockets, on attempts to disconnect */
649224cf5adSJeff Kirsher 	error = -EALREADY;
650224cf5adSJeff Kirsher 	if ((sk->sk_state & PPPOX_DEAD) &&
651224cf5adSJeff Kirsher 	     !stage_session(sp->sa_addr.pppoe.sid))
652224cf5adSJeff Kirsher 		goto end;
653224cf5adSJeff Kirsher 
654224cf5adSJeff Kirsher 	error = 0;
655224cf5adSJeff Kirsher 
656224cf5adSJeff Kirsher 	/* Delete the old binding */
657224cf5adSJeff Kirsher 	if (stage_session(po->pppoe_pa.sid)) {
658224cf5adSJeff Kirsher 		pppox_unbind_sock(sk);
659224cf5adSJeff Kirsher 		pn = pppoe_pernet(sock_net(sk));
660224cf5adSJeff Kirsher 		delete_item(pn, po->pppoe_pa.sid,
661224cf5adSJeff Kirsher 			    po->pppoe_pa.remote, po->pppoe_ifindex);
662224cf5adSJeff Kirsher 		if (po->pppoe_dev) {
663224cf5adSJeff Kirsher 			dev_put(po->pppoe_dev);
664224cf5adSJeff Kirsher 			po->pppoe_dev = NULL;
665224cf5adSJeff Kirsher 		}
666224cf5adSJeff Kirsher 
667224cf5adSJeff Kirsher 		memset(sk_pppox(po) + 1, 0,
668224cf5adSJeff Kirsher 		       sizeof(struct pppox_sock) - sizeof(struct sock));
669224cf5adSJeff Kirsher 		sk->sk_state = PPPOX_NONE;
670224cf5adSJeff Kirsher 	}
671224cf5adSJeff Kirsher 
672224cf5adSJeff Kirsher 	/* Re-bind in session stage only */
673224cf5adSJeff Kirsher 	if (stage_session(sp->sa_addr.pppoe.sid)) {
674224cf5adSJeff Kirsher 		error = -ENODEV;
675224cf5adSJeff Kirsher 		net = sock_net(sk);
676224cf5adSJeff Kirsher 		dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev);
677224cf5adSJeff Kirsher 		if (!dev)
678224cf5adSJeff Kirsher 			goto err_put;
679224cf5adSJeff Kirsher 
680224cf5adSJeff Kirsher 		po->pppoe_dev = dev;
681224cf5adSJeff Kirsher 		po->pppoe_ifindex = dev->ifindex;
682224cf5adSJeff Kirsher 		pn = pppoe_pernet(net);
683224cf5adSJeff Kirsher 		if (!(dev->flags & IFF_UP)) {
684224cf5adSJeff Kirsher 			goto err_put;
685224cf5adSJeff Kirsher 		}
686224cf5adSJeff Kirsher 
687224cf5adSJeff Kirsher 		memcpy(&po->pppoe_pa,
688224cf5adSJeff Kirsher 		       &sp->sa_addr.pppoe,
689224cf5adSJeff Kirsher 		       sizeof(struct pppoe_addr));
690224cf5adSJeff Kirsher 
691224cf5adSJeff Kirsher 		write_lock_bh(&pn->hash_lock);
692224cf5adSJeff Kirsher 		error = __set_item(pn, po);
693224cf5adSJeff Kirsher 		write_unlock_bh(&pn->hash_lock);
694224cf5adSJeff Kirsher 		if (error < 0)
695224cf5adSJeff Kirsher 			goto err_put;
696224cf5adSJeff Kirsher 
697224cf5adSJeff Kirsher 		po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
698224cf5adSJeff Kirsher 				   dev->hard_header_len);
699224cf5adSJeff Kirsher 
700a8a3e41cSChristoph Schulz 		po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr) - 2;
701224cf5adSJeff Kirsher 		po->chan.private = sk;
702224cf5adSJeff Kirsher 		po->chan.ops = &pppoe_chan_ops;
703224cf5adSJeff Kirsher 
704224cf5adSJeff Kirsher 		error = ppp_register_net_channel(dev_net(dev), &po->chan);
705224cf5adSJeff Kirsher 		if (error) {
706224cf5adSJeff Kirsher 			delete_item(pn, po->pppoe_pa.sid,
707224cf5adSJeff Kirsher 				    po->pppoe_pa.remote, po->pppoe_ifindex);
708224cf5adSJeff Kirsher 			goto err_put;
709224cf5adSJeff Kirsher 		}
710224cf5adSJeff Kirsher 
711224cf5adSJeff Kirsher 		sk->sk_state = PPPOX_CONNECTED;
712224cf5adSJeff Kirsher 	}
713224cf5adSJeff Kirsher 
714224cf5adSJeff Kirsher 	po->num = sp->sa_addr.pppoe.sid;
715224cf5adSJeff Kirsher 
716224cf5adSJeff Kirsher end:
717224cf5adSJeff Kirsher 	release_sock(sk);
718224cf5adSJeff Kirsher 	return error;
719224cf5adSJeff Kirsher err_put:
720224cf5adSJeff Kirsher 	if (po->pppoe_dev) {
721224cf5adSJeff Kirsher 		dev_put(po->pppoe_dev);
722224cf5adSJeff Kirsher 		po->pppoe_dev = NULL;
723224cf5adSJeff Kirsher 	}
724224cf5adSJeff Kirsher 	goto end;
725224cf5adSJeff Kirsher }
726224cf5adSJeff Kirsher 
727224cf5adSJeff Kirsher static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
728224cf5adSJeff Kirsher 		  int *usockaddr_len, int peer)
729224cf5adSJeff Kirsher {
730224cf5adSJeff Kirsher 	int len = sizeof(struct sockaddr_pppox);
731224cf5adSJeff Kirsher 	struct sockaddr_pppox sp;
732224cf5adSJeff Kirsher 
733224cf5adSJeff Kirsher 	sp.sa_family	= AF_PPPOX;
734224cf5adSJeff Kirsher 	sp.sa_protocol	= PX_PROTO_OE;
735224cf5adSJeff Kirsher 	memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
736224cf5adSJeff Kirsher 	       sizeof(struct pppoe_addr));
737224cf5adSJeff Kirsher 
738224cf5adSJeff Kirsher 	memcpy(uaddr, &sp, len);
739224cf5adSJeff Kirsher 
740224cf5adSJeff Kirsher 	*usockaddr_len = len;
741224cf5adSJeff Kirsher 
742224cf5adSJeff Kirsher 	return 0;
743224cf5adSJeff Kirsher }
744224cf5adSJeff Kirsher 
745224cf5adSJeff Kirsher static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
746224cf5adSJeff Kirsher 		unsigned long arg)
747224cf5adSJeff Kirsher {
748224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
749224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
750224cf5adSJeff Kirsher 	int val;
751224cf5adSJeff Kirsher 	int err;
752224cf5adSJeff Kirsher 
753224cf5adSJeff Kirsher 	switch (cmd) {
754224cf5adSJeff Kirsher 	case PPPIOCGMRU:
755224cf5adSJeff Kirsher 		err = -ENXIO;
756224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
757224cf5adSJeff Kirsher 			break;
758224cf5adSJeff Kirsher 
759224cf5adSJeff Kirsher 		err = -EFAULT;
760224cf5adSJeff Kirsher 		if (put_user(po->pppoe_dev->mtu -
761224cf5adSJeff Kirsher 			     sizeof(struct pppoe_hdr) -
762224cf5adSJeff Kirsher 			     PPP_HDRLEN,
763224cf5adSJeff Kirsher 			     (int __user *)arg))
764224cf5adSJeff Kirsher 			break;
765224cf5adSJeff Kirsher 		err = 0;
766224cf5adSJeff Kirsher 		break;
767224cf5adSJeff Kirsher 
768224cf5adSJeff Kirsher 	case PPPIOCSMRU:
769224cf5adSJeff Kirsher 		err = -ENXIO;
770224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
771224cf5adSJeff Kirsher 			break;
772224cf5adSJeff Kirsher 
773224cf5adSJeff Kirsher 		err = -EFAULT;
774224cf5adSJeff Kirsher 		if (get_user(val, (int __user *)arg))
775224cf5adSJeff Kirsher 			break;
776224cf5adSJeff Kirsher 
777224cf5adSJeff Kirsher 		if (val < (po->pppoe_dev->mtu
778224cf5adSJeff Kirsher 			   - sizeof(struct pppoe_hdr)
779224cf5adSJeff Kirsher 			   - PPP_HDRLEN))
780224cf5adSJeff Kirsher 			err = 0;
781224cf5adSJeff Kirsher 		else
782224cf5adSJeff Kirsher 			err = -EINVAL;
783224cf5adSJeff Kirsher 		break;
784224cf5adSJeff Kirsher 
785224cf5adSJeff Kirsher 	case PPPIOCSFLAGS:
786224cf5adSJeff Kirsher 		err = -EFAULT;
787224cf5adSJeff Kirsher 		if (get_user(val, (int __user *)arg))
788224cf5adSJeff Kirsher 			break;
789224cf5adSJeff Kirsher 		err = 0;
790224cf5adSJeff Kirsher 		break;
791224cf5adSJeff Kirsher 
792224cf5adSJeff Kirsher 	case PPPOEIOCSFWD:
793224cf5adSJeff Kirsher 	{
794224cf5adSJeff Kirsher 		struct pppox_sock *relay_po;
795224cf5adSJeff Kirsher 
796224cf5adSJeff Kirsher 		err = -EBUSY;
797224cf5adSJeff Kirsher 		if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
798224cf5adSJeff Kirsher 			break;
799224cf5adSJeff Kirsher 
800224cf5adSJeff Kirsher 		err = -ENOTCONN;
801224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_CONNECTED))
802224cf5adSJeff Kirsher 			break;
803224cf5adSJeff Kirsher 
804224cf5adSJeff Kirsher 		/* PPPoE address from the user specifies an outbound
805224cf5adSJeff Kirsher 		   PPPoE address which frames are forwarded to */
806224cf5adSJeff Kirsher 		err = -EFAULT;
807224cf5adSJeff Kirsher 		if (copy_from_user(&po->pppoe_relay,
808224cf5adSJeff Kirsher 				   (void __user *)arg,
809224cf5adSJeff Kirsher 				   sizeof(struct sockaddr_pppox)))
810224cf5adSJeff Kirsher 			break;
811224cf5adSJeff Kirsher 
812224cf5adSJeff Kirsher 		err = -EINVAL;
813224cf5adSJeff Kirsher 		if (po->pppoe_relay.sa_family != AF_PPPOX ||
814224cf5adSJeff Kirsher 		    po->pppoe_relay.sa_protocol != PX_PROTO_OE)
815224cf5adSJeff Kirsher 			break;
816224cf5adSJeff Kirsher 
817224cf5adSJeff Kirsher 		/* Check that the socket referenced by the address
818224cf5adSJeff Kirsher 		   actually exists. */
819224cf5adSJeff Kirsher 		relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
820224cf5adSJeff Kirsher 		if (!relay_po)
821224cf5adSJeff Kirsher 			break;
822224cf5adSJeff Kirsher 
823224cf5adSJeff Kirsher 		sock_put(sk_pppox(relay_po));
824224cf5adSJeff Kirsher 		sk->sk_state |= PPPOX_RELAY;
825224cf5adSJeff Kirsher 		err = 0;
826224cf5adSJeff Kirsher 		break;
827224cf5adSJeff Kirsher 	}
828224cf5adSJeff Kirsher 
829224cf5adSJeff Kirsher 	case PPPOEIOCDFWD:
830224cf5adSJeff Kirsher 		err = -EALREADY;
831224cf5adSJeff Kirsher 		if (!(sk->sk_state & PPPOX_RELAY))
832224cf5adSJeff Kirsher 			break;
833224cf5adSJeff Kirsher 
834224cf5adSJeff Kirsher 		sk->sk_state &= ~PPPOX_RELAY;
835224cf5adSJeff Kirsher 		err = 0;
836224cf5adSJeff Kirsher 		break;
837224cf5adSJeff Kirsher 
838224cf5adSJeff Kirsher 	default:
839224cf5adSJeff Kirsher 		err = -ENOTTY;
840224cf5adSJeff Kirsher 	}
841224cf5adSJeff Kirsher 
842224cf5adSJeff Kirsher 	return err;
843224cf5adSJeff Kirsher }
844224cf5adSJeff Kirsher 
8451b784140SYing Xue static int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
8461b784140SYing Xue 			 size_t total_len)
847224cf5adSJeff Kirsher {
848224cf5adSJeff Kirsher 	struct sk_buff *skb;
849224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
850224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
851224cf5adSJeff Kirsher 	int error;
852224cf5adSJeff Kirsher 	struct pppoe_hdr hdr;
853224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
854224cf5adSJeff Kirsher 	struct net_device *dev;
855224cf5adSJeff Kirsher 	char *start;
856224cf5adSJeff Kirsher 
857224cf5adSJeff Kirsher 	lock_sock(sk);
858224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
859224cf5adSJeff Kirsher 		error = -ENOTCONN;
860224cf5adSJeff Kirsher 		goto end;
861224cf5adSJeff Kirsher 	}
862224cf5adSJeff Kirsher 
863224cf5adSJeff Kirsher 	hdr.ver = 1;
864224cf5adSJeff Kirsher 	hdr.type = 1;
865224cf5adSJeff Kirsher 	hdr.code = 0;
866224cf5adSJeff Kirsher 	hdr.sid = po->num;
867224cf5adSJeff Kirsher 
868224cf5adSJeff Kirsher 	dev = po->pppoe_dev;
869224cf5adSJeff Kirsher 
870224cf5adSJeff Kirsher 	error = -EMSGSIZE;
871224cf5adSJeff Kirsher 	if (total_len > (dev->mtu + dev->hard_header_len))
872224cf5adSJeff Kirsher 		goto end;
873224cf5adSJeff Kirsher 
874224cf5adSJeff Kirsher 
875224cf5adSJeff Kirsher 	skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
876224cf5adSJeff Kirsher 			   0, GFP_KERNEL);
877224cf5adSJeff Kirsher 	if (!skb) {
878224cf5adSJeff Kirsher 		error = -ENOMEM;
879224cf5adSJeff Kirsher 		goto end;
880224cf5adSJeff Kirsher 	}
881224cf5adSJeff Kirsher 
882224cf5adSJeff Kirsher 	/* Reserve space for headers. */
883224cf5adSJeff Kirsher 	skb_reserve(skb, dev->hard_header_len);
884224cf5adSJeff Kirsher 	skb_reset_network_header(skb);
885224cf5adSJeff Kirsher 
886224cf5adSJeff Kirsher 	skb->dev = dev;
887224cf5adSJeff Kirsher 
888224cf5adSJeff Kirsher 	skb->priority = sk->sk_priority;
889224cf5adSJeff Kirsher 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
890224cf5adSJeff Kirsher 
891224cf5adSJeff Kirsher 	ph = (struct pppoe_hdr *)skb_put(skb, total_len + sizeof(struct pppoe_hdr));
892224cf5adSJeff Kirsher 	start = (char *)&ph->tag[0];
893224cf5adSJeff Kirsher 
8946ce8e9ceSAl Viro 	error = memcpy_from_msg(start, m, total_len);
895224cf5adSJeff Kirsher 	if (error < 0) {
896224cf5adSJeff Kirsher 		kfree_skb(skb);
897224cf5adSJeff Kirsher 		goto end;
898224cf5adSJeff Kirsher 	}
899224cf5adSJeff Kirsher 
900224cf5adSJeff Kirsher 	error = total_len;
901224cf5adSJeff Kirsher 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
902224cf5adSJeff Kirsher 			po->pppoe_pa.remote, NULL, total_len);
903224cf5adSJeff Kirsher 
904224cf5adSJeff Kirsher 	memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
905224cf5adSJeff Kirsher 
906224cf5adSJeff Kirsher 	ph->length = htons(total_len);
907224cf5adSJeff Kirsher 
908224cf5adSJeff Kirsher 	dev_queue_xmit(skb);
909224cf5adSJeff Kirsher 
910224cf5adSJeff Kirsher end:
911224cf5adSJeff Kirsher 	release_sock(sk);
912224cf5adSJeff Kirsher 	return error;
913224cf5adSJeff Kirsher }
914224cf5adSJeff Kirsher 
915224cf5adSJeff Kirsher /************************************************************************
916224cf5adSJeff Kirsher  *
917224cf5adSJeff Kirsher  * xmit function for internal use.
918224cf5adSJeff Kirsher  *
919224cf5adSJeff Kirsher  ***********************************************************************/
920224cf5adSJeff Kirsher static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
921224cf5adSJeff Kirsher {
922224cf5adSJeff Kirsher 	struct pppox_sock *po = pppox_sk(sk);
923224cf5adSJeff Kirsher 	struct net_device *dev = po->pppoe_dev;
924224cf5adSJeff Kirsher 	struct pppoe_hdr *ph;
925224cf5adSJeff Kirsher 	int data_len = skb->len;
926224cf5adSJeff Kirsher 
927224cf5adSJeff Kirsher 	/* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
928224cf5adSJeff Kirsher 	 * xmit operations conclude prior to an unregistration call.  Thus
929224cf5adSJeff Kirsher 	 * sk->sk_state cannot change, so we don't need to do lock_sock().
930224cf5adSJeff Kirsher 	 * But, we also can't do a lock_sock since that introduces a potential
931224cf5adSJeff Kirsher 	 * deadlock as we'd reverse the lock ordering used when calling
932224cf5adSJeff Kirsher 	 * ppp_unregister_channel().
933224cf5adSJeff Kirsher 	 */
934224cf5adSJeff Kirsher 
935224cf5adSJeff Kirsher 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
936224cf5adSJeff Kirsher 		goto abort;
937224cf5adSJeff Kirsher 
938224cf5adSJeff Kirsher 	if (!dev)
939224cf5adSJeff Kirsher 		goto abort;
940224cf5adSJeff Kirsher 
941224cf5adSJeff Kirsher 	/* Copy the data if there is no space for the header or if it's
942224cf5adSJeff Kirsher 	 * read-only.
943224cf5adSJeff Kirsher 	 */
944224cf5adSJeff Kirsher 	if (skb_cow_head(skb, sizeof(*ph) + dev->hard_header_len))
945224cf5adSJeff Kirsher 		goto abort;
946224cf5adSJeff Kirsher 
947224cf5adSJeff Kirsher 	__skb_push(skb, sizeof(*ph));
948224cf5adSJeff Kirsher 	skb_reset_network_header(skb);
949224cf5adSJeff Kirsher 
950224cf5adSJeff Kirsher 	ph = pppoe_hdr(skb);
951224cf5adSJeff Kirsher 	ph->ver	= 1;
952224cf5adSJeff Kirsher 	ph->type = 1;
953224cf5adSJeff Kirsher 	ph->code = 0;
954224cf5adSJeff Kirsher 	ph->sid	= po->num;
955224cf5adSJeff Kirsher 	ph->length = htons(data_len);
956224cf5adSJeff Kirsher 
957224cf5adSJeff Kirsher 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
958224cf5adSJeff Kirsher 	skb->dev = dev;
959224cf5adSJeff Kirsher 
960224cf5adSJeff Kirsher 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
961224cf5adSJeff Kirsher 			po->pppoe_pa.remote, NULL, data_len);
962224cf5adSJeff Kirsher 
963224cf5adSJeff Kirsher 	dev_queue_xmit(skb);
964224cf5adSJeff Kirsher 	return 1;
965224cf5adSJeff Kirsher 
966224cf5adSJeff Kirsher abort:
967224cf5adSJeff Kirsher 	kfree_skb(skb);
968224cf5adSJeff Kirsher 	return 1;
969224cf5adSJeff Kirsher }
970224cf5adSJeff Kirsher 
971224cf5adSJeff Kirsher /************************************************************************
972224cf5adSJeff Kirsher  *
973224cf5adSJeff Kirsher  * xmit function called by generic PPP driver
974224cf5adSJeff Kirsher  * sends PPP frame over PPPoE socket
975224cf5adSJeff Kirsher  *
976224cf5adSJeff Kirsher  ***********************************************************************/
977224cf5adSJeff Kirsher static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
978224cf5adSJeff Kirsher {
979224cf5adSJeff Kirsher 	struct sock *sk = (struct sock *)chan->private;
980224cf5adSJeff Kirsher 	return __pppoe_xmit(sk, skb);
981224cf5adSJeff Kirsher }
982224cf5adSJeff Kirsher 
983224cf5adSJeff Kirsher static const struct ppp_channel_ops pppoe_chan_ops = {
984224cf5adSJeff Kirsher 	.start_xmit = pppoe_xmit,
985224cf5adSJeff Kirsher };
986224cf5adSJeff Kirsher 
9871b784140SYing Xue static int pppoe_recvmsg(struct socket *sock, struct msghdr *m,
9881b784140SYing Xue 			 size_t total_len, int flags)
989224cf5adSJeff Kirsher {
990224cf5adSJeff Kirsher 	struct sock *sk = sock->sk;
991224cf5adSJeff Kirsher 	struct sk_buff *skb;
992224cf5adSJeff Kirsher 	int error = 0;
993224cf5adSJeff Kirsher 
994224cf5adSJeff Kirsher 	if (sk->sk_state & PPPOX_BOUND) {
995224cf5adSJeff Kirsher 		error = -EIO;
996224cf5adSJeff Kirsher 		goto end;
997224cf5adSJeff Kirsher 	}
998224cf5adSJeff Kirsher 
999224cf5adSJeff Kirsher 	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1000224cf5adSJeff Kirsher 				flags & MSG_DONTWAIT, &error);
1001224cf5adSJeff Kirsher 	if (error < 0)
1002224cf5adSJeff Kirsher 		goto end;
1003224cf5adSJeff Kirsher 
1004224cf5adSJeff Kirsher 	if (skb) {
1005224cf5adSJeff Kirsher 		total_len = min_t(size_t, total_len, skb->len);
100651f3d02bSDavid S. Miller 		error = skb_copy_datagram_msg(skb, 0, m, total_len);
1007968d7018SEric Dumazet 		if (error == 0) {
1008968d7018SEric Dumazet 			consume_skb(skb);
1009968d7018SEric Dumazet 			return total_len;
1010968d7018SEric Dumazet 		}
1011224cf5adSJeff Kirsher 	}
1012224cf5adSJeff Kirsher 
1013224cf5adSJeff Kirsher 	kfree_skb(skb);
1014224cf5adSJeff Kirsher end:
1015224cf5adSJeff Kirsher 	return error;
1016224cf5adSJeff Kirsher }
1017224cf5adSJeff Kirsher 
1018224cf5adSJeff Kirsher #ifdef CONFIG_PROC_FS
1019224cf5adSJeff Kirsher static int pppoe_seq_show(struct seq_file *seq, void *v)
1020224cf5adSJeff Kirsher {
1021224cf5adSJeff Kirsher 	struct pppox_sock *po;
1022224cf5adSJeff Kirsher 	char *dev_name;
1023224cf5adSJeff Kirsher 
1024224cf5adSJeff Kirsher 	if (v == SEQ_START_TOKEN) {
1025224cf5adSJeff Kirsher 		seq_puts(seq, "Id       Address              Device\n");
1026224cf5adSJeff Kirsher 		goto out;
1027224cf5adSJeff Kirsher 	}
1028224cf5adSJeff Kirsher 
1029224cf5adSJeff Kirsher 	po = v;
1030224cf5adSJeff Kirsher 	dev_name = po->pppoe_pa.dev;
1031224cf5adSJeff Kirsher 
1032224cf5adSJeff Kirsher 	seq_printf(seq, "%08X %pM %8s\n",
1033224cf5adSJeff Kirsher 		po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
1034224cf5adSJeff Kirsher out:
1035224cf5adSJeff Kirsher 	return 0;
1036224cf5adSJeff Kirsher }
1037224cf5adSJeff Kirsher 
1038224cf5adSJeff Kirsher static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
1039224cf5adSJeff Kirsher {
1040224cf5adSJeff Kirsher 	struct pppox_sock *po;
1041224cf5adSJeff Kirsher 	int i;
1042224cf5adSJeff Kirsher 
1043224cf5adSJeff Kirsher 	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
1044224cf5adSJeff Kirsher 		po = pn->hash_table[i];
1045224cf5adSJeff Kirsher 		while (po) {
1046224cf5adSJeff Kirsher 			if (!pos--)
1047224cf5adSJeff Kirsher 				goto out;
1048224cf5adSJeff Kirsher 			po = po->next;
1049224cf5adSJeff Kirsher 		}
1050224cf5adSJeff Kirsher 	}
1051224cf5adSJeff Kirsher 
1052224cf5adSJeff Kirsher out:
1053224cf5adSJeff Kirsher 	return po;
1054224cf5adSJeff Kirsher }
1055224cf5adSJeff Kirsher 
1056224cf5adSJeff Kirsher static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
1057224cf5adSJeff Kirsher 	__acquires(pn->hash_lock)
1058224cf5adSJeff Kirsher {
1059224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1060224cf5adSJeff Kirsher 	loff_t l = *pos;
1061224cf5adSJeff Kirsher 
1062224cf5adSJeff Kirsher 	read_lock_bh(&pn->hash_lock);
1063224cf5adSJeff Kirsher 	return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
1064224cf5adSJeff Kirsher }
1065224cf5adSJeff Kirsher 
1066224cf5adSJeff Kirsher static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1067224cf5adSJeff Kirsher {
1068224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1069224cf5adSJeff Kirsher 	struct pppox_sock *po;
1070224cf5adSJeff Kirsher 
1071224cf5adSJeff Kirsher 	++*pos;
1072224cf5adSJeff Kirsher 	if (v == SEQ_START_TOKEN) {
1073224cf5adSJeff Kirsher 		po = pppoe_get_idx(pn, 0);
1074224cf5adSJeff Kirsher 		goto out;
1075224cf5adSJeff Kirsher 	}
1076224cf5adSJeff Kirsher 	po = v;
1077224cf5adSJeff Kirsher 	if (po->next)
1078224cf5adSJeff Kirsher 		po = po->next;
1079224cf5adSJeff Kirsher 	else {
1080224cf5adSJeff Kirsher 		int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1081224cf5adSJeff Kirsher 
1082224cf5adSJeff Kirsher 		po = NULL;
1083224cf5adSJeff Kirsher 		while (++hash < PPPOE_HASH_SIZE) {
1084224cf5adSJeff Kirsher 			po = pn->hash_table[hash];
1085224cf5adSJeff Kirsher 			if (po)
1086224cf5adSJeff Kirsher 				break;
1087224cf5adSJeff Kirsher 		}
1088224cf5adSJeff Kirsher 	}
1089224cf5adSJeff Kirsher 
1090224cf5adSJeff Kirsher out:
1091224cf5adSJeff Kirsher 	return po;
1092224cf5adSJeff Kirsher }
1093224cf5adSJeff Kirsher 
1094224cf5adSJeff Kirsher static void pppoe_seq_stop(struct seq_file *seq, void *v)
1095224cf5adSJeff Kirsher 	__releases(pn->hash_lock)
1096224cf5adSJeff Kirsher {
1097224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1098224cf5adSJeff Kirsher 	read_unlock_bh(&pn->hash_lock);
1099224cf5adSJeff Kirsher }
1100224cf5adSJeff Kirsher 
1101224cf5adSJeff Kirsher static const struct seq_operations pppoe_seq_ops = {
1102224cf5adSJeff Kirsher 	.start		= pppoe_seq_start,
1103224cf5adSJeff Kirsher 	.next		= pppoe_seq_next,
1104224cf5adSJeff Kirsher 	.stop		= pppoe_seq_stop,
1105224cf5adSJeff Kirsher 	.show		= pppoe_seq_show,
1106224cf5adSJeff Kirsher };
1107224cf5adSJeff Kirsher 
1108224cf5adSJeff Kirsher static int pppoe_seq_open(struct inode *inode, struct file *file)
1109224cf5adSJeff Kirsher {
1110224cf5adSJeff Kirsher 	return seq_open_net(inode, file, &pppoe_seq_ops,
1111224cf5adSJeff Kirsher 			sizeof(struct seq_net_private));
1112224cf5adSJeff Kirsher }
1113224cf5adSJeff Kirsher 
1114224cf5adSJeff Kirsher static const struct file_operations pppoe_seq_fops = {
1115224cf5adSJeff Kirsher 	.owner		= THIS_MODULE,
1116224cf5adSJeff Kirsher 	.open		= pppoe_seq_open,
1117224cf5adSJeff Kirsher 	.read		= seq_read,
1118224cf5adSJeff Kirsher 	.llseek		= seq_lseek,
1119224cf5adSJeff Kirsher 	.release	= seq_release_net,
1120224cf5adSJeff Kirsher };
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,
1133224cf5adSJeff Kirsher 	.poll		= datagram_poll,
1134224cf5adSJeff Kirsher 	.listen		= sock_no_listen,
1135224cf5adSJeff Kirsher 	.shutdown	= sock_no_shutdown,
1136224cf5adSJeff Kirsher 	.setsockopt	= sock_no_setsockopt,
1137224cf5adSJeff Kirsher 	.getsockopt	= sock_no_getsockopt,
1138224cf5adSJeff Kirsher 	.sendmsg	= pppoe_sendmsg,
1139224cf5adSJeff Kirsher 	.recvmsg	= pppoe_recvmsg,
1140224cf5adSJeff Kirsher 	.mmap		= sock_no_mmap,
1141224cf5adSJeff Kirsher 	.ioctl		= pppox_ioctl,
1142224cf5adSJeff Kirsher };
1143224cf5adSJeff Kirsher 
1144224cf5adSJeff Kirsher static const struct pppox_proto pppoe_proto = {
1145224cf5adSJeff Kirsher 	.create	= pppoe_create,
1146224cf5adSJeff Kirsher 	.ioctl	= pppoe_ioctl,
1147224cf5adSJeff Kirsher 	.owner	= THIS_MODULE,
1148224cf5adSJeff Kirsher };
1149224cf5adSJeff Kirsher 
1150224cf5adSJeff Kirsher static __net_init int pppoe_init_net(struct net *net)
1151224cf5adSJeff Kirsher {
1152224cf5adSJeff Kirsher 	struct pppoe_net *pn = pppoe_pernet(net);
1153224cf5adSJeff Kirsher 	struct proc_dir_entry *pde;
1154224cf5adSJeff Kirsher 
1155224cf5adSJeff Kirsher 	rwlock_init(&pn->hash_lock);
1156224cf5adSJeff Kirsher 
1157d4beaa66SGao feng 	pde = proc_create("pppoe", S_IRUGO, net->proc_net, &pppoe_seq_fops);
1158224cf5adSJeff Kirsher #ifdef CONFIG_PROC_FS
1159224cf5adSJeff Kirsher 	if (!pde)
1160224cf5adSJeff Kirsher 		return -ENOMEM;
1161224cf5adSJeff Kirsher #endif
1162224cf5adSJeff Kirsher 
1163224cf5adSJeff Kirsher 	return 0;
1164224cf5adSJeff Kirsher }
1165224cf5adSJeff Kirsher 
1166224cf5adSJeff Kirsher static __net_exit void pppoe_exit_net(struct net *net)
1167224cf5adSJeff Kirsher {
1168ece31ffdSGao feng 	remove_proc_entry("pppoe", net->proc_net);
1169224cf5adSJeff Kirsher }
1170224cf5adSJeff Kirsher 
1171224cf5adSJeff Kirsher static struct pernet_operations pppoe_net_ops = {
1172224cf5adSJeff Kirsher 	.init = pppoe_init_net,
1173224cf5adSJeff Kirsher 	.exit = pppoe_exit_net,
1174224cf5adSJeff Kirsher 	.id   = &pppoe_net_id,
1175224cf5adSJeff Kirsher 	.size = sizeof(struct pppoe_net),
1176224cf5adSJeff Kirsher };
1177224cf5adSJeff Kirsher 
1178224cf5adSJeff Kirsher static int __init pppoe_init(void)
1179224cf5adSJeff Kirsher {
1180224cf5adSJeff Kirsher 	int err;
1181224cf5adSJeff Kirsher 
1182224cf5adSJeff Kirsher 	err = register_pernet_device(&pppoe_net_ops);
1183224cf5adSJeff Kirsher 	if (err)
1184224cf5adSJeff Kirsher 		goto out;
1185224cf5adSJeff Kirsher 
1186224cf5adSJeff Kirsher 	err = proto_register(&pppoe_sk_proto, 0);
1187224cf5adSJeff Kirsher 	if (err)
1188224cf5adSJeff Kirsher 		goto out_unregister_net_ops;
1189224cf5adSJeff Kirsher 
1190224cf5adSJeff Kirsher 	err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1191224cf5adSJeff Kirsher 	if (err)
1192224cf5adSJeff Kirsher 		goto out_unregister_pppoe_proto;
1193224cf5adSJeff Kirsher 
1194224cf5adSJeff Kirsher 	dev_add_pack(&pppoes_ptype);
1195224cf5adSJeff Kirsher 	dev_add_pack(&pppoed_ptype);
1196224cf5adSJeff Kirsher 	register_netdevice_notifier(&pppoe_notifier);
1197224cf5adSJeff Kirsher 
1198224cf5adSJeff Kirsher 	return 0;
1199224cf5adSJeff Kirsher 
1200224cf5adSJeff Kirsher out_unregister_pppoe_proto:
1201224cf5adSJeff Kirsher 	proto_unregister(&pppoe_sk_proto);
1202224cf5adSJeff Kirsher out_unregister_net_ops:
1203224cf5adSJeff Kirsher 	unregister_pernet_device(&pppoe_net_ops);
1204224cf5adSJeff Kirsher out:
1205224cf5adSJeff Kirsher 	return err;
1206224cf5adSJeff Kirsher }
1207224cf5adSJeff Kirsher 
1208224cf5adSJeff Kirsher static void __exit pppoe_exit(void)
1209224cf5adSJeff Kirsher {
1210224cf5adSJeff Kirsher 	unregister_netdevice_notifier(&pppoe_notifier);
1211224cf5adSJeff Kirsher 	dev_remove_pack(&pppoed_ptype);
1212224cf5adSJeff Kirsher 	dev_remove_pack(&pppoes_ptype);
1213224cf5adSJeff Kirsher 	unregister_pppox_proto(PX_PROTO_OE);
1214224cf5adSJeff Kirsher 	proto_unregister(&pppoe_sk_proto);
1215224cf5adSJeff Kirsher 	unregister_pernet_device(&pppoe_net_ops);
1216224cf5adSJeff Kirsher }
1217224cf5adSJeff Kirsher 
1218224cf5adSJeff Kirsher module_init(pppoe_init);
1219224cf5adSJeff Kirsher module_exit(pppoe_exit);
1220224cf5adSJeff Kirsher 
1221224cf5adSJeff Kirsher MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1222224cf5adSJeff Kirsher MODULE_DESCRIPTION("PPP over Ethernet driver");
1223224cf5adSJeff Kirsher MODULE_LICENSE("GPL");
1224224cf5adSJeff Kirsher MODULE_ALIAS_NETPROTO(PF_PPPOX);
1225