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 inet6_stream_ops */
481 WRITE_ONCE(sk->sk_prot, &tcp_prot);
482 /* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
483 WRITE_ONCE(icsk->icsk_af_ops, &ipv4_specific);
484 sk->sk_socket->ops = &inet_stream_ops;
485 sk->sk_family = PF_INET;
486 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
488 struct proto *prot = &udp_prot;
490 if (sk->sk_protocol == IPPROTO_UDPLITE)
491 prot = &udplite_prot;
493 sock_prot_inuse_add(net, sk->sk_prot, -1);
494 sock_prot_inuse_add(net, prot, 1);
496 /* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */
497 WRITE_ONCE(sk->sk_prot, prot);
498 sk->sk_socket->ops = &inet_dgram_ops;
499 sk->sk_family = PF_INET;
502 /* Disable all options not to allocate memory anymore,
503 * but there is still a race. See the lockless path
504 * in udpv6_sendmsg() and ipv6_local_rxpmtu().
508 inet6_cleanup_sock(sk);
511 * ... and add it to the refcnt debug socks count
512 * in the new family. -acme
514 sk_refcnt_debug_inc(sk);
515 module_put(THIS_MODULE);
522 if (optlen < sizeof(int) ||
523 inet_sk(sk)->inet_num)
525 sk->sk_ipv6only = valbool;
529 case IPV6_RECVPKTINFO:
530 if (optlen < sizeof(int))
532 np->rxopt.bits.rxinfo = valbool;
536 case IPV6_2292PKTINFO:
537 if (optlen < sizeof(int))
539 np->rxopt.bits.rxoinfo = valbool;
543 case IPV6_RECVHOPLIMIT:
544 if (optlen < sizeof(int))
546 np->rxopt.bits.rxhlim = valbool;
550 case IPV6_2292HOPLIMIT:
551 if (optlen < sizeof(int))
553 np->rxopt.bits.rxohlim = valbool;
558 if (optlen < sizeof(int))
560 np->rxopt.bits.srcrt = valbool;
565 if (optlen < sizeof(int))
567 np->rxopt.bits.osrcrt = valbool;
571 case IPV6_RECVHOPOPTS:
572 if (optlen < sizeof(int))
574 np->rxopt.bits.hopopts = valbool;
578 case IPV6_2292HOPOPTS:
579 if (optlen < sizeof(int))
581 np->rxopt.bits.ohopopts = valbool;
585 case IPV6_RECVDSTOPTS:
586 if (optlen < sizeof(int))
588 np->rxopt.bits.dstopts = valbool;
592 case IPV6_2292DSTOPTS:
593 if (optlen < sizeof(int))
595 np->rxopt.bits.odstopts = valbool;
600 if (optlen < sizeof(int))
602 if (val < -1 || val > 0xff)
604 /* RFC 3542, 6.5: default traffic class of 0x0 */
607 if (sk->sk_type == SOCK_STREAM) {
608 val &= ~INET_ECN_MASK;
609 val |= np->tclass & INET_ECN_MASK;
611 if (np->tclass != val) {
618 case IPV6_RECVTCLASS:
619 if (optlen < sizeof(int))
621 np->rxopt.bits.rxtclass = valbool;
626 if (optlen < sizeof(int))
628 np->rxopt.bits.rxflow = valbool;
632 case IPV6_RECVPATHMTU:
633 if (optlen < sizeof(int))
635 np->rxopt.bits.rxpmtu = valbool;
639 case IPV6_TRANSPARENT:
640 if (valbool && !sockopt_ns_capable(net->user_ns, CAP_NET_RAW) &&
641 !sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN)) {
645 if (optlen < sizeof(int))
647 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
648 inet_sk(sk)->transparent = valbool;
653 if (optlen < sizeof(int))
655 /* we also don't have a separate freebind bit for IPV6 */
656 inet_sk(sk)->freebind = valbool;
660 case IPV6_RECVORIGDSTADDR:
661 if (optlen < sizeof(int))
663 np->rxopt.bits.rxorigdstaddr = valbool;
668 case IPV6_RTHDRDSTOPTS:
671 retv = ipv6_set_opt_hdr(sk, optname, optval, optlen);
676 struct in6_pktinfo pkt;
680 else if (optlen < sizeof(struct in6_pktinfo) ||
681 sockptr_is_null(optval))
684 if (copy_from_sockptr(&pkt, optval, sizeof(pkt))) {
688 if (!sk_dev_equal_l3scope(sk, pkt.ipi6_ifindex))
691 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
692 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
697 case IPV6_2292PKTOPTIONS:
699 struct ipv6_txoptions *opt = NULL;
702 struct ipcm6_cookie ipc6;
704 memset(&fl6, 0, sizeof(fl6));
705 fl6.flowi6_oif = sk->sk_bound_dev_if;
706 fl6.flowi6_mark = sk->sk_mark;
711 /* 1K is probably excessive
712 * 1K is surely not enough, 2K per standard header is 16K.
715 if (optlen > 64*1024)
718 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
723 memset(opt, 0, sizeof(*opt));
724 refcount_set(&opt->refcnt, 1);
725 opt->tot_len = sizeof(*opt) + optlen;
727 if (copy_from_sockptr(opt + 1, optval, optlen))
730 msg.msg_controllen = optlen;
731 msg.msg_control = (void *)(opt+1);
734 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, &ipc6);
739 opt = ipv6_update_options(sk, opt);
742 atomic_sub(opt->tot_len, &sk->sk_omem_alloc);
747 case IPV6_UNICAST_HOPS:
748 if (optlen < sizeof(int))
750 if (val > 255 || val < -1)
756 case IPV6_MULTICAST_HOPS:
757 if (sk->sk_type == SOCK_STREAM)
759 if (optlen < sizeof(int))
761 if (val > 255 || val < -1)
763 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
767 case IPV6_MULTICAST_LOOP:
768 if (optlen < sizeof(int))
772 np->mc_loop = valbool;
776 case IPV6_UNICAST_IF:
778 struct net_device *dev = NULL;
781 if (optlen != sizeof(int))
784 ifindex = (__force int)ntohl((__force __be32)val);
791 dev = dev_get_by_index(net, ifindex);
792 retv = -EADDRNOTAVAIL;
798 if (sk->sk_bound_dev_if)
801 np->ucast_oif = ifindex;
806 case IPV6_MULTICAST_IF:
807 if (sk->sk_type == SOCK_STREAM)
809 if (optlen < sizeof(int))
813 struct net_device *dev;
818 dev = dev_get_by_index_rcu(net, val);
824 midx = l3mdev_master_ifindex_rcu(dev);
828 if (sk->sk_bound_dev_if &&
829 sk->sk_bound_dev_if != val &&
830 (!midx || midx != sk->sk_bound_dev_if))
836 case IPV6_ADD_MEMBERSHIP:
837 case IPV6_DROP_MEMBERSHIP:
839 struct ipv6_mreq mreq;
841 if (optlen < sizeof(struct ipv6_mreq))
845 if (inet_sk(sk)->is_icsk)
849 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
852 if (optname == IPV6_ADD_MEMBERSHIP)
853 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
855 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
858 case IPV6_JOIN_ANYCAST:
859 case IPV6_LEAVE_ANYCAST:
861 struct ipv6_mreq mreq;
863 if (optlen < sizeof(struct ipv6_mreq))
867 if (copy_from_sockptr(&mreq, optval, sizeof(struct ipv6_mreq)))
870 if (optname == IPV6_JOIN_ANYCAST)
871 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
873 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
876 case IPV6_MULTICAST_ALL:
877 if (optlen < sizeof(int))
879 np->mc_all = valbool;
883 case MCAST_JOIN_GROUP:
884 case MCAST_LEAVE_GROUP:
885 if (in_compat_syscall())
886 retv = compat_ipv6_mcast_join_leave(sk, optname, optval,
889 retv = ipv6_mcast_join_leave(sk, optname, optval,
892 case MCAST_JOIN_SOURCE_GROUP:
893 case MCAST_LEAVE_SOURCE_GROUP:
894 case MCAST_BLOCK_SOURCE:
895 case MCAST_UNBLOCK_SOURCE:
896 retv = do_ipv6_mcast_group_source(sk, optname, optval, optlen);
899 if (in_compat_syscall())
900 retv = compat_ipv6_set_mcast_msfilter(sk, optval,
903 retv = ipv6_set_mcast_msfilter(sk, optval, optlen);
905 case IPV6_ROUTER_ALERT:
906 if (optlen < sizeof(int))
908 retv = ip6_ra_control(sk, val);
910 case IPV6_ROUTER_ALERT_ISOLATE:
911 if (optlen < sizeof(int))
913 np->rtalert_isolate = valbool;
916 case IPV6_MTU_DISCOVER:
917 if (optlen < sizeof(int))
919 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
925 if (optlen < sizeof(int))
927 if (val && val < IPV6_MIN_MTU)
933 if (optlen < sizeof(int))
935 np->recverr = valbool;
937 skb_queue_purge(&sk->sk_error_queue);
940 case IPV6_FLOWINFO_SEND:
941 if (optlen < sizeof(int))
943 np->sndflow = valbool;
946 case IPV6_FLOWLABEL_MGR:
947 retv = ipv6_flowlabel_opt(sk, optval, optlen);
949 case IPV6_IPSEC_POLICY:
950 case IPV6_XFRM_POLICY:
952 if (!sockopt_ns_capable(net->user_ns, CAP_NET_ADMIN))
954 retv = xfrm_user_policy(sk, optname, optval, optlen);
957 case IPV6_ADDR_PREFERENCES:
958 if (optlen < sizeof(int))
960 retv = __ip6_sock_set_addr_preferences(sk, val);
962 case IPV6_MINHOPCOUNT:
963 if (optlen < sizeof(int))
965 if (val < 0 || val > 255)
969 static_branch_enable(&ip6_min_hopcount);
971 /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount
972 * while we are changing it.
974 WRITE_ONCE(np->min_hopcount, val);
978 np->dontfrag = valbool;
981 case IPV6_AUTOFLOWLABEL:
982 np->autoflowlabel = valbool;
983 np->autoflowlabel_set = 1;
986 case IPV6_RECVFRAGSIZE:
987 np->rxopt.bits.recvfragsize = valbool;
990 case IPV6_RECVERR_RFC4884:
991 if (optlen < sizeof(int))
993 if (val < 0 || val > 1)
995 np->recverr_rfc4884 = valbool;
1001 sockopt_release_sock(sk);
1008 sockopt_release_sock(sk);
1014 int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1015 unsigned int optlen)
1019 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1020 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
1022 if (level != SOL_IPV6)
1023 return -ENOPROTOOPT;
1025 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
1026 #ifdef CONFIG_NETFILTER
1027 /* we need to exclude all possible ENOPROTOOPTs except default case */
1028 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
1029 optname != IPV6_XFRM_POLICY)
1030 err = nf_setsockopt(sk, PF_INET6, optname, optval, optlen);
1034 EXPORT_SYMBOL(ipv6_setsockopt);
1036 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
1037 int optname, sockptr_t optval, int len)
1039 struct ipv6_opt_hdr *hdr;
1048 case IPV6_RTHDRDSTOPTS:
1052 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
1058 return -EINVAL; /* should not happen */
1064 len = min_t(unsigned int, len, ipv6_optlen(hdr));
1065 if (copy_to_sockptr(optval, hdr, len))
1070 static int ipv6_get_msfilter(struct sock *sk, sockptr_t optval,
1071 sockptr_t optlen, int len)
1073 const int size0 = offsetof(struct group_filter, gf_slist_flex);
1074 struct group_filter gsf;
1080 if (copy_from_sockptr(&gsf, optval, size0))
1082 if (gsf.gf_group.ss_family != AF_INET6)
1083 return -EADDRNOTAVAIL;
1084 num = gsf.gf_numsrc;
1085 sockopt_lock_sock(sk);
1086 err = ip6_mc_msfget(sk, &gsf, optval, size0);
1088 if (num > gsf.gf_numsrc)
1089 num = gsf.gf_numsrc;
1090 len = GROUP_FILTER_SIZE(num);
1091 if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1092 copy_to_sockptr(optval, &gsf, size0))
1095 sockopt_release_sock(sk);
1099 static int compat_ipv6_get_msfilter(struct sock *sk, sockptr_t optval,
1100 sockptr_t optlen, int len)
1102 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1103 struct compat_group_filter gf32;
1104 struct group_filter gf;
1111 if (copy_from_sockptr(&gf32, optval, size0))
1113 gf.gf_interface = gf32.gf_interface;
1114 gf.gf_fmode = gf32.gf_fmode;
1115 num = gf.gf_numsrc = gf32.gf_numsrc;
1116 gf.gf_group = gf32.gf_group;
1118 if (gf.gf_group.ss_family != AF_INET6)
1119 return -EADDRNOTAVAIL;
1121 sockopt_lock_sock(sk);
1122 err = ip6_mc_msfget(sk, &gf, optval, size0);
1123 sockopt_release_sock(sk);
1126 if (num > gf.gf_numsrc)
1128 len = GROUP_FILTER_SIZE(num) - (sizeof(gf)-sizeof(gf32));
1129 if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1130 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode),
1131 &gf.gf_fmode, sizeof(gf32.gf_fmode)) ||
1132 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc),
1133 &gf.gf_numsrc, sizeof(gf32.gf_numsrc)))
1138 int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1139 sockptr_t optval, sockptr_t optlen)
1141 struct ipv6_pinfo *np = inet6_sk(sk);
1145 if (ip6_mroute_opt(optname))
1146 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
1148 if (copy_from_sockptr(&len, optlen, sizeof(int)))
1152 if (sk->sk_protocol != IPPROTO_UDP &&
1153 sk->sk_protocol != IPPROTO_UDPLITE &&
1154 sk->sk_protocol != IPPROTO_TCP)
1155 return -ENOPROTOOPT;
1156 if (sk->sk_state != TCP_ESTABLISHED)
1158 val = sk->sk_family;
1160 case MCAST_MSFILTER:
1161 if (in_compat_syscall())
1162 return compat_ipv6_get_msfilter(sk, optval, optlen, len);
1163 return ipv6_get_msfilter(sk, optval, optlen, len);
1164 case IPV6_2292PKTOPTIONS:
1167 struct sk_buff *skb;
1169 if (sk->sk_type != SOCK_STREAM)
1170 return -ENOPROTOOPT;
1172 if (optval.is_kernel) {
1173 msg.msg_control_is_user = false;
1174 msg.msg_control = optval.kernel;
1176 msg.msg_control_is_user = true;
1177 msg.msg_control_user = optval.user;
1179 msg.msg_controllen = len;
1182 sockopt_lock_sock(sk);
1183 skb = np->pktoptions;
1185 ip6_datagram_recv_ctl(sk, &msg, skb);
1186 sockopt_release_sock(sk);
1188 if (np->rxopt.bits.rxinfo) {
1189 struct in6_pktinfo src_info;
1190 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1191 np->sticky_pktinfo.ipi6_ifindex;
1192 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1193 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1195 if (np->rxopt.bits.rxhlim) {
1196 int hlim = np->mcast_hops;
1197 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1199 if (np->rxopt.bits.rxtclass) {
1200 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1202 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1204 if (np->rxopt.bits.rxoinfo) {
1205 struct in6_pktinfo src_info;
1206 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1207 np->sticky_pktinfo.ipi6_ifindex;
1208 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1209 np->sticky_pktinfo.ipi6_addr;
1210 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1212 if (np->rxopt.bits.rxohlim) {
1213 int hlim = np->mcast_hops;
1214 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1216 if (np->rxopt.bits.rxflow) {
1217 __be32 flowinfo = np->rcv_flowinfo;
1219 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1222 len -= msg.msg_controllen;
1223 return copy_to_sockptr(optlen, &len, sizeof(int));
1227 struct dst_entry *dst;
1231 dst = __sk_dst_get(sk);
1241 val = sk->sk_ipv6only;
1244 case IPV6_RECVPKTINFO:
1245 val = np->rxopt.bits.rxinfo;
1248 case IPV6_2292PKTINFO:
1249 val = np->rxopt.bits.rxoinfo;
1252 case IPV6_RECVHOPLIMIT:
1253 val = np->rxopt.bits.rxhlim;
1256 case IPV6_2292HOPLIMIT:
1257 val = np->rxopt.bits.rxohlim;
1260 case IPV6_RECVRTHDR:
1261 val = np->rxopt.bits.srcrt;
1264 case IPV6_2292RTHDR:
1265 val = np->rxopt.bits.osrcrt;
1269 case IPV6_RTHDRDSTOPTS:
1273 struct ipv6_txoptions *opt;
1275 sockopt_lock_sock(sk);
1276 opt = rcu_dereference_protected(np->opt,
1277 lockdep_sock_is_held(sk));
1278 len = ipv6_getsockopt_sticky(sk, opt, optname, optval, len);
1279 sockopt_release_sock(sk);
1280 /* check if ipv6_getsockopt_sticky() returns err code */
1283 return copy_to_sockptr(optlen, &len, sizeof(int));
1286 case IPV6_RECVHOPOPTS:
1287 val = np->rxopt.bits.hopopts;
1290 case IPV6_2292HOPOPTS:
1291 val = np->rxopt.bits.ohopopts;
1294 case IPV6_RECVDSTOPTS:
1295 val = np->rxopt.bits.dstopts;
1298 case IPV6_2292DSTOPTS:
1299 val = np->rxopt.bits.odstopts;
1306 case IPV6_RECVTCLASS:
1307 val = np->rxopt.bits.rxtclass;
1311 val = np->rxopt.bits.rxflow;
1314 case IPV6_RECVPATHMTU:
1315 val = np->rxopt.bits.rxpmtu;
1320 struct dst_entry *dst;
1321 struct ip6_mtuinfo mtuinfo;
1323 if (len < sizeof(mtuinfo))
1326 len = sizeof(mtuinfo);
1327 memset(&mtuinfo, 0, sizeof(mtuinfo));
1330 dst = __sk_dst_get(sk);
1332 mtuinfo.ip6m_mtu = dst_mtu(dst);
1334 if (!mtuinfo.ip6m_mtu)
1337 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1339 if (copy_to_sockptr(optval, &mtuinfo, len))
1345 case IPV6_TRANSPARENT:
1346 val = inet_sk(sk)->transparent;
1350 val = inet_sk(sk)->freebind;
1353 case IPV6_RECVORIGDSTADDR:
1354 val = np->rxopt.bits.rxorigdstaddr;
1357 case IPV6_UNICAST_HOPS:
1358 case IPV6_MULTICAST_HOPS:
1360 struct dst_entry *dst;
1362 if (optname == IPV6_UNICAST_HOPS)
1363 val = np->hop_limit;
1365 val = np->mcast_hops;
1369 dst = __sk_dst_get(sk);
1371 val = ip6_dst_hoplimit(dst);
1376 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1380 case IPV6_MULTICAST_LOOP:
1384 case IPV6_MULTICAST_IF:
1385 val = np->mcast_oif;
1388 case IPV6_MULTICAST_ALL:
1392 case IPV6_UNICAST_IF:
1393 val = (__force int)htonl((__u32) np->ucast_oif);
1396 case IPV6_MTU_DISCOVER:
1404 case IPV6_FLOWINFO_SEND:
1408 case IPV6_FLOWLABEL_MGR:
1410 struct in6_flowlabel_req freq;
1413 if (len < sizeof(freq))
1416 if (copy_from_sockptr(&freq, optval, sizeof(freq)))
1419 if (freq.flr_action != IPV6_FL_A_GET)
1423 flags = freq.flr_flags;
1425 memset(&freq, 0, sizeof(freq));
1427 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1431 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1433 if (copy_to_sockptr(optval, &freq, len))
1439 case IPV6_ADDR_PREFERENCES:
1442 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1443 val |= IPV6_PREFER_SRC_TMP;
1444 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1445 val |= IPV6_PREFER_SRC_PUBLIC;
1447 /* XXX: should we return system default? */
1448 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1451 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1452 val |= IPV6_PREFER_SRC_COA;
1454 val |= IPV6_PREFER_SRC_HOME;
1457 case IPV6_MINHOPCOUNT:
1458 val = np->min_hopcount;
1465 case IPV6_AUTOFLOWLABEL:
1466 val = ip6_autoflowlabel(sock_net(sk), np);
1469 case IPV6_RECVFRAGSIZE:
1470 val = np->rxopt.bits.recvfragsize;
1473 case IPV6_ROUTER_ALERT_ISOLATE:
1474 val = np->rtalert_isolate;
1477 case IPV6_RECVERR_RFC4884:
1478 val = np->recverr_rfc4884;
1482 return -ENOPROTOOPT;
1484 len = min_t(unsigned int, sizeof(int), len);
1485 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1487 if (copy_to_sockptr(optval, &val, len))
1492 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1493 char __user *optval, int __user *optlen)
1497 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1498 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1500 if (level != SOL_IPV6)
1501 return -ENOPROTOOPT;
1503 err = do_ipv6_getsockopt(sk, level, optname,
1504 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
1505 #ifdef CONFIG_NETFILTER
1506 /* we need to exclude all possible ENOPROTOOPTs except default case */
1507 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1510 if (get_user(len, optlen))
1513 err = nf_getsockopt(sk, PF_INET6, optname, optval, &len);
1515 err = put_user(len, optlen);
1520 EXPORT_SYMBOL(ipv6_getsockopt);