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(&inet6_sk(sk
)->opt
, 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(&np
->opt
, NULL
);
236 sock_kfree_s(sk
, opt
, opt
->tot_len
);
237 pktopt
= xchg(&np
->pktoptions
, NULL
);
240 sk
->sk_destruct
= inet_sock_destruct
;
242 * ... and add it to the refcnt debug socks count
243 * in the new family. -acme
245 sk_refcnt_debug_inc(sk
);
246 module_put(THIS_MODULE
);
253 if (optlen
< sizeof(int) ||
254 inet_sk(sk
)->inet_num
)
256 sk
->sk_ipv6only
= valbool
;
260 case IPV6_RECVPKTINFO
:
261 if (optlen
< sizeof(int))
263 np
->rxopt
.bits
.rxinfo
= valbool
;
267 case IPV6_2292PKTINFO
:
268 if (optlen
< sizeof(int))
270 np
->rxopt
.bits
.rxoinfo
= valbool
;
274 case IPV6_RECVHOPLIMIT
:
275 if (optlen
< sizeof(int))
277 np
->rxopt
.bits
.rxhlim
= valbool
;
281 case IPV6_2292HOPLIMIT
:
282 if (optlen
< sizeof(int))
284 np
->rxopt
.bits
.rxohlim
= valbool
;
289 if (optlen
< sizeof(int))
291 np
->rxopt
.bits
.srcrt
= valbool
;
296 if (optlen
< sizeof(int))
298 np
->rxopt
.bits
.osrcrt
= valbool
;
302 case IPV6_RECVHOPOPTS
:
303 if (optlen
< sizeof(int))
305 np
->rxopt
.bits
.hopopts
= valbool
;
309 case IPV6_2292HOPOPTS
:
310 if (optlen
< sizeof(int))
312 np
->rxopt
.bits
.ohopopts
= valbool
;
316 case IPV6_RECVDSTOPTS
:
317 if (optlen
< sizeof(int))
319 np
->rxopt
.bits
.dstopts
= valbool
;
323 case IPV6_2292DSTOPTS
:
324 if (optlen
< sizeof(int))
326 np
->rxopt
.bits
.odstopts
= valbool
;
331 if (optlen
< sizeof(int))
333 if (val
< -1 || val
> 0xff)
335 /* RFC 3542, 6.5: default traffic class of 0x0 */
342 case IPV6_RECVTCLASS
:
343 if (optlen
< sizeof(int))
345 np
->rxopt
.bits
.rxtclass
= valbool
;
350 if (optlen
< sizeof(int))
352 np
->rxopt
.bits
.rxflow
= valbool
;
356 case IPV6_RECVPATHMTU
:
357 if (optlen
< sizeof(int))
359 np
->rxopt
.bits
.rxpmtu
= valbool
;
363 case IPV6_TRANSPARENT
:
364 if (valbool
&& !ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
365 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
369 if (optlen
< sizeof(int))
371 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
372 inet_sk(sk
)->transparent
= valbool
;
376 case IPV6_RECVORIGDSTADDR
:
377 if (optlen
< sizeof(int))
379 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
384 case IPV6_RTHDRDSTOPTS
:
388 struct ipv6_txoptions
*opt
;
390 /* remove any sticky options header with a zero option
391 * length, per RFC3542.
397 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
398 optlen
& 0x7 || optlen
> 8 * 255)
401 /* hop-by-hop / destination options are privileged option */
403 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
406 opt
= ipv6_renew_options(sk
, np
->opt
, optname
,
407 (struct ipv6_opt_hdr __user
*)optval
,
414 /* routing header option needs extra check */
416 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
417 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
418 switch (rthdr
->type
) {
419 #if IS_ENABLED(CONFIG_IPV6_MIP6)
420 case IPV6_SRCRT_TYPE_2
:
421 if (rthdr
->hdrlen
!= 2 ||
422 rthdr
->segments_left
!= 1)
433 opt
= ipv6_update_options(sk
, opt
);
436 sock_kfree_s(sk
, opt
, opt
->tot_len
);
442 struct in6_pktinfo pkt
;
446 else if (optlen
< sizeof(struct in6_pktinfo
) || !optval
)
449 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
453 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
456 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
457 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
462 case IPV6_2292PKTOPTIONS
:
464 struct ipv6_txoptions
*opt
= NULL
;
469 memset(&fl6
, 0, sizeof(fl6
));
470 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
471 fl6
.flowi6_mark
= sk
->sk_mark
;
476 /* 1K is probably excessive
477 * 1K is surely not enough, 2K per standard header is 16K.
480 if (optlen
> 64*1024)
483 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
488 memset(opt
, 0, sizeof(*opt
));
489 opt
->tot_len
= sizeof(*opt
) + optlen
;
491 if (copy_from_user(opt
+1, optval
, optlen
))
494 msg
.msg_controllen
= optlen
;
495 msg
.msg_control
= (void *)(opt
+1);
497 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, opt
, &junk
,
503 opt
= ipv6_update_options(sk
, opt
);
506 sock_kfree_s(sk
, opt
, opt
->tot_len
);
509 case IPV6_UNICAST_HOPS
:
510 if (optlen
< sizeof(int))
512 if (val
> 255 || val
< -1)
518 case IPV6_MULTICAST_HOPS
:
519 if (sk
->sk_type
== SOCK_STREAM
)
521 if (optlen
< sizeof(int))
523 if (val
> 255 || val
< -1)
525 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
529 case IPV6_MULTICAST_LOOP
:
530 if (optlen
< sizeof(int))
534 np
->mc_loop
= valbool
;
538 case IPV6_UNICAST_IF
:
540 struct net_device
*dev
= NULL
;
543 if (optlen
!= sizeof(int))
546 ifindex
= (__force
int)ntohl((__force __be32
)val
);
553 dev
= dev_get_by_index(net
, ifindex
);
554 retv
= -EADDRNOTAVAIL
;
560 if (sk
->sk_bound_dev_if
)
563 np
->ucast_oif
= ifindex
;
568 case IPV6_MULTICAST_IF
:
569 if (sk
->sk_type
== SOCK_STREAM
)
571 if (optlen
< sizeof(int))
575 struct net_device
*dev
;
577 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
580 dev
= dev_get_by_index(net
, val
);
590 case IPV6_ADD_MEMBERSHIP
:
591 case IPV6_DROP_MEMBERSHIP
:
593 struct ipv6_mreq mreq
;
595 if (optlen
< sizeof(struct ipv6_mreq
))
599 if (inet_sk(sk
)->is_icsk
)
603 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
606 if (optname
== IPV6_ADD_MEMBERSHIP
)
607 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
609 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
612 case IPV6_JOIN_ANYCAST
:
613 case IPV6_LEAVE_ANYCAST
:
615 struct ipv6_mreq mreq
;
617 if (optlen
< sizeof(struct ipv6_mreq
))
621 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
624 if (optname
== IPV6_JOIN_ANYCAST
)
625 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
627 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
630 case MCAST_JOIN_GROUP
:
631 case MCAST_LEAVE_GROUP
:
633 struct group_req greq
;
634 struct sockaddr_in6
*psin6
;
636 if (optlen
< sizeof(struct group_req
))
640 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
642 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
643 retv
= -EADDRNOTAVAIL
;
646 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
647 if (optname
== MCAST_JOIN_GROUP
)
648 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
651 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
655 case MCAST_JOIN_SOURCE_GROUP
:
656 case MCAST_LEAVE_SOURCE_GROUP
:
657 case MCAST_BLOCK_SOURCE
:
658 case MCAST_UNBLOCK_SOURCE
:
660 struct group_source_req greqs
;
663 if (optlen
< sizeof(struct group_source_req
))
665 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
669 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
670 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
671 retv
= -EADDRNOTAVAIL
;
674 if (optname
== MCAST_BLOCK_SOURCE
) {
675 omode
= MCAST_EXCLUDE
;
677 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
678 omode
= MCAST_EXCLUDE
;
680 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
681 struct sockaddr_in6
*psin6
;
683 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
684 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
686 /* prior join w/ different source is ok */
687 if (retv
&& retv
!= -EADDRINUSE
)
689 omode
= MCAST_INCLUDE
;
691 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
692 omode
= MCAST_INCLUDE
;
695 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
700 struct group_filter
*gsf
;
702 if (optlen
< GROUP_FILTER_SIZE(0))
704 if (optlen
> sysctl_optmem_max
) {
708 gsf
= kmalloc(optlen
, GFP_KERNEL
);
714 if (copy_from_user(gsf
, optval
, optlen
)) {
718 /* numsrc >= (4G-140)/128 overflow in 32 bits */
719 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
720 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
725 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
730 retv
= ip6_mc_msfilter(sk
, gsf
);
735 case IPV6_ROUTER_ALERT
:
736 if (optlen
< sizeof(int))
738 retv
= ip6_ra_control(sk
, val
);
740 case IPV6_MTU_DISCOVER
:
741 if (optlen
< sizeof(int))
743 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
749 if (optlen
< sizeof(int))
751 if (val
&& val
< IPV6_MIN_MTU
)
757 if (optlen
< sizeof(int))
759 np
->recverr
= valbool
;
761 skb_queue_purge(&sk
->sk_error_queue
);
764 case IPV6_FLOWINFO_SEND
:
765 if (optlen
< sizeof(int))
767 np
->sndflow
= valbool
;
770 case IPV6_FLOWLABEL_MGR
:
771 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
773 case IPV6_IPSEC_POLICY
:
774 case IPV6_XFRM_POLICY
:
776 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
778 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
781 case IPV6_ADDR_PREFERENCES
:
783 unsigned int pref
= 0;
784 unsigned int prefmask
= ~0;
786 if (optlen
< sizeof(int))
791 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
792 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
794 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
795 case IPV6_PREFER_SRC_PUBLIC
:
796 pref
|= IPV6_PREFER_SRC_PUBLIC
;
798 case IPV6_PREFER_SRC_TMP
:
799 pref
|= IPV6_PREFER_SRC_TMP
;
801 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
804 goto pref_skip_pubtmp
;
809 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
810 IPV6_PREFER_SRC_TMP
);
813 /* check HOME/COA conflicts */
814 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
815 case IPV6_PREFER_SRC_HOME
:
817 case IPV6_PREFER_SRC_COA
:
818 pref
|= IPV6_PREFER_SRC_COA
;
825 prefmask
&= ~IPV6_PREFER_SRC_COA
;
828 /* check CGA/NONCGA conflicts */
829 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
830 case IPV6_PREFER_SRC_CGA
:
831 case IPV6_PREFER_SRC_NONCGA
:
838 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
843 case IPV6_MINHOPCOUNT
:
844 if (optlen
< sizeof(int))
846 if (val
< 0 || val
> 255)
848 np
->min_hopcount
= val
;
852 np
->dontfrag
= valbool
;
855 case IPV6_AUTOFLOWLABEL
:
856 np
->autoflowlabel
= valbool
;
874 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
875 char __user
*optval
, unsigned int optlen
)
879 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
880 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
882 if (level
!= SOL_IPV6
)
885 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
886 #ifdef CONFIG_NETFILTER
887 /* we need to exclude all possible ENOPROTOOPTs except default case */
888 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
889 optname
!= IPV6_XFRM_POLICY
) {
891 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
898 EXPORT_SYMBOL(ipv6_setsockopt
);
901 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
902 char __user
*optval
, unsigned int optlen
)
906 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
907 if (udp_prot
.compat_setsockopt
!= NULL
)
908 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
910 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
913 if (level
!= SOL_IPV6
)
916 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
917 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
920 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
921 #ifdef CONFIG_NETFILTER
922 /* we need to exclude all possible ENOPROTOOPTs except default case */
923 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
924 optname
!= IPV6_XFRM_POLICY
) {
926 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
933 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
936 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
937 int optname
, char __user
*optval
, int len
)
939 struct ipv6_opt_hdr
*hdr
;
948 case IPV6_RTHDRDSTOPTS
:
952 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
958 return -EINVAL
; /* should not happen */
964 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
965 if (copy_to_user(optval
, hdr
, len
))
970 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
971 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
973 struct ipv6_pinfo
*np
= inet6_sk(sk
);
977 if (ip6_mroute_opt(optname
))
978 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
980 if (get_user(len
, optlen
))
984 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
985 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
986 sk
->sk_protocol
!= IPPROTO_TCP
)
988 if (sk
->sk_state
!= TCP_ESTABLISHED
)
994 struct group_filter gsf
;
997 if (len
< GROUP_FILTER_SIZE(0))
999 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
1001 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1002 return -EADDRNOTAVAIL
;
1004 err
= ip6_mc_msfget(sk
, &gsf
,
1005 (struct group_filter __user
*)optval
, optlen
);
1010 case IPV6_2292PKTOPTIONS
:
1013 struct sk_buff
*skb
;
1015 if (sk
->sk_type
!= SOCK_STREAM
)
1016 return -ENOPROTOOPT
;
1018 msg
.msg_control
= optval
;
1019 msg
.msg_controllen
= len
;
1020 msg
.msg_flags
= flags
;
1023 skb
= np
->pktoptions
;
1025 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1028 if (np
->rxopt
.bits
.rxinfo
) {
1029 struct in6_pktinfo src_info
;
1030 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1031 np
->sticky_pktinfo
.ipi6_ifindex
;
1032 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1033 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1035 if (np
->rxopt
.bits
.rxhlim
) {
1036 int hlim
= np
->mcast_hops
;
1037 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1039 if (np
->rxopt
.bits
.rxtclass
) {
1040 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1042 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1044 if (np
->rxopt
.bits
.rxoinfo
) {
1045 struct in6_pktinfo src_info
;
1046 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1047 np
->sticky_pktinfo
.ipi6_ifindex
;
1048 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1049 np
->sticky_pktinfo
.ipi6_addr
;
1050 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1052 if (np
->rxopt
.bits
.rxohlim
) {
1053 int hlim
= np
->mcast_hops
;
1054 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1056 if (np
->rxopt
.bits
.rxflow
) {
1057 __be32 flowinfo
= np
->rcv_flowinfo
;
1059 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1062 len
-= msg
.msg_controllen
;
1063 return put_user(len
, optlen
);
1067 struct dst_entry
*dst
;
1071 dst
= __sk_dst_get(sk
);
1081 val
= sk
->sk_ipv6only
;
1084 case IPV6_RECVPKTINFO
:
1085 val
= np
->rxopt
.bits
.rxinfo
;
1088 case IPV6_2292PKTINFO
:
1089 val
= np
->rxopt
.bits
.rxoinfo
;
1092 case IPV6_RECVHOPLIMIT
:
1093 val
= np
->rxopt
.bits
.rxhlim
;
1096 case IPV6_2292HOPLIMIT
:
1097 val
= np
->rxopt
.bits
.rxohlim
;
1100 case IPV6_RECVRTHDR
:
1101 val
= np
->rxopt
.bits
.srcrt
;
1104 case IPV6_2292RTHDR
:
1105 val
= np
->rxopt
.bits
.osrcrt
;
1109 case IPV6_RTHDRDSTOPTS
:
1115 len
= ipv6_getsockopt_sticky(sk
, np
->opt
,
1116 optname
, optval
, len
);
1118 /* check if ipv6_getsockopt_sticky() returns err code */
1121 return put_user(len
, optlen
);
1124 case IPV6_RECVHOPOPTS
:
1125 val
= np
->rxopt
.bits
.hopopts
;
1128 case IPV6_2292HOPOPTS
:
1129 val
= np
->rxopt
.bits
.ohopopts
;
1132 case IPV6_RECVDSTOPTS
:
1133 val
= np
->rxopt
.bits
.dstopts
;
1136 case IPV6_2292DSTOPTS
:
1137 val
= np
->rxopt
.bits
.odstopts
;
1144 case IPV6_RECVTCLASS
:
1145 val
= np
->rxopt
.bits
.rxtclass
;
1149 val
= np
->rxopt
.bits
.rxflow
;
1152 case IPV6_RECVPATHMTU
:
1153 val
= np
->rxopt
.bits
.rxpmtu
;
1158 struct dst_entry
*dst
;
1159 struct ip6_mtuinfo mtuinfo
;
1161 if (len
< sizeof(mtuinfo
))
1164 len
= sizeof(mtuinfo
);
1165 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1168 dst
= __sk_dst_get(sk
);
1170 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1172 if (!mtuinfo
.ip6m_mtu
)
1175 if (put_user(len
, optlen
))
1177 if (copy_to_user(optval
, &mtuinfo
, len
))
1183 case IPV6_TRANSPARENT
:
1184 val
= inet_sk(sk
)->transparent
;
1187 case IPV6_RECVORIGDSTADDR
:
1188 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1191 case IPV6_UNICAST_HOPS
:
1192 case IPV6_MULTICAST_HOPS
:
1194 struct dst_entry
*dst
;
1196 if (optname
== IPV6_UNICAST_HOPS
)
1197 val
= np
->hop_limit
;
1199 val
= np
->mcast_hops
;
1203 dst
= __sk_dst_get(sk
);
1205 val
= ip6_dst_hoplimit(dst
);
1210 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1214 case IPV6_MULTICAST_LOOP
:
1218 case IPV6_MULTICAST_IF
:
1219 val
= np
->mcast_oif
;
1222 case IPV6_UNICAST_IF
:
1223 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1226 case IPV6_MTU_DISCOVER
:
1234 case IPV6_FLOWINFO_SEND
:
1238 case IPV6_FLOWLABEL_MGR
:
1240 struct in6_flowlabel_req freq
;
1243 if (len
< sizeof(freq
))
1246 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1249 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1253 flags
= freq
.flr_flags
;
1255 memset(&freq
, 0, sizeof(freq
));
1257 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1261 if (put_user(len
, optlen
))
1263 if (copy_to_user(optval
, &freq
, len
))
1269 case IPV6_ADDR_PREFERENCES
:
1272 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1273 val
|= IPV6_PREFER_SRC_TMP
;
1274 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1275 val
|= IPV6_PREFER_SRC_PUBLIC
;
1277 /* XXX: should we return system default? */
1278 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1281 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1282 val
|= IPV6_PREFER_SRC_COA
;
1284 val
|= IPV6_PREFER_SRC_HOME
;
1287 case IPV6_MINHOPCOUNT
:
1288 val
= np
->min_hopcount
;
1295 case IPV6_AUTOFLOWLABEL
:
1296 val
= np
->autoflowlabel
;
1300 return -ENOPROTOOPT
;
1302 len
= min_t(unsigned int, sizeof(int), len
);
1303 if (put_user(len
, optlen
))
1305 if (copy_to_user(optval
, &val
, len
))
1310 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1311 char __user
*optval
, int __user
*optlen
)
1315 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1316 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1318 if (level
!= SOL_IPV6
)
1319 return -ENOPROTOOPT
;
1321 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1322 #ifdef CONFIG_NETFILTER
1323 /* we need to exclude all possible ENOPROTOOPTs except default case */
1324 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1327 if (get_user(len
, optlen
))
1331 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1335 err
= put_user(len
, optlen
);
1340 EXPORT_SYMBOL(ipv6_getsockopt
);
1342 #ifdef CONFIG_COMPAT
1343 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1344 char __user
*optval
, int __user
*optlen
)
1348 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1349 if (udp_prot
.compat_getsockopt
!= NULL
)
1350 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1352 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1355 if (level
!= SOL_IPV6
)
1356 return -ENOPROTOOPT
;
1358 if (optname
== MCAST_MSFILTER
)
1359 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1362 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1364 #ifdef CONFIG_NETFILTER
1365 /* we need to exclude all possible ENOPROTOOPTs except default case */
1366 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1369 if (get_user(len
, optlen
))
1373 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1374 optname
, optval
, &len
);
1377 err
= put_user(len
, optlen
);
1382 EXPORT_SYMBOL(compat_ipv6_getsockopt
);