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