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
) {
188 } else if (sk
->sk_protocol
!= IPPROTO_TCP
)
191 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
196 if (ipv6_only_sock(sk
) ||
197 !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
198 retv
= -EADDRNOTAVAIL
;
202 fl6_free_socklist(sk
);
203 __ipv6_sock_mc_close(sk
);
206 * Sock is moving from IPv6 to IPv4 (sk_prot), so
207 * remove it from the refcnt debug socks count in the
210 sk_refcnt_debug_dec(sk
);
212 if (sk
->sk_protocol
== IPPROTO_TCP
) {
213 struct inet_connection_sock
*icsk
= inet_csk(sk
);
215 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
216 sock_prot_inuse_add(net
, &tcp_prot
, 1);
218 sk
->sk_prot
= &tcp_prot
;
219 icsk
->icsk_af_ops
= &ipv4_specific
;
220 sk
->sk_socket
->ops
= &inet_stream_ops
;
221 sk
->sk_family
= PF_INET
;
222 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
224 struct proto
*prot
= &udp_prot
;
226 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
227 prot
= &udplite_prot
;
229 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
230 sock_prot_inuse_add(net
, prot
, 1);
233 sk
->sk_socket
->ops
= &inet_dgram_ops
;
234 sk
->sk_family
= PF_INET
;
236 opt
= xchg((__force
struct ipv6_txoptions
**)&np
->opt
,
239 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
242 pktopt
= xchg(&np
->pktoptions
, NULL
);
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
;
381 if (optlen
< sizeof(int))
383 /* we also don't have a separate freebind bit for IPV6 */
384 inet_sk(sk
)->freebind
= valbool
;
388 case IPV6_RECVORIGDSTADDR
:
389 if (optlen
< sizeof(int))
391 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
396 case IPV6_RTHDRDSTOPTS
:
400 struct ipv6_txoptions
*opt
;
401 struct ipv6_opt_hdr
*new = NULL
;
403 /* hop-by-hop / destination options are privileged option */
405 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
408 /* remove any sticky options header with a zero option
409 * length, per RFC3542.
415 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
416 optlen
& 0x7 || optlen
> 8 * 255)
419 new = memdup_user(optval
, optlen
);
424 if (unlikely(ipv6_optlen(new) > optlen
)) {
430 opt
= rcu_dereference_protected(np
->opt
,
431 lockdep_sock_is_held(sk
));
432 opt
= ipv6_renew_options(sk
, opt
, optname
, new);
439 /* routing header option needs extra check */
441 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
442 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
443 switch (rthdr
->type
) {
444 #if IS_ENABLED(CONFIG_IPV6_MIP6)
445 case IPV6_SRCRT_TYPE_2
:
446 if (rthdr
->hdrlen
!= 2 ||
447 rthdr
->segments_left
!= 1)
452 case IPV6_SRCRT_TYPE_4
:
454 struct ipv6_sr_hdr
*srh
= (struct ipv6_sr_hdr
*)
457 if (!seg6_validate_srh(srh
, optlen
))
467 opt
= ipv6_update_options(sk
, opt
);
470 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
478 struct in6_pktinfo pkt
;
482 else if (optlen
< sizeof(struct in6_pktinfo
) || !optval
)
485 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
489 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
492 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
493 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
498 case IPV6_2292PKTOPTIONS
:
500 struct ipv6_txoptions
*opt
= NULL
;
503 struct ipcm6_cookie ipc6
;
505 memset(&fl6
, 0, sizeof(fl6
));
506 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
507 fl6
.flowi6_mark
= sk
->sk_mark
;
512 /* 1K is probably excessive
513 * 1K is surely not enough, 2K per standard header is 16K.
516 if (optlen
> 64*1024)
519 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
524 memset(opt
, 0, sizeof(*opt
));
525 refcount_set(&opt
->refcnt
, 1);
526 opt
->tot_len
= sizeof(*opt
) + optlen
;
528 if (copy_from_user(opt
+1, optval
, optlen
))
531 msg
.msg_controllen
= optlen
;
532 msg
.msg_control
= (void *)(opt
+1);
535 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, &ipc6
);
540 opt
= ipv6_update_options(sk
, opt
);
543 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
548 case IPV6_UNICAST_HOPS
:
549 if (optlen
< sizeof(int))
551 if (val
> 255 || val
< -1)
557 case IPV6_MULTICAST_HOPS
:
558 if (sk
->sk_type
== SOCK_STREAM
)
560 if (optlen
< sizeof(int))
562 if (val
> 255 || val
< -1)
564 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
568 case IPV6_MULTICAST_LOOP
:
569 if (optlen
< sizeof(int))
573 np
->mc_loop
= valbool
;
577 case IPV6_UNICAST_IF
:
579 struct net_device
*dev
= NULL
;
582 if (optlen
!= sizeof(int))
585 ifindex
= (__force
int)ntohl((__force __be32
)val
);
592 dev
= dev_get_by_index(net
, ifindex
);
593 retv
= -EADDRNOTAVAIL
;
599 if (sk
->sk_bound_dev_if
)
602 np
->ucast_oif
= ifindex
;
607 case IPV6_MULTICAST_IF
:
608 if (sk
->sk_type
== SOCK_STREAM
)
610 if (optlen
< sizeof(int))
614 struct net_device
*dev
;
619 dev
= dev_get_by_index_rcu(net
, val
);
625 midx
= l3mdev_master_ifindex_rcu(dev
);
629 if (sk
->sk_bound_dev_if
&&
630 sk
->sk_bound_dev_if
!= val
&&
631 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
637 case IPV6_ADD_MEMBERSHIP
:
638 case IPV6_DROP_MEMBERSHIP
:
640 struct ipv6_mreq mreq
;
642 if (optlen
< sizeof(struct ipv6_mreq
))
646 if (inet_sk(sk
)->is_icsk
)
650 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
653 if (optname
== IPV6_ADD_MEMBERSHIP
)
654 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
656 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
659 case IPV6_JOIN_ANYCAST
:
660 case IPV6_LEAVE_ANYCAST
:
662 struct ipv6_mreq mreq
;
664 if (optlen
< sizeof(struct ipv6_mreq
))
668 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
671 if (optname
== IPV6_JOIN_ANYCAST
)
672 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
674 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
677 case IPV6_MULTICAST_ALL
:
678 if (optlen
< sizeof(int))
680 np
->mc_all
= valbool
;
684 case MCAST_JOIN_GROUP
:
685 case MCAST_LEAVE_GROUP
:
687 struct group_req greq
;
688 struct sockaddr_in6
*psin6
;
690 if (optlen
< sizeof(struct group_req
))
694 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
696 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
697 retv
= -EADDRNOTAVAIL
;
700 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
701 if (optname
== MCAST_JOIN_GROUP
)
702 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
705 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
709 case MCAST_JOIN_SOURCE_GROUP
:
710 case MCAST_LEAVE_SOURCE_GROUP
:
711 case MCAST_BLOCK_SOURCE
:
712 case MCAST_UNBLOCK_SOURCE
:
714 struct group_source_req greqs
;
717 if (optlen
< sizeof(struct group_source_req
))
719 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
723 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
724 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
725 retv
= -EADDRNOTAVAIL
;
728 if (optname
== MCAST_BLOCK_SOURCE
) {
729 omode
= MCAST_EXCLUDE
;
731 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
732 omode
= MCAST_EXCLUDE
;
734 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
735 struct sockaddr_in6
*psin6
;
737 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
738 retv
= ipv6_sock_mc_join_ssm(sk
, greqs
.gsr_interface
,
741 /* prior join w/ different source is ok */
742 if (retv
&& retv
!= -EADDRINUSE
)
744 omode
= MCAST_INCLUDE
;
746 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
747 omode
= MCAST_INCLUDE
;
750 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
755 struct group_filter
*gsf
;
757 if (optlen
< GROUP_FILTER_SIZE(0))
759 if (optlen
> sysctl_optmem_max
) {
763 gsf
= memdup_user(optval
, optlen
);
768 /* numsrc >= (4G-140)/128 overflow in 32 bits */
769 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
770 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
775 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
780 retv
= ip6_mc_msfilter(sk
, gsf
);
785 case IPV6_ROUTER_ALERT
:
786 if (optlen
< sizeof(int))
788 retv
= ip6_ra_control(sk
, val
);
790 case IPV6_MTU_DISCOVER
:
791 if (optlen
< sizeof(int))
793 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
799 if (optlen
< sizeof(int))
801 if (val
&& val
< IPV6_MIN_MTU
)
807 if (optlen
< sizeof(int))
809 np
->recverr
= valbool
;
811 skb_queue_purge(&sk
->sk_error_queue
);
814 case IPV6_FLOWINFO_SEND
:
815 if (optlen
< sizeof(int))
817 np
->sndflow
= valbool
;
820 case IPV6_FLOWLABEL_MGR
:
821 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
823 case IPV6_IPSEC_POLICY
:
824 case IPV6_XFRM_POLICY
:
826 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
828 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
831 case IPV6_ADDR_PREFERENCES
:
833 unsigned int pref
= 0;
834 unsigned int prefmask
= ~0;
836 if (optlen
< sizeof(int))
841 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
842 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
844 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
845 case IPV6_PREFER_SRC_PUBLIC
:
846 pref
|= IPV6_PREFER_SRC_PUBLIC
;
848 case IPV6_PREFER_SRC_TMP
:
849 pref
|= IPV6_PREFER_SRC_TMP
;
851 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
854 goto pref_skip_pubtmp
;
859 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
860 IPV6_PREFER_SRC_TMP
);
863 /* check HOME/COA conflicts */
864 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
865 case IPV6_PREFER_SRC_HOME
:
867 case IPV6_PREFER_SRC_COA
:
868 pref
|= IPV6_PREFER_SRC_COA
;
875 prefmask
&= ~IPV6_PREFER_SRC_COA
;
878 /* check CGA/NONCGA conflicts */
879 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
880 case IPV6_PREFER_SRC_CGA
:
881 case IPV6_PREFER_SRC_NONCGA
:
888 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
893 case IPV6_MINHOPCOUNT
:
894 if (optlen
< sizeof(int))
896 if (val
< 0 || val
> 255)
898 np
->min_hopcount
= val
;
902 np
->dontfrag
= valbool
;
905 case IPV6_AUTOFLOWLABEL
:
906 np
->autoflowlabel
= valbool
;
907 np
->autoflowlabel_set
= 1;
910 case IPV6_RECVFRAGSIZE
:
911 np
->rxopt
.bits
.recvfragsize
= valbool
;
929 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
930 char __user
*optval
, unsigned int optlen
)
934 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
935 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
937 if (level
!= SOL_IPV6
)
940 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
941 #ifdef CONFIG_NETFILTER
942 /* we need to exclude all possible ENOPROTOOPTs except default case */
943 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
944 optname
!= IPV6_XFRM_POLICY
)
945 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
, optlen
);
949 EXPORT_SYMBOL(ipv6_setsockopt
);
952 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
953 char __user
*optval
, unsigned int optlen
)
957 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
958 if (udp_prot
.compat_setsockopt
!= NULL
)
959 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
961 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
964 if (level
!= SOL_IPV6
)
967 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
968 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
971 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
972 #ifdef CONFIG_NETFILTER
973 /* we need to exclude all possible ENOPROTOOPTs except default case */
974 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
975 optname
!= IPV6_XFRM_POLICY
)
976 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
981 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
984 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
985 int optname
, char __user
*optval
, int len
)
987 struct ipv6_opt_hdr
*hdr
;
996 case IPV6_RTHDRDSTOPTS
:
1000 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
1006 return -EINVAL
; /* should not happen */
1012 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
1013 if (copy_to_user(optval
, hdr
, len
))
1018 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1019 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1021 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1025 if (ip6_mroute_opt(optname
))
1026 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1028 if (get_user(len
, optlen
))
1032 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
1033 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
1034 sk
->sk_protocol
!= IPPROTO_TCP
)
1035 return -ENOPROTOOPT
;
1036 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1038 val
= sk
->sk_family
;
1040 case MCAST_MSFILTER
:
1042 struct group_filter gsf
;
1045 if (len
< GROUP_FILTER_SIZE(0))
1047 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
1049 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1050 return -EADDRNOTAVAIL
;
1052 err
= ip6_mc_msfget(sk
, &gsf
,
1053 (struct group_filter __user
*)optval
, optlen
);
1058 case IPV6_2292PKTOPTIONS
:
1061 struct sk_buff
*skb
;
1063 if (sk
->sk_type
!= SOCK_STREAM
)
1064 return -ENOPROTOOPT
;
1066 msg
.msg_control
= optval
;
1067 msg
.msg_controllen
= len
;
1068 msg
.msg_flags
= flags
;
1071 skb
= np
->pktoptions
;
1073 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1076 if (np
->rxopt
.bits
.rxinfo
) {
1077 struct in6_pktinfo src_info
;
1078 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1079 np
->sticky_pktinfo
.ipi6_ifindex
;
1080 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1081 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1083 if (np
->rxopt
.bits
.rxhlim
) {
1084 int hlim
= np
->mcast_hops
;
1085 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1087 if (np
->rxopt
.bits
.rxtclass
) {
1088 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1090 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1092 if (np
->rxopt
.bits
.rxoinfo
) {
1093 struct in6_pktinfo src_info
;
1094 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1095 np
->sticky_pktinfo
.ipi6_ifindex
;
1096 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1097 np
->sticky_pktinfo
.ipi6_addr
;
1098 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1100 if (np
->rxopt
.bits
.rxohlim
) {
1101 int hlim
= np
->mcast_hops
;
1102 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1104 if (np
->rxopt
.bits
.rxflow
) {
1105 __be32 flowinfo
= np
->rcv_flowinfo
;
1107 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1110 len
-= msg
.msg_controllen
;
1111 return put_user(len
, optlen
);
1115 struct dst_entry
*dst
;
1119 dst
= __sk_dst_get(sk
);
1129 val
= sk
->sk_ipv6only
;
1132 case IPV6_RECVPKTINFO
:
1133 val
= np
->rxopt
.bits
.rxinfo
;
1136 case IPV6_2292PKTINFO
:
1137 val
= np
->rxopt
.bits
.rxoinfo
;
1140 case IPV6_RECVHOPLIMIT
:
1141 val
= np
->rxopt
.bits
.rxhlim
;
1144 case IPV6_2292HOPLIMIT
:
1145 val
= np
->rxopt
.bits
.rxohlim
;
1148 case IPV6_RECVRTHDR
:
1149 val
= np
->rxopt
.bits
.srcrt
;
1152 case IPV6_2292RTHDR
:
1153 val
= np
->rxopt
.bits
.osrcrt
;
1157 case IPV6_RTHDRDSTOPTS
:
1161 struct ipv6_txoptions
*opt
;
1164 opt
= rcu_dereference_protected(np
->opt
,
1165 lockdep_sock_is_held(sk
));
1166 len
= ipv6_getsockopt_sticky(sk
, opt
, optname
, optval
, len
);
1168 /* check if ipv6_getsockopt_sticky() returns err code */
1171 return put_user(len
, optlen
);
1174 case IPV6_RECVHOPOPTS
:
1175 val
= np
->rxopt
.bits
.hopopts
;
1178 case IPV6_2292HOPOPTS
:
1179 val
= np
->rxopt
.bits
.ohopopts
;
1182 case IPV6_RECVDSTOPTS
:
1183 val
= np
->rxopt
.bits
.dstopts
;
1186 case IPV6_2292DSTOPTS
:
1187 val
= np
->rxopt
.bits
.odstopts
;
1194 case IPV6_RECVTCLASS
:
1195 val
= np
->rxopt
.bits
.rxtclass
;
1199 val
= np
->rxopt
.bits
.rxflow
;
1202 case IPV6_RECVPATHMTU
:
1203 val
= np
->rxopt
.bits
.rxpmtu
;
1208 struct dst_entry
*dst
;
1209 struct ip6_mtuinfo mtuinfo
;
1211 if (len
< sizeof(mtuinfo
))
1214 len
= sizeof(mtuinfo
);
1215 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1218 dst
= __sk_dst_get(sk
);
1220 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1222 if (!mtuinfo
.ip6m_mtu
)
1225 if (put_user(len
, optlen
))
1227 if (copy_to_user(optval
, &mtuinfo
, len
))
1233 case IPV6_TRANSPARENT
:
1234 val
= inet_sk(sk
)->transparent
;
1238 val
= inet_sk(sk
)->freebind
;
1241 case IPV6_RECVORIGDSTADDR
:
1242 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1245 case IPV6_UNICAST_HOPS
:
1246 case IPV6_MULTICAST_HOPS
:
1248 struct dst_entry
*dst
;
1250 if (optname
== IPV6_UNICAST_HOPS
)
1251 val
= np
->hop_limit
;
1253 val
= np
->mcast_hops
;
1257 dst
= __sk_dst_get(sk
);
1259 val
= ip6_dst_hoplimit(dst
);
1264 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1268 case IPV6_MULTICAST_LOOP
:
1272 case IPV6_MULTICAST_IF
:
1273 val
= np
->mcast_oif
;
1276 case IPV6_MULTICAST_ALL
:
1280 case IPV6_UNICAST_IF
:
1281 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1284 case IPV6_MTU_DISCOVER
:
1292 case IPV6_FLOWINFO_SEND
:
1296 case IPV6_FLOWLABEL_MGR
:
1298 struct in6_flowlabel_req freq
;
1301 if (len
< sizeof(freq
))
1304 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1307 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1311 flags
= freq
.flr_flags
;
1313 memset(&freq
, 0, sizeof(freq
));
1315 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1319 if (put_user(len
, optlen
))
1321 if (copy_to_user(optval
, &freq
, len
))
1327 case IPV6_ADDR_PREFERENCES
:
1330 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1331 val
|= IPV6_PREFER_SRC_TMP
;
1332 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1333 val
|= IPV6_PREFER_SRC_PUBLIC
;
1335 /* XXX: should we return system default? */
1336 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1339 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1340 val
|= IPV6_PREFER_SRC_COA
;
1342 val
|= IPV6_PREFER_SRC_HOME
;
1345 case IPV6_MINHOPCOUNT
:
1346 val
= np
->min_hopcount
;
1353 case IPV6_AUTOFLOWLABEL
:
1354 val
= ip6_autoflowlabel(sock_net(sk
), np
);
1357 case IPV6_RECVFRAGSIZE
:
1358 val
= np
->rxopt
.bits
.recvfragsize
;
1362 return -ENOPROTOOPT
;
1364 len
= min_t(unsigned int, sizeof(int), len
);
1365 if (put_user(len
, optlen
))
1367 if (copy_to_user(optval
, &val
, len
))
1372 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1373 char __user
*optval
, int __user
*optlen
)
1377 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1378 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1380 if (level
!= SOL_IPV6
)
1381 return -ENOPROTOOPT
;
1383 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1384 #ifdef CONFIG_NETFILTER
1385 /* we need to exclude all possible ENOPROTOOPTs except default case */
1386 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1389 if (get_user(len
, optlen
))
1392 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1394 err
= put_user(len
, optlen
);
1399 EXPORT_SYMBOL(ipv6_getsockopt
);
1401 #ifdef CONFIG_COMPAT
1402 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1403 char __user
*optval
, int __user
*optlen
)
1407 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1408 if (udp_prot
.compat_getsockopt
!= NULL
)
1409 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1411 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1414 if (level
!= SOL_IPV6
)
1415 return -ENOPROTOOPT
;
1417 if (optname
== MCAST_MSFILTER
)
1418 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1421 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1423 #ifdef CONFIG_NETFILTER
1424 /* we need to exclude all possible ENOPROTOOPTs except default case */
1425 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1428 if (get_user(len
, optlen
))
1431 err
= compat_nf_getsockopt(sk
, PF_INET6
, optname
, optval
, &len
);
1433 err
= put_user(len
, optlen
);
1438 EXPORT_SYMBOL(compat_ipv6_getsockopt
);