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 DEFINE_SNMP_STAT(struct ipstats_mib
, ipv6_statistics
) __read_mostly
;
60 struct ip6_ra_chain
*ip6_ra_chain
;
61 DEFINE_RWLOCK(ip6_ra_lock
);
63 int ip6_ra_control(struct sock
*sk
, int sel
)
65 struct ip6_ra_chain
*ra
, *new_ra
, **rap
;
67 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
68 if (sk
->sk_type
!= SOCK_RAW
|| inet_sk(sk
)->inet_num
!= IPPROTO_RAW
)
71 new_ra
= (sel
>=0) ? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
73 write_lock_bh(&ip6_ra_lock
);
74 for (rap
= &ip6_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
77 write_unlock_bh(&ip6_ra_lock
);
83 write_unlock_bh(&ip6_ra_lock
);
91 write_unlock_bh(&ip6_ra_lock
);
99 write_unlock_bh(&ip6_ra_lock
);
104 struct ipv6_txoptions
*ipv6_update_options(struct sock
*sk
,
105 struct ipv6_txoptions
*opt
)
107 if (inet_sk(sk
)->is_icsk
) {
109 !((1 << sk
->sk_state
) & (TCPF_LISTEN
| TCPF_CLOSE
)) &&
110 inet_sk(sk
)->inet_daddr
!= LOOPBACK4_IPV6
) {
111 struct inet_connection_sock
*icsk
= inet_csk(sk
);
112 icsk
->icsk_ext_hdr_len
= opt
->opt_flen
+ opt
->opt_nflen
;
113 icsk
->icsk_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
115 opt
= xchg(&inet6_sk(sk
)->opt
, opt
);
117 write_lock(&sk
->sk_dst_lock
);
118 opt
= xchg(&inet6_sk(sk
)->opt
, opt
);
119 write_unlock(&sk
->sk_dst_lock
);
126 static int do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
127 char __user
*optval
, unsigned int optlen
)
129 struct ipv6_pinfo
*np
= inet6_sk(sk
);
130 struct net
*net
= sock_net(sk
);
132 int retv
= -ENOPROTOOPT
;
137 if (optlen
>= sizeof(int)) {
138 if (get_user(val
, (int __user
*) optval
))
146 if (ip6_mroute_opt(optname
))
147 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
154 if (optlen
< sizeof(int))
156 if (val
== PF_INET
) {
157 struct ipv6_txoptions
*opt
;
158 struct sk_buff
*pktopt
;
160 if (sk
->sk_type
== SOCK_RAW
)
163 if (sk
->sk_protocol
== IPPROTO_UDP
||
164 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
165 struct udp_sock
*up
= udp_sk(sk
);
166 if (up
->pending
== AF_INET6
) {
170 } else if (sk
->sk_protocol
!= IPPROTO_TCP
)
173 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
178 if (ipv6_only_sock(sk
) ||
179 !ipv6_addr_v4mapped(&np
->daddr
)) {
180 retv
= -EADDRNOTAVAIL
;
184 fl6_free_socklist(sk
);
185 ipv6_sock_mc_close(sk
);
188 * Sock is moving from IPv6 to IPv4 (sk_prot), so
189 * remove it from the refcnt debug socks count in the
192 sk_refcnt_debug_dec(sk
);
194 if (sk
->sk_protocol
== IPPROTO_TCP
) {
195 struct inet_connection_sock
*icsk
= inet_csk(sk
);
197 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
198 sock_prot_inuse_add(net
, &tcp_prot
, 1);
200 sk
->sk_prot
= &tcp_prot
;
201 icsk
->icsk_af_ops
= &ipv4_specific
;
202 sk
->sk_socket
->ops
= &inet_stream_ops
;
203 sk
->sk_family
= PF_INET
;
204 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
206 struct proto
*prot
= &udp_prot
;
208 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
209 prot
= &udplite_prot
;
211 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
212 sock_prot_inuse_add(net
, prot
, 1);
215 sk
->sk_socket
->ops
= &inet_dgram_ops
;
216 sk
->sk_family
= PF_INET
;
218 opt
= xchg(&np
->opt
, NULL
);
220 sock_kfree_s(sk
, opt
, opt
->tot_len
);
221 pktopt
= xchg(&np
->pktoptions
, NULL
);
224 sk
->sk_destruct
= inet_sock_destruct
;
226 * ... and add it to the refcnt debug socks count
227 * in the new family. -acme
229 sk_refcnt_debug_inc(sk
);
230 module_put(THIS_MODULE
);
237 if (optlen
< sizeof(int) ||
238 inet_sk(sk
)->inet_num
)
240 np
->ipv6only
= valbool
;
244 case IPV6_RECVPKTINFO
:
245 if (optlen
< sizeof(int))
247 np
->rxopt
.bits
.rxinfo
= valbool
;
251 case IPV6_2292PKTINFO
:
252 if (optlen
< sizeof(int))
254 np
->rxopt
.bits
.rxoinfo
= valbool
;
258 case IPV6_RECVHOPLIMIT
:
259 if (optlen
< sizeof(int))
261 np
->rxopt
.bits
.rxhlim
= valbool
;
265 case IPV6_2292HOPLIMIT
:
266 if (optlen
< sizeof(int))
268 np
->rxopt
.bits
.rxohlim
= valbool
;
273 if (optlen
< sizeof(int))
275 np
->rxopt
.bits
.srcrt
= valbool
;
280 if (optlen
< sizeof(int))
282 np
->rxopt
.bits
.osrcrt
= valbool
;
286 case IPV6_RECVHOPOPTS
:
287 if (optlen
< sizeof(int))
289 np
->rxopt
.bits
.hopopts
= valbool
;
293 case IPV6_2292HOPOPTS
:
294 if (optlen
< sizeof(int))
296 np
->rxopt
.bits
.ohopopts
= valbool
;
300 case IPV6_RECVDSTOPTS
:
301 if (optlen
< sizeof(int))
303 np
->rxopt
.bits
.dstopts
= valbool
;
307 case IPV6_2292DSTOPTS
:
308 if (optlen
< sizeof(int))
310 np
->rxopt
.bits
.odstopts
= valbool
;
315 if (optlen
< sizeof(int))
317 if (val
< -1 || val
> 0xff)
319 /* RFC 3542, 6.5: default traffic class of 0x0 */
326 case IPV6_RECVTCLASS
:
327 if (optlen
< sizeof(int))
329 np
->rxopt
.bits
.rxtclass
= valbool
;
334 if (optlen
< sizeof(int))
336 np
->rxopt
.bits
.rxflow
= valbool
;
341 case IPV6_RTHDRDSTOPTS
:
345 struct ipv6_txoptions
*opt
;
347 /* remove any sticky options header with a zero option
348 * length, per RFC3542.
352 else if (optval
== NULL
)
354 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
355 optlen
& 0x7 || optlen
> 8 * 255)
358 /* hop-by-hop / destination options are privileged option */
360 if (optname
!= IPV6_RTHDR
&& !capable(CAP_NET_RAW
))
363 opt
= ipv6_renew_options(sk
, np
->opt
, optname
,
364 (struct ipv6_opt_hdr __user
*)optval
,
371 /* routing header option needs extra check */
373 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
374 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
375 switch (rthdr
->type
) {
376 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
377 case IPV6_SRCRT_TYPE_2
:
378 if (rthdr
->hdrlen
!= 2 ||
379 rthdr
->segments_left
!= 1)
390 opt
= ipv6_update_options(sk
, opt
);
393 sock_kfree_s(sk
, opt
, opt
->tot_len
);
399 struct in6_pktinfo pkt
;
403 else if (optlen
< sizeof(struct in6_pktinfo
) || optval
== NULL
)
406 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
410 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
413 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
414 ipv6_addr_copy(&np
->sticky_pktinfo
.ipi6_addr
, &pkt
.ipi6_addr
);
419 case IPV6_2292PKTOPTIONS
:
421 struct ipv6_txoptions
*opt
= NULL
;
426 fl
.fl6_flowlabel
= 0;
427 fl
.oif
= sk
->sk_bound_dev_if
;
428 fl
.mark
= sk
->sk_mark
;
433 /* 1K is probably excessive
434 * 1K is surely not enough, 2K per standard header is 16K.
437 if (optlen
> 64*1024)
440 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
445 memset(opt
, 0, sizeof(*opt
));
446 opt
->tot_len
= sizeof(*opt
) + optlen
;
448 if (copy_from_user(opt
+1, optval
, optlen
))
451 msg
.msg_controllen
= optlen
;
452 msg
.msg_control
= (void*)(opt
+1);
454 retv
= datagram_send_ctl(net
, &msg
, &fl
, opt
, &junk
, &junk
);
459 opt
= ipv6_update_options(sk
, opt
);
462 sock_kfree_s(sk
, opt
, opt
->tot_len
);
465 case IPV6_UNICAST_HOPS
:
466 if (optlen
< sizeof(int))
468 if (val
> 255 || val
< -1)
474 case IPV6_MULTICAST_HOPS
:
475 if (sk
->sk_type
== SOCK_STREAM
)
477 if (optlen
< sizeof(int))
479 if (val
> 255 || val
< -1)
481 np
->mcast_hops
= val
;
485 case IPV6_MULTICAST_LOOP
:
486 if (optlen
< sizeof(int))
490 np
->mc_loop
= valbool
;
494 case IPV6_MULTICAST_IF
:
495 if (sk
->sk_type
== SOCK_STREAM
)
497 if (optlen
< sizeof(int))
501 struct net_device
*dev
;
503 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
506 dev
= dev_get_by_index(net
, val
);
516 case IPV6_ADD_MEMBERSHIP
:
517 case IPV6_DROP_MEMBERSHIP
:
519 struct ipv6_mreq mreq
;
521 if (optlen
< sizeof(struct ipv6_mreq
))
525 if (inet_sk(sk
)->is_icsk
)
529 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
532 if (optname
== IPV6_ADD_MEMBERSHIP
)
533 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
535 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
538 case IPV6_JOIN_ANYCAST
:
539 case IPV6_LEAVE_ANYCAST
:
541 struct ipv6_mreq mreq
;
543 if (optlen
< sizeof(struct ipv6_mreq
))
547 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
550 if (optname
== IPV6_JOIN_ANYCAST
)
551 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
553 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
556 case MCAST_JOIN_GROUP
:
557 case MCAST_LEAVE_GROUP
:
559 struct group_req greq
;
560 struct sockaddr_in6
*psin6
;
562 if (optlen
< sizeof(struct group_req
))
566 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
568 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
569 retv
= -EADDRNOTAVAIL
;
572 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
573 if (optname
== MCAST_JOIN_GROUP
)
574 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
577 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
581 case MCAST_JOIN_SOURCE_GROUP
:
582 case MCAST_LEAVE_SOURCE_GROUP
:
583 case MCAST_BLOCK_SOURCE
:
584 case MCAST_UNBLOCK_SOURCE
:
586 struct group_source_req greqs
;
589 if (optlen
< sizeof(struct group_source_req
))
591 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
595 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
596 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
597 retv
= -EADDRNOTAVAIL
;
600 if (optname
== MCAST_BLOCK_SOURCE
) {
601 omode
= MCAST_EXCLUDE
;
603 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
604 omode
= MCAST_EXCLUDE
;
606 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
607 struct sockaddr_in6
*psin6
;
609 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
610 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
612 /* prior join w/ different source is ok */
613 if (retv
&& retv
!= -EADDRINUSE
)
615 omode
= MCAST_INCLUDE
;
617 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
618 omode
= MCAST_INCLUDE
;
621 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
626 extern int sysctl_mld_max_msf
;
627 struct group_filter
*gsf
;
629 if (optlen
< GROUP_FILTER_SIZE(0))
631 if (optlen
> sysctl_optmem_max
) {
635 gsf
= kmalloc(optlen
,GFP_KERNEL
);
641 if (copy_from_user(gsf
, optval
, optlen
)) {
645 /* numsrc >= (4G-140)/128 overflow in 32 bits */
646 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
647 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
652 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
657 retv
= ip6_mc_msfilter(sk
, gsf
);
662 case IPV6_ROUTER_ALERT
:
663 if (optlen
< sizeof(int))
665 retv
= ip6_ra_control(sk
, val
);
667 case IPV6_MTU_DISCOVER
:
668 if (optlen
< sizeof(int))
670 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
676 if (optlen
< sizeof(int))
678 if (val
&& val
< IPV6_MIN_MTU
)
684 if (optlen
< sizeof(int))
686 np
->recverr
= valbool
;
688 skb_queue_purge(&sk
->sk_error_queue
);
691 case IPV6_FLOWINFO_SEND
:
692 if (optlen
< sizeof(int))
694 np
->sndflow
= valbool
;
697 case IPV6_FLOWLABEL_MGR
:
698 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
700 case IPV6_IPSEC_POLICY
:
701 case IPV6_XFRM_POLICY
:
703 if (!capable(CAP_NET_ADMIN
))
705 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
708 case IPV6_ADDR_PREFERENCES
:
710 unsigned int pref
= 0;
711 unsigned int prefmask
= ~0;
713 if (optlen
< sizeof(int))
718 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
719 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
721 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
722 case IPV6_PREFER_SRC_PUBLIC
:
723 pref
|= IPV6_PREFER_SRC_PUBLIC
;
725 case IPV6_PREFER_SRC_TMP
:
726 pref
|= IPV6_PREFER_SRC_TMP
;
728 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
731 goto pref_skip_pubtmp
;
736 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
737 IPV6_PREFER_SRC_TMP
);
740 /* check HOME/COA conflicts */
741 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
742 case IPV6_PREFER_SRC_HOME
:
744 case IPV6_PREFER_SRC_COA
:
745 pref
|= IPV6_PREFER_SRC_COA
;
752 prefmask
&= ~IPV6_PREFER_SRC_COA
;
755 /* check CGA/NONCGA conflicts */
756 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
757 case IPV6_PREFER_SRC_CGA
:
758 case IPV6_PREFER_SRC_NONCGA
:
765 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
781 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
782 char __user
*optval
, unsigned int optlen
)
786 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
787 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
789 if (level
!= SOL_IPV6
)
792 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
793 #ifdef CONFIG_NETFILTER
794 /* we need to exclude all possible ENOPROTOOPTs except default case */
795 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
796 optname
!= IPV6_XFRM_POLICY
) {
798 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
806 EXPORT_SYMBOL(ipv6_setsockopt
);
809 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
810 char __user
*optval
, unsigned int optlen
)
814 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
815 if (udp_prot
.compat_setsockopt
!= NULL
)
816 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
818 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
821 if (level
!= SOL_IPV6
)
824 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
825 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
828 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
829 #ifdef CONFIG_NETFILTER
830 /* we need to exclude all possible ENOPROTOOPTs except default case */
831 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
832 optname
!= IPV6_XFRM_POLICY
) {
834 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
842 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
845 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
846 int optname
, char __user
*optval
, int len
)
848 struct ipv6_opt_hdr
*hdr
;
857 case IPV6_RTHDRDSTOPTS
:
861 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
867 return -EINVAL
; /* should not happen */
873 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
874 if (copy_to_user(optval
, hdr
, len
))
879 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
880 char __user
*optval
, int __user
*optlen
)
882 struct ipv6_pinfo
*np
= inet6_sk(sk
);
886 if (ip6_mroute_opt(optname
))
887 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
889 if (get_user(len
, optlen
))
893 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
894 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
895 sk
->sk_protocol
!= IPPROTO_TCP
)
897 if (sk
->sk_state
!= TCP_ESTABLISHED
)
903 struct group_filter gsf
;
906 if (len
< GROUP_FILTER_SIZE(0))
908 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
910 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
911 return -EADDRNOTAVAIL
;
913 err
= ip6_mc_msfget(sk
, &gsf
,
914 (struct group_filter __user
*)optval
, optlen
);
919 case IPV6_2292PKTOPTIONS
:
924 if (sk
->sk_type
!= SOCK_STREAM
)
927 msg
.msg_control
= optval
;
928 msg
.msg_controllen
= len
;
932 skb
= np
->pktoptions
;
934 atomic_inc(&skb
->users
);
938 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
943 if (np
->rxopt
.bits
.rxinfo
) {
944 struct in6_pktinfo src_info
;
945 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
946 np
->sticky_pktinfo
.ipi6_ifindex
;
947 np
->mcast_oif
? ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
) :
948 ipv6_addr_copy(&src_info
.ipi6_addr
, &(np
->sticky_pktinfo
.ipi6_addr
));
949 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
951 if (np
->rxopt
.bits
.rxhlim
) {
952 int hlim
= np
->mcast_hops
;
953 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
955 if (np
->rxopt
.bits
.rxoinfo
) {
956 struct in6_pktinfo src_info
;
957 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
958 np
->sticky_pktinfo
.ipi6_ifindex
;
959 np
->mcast_oif
? ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
) :
960 ipv6_addr_copy(&src_info
.ipi6_addr
, &(np
->sticky_pktinfo
.ipi6_addr
));
961 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
963 if (np
->rxopt
.bits
.rxohlim
) {
964 int hlim
= np
->mcast_hops
;
965 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
968 len
-= msg
.msg_controllen
;
969 return put_user(len
, optlen
);
973 struct dst_entry
*dst
;
976 dst
= sk_dst_get(sk
);
991 case IPV6_RECVPKTINFO
:
992 val
= np
->rxopt
.bits
.rxinfo
;
995 case IPV6_2292PKTINFO
:
996 val
= np
->rxopt
.bits
.rxoinfo
;
999 case IPV6_RECVHOPLIMIT
:
1000 val
= np
->rxopt
.bits
.rxhlim
;
1003 case IPV6_2292HOPLIMIT
:
1004 val
= np
->rxopt
.bits
.rxohlim
;
1007 case IPV6_RECVRTHDR
:
1008 val
= np
->rxopt
.bits
.srcrt
;
1011 case IPV6_2292RTHDR
:
1012 val
= np
->rxopt
.bits
.osrcrt
;
1016 case IPV6_RTHDRDSTOPTS
:
1022 len
= ipv6_getsockopt_sticky(sk
, np
->opt
,
1023 optname
, optval
, len
);
1025 /* check if ipv6_getsockopt_sticky() returns err code */
1028 return put_user(len
, optlen
);
1031 case IPV6_RECVHOPOPTS
:
1032 val
= np
->rxopt
.bits
.hopopts
;
1035 case IPV6_2292HOPOPTS
:
1036 val
= np
->rxopt
.bits
.ohopopts
;
1039 case IPV6_RECVDSTOPTS
:
1040 val
= np
->rxopt
.bits
.dstopts
;
1043 case IPV6_2292DSTOPTS
:
1044 val
= np
->rxopt
.bits
.odstopts
;
1051 case IPV6_RECVTCLASS
:
1052 val
= np
->rxopt
.bits
.rxtclass
;
1056 val
= np
->rxopt
.bits
.rxflow
;
1059 case IPV6_UNICAST_HOPS
:
1060 case IPV6_MULTICAST_HOPS
:
1062 struct dst_entry
*dst
;
1064 if (optname
== IPV6_UNICAST_HOPS
)
1065 val
= np
->hop_limit
;
1067 val
= np
->mcast_hops
;
1069 dst
= sk_dst_get(sk
);
1072 val
= ip6_dst_hoplimit(dst
);
1076 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1080 case IPV6_MULTICAST_LOOP
:
1084 case IPV6_MULTICAST_IF
:
1085 val
= np
->mcast_oif
;
1088 case IPV6_MTU_DISCOVER
:
1096 case IPV6_FLOWINFO_SEND
:
1100 case IPV6_ADDR_PREFERENCES
:
1103 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1104 val
|= IPV6_PREFER_SRC_TMP
;
1105 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1106 val
|= IPV6_PREFER_SRC_PUBLIC
;
1108 /* XXX: should we return system default? */
1109 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1112 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1113 val
|= IPV6_PREFER_SRC_COA
;
1115 val
|= IPV6_PREFER_SRC_HOME
;
1119 return -ENOPROTOOPT
;
1121 len
= min_t(unsigned int, sizeof(int), len
);
1122 if(put_user(len
, optlen
))
1124 if(copy_to_user(optval
,&val
,len
))
1129 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1130 char __user
*optval
, int __user
*optlen
)
1134 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1135 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1137 if(level
!= SOL_IPV6
)
1138 return -ENOPROTOOPT
;
1140 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1141 #ifdef CONFIG_NETFILTER
1142 /* we need to exclude all possible ENOPROTOOPTs except default case */
1143 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1146 if (get_user(len
, optlen
))
1150 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1154 err
= put_user(len
, optlen
);
1160 EXPORT_SYMBOL(ipv6_getsockopt
);
1162 #ifdef CONFIG_COMPAT
1163 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1164 char __user
*optval
, int __user
*optlen
)
1168 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1169 if (udp_prot
.compat_getsockopt
!= NULL
)
1170 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1172 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1175 if (level
!= SOL_IPV6
)
1176 return -ENOPROTOOPT
;
1178 if (optname
== MCAST_MSFILTER
)
1179 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1182 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1183 #ifdef CONFIG_NETFILTER
1184 /* we need to exclude all possible ENOPROTOOPTs except default case */
1185 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1188 if (get_user(len
, optlen
))
1192 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1193 optname
, optval
, &len
);
1196 err
= put_user(len
, optlen
);
1202 EXPORT_SYMBOL(compat_ipv6_getsockopt
);