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 int ip6_ra_control(struct sock *sk, int sel)
60 struct ip6_ra_chain *ra, *new_ra, **rap;
62 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
63 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num != IPPROTO_RAW)
66 new_ra = (sel >= 0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
67 if (sel >= 0 && !new_ra)
70 write_lock_bh(&ip6_ra_lock);
71 for (rap = &ip6_ra_chain; (ra = *rap) != NULL; rap = &ra->next) {
74 write_unlock_bh(&ip6_ra_lock);
80 write_unlock_bh(&ip6_ra_lock);
88 write_unlock_bh(&ip6_ra_lock);
96 write_unlock_bh(&ip6_ra_lock);
100 struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
101 struct ipv6_txoptions *opt)
103 if (inet_sk(sk)->is_icsk) {
105 !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
106 inet_sk(sk)->inet_daddr != LOOPBACK4_IPV6) {
107 struct inet_connection_sock *icsk = inet_csk(sk);
108 icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
109 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
112 opt = xchg((__force struct ipv6_txoptions **)&inet6_sk(sk)->opt,
119 static bool setsockopt_needs_rtnl(int optname)
123 case IPV6_ADD_MEMBERSHIP:
124 case IPV6_DROP_MEMBERSHIP:
125 case IPV6_JOIN_ANYCAST:
126 case IPV6_LEAVE_ANYCAST:
127 case MCAST_JOIN_GROUP:
128 case MCAST_LEAVE_GROUP:
129 case MCAST_JOIN_SOURCE_GROUP:
130 case MCAST_LEAVE_SOURCE_GROUP:
131 case MCAST_BLOCK_SOURCE:
132 case MCAST_UNBLOCK_SOURCE:
139 static int copy_group_source_from_sockptr(struct group_source_req *greqs,
140 sockptr_t optval, int optlen)
142 if (in_compat_syscall()) {
143 struct compat_group_source_req gr32;
145 if (optlen < sizeof(gr32))
147 if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
149 greqs->gsr_interface = gr32.gsr_interface;
150 greqs->gsr_group = gr32.gsr_group;
151 greqs->gsr_source = gr32.gsr_source;
153 if (optlen < sizeof(*greqs))
155 if (copy_from_sockptr(greqs, optval, sizeof(*greqs)))
162 static int do_ipv6_mcast_group_source(struct sock *sk, int optname,
163 sockptr_t optval, int optlen)
165 struct group_source_req greqs;
169 ret = copy_group_source_from_sockptr(&greqs, optval, optlen);
173 if (greqs.gsr_group.ss_family != AF_INET6 ||
174 greqs.gsr_source.ss_family != AF_INET6)
175 return -EADDRNOTAVAIL;
177 if (optname == MCAST_BLOCK_SOURCE) {
178 omode = MCAST_EXCLUDE;
180 } else if (optname == MCAST_UNBLOCK_SOURCE) {
181 omode = MCAST_EXCLUDE;
183 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
184 struct sockaddr_in6 *psin6;
187 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
188 retv = ipv6_sock_mc_join_ssm(sk, greqs.gsr_interface,
191 /* prior join w/ different source is ok */
192 if (retv && retv != -EADDRINUSE)
194 omode = MCAST_INCLUDE;
196 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
197 omode = MCAST_INCLUDE;
200 return ip6_mc_source(add, omode, sk, &greqs);
203 static int ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
206 struct group_filter *gsf;
209 if (optlen < GROUP_FILTER_SIZE(0))
211 if (optlen > sysctl_optmem_max)
214 gsf = memdup_sockptr(optval, optlen);
218 /* numsrc >= (4G-140)/128 overflow in 32 bits */
220 if (gsf->gf_numsrc >= 0x1ffffffU ||
221 gsf->gf_numsrc > sysctl_mld_max_msf)
225 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
228 ret = ip6_mc_msfilter(sk, gsf, gsf->gf_slist_flex);
234 static int compat_ipv6_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
237 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
238 struct compat_group_filter *gf32;
245 if (optlen > sysctl_optmem_max - 4)
248 p = kmalloc(optlen + 4, GFP_KERNEL);
252 gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
254 if (copy_from_sockptr(gf32, optval, optlen))
257 /* numsrc >= (4G-140)/128 overflow in 32 bits */
260 if (n >= 0x1ffffffU || n > sysctl_mld_max_msf)
264 if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
267 ret = ip6_mc_msfilter(sk, &(struct group_filter){
268 .gf_interface = gf32->gf_interface,
269 .gf_group = gf32->gf_group,
270 .gf_fmode = gf32->gf_fmode,
271 .gf_numsrc = gf32->gf_numsrc}, gf32->gf_slist_flex);
278 static int ipv6_mcast_join_leave(struct sock *sk, int optname,
279 sockptr_t optval, int optlen)
281 struct sockaddr_in6 *psin6;
282 struct group_req greq;
284 if (optlen < sizeof(greq))
286 if (copy_from_sockptr(&greq, optval, sizeof(greq)))
289 if (greq.gr_group.ss_family != AF_INET6)
290 return -EADDRNOTAVAIL;
291 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
292 if (optname == MCAST_JOIN_GROUP)
293 return ipv6_sock_mc_join(sk, greq.gr_interface,
295 return ipv6_sock_mc_drop(sk, greq.gr_interface, &psin6->sin6_addr);
298 static int compat_ipv6_mcast_join_leave(struct sock *sk, int optname,
299 sockptr_t optval, int optlen)
301 struct compat_group_req gr32;
302 struct sockaddr_in6 *psin6;
304 if (optlen < sizeof(gr32))
306 if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
309 if (gr32.gr_group.ss_family != AF_INET6)
310 return -EADDRNOTAVAIL;
311 psin6 = (struct sockaddr_in6 *)&gr32.gr_group;
312 if (optname == MCAST_JOIN_GROUP)
313 return ipv6_sock_mc_join(sk, gr32.gr_interface,
315 return ipv6_sock_mc_drop(sk, gr32.gr_interface, &psin6->sin6_addr);
318 static int ipv6_set_opt_hdr(struct sock *sk, int optname, sockptr_t optval,
321 struct ipv6_pinfo *np = inet6_sk(sk);
322 struct ipv6_opt_hdr *new = NULL;
323 struct net *net = sock_net(sk);
324 struct ipv6_txoptions *opt;
327 /* hop-by-hop / destination options are privileged option */
328 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
331 /* remove any sticky options header with a zero option
332 * length, per RFC3542.
335 if (sockptr_is_null(optval))
337 if (optlen < sizeof(struct ipv6_opt_hdr) ||
342 new = memdup_sockptr(optval, optlen);
345 if (unlikely(ipv6_optlen(new) > optlen)) {
351 opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
352 opt = ipv6_renew_options(sk, opt, optname, new);
357 /* routing header option needs extra check */
359 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
360 struct ipv6_rt_hdr *rthdr = opt->srcrt;
361 switch (rthdr->type) {
362 #if IS_ENABLED(CONFIG_IPV6_MIP6)
363 case IPV6_SRCRT_TYPE_2:
364 if (rthdr->hdrlen != 2 || rthdr->segments_left != 1)
368 case IPV6_SRCRT_TYPE_4:
370 struct ipv6_sr_hdr *srh =
371 (struct ipv6_sr_hdr *)opt->srcrt;
373 if (!seg6_validate_srh(srh, optlen, false))
383 opt = ipv6_update_options(sk, opt);
386 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
392 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
393 sockptr_t optval, unsigned int optlen)
395 struct ipv6_pinfo *np = inet6_sk(sk);
396 struct net *net = sock_net(sk);
398 int retv = -ENOPROTOOPT;
399 bool needs_rtnl = setsockopt_needs_rtnl(optname);
401 if (sockptr_is_null(optval))
404 if (optlen >= sizeof(int)) {
405 if (copy_from_sockptr(&val, optval, sizeof(val)))
411 valbool = (val != 0);
413 if (ip6_mroute_opt(optname))
414 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
423 if (optlen < sizeof(int))
425 if (val == PF_INET) {
426 struct ipv6_txoptions *opt;
427 struct sk_buff *pktopt;
429 if (sk->sk_type == SOCK_RAW)
432 if (sk->sk_protocol == IPPROTO_UDP ||
433 sk->sk_protocol == IPPROTO_UDPLITE) {
434 struct udp_sock *up = udp_sk(sk);
435 if (up->pending == AF_INET6) {
439 } else if (sk->sk_protocol == IPPROTO_TCP) {
440 if (sk->sk_prot != &tcpv6_prot) {
448 if (sk->sk_state != TCP_ESTABLISHED) {
453 if (ipv6_only_sock(sk) ||
454 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
455 retv = -EADDRNOTAVAIL;
459 fl6_free_socklist(sk);
460 __ipv6_sock_mc_close(sk);
461 __ipv6_sock_ac_close(sk);
464 * Sock is moving from IPv6 to IPv4 (sk_prot), so
465 * remove it from the refcnt debug socks count in the
468 sk_refcnt_debug_dec(sk);
470 if (sk->sk_protocol == IPPROTO_TCP) {
471 struct inet_connection_sock *icsk = inet_csk(sk);
473 sock_prot_inuse_add(net, sk->sk_prot, -1);
474 sock_prot_inuse_add(net, &tcp_prot, 1);
476 sk->sk_prot = &tcp_prot;
477 icsk->icsk_af_ops = &ipv4_specific;
478 sk->sk_socket->ops = &inet_stream_ops;
479 sk->sk_family = PF_INET;
480 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
482 struct proto *prot = &udp_prot;
484 if (sk->sk_protocol == IPPROTO_UDPLITE)
485 prot = &udplite_prot;
487 sock_prot_inuse_add(net, sk->sk_prot, -1);
488 sock_prot_inuse_add(net, prot, 1);
491 sk->sk_socket->ops = &inet_dgram_ops;
492 sk->sk_family = PF_INET;
494 opt = xchg((__force struct ipv6_txoptions **)&np->opt,
497 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
500 pktopt = xchg(&np->pktoptions, NULL);
504 * ... and add it to the refcnt debug socks count
505 * in the new family. -acme
507 sk_refcnt_debug_inc(sk);
508 module_put(THIS_MODULE);
515 if (optlen < sizeof(int) ||
516 inet_sk(sk)->inet_num)
518 sk->sk_ipv6only = valbool;
522 case IPV6_RECVPKTINFO:
523 if (optlen < sizeof(int))
525 np->rxopt.bits.rxinfo = valbool;
529 case IPV6_2292PKTINFO:
530 if (optlen < sizeof(int))
532 np->rxopt.bits.rxoinfo = valbool;
536 case IPV6_RECVHOPLIMIT:
537 if (optlen < sizeof(int))
539 np->rxopt.bits.rxhlim = valbool;
543 case IPV6_2292HOPLIMIT:
544 if (optlen < sizeof(int))
546 np->rxopt.bits.rxohlim = valbool;
551 if (optlen < sizeof(int))
553 np->rxopt.bits.srcrt = valbool;
558 if (optlen < sizeof(int))
560 np->rxopt.bits.osrcrt = valbool;
564 case IPV6_RECVHOPOPTS:
565 if (optlen < sizeof(int))
567 np->rxopt.bits.hopopts = valbool;
571 case IPV6_2292HOPOPTS:
572 if (optlen < sizeof(int))
574 np->rxopt.bits.ohopopts = valbool;
578 case IPV6_RECVDSTOPTS:
579 if (optlen < sizeof(int))
581 np->rxopt.bits.dstopts = valbool;
585 case IPV6_2292DSTOPTS:
586 if (optlen < sizeof(int))
588 np->rxopt.bits.odstopts = valbool;
593 if (optlen < sizeof(int))
595 if (val < -1 || val > 0xff)
597 /* RFC 3542, 6.5: default traffic class of 0x0 */
604 case IPV6_RECVTCLASS:
605 if (optlen < sizeof(int))
607 np->rxopt.bits.rxtclass = valbool;
612 if (optlen < sizeof(int))
614 np->rxopt.bits.rxflow = valbool;
618 case IPV6_RECVPATHMTU:
619 if (optlen < sizeof(int))
621 np->rxopt.bits.rxpmtu = valbool;
625 case IPV6_TRANSPARENT:
626 if (valbool && !ns_capable(net->user_ns, CAP_NET_RAW) &&
627 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
631 if (optlen < sizeof(int))
633 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
634 inet_sk(sk)->transparent = valbool;
639 if (optlen < sizeof(int))
641 /* we also don't have a separate freebind bit for IPV6 */
642 inet_sk(sk)->freebind = valbool;
646 case IPV6_RECVORIGDSTADDR:
647 if (optlen < sizeof(int))
649 np->rxopt.bits.rxorigdstaddr = valbool;
654 case IPV6_RTHDRDSTOPTS:
657 retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
662 struct in6_pktinfo pkt;
666 else if (optlen < sizeof(struct in6_pktinfo) ||
667 sockptr_is_null(optval))
670 if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) {
674 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
677 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
678 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
683 case IPV6_2292PKTOPTIONS:
685 struct ipv6_txoptions *opt = NULL;
688 struct ipcm6_cookie ipc6;
690 memset(&fl6, 0, sizeof(fl6));
691 fl6.flowi6_oif = sk->sk_bound_dev_if;
692 fl6.flowi6_mark = sk->sk_mark;
697 /* 1K is probably excessive
698 * 1K is surely not enough, 2K per standard header is 16K.
701 if (optlen > 64*1024)
704 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
709 memset(opt, 0, sizeof(*opt));
710 refcount_set(&opt->refcnt, 1);
711 opt->tot_len = sizeof(*opt) + optlen;
713 if (copy_from_sockptr(opt + 1, optval, optlen))
716 msg.msg_controllen = optlen;
717 msg.msg_control = (void *)(opt+1);
720 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
725 opt = ipv6_update_options(sk, opt);
728 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
733 case IPV6_UNICAST_HOPS:
734 if (optlen < sizeof(int))
736 if (val > 255 || val < -1)
742 case IPV6_MULTICAST_HOPS:
743 if (sk->sk_type == SOCK_STREAM)
745 if (optlen < sizeof(int))
747 if (val > 255 || val < -1)
749 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
753 case IPV6_MULTICAST_LOOP:
754 if (optlen < sizeof(int))
758 np->mc_loop = valbool;
762 case IPV6_UNICAST_IF:
764 struct net_device *dev = NULL;
767 if (optlen != sizeof(int))
770 ifindex = (__force int)ntohl((__force __be32)val);
777 dev = dev_get_by_index(net, ifindex);
778 retv = -EADDRNOTAVAIL;
784 if (sk->sk_bound_dev_if)
787 np->ucast_oif = ifindex;
792 case IPV6_MULTICAST_IF:
793 if (sk->sk_type == SOCK_STREAM)
795 if (optlen < sizeof(int))
799 struct net_device *dev;
804 dev = dev_get_by_index_rcu(net, val);
810 midx = l3mdev_master_ifindex_rcu(dev);
814 if (sk->sk_bound_dev_if &&
815 sk->sk_bound_dev_if != val &&
816 (!midx || midx != sk->sk_bound_dev_if))
822 case IPV6_ADD_MEMBERSHIP:
823 case IPV6_DROP_MEMBERSHIP:
825 struct ipv6_mreq mreq;
827 if (optlen < sizeof(struct ipv6_mreq))
831 if (inet_sk(sk)->is_icsk)
835 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
838 if (optname == IPV6_ADD_MEMBERSHIP)
839 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
841 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
844 case IPV6_JOIN_ANYCAST:
845 case IPV6_LEAVE_ANYCAST:
847 struct ipv6_mreq mreq;
849 if (optlen < sizeof(struct ipv6_mreq))
853 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
856 if (optname == IPV6_JOIN_ANYCAST)
857 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
859 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
862 case IPV6_MULTICAST_ALL:
863 if (optlen < sizeof(int))
865 np->mc_all = valbool;
869 case MCAST_JOIN_GROUP:
870 case MCAST_LEAVE_GROUP:
871 if (in_compat_syscall())
872 retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
875 retv = ipv6_mcast_join_leave(sk, optname, optval,
878 case MCAST_JOIN_SOURCE_GROUP:
879 case MCAST_LEAVE_SOURCE_GROUP:
880 case MCAST_BLOCK_SOURCE:
881 case MCAST_UNBLOCK_SOURCE:
882 retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
885 if (in_compat_syscall())
886 retv = compat_ipv6_set_mcast_msfilter(sk, optval,
889 retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
891 case IPV6_ROUTER_ALERT:
892 if (optlen < sizeof(int))
894 retv = ip6_ra_control(sk, val);
896 case IPV6_ROUTER_ALERT_ISOLATE:
897 if (optlen < sizeof(int))
899 np->rtalert_isolate = valbool;
902 case IPV6_MTU_DISCOVER:
903 if (optlen < sizeof(int))
905 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
911 if (optlen < sizeof(int))
913 if (val && val < IPV6_MIN_MTU)
919 if (optlen < sizeof(int))
921 np->recverr = valbool;
923 skb_queue_purge(&sk->sk_error_queue);
926 case IPV6_FLOWINFO_SEND:
927 if (optlen < sizeof(int))
929 np->sndflow = valbool;
932 case IPV6_FLOWLABEL_MGR:
933 retv = ipv6_flowlabel_opt(sk, optval, optlen);
935 case IPV6_IPSEC_POLICY:
936 case IPV6_XFRM_POLICY:
938 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
940 retv = xfrm_user_policy(sk, optname, optval, optlen);
943 case IPV6_ADDR_PREFERENCES:
944 if (optlen < sizeof(int))
946 retv = __ip6_sock_set_addr_preferences(sk, val);
948 case IPV6_MINHOPCOUNT:
949 if (optlen < sizeof(int))
951 if (val < 0 || val > 255)
953 np->min_hopcount = val;
957 np->dontfrag = valbool;
960 case IPV6_AUTOFLOWLABEL:
961 np->autoflowlabel = valbool;
962 np->autoflowlabel_set = 1;
965 case IPV6_RECVFRAGSIZE:
966 np->rxopt.bits.recvfragsize = valbool;
969 case IPV6_RECVERR_RFC4884:
970 if (optlen < sizeof(int))
972 if (val < 0 || val > 1)
974 np->recverr_rfc4884 = valbool;
992 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
997 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
998 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1000 if (level != SOL_IPV6)
1001 return -ENOPROTOOPT;
1003 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1004 #ifdef CONFIG_NETFILTER
1005 /* we need to exclude all possible ENOPROTOOPTs except default case */
1006 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
1007 optname != IPV6_XFRM_POLICY)
1008 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
1012 EXPORT_SYMBOL(ipv6_setsockopt);
1014 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
1015 int optname, char __user *optval, int len)
1017 struct ipv6_opt_hdr *hdr;
1026 case IPV6_RTHDRDSTOPTS:
1030 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1036 return -EINVAL; /* should not happen */
1042 len = min_t(unsigned int, len, ipv6_optlen(hdr));
1043 if (copy_to_user(optval, hdr, len))
1048 static int ipv6_get_msfilter(struct sock *sk, void __user *optval,
1049 int __user *optlen, int len)
1051 const int size0 = offsetof(struct group_filter, gf_slist_flex);
1052 struct group_filter __user *p = optval;
1053 struct group_filter gsf;
1059 if (copy_from_user(&gsf, p, size0))
1061 if (gsf.gf_group.ss_family != AF_INET6)
1062 return -EADDRNOTAVAIL;
1063 num = gsf.gf_numsrc;
1065 err = ip6_mc_msfget(sk, &gsf, p->gf_slist_flex);
1067 if (num > gsf.gf_numsrc)
1068 num = gsf.gf_numsrc;
1069 if (put_user(GROUP_FILTER_SIZE(num), optlen) ||
1070 copy_to_user(p, &gsf, size0))
1077 static int compat_ipv6_get_msfilter(struct sock *sk, void __user *optval,
1080 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1081 struct compat_group_filter __user *p = optval;
1082 struct compat_group_filter gf32;
1083 struct group_filter gf;
1087 if (get_user(len, optlen))
1092 if (copy_from_user(&gf32, p, size0))
1094 gf.gf_interface = gf32.gf_interface;
1095 gf.gf_fmode = gf32.gf_fmode;
1096 num = gf.gf_numsrc = gf32.gf_numsrc;
1097 gf.gf_group = gf32.gf_group;
1099 if (gf.gf_group.ss_family != AF_INET6)
1100 return -EADDRNOTAVAIL;
1103 err = ip6_mc_msfget(sk, &gf, p->gf_slist_flex);
1107 if (num > gf.gf_numsrc)
1109 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1110 if (put_user(len, optlen) ||
1111 put_user(gf.gf_fmode, &p->gf_fmode) ||
1112 put_user(gf.gf_numsrc, &p->gf_numsrc))
1117 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1118 char __user *optval, int __user *optlen, unsigned int flags)
1120 struct ipv6_pinfo *np = inet6_sk(sk);
1124 if (ip6_mroute_opt(optname))
1125 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1127 if (get_user(len, optlen))
1131 if (sk->sk_protocol != IPPROTO_UDP &&
1132 sk->sk_protocol != IPPROTO_UDPLITE &&
1133 sk->sk_protocol != IPPROTO_TCP)
1134 return -ENOPROTOOPT;
1135 if (sk->sk_state != TCP_ESTABLISHED)
1137 val = sk->sk_family;
1139 case MCAST_MSFILTER:
1140 if (in_compat_syscall())
1141 return compat_ipv6_get_msfilter(sk, optval, optlen);
1142 return ipv6_get_msfilter(sk, optval, optlen, len);
1143 case IPV6_2292PKTOPTIONS:
1146 struct sk_buff *skb;
1148 if (sk->sk_type != SOCK_STREAM)
1149 return -ENOPROTOOPT;
1151 msg.msg_control_user = optval;
1152 msg.msg_controllen = len;
1153 msg.msg_flags = flags;
1154 msg.msg_control_is_user = true;
1157 skb = np->pktoptions;
1159 ip6_datagram_recv_ctl(sk, &msg, skb);
1162 if (np->rxopt.bits.rxinfo) {
1163 struct in6_pktinfo src_info;
1164 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1165 np->sticky_pktinfo.ipi6_ifindex;
1166 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1167 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1169 if (np->rxopt.bits.rxhlim) {
1170 int hlim = np->mcast_hops;
1171 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1173 if (np->rxopt.bits.rxtclass) {
1174 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1176 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1178 if (np->rxopt.bits.rxoinfo) {
1179 struct in6_pktinfo src_info;
1180 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1181 np->sticky_pktinfo.ipi6_ifindex;
1182 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1183 np->sticky_pktinfo.ipi6_addr;
1184 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1186 if (np->rxopt.bits.rxohlim) {
1187 int hlim = np->mcast_hops;
1188 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1190 if (np->rxopt.bits.rxflow) {
1191 __be32 flowinfo = np->rcv_flowinfo;
1193 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1196 len -= msg.msg_controllen;
1197 return put_user(len, optlen);
1201 struct dst_entry *dst;
1205 dst = __sk_dst_get(sk);
1215 val = sk->sk_ipv6only;
1218 case IPV6_RECVPKTINFO:
1219 val = np->rxopt.bits.rxinfo;
1222 case IPV6_2292PKTINFO:
1223 val = np->rxopt.bits.rxoinfo;
1226 case IPV6_RECVHOPLIMIT:
1227 val = np->rxopt.bits.rxhlim;
1230 case IPV6_2292HOPLIMIT:
1231 val = np->rxopt.bits.rxohlim;
1234 case IPV6_RECVRTHDR:
1235 val = np->rxopt.bits.srcrt;
1238 case IPV6_2292RTHDR:
1239 val = np->rxopt.bits.osrcrt;
1243 case IPV6_RTHDRDSTOPTS:
1247 struct ipv6_txoptions *opt;
1250 opt = rcu_dereference_protected(np->opt,
1251 lockdep_sock_is_held(sk));
1252 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1254 /* check if ipv6_getsockopt_sticky() returns err code */
1257 return put_user(len, optlen);
1260 case IPV6_RECVHOPOPTS:
1261 val = np->rxopt.bits.hopopts;
1264 case IPV6_2292HOPOPTS:
1265 val = np->rxopt.bits.ohopopts;
1268 case IPV6_RECVDSTOPTS:
1269 val = np->rxopt.bits.dstopts;
1272 case IPV6_2292DSTOPTS:
1273 val = np->rxopt.bits.odstopts;
1280 case IPV6_RECVTCLASS:
1281 val = np->rxopt.bits.rxtclass;
1285 val = np->rxopt.bits.rxflow;
1288 case IPV6_RECVPATHMTU:
1289 val = np->rxopt.bits.rxpmtu;
1294 struct dst_entry *dst;
1295 struct ip6_mtuinfo mtuinfo;
1297 if (len < sizeof(mtuinfo))
1300 len = sizeof(mtuinfo);
1301 memset(&mtuinfo, 0, sizeof(mtuinfo));
1304 dst = __sk_dst_get(sk);
1306 mtuinfo.ip6m_mtu = dst_mtu(dst);
1308 if (!mtuinfo.ip6m_mtu)
1311 if (put_user(len, optlen))
1313 if (copy_to_user(optval, &mtuinfo, len))
1319 case IPV6_TRANSPARENT:
1320 val = inet_sk(sk)->transparent;
1324 val = inet_sk(sk)->freebind;
1327 case IPV6_RECVORIGDSTADDR:
1328 val = np->rxopt.bits.rxorigdstaddr;
1331 case IPV6_UNICAST_HOPS:
1332 case IPV6_MULTICAST_HOPS:
1334 struct dst_entry *dst;
1336 if (optname == IPV6_UNICAST_HOPS)
1337 val = np->hop_limit;
1339 val = np->mcast_hops;
1343 dst = __sk_dst_get(sk);
1345 val = ip6_dst_hoplimit(dst);
1350 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1354 case IPV6_MULTICAST_LOOP:
1358 case IPV6_MULTICAST_IF:
1359 val = np->mcast_oif;
1362 case IPV6_MULTICAST_ALL:
1366 case IPV6_UNICAST_IF:
1367 val = (__force int)htonl((__u32) np->ucast_oif);
1370 case IPV6_MTU_DISCOVER:
1378 case IPV6_FLOWINFO_SEND:
1382 case IPV6_FLOWLABEL_MGR:
1384 struct in6_flowlabel_req freq;
1387 if (len < sizeof(freq))
1390 if (copy_from_user(&freq, optval, sizeof(freq)))
1393 if (freq.flr_action != IPV6_FL_A_GET)
1397 flags = freq.flr_flags;
1399 memset(&freq, 0, sizeof(freq));
1401 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1405 if (put_user(len, optlen))
1407 if (copy_to_user(optval, &freq, len))
1413 case IPV6_ADDR_PREFERENCES:
1416 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1417 val |= IPV6_PREFER_SRC_TMP;
1418 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1419 val |= IPV6_PREFER_SRC_PUBLIC;
1421 /* XXX: should we return system default? */
1422 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1425 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1426 val |= IPV6_PREFER_SRC_COA;
1428 val |= IPV6_PREFER_SRC_HOME;
1431 case IPV6_MINHOPCOUNT:
1432 val = np->min_hopcount;
1439 case IPV6_AUTOFLOWLABEL:
1440 val = ip6_autoflowlabel(sock_net(sk), np);
1443 case IPV6_RECVFRAGSIZE:
1444 val = np->rxopt.bits.recvfragsize;
1447 case IPV6_ROUTER_ALERT_ISOLATE:
1448 val = np->rtalert_isolate;
1451 case IPV6_RECVERR_RFC4884:
1452 val = np->recverr_rfc4884;
1456 return -ENOPROTOOPT;
1458 len = min_t(unsigned int, sizeof(int), len);
1459 if (put_user(len, optlen))
1461 if (copy_to_user(optval, &val, len))
1466 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1467 char __user *optval, int __user *optlen)
1471 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1472 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1474 if (level != SOL_IPV6)
1475 return -ENOPROTOOPT;
1477 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1478 #ifdef CONFIG_NETFILTER
1479 /* we need to exclude all possible ENOPROTOOPTs except default case */
1480 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1483 if (get_user(len, optlen))
1486 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1488 err = put_user(len, optlen);
1493 EXPORT_SYMBOL(ipv6_getsockopt);