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>
56 #include <asm/uaccess.h>
58 struct ip6_ra_chain
*ip6_ra_chain
;
59 DEFINE_RWLOCK(ip6_ra_lock
);
61 int ip6_ra_control(struct sock
*sk
, int sel
)
63 struct ip6_ra_chain
*ra
, *new_ra
, **rap
;
65 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
66 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
!= IPPROTO_RAW
)
69 new_ra
= (sel
>= 0) ? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
71 write_lock_bh(&ip6_ra_lock
);
72 for (rap
= &ip6_ra_chain
; (ra
= *rap
) != NULL
; rap
= &ra
->next
) {
75 write_unlock_bh(&ip6_ra_lock
);
81 write_unlock_bh(&ip6_ra_lock
);
89 write_unlock_bh(&ip6_ra_lock
);
97 write_unlock_bh(&ip6_ra_lock
);
101 struct ipv6_txoptions
*ipv6_update_options(struct sock
*sk
,
102 struct ipv6_txoptions
*opt
)
104 if (inet_sk(sk
)->is_icsk
) {
106 !((1 << sk
->sk_state
) & (TCPF_LISTEN
| TCPF_CLOSE
)) &&
107 inet_sk(sk
)->inet_daddr
!= LOOPBACK4_IPV6
) {
108 struct inet_connection_sock
*icsk
= inet_csk(sk
);
109 icsk
->icsk_ext_hdr_len
= opt
->opt_flen
+ opt
->opt_nflen
;
110 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
113 opt
= xchg((__force
struct ipv6_txoptions
**)&inet6_sk(sk
)->opt
,
120 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
);
243 sk
->sk_destruct
= inet_sock_destruct
;
245 * ... and add it to the refcnt debug socks count
246 * in the new family. -acme
248 sk_refcnt_debug_inc(sk
);
249 module_put(THIS_MODULE
);
256 if (optlen
< sizeof(int) ||
257 inet_sk(sk
)->inet_num
)
259 sk
->sk_ipv6only
= valbool
;
263 case IPV6_RECVPKTINFO
:
264 if (optlen
< sizeof(int))
266 np
->rxopt
.bits
.rxinfo
= valbool
;
270 case IPV6_2292PKTINFO
:
271 if (optlen
< sizeof(int))
273 np
->rxopt
.bits
.rxoinfo
= valbool
;
277 case IPV6_RECVHOPLIMIT
:
278 if (optlen
< sizeof(int))
280 np
->rxopt
.bits
.rxhlim
= valbool
;
284 case IPV6_2292HOPLIMIT
:
285 if (optlen
< sizeof(int))
287 np
->rxopt
.bits
.rxohlim
= valbool
;
292 if (optlen
< sizeof(int))
294 np
->rxopt
.bits
.srcrt
= valbool
;
299 if (optlen
< sizeof(int))
301 np
->rxopt
.bits
.osrcrt
= valbool
;
305 case IPV6_RECVHOPOPTS
:
306 if (optlen
< sizeof(int))
308 np
->rxopt
.bits
.hopopts
= valbool
;
312 case IPV6_2292HOPOPTS
:
313 if (optlen
< sizeof(int))
315 np
->rxopt
.bits
.ohopopts
= valbool
;
319 case IPV6_RECVDSTOPTS
:
320 if (optlen
< sizeof(int))
322 np
->rxopt
.bits
.dstopts
= valbool
;
326 case IPV6_2292DSTOPTS
:
327 if (optlen
< sizeof(int))
329 np
->rxopt
.bits
.odstopts
= valbool
;
334 if (optlen
< sizeof(int))
336 if (val
< -1 || val
> 0xff)
338 /* RFC 3542, 6.5: default traffic class of 0x0 */
345 case IPV6_RECVTCLASS
:
346 if (optlen
< sizeof(int))
348 np
->rxopt
.bits
.rxtclass
= valbool
;
353 if (optlen
< sizeof(int))
355 np
->rxopt
.bits
.rxflow
= valbool
;
359 case IPV6_RECVPATHMTU
:
360 if (optlen
< sizeof(int))
362 np
->rxopt
.bits
.rxpmtu
= valbool
;
366 case IPV6_TRANSPARENT
:
367 if (valbool
&& !ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
368 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
372 if (optlen
< sizeof(int))
374 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
375 inet_sk(sk
)->transparent
= valbool
;
379 case IPV6_RECVORIGDSTADDR
:
380 if (optlen
< sizeof(int))
382 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
387 case IPV6_RTHDRDSTOPTS
:
391 struct ipv6_txoptions
*opt
;
393 /* remove any sticky options header with a zero option
394 * length, per RFC3542.
400 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
401 optlen
& 0x7 || optlen
> 8 * 255)
404 /* hop-by-hop / destination options are privileged option */
406 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
409 opt
= rcu_dereference_protected(np
->opt
,
410 lockdep_sock_is_held(sk
));
411 opt
= ipv6_renew_options(sk
, opt
, optname
,
412 (struct ipv6_opt_hdr __user
*)optval
,
419 /* routing header option needs extra check */
421 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
422 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
423 switch (rthdr
->type
) {
424 #if IS_ENABLED(CONFIG_IPV6_MIP6)
425 case IPV6_SRCRT_TYPE_2
:
426 if (rthdr
->hdrlen
!= 2 ||
427 rthdr
->segments_left
!= 1)
438 opt
= ipv6_update_options(sk
, opt
);
441 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
449 struct in6_pktinfo pkt
;
453 else if (optlen
< sizeof(struct in6_pktinfo
) || !optval
)
456 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
460 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
463 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
464 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
469 case IPV6_2292PKTOPTIONS
:
471 struct ipv6_txoptions
*opt
= NULL
;
474 struct sockcm_cookie sockc_junk
;
475 struct ipcm6_cookie ipc6
;
477 memset(&fl6
, 0, sizeof(fl6
));
478 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
479 fl6
.flowi6_mark
= sk
->sk_mark
;
484 /* 1K is probably excessive
485 * 1K is surely not enough, 2K per standard header is 16K.
488 if (optlen
> 64*1024)
491 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
496 memset(opt
, 0, sizeof(*opt
));
497 atomic_set(&opt
->refcnt
, 1);
498 opt
->tot_len
= sizeof(*opt
) + optlen
;
500 if (copy_from_user(opt
+1, optval
, optlen
))
503 msg
.msg_controllen
= optlen
;
504 msg
.msg_control
= (void *)(opt
+1);
507 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, &ipc6
, &sockc_junk
);
512 opt
= ipv6_update_options(sk
, opt
);
515 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
520 case IPV6_UNICAST_HOPS
:
521 if (optlen
< sizeof(int))
523 if (val
> 255 || val
< -1)
529 case IPV6_MULTICAST_HOPS
:
530 if (sk
->sk_type
== SOCK_STREAM
)
532 if (optlen
< sizeof(int))
534 if (val
> 255 || val
< -1)
536 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
540 case IPV6_MULTICAST_LOOP
:
541 if (optlen
< sizeof(int))
545 np
->mc_loop
= valbool
;
549 case IPV6_UNICAST_IF
:
551 struct net_device
*dev
= NULL
;
554 if (optlen
!= sizeof(int))
557 ifindex
= (__force
int)ntohl((__force __be32
)val
);
564 dev
= dev_get_by_index(net
, ifindex
);
565 retv
= -EADDRNOTAVAIL
;
571 if (sk
->sk_bound_dev_if
)
574 np
->ucast_oif
= ifindex
;
579 case IPV6_MULTICAST_IF
:
580 if (sk
->sk_type
== SOCK_STREAM
)
582 if (optlen
< sizeof(int))
586 struct net_device
*dev
;
588 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
591 dev
= dev_get_by_index(net
, val
);
601 case IPV6_ADD_MEMBERSHIP
:
602 case IPV6_DROP_MEMBERSHIP
:
604 struct ipv6_mreq mreq
;
606 if (optlen
< sizeof(struct ipv6_mreq
))
610 if (inet_sk(sk
)->is_icsk
)
614 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
617 if (optname
== IPV6_ADD_MEMBERSHIP
)
618 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
620 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
623 case IPV6_JOIN_ANYCAST
:
624 case IPV6_LEAVE_ANYCAST
:
626 struct ipv6_mreq mreq
;
628 if (optlen
< sizeof(struct ipv6_mreq
))
632 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
635 if (optname
== IPV6_JOIN_ANYCAST
)
636 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
638 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
641 case MCAST_JOIN_GROUP
:
642 case MCAST_LEAVE_GROUP
:
644 struct group_req greq
;
645 struct sockaddr_in6
*psin6
;
647 if (optlen
< sizeof(struct group_req
))
651 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
653 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
654 retv
= -EADDRNOTAVAIL
;
657 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
658 if (optname
== MCAST_JOIN_GROUP
)
659 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
662 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
666 case MCAST_JOIN_SOURCE_GROUP
:
667 case MCAST_LEAVE_SOURCE_GROUP
:
668 case MCAST_BLOCK_SOURCE
:
669 case MCAST_UNBLOCK_SOURCE
:
671 struct group_source_req greqs
;
674 if (optlen
< sizeof(struct group_source_req
))
676 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
680 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
681 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
682 retv
= -EADDRNOTAVAIL
;
685 if (optname
== MCAST_BLOCK_SOURCE
) {
686 omode
= MCAST_EXCLUDE
;
688 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
689 omode
= MCAST_EXCLUDE
;
691 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
692 struct sockaddr_in6
*psin6
;
694 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
695 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
697 /* prior join w/ different source is ok */
698 if (retv
&& retv
!= -EADDRINUSE
)
700 omode
= MCAST_INCLUDE
;
702 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
703 omode
= MCAST_INCLUDE
;
706 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
711 struct group_filter
*gsf
;
713 if (optlen
< GROUP_FILTER_SIZE(0))
715 if (optlen
> sysctl_optmem_max
) {
719 gsf
= kmalloc(optlen
, GFP_KERNEL
);
725 if (copy_from_user(gsf
, optval
, optlen
)) {
729 /* numsrc >= (4G-140)/128 overflow in 32 bits */
730 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
731 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
736 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
741 retv
= ip6_mc_msfilter(sk
, gsf
);
746 case IPV6_ROUTER_ALERT
:
747 if (optlen
< sizeof(int))
749 retv
= ip6_ra_control(sk
, val
);
751 case IPV6_MTU_DISCOVER
:
752 if (optlen
< sizeof(int))
754 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
760 if (optlen
< sizeof(int))
762 if (val
&& val
< IPV6_MIN_MTU
)
768 if (optlen
< sizeof(int))
770 np
->recverr
= valbool
;
772 skb_queue_purge(&sk
->sk_error_queue
);
775 case IPV6_FLOWINFO_SEND
:
776 if (optlen
< sizeof(int))
778 np
->sndflow
= valbool
;
781 case IPV6_FLOWLABEL_MGR
:
782 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
784 case IPV6_IPSEC_POLICY
:
785 case IPV6_XFRM_POLICY
:
787 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
789 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
792 case IPV6_ADDR_PREFERENCES
:
794 unsigned int pref
= 0;
795 unsigned int prefmask
= ~0;
797 if (optlen
< sizeof(int))
802 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
803 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
805 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
806 case IPV6_PREFER_SRC_PUBLIC
:
807 pref
|= IPV6_PREFER_SRC_PUBLIC
;
809 case IPV6_PREFER_SRC_TMP
:
810 pref
|= IPV6_PREFER_SRC_TMP
;
812 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
815 goto pref_skip_pubtmp
;
820 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
821 IPV6_PREFER_SRC_TMP
);
824 /* check HOME/COA conflicts */
825 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
826 case IPV6_PREFER_SRC_HOME
:
828 case IPV6_PREFER_SRC_COA
:
829 pref
|= IPV6_PREFER_SRC_COA
;
836 prefmask
&= ~IPV6_PREFER_SRC_COA
;
839 /* check CGA/NONCGA conflicts */
840 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
841 case IPV6_PREFER_SRC_CGA
:
842 case IPV6_PREFER_SRC_NONCGA
:
849 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
854 case IPV6_MINHOPCOUNT
:
855 if (optlen
< sizeof(int))
857 if (val
< 0 || val
> 255)
859 np
->min_hopcount
= val
;
863 np
->dontfrag
= valbool
;
866 case IPV6_AUTOFLOWLABEL
:
867 np
->autoflowlabel
= valbool
;
885 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
886 char __user
*optval
, unsigned int optlen
)
890 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
891 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
893 if (level
!= SOL_IPV6
)
896 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
897 #ifdef CONFIG_NETFILTER
898 /* we need to exclude all possible ENOPROTOOPTs except default case */
899 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
900 optname
!= IPV6_XFRM_POLICY
) {
902 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
909 EXPORT_SYMBOL(ipv6_setsockopt
);
912 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
913 char __user
*optval
, unsigned int optlen
)
917 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
918 if (udp_prot
.compat_setsockopt
!= NULL
)
919 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
921 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
924 if (level
!= SOL_IPV6
)
927 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
928 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
931 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
932 #ifdef CONFIG_NETFILTER
933 /* we need to exclude all possible ENOPROTOOPTs except default case */
934 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
935 optname
!= IPV6_XFRM_POLICY
) {
937 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
944 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
947 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
948 int optname
, char __user
*optval
, int len
)
950 struct ipv6_opt_hdr
*hdr
;
959 case IPV6_RTHDRDSTOPTS
:
963 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
969 return -EINVAL
; /* should not happen */
975 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
976 if (copy_to_user(optval
, hdr
, len
))
981 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
982 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
984 struct ipv6_pinfo
*np
= inet6_sk(sk
);
988 if (ip6_mroute_opt(optname
))
989 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
991 if (get_user(len
, optlen
))
995 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
996 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
997 sk
->sk_protocol
!= IPPROTO_TCP
)
999 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1001 val
= sk
->sk_family
;
1003 case MCAST_MSFILTER
:
1005 struct group_filter gsf
;
1008 if (len
< GROUP_FILTER_SIZE(0))
1010 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
1012 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1013 return -EADDRNOTAVAIL
;
1015 err
= ip6_mc_msfget(sk
, &gsf
,
1016 (struct group_filter __user
*)optval
, optlen
);
1021 case IPV6_2292PKTOPTIONS
:
1024 struct sk_buff
*skb
;
1026 if (sk
->sk_type
!= SOCK_STREAM
)
1027 return -ENOPROTOOPT
;
1029 msg
.msg_control
= optval
;
1030 msg
.msg_controllen
= len
;
1031 msg
.msg_flags
= flags
;
1034 skb
= np
->pktoptions
;
1036 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1039 if (np
->rxopt
.bits
.rxinfo
) {
1040 struct in6_pktinfo src_info
;
1041 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1042 np
->sticky_pktinfo
.ipi6_ifindex
;
1043 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1044 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1046 if (np
->rxopt
.bits
.rxhlim
) {
1047 int hlim
= np
->mcast_hops
;
1048 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1050 if (np
->rxopt
.bits
.rxtclass
) {
1051 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1053 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1055 if (np
->rxopt
.bits
.rxoinfo
) {
1056 struct in6_pktinfo src_info
;
1057 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1058 np
->sticky_pktinfo
.ipi6_ifindex
;
1059 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1060 np
->sticky_pktinfo
.ipi6_addr
;
1061 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1063 if (np
->rxopt
.bits
.rxohlim
) {
1064 int hlim
= np
->mcast_hops
;
1065 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1067 if (np
->rxopt
.bits
.rxflow
) {
1068 __be32 flowinfo
= np
->rcv_flowinfo
;
1070 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1073 len
-= msg
.msg_controllen
;
1074 return put_user(len
, optlen
);
1078 struct dst_entry
*dst
;
1082 dst
= __sk_dst_get(sk
);
1092 val
= sk
->sk_ipv6only
;
1095 case IPV6_RECVPKTINFO
:
1096 val
= np
->rxopt
.bits
.rxinfo
;
1099 case IPV6_2292PKTINFO
:
1100 val
= np
->rxopt
.bits
.rxoinfo
;
1103 case IPV6_RECVHOPLIMIT
:
1104 val
= np
->rxopt
.bits
.rxhlim
;
1107 case IPV6_2292HOPLIMIT
:
1108 val
= np
->rxopt
.bits
.rxohlim
;
1111 case IPV6_RECVRTHDR
:
1112 val
= np
->rxopt
.bits
.srcrt
;
1115 case IPV6_2292RTHDR
:
1116 val
= np
->rxopt
.bits
.osrcrt
;
1120 case IPV6_RTHDRDSTOPTS
:
1124 struct ipv6_txoptions
*opt
;
1127 opt
= rcu_dereference_protected(np
->opt
,
1128 lockdep_sock_is_held(sk
));
1129 len
= ipv6_getsockopt_sticky(sk
, opt
, optname
, optval
, len
);
1131 /* check if ipv6_getsockopt_sticky() returns err code */
1134 return put_user(len
, optlen
);
1137 case IPV6_RECVHOPOPTS
:
1138 val
= np
->rxopt
.bits
.hopopts
;
1141 case IPV6_2292HOPOPTS
:
1142 val
= np
->rxopt
.bits
.ohopopts
;
1145 case IPV6_RECVDSTOPTS
:
1146 val
= np
->rxopt
.bits
.dstopts
;
1149 case IPV6_2292DSTOPTS
:
1150 val
= np
->rxopt
.bits
.odstopts
;
1157 case IPV6_RECVTCLASS
:
1158 val
= np
->rxopt
.bits
.rxtclass
;
1162 val
= np
->rxopt
.bits
.rxflow
;
1165 case IPV6_RECVPATHMTU
:
1166 val
= np
->rxopt
.bits
.rxpmtu
;
1171 struct dst_entry
*dst
;
1172 struct ip6_mtuinfo mtuinfo
;
1174 if (len
< sizeof(mtuinfo
))
1177 len
= sizeof(mtuinfo
);
1178 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1181 dst
= __sk_dst_get(sk
);
1183 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1185 if (!mtuinfo
.ip6m_mtu
)
1188 if (put_user(len
, optlen
))
1190 if (copy_to_user(optval
, &mtuinfo
, len
))
1196 case IPV6_TRANSPARENT
:
1197 val
= inet_sk(sk
)->transparent
;
1200 case IPV6_RECVORIGDSTADDR
:
1201 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1204 case IPV6_UNICAST_HOPS
:
1205 case IPV6_MULTICAST_HOPS
:
1207 struct dst_entry
*dst
;
1209 if (optname
== IPV6_UNICAST_HOPS
)
1210 val
= np
->hop_limit
;
1212 val
= np
->mcast_hops
;
1216 dst
= __sk_dst_get(sk
);
1218 val
= ip6_dst_hoplimit(dst
);
1223 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1227 case IPV6_MULTICAST_LOOP
:
1231 case IPV6_MULTICAST_IF
:
1232 val
= np
->mcast_oif
;
1235 case IPV6_UNICAST_IF
:
1236 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1239 case IPV6_MTU_DISCOVER
:
1247 case IPV6_FLOWINFO_SEND
:
1251 case IPV6_FLOWLABEL_MGR
:
1253 struct in6_flowlabel_req freq
;
1256 if (len
< sizeof(freq
))
1259 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1262 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1266 flags
= freq
.flr_flags
;
1268 memset(&freq
, 0, sizeof(freq
));
1270 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1274 if (put_user(len
, optlen
))
1276 if (copy_to_user(optval
, &freq
, len
))
1282 case IPV6_ADDR_PREFERENCES
:
1285 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1286 val
|= IPV6_PREFER_SRC_TMP
;
1287 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1288 val
|= IPV6_PREFER_SRC_PUBLIC
;
1290 /* XXX: should we return system default? */
1291 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1294 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1295 val
|= IPV6_PREFER_SRC_COA
;
1297 val
|= IPV6_PREFER_SRC_HOME
;
1300 case IPV6_MINHOPCOUNT
:
1301 val
= np
->min_hopcount
;
1308 case IPV6_AUTOFLOWLABEL
:
1309 val
= np
->autoflowlabel
;
1313 return -ENOPROTOOPT
;
1315 len
= min_t(unsigned int, sizeof(int), len
);
1316 if (put_user(len
, optlen
))
1318 if (copy_to_user(optval
, &val
, len
))
1323 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1324 char __user
*optval
, int __user
*optlen
)
1328 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1329 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1331 if (level
!= SOL_IPV6
)
1332 return -ENOPROTOOPT
;
1334 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1335 #ifdef CONFIG_NETFILTER
1336 /* we need to exclude all possible ENOPROTOOPTs except default case */
1337 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1340 if (get_user(len
, optlen
))
1344 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1348 err
= put_user(len
, optlen
);
1353 EXPORT_SYMBOL(ipv6_getsockopt
);
1355 #ifdef CONFIG_COMPAT
1356 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1357 char __user
*optval
, int __user
*optlen
)
1361 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1362 if (udp_prot
.compat_getsockopt
!= NULL
)
1363 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1365 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1368 if (level
!= SOL_IPV6
)
1369 return -ENOPROTOOPT
;
1371 if (optname
== MCAST_MSFILTER
)
1372 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1375 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1377 #ifdef CONFIG_NETFILTER
1378 /* we need to exclude all possible ENOPROTOOPTs except default case */
1379 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1382 if (get_user(len
, optlen
))
1386 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1387 optname
, optval
, &len
);
1390 err
= put_user(len
, optlen
);
1395 EXPORT_SYMBOL(compat_ipv6_getsockopt
);