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
, 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
);
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) ||
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)
323 case IPV6_RECVTCLASS
:
324 if (optlen
< sizeof(int))
326 np
->rxopt
.bits
.rxtclass
= valbool
;
331 if (optlen
< sizeof(int))
333 np
->rxopt
.bits
.rxflow
= valbool
;
338 case IPV6_RTHDRDSTOPTS
:
342 struct ipv6_txoptions
*opt
;
344 /* remove any sticky options header with a zero option
345 * length, per RFC3542.
349 else if (optval
== NULL
)
351 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
352 optlen
& 0x7 || optlen
> 8 * 255)
355 /* hop-by-hop / destination options are privileged option */
357 if (optname
!= IPV6_RTHDR
&& !capable(CAP_NET_RAW
))
360 opt
= ipv6_renew_options(sk
, np
->opt
, optname
,
361 (struct ipv6_opt_hdr __user
*)optval
,
368 /* routing header option needs extra check */
370 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
371 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
372 switch (rthdr
->type
) {
373 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
374 case IPV6_SRCRT_TYPE_2
:
375 if (rthdr
->hdrlen
!= 2 ||
376 rthdr
->segments_left
!= 1)
385 if ((rthdr
->hdrlen
& 1) ||
386 (rthdr
->hdrlen
>> 1) != rthdr
->segments_left
)
391 opt
= ipv6_update_options(sk
, opt
);
394 sock_kfree_s(sk
, opt
, opt
->tot_len
);
398 case IPV6_2292PKTOPTIONS
:
400 struct ipv6_txoptions
*opt
= NULL
;
405 fl
.fl6_flowlabel
= 0;
406 fl
.oif
= sk
->sk_bound_dev_if
;
411 /* 1K is probably excessive
412 * 1K is surely not enough, 2K per standard header is 16K.
415 if (optlen
> 64*1024)
418 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
423 memset(opt
, 0, sizeof(*opt
));
424 opt
->tot_len
= sizeof(*opt
) + optlen
;
426 if (copy_from_user(opt
+1, optval
, optlen
))
429 msg
.msg_controllen
= optlen
;
430 msg
.msg_control
= (void*)(opt
+1);
432 retv
= datagram_send_ctl(net
, &msg
, &fl
, opt
, &junk
, &junk
);
437 opt
= ipv6_update_options(sk
, opt
);
440 sock_kfree_s(sk
, opt
, opt
->tot_len
);
443 case IPV6_UNICAST_HOPS
:
444 if (optlen
< sizeof(int))
446 if (val
> 255 || val
< -1)
452 case IPV6_MULTICAST_HOPS
:
453 if (sk
->sk_type
== SOCK_STREAM
)
455 if (optlen
< sizeof(int))
457 if (val
> 255 || val
< -1)
459 np
->mcast_hops
= val
;
463 case IPV6_MULTICAST_LOOP
:
464 if (optlen
< sizeof(int))
468 np
->mc_loop
= valbool
;
472 case IPV6_MULTICAST_IF
:
473 if (sk
->sk_type
== SOCK_STREAM
)
475 if (optlen
< sizeof(int))
479 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
482 if (__dev_get_by_index(net
, val
) == NULL
) {
490 case IPV6_ADD_MEMBERSHIP
:
491 case IPV6_DROP_MEMBERSHIP
:
493 struct ipv6_mreq mreq
;
495 if (optlen
< sizeof(struct ipv6_mreq
))
499 if (inet_sk(sk
)->is_icsk
)
503 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
506 if (optname
== IPV6_ADD_MEMBERSHIP
)
507 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
509 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
512 case IPV6_JOIN_ANYCAST
:
513 case IPV6_LEAVE_ANYCAST
:
515 struct ipv6_mreq mreq
;
517 if (optlen
< sizeof(struct ipv6_mreq
))
521 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
524 if (optname
== IPV6_JOIN_ANYCAST
)
525 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
527 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
530 case MCAST_JOIN_GROUP
:
531 case MCAST_LEAVE_GROUP
:
533 struct group_req greq
;
534 struct sockaddr_in6
*psin6
;
536 if (optlen
< sizeof(struct group_req
))
540 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
542 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
543 retv
= -EADDRNOTAVAIL
;
546 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
547 if (optname
== MCAST_JOIN_GROUP
)
548 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
551 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
555 case MCAST_JOIN_SOURCE_GROUP
:
556 case MCAST_LEAVE_SOURCE_GROUP
:
557 case MCAST_BLOCK_SOURCE
:
558 case MCAST_UNBLOCK_SOURCE
:
560 struct group_source_req greqs
;
563 if (optlen
< sizeof(struct group_source_req
))
565 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
569 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
570 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
571 retv
= -EADDRNOTAVAIL
;
574 if (optname
== MCAST_BLOCK_SOURCE
) {
575 omode
= MCAST_EXCLUDE
;
577 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
578 omode
= MCAST_EXCLUDE
;
580 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
581 struct sockaddr_in6
*psin6
;
583 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
584 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
586 /* prior join w/ different source is ok */
587 if (retv
&& retv
!= -EADDRINUSE
)
589 omode
= MCAST_INCLUDE
;
591 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
592 omode
= MCAST_INCLUDE
;
595 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
600 extern int sysctl_mld_max_msf
;
601 struct group_filter
*gsf
;
603 if (optlen
< GROUP_FILTER_SIZE(0))
605 if (optlen
> sysctl_optmem_max
) {
609 gsf
= kmalloc(optlen
,GFP_KERNEL
);
615 if (copy_from_user(gsf
, optval
, optlen
)) {
619 /* numsrc >= (4G-140)/128 overflow in 32 bits */
620 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
621 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
626 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
631 retv
= ip6_mc_msfilter(sk
, gsf
);
636 case IPV6_ROUTER_ALERT
:
637 if (optlen
< sizeof(int))
639 retv
= ip6_ra_control(sk
, val
);
641 case IPV6_MTU_DISCOVER
:
642 if (optlen
< sizeof(int))
650 if (optlen
< sizeof(int))
652 if (val
&& val
< IPV6_MIN_MTU
)
658 if (optlen
< sizeof(int))
660 np
->recverr
= valbool
;
662 skb_queue_purge(&sk
->sk_error_queue
);
665 case IPV6_FLOWINFO_SEND
:
666 if (optlen
< sizeof(int))
668 np
->sndflow
= valbool
;
671 case IPV6_FLOWLABEL_MGR
:
672 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
674 case IPV6_IPSEC_POLICY
:
675 case IPV6_XFRM_POLICY
:
677 if (!capable(CAP_NET_ADMIN
))
679 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
682 case IPV6_ADDR_PREFERENCES
:
684 unsigned int pref
= 0;
685 unsigned int prefmask
= ~0;
687 if (optlen
< sizeof(int))
692 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
693 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
695 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
696 case IPV6_PREFER_SRC_PUBLIC
:
697 pref
|= IPV6_PREFER_SRC_PUBLIC
;
699 case IPV6_PREFER_SRC_TMP
:
700 pref
|= IPV6_PREFER_SRC_TMP
;
702 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
705 goto pref_skip_pubtmp
;
710 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
711 IPV6_PREFER_SRC_TMP
);
714 /* check HOME/COA conflicts */
715 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
716 case IPV6_PREFER_SRC_HOME
:
718 case IPV6_PREFER_SRC_COA
:
719 pref
|= IPV6_PREFER_SRC_COA
;
726 prefmask
&= ~IPV6_PREFER_SRC_COA
;
729 /* check CGA/NONCGA conflicts */
730 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
731 case IPV6_PREFER_SRC_CGA
:
732 case IPV6_PREFER_SRC_NONCGA
:
739 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
755 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
756 char __user
*optval
, int optlen
)
760 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
761 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
763 if (level
!= SOL_IPV6
)
766 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
767 #ifdef CONFIG_NETFILTER
768 /* we need to exclude all possible ENOPROTOOPTs except default case */
769 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
770 optname
!= IPV6_XFRM_POLICY
) {
772 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
780 EXPORT_SYMBOL(ipv6_setsockopt
);
783 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
784 char __user
*optval
, int optlen
)
788 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
789 if (udp_prot
.compat_setsockopt
!= NULL
)
790 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
792 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
795 if (level
!= SOL_IPV6
)
798 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
799 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
802 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
803 #ifdef CONFIG_NETFILTER
804 /* we need to exclude all possible ENOPROTOOPTs except default case */
805 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
806 optname
!= IPV6_XFRM_POLICY
) {
808 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
816 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
819 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
820 int optname
, char __user
*optval
, int len
)
822 struct ipv6_opt_hdr
*hdr
;
831 case IPV6_RTHDRDSTOPTS
:
835 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
841 return -EINVAL
; /* should not happen */
847 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
848 if (copy_to_user(optval
, hdr
, len
))
853 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
854 char __user
*optval
, int __user
*optlen
)
856 struct ipv6_pinfo
*np
= inet6_sk(sk
);
860 if (ip6_mroute_opt(optname
))
861 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
863 if (get_user(len
, optlen
))
867 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
868 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
869 sk
->sk_protocol
!= IPPROTO_TCP
)
871 if (sk
->sk_state
!= TCP_ESTABLISHED
)
877 struct group_filter gsf
;
880 if (len
< GROUP_FILTER_SIZE(0))
882 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
884 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
885 return -EADDRNOTAVAIL
;
887 err
= ip6_mc_msfget(sk
, &gsf
,
888 (struct group_filter __user
*)optval
, optlen
);
893 case IPV6_2292PKTOPTIONS
:
898 if (sk
->sk_type
!= SOCK_STREAM
)
901 msg
.msg_control
= optval
;
902 msg
.msg_controllen
= len
;
906 skb
= np
->pktoptions
;
908 atomic_inc(&skb
->users
);
912 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
917 if (np
->rxopt
.bits
.rxinfo
) {
918 struct in6_pktinfo src_info
;
919 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
: sk
->sk_bound_dev_if
;
920 ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
);
921 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
923 if (np
->rxopt
.bits
.rxhlim
) {
924 int hlim
= np
->mcast_hops
;
925 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
927 if (np
->rxopt
.bits
.rxoinfo
) {
928 struct in6_pktinfo src_info
;
929 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
: sk
->sk_bound_dev_if
;
930 ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
);
931 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
933 if (np
->rxopt
.bits
.rxohlim
) {
934 int hlim
= np
->mcast_hops
;
935 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
938 len
-= msg
.msg_controllen
;
939 return put_user(len
, optlen
);
943 struct dst_entry
*dst
;
946 dst
= sk_dst_get(sk
);
961 case IPV6_RECVPKTINFO
:
962 val
= np
->rxopt
.bits
.rxinfo
;
965 case IPV6_2292PKTINFO
:
966 val
= np
->rxopt
.bits
.rxoinfo
;
969 case IPV6_RECVHOPLIMIT
:
970 val
= np
->rxopt
.bits
.rxhlim
;
973 case IPV6_2292HOPLIMIT
:
974 val
= np
->rxopt
.bits
.rxohlim
;
978 val
= np
->rxopt
.bits
.srcrt
;
982 val
= np
->rxopt
.bits
.osrcrt
;
986 case IPV6_RTHDRDSTOPTS
:
992 len
= ipv6_getsockopt_sticky(sk
, np
->opt
,
993 optname
, optval
, len
);
995 /* check if ipv6_getsockopt_sticky() returns err code */
998 return put_user(len
, optlen
);
1001 case IPV6_RECVHOPOPTS
:
1002 val
= np
->rxopt
.bits
.hopopts
;
1005 case IPV6_2292HOPOPTS
:
1006 val
= np
->rxopt
.bits
.ohopopts
;
1009 case IPV6_RECVDSTOPTS
:
1010 val
= np
->rxopt
.bits
.dstopts
;
1013 case IPV6_2292DSTOPTS
:
1014 val
= np
->rxopt
.bits
.odstopts
;
1023 case IPV6_RECVTCLASS
:
1024 val
= np
->rxopt
.bits
.rxtclass
;
1028 val
= np
->rxopt
.bits
.rxflow
;
1031 case IPV6_UNICAST_HOPS
:
1032 case IPV6_MULTICAST_HOPS
:
1034 struct dst_entry
*dst
;
1036 if (optname
== IPV6_UNICAST_HOPS
)
1037 val
= np
->hop_limit
;
1039 val
= np
->mcast_hops
;
1041 dst
= sk_dst_get(sk
);
1044 val
= ip6_dst_hoplimit(dst
);
1048 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1052 case IPV6_MULTICAST_LOOP
:
1056 case IPV6_MULTICAST_IF
:
1057 val
= np
->mcast_oif
;
1060 case IPV6_MTU_DISCOVER
:
1068 case IPV6_FLOWINFO_SEND
:
1072 case IPV6_ADDR_PREFERENCES
:
1075 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1076 val
|= IPV6_PREFER_SRC_TMP
;
1077 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1078 val
|= IPV6_PREFER_SRC_PUBLIC
;
1080 /* XXX: should we return system default? */
1081 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1084 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1085 val
|= IPV6_PREFER_SRC_COA
;
1087 val
|= IPV6_PREFER_SRC_HOME
;
1091 return -ENOPROTOOPT
;
1093 len
= min_t(unsigned int, sizeof(int), len
);
1094 if(put_user(len
, optlen
))
1096 if(copy_to_user(optval
,&val
,len
))
1101 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1102 char __user
*optval
, int __user
*optlen
)
1106 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1107 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1109 if(level
!= SOL_IPV6
)
1110 return -ENOPROTOOPT
;
1112 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1113 #ifdef CONFIG_NETFILTER
1114 /* we need to exclude all possible ENOPROTOOPTs except default case */
1115 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1118 if (get_user(len
, optlen
))
1122 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1126 err
= put_user(len
, optlen
);
1132 EXPORT_SYMBOL(ipv6_getsockopt
);
1134 #ifdef CONFIG_COMPAT
1135 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1136 char __user
*optval
, int __user
*optlen
)
1140 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1141 if (udp_prot
.compat_getsockopt
!= NULL
)
1142 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1144 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1147 if (level
!= SOL_IPV6
)
1148 return -ENOPROTOOPT
;
1150 if (optname
== MCAST_MSFILTER
)
1151 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1154 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1155 #ifdef CONFIG_NETFILTER
1156 /* we need to exclude all possible ENOPROTOOPTs except default case */
1157 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1160 if (get_user(len
, optlen
))
1164 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1165 optname
, optval
, &len
);
1168 err
= put_user(len
, optlen
);
1174 EXPORT_SYMBOL(compat_ipv6_getsockopt
);