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