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