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_test_bit(IS_ICSK
, sk
)) {
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
= unrcu_pointer(xchg(&inet6_sk(sk
)->opt
, RCU_INITIALIZER(opt
)));
120 static bool setsockopt_needs_rtnl(int optname
)
124 case IPV6_ADD_MEMBERSHIP
:
125 case IPV6_DROP_MEMBERSHIP
:
126 case IPV6_JOIN_ANYCAST
:
127 case IPV6_LEAVE_ANYCAST
:
128 case MCAST_JOIN_GROUP
:
129 case MCAST_LEAVE_GROUP
:
130 case MCAST_JOIN_SOURCE_GROUP
:
131 case MCAST_LEAVE_SOURCE_GROUP
:
132 case MCAST_BLOCK_SOURCE
:
133 case MCAST_UNBLOCK_SOURCE
:
140 static int copy_group_source_from_sockptr(struct group_source_req
*greqs
,
141 sockptr_t optval
, int optlen
)
143 if (in_compat_syscall()) {
144 struct compat_group_source_req gr32
;
146 if (optlen
< sizeof(gr32
))
148 if (copy_from_sockptr(&gr32
, optval
, sizeof(gr32
)))
150 greqs
->gsr_interface
= gr32
.gsr_interface
;
151 greqs
->gsr_group
= gr32
.gsr_group
;
152 greqs
->gsr_source
= gr32
.gsr_source
;
154 if (optlen
< sizeof(*greqs
))
156 if (copy_from_sockptr(greqs
, optval
, sizeof(*greqs
)))
163 static int do_ipv6_mcast_group_source(struct sock
*sk
, int optname
,
164 sockptr_t optval
, int optlen
)
166 struct group_source_req greqs
;
170 ret
= copy_group_source_from_sockptr(&greqs
, optval
, optlen
);
174 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
175 greqs
.gsr_source
.ss_family
!= AF_INET6
)
176 return -EADDRNOTAVAIL
;
178 if (optname
== MCAST_BLOCK_SOURCE
) {
179 omode
= MCAST_EXCLUDE
;
181 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
182 omode
= MCAST_EXCLUDE
;
184 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
185 struct sockaddr_in6
*psin6
;
188 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
189 retv
= ipv6_sock_mc_join_ssm(sk
, greqs
.gsr_interface
,
192 /* prior join w/ different source is ok */
193 if (retv
&& retv
!= -EADDRINUSE
)
195 omode
= MCAST_INCLUDE
;
197 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
198 omode
= MCAST_INCLUDE
;
201 return ip6_mc_source(add
, omode
, sk
, &greqs
);
204 static int ipv6_set_mcast_msfilter(struct sock
*sk
, sockptr_t optval
,
207 struct group_filter
*gsf
;
210 if (optlen
< GROUP_FILTER_SIZE(0))
212 if (optlen
> READ_ONCE(sock_net(sk
)->core
.sysctl_optmem_max
))
215 gsf
= memdup_sockptr(optval
, optlen
);
219 /* numsrc >= (4G-140)/128 overflow in 32 bits */
221 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
222 gsf
->gf_numsrc
> sysctl_mld_max_msf
)
226 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
)
229 ret
= ip6_mc_msfilter(sk
, gsf
, gsf
->gf_slist_flex
);
235 static int compat_ipv6_set_mcast_msfilter(struct sock
*sk
, sockptr_t optval
,
238 const int size0
= offsetof(struct compat_group_filter
, gf_slist_flex
);
239 struct compat_group_filter
*gf32
;
246 if (optlen
> READ_ONCE(sock_net(sk
)->core
.sysctl_optmem_max
) - 4)
249 p
= kmalloc(optlen
+ 4, GFP_KERNEL
);
253 gf32
= p
+ 4; /* we want ->gf_group and ->gf_slist_flex aligned */
255 if (copy_from_sockptr(gf32
, optval
, optlen
))
258 /* numsrc >= (4G-140)/128 overflow in 32 bits */
261 if (n
>= 0x1ffffffU
|| n
> sysctl_mld_max_msf
)
265 if (offsetof(struct compat_group_filter
, gf_slist_flex
[n
]) > optlen
)
268 ret
= ip6_mc_msfilter(sk
, &(struct group_filter
){
269 .gf_interface
= gf32
->gf_interface
,
270 .gf_group
= gf32
->gf_group
,
271 .gf_fmode
= gf32
->gf_fmode
,
272 .gf_numsrc
= gf32
->gf_numsrc
}, gf32
->gf_slist_flex
);
279 static int ipv6_mcast_join_leave(struct sock
*sk
, int optname
,
280 sockptr_t optval
, int optlen
)
282 struct sockaddr_in6
*psin6
;
283 struct group_req greq
;
285 if (optlen
< sizeof(greq
))
287 if (copy_from_sockptr(&greq
, optval
, sizeof(greq
)))
290 if (greq
.gr_group
.ss_family
!= AF_INET6
)
291 return -EADDRNOTAVAIL
;
292 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
293 if (optname
== MCAST_JOIN_GROUP
)
294 return ipv6_sock_mc_join(sk
, greq
.gr_interface
,
296 return ipv6_sock_mc_drop(sk
, greq
.gr_interface
, &psin6
->sin6_addr
);
299 static int compat_ipv6_mcast_join_leave(struct sock
*sk
, int optname
,
300 sockptr_t optval
, int optlen
)
302 struct compat_group_req gr32
;
303 struct sockaddr_in6
*psin6
;
305 if (optlen
< sizeof(gr32
))
307 if (copy_from_sockptr(&gr32
, optval
, sizeof(gr32
)))
310 if (gr32
.gr_group
.ss_family
!= AF_INET6
)
311 return -EADDRNOTAVAIL
;
312 psin6
= (struct sockaddr_in6
*)&gr32
.gr_group
;
313 if (optname
== MCAST_JOIN_GROUP
)
314 return ipv6_sock_mc_join(sk
, gr32
.gr_interface
,
316 return ipv6_sock_mc_drop(sk
, gr32
.gr_interface
, &psin6
->sin6_addr
);
319 static int ipv6_set_opt_hdr(struct sock
*sk
, int optname
, sockptr_t optval
,
322 struct ipv6_pinfo
*np
= inet6_sk(sk
);
323 struct ipv6_opt_hdr
*new = NULL
;
324 struct net
*net
= sock_net(sk
);
325 struct ipv6_txoptions
*opt
;
328 /* hop-by-hop / destination options are privileged option */
329 if (optname
!= IPV6_RTHDR
&& !sockopt_ns_capable(net
->user_ns
, CAP_NET_RAW
))
332 /* remove any sticky options header with a zero option
333 * length, per RFC3542.
336 if (sockptr_is_null(optval
))
338 if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
343 new = memdup_sockptr(optval
, optlen
);
346 if (unlikely(ipv6_optlen(new) > optlen
)) {
352 opt
= rcu_dereference_protected(np
->opt
, lockdep_sock_is_held(sk
));
353 opt
= ipv6_renew_options(sk
, opt
, optname
, new);
358 /* routing header option needs extra check */
360 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
361 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
362 switch (rthdr
->type
) {
363 #if IS_ENABLED(CONFIG_IPV6_MIP6)
364 case IPV6_SRCRT_TYPE_2
:
365 if (rthdr
->hdrlen
!= 2 || rthdr
->segments_left
!= 1)
369 case IPV6_SRCRT_TYPE_4
:
371 struct ipv6_sr_hdr
*srh
=
372 (struct ipv6_sr_hdr
*)opt
->srcrt
;
374 if (!seg6_validate_srh(srh
, optlen
, false))
384 opt
= ipv6_update_options(sk
, opt
);
387 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
393 int do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
394 sockptr_t optval
, unsigned int optlen
)
396 struct ipv6_pinfo
*np
= inet6_sk(sk
);
397 struct net
*net
= sock_net(sk
);
399 int retv
= -ENOPROTOOPT
;
400 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
402 if (sockptr_is_null(optval
))
405 if (optlen
>= sizeof(int)) {
406 if (copy_from_sockptr(&val
, optval
, sizeof(val
)))
412 valbool
= (val
!= 0);
414 if (ip6_mroute_opt(optname
))
415 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
417 /* Handle options that can be set without locking the socket. */
419 case IPV6_UNICAST_HOPS
:
420 if (optlen
< sizeof(int))
422 if (val
> 255 || val
< -1)
424 WRITE_ONCE(np
->hop_limit
, val
);
426 case IPV6_MULTICAST_LOOP
:
427 if (optlen
< sizeof(int))
431 inet6_assign_bit(MC6_LOOP
, sk
, valbool
);
433 case IPV6_MULTICAST_HOPS
:
434 if (sk
->sk_type
== SOCK_STREAM
)
436 if (optlen
< sizeof(int))
438 if (val
> 255 || val
< -1)
440 WRITE_ONCE(np
->mcast_hops
,
441 val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
444 if (optlen
< sizeof(int))
446 if (val
&& val
< IPV6_MIN_MTU
)
448 WRITE_ONCE(np
->frag_size
, val
);
450 case IPV6_MINHOPCOUNT
:
451 if (optlen
< sizeof(int))
453 if (val
< 0 || val
> 255)
457 static_branch_enable(&ip6_min_hopcount
);
459 /* tcp_v6_err() and tcp_v6_rcv() might read min_hopcount
460 * while we are changing it.
462 WRITE_ONCE(np
->min_hopcount
, val
);
464 case IPV6_RECVERR_RFC4884
:
465 if (optlen
< sizeof(int))
467 if (val
< 0 || val
> 1)
469 inet6_assign_bit(RECVERR6_RFC4884
, sk
, valbool
);
471 case IPV6_MULTICAST_ALL
:
472 if (optlen
< sizeof(int))
474 inet6_assign_bit(MC6_ALL
, sk
, valbool
);
476 case IPV6_AUTOFLOWLABEL
:
477 inet6_assign_bit(AUTOFLOWLABEL
, sk
, valbool
);
478 inet6_set_bit(AUTOFLOWLABEL_SET
, sk
);
481 inet6_assign_bit(DONTFRAG
, sk
, valbool
);
484 if (optlen
< sizeof(int))
486 inet6_assign_bit(RECVERR6
, sk
, valbool
);
488 skb_errqueue_purge(&sk
->sk_error_queue
);
490 case IPV6_ROUTER_ALERT_ISOLATE
:
491 if (optlen
< sizeof(int))
493 inet6_assign_bit(RTALERT_ISOLATE
, sk
, valbool
);
495 case IPV6_MTU_DISCOVER
:
496 if (optlen
< sizeof(int))
498 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
500 WRITE_ONCE(np
->pmtudisc
, val
);
502 case IPV6_FLOWINFO_SEND
:
503 if (optlen
< sizeof(int))
505 inet6_assign_bit(SNDFLOW
, sk
, valbool
);
507 case IPV6_ADDR_PREFERENCES
:
508 if (optlen
< sizeof(int))
510 return ip6_sock_set_addr_preferences(sk
, val
);
511 case IPV6_MULTICAST_IF
:
512 if (sk
->sk_type
== SOCK_STREAM
)
514 if (optlen
< sizeof(int))
517 struct net_device
*dev
;
518 int bound_dev_if
, midx
;
522 dev
= dev_get_by_index_rcu(net
, val
);
527 midx
= l3mdev_master_ifindex_rcu(dev
);
531 bound_dev_if
= READ_ONCE(sk
->sk_bound_dev_if
);
533 bound_dev_if
!= val
&&
534 (!midx
|| midx
!= bound_dev_if
))
537 WRITE_ONCE(np
->mcast_oif
, val
);
539 case IPV6_UNICAST_IF
:
541 struct net_device
*dev
;
544 if (optlen
!= sizeof(int))
547 ifindex
= (__force
int)ntohl((__force __be32
)val
);
549 WRITE_ONCE(np
->ucast_oif
, 0);
553 dev
= dev_get_by_index(net
, ifindex
);
555 return -EADDRNOTAVAIL
;
558 if (READ_ONCE(sk
->sk_bound_dev_if
))
561 WRITE_ONCE(np
->ucast_oif
, ifindex
);
567 sockopt_lock_sock(sk
);
569 /* Another thread has converted the socket into IPv4 with
570 * IPV6_ADDRFORM concurrently.
572 if (unlikely(sk
->sk_family
!= AF_INET6
))
578 if (optlen
< sizeof(int))
580 if (val
== PF_INET
) {
581 if (sk
->sk_type
== SOCK_RAW
)
584 if (sk
->sk_protocol
== IPPROTO_UDP
||
585 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
586 struct udp_sock
*up
= udp_sk(sk
);
587 if (up
->pending
== AF_INET6
) {
591 } else if (sk
->sk_protocol
== IPPROTO_TCP
) {
592 if (sk
->sk_prot
!= &tcpv6_prot
) {
600 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
605 if (ipv6_only_sock(sk
) ||
606 !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
607 retv
= -EADDRNOTAVAIL
;
611 __ipv6_sock_mc_close(sk
);
612 __ipv6_sock_ac_close(sk
);
614 if (sk
->sk_protocol
== IPPROTO_TCP
) {
615 struct inet_connection_sock
*icsk
= inet_csk(sk
);
617 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
618 sock_prot_inuse_add(net
, &tcp_prot
, 1);
620 /* Paired with READ_ONCE(sk->sk_prot) in inet6_stream_ops */
621 WRITE_ONCE(sk
->sk_prot
, &tcp_prot
);
622 /* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
623 WRITE_ONCE(icsk
->icsk_af_ops
, &ipv4_specific
);
624 WRITE_ONCE(sk
->sk_socket
->ops
, &inet_stream_ops
);
625 WRITE_ONCE(sk
->sk_family
, PF_INET
);
626 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
628 struct proto
*prot
= &udp_prot
;
630 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
631 prot
= &udplite_prot
;
633 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
634 sock_prot_inuse_add(net
, prot
, 1);
636 /* Paired with READ_ONCE(sk->sk_prot) in inet6_dgram_ops */
637 WRITE_ONCE(sk
->sk_prot
, prot
);
638 WRITE_ONCE(sk
->sk_socket
->ops
, &inet_dgram_ops
);
639 WRITE_ONCE(sk
->sk_family
, PF_INET
);
642 /* Disable all options not to allocate memory anymore,
643 * but there is still a race. See the lockless path
644 * in udpv6_sendmsg() and ipv6_local_rxpmtu().
648 inet6_cleanup_sock(sk
);
650 module_put(THIS_MODULE
);
657 if (optlen
< sizeof(int) ||
658 inet_sk(sk
)->inet_num
)
660 sk
->sk_ipv6only
= valbool
;
664 case IPV6_RECVPKTINFO
:
665 if (optlen
< sizeof(int))
667 np
->rxopt
.bits
.rxinfo
= valbool
;
671 case IPV6_2292PKTINFO
:
672 if (optlen
< sizeof(int))
674 np
->rxopt
.bits
.rxoinfo
= valbool
;
678 case IPV6_RECVHOPLIMIT
:
679 if (optlen
< sizeof(int))
681 np
->rxopt
.bits
.rxhlim
= valbool
;
685 case IPV6_2292HOPLIMIT
:
686 if (optlen
< sizeof(int))
688 np
->rxopt
.bits
.rxohlim
= valbool
;
693 if (optlen
< sizeof(int))
695 np
->rxopt
.bits
.srcrt
= valbool
;
700 if (optlen
< sizeof(int))
702 np
->rxopt
.bits
.osrcrt
= valbool
;
706 case IPV6_RECVHOPOPTS
:
707 if (optlen
< sizeof(int))
709 np
->rxopt
.bits
.hopopts
= valbool
;
713 case IPV6_2292HOPOPTS
:
714 if (optlen
< sizeof(int))
716 np
->rxopt
.bits
.ohopopts
= valbool
;
720 case IPV6_RECVDSTOPTS
:
721 if (optlen
< sizeof(int))
723 np
->rxopt
.bits
.dstopts
= valbool
;
727 case IPV6_2292DSTOPTS
:
728 if (optlen
< sizeof(int))
730 np
->rxopt
.bits
.odstopts
= valbool
;
735 if (optlen
< sizeof(int))
737 if (val
< -1 || val
> 0xff)
739 /* RFC 3542, 6.5: default traffic class of 0x0 */
742 if (sk
->sk_type
== SOCK_STREAM
) {
743 val
&= ~INET_ECN_MASK
;
744 val
|= np
->tclass
& INET_ECN_MASK
;
746 if (np
->tclass
!= val
) {
753 case IPV6_RECVTCLASS
:
754 if (optlen
< sizeof(int))
756 np
->rxopt
.bits
.rxtclass
= valbool
;
761 if (optlen
< sizeof(int))
763 np
->rxopt
.bits
.rxflow
= valbool
;
767 case IPV6_RECVPATHMTU
:
768 if (optlen
< sizeof(int))
770 np
->rxopt
.bits
.rxpmtu
= valbool
;
774 case IPV6_TRANSPARENT
:
775 if (valbool
&& !sockopt_ns_capable(net
->user_ns
, CAP_NET_RAW
) &&
776 !sockopt_ns_capable(net
->user_ns
, CAP_NET_ADMIN
)) {
780 if (optlen
< sizeof(int))
782 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
783 inet_assign_bit(TRANSPARENT
, sk
, valbool
);
788 if (optlen
< sizeof(int))
790 /* we also don't have a separate freebind bit for IPV6 */
791 inet_assign_bit(FREEBIND
, sk
, valbool
);
795 case IPV6_RECVORIGDSTADDR
:
796 if (optlen
< sizeof(int))
798 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
803 case IPV6_RTHDRDSTOPTS
:
806 retv
= ipv6_set_opt_hdr(sk
, optname
, optval
, optlen
);
811 struct in6_pktinfo pkt
;
815 else if (optlen
< sizeof(struct in6_pktinfo
) ||
816 sockptr_is_null(optval
))
819 if (copy_from_sockptr(&pkt
, optval
, sizeof(pkt
))) {
823 if (!sk_dev_equal_l3scope(sk
, pkt
.ipi6_ifindex
))
826 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
827 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
832 case IPV6_2292PKTOPTIONS
:
834 struct ipv6_txoptions
*opt
= NULL
;
837 struct ipcm6_cookie ipc6
;
839 memset(&fl6
, 0, sizeof(fl6
));
840 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
841 fl6
.flowi6_mark
= sk
->sk_mark
;
846 /* 1K is probably excessive
847 * 1K is surely not enough, 2K per standard header is 16K.
850 if (optlen
> 64*1024)
853 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
858 memset(opt
, 0, sizeof(*opt
));
859 refcount_set(&opt
->refcnt
, 1);
860 opt
->tot_len
= sizeof(*opt
) + optlen
;
862 if (copy_from_sockptr(opt
+ 1, optval
, optlen
))
865 msg
.msg_controllen
= optlen
;
866 msg
.msg_control_is_user
= false;
867 msg
.msg_control
= (void *)(opt
+1);
870 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, &ipc6
);
875 opt
= ipv6_update_options(sk
, opt
);
878 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
884 case IPV6_ADD_MEMBERSHIP
:
885 case IPV6_DROP_MEMBERSHIP
:
887 struct ipv6_mreq mreq
;
889 if (optlen
< sizeof(struct ipv6_mreq
))
893 if (inet_test_bit(IS_ICSK
, sk
))
897 if (copy_from_sockptr(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
900 if (optname
== IPV6_ADD_MEMBERSHIP
)
901 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
903 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
906 case IPV6_JOIN_ANYCAST
:
907 case IPV6_LEAVE_ANYCAST
:
909 struct ipv6_mreq mreq
;
911 if (optlen
< sizeof(struct ipv6_mreq
))
915 if (copy_from_sockptr(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
918 if (optname
== IPV6_JOIN_ANYCAST
)
919 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
921 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
924 case MCAST_JOIN_GROUP
:
925 case MCAST_LEAVE_GROUP
:
926 if (in_compat_syscall())
927 retv
= compat_ipv6_mcast_join_leave(sk
, optname
, optval
,
930 retv
= ipv6_mcast_join_leave(sk
, optname
, optval
,
933 case MCAST_JOIN_SOURCE_GROUP
:
934 case MCAST_LEAVE_SOURCE_GROUP
:
935 case MCAST_BLOCK_SOURCE
:
936 case MCAST_UNBLOCK_SOURCE
:
937 retv
= do_ipv6_mcast_group_source(sk
, optname
, optval
, optlen
);
940 if (in_compat_syscall())
941 retv
= compat_ipv6_set_mcast_msfilter(sk
, optval
,
944 retv
= ipv6_set_mcast_msfilter(sk
, optval
, optlen
);
946 case IPV6_ROUTER_ALERT
:
947 if (optlen
< sizeof(int))
949 retv
= ip6_ra_control(sk
, val
);
951 inet6_assign_bit(RTALERT
, sk
, valbool
);
953 case IPV6_FLOWLABEL_MGR
:
954 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
956 case IPV6_IPSEC_POLICY
:
957 case IPV6_XFRM_POLICY
:
959 if (!sockopt_ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
961 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
964 case IPV6_RECVFRAGSIZE
:
965 np
->rxopt
.bits
.recvfragsize
= valbool
;
971 sockopt_release_sock(sk
);
982 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
, sockptr_t optval
,
987 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
988 return ip_setsockopt(sk
, level
, optname
, optval
, optlen
);
990 if (level
!= SOL_IPV6
)
993 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
994 #ifdef CONFIG_NETFILTER
995 /* we need to exclude all possible ENOPROTOOPTs except default case */
996 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
997 optname
!= IPV6_XFRM_POLICY
)
998 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
, optlen
);
1002 EXPORT_SYMBOL(ipv6_setsockopt
);
1004 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
1005 int optname
, sockptr_t optval
, int len
)
1007 struct ipv6_opt_hdr
*hdr
;
1016 case IPV6_RTHDRDSTOPTS
:
1020 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
1026 return -EINVAL
; /* should not happen */
1032 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
1033 if (copy_to_sockptr(optval
, hdr
, len
))
1038 static int ipv6_get_msfilter(struct sock
*sk
, sockptr_t optval
,
1039 sockptr_t optlen
, int len
)
1041 const int size0
= offsetof(struct group_filter
, gf_slist_flex
);
1042 struct group_filter gsf
;
1048 if (copy_from_sockptr(&gsf
, optval
, size0
))
1050 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1051 return -EADDRNOTAVAIL
;
1052 num
= gsf
.gf_numsrc
;
1053 sockopt_lock_sock(sk
);
1054 err
= ip6_mc_msfget(sk
, &gsf
, optval
, size0
);
1056 if (num
> gsf
.gf_numsrc
)
1057 num
= gsf
.gf_numsrc
;
1058 len
= GROUP_FILTER_SIZE(num
);
1059 if (copy_to_sockptr(optlen
, &len
, sizeof(int)) ||
1060 copy_to_sockptr(optval
, &gsf
, size0
))
1063 sockopt_release_sock(sk
);
1067 static int compat_ipv6_get_msfilter(struct sock
*sk
, sockptr_t optval
,
1068 sockptr_t optlen
, int len
)
1070 const int size0
= offsetof(struct compat_group_filter
, gf_slist_flex
);
1071 struct compat_group_filter gf32
;
1072 struct group_filter gf
;
1079 if (copy_from_sockptr(&gf32
, optval
, size0
))
1081 gf
.gf_interface
= gf32
.gf_interface
;
1082 gf
.gf_fmode
= gf32
.gf_fmode
;
1083 num
= gf
.gf_numsrc
= gf32
.gf_numsrc
;
1084 gf
.gf_group
= gf32
.gf_group
;
1086 if (gf
.gf_group
.ss_family
!= AF_INET6
)
1087 return -EADDRNOTAVAIL
;
1089 sockopt_lock_sock(sk
);
1090 err
= ip6_mc_msfget(sk
, &gf
, optval
, size0
);
1091 sockopt_release_sock(sk
);
1094 if (num
> gf
.gf_numsrc
)
1096 len
= GROUP_FILTER_SIZE(num
) - (sizeof(gf
)-sizeof(gf32
));
1097 if (copy_to_sockptr(optlen
, &len
, sizeof(int)) ||
1098 copy_to_sockptr_offset(optval
, offsetof(struct compat_group_filter
, gf_fmode
),
1099 &gf
.gf_fmode
, sizeof(gf32
.gf_fmode
)) ||
1100 copy_to_sockptr_offset(optval
, offsetof(struct compat_group_filter
, gf_numsrc
),
1101 &gf
.gf_numsrc
, sizeof(gf32
.gf_numsrc
)))
1106 int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1107 sockptr_t optval
, sockptr_t optlen
)
1109 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1113 if (ip6_mroute_opt(optname
))
1114 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1116 if (copy_from_sockptr(&len
, optlen
, sizeof(int)))
1120 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
1121 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
1122 sk
->sk_protocol
!= IPPROTO_TCP
)
1123 return -ENOPROTOOPT
;
1124 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1126 val
= sk
->sk_family
;
1128 case MCAST_MSFILTER
:
1129 if (in_compat_syscall())
1130 return compat_ipv6_get_msfilter(sk
, optval
, optlen
, len
);
1131 return ipv6_get_msfilter(sk
, optval
, optlen
, len
);
1132 case IPV6_2292PKTOPTIONS
:
1135 struct sk_buff
*skb
;
1137 if (sk
->sk_type
!= SOCK_STREAM
)
1138 return -ENOPROTOOPT
;
1140 if (optval
.is_kernel
) {
1141 msg
.msg_control_is_user
= false;
1142 msg
.msg_control
= optval
.kernel
;
1144 msg
.msg_control_is_user
= true;
1145 msg
.msg_control_user
= optval
.user
;
1147 msg
.msg_controllen
= len
;
1150 sockopt_lock_sock(sk
);
1151 skb
= np
->pktoptions
;
1153 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1154 sockopt_release_sock(sk
);
1156 if (np
->rxopt
.bits
.rxinfo
) {
1157 int mcast_oif
= READ_ONCE(np
->mcast_oif
);
1158 struct in6_pktinfo src_info
;
1160 src_info
.ipi6_ifindex
= mcast_oif
? :
1161 np
->sticky_pktinfo
.ipi6_ifindex
;
1162 src_info
.ipi6_addr
= mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1163 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1165 if (np
->rxopt
.bits
.rxhlim
) {
1166 int hlim
= READ_ONCE(np
->mcast_hops
);
1168 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1170 if (np
->rxopt
.bits
.rxtclass
) {
1171 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1173 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1175 if (np
->rxopt
.bits
.rxoinfo
) {
1176 int mcast_oif
= READ_ONCE(np
->mcast_oif
);
1177 struct in6_pktinfo src_info
;
1179 src_info
.ipi6_ifindex
= mcast_oif
? :
1180 np
->sticky_pktinfo
.ipi6_ifindex
;
1181 src_info
.ipi6_addr
= mcast_oif
? sk
->sk_v6_daddr
:
1182 np
->sticky_pktinfo
.ipi6_addr
;
1183 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1185 if (np
->rxopt
.bits
.rxohlim
) {
1186 int hlim
= READ_ONCE(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 copy_to_sockptr(optlen
, &len
, sizeof(int));
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
;
1249 sockopt_lock_sock(sk
);
1250 opt
= rcu_dereference_protected(np
->opt
,
1251 lockdep_sock_is_held(sk
));
1252 len
= ipv6_getsockopt_sticky(sk
, opt
, optname
, optval
, len
);
1253 sockopt_release_sock(sk
);
1254 /* check if ipv6_getsockopt_sticky() returns err code */
1257 return copy_to_sockptr(optlen
, &len
, sizeof(int));
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 (copy_to_sockptr(optlen
, &len
, sizeof(int)))
1313 if (copy_to_sockptr(optval
, &mtuinfo
, len
))
1319 case IPV6_TRANSPARENT
:
1320 val
= inet_test_bit(TRANSPARENT
, sk
);
1324 val
= inet_test_bit(FREEBIND
, sk
);
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
= READ_ONCE(np
->hop_limit
);
1339 val
= READ_ONCE(np
->mcast_hops
);
1343 dst
= __sk_dst_get(sk
);
1345 val
= ip6_dst_hoplimit(dst
);
1350 val
= READ_ONCE(sock_net(sk
)->ipv6
.devconf_all
->hop_limit
);
1354 case IPV6_MULTICAST_LOOP
:
1355 val
= inet6_test_bit(MC6_LOOP
, sk
);
1358 case IPV6_MULTICAST_IF
:
1359 val
= READ_ONCE(np
->mcast_oif
);
1362 case IPV6_MULTICAST_ALL
:
1363 val
= inet6_test_bit(MC6_ALL
, sk
);
1366 case IPV6_UNICAST_IF
:
1367 val
= (__force
int)htonl((__u32
) READ_ONCE(np
->ucast_oif
));
1370 case IPV6_MTU_DISCOVER
:
1371 val
= READ_ONCE(np
->pmtudisc
);
1375 val
= inet6_test_bit(RECVERR6
, sk
);
1378 case IPV6_FLOWINFO_SEND
:
1379 val
= inet6_test_bit(SNDFLOW
, sk
);
1382 case IPV6_FLOWLABEL_MGR
:
1384 struct in6_flowlabel_req freq
;
1387 if (len
< sizeof(freq
))
1390 if (copy_from_sockptr(&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 (copy_to_sockptr(optlen
, &len
, sizeof(int)))
1407 if (copy_to_sockptr(optval
, &freq
, len
))
1413 case IPV6_ADDR_PREFERENCES
:
1415 u8 srcprefs
= READ_ONCE(np
->srcprefs
);
1418 if (srcprefs
& IPV6_PREFER_SRC_TMP
)
1419 val
|= IPV6_PREFER_SRC_TMP
;
1420 else if (srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1421 val
|= IPV6_PREFER_SRC_PUBLIC
;
1423 /* XXX: should we return system default? */
1424 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1427 if (srcprefs
& IPV6_PREFER_SRC_COA
)
1428 val
|= IPV6_PREFER_SRC_COA
;
1430 val
|= IPV6_PREFER_SRC_HOME
;
1433 case IPV6_MINHOPCOUNT
:
1434 val
= READ_ONCE(np
->min_hopcount
);
1438 val
= inet6_test_bit(DONTFRAG
, sk
);
1441 case IPV6_AUTOFLOWLABEL
:
1442 val
= ip6_autoflowlabel(sock_net(sk
), sk
);
1445 case IPV6_RECVFRAGSIZE
:
1446 val
= np
->rxopt
.bits
.recvfragsize
;
1449 case IPV6_ROUTER_ALERT
:
1450 val
= inet6_test_bit(RTALERT
, sk
);
1453 case IPV6_ROUTER_ALERT_ISOLATE
:
1454 val
= inet6_test_bit(RTALERT_ISOLATE
, sk
);
1457 case IPV6_RECVERR_RFC4884
:
1458 val
= inet6_test_bit(RECVERR6_RFC4884
, sk
);
1462 return -ENOPROTOOPT
;
1464 len
= min_t(unsigned int, sizeof(int), len
);
1465 if (copy_to_sockptr(optlen
, &len
, sizeof(int)))
1467 if (copy_to_sockptr(optval
, &val
, len
))
1472 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1473 char __user
*optval
, int __user
*optlen
)
1477 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1478 return ip_getsockopt(sk
, level
, optname
, optval
, optlen
);
1480 if (level
!= SOL_IPV6
)
1481 return -ENOPROTOOPT
;
1483 err
= do_ipv6_getsockopt(sk
, level
, optname
,
1484 USER_SOCKPTR(optval
), USER_SOCKPTR(optlen
));
1485 #ifdef CONFIG_NETFILTER
1486 /* we need to exclude all possible ENOPROTOOPTs except default case */
1487 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1490 if (get_user(len
, optlen
))
1493 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1495 err
= put_user(len
, optlen
);
1500 EXPORT_SYMBOL(ipv6_getsockopt
);