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