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>
43 #include <net/ndisc.h>
44 #include <net/protocol.h>
45 #include <net/transp_v6.h>
46 #include <net/ip6_route.h>
47 #include <net/addrconf.h>
48 #include <net/inet_common.h>
51 #include <net/udplite.h>
53 #include <net/compat.h>
55 #include <asm/uaccess.h>
57 DEFINE_SNMP_STAT(struct ipstats_mib
, ipv6_statistics
) __read_mostly
;
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
)->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
);
103 struct ipv6_txoptions
*ipv6_update_options(struct sock
*sk
,
104 struct ipv6_txoptions
*opt
)
106 if (inet_sk(sk
)->is_icsk
) {
108 !((1 << sk
->sk_state
) & (TCPF_LISTEN
| TCPF_CLOSE
)) &&
109 inet_sk(sk
)->daddr
!= LOOPBACK4_IPV6
) {
110 struct inet_connection_sock
*icsk
= inet_csk(sk
);
111 icsk
->icsk_ext_hdr_len
= opt
->opt_flen
+ opt
->opt_nflen
;
112 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
114 opt
= xchg(&inet6_sk(sk
)->opt
, opt
);
116 write_lock(&sk
->sk_dst_lock
);
117 opt
= xchg(&inet6_sk(sk
)->opt
, opt
);
118 write_unlock(&sk
->sk_dst_lock
);
125 static int do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
126 char __user
*optval
, unsigned int optlen
)
128 struct ipv6_pinfo
*np
= inet6_sk(sk
);
129 struct net
*net
= sock_net(sk
);
131 int retv
= -ENOPROTOOPT
;
136 if (optlen
>= sizeof(int)) {
137 if (get_user(val
, (int __user
*) optval
))
145 if (ip6_mroute_opt(optname
))
146 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
153 if (optlen
< sizeof(int))
155 if (val
== PF_INET
) {
156 struct ipv6_txoptions
*opt
;
157 struct sk_buff
*pktopt
;
159 if (sk
->sk_type
== SOCK_RAW
)
162 if (sk
->sk_protocol
== IPPROTO_UDP
||
163 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
164 struct udp_sock
*up
= udp_sk(sk
);
165 if (up
->pending
== AF_INET6
) {
169 } else if (sk
->sk_protocol
!= IPPROTO_TCP
)
172 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
177 if (ipv6_only_sock(sk
) ||
178 !ipv6_addr_v4mapped(&np
->daddr
)) {
179 retv
= -EADDRNOTAVAIL
;
183 fl6_free_socklist(sk
);
184 ipv6_sock_mc_close(sk
);
187 * Sock is moving from IPv6 to IPv4 (sk_prot), so
188 * remove it from the refcnt debug socks count in the
191 sk_refcnt_debug_dec(sk
);
193 if (sk
->sk_protocol
== IPPROTO_TCP
) {
194 struct inet_connection_sock
*icsk
= inet_csk(sk
);
196 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
197 sock_prot_inuse_add(net
, &tcp_prot
, 1);
199 sk
->sk_prot
= &tcp_prot
;
200 icsk
->icsk_af_ops
= &ipv4_specific
;
201 sk
->sk_socket
->ops
= &inet_stream_ops
;
202 sk
->sk_family
= PF_INET
;
203 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
205 struct proto
*prot
= &udp_prot
;
207 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
208 prot
= &udplite_prot
;
210 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
211 sock_prot_inuse_add(net
, prot
, 1);
214 sk
->sk_socket
->ops
= &inet_dgram_ops
;
215 sk
->sk_family
= PF_INET
;
217 opt
= xchg(&np
->opt
, NULL
);
219 sock_kfree_s(sk
, opt
, opt
->tot_len
);
220 pktopt
= xchg(&np
->pktoptions
, NULL
);
223 sk
->sk_destruct
= inet_sock_destruct
;
225 * ... and add it to the refcnt debug socks count
226 * in the new family. -acme
228 sk_refcnt_debug_inc(sk
);
229 module_put(THIS_MODULE
);
236 if (optlen
< sizeof(int) ||
239 np
->ipv6only
= valbool
;
243 case IPV6_RECVPKTINFO
:
244 if (optlen
< sizeof(int))
246 np
->rxopt
.bits
.rxinfo
= valbool
;
250 case IPV6_2292PKTINFO
:
251 if (optlen
< sizeof(int))
253 np
->rxopt
.bits
.rxoinfo
= valbool
;
257 case IPV6_RECVHOPLIMIT
:
258 if (optlen
< sizeof(int))
260 np
->rxopt
.bits
.rxhlim
= valbool
;
264 case IPV6_2292HOPLIMIT
:
265 if (optlen
< sizeof(int))
267 np
->rxopt
.bits
.rxohlim
= valbool
;
272 if (optlen
< sizeof(int))
274 np
->rxopt
.bits
.srcrt
= valbool
;
279 if (optlen
< sizeof(int))
281 np
->rxopt
.bits
.osrcrt
= valbool
;
285 case IPV6_RECVHOPOPTS
:
286 if (optlen
< sizeof(int))
288 np
->rxopt
.bits
.hopopts
= valbool
;
292 case IPV6_2292HOPOPTS
:
293 if (optlen
< sizeof(int))
295 np
->rxopt
.bits
.ohopopts
= valbool
;
299 case IPV6_RECVDSTOPTS
:
300 if (optlen
< sizeof(int))
302 np
->rxopt
.bits
.dstopts
= valbool
;
306 case IPV6_2292DSTOPTS
:
307 if (optlen
< sizeof(int))
309 np
->rxopt
.bits
.odstopts
= valbool
;
314 if (optlen
< sizeof(int))
316 if (val
< -1 || val
> 0xff)
318 /* RFC 3542, 6.5: default traffic class of 0x0 */
325 case IPV6_RECVTCLASS
:
326 if (optlen
< sizeof(int))
328 np
->rxopt
.bits
.rxtclass
= valbool
;
333 if (optlen
< sizeof(int))
335 np
->rxopt
.bits
.rxflow
= valbool
;
340 case IPV6_RTHDRDSTOPTS
:
344 struct ipv6_txoptions
*opt
;
346 /* remove any sticky options header with a zero option
347 * length, per RFC3542.
351 else if (optval
== NULL
)
353 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
354 optlen
& 0x7 || optlen
> 8 * 255)
357 /* hop-by-hop / destination options are privileged option */
359 if (optname
!= IPV6_RTHDR
&& !capable(CAP_NET_RAW
))
362 opt
= ipv6_renew_options(sk
, np
->opt
, optname
,
363 (struct ipv6_opt_hdr __user
*)optval
,
370 /* routing header option needs extra check */
372 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
373 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
374 switch (rthdr
->type
) {
375 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
376 case IPV6_SRCRT_TYPE_2
:
377 if (rthdr
->hdrlen
!= 2 ||
378 rthdr
->segments_left
!= 1)
389 opt
= ipv6_update_options(sk
, opt
);
392 sock_kfree_s(sk
, opt
, opt
->tot_len
);
398 struct in6_pktinfo pkt
;
402 else if (optlen
< sizeof(struct in6_pktinfo
) || optval
== NULL
)
405 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
409 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
412 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
413 ipv6_addr_copy(&np
->sticky_pktinfo
.ipi6_addr
, &pkt
.ipi6_addr
);
418 case IPV6_2292PKTOPTIONS
:
420 struct ipv6_txoptions
*opt
= NULL
;
425 fl
.fl6_flowlabel
= 0;
426 fl
.oif
= sk
->sk_bound_dev_if
;
431 /* 1K is probably excessive
432 * 1K is surely not enough, 2K per standard header is 16K.
435 if (optlen
> 64*1024)
438 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
443 memset(opt
, 0, sizeof(*opt
));
444 opt
->tot_len
= sizeof(*opt
) + optlen
;
446 if (copy_from_user(opt
+1, optval
, optlen
))
449 msg
.msg_controllen
= optlen
;
450 msg
.msg_control
= (void*)(opt
+1);
452 retv
= datagram_send_ctl(net
, &msg
, &fl
, opt
, &junk
, &junk
);
457 opt
= ipv6_update_options(sk
, opt
);
460 sock_kfree_s(sk
, opt
, opt
->tot_len
);
463 case IPV6_UNICAST_HOPS
:
464 if (optlen
< sizeof(int))
466 if (val
> 255 || val
< -1)
472 case IPV6_MULTICAST_HOPS
:
473 if (sk
->sk_type
== SOCK_STREAM
)
475 if (optlen
< sizeof(int))
477 if (val
> 255 || val
< -1)
479 np
->mcast_hops
= val
;
483 case IPV6_MULTICAST_LOOP
:
484 if (optlen
< sizeof(int))
488 np
->mc_loop
= valbool
;
492 case IPV6_MULTICAST_IF
:
493 if (sk
->sk_type
== SOCK_STREAM
)
495 if (optlen
< sizeof(int))
499 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
502 if (__dev_get_by_index(net
, val
) == NULL
) {
510 case IPV6_ADD_MEMBERSHIP
:
511 case IPV6_DROP_MEMBERSHIP
:
513 struct ipv6_mreq mreq
;
515 if (optlen
< sizeof(struct ipv6_mreq
))
519 if (inet_sk(sk
)->is_icsk
)
523 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
526 if (optname
== IPV6_ADD_MEMBERSHIP
)
527 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
529 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
532 case IPV6_JOIN_ANYCAST
:
533 case IPV6_LEAVE_ANYCAST
:
535 struct ipv6_mreq mreq
;
537 if (optlen
< sizeof(struct ipv6_mreq
))
541 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
544 if (optname
== IPV6_JOIN_ANYCAST
)
545 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
547 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
550 case MCAST_JOIN_GROUP
:
551 case MCAST_LEAVE_GROUP
:
553 struct group_req greq
;
554 struct sockaddr_in6
*psin6
;
556 if (optlen
< sizeof(struct group_req
))
560 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
562 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
563 retv
= -EADDRNOTAVAIL
;
566 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
567 if (optname
== MCAST_JOIN_GROUP
)
568 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
571 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
575 case MCAST_JOIN_SOURCE_GROUP
:
576 case MCAST_LEAVE_SOURCE_GROUP
:
577 case MCAST_BLOCK_SOURCE
:
578 case MCAST_UNBLOCK_SOURCE
:
580 struct group_source_req greqs
;
583 if (optlen
< sizeof(struct group_source_req
))
585 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
589 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
590 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
591 retv
= -EADDRNOTAVAIL
;
594 if (optname
== MCAST_BLOCK_SOURCE
) {
595 omode
= MCAST_EXCLUDE
;
597 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
598 omode
= MCAST_EXCLUDE
;
600 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
601 struct sockaddr_in6
*psin6
;
603 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
604 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
606 /* prior join w/ different source is ok */
607 if (retv
&& retv
!= -EADDRINUSE
)
609 omode
= MCAST_INCLUDE
;
611 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
612 omode
= MCAST_INCLUDE
;
615 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
620 extern int sysctl_mld_max_msf
;
621 struct group_filter
*gsf
;
623 if (optlen
< GROUP_FILTER_SIZE(0))
625 if (optlen
> sysctl_optmem_max
) {
629 gsf
= kmalloc(optlen
,GFP_KERNEL
);
635 if (copy_from_user(gsf
, optval
, optlen
)) {
639 /* numsrc >= (4G-140)/128 overflow in 32 bits */
640 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
641 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
646 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
651 retv
= ip6_mc_msfilter(sk
, gsf
);
656 case IPV6_ROUTER_ALERT
:
657 if (optlen
< sizeof(int))
659 retv
= ip6_ra_control(sk
, val
);
661 case IPV6_MTU_DISCOVER
:
662 if (optlen
< sizeof(int))
670 if (optlen
< sizeof(int))
672 if (val
&& val
< IPV6_MIN_MTU
)
678 if (optlen
< sizeof(int))
680 np
->recverr
= valbool
;
682 skb_queue_purge(&sk
->sk_error_queue
);
685 case IPV6_FLOWINFO_SEND
:
686 if (optlen
< sizeof(int))
688 np
->sndflow
= valbool
;
691 case IPV6_FLOWLABEL_MGR
:
692 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
694 case IPV6_IPSEC_POLICY
:
695 case IPV6_XFRM_POLICY
:
697 if (!capable(CAP_NET_ADMIN
))
699 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
702 case IPV6_ADDR_PREFERENCES
:
704 unsigned int pref
= 0;
705 unsigned int prefmask
= ~0;
707 if (optlen
< sizeof(int))
712 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
713 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
715 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
716 case IPV6_PREFER_SRC_PUBLIC
:
717 pref
|= IPV6_PREFER_SRC_PUBLIC
;
719 case IPV6_PREFER_SRC_TMP
:
720 pref
|= IPV6_PREFER_SRC_TMP
;
722 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
725 goto pref_skip_pubtmp
;
730 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
731 IPV6_PREFER_SRC_TMP
);
734 /* check HOME/COA conflicts */
735 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
736 case IPV6_PREFER_SRC_HOME
:
738 case IPV6_PREFER_SRC_COA
:
739 pref
|= IPV6_PREFER_SRC_COA
;
746 prefmask
&= ~IPV6_PREFER_SRC_COA
;
749 /* check CGA/NONCGA conflicts */
750 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
751 case IPV6_PREFER_SRC_CGA
:
752 case IPV6_PREFER_SRC_NONCGA
:
759 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
775 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
776 char __user
*optval
, unsigned int optlen
)
780 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
781 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
783 if (level
!= SOL_IPV6
)
786 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
787 #ifdef CONFIG_NETFILTER
788 /* we need to exclude all possible ENOPROTOOPTs except default case */
789 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
790 optname
!= IPV6_XFRM_POLICY
) {
792 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
800 EXPORT_SYMBOL(ipv6_setsockopt
);
803 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
804 char __user
*optval
, unsigned int optlen
)
808 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
809 if (udp_prot
.compat_setsockopt
!= NULL
)
810 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
812 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
815 if (level
!= SOL_IPV6
)
818 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
819 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
822 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
823 #ifdef CONFIG_NETFILTER
824 /* we need to exclude all possible ENOPROTOOPTs except default case */
825 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
826 optname
!= IPV6_XFRM_POLICY
) {
828 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
836 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
839 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
840 int optname
, char __user
*optval
, int len
)
842 struct ipv6_opt_hdr
*hdr
;
851 case IPV6_RTHDRDSTOPTS
:
855 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
861 return -EINVAL
; /* should not happen */
867 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
868 if (copy_to_user(optval
, hdr
, len
))
873 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
874 char __user
*optval
, int __user
*optlen
)
876 struct ipv6_pinfo
*np
= inet6_sk(sk
);
880 if (ip6_mroute_opt(optname
))
881 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
883 if (get_user(len
, optlen
))
887 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
888 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
889 sk
->sk_protocol
!= IPPROTO_TCP
)
891 if (sk
->sk_state
!= TCP_ESTABLISHED
)
897 struct group_filter gsf
;
900 if (len
< GROUP_FILTER_SIZE(0))
902 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
904 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
905 return -EADDRNOTAVAIL
;
907 err
= ip6_mc_msfget(sk
, &gsf
,
908 (struct group_filter __user
*)optval
, optlen
);
913 case IPV6_2292PKTOPTIONS
:
918 if (sk
->sk_type
!= SOCK_STREAM
)
921 msg
.msg_control
= optval
;
922 msg
.msg_controllen
= len
;
926 skb
= np
->pktoptions
;
928 atomic_inc(&skb
->users
);
932 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
937 if (np
->rxopt
.bits
.rxinfo
) {
938 struct in6_pktinfo src_info
;
939 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
940 np
->sticky_pktinfo
.ipi6_ifindex
;
941 np
->mcast_oif
? ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
) :
942 ipv6_addr_copy(&src_info
.ipi6_addr
, &(np
->sticky_pktinfo
.ipi6_addr
));
943 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
945 if (np
->rxopt
.bits
.rxhlim
) {
946 int hlim
= np
->mcast_hops
;
947 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
949 if (np
->rxopt
.bits
.rxoinfo
) {
950 struct in6_pktinfo src_info
;
951 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
952 np
->sticky_pktinfo
.ipi6_ifindex
;
953 np
->mcast_oif
? ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
) :
954 ipv6_addr_copy(&src_info
.ipi6_addr
, &(np
->sticky_pktinfo
.ipi6_addr
));
955 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
957 if (np
->rxopt
.bits
.rxohlim
) {
958 int hlim
= np
->mcast_hops
;
959 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
962 len
-= msg
.msg_controllen
;
963 return put_user(len
, optlen
);
967 struct dst_entry
*dst
;
970 dst
= sk_dst_get(sk
);
985 case IPV6_RECVPKTINFO
:
986 val
= np
->rxopt
.bits
.rxinfo
;
989 case IPV6_2292PKTINFO
:
990 val
= np
->rxopt
.bits
.rxoinfo
;
993 case IPV6_RECVHOPLIMIT
:
994 val
= np
->rxopt
.bits
.rxhlim
;
997 case IPV6_2292HOPLIMIT
:
998 val
= np
->rxopt
.bits
.rxohlim
;
1001 case IPV6_RECVRTHDR
:
1002 val
= np
->rxopt
.bits
.srcrt
;
1005 case IPV6_2292RTHDR
:
1006 val
= np
->rxopt
.bits
.osrcrt
;
1010 case IPV6_RTHDRDSTOPTS
:
1016 len
= ipv6_getsockopt_sticky(sk
, np
->opt
,
1017 optname
, optval
, len
);
1019 /* check if ipv6_getsockopt_sticky() returns err code */
1022 return put_user(len
, optlen
);
1025 case IPV6_RECVHOPOPTS
:
1026 val
= np
->rxopt
.bits
.hopopts
;
1029 case IPV6_2292HOPOPTS
:
1030 val
= np
->rxopt
.bits
.ohopopts
;
1033 case IPV6_RECVDSTOPTS
:
1034 val
= np
->rxopt
.bits
.dstopts
;
1037 case IPV6_2292DSTOPTS
:
1038 val
= np
->rxopt
.bits
.odstopts
;
1045 case IPV6_RECVTCLASS
:
1046 val
= np
->rxopt
.bits
.rxtclass
;
1050 val
= np
->rxopt
.bits
.rxflow
;
1053 case IPV6_UNICAST_HOPS
:
1054 case IPV6_MULTICAST_HOPS
:
1056 struct dst_entry
*dst
;
1058 if (optname
== IPV6_UNICAST_HOPS
)
1059 val
= np
->hop_limit
;
1061 val
= np
->mcast_hops
;
1063 dst
= sk_dst_get(sk
);
1066 val
= ip6_dst_hoplimit(dst
);
1070 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1074 case IPV6_MULTICAST_LOOP
:
1078 case IPV6_MULTICAST_IF
:
1079 val
= np
->mcast_oif
;
1082 case IPV6_MTU_DISCOVER
:
1090 case IPV6_FLOWINFO_SEND
:
1094 case IPV6_ADDR_PREFERENCES
:
1097 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1098 val
|= IPV6_PREFER_SRC_TMP
;
1099 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1100 val
|= IPV6_PREFER_SRC_PUBLIC
;
1102 /* XXX: should we return system default? */
1103 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1106 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1107 val
|= IPV6_PREFER_SRC_COA
;
1109 val
|= IPV6_PREFER_SRC_HOME
;
1113 return -ENOPROTOOPT
;
1115 len
= min_t(unsigned int, sizeof(int), len
);
1116 if(put_user(len
, optlen
))
1118 if(copy_to_user(optval
,&val
,len
))
1123 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1124 char __user
*optval
, int __user
*optlen
)
1128 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1129 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1131 if(level
!= SOL_IPV6
)
1132 return -ENOPROTOOPT
;
1134 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1135 #ifdef CONFIG_NETFILTER
1136 /* we need to exclude all possible ENOPROTOOPTs except default case */
1137 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1140 if (get_user(len
, optlen
))
1144 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1148 err
= put_user(len
, optlen
);
1154 EXPORT_SYMBOL(ipv6_getsockopt
);
1156 #ifdef CONFIG_COMPAT
1157 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1158 char __user
*optval
, int __user
*optlen
)
1162 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1163 if (udp_prot
.compat_getsockopt
!= NULL
)
1164 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1166 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1169 if (level
!= SOL_IPV6
)
1170 return -ENOPROTOOPT
;
1172 if (optname
== MCAST_MSFILTER
)
1173 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1176 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1177 #ifdef CONFIG_NETFILTER
1178 /* we need to exclude all possible ENOPROTOOPTs except default case */
1179 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1182 if (get_user(len
, optlen
))
1186 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1187 optname
, optval
, &len
);
1190 err
= put_user(len
, optlen
);
1196 EXPORT_SYMBOL(compat_ipv6_getsockopt
);