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 spin_lock(&sk
->sk_dst_lock
);
118 opt
= xchg(&inet6_sk(sk
)->opt
, opt
);
119 spin_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
;
340 case IPV6_RECVPATHMTU
:
341 if (optlen
< sizeof(int))
343 np
->rxopt
.bits
.rxpmtu
= valbool
;
348 case IPV6_RTHDRDSTOPTS
:
352 struct ipv6_txoptions
*opt
;
354 /* remove any sticky options header with a zero option
355 * length, per RFC3542.
359 else if (optval
== NULL
)
361 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
362 optlen
& 0x7 || optlen
> 8 * 255)
365 /* hop-by-hop / destination options are privileged option */
367 if (optname
!= IPV6_RTHDR
&& !capable(CAP_NET_RAW
))
370 opt
= ipv6_renew_options(sk
, np
->opt
, optname
,
371 (struct ipv6_opt_hdr __user
*)optval
,
378 /* routing header option needs extra check */
380 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
381 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
382 switch (rthdr
->type
) {
383 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
384 case IPV6_SRCRT_TYPE_2
:
385 if (rthdr
->hdrlen
!= 2 ||
386 rthdr
->segments_left
!= 1)
397 opt
= ipv6_update_options(sk
, opt
);
400 sock_kfree_s(sk
, opt
, opt
->tot_len
);
406 struct in6_pktinfo pkt
;
410 else if (optlen
< sizeof(struct in6_pktinfo
) || optval
== NULL
)
413 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
417 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
420 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
421 ipv6_addr_copy(&np
->sticky_pktinfo
.ipi6_addr
, &pkt
.ipi6_addr
);
426 case IPV6_2292PKTOPTIONS
:
428 struct ipv6_txoptions
*opt
= NULL
;
433 fl
.fl6_flowlabel
= 0;
434 fl
.oif
= sk
->sk_bound_dev_if
;
435 fl
.mark
= sk
->sk_mark
;
440 /* 1K is probably excessive
441 * 1K is surely not enough, 2K per standard header is 16K.
444 if (optlen
> 64*1024)
447 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
452 memset(opt
, 0, sizeof(*opt
));
453 opt
->tot_len
= sizeof(*opt
) + optlen
;
455 if (copy_from_user(opt
+1, optval
, optlen
))
458 msg
.msg_controllen
= optlen
;
459 msg
.msg_control
= (void*)(opt
+1);
461 retv
= datagram_send_ctl(net
, &msg
, &fl
, opt
, &junk
, &junk
,
467 opt
= ipv6_update_options(sk
, opt
);
470 sock_kfree_s(sk
, opt
, opt
->tot_len
);
473 case IPV6_UNICAST_HOPS
:
474 if (optlen
< sizeof(int))
476 if (val
> 255 || val
< -1)
482 case IPV6_MULTICAST_HOPS
:
483 if (sk
->sk_type
== SOCK_STREAM
)
485 if (optlen
< sizeof(int))
487 if (val
> 255 || val
< -1)
489 np
->mcast_hops
= val
;
493 case IPV6_MULTICAST_LOOP
:
494 if (optlen
< sizeof(int))
498 np
->mc_loop
= valbool
;
502 case IPV6_MULTICAST_IF
:
503 if (sk
->sk_type
== SOCK_STREAM
)
505 if (optlen
< sizeof(int))
509 struct net_device
*dev
;
511 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
514 dev
= dev_get_by_index(net
, val
);
524 case IPV6_ADD_MEMBERSHIP
:
525 case IPV6_DROP_MEMBERSHIP
:
527 struct ipv6_mreq mreq
;
529 if (optlen
< sizeof(struct ipv6_mreq
))
533 if (inet_sk(sk
)->is_icsk
)
537 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
540 if (optname
== IPV6_ADD_MEMBERSHIP
)
541 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
543 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
546 case IPV6_JOIN_ANYCAST
:
547 case IPV6_LEAVE_ANYCAST
:
549 struct ipv6_mreq mreq
;
551 if (optlen
< sizeof(struct ipv6_mreq
))
555 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
558 if (optname
== IPV6_JOIN_ANYCAST
)
559 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
561 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
564 case MCAST_JOIN_GROUP
:
565 case MCAST_LEAVE_GROUP
:
567 struct group_req greq
;
568 struct sockaddr_in6
*psin6
;
570 if (optlen
< sizeof(struct group_req
))
574 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
576 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
577 retv
= -EADDRNOTAVAIL
;
580 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
581 if (optname
== MCAST_JOIN_GROUP
)
582 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
585 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
589 case MCAST_JOIN_SOURCE_GROUP
:
590 case MCAST_LEAVE_SOURCE_GROUP
:
591 case MCAST_BLOCK_SOURCE
:
592 case MCAST_UNBLOCK_SOURCE
:
594 struct group_source_req greqs
;
597 if (optlen
< sizeof(struct group_source_req
))
599 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
603 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
604 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
605 retv
= -EADDRNOTAVAIL
;
608 if (optname
== MCAST_BLOCK_SOURCE
) {
609 omode
= MCAST_EXCLUDE
;
611 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
612 omode
= MCAST_EXCLUDE
;
614 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
615 struct sockaddr_in6
*psin6
;
617 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
618 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
620 /* prior join w/ different source is ok */
621 if (retv
&& retv
!= -EADDRINUSE
)
623 omode
= MCAST_INCLUDE
;
625 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
626 omode
= MCAST_INCLUDE
;
629 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
634 extern int sysctl_mld_max_msf
;
635 struct group_filter
*gsf
;
637 if (optlen
< GROUP_FILTER_SIZE(0))
639 if (optlen
> sysctl_optmem_max
) {
643 gsf
= kmalloc(optlen
,GFP_KERNEL
);
649 if (copy_from_user(gsf
, optval
, optlen
)) {
653 /* numsrc >= (4G-140)/128 overflow in 32 bits */
654 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
655 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
660 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
665 retv
= ip6_mc_msfilter(sk
, gsf
);
670 case IPV6_ROUTER_ALERT
:
671 if (optlen
< sizeof(int))
673 retv
= ip6_ra_control(sk
, val
);
675 case IPV6_MTU_DISCOVER
:
676 if (optlen
< sizeof(int))
678 if (val
< IP_PMTUDISC_DONT
|| val
> IP_PMTUDISC_PROBE
)
684 if (optlen
< sizeof(int))
686 if (val
&& val
< IPV6_MIN_MTU
)
692 if (optlen
< sizeof(int))
694 np
->recverr
= valbool
;
696 skb_queue_purge(&sk
->sk_error_queue
);
699 case IPV6_FLOWINFO_SEND
:
700 if (optlen
< sizeof(int))
702 np
->sndflow
= valbool
;
705 case IPV6_FLOWLABEL_MGR
:
706 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
708 case IPV6_IPSEC_POLICY
:
709 case IPV6_XFRM_POLICY
:
711 if (!capable(CAP_NET_ADMIN
))
713 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
716 case IPV6_ADDR_PREFERENCES
:
718 unsigned int pref
= 0;
719 unsigned int prefmask
= ~0;
721 if (optlen
< sizeof(int))
726 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
727 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
729 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
730 case IPV6_PREFER_SRC_PUBLIC
:
731 pref
|= IPV6_PREFER_SRC_PUBLIC
;
733 case IPV6_PREFER_SRC_TMP
:
734 pref
|= IPV6_PREFER_SRC_TMP
;
736 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
739 goto pref_skip_pubtmp
;
744 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
745 IPV6_PREFER_SRC_TMP
);
748 /* check HOME/COA conflicts */
749 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
750 case IPV6_PREFER_SRC_HOME
:
752 case IPV6_PREFER_SRC_COA
:
753 pref
|= IPV6_PREFER_SRC_COA
;
760 prefmask
&= ~IPV6_PREFER_SRC_COA
;
763 /* check CGA/NONCGA conflicts */
764 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
765 case IPV6_PREFER_SRC_CGA
:
766 case IPV6_PREFER_SRC_NONCGA
:
773 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
778 case IPV6_MINHOPCOUNT
:
779 if (optlen
< sizeof(int))
781 if (val
< 0 || val
> 255)
783 np
->min_hopcount
= val
;
786 np
->dontfrag
= valbool
;
800 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
801 char __user
*optval
, unsigned int optlen
)
805 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
806 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
808 if (level
!= SOL_IPV6
)
811 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
812 #ifdef CONFIG_NETFILTER
813 /* we need to exclude all possible ENOPROTOOPTs except default case */
814 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
815 optname
!= IPV6_XFRM_POLICY
) {
817 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
825 EXPORT_SYMBOL(ipv6_setsockopt
);
828 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
829 char __user
*optval
, unsigned int optlen
)
833 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
834 if (udp_prot
.compat_setsockopt
!= NULL
)
835 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
837 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
840 if (level
!= SOL_IPV6
)
843 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
844 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
847 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
848 #ifdef CONFIG_NETFILTER
849 /* we need to exclude all possible ENOPROTOOPTs except default case */
850 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
851 optname
!= IPV6_XFRM_POLICY
) {
853 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
861 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
864 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
865 int optname
, char __user
*optval
, int len
)
867 struct ipv6_opt_hdr
*hdr
;
876 case IPV6_RTHDRDSTOPTS
:
880 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
886 return -EINVAL
; /* should not happen */
892 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
893 if (copy_to_user(optval
, hdr
, len
))
898 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
899 char __user
*optval
, int __user
*optlen
)
901 struct ipv6_pinfo
*np
= inet6_sk(sk
);
905 if (ip6_mroute_opt(optname
))
906 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
908 if (get_user(len
, optlen
))
912 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
913 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
914 sk
->sk_protocol
!= IPPROTO_TCP
)
916 if (sk
->sk_state
!= TCP_ESTABLISHED
)
922 struct group_filter gsf
;
925 if (len
< GROUP_FILTER_SIZE(0))
927 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
929 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
930 return -EADDRNOTAVAIL
;
932 err
= ip6_mc_msfget(sk
, &gsf
,
933 (struct group_filter __user
*)optval
, optlen
);
938 case IPV6_2292PKTOPTIONS
:
943 if (sk
->sk_type
!= SOCK_STREAM
)
946 msg
.msg_control
= optval
;
947 msg
.msg_controllen
= len
;
951 skb
= np
->pktoptions
;
953 atomic_inc(&skb
->users
);
957 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
962 if (np
->rxopt
.bits
.rxinfo
) {
963 struct in6_pktinfo src_info
;
964 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
965 np
->sticky_pktinfo
.ipi6_ifindex
;
966 np
->mcast_oif
? ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
) :
967 ipv6_addr_copy(&src_info
.ipi6_addr
, &(np
->sticky_pktinfo
.ipi6_addr
));
968 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
970 if (np
->rxopt
.bits
.rxhlim
) {
971 int hlim
= np
->mcast_hops
;
972 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
974 if (np
->rxopt
.bits
.rxoinfo
) {
975 struct in6_pktinfo src_info
;
976 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
977 np
->sticky_pktinfo
.ipi6_ifindex
;
978 np
->mcast_oif
? ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
) :
979 ipv6_addr_copy(&src_info
.ipi6_addr
, &(np
->sticky_pktinfo
.ipi6_addr
));
980 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
982 if (np
->rxopt
.bits
.rxohlim
) {
983 int hlim
= np
->mcast_hops
;
984 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
987 len
-= msg
.msg_controllen
;
988 return put_user(len
, optlen
);
992 struct dst_entry
*dst
;
996 dst
= __sk_dst_get(sk
);
1009 case IPV6_RECVPKTINFO
:
1010 val
= np
->rxopt
.bits
.rxinfo
;
1013 case IPV6_2292PKTINFO
:
1014 val
= np
->rxopt
.bits
.rxoinfo
;
1017 case IPV6_RECVHOPLIMIT
:
1018 val
= np
->rxopt
.bits
.rxhlim
;
1021 case IPV6_2292HOPLIMIT
:
1022 val
= np
->rxopt
.bits
.rxohlim
;
1025 case IPV6_RECVRTHDR
:
1026 val
= np
->rxopt
.bits
.srcrt
;
1029 case IPV6_2292RTHDR
:
1030 val
= np
->rxopt
.bits
.osrcrt
;
1034 case IPV6_RTHDRDSTOPTS
:
1040 len
= ipv6_getsockopt_sticky(sk
, np
->opt
,
1041 optname
, optval
, len
);
1043 /* check if ipv6_getsockopt_sticky() returns err code */
1046 return put_user(len
, optlen
);
1049 case IPV6_RECVHOPOPTS
:
1050 val
= np
->rxopt
.bits
.hopopts
;
1053 case IPV6_2292HOPOPTS
:
1054 val
= np
->rxopt
.bits
.ohopopts
;
1057 case IPV6_RECVDSTOPTS
:
1058 val
= np
->rxopt
.bits
.dstopts
;
1061 case IPV6_2292DSTOPTS
:
1062 val
= np
->rxopt
.bits
.odstopts
;
1069 case IPV6_RECVTCLASS
:
1070 val
= np
->rxopt
.bits
.rxtclass
;
1074 val
= np
->rxopt
.bits
.rxflow
;
1077 case IPV6_RECVPATHMTU
:
1078 val
= np
->rxopt
.bits
.rxpmtu
;
1083 struct dst_entry
*dst
;
1084 struct ip6_mtuinfo mtuinfo
;
1086 if (len
< sizeof(mtuinfo
))
1089 len
= sizeof(mtuinfo
);
1090 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1093 dst
= __sk_dst_get(sk
);
1095 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1097 if (!mtuinfo
.ip6m_mtu
)
1100 if (put_user(len
, optlen
))
1102 if (copy_to_user(optval
, &mtuinfo
, len
))
1109 case IPV6_UNICAST_HOPS
:
1110 case IPV6_MULTICAST_HOPS
:
1112 struct dst_entry
*dst
;
1114 if (optname
== IPV6_UNICAST_HOPS
)
1115 val
= np
->hop_limit
;
1117 val
= np
->mcast_hops
;
1121 dst
= __sk_dst_get(sk
);
1123 val
= ip6_dst_hoplimit(dst
);
1128 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1132 case IPV6_MULTICAST_LOOP
:
1136 case IPV6_MULTICAST_IF
:
1137 val
= np
->mcast_oif
;
1140 case IPV6_MTU_DISCOVER
:
1148 case IPV6_FLOWINFO_SEND
:
1152 case IPV6_ADDR_PREFERENCES
:
1155 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1156 val
|= IPV6_PREFER_SRC_TMP
;
1157 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1158 val
|= IPV6_PREFER_SRC_PUBLIC
;
1160 /* XXX: should we return system default? */
1161 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1164 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1165 val
|= IPV6_PREFER_SRC_COA
;
1167 val
|= IPV6_PREFER_SRC_HOME
;
1170 case IPV6_MINHOPCOUNT
:
1171 val
= np
->min_hopcount
;
1179 return -ENOPROTOOPT
;
1181 len
= min_t(unsigned int, sizeof(int), len
);
1182 if(put_user(len
, optlen
))
1184 if(copy_to_user(optval
,&val
,len
))
1189 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1190 char __user
*optval
, int __user
*optlen
)
1194 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1195 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1197 if(level
!= SOL_IPV6
)
1198 return -ENOPROTOOPT
;
1200 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1201 #ifdef CONFIG_NETFILTER
1202 /* we need to exclude all possible ENOPROTOOPTs except default case */
1203 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1206 if (get_user(len
, optlen
))
1210 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1214 err
= put_user(len
, optlen
);
1220 EXPORT_SYMBOL(ipv6_getsockopt
);
1222 #ifdef CONFIG_COMPAT
1223 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1224 char __user
*optval
, int __user
*optlen
)
1228 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1229 if (udp_prot
.compat_getsockopt
!= NULL
)
1230 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1232 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1235 if (level
!= SOL_IPV6
)
1236 return -ENOPROTOOPT
;
1238 if (optname
== MCAST_MSFILTER
)
1239 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1242 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1243 #ifdef CONFIG_NETFILTER
1244 /* we need to exclude all possible ENOPROTOOPTs except default case */
1245 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1248 if (get_user(len
, optlen
))
1252 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1253 optname
, optval
, &len
);
1256 err
= put_user(len
, optlen
);
1262 EXPORT_SYMBOL(compat_ipv6_getsockopt
);