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_sk(sk)->is_icsk) { 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 > 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 > 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 && !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 static 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 lock_sock(sk); 421 422 switch (optname) { 423 424 case IPV6_ADDRFORM: 425 if (optlen < sizeof(int)) 426 goto e_inval; 427 if (val == PF_INET) { 428 struct ipv6_txoptions *opt; 429 struct sk_buff *pktopt; 430 431 if (sk->sk_type == SOCK_RAW) 432 break; 433 434 if (sk->sk_protocol == IPPROTO_UDP || 435 sk->sk_protocol == IPPROTO_UDPLITE) { 436 struct udp_sock *up = udp_sk(sk); 437 if (up->pending == AF_INET6) { 438 retv = -EBUSY; 439 break; 440 } 441 } else if (sk->sk_protocol == IPPROTO_TCP) { 442 if (sk->sk_prot != &tcpv6_prot) { 443 retv = -EBUSY; 444 break; 445 } 446 } else { 447 break; 448 } 449 450 if (sk->sk_state != TCP_ESTABLISHED) { 451 retv = -ENOTCONN; 452 break; 453 } 454 455 if (ipv6_only_sock(sk) || 456 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { 457 retv = -EADDRNOTAVAIL; 458 break; 459 } 460 461 fl6_free_socklist(sk); 462 __ipv6_sock_mc_close(sk); 463 __ipv6_sock_ac_close(sk); 464 465 /* 466 * Sock is moving from IPv6 to IPv4 (sk_prot), so 467 * remove it from the refcnt debug socks count in the 468 * original family... 469 */ 470 sk_refcnt_debug_dec(sk); 471 472 if (sk->sk_protocol == IPPROTO_TCP) { 473 struct inet_connection_sock *icsk = inet_csk(sk); 474 local_bh_disable(); 475 sock_prot_inuse_add(net, sk->sk_prot, -1); 476 sock_prot_inuse_add(net, &tcp_prot, 1); 477 local_bh_enable(); 478 sk->sk_prot = &tcp_prot; 479 icsk->icsk_af_ops = &ipv4_specific; 480 sk->sk_socket->ops = &inet_stream_ops; 481 sk->sk_family = PF_INET; 482 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 483 } else { 484 struct proto *prot = &udp_prot; 485 486 if (sk->sk_protocol == IPPROTO_UDPLITE) 487 prot = &udplite_prot; 488 local_bh_disable(); 489 sock_prot_inuse_add(net, sk->sk_prot, -1); 490 sock_prot_inuse_add(net, prot, 1); 491 local_bh_enable(); 492 sk->sk_prot = prot; 493 sk->sk_socket->ops = &inet_dgram_ops; 494 sk->sk_family = PF_INET; 495 } 496 opt = xchg((__force struct ipv6_txoptions **)&np->opt, 497 NULL); 498 if (opt) { 499 atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 500 txopt_put(opt); 501 } 502 pktopt = xchg(&np->pktoptions, NULL); 503 kfree_skb(pktopt); 504 505 /* 506 * ... and add it to the refcnt debug socks count 507 * in the new family. -acme 508 */ 509 sk_refcnt_debug_inc(sk); 510 module_put(THIS_MODULE); 511 retv = 0; 512 break; 513 } 514 goto e_inval; 515 516 case IPV6_V6ONLY: 517 if (optlen < sizeof(int) || 518 inet_sk(sk)->inet_num) 519 goto e_inval; 520 sk->sk_ipv6only = valbool; 521 retv = 0; 522 break; 523 524 case IPV6_RECVPKTINFO: 525 if (optlen < sizeof(int)) 526 goto e_inval; 527 np->rxopt.bits.rxinfo = valbool; 528 retv = 0; 529 break; 530 531 case IPV6_2292PKTINFO: 532 if (optlen < sizeof(int)) 533 goto e_inval; 534 np->rxopt.bits.rxoinfo = valbool; 535 retv = 0; 536 break; 537 538 case IPV6_RECVHOPLIMIT: 539 if (optlen < sizeof(int)) 540 goto e_inval; 541 np->rxopt.bits.rxhlim = valbool; 542 retv = 0; 543 break; 544 545 case IPV6_2292HOPLIMIT: 546 if (optlen < sizeof(int)) 547 goto e_inval; 548 np->rxopt.bits.rxohlim = valbool; 549 retv = 0; 550 break; 551 552 case IPV6_RECVRTHDR: 553 if (optlen < sizeof(int)) 554 goto e_inval; 555 np->rxopt.bits.srcrt = valbool; 556 retv = 0; 557 break; 558 559 case IPV6_2292RTHDR: 560 if (optlen < sizeof(int)) 561 goto e_inval; 562 np->rxopt.bits.osrcrt = valbool; 563 retv = 0; 564 break; 565 566 case IPV6_RECVHOPOPTS: 567 if (optlen < sizeof(int)) 568 goto e_inval; 569 np->rxopt.bits.hopopts = valbool; 570 retv = 0; 571 break; 572 573 case IPV6_2292HOPOPTS: 574 if (optlen < sizeof(int)) 575 goto e_inval; 576 np->rxopt.bits.ohopopts = valbool; 577 retv = 0; 578 break; 579 580 case IPV6_RECVDSTOPTS: 581 if (optlen < sizeof(int)) 582 goto e_inval; 583 np->rxopt.bits.dstopts = valbool; 584 retv = 0; 585 break; 586 587 case IPV6_2292DSTOPTS: 588 if (optlen < sizeof(int)) 589 goto e_inval; 590 np->rxopt.bits.odstopts = valbool; 591 retv = 0; 592 break; 593 594 case IPV6_TCLASS: 595 if (optlen < sizeof(int)) 596 goto e_inval; 597 if (val < -1 || val > 0xff) 598 goto e_inval; 599 /* RFC 3542, 6.5: default traffic class of 0x0 */ 600 if (val == -1) 601 val = 0; 602 np->tclass = val; 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 && !ns_capable(net->user_ns, CAP_NET_RAW) && 629 !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_sk(sk)->transparent = 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_sk(sk)->freebind = 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 = (void *)(opt+1); 720 ipc6.opt = opt; 721 722 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6); 723 if (retv) 724 goto done; 725 update: 726 retv = 0; 727 opt = ipv6_update_options(sk, opt); 728 done: 729 if (opt) { 730 atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 731 txopt_put(opt); 732 } 733 break; 734 } 735 case IPV6_UNICAST_HOPS: 736 if (optlen < sizeof(int)) 737 goto e_inval; 738 if (val > 255 || val < -1) 739 goto e_inval; 740 np->hop_limit = val; 741 retv = 0; 742 break; 743 744 case IPV6_MULTICAST_HOPS: 745 if (sk->sk_type == SOCK_STREAM) 746 break; 747 if (optlen < sizeof(int)) 748 goto e_inval; 749 if (val > 255 || val < -1) 750 goto e_inval; 751 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val); 752 retv = 0; 753 break; 754 755 case IPV6_MULTICAST_LOOP: 756 if (optlen < sizeof(int)) 757 goto e_inval; 758 if (val != valbool) 759 goto e_inval; 760 np->mc_loop = valbool; 761 retv = 0; 762 break; 763 764 case IPV6_UNICAST_IF: 765 { 766 struct net_device *dev = NULL; 767 int ifindex; 768 769 if (optlen != sizeof(int)) 770 goto e_inval; 771 772 ifindex = (__force int)ntohl((__force __be32)val); 773 if (ifindex == 0) { 774 np->ucast_oif = 0; 775 retv = 0; 776 break; 777 } 778 779 dev = dev_get_by_index(net, ifindex); 780 retv = -EADDRNOTAVAIL; 781 if (!dev) 782 break; 783 dev_put(dev); 784 785 retv = -EINVAL; 786 if (sk->sk_bound_dev_if) 787 break; 788 789 np->ucast_oif = ifindex; 790 retv = 0; 791 break; 792 } 793 794 case IPV6_MULTICAST_IF: 795 if (sk->sk_type == SOCK_STREAM) 796 break; 797 if (optlen < sizeof(int)) 798 goto e_inval; 799 800 if (val) { 801 struct net_device *dev; 802 int midx; 803 804 rcu_read_lock(); 805 806 dev = dev_get_by_index_rcu(net, val); 807 if (!dev) { 808 rcu_read_unlock(); 809 retv = -ENODEV; 810 break; 811 } 812 midx = l3mdev_master_ifindex_rcu(dev); 813 814 rcu_read_unlock(); 815 816 if (sk->sk_bound_dev_if && 817 sk->sk_bound_dev_if != val && 818 (!midx || midx != sk->sk_bound_dev_if)) 819 goto e_inval; 820 } 821 np->mcast_oif = val; 822 retv = 0; 823 break; 824 case IPV6_ADD_MEMBERSHIP: 825 case IPV6_DROP_MEMBERSHIP: 826 { 827 struct ipv6_mreq mreq; 828 829 if (optlen < sizeof(struct ipv6_mreq)) 830 goto e_inval; 831 832 retv = -EPROTO; 833 if (inet_sk(sk)->is_icsk) 834 break; 835 836 retv = -EFAULT; 837 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq))) 838 break; 839 840 if (optname == IPV6_ADD_MEMBERSHIP) 841 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); 842 else 843 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr); 844 break; 845 } 846 case IPV6_JOIN_ANYCAST: 847 case IPV6_LEAVE_ANYCAST: 848 { 849 struct ipv6_mreq mreq; 850 851 if (optlen < sizeof(struct ipv6_mreq)) 852 goto e_inval; 853 854 retv = -EFAULT; 855 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq))) 856 break; 857 858 if (optname == IPV6_JOIN_ANYCAST) 859 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); 860 else 861 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr); 862 break; 863 } 864 case IPV6_MULTICAST_ALL: 865 if (optlen < sizeof(int)) 866 goto e_inval; 867 np->mc_all = valbool; 868 retv = 0; 869 break; 870 871 case MCAST_JOIN_GROUP: 872 case MCAST_LEAVE_GROUP: 873 if (in_compat_syscall()) 874 retv = compat_ipv6_mcast_join_leave(sk, optname, optval, 875 optlen); 876 else 877 retv = ipv6_mcast_join_leave(sk, optname, optval, 878 optlen); 879 break; 880 case MCAST_JOIN_SOURCE_GROUP: 881 case MCAST_LEAVE_SOURCE_GROUP: 882 case MCAST_BLOCK_SOURCE: 883 case MCAST_UNBLOCK_SOURCE: 884 retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen); 885 break; 886 case MCAST_MSFILTER: 887 if (in_compat_syscall()) 888 retv = compat_ipv6_set_mcast_msfilter(sk, optval, 889 optlen); 890 else 891 retv = ipv6_set_mcast_msfilter(sk, optval, optlen); 892 break; 893 case IPV6_ROUTER_ALERT: 894 if (optlen < sizeof(int)) 895 goto e_inval; 896 retv = ip6_ra_control(sk, val); 897 break; 898 case IPV6_ROUTER_ALERT_ISOLATE: 899 if (optlen < sizeof(int)) 900 goto e_inval; 901 np->rtalert_isolate = valbool; 902 retv = 0; 903 break; 904 case IPV6_MTU_DISCOVER: 905 if (optlen < sizeof(int)) 906 goto e_inval; 907 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT) 908 goto e_inval; 909 np->pmtudisc = val; 910 retv = 0; 911 break; 912 case IPV6_MTU: 913 if (optlen < sizeof(int)) 914 goto e_inval; 915 if (val && val < IPV6_MIN_MTU) 916 goto e_inval; 917 np->frag_size = val; 918 retv = 0; 919 break; 920 case IPV6_RECVERR: 921 if (optlen < sizeof(int)) 922 goto e_inval; 923 np->recverr = valbool; 924 if (!val) 925 skb_queue_purge(&sk->sk_error_queue); 926 retv = 0; 927 break; 928 case IPV6_FLOWINFO_SEND: 929 if (optlen < sizeof(int)) 930 goto e_inval; 931 np->sndflow = valbool; 932 retv = 0; 933 break; 934 case IPV6_FLOWLABEL_MGR: 935 retv = ipv6_flowlabel_opt(sk, optval, optlen); 936 break; 937 case IPV6_IPSEC_POLICY: 938 case IPV6_XFRM_POLICY: 939 retv = -EPERM; 940 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 941 break; 942 retv = xfrm_user_policy(sk, optname, optval, optlen); 943 break; 944 945 case IPV6_ADDR_PREFERENCES: 946 if (optlen < sizeof(int)) 947 goto e_inval; 948 retv = __ip6_sock_set_addr_preferences(sk, val); 949 break; 950 case IPV6_MINHOPCOUNT: 951 if (optlen < sizeof(int)) 952 goto e_inval; 953 if (val < 0 || val > 255) 954 goto e_inval; 955 956 if (val) 957 static_branch_enable(&ip6_min_hopcount); 958 959 /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount 960 * while we are changing it. 961 */ 962 WRITE_ONCE(np->min_hopcount, val); 963 retv = 0; 964 break; 965 case IPV6_DONTFRAG: 966 np->dontfrag = valbool; 967 retv = 0; 968 break; 969 case IPV6_AUTOFLOWLABEL: 970 np->autoflowlabel = valbool; 971 np->autoflowlabel_set = 1; 972 retv = 0; 973 break; 974 case IPV6_RECVFRAGSIZE: 975 np->rxopt.bits.recvfragsize = valbool; 976 retv = 0; 977 break; 978 case IPV6_RECVERR_RFC4884: 979 if (optlen < sizeof(int)) 980 goto e_inval; 981 if (val < 0 || val > 1) 982 goto e_inval; 983 np->recverr_rfc4884 = valbool; 984 retv = 0; 985 break; 986 } 987 988 release_sock(sk); 989 if (needs_rtnl) 990 rtnl_unlock(); 991 992 return retv; 993 994 e_inval: 995 release_sock(sk); 996 if (needs_rtnl) 997 rtnl_unlock(); 998 return -EINVAL; 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, char __user *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_user(optval, hdr, len)) 1053 return -EFAULT; 1054 return len; 1055 } 1056 1057 static int ipv6_get_msfilter(struct sock *sk, void __user *optval, 1058 int __user *optlen, int len) 1059 { 1060 const int size0 = offsetof(struct group_filter, gf_slist_flex); 1061 struct group_filter __user *p = optval; 1062 struct group_filter gsf; 1063 int num; 1064 int err; 1065 1066 if (len < size0) 1067 return -EINVAL; 1068 if (copy_from_user(&gsf, p, size0)) 1069 return -EFAULT; 1070 if (gsf.gf_group.ss_family != AF_INET6) 1071 return -EADDRNOTAVAIL; 1072 num = gsf.gf_numsrc; 1073 lock_sock(sk); 1074 err = ip6_mc_msfget(sk, &gsf, p->gf_slist_flex); 1075 if (!err) { 1076 if (num > gsf.gf_numsrc) 1077 num = gsf.gf_numsrc; 1078 if (put_user(GROUP_FILTER_SIZE(num), optlen) || 1079 copy_to_user(p, &gsf, size0)) 1080 err = -EFAULT; 1081 } 1082 release_sock(sk); 1083 return err; 1084 } 1085 1086 static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval, 1087 int __user *optlen) 1088 { 1089 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); 1090 struct compat_group_filter __user *p = optval; 1091 struct compat_group_filter gf32; 1092 struct group_filter gf; 1093 int len, err; 1094 int num; 1095 1096 if (get_user(len, optlen)) 1097 return -EFAULT; 1098 if (len < size0) 1099 return -EINVAL; 1100 1101 if (copy_from_user(&gf32, p, size0)) 1102 return -EFAULT; 1103 gf.gf_interface = gf32.gf_interface; 1104 gf.gf_fmode = gf32.gf_fmode; 1105 num = gf.gf_numsrc = gf32.gf_numsrc; 1106 gf.gf_group = gf32.gf_group; 1107 1108 if (gf.gf_group.ss_family != AF_INET6) 1109 return -EADDRNOTAVAIL; 1110 1111 lock_sock(sk); 1112 err = ip6_mc_msfget(sk, &gf, p->gf_slist_flex); 1113 release_sock(sk); 1114 if (err) 1115 return err; 1116 if (num > gf.gf_numsrc) 1117 num = gf.gf_numsrc; 1118 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32)); 1119 if (put_user(len, optlen) || 1120 put_user(gf.gf_fmode, &p->gf_fmode) || 1121 put_user(gf.gf_numsrc, &p->gf_numsrc)) 1122 return -EFAULT; 1123 return 0; 1124 } 1125 1126 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname, 1127 char __user *optval, int __user *optlen, unsigned int flags) 1128 { 1129 struct ipv6_pinfo *np = inet6_sk(sk); 1130 int len; 1131 int val; 1132 1133 if (ip6_mroute_opt(optname)) 1134 return ip6_mroute_getsockopt(sk, optname, optval, optlen); 1135 1136 if (get_user(len, optlen)) 1137 return -EFAULT; 1138 switch (optname) { 1139 case IPV6_ADDRFORM: 1140 if (sk->sk_protocol != IPPROTO_UDP && 1141 sk->sk_protocol != IPPROTO_UDPLITE && 1142 sk->sk_protocol != IPPROTO_TCP) 1143 return -ENOPROTOOPT; 1144 if (sk->sk_state != TCP_ESTABLISHED) 1145 return -ENOTCONN; 1146 val = sk->sk_family; 1147 break; 1148 case MCAST_MSFILTER: 1149 if (in_compat_syscall()) 1150 return compat_ipv6_get_msfilter(sk, optval, optlen); 1151 return ipv6_get_msfilter(sk, optval, optlen, len); 1152 case IPV6_2292PKTOPTIONS: 1153 { 1154 struct msghdr msg; 1155 struct sk_buff *skb; 1156 1157 if (sk->sk_type != SOCK_STREAM) 1158 return -ENOPROTOOPT; 1159 1160 msg.msg_control_user = optval; 1161 msg.msg_controllen = len; 1162 msg.msg_flags = flags; 1163 msg.msg_control_is_user = true; 1164 1165 lock_sock(sk); 1166 skb = np->pktoptions; 1167 if (skb) 1168 ip6_datagram_recv_ctl(sk, &msg, skb); 1169 release_sock(sk); 1170 if (!skb) { 1171 if (np->rxopt.bits.rxinfo) { 1172 struct in6_pktinfo src_info; 1173 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : 1174 np->sticky_pktinfo.ipi6_ifindex; 1175 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr; 1176 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); 1177 } 1178 if (np->rxopt.bits.rxhlim) { 1179 int hlim = np->mcast_hops; 1180 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); 1181 } 1182 if (np->rxopt.bits.rxtclass) { 1183 int tclass = (int)ip6_tclass(np->rcv_flowinfo); 1184 1185 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); 1186 } 1187 if (np->rxopt.bits.rxoinfo) { 1188 struct in6_pktinfo src_info; 1189 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : 1190 np->sticky_pktinfo.ipi6_ifindex; 1191 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : 1192 np->sticky_pktinfo.ipi6_addr; 1193 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); 1194 } 1195 if (np->rxopt.bits.rxohlim) { 1196 int hlim = np->mcast_hops; 1197 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); 1198 } 1199 if (np->rxopt.bits.rxflow) { 1200 __be32 flowinfo = np->rcv_flowinfo; 1201 1202 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); 1203 } 1204 } 1205 len -= msg.msg_controllen; 1206 return put_user(len, optlen); 1207 } 1208 case IPV6_MTU: 1209 { 1210 struct dst_entry *dst; 1211 1212 val = 0; 1213 rcu_read_lock(); 1214 dst = __sk_dst_get(sk); 1215 if (dst) 1216 val = dst_mtu(dst); 1217 rcu_read_unlock(); 1218 if (!val) 1219 return -ENOTCONN; 1220 break; 1221 } 1222 1223 case IPV6_V6ONLY: 1224 val = sk->sk_ipv6only; 1225 break; 1226 1227 case IPV6_RECVPKTINFO: 1228 val = np->rxopt.bits.rxinfo; 1229 break; 1230 1231 case IPV6_2292PKTINFO: 1232 val = np->rxopt.bits.rxoinfo; 1233 break; 1234 1235 case IPV6_RECVHOPLIMIT: 1236 val = np->rxopt.bits.rxhlim; 1237 break; 1238 1239 case IPV6_2292HOPLIMIT: 1240 val = np->rxopt.bits.rxohlim; 1241 break; 1242 1243 case IPV6_RECVRTHDR: 1244 val = np->rxopt.bits.srcrt; 1245 break; 1246 1247 case IPV6_2292RTHDR: 1248 val = np->rxopt.bits.osrcrt; 1249 break; 1250 1251 case IPV6_HOPOPTS: 1252 case IPV6_RTHDRDSTOPTS: 1253 case IPV6_RTHDR: 1254 case IPV6_DSTOPTS: 1255 { 1256 struct ipv6_txoptions *opt; 1257 1258 lock_sock(sk); 1259 opt = rcu_dereference_protected(np->opt, 1260 lockdep_sock_is_held(sk)); 1261 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len); 1262 release_sock(sk); 1263 /* check if ipv6_getsockopt_sticky() returns err code */ 1264 if (len < 0) 1265 return len; 1266 return put_user(len, optlen); 1267 } 1268 1269 case IPV6_RECVHOPOPTS: 1270 val = np->rxopt.bits.hopopts; 1271 break; 1272 1273 case IPV6_2292HOPOPTS: 1274 val = np->rxopt.bits.ohopopts; 1275 break; 1276 1277 case IPV6_RECVDSTOPTS: 1278 val = np->rxopt.bits.dstopts; 1279 break; 1280 1281 case IPV6_2292DSTOPTS: 1282 val = np->rxopt.bits.odstopts; 1283 break; 1284 1285 case IPV6_TCLASS: 1286 val = np->tclass; 1287 break; 1288 1289 case IPV6_RECVTCLASS: 1290 val = np->rxopt.bits.rxtclass; 1291 break; 1292 1293 case IPV6_FLOWINFO: 1294 val = np->rxopt.bits.rxflow; 1295 break; 1296 1297 case IPV6_RECVPATHMTU: 1298 val = np->rxopt.bits.rxpmtu; 1299 break; 1300 1301 case IPV6_PATHMTU: 1302 { 1303 struct dst_entry *dst; 1304 struct ip6_mtuinfo mtuinfo; 1305 1306 if (len < sizeof(mtuinfo)) 1307 return -EINVAL; 1308 1309 len = sizeof(mtuinfo); 1310 memset(&mtuinfo, 0, sizeof(mtuinfo)); 1311 1312 rcu_read_lock(); 1313 dst = __sk_dst_get(sk); 1314 if (dst) 1315 mtuinfo.ip6m_mtu = dst_mtu(dst); 1316 rcu_read_unlock(); 1317 if (!mtuinfo.ip6m_mtu) 1318 return -ENOTCONN; 1319 1320 if (put_user(len, optlen)) 1321 return -EFAULT; 1322 if (copy_to_user(optval, &mtuinfo, len)) 1323 return -EFAULT; 1324 1325 return 0; 1326 } 1327 1328 case IPV6_TRANSPARENT: 1329 val = inet_sk(sk)->transparent; 1330 break; 1331 1332 case IPV6_FREEBIND: 1333 val = inet_sk(sk)->freebind; 1334 break; 1335 1336 case IPV6_RECVORIGDSTADDR: 1337 val = np->rxopt.bits.rxorigdstaddr; 1338 break; 1339 1340 case IPV6_UNICAST_HOPS: 1341 case IPV6_MULTICAST_HOPS: 1342 { 1343 struct dst_entry *dst; 1344 1345 if (optname == IPV6_UNICAST_HOPS) 1346 val = np->hop_limit; 1347 else 1348 val = np->mcast_hops; 1349 1350 if (val < 0) { 1351 rcu_read_lock(); 1352 dst = __sk_dst_get(sk); 1353 if (dst) 1354 val = ip6_dst_hoplimit(dst); 1355 rcu_read_unlock(); 1356 } 1357 1358 if (val < 0) 1359 val = sock_net(sk)->ipv6.devconf_all->hop_limit; 1360 break; 1361 } 1362 1363 case IPV6_MULTICAST_LOOP: 1364 val = np->mc_loop; 1365 break; 1366 1367 case IPV6_MULTICAST_IF: 1368 val = np->mcast_oif; 1369 break; 1370 1371 case IPV6_MULTICAST_ALL: 1372 val = np->mc_all; 1373 break; 1374 1375 case IPV6_UNICAST_IF: 1376 val = (__force int)htonl((__u32) np->ucast_oif); 1377 break; 1378 1379 case IPV6_MTU_DISCOVER: 1380 val = np->pmtudisc; 1381 break; 1382 1383 case IPV6_RECVERR: 1384 val = np->recverr; 1385 break; 1386 1387 case IPV6_FLOWINFO_SEND: 1388 val = np->sndflow; 1389 break; 1390 1391 case IPV6_FLOWLABEL_MGR: 1392 { 1393 struct in6_flowlabel_req freq; 1394 int flags; 1395 1396 if (len < sizeof(freq)) 1397 return -EINVAL; 1398 1399 if (copy_from_user(&freq, optval, sizeof(freq))) 1400 return -EFAULT; 1401 1402 if (freq.flr_action != IPV6_FL_A_GET) 1403 return -EINVAL; 1404 1405 len = sizeof(freq); 1406 flags = freq.flr_flags; 1407 1408 memset(&freq, 0, sizeof(freq)); 1409 1410 val = ipv6_flowlabel_opt_get(sk, &freq, flags); 1411 if (val < 0) 1412 return val; 1413 1414 if (put_user(len, optlen)) 1415 return -EFAULT; 1416 if (copy_to_user(optval, &freq, len)) 1417 return -EFAULT; 1418 1419 return 0; 1420 } 1421 1422 case IPV6_ADDR_PREFERENCES: 1423 val = 0; 1424 1425 if (np->srcprefs & IPV6_PREFER_SRC_TMP) 1426 val |= IPV6_PREFER_SRC_TMP; 1427 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC) 1428 val |= IPV6_PREFER_SRC_PUBLIC; 1429 else { 1430 /* XXX: should we return system default? */ 1431 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT; 1432 } 1433 1434 if (np->srcprefs & IPV6_PREFER_SRC_COA) 1435 val |= IPV6_PREFER_SRC_COA; 1436 else 1437 val |= IPV6_PREFER_SRC_HOME; 1438 break; 1439 1440 case IPV6_MINHOPCOUNT: 1441 val = np->min_hopcount; 1442 break; 1443 1444 case IPV6_DONTFRAG: 1445 val = np->dontfrag; 1446 break; 1447 1448 case IPV6_AUTOFLOWLABEL: 1449 val = ip6_autoflowlabel(sock_net(sk), np); 1450 break; 1451 1452 case IPV6_RECVFRAGSIZE: 1453 val = np->rxopt.bits.recvfragsize; 1454 break; 1455 1456 case IPV6_ROUTER_ALERT_ISOLATE: 1457 val = np->rtalert_isolate; 1458 break; 1459 1460 case IPV6_RECVERR_RFC4884: 1461 val = np->recverr_rfc4884; 1462 break; 1463 1464 default: 1465 return -ENOPROTOOPT; 1466 } 1467 len = min_t(unsigned int, sizeof(int), len); 1468 if (put_user(len, optlen)) 1469 return -EFAULT; 1470 if (copy_to_user(optval, &val, len)) 1471 return -EFAULT; 1472 return 0; 1473 } 1474 1475 int ipv6_getsockopt(struct sock *sk, int level, int optname, 1476 char __user *optval, int __user *optlen) 1477 { 1478 int err; 1479 1480 if (level == SOL_IP && sk->sk_type != SOCK_RAW) 1481 return udp_prot.getsockopt(sk, level, optname, optval, optlen); 1482 1483 if (level != SOL_IPV6) 1484 return -ENOPROTOOPT; 1485 1486 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0); 1487 #ifdef CONFIG_NETFILTER 1488 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1489 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { 1490 int len; 1491 1492 if (get_user(len, optlen)) 1493 return -EFAULT; 1494 1495 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len); 1496 if (err >= 0) 1497 err = put_user(len, optlen); 1498 } 1499 #endif 1500 return err; 1501 } 1502 EXPORT_SYMBOL(ipv6_getsockopt); 1503