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
);
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
)
124 case IPV6_ADD_MEMBERSHIP
:
125 case IPV6_DROP_MEMBERSHIP
:
126 case IPV6_JOIN_ANYCAST
:
127 case IPV6_LEAVE_ANYCAST
:
128 case MCAST_JOIN_GROUP
:
129 case MCAST_LEAVE_GROUP
:
130 case MCAST_JOIN_SOURCE_GROUP
:
131 case MCAST_LEAVE_SOURCE_GROUP
:
132 case MCAST_BLOCK_SOURCE
:
133 case MCAST_UNBLOCK_SOURCE
:
140 static int do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
141 char __user
*optval
, unsigned int optlen
)
143 struct ipv6_pinfo
*np
= inet6_sk(sk
);
144 struct net
*net
= sock_net(sk
);
146 int retv
= -ENOPROTOOPT
;
147 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
152 if (optlen
>= sizeof(int)) {
153 if (get_user(val
, (int __user
*) optval
))
159 valbool
= (val
!= 0);
161 if (ip6_mroute_opt(optname
))
162 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
171 if (optlen
< sizeof(int))
173 if (val
== PF_INET
) {
174 struct ipv6_txoptions
*opt
;
175 struct sk_buff
*pktopt
;
177 if (sk
->sk_type
== SOCK_RAW
)
180 if (sk
->sk_protocol
== IPPROTO_UDP
||
181 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
182 struct udp_sock
*up
= udp_sk(sk
);
183 if (up
->pending
== AF_INET6
) {
187 } else if (sk
->sk_protocol
!= IPPROTO_TCP
)
190 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
195 if (ipv6_only_sock(sk
) ||
196 !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
197 retv
= -EADDRNOTAVAIL
;
201 fl6_free_socklist(sk
);
202 ipv6_sock_mc_close(sk
);
205 * Sock is moving from IPv6 to IPv4 (sk_prot), so
206 * remove it from the refcnt debug socks count in the
209 sk_refcnt_debug_dec(sk
);
211 if (sk
->sk_protocol
== IPPROTO_TCP
) {
212 struct inet_connection_sock
*icsk
= inet_csk(sk
);
214 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
215 sock_prot_inuse_add(net
, &tcp_prot
, 1);
217 sk
->sk_prot
= &tcp_prot
;
218 icsk
->icsk_af_ops
= &ipv4_specific
;
219 sk
->sk_socket
->ops
= &inet_stream_ops
;
220 sk
->sk_family
= PF_INET
;
221 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
223 struct proto
*prot
= &udp_prot
;
225 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
226 prot
= &udplite_prot
;
228 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
229 sock_prot_inuse_add(net
, prot
, 1);
232 sk
->sk_socket
->ops
= &inet_dgram_ops
;
233 sk
->sk_family
= PF_INET
;
235 opt
= xchg((__force
struct ipv6_txoptions
**)&np
->opt
,
238 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
241 pktopt
= xchg(&np
->pktoptions
, NULL
);
244 sk
->sk_destruct
= inet_sock_destruct
;
246 * ... and add it to the refcnt debug socks count
247 * in the new family. -acme
249 sk_refcnt_debug_inc(sk
);
250 module_put(THIS_MODULE
);
257 if (optlen
< sizeof(int) ||
258 inet_sk(sk
)->inet_num
)
260 sk
->sk_ipv6only
= valbool
;
264 case IPV6_RECVPKTINFO
:
265 if (optlen
< sizeof(int))
267 np
->rxopt
.bits
.rxinfo
= valbool
;
271 case IPV6_2292PKTINFO
:
272 if (optlen
< sizeof(int))
274 np
->rxopt
.bits
.rxoinfo
= valbool
;
278 case IPV6_RECVHOPLIMIT
:
279 if (optlen
< sizeof(int))
281 np
->rxopt
.bits
.rxhlim
= valbool
;
285 case IPV6_2292HOPLIMIT
:
286 if (optlen
< sizeof(int))
288 np
->rxopt
.bits
.rxohlim
= valbool
;
293 if (optlen
< sizeof(int))
295 np
->rxopt
.bits
.srcrt
= valbool
;
300 if (optlen
< sizeof(int))
302 np
->rxopt
.bits
.osrcrt
= valbool
;
306 case IPV6_RECVHOPOPTS
:
307 if (optlen
< sizeof(int))
309 np
->rxopt
.bits
.hopopts
= valbool
;
313 case IPV6_2292HOPOPTS
:
314 if (optlen
< sizeof(int))
316 np
->rxopt
.bits
.ohopopts
= valbool
;
320 case IPV6_RECVDSTOPTS
:
321 if (optlen
< sizeof(int))
323 np
->rxopt
.bits
.dstopts
= valbool
;
327 case IPV6_2292DSTOPTS
:
328 if (optlen
< sizeof(int))
330 np
->rxopt
.bits
.odstopts
= valbool
;
335 if (optlen
< sizeof(int))
337 if (val
< -1 || val
> 0xff)
339 /* RFC 3542, 6.5: default traffic class of 0x0 */
346 case IPV6_RECVTCLASS
:
347 if (optlen
< sizeof(int))
349 np
->rxopt
.bits
.rxtclass
= valbool
;
354 if (optlen
< sizeof(int))
356 np
->rxopt
.bits
.rxflow
= valbool
;
360 case IPV6_RECVPATHMTU
:
361 if (optlen
< sizeof(int))
363 np
->rxopt
.bits
.rxpmtu
= valbool
;
367 case IPV6_TRANSPARENT
:
368 if (valbool
&& !ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
369 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
373 if (optlen
< sizeof(int))
375 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
376 inet_sk(sk
)->transparent
= valbool
;
380 case IPV6_RECVORIGDSTADDR
:
381 if (optlen
< sizeof(int))
383 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
388 case IPV6_RTHDRDSTOPTS
:
392 struct ipv6_txoptions
*opt
;
394 /* remove any sticky options header with a zero option
395 * length, per RFC3542.
401 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
402 optlen
& 0x7 || optlen
> 8 * 255)
405 /* hop-by-hop / destination options are privileged option */
407 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
410 opt
= rcu_dereference_protected(np
->opt
, sock_owned_by_user(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
;
476 memset(&fl6
, 0, sizeof(fl6
));
477 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
478 fl6
.flowi6_mark
= sk
->sk_mark
;
483 /* 1K is probably excessive
484 * 1K is surely not enough, 2K per standard header is 16K.
487 if (optlen
> 64*1024)
490 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
495 memset(opt
, 0, sizeof(*opt
));
496 atomic_set(&opt
->refcnt
, 1);
497 opt
->tot_len
= sizeof(*opt
) + optlen
;
499 if (copy_from_user(opt
+1, optval
, optlen
))
502 msg
.msg_controllen
= optlen
;
503 msg
.msg_control
= (void *)(opt
+1);
505 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, opt
, &junk
,
511 opt
= ipv6_update_options(sk
, opt
);
514 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
519 case IPV6_UNICAST_HOPS
:
520 if (optlen
< sizeof(int))
522 if (val
> 255 || val
< -1)
528 case IPV6_MULTICAST_HOPS
:
529 if (sk
->sk_type
== SOCK_STREAM
)
531 if (optlen
< sizeof(int))
533 if (val
> 255 || val
< -1)
535 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
539 case IPV6_MULTICAST_LOOP
:
540 if (optlen
< sizeof(int))
544 np
->mc_loop
= valbool
;
548 case IPV6_UNICAST_IF
:
550 struct net_device
*dev
= NULL
;
553 if (optlen
!= sizeof(int))
556 ifindex
= (__force
int)ntohl((__force __be32
)val
);
563 dev
= dev_get_by_index(net
, ifindex
);
564 retv
= -EADDRNOTAVAIL
;
570 if (sk
->sk_bound_dev_if
)
573 np
->ucast_oif
= ifindex
;
578 case IPV6_MULTICAST_IF
:
579 if (sk
->sk_type
== SOCK_STREAM
)
581 if (optlen
< sizeof(int))
585 struct net_device
*dev
;
587 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
590 dev
= dev_get_by_index(net
, val
);
600 case IPV6_ADD_MEMBERSHIP
:
601 case IPV6_DROP_MEMBERSHIP
:
603 struct ipv6_mreq mreq
;
605 if (optlen
< sizeof(struct ipv6_mreq
))
609 if (inet_sk(sk
)->is_icsk
)
613 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
616 if (optname
== IPV6_ADD_MEMBERSHIP
)
617 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
619 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
622 case IPV6_JOIN_ANYCAST
:
623 case IPV6_LEAVE_ANYCAST
:
625 struct ipv6_mreq mreq
;
627 if (optlen
< sizeof(struct ipv6_mreq
))
631 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
634 if (optname
== IPV6_JOIN_ANYCAST
)
635 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
637 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
640 case MCAST_JOIN_GROUP
:
641 case MCAST_LEAVE_GROUP
:
643 struct group_req greq
;
644 struct sockaddr_in6
*psin6
;
646 if (optlen
< sizeof(struct group_req
))
650 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
652 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
653 retv
= -EADDRNOTAVAIL
;
656 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
657 if (optname
== MCAST_JOIN_GROUP
)
658 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
661 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
665 case MCAST_JOIN_SOURCE_GROUP
:
666 case MCAST_LEAVE_SOURCE_GROUP
:
667 case MCAST_BLOCK_SOURCE
:
668 case MCAST_UNBLOCK_SOURCE
:
670 struct group_source_req greqs
;
673 if (optlen
< sizeof(struct group_source_req
))
675 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
679 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
680 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
681 retv
= -EADDRNOTAVAIL
;
684 if (optname
== MCAST_BLOCK_SOURCE
) {
685 omode
= MCAST_EXCLUDE
;
687 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
688 omode
= MCAST_EXCLUDE
;
690 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
691 struct sockaddr_in6
*psin6
;
693 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
694 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
696 /* prior join w/ different source is ok */
697 if (retv
&& retv
!= -EADDRINUSE
)
699 omode
= MCAST_INCLUDE
;
701 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
702 omode
= MCAST_INCLUDE
;
705 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
710 struct group_filter
*gsf
;
712 if (optlen
< GROUP_FILTER_SIZE(0))
714 if (optlen
> sysctl_optmem_max
) {
718 gsf
= kmalloc(optlen
, GFP_KERNEL
);
724 if (copy_from_user(gsf
, optval
, optlen
)) {
728 /* numsrc >= (4G-140)/128 overflow in 32 bits */
729 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
730 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
735 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
740 retv
= ip6_mc_msfilter(sk
, gsf
);
745 case IPV6_ROUTER_ALERT
:
746 if (optlen
< sizeof(int))
748 retv
= ip6_ra_control(sk
, val
);
750 case IPV6_MTU_DISCOVER
:
751 if (optlen
< sizeof(int))
753 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
759 if (optlen
< sizeof(int))
761 if (val
&& val
< IPV6_MIN_MTU
)
767 if (optlen
< sizeof(int))
769 np
->recverr
= valbool
;
771 skb_queue_purge(&sk
->sk_error_queue
);
774 case IPV6_FLOWINFO_SEND
:
775 if (optlen
< sizeof(int))
777 np
->sndflow
= valbool
;
780 case IPV6_FLOWLABEL_MGR
:
781 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
783 case IPV6_IPSEC_POLICY
:
784 case IPV6_XFRM_POLICY
:
786 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
788 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
791 case IPV6_ADDR_PREFERENCES
:
793 unsigned int pref
= 0;
794 unsigned int prefmask
= ~0;
796 if (optlen
< sizeof(int))
801 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
802 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
804 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
805 case IPV6_PREFER_SRC_PUBLIC
:
806 pref
|= IPV6_PREFER_SRC_PUBLIC
;
808 case IPV6_PREFER_SRC_TMP
:
809 pref
|= IPV6_PREFER_SRC_TMP
;
811 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
814 goto pref_skip_pubtmp
;
819 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
820 IPV6_PREFER_SRC_TMP
);
823 /* check HOME/COA conflicts */
824 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
825 case IPV6_PREFER_SRC_HOME
:
827 case IPV6_PREFER_SRC_COA
:
828 pref
|= IPV6_PREFER_SRC_COA
;
835 prefmask
&= ~IPV6_PREFER_SRC_COA
;
838 /* check CGA/NONCGA conflicts */
839 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
840 case IPV6_PREFER_SRC_CGA
:
841 case IPV6_PREFER_SRC_NONCGA
:
848 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
853 case IPV6_MINHOPCOUNT
:
854 if (optlen
< sizeof(int))
856 if (val
< 0 || val
> 255)
858 np
->min_hopcount
= val
;
862 np
->dontfrag
= valbool
;
865 case IPV6_AUTOFLOWLABEL
:
866 np
->autoflowlabel
= valbool
;
884 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
885 char __user
*optval
, unsigned int optlen
)
889 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
890 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
892 if (level
!= SOL_IPV6
)
895 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
896 #ifdef CONFIG_NETFILTER
897 /* we need to exclude all possible ENOPROTOOPTs except default case */
898 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
899 optname
!= IPV6_XFRM_POLICY
) {
901 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
908 EXPORT_SYMBOL(ipv6_setsockopt
);
911 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
912 char __user
*optval
, unsigned int optlen
)
916 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
917 if (udp_prot
.compat_setsockopt
!= NULL
)
918 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
920 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
923 if (level
!= SOL_IPV6
)
926 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
927 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
930 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
931 #ifdef CONFIG_NETFILTER
932 /* we need to exclude all possible ENOPROTOOPTs except default case */
933 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
934 optname
!= IPV6_XFRM_POLICY
) {
936 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
943 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
946 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
947 int optname
, char __user
*optval
, int len
)
949 struct ipv6_opt_hdr
*hdr
;
958 case IPV6_RTHDRDSTOPTS
:
962 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
968 return -EINVAL
; /* should not happen */
974 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
975 if (copy_to_user(optval
, hdr
, len
))
980 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
981 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
983 struct ipv6_pinfo
*np
= inet6_sk(sk
);
987 if (ip6_mroute_opt(optname
))
988 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
990 if (get_user(len
, optlen
))
994 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
995 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
996 sk
->sk_protocol
!= IPPROTO_TCP
)
998 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1000 val
= sk
->sk_family
;
1002 case MCAST_MSFILTER
:
1004 struct group_filter gsf
;
1007 if (len
< GROUP_FILTER_SIZE(0))
1009 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
1011 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1012 return -EADDRNOTAVAIL
;
1014 err
= ip6_mc_msfget(sk
, &gsf
,
1015 (struct group_filter __user
*)optval
, optlen
);
1020 case IPV6_2292PKTOPTIONS
:
1023 struct sk_buff
*skb
;
1025 if (sk
->sk_type
!= SOCK_STREAM
)
1026 return -ENOPROTOOPT
;
1028 msg
.msg_control
= optval
;
1029 msg
.msg_controllen
= len
;
1030 msg
.msg_flags
= flags
;
1033 skb
= np
->pktoptions
;
1035 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1038 if (np
->rxopt
.bits
.rxinfo
) {
1039 struct in6_pktinfo src_info
;
1040 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1041 np
->sticky_pktinfo
.ipi6_ifindex
;
1042 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1043 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1045 if (np
->rxopt
.bits
.rxhlim
) {
1046 int hlim
= np
->mcast_hops
;
1047 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1049 if (np
->rxopt
.bits
.rxtclass
) {
1050 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1052 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1054 if (np
->rxopt
.bits
.rxoinfo
) {
1055 struct in6_pktinfo src_info
;
1056 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1057 np
->sticky_pktinfo
.ipi6_ifindex
;
1058 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1059 np
->sticky_pktinfo
.ipi6_addr
;
1060 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1062 if (np
->rxopt
.bits
.rxohlim
) {
1063 int hlim
= np
->mcast_hops
;
1064 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1066 if (np
->rxopt
.bits
.rxflow
) {
1067 __be32 flowinfo
= np
->rcv_flowinfo
;
1069 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1072 len
-= msg
.msg_controllen
;
1073 return put_user(len
, optlen
);
1077 struct dst_entry
*dst
;
1081 dst
= __sk_dst_get(sk
);
1091 val
= sk
->sk_ipv6only
;
1094 case IPV6_RECVPKTINFO
:
1095 val
= np
->rxopt
.bits
.rxinfo
;
1098 case IPV6_2292PKTINFO
:
1099 val
= np
->rxopt
.bits
.rxoinfo
;
1102 case IPV6_RECVHOPLIMIT
:
1103 val
= np
->rxopt
.bits
.rxhlim
;
1106 case IPV6_2292HOPLIMIT
:
1107 val
= np
->rxopt
.bits
.rxohlim
;
1110 case IPV6_RECVRTHDR
:
1111 val
= np
->rxopt
.bits
.srcrt
;
1114 case IPV6_2292RTHDR
:
1115 val
= np
->rxopt
.bits
.osrcrt
;
1119 case IPV6_RTHDRDSTOPTS
:
1123 struct ipv6_txoptions
*opt
;
1126 opt
= rcu_dereference_protected(np
->opt
, sock_owned_by_user(sk
));
1127 len
= ipv6_getsockopt_sticky(sk
, opt
, optname
, optval
, len
);
1129 /* check if ipv6_getsockopt_sticky() returns err code */
1132 return put_user(len
, optlen
);
1135 case IPV6_RECVHOPOPTS
:
1136 val
= np
->rxopt
.bits
.hopopts
;
1139 case IPV6_2292HOPOPTS
:
1140 val
= np
->rxopt
.bits
.ohopopts
;
1143 case IPV6_RECVDSTOPTS
:
1144 val
= np
->rxopt
.bits
.dstopts
;
1147 case IPV6_2292DSTOPTS
:
1148 val
= np
->rxopt
.bits
.odstopts
;
1155 case IPV6_RECVTCLASS
:
1156 val
= np
->rxopt
.bits
.rxtclass
;
1160 val
= np
->rxopt
.bits
.rxflow
;
1163 case IPV6_RECVPATHMTU
:
1164 val
= np
->rxopt
.bits
.rxpmtu
;
1169 struct dst_entry
*dst
;
1170 struct ip6_mtuinfo mtuinfo
;
1172 if (len
< sizeof(mtuinfo
))
1175 len
= sizeof(mtuinfo
);
1176 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1179 dst
= __sk_dst_get(sk
);
1181 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1183 if (!mtuinfo
.ip6m_mtu
)
1186 if (put_user(len
, optlen
))
1188 if (copy_to_user(optval
, &mtuinfo
, len
))
1194 case IPV6_TRANSPARENT
:
1195 val
= inet_sk(sk
)->transparent
;
1198 case IPV6_RECVORIGDSTADDR
:
1199 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1202 case IPV6_UNICAST_HOPS
:
1203 case IPV6_MULTICAST_HOPS
:
1205 struct dst_entry
*dst
;
1207 if (optname
== IPV6_UNICAST_HOPS
)
1208 val
= np
->hop_limit
;
1210 val
= np
->mcast_hops
;
1214 dst
= __sk_dst_get(sk
);
1216 val
= ip6_dst_hoplimit(dst
);
1221 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1225 case IPV6_MULTICAST_LOOP
:
1229 case IPV6_MULTICAST_IF
:
1230 val
= np
->mcast_oif
;
1233 case IPV6_UNICAST_IF
:
1234 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1237 case IPV6_MTU_DISCOVER
:
1245 case IPV6_FLOWINFO_SEND
:
1249 case IPV6_FLOWLABEL_MGR
:
1251 struct in6_flowlabel_req freq
;
1254 if (len
< sizeof(freq
))
1257 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1260 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1264 flags
= freq
.flr_flags
;
1266 memset(&freq
, 0, sizeof(freq
));
1268 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1272 if (put_user(len
, optlen
))
1274 if (copy_to_user(optval
, &freq
, len
))
1280 case IPV6_ADDR_PREFERENCES
:
1283 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1284 val
|= IPV6_PREFER_SRC_TMP
;
1285 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1286 val
|= IPV6_PREFER_SRC_PUBLIC
;
1288 /* XXX: should we return system default? */
1289 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1292 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1293 val
|= IPV6_PREFER_SRC_COA
;
1295 val
|= IPV6_PREFER_SRC_HOME
;
1298 case IPV6_MINHOPCOUNT
:
1299 val
= np
->min_hopcount
;
1306 case IPV6_AUTOFLOWLABEL
:
1307 val
= np
->autoflowlabel
;
1311 return -ENOPROTOOPT
;
1313 len
= min_t(unsigned int, sizeof(int), len
);
1314 if (put_user(len
, optlen
))
1316 if (copy_to_user(optval
, &val
, len
))
1321 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1322 char __user
*optval
, int __user
*optlen
)
1326 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1327 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1329 if (level
!= SOL_IPV6
)
1330 return -ENOPROTOOPT
;
1332 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1333 #ifdef CONFIG_NETFILTER
1334 /* we need to exclude all possible ENOPROTOOPTs except default case */
1335 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1338 if (get_user(len
, optlen
))
1342 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1346 err
= put_user(len
, optlen
);
1351 EXPORT_SYMBOL(ipv6_getsockopt
);
1353 #ifdef CONFIG_COMPAT
1354 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1355 char __user
*optval
, int __user
*optlen
)
1359 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1360 if (udp_prot
.compat_getsockopt
!= NULL
)
1361 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1363 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1366 if (level
!= SOL_IPV6
)
1367 return -ENOPROTOOPT
;
1369 if (optname
== MCAST_MSFILTER
)
1370 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1373 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1375 #ifdef CONFIG_NETFILTER
1376 /* we need to exclude all possible ENOPROTOOPTs except default case */
1377 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1380 if (get_user(len
, optlen
))
1384 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1385 optname
, optval
, &len
);
1388 err
= put_user(len
, optlen
);
1393 EXPORT_SYMBOL(compat_ipv6_getsockopt
);