1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PF_INET6 socket protocol family 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 * 9 * Adapted from linux/net/ipv4/af_inet.c 10 * 11 * Fixes: 12 * piggy, Karl Knutson : Socket protocol table 13 * Hideaki YOSHIFUJI : sin6_scope_id support 14 * Arnaldo Melo : check proc_net_create return, cleanups 15 */ 16 17 #define pr_fmt(fmt) "IPv6: " fmt 18 19 #include <linux/module.h> 20 #include <linux/capability.h> 21 #include <linux/errno.h> 22 #include <linux/types.h> 23 #include <linux/socket.h> 24 #include <linux/in.h> 25 #include <linux/kernel.h> 26 #include <linux/timer.h> 27 #include <linux/string.h> 28 #include <linux/sockios.h> 29 #include <linux/net.h> 30 #include <linux/fcntl.h> 31 #include <linux/mm.h> 32 #include <linux/interrupt.h> 33 #include <linux/proc_fs.h> 34 #include <linux/stat.h> 35 #include <linux/init.h> 36 #include <linux/slab.h> 37 38 #include <linux/inet.h> 39 #include <linux/netdevice.h> 40 #include <linux/icmpv6.h> 41 #include <linux/netfilter_ipv6.h> 42 43 #include <net/ip.h> 44 #include <net/ipv6.h> 45 #include <net/udp.h> 46 #include <net/udplite.h> 47 #include <net/tcp.h> 48 #include <net/ping.h> 49 #include <net/protocol.h> 50 #include <net/inet_common.h> 51 #include <net/route.h> 52 #include <net/transp_v6.h> 53 #include <net/ip6_route.h> 54 #include <net/addrconf.h> 55 #include <net/ipv6_stubs.h> 56 #include <net/ndisc.h> 57 #ifdef CONFIG_IPV6_TUNNEL 58 #include <net/ip6_tunnel.h> 59 #endif 60 #include <net/calipso.h> 61 #include <net/seg6.h> 62 #include <net/rpl.h> 63 #include <net/compat.h> 64 #include <net/xfrm.h> 65 #include <net/ioam6.h> 66 #include <net/rawv6.h> 67 68 #include <linux/uaccess.h> 69 #include <linux/mroute6.h> 70 71 #include "ip6_offload.h" 72 73 MODULE_AUTHOR("Cast of dozens"); 74 MODULE_DESCRIPTION("IPv6 protocol stack for Linux"); 75 MODULE_LICENSE("GPL"); 76 77 /* The inetsw6 table contains everything that inet6_create needs to 78 * build a new socket. 79 */ 80 static struct list_head inetsw6[SOCK_MAX]; 81 static DEFINE_SPINLOCK(inetsw6_lock); 82 83 struct ipv6_params ipv6_defaults = { 84 .disable_ipv6 = 0, 85 .autoconf = 1, 86 }; 87 88 static int disable_ipv6_mod; 89 90 module_param_named(disable, disable_ipv6_mod, int, 0444); 91 MODULE_PARM_DESC(disable, "Disable IPv6 module such that it is non-functional"); 92 93 module_param_named(disable_ipv6, ipv6_defaults.disable_ipv6, int, 0444); 94 MODULE_PARM_DESC(disable_ipv6, "Disable IPv6 on all interfaces"); 95 96 module_param_named(autoconf, ipv6_defaults.autoconf, int, 0444); 97 MODULE_PARM_DESC(autoconf, "Enable IPv6 address autoconfiguration on all interfaces"); 98 99 bool ipv6_mod_enabled(void) 100 { 101 return disable_ipv6_mod == 0; 102 } 103 EXPORT_SYMBOL_GPL(ipv6_mod_enabled); 104 105 static struct ipv6_pinfo *inet6_sk_generic(struct sock *sk) 106 { 107 const int offset = sk->sk_prot->ipv6_pinfo_offset; 108 109 return (struct ipv6_pinfo *)(((u8 *)sk) + offset); 110 } 111 112 void inet6_sock_destruct(struct sock *sk) 113 { 114 inet6_cleanup_sock(sk); 115 inet_sock_destruct(sk); 116 } 117 EXPORT_SYMBOL_GPL(inet6_sock_destruct); 118 119 static int inet6_create(struct net *net, struct socket *sock, int protocol, 120 int kern) 121 { 122 struct inet_sock *inet; 123 struct ipv6_pinfo *np; 124 struct sock *sk; 125 struct inet_protosw *answer; 126 struct proto *answer_prot; 127 unsigned char answer_flags; 128 int try_loading_module = 0; 129 int err; 130 131 if (protocol < 0 || protocol >= IPPROTO_MAX) 132 return -EINVAL; 133 134 /* Look for the requested type/protocol pair. */ 135 lookup_protocol: 136 err = -ESOCKTNOSUPPORT; 137 rcu_read_lock(); 138 list_for_each_entry_rcu(answer, &inetsw6[sock->type], list) { 139 140 err = 0; 141 /* Check the non-wild match. */ 142 if (protocol == answer->protocol) { 143 if (protocol != IPPROTO_IP) 144 break; 145 } else { 146 /* Check for the two wild cases. */ 147 if (IPPROTO_IP == protocol) { 148 protocol = answer->protocol; 149 break; 150 } 151 if (IPPROTO_IP == answer->protocol) 152 break; 153 } 154 err = -EPROTONOSUPPORT; 155 } 156 157 if (err) { 158 if (try_loading_module < 2) { 159 rcu_read_unlock(); 160 /* 161 * Be more specific, e.g. net-pf-10-proto-132-type-1 162 * (net-pf-PF_INET6-proto-IPPROTO_SCTP-type-SOCK_STREAM) 163 */ 164 if (++try_loading_module == 1) 165 request_module("net-pf-%d-proto-%d-type-%d", 166 PF_INET6, protocol, sock->type); 167 /* 168 * Fall back to generic, e.g. net-pf-10-proto-132 169 * (net-pf-PF_INET6-proto-IPPROTO_SCTP) 170 */ 171 else 172 request_module("net-pf-%d-proto-%d", 173 PF_INET6, protocol); 174 goto lookup_protocol; 175 } else 176 goto out_rcu_unlock; 177 } 178 179 err = -EPERM; 180 if (sock->type == SOCK_RAW && !kern && 181 !ns_capable(net->user_ns, CAP_NET_RAW)) 182 goto out_rcu_unlock; 183 184 sock->ops = answer->ops; 185 answer_prot = answer->prot; 186 answer_flags = answer->flags; 187 rcu_read_unlock(); 188 189 WARN_ON(!answer_prot->slab); 190 191 err = -ENOBUFS; 192 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot, kern); 193 if (!sk) 194 goto out; 195 196 sock_init_data(sock, sk); 197 198 err = 0; 199 if (INET_PROTOSW_REUSE & answer_flags) 200 sk->sk_reuse = SK_CAN_REUSE; 201 202 inet = inet_sk(sk); 203 inet_assign_bit(IS_ICSK, sk, INET_PROTOSW_ICSK & answer_flags); 204 205 if (SOCK_RAW == sock->type) { 206 inet->inet_num = protocol; 207 if (IPPROTO_RAW == protocol) 208 inet_set_bit(HDRINCL, sk); 209 } 210 211 sk->sk_destruct = inet6_sock_destruct; 212 sk->sk_family = PF_INET6; 213 sk->sk_protocol = protocol; 214 215 sk->sk_backlog_rcv = answer->prot->backlog_rcv; 216 217 inet_sk(sk)->pinet6 = np = inet6_sk_generic(sk); 218 np->hop_limit = -1; 219 np->mcast_hops = IPV6_DEFAULT_MCASTHOPS; 220 inet6_set_bit(MC6_LOOP, sk); 221 inet6_set_bit(MC6_ALL, sk); 222 np->pmtudisc = IPV6_PMTUDISC_WANT; 223 inet6_assign_bit(REPFLOW, sk, net->ipv6.sysctl.flowlabel_reflect & 224 FLOWLABEL_REFLECT_ESTABLISHED); 225 sk->sk_ipv6only = net->ipv6.sysctl.bindv6only; 226 sk->sk_txrehash = READ_ONCE(net->core.sysctl_txrehash); 227 228 /* Init the ipv4 part of the socket since we can have sockets 229 * using v6 API for ipv4. 230 */ 231 inet->uc_ttl = -1; 232 233 inet_set_bit(MC_LOOP, sk); 234 inet->mc_ttl = 1; 235 inet->mc_index = 0; 236 RCU_INIT_POINTER(inet->mc_list, NULL); 237 inet->rcv_tos = 0; 238 239 if (READ_ONCE(net->ipv4.sysctl_ip_no_pmtu_disc)) 240 inet->pmtudisc = IP_PMTUDISC_DONT; 241 else 242 inet->pmtudisc = IP_PMTUDISC_WANT; 243 244 if (inet->inet_num) { 245 /* It assumes that any protocol which allows 246 * the user to assign a number at socket 247 * creation time automatically shares. 248 */ 249 inet->inet_sport = htons(inet->inet_num); 250 err = sk->sk_prot->hash(sk); 251 if (err) { 252 sk_common_release(sk); 253 goto out; 254 } 255 } 256 if (sk->sk_prot->init) { 257 err = sk->sk_prot->init(sk); 258 if (err) { 259 sk_common_release(sk); 260 goto out; 261 } 262 } 263 264 if (!kern) { 265 err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk); 266 if (err) { 267 sk_common_release(sk); 268 goto out; 269 } 270 } 271 out: 272 return err; 273 out_rcu_unlock: 274 rcu_read_unlock(); 275 goto out; 276 } 277 278 static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len, 279 u32 flags) 280 { 281 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)uaddr; 282 struct inet_sock *inet = inet_sk(sk); 283 struct ipv6_pinfo *np = inet6_sk(sk); 284 struct net *net = sock_net(sk); 285 __be32 v4addr = 0; 286 unsigned short snum; 287 bool saved_ipv6only; 288 int addr_type = 0; 289 int err = 0; 290 291 if (addr->sin6_family != AF_INET6) 292 return -EAFNOSUPPORT; 293 294 addr_type = ipv6_addr_type(&addr->sin6_addr); 295 if ((addr_type & IPV6_ADDR_MULTICAST) && sk->sk_type == SOCK_STREAM) 296 return -EINVAL; 297 298 snum = ntohs(addr->sin6_port); 299 if (!(flags & BIND_NO_CAP_NET_BIND_SERVICE) && 300 snum && inet_port_requires_bind_service(net, snum) && 301 !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE)) 302 return -EACCES; 303 304 if (flags & BIND_WITH_LOCK) 305 lock_sock(sk); 306 307 /* Check these errors (active socket, double bind). */ 308 if (sk->sk_state != TCP_CLOSE || inet->inet_num) { 309 err = -EINVAL; 310 goto out; 311 } 312 313 /* Check if the address belongs to the host. */ 314 if (addr_type == IPV6_ADDR_MAPPED) { 315 struct net_device *dev = NULL; 316 int chk_addr_ret; 317 318 /* Binding to v4-mapped address on a v6-only socket 319 * makes no sense 320 */ 321 if (ipv6_only_sock(sk)) { 322 err = -EINVAL; 323 goto out; 324 } 325 326 rcu_read_lock(); 327 if (sk->sk_bound_dev_if) { 328 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if); 329 if (!dev) { 330 err = -ENODEV; 331 goto out_unlock; 332 } 333 } 334 335 /* Reproduce AF_INET checks to make the bindings consistent */ 336 v4addr = addr->sin6_addr.s6_addr32[3]; 337 chk_addr_ret = inet_addr_type_dev_table(net, dev, v4addr); 338 rcu_read_unlock(); 339 340 if (!inet_addr_valid_or_nonlocal(net, inet, v4addr, 341 chk_addr_ret)) { 342 err = -EADDRNOTAVAIL; 343 goto out; 344 } 345 } else { 346 if (addr_type != IPV6_ADDR_ANY) { 347 struct net_device *dev = NULL; 348 349 rcu_read_lock(); 350 if (__ipv6_addr_needs_scope_id(addr_type)) { 351 if (addr_len >= sizeof(struct sockaddr_in6) && 352 addr->sin6_scope_id) { 353 /* Override any existing binding, if another one 354 * is supplied by user. 355 */ 356 sk->sk_bound_dev_if = addr->sin6_scope_id; 357 } 358 359 /* Binding to link-local address requires an interface */ 360 if (!sk->sk_bound_dev_if) { 361 err = -EINVAL; 362 goto out_unlock; 363 } 364 } 365 366 if (sk->sk_bound_dev_if) { 367 dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if); 368 if (!dev) { 369 err = -ENODEV; 370 goto out_unlock; 371 } 372 } 373 374 /* ipv4 addr of the socket is invalid. Only the 375 * unspecified and mapped address have a v4 equivalent. 376 */ 377 v4addr = LOOPBACK4_IPV6; 378 if (!(addr_type & IPV6_ADDR_MULTICAST)) { 379 if (!ipv6_can_nonlocal_bind(net, inet) && 380 !ipv6_chk_addr(net, &addr->sin6_addr, 381 dev, 0)) { 382 err = -EADDRNOTAVAIL; 383 goto out_unlock; 384 } 385 } 386 rcu_read_unlock(); 387 } 388 } 389 390 inet->inet_rcv_saddr = v4addr; 391 inet->inet_saddr = v4addr; 392 393 sk->sk_v6_rcv_saddr = addr->sin6_addr; 394 395 if (!(addr_type & IPV6_ADDR_MULTICAST)) 396 np->saddr = addr->sin6_addr; 397 398 saved_ipv6only = sk->sk_ipv6only; 399 if (addr_type != IPV6_ADDR_ANY && addr_type != IPV6_ADDR_MAPPED) 400 sk->sk_ipv6only = 1; 401 402 /* Make sure we are allowed to bind here. */ 403 if (snum || !(inet_test_bit(BIND_ADDRESS_NO_PORT, sk) || 404 (flags & BIND_FORCE_ADDRESS_NO_PORT))) { 405 err = sk->sk_prot->get_port(sk, snum); 406 if (err) { 407 sk->sk_ipv6only = saved_ipv6only; 408 inet_reset_saddr(sk); 409 goto out; 410 } 411 if (!(flags & BIND_FROM_BPF)) { 412 err = BPF_CGROUP_RUN_PROG_INET6_POST_BIND(sk); 413 if (err) { 414 sk->sk_ipv6only = saved_ipv6only; 415 inet_reset_saddr(sk); 416 if (sk->sk_prot->put_port) 417 sk->sk_prot->put_port(sk); 418 goto out; 419 } 420 } 421 } 422 423 if (addr_type != IPV6_ADDR_ANY) 424 sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 425 if (snum) 426 sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 427 inet->inet_sport = htons(inet->inet_num); 428 inet->inet_dport = 0; 429 inet->inet_daddr = 0; 430 out: 431 if (flags & BIND_WITH_LOCK) 432 release_sock(sk); 433 return err; 434 out_unlock: 435 rcu_read_unlock(); 436 goto out; 437 } 438 439 int inet6_bind_sk(struct sock *sk, struct sockaddr *uaddr, int addr_len) 440 { 441 u32 flags = BIND_WITH_LOCK; 442 const struct proto *prot; 443 int err = 0; 444 445 /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 446 prot = READ_ONCE(sk->sk_prot); 447 /* If the socket has its own bind function then use it. */ 448 if (prot->bind) 449 return prot->bind(sk, uaddr, addr_len); 450 451 if (addr_len < SIN6_LEN_RFC2133) 452 return -EINVAL; 453 454 /* BPF prog is run before any checks are done so that if the prog 455 * changes context in a wrong way it will be caught. 456 */ 457 err = BPF_CGROUP_RUN_PROG_INET_BIND_LOCK(sk, uaddr, 458 CGROUP_INET6_BIND, &flags); 459 if (err) 460 return err; 461 462 return __inet6_bind(sk, uaddr, addr_len, flags); 463 } 464 465 /* bind for INET6 API */ 466 int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 467 { 468 return inet6_bind_sk(sock->sk, uaddr, addr_len); 469 } 470 EXPORT_SYMBOL(inet6_bind); 471 472 int inet6_release(struct socket *sock) 473 { 474 struct sock *sk = sock->sk; 475 476 if (!sk) 477 return -EINVAL; 478 479 /* Free mc lists */ 480 ipv6_sock_mc_close(sk); 481 482 /* Free ac lists */ 483 ipv6_sock_ac_close(sk); 484 485 return inet_release(sock); 486 } 487 EXPORT_SYMBOL(inet6_release); 488 489 void inet6_cleanup_sock(struct sock *sk) 490 { 491 struct ipv6_pinfo *np = inet6_sk(sk); 492 struct sk_buff *skb; 493 struct ipv6_txoptions *opt; 494 495 /* Release rx options */ 496 497 skb = xchg(&np->pktoptions, NULL); 498 kfree_skb(skb); 499 500 skb = xchg(&np->rxpmtu, NULL); 501 kfree_skb(skb); 502 503 /* Free flowlabels */ 504 fl6_free_socklist(sk); 505 506 /* Free tx options */ 507 508 opt = xchg((__force struct ipv6_txoptions **)&np->opt, NULL); 509 if (opt) { 510 atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 511 txopt_put(opt); 512 } 513 } 514 EXPORT_SYMBOL_GPL(inet6_cleanup_sock); 515 516 /* 517 * This does both peername and sockname. 518 */ 519 int inet6_getname(struct socket *sock, struct sockaddr *uaddr, 520 int peer) 521 { 522 struct sockaddr_in6 *sin = (struct sockaddr_in6 *)uaddr; 523 struct sock *sk = sock->sk; 524 struct inet_sock *inet = inet_sk(sk); 525 struct ipv6_pinfo *np = inet6_sk(sk); 526 527 sin->sin6_family = AF_INET6; 528 sin->sin6_flowinfo = 0; 529 sin->sin6_scope_id = 0; 530 lock_sock(sk); 531 if (peer) { 532 if (!inet->inet_dport || 533 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) && 534 peer == 1)) { 535 release_sock(sk); 536 return -ENOTCONN; 537 } 538 sin->sin6_port = inet->inet_dport; 539 sin->sin6_addr = sk->sk_v6_daddr; 540 if (inet6_test_bit(SNDFLOW, sk)) 541 sin->sin6_flowinfo = np->flow_label; 542 BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, 543 CGROUP_INET6_GETPEERNAME); 544 } else { 545 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) 546 sin->sin6_addr = np->saddr; 547 else 548 sin->sin6_addr = sk->sk_v6_rcv_saddr; 549 sin->sin6_port = inet->inet_sport; 550 BPF_CGROUP_RUN_SA_PROG(sk, (struct sockaddr *)sin, 551 CGROUP_INET6_GETSOCKNAME); 552 } 553 sin->sin6_scope_id = ipv6_iface_scope_id(&sin->sin6_addr, 554 sk->sk_bound_dev_if); 555 release_sock(sk); 556 return sizeof(*sin); 557 } 558 EXPORT_SYMBOL(inet6_getname); 559 560 int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 561 { 562 void __user *argp = (void __user *)arg; 563 struct sock *sk = sock->sk; 564 struct net *net = sock_net(sk); 565 const struct proto *prot; 566 567 switch (cmd) { 568 case SIOCADDRT: 569 case SIOCDELRT: { 570 struct in6_rtmsg rtmsg; 571 572 if (copy_from_user(&rtmsg, argp, sizeof(rtmsg))) 573 return -EFAULT; 574 return ipv6_route_ioctl(net, cmd, &rtmsg); 575 } 576 case SIOCSIFADDR: 577 return addrconf_add_ifaddr(net, argp); 578 case SIOCDIFADDR: 579 return addrconf_del_ifaddr(net, argp); 580 case SIOCSIFDSTADDR: 581 return addrconf_set_dstaddr(net, argp); 582 default: 583 /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 584 prot = READ_ONCE(sk->sk_prot); 585 if (!prot->ioctl) 586 return -ENOIOCTLCMD; 587 return sk_ioctl(sk, cmd, (void __user *)arg); 588 } 589 /*NOTREACHED*/ 590 return 0; 591 } 592 EXPORT_SYMBOL(inet6_ioctl); 593 594 #ifdef CONFIG_COMPAT 595 struct compat_in6_rtmsg { 596 struct in6_addr rtmsg_dst; 597 struct in6_addr rtmsg_src; 598 struct in6_addr rtmsg_gateway; 599 u32 rtmsg_type; 600 u16 rtmsg_dst_len; 601 u16 rtmsg_src_len; 602 u32 rtmsg_metric; 603 u32 rtmsg_info; 604 u32 rtmsg_flags; 605 s32 rtmsg_ifindex; 606 }; 607 608 static int inet6_compat_routing_ioctl(struct sock *sk, unsigned int cmd, 609 struct compat_in6_rtmsg __user *ur) 610 { 611 struct in6_rtmsg rt; 612 613 if (copy_from_user(&rt.rtmsg_dst, &ur->rtmsg_dst, 614 3 * sizeof(struct in6_addr)) || 615 get_user(rt.rtmsg_type, &ur->rtmsg_type) || 616 get_user(rt.rtmsg_dst_len, &ur->rtmsg_dst_len) || 617 get_user(rt.rtmsg_src_len, &ur->rtmsg_src_len) || 618 get_user(rt.rtmsg_metric, &ur->rtmsg_metric) || 619 get_user(rt.rtmsg_info, &ur->rtmsg_info) || 620 get_user(rt.rtmsg_flags, &ur->rtmsg_flags) || 621 get_user(rt.rtmsg_ifindex, &ur->rtmsg_ifindex)) 622 return -EFAULT; 623 624 625 return ipv6_route_ioctl(sock_net(sk), cmd, &rt); 626 } 627 628 int inet6_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 629 { 630 void __user *argp = compat_ptr(arg); 631 struct sock *sk = sock->sk; 632 633 switch (cmd) { 634 case SIOCADDRT: 635 case SIOCDELRT: 636 return inet6_compat_routing_ioctl(sk, cmd, argp); 637 default: 638 return -ENOIOCTLCMD; 639 } 640 } 641 EXPORT_SYMBOL_GPL(inet6_compat_ioctl); 642 #endif /* CONFIG_COMPAT */ 643 644 INDIRECT_CALLABLE_DECLARE(int udpv6_sendmsg(struct sock *, struct msghdr *, 645 size_t)); 646 int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 647 { 648 struct sock *sk = sock->sk; 649 const struct proto *prot; 650 651 if (unlikely(inet_send_prepare(sk))) 652 return -EAGAIN; 653 654 /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 655 prot = READ_ONCE(sk->sk_prot); 656 return INDIRECT_CALL_2(prot->sendmsg, tcp_sendmsg, udpv6_sendmsg, 657 sk, msg, size); 658 } 659 660 INDIRECT_CALLABLE_DECLARE(int udpv6_recvmsg(struct sock *, struct msghdr *, 661 size_t, int, int *)); 662 int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 663 int flags) 664 { 665 struct sock *sk = sock->sk; 666 const struct proto *prot; 667 int addr_len = 0; 668 int err; 669 670 if (likely(!(flags & MSG_ERRQUEUE))) 671 sock_rps_record_flow(sk); 672 673 /* IPV6_ADDRFORM can change sk->sk_prot under us. */ 674 prot = READ_ONCE(sk->sk_prot); 675 err = INDIRECT_CALL_2(prot->recvmsg, tcp_recvmsg, udpv6_recvmsg, 676 sk, msg, size, flags, &addr_len); 677 if (err >= 0) 678 msg->msg_namelen = addr_len; 679 return err; 680 } 681 682 const struct proto_ops inet6_stream_ops = { 683 .family = PF_INET6, 684 .owner = THIS_MODULE, 685 .release = inet6_release, 686 .bind = inet6_bind, 687 .connect = inet_stream_connect, /* ok */ 688 .socketpair = sock_no_socketpair, /* a do nothing */ 689 .accept = inet_accept, /* ok */ 690 .getname = inet6_getname, 691 .poll = tcp_poll, /* ok */ 692 .ioctl = inet6_ioctl, /* must change */ 693 .gettstamp = sock_gettstamp, 694 .listen = inet_listen, /* ok */ 695 .shutdown = inet_shutdown, /* ok */ 696 .setsockopt = sock_common_setsockopt, /* ok */ 697 .getsockopt = sock_common_getsockopt, /* ok */ 698 .sendmsg = inet6_sendmsg, /* retpoline's sake */ 699 .recvmsg = inet6_recvmsg, /* retpoline's sake */ 700 #ifdef CONFIG_MMU 701 .mmap = tcp_mmap, 702 #endif 703 .splice_eof = inet_splice_eof, 704 .sendmsg_locked = tcp_sendmsg_locked, 705 .splice_read = tcp_splice_read, 706 .read_sock = tcp_read_sock, 707 .read_skb = tcp_read_skb, 708 .peek_len = tcp_peek_len, 709 #ifdef CONFIG_COMPAT 710 .compat_ioctl = inet6_compat_ioctl, 711 #endif 712 .set_rcvlowat = tcp_set_rcvlowat, 713 }; 714 715 const struct proto_ops inet6_dgram_ops = { 716 .family = PF_INET6, 717 .owner = THIS_MODULE, 718 .release = inet6_release, 719 .bind = inet6_bind, 720 .connect = inet_dgram_connect, /* ok */ 721 .socketpair = sock_no_socketpair, /* a do nothing */ 722 .accept = sock_no_accept, /* a do nothing */ 723 .getname = inet6_getname, 724 .poll = udp_poll, /* ok */ 725 .ioctl = inet6_ioctl, /* must change */ 726 .gettstamp = sock_gettstamp, 727 .listen = sock_no_listen, /* ok */ 728 .shutdown = inet_shutdown, /* ok */ 729 .setsockopt = sock_common_setsockopt, /* ok */ 730 .getsockopt = sock_common_getsockopt, /* ok */ 731 .sendmsg = inet6_sendmsg, /* retpoline's sake */ 732 .recvmsg = inet6_recvmsg, /* retpoline's sake */ 733 .read_skb = udp_read_skb, 734 .mmap = sock_no_mmap, 735 .set_peek_off = sk_set_peek_off, 736 #ifdef CONFIG_COMPAT 737 .compat_ioctl = inet6_compat_ioctl, 738 #endif 739 }; 740 741 static const struct net_proto_family inet6_family_ops = { 742 .family = PF_INET6, 743 .create = inet6_create, 744 .owner = THIS_MODULE, 745 }; 746 747 int inet6_register_protosw(struct inet_protosw *p) 748 { 749 struct list_head *lh; 750 struct inet_protosw *answer; 751 struct list_head *last_perm; 752 int protocol = p->protocol; 753 int ret; 754 755 spin_lock_bh(&inetsw6_lock); 756 757 ret = -EINVAL; 758 if (p->type >= SOCK_MAX) 759 goto out_illegal; 760 761 /* If we are trying to override a permanent protocol, bail. */ 762 answer = NULL; 763 ret = -EPERM; 764 last_perm = &inetsw6[p->type]; 765 list_for_each(lh, &inetsw6[p->type]) { 766 answer = list_entry(lh, struct inet_protosw, list); 767 768 /* Check only the non-wild match. */ 769 if (INET_PROTOSW_PERMANENT & answer->flags) { 770 if (protocol == answer->protocol) 771 break; 772 last_perm = lh; 773 } 774 775 answer = NULL; 776 } 777 if (answer) 778 goto out_permanent; 779 780 /* Add the new entry after the last permanent entry if any, so that 781 * the new entry does not override a permanent entry when matched with 782 * a wild-card protocol. But it is allowed to override any existing 783 * non-permanent entry. This means that when we remove this entry, the 784 * system automatically returns to the old behavior. 785 */ 786 list_add_rcu(&p->list, last_perm); 787 ret = 0; 788 out: 789 spin_unlock_bh(&inetsw6_lock); 790 return ret; 791 792 out_permanent: 793 pr_err("Attempt to override permanent protocol %d\n", protocol); 794 goto out; 795 796 out_illegal: 797 pr_err("Ignoring attempt to register invalid socket type %d\n", 798 p->type); 799 goto out; 800 } 801 EXPORT_SYMBOL(inet6_register_protosw); 802 803 void 804 inet6_unregister_protosw(struct inet_protosw *p) 805 { 806 if (INET_PROTOSW_PERMANENT & p->flags) { 807 pr_err("Attempt to unregister permanent protocol %d\n", 808 p->protocol); 809 } else { 810 spin_lock_bh(&inetsw6_lock); 811 list_del_rcu(&p->list); 812 spin_unlock_bh(&inetsw6_lock); 813 814 synchronize_net(); 815 } 816 } 817 EXPORT_SYMBOL(inet6_unregister_protosw); 818 819 int inet6_sk_rebuild_header(struct sock *sk) 820 { 821 struct ipv6_pinfo *np = inet6_sk(sk); 822 struct dst_entry *dst; 823 824 dst = __sk_dst_check(sk, np->dst_cookie); 825 826 if (!dst) { 827 struct inet_sock *inet = inet_sk(sk); 828 struct in6_addr *final_p, final; 829 struct flowi6 fl6; 830 831 memset(&fl6, 0, sizeof(fl6)); 832 fl6.flowi6_proto = sk->sk_protocol; 833 fl6.daddr = sk->sk_v6_daddr; 834 fl6.saddr = np->saddr; 835 fl6.flowlabel = np->flow_label; 836 fl6.flowi6_oif = sk->sk_bound_dev_if; 837 fl6.flowi6_mark = sk->sk_mark; 838 fl6.fl6_dport = inet->inet_dport; 839 fl6.fl6_sport = inet->inet_sport; 840 fl6.flowi6_uid = sk->sk_uid; 841 security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6)); 842 843 rcu_read_lock(); 844 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), 845 &final); 846 rcu_read_unlock(); 847 848 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p); 849 if (IS_ERR(dst)) { 850 sk->sk_route_caps = 0; 851 WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst)); 852 return PTR_ERR(dst); 853 } 854 855 ip6_dst_store(sk, dst, NULL, NULL); 856 } 857 858 return 0; 859 } 860 EXPORT_SYMBOL_GPL(inet6_sk_rebuild_header); 861 862 bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb, 863 const struct inet6_skb_parm *opt) 864 { 865 const struct ipv6_pinfo *np = inet6_sk(sk); 866 867 if (np->rxopt.all) { 868 if (((opt->flags & IP6SKB_HOPBYHOP) && 869 (np->rxopt.bits.hopopts || np->rxopt.bits.ohopopts)) || 870 (ip6_flowinfo((struct ipv6hdr *) skb_network_header(skb)) && 871 np->rxopt.bits.rxflow) || 872 (opt->srcrt && (np->rxopt.bits.srcrt || 873 np->rxopt.bits.osrcrt)) || 874 ((opt->dst1 || opt->dst0) && 875 (np->rxopt.bits.dstopts || np->rxopt.bits.odstopts))) 876 return true; 877 } 878 return false; 879 } 880 EXPORT_SYMBOL_GPL(ipv6_opt_accepted); 881 882 static struct packet_type ipv6_packet_type __read_mostly = { 883 .type = cpu_to_be16(ETH_P_IPV6), 884 .func = ipv6_rcv, 885 .list_func = ipv6_list_rcv, 886 }; 887 888 static int __init ipv6_packet_init(void) 889 { 890 dev_add_pack(&ipv6_packet_type); 891 return 0; 892 } 893 894 static void ipv6_packet_cleanup(void) 895 { 896 dev_remove_pack(&ipv6_packet_type); 897 } 898 899 static int __net_init ipv6_init_mibs(struct net *net) 900 { 901 int i; 902 903 net->mib.udp_stats_in6 = alloc_percpu(struct udp_mib); 904 if (!net->mib.udp_stats_in6) 905 return -ENOMEM; 906 net->mib.udplite_stats_in6 = alloc_percpu(struct udp_mib); 907 if (!net->mib.udplite_stats_in6) 908 goto err_udplite_mib; 909 net->mib.ipv6_statistics = alloc_percpu(struct ipstats_mib); 910 if (!net->mib.ipv6_statistics) 911 goto err_ip_mib; 912 913 for_each_possible_cpu(i) { 914 struct ipstats_mib *af_inet6_stats; 915 af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics, i); 916 u64_stats_init(&af_inet6_stats->syncp); 917 } 918 919 920 net->mib.icmpv6_statistics = alloc_percpu(struct icmpv6_mib); 921 if (!net->mib.icmpv6_statistics) 922 goto err_icmp_mib; 923 net->mib.icmpv6msg_statistics = kzalloc(sizeof(struct icmpv6msg_mib), 924 GFP_KERNEL); 925 if (!net->mib.icmpv6msg_statistics) 926 goto err_icmpmsg_mib; 927 return 0; 928 929 err_icmpmsg_mib: 930 free_percpu(net->mib.icmpv6_statistics); 931 err_icmp_mib: 932 free_percpu(net->mib.ipv6_statistics); 933 err_ip_mib: 934 free_percpu(net->mib.udplite_stats_in6); 935 err_udplite_mib: 936 free_percpu(net->mib.udp_stats_in6); 937 return -ENOMEM; 938 } 939 940 static void ipv6_cleanup_mibs(struct net *net) 941 { 942 free_percpu(net->mib.udp_stats_in6); 943 free_percpu(net->mib.udplite_stats_in6); 944 free_percpu(net->mib.ipv6_statistics); 945 free_percpu(net->mib.icmpv6_statistics); 946 kfree(net->mib.icmpv6msg_statistics); 947 } 948 949 static int __net_init inet6_net_init(struct net *net) 950 { 951 int err = 0; 952 953 net->ipv6.sysctl.bindv6only = 0; 954 net->ipv6.sysctl.icmpv6_time = 1*HZ; 955 net->ipv6.sysctl.icmpv6_echo_ignore_all = 0; 956 net->ipv6.sysctl.icmpv6_echo_ignore_multicast = 0; 957 net->ipv6.sysctl.icmpv6_echo_ignore_anycast = 0; 958 net->ipv6.sysctl.icmpv6_error_anycast_as_unicast = 0; 959 960 /* By default, rate limit error messages. 961 * Except for pmtu discovery, it would break it. 962 * proc_do_large_bitmap needs pointer to the bitmap. 963 */ 964 bitmap_set(net->ipv6.sysctl.icmpv6_ratemask, 0, ICMPV6_ERRMSG_MAX + 1); 965 bitmap_clear(net->ipv6.sysctl.icmpv6_ratemask, ICMPV6_PKT_TOOBIG, 1); 966 net->ipv6.sysctl.icmpv6_ratemask_ptr = net->ipv6.sysctl.icmpv6_ratemask; 967 968 net->ipv6.sysctl.flowlabel_consistency = 1; 969 net->ipv6.sysctl.auto_flowlabels = IP6_DEFAULT_AUTO_FLOW_LABELS; 970 net->ipv6.sysctl.idgen_retries = 3; 971 net->ipv6.sysctl.idgen_delay = 1 * HZ; 972 net->ipv6.sysctl.flowlabel_state_ranges = 0; 973 net->ipv6.sysctl.max_dst_opts_cnt = IP6_DEFAULT_MAX_DST_OPTS_CNT; 974 net->ipv6.sysctl.max_hbh_opts_cnt = IP6_DEFAULT_MAX_HBH_OPTS_CNT; 975 net->ipv6.sysctl.max_dst_opts_len = IP6_DEFAULT_MAX_DST_OPTS_LEN; 976 net->ipv6.sysctl.max_hbh_opts_len = IP6_DEFAULT_MAX_HBH_OPTS_LEN; 977 net->ipv6.sysctl.fib_notify_on_flag_change = 0; 978 atomic_set(&net->ipv6.fib6_sernum, 1); 979 980 net->ipv6.sysctl.ioam6_id = IOAM6_DEFAULT_ID; 981 net->ipv6.sysctl.ioam6_id_wide = IOAM6_DEFAULT_ID_WIDE; 982 983 err = ipv6_init_mibs(net); 984 if (err) 985 return err; 986 #ifdef CONFIG_PROC_FS 987 err = udp6_proc_init(net); 988 if (err) 989 goto out; 990 err = tcp6_proc_init(net); 991 if (err) 992 goto proc_tcp6_fail; 993 err = ac6_proc_init(net); 994 if (err) 995 goto proc_ac6_fail; 996 #endif 997 return err; 998 999 #ifdef CONFIG_PROC_FS 1000 proc_ac6_fail: 1001 tcp6_proc_exit(net); 1002 proc_tcp6_fail: 1003 udp6_proc_exit(net); 1004 out: 1005 ipv6_cleanup_mibs(net); 1006 return err; 1007 #endif 1008 } 1009 1010 static void __net_exit inet6_net_exit(struct net *net) 1011 { 1012 #ifdef CONFIG_PROC_FS 1013 udp6_proc_exit(net); 1014 tcp6_proc_exit(net); 1015 ac6_proc_exit(net); 1016 #endif 1017 ipv6_cleanup_mibs(net); 1018 } 1019 1020 static struct pernet_operations inet6_net_ops = { 1021 .init = inet6_net_init, 1022 .exit = inet6_net_exit, 1023 }; 1024 1025 static int ipv6_route_input(struct sk_buff *skb) 1026 { 1027 ip6_route_input(skb); 1028 return skb_dst(skb)->error; 1029 } 1030 1031 static const struct ipv6_stub ipv6_stub_impl = { 1032 .ipv6_sock_mc_join = ipv6_sock_mc_join, 1033 .ipv6_sock_mc_drop = ipv6_sock_mc_drop, 1034 .ipv6_dst_lookup_flow = ip6_dst_lookup_flow, 1035 .ipv6_route_input = ipv6_route_input, 1036 .fib6_get_table = fib6_get_table, 1037 .fib6_table_lookup = fib6_table_lookup, 1038 .fib6_lookup = fib6_lookup, 1039 .fib6_select_path = fib6_select_path, 1040 .ip6_mtu_from_fib6 = ip6_mtu_from_fib6, 1041 .fib6_nh_init = fib6_nh_init, 1042 .fib6_nh_release = fib6_nh_release, 1043 .fib6_nh_release_dsts = fib6_nh_release_dsts, 1044 .fib6_update_sernum = fib6_update_sernum_stub, 1045 .fib6_rt_update = fib6_rt_update, 1046 .ip6_del_rt = ip6_del_rt, 1047 .udpv6_encap_enable = udpv6_encap_enable, 1048 .ndisc_send_na = ndisc_send_na, 1049 #if IS_ENABLED(CONFIG_XFRM) 1050 .xfrm6_local_rxpmtu = xfrm6_local_rxpmtu, 1051 .xfrm6_udp_encap_rcv = xfrm6_udp_encap_rcv, 1052 .xfrm6_rcv_encap = xfrm6_rcv_encap, 1053 #endif 1054 .nd_tbl = &nd_tbl, 1055 .ipv6_fragment = ip6_fragment, 1056 .ipv6_dev_find = ipv6_dev_find, 1057 }; 1058 1059 static const struct ipv6_bpf_stub ipv6_bpf_stub_impl = { 1060 .inet6_bind = __inet6_bind, 1061 .udp6_lib_lookup = __udp6_lib_lookup, 1062 .ipv6_setsockopt = do_ipv6_setsockopt, 1063 .ipv6_getsockopt = do_ipv6_getsockopt, 1064 }; 1065 1066 static int __init inet6_init(void) 1067 { 1068 struct list_head *r; 1069 int err = 0; 1070 1071 sock_skb_cb_check_size(sizeof(struct inet6_skb_parm)); 1072 1073 /* Register the socket-side information for inet6_create. */ 1074 for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r) 1075 INIT_LIST_HEAD(r); 1076 1077 raw_hashinfo_init(&raw_v6_hashinfo); 1078 1079 if (disable_ipv6_mod) { 1080 pr_info("Loaded, but administratively disabled, reboot required to enable\n"); 1081 goto out; 1082 } 1083 1084 err = proto_register(&tcpv6_prot, 1); 1085 if (err) 1086 goto out; 1087 1088 err = proto_register(&udpv6_prot, 1); 1089 if (err) 1090 goto out_unregister_tcp_proto; 1091 1092 err = proto_register(&udplitev6_prot, 1); 1093 if (err) 1094 goto out_unregister_udp_proto; 1095 1096 err = proto_register(&rawv6_prot, 1); 1097 if (err) 1098 goto out_unregister_udplite_proto; 1099 1100 err = proto_register(&pingv6_prot, 1); 1101 if (err) 1102 goto out_unregister_raw_proto; 1103 1104 /* We MUST register RAW sockets before we create the ICMP6, 1105 * IGMP6, or NDISC control sockets. 1106 */ 1107 err = rawv6_init(); 1108 if (err) 1109 goto out_unregister_ping_proto; 1110 1111 /* Register the family here so that the init calls below will 1112 * be able to create sockets. (?? is this dangerous ??) 1113 */ 1114 err = sock_register(&inet6_family_ops); 1115 if (err) 1116 goto out_sock_register_fail; 1117 1118 /* 1119 * ipngwg API draft makes clear that the correct semantics 1120 * for TCP and UDP is to consider one TCP and UDP instance 1121 * in a host available by both INET and INET6 APIs and 1122 * able to communicate via both network protocols. 1123 */ 1124 1125 err = register_pernet_subsys(&inet6_net_ops); 1126 if (err) 1127 goto register_pernet_fail; 1128 err = ip6_mr_init(); 1129 if (err) 1130 goto ipmr_fail; 1131 err = icmpv6_init(); 1132 if (err) 1133 goto icmp_fail; 1134 err = ndisc_init(); 1135 if (err) 1136 goto ndisc_fail; 1137 err = igmp6_init(); 1138 if (err) 1139 goto igmp_fail; 1140 1141 err = ipv6_netfilter_init(); 1142 if (err) 1143 goto netfilter_fail; 1144 /* Create /proc/foo6 entries. */ 1145 #ifdef CONFIG_PROC_FS 1146 err = -ENOMEM; 1147 if (raw6_proc_init()) 1148 goto proc_raw6_fail; 1149 if (udplite6_proc_init()) 1150 goto proc_udplite6_fail; 1151 if (ipv6_misc_proc_init()) 1152 goto proc_misc6_fail; 1153 if (if6_proc_init()) 1154 goto proc_if6_fail; 1155 #endif 1156 err = ip6_route_init(); 1157 if (err) 1158 goto ip6_route_fail; 1159 err = ndisc_late_init(); 1160 if (err) 1161 goto ndisc_late_fail; 1162 err = ip6_flowlabel_init(); 1163 if (err) 1164 goto ip6_flowlabel_fail; 1165 err = ipv6_anycast_init(); 1166 if (err) 1167 goto ipv6_anycast_fail; 1168 err = addrconf_init(); 1169 if (err) 1170 goto addrconf_fail; 1171 1172 /* Init v6 extension headers. */ 1173 err = ipv6_exthdrs_init(); 1174 if (err) 1175 goto ipv6_exthdrs_fail; 1176 1177 err = ipv6_frag_init(); 1178 if (err) 1179 goto ipv6_frag_fail; 1180 1181 /* Init v6 transport protocols. */ 1182 err = udpv6_init(); 1183 if (err) 1184 goto udpv6_fail; 1185 1186 err = udplitev6_init(); 1187 if (err) 1188 goto udplitev6_fail; 1189 1190 err = udpv6_offload_init(); 1191 if (err) 1192 goto udpv6_offload_fail; 1193 1194 err = tcpv6_init(); 1195 if (err) 1196 goto tcpv6_fail; 1197 1198 err = ipv6_packet_init(); 1199 if (err) 1200 goto ipv6_packet_fail; 1201 1202 err = pingv6_init(); 1203 if (err) 1204 goto pingv6_fail; 1205 1206 err = calipso_init(); 1207 if (err) 1208 goto calipso_fail; 1209 1210 err = seg6_init(); 1211 if (err) 1212 goto seg6_fail; 1213 1214 err = rpl_init(); 1215 if (err) 1216 goto rpl_fail; 1217 1218 err = ioam6_init(); 1219 if (err) 1220 goto ioam6_fail; 1221 1222 err = igmp6_late_init(); 1223 if (err) 1224 goto igmp6_late_err; 1225 1226 #ifdef CONFIG_SYSCTL 1227 err = ipv6_sysctl_register(); 1228 if (err) 1229 goto sysctl_fail; 1230 #endif 1231 1232 /* ensure that ipv6 stubs are visible only after ipv6 is ready */ 1233 wmb(); 1234 ipv6_stub = &ipv6_stub_impl; 1235 ipv6_bpf_stub = &ipv6_bpf_stub_impl; 1236 out: 1237 return err; 1238 1239 #ifdef CONFIG_SYSCTL 1240 sysctl_fail: 1241 igmp6_late_cleanup(); 1242 #endif 1243 igmp6_late_err: 1244 ioam6_exit(); 1245 ioam6_fail: 1246 rpl_exit(); 1247 rpl_fail: 1248 seg6_exit(); 1249 seg6_fail: 1250 calipso_exit(); 1251 calipso_fail: 1252 pingv6_exit(); 1253 pingv6_fail: 1254 ipv6_packet_cleanup(); 1255 ipv6_packet_fail: 1256 tcpv6_exit(); 1257 tcpv6_fail: 1258 udpv6_offload_exit(); 1259 udpv6_offload_fail: 1260 udplitev6_exit(); 1261 udplitev6_fail: 1262 udpv6_exit(); 1263 udpv6_fail: 1264 ipv6_frag_exit(); 1265 ipv6_frag_fail: 1266 ipv6_exthdrs_exit(); 1267 ipv6_exthdrs_fail: 1268 addrconf_cleanup(); 1269 addrconf_fail: 1270 ipv6_anycast_cleanup(); 1271 ipv6_anycast_fail: 1272 ip6_flowlabel_cleanup(); 1273 ip6_flowlabel_fail: 1274 ndisc_late_cleanup(); 1275 ndisc_late_fail: 1276 ip6_route_cleanup(); 1277 ip6_route_fail: 1278 #ifdef CONFIG_PROC_FS 1279 if6_proc_exit(); 1280 proc_if6_fail: 1281 ipv6_misc_proc_exit(); 1282 proc_misc6_fail: 1283 udplite6_proc_exit(); 1284 proc_udplite6_fail: 1285 raw6_proc_exit(); 1286 proc_raw6_fail: 1287 #endif 1288 ipv6_netfilter_fini(); 1289 netfilter_fail: 1290 igmp6_cleanup(); 1291 igmp_fail: 1292 ndisc_cleanup(); 1293 ndisc_fail: 1294 icmpv6_cleanup(); 1295 icmp_fail: 1296 ip6_mr_cleanup(); 1297 ipmr_fail: 1298 unregister_pernet_subsys(&inet6_net_ops); 1299 register_pernet_fail: 1300 sock_unregister(PF_INET6); 1301 rtnl_unregister_all(PF_INET6); 1302 out_sock_register_fail: 1303 rawv6_exit(); 1304 out_unregister_ping_proto: 1305 proto_unregister(&pingv6_prot); 1306 out_unregister_raw_proto: 1307 proto_unregister(&rawv6_prot); 1308 out_unregister_udplite_proto: 1309 proto_unregister(&udplitev6_prot); 1310 out_unregister_udp_proto: 1311 proto_unregister(&udpv6_prot); 1312 out_unregister_tcp_proto: 1313 proto_unregister(&tcpv6_prot); 1314 goto out; 1315 } 1316 module_init(inet6_init); 1317 1318 MODULE_ALIAS_NETPROTO(PF_INET6); 1319