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 do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
140 char __user
*optval
, unsigned int optlen
)
142 struct ipv6_pinfo
*np
= inet6_sk(sk
);
143 struct net
*net
= sock_net(sk
);
145 int retv
= -ENOPROTOOPT
;
146 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
151 if (optlen
>= sizeof(int)) {
152 if (get_user(val
, (int __user
*) optval
))
158 valbool
= (val
!= 0);
160 if (ip6_mroute_opt(optname
))
161 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
170 if (optlen
< sizeof(int))
172 if (val
== PF_INET
) {
173 struct ipv6_txoptions
*opt
;
174 struct sk_buff
*pktopt
;
176 if (sk
->sk_type
== SOCK_RAW
)
179 if (sk
->sk_protocol
== IPPROTO_UDP
||
180 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
181 struct udp_sock
*up
= udp_sk(sk
);
182 if (up
->pending
== AF_INET6
) {
187 if (sk
->sk_protocol
== IPPROTO_TCP
&&
188 sk
->sk_prot
!= &tcpv6_prot
) {
192 if (sk
->sk_protocol
!= IPPROTO_TCP
)
194 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
199 if (ipv6_only_sock(sk
) ||
200 !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
201 retv
= -EADDRNOTAVAIL
;
205 fl6_free_socklist(sk
);
206 __ipv6_sock_mc_close(sk
);
209 * Sock is moving from IPv6 to IPv4 (sk_prot), so
210 * remove it from the refcnt debug socks count in the
213 sk_refcnt_debug_dec(sk
);
215 if (sk
->sk_protocol
== IPPROTO_TCP
) {
216 struct inet_connection_sock
*icsk
= inet_csk(sk
);
218 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
219 sock_prot_inuse_add(net
, &tcp_prot
, 1);
221 sk
->sk_prot
= &tcp_prot
;
222 icsk
->icsk_af_ops
= &ipv4_specific
;
223 sk
->sk_socket
->ops
= &inet_stream_ops
;
224 sk
->sk_family
= PF_INET
;
225 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
227 struct proto
*prot
= &udp_prot
;
229 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
230 prot
= &udplite_prot
;
232 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
233 sock_prot_inuse_add(net
, prot
, 1);
236 sk
->sk_socket
->ops
= &inet_dgram_ops
;
237 sk
->sk_family
= PF_INET
;
239 opt
= xchg((__force
struct ipv6_txoptions
**)&np
->opt
,
242 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
245 pktopt
= xchg(&np
->pktoptions
, NULL
);
249 * ... and add it to the refcnt debug socks count
250 * in the new family. -acme
252 sk_refcnt_debug_inc(sk
);
253 module_put(THIS_MODULE
);
260 if (optlen
< sizeof(int) ||
261 inet_sk(sk
)->inet_num
)
263 sk
->sk_ipv6only
= valbool
;
267 case IPV6_RECVPKTINFO
:
268 if (optlen
< sizeof(int))
270 np
->rxopt
.bits
.rxinfo
= valbool
;
274 case IPV6_2292PKTINFO
:
275 if (optlen
< sizeof(int))
277 np
->rxopt
.bits
.rxoinfo
= valbool
;
281 case IPV6_RECVHOPLIMIT
:
282 if (optlen
< sizeof(int))
284 np
->rxopt
.bits
.rxhlim
= valbool
;
288 case IPV6_2292HOPLIMIT
:
289 if (optlen
< sizeof(int))
291 np
->rxopt
.bits
.rxohlim
= valbool
;
296 if (optlen
< sizeof(int))
298 np
->rxopt
.bits
.srcrt
= valbool
;
303 if (optlen
< sizeof(int))
305 np
->rxopt
.bits
.osrcrt
= valbool
;
309 case IPV6_RECVHOPOPTS
:
310 if (optlen
< sizeof(int))
312 np
->rxopt
.bits
.hopopts
= valbool
;
316 case IPV6_2292HOPOPTS
:
317 if (optlen
< sizeof(int))
319 np
->rxopt
.bits
.ohopopts
= valbool
;
323 case IPV6_RECVDSTOPTS
:
324 if (optlen
< sizeof(int))
326 np
->rxopt
.bits
.dstopts
= valbool
;
330 case IPV6_2292DSTOPTS
:
331 if (optlen
< sizeof(int))
333 np
->rxopt
.bits
.odstopts
= valbool
;
338 if (optlen
< sizeof(int))
340 if (val
< -1 || val
> 0xff)
342 /* RFC 3542, 6.5: default traffic class of 0x0 */
349 case IPV6_RECVTCLASS
:
350 if (optlen
< sizeof(int))
352 np
->rxopt
.bits
.rxtclass
= valbool
;
357 if (optlen
< sizeof(int))
359 np
->rxopt
.bits
.rxflow
= valbool
;
363 case IPV6_RECVPATHMTU
:
364 if (optlen
< sizeof(int))
366 np
->rxopt
.bits
.rxpmtu
= valbool
;
370 case IPV6_TRANSPARENT
:
371 if (valbool
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
) &&
372 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
)) {
376 if (optlen
< sizeof(int))
378 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
379 inet_sk(sk
)->transparent
= valbool
;
384 if (optlen
< sizeof(int))
386 /* we also don't have a separate freebind bit for IPV6 */
387 inet_sk(sk
)->freebind
= valbool
;
391 case IPV6_RECVORIGDSTADDR
:
392 if (optlen
< sizeof(int))
394 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
399 case IPV6_RTHDRDSTOPTS
:
403 struct ipv6_txoptions
*opt
;
404 struct ipv6_opt_hdr
*new = NULL
;
406 /* hop-by-hop / destination options are privileged option */
408 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
411 /* remove any sticky options header with a zero option
412 * length, per RFC3542.
418 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
419 optlen
& 0x7 || optlen
> 8 * 255)
422 new = memdup_user(optval
, optlen
);
427 if (unlikely(ipv6_optlen(new) > optlen
)) {
433 opt
= rcu_dereference_protected(np
->opt
,
434 lockdep_sock_is_held(sk
));
435 opt
= ipv6_renew_options(sk
, opt
, optname
, new);
442 /* routing header option needs extra check */
444 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
445 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
446 switch (rthdr
->type
) {
447 #if IS_ENABLED(CONFIG_IPV6_MIP6)
448 case IPV6_SRCRT_TYPE_2
:
449 if (rthdr
->hdrlen
!= 2 ||
450 rthdr
->segments_left
!= 1)
455 case IPV6_SRCRT_TYPE_4
:
457 struct ipv6_sr_hdr
*srh
= (struct ipv6_sr_hdr
*)
460 if (!seg6_validate_srh(srh
, optlen
))
470 opt
= ipv6_update_options(sk
, opt
);
473 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
481 struct in6_pktinfo pkt
;
485 else if (optlen
< sizeof(struct in6_pktinfo
) || !optval
)
488 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
492 if (!sk_dev_equal_l3scope(sk
, pkt
.ipi6_ifindex
))
495 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
496 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
501 case IPV6_2292PKTOPTIONS
:
503 struct ipv6_txoptions
*opt
= NULL
;
506 struct ipcm6_cookie ipc6
;
508 memset(&fl6
, 0, sizeof(fl6
));
509 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
510 fl6
.flowi6_mark
= sk
->sk_mark
;
515 /* 1K is probably excessive
516 * 1K is surely not enough, 2K per standard header is 16K.
519 if (optlen
> 64*1024)
522 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
527 memset(opt
, 0, sizeof(*opt
));
528 refcount_set(&opt
->refcnt
, 1);
529 opt
->tot_len
= sizeof(*opt
) + optlen
;
531 if (copy_from_user(opt
+1, optval
, optlen
))
534 msg
.msg_controllen
= optlen
;
535 msg
.msg_control
= (void *)(opt
+1);
538 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, &ipc6
);
543 opt
= ipv6_update_options(sk
, opt
);
546 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
551 case IPV6_UNICAST_HOPS
:
552 if (optlen
< sizeof(int))
554 if (val
> 255 || val
< -1)
560 case IPV6_MULTICAST_HOPS
:
561 if (sk
->sk_type
== SOCK_STREAM
)
563 if (optlen
< sizeof(int))
565 if (val
> 255 || val
< -1)
567 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
571 case IPV6_MULTICAST_LOOP
:
572 if (optlen
< sizeof(int))
576 np
->mc_loop
= valbool
;
580 case IPV6_UNICAST_IF
:
582 struct net_device
*dev
= NULL
;
585 if (optlen
!= sizeof(int))
588 ifindex
= (__force
int)ntohl((__force __be32
)val
);
595 dev
= dev_get_by_index(net
, ifindex
);
596 retv
= -EADDRNOTAVAIL
;
602 if (sk
->sk_bound_dev_if
)
605 np
->ucast_oif
= ifindex
;
610 case IPV6_MULTICAST_IF
:
611 if (sk
->sk_type
== SOCK_STREAM
)
613 if (optlen
< sizeof(int))
617 struct net_device
*dev
;
622 dev
= dev_get_by_index_rcu(net
, val
);
628 midx
= l3mdev_master_ifindex_rcu(dev
);
632 if (sk
->sk_bound_dev_if
&&
633 sk
->sk_bound_dev_if
!= val
&&
634 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
640 case IPV6_ADD_MEMBERSHIP
:
641 case IPV6_DROP_MEMBERSHIP
:
643 struct ipv6_mreq mreq
;
645 if (optlen
< sizeof(struct ipv6_mreq
))
649 if (inet_sk(sk
)->is_icsk
)
653 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
656 if (optname
== IPV6_ADD_MEMBERSHIP
)
657 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
659 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
662 case IPV6_JOIN_ANYCAST
:
663 case IPV6_LEAVE_ANYCAST
:
665 struct ipv6_mreq mreq
;
667 if (optlen
< sizeof(struct ipv6_mreq
))
671 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
674 if (optname
== IPV6_JOIN_ANYCAST
)
675 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
677 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
680 case IPV6_MULTICAST_ALL
:
681 if (optlen
< sizeof(int))
683 np
->mc_all
= valbool
;
687 case MCAST_JOIN_GROUP
:
688 case MCAST_LEAVE_GROUP
:
690 struct group_req greq
;
691 struct sockaddr_in6
*psin6
;
693 if (optlen
< sizeof(struct group_req
))
697 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
699 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
700 retv
= -EADDRNOTAVAIL
;
703 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
704 if (optname
== MCAST_JOIN_GROUP
)
705 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
708 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
712 case MCAST_JOIN_SOURCE_GROUP
:
713 case MCAST_LEAVE_SOURCE_GROUP
:
714 case MCAST_BLOCK_SOURCE
:
715 case MCAST_UNBLOCK_SOURCE
:
717 struct group_source_req greqs
;
720 if (optlen
< sizeof(struct group_source_req
))
722 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
726 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
727 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
728 retv
= -EADDRNOTAVAIL
;
731 if (optname
== MCAST_BLOCK_SOURCE
) {
732 omode
= MCAST_EXCLUDE
;
734 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
735 omode
= MCAST_EXCLUDE
;
737 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
738 struct sockaddr_in6
*psin6
;
740 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
741 retv
= ipv6_sock_mc_join_ssm(sk
, greqs
.gsr_interface
,
744 /* prior join w/ different source is ok */
745 if (retv
&& retv
!= -EADDRINUSE
)
747 omode
= MCAST_INCLUDE
;
749 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
750 omode
= MCAST_INCLUDE
;
753 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
758 struct group_filter
*gsf
;
760 if (optlen
< GROUP_FILTER_SIZE(0))
762 if (optlen
> sysctl_optmem_max
) {
766 gsf
= memdup_user(optval
, optlen
);
771 /* numsrc >= (4G-140)/128 overflow in 32 bits */
772 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
773 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
778 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
783 retv
= ip6_mc_msfilter(sk
, gsf
);
788 case IPV6_ROUTER_ALERT
:
789 if (optlen
< sizeof(int))
791 retv
= ip6_ra_control(sk
, val
);
793 case IPV6_ROUTER_ALERT_ISOLATE
:
794 if (optlen
< sizeof(int))
796 np
->rtalert_isolate
= valbool
;
799 case IPV6_MTU_DISCOVER
:
800 if (optlen
< sizeof(int))
802 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
808 if (optlen
< sizeof(int))
810 if (val
&& val
< IPV6_MIN_MTU
)
816 if (optlen
< sizeof(int))
818 np
->recverr
= valbool
;
820 skb_queue_purge(&sk
->sk_error_queue
);
823 case IPV6_FLOWINFO_SEND
:
824 if (optlen
< sizeof(int))
826 np
->sndflow
= valbool
;
829 case IPV6_FLOWLABEL_MGR
:
830 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
832 case IPV6_IPSEC_POLICY
:
833 case IPV6_XFRM_POLICY
:
835 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
837 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
840 case IPV6_ADDR_PREFERENCES
:
842 unsigned int pref
= 0;
843 unsigned int prefmask
= ~0;
845 if (optlen
< sizeof(int))
850 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
851 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
853 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
854 case IPV6_PREFER_SRC_PUBLIC
:
855 pref
|= IPV6_PREFER_SRC_PUBLIC
;
857 case IPV6_PREFER_SRC_TMP
:
858 pref
|= IPV6_PREFER_SRC_TMP
;
860 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
863 goto pref_skip_pubtmp
;
868 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
869 IPV6_PREFER_SRC_TMP
);
872 /* check HOME/COA conflicts */
873 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
874 case IPV6_PREFER_SRC_HOME
:
876 case IPV6_PREFER_SRC_COA
:
877 pref
|= IPV6_PREFER_SRC_COA
;
884 prefmask
&= ~IPV6_PREFER_SRC_COA
;
887 /* check CGA/NONCGA conflicts */
888 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
889 case IPV6_PREFER_SRC_CGA
:
890 case IPV6_PREFER_SRC_NONCGA
:
897 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
902 case IPV6_MINHOPCOUNT
:
903 if (optlen
< sizeof(int))
905 if (val
< 0 || val
> 255)
907 np
->min_hopcount
= val
;
911 np
->dontfrag
= valbool
;
914 case IPV6_AUTOFLOWLABEL
:
915 np
->autoflowlabel
= valbool
;
916 np
->autoflowlabel_set
= 1;
919 case IPV6_RECVFRAGSIZE
:
920 np
->rxopt
.bits
.recvfragsize
= valbool
;
938 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
939 char __user
*optval
, unsigned int optlen
)
943 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
944 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
946 if (level
!= SOL_IPV6
)
949 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
950 #ifdef CONFIG_NETFILTER
951 /* we need to exclude all possible ENOPROTOOPTs except default case */
952 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
953 optname
!= IPV6_XFRM_POLICY
)
954 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
, optlen
);
958 EXPORT_SYMBOL(ipv6_setsockopt
);
961 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
962 char __user
*optval
, unsigned int optlen
)
966 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
967 if (udp_prot
.compat_setsockopt
!= NULL
)
968 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
970 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
973 if (level
!= SOL_IPV6
)
976 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
977 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
980 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
981 #ifdef CONFIG_NETFILTER
982 /* we need to exclude all possible ENOPROTOOPTs except default case */
983 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
984 optname
!= IPV6_XFRM_POLICY
)
985 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
990 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
993 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
994 int optname
, char __user
*optval
, int len
)
996 struct ipv6_opt_hdr
*hdr
;
1005 case IPV6_RTHDRDSTOPTS
:
1009 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
1015 return -EINVAL
; /* should not happen */
1021 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
1022 if (copy_to_user(optval
, hdr
, len
))
1027 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1028 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1030 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1034 if (ip6_mroute_opt(optname
))
1035 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1037 if (get_user(len
, optlen
))
1041 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
1042 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
1043 sk
->sk_protocol
!= IPPROTO_TCP
)
1044 return -ENOPROTOOPT
;
1045 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1047 val
= sk
->sk_family
;
1049 case MCAST_MSFILTER
:
1051 struct group_filter gsf
;
1054 if (len
< GROUP_FILTER_SIZE(0))
1056 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
1058 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1059 return -EADDRNOTAVAIL
;
1061 err
= ip6_mc_msfget(sk
, &gsf
,
1062 (struct group_filter __user
*)optval
, optlen
);
1067 case IPV6_2292PKTOPTIONS
:
1070 struct sk_buff
*skb
;
1072 if (sk
->sk_type
!= SOCK_STREAM
)
1073 return -ENOPROTOOPT
;
1075 msg
.msg_control
= optval
;
1076 msg
.msg_controllen
= len
;
1077 msg
.msg_flags
= flags
;
1080 skb
= np
->pktoptions
;
1082 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1085 if (np
->rxopt
.bits
.rxinfo
) {
1086 struct in6_pktinfo src_info
;
1087 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1088 np
->sticky_pktinfo
.ipi6_ifindex
;
1089 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1090 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1092 if (np
->rxopt
.bits
.rxhlim
) {
1093 int hlim
= np
->mcast_hops
;
1094 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1096 if (np
->rxopt
.bits
.rxtclass
) {
1097 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1099 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1101 if (np
->rxopt
.bits
.rxoinfo
) {
1102 struct in6_pktinfo src_info
;
1103 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1104 np
->sticky_pktinfo
.ipi6_ifindex
;
1105 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1106 np
->sticky_pktinfo
.ipi6_addr
;
1107 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1109 if (np
->rxopt
.bits
.rxohlim
) {
1110 int hlim
= np
->mcast_hops
;
1111 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1113 if (np
->rxopt
.bits
.rxflow
) {
1114 __be32 flowinfo
= np
->rcv_flowinfo
;
1116 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1119 len
-= msg
.msg_controllen
;
1120 return put_user(len
, optlen
);
1124 struct dst_entry
*dst
;
1128 dst
= __sk_dst_get(sk
);
1138 val
= sk
->sk_ipv6only
;
1141 case IPV6_RECVPKTINFO
:
1142 val
= np
->rxopt
.bits
.rxinfo
;
1145 case IPV6_2292PKTINFO
:
1146 val
= np
->rxopt
.bits
.rxoinfo
;
1149 case IPV6_RECVHOPLIMIT
:
1150 val
= np
->rxopt
.bits
.rxhlim
;
1153 case IPV6_2292HOPLIMIT
:
1154 val
= np
->rxopt
.bits
.rxohlim
;
1157 case IPV6_RECVRTHDR
:
1158 val
= np
->rxopt
.bits
.srcrt
;
1161 case IPV6_2292RTHDR
:
1162 val
= np
->rxopt
.bits
.osrcrt
;
1166 case IPV6_RTHDRDSTOPTS
:
1170 struct ipv6_txoptions
*opt
;
1173 opt
= rcu_dereference_protected(np
->opt
,
1174 lockdep_sock_is_held(sk
));
1175 len
= ipv6_getsockopt_sticky(sk
, opt
, optname
, optval
, len
);
1177 /* check if ipv6_getsockopt_sticky() returns err code */
1180 return put_user(len
, optlen
);
1183 case IPV6_RECVHOPOPTS
:
1184 val
= np
->rxopt
.bits
.hopopts
;
1187 case IPV6_2292HOPOPTS
:
1188 val
= np
->rxopt
.bits
.ohopopts
;
1191 case IPV6_RECVDSTOPTS
:
1192 val
= np
->rxopt
.bits
.dstopts
;
1195 case IPV6_2292DSTOPTS
:
1196 val
= np
->rxopt
.bits
.odstopts
;
1203 case IPV6_RECVTCLASS
:
1204 val
= np
->rxopt
.bits
.rxtclass
;
1208 val
= np
->rxopt
.bits
.rxflow
;
1211 case IPV6_RECVPATHMTU
:
1212 val
= np
->rxopt
.bits
.rxpmtu
;
1217 struct dst_entry
*dst
;
1218 struct ip6_mtuinfo mtuinfo
;
1220 if (len
< sizeof(mtuinfo
))
1223 len
= sizeof(mtuinfo
);
1224 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1227 dst
= __sk_dst_get(sk
);
1229 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1231 if (!mtuinfo
.ip6m_mtu
)
1234 if (put_user(len
, optlen
))
1236 if (copy_to_user(optval
, &mtuinfo
, len
))
1242 case IPV6_TRANSPARENT
:
1243 val
= inet_sk(sk
)->transparent
;
1247 val
= inet_sk(sk
)->freebind
;
1250 case IPV6_RECVORIGDSTADDR
:
1251 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1254 case IPV6_UNICAST_HOPS
:
1255 case IPV6_MULTICAST_HOPS
:
1257 struct dst_entry
*dst
;
1259 if (optname
== IPV6_UNICAST_HOPS
)
1260 val
= np
->hop_limit
;
1262 val
= np
->mcast_hops
;
1266 dst
= __sk_dst_get(sk
);
1268 val
= ip6_dst_hoplimit(dst
);
1273 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1277 case IPV6_MULTICAST_LOOP
:
1281 case IPV6_MULTICAST_IF
:
1282 val
= np
->mcast_oif
;
1285 case IPV6_MULTICAST_ALL
:
1289 case IPV6_UNICAST_IF
:
1290 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1293 case IPV6_MTU_DISCOVER
:
1301 case IPV6_FLOWINFO_SEND
:
1305 case IPV6_FLOWLABEL_MGR
:
1307 struct in6_flowlabel_req freq
;
1310 if (len
< sizeof(freq
))
1313 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1316 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1320 flags
= freq
.flr_flags
;
1322 memset(&freq
, 0, sizeof(freq
));
1324 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1328 if (put_user(len
, optlen
))
1330 if (copy_to_user(optval
, &freq
, len
))
1336 case IPV6_ADDR_PREFERENCES
:
1339 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1340 val
|= IPV6_PREFER_SRC_TMP
;
1341 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1342 val
|= IPV6_PREFER_SRC_PUBLIC
;
1344 /* XXX: should we return system default? */
1345 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1348 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1349 val
|= IPV6_PREFER_SRC_COA
;
1351 val
|= IPV6_PREFER_SRC_HOME
;
1354 case IPV6_MINHOPCOUNT
:
1355 val
= np
->min_hopcount
;
1362 case IPV6_AUTOFLOWLABEL
:
1363 val
= ip6_autoflowlabel(sock_net(sk
), np
);
1366 case IPV6_RECVFRAGSIZE
:
1367 val
= np
->rxopt
.bits
.recvfragsize
;
1370 case IPV6_ROUTER_ALERT_ISOLATE
:
1371 val
= np
->rtalert_isolate
;
1375 return -ENOPROTOOPT
;
1377 len
= min_t(unsigned int, sizeof(int), len
);
1378 if (put_user(len
, optlen
))
1380 if (copy_to_user(optval
, &val
, len
))
1385 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1386 char __user
*optval
, int __user
*optlen
)
1390 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1391 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1393 if (level
!= SOL_IPV6
)
1394 return -ENOPROTOOPT
;
1396 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1397 #ifdef CONFIG_NETFILTER
1398 /* we need to exclude all possible ENOPROTOOPTs except default case */
1399 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1402 if (get_user(len
, optlen
))
1405 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1407 err
= put_user(len
, optlen
);
1412 EXPORT_SYMBOL(ipv6_getsockopt
);
1414 #ifdef CONFIG_COMPAT
1415 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1416 char __user
*optval
, int __user
*optlen
)
1420 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1421 if (udp_prot
.compat_getsockopt
!= NULL
)
1422 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1424 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1427 if (level
!= SOL_IPV6
)
1428 return -ENOPROTOOPT
;
1430 if (optname
== MCAST_MSFILTER
)
1431 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1434 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1436 #ifdef CONFIG_NETFILTER
1437 /* we need to exclude all possible ENOPROTOOPTs except default case */
1438 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1441 if (get_user(len
, optlen
))
1444 err
= compat_nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1446 err
= put_user(len
, optlen
);
1451 EXPORT_SYMBOL(compat_ipv6_getsockopt
);