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
) {
186 } else if (sk
->sk_protocol
!= IPPROTO_TCP
)
189 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
194 if (ipv6_only_sock(sk
) ||
195 !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
196 retv
= -EADDRNOTAVAIL
;
200 fl6_free_socklist(sk
);
201 __ipv6_sock_mc_close(sk
);
204 * Sock is moving from IPv6 to IPv4 (sk_prot), so
205 * remove it from the refcnt debug socks count in the
208 sk_refcnt_debug_dec(sk
);
210 if (sk
->sk_protocol
== IPPROTO_TCP
) {
211 struct inet_connection_sock
*icsk
= inet_csk(sk
);
213 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
214 sock_prot_inuse_add(net
, &tcp_prot
, 1);
216 sk
->sk_prot
= &tcp_prot
;
217 icsk
->icsk_af_ops
= &ipv4_specific
;
218 sk
->sk_socket
->ops
= &inet_stream_ops
;
219 sk
->sk_family
= PF_INET
;
220 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
222 struct proto
*prot
= &udp_prot
;
224 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
225 prot
= &udplite_prot
;
227 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
228 sock_prot_inuse_add(net
, prot
, 1);
231 sk
->sk_socket
->ops
= &inet_dgram_ops
;
232 sk
->sk_family
= PF_INET
;
234 opt
= xchg((__force
struct ipv6_txoptions
**)&np
->opt
,
237 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
240 pktopt
= xchg(&np
->pktoptions
, NULL
);
244 * ... and add it to the refcnt debug socks count
245 * in the new family. -acme
247 sk_refcnt_debug_inc(sk
);
248 module_put(THIS_MODULE
);
255 if (optlen
< sizeof(int) ||
256 inet_sk(sk
)->inet_num
)
258 sk
->sk_ipv6only
= valbool
;
262 case IPV6_RECVPKTINFO
:
263 if (optlen
< sizeof(int))
265 np
->rxopt
.bits
.rxinfo
= valbool
;
269 case IPV6_2292PKTINFO
:
270 if (optlen
< sizeof(int))
272 np
->rxopt
.bits
.rxoinfo
= valbool
;
276 case IPV6_RECVHOPLIMIT
:
277 if (optlen
< sizeof(int))
279 np
->rxopt
.bits
.rxhlim
= valbool
;
283 case IPV6_2292HOPLIMIT
:
284 if (optlen
< sizeof(int))
286 np
->rxopt
.bits
.rxohlim
= valbool
;
291 if (optlen
< sizeof(int))
293 np
->rxopt
.bits
.srcrt
= valbool
;
298 if (optlen
< sizeof(int))
300 np
->rxopt
.bits
.osrcrt
= valbool
;
304 case IPV6_RECVHOPOPTS
:
305 if (optlen
< sizeof(int))
307 np
->rxopt
.bits
.hopopts
= valbool
;
311 case IPV6_2292HOPOPTS
:
312 if (optlen
< sizeof(int))
314 np
->rxopt
.bits
.ohopopts
= valbool
;
318 case IPV6_RECVDSTOPTS
:
319 if (optlen
< sizeof(int))
321 np
->rxopt
.bits
.dstopts
= valbool
;
325 case IPV6_2292DSTOPTS
:
326 if (optlen
< sizeof(int))
328 np
->rxopt
.bits
.odstopts
= valbool
;
333 if (optlen
< sizeof(int))
335 if (val
< -1 || val
> 0xff)
337 /* RFC 3542, 6.5: default traffic class of 0x0 */
344 case IPV6_RECVTCLASS
:
345 if (optlen
< sizeof(int))
347 np
->rxopt
.bits
.rxtclass
= valbool
;
352 if (optlen
< sizeof(int))
354 np
->rxopt
.bits
.rxflow
= valbool
;
358 case IPV6_RECVPATHMTU
:
359 if (optlen
< sizeof(int))
361 np
->rxopt
.bits
.rxpmtu
= valbool
;
365 case IPV6_TRANSPARENT
:
366 if (valbool
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
) &&
367 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
)) {
371 if (optlen
< sizeof(int))
373 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
374 inet_sk(sk
)->transparent
= valbool
;
379 if (optlen
< sizeof(int))
381 /* we also don't have a separate freebind bit for IPV6 */
382 inet_sk(sk
)->freebind
= valbool
;
386 case IPV6_RECVORIGDSTADDR
:
387 if (optlen
< sizeof(int))
389 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
394 case IPV6_RTHDRDSTOPTS
:
398 struct ipv6_txoptions
*opt
;
399 struct ipv6_opt_hdr
*new = NULL
;
401 /* hop-by-hop / destination options are privileged option */
403 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
406 /* remove any sticky options header with a zero option
407 * length, per RFC3542.
413 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
414 optlen
& 0x7 || optlen
> 8 * 255)
417 new = memdup_user(optval
, optlen
);
422 if (unlikely(ipv6_optlen(new) > optlen
)) {
428 opt
= rcu_dereference_protected(np
->opt
,
429 lockdep_sock_is_held(sk
));
430 opt
= ipv6_renew_options(sk
, opt
, optname
, new);
437 /* routing header option needs extra check */
439 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
440 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
441 switch (rthdr
->type
) {
442 #if IS_ENABLED(CONFIG_IPV6_MIP6)
443 case IPV6_SRCRT_TYPE_2
:
444 if (rthdr
->hdrlen
!= 2 ||
445 rthdr
->segments_left
!= 1)
450 case IPV6_SRCRT_TYPE_4
:
452 struct ipv6_sr_hdr
*srh
= (struct ipv6_sr_hdr
*)
455 if (!seg6_validate_srh(srh
, optlen
))
465 opt
= ipv6_update_options(sk
, opt
);
468 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
476 struct in6_pktinfo pkt
;
480 else if (optlen
< sizeof(struct in6_pktinfo
) || !optval
)
483 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
487 if (!sk_dev_equal_l3scope(sk
, pkt
.ipi6_ifindex
))
490 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
491 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
496 case IPV6_2292PKTOPTIONS
:
498 struct ipv6_txoptions
*opt
= NULL
;
501 struct ipcm6_cookie ipc6
;
503 memset(&fl6
, 0, sizeof(fl6
));
504 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
505 fl6
.flowi6_mark
= sk
->sk_mark
;
510 /* 1K is probably excessive
511 * 1K is surely not enough, 2K per standard header is 16K.
514 if (optlen
> 64*1024)
517 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
522 memset(opt
, 0, sizeof(*opt
));
523 refcount_set(&opt
->refcnt
, 1);
524 opt
->tot_len
= sizeof(*opt
) + optlen
;
526 if (copy_from_user(opt
+1, optval
, optlen
))
529 msg
.msg_controllen
= optlen
;
530 msg
.msg_control
= (void *)(opt
+1);
533 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, &ipc6
);
538 opt
= ipv6_update_options(sk
, opt
);
541 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
546 case IPV6_UNICAST_HOPS
:
547 if (optlen
< sizeof(int))
549 if (val
> 255 || val
< -1)
555 case IPV6_MULTICAST_HOPS
:
556 if (sk
->sk_type
== SOCK_STREAM
)
558 if (optlen
< sizeof(int))
560 if (val
> 255 || val
< -1)
562 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
566 case IPV6_MULTICAST_LOOP
:
567 if (optlen
< sizeof(int))
571 np
->mc_loop
= valbool
;
575 case IPV6_UNICAST_IF
:
577 struct net_device
*dev
= NULL
;
580 if (optlen
!= sizeof(int))
583 ifindex
= (__force
int)ntohl((__force __be32
)val
);
590 dev
= dev_get_by_index(net
, ifindex
);
591 retv
= -EADDRNOTAVAIL
;
597 if (sk
->sk_bound_dev_if
)
600 np
->ucast_oif
= ifindex
;
605 case IPV6_MULTICAST_IF
:
606 if (sk
->sk_type
== SOCK_STREAM
)
608 if (optlen
< sizeof(int))
612 struct net_device
*dev
;
617 dev
= dev_get_by_index_rcu(net
, val
);
623 midx
= l3mdev_master_ifindex_rcu(dev
);
627 if (sk
->sk_bound_dev_if
&&
628 sk
->sk_bound_dev_if
!= val
&&
629 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
635 case IPV6_ADD_MEMBERSHIP
:
636 case IPV6_DROP_MEMBERSHIP
:
638 struct ipv6_mreq mreq
;
640 if (optlen
< sizeof(struct ipv6_mreq
))
644 if (inet_sk(sk
)->is_icsk
)
648 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
651 if (optname
== IPV6_ADD_MEMBERSHIP
)
652 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
654 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
657 case IPV6_JOIN_ANYCAST
:
658 case IPV6_LEAVE_ANYCAST
:
660 struct ipv6_mreq mreq
;
662 if (optlen
< sizeof(struct ipv6_mreq
))
666 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
669 if (optname
== IPV6_JOIN_ANYCAST
)
670 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
672 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
675 case IPV6_MULTICAST_ALL
:
676 if (optlen
< sizeof(int))
678 np
->mc_all
= valbool
;
682 case MCAST_JOIN_GROUP
:
683 case MCAST_LEAVE_GROUP
:
685 struct group_req greq
;
686 struct sockaddr_in6
*psin6
;
688 if (optlen
< sizeof(struct group_req
))
692 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
694 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
695 retv
= -EADDRNOTAVAIL
;
698 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
699 if (optname
== MCAST_JOIN_GROUP
)
700 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
703 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
707 case MCAST_JOIN_SOURCE_GROUP
:
708 case MCAST_LEAVE_SOURCE_GROUP
:
709 case MCAST_BLOCK_SOURCE
:
710 case MCAST_UNBLOCK_SOURCE
:
712 struct group_source_req greqs
;
715 if (optlen
< sizeof(struct group_source_req
))
717 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
721 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
722 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
723 retv
= -EADDRNOTAVAIL
;
726 if (optname
== MCAST_BLOCK_SOURCE
) {
727 omode
= MCAST_EXCLUDE
;
729 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
730 omode
= MCAST_EXCLUDE
;
732 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
733 struct sockaddr_in6
*psin6
;
735 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
736 retv
= ipv6_sock_mc_join_ssm(sk
, greqs
.gsr_interface
,
739 /* prior join w/ different source is ok */
740 if (retv
&& retv
!= -EADDRINUSE
)
742 omode
= MCAST_INCLUDE
;
744 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
745 omode
= MCAST_INCLUDE
;
748 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
753 struct group_filter
*gsf
;
755 if (optlen
< GROUP_FILTER_SIZE(0))
757 if (optlen
> sysctl_optmem_max
) {
761 gsf
= memdup_user(optval
, optlen
);
766 /* numsrc >= (4G-140)/128 overflow in 32 bits */
767 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
768 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
773 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
778 retv
= ip6_mc_msfilter(sk
, gsf
);
783 case IPV6_ROUTER_ALERT
:
784 if (optlen
< sizeof(int))
786 retv
= ip6_ra_control(sk
, val
);
788 case IPV6_ROUTER_ALERT_ISOLATE
:
789 if (optlen
< sizeof(int))
791 np
->rtalert_isolate
= valbool
;
794 case IPV6_MTU_DISCOVER
:
795 if (optlen
< sizeof(int))
797 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
803 if (optlen
< sizeof(int))
805 if (val
&& val
< IPV6_MIN_MTU
)
811 if (optlen
< sizeof(int))
813 np
->recverr
= valbool
;
815 skb_queue_purge(&sk
->sk_error_queue
);
818 case IPV6_FLOWINFO_SEND
:
819 if (optlen
< sizeof(int))
821 np
->sndflow
= valbool
;
824 case IPV6_FLOWLABEL_MGR
:
825 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
827 case IPV6_IPSEC_POLICY
:
828 case IPV6_XFRM_POLICY
:
830 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
832 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
835 case IPV6_ADDR_PREFERENCES
:
837 unsigned int pref
= 0;
838 unsigned int prefmask
= ~0;
840 if (optlen
< sizeof(int))
845 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
846 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
848 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
849 case IPV6_PREFER_SRC_PUBLIC
:
850 pref
|= IPV6_PREFER_SRC_PUBLIC
;
852 case IPV6_PREFER_SRC_TMP
:
853 pref
|= IPV6_PREFER_SRC_TMP
;
855 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
858 goto pref_skip_pubtmp
;
863 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
864 IPV6_PREFER_SRC_TMP
);
867 /* check HOME/COA conflicts */
868 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
869 case IPV6_PREFER_SRC_HOME
:
871 case IPV6_PREFER_SRC_COA
:
872 pref
|= IPV6_PREFER_SRC_COA
;
879 prefmask
&= ~IPV6_PREFER_SRC_COA
;
882 /* check CGA/NONCGA conflicts */
883 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
884 case IPV6_PREFER_SRC_CGA
:
885 case IPV6_PREFER_SRC_NONCGA
:
892 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
897 case IPV6_MINHOPCOUNT
:
898 if (optlen
< sizeof(int))
900 if (val
< 0 || val
> 255)
902 np
->min_hopcount
= val
;
906 np
->dontfrag
= valbool
;
909 case IPV6_AUTOFLOWLABEL
:
910 np
->autoflowlabel
= valbool
;
911 np
->autoflowlabel_set
= 1;
914 case IPV6_RECVFRAGSIZE
:
915 np
->rxopt
.bits
.recvfragsize
= valbool
;
933 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
934 char __user
*optval
, unsigned int optlen
)
938 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
939 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
941 if (level
!= SOL_IPV6
)
944 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
945 #ifdef CONFIG_NETFILTER
946 /* we need to exclude all possible ENOPROTOOPTs except default case */
947 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
948 optname
!= IPV6_XFRM_POLICY
)
949 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
, optlen
);
953 EXPORT_SYMBOL(ipv6_setsockopt
);
956 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
957 char __user
*optval
, unsigned int optlen
)
961 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
962 if (udp_prot
.compat_setsockopt
!= NULL
)
963 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
965 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
968 if (level
!= SOL_IPV6
)
971 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
972 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
975 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
976 #ifdef CONFIG_NETFILTER
977 /* we need to exclude all possible ENOPROTOOPTs except default case */
978 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
979 optname
!= IPV6_XFRM_POLICY
)
980 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
985 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
988 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
989 int optname
, char __user
*optval
, int len
)
991 struct ipv6_opt_hdr
*hdr
;
1000 case IPV6_RTHDRDSTOPTS
:
1004 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
1010 return -EINVAL
; /* should not happen */
1016 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
1017 if (copy_to_user(optval
, hdr
, len
))
1022 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1023 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1025 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1029 if (ip6_mroute_opt(optname
))
1030 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1032 if (get_user(len
, optlen
))
1036 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
1037 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
1038 sk
->sk_protocol
!= IPPROTO_TCP
)
1039 return -ENOPROTOOPT
;
1040 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1042 val
= sk
->sk_family
;
1044 case MCAST_MSFILTER
:
1046 struct group_filter gsf
;
1049 if (len
< GROUP_FILTER_SIZE(0))
1051 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
1053 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1054 return -EADDRNOTAVAIL
;
1056 err
= ip6_mc_msfget(sk
, &gsf
,
1057 (struct group_filter __user
*)optval
, optlen
);
1062 case IPV6_2292PKTOPTIONS
:
1065 struct sk_buff
*skb
;
1067 if (sk
->sk_type
!= SOCK_STREAM
)
1068 return -ENOPROTOOPT
;
1070 msg
.msg_control
= optval
;
1071 msg
.msg_controllen
= len
;
1072 msg
.msg_flags
= flags
;
1075 skb
= np
->pktoptions
;
1077 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1080 if (np
->rxopt
.bits
.rxinfo
) {
1081 struct in6_pktinfo src_info
;
1082 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1083 np
->sticky_pktinfo
.ipi6_ifindex
;
1084 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1085 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1087 if (np
->rxopt
.bits
.rxhlim
) {
1088 int hlim
= np
->mcast_hops
;
1089 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1091 if (np
->rxopt
.bits
.rxtclass
) {
1092 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1094 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1096 if (np
->rxopt
.bits
.rxoinfo
) {
1097 struct in6_pktinfo src_info
;
1098 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1099 np
->sticky_pktinfo
.ipi6_ifindex
;
1100 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1101 np
->sticky_pktinfo
.ipi6_addr
;
1102 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1104 if (np
->rxopt
.bits
.rxohlim
) {
1105 int hlim
= np
->mcast_hops
;
1106 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1108 if (np
->rxopt
.bits
.rxflow
) {
1109 __be32 flowinfo
= np
->rcv_flowinfo
;
1111 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1114 len
-= msg
.msg_controllen
;
1115 return put_user(len
, optlen
);
1119 struct dst_entry
*dst
;
1123 dst
= __sk_dst_get(sk
);
1133 val
= sk
->sk_ipv6only
;
1136 case IPV6_RECVPKTINFO
:
1137 val
= np
->rxopt
.bits
.rxinfo
;
1140 case IPV6_2292PKTINFO
:
1141 val
= np
->rxopt
.bits
.rxoinfo
;
1144 case IPV6_RECVHOPLIMIT
:
1145 val
= np
->rxopt
.bits
.rxhlim
;
1148 case IPV6_2292HOPLIMIT
:
1149 val
= np
->rxopt
.bits
.rxohlim
;
1152 case IPV6_RECVRTHDR
:
1153 val
= np
->rxopt
.bits
.srcrt
;
1156 case IPV6_2292RTHDR
:
1157 val
= np
->rxopt
.bits
.osrcrt
;
1161 case IPV6_RTHDRDSTOPTS
:
1165 struct ipv6_txoptions
*opt
;
1168 opt
= rcu_dereference_protected(np
->opt
,
1169 lockdep_sock_is_held(sk
));
1170 len
= ipv6_getsockopt_sticky(sk
, opt
, optname
, optval
, len
);
1172 /* check if ipv6_getsockopt_sticky() returns err code */
1175 return put_user(len
, optlen
);
1178 case IPV6_RECVHOPOPTS
:
1179 val
= np
->rxopt
.bits
.hopopts
;
1182 case IPV6_2292HOPOPTS
:
1183 val
= np
->rxopt
.bits
.ohopopts
;
1186 case IPV6_RECVDSTOPTS
:
1187 val
= np
->rxopt
.bits
.dstopts
;
1190 case IPV6_2292DSTOPTS
:
1191 val
= np
->rxopt
.bits
.odstopts
;
1198 case IPV6_RECVTCLASS
:
1199 val
= np
->rxopt
.bits
.rxtclass
;
1203 val
= np
->rxopt
.bits
.rxflow
;
1206 case IPV6_RECVPATHMTU
:
1207 val
= np
->rxopt
.bits
.rxpmtu
;
1212 struct dst_entry
*dst
;
1213 struct ip6_mtuinfo mtuinfo
;
1215 if (len
< sizeof(mtuinfo
))
1218 len
= sizeof(mtuinfo
);
1219 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1222 dst
= __sk_dst_get(sk
);
1224 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1226 if (!mtuinfo
.ip6m_mtu
)
1229 if (put_user(len
, optlen
))
1231 if (copy_to_user(optval
, &mtuinfo
, len
))
1237 case IPV6_TRANSPARENT
:
1238 val
= inet_sk(sk
)->transparent
;
1242 val
= inet_sk(sk
)->freebind
;
1245 case IPV6_RECVORIGDSTADDR
:
1246 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1249 case IPV6_UNICAST_HOPS
:
1250 case IPV6_MULTICAST_HOPS
:
1252 struct dst_entry
*dst
;
1254 if (optname
== IPV6_UNICAST_HOPS
)
1255 val
= np
->hop_limit
;
1257 val
= np
->mcast_hops
;
1261 dst
= __sk_dst_get(sk
);
1263 val
= ip6_dst_hoplimit(dst
);
1268 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1272 case IPV6_MULTICAST_LOOP
:
1276 case IPV6_MULTICAST_IF
:
1277 val
= np
->mcast_oif
;
1280 case IPV6_MULTICAST_ALL
:
1284 case IPV6_UNICAST_IF
:
1285 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1288 case IPV6_MTU_DISCOVER
:
1296 case IPV6_FLOWINFO_SEND
:
1300 case IPV6_FLOWLABEL_MGR
:
1302 struct in6_flowlabel_req freq
;
1305 if (len
< sizeof(freq
))
1308 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1311 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1315 flags
= freq
.flr_flags
;
1317 memset(&freq
, 0, sizeof(freq
));
1319 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1323 if (put_user(len
, optlen
))
1325 if (copy_to_user(optval
, &freq
, len
))
1331 case IPV6_ADDR_PREFERENCES
:
1334 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1335 val
|= IPV6_PREFER_SRC_TMP
;
1336 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1337 val
|= IPV6_PREFER_SRC_PUBLIC
;
1339 /* XXX: should we return system default? */
1340 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1343 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1344 val
|= IPV6_PREFER_SRC_COA
;
1346 val
|= IPV6_PREFER_SRC_HOME
;
1349 case IPV6_MINHOPCOUNT
:
1350 val
= np
->min_hopcount
;
1357 case IPV6_AUTOFLOWLABEL
:
1358 val
= ip6_autoflowlabel(sock_net(sk
), np
);
1361 case IPV6_RECVFRAGSIZE
:
1362 val
= np
->rxopt
.bits
.recvfragsize
;
1365 case IPV6_ROUTER_ALERT_ISOLATE
:
1366 val
= np
->rtalert_isolate
;
1370 return -ENOPROTOOPT
;
1372 len
= min_t(unsigned int, sizeof(int), len
);
1373 if (put_user(len
, optlen
))
1375 if (copy_to_user(optval
, &val
, len
))
1380 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1381 char __user
*optval
, int __user
*optlen
)
1385 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1386 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1388 if (level
!= SOL_IPV6
)
1389 return -ENOPROTOOPT
;
1391 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1392 #ifdef CONFIG_NETFILTER
1393 /* we need to exclude all possible ENOPROTOOPTs except default case */
1394 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1397 if (get_user(len
, optlen
))
1400 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1402 err
= put_user(len
, optlen
);
1407 EXPORT_SYMBOL(ipv6_getsockopt
);
1409 #ifdef CONFIG_COMPAT
1410 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1411 char __user
*optval
, int __user
*optlen
)
1415 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1416 if (udp_prot
.compat_getsockopt
!= NULL
)
1417 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1419 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1422 if (level
!= SOL_IPV6
)
1423 return -ENOPROTOOPT
;
1425 if (optname
== MCAST_MSFILTER
)
1426 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1429 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1431 #ifdef CONFIG_NETFILTER
1432 /* we need to exclude all possible ENOPROTOOPTs except default case */
1433 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1436 if (get_user(len
, optlen
))
1439 err
= compat_nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1441 err
= put_user(len
, optlen
);
1446 EXPORT_SYMBOL(compat_ipv6_getsockopt
);