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 > READ_ONCE(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 > READ_ONCE(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 && !sockopt_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 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);
420 sockopt_lock_sock(sk);
422 /* Another thread has converted the socket into IPv4 with
423 * IPV6_ADDRFORM concurrently.
425 if (unlikely(sk->sk_family != AF_INET6))
431 if (optlen < sizeof(int))
433 if (val == PF_INET) {
434 if (sk->sk_type == SOCK_RAW)
437 if (sk->sk_protocol == IPPROTO_UDP ||
438 sk->sk_protocol == IPPROTO_UDPLITE) {
439 struct udp_sock *up = udp_sk(sk);
440 if (up->pending == AF_INET6) {
444 } else if (sk->sk_protocol == IPPROTO_TCP) {
445 if (sk->sk_prot != &tcpv6_prot) {
453 if (sk->sk_state != TCP_ESTABLISHED) {
458 if (ipv6_only_sock(sk) ||
459 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
460 retv = -EADDRNOTAVAIL;
464 __ipv6_sock_mc_close(sk);
465 __ipv6_sock_ac_close(sk);
468 * Sock is moving from IPv6 to IPv4 (sk_prot), so
469 * remove it from the refcnt debug socks count in the
472 sk_refcnt_debug_dec(sk);
474 if (sk->sk_protocol == IPPROTO_TCP) {
475 struct inet_connection_sock *icsk = inet_csk(sk);
477 sock_prot_inuse_add(net, sk->sk_prot, -1);
478 sock_prot_inuse_add(net, &tcp_prot, 1);
480 /* Paired with READ_ONCE(sk->sk_prot) in net/ipv6/af_inet6.c */
481 WRITE_ONCE(sk->sk_prot, &tcp_prot);
482 icsk->icsk_af_ops = &ipv4_specific;
483 sk->sk_socket->ops = &inet_stream_ops;
484 sk->sk_family = PF_INET;
485 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
487 struct proto *prot = &udp_prot;
489 if (sk->sk_protocol == IPPROTO_UDPLITE)
490 prot = &udplite_prot;
492 sock_prot_inuse_add(net, sk->sk_prot, -1);
493 sock_prot_inuse_add(net, prot, 1);
495 /* Paired with READ_ONCE(sk->sk_prot) in net/ipv6/af_inet6.c */
496 WRITE_ONCE(sk->sk_prot, prot);
497 sk->sk_socket->ops = &inet_dgram_ops;
498 sk->sk_family = PF_INET;
501 /* Disable all options not to allocate memory anymore,
502 * but there is still a race. See the lockless path
503 * in udpv6_sendmsg() and ipv6_local_rxpmtu().
507 inet6_cleanup_sock(sk);
510 * ... and add it to the refcnt debug socks count
511 * in the new family. -acme
513 sk_refcnt_debug_inc(sk);
514 module_put(THIS_MODULE);
521 if (optlen < sizeof(int) ||
522 inet_sk(sk)->inet_num)
524 sk->sk_ipv6only = valbool;
528 case IPV6_RECVPKTINFO:
529 if (optlen < sizeof(int))
531 np->rxopt.bits.rxinfo = valbool;
535 case IPV6_2292PKTINFO:
536 if (optlen < sizeof(int))
538 np->rxopt.bits.rxoinfo = valbool;
542 case IPV6_RECVHOPLIMIT:
543 if (optlen < sizeof(int))
545 np->rxopt.bits.rxhlim = valbool;
549 case IPV6_2292HOPLIMIT:
550 if (optlen < sizeof(int))
552 np->rxopt.bits.rxohlim = valbool;
557 if (optlen < sizeof(int))
559 np->rxopt.bits.srcrt = valbool;
564 if (optlen < sizeof(int))
566 np->rxopt.bits.osrcrt = valbool;
570 case IPV6_RECVHOPOPTS:
571 if (optlen < sizeof(int))
573 np->rxopt.bits.hopopts = valbool;
577 case IPV6_2292HOPOPTS:
578 if (optlen < sizeof(int))
580 np->rxopt.bits.ohopopts = valbool;
584 case IPV6_RECVDSTOPTS:
585 if (optlen < sizeof(int))
587 np->rxopt.bits.dstopts = valbool;
591 case IPV6_2292DSTOPTS:
592 if (optlen < sizeof(int))
594 np->rxopt.bits.odstopts = valbool;
599 if (optlen < sizeof(int))
601 if (val < -1 || val > 0xff)
603 /* RFC 3542, 6.5: default traffic class of 0x0 */
606 if (sk->sk_type == SOCK_STREAM) {
607 val &= ~INET_ECN_MASK;
608 val |= np->tclass & INET_ECN_MASK;
610 if (np->tclass != val) {
617 case IPV6_RECVTCLASS:
618 if (optlen < sizeof(int))
620 np->rxopt.bits.rxtclass = valbool;
625 if (optlen < sizeof(int))
627 np->rxopt.bits.rxflow = valbool;
631 case IPV6_RECVPATHMTU:
632 if (optlen < sizeof(int))
634 np->rxopt.bits.rxpmtu = valbool;
638 case IPV6_TRANSPARENT:
639 if (valbool && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW) &&
640 !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) {
644 if (optlen < sizeof(int))
646 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
647 inet_sk(sk)->transparent = valbool;
652 if (optlen < sizeof(int))
654 /* we also don't have a separate freebind bit for IPV6 */
655 inet_sk(sk)->freebind = valbool;
659 case IPV6_RECVORIGDSTADDR:
660 if (optlen < sizeof(int))
662 np->rxopt.bits.rxorigdstaddr = valbool;
667 case IPV6_RTHDRDSTOPTS:
670 retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
675 struct in6_pktinfo pkt;
679 else if (optlen < sizeof(struct in6_pktinfo) ||
680 sockptr_is_null(optval))
683 if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) {
687 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
690 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
691 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
696 case IPV6_2292PKTOPTIONS:
698 struct ipv6_txoptions *opt = NULL;
701 struct ipcm6_cookie ipc6;
703 memset(&fl6, 0, sizeof(fl6));
704 fl6.flowi6_oif = sk->sk_bound_dev_if;
705 fl6.flowi6_mark = sk->sk_mark;
710 /* 1K is probably excessive
711 * 1K is surely not enough, 2K per standard header is 16K.
714 if (optlen > 64*1024)
717 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
722 memset(opt, 0, sizeof(*opt));
723 refcount_set(&opt->refcnt, 1);
724 opt->tot_len = sizeof(*opt) + optlen;
726 if (copy_from_sockptr(opt + 1, optval, optlen))
729 msg.msg_controllen = optlen;
730 msg.msg_control = (void *)(opt+1);
733 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
738 opt = ipv6_update_options(sk, opt);
741 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
746 case IPV6_UNICAST_HOPS:
747 if (optlen < sizeof(int))
749 if (val > 255 || val < -1)
755 case IPV6_MULTICAST_HOPS:
756 if (sk->sk_type == SOCK_STREAM)
758 if (optlen < sizeof(int))
760 if (val > 255 || val < -1)
762 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
766 case IPV6_MULTICAST_LOOP:
767 if (optlen < sizeof(int))
771 np->mc_loop = valbool;
775 case IPV6_UNICAST_IF:
777 struct net_device *dev = NULL;
780 if (optlen != sizeof(int))
783 ifindex = (__force int)ntohl((__force __be32)val);
790 dev = dev_get_by_index(net, ifindex);
791 retv = -EADDRNOTAVAIL;
797 if (sk->sk_bound_dev_if)
800 np->ucast_oif = ifindex;
805 case IPV6_MULTICAST_IF:
806 if (sk->sk_type == SOCK_STREAM)
808 if (optlen < sizeof(int))
812 struct net_device *dev;
817 dev = dev_get_by_index_rcu(net, val);
823 midx = l3mdev_master_ifindex_rcu(dev);
827 if (sk->sk_bound_dev_if &&
828 sk->sk_bound_dev_if != val &&
829 (!midx || midx != sk->sk_bound_dev_if))
835 case IPV6_ADD_MEMBERSHIP:
836 case IPV6_DROP_MEMBERSHIP:
838 struct ipv6_mreq mreq;
840 if (optlen < sizeof(struct ipv6_mreq))
844 if (inet_sk(sk)->is_icsk)
848 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
851 if (optname == IPV6_ADD_MEMBERSHIP)
852 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
854 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
857 case IPV6_JOIN_ANYCAST:
858 case IPV6_LEAVE_ANYCAST:
860 struct ipv6_mreq mreq;
862 if (optlen < sizeof(struct ipv6_mreq))
866 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
869 if (optname == IPV6_JOIN_ANYCAST)
870 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
872 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
875 case IPV6_MULTICAST_ALL:
876 if (optlen < sizeof(int))
878 np->mc_all = valbool;
882 case MCAST_JOIN_GROUP:
883 case MCAST_LEAVE_GROUP:
884 if (in_compat_syscall())
885 retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
888 retv = ipv6_mcast_join_leave(sk, optname, optval,
891 case MCAST_JOIN_SOURCE_GROUP:
892 case MCAST_LEAVE_SOURCE_GROUP:
893 case MCAST_BLOCK_SOURCE:
894 case MCAST_UNBLOCK_SOURCE:
895 retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
898 if (in_compat_syscall())
899 retv = compat_ipv6_set_mcast_msfilter(sk, optval,
902 retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
904 case IPV6_ROUTER_ALERT:
905 if (optlen < sizeof(int))
907 retv = ip6_ra_control(sk, val);
909 case IPV6_ROUTER_ALERT_ISOLATE:
910 if (optlen < sizeof(int))
912 np->rtalert_isolate = valbool;
915 case IPV6_MTU_DISCOVER:
916 if (optlen < sizeof(int))
918 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
924 if (optlen < sizeof(int))
926 if (val && val < IPV6_MIN_MTU)
932 if (optlen < sizeof(int))
934 np->recverr = valbool;
936 skb_queue_purge(&sk->sk_error_queue);
939 case IPV6_FLOWINFO_SEND:
940 if (optlen < sizeof(int))
942 np->sndflow = valbool;
945 case IPV6_FLOWLABEL_MGR:
946 retv = ipv6_flowlabel_opt(sk, optval, optlen);
948 case IPV6_IPSEC_POLICY:
949 case IPV6_XFRM_POLICY:
951 if (!sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN))
953 retv = xfrm_user_policy(sk, optname, optval, optlen);
956 case IPV6_ADDR_PREFERENCES:
957 if (optlen < sizeof(int))
959 retv = __ip6_sock_set_addr_preferences(sk, val);
961 case IPV6_MINHOPCOUNT:
962 if (optlen < sizeof(int))
964 if (val < 0 || val > 255)
968 static_branch_enable(&ip6_min_hopcount);
970 /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount
971 * while we are changing it.
973 WRITE_ONCE(np->min_hopcount, val);
977 np->dontfrag = valbool;
980 case IPV6_AUTOFLOWLABEL:
981 np->autoflowlabel = valbool;
982 np->autoflowlabel_set = 1;
985 case IPV6_RECVFRAGSIZE:
986 np->rxopt.bits.recvfragsize = valbool;
989 case IPV6_RECVERR_RFC4884:
990 if (optlen < sizeof(int))
992 if (val < 0 || val > 1)
994 np->recverr_rfc4884 = valbool;
1000 sockopt_release_sock(sk);
1007 sockopt_release_sock(sk);
1013 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1014 unsigned int optlen)
1018 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1019 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1021 if (level != SOL_IPV6)
1022 return -ENOPROTOOPT;
1024 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1025 #ifdef CONFIG_NETFILTER
1026 /* we need to exclude all possible ENOPROTOOPTs except default case */
1027 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
1028 optname != IPV6_XFRM_POLICY)
1029 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
1033 EXPORT_SYMBOL(ipv6_setsockopt);
1035 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
1036 int optname, sockptr_t optval, int len)
1038 struct ipv6_opt_hdr *hdr;
1047 case IPV6_RTHDRDSTOPTS:
1051 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1057 return -EINVAL; /* should not happen */
1063 len = min_t(unsigned int, len, ipv6_optlen(hdr));
1064 if (copy_to_sockptr(optval, hdr, len))
1069 static int ipv6_get_msfilter(struct sock *sk, sockptr_t optval,
1070 sockptr_t optlen, int len)
1072 const int size0 = offsetof(struct group_filter, gf_slist_flex);
1073 struct group_filter gsf;
1079 if (copy_from_sockptr(&gsf, optval, size0))
1081 if (gsf.gf_group.ss_family != AF_INET6)
1082 return -EADDRNOTAVAIL;
1083 num = gsf.gf_numsrc;
1084 sockopt_lock_sock(sk);
1085 err = ip6_mc_msfget(sk, &gsf, optval, size0);
1087 if (num > gsf.gf_numsrc)
1088 num = gsf.gf_numsrc;
1089 len = GROUP_FILTER_SIZE(num);
1090 if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1091 copy_to_sockptr(optval, &gsf, size0))
1094 sockopt_release_sock(sk);
1098 static int compat_ipv6_get_msfilter(struct sock *sk, sockptr_t optval,
1099 sockptr_t optlen, int len)
1101 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1102 struct compat_group_filter gf32;
1103 struct group_filter gf;
1110 if (copy_from_sockptr(&gf32, optval, 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;
1120 sockopt_lock_sock(sk);
1121 err = ip6_mc_msfget(sk, &gf, optval, size0);
1122 sockopt_release_sock(sk);
1125 if (num > gf.gf_numsrc)
1127 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1128 if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1129 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode),
1130 &gf.gf_fmode, sizeof(gf32.gf_fmode)) ||
1131 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc),
1132 &gf.gf_numsrc, sizeof(gf32.gf_numsrc)))
1137 int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1138 sockptr_t optval, sockptr_t optlen)
1140 struct ipv6_pinfo *np = inet6_sk(sk);
1144 if (ip6_mroute_opt(optname))
1145 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1147 if (copy_from_sockptr(&len, optlen, sizeof(int)))
1151 if (sk->sk_protocol != IPPROTO_UDP &&
1152 sk->sk_protocol != IPPROTO_UDPLITE &&
1153 sk->sk_protocol != IPPROTO_TCP)
1154 return -ENOPROTOOPT;
1155 if (sk->sk_state != TCP_ESTABLISHED)
1157 val = sk->sk_family;
1159 case MCAST_MSFILTER:
1160 if (in_compat_syscall())
1161 return compat_ipv6_get_msfilter(sk, optval, optlen, len);
1162 return ipv6_get_msfilter(sk, optval, optlen, len);
1163 case IPV6_2292PKTOPTIONS:
1166 struct sk_buff *skb;
1168 if (sk->sk_type != SOCK_STREAM)
1169 return -ENOPROTOOPT;
1171 if (optval.is_kernel) {
1172 msg.msg_control_is_user = false;
1173 msg.msg_control = optval.kernel;
1175 msg.msg_control_is_user = true;
1176 msg.msg_control_user = optval.user;
1178 msg.msg_controllen = len;
1181 sockopt_lock_sock(sk);
1182 skb = np->pktoptions;
1184 ip6_datagram_recv_ctl(sk, &msg, skb);
1185 sockopt_release_sock(sk);
1187 if (np->rxopt.bits.rxinfo) {
1188 struct in6_pktinfo src_info;
1189 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1190 np->sticky_pktinfo.ipi6_ifindex;
1191 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1192 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1194 if (np->rxopt.bits.rxhlim) {
1195 int hlim = np->mcast_hops;
1196 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1198 if (np->rxopt.bits.rxtclass) {
1199 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1201 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1203 if (np->rxopt.bits.rxoinfo) {
1204 struct in6_pktinfo src_info;
1205 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1206 np->sticky_pktinfo.ipi6_ifindex;
1207 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1208 np->sticky_pktinfo.ipi6_addr;
1209 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1211 if (np->rxopt.bits.rxohlim) {
1212 int hlim = np->mcast_hops;
1213 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1215 if (np->rxopt.bits.rxflow) {
1216 __be32 flowinfo = np->rcv_flowinfo;
1218 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1221 len -= msg.msg_controllen;
1222 return copy_to_sockptr(optlen, &len, sizeof(int));
1226 struct dst_entry *dst;
1230 dst = __sk_dst_get(sk);
1240 val = sk->sk_ipv6only;
1243 case IPV6_RECVPKTINFO:
1244 val = np->rxopt.bits.rxinfo;
1247 case IPV6_2292PKTINFO:
1248 val = np->rxopt.bits.rxoinfo;
1251 case IPV6_RECVHOPLIMIT:
1252 val = np->rxopt.bits.rxhlim;
1255 case IPV6_2292HOPLIMIT:
1256 val = np->rxopt.bits.rxohlim;
1259 case IPV6_RECVRTHDR:
1260 val = np->rxopt.bits.srcrt;
1263 case IPV6_2292RTHDR:
1264 val = np->rxopt.bits.osrcrt;
1268 case IPV6_RTHDRDSTOPTS:
1272 struct ipv6_txoptions *opt;
1274 sockopt_lock_sock(sk);
1275 opt = rcu_dereference_protected(np->opt,
1276 lockdep_sock_is_held(sk));
1277 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1278 sockopt_release_sock(sk);
1279 /* check if ipv6_getsockopt_sticky() returns err code */
1282 return copy_to_sockptr(optlen, &len, sizeof(int));
1285 case IPV6_RECVHOPOPTS:
1286 val = np->rxopt.bits.hopopts;
1289 case IPV6_2292HOPOPTS:
1290 val = np->rxopt.bits.ohopopts;
1293 case IPV6_RECVDSTOPTS:
1294 val = np->rxopt.bits.dstopts;
1297 case IPV6_2292DSTOPTS:
1298 val = np->rxopt.bits.odstopts;
1305 case IPV6_RECVTCLASS:
1306 val = np->rxopt.bits.rxtclass;
1310 val = np->rxopt.bits.rxflow;
1313 case IPV6_RECVPATHMTU:
1314 val = np->rxopt.bits.rxpmtu;
1319 struct dst_entry *dst;
1320 struct ip6_mtuinfo mtuinfo;
1322 if (len < sizeof(mtuinfo))
1325 len = sizeof(mtuinfo);
1326 memset(&mtuinfo, 0, sizeof(mtuinfo));
1329 dst = __sk_dst_get(sk);
1331 mtuinfo.ip6m_mtu = dst_mtu(dst);
1333 if (!mtuinfo.ip6m_mtu)
1336 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1338 if (copy_to_sockptr(optval, &mtuinfo, len))
1344 case IPV6_TRANSPARENT:
1345 val = inet_sk(sk)->transparent;
1349 val = inet_sk(sk)->freebind;
1352 case IPV6_RECVORIGDSTADDR:
1353 val = np->rxopt.bits.rxorigdstaddr;
1356 case IPV6_UNICAST_HOPS:
1357 case IPV6_MULTICAST_HOPS:
1359 struct dst_entry *dst;
1361 if (optname == IPV6_UNICAST_HOPS)
1362 val = np->hop_limit;
1364 val = np->mcast_hops;
1368 dst = __sk_dst_get(sk);
1370 val = ip6_dst_hoplimit(dst);
1375 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1379 case IPV6_MULTICAST_LOOP:
1383 case IPV6_MULTICAST_IF:
1384 val = np->mcast_oif;
1387 case IPV6_MULTICAST_ALL:
1391 case IPV6_UNICAST_IF:
1392 val = (__force int)htonl((__u32) np->ucast_oif);
1395 case IPV6_MTU_DISCOVER:
1403 case IPV6_FLOWINFO_SEND:
1407 case IPV6_FLOWLABEL_MGR:
1409 struct in6_flowlabel_req freq;
1412 if (len < sizeof(freq))
1415 if (copy_from_sockptr(&freq, optval, sizeof(freq)))
1418 if (freq.flr_action != IPV6_FL_A_GET)
1422 flags = freq.flr_flags;
1424 memset(&freq, 0, sizeof(freq));
1426 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1430 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1432 if (copy_to_sockptr(optval, &freq, len))
1438 case IPV6_ADDR_PREFERENCES:
1441 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1442 val |= IPV6_PREFER_SRC_TMP;
1443 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1444 val |= IPV6_PREFER_SRC_PUBLIC;
1446 /* XXX: should we return system default? */
1447 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1450 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1451 val |= IPV6_PREFER_SRC_COA;
1453 val |= IPV6_PREFER_SRC_HOME;
1456 case IPV6_MINHOPCOUNT:
1457 val = np->min_hopcount;
1464 case IPV6_AUTOFLOWLABEL:
1465 val = ip6_autoflowlabel(sock_net(sk), np);
1468 case IPV6_RECVFRAGSIZE:
1469 val = np->rxopt.bits.recvfragsize;
1472 case IPV6_ROUTER_ALERT_ISOLATE:
1473 val = np->rtalert_isolate;
1476 case IPV6_RECVERR_RFC4884:
1477 val = np->recverr_rfc4884;
1481 return -ENOPROTOOPT;
1483 len = min_t(unsigned int, sizeof(int), len);
1484 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1486 if (copy_to_sockptr(optval, &val, len))
1491 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1492 char __user *optval, int __user *optlen)
1496 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1497 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1499 if (level != SOL_IPV6)
1500 return -ENOPROTOOPT;
1502 err = do_ipv6_getsockopt(sk, level, optname,
1503 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
1504 #ifdef CONFIG_NETFILTER
1505 /* we need to exclude all possible ENOPROTOOPTs except default case */
1506 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1509 if (get_user(len, optlen))
1512 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1514 err = put_user(len, optlen);
1519 EXPORT_SYMBOL(ipv6_getsockopt);