xref: /linux/drivers/net/ppp/pppoe.c (revision d91517839e5d95adc0cf4b28caa7af62a71de526)
1 /** -*- linux-c -*- ***********************************************************
2  * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
3  *
4  * PPPoX --- Generic PPP encapsulation socket family
5  * PPPoE --- PPP over Ethernet (RFC 2516)
6  *
7  *
8  * Version:	0.7.0
9  *
10  * 070228 :	Fix to allow multiple sessions with same remote MAC and same
11  *		session id by including the local device ifindex in the
12  *		tuple identifying a session. This also ensures packets can't
13  *		be injected into a session from interfaces other than the one
14  *		specified by userspace. Florian Zumbiehl <florz@florz.de>
15  *		(Oh, BTW, this one is YYMMDD, in case you were wondering ...)
16  * 220102 :	Fix module use count on failure in pppoe_create, pppox_sk -acme
17  * 030700 :	Fixed connect logic to allow for disconnect.
18  * 270700 :	Fixed potential SMP problems; we must protect against
19  *		simultaneous invocation of ppp_input
20  *		and ppp_unregister_channel.
21  * 040800 :	Respect reference count mechanisms on net-devices.
22  * 200800 :	fix kfree(skb) in pppoe_rcv (acme)
23  *		Module reference count is decremented in the right spot now,
24  *		guards against sock_put not actually freeing the sk
25  *		in pppoe_release.
26  * 051000 :	Initialization cleanup.
27  * 111100 :	Fix recvmsg.
28  * 050101 :	Fix PADT procesing.
29  * 140501 :	Use pppoe_rcv_core to handle all backlog. (Alexey)
30  * 170701 :	Do not lock_sock with rwlock held. (DaveM)
31  *		Ignore discovery frames if user has socket
32  *		locked. (DaveM)
33  *		Ignore return value of dev_queue_xmit in __pppoe_xmit
34  *		or else we may kfree an SKB twice. (DaveM)
35  * 190701 :	When doing copies of skb's in __pppoe_xmit, always delete
36  *		the original skb that was passed in on success, never on
37  *		failure.  Delete the copy of the skb on failure to avoid
38  *		a memory leak.
39  * 081001 :	Misc. cleanup (licence string, non-blocking, prevent
40  *		reference of device on close).
41  * 121301 :	New ppp channels interface; cannot unregister a channel
42  *		from interrupts.  Thus, we mark the socket as a ZOMBIE
43  *		and do the unregistration later.
44  * 081002 :	seq_file support for proc stuff -acme
45  * 111602 :	Merge all 2.4 fixes into 2.5/2.6 tree.  Label 2.5/2.6
46  *		as version 0.7.  Spacing cleanup.
47  * Author:	Michal Ostrowski <mostrows@speakeasy.net>
48  * Contributors:
49  * 		Arnaldo Carvalho de Melo <acme@conectiva.com.br>
50  *		David S. Miller (davem@redhat.com)
51  *
52  * License:
53  *		This program is free software; you can redistribute it and/or
54  *		modify it under the terms of the GNU General Public License
55  *		as published by the Free Software Foundation; either version
56  *		2 of the License, or (at your option) any later version.
57  *
58  */
59 
60 #include <linux/string.h>
61 #include <linux/module.h>
62 #include <linux/kernel.h>
63 #include <linux/slab.h>
64 #include <linux/errno.h>
65 #include <linux/netdevice.h>
66 #include <linux/net.h>
67 #include <linux/inetdevice.h>
68 #include <linux/etherdevice.h>
69 #include <linux/skbuff.h>
70 #include <linux/init.h>
71 #include <linux/if_ether.h>
72 #include <linux/if_pppox.h>
73 #include <linux/ppp_channel.h>
74 #include <linux/ppp_defs.h>
75 #include <linux/ppp-ioctl.h>
76 #include <linux/notifier.h>
77 #include <linux/file.h>
78 #include <linux/proc_fs.h>
79 #include <linux/seq_file.h>
80 
81 #include <linux/nsproxy.h>
82 #include <net/net_namespace.h>
83 #include <net/netns/generic.h>
84 #include <net/sock.h>
85 
86 #include <asm/uaccess.h>
87 
88 #define PPPOE_HASH_BITS 4
89 #define PPPOE_HASH_SIZE (1 << PPPOE_HASH_BITS)
90 #define PPPOE_HASH_MASK	(PPPOE_HASH_SIZE - 1)
91 
92 static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
93 
94 static const struct proto_ops pppoe_ops;
95 static const struct ppp_channel_ops pppoe_chan_ops;
96 
97 /* per-net private data for this module */
98 static int pppoe_net_id __read_mostly;
99 struct pppoe_net {
100 	/*
101 	 * we could use _single_ hash table for all
102 	 * nets by injecting net id into the hash but
103 	 * it would increase hash chains and add
104 	 * a few additional math comparations messy
105 	 * as well, moreover in case of SMP less locking
106 	 * controversy here
107 	 */
108 	struct pppox_sock *hash_table[PPPOE_HASH_SIZE];
109 	rwlock_t hash_lock;
110 };
111 
112 /*
113  * PPPoE could be in the following stages:
114  * 1) Discovery stage (to obtain remote MAC and Session ID)
115  * 2) Session stage (MAC and SID are known)
116  *
117  * Ethernet frames have a special tag for this but
118  * we use simpler approach based on session id
119  */
120 static inline bool stage_session(__be16 sid)
121 {
122 	return sid != 0;
123 }
124 
125 static inline struct pppoe_net *pppoe_pernet(struct net *net)
126 {
127 	BUG_ON(!net);
128 
129 	return net_generic(net, pppoe_net_id);
130 }
131 
132 static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
133 {
134 	return a->sid == b->sid && ether_addr_equal(a->remote, b->remote);
135 }
136 
137 static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr)
138 {
139 	return a->sid == sid && ether_addr_equal(a->remote, addr);
140 }
141 
142 #if 8 % PPPOE_HASH_BITS
143 #error 8 must be a multiple of PPPOE_HASH_BITS
144 #endif
145 
146 static int hash_item(__be16 sid, unsigned char *addr)
147 {
148 	unsigned char hash = 0;
149 	unsigned int i;
150 
151 	for (i = 0; i < ETH_ALEN; i++)
152 		hash ^= addr[i];
153 	for (i = 0; i < sizeof(sid_t) * 8; i += 8)
154 		hash ^= (__force __u32)sid >> i;
155 	for (i = 8; (i >>= 1) >= PPPOE_HASH_BITS;)
156 		hash ^= hash >> i;
157 
158 	return hash & PPPOE_HASH_MASK;
159 }
160 
161 /**********************************************************************
162  *
163  *  Set/get/delete/rehash items  (internal versions)
164  *
165  **********************************************************************/
166 static struct pppox_sock *__get_item(struct pppoe_net *pn, __be16 sid,
167 				unsigned char *addr, int ifindex)
168 {
169 	int hash = hash_item(sid, addr);
170 	struct pppox_sock *ret;
171 
172 	ret = pn->hash_table[hash];
173 	while (ret) {
174 		if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
175 		    ret->pppoe_ifindex == ifindex)
176 			return ret;
177 
178 		ret = ret->next;
179 	}
180 
181 	return NULL;
182 }
183 
184 static int __set_item(struct pppoe_net *pn, struct pppox_sock *po)
185 {
186 	int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
187 	struct pppox_sock *ret;
188 
189 	ret = pn->hash_table[hash];
190 	while (ret) {
191 		if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) &&
192 		    ret->pppoe_ifindex == po->pppoe_ifindex)
193 			return -EALREADY;
194 
195 		ret = ret->next;
196 	}
197 
198 	po->next = pn->hash_table[hash];
199 	pn->hash_table[hash] = po;
200 
201 	return 0;
202 }
203 
204 static void __delete_item(struct pppoe_net *pn, __be16 sid,
205 					char *addr, int ifindex)
206 {
207 	int hash = hash_item(sid, addr);
208 	struct pppox_sock *ret, **src;
209 
210 	ret = pn->hash_table[hash];
211 	src = &pn->hash_table[hash];
212 
213 	while (ret) {
214 		if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
215 		    ret->pppoe_ifindex == ifindex) {
216 			*src = ret->next;
217 			break;
218 		}
219 
220 		src = &ret->next;
221 		ret = ret->next;
222 	}
223 }
224 
225 /**********************************************************************
226  *
227  *  Set/get/delete/rehash items
228  *
229  **********************************************************************/
230 static inline struct pppox_sock *get_item(struct pppoe_net *pn, __be16 sid,
231 					unsigned char *addr, int ifindex)
232 {
233 	struct pppox_sock *po;
234 
235 	read_lock_bh(&pn->hash_lock);
236 	po = __get_item(pn, sid, addr, ifindex);
237 	if (po)
238 		sock_hold(sk_pppox(po));
239 	read_unlock_bh(&pn->hash_lock);
240 
241 	return po;
242 }
243 
244 static inline struct pppox_sock *get_item_by_addr(struct net *net,
245 						struct sockaddr_pppox *sp)
246 {
247 	struct net_device *dev;
248 	struct pppoe_net *pn;
249 	struct pppox_sock *pppox_sock = NULL;
250 
251 	int ifindex;
252 
253 	rcu_read_lock();
254 	dev = dev_get_by_name_rcu(net, sp->sa_addr.pppoe.dev);
255 	if (dev) {
256 		ifindex = dev->ifindex;
257 		pn = pppoe_pernet(net);
258 		pppox_sock = get_item(pn, sp->sa_addr.pppoe.sid,
259 				sp->sa_addr.pppoe.remote, ifindex);
260 	}
261 	rcu_read_unlock();
262 	return pppox_sock;
263 }
264 
265 static inline void delete_item(struct pppoe_net *pn, __be16 sid,
266 					char *addr, int ifindex)
267 {
268 	write_lock_bh(&pn->hash_lock);
269 	__delete_item(pn, sid, addr, ifindex);
270 	write_unlock_bh(&pn->hash_lock);
271 }
272 
273 /***************************************************************************
274  *
275  *  Handler for device events.
276  *  Certain device events require that sockets be unconnected.
277  *
278  **************************************************************************/
279 
280 static void pppoe_flush_dev(struct net_device *dev)
281 {
282 	struct pppoe_net *pn;
283 	int i;
284 
285 	pn = pppoe_pernet(dev_net(dev));
286 	write_lock_bh(&pn->hash_lock);
287 	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
288 		struct pppox_sock *po = pn->hash_table[i];
289 		struct sock *sk;
290 
291 		while (po) {
292 			while (po && po->pppoe_dev != dev) {
293 				po = po->next;
294 			}
295 
296 			if (!po)
297 				break;
298 
299 			sk = sk_pppox(po);
300 
301 			/* We always grab the socket lock, followed by the
302 			 * hash_lock, in that order.  Since we should hold the
303 			 * sock lock while doing any unbinding, we need to
304 			 * release the lock we're holding.  Hold a reference to
305 			 * the sock so it doesn't disappear as we're jumping
306 			 * between locks.
307 			 */
308 
309 			sock_hold(sk);
310 			write_unlock_bh(&pn->hash_lock);
311 			lock_sock(sk);
312 
313 			if (po->pppoe_dev == dev &&
314 			    sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND | PPPOX_ZOMBIE)) {
315 				pppox_unbind_sock(sk);
316 				sk->sk_state = PPPOX_ZOMBIE;
317 				sk->sk_state_change(sk);
318 				po->pppoe_dev = NULL;
319 				dev_put(dev);
320 			}
321 
322 			release_sock(sk);
323 			sock_put(sk);
324 
325 			/* Restart the process from the start of the current
326 			 * hash chain. We dropped locks so the world may have
327 			 * change from underneath us.
328 			 */
329 
330 			BUG_ON(pppoe_pernet(dev_net(dev)) == NULL);
331 			write_lock_bh(&pn->hash_lock);
332 			po = pn->hash_table[i];
333 		}
334 	}
335 	write_unlock_bh(&pn->hash_lock);
336 }
337 
338 static int pppoe_device_event(struct notifier_block *this,
339 			      unsigned long event, void *ptr)
340 {
341 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
342 
343 	/* Only look at sockets that are using this specific device. */
344 	switch (event) {
345 	case NETDEV_CHANGEADDR:
346 	case NETDEV_CHANGEMTU:
347 		/* A change in mtu or address is a bad thing, requiring
348 		 * LCP re-negotiation.
349 		 */
350 
351 	case NETDEV_GOING_DOWN:
352 	case NETDEV_DOWN:
353 		/* Find every socket on this device and kill it. */
354 		pppoe_flush_dev(dev);
355 		break;
356 
357 	default:
358 		break;
359 	}
360 
361 	return NOTIFY_DONE;
362 }
363 
364 static struct notifier_block pppoe_notifier = {
365 	.notifier_call = pppoe_device_event,
366 };
367 
368 /************************************************************************
369  *
370  * Do the real work of receiving a PPPoE Session frame.
371  *
372  ***********************************************************************/
373 static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
374 {
375 	struct pppox_sock *po = pppox_sk(sk);
376 	struct pppox_sock *relay_po;
377 
378 	/* Backlog receive. Semantics of backlog rcv preclude any code from
379 	 * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state
380 	 * can't change.
381 	 */
382 
383 	if (sk->sk_state & PPPOX_BOUND) {
384 		ppp_input(&po->chan, skb);
385 	} else if (sk->sk_state & PPPOX_RELAY) {
386 		relay_po = get_item_by_addr(sock_net(sk),
387 					    &po->pppoe_relay);
388 		if (relay_po == NULL)
389 			goto abort_kfree;
390 
391 		if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
392 			goto abort_put;
393 
394 		if (!__pppoe_xmit(sk_pppox(relay_po), skb))
395 			goto abort_put;
396 	} else {
397 		if (sock_queue_rcv_skb(sk, skb))
398 			goto abort_kfree;
399 	}
400 
401 	return NET_RX_SUCCESS;
402 
403 abort_put:
404 	sock_put(sk_pppox(relay_po));
405 
406 abort_kfree:
407 	kfree_skb(skb);
408 	return NET_RX_DROP;
409 }
410 
411 /************************************************************************
412  *
413  * Receive wrapper called in BH context.
414  *
415  ***********************************************************************/
416 static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev,
417 		     struct packet_type *pt, struct net_device *orig_dev)
418 {
419 	struct pppoe_hdr *ph;
420 	struct pppox_sock *po;
421 	struct pppoe_net *pn;
422 	int len;
423 
424 	skb = skb_share_check(skb, GFP_ATOMIC);
425 	if (!skb)
426 		goto out;
427 
428 	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
429 		goto drop;
430 
431 	ph = pppoe_hdr(skb);
432 	len = ntohs(ph->length);
433 
434 	skb_pull_rcsum(skb, sizeof(*ph));
435 	if (skb->len < len)
436 		goto drop;
437 
438 	if (pskb_trim_rcsum(skb, len))
439 		goto drop;
440 
441 	pn = pppoe_pernet(dev_net(dev));
442 
443 	/* Note that get_item does a sock_hold(), so sk_pppox(po)
444 	 * is known to be safe.
445 	 */
446 	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
447 	if (!po)
448 		goto drop;
449 
450 	return sk_receive_skb(sk_pppox(po), skb, 0);
451 
452 drop:
453 	kfree_skb(skb);
454 out:
455 	return NET_RX_DROP;
456 }
457 
458 /************************************************************************
459  *
460  * Receive a PPPoE Discovery frame.
461  * This is solely for detection of PADT frames
462  *
463  ***********************************************************************/
464 static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev,
465 			  struct packet_type *pt, struct net_device *orig_dev)
466 
467 {
468 	struct pppoe_hdr *ph;
469 	struct pppox_sock *po;
470 	struct pppoe_net *pn;
471 
472 	skb = skb_share_check(skb, GFP_ATOMIC);
473 	if (!skb)
474 		goto out;
475 
476 	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
477 		goto abort;
478 
479 	ph = pppoe_hdr(skb);
480 	if (ph->code != PADT_CODE)
481 		goto abort;
482 
483 	pn = pppoe_pernet(dev_net(dev));
484 	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
485 	if (po) {
486 		struct sock *sk = sk_pppox(po);
487 
488 		bh_lock_sock(sk);
489 
490 		/* If the user has locked the socket, just ignore
491 		 * the packet.  With the way two rcv protocols hook into
492 		 * one socket family type, we cannot (easily) distinguish
493 		 * what kind of SKB it is during backlog rcv.
494 		 */
495 		if (sock_owned_by_user(sk) == 0) {
496 			/* We're no longer connect at the PPPOE layer,
497 			 * and must wait for ppp channel to disconnect us.
498 			 */
499 			sk->sk_state = PPPOX_ZOMBIE;
500 		}
501 
502 		bh_unlock_sock(sk);
503 		sock_put(sk);
504 	}
505 
506 abort:
507 	kfree_skb(skb);
508 out:
509 	return NET_RX_SUCCESS; /* Lies... :-) */
510 }
511 
512 static struct packet_type pppoes_ptype __read_mostly = {
513 	.type	= cpu_to_be16(ETH_P_PPP_SES),
514 	.func	= pppoe_rcv,
515 };
516 
517 static struct packet_type pppoed_ptype __read_mostly = {
518 	.type	= cpu_to_be16(ETH_P_PPP_DISC),
519 	.func	= pppoe_disc_rcv,
520 };
521 
522 static struct proto pppoe_sk_proto __read_mostly = {
523 	.name	  = "PPPOE",
524 	.owner	  = THIS_MODULE,
525 	.obj_size = sizeof(struct pppox_sock),
526 };
527 
528 /***********************************************************************
529  *
530  * Initialize a new struct sock.
531  *
532  **********************************************************************/
533 static int pppoe_create(struct net *net, struct socket *sock)
534 {
535 	struct sock *sk;
536 
537 	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto);
538 	if (!sk)
539 		return -ENOMEM;
540 
541 	sock_init_data(sock, sk);
542 
543 	sock->state	= SS_UNCONNECTED;
544 	sock->ops	= &pppoe_ops;
545 
546 	sk->sk_backlog_rcv	= pppoe_rcv_core;
547 	sk->sk_state		= PPPOX_NONE;
548 	sk->sk_type		= SOCK_STREAM;
549 	sk->sk_family		= PF_PPPOX;
550 	sk->sk_protocol		= PX_PROTO_OE;
551 
552 	return 0;
553 }
554 
555 static int pppoe_release(struct socket *sock)
556 {
557 	struct sock *sk = sock->sk;
558 	struct pppox_sock *po;
559 	struct pppoe_net *pn;
560 	struct net *net = NULL;
561 
562 	if (!sk)
563 		return 0;
564 
565 	lock_sock(sk);
566 	if (sock_flag(sk, SOCK_DEAD)) {
567 		release_sock(sk);
568 		return -EBADF;
569 	}
570 
571 	po = pppox_sk(sk);
572 
573 	if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND | PPPOX_ZOMBIE)) {
574 		dev_put(po->pppoe_dev);
575 		po->pppoe_dev = NULL;
576 	}
577 
578 	pppox_unbind_sock(sk);
579 
580 	/* Signal the death of the socket. */
581 	sk->sk_state = PPPOX_DEAD;
582 
583 	net = sock_net(sk);
584 	pn = pppoe_pernet(net);
585 
586 	/*
587 	 * protect "po" from concurrent updates
588 	 * on pppoe_flush_dev
589 	 */
590 	delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
591 		    po->pppoe_ifindex);
592 
593 	sock_orphan(sk);
594 	sock->sk = NULL;
595 
596 	skb_queue_purge(&sk->sk_receive_queue);
597 	release_sock(sk);
598 	sock_put(sk);
599 
600 	return 0;
601 }
602 
603 static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
604 		  int sockaddr_len, int flags)
605 {
606 	struct sock *sk = sock->sk;
607 	struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr;
608 	struct pppox_sock *po = pppox_sk(sk);
609 	struct net_device *dev = NULL;
610 	struct pppoe_net *pn;
611 	struct net *net = NULL;
612 	int error;
613 
614 	lock_sock(sk);
615 
616 	error = -EINVAL;
617 	if (sp->sa_protocol != PX_PROTO_OE)
618 		goto end;
619 
620 	/* Check for already bound sockets */
621 	error = -EBUSY;
622 	if ((sk->sk_state & PPPOX_CONNECTED) &&
623 	     stage_session(sp->sa_addr.pppoe.sid))
624 		goto end;
625 
626 	/* Check for already disconnected sockets, on attempts to disconnect */
627 	error = -EALREADY;
628 	if ((sk->sk_state & PPPOX_DEAD) &&
629 	     !stage_session(sp->sa_addr.pppoe.sid))
630 		goto end;
631 
632 	error = 0;
633 
634 	/* Delete the old binding */
635 	if (stage_session(po->pppoe_pa.sid)) {
636 		pppox_unbind_sock(sk);
637 		pn = pppoe_pernet(sock_net(sk));
638 		delete_item(pn, po->pppoe_pa.sid,
639 			    po->pppoe_pa.remote, po->pppoe_ifindex);
640 		if (po->pppoe_dev) {
641 			dev_put(po->pppoe_dev);
642 			po->pppoe_dev = NULL;
643 		}
644 
645 		memset(sk_pppox(po) + 1, 0,
646 		       sizeof(struct pppox_sock) - sizeof(struct sock));
647 		sk->sk_state = PPPOX_NONE;
648 	}
649 
650 	/* Re-bind in session stage only */
651 	if (stage_session(sp->sa_addr.pppoe.sid)) {
652 		error = -ENODEV;
653 		net = sock_net(sk);
654 		dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev);
655 		if (!dev)
656 			goto err_put;
657 
658 		po->pppoe_dev = dev;
659 		po->pppoe_ifindex = dev->ifindex;
660 		pn = pppoe_pernet(net);
661 		if (!(dev->flags & IFF_UP)) {
662 			goto err_put;
663 		}
664 
665 		memcpy(&po->pppoe_pa,
666 		       &sp->sa_addr.pppoe,
667 		       sizeof(struct pppoe_addr));
668 
669 		write_lock_bh(&pn->hash_lock);
670 		error = __set_item(pn, po);
671 		write_unlock_bh(&pn->hash_lock);
672 		if (error < 0)
673 			goto err_put;
674 
675 		po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
676 				   dev->hard_header_len);
677 
678 		po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr);
679 		po->chan.private = sk;
680 		po->chan.ops = &pppoe_chan_ops;
681 
682 		error = ppp_register_net_channel(dev_net(dev), &po->chan);
683 		if (error) {
684 			delete_item(pn, po->pppoe_pa.sid,
685 				    po->pppoe_pa.remote, po->pppoe_ifindex);
686 			goto err_put;
687 		}
688 
689 		sk->sk_state = PPPOX_CONNECTED;
690 	}
691 
692 	po->num = sp->sa_addr.pppoe.sid;
693 
694 end:
695 	release_sock(sk);
696 	return error;
697 err_put:
698 	if (po->pppoe_dev) {
699 		dev_put(po->pppoe_dev);
700 		po->pppoe_dev = NULL;
701 	}
702 	goto end;
703 }
704 
705 static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
706 		  int *usockaddr_len, int peer)
707 {
708 	int len = sizeof(struct sockaddr_pppox);
709 	struct sockaddr_pppox sp;
710 
711 	sp.sa_family	= AF_PPPOX;
712 	sp.sa_protocol	= PX_PROTO_OE;
713 	memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
714 	       sizeof(struct pppoe_addr));
715 
716 	memcpy(uaddr, &sp, len);
717 
718 	*usockaddr_len = len;
719 
720 	return 0;
721 }
722 
723 static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
724 		unsigned long arg)
725 {
726 	struct sock *sk = sock->sk;
727 	struct pppox_sock *po = pppox_sk(sk);
728 	int val;
729 	int err;
730 
731 	switch (cmd) {
732 	case PPPIOCGMRU:
733 		err = -ENXIO;
734 		if (!(sk->sk_state & PPPOX_CONNECTED))
735 			break;
736 
737 		err = -EFAULT;
738 		if (put_user(po->pppoe_dev->mtu -
739 			     sizeof(struct pppoe_hdr) -
740 			     PPP_HDRLEN,
741 			     (int __user *)arg))
742 			break;
743 		err = 0;
744 		break;
745 
746 	case PPPIOCSMRU:
747 		err = -ENXIO;
748 		if (!(sk->sk_state & PPPOX_CONNECTED))
749 			break;
750 
751 		err = -EFAULT;
752 		if (get_user(val, (int __user *)arg))
753 			break;
754 
755 		if (val < (po->pppoe_dev->mtu
756 			   - sizeof(struct pppoe_hdr)
757 			   - PPP_HDRLEN))
758 			err = 0;
759 		else
760 			err = -EINVAL;
761 		break;
762 
763 	case PPPIOCSFLAGS:
764 		err = -EFAULT;
765 		if (get_user(val, (int __user *)arg))
766 			break;
767 		err = 0;
768 		break;
769 
770 	case PPPOEIOCSFWD:
771 	{
772 		struct pppox_sock *relay_po;
773 
774 		err = -EBUSY;
775 		if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
776 			break;
777 
778 		err = -ENOTCONN;
779 		if (!(sk->sk_state & PPPOX_CONNECTED))
780 			break;
781 
782 		/* PPPoE address from the user specifies an outbound
783 		   PPPoE address which frames are forwarded to */
784 		err = -EFAULT;
785 		if (copy_from_user(&po->pppoe_relay,
786 				   (void __user *)arg,
787 				   sizeof(struct sockaddr_pppox)))
788 			break;
789 
790 		err = -EINVAL;
791 		if (po->pppoe_relay.sa_family != AF_PPPOX ||
792 		    po->pppoe_relay.sa_protocol != PX_PROTO_OE)
793 			break;
794 
795 		/* Check that the socket referenced by the address
796 		   actually exists. */
797 		relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
798 		if (!relay_po)
799 			break;
800 
801 		sock_put(sk_pppox(relay_po));
802 		sk->sk_state |= PPPOX_RELAY;
803 		err = 0;
804 		break;
805 	}
806 
807 	case PPPOEIOCDFWD:
808 		err = -EALREADY;
809 		if (!(sk->sk_state & PPPOX_RELAY))
810 			break;
811 
812 		sk->sk_state &= ~PPPOX_RELAY;
813 		err = 0;
814 		break;
815 
816 	default:
817 		err = -ENOTTY;
818 	}
819 
820 	return err;
821 }
822 
823 static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
824 		  struct msghdr *m, size_t total_len)
825 {
826 	struct sk_buff *skb;
827 	struct sock *sk = sock->sk;
828 	struct pppox_sock *po = pppox_sk(sk);
829 	int error;
830 	struct pppoe_hdr hdr;
831 	struct pppoe_hdr *ph;
832 	struct net_device *dev;
833 	char *start;
834 
835 	lock_sock(sk);
836 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
837 		error = -ENOTCONN;
838 		goto end;
839 	}
840 
841 	hdr.ver = 1;
842 	hdr.type = 1;
843 	hdr.code = 0;
844 	hdr.sid = po->num;
845 
846 	dev = po->pppoe_dev;
847 
848 	error = -EMSGSIZE;
849 	if (total_len > (dev->mtu + dev->hard_header_len))
850 		goto end;
851 
852 
853 	skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
854 			   0, GFP_KERNEL);
855 	if (!skb) {
856 		error = -ENOMEM;
857 		goto end;
858 	}
859 
860 	/* Reserve space for headers. */
861 	skb_reserve(skb, dev->hard_header_len);
862 	skb_reset_network_header(skb);
863 
864 	skb->dev = dev;
865 
866 	skb->priority = sk->sk_priority;
867 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
868 
869 	ph = (struct pppoe_hdr *)skb_put(skb, total_len + sizeof(struct pppoe_hdr));
870 	start = (char *)&ph->tag[0];
871 
872 	error = memcpy_fromiovec(start, m->msg_iov, total_len);
873 	if (error < 0) {
874 		kfree_skb(skb);
875 		goto end;
876 	}
877 
878 	error = total_len;
879 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
880 			po->pppoe_pa.remote, NULL, total_len);
881 
882 	memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
883 
884 	ph->length = htons(total_len);
885 
886 	dev_queue_xmit(skb);
887 
888 end:
889 	release_sock(sk);
890 	return error;
891 }
892 
893 /************************************************************************
894  *
895  * xmit function for internal use.
896  *
897  ***********************************************************************/
898 static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
899 {
900 	struct pppox_sock *po = pppox_sk(sk);
901 	struct net_device *dev = po->pppoe_dev;
902 	struct pppoe_hdr *ph;
903 	int data_len = skb->len;
904 
905 	/* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
906 	 * xmit operations conclude prior to an unregistration call.  Thus
907 	 * sk->sk_state cannot change, so we don't need to do lock_sock().
908 	 * But, we also can't do a lock_sock since that introduces a potential
909 	 * deadlock as we'd reverse the lock ordering used when calling
910 	 * ppp_unregister_channel().
911 	 */
912 
913 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
914 		goto abort;
915 
916 	if (!dev)
917 		goto abort;
918 
919 	/* Copy the data if there is no space for the header or if it's
920 	 * read-only.
921 	 */
922 	if (skb_cow_head(skb, sizeof(*ph) + dev->hard_header_len))
923 		goto abort;
924 
925 	__skb_push(skb, sizeof(*ph));
926 	skb_reset_network_header(skb);
927 
928 	ph = pppoe_hdr(skb);
929 	ph->ver	= 1;
930 	ph->type = 1;
931 	ph->code = 0;
932 	ph->sid	= po->num;
933 	ph->length = htons(data_len);
934 
935 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
936 	skb->dev = dev;
937 
938 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
939 			po->pppoe_pa.remote, NULL, data_len);
940 
941 	dev_queue_xmit(skb);
942 	return 1;
943 
944 abort:
945 	kfree_skb(skb);
946 	return 1;
947 }
948 
949 /************************************************************************
950  *
951  * xmit function called by generic PPP driver
952  * sends PPP frame over PPPoE socket
953  *
954  ***********************************************************************/
955 static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
956 {
957 	struct sock *sk = (struct sock *)chan->private;
958 	return __pppoe_xmit(sk, skb);
959 }
960 
961 static const struct ppp_channel_ops pppoe_chan_ops = {
962 	.start_xmit = pppoe_xmit,
963 };
964 
965 static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
966 		  struct msghdr *m, size_t total_len, int flags)
967 {
968 	struct sock *sk = sock->sk;
969 	struct sk_buff *skb;
970 	int error = 0;
971 
972 	if (sk->sk_state & PPPOX_BOUND) {
973 		error = -EIO;
974 		goto end;
975 	}
976 
977 	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
978 				flags & MSG_DONTWAIT, &error);
979 	if (error < 0)
980 		goto end;
981 
982 	if (skb) {
983 		total_len = min_t(size_t, total_len, skb->len);
984 		error = skb_copy_datagram_iovec(skb, 0, m->msg_iov, total_len);
985 		if (error == 0) {
986 			consume_skb(skb);
987 			return total_len;
988 		}
989 	}
990 
991 	kfree_skb(skb);
992 end:
993 	return error;
994 }
995 
996 #ifdef CONFIG_PROC_FS
997 static int pppoe_seq_show(struct seq_file *seq, void *v)
998 {
999 	struct pppox_sock *po;
1000 	char *dev_name;
1001 
1002 	if (v == SEQ_START_TOKEN) {
1003 		seq_puts(seq, "Id       Address              Device\n");
1004 		goto out;
1005 	}
1006 
1007 	po = v;
1008 	dev_name = po->pppoe_pa.dev;
1009 
1010 	seq_printf(seq, "%08X %pM %8s\n",
1011 		po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
1012 out:
1013 	return 0;
1014 }
1015 
1016 static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
1017 {
1018 	struct pppox_sock *po;
1019 	int i;
1020 
1021 	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
1022 		po = pn->hash_table[i];
1023 		while (po) {
1024 			if (!pos--)
1025 				goto out;
1026 			po = po->next;
1027 		}
1028 	}
1029 
1030 out:
1031 	return po;
1032 }
1033 
1034 static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
1035 	__acquires(pn->hash_lock)
1036 {
1037 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1038 	loff_t l = *pos;
1039 
1040 	read_lock_bh(&pn->hash_lock);
1041 	return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
1042 }
1043 
1044 static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1045 {
1046 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1047 	struct pppox_sock *po;
1048 
1049 	++*pos;
1050 	if (v == SEQ_START_TOKEN) {
1051 		po = pppoe_get_idx(pn, 0);
1052 		goto out;
1053 	}
1054 	po = v;
1055 	if (po->next)
1056 		po = po->next;
1057 	else {
1058 		int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1059 
1060 		po = NULL;
1061 		while (++hash < PPPOE_HASH_SIZE) {
1062 			po = pn->hash_table[hash];
1063 			if (po)
1064 				break;
1065 		}
1066 	}
1067 
1068 out:
1069 	return po;
1070 }
1071 
1072 static void pppoe_seq_stop(struct seq_file *seq, void *v)
1073 	__releases(pn->hash_lock)
1074 {
1075 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1076 	read_unlock_bh(&pn->hash_lock);
1077 }
1078 
1079 static const struct seq_operations pppoe_seq_ops = {
1080 	.start		= pppoe_seq_start,
1081 	.next		= pppoe_seq_next,
1082 	.stop		= pppoe_seq_stop,
1083 	.show		= pppoe_seq_show,
1084 };
1085 
1086 static int pppoe_seq_open(struct inode *inode, struct file *file)
1087 {
1088 	return seq_open_net(inode, file, &pppoe_seq_ops,
1089 			sizeof(struct seq_net_private));
1090 }
1091 
1092 static const struct file_operations pppoe_seq_fops = {
1093 	.owner		= THIS_MODULE,
1094 	.open		= pppoe_seq_open,
1095 	.read		= seq_read,
1096 	.llseek		= seq_lseek,
1097 	.release	= seq_release_net,
1098 };
1099 
1100 #endif /* CONFIG_PROC_FS */
1101 
1102 static const struct proto_ops pppoe_ops = {
1103 	.family		= AF_PPPOX,
1104 	.owner		= THIS_MODULE,
1105 	.release	= pppoe_release,
1106 	.bind		= sock_no_bind,
1107 	.connect	= pppoe_connect,
1108 	.socketpair	= sock_no_socketpair,
1109 	.accept		= sock_no_accept,
1110 	.getname	= pppoe_getname,
1111 	.poll		= datagram_poll,
1112 	.listen		= sock_no_listen,
1113 	.shutdown	= sock_no_shutdown,
1114 	.setsockopt	= sock_no_setsockopt,
1115 	.getsockopt	= sock_no_getsockopt,
1116 	.sendmsg	= pppoe_sendmsg,
1117 	.recvmsg	= pppoe_recvmsg,
1118 	.mmap		= sock_no_mmap,
1119 	.ioctl		= pppox_ioctl,
1120 };
1121 
1122 static const struct pppox_proto pppoe_proto = {
1123 	.create	= pppoe_create,
1124 	.ioctl	= pppoe_ioctl,
1125 	.owner	= THIS_MODULE,
1126 };
1127 
1128 static __net_init int pppoe_init_net(struct net *net)
1129 {
1130 	struct pppoe_net *pn = pppoe_pernet(net);
1131 	struct proc_dir_entry *pde;
1132 
1133 	rwlock_init(&pn->hash_lock);
1134 
1135 	pde = proc_create("pppoe", S_IRUGO, net->proc_net, &pppoe_seq_fops);
1136 #ifdef CONFIG_PROC_FS
1137 	if (!pde)
1138 		return -ENOMEM;
1139 #endif
1140 
1141 	return 0;
1142 }
1143 
1144 static __net_exit void pppoe_exit_net(struct net *net)
1145 {
1146 	remove_proc_entry("pppoe", net->proc_net);
1147 }
1148 
1149 static struct pernet_operations pppoe_net_ops = {
1150 	.init = pppoe_init_net,
1151 	.exit = pppoe_exit_net,
1152 	.id   = &pppoe_net_id,
1153 	.size = sizeof(struct pppoe_net),
1154 };
1155 
1156 static int __init pppoe_init(void)
1157 {
1158 	int err;
1159 
1160 	err = register_pernet_device(&pppoe_net_ops);
1161 	if (err)
1162 		goto out;
1163 
1164 	err = proto_register(&pppoe_sk_proto, 0);
1165 	if (err)
1166 		goto out_unregister_net_ops;
1167 
1168 	err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1169 	if (err)
1170 		goto out_unregister_pppoe_proto;
1171 
1172 	dev_add_pack(&pppoes_ptype);
1173 	dev_add_pack(&pppoed_ptype);
1174 	register_netdevice_notifier(&pppoe_notifier);
1175 
1176 	return 0;
1177 
1178 out_unregister_pppoe_proto:
1179 	proto_unregister(&pppoe_sk_proto);
1180 out_unregister_net_ops:
1181 	unregister_pernet_device(&pppoe_net_ops);
1182 out:
1183 	return err;
1184 }
1185 
1186 static void __exit pppoe_exit(void)
1187 {
1188 	unregister_netdevice_notifier(&pppoe_notifier);
1189 	dev_remove_pack(&pppoed_ptype);
1190 	dev_remove_pack(&pppoes_ptype);
1191 	unregister_pppox_proto(PX_PROTO_OE);
1192 	proto_unregister(&pppoe_sk_proto);
1193 	unregister_pernet_device(&pppoe_net_ops);
1194 }
1195 
1196 module_init(pppoe_init);
1197 module_exit(pppoe_exit);
1198 
1199 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1200 MODULE_DESCRIPTION("PPP over Ethernet driver");
1201 MODULE_LICENSE("GPL");
1202 MODULE_ALIAS_NETPROTO(PF_PPPOX);
1203