2 * IPv6 BSD socket options interface
3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/net/ipv4/ip_sockglue.c
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * FIXME: Make the setsockopt code POSIX compliant: That is
17 * o Truncate getsockopt returns
18 * o Return an optlen of the truncated length if need be
21 * David L Stevens <dlstevens@us.ibm.com>:
22 * - added multicast source filtering API for MLDv2
25 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/errno.h>
28 #include <linux/types.h>
29 #include <linux/socket.h>
30 #include <linux/sockios.h>
31 #include <linux/net.h>
32 #include <linux/in6.h>
33 #include <linux/mroute6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/init.h>
37 #include <linux/sysctl.h>
38 #include <linux/netfilter.h>
39 #include <linux/slab.h>
44 #include <net/ndisc.h>
45 #include <net/protocol.h>
46 #include <net/transp_v6.h>
47 #include <net/ip6_route.h>
48 #include <net/addrconf.h>
49 #include <net/inet_common.h>
52 #include <net/udplite.h>
54 #include <net/compat.h>
57 #include <linux/uaccess.h>
59 struct ip6_ra_chain
*ip6_ra_chain
;
60 DEFINE_RWLOCK(ip6_ra_lock
);
62 int ip6_ra_control(struct sock
*sk
, int sel
)
64 struct ip6_ra_chain
*ra
, *new_ra
, **rap
;
66 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
67 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
!= IPPROTO_RAW
)
70 new_ra
= (sel
>= 0) ? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
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 do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
142 char __user
*optval
, unsigned int optlen
)
144 struct ipv6_pinfo
*np
= inet6_sk(sk
);
145 struct net
*net
= sock_net(sk
);
147 int retv
= -ENOPROTOOPT
;
148 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
153 if (optlen
>= sizeof(int)) {
154 if (get_user(val
, (int __user
*) optval
))
160 valbool
= (val
!= 0);
162 if (ip6_mroute_opt(optname
))
163 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
172 if (optlen
< sizeof(int))
174 if (val
== PF_INET
) {
175 struct ipv6_txoptions
*opt
;
176 struct sk_buff
*pktopt
;
178 if (sk
->sk_type
== SOCK_RAW
)
181 if (sk
->sk_protocol
== IPPROTO_UDP
||
182 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
183 struct udp_sock
*up
= udp_sk(sk
);
184 if (up
->pending
== AF_INET6
) {
189 if (sk
->sk_protocol
== IPPROTO_TCP
&&
190 sk
->sk_prot
!= &tcpv6_prot
) {
194 if (sk
->sk_protocol
!= IPPROTO_TCP
)
196 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
201 if (ipv6_only_sock(sk
) ||
202 !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
203 retv
= -EADDRNOTAVAIL
;
207 fl6_free_socklist(sk
);
208 __ipv6_sock_mc_close(sk
);
211 * Sock is moving from IPv6 to IPv4 (sk_prot), so
212 * remove it from the refcnt debug socks count in the
215 sk_refcnt_debug_dec(sk
);
217 if (sk
->sk_protocol
== IPPROTO_TCP
) {
218 struct inet_connection_sock
*icsk
= inet_csk(sk
);
220 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
221 sock_prot_inuse_add(net
, &tcp_prot
, 1);
223 sk
->sk_prot
= &tcp_prot
;
224 icsk
->icsk_af_ops
= &ipv4_specific
;
225 sk
->sk_socket
->ops
= &inet_stream_ops
;
226 sk
->sk_family
= PF_INET
;
227 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
229 struct proto
*prot
= &udp_prot
;
231 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
232 prot
= &udplite_prot
;
234 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
235 sock_prot_inuse_add(net
, prot
, 1);
238 sk
->sk_socket
->ops
= &inet_dgram_ops
;
239 sk
->sk_family
= PF_INET
;
241 opt
= xchg((__force
struct ipv6_txoptions
**)&np
->opt
,
244 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
247 pktopt
= xchg(&np
->pktoptions
, NULL
);
251 * ... and add it to the refcnt debug socks count
252 * in the new family. -acme
254 sk_refcnt_debug_inc(sk
);
255 module_put(THIS_MODULE
);
262 if (optlen
< sizeof(int) ||
263 inet_sk(sk
)->inet_num
)
265 sk
->sk_ipv6only
= valbool
;
269 case IPV6_RECVPKTINFO
:
270 if (optlen
< sizeof(int))
272 np
->rxopt
.bits
.rxinfo
= valbool
;
276 case IPV6_2292PKTINFO
:
277 if (optlen
< sizeof(int))
279 np
->rxopt
.bits
.rxoinfo
= valbool
;
283 case IPV6_RECVHOPLIMIT
:
284 if (optlen
< sizeof(int))
286 np
->rxopt
.bits
.rxhlim
= valbool
;
290 case IPV6_2292HOPLIMIT
:
291 if (optlen
< sizeof(int))
293 np
->rxopt
.bits
.rxohlim
= valbool
;
298 if (optlen
< sizeof(int))
300 np
->rxopt
.bits
.srcrt
= valbool
;
305 if (optlen
< sizeof(int))
307 np
->rxopt
.bits
.osrcrt
= valbool
;
311 case IPV6_RECVHOPOPTS
:
312 if (optlen
< sizeof(int))
314 np
->rxopt
.bits
.hopopts
= valbool
;
318 case IPV6_2292HOPOPTS
:
319 if (optlen
< sizeof(int))
321 np
->rxopt
.bits
.ohopopts
= valbool
;
325 case IPV6_RECVDSTOPTS
:
326 if (optlen
< sizeof(int))
328 np
->rxopt
.bits
.dstopts
= valbool
;
332 case IPV6_2292DSTOPTS
:
333 if (optlen
< sizeof(int))
335 np
->rxopt
.bits
.odstopts
= valbool
;
340 if (optlen
< sizeof(int))
342 if (val
< -1 || val
> 0xff)
344 /* RFC 3542, 6.5: default traffic class of 0x0 */
351 case IPV6_RECVTCLASS
:
352 if (optlen
< sizeof(int))
354 np
->rxopt
.bits
.rxtclass
= valbool
;
359 if (optlen
< sizeof(int))
361 np
->rxopt
.bits
.rxflow
= valbool
;
365 case IPV6_RECVPATHMTU
:
366 if (optlen
< sizeof(int))
368 np
->rxopt
.bits
.rxpmtu
= valbool
;
372 case IPV6_TRANSPARENT
:
373 if (valbool
&& !ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
374 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
378 if (optlen
< sizeof(int))
380 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
381 inet_sk(sk
)->transparent
= valbool
;
386 if (optlen
< sizeof(int))
388 /* we also don't have a separate freebind bit for IPV6 */
389 inet_sk(sk
)->freebind
= valbool
;
393 case IPV6_RECVORIGDSTADDR
:
394 if (optlen
< sizeof(int))
396 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
401 case IPV6_RTHDRDSTOPTS
:
405 struct ipv6_txoptions
*opt
;
406 struct ipv6_opt_hdr
*new = NULL
;
408 /* hop-by-hop / destination options are privileged option */
410 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
413 /* remove any sticky options header with a zero option
414 * length, per RFC3542.
420 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
421 optlen
& 0x7 || optlen
> 8 * 255)
424 new = memdup_user(optval
, optlen
);
429 if (unlikely(ipv6_optlen(new) > optlen
)) {
435 opt
= rcu_dereference_protected(np
->opt
,
436 lockdep_sock_is_held(sk
));
437 opt
= ipv6_renew_options(sk
, opt
, optname
, new);
444 /* routing header option needs extra check */
446 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
447 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
448 switch (rthdr
->type
) {
449 #if IS_ENABLED(CONFIG_IPV6_MIP6)
450 case IPV6_SRCRT_TYPE_2
:
451 if (rthdr
->hdrlen
!= 2 ||
452 rthdr
->segments_left
!= 1)
457 case IPV6_SRCRT_TYPE_4
:
459 struct ipv6_sr_hdr
*srh
= (struct ipv6_sr_hdr
*)
462 if (!seg6_validate_srh(srh
, optlen
))
472 opt
= ipv6_update_options(sk
, opt
);
475 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
483 struct in6_pktinfo pkt
;
487 else if (optlen
< sizeof(struct in6_pktinfo
) || !optval
)
490 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
494 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
497 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
498 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
503 case IPV6_2292PKTOPTIONS
:
505 struct ipv6_txoptions
*opt
= NULL
;
508 struct ipcm6_cookie ipc6
;
510 memset(&fl6
, 0, sizeof(fl6
));
511 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
512 fl6
.flowi6_mark
= sk
->sk_mark
;
517 /* 1K is probably excessive
518 * 1K is surely not enough, 2K per standard header is 16K.
521 if (optlen
> 64*1024)
524 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
529 memset(opt
, 0, sizeof(*opt
));
530 refcount_set(&opt
->refcnt
, 1);
531 opt
->tot_len
= sizeof(*opt
) + optlen
;
533 if (copy_from_user(opt
+1, optval
, optlen
))
536 msg
.msg_controllen
= optlen
;
537 msg
.msg_control
= (void *)(opt
+1);
540 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, &ipc6
);
545 opt
= ipv6_update_options(sk
, opt
);
548 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
553 case IPV6_UNICAST_HOPS
:
554 if (optlen
< sizeof(int))
556 if (val
> 255 || val
< -1)
562 case IPV6_MULTICAST_HOPS
:
563 if (sk
->sk_type
== SOCK_STREAM
)
565 if (optlen
< sizeof(int))
567 if (val
> 255 || val
< -1)
569 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
573 case IPV6_MULTICAST_LOOP
:
574 if (optlen
< sizeof(int))
578 np
->mc_loop
= valbool
;
582 case IPV6_UNICAST_IF
:
584 struct net_device
*dev
= NULL
;
587 if (optlen
!= sizeof(int))
590 ifindex
= (__force
int)ntohl((__force __be32
)val
);
597 dev
= dev_get_by_index(net
, ifindex
);
598 retv
= -EADDRNOTAVAIL
;
604 if (sk
->sk_bound_dev_if
)
607 np
->ucast_oif
= ifindex
;
612 case IPV6_MULTICAST_IF
:
613 if (sk
->sk_type
== SOCK_STREAM
)
615 if (optlen
< sizeof(int))
619 struct net_device
*dev
;
624 dev
= dev_get_by_index_rcu(net
, val
);
630 midx
= l3mdev_master_ifindex_rcu(dev
);
634 if (sk
->sk_bound_dev_if
&&
635 sk
->sk_bound_dev_if
!= val
&&
636 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
642 case IPV6_ADD_MEMBERSHIP
:
643 case IPV6_DROP_MEMBERSHIP
:
645 struct ipv6_mreq mreq
;
647 if (optlen
< sizeof(struct ipv6_mreq
))
651 if (inet_sk(sk
)->is_icsk
)
655 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
658 if (optname
== IPV6_ADD_MEMBERSHIP
)
659 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
661 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
664 case IPV6_JOIN_ANYCAST
:
665 case IPV6_LEAVE_ANYCAST
:
667 struct ipv6_mreq mreq
;
669 if (optlen
< sizeof(struct ipv6_mreq
))
673 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
676 if (optname
== IPV6_JOIN_ANYCAST
)
677 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
679 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
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_MTU_DISCOVER
:
789 if (optlen
< sizeof(int))
791 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
797 if (optlen
< sizeof(int))
799 if (val
&& val
< IPV6_MIN_MTU
)
805 if (optlen
< sizeof(int))
807 np
->recverr
= valbool
;
809 skb_queue_purge(&sk
->sk_error_queue
);
812 case IPV6_FLOWINFO_SEND
:
813 if (optlen
< sizeof(int))
815 np
->sndflow
= valbool
;
818 case IPV6_FLOWLABEL_MGR
:
819 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
821 case IPV6_IPSEC_POLICY
:
822 case IPV6_XFRM_POLICY
:
824 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
826 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
829 case IPV6_ADDR_PREFERENCES
:
831 unsigned int pref
= 0;
832 unsigned int prefmask
= ~0;
834 if (optlen
< sizeof(int))
839 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
840 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
842 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
843 case IPV6_PREFER_SRC_PUBLIC
:
844 pref
|= IPV6_PREFER_SRC_PUBLIC
;
846 case IPV6_PREFER_SRC_TMP
:
847 pref
|= IPV6_PREFER_SRC_TMP
;
849 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
852 goto pref_skip_pubtmp
;
857 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
858 IPV6_PREFER_SRC_TMP
);
861 /* check HOME/COA conflicts */
862 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
863 case IPV6_PREFER_SRC_HOME
:
865 case IPV6_PREFER_SRC_COA
:
866 pref
|= IPV6_PREFER_SRC_COA
;
873 prefmask
&= ~IPV6_PREFER_SRC_COA
;
876 /* check CGA/NONCGA conflicts */
877 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
878 case IPV6_PREFER_SRC_CGA
:
879 case IPV6_PREFER_SRC_NONCGA
:
886 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
891 case IPV6_MINHOPCOUNT
:
892 if (optlen
< sizeof(int))
894 if (val
< 0 || val
> 255)
896 np
->min_hopcount
= val
;
900 np
->dontfrag
= valbool
;
903 case IPV6_AUTOFLOWLABEL
:
904 np
->autoflowlabel
= valbool
;
905 np
->autoflowlabel_set
= 1;
908 case IPV6_RECVFRAGSIZE
:
909 np
->rxopt
.bits
.recvfragsize
= valbool
;
927 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
928 char __user
*optval
, unsigned int optlen
)
932 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
933 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
935 if (level
!= SOL_IPV6
)
938 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
939 #ifdef CONFIG_NETFILTER
940 /* we need to exclude all possible ENOPROTOOPTs except default case */
941 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
942 optname
!= IPV6_XFRM_POLICY
)
943 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
, optlen
);
947 EXPORT_SYMBOL(ipv6_setsockopt
);
950 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
951 char __user
*optval
, unsigned int optlen
)
955 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
956 if (udp_prot
.compat_setsockopt
!= NULL
)
957 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
959 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
962 if (level
!= SOL_IPV6
)
965 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
966 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
969 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
970 #ifdef CONFIG_NETFILTER
971 /* we need to exclude all possible ENOPROTOOPTs except default case */
972 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
973 optname
!= IPV6_XFRM_POLICY
)
974 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
979 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
982 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
983 int optname
, char __user
*optval
, int len
)
985 struct ipv6_opt_hdr
*hdr
;
994 case IPV6_RTHDRDSTOPTS
:
998 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
1004 return -EINVAL
; /* should not happen */
1010 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
1011 if (copy_to_user(optval
, hdr
, len
))
1016 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1017 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1019 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1023 if (ip6_mroute_opt(optname
))
1024 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1026 if (get_user(len
, optlen
))
1030 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
1031 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
1032 sk
->sk_protocol
!= IPPROTO_TCP
)
1033 return -ENOPROTOOPT
;
1034 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1036 val
= sk
->sk_family
;
1038 case MCAST_MSFILTER
:
1040 struct group_filter gsf
;
1043 if (len
< GROUP_FILTER_SIZE(0))
1045 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
1047 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1048 return -EADDRNOTAVAIL
;
1050 err
= ip6_mc_msfget(sk
, &gsf
,
1051 (struct group_filter __user
*)optval
, optlen
);
1056 case IPV6_2292PKTOPTIONS
:
1059 struct sk_buff
*skb
;
1061 if (sk
->sk_type
!= SOCK_STREAM
)
1062 return -ENOPROTOOPT
;
1064 msg
.msg_control
= optval
;
1065 msg
.msg_controllen
= len
;
1066 msg
.msg_flags
= flags
;
1069 skb
= np
->pktoptions
;
1071 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1074 if (np
->rxopt
.bits
.rxinfo
) {
1075 struct in6_pktinfo src_info
;
1076 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1077 np
->sticky_pktinfo
.ipi6_ifindex
;
1078 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1079 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1081 if (np
->rxopt
.bits
.rxhlim
) {
1082 int hlim
= np
->mcast_hops
;
1083 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1085 if (np
->rxopt
.bits
.rxtclass
) {
1086 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1088 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1090 if (np
->rxopt
.bits
.rxoinfo
) {
1091 struct in6_pktinfo src_info
;
1092 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1093 np
->sticky_pktinfo
.ipi6_ifindex
;
1094 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1095 np
->sticky_pktinfo
.ipi6_addr
;
1096 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1098 if (np
->rxopt
.bits
.rxohlim
) {
1099 int hlim
= np
->mcast_hops
;
1100 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1102 if (np
->rxopt
.bits
.rxflow
) {
1103 __be32 flowinfo
= np
->rcv_flowinfo
;
1105 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1108 len
-= msg
.msg_controllen
;
1109 return put_user(len
, optlen
);
1113 struct dst_entry
*dst
;
1117 dst
= __sk_dst_get(sk
);
1127 val
= sk
->sk_ipv6only
;
1130 case IPV6_RECVPKTINFO
:
1131 val
= np
->rxopt
.bits
.rxinfo
;
1134 case IPV6_2292PKTINFO
:
1135 val
= np
->rxopt
.bits
.rxoinfo
;
1138 case IPV6_RECVHOPLIMIT
:
1139 val
= np
->rxopt
.bits
.rxhlim
;
1142 case IPV6_2292HOPLIMIT
:
1143 val
= np
->rxopt
.bits
.rxohlim
;
1146 case IPV6_RECVRTHDR
:
1147 val
= np
->rxopt
.bits
.srcrt
;
1150 case IPV6_2292RTHDR
:
1151 val
= np
->rxopt
.bits
.osrcrt
;
1155 case IPV6_RTHDRDSTOPTS
:
1159 struct ipv6_txoptions
*opt
;
1162 opt
= rcu_dereference_protected(np
->opt
,
1163 lockdep_sock_is_held(sk
));
1164 len
= ipv6_getsockopt_sticky(sk
, opt
, optname
, optval
, len
);
1166 /* check if ipv6_getsockopt_sticky() returns err code */
1169 return put_user(len
, optlen
);
1172 case IPV6_RECVHOPOPTS
:
1173 val
= np
->rxopt
.bits
.hopopts
;
1176 case IPV6_2292HOPOPTS
:
1177 val
= np
->rxopt
.bits
.ohopopts
;
1180 case IPV6_RECVDSTOPTS
:
1181 val
= np
->rxopt
.bits
.dstopts
;
1184 case IPV6_2292DSTOPTS
:
1185 val
= np
->rxopt
.bits
.odstopts
;
1192 case IPV6_RECVTCLASS
:
1193 val
= np
->rxopt
.bits
.rxtclass
;
1197 val
= np
->rxopt
.bits
.rxflow
;
1200 case IPV6_RECVPATHMTU
:
1201 val
= np
->rxopt
.bits
.rxpmtu
;
1206 struct dst_entry
*dst
;
1207 struct ip6_mtuinfo mtuinfo
;
1209 if (len
< sizeof(mtuinfo
))
1212 len
= sizeof(mtuinfo
);
1213 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1216 dst
= __sk_dst_get(sk
);
1218 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1220 if (!mtuinfo
.ip6m_mtu
)
1223 if (put_user(len
, optlen
))
1225 if (copy_to_user(optval
, &mtuinfo
, len
))
1231 case IPV6_TRANSPARENT
:
1232 val
= inet_sk(sk
)->transparent
;
1236 val
= inet_sk(sk
)->freebind
;
1239 case IPV6_RECVORIGDSTADDR
:
1240 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1243 case IPV6_UNICAST_HOPS
:
1244 case IPV6_MULTICAST_HOPS
:
1246 struct dst_entry
*dst
;
1248 if (optname
== IPV6_UNICAST_HOPS
)
1249 val
= np
->hop_limit
;
1251 val
= np
->mcast_hops
;
1255 dst
= __sk_dst_get(sk
);
1257 val
= ip6_dst_hoplimit(dst
);
1262 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1266 case IPV6_MULTICAST_LOOP
:
1270 case IPV6_MULTICAST_IF
:
1271 val
= np
->mcast_oif
;
1274 case IPV6_UNICAST_IF
:
1275 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1278 case IPV6_MTU_DISCOVER
:
1286 case IPV6_FLOWINFO_SEND
:
1290 case IPV6_FLOWLABEL_MGR
:
1292 struct in6_flowlabel_req freq
;
1295 if (len
< sizeof(freq
))
1298 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1301 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1305 flags
= freq
.flr_flags
;
1307 memset(&freq
, 0, sizeof(freq
));
1309 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1313 if (put_user(len
, optlen
))
1315 if (copy_to_user(optval
, &freq
, len
))
1321 case IPV6_ADDR_PREFERENCES
:
1324 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1325 val
|= IPV6_PREFER_SRC_TMP
;
1326 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1327 val
|= IPV6_PREFER_SRC_PUBLIC
;
1329 /* XXX: should we return system default? */
1330 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1333 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1334 val
|= IPV6_PREFER_SRC_COA
;
1336 val
|= IPV6_PREFER_SRC_HOME
;
1339 case IPV6_MINHOPCOUNT
:
1340 val
= np
->min_hopcount
;
1347 case IPV6_AUTOFLOWLABEL
:
1348 val
= ip6_autoflowlabel(sock_net(sk
), np
);
1351 case IPV6_RECVFRAGSIZE
:
1352 val
= np
->rxopt
.bits
.recvfragsize
;
1356 return -ENOPROTOOPT
;
1358 len
= min_t(unsigned int, sizeof(int), len
);
1359 if (put_user(len
, optlen
))
1361 if (copy_to_user(optval
, &val
, len
))
1366 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1367 char __user
*optval
, int __user
*optlen
)
1371 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1372 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1374 if (level
!= SOL_IPV6
)
1375 return -ENOPROTOOPT
;
1377 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1378 #ifdef CONFIG_NETFILTER
1379 /* we need to exclude all possible ENOPROTOOPTs except default case */
1380 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1383 if (get_user(len
, optlen
))
1386 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1388 err
= put_user(len
, optlen
);
1393 EXPORT_SYMBOL(ipv6_getsockopt
);
1395 #ifdef CONFIG_COMPAT
1396 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1397 char __user
*optval
, int __user
*optlen
)
1401 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1402 if (udp_prot
.compat_getsockopt
!= NULL
)
1403 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1405 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1408 if (level
!= SOL_IPV6
)
1409 return -ENOPROTOOPT
;
1411 if (optname
== MCAST_MSFILTER
)
1412 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1415 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1417 #ifdef CONFIG_NETFILTER
1418 /* we need to exclude all possible ENOPROTOOPTs except default case */
1419 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1422 if (get_user(len
, optlen
))
1425 err
= compat_nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1427 err
= put_user(len
, optlen
);
1432 EXPORT_SYMBOL(compat_ipv6_getsockopt
);