1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * IPv6 BSD socket options interface 4 * Linux INET6 implementation 5 * 6 * Authors: 7 * Pedro Roque <roque@di.fc.ul.pt> 8 * 9 * Based on linux/net/ipv4/ip_sockglue.c 10 * 11 * FIXME: Make the setsockopt code POSIX compliant: That is 12 * 13 * o Truncate getsockopt returns 14 * o Return an optlen of the truncated length if need be 15 * 16 * Changes: 17 * David L Stevens <dlstevens@us.ibm.com>: 18 * - added multicast source filtering API for MLDv2 19 */ 20 21 #include <linux/module.h> 22 #include <linux/capability.h> 23 #include <linux/errno.h> 24 #include <linux/types.h> 25 #include <linux/socket.h> 26 #include <linux/sockios.h> 27 #include <linux/net.h> 28 #include <linux/in6.h> 29 #include <linux/mroute6.h> 30 #include <linux/netdevice.h> 31 #include <linux/if_arp.h> 32 #include <linux/init.h> 33 #include <linux/sysctl.h> 34 #include <linux/netfilter.h> 35 #include <linux/slab.h> 36 37 #include <net/sock.h> 38 #include <net/snmp.h> 39 #include <net/ipv6.h> 40 #include <net/ndisc.h> 41 #include <net/protocol.h> 42 #include <net/transp_v6.h> 43 #include <net/ip6_route.h> 44 #include <net/addrconf.h> 45 #include <net/inet_common.h> 46 #include <net/tcp.h> 47 #include <net/udp.h> 48 #include <net/udplite.h> 49 #include <net/xfrm.h> 50 #include <net/compat.h> 51 #include <net/seg6.h> 52 53 #include <linux/uaccess.h> 54 55 struct ip6_ra_chain *ip6_ra_chain; 56 DEFINE_RWLOCK(ip6_ra_lock); 57 58 DEFINE_STATIC_KEY_FALSE(ip6_min_hopcount); 59 60 int ip6_ra_control(struct sock *sk, int sel) 61 { 62 struct ip6_ra_chain *ra, *new_ra, **rap; 63 64 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */ 65 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW) 66 return -ENOPROTOOPT; 67 68 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; 69 if (sel >= 0 && !new_ra) 70 return -ENOMEM; 71 72 write_lock_bh(&ip6_ra_lock); 73 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) { 74 if (ra->sk == sk) { 75 if (sel >= 0) { 76 write_unlock_bh(&ip6_ra_lock); 77 kfree(new_ra); 78 return -EADDRINUSE; 79 } 80 81 *rap = ra->next; 82 write_unlock_bh(&ip6_ra_lock); 83 84 sock_put(sk); 85 kfree(ra); 86 return 0; 87 } 88 } 89 if (!new_ra) { 90 write_unlock_bh(&ip6_ra_lock); 91 return -ENOBUFS; 92 } 93 new_ra->sk = sk; 94 new_ra->sel = sel; 95 new_ra->next = ra; 96 *rap = new_ra; 97 sock_hold(sk); 98 write_unlock_bh(&ip6_ra_lock); 99 return 0; 100 } 101 102 struct ipv6_txoptions *ipv6_update_options(struct sock *sk, 103 struct ipv6_txoptions *opt) 104 { 105 if (inet_test_bit(IS_ICSK, sk)) { 106 if (opt && 107 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && 108 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) { 109 struct inet_connection_sock *icsk = inet_csk(sk); 110 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen; 111 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); 112 } 113 } 114 opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt, 115 opt); 116 sk_dst_reset(sk); 117 118 return opt; 119 } 120 121 static bool setsockopt_needs_rtnl(int optname) 122 { 123 switch (optname) { 124 case IPV6_ADDRFORM: 125 case IPV6_ADD_MEMBERSHIP: 126 case IPV6_DROP_MEMBERSHIP: 127 case IPV6_JOIN_ANYCAST: 128 case IPV6_LEAVE_ANYCAST: 129 case MCAST_JOIN_GROUP: 130 case MCAST_LEAVE_GROUP: 131 case MCAST_JOIN_SOURCE_GROUP: 132 case MCAST_LEAVE_SOURCE_GROUP: 133 case MCAST_BLOCK_SOURCE: 134 case MCAST_UNBLOCK_SOURCE: 135 case MCAST_MSFILTER: 136 return true; 137 } 138 return false; 139 } 140 141 static int copy_group_source_from_sockptr(struct group_source_req *greqs, 142 sockptr_t optval, int optlen) 143 { 144 if (in_compat_syscall()) { 145 struct compat_group_source_req gr32; 146 147 if (optlen < sizeof(gr32)) 148 return -EINVAL; 149 if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) 150 return -EFAULT; 151 greqs->gsr_interface = gr32.gsr_interface; 152 greqs->gsr_group = gr32.gsr_group; 153 greqs->gsr_source = gr32.gsr_source; 154 } else { 155 if (optlen < sizeof(*greqs)) 156 return -EINVAL; 157 if (copy_from_sockptr(greqs, optval, sizeof(*greqs))) 158 return -EFAULT; 159 } 160 161 return 0; 162 } 163 164 static int do_ipv6_mcast_group_source(struct sock *sk, int optname, 165 sockptr_t optval, int optlen) 166 { 167 struct group_source_req greqs; 168 int omode, add; 169 int ret; 170 171 ret = copy_group_source_from_sockptr(&greqs, optval, optlen); 172 if (ret) 173 return ret; 174 175 if (greqs.gsr_group.ss_family != AF_INET6 || 176 greqs.gsr_source.ss_family != AF_INET6) 177 return -EADDRNOTAVAIL; 178 179 if (optname == MCAST_BLOCK_SOURCE) { 180 omode = MCAST_EXCLUDE; 181 add = 1; 182 } else if (optname == MCAST_UNBLOCK_SOURCE) { 183 omode = MCAST_EXCLUDE; 184 add = 0; 185 } else if (optname == MCAST_JOIN_SOURCE_GROUP) { 186 struct sockaddr_in6 *psin6; 187 int retv; 188 189 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; 190 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface, 191 &psin6->sin6_addr, 192 MCAST_INCLUDE); 193 /* prior join w/ different source is ok */ 194 if (retv && retv != -EADDRINUSE) 195 return retv; 196 omode = MCAST_INCLUDE; 197 add = 1; 198 } else /* MCAST_LEAVE_SOURCE_GROUP */ { 199 omode = MCAST_INCLUDE; 200 add = 0; 201 } 202 return ip6_mc_source(add, omode, sk, &greqs); 203 } 204 205 static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, 206 int optlen) 207 { 208 struct group_filter *gsf; 209 int ret; 210 211 if (optlen < GROUP_FILTER_SIZE(0)) 212 return -EINVAL; 213 if (optlen > READ_ONCE(sysctl_optmem_max)) 214 return -ENOBUFS; 215 216 gsf = memdup_sockptr(optval, optlen); 217 if (IS_ERR(gsf)) 218 return PTR_ERR(gsf); 219 220 /* numsrc >= (4G-140)/128 overflow in 32 bits */ 221 ret = -ENOBUFS; 222 if (gsf->gf_numsrc >= 0x1ffffffU || 223 gsf->gf_numsrc > sysctl_mld_max_msf) 224 goto out_free_gsf; 225 226 ret = -EINVAL; 227 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) 228 goto out_free_gsf; 229 230 ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex); 231 out_free_gsf: 232 kfree(gsf); 233 return ret; 234 } 235 236 static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, 237 int optlen) 238 { 239 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); 240 struct compat_group_filter *gf32; 241 void *p; 242 int ret; 243 int n; 244 245 if (optlen < size0) 246 return -EINVAL; 247 if (optlen > READ_ONCE(sysctl_optmem_max) - 4) 248 return -ENOBUFS; 249 250 p = kmalloc(optlen + 4, GFP_KERNEL); 251 if (!p) 252 return -ENOMEM; 253 254 gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */ 255 ret = -EFAULT; 256 if (copy_from_sockptr(gf32, optval, optlen)) 257 goto out_free_p; 258 259 /* numsrc >= (4G-140)/128 overflow in 32 bits */ 260 ret = -ENOBUFS; 261 n = gf32->gf_numsrc; 262 if (n >= 0x1ffffffU || n > sysctl_mld_max_msf) 263 goto out_free_p; 264 265 ret = -EINVAL; 266 if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen) 267 goto out_free_p; 268 269 ret = ip6_mc_msfilter(sk, &(struct group_filter){ 270 .gf_interface = gf32->gf_interface, 271 .gf_group = gf32->gf_group, 272 .gf_fmode = gf32->gf_fmode, 273 .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex); 274 275 out_free_p: 276 kfree(p); 277 return ret; 278 } 279 280 static int ipv6_mcast_join_leave(struct sock *sk, int optname, 281 sockptr_t optval, int optlen) 282 { 283 struct sockaddr_in6 *psin6; 284 struct group_req greq; 285 286 if (optlen < sizeof(greq)) 287 return -EINVAL; 288 if (copy_from_sockptr(&greq, optval, sizeof(greq))) 289 return -EFAULT; 290 291 if (greq.gr_group.ss_family != AF_INET6) 292 return -EADDRNOTAVAIL; 293 psin6 = (struct sockaddr_in6 *)&greq.gr_group; 294 if (optname == MCAST_JOIN_GROUP) 295 return ipv6_sock_mc_join(sk, greq.gr_interface, 296 &psin6->sin6_addr); 297 return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr); 298 } 299 300 static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname, 301 sockptr_t optval, int optlen) 302 { 303 struct compat_group_req gr32; 304 struct sockaddr_in6 *psin6; 305 306 if (optlen < sizeof(gr32)) 307 return -EINVAL; 308 if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) 309 return -EFAULT; 310 311 if (gr32.gr_group.ss_family != AF_INET6) 312 return -EADDRNOTAVAIL; 313 psin6 = (struct sockaddr_in6 *)&gr32.gr_group; 314 if (optname == MCAST_JOIN_GROUP) 315 return ipv6_sock_mc_join(sk, gr32.gr_interface, 316 &psin6->sin6_addr); 317 return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr); 318 } 319 320 static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval, 321 int optlen) 322 { 323 struct ipv6_pinfo *np = inet6_sk(sk); 324 struct ipv6_opt_hdr *new = NULL; 325 struct net *net = sock_net(sk); 326 struct ipv6_txoptions *opt; 327 int err; 328 329 /* hop-by-hop / destination options are privileged option */ 330 if (optname != IPV6_RTHDR && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW)) 331 return -EPERM; 332 333 /* remove any sticky options header with a zero option 334 * length, per RFC3542. 335 */ 336 if (optlen > 0) { 337 if (sockptr_is_null(optval)) 338 return -EINVAL; 339 if (optlen < sizeof(struct ipv6_opt_hdr) || 340 optlen & 0x7 || 341 optlen > 8 * 255) 342 return -EINVAL; 343 344 new = memdup_sockptr(optval, optlen); 345 if (IS_ERR(new)) 346 return PTR_ERR(new); 347 if (unlikely(ipv6_optlen(new) > optlen)) { 348 kfree(new); 349 return -EINVAL; 350 } 351 } 352 353 opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk)); 354 opt = ipv6_renew_options(sk, opt, optname, new); 355 kfree(new); 356 if (IS_ERR(opt)) 357 return PTR_ERR(opt); 358 359 /* routing header option needs extra check */ 360 err = -EINVAL; 361 if (optname == IPV6_RTHDR && opt && opt->srcrt) { 362 struct ipv6_rt_hdr *rthdr = opt->srcrt; 363 switch (rthdr->type) { 364 #if IS_ENABLED(CONFIG_IPV6_MIP6) 365 case IPV6_SRCRT_TYPE_2: 366 if (rthdr->hdrlen != 2 || rthdr->segments_left != 1) 367 goto sticky_done; 368 break; 369 #endif 370 case IPV6_SRCRT_TYPE_4: 371 { 372 struct ipv6_sr_hdr *srh = 373 (struct ipv6_sr_hdr *)opt->srcrt; 374 375 if (!seg6_validate_srh(srh, optlen, false)) 376 goto sticky_done; 377 break; 378 } 379 default: 380 goto sticky_done; 381 } 382 } 383 384 err = 0; 385 opt = ipv6_update_options(sk, opt); 386 sticky_done: 387 if (opt) { 388 atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 389 txopt_put(opt); 390 } 391 return err; 392 } 393 394 int do_ipv6_setsockopt(struct sock *sk, int level, int optname, 395 sockptr_t optval, unsigned int optlen) 396 { 397 struct ipv6_pinfo *np = inet6_sk(sk); 398 struct net *net = sock_net(sk); 399 int val, valbool; 400 int retv = -ENOPROTOOPT; 401 bool needs_rtnl = setsockopt_needs_rtnl(optname); 402 403 if (sockptr_is_null(optval)) 404 val = 0; 405 else { 406 if (optlen >= sizeof(int)) { 407 if (copy_from_sockptr(&val, optval, sizeof(val))) 408 return -EFAULT; 409 } else 410 val = 0; 411 } 412 413 valbool = (val != 0); 414 415 if (ip6_mroute_opt(optname)) 416 return ip6_mroute_setsockopt(sk, optname, optval, optlen); 417 418 if (needs_rtnl) 419 rtnl_lock(); 420 sockopt_lock_sock(sk); 421 422 /* Another thread has converted the socket into IPv4 with 423 * IPV6_ADDRFORM concurrently. 424 */ 425 if (unlikely(sk->sk_family != AF_INET6)) 426 goto unlock; 427 428 switch (optname) { 429 430 case IPV6_ADDRFORM: 431 if (optlen < sizeof(int)) 432 goto e_inval; 433 if (val == PF_INET) { 434 if (sk->sk_type == SOCK_RAW) 435 break; 436 437 if (sk->sk_protocol == IPPROTO_UDP || 438 sk->sk_protocol == IPPROTO_UDPLITE) { 439 struct udp_sock *up = udp_sk(sk); 440 if (up->pending == AF_INET6) { 441 retv = -EBUSY; 442 break; 443 } 444 } else if (sk->sk_protocol == IPPROTO_TCP) { 445 if (sk->sk_prot != &tcpv6_prot) { 446 retv = -EBUSY; 447 break; 448 } 449 } else { 450 break; 451 } 452 453 if (sk->sk_state != TCP_ESTABLISHED) { 454 retv = -ENOTCONN; 455 break; 456 } 457 458 if (ipv6_only_sock(sk) || 459 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { 460 retv = -EADDRNOTAVAIL; 461 break; 462 } 463 464 __ipv6_sock_mc_close(sk); 465 __ipv6_sock_ac_close(sk); 466 467 if (sk->sk_protocol == IPPROTO_TCP) { 468 struct inet_connection_sock *icsk = inet_csk(sk); 469 470 sock_prot_inuse_add(net, sk->sk_prot, -1); 471 sock_prot_inuse_add(net, &tcp_prot, 1); 472 473 /* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */ 474 WRITE_ONCE(sk->sk_prot, &tcp_prot); 475 /* Paired with READ_ONCE() in tcp_(get|set)sockopt() */ 476 WRITE_ONCE(icsk->icsk_af_ops, &ipv4_specific); 477 WRITE_ONCE(sk->sk_socket->ops, &inet_stream_ops); 478 WRITE_ONCE(sk->sk_family, PF_INET); 479 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 480 } else { 481 struct proto *prot = &udp_prot; 482 483 if (sk->sk_protocol == IPPROTO_UDPLITE) 484 prot = &udplite_prot; 485 486 sock_prot_inuse_add(net, sk->sk_prot, -1); 487 sock_prot_inuse_add(net, prot, 1); 488 489 /* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */ 490 WRITE_ONCE(sk->sk_prot, prot); 491 WRITE_ONCE(sk->sk_socket->ops, &inet_dgram_ops); 492 WRITE_ONCE(sk->sk_family, PF_INET); 493 } 494 495 /* Disable all options not to allocate memory anymore, 496 * but there is still a race. See the lockless path 497 * in udpv6_sendmsg() and ipv6_local_rxpmtu(). 498 */ 499 np->rxopt.all = 0; 500 501 inet6_cleanup_sock(sk); 502 503 module_put(THIS_MODULE); 504 retv = 0; 505 break; 506 } 507 goto e_inval; 508 509 case IPV6_V6ONLY: 510 if (optlen < sizeof(int) || 511 inet_sk(sk)->inet_num) 512 goto e_inval; 513 sk->sk_ipv6only = valbool; 514 retv = 0; 515 break; 516 517 case IPV6_RECVPKTINFO: 518 if (optlen < sizeof(int)) 519 goto e_inval; 520 np->rxopt.bits.rxinfo = valbool; 521 retv = 0; 522 break; 523 524 case IPV6_2292PKTINFO: 525 if (optlen < sizeof(int)) 526 goto e_inval; 527 np->rxopt.bits.rxoinfo = valbool; 528 retv = 0; 529 break; 530 531 case IPV6_RECVHOPLIMIT: 532 if (optlen < sizeof(int)) 533 goto e_inval; 534 np->rxopt.bits.rxhlim = valbool; 535 retv = 0; 536 break; 537 538 case IPV6_2292HOPLIMIT: 539 if (optlen < sizeof(int)) 540 goto e_inval; 541 np->rxopt.bits.rxohlim = valbool; 542 retv = 0; 543 break; 544 545 case IPV6_RECVRTHDR: 546 if (optlen < sizeof(int)) 547 goto e_inval; 548 np->rxopt.bits.srcrt = valbool; 549 retv = 0; 550 break; 551 552 case IPV6_2292RTHDR: 553 if (optlen < sizeof(int)) 554 goto e_inval; 555 np->rxopt.bits.osrcrt = valbool; 556 retv = 0; 557 break; 558 559 case IPV6_RECVHOPOPTS: 560 if (optlen < sizeof(int)) 561 goto e_inval; 562 np->rxopt.bits.hopopts = valbool; 563 retv = 0; 564 break; 565 566 case IPV6_2292HOPOPTS: 567 if (optlen < sizeof(int)) 568 goto e_inval; 569 np->rxopt.bits.ohopopts = valbool; 570 retv = 0; 571 break; 572 573 case IPV6_RECVDSTOPTS: 574 if (optlen < sizeof(int)) 575 goto e_inval; 576 np->rxopt.bits.dstopts = valbool; 577 retv = 0; 578 break; 579 580 case IPV6_2292DSTOPTS: 581 if (optlen < sizeof(int)) 582 goto e_inval; 583 np->rxopt.bits.odstopts = valbool; 584 retv = 0; 585 break; 586 587 case IPV6_TCLASS: 588 if (optlen < sizeof(int)) 589 goto e_inval; 590 if (val < -1 || val > 0xff) 591 goto e_inval; 592 /* RFC 3542, 6.5: default traffic class of 0x0 */ 593 if (val == -1) 594 val = 0; 595 if (sk->sk_type == SOCK_STREAM) { 596 val &= ~INET_ECN_MASK; 597 val |= np->tclass & INET_ECN_MASK; 598 } 599 if (np->tclass != val) { 600 np->tclass = val; 601 sk_dst_reset(sk); 602 } 603 retv = 0; 604 break; 605 606 case IPV6_RECVTCLASS: 607 if (optlen < sizeof(int)) 608 goto e_inval; 609 np->rxopt.bits.rxtclass = valbool; 610 retv = 0; 611 break; 612 613 case IPV6_FLOWINFO: 614 if (optlen < sizeof(int)) 615 goto e_inval; 616 np->rxopt.bits.rxflow = valbool; 617 retv = 0; 618 break; 619 620 case IPV6_RECVPATHMTU: 621 if (optlen < sizeof(int)) 622 goto e_inval; 623 np->rxopt.bits.rxpmtu = valbool; 624 retv = 0; 625 break; 626 627 case IPV6_TRANSPARENT: 628 if (valbool && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW) && 629 !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) { 630 retv = -EPERM; 631 break; 632 } 633 if (optlen < sizeof(int)) 634 goto e_inval; 635 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */ 636 inet_assign_bit(TRANSPARENT, sk, valbool); 637 retv = 0; 638 break; 639 640 case IPV6_FREEBIND: 641 if (optlen < sizeof(int)) 642 goto e_inval; 643 /* we also don't have a separate freebind bit for IPV6 */ 644 inet_assign_bit(FREEBIND, sk, valbool); 645 retv = 0; 646 break; 647 648 case IPV6_RECVORIGDSTADDR: 649 if (optlen < sizeof(int)) 650 goto e_inval; 651 np->rxopt.bits.rxorigdstaddr = valbool; 652 retv = 0; 653 break; 654 655 case IPV6_HOPOPTS: 656 case IPV6_RTHDRDSTOPTS: 657 case IPV6_RTHDR: 658 case IPV6_DSTOPTS: 659 retv = ipv6_set_opt_hdr(sk, optname, optval, optlen); 660 break; 661 662 case IPV6_PKTINFO: 663 { 664 struct in6_pktinfo pkt; 665 666 if (optlen == 0) 667 goto e_inval; 668 else if (optlen < sizeof(struct in6_pktinfo) || 669 sockptr_is_null(optval)) 670 goto e_inval; 671 672 if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) { 673 retv = -EFAULT; 674 break; 675 } 676 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex)) 677 goto e_inval; 678 679 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex; 680 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr; 681 retv = 0; 682 break; 683 } 684 685 case IPV6_2292PKTOPTIONS: 686 { 687 struct ipv6_txoptions *opt = NULL; 688 struct msghdr msg; 689 struct flowi6 fl6; 690 struct ipcm6_cookie ipc6; 691 692 memset(&fl6, 0, sizeof(fl6)); 693 fl6.flowi6_oif = sk->sk_bound_dev_if; 694 fl6.flowi6_mark = sk->sk_mark; 695 696 if (optlen == 0) 697 goto update; 698 699 /* 1K is probably excessive 700 * 1K is surely not enough, 2K per standard header is 16K. 701 */ 702 retv = -EINVAL; 703 if (optlen > 64*1024) 704 break; 705 706 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); 707 retv = -ENOBUFS; 708 if (!opt) 709 break; 710 711 memset(opt, 0, sizeof(*opt)); 712 refcount_set(&opt->refcnt, 1); 713 opt->tot_len = sizeof(*opt) + optlen; 714 retv = -EFAULT; 715 if (copy_from_sockptr(opt + 1, optval, optlen)) 716 goto done; 717 718 msg.msg_controllen = optlen; 719 msg.msg_control_is_user = false; 720 msg.msg_control = (void *)(opt+1); 721 ipc6.opt = opt; 722 723 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6); 724 if (retv) 725 goto done; 726 update: 727 retv = 0; 728 opt = ipv6_update_options(sk, opt); 729 done: 730 if (opt) { 731 atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 732 txopt_put(opt); 733 } 734 break; 735 } 736 case IPV6_UNICAST_HOPS: 737 if (optlen < sizeof(int)) 738 goto e_inval; 739 if (val > 255 || val < -1) 740 goto e_inval; 741 np->hop_limit = val; 742 retv = 0; 743 break; 744 745 case IPV6_MULTICAST_HOPS: 746 if (sk->sk_type == SOCK_STREAM) 747 break; 748 if (optlen < sizeof(int)) 749 goto e_inval; 750 if (val > 255 || val < -1) 751 goto e_inval; 752 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val); 753 retv = 0; 754 break; 755 756 case IPV6_MULTICAST_LOOP: 757 if (optlen < sizeof(int)) 758 goto e_inval; 759 if (val != valbool) 760 goto e_inval; 761 np->mc_loop = valbool; 762 retv = 0; 763 break; 764 765 case IPV6_UNICAST_IF: 766 { 767 struct net_device *dev = NULL; 768 int ifindex; 769 770 if (optlen != sizeof(int)) 771 goto e_inval; 772 773 ifindex = (__force int)ntohl((__force __be32)val); 774 if (ifindex == 0) { 775 np->ucast_oif = 0; 776 retv = 0; 777 break; 778 } 779 780 dev = dev_get_by_index(net, ifindex); 781 retv = -EADDRNOTAVAIL; 782 if (!dev) 783 break; 784 dev_put(dev); 785 786 retv = -EINVAL; 787 if (sk->sk_bound_dev_if) 788 break; 789 790 np->ucast_oif = ifindex; 791 retv = 0; 792 break; 793 } 794 795 case IPV6_MULTICAST_IF: 796 if (sk->sk_type == SOCK_STREAM) 797 break; 798 if (optlen < sizeof(int)) 799 goto e_inval; 800 801 if (val) { 802 struct net_device *dev; 803 int midx; 804 805 rcu_read_lock(); 806 807 dev = dev_get_by_index_rcu(net, val); 808 if (!dev) { 809 rcu_read_unlock(); 810 retv = -ENODEV; 811 break; 812 } 813 midx = l3mdev_master_ifindex_rcu(dev); 814 815 rcu_read_unlock(); 816 817 if (sk->sk_bound_dev_if && 818 sk->sk_bound_dev_if != val && 819 (!midx || midx != sk->sk_bound_dev_if)) 820 goto e_inval; 821 } 822 np->mcast_oif = val; 823 retv = 0; 824 break; 825 case IPV6_ADD_MEMBERSHIP: 826 case IPV6_DROP_MEMBERSHIP: 827 { 828 struct ipv6_mreq mreq; 829 830 if (optlen < sizeof(struct ipv6_mreq)) 831 goto e_inval; 832 833 retv = -EPROTO; 834 if (inet_test_bit(IS_ICSK, sk)) 835 break; 836 837 retv = -EFAULT; 838 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq))) 839 break; 840 841 if (optname == IPV6_ADD_MEMBERSHIP) 842 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); 843 else 844 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); 845 break; 846 } 847 case IPV6_JOIN_ANYCAST: 848 case IPV6_LEAVE_ANYCAST: 849 { 850 struct ipv6_mreq mreq; 851 852 if (optlen < sizeof(struct ipv6_mreq)) 853 goto e_inval; 854 855 retv = -EFAULT; 856 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq))) 857 break; 858 859 if (optname == IPV6_JOIN_ANYCAST) 860 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); 861 else 862 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); 863 break; 864 } 865 case IPV6_MULTICAST_ALL: 866 if (optlen < sizeof(int)) 867 goto e_inval; 868 np->mc_all = valbool; 869 retv = 0; 870 break; 871 872 case MCAST_JOIN_GROUP: 873 case MCAST_LEAVE_GROUP: 874 if (in_compat_syscall()) 875 retv = compat_ipv6_mcast_join_leave(sk, optname, optval, 876 optlen); 877 else 878 retv = ipv6_mcast_join_leave(sk, optname, optval, 879 optlen); 880 break; 881 case MCAST_JOIN_SOURCE_GROUP: 882 case MCAST_LEAVE_SOURCE_GROUP: 883 case MCAST_BLOCK_SOURCE: 884 case MCAST_UNBLOCK_SOURCE: 885 retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen); 886 break; 887 case MCAST_MSFILTER: 888 if (in_compat_syscall()) 889 retv = compat_ipv6_set_mcast_msfilter(sk, optval, 890 optlen); 891 else 892 retv = ipv6_set_mcast_msfilter(sk, optval, optlen); 893 break; 894 case IPV6_ROUTER_ALERT: 895 if (optlen < sizeof(int)) 896 goto e_inval; 897 retv = ip6_ra_control(sk, val); 898 break; 899 case IPV6_ROUTER_ALERT_ISOLATE: 900 if (optlen < sizeof(int)) 901 goto e_inval; 902 np->rtalert_isolate = valbool; 903 retv = 0; 904 break; 905 case IPV6_MTU_DISCOVER: 906 if (optlen < sizeof(int)) 907 goto e_inval; 908 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT) 909 goto e_inval; 910 np->pmtudisc = val; 911 retv = 0; 912 break; 913 case IPV6_MTU: 914 if (optlen < sizeof(int)) 915 goto e_inval; 916 if (val && val < IPV6_MIN_MTU) 917 goto e_inval; 918 np->frag_size = val; 919 retv = 0; 920 break; 921 case IPV6_RECVERR: 922 if (optlen < sizeof(int)) 923 goto e_inval; 924 np->recverr = valbool; 925 if (!val) 926 skb_errqueue_purge(&sk->sk_error_queue); 927 retv = 0; 928 break; 929 case IPV6_FLOWINFO_SEND: 930 if (optlen < sizeof(int)) 931 goto e_inval; 932 np->sndflow = valbool; 933 retv = 0; 934 break; 935 case IPV6_FLOWLABEL_MGR: 936 retv = ipv6_flowlabel_opt(sk, optval, optlen); 937 break; 938 case IPV6_IPSEC_POLICY: 939 case IPV6_XFRM_POLICY: 940 retv = -EPERM; 941 if (!sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) 942 break; 943 retv = xfrm_user_policy(sk, optname, optval, optlen); 944 break; 945 946 case IPV6_ADDR_PREFERENCES: 947 if (optlen < sizeof(int)) 948 goto e_inval; 949 retv = __ip6_sock_set_addr_preferences(sk, val); 950 break; 951 case IPV6_MINHOPCOUNT: 952 if (optlen < sizeof(int)) 953 goto e_inval; 954 if (val < 0 || val > 255) 955 goto e_inval; 956 957 if (val) 958 static_branch_enable(&ip6_min_hopcount); 959 960 /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount 961 * while we are changing it. 962 */ 963 WRITE_ONCE(np->min_hopcount, val); 964 retv = 0; 965 break; 966 case IPV6_DONTFRAG: 967 np->dontfrag = valbool; 968 retv = 0; 969 break; 970 case IPV6_AUTOFLOWLABEL: 971 np->autoflowlabel = valbool; 972 np->autoflowlabel_set = 1; 973 retv = 0; 974 break; 975 case IPV6_RECVFRAGSIZE: 976 np->rxopt.bits.recvfragsize = valbool; 977 retv = 0; 978 break; 979 case IPV6_RECVERR_RFC4884: 980 if (optlen < sizeof(int)) 981 goto e_inval; 982 if (val < 0 || val > 1) 983 goto e_inval; 984 np->recverr_rfc4884 = valbool; 985 retv = 0; 986 break; 987 } 988 989 unlock: 990 sockopt_release_sock(sk); 991 if (needs_rtnl) 992 rtnl_unlock(); 993 994 return retv; 995 996 e_inval: 997 retv = -EINVAL; 998 goto unlock; 999 } 1000 1001 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 1002 unsigned int optlen) 1003 { 1004 int err; 1005 1006 if (level == SOL_IP && sk->sk_type != SOCK_RAW) 1007 return udp_prot.setsockopt(sk, level, optname, optval, optlen); 1008 1009 if (level != SOL_IPV6) 1010 return -ENOPROTOOPT; 1011 1012 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); 1013 #ifdef CONFIG_NETFILTER 1014 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1015 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && 1016 optname != IPV6_XFRM_POLICY) 1017 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen); 1018 #endif 1019 return err; 1020 } 1021 EXPORT_SYMBOL(ipv6_setsockopt); 1022 1023 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt, 1024 int optname, sockptr_t optval, int len) 1025 { 1026 struct ipv6_opt_hdr *hdr; 1027 1028 if (!opt) 1029 return 0; 1030 1031 switch (optname) { 1032 case IPV6_HOPOPTS: 1033 hdr = opt->hopopt; 1034 break; 1035 case IPV6_RTHDRDSTOPTS: 1036 hdr = opt->dst0opt; 1037 break; 1038 case IPV6_RTHDR: 1039 hdr = (struct ipv6_opt_hdr *)opt->srcrt; 1040 break; 1041 case IPV6_DSTOPTS: 1042 hdr = opt->dst1opt; 1043 break; 1044 default: 1045 return -EINVAL; /* should not happen */ 1046 } 1047 1048 if (!hdr) 1049 return 0; 1050 1051 len = min_t(unsigned int, len, ipv6_optlen(hdr)); 1052 if (copy_to_sockptr(optval, hdr, len)) 1053 return -EFAULT; 1054 return len; 1055 } 1056 1057 static int ipv6_get_msfilter(struct sock *sk, sockptr_t optval, 1058 sockptr_t optlen, int len) 1059 { 1060 const int size0 = offsetof(struct group_filter, gf_slist_flex); 1061 struct group_filter gsf; 1062 int num; 1063 int err; 1064 1065 if (len < size0) 1066 return -EINVAL; 1067 if (copy_from_sockptr(&gsf, optval, size0)) 1068 return -EFAULT; 1069 if (gsf.gf_group.ss_family != AF_INET6) 1070 return -EADDRNOTAVAIL; 1071 num = gsf.gf_numsrc; 1072 sockopt_lock_sock(sk); 1073 err = ip6_mc_msfget(sk, &gsf, optval, size0); 1074 if (!err) { 1075 if (num > gsf.gf_numsrc) 1076 num = gsf.gf_numsrc; 1077 len = GROUP_FILTER_SIZE(num); 1078 if (copy_to_sockptr(optlen, &len, sizeof(int)) || 1079 copy_to_sockptr(optval, &gsf, size0)) 1080 err = -EFAULT; 1081 } 1082 sockopt_release_sock(sk); 1083 return err; 1084 } 1085 1086 static int compat_ipv6_get_msfilter(struct sock *sk, sockptr_t optval, 1087 sockptr_t optlen, int len) 1088 { 1089 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); 1090 struct compat_group_filter gf32; 1091 struct group_filter gf; 1092 int err; 1093 int num; 1094 1095 if (len < size0) 1096 return -EINVAL; 1097 1098 if (copy_from_sockptr(&gf32, optval, size0)) 1099 return -EFAULT; 1100 gf.gf_interface = gf32.gf_interface; 1101 gf.gf_fmode = gf32.gf_fmode; 1102 num = gf.gf_numsrc = gf32.gf_numsrc; 1103 gf.gf_group = gf32.gf_group; 1104 1105 if (gf.gf_group.ss_family != AF_INET6) 1106 return -EADDRNOTAVAIL; 1107 1108 sockopt_lock_sock(sk); 1109 err = ip6_mc_msfget(sk, &gf, optval, size0); 1110 sockopt_release_sock(sk); 1111 if (err) 1112 return err; 1113 if (num > gf.gf_numsrc) 1114 num = gf.gf_numsrc; 1115 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32)); 1116 if (copy_to_sockptr(optlen, &len, sizeof(int)) || 1117 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode), 1118 &gf.gf_fmode, sizeof(gf32.gf_fmode)) || 1119 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc), 1120 &gf.gf_numsrc, sizeof(gf32.gf_numsrc))) 1121 return -EFAULT; 1122 return 0; 1123 } 1124 1125 int do_ipv6_getsockopt(struct sock *sk, int level, int optname, 1126 sockptr_t optval, sockptr_t optlen) 1127 { 1128 struct ipv6_pinfo *np = inet6_sk(sk); 1129 int len; 1130 int val; 1131 1132 if (ip6_mroute_opt(optname)) 1133 return ip6_mroute_getsockopt(sk, optname, optval, optlen); 1134 1135 if (copy_from_sockptr(&len, optlen, sizeof(int))) 1136 return -EFAULT; 1137 switch (optname) { 1138 case IPV6_ADDRFORM: 1139 if (sk->sk_protocol != IPPROTO_UDP && 1140 sk->sk_protocol != IPPROTO_UDPLITE && 1141 sk->sk_protocol != IPPROTO_TCP) 1142 return -ENOPROTOOPT; 1143 if (sk->sk_state != TCP_ESTABLISHED) 1144 return -ENOTCONN; 1145 val = sk->sk_family; 1146 break; 1147 case MCAST_MSFILTER: 1148 if (in_compat_syscall()) 1149 return compat_ipv6_get_msfilter(sk, optval, optlen, len); 1150 return ipv6_get_msfilter(sk, optval, optlen, len); 1151 case IPV6_2292PKTOPTIONS: 1152 { 1153 struct msghdr msg; 1154 struct sk_buff *skb; 1155 1156 if (sk->sk_type != SOCK_STREAM) 1157 return -ENOPROTOOPT; 1158 1159 if (optval.is_kernel) { 1160 msg.msg_control_is_user = false; 1161 msg.msg_control = optval.kernel; 1162 } else { 1163 msg.msg_control_is_user = true; 1164 msg.msg_control_user = optval.user; 1165 } 1166 msg.msg_controllen = len; 1167 msg.msg_flags = 0; 1168 1169 sockopt_lock_sock(sk); 1170 skb = np->pktoptions; 1171 if (skb) 1172 ip6_datagram_recv_ctl(sk, &msg, skb); 1173 sockopt_release_sock(sk); 1174 if (!skb) { 1175 if (np->rxopt.bits.rxinfo) { 1176 struct in6_pktinfo src_info; 1177 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : 1178 np->sticky_pktinfo.ipi6_ifindex; 1179 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr; 1180 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); 1181 } 1182 if (np->rxopt.bits.rxhlim) { 1183 int hlim = np->mcast_hops; 1184 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); 1185 } 1186 if (np->rxopt.bits.rxtclass) { 1187 int tclass = (int)ip6_tclass(np->rcv_flowinfo); 1188 1189 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); 1190 } 1191 if (np->rxopt.bits.rxoinfo) { 1192 struct in6_pktinfo src_info; 1193 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : 1194 np->sticky_pktinfo.ipi6_ifindex; 1195 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : 1196 np->sticky_pktinfo.ipi6_addr; 1197 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); 1198 } 1199 if (np->rxopt.bits.rxohlim) { 1200 int hlim = np->mcast_hops; 1201 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); 1202 } 1203 if (np->rxopt.bits.rxflow) { 1204 __be32 flowinfo = np->rcv_flowinfo; 1205 1206 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); 1207 } 1208 } 1209 len -= msg.msg_controllen; 1210 return copy_to_sockptr(optlen, &len, sizeof(int)); 1211 } 1212 case IPV6_MTU: 1213 { 1214 struct dst_entry *dst; 1215 1216 val = 0; 1217 rcu_read_lock(); 1218 dst = __sk_dst_get(sk); 1219 if (dst) 1220 val = dst_mtu(dst); 1221 rcu_read_unlock(); 1222 if (!val) 1223 return -ENOTCONN; 1224 break; 1225 } 1226 1227 case IPV6_V6ONLY: 1228 val = sk->sk_ipv6only; 1229 break; 1230 1231 case IPV6_RECVPKTINFO: 1232 val = np->rxopt.bits.rxinfo; 1233 break; 1234 1235 case IPV6_2292PKTINFO: 1236 val = np->rxopt.bits.rxoinfo; 1237 break; 1238 1239 case IPV6_RECVHOPLIMIT: 1240 val = np->rxopt.bits.rxhlim; 1241 break; 1242 1243 case IPV6_2292HOPLIMIT: 1244 val = np->rxopt.bits.rxohlim; 1245 break; 1246 1247 case IPV6_RECVRTHDR: 1248 val = np->rxopt.bits.srcrt; 1249 break; 1250 1251 case IPV6_2292RTHDR: 1252 val = np->rxopt.bits.osrcrt; 1253 break; 1254 1255 case IPV6_HOPOPTS: 1256 case IPV6_RTHDRDSTOPTS: 1257 case IPV6_RTHDR: 1258 case IPV6_DSTOPTS: 1259 { 1260 struct ipv6_txoptions *opt; 1261 1262 sockopt_lock_sock(sk); 1263 opt = rcu_dereference_protected(np->opt, 1264 lockdep_sock_is_held(sk)); 1265 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len); 1266 sockopt_release_sock(sk); 1267 /* check if ipv6_getsockopt_sticky() returns err code */ 1268 if (len < 0) 1269 return len; 1270 return copy_to_sockptr(optlen, &len, sizeof(int)); 1271 } 1272 1273 case IPV6_RECVHOPOPTS: 1274 val = np->rxopt.bits.hopopts; 1275 break; 1276 1277 case IPV6_2292HOPOPTS: 1278 val = np->rxopt.bits.ohopopts; 1279 break; 1280 1281 case IPV6_RECVDSTOPTS: 1282 val = np->rxopt.bits.dstopts; 1283 break; 1284 1285 case IPV6_2292DSTOPTS: 1286 val = np->rxopt.bits.odstopts; 1287 break; 1288 1289 case IPV6_TCLASS: 1290 val = np->tclass; 1291 break; 1292 1293 case IPV6_RECVTCLASS: 1294 val = np->rxopt.bits.rxtclass; 1295 break; 1296 1297 case IPV6_FLOWINFO: 1298 val = np->rxopt.bits.rxflow; 1299 break; 1300 1301 case IPV6_RECVPATHMTU: 1302 val = np->rxopt.bits.rxpmtu; 1303 break; 1304 1305 case IPV6_PATHMTU: 1306 { 1307 struct dst_entry *dst; 1308 struct ip6_mtuinfo mtuinfo; 1309 1310 if (len < sizeof(mtuinfo)) 1311 return -EINVAL; 1312 1313 len = sizeof(mtuinfo); 1314 memset(&mtuinfo, 0, sizeof(mtuinfo)); 1315 1316 rcu_read_lock(); 1317 dst = __sk_dst_get(sk); 1318 if (dst) 1319 mtuinfo.ip6m_mtu = dst_mtu(dst); 1320 rcu_read_unlock(); 1321 if (!mtuinfo.ip6m_mtu) 1322 return -ENOTCONN; 1323 1324 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1325 return -EFAULT; 1326 if (copy_to_sockptr(optval, &mtuinfo, len)) 1327 return -EFAULT; 1328 1329 return 0; 1330 } 1331 1332 case IPV6_TRANSPARENT: 1333 val = inet_test_bit(TRANSPARENT, sk); 1334 break; 1335 1336 case IPV6_FREEBIND: 1337 val = inet_test_bit(FREEBIND, sk); 1338 break; 1339 1340 case IPV6_RECVORIGDSTADDR: 1341 val = np->rxopt.bits.rxorigdstaddr; 1342 break; 1343 1344 case IPV6_UNICAST_HOPS: 1345 case IPV6_MULTICAST_HOPS: 1346 { 1347 struct dst_entry *dst; 1348 1349 if (optname == IPV6_UNICAST_HOPS) 1350 val = np->hop_limit; 1351 else 1352 val = np->mcast_hops; 1353 1354 if (val < 0) { 1355 rcu_read_lock(); 1356 dst = __sk_dst_get(sk); 1357 if (dst) 1358 val = ip6_dst_hoplimit(dst); 1359 rcu_read_unlock(); 1360 } 1361 1362 if (val < 0) 1363 val = sock_net(sk)->ipv6.devconf_all->hop_limit; 1364 break; 1365 } 1366 1367 case IPV6_MULTICAST_LOOP: 1368 val = np->mc_loop; 1369 break; 1370 1371 case IPV6_MULTICAST_IF: 1372 val = np->mcast_oif; 1373 break; 1374 1375 case IPV6_MULTICAST_ALL: 1376 val = np->mc_all; 1377 break; 1378 1379 case IPV6_UNICAST_IF: 1380 val = (__force int)htonl((__u32) np->ucast_oif); 1381 break; 1382 1383 case IPV6_MTU_DISCOVER: 1384 val = np->pmtudisc; 1385 break; 1386 1387 case IPV6_RECVERR: 1388 val = np->recverr; 1389 break; 1390 1391 case IPV6_FLOWINFO_SEND: 1392 val = np->sndflow; 1393 break; 1394 1395 case IPV6_FLOWLABEL_MGR: 1396 { 1397 struct in6_flowlabel_req freq; 1398 int flags; 1399 1400 if (len < sizeof(freq)) 1401 return -EINVAL; 1402 1403 if (copy_from_sockptr(&freq, optval, sizeof(freq))) 1404 return -EFAULT; 1405 1406 if (freq.flr_action != IPV6_FL_A_GET) 1407 return -EINVAL; 1408 1409 len = sizeof(freq); 1410 flags = freq.flr_flags; 1411 1412 memset(&freq, 0, sizeof(freq)); 1413 1414 val = ipv6_flowlabel_opt_get(sk, &freq, flags); 1415 if (val < 0) 1416 return val; 1417 1418 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1419 return -EFAULT; 1420 if (copy_to_sockptr(optval, &freq, len)) 1421 return -EFAULT; 1422 1423 return 0; 1424 } 1425 1426 case IPV6_ADDR_PREFERENCES: 1427 val = 0; 1428 1429 if (np->srcprefs & IPV6_PREFER_SRC_TMP) 1430 val |= IPV6_PREFER_SRC_TMP; 1431 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC) 1432 val |= IPV6_PREFER_SRC_PUBLIC; 1433 else { 1434 /* XXX: should we return system default? */ 1435 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT; 1436 } 1437 1438 if (np->srcprefs & IPV6_PREFER_SRC_COA) 1439 val |= IPV6_PREFER_SRC_COA; 1440 else 1441 val |= IPV6_PREFER_SRC_HOME; 1442 break; 1443 1444 case IPV6_MINHOPCOUNT: 1445 val = np->min_hopcount; 1446 break; 1447 1448 case IPV6_DONTFRAG: 1449 val = np->dontfrag; 1450 break; 1451 1452 case IPV6_AUTOFLOWLABEL: 1453 val = ip6_autoflowlabel(sock_net(sk), np); 1454 break; 1455 1456 case IPV6_RECVFRAGSIZE: 1457 val = np->rxopt.bits.recvfragsize; 1458 break; 1459 1460 case IPV6_ROUTER_ALERT_ISOLATE: 1461 val = np->rtalert_isolate; 1462 break; 1463 1464 case IPV6_RECVERR_RFC4884: 1465 val = np->recverr_rfc4884; 1466 break; 1467 1468 default: 1469 return -ENOPROTOOPT; 1470 } 1471 len = min_t(unsigned int, sizeof(int), len); 1472 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1473 return -EFAULT; 1474 if (copy_to_sockptr(optval, &val, len)) 1475 return -EFAULT; 1476 return 0; 1477 } 1478 1479 int ipv6_getsockopt(struct sock *sk, int level, int optname, 1480 char __user *optval, int __user *optlen) 1481 { 1482 int err; 1483 1484 if (level == SOL_IP && sk->sk_type != SOCK_RAW) 1485 return udp_prot.getsockopt(sk, level, optname, optval, optlen); 1486 1487 if (level != SOL_IPV6) 1488 return -ENOPROTOOPT; 1489 1490 err = do_ipv6_getsockopt(sk, level, optname, 1491 USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); 1492 #ifdef CONFIG_NETFILTER 1493 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1494 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { 1495 int len; 1496 1497 if (get_user(len, optlen)) 1498 return -EFAULT; 1499 1500 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len); 1501 if (err >= 0) 1502 err = put_user(len, optlen); 1503 } 1504 #endif 1505 return err; 1506 } 1507 EXPORT_SYMBOL(ipv6_getsockopt); 1508