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