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