1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPv6 BSD socket options interface
4 * Linux INET6 implementation
7 * Pedro Roque <roque@di.fc.ul.pt>
9 * Based on linux/net/ipv4/ip_sockglue.c
11 * FIXME: Make the setsockopt code POSIX compliant: That is
13 * o Truncate getsockopt returns
14 * o Return an optlen of the truncated length if need be
17 * David L Stevens <dlstevens@us.ibm.com>:
18 * - added multicast source filtering API for MLDv2
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>
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>
48 #include <net/udplite.h>
50 #include <net/compat.h>
53 #include <linux/uaccess.h>
55 struct ip6_ra_chain *ip6_ra_chain;
56 DEFINE_RWLOCK(ip6_ra_lock);
58 DEFINE_STATIC_KEY_FALSE(ip6_min_hopcount);
60 int ip6_ra_control(struct sock *sk, int sel)
62 struct ip6_ra_chain *ra, *new_ra, **rap;
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)
68 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
69 if (sel >= 0 && !new_ra)
72 write_lock_bh(&ip6_ra_lock);
73 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
76 write_unlock_bh(&ip6_ra_lock);
82 write_unlock_bh(&ip6_ra_lock);
90 write_unlock_bh(&ip6_ra_lock);
98 write_unlock_bh(&ip6_ra_lock);
102 struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
103 struct ipv6_txoptions *opt)
105 if (inet_sk(sk)->is_icsk) {
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);
114 opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
121 static bool setsockopt_needs_rtnl(int optname)
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:
141 static int copy_group_source_from_sockptr(struct group_source_req *greqs,
142 sockptr_t optval, int optlen)
144 if (in_compat_syscall()) {
145 struct compat_group_source_req gr32;
147 if (optlen < sizeof(gr32))
149 if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
151 greqs->gsr_interface = gr32.gsr_interface;
152 greqs->gsr_group = gr32.gsr_group;
153 greqs->gsr_source = gr32.gsr_source;
155 if (optlen < sizeof(*greqs))
157 if (copy_from_sockptr(greqs, optval, sizeof(*greqs)))
164 static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
165 sockptr_t optval, int optlen)
167 struct group_source_req greqs;
171 ret = copy_group_source_from_sockptr(&greqs, optval, optlen);
175 if (greqs.gsr_group.ss_family != AF_INET6 ||
176 greqs.gsr_source.ss_family != AF_INET6)
177 return -EADDRNOTAVAIL;
179 if (optname == MCAST_BLOCK_SOURCE) {
180 omode = MCAST_EXCLUDE;
182 } else if (optname == MCAST_UNBLOCK_SOURCE) {
183 omode = MCAST_EXCLUDE;
185 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
186 struct sockaddr_in6 *psin6;
189 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
190 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
193 /* prior join w/ different source is ok */
194 if (retv && retv != -EADDRINUSE)
196 omode = MCAST_INCLUDE;
198 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
199 omode = MCAST_INCLUDE;
202 return ip6_mc_source(add, omode, sk, &greqs);
205 static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
208 struct group_filter *gsf;
211 if (optlen < GROUP_FILTER_SIZE(0))
213 if (optlen > sysctl_optmem_max)
216 gsf = memdup_sockptr(optval, optlen);
220 /* numsrc >= (4G-140)/128 overflow in 32 bits */
222 if (gsf->gf_numsrc >= 0x1ffffffU ||
223 gsf->gf_numsrc > sysctl_mld_max_msf)
227 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
230 ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex);
236 static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
239 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
240 struct compat_group_filter *gf32;
247 if (optlen > sysctl_optmem_max - 4)
250 p = kmalloc(optlen + 4, GFP_KERNEL);
254 gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
256 if (copy_from_sockptr(gf32, optval, optlen))
259 /* numsrc >= (4G-140)/128 overflow in 32 bits */
262 if (n >= 0x1ffffffU || n > sysctl_mld_max_msf)
266 if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
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);
280 static int ipv6_mcast_join_leave(struct sock *sk, int optname,
281 sockptr_t optval, int optlen)
283 struct sockaddr_in6 *psin6;
284 struct group_req greq;
286 if (optlen < sizeof(greq))
288 if (copy_from_sockptr(&greq, optval, sizeof(greq)))
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,
297 return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr);
300 static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname,
301 sockptr_t optval, int optlen)
303 struct compat_group_req gr32;
304 struct sockaddr_in6 *psin6;
306 if (optlen < sizeof(gr32))
308 if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
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,
317 return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr);
320 static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval,
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;
329 /* hop-by-hop / destination options are privileged option */
330 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
333 /* remove any sticky options header with a zero option
334 * length, per RFC3542.
337 if (sockptr_is_null(optval))
339 if (optlen < sizeof(struct ipv6_opt_hdr) ||
344 new = memdup_sockptr(optval, optlen);
347 if (unlikely(ipv6_optlen(new) > optlen)) {
353 opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
354 opt = ipv6_renew_options(sk, opt, optname, new);
359 /* routing header option needs extra check */
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)
370 case IPV6_SRCRT_TYPE_4:
372 struct ipv6_sr_hdr *srh =
373 (struct ipv6_sr_hdr *)opt->srcrt;
375 if (!seg6_validate_srh(srh, optlen, false))
385 opt = ipv6_update_options(sk, opt);
388 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
394 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
395 sockptr_t optval, unsigned int optlen)
397 struct ipv6_pinfo *np = inet6_sk(sk);
398 struct net *net = sock_net(sk);
400 int retv = -ENOPROTOOPT;
401 bool needs_rtnl = setsockopt_needs_rtnl(optname);
403 if (sockptr_is_null(optval))
406 if (optlen >= sizeof(int)) {
407 if (copy_from_sockptr(&val, optval, sizeof(val)))
413 valbool = (val != 0);
415 if (ip6_mroute_opt(optname))
416 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
425 if (optlen < sizeof(int))
427 if (val == PF_INET) {
428 struct ipv6_txoptions *opt;
429 struct sk_buff *pktopt;
431 if (sk->sk_type == SOCK_RAW)
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) {
441 } else if (sk->sk_protocol == IPPROTO_TCP) {
442 if (sk->sk_prot != &tcpv6_prot) {
450 if (sk->sk_state != TCP_ESTABLISHED) {
455 if (ipv6_only_sock(sk) ||
456 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
457 retv = -EADDRNOTAVAIL;
461 fl6_free_socklist(sk);
462 __ipv6_sock_mc_close(sk);
463 __ipv6_sock_ac_close(sk);
466 * Sock is moving from IPv6 to IPv4 (sk_prot), so
467 * remove it from the refcnt debug socks count in the
470 sk_refcnt_debug_dec(sk);
472 if (sk->sk_protocol == IPPROTO_TCP) {
473 struct inet_connection_sock *icsk = inet_csk(sk);
475 sock_prot_inuse_add(net, sk->sk_prot, -1);
476 sock_prot_inuse_add(net, &tcp_prot, 1);
478 /* Paired with READ_ONCE(sk->sk_prot) in net/ipv6/af_inet6.c */
479 WRITE_ONCE(sk->sk_prot, &tcp_prot);
480 icsk->icsk_af_ops = &ipv4_specific;
481 sk->sk_socket->ops = &inet_stream_ops;
482 sk->sk_family = PF_INET;
483 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
485 struct proto *prot = &udp_prot;
487 if (sk->sk_protocol == IPPROTO_UDPLITE)
488 prot = &udplite_prot;
490 sock_prot_inuse_add(net, sk->sk_prot, -1);
491 sock_prot_inuse_add(net, prot, 1);
493 /* Paired with READ_ONCE(sk->sk_prot) in net/ipv6/af_inet6.c */
494 WRITE_ONCE(sk->sk_prot, prot);
495 sk->sk_socket->ops = &inet_dgram_ops;
496 sk->sk_family = PF_INET;
498 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
501 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
504 pktopt = xchg(&np->pktoptions, NULL);
508 * ... and add it to the refcnt debug socks count
509 * in the new family. -acme
511 sk_refcnt_debug_inc(sk);
512 module_put(THIS_MODULE);
519 if (optlen < sizeof(int) ||
520 inet_sk(sk)->inet_num)
522 sk->sk_ipv6only = valbool;
526 case IPV6_RECVPKTINFO:
527 if (optlen < sizeof(int))
529 np->rxopt.bits.rxinfo = valbool;
533 case IPV6_2292PKTINFO:
534 if (optlen < sizeof(int))
536 np->rxopt.bits.rxoinfo = valbool;
540 case IPV6_RECVHOPLIMIT:
541 if (optlen < sizeof(int))
543 np->rxopt.bits.rxhlim = valbool;
547 case IPV6_2292HOPLIMIT:
548 if (optlen < sizeof(int))
550 np->rxopt.bits.rxohlim = valbool;
555 if (optlen < sizeof(int))
557 np->rxopt.bits.srcrt = valbool;
562 if (optlen < sizeof(int))
564 np->rxopt.bits.osrcrt = valbool;
568 case IPV6_RECVHOPOPTS:
569 if (optlen < sizeof(int))
571 np->rxopt.bits.hopopts = valbool;
575 case IPV6_2292HOPOPTS:
576 if (optlen < sizeof(int))
578 np->rxopt.bits.ohopopts = valbool;
582 case IPV6_RECVDSTOPTS:
583 if (optlen < sizeof(int))
585 np->rxopt.bits.dstopts = valbool;
589 case IPV6_2292DSTOPTS:
590 if (optlen < sizeof(int))
592 np->rxopt.bits.odstopts = valbool;
597 if (optlen < sizeof(int))
599 if (val < -1 || val > 0xff)
601 /* RFC 3542, 6.5: default traffic class of 0x0 */
604 if (sk->sk_type == SOCK_STREAM) {
605 val &= ~INET_ECN_MASK;
606 val |= np->tclass & INET_ECN_MASK;
608 if (np->tclass != val) {
615 case IPV6_RECVTCLASS:
616 if (optlen < sizeof(int))
618 np->rxopt.bits.rxtclass = valbool;
623 if (optlen < sizeof(int))
625 np->rxopt.bits.rxflow = valbool;
629 case IPV6_RECVPATHMTU:
630 if (optlen < sizeof(int))
632 np->rxopt.bits.rxpmtu = valbool;
636 case IPV6_TRANSPARENT:
637 if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) &&
638 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
642 if (optlen < sizeof(int))
644 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
645 inet_sk(sk)->transparent = valbool;
650 if (optlen < sizeof(int))
652 /* we also don't have a separate freebind bit for IPV6 */
653 inet_sk(sk)->freebind = valbool;
657 case IPV6_RECVORIGDSTADDR:
658 if (optlen < sizeof(int))
660 np->rxopt.bits.rxorigdstaddr = valbool;
665 case IPV6_RTHDRDSTOPTS:
668 retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
673 struct in6_pktinfo pkt;
677 else if (optlen < sizeof(struct in6_pktinfo) ||
678 sockptr_is_null(optval))
681 if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) {
685 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
688 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
689 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
694 case IPV6_2292PKTOPTIONS:
696 struct ipv6_txoptions *opt = NULL;
699 struct ipcm6_cookie ipc6;
701 memset(&fl6, 0, sizeof(fl6));
702 fl6.flowi6_oif = sk->sk_bound_dev_if;
703 fl6.flowi6_mark = sk->sk_mark;
708 /* 1K is probably excessive
709 * 1K is surely not enough, 2K per standard header is 16K.
712 if (optlen > 64*1024)
715 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
720 memset(opt, 0, sizeof(*opt));
721 refcount_set(&opt->refcnt, 1);
722 opt->tot_len = sizeof(*opt) + optlen;
724 if (copy_from_sockptr(opt + 1, optval, optlen))
727 msg.msg_controllen = optlen;
728 msg.msg_control = (void *)(opt+1);
731 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
736 opt = ipv6_update_options(sk, opt);
739 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
744 case IPV6_UNICAST_HOPS:
745 if (optlen < sizeof(int))
747 if (val > 255 || val < -1)
753 case IPV6_MULTICAST_HOPS:
754 if (sk->sk_type == SOCK_STREAM)
756 if (optlen < sizeof(int))
758 if (val > 255 || val < -1)
760 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
764 case IPV6_MULTICAST_LOOP:
765 if (optlen < sizeof(int))
769 np->mc_loop = valbool;
773 case IPV6_UNICAST_IF:
775 struct net_device *dev = NULL;
778 if (optlen != sizeof(int))
781 ifindex = (__force int)ntohl((__force __be32)val);
788 dev = dev_get_by_index(net, ifindex);
789 retv = -EADDRNOTAVAIL;
795 if (sk->sk_bound_dev_if)
798 np->ucast_oif = ifindex;
803 case IPV6_MULTICAST_IF:
804 if (sk->sk_type == SOCK_STREAM)
806 if (optlen < sizeof(int))
810 struct net_device *dev;
815 dev = dev_get_by_index_rcu(net, val);
821 midx = l3mdev_master_ifindex_rcu(dev);
825 if (sk->sk_bound_dev_if &&
826 sk->sk_bound_dev_if != val &&
827 (!midx || midx != sk->sk_bound_dev_if))
833 case IPV6_ADD_MEMBERSHIP:
834 case IPV6_DROP_MEMBERSHIP:
836 struct ipv6_mreq mreq;
838 if (optlen < sizeof(struct ipv6_mreq))
842 if (inet_sk(sk)->is_icsk)
846 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
849 if (optname == IPV6_ADD_MEMBERSHIP)
850 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
852 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
855 case IPV6_JOIN_ANYCAST:
856 case IPV6_LEAVE_ANYCAST:
858 struct ipv6_mreq mreq;
860 if (optlen < sizeof(struct ipv6_mreq))
864 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
867 if (optname == IPV6_JOIN_ANYCAST)
868 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
870 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
873 case IPV6_MULTICAST_ALL:
874 if (optlen < sizeof(int))
876 np->mc_all = valbool;
880 case MCAST_JOIN_GROUP:
881 case MCAST_LEAVE_GROUP:
882 if (in_compat_syscall())
883 retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
886 retv = ipv6_mcast_join_leave(sk, optname, optval,
889 case MCAST_JOIN_SOURCE_GROUP:
890 case MCAST_LEAVE_SOURCE_GROUP:
891 case MCAST_BLOCK_SOURCE:
892 case MCAST_UNBLOCK_SOURCE:
893 retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
896 if (in_compat_syscall())
897 retv = compat_ipv6_set_mcast_msfilter(sk, optval,
900 retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
902 case IPV6_ROUTER_ALERT:
903 if (optlen < sizeof(int))
905 retv = ip6_ra_control(sk, val);
907 case IPV6_ROUTER_ALERT_ISOLATE:
908 if (optlen < sizeof(int))
910 np->rtalert_isolate = valbool;
913 case IPV6_MTU_DISCOVER:
914 if (optlen < sizeof(int))
916 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
922 if (optlen < sizeof(int))
924 if (val && val < IPV6_MIN_MTU)
930 if (optlen < sizeof(int))
932 np->recverr = valbool;
934 skb_queue_purge(&sk->sk_error_queue);
937 case IPV6_FLOWINFO_SEND:
938 if (optlen < sizeof(int))
940 np->sndflow = valbool;
943 case IPV6_FLOWLABEL_MGR:
944 retv = ipv6_flowlabel_opt(sk, optval, optlen);
946 case IPV6_IPSEC_POLICY:
947 case IPV6_XFRM_POLICY:
949 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
951 retv = xfrm_user_policy(sk, optname, optval, optlen);
954 case IPV6_ADDR_PREFERENCES:
955 if (optlen < sizeof(int))
957 retv = __ip6_sock_set_addr_preferences(sk, val);
959 case IPV6_MINHOPCOUNT:
960 if (optlen < sizeof(int))
962 if (val < 0 || val > 255)
966 static_branch_enable(&ip6_min_hopcount);
968 /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount
969 * while we are changing it.
971 WRITE_ONCE(np->min_hopcount, val);
975 np->dontfrag = valbool;
978 case IPV6_AUTOFLOWLABEL:
979 np->autoflowlabel = valbool;
980 np->autoflowlabel_set = 1;
983 case IPV6_RECVFRAGSIZE:
984 np->rxopt.bits.recvfragsize = valbool;
987 case IPV6_RECVERR_RFC4884:
988 if (optlen < sizeof(int))
990 if (val < 0 || val > 1)
992 np->recverr_rfc4884 = valbool;
1010 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1011 unsigned int optlen)
1015 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1016 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1018 if (level != SOL_IPV6)
1019 return -ENOPROTOOPT;
1021 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1022 #ifdef CONFIG_NETFILTER
1023 /* we need to exclude all possible ENOPROTOOPTs except default case */
1024 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
1025 optname != IPV6_XFRM_POLICY)
1026 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
1030 EXPORT_SYMBOL(ipv6_setsockopt);
1032 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
1033 int optname, char __user *optval, int len)
1035 struct ipv6_opt_hdr *hdr;
1044 case IPV6_RTHDRDSTOPTS:
1048 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1054 return -EINVAL; /* should not happen */
1060 len = min_t(unsigned int, len, ipv6_optlen(hdr));
1061 if (copy_to_user(optval, hdr, len))
1066 static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
1067 int __user *optlen, int len)
1069 const int size0 = offsetof(struct group_filter, gf_slist_flex);
1070 struct group_filter __user *p = optval;
1071 struct group_filter gsf;
1077 if (copy_from_user(&gsf, p, size0))
1079 if (gsf.gf_group.ss_family != AF_INET6)
1080 return -EADDRNOTAVAIL;
1081 num = gsf.gf_numsrc;
1083 err = ip6_mc_msfget(sk, &gsf, p->gf_slist_flex);
1085 if (num > gsf.gf_numsrc)
1086 num = gsf.gf_numsrc;
1087 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1088 copy_to_user(p, &gsf, size0))
1095 static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval,
1098 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1099 struct compat_group_filter __user *p = optval;
1100 struct compat_group_filter gf32;
1101 struct group_filter gf;
1105 if (get_user(len, optlen))
1110 if (copy_from_user(&gf32, p, size0))
1112 gf.gf_interface = gf32.gf_interface;
1113 gf.gf_fmode = gf32.gf_fmode;
1114 num = gf.gf_numsrc = gf32.gf_numsrc;
1115 gf.gf_group = gf32.gf_group;
1117 if (gf.gf_group.ss_family != AF_INET6)
1118 return -EADDRNOTAVAIL;
1121 err = ip6_mc_msfget(sk, &gf, p->gf_slist_flex);
1125 if (num > gf.gf_numsrc)
1127 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1128 if (put_user(len, optlen) ||
1129 put_user(gf.gf_fmode, &p->gf_fmode) ||
1130 put_user(gf.gf_numsrc, &p->gf_numsrc))
1135 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1136 char __user *optval, int __user *optlen, unsigned int flags)
1138 struct ipv6_pinfo *np = inet6_sk(sk);
1142 if (ip6_mroute_opt(optname))
1143 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1145 if (get_user(len, optlen))
1149 if (sk->sk_protocol != IPPROTO_UDP &&
1150 sk->sk_protocol != IPPROTO_UDPLITE &&
1151 sk->sk_protocol != IPPROTO_TCP)
1152 return -ENOPROTOOPT;
1153 if (sk->sk_state != TCP_ESTABLISHED)
1155 val = sk->sk_family;
1157 case MCAST_MSFILTER:
1158 if (in_compat_syscall())
1159 return compat_ipv6_get_msfilter(sk, optval, optlen);
1160 return ipv6_get_msfilter(sk, optval, optlen, len);
1161 case IPV6_2292PKTOPTIONS:
1164 struct sk_buff *skb;
1166 if (sk->sk_type != SOCK_STREAM)
1167 return -ENOPROTOOPT;
1169 msg.msg_control_user = optval;
1170 msg.msg_controllen = len;
1171 msg.msg_flags = flags;
1172 msg.msg_control_is_user = true;
1175 skb = np->pktoptions;
1177 ip6_datagram_recv_ctl(sk, &msg, skb);
1180 if (np->rxopt.bits.rxinfo) {
1181 struct in6_pktinfo src_info;
1182 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1183 np->sticky_pktinfo.ipi6_ifindex;
1184 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1185 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1187 if (np->rxopt.bits.rxhlim) {
1188 int hlim = np->mcast_hops;
1189 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1191 if (np->rxopt.bits.rxtclass) {
1192 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1194 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1196 if (np->rxopt.bits.rxoinfo) {
1197 struct in6_pktinfo src_info;
1198 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1199 np->sticky_pktinfo.ipi6_ifindex;
1200 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1201 np->sticky_pktinfo.ipi6_addr;
1202 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1204 if (np->rxopt.bits.rxohlim) {
1205 int hlim = np->mcast_hops;
1206 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1208 if (np->rxopt.bits.rxflow) {
1209 __be32 flowinfo = np->rcv_flowinfo;
1211 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1214 len -= msg.msg_controllen;
1215 return put_user(len, optlen);
1219 struct dst_entry *dst;
1223 dst = __sk_dst_get(sk);
1233 val = sk->sk_ipv6only;
1236 case IPV6_RECVPKTINFO:
1237 val = np->rxopt.bits.rxinfo;
1240 case IPV6_2292PKTINFO:
1241 val = np->rxopt.bits.rxoinfo;
1244 case IPV6_RECVHOPLIMIT:
1245 val = np->rxopt.bits.rxhlim;
1248 case IPV6_2292HOPLIMIT:
1249 val = np->rxopt.bits.rxohlim;
1252 case IPV6_RECVRTHDR:
1253 val = np->rxopt.bits.srcrt;
1256 case IPV6_2292RTHDR:
1257 val = np->rxopt.bits.osrcrt;
1261 case IPV6_RTHDRDSTOPTS:
1265 struct ipv6_txoptions *opt;
1268 opt = rcu_dereference_protected(np->opt,
1269 lockdep_sock_is_held(sk));
1270 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1272 /* check if ipv6_getsockopt_sticky() returns err code */
1275 return put_user(len, optlen);
1278 case IPV6_RECVHOPOPTS:
1279 val = np->rxopt.bits.hopopts;
1282 case IPV6_2292HOPOPTS:
1283 val = np->rxopt.bits.ohopopts;
1286 case IPV6_RECVDSTOPTS:
1287 val = np->rxopt.bits.dstopts;
1290 case IPV6_2292DSTOPTS:
1291 val = np->rxopt.bits.odstopts;
1298 case IPV6_RECVTCLASS:
1299 val = np->rxopt.bits.rxtclass;
1303 val = np->rxopt.bits.rxflow;
1306 case IPV6_RECVPATHMTU:
1307 val = np->rxopt.bits.rxpmtu;
1312 struct dst_entry *dst;
1313 struct ip6_mtuinfo mtuinfo;
1315 if (len < sizeof(mtuinfo))
1318 len = sizeof(mtuinfo);
1319 memset(&mtuinfo, 0, sizeof(mtuinfo));
1322 dst = __sk_dst_get(sk);
1324 mtuinfo.ip6m_mtu = dst_mtu(dst);
1326 if (!mtuinfo.ip6m_mtu)
1329 if (put_user(len, optlen))
1331 if (copy_to_user(optval, &mtuinfo, len))
1337 case IPV6_TRANSPARENT:
1338 val = inet_sk(sk)->transparent;
1342 val = inet_sk(sk)->freebind;
1345 case IPV6_RECVORIGDSTADDR:
1346 val = np->rxopt.bits.rxorigdstaddr;
1349 case IPV6_UNICAST_HOPS:
1350 case IPV6_MULTICAST_HOPS:
1352 struct dst_entry *dst;
1354 if (optname == IPV6_UNICAST_HOPS)
1355 val = np->hop_limit;
1357 val = np->mcast_hops;
1361 dst = __sk_dst_get(sk);
1363 val = ip6_dst_hoplimit(dst);
1368 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1372 case IPV6_MULTICAST_LOOP:
1376 case IPV6_MULTICAST_IF:
1377 val = np->mcast_oif;
1380 case IPV6_MULTICAST_ALL:
1384 case IPV6_UNICAST_IF:
1385 val = (__force int)htonl((__u32) np->ucast_oif);
1388 case IPV6_MTU_DISCOVER:
1396 case IPV6_FLOWINFO_SEND:
1400 case IPV6_FLOWLABEL_MGR:
1402 struct in6_flowlabel_req freq;
1405 if (len < sizeof(freq))
1408 if (copy_from_user(&freq, optval, sizeof(freq)))
1411 if (freq.flr_action != IPV6_FL_A_GET)
1415 flags = freq.flr_flags;
1417 memset(&freq, 0, sizeof(freq));
1419 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1423 if (put_user(len, optlen))
1425 if (copy_to_user(optval, &freq, len))
1431 case IPV6_ADDR_PREFERENCES:
1434 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1435 val |= IPV6_PREFER_SRC_TMP;
1436 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1437 val |= IPV6_PREFER_SRC_PUBLIC;
1439 /* XXX: should we return system default? */
1440 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1443 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1444 val |= IPV6_PREFER_SRC_COA;
1446 val |= IPV6_PREFER_SRC_HOME;
1449 case IPV6_MINHOPCOUNT:
1450 val = np->min_hopcount;
1457 case IPV6_AUTOFLOWLABEL:
1458 val = ip6_autoflowlabel(sock_net(sk), np);
1461 case IPV6_RECVFRAGSIZE:
1462 val = np->rxopt.bits.recvfragsize;
1465 case IPV6_ROUTER_ALERT_ISOLATE:
1466 val = np->rtalert_isolate;
1469 case IPV6_RECVERR_RFC4884:
1470 val = np->recverr_rfc4884;
1474 return -ENOPROTOOPT;
1476 len = min_t(unsigned int, sizeof(int), len);
1477 if (put_user(len, optlen))
1479 if (copy_to_user(optval, &val, len))
1484 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1485 char __user *optval, int __user *optlen)
1489 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1490 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1492 if (level != SOL_IPV6)
1493 return -ENOPROTOOPT;
1495 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1496 #ifdef CONFIG_NETFILTER
1497 /* we need to exclude all possible ENOPROTOOPTs except default case */
1498 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1501 if (get_user(len, optlen))
1504 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1506 err = put_user(len, optlen);
1511 EXPORT_SYMBOL(ipv6_getsockopt);