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 = unrcu_pointer(xchg(&inet6_sk(sk)->opt, RCU_INITIALIZER(opt))); 115 sk_dst_reset(sk); 116 117 return opt; 118 } 119 120 static bool setsockopt_needs_rtnl(int optname) 121 { 122 switch (optname) { 123 case IPV6_ADDRFORM: 124 case IPV6_ADD_MEMBERSHIP: 125 case IPV6_DROP_MEMBERSHIP: 126 case IPV6_JOIN_ANYCAST: 127 case IPV6_LEAVE_ANYCAST: 128 case MCAST_JOIN_GROUP: 129 case MCAST_LEAVE_GROUP: 130 case MCAST_JOIN_SOURCE_GROUP: 131 case MCAST_LEAVE_SOURCE_GROUP: 132 case MCAST_BLOCK_SOURCE: 133 case MCAST_UNBLOCK_SOURCE: 134 case MCAST_MSFILTER: 135 return true; 136 } 137 return false; 138 } 139 140 static int copy_group_source_from_sockptr(struct group_source_req *greqs, 141 sockptr_t optval, int optlen) 142 { 143 if (in_compat_syscall()) { 144 struct compat_group_source_req gr32; 145 146 if (optlen < sizeof(gr32)) 147 return -EINVAL; 148 if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) 149 return -EFAULT; 150 greqs->gsr_interface = gr32.gsr_interface; 151 greqs->gsr_group = gr32.gsr_group; 152 greqs->gsr_source = gr32.gsr_source; 153 } else { 154 if (optlen < sizeof(*greqs)) 155 return -EINVAL; 156 if (copy_from_sockptr(greqs, optval, sizeof(*greqs))) 157 return -EFAULT; 158 } 159 160 return 0; 161 } 162 163 static int do_ipv6_mcast_group_source(struct sock *sk, int optname, 164 sockptr_t optval, int optlen) 165 { 166 struct group_source_req greqs; 167 int omode, add; 168 int ret; 169 170 ret = copy_group_source_from_sockptr(&greqs, optval, optlen); 171 if (ret) 172 return ret; 173 174 if (greqs.gsr_group.ss_family != AF_INET6 || 175 greqs.gsr_source.ss_family != AF_INET6) 176 return -EADDRNOTAVAIL; 177 178 if (optname == MCAST_BLOCK_SOURCE) { 179 omode = MCAST_EXCLUDE; 180 add = 1; 181 } else if (optname == MCAST_UNBLOCK_SOURCE) { 182 omode = MCAST_EXCLUDE; 183 add = 0; 184 } else if (optname == MCAST_JOIN_SOURCE_GROUP) { 185 struct sockaddr_in6 *psin6; 186 int retv; 187 188 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; 189 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface, 190 &psin6->sin6_addr, 191 MCAST_INCLUDE); 192 /* prior join w/ different source is ok */ 193 if (retv && retv != -EADDRINUSE) 194 return retv; 195 omode = MCAST_INCLUDE; 196 add = 1; 197 } else /* MCAST_LEAVE_SOURCE_GROUP */ { 198 omode = MCAST_INCLUDE; 199 add = 0; 200 } 201 return ip6_mc_source(add, omode, sk, &greqs); 202 } 203 204 static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, 205 int optlen) 206 { 207 struct group_filter *gsf; 208 int ret; 209 210 if (optlen < GROUP_FILTER_SIZE(0)) 211 return -EINVAL; 212 if (optlen > READ_ONCE(sysctl_optmem_max)) 213 return -ENOBUFS; 214 215 gsf = memdup_sockptr(optval, optlen); 216 if (IS_ERR(gsf)) 217 return PTR_ERR(gsf); 218 219 /* numsrc >= (4G-140)/128 overflow in 32 bits */ 220 ret = -ENOBUFS; 221 if (gsf->gf_numsrc >= 0x1ffffffU || 222 gsf->gf_numsrc > sysctl_mld_max_msf) 223 goto out_free_gsf; 224 225 ret = -EINVAL; 226 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) 227 goto out_free_gsf; 228 229 ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex); 230 out_free_gsf: 231 kfree(gsf); 232 return ret; 233 } 234 235 static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval, 236 int optlen) 237 { 238 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); 239 struct compat_group_filter *gf32; 240 void *p; 241 int ret; 242 int n; 243 244 if (optlen < size0) 245 return -EINVAL; 246 if (optlen > READ_ONCE(sysctl_optmem_max) - 4) 247 return -ENOBUFS; 248 249 p = kmalloc(optlen + 4, GFP_KERNEL); 250 if (!p) 251 return -ENOMEM; 252 253 gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */ 254 ret = -EFAULT; 255 if (copy_from_sockptr(gf32, optval, optlen)) 256 goto out_free_p; 257 258 /* numsrc >= (4G-140)/128 overflow in 32 bits */ 259 ret = -ENOBUFS; 260 n = gf32->gf_numsrc; 261 if (n >= 0x1ffffffU || n > sysctl_mld_max_msf) 262 goto out_free_p; 263 264 ret = -EINVAL; 265 if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen) 266 goto out_free_p; 267 268 ret = ip6_mc_msfilter(sk, &(struct group_filter){ 269 .gf_interface = gf32->gf_interface, 270 .gf_group = gf32->gf_group, 271 .gf_fmode = gf32->gf_fmode, 272 .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex); 273 274 out_free_p: 275 kfree(p); 276 return ret; 277 } 278 279 static int ipv6_mcast_join_leave(struct sock *sk, int optname, 280 sockptr_t optval, int optlen) 281 { 282 struct sockaddr_in6 *psin6; 283 struct group_req greq; 284 285 if (optlen < sizeof(greq)) 286 return -EINVAL; 287 if (copy_from_sockptr(&greq, optval, sizeof(greq))) 288 return -EFAULT; 289 290 if (greq.gr_group.ss_family != AF_INET6) 291 return -EADDRNOTAVAIL; 292 psin6 = (struct sockaddr_in6 *)&greq.gr_group; 293 if (optname == MCAST_JOIN_GROUP) 294 return ipv6_sock_mc_join(sk, greq.gr_interface, 295 &psin6->sin6_addr); 296 return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr); 297 } 298 299 static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname, 300 sockptr_t optval, int optlen) 301 { 302 struct compat_group_req gr32; 303 struct sockaddr_in6 *psin6; 304 305 if (optlen < sizeof(gr32)) 306 return -EINVAL; 307 if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) 308 return -EFAULT; 309 310 if (gr32.gr_group.ss_family != AF_INET6) 311 return -EADDRNOTAVAIL; 312 psin6 = (struct sockaddr_in6 *)&gr32.gr_group; 313 if (optname == MCAST_JOIN_GROUP) 314 return ipv6_sock_mc_join(sk, gr32.gr_interface, 315 &psin6->sin6_addr); 316 return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr); 317 } 318 319 static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval, 320 int optlen) 321 { 322 struct ipv6_pinfo *np = inet6_sk(sk); 323 struct ipv6_opt_hdr *new = NULL; 324 struct net *net = sock_net(sk); 325 struct ipv6_txoptions *opt; 326 int err; 327 328 /* hop-by-hop / destination options are privileged option */ 329 if (optname != IPV6_RTHDR && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW)) 330 return -EPERM; 331 332 /* remove any sticky options header with a zero option 333 * length, per RFC3542. 334 */ 335 if (optlen > 0) { 336 if (sockptr_is_null(optval)) 337 return -EINVAL; 338 if (optlen < sizeof(struct ipv6_opt_hdr) || 339 optlen & 0x7 || 340 optlen > 8 * 255) 341 return -EINVAL; 342 343 new = memdup_sockptr(optval, optlen); 344 if (IS_ERR(new)) 345 return PTR_ERR(new); 346 if (unlikely(ipv6_optlen(new) > optlen)) { 347 kfree(new); 348 return -EINVAL; 349 } 350 } 351 352 opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk)); 353 opt = ipv6_renew_options(sk, opt, optname, new); 354 kfree(new); 355 if (IS_ERR(opt)) 356 return PTR_ERR(opt); 357 358 /* routing header option needs extra check */ 359 err = -EINVAL; 360 if (optname == IPV6_RTHDR && opt && opt->srcrt) { 361 struct ipv6_rt_hdr *rthdr = opt->srcrt; 362 switch (rthdr->type) { 363 #if IS_ENABLED(CONFIG_IPV6_MIP6) 364 case IPV6_SRCRT_TYPE_2: 365 if (rthdr->hdrlen != 2 || rthdr->segments_left != 1) 366 goto sticky_done; 367 break; 368 #endif 369 case IPV6_SRCRT_TYPE_4: 370 { 371 struct ipv6_sr_hdr *srh = 372 (struct ipv6_sr_hdr *)opt->srcrt; 373 374 if (!seg6_validate_srh(srh, optlen, false)) 375 goto sticky_done; 376 break; 377 } 378 default: 379 goto sticky_done; 380 } 381 } 382 383 err = 0; 384 opt = ipv6_update_options(sk, opt); 385 sticky_done: 386 if (opt) { 387 atomic_sub(opt->tot_len, &sk->sk_omem_alloc); 388 txopt_put(opt); 389 } 390 return err; 391 } 392 393 int do_ipv6_setsockopt(struct sock *sk, int level, int optname, 394 sockptr_t optval, unsigned int optlen) 395 { 396 struct ipv6_pinfo *np = inet6_sk(sk); 397 struct net *net = sock_net(sk); 398 int val, valbool; 399 int retv = -ENOPROTOOPT; 400 bool needs_rtnl = setsockopt_needs_rtnl(optname); 401 402 if (sockptr_is_null(optval)) 403 val = 0; 404 else { 405 if (optlen >= sizeof(int)) { 406 if (copy_from_sockptr(&val, optval, sizeof(val))) 407 return -EFAULT; 408 } else 409 val = 0; 410 } 411 412 valbool = (val != 0); 413 414 if (ip6_mroute_opt(optname)) 415 return ip6_mroute_setsockopt(sk, optname, optval, optlen); 416 417 if (needs_rtnl) 418 rtnl_lock(); 419 sockopt_lock_sock(sk); 420 421 /* Another thread has converted the socket into IPv4 with 422 * IPV6_ADDRFORM concurrently. 423 */ 424 if (unlikely(sk->sk_family != AF_INET6)) 425 goto unlock; 426 427 switch (optname) { 428 429 case IPV6_ADDRFORM: 430 if (optlen < sizeof(int)) 431 goto e_inval; 432 if (val == PF_INET) { 433 if (sk->sk_type == SOCK_RAW) 434 break; 435 436 if (sk->sk_protocol == IPPROTO_UDP || 437 sk->sk_protocol == IPPROTO_UDPLITE) { 438 struct udp_sock *up = udp_sk(sk); 439 if (up->pending == AF_INET6) { 440 retv = -EBUSY; 441 break; 442 } 443 } else if (sk->sk_protocol == IPPROTO_TCP) { 444 if (sk->sk_prot != &tcpv6_prot) { 445 retv = -EBUSY; 446 break; 447 } 448 } else { 449 break; 450 } 451 452 if (sk->sk_state != TCP_ESTABLISHED) { 453 retv = -ENOTCONN; 454 break; 455 } 456 457 if (ipv6_only_sock(sk) || 458 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) { 459 retv = -EADDRNOTAVAIL; 460 break; 461 } 462 463 __ipv6_sock_mc_close(sk); 464 __ipv6_sock_ac_close(sk); 465 466 if (sk->sk_protocol == IPPROTO_TCP) { 467 struct inet_connection_sock *icsk = inet_csk(sk); 468 469 sock_prot_inuse_add(net, sk->sk_prot, -1); 470 sock_prot_inuse_add(net, &tcp_prot, 1); 471 472 /* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */ 473 WRITE_ONCE(sk->sk_prot, &tcp_prot); 474 /* Paired with READ_ONCE() in tcp_(get|set)sockopt() */ 475 WRITE_ONCE(icsk->icsk_af_ops, &ipv4_specific); 476 WRITE_ONCE(sk->sk_socket->ops, &inet_stream_ops); 477 WRITE_ONCE(sk->sk_family, PF_INET); 478 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 479 } else { 480 struct proto *prot = &udp_prot; 481 482 if (sk->sk_protocol == IPPROTO_UDPLITE) 483 prot = &udplite_prot; 484 485 sock_prot_inuse_add(net, sk->sk_prot, -1); 486 sock_prot_inuse_add(net, prot, 1); 487 488 /* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */ 489 WRITE_ONCE(sk->sk_prot, prot); 490 WRITE_ONCE(sk->sk_socket->ops, &inet_dgram_ops); 491 WRITE_ONCE(sk->sk_family, PF_INET); 492 } 493 494 /* Disable all options not to allocate memory anymore, 495 * but there is still a race. See the lockless path 496 * in udpv6_sendmsg() and ipv6_local_rxpmtu(). 497 */ 498 np->rxopt.all = 0; 499 500 inet6_cleanup_sock(sk); 501 502 module_put(THIS_MODULE); 503 retv = 0; 504 break; 505 } 506 goto e_inval; 507 508 case IPV6_V6ONLY: 509 if (optlen < sizeof(int) || 510 inet_sk(sk)->inet_num) 511 goto e_inval; 512 sk->sk_ipv6only = valbool; 513 retv = 0; 514 break; 515 516 case IPV6_RECVPKTINFO: 517 if (optlen < sizeof(int)) 518 goto e_inval; 519 np->rxopt.bits.rxinfo = valbool; 520 retv = 0; 521 break; 522 523 case IPV6_2292PKTINFO: 524 if (optlen < sizeof(int)) 525 goto e_inval; 526 np->rxopt.bits.rxoinfo = valbool; 527 retv = 0; 528 break; 529 530 case IPV6_RECVHOPLIMIT: 531 if (optlen < sizeof(int)) 532 goto e_inval; 533 np->rxopt.bits.rxhlim = valbool; 534 retv = 0; 535 break; 536 537 case IPV6_2292HOPLIMIT: 538 if (optlen < sizeof(int)) 539 goto e_inval; 540 np->rxopt.bits.rxohlim = valbool; 541 retv = 0; 542 break; 543 544 case IPV6_RECVRTHDR: 545 if (optlen < sizeof(int)) 546 goto e_inval; 547 np->rxopt.bits.srcrt = valbool; 548 retv = 0; 549 break; 550 551 case IPV6_2292RTHDR: 552 if (optlen < sizeof(int)) 553 goto e_inval; 554 np->rxopt.bits.osrcrt = valbool; 555 retv = 0; 556 break; 557 558 case IPV6_RECVHOPOPTS: 559 if (optlen < sizeof(int)) 560 goto e_inval; 561 np->rxopt.bits.hopopts = valbool; 562 retv = 0; 563 break; 564 565 case IPV6_2292HOPOPTS: 566 if (optlen < sizeof(int)) 567 goto e_inval; 568 np->rxopt.bits.ohopopts = valbool; 569 retv = 0; 570 break; 571 572 case IPV6_RECVDSTOPTS: 573 if (optlen < sizeof(int)) 574 goto e_inval; 575 np->rxopt.bits.dstopts = valbool; 576 retv = 0; 577 break; 578 579 case IPV6_2292DSTOPTS: 580 if (optlen < sizeof(int)) 581 goto e_inval; 582 np->rxopt.bits.odstopts = valbool; 583 retv = 0; 584 break; 585 586 case IPV6_TCLASS: 587 if (optlen < sizeof(int)) 588 goto e_inval; 589 if (val < -1 || val > 0xff) 590 goto e_inval; 591 /* RFC 3542, 6.5: default traffic class of 0x0 */ 592 if (val == -1) 593 val = 0; 594 if (sk->sk_type == SOCK_STREAM) { 595 val &= ~INET_ECN_MASK; 596 val |= np->tclass & INET_ECN_MASK; 597 } 598 if (np->tclass != val) { 599 np->tclass = val; 600 sk_dst_reset(sk); 601 } 602 retv = 0; 603 break; 604 605 case IPV6_RECVTCLASS: 606 if (optlen < sizeof(int)) 607 goto e_inval; 608 np->rxopt.bits.rxtclass = valbool; 609 retv = 0; 610 break; 611 612 case IPV6_FLOWINFO: 613 if (optlen < sizeof(int)) 614 goto e_inval; 615 np->rxopt.bits.rxflow = valbool; 616 retv = 0; 617 break; 618 619 case IPV6_RECVPATHMTU: 620 if (optlen < sizeof(int)) 621 goto e_inval; 622 np->rxopt.bits.rxpmtu = valbool; 623 retv = 0; 624 break; 625 626 case IPV6_TRANSPARENT: 627 if (valbool && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW) && 628 !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) { 629 retv = -EPERM; 630 break; 631 } 632 if (optlen < sizeof(int)) 633 goto e_inval; 634 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */ 635 inet_assign_bit(TRANSPARENT, sk, valbool); 636 retv = 0; 637 break; 638 639 case IPV6_FREEBIND: 640 if (optlen < sizeof(int)) 641 goto e_inval; 642 /* we also don't have a separate freebind bit for IPV6 */ 643 inet_assign_bit(FREEBIND, sk, valbool); 644 retv = 0; 645 break; 646 647 case IPV6_RECVORIGDSTADDR: 648 if (optlen < sizeof(int)) 649 goto e_inval; 650 np->rxopt.bits.rxorigdstaddr = valbool; 651 retv = 0; 652 break; 653 654 case IPV6_HOPOPTS: 655 case IPV6_RTHDRDSTOPTS: 656 case IPV6_RTHDR: 657 case IPV6_DSTOPTS: 658 retv = ipv6_set_opt_hdr(sk, optname, optval, optlen); 659 break; 660 661 case IPV6_PKTINFO: 662 { 663 struct in6_pktinfo pkt; 664 665 if (optlen == 0) 666 goto e_inval; 667 else if (optlen < sizeof(struct in6_pktinfo) || 668 sockptr_is_null(optval)) 669 goto e_inval; 670 671 if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) { 672 retv = -EFAULT; 673 break; 674 } 675 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex)) 676 goto e_inval; 677 678 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex; 679 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr; 680 retv = 0; 681 break; 682 } 683 684 case IPV6_2292PKTOPTIONS: 685 { 686 struct ipv6_txoptions *opt = NULL; 687 struct msghdr msg; 688 struct flowi6 fl6; 689 struct ipcm6_cookie ipc6; 690 691 memset(&fl6, 0, sizeof(fl6)); 692 fl6.flowi6_oif = sk->sk_bound_dev_if; 693 fl6.flowi6_mark = sk->sk_mark; 694 695 if (optlen == 0) 696 goto update; 697 698 /* 1K is probably excessive 699 * 1K is surely not enough, 2K per standard header is 16K. 700 */ 701 retv = -EINVAL; 702 if (optlen > 64*1024) 703 break; 704 705 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); 706 retv = -ENOBUFS; 707 if (!opt) 708 break; 709 710 memset(opt, 0, sizeof(*opt)); 711 refcount_set(&opt->refcnt, 1); 712 opt->tot_len = sizeof(*opt) + optlen; 713 retv = -EFAULT; 714 if (copy_from_sockptr(opt + 1, optval, optlen)) 715 goto done; 716 717 msg.msg_controllen = optlen; 718 msg.msg_control_is_user = false; 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_test_bit(IS_ICSK, sk)) 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_errqueue_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 (!sockopt_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 unlock: 989 sockopt_release_sock(sk); 990 if (needs_rtnl) 991 rtnl_unlock(); 992 993 return retv; 994 995 e_inval: 996 retv = -EINVAL; 997 goto unlock; 998 } 999 1000 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 1001 unsigned int optlen) 1002 { 1003 int err; 1004 1005 if (level == SOL_IP && sk->sk_type != SOCK_RAW) 1006 return udp_prot.setsockopt(sk, level, optname, optval, optlen); 1007 1008 if (level != SOL_IPV6) 1009 return -ENOPROTOOPT; 1010 1011 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen); 1012 #ifdef CONFIG_NETFILTER 1013 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1014 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY && 1015 optname != IPV6_XFRM_POLICY) 1016 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen); 1017 #endif 1018 return err; 1019 } 1020 EXPORT_SYMBOL(ipv6_setsockopt); 1021 1022 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt, 1023 int optname, sockptr_t optval, int len) 1024 { 1025 struct ipv6_opt_hdr *hdr; 1026 1027 if (!opt) 1028 return 0; 1029 1030 switch (optname) { 1031 case IPV6_HOPOPTS: 1032 hdr = opt->hopopt; 1033 break; 1034 case IPV6_RTHDRDSTOPTS: 1035 hdr = opt->dst0opt; 1036 break; 1037 case IPV6_RTHDR: 1038 hdr = (struct ipv6_opt_hdr *)opt->srcrt; 1039 break; 1040 case IPV6_DSTOPTS: 1041 hdr = opt->dst1opt; 1042 break; 1043 default: 1044 return -EINVAL; /* should not happen */ 1045 } 1046 1047 if (!hdr) 1048 return 0; 1049 1050 len = min_t(unsigned int, len, ipv6_optlen(hdr)); 1051 if (copy_to_sockptr(optval, hdr, len)) 1052 return -EFAULT; 1053 return len; 1054 } 1055 1056 static int ipv6_get_msfilter(struct sock *sk, sockptr_t optval, 1057 sockptr_t optlen, int len) 1058 { 1059 const int size0 = offsetof(struct group_filter, gf_slist_flex); 1060 struct group_filter gsf; 1061 int num; 1062 int err; 1063 1064 if (len < size0) 1065 return -EINVAL; 1066 if (copy_from_sockptr(&gsf, optval, size0)) 1067 return -EFAULT; 1068 if (gsf.gf_group.ss_family != AF_INET6) 1069 return -EADDRNOTAVAIL; 1070 num = gsf.gf_numsrc; 1071 sockopt_lock_sock(sk); 1072 err = ip6_mc_msfget(sk, &gsf, optval, size0); 1073 if (!err) { 1074 if (num > gsf.gf_numsrc) 1075 num = gsf.gf_numsrc; 1076 len = GROUP_FILTER_SIZE(num); 1077 if (copy_to_sockptr(optlen, &len, sizeof(int)) || 1078 copy_to_sockptr(optval, &gsf, size0)) 1079 err = -EFAULT; 1080 } 1081 sockopt_release_sock(sk); 1082 return err; 1083 } 1084 1085 static int compat_ipv6_get_msfilter(struct sock *sk, sockptr_t optval, 1086 sockptr_t optlen, int len) 1087 { 1088 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); 1089 struct compat_group_filter gf32; 1090 struct group_filter gf; 1091 int err; 1092 int num; 1093 1094 if (len < size0) 1095 return -EINVAL; 1096 1097 if (copy_from_sockptr(&gf32, optval, size0)) 1098 return -EFAULT; 1099 gf.gf_interface = gf32.gf_interface; 1100 gf.gf_fmode = gf32.gf_fmode; 1101 num = gf.gf_numsrc = gf32.gf_numsrc; 1102 gf.gf_group = gf32.gf_group; 1103 1104 if (gf.gf_group.ss_family != AF_INET6) 1105 return -EADDRNOTAVAIL; 1106 1107 sockopt_lock_sock(sk); 1108 err = ip6_mc_msfget(sk, &gf, optval, size0); 1109 sockopt_release_sock(sk); 1110 if (err) 1111 return err; 1112 if (num > gf.gf_numsrc) 1113 num = gf.gf_numsrc; 1114 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32)); 1115 if (copy_to_sockptr(optlen, &len, sizeof(int)) || 1116 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode), 1117 &gf.gf_fmode, sizeof(gf32.gf_fmode)) || 1118 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc), 1119 &gf.gf_numsrc, sizeof(gf32.gf_numsrc))) 1120 return -EFAULT; 1121 return 0; 1122 } 1123 1124 int do_ipv6_getsockopt(struct sock *sk, int level, int optname, 1125 sockptr_t optval, sockptr_t optlen) 1126 { 1127 struct ipv6_pinfo *np = inet6_sk(sk); 1128 int len; 1129 int val; 1130 1131 if (ip6_mroute_opt(optname)) 1132 return ip6_mroute_getsockopt(sk, optname, optval, optlen); 1133 1134 if (copy_from_sockptr(&len, optlen, sizeof(int))) 1135 return -EFAULT; 1136 switch (optname) { 1137 case IPV6_ADDRFORM: 1138 if (sk->sk_protocol != IPPROTO_UDP && 1139 sk->sk_protocol != IPPROTO_UDPLITE && 1140 sk->sk_protocol != IPPROTO_TCP) 1141 return -ENOPROTOOPT; 1142 if (sk->sk_state != TCP_ESTABLISHED) 1143 return -ENOTCONN; 1144 val = sk->sk_family; 1145 break; 1146 case MCAST_MSFILTER: 1147 if (in_compat_syscall()) 1148 return compat_ipv6_get_msfilter(sk, optval, optlen, len); 1149 return ipv6_get_msfilter(sk, optval, optlen, len); 1150 case IPV6_2292PKTOPTIONS: 1151 { 1152 struct msghdr msg; 1153 struct sk_buff *skb; 1154 1155 if (sk->sk_type != SOCK_STREAM) 1156 return -ENOPROTOOPT; 1157 1158 if (optval.is_kernel) { 1159 msg.msg_control_is_user = false; 1160 msg.msg_control = optval.kernel; 1161 } else { 1162 msg.msg_control_is_user = true; 1163 msg.msg_control_user = optval.user; 1164 } 1165 msg.msg_controllen = len; 1166 msg.msg_flags = 0; 1167 1168 sockopt_lock_sock(sk); 1169 skb = np->pktoptions; 1170 if (skb) 1171 ip6_datagram_recv_ctl(sk, &msg, skb); 1172 sockopt_release_sock(sk); 1173 if (!skb) { 1174 if (np->rxopt.bits.rxinfo) { 1175 struct in6_pktinfo src_info; 1176 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : 1177 np->sticky_pktinfo.ipi6_ifindex; 1178 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr; 1179 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); 1180 } 1181 if (np->rxopt.bits.rxhlim) { 1182 int hlim = np->mcast_hops; 1183 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim); 1184 } 1185 if (np->rxopt.bits.rxtclass) { 1186 int tclass = (int)ip6_tclass(np->rcv_flowinfo); 1187 1188 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass); 1189 } 1190 if (np->rxopt.bits.rxoinfo) { 1191 struct in6_pktinfo src_info; 1192 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : 1193 np->sticky_pktinfo.ipi6_ifindex; 1194 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : 1195 np->sticky_pktinfo.ipi6_addr; 1196 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); 1197 } 1198 if (np->rxopt.bits.rxohlim) { 1199 int hlim = np->mcast_hops; 1200 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim); 1201 } 1202 if (np->rxopt.bits.rxflow) { 1203 __be32 flowinfo = np->rcv_flowinfo; 1204 1205 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo); 1206 } 1207 } 1208 len -= msg.msg_controllen; 1209 return copy_to_sockptr(optlen, &len, sizeof(int)); 1210 } 1211 case IPV6_MTU: 1212 { 1213 struct dst_entry *dst; 1214 1215 val = 0; 1216 rcu_read_lock(); 1217 dst = __sk_dst_get(sk); 1218 if (dst) 1219 val = dst_mtu(dst); 1220 rcu_read_unlock(); 1221 if (!val) 1222 return -ENOTCONN; 1223 break; 1224 } 1225 1226 case IPV6_V6ONLY: 1227 val = sk->sk_ipv6only; 1228 break; 1229 1230 case IPV6_RECVPKTINFO: 1231 val = np->rxopt.bits.rxinfo; 1232 break; 1233 1234 case IPV6_2292PKTINFO: 1235 val = np->rxopt.bits.rxoinfo; 1236 break; 1237 1238 case IPV6_RECVHOPLIMIT: 1239 val = np->rxopt.bits.rxhlim; 1240 break; 1241 1242 case IPV6_2292HOPLIMIT: 1243 val = np->rxopt.bits.rxohlim; 1244 break; 1245 1246 case IPV6_RECVRTHDR: 1247 val = np->rxopt.bits.srcrt; 1248 break; 1249 1250 case IPV6_2292RTHDR: 1251 val = np->rxopt.bits.osrcrt; 1252 break; 1253 1254 case IPV6_HOPOPTS: 1255 case IPV6_RTHDRDSTOPTS: 1256 case IPV6_RTHDR: 1257 case IPV6_DSTOPTS: 1258 { 1259 struct ipv6_txoptions *opt; 1260 1261 sockopt_lock_sock(sk); 1262 opt = rcu_dereference_protected(np->opt, 1263 lockdep_sock_is_held(sk)); 1264 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len); 1265 sockopt_release_sock(sk); 1266 /* check if ipv6_getsockopt_sticky() returns err code */ 1267 if (len < 0) 1268 return len; 1269 return copy_to_sockptr(optlen, &len, sizeof(int)); 1270 } 1271 1272 case IPV6_RECVHOPOPTS: 1273 val = np->rxopt.bits.hopopts; 1274 break; 1275 1276 case IPV6_2292HOPOPTS: 1277 val = np->rxopt.bits.ohopopts; 1278 break; 1279 1280 case IPV6_RECVDSTOPTS: 1281 val = np->rxopt.bits.dstopts; 1282 break; 1283 1284 case IPV6_2292DSTOPTS: 1285 val = np->rxopt.bits.odstopts; 1286 break; 1287 1288 case IPV6_TCLASS: 1289 val = np->tclass; 1290 break; 1291 1292 case IPV6_RECVTCLASS: 1293 val = np->rxopt.bits.rxtclass; 1294 break; 1295 1296 case IPV6_FLOWINFO: 1297 val = np->rxopt.bits.rxflow; 1298 break; 1299 1300 case IPV6_RECVPATHMTU: 1301 val = np->rxopt.bits.rxpmtu; 1302 break; 1303 1304 case IPV6_PATHMTU: 1305 { 1306 struct dst_entry *dst; 1307 struct ip6_mtuinfo mtuinfo; 1308 1309 if (len < sizeof(mtuinfo)) 1310 return -EINVAL; 1311 1312 len = sizeof(mtuinfo); 1313 memset(&mtuinfo, 0, sizeof(mtuinfo)); 1314 1315 rcu_read_lock(); 1316 dst = __sk_dst_get(sk); 1317 if (dst) 1318 mtuinfo.ip6m_mtu = dst_mtu(dst); 1319 rcu_read_unlock(); 1320 if (!mtuinfo.ip6m_mtu) 1321 return -ENOTCONN; 1322 1323 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1324 return -EFAULT; 1325 if (copy_to_sockptr(optval, &mtuinfo, len)) 1326 return -EFAULT; 1327 1328 return 0; 1329 } 1330 1331 case IPV6_TRANSPARENT: 1332 val = inet_test_bit(TRANSPARENT, sk); 1333 break; 1334 1335 case IPV6_FREEBIND: 1336 val = inet_test_bit(FREEBIND, sk); 1337 break; 1338 1339 case IPV6_RECVORIGDSTADDR: 1340 val = np->rxopt.bits.rxorigdstaddr; 1341 break; 1342 1343 case IPV6_UNICAST_HOPS: 1344 case IPV6_MULTICAST_HOPS: 1345 { 1346 struct dst_entry *dst; 1347 1348 if (optname == IPV6_UNICAST_HOPS) 1349 val = np->hop_limit; 1350 else 1351 val = np->mcast_hops; 1352 1353 if (val < 0) { 1354 rcu_read_lock(); 1355 dst = __sk_dst_get(sk); 1356 if (dst) 1357 val = ip6_dst_hoplimit(dst); 1358 rcu_read_unlock(); 1359 } 1360 1361 if (val < 0) 1362 val = sock_net(sk)->ipv6.devconf_all->hop_limit; 1363 break; 1364 } 1365 1366 case IPV6_MULTICAST_LOOP: 1367 val = np->mc_loop; 1368 break; 1369 1370 case IPV6_MULTICAST_IF: 1371 val = np->mcast_oif; 1372 break; 1373 1374 case IPV6_MULTICAST_ALL: 1375 val = np->mc_all; 1376 break; 1377 1378 case IPV6_UNICAST_IF: 1379 val = (__force int)htonl((__u32) np->ucast_oif); 1380 break; 1381 1382 case IPV6_MTU_DISCOVER: 1383 val = np->pmtudisc; 1384 break; 1385 1386 case IPV6_RECVERR: 1387 val = np->recverr; 1388 break; 1389 1390 case IPV6_FLOWINFO_SEND: 1391 val = np->sndflow; 1392 break; 1393 1394 case IPV6_FLOWLABEL_MGR: 1395 { 1396 struct in6_flowlabel_req freq; 1397 int flags; 1398 1399 if (len < sizeof(freq)) 1400 return -EINVAL; 1401 1402 if (copy_from_sockptr(&freq, optval, sizeof(freq))) 1403 return -EFAULT; 1404 1405 if (freq.flr_action != IPV6_FL_A_GET) 1406 return -EINVAL; 1407 1408 len = sizeof(freq); 1409 flags = freq.flr_flags; 1410 1411 memset(&freq, 0, sizeof(freq)); 1412 1413 val = ipv6_flowlabel_opt_get(sk, &freq, flags); 1414 if (val < 0) 1415 return val; 1416 1417 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1418 return -EFAULT; 1419 if (copy_to_sockptr(optval, &freq, len)) 1420 return -EFAULT; 1421 1422 return 0; 1423 } 1424 1425 case IPV6_ADDR_PREFERENCES: 1426 val = 0; 1427 1428 if (np->srcprefs & IPV6_PREFER_SRC_TMP) 1429 val |= IPV6_PREFER_SRC_TMP; 1430 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC) 1431 val |= IPV6_PREFER_SRC_PUBLIC; 1432 else { 1433 /* XXX: should we return system default? */ 1434 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT; 1435 } 1436 1437 if (np->srcprefs & IPV6_PREFER_SRC_COA) 1438 val |= IPV6_PREFER_SRC_COA; 1439 else 1440 val |= IPV6_PREFER_SRC_HOME; 1441 break; 1442 1443 case IPV6_MINHOPCOUNT: 1444 val = np->min_hopcount; 1445 break; 1446 1447 case IPV6_DONTFRAG: 1448 val = np->dontfrag; 1449 break; 1450 1451 case IPV6_AUTOFLOWLABEL: 1452 val = ip6_autoflowlabel(sock_net(sk), np); 1453 break; 1454 1455 case IPV6_RECVFRAGSIZE: 1456 val = np->rxopt.bits.recvfragsize; 1457 break; 1458 1459 case IPV6_ROUTER_ALERT_ISOLATE: 1460 val = np->rtalert_isolate; 1461 break; 1462 1463 case IPV6_RECVERR_RFC4884: 1464 val = np->recverr_rfc4884; 1465 break; 1466 1467 default: 1468 return -ENOPROTOOPT; 1469 } 1470 len = min_t(unsigned int, sizeof(int), len); 1471 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1472 return -EFAULT; 1473 if (copy_to_sockptr(optval, &val, len)) 1474 return -EFAULT; 1475 return 0; 1476 } 1477 1478 int ipv6_getsockopt(struct sock *sk, int level, int optname, 1479 char __user *optval, int __user *optlen) 1480 { 1481 int err; 1482 1483 if (level == SOL_IP && sk->sk_type != SOCK_RAW) 1484 return udp_prot.getsockopt(sk, level, optname, optval, optlen); 1485 1486 if (level != SOL_IPV6) 1487 return -ENOPROTOOPT; 1488 1489 err = do_ipv6_getsockopt(sk, level, optname, 1490 USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); 1491 #ifdef CONFIG_NETFILTER 1492 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1493 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) { 1494 int len; 1495 1496 if (get_user(len, optlen)) 1497 return -EFAULT; 1498 1499 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len); 1500 if (err >= 0) 1501 err = put_user(len, optlen); 1502 } 1503 #endif 1504 return err; 1505 } 1506 EXPORT_SYMBOL(ipv6_getsockopt); 1507