xref: /linux/drivers/net/ppp/pppoe.c (revision 5d7f3375cd574fc83f0cd30839b21455c0e79ae2)
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 
675 		error = ppp_register_net_channel(dev_net(dev), &po->chan);
676 		if (error) {
677 			delete_item(pn, po);
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 	ph = pppoe_hdr(skb);
828 	memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
829 
830 	ph->length = htons(total_len);
831 
832 	dev_queue_xmit(skb);
833 
834 end:
835 	release_sock(sk);
836 	return error;
837 }
838 
839 /************************************************************************
840  *
841  * xmit function called by generic PPP driver
842  * sends PPP frame over PPPoE socket
843  *
844  ***********************************************************************/
845 static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
846 {
847 	struct sock *sk = chan->private;
848 	struct pppox_sock *po = pppox_sk(sk);
849 	struct net_device *dev = po->pppoe_dev;
850 	struct pppoe_hdr *ph;
851 	int data_len = skb->len;
852 
853 	/* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
854 	 * xmit operations conclude prior to an unregistration call.  Thus
855 	 * sk->sk_state cannot change, so we don't need to do lock_sock().
856 	 * But, we also can't do a lock_sock since that introduces a potential
857 	 * deadlock as we'd reverse the lock ordering used when calling
858 	 * ppp_unregister_channel().
859 	 */
860 
861 	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
862 		goto abort;
863 
864 	if (!dev)
865 		goto abort;
866 
867 	/* Copy the data if there is no space for the header or if it's
868 	 * read-only.
869 	 */
870 	if (skb_cow_head(skb, LL_RESERVED_SPACE(dev) + sizeof(*ph)))
871 		goto abort;
872 
873 	__skb_push(skb, sizeof(*ph));
874 	skb_reset_network_header(skb);
875 
876 	ph = pppoe_hdr(skb);
877 	ph->ver	= 1;
878 	ph->type = 1;
879 	ph->code = 0;
880 	ph->sid	= po->num;
881 	ph->length = htons(data_len);
882 
883 	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
884 	skb->dev = dev;
885 
886 	dev_hard_header(skb, dev, ETH_P_PPP_SES,
887 			po->pppoe_pa.remote, NULL, data_len);
888 
889 	dev_queue_xmit(skb);
890 	return 1;
891 
892 abort:
893 	kfree_skb(skb);
894 	return 1;
895 }
896 
897 static int pppoe_fill_forward_path(struct net_device_path_ctx *ctx,
898 				   struct net_device_path *path,
899 				   const struct ppp_channel *chan)
900 {
901 	struct sock *sk = chan->private;
902 	struct pppox_sock *po = pppox_sk(sk);
903 	struct net_device *dev = po->pppoe_dev;
904 
905 	if (sock_flag(sk, SOCK_DEAD) ||
906 	    !(sk->sk_state & PPPOX_CONNECTED) || !dev)
907 		return -1;
908 
909 	path->type = DEV_PATH_PPPOE;
910 	path->encap.proto = htons(ETH_P_PPP_SES);
911 	path->encap.id = be16_to_cpu(po->num);
912 	memcpy(path->encap.h_dest, po->pppoe_pa.remote, ETH_ALEN);
913 	memcpy(ctx->daddr, po->pppoe_pa.remote, ETH_ALEN);
914 	path->dev = ctx->dev;
915 	ctx->dev = dev;
916 
917 	return 0;
918 }
919 
920 static const struct ppp_channel_ops pppoe_chan_ops = {
921 	.start_xmit = pppoe_xmit,
922 	.fill_forward_path = pppoe_fill_forward_path,
923 };
924 
925 static int pppoe_recvmsg(struct socket *sock, struct msghdr *m,
926 			 size_t total_len, int flags)
927 {
928 	struct sock *sk = sock->sk;
929 	struct sk_buff *skb;
930 	int error = 0;
931 
932 	if (sk->sk_state & PPPOX_BOUND)
933 		return -EIO;
934 
935 	skb = skb_recv_datagram(sk, flags, &error);
936 	if (!skb)
937 		return error;
938 
939 	total_len = min_t(size_t, total_len, skb->len);
940 	error = skb_copy_datagram_msg(skb, 0, m, total_len);
941 	if (error == 0) {
942 		consume_skb(skb);
943 		return total_len;
944 	}
945 
946 	kfree_skb(skb);
947 	return error;
948 }
949 
950 #ifdef CONFIG_PROC_FS
951 static int pppoe_seq_show(struct seq_file *seq, void *v)
952 {
953 	struct pppox_sock *po;
954 	char *dev_name;
955 
956 	if (v == SEQ_START_TOKEN) {
957 		seq_puts(seq, "Id       Address              Device\n");
958 		goto out;
959 	}
960 
961 	po = v;
962 	dev_name = po->pppoe_pa.dev;
963 
964 	seq_printf(seq, "%08X %pM %8s\n",
965 		po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
966 out:
967 	return 0;
968 }
969 
970 static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
971 {
972 	struct pppox_sock *po;
973 	int i;
974 
975 	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
976 		po = rcu_dereference(pn->hash_table[i]);
977 		while (po) {
978 			if (!pos--)
979 				goto out;
980 			po = rcu_dereference(po->next);
981 		}
982 	}
983 
984 out:
985 	return po;
986 }
987 
988 static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
989 	__acquires(RCU)
990 {
991 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
992 	loff_t l = *pos;
993 
994 	rcu_read_lock();
995 	return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
996 }
997 
998 static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
999 {
1000 	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1001 	struct pppox_sock *po, *next;
1002 
1003 	++*pos;
1004 	if (v == SEQ_START_TOKEN) {
1005 		po = pppoe_get_idx(pn, 0);
1006 		goto out;
1007 	}
1008 	po = v;
1009 	next = rcu_dereference(po->next);
1010 	if (next)
1011 		po = next;
1012 	else {
1013 		int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1014 
1015 		po = NULL;
1016 		while (++hash < PPPOE_HASH_SIZE) {
1017 			po = rcu_dereference(pn->hash_table[hash]);
1018 			if (po)
1019 				break;
1020 		}
1021 	}
1022 
1023 out:
1024 	return po;
1025 }
1026 
1027 static void pppoe_seq_stop(struct seq_file *seq, void *v)
1028 	__releases(RCU)
1029 {
1030 	rcu_read_unlock();
1031 }
1032 
1033 static const struct seq_operations pppoe_seq_ops = {
1034 	.start		= pppoe_seq_start,
1035 	.next		= pppoe_seq_next,
1036 	.stop		= pppoe_seq_stop,
1037 	.show		= pppoe_seq_show,
1038 };
1039 #endif /* CONFIG_PROC_FS */
1040 
1041 static const struct proto_ops pppoe_ops = {
1042 	.family		= AF_PPPOX,
1043 	.owner		= THIS_MODULE,
1044 	.release	= pppoe_release,
1045 	.bind		= sock_no_bind,
1046 	.connect	= pppoe_connect,
1047 	.socketpair	= sock_no_socketpair,
1048 	.accept		= sock_no_accept,
1049 	.getname	= pppoe_getname,
1050 	.poll		= datagram_poll,
1051 	.listen		= sock_no_listen,
1052 	.shutdown	= sock_no_shutdown,
1053 	.sendmsg	= pppoe_sendmsg,
1054 	.recvmsg	= pppoe_recvmsg,
1055 	.mmap		= sock_no_mmap,
1056 	.ioctl		= pppox_ioctl,
1057 #ifdef CONFIG_COMPAT
1058 	.compat_ioctl	= pppox_compat_ioctl,
1059 #endif
1060 };
1061 
1062 static const struct pppox_proto pppoe_proto = {
1063 	.create	= pppoe_create,
1064 	.ioctl	= pppoe_ioctl,
1065 	.owner	= THIS_MODULE,
1066 };
1067 
1068 static __net_init int pppoe_init_net(struct net *net)
1069 {
1070 	struct pppoe_net *pn = pppoe_pernet(net);
1071 	struct proc_dir_entry *pde;
1072 
1073 	spin_lock_init(&pn->hash_lock);
1074 
1075 	pde = proc_create_net("pppoe", 0444, net->proc_net,
1076 			&pppoe_seq_ops, sizeof(struct seq_net_private));
1077 #ifdef CONFIG_PROC_FS
1078 	if (!pde)
1079 		return -ENOMEM;
1080 #endif
1081 
1082 	return 0;
1083 }
1084 
1085 static __net_exit void pppoe_exit_net(struct net *net)
1086 {
1087 	remove_proc_entry("pppoe", net->proc_net);
1088 }
1089 
1090 static struct pernet_operations pppoe_net_ops = {
1091 	.init = pppoe_init_net,
1092 	.exit = pppoe_exit_net,
1093 	.id   = &pppoe_net_id,
1094 	.size = sizeof(struct pppoe_net),
1095 };
1096 
1097 static u16
1098 compare_pppoe_header(const struct pppoe_hdr *phdr,
1099 		     const struct pppoe_hdr *phdr2)
1100 {
1101 	__be16 proto = *(const __be16 *)(phdr + 1);
1102 	__be16 proto2 = *(const __be16 *)(phdr2 + 1);
1103 
1104 	return (__force u16)((phdr->sid ^ phdr2->sid) | (proto ^ proto2));
1105 }
1106 
1107 static __be16 pppoe_hdr_proto(const struct pppoe_hdr *phdr)
1108 {
1109 	__be16 proto = *(const __be16 *)(phdr + 1);
1110 
1111 	switch (proto) {
1112 	case cpu_to_be16(PPP_IP):
1113 		return cpu_to_be16(ETH_P_IP);
1114 #if IS_ENABLED(CONFIG_IPV6)
1115 	case cpu_to_be16(PPP_IPV6):
1116 		return cpu_to_be16(ETH_P_IPV6);
1117 #endif
1118 	default:
1119 		return 0;
1120 	}
1121 }
1122 
1123 static struct sk_buff *pppoe_gro_receive(struct list_head *head,
1124 					 struct sk_buff *skb)
1125 {
1126 	const struct packet_offload *ptype;
1127 	unsigned int hlen, off_pppoe;
1128 	const struct pppoe_hdr *phdr;
1129 	struct sk_buff *pp = NULL;
1130 	struct sk_buff *p;
1131 	int flush = 1;
1132 	__be16 type;
1133 
1134 	off_pppoe = skb_gro_offset(skb);
1135 	hlen = off_pppoe + PPPOE_SES_HLEN;
1136 	phdr = skb_gro_header(skb, hlen, off_pppoe);
1137 	if (unlikely(!phdr))
1138 		goto out;
1139 
1140 	/* filter for session packets (type:1, ver:1, code:0) */
1141 	if (*(const __be16 *)phdr != cpu_to_be16(0x1100))
1142 		goto out;
1143 
1144 	/* ignore packets with padding or invalid length */
1145 	if (skb_gro_len(skb) != be16_to_cpu(phdr->length) + sizeof(*phdr))
1146 		goto out;
1147 
1148 	type = pppoe_hdr_proto(phdr);
1149 	ptype = gro_find_receive_by_type(type);
1150 	if (!ptype)
1151 		goto out;
1152 
1153 	flush = 0;
1154 
1155 	list_for_each_entry(p, head, list) {
1156 		const struct pppoe_hdr *phdr2;
1157 
1158 		if (!NAPI_GRO_CB(p)->same_flow)
1159 			continue;
1160 
1161 		phdr2 = (const struct pppoe_hdr *)(p->data + off_pppoe);
1162 		if (compare_pppoe_header(phdr, phdr2))
1163 			NAPI_GRO_CB(p)->same_flow = 0;
1164 	}
1165 
1166 	skb_gro_pull(skb, PPPOE_SES_HLEN);
1167 	skb_gro_postpull_rcsum(skb, phdr, PPPOE_SES_HLEN);
1168 
1169 	pp = indirect_call_gro_receive_inet(ptype->callbacks.gro_receive,
1170 					    ipv6_gro_receive, inet_gro_receive,
1171 					    head, skb);
1172 
1173 out:
1174 	skb_gro_flush_final(skb, pp, flush);
1175 
1176 	return pp;
1177 }
1178 
1179 static int pppoe_gro_complete(struct sk_buff *skb, int nhoff)
1180 {
1181 	struct pppoe_hdr *phdr = (struct pppoe_hdr *)(skb->data + nhoff);
1182 	__be16 type = pppoe_hdr_proto(phdr);
1183 	struct packet_offload *ptype;
1184 	unsigned int len;
1185 
1186 	ptype = gro_find_complete_by_type(type);
1187 	if (!ptype)
1188 		return -ENOENT;
1189 
1190 	len = skb->len - (nhoff + sizeof(*phdr));
1191 	len = min(len, 0xFFFFU);
1192 	phdr->length = cpu_to_be16(len);
1193 
1194 	return INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
1195 				  ipv6_gro_complete, inet_gro_complete,
1196 				  skb, nhoff + PPPOE_SES_HLEN);
1197 }
1198 
1199 static struct sk_buff *pppoe_gso_segment(struct sk_buff *skb,
1200 					 netdev_features_t features)
1201 {
1202 	struct sk_buff *segs = ERR_PTR(-EINVAL);
1203 	struct packet_offload *ptype;
1204 	struct pppoe_hdr *phdr;
1205 	__be16 orig_type, type;
1206 	int len, nhoff;
1207 
1208 	skb_reset_network_header(skb);
1209 	nhoff = skb_network_header(skb) - skb_mac_header(skb);
1210 
1211 	if (unlikely(!pskb_may_pull(skb, PPPOE_SES_HLEN)))
1212 		goto out;
1213 
1214 	phdr = (struct pppoe_hdr *)skb_network_header(skb);
1215 	type = pppoe_hdr_proto(phdr);
1216 	ptype = gro_find_complete_by_type(type);
1217 	if (!ptype)
1218 		goto out;
1219 
1220 	orig_type = skb->protocol;
1221 	__skb_pull(skb, PPPOE_SES_HLEN);
1222 	features &= ~NETIF_F_GSO_SOFTWARE;
1223 	segs = ptype->callbacks.gso_segment(skb, features);
1224 	if (IS_ERR_OR_NULL(segs))
1225 		goto out;
1226 
1227 	skb = segs;
1228 	do {
1229 		phdr = (struct pppoe_hdr *)(skb_mac_header(skb) + nhoff);
1230 		len = skb->len - (nhoff + sizeof(*phdr));
1231 		phdr->length = cpu_to_be16(len);
1232 		skb->network_header = (u8 *)phdr - skb->head;
1233 		skb->protocol = orig_type;
1234 		skb_reset_mac_len(skb);
1235 	} while ((skb = skb->next));
1236 
1237 out:
1238 	return segs;
1239 }
1240 
1241 static struct packet_offload pppoe_packet_offload __read_mostly = {
1242 	.type = cpu_to_be16(ETH_P_PPP_SES),
1243 	.priority = 20,
1244 	.callbacks = {
1245 		.gro_receive = pppoe_gro_receive,
1246 		.gro_complete = pppoe_gro_complete,
1247 		.gso_segment = pppoe_gso_segment,
1248 	},
1249 };
1250 
1251 static int __init pppoe_init(void)
1252 {
1253 	int err;
1254 
1255 	err = register_pernet_device(&pppoe_net_ops);
1256 	if (err)
1257 		goto out;
1258 
1259 	err = proto_register(&pppoe_sk_proto, 0);
1260 	if (err)
1261 		goto out_unregister_net_ops;
1262 
1263 	err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1264 	if (err)
1265 		goto out_unregister_pppoe_proto;
1266 
1267 	if (IS_ENABLED(CONFIG_INET))
1268 		dev_add_offload(&pppoe_packet_offload);
1269 	dev_add_pack(&pppoes_ptype);
1270 	dev_add_pack(&pppoed_ptype);
1271 	register_netdevice_notifier(&pppoe_notifier);
1272 
1273 	return 0;
1274 
1275 out_unregister_pppoe_proto:
1276 	proto_unregister(&pppoe_sk_proto);
1277 out_unregister_net_ops:
1278 	unregister_pernet_device(&pppoe_net_ops);
1279 out:
1280 	return err;
1281 }
1282 
1283 static void __exit pppoe_exit(void)
1284 {
1285 	unregister_netdevice_notifier(&pppoe_notifier);
1286 	dev_remove_pack(&pppoed_ptype);
1287 	dev_remove_pack(&pppoes_ptype);
1288 	if (IS_ENABLED(CONFIG_INET))
1289 		dev_remove_offload(&pppoe_packet_offload);
1290 	unregister_pppox_proto(PX_PROTO_OE);
1291 	proto_unregister(&pppoe_sk_proto);
1292 	unregister_pernet_device(&pppoe_net_ops);
1293 }
1294 
1295 module_init(pppoe_init);
1296 module_exit(pppoe_exit);
1297 
1298 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1299 MODULE_DESCRIPTION("PPP over Ethernet driver");
1300 MODULE_LICENSE("GPL");
1301 MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OE);
1302