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
);
113 opt
= xchg(&inet6_sk(sk
)->opt
, opt
);
115 spin_lock(&sk
->sk_dst_lock
);
116 opt
= xchg(&inet6_sk(sk
)->opt
, opt
);
117 spin_unlock(&sk
->sk_dst_lock
);
124 static int do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
125 char __user
*optval
, unsigned int optlen
)
127 struct ipv6_pinfo
*np
= inet6_sk(sk
);
128 struct net
*net
= sock_net(sk
);
130 int retv
= -ENOPROTOOPT
;
135 if (optlen
>= sizeof(int)) {
136 if (get_user(val
, (int __user
*) optval
))
144 if (ip6_mroute_opt(optname
))
145 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
152 if (optlen
< sizeof(int))
154 if (val
== PF_INET
) {
155 struct ipv6_txoptions
*opt
;
156 struct sk_buff
*pktopt
;
158 if (sk
->sk_type
== SOCK_RAW
)
161 if (sk
->sk_protocol
== IPPROTO_UDP
||
162 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
163 struct udp_sock
*up
= udp_sk(sk
);
164 if (up
->pending
== AF_INET6
) {
168 } else if (sk
->sk_protocol
!= IPPROTO_TCP
)
171 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
176 if (ipv6_only_sock(sk
) ||
177 !ipv6_addr_v4mapped(&np
->daddr
)) {
178 retv
= -EADDRNOTAVAIL
;
182 fl6_free_socklist(sk
);
183 ipv6_sock_mc_close(sk
);
186 * Sock is moving from IPv6 to IPv4 (sk_prot), so
187 * remove it from the refcnt debug socks count in the
190 sk_refcnt_debug_dec(sk
);
192 if (sk
->sk_protocol
== IPPROTO_TCP
) {
193 struct inet_connection_sock
*icsk
= inet_csk(sk
);
195 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
196 sock_prot_inuse_add(net
, &tcp_prot
, 1);
198 sk
->sk_prot
= &tcp_prot
;
199 icsk
->icsk_af_ops
= &ipv4_specific
;
200 sk
->sk_socket
->ops
= &inet_stream_ops
;
201 sk
->sk_family
= PF_INET
;
202 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
204 struct proto
*prot
= &udp_prot
;
206 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
207 prot
= &udplite_prot
;
209 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
210 sock_prot_inuse_add(net
, prot
, 1);
213 sk
->sk_socket
->ops
= &inet_dgram_ops
;
214 sk
->sk_family
= PF_INET
;
216 opt
= xchg(&np
->opt
, NULL
);
218 sock_kfree_s(sk
, opt
, opt
->tot_len
);
219 pktopt
= xchg(&np
->pktoptions
, NULL
);
222 sk
->sk_destruct
= inet_sock_destruct
;
224 * ... and add it to the refcnt debug socks count
225 * in the new family. -acme
227 sk_refcnt_debug_inc(sk
);
228 module_put(THIS_MODULE
);
235 if (optlen
< sizeof(int) ||
236 inet_sk(sk
)->inet_num
)
238 np
->ipv6only
= valbool
;
242 case IPV6_RECVPKTINFO
:
243 if (optlen
< sizeof(int))
245 np
->rxopt
.bits
.rxinfo
= valbool
;
249 case IPV6_2292PKTINFO
:
250 if (optlen
< sizeof(int))
252 np
->rxopt
.bits
.rxoinfo
= valbool
;
256 case IPV6_RECVHOPLIMIT
:
257 if (optlen
< sizeof(int))
259 np
->rxopt
.bits
.rxhlim
= valbool
;
263 case IPV6_2292HOPLIMIT
:
264 if (optlen
< sizeof(int))
266 np
->rxopt
.bits
.rxohlim
= valbool
;
271 if (optlen
< sizeof(int))
273 np
->rxopt
.bits
.srcrt
= valbool
;
278 if (optlen
< sizeof(int))
280 np
->rxopt
.bits
.osrcrt
= valbool
;
284 case IPV6_RECVHOPOPTS
:
285 if (optlen
< sizeof(int))
287 np
->rxopt
.bits
.hopopts
= valbool
;
291 case IPV6_2292HOPOPTS
:
292 if (optlen
< sizeof(int))
294 np
->rxopt
.bits
.ohopopts
= valbool
;
298 case IPV6_RECVDSTOPTS
:
299 if (optlen
< sizeof(int))
301 np
->rxopt
.bits
.dstopts
= valbool
;
305 case IPV6_2292DSTOPTS
:
306 if (optlen
< sizeof(int))
308 np
->rxopt
.bits
.odstopts
= valbool
;
313 if (optlen
< sizeof(int))
315 if (val
< -1 || val
> 0xff)
317 /* RFC 3542, 6.5: default traffic class of 0x0 */
324 case IPV6_RECVTCLASS
:
325 if (optlen
< sizeof(int))
327 np
->rxopt
.bits
.rxtclass
= valbool
;
332 if (optlen
< sizeof(int))
334 np
->rxopt
.bits
.rxflow
= valbool
;
338 case IPV6_RECVPATHMTU
:
339 if (optlen
< sizeof(int))
341 np
->rxopt
.bits
.rxpmtu
= valbool
;
346 case IPV6_RTHDRDSTOPTS
:
350 struct ipv6_txoptions
*opt
;
352 /* remove any sticky options header with a zero option
353 * length, per RFC3542.
357 else if (optval
== NULL
)
359 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
360 optlen
& 0x7 || optlen
> 8 * 255)
363 /* hop-by-hop / destination options are privileged option */
365 if (optname
!= IPV6_RTHDR
&& !capable(CAP_NET_RAW
))
368 opt
= ipv6_renew_options(sk
, np
->opt
, optname
,
369 (struct ipv6_opt_hdr __user
*)optval
,
376 /* routing header option needs extra check */
378 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
379 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
380 switch (rthdr
->type
) {
381 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
382 case IPV6_SRCRT_TYPE_2
:
383 if (rthdr
->hdrlen
!= 2 ||
384 rthdr
->segments_left
!= 1)
395 opt
= ipv6_update_options(sk
, opt
);
398 sock_kfree_s(sk
, opt
, opt
->tot_len
);
404 struct in6_pktinfo pkt
;
408 else if (optlen
< sizeof(struct in6_pktinfo
) || optval
== NULL
)
411 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
415 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
418 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
419 ipv6_addr_copy(&np
->sticky_pktinfo
.ipi6_addr
, &pkt
.ipi6_addr
);
424 case IPV6_2292PKTOPTIONS
:
426 struct ipv6_txoptions
*opt
= NULL
;
431 fl
.fl6_flowlabel
= 0;
432 fl
.oif
= sk
->sk_bound_dev_if
;
433 fl
.mark
= sk
->sk_mark
;
438 /* 1K is probably excessive
439 * 1K is surely not enough, 2K per standard header is 16K.
442 if (optlen
> 64*1024)
445 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
450 memset(opt
, 0, sizeof(*opt
));
451 opt
->tot_len
= sizeof(*opt
) + optlen
;
453 if (copy_from_user(opt
+1, optval
, optlen
))
456 msg
.msg_controllen
= optlen
;
457 msg
.msg_control
= (void*)(opt
+1);
459 retv
= datagram_send_ctl(net
, &msg
, &fl
, opt
, &junk
, &junk
,
465 opt
= ipv6_update_options(sk
, opt
);
468 sock_kfree_s(sk
, opt
, opt
->tot_len
);
471 case IPV6_UNICAST_HOPS
:
472 if (optlen
< sizeof(int))
474 if (val
> 255 || val
< -1)
480 case IPV6_MULTICAST_HOPS
:
481 if (sk
->sk_type
== SOCK_STREAM
)
483 if (optlen
< sizeof(int))
485 if (val
> 255 || val
< -1)
487 np
->mcast_hops
= val
;
491 case IPV6_MULTICAST_LOOP
:
492 if (optlen
< sizeof(int))
496 np
->mc_loop
= valbool
;
500 case IPV6_MULTICAST_IF
:
501 if (sk
->sk_type
== SOCK_STREAM
)
503 if (optlen
< sizeof(int))
507 struct net_device
*dev
;
509 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
512 dev
= dev_get_by_index(net
, val
);
522 case IPV6_ADD_MEMBERSHIP
:
523 case IPV6_DROP_MEMBERSHIP
:
525 struct ipv6_mreq mreq
;
527 if (optlen
< sizeof(struct ipv6_mreq
))
531 if (inet_sk(sk
)->is_icsk
)
535 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
538 if (optname
== IPV6_ADD_MEMBERSHIP
)
539 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
541 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
544 case IPV6_JOIN_ANYCAST
:
545 case IPV6_LEAVE_ANYCAST
:
547 struct ipv6_mreq mreq
;
549 if (optlen
< sizeof(struct ipv6_mreq
))
553 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
556 if (optname
== IPV6_JOIN_ANYCAST
)
557 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
559 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
562 case MCAST_JOIN_GROUP
:
563 case MCAST_LEAVE_GROUP
:
565 struct group_req greq
;
566 struct sockaddr_in6
*psin6
;
568 if (optlen
< sizeof(struct group_req
))
572 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
574 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
575 retv
= -EADDRNOTAVAIL
;
578 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
579 if (optname
== MCAST_JOIN_GROUP
)
580 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
583 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
587 case MCAST_JOIN_SOURCE_GROUP
:
588 case MCAST_LEAVE_SOURCE_GROUP
:
589 case MCAST_BLOCK_SOURCE
:
590 case MCAST_UNBLOCK_SOURCE
:
592 struct group_source_req greqs
;
595 if (optlen
< sizeof(struct group_source_req
))
597 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
601 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
602 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
603 retv
= -EADDRNOTAVAIL
;
606 if (optname
== MCAST_BLOCK_SOURCE
) {
607 omode
= MCAST_EXCLUDE
;
609 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
610 omode
= MCAST_EXCLUDE
;
612 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
613 struct sockaddr_in6
*psin6
;
615 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
616 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
618 /* prior join w/ different source is ok */
619 if (retv
&& retv
!= -EADDRINUSE
)
621 omode
= MCAST_INCLUDE
;
623 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
624 omode
= MCAST_INCLUDE
;
627 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
632 extern int sysctl_mld_max_msf
;
633 struct group_filter
*gsf
;
635 if (optlen
< GROUP_FILTER_SIZE(0))
637 if (optlen
> sysctl_optmem_max
) {
641 gsf
= kmalloc(optlen
,GFP_KERNEL
);
647 if (copy_from_user(gsf
, optval
, optlen
)) {
651 /* numsrc >= (4G-140)/128 overflow in 32 bits */
652 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
653 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
658 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
663 retv
= ip6_mc_msfilter(sk
, gsf
);
668 case IPV6_ROUTER_ALERT
:
669 if (optlen
< sizeof(int))
671 retv
= ip6_ra_control(sk
, val
);
673 case IPV6_MTU_DISCOVER
:
674 if (optlen
< sizeof(int))
676 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
682 if (optlen
< sizeof(int))
684 if (val
&& val
< IPV6_MIN_MTU
)
690 if (optlen
< sizeof(int))
692 np
->recverr
= valbool
;
694 skb_queue_purge(&sk
->sk_error_queue
);
697 case IPV6_FLOWINFO_SEND
:
698 if (optlen
< sizeof(int))
700 np
->sndflow
= valbool
;
703 case IPV6_FLOWLABEL_MGR
:
704 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
706 case IPV6_IPSEC_POLICY
:
707 case IPV6_XFRM_POLICY
:
709 if (!capable(CAP_NET_ADMIN
))
711 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
714 case IPV6_ADDR_PREFERENCES
:
716 unsigned int pref
= 0;
717 unsigned int prefmask
= ~0;
719 if (optlen
< sizeof(int))
724 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
725 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
727 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
728 case IPV6_PREFER_SRC_PUBLIC
:
729 pref
|= IPV6_PREFER_SRC_PUBLIC
;
731 case IPV6_PREFER_SRC_TMP
:
732 pref
|= IPV6_PREFER_SRC_TMP
;
734 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
737 goto pref_skip_pubtmp
;
742 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
743 IPV6_PREFER_SRC_TMP
);
746 /* check HOME/COA conflicts */
747 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
748 case IPV6_PREFER_SRC_HOME
:
750 case IPV6_PREFER_SRC_COA
:
751 pref
|= IPV6_PREFER_SRC_COA
;
758 prefmask
&= ~IPV6_PREFER_SRC_COA
;
761 /* check CGA/NONCGA conflicts */
762 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
763 case IPV6_PREFER_SRC_CGA
:
764 case IPV6_PREFER_SRC_NONCGA
:
771 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
776 case IPV6_MINHOPCOUNT
:
777 if (optlen
< sizeof(int))
779 if (val
< 0 || val
> 255)
781 np
->min_hopcount
= val
;
784 np
->dontfrag
= valbool
;
798 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
799 char __user
*optval
, unsigned int optlen
)
803 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
804 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
806 if (level
!= SOL_IPV6
)
809 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
810 #ifdef CONFIG_NETFILTER
811 /* we need to exclude all possible ENOPROTOOPTs except default case */
812 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
813 optname
!= IPV6_XFRM_POLICY
) {
815 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
823 EXPORT_SYMBOL(ipv6_setsockopt
);
826 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
827 char __user
*optval
, unsigned int optlen
)
831 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
832 if (udp_prot
.compat_setsockopt
!= NULL
)
833 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
835 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
838 if (level
!= SOL_IPV6
)
841 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
842 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
845 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
846 #ifdef CONFIG_NETFILTER
847 /* we need to exclude all possible ENOPROTOOPTs except default case */
848 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
849 optname
!= IPV6_XFRM_POLICY
) {
851 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
859 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
862 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
863 int optname
, char __user
*optval
, int len
)
865 struct ipv6_opt_hdr
*hdr
;
874 case IPV6_RTHDRDSTOPTS
:
878 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
884 return -EINVAL
; /* should not happen */
890 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
891 if (copy_to_user(optval
, hdr
, len
))
896 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
897 char __user
*optval
, int __user
*optlen
)
899 struct ipv6_pinfo
*np
= inet6_sk(sk
);
903 if (ip6_mroute_opt(optname
))
904 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
906 if (get_user(len
, optlen
))
910 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
911 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
912 sk
->sk_protocol
!= IPPROTO_TCP
)
914 if (sk
->sk_state
!= TCP_ESTABLISHED
)
920 struct group_filter gsf
;
923 if (len
< GROUP_FILTER_SIZE(0))
925 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
927 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
928 return -EADDRNOTAVAIL
;
930 err
= ip6_mc_msfget(sk
, &gsf
,
931 (struct group_filter __user
*)optval
, optlen
);
936 case IPV6_2292PKTOPTIONS
:
941 if (sk
->sk_type
!= SOCK_STREAM
)
944 msg
.msg_control
= optval
;
945 msg
.msg_controllen
= len
;
949 skb
= np
->pktoptions
;
951 atomic_inc(&skb
->users
);
955 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
960 if (np
->rxopt
.bits
.rxinfo
) {
961 struct in6_pktinfo src_info
;
962 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
963 np
->sticky_pktinfo
.ipi6_ifindex
;
964 np
->mcast_oif
? ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
) :
965 ipv6_addr_copy(&src_info
.ipi6_addr
, &(np
->sticky_pktinfo
.ipi6_addr
));
966 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
968 if (np
->rxopt
.bits
.rxhlim
) {
969 int hlim
= np
->mcast_hops
;
970 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
972 if (np
->rxopt
.bits
.rxoinfo
) {
973 struct in6_pktinfo src_info
;
974 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
975 np
->sticky_pktinfo
.ipi6_ifindex
;
976 np
->mcast_oif
? ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
) :
977 ipv6_addr_copy(&src_info
.ipi6_addr
, &(np
->sticky_pktinfo
.ipi6_addr
));
978 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
980 if (np
->rxopt
.bits
.rxohlim
) {
981 int hlim
= np
->mcast_hops
;
982 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
985 len
-= msg
.msg_controllen
;
986 return put_user(len
, optlen
);
990 struct dst_entry
*dst
;
994 dst
= __sk_dst_get(sk
);
1007 case IPV6_RECVPKTINFO
:
1008 val
= np
->rxopt
.bits
.rxinfo
;
1011 case IPV6_2292PKTINFO
:
1012 val
= np
->rxopt
.bits
.rxoinfo
;
1015 case IPV6_RECVHOPLIMIT
:
1016 val
= np
->rxopt
.bits
.rxhlim
;
1019 case IPV6_2292HOPLIMIT
:
1020 val
= np
->rxopt
.bits
.rxohlim
;
1023 case IPV6_RECVRTHDR
:
1024 val
= np
->rxopt
.bits
.srcrt
;
1027 case IPV6_2292RTHDR
:
1028 val
= np
->rxopt
.bits
.osrcrt
;
1032 case IPV6_RTHDRDSTOPTS
:
1038 len
= ipv6_getsockopt_sticky(sk
, np
->opt
,
1039 optname
, optval
, len
);
1041 /* check if ipv6_getsockopt_sticky() returns err code */
1044 return put_user(len
, optlen
);
1047 case IPV6_RECVHOPOPTS
:
1048 val
= np
->rxopt
.bits
.hopopts
;
1051 case IPV6_2292HOPOPTS
:
1052 val
= np
->rxopt
.bits
.ohopopts
;
1055 case IPV6_RECVDSTOPTS
:
1056 val
= np
->rxopt
.bits
.dstopts
;
1059 case IPV6_2292DSTOPTS
:
1060 val
= np
->rxopt
.bits
.odstopts
;
1067 case IPV6_RECVTCLASS
:
1068 val
= np
->rxopt
.bits
.rxtclass
;
1072 val
= np
->rxopt
.bits
.rxflow
;
1075 case IPV6_RECVPATHMTU
:
1076 val
= np
->rxopt
.bits
.rxpmtu
;
1081 struct dst_entry
*dst
;
1082 struct ip6_mtuinfo mtuinfo
;
1084 if (len
< sizeof(mtuinfo
))
1087 len
= sizeof(mtuinfo
);
1088 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1091 dst
= __sk_dst_get(sk
);
1093 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1095 if (!mtuinfo
.ip6m_mtu
)
1098 if (put_user(len
, optlen
))
1100 if (copy_to_user(optval
, &mtuinfo
, len
))
1107 case IPV6_UNICAST_HOPS
:
1108 case IPV6_MULTICAST_HOPS
:
1110 struct dst_entry
*dst
;
1112 if (optname
== IPV6_UNICAST_HOPS
)
1113 val
= np
->hop_limit
;
1115 val
= np
->mcast_hops
;
1119 dst
= __sk_dst_get(sk
);
1121 val
= ip6_dst_hoplimit(dst
);
1126 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1130 case IPV6_MULTICAST_LOOP
:
1134 case IPV6_MULTICAST_IF
:
1135 val
= np
->mcast_oif
;
1138 case IPV6_MTU_DISCOVER
:
1146 case IPV6_FLOWINFO_SEND
:
1150 case IPV6_ADDR_PREFERENCES
:
1153 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1154 val
|= IPV6_PREFER_SRC_TMP
;
1155 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1156 val
|= IPV6_PREFER_SRC_PUBLIC
;
1158 /* XXX: should we return system default? */
1159 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1162 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1163 val
|= IPV6_PREFER_SRC_COA
;
1165 val
|= IPV6_PREFER_SRC_HOME
;
1168 case IPV6_MINHOPCOUNT
:
1169 val
= np
->min_hopcount
;
1177 return -ENOPROTOOPT
;
1179 len
= min_t(unsigned int, sizeof(int), len
);
1180 if(put_user(len
, optlen
))
1182 if(copy_to_user(optval
,&val
,len
))
1187 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1188 char __user
*optval
, int __user
*optlen
)
1192 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1193 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1195 if(level
!= SOL_IPV6
)
1196 return -ENOPROTOOPT
;
1198 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1199 #ifdef CONFIG_NETFILTER
1200 /* we need to exclude all possible ENOPROTOOPTs except default case */
1201 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1204 if (get_user(len
, optlen
))
1208 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1212 err
= put_user(len
, optlen
);
1218 EXPORT_SYMBOL(ipv6_getsockopt
);
1220 #ifdef CONFIG_COMPAT
1221 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1222 char __user
*optval
, int __user
*optlen
)
1226 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1227 if (udp_prot
.compat_getsockopt
!= NULL
)
1228 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1230 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1233 if (level
!= SOL_IPV6
)
1234 return -ENOPROTOOPT
;
1236 if (optname
== MCAST_MSFILTER
)
1237 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1240 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1241 #ifdef CONFIG_NETFILTER
1242 /* we need to exclude all possible ENOPROTOOPTs except default case */
1243 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1246 if (get_user(len
, optlen
))
1250 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1251 optname
, optval
, &len
);
1254 err
= put_user(len
, optlen
);
1260 EXPORT_SYMBOL(compat_ipv6_getsockopt
);