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