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
);
114 opt
= xchg(&inet6_sk(sk
)->opt
, opt
);
120 static int do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
121 char __user
*optval
, unsigned int optlen
)
123 struct ipv6_pinfo
*np
= inet6_sk(sk
);
124 struct net
*net
= sock_net(sk
);
126 int retv
= -ENOPROTOOPT
;
131 if (optlen
>= sizeof(int)) {
132 if (get_user(val
, (int __user
*) optval
))
138 valbool
= (val
!= 0);
140 if (ip6_mroute_opt(optname
))
141 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
148 if (optlen
< sizeof(int))
150 if (val
== PF_INET
) {
151 struct ipv6_txoptions
*opt
;
152 struct sk_buff
*pktopt
;
154 if (sk
->sk_type
== SOCK_RAW
)
157 if (sk
->sk_protocol
== IPPROTO_UDP
||
158 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
159 struct udp_sock
*up
= udp_sk(sk
);
160 if (up
->pending
== AF_INET6
) {
164 } else if (sk
->sk_protocol
!= IPPROTO_TCP
)
167 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
172 if (ipv6_only_sock(sk
) ||
173 !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
174 retv
= -EADDRNOTAVAIL
;
178 fl6_free_socklist(sk
);
179 ipv6_sock_mc_close(sk
);
182 * Sock is moving from IPv6 to IPv4 (sk_prot), so
183 * remove it from the refcnt debug socks count in the
186 sk_refcnt_debug_dec(sk
);
188 if (sk
->sk_protocol
== IPPROTO_TCP
) {
189 struct inet_connection_sock
*icsk
= inet_csk(sk
);
191 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
192 sock_prot_inuse_add(net
, &tcp_prot
, 1);
194 sk
->sk_prot
= &tcp_prot
;
195 icsk
->icsk_af_ops
= &ipv4_specific
;
196 sk
->sk_socket
->ops
= &inet_stream_ops
;
197 sk
->sk_family
= PF_INET
;
198 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
200 struct proto
*prot
= &udp_prot
;
202 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
203 prot
= &udplite_prot
;
205 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
206 sock_prot_inuse_add(net
, prot
, 1);
209 sk
->sk_socket
->ops
= &inet_dgram_ops
;
210 sk
->sk_family
= PF_INET
;
212 opt
= xchg(&np
->opt
, NULL
);
214 sock_kfree_s(sk
, opt
, opt
->tot_len
);
215 pktopt
= xchg(&np
->pktoptions
, NULL
);
218 sk
->sk_destruct
= inet_sock_destruct
;
220 * ... and add it to the refcnt debug socks count
221 * in the new family. -acme
223 sk_refcnt_debug_inc(sk
);
224 module_put(THIS_MODULE
);
231 if (optlen
< sizeof(int) ||
232 inet_sk(sk
)->inet_num
)
234 sk
->sk_ipv6only
= valbool
;
238 case IPV6_RECVPKTINFO
:
239 if (optlen
< sizeof(int))
241 np
->rxopt
.bits
.rxinfo
= valbool
;
245 case IPV6_2292PKTINFO
:
246 if (optlen
< sizeof(int))
248 np
->rxopt
.bits
.rxoinfo
= valbool
;
252 case IPV6_RECVHOPLIMIT
:
253 if (optlen
< sizeof(int))
255 np
->rxopt
.bits
.rxhlim
= valbool
;
259 case IPV6_2292HOPLIMIT
:
260 if (optlen
< sizeof(int))
262 np
->rxopt
.bits
.rxohlim
= valbool
;
267 if (optlen
< sizeof(int))
269 np
->rxopt
.bits
.srcrt
= valbool
;
274 if (optlen
< sizeof(int))
276 np
->rxopt
.bits
.osrcrt
= valbool
;
280 case IPV6_RECVHOPOPTS
:
281 if (optlen
< sizeof(int))
283 np
->rxopt
.bits
.hopopts
= valbool
;
287 case IPV6_2292HOPOPTS
:
288 if (optlen
< sizeof(int))
290 np
->rxopt
.bits
.ohopopts
= valbool
;
294 case IPV6_RECVDSTOPTS
:
295 if (optlen
< sizeof(int))
297 np
->rxopt
.bits
.dstopts
= valbool
;
301 case IPV6_2292DSTOPTS
:
302 if (optlen
< sizeof(int))
304 np
->rxopt
.bits
.odstopts
= valbool
;
309 if (optlen
< sizeof(int))
311 if (val
< -1 || val
> 0xff)
313 /* RFC 3542, 6.5: default traffic class of 0x0 */
320 case IPV6_RECVTCLASS
:
321 if (optlen
< sizeof(int))
323 np
->rxopt
.bits
.rxtclass
= valbool
;
328 if (optlen
< sizeof(int))
330 np
->rxopt
.bits
.rxflow
= valbool
;
334 case IPV6_RECVPATHMTU
:
335 if (optlen
< sizeof(int))
337 np
->rxopt
.bits
.rxpmtu
= valbool
;
341 case IPV6_TRANSPARENT
:
342 if (valbool
&& !ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
343 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
347 if (optlen
< sizeof(int))
349 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
350 inet_sk(sk
)->transparent
= valbool
;
354 case IPV6_RECVORIGDSTADDR
:
355 if (optlen
< sizeof(int))
357 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
362 case IPV6_RTHDRDSTOPTS
:
366 struct ipv6_txoptions
*opt
;
368 /* remove any sticky options header with a zero option
369 * length, per RFC3542.
373 else if (optval
== NULL
)
375 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
376 optlen
& 0x7 || optlen
> 8 * 255)
379 /* hop-by-hop / destination options are privileged option */
381 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
384 opt
= ipv6_renew_options(sk
, np
->opt
, optname
,
385 (struct ipv6_opt_hdr __user
*)optval
,
392 /* routing header option needs extra check */
394 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
395 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
396 switch (rthdr
->type
) {
397 #if IS_ENABLED(CONFIG_IPV6_MIP6)
398 case IPV6_SRCRT_TYPE_2
:
399 if (rthdr
->hdrlen
!= 2 ||
400 rthdr
->segments_left
!= 1)
411 opt
= ipv6_update_options(sk
, opt
);
414 sock_kfree_s(sk
, opt
, opt
->tot_len
);
420 struct in6_pktinfo pkt
;
424 else if (optlen
< sizeof(struct in6_pktinfo
) || optval
== NULL
)
427 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
431 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
434 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
435 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
440 case IPV6_2292PKTOPTIONS
:
442 struct ipv6_txoptions
*opt
= NULL
;
447 memset(&fl6
, 0, sizeof(fl6
));
448 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
449 fl6
.flowi6_mark
= sk
->sk_mark
;
454 /* 1K is probably excessive
455 * 1K is surely not enough, 2K per standard header is 16K.
458 if (optlen
> 64*1024)
461 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
466 memset(opt
, 0, sizeof(*opt
));
467 opt
->tot_len
= sizeof(*opt
) + optlen
;
469 if (copy_from_user(opt
+1, optval
, optlen
))
472 msg
.msg_controllen
= optlen
;
473 msg
.msg_control
= (void *)(opt
+1);
475 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, opt
, &junk
,
481 opt
= ipv6_update_options(sk
, opt
);
484 sock_kfree_s(sk
, opt
, opt
->tot_len
);
487 case IPV6_UNICAST_HOPS
:
488 if (optlen
< sizeof(int))
490 if (val
> 255 || val
< -1)
496 case IPV6_MULTICAST_HOPS
:
497 if (sk
->sk_type
== SOCK_STREAM
)
499 if (optlen
< sizeof(int))
501 if (val
> 255 || val
< -1)
503 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
507 case IPV6_MULTICAST_LOOP
:
508 if (optlen
< sizeof(int))
512 np
->mc_loop
= valbool
;
516 case IPV6_UNICAST_IF
:
518 struct net_device
*dev
= NULL
;
521 if (optlen
!= sizeof(int))
524 ifindex
= (__force
int)ntohl((__force __be32
)val
);
531 dev
= dev_get_by_index(net
, ifindex
);
532 retv
= -EADDRNOTAVAIL
;
538 if (sk
->sk_bound_dev_if
)
541 np
->ucast_oif
= ifindex
;
546 case IPV6_MULTICAST_IF
:
547 if (sk
->sk_type
== SOCK_STREAM
)
549 if (optlen
< sizeof(int))
553 struct net_device
*dev
;
555 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
558 dev
= dev_get_by_index(net
, val
);
568 case IPV6_ADD_MEMBERSHIP
:
569 case IPV6_DROP_MEMBERSHIP
:
571 struct ipv6_mreq mreq
;
573 if (optlen
< sizeof(struct ipv6_mreq
))
577 if (inet_sk(sk
)->is_icsk
)
581 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
584 if (optname
== IPV6_ADD_MEMBERSHIP
)
585 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
587 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
590 case IPV6_JOIN_ANYCAST
:
591 case IPV6_LEAVE_ANYCAST
:
593 struct ipv6_mreq mreq
;
595 if (optlen
< sizeof(struct ipv6_mreq
))
599 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
602 if (optname
== IPV6_JOIN_ANYCAST
)
603 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
605 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
608 case MCAST_JOIN_GROUP
:
609 case MCAST_LEAVE_GROUP
:
611 struct group_req greq
;
612 struct sockaddr_in6
*psin6
;
614 if (optlen
< sizeof(struct group_req
))
618 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
620 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
621 retv
= -EADDRNOTAVAIL
;
624 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
625 if (optname
== MCAST_JOIN_GROUP
)
626 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
629 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
633 case MCAST_JOIN_SOURCE_GROUP
:
634 case MCAST_LEAVE_SOURCE_GROUP
:
635 case MCAST_BLOCK_SOURCE
:
636 case MCAST_UNBLOCK_SOURCE
:
638 struct group_source_req greqs
;
641 if (optlen
< sizeof(struct group_source_req
))
643 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
647 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
648 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
649 retv
= -EADDRNOTAVAIL
;
652 if (optname
== MCAST_BLOCK_SOURCE
) {
653 omode
= MCAST_EXCLUDE
;
655 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
656 omode
= MCAST_EXCLUDE
;
658 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
659 struct sockaddr_in6
*psin6
;
661 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
662 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
664 /* prior join w/ different source is ok */
665 if (retv
&& retv
!= -EADDRINUSE
)
667 omode
= MCAST_INCLUDE
;
669 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
670 omode
= MCAST_INCLUDE
;
673 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
678 struct group_filter
*gsf
;
680 if (optlen
< GROUP_FILTER_SIZE(0))
682 if (optlen
> sysctl_optmem_max
) {
686 gsf
= kmalloc(optlen
, GFP_KERNEL
);
692 if (copy_from_user(gsf
, optval
, optlen
)) {
696 /* numsrc >= (4G-140)/128 overflow in 32 bits */
697 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
698 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
703 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
708 retv
= ip6_mc_msfilter(sk
, gsf
);
713 case IPV6_ROUTER_ALERT
:
714 if (optlen
< sizeof(int))
716 retv
= ip6_ra_control(sk
, val
);
718 case IPV6_MTU_DISCOVER
:
719 if (optlen
< sizeof(int))
721 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
727 if (optlen
< sizeof(int))
729 if (val
&& val
< IPV6_MIN_MTU
)
735 if (optlen
< sizeof(int))
737 np
->recverr
= valbool
;
739 skb_queue_purge(&sk
->sk_error_queue
);
742 case IPV6_FLOWINFO_SEND
:
743 if (optlen
< sizeof(int))
745 np
->sndflow
= valbool
;
748 case IPV6_FLOWLABEL_MGR
:
749 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
751 case IPV6_IPSEC_POLICY
:
752 case IPV6_XFRM_POLICY
:
754 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
756 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
759 case IPV6_ADDR_PREFERENCES
:
761 unsigned int pref
= 0;
762 unsigned int prefmask
= ~0;
764 if (optlen
< sizeof(int))
769 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
770 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
772 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
773 case IPV6_PREFER_SRC_PUBLIC
:
774 pref
|= IPV6_PREFER_SRC_PUBLIC
;
776 case IPV6_PREFER_SRC_TMP
:
777 pref
|= IPV6_PREFER_SRC_TMP
;
779 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
782 goto pref_skip_pubtmp
;
787 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
788 IPV6_PREFER_SRC_TMP
);
791 /* check HOME/COA conflicts */
792 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
793 case IPV6_PREFER_SRC_HOME
:
795 case IPV6_PREFER_SRC_COA
:
796 pref
|= IPV6_PREFER_SRC_COA
;
803 prefmask
&= ~IPV6_PREFER_SRC_COA
;
806 /* check CGA/NONCGA conflicts */
807 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
808 case IPV6_PREFER_SRC_CGA
:
809 case IPV6_PREFER_SRC_NONCGA
:
816 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
821 case IPV6_MINHOPCOUNT
:
822 if (optlen
< sizeof(int))
824 if (val
< 0 || val
> 255)
826 np
->min_hopcount
= val
;
830 np
->dontfrag
= valbool
;
833 case IPV6_AUTOFLOWLABEL
:
834 np
->autoflowlabel
= valbool
;
848 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
849 char __user
*optval
, unsigned int optlen
)
853 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
854 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
856 if (level
!= SOL_IPV6
)
859 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
860 #ifdef CONFIG_NETFILTER
861 /* we need to exclude all possible ENOPROTOOPTs except default case */
862 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
863 optname
!= IPV6_XFRM_POLICY
) {
865 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
872 EXPORT_SYMBOL(ipv6_setsockopt
);
875 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
876 char __user
*optval
, unsigned int optlen
)
880 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
881 if (udp_prot
.compat_setsockopt
!= NULL
)
882 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
884 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
887 if (level
!= SOL_IPV6
)
890 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
891 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
894 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
895 #ifdef CONFIG_NETFILTER
896 /* we need to exclude all possible ENOPROTOOPTs except default case */
897 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
898 optname
!= IPV6_XFRM_POLICY
) {
900 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
907 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
910 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
911 int optname
, char __user
*optval
, int len
)
913 struct ipv6_opt_hdr
*hdr
;
922 case IPV6_RTHDRDSTOPTS
:
926 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
932 return -EINVAL
; /* should not happen */
938 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
939 if (copy_to_user(optval
, hdr
, len
))
944 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
945 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
947 struct ipv6_pinfo
*np
= inet6_sk(sk
);
951 if (ip6_mroute_opt(optname
))
952 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
954 if (get_user(len
, optlen
))
958 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
959 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
960 sk
->sk_protocol
!= IPPROTO_TCP
)
962 if (sk
->sk_state
!= TCP_ESTABLISHED
)
968 struct group_filter gsf
;
971 if (len
< GROUP_FILTER_SIZE(0))
973 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
975 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
976 return -EADDRNOTAVAIL
;
978 err
= ip6_mc_msfget(sk
, &gsf
,
979 (struct group_filter __user
*)optval
, optlen
);
984 case IPV6_2292PKTOPTIONS
:
989 if (sk
->sk_type
!= SOCK_STREAM
)
992 msg
.msg_control
= optval
;
993 msg
.msg_controllen
= len
;
994 msg
.msg_flags
= flags
;
997 skb
= np
->pktoptions
;
999 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1002 if (np
->rxopt
.bits
.rxinfo
) {
1003 struct in6_pktinfo src_info
;
1004 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1005 np
->sticky_pktinfo
.ipi6_ifindex
;
1006 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1007 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1009 if (np
->rxopt
.bits
.rxhlim
) {
1010 int hlim
= np
->mcast_hops
;
1011 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1013 if (np
->rxopt
.bits
.rxtclass
) {
1014 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1016 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1018 if (np
->rxopt
.bits
.rxoinfo
) {
1019 struct in6_pktinfo src_info
;
1020 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1021 np
->sticky_pktinfo
.ipi6_ifindex
;
1022 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1023 np
->sticky_pktinfo
.ipi6_addr
;
1024 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1026 if (np
->rxopt
.bits
.rxohlim
) {
1027 int hlim
= np
->mcast_hops
;
1028 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1030 if (np
->rxopt
.bits
.rxflow
) {
1031 __be32 flowinfo
= np
->rcv_flowinfo
;
1033 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1036 len
-= msg
.msg_controllen
;
1037 return put_user(len
, optlen
);
1041 struct dst_entry
*dst
;
1045 dst
= __sk_dst_get(sk
);
1055 val
= sk
->sk_ipv6only
;
1058 case IPV6_RECVPKTINFO
:
1059 val
= np
->rxopt
.bits
.rxinfo
;
1062 case IPV6_2292PKTINFO
:
1063 val
= np
->rxopt
.bits
.rxoinfo
;
1066 case IPV6_RECVHOPLIMIT
:
1067 val
= np
->rxopt
.bits
.rxhlim
;
1070 case IPV6_2292HOPLIMIT
:
1071 val
= np
->rxopt
.bits
.rxohlim
;
1074 case IPV6_RECVRTHDR
:
1075 val
= np
->rxopt
.bits
.srcrt
;
1078 case IPV6_2292RTHDR
:
1079 val
= np
->rxopt
.bits
.osrcrt
;
1083 case IPV6_RTHDRDSTOPTS
:
1089 len
= ipv6_getsockopt_sticky(sk
, np
->opt
,
1090 optname
, optval
, len
);
1092 /* check if ipv6_getsockopt_sticky() returns err code */
1095 return put_user(len
, optlen
);
1098 case IPV6_RECVHOPOPTS
:
1099 val
= np
->rxopt
.bits
.hopopts
;
1102 case IPV6_2292HOPOPTS
:
1103 val
= np
->rxopt
.bits
.ohopopts
;
1106 case IPV6_RECVDSTOPTS
:
1107 val
= np
->rxopt
.bits
.dstopts
;
1110 case IPV6_2292DSTOPTS
:
1111 val
= np
->rxopt
.bits
.odstopts
;
1118 case IPV6_RECVTCLASS
:
1119 val
= np
->rxopt
.bits
.rxtclass
;
1123 val
= np
->rxopt
.bits
.rxflow
;
1126 case IPV6_RECVPATHMTU
:
1127 val
= np
->rxopt
.bits
.rxpmtu
;
1132 struct dst_entry
*dst
;
1133 struct ip6_mtuinfo mtuinfo
;
1135 if (len
< sizeof(mtuinfo
))
1138 len
= sizeof(mtuinfo
);
1139 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1142 dst
= __sk_dst_get(sk
);
1144 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1146 if (!mtuinfo
.ip6m_mtu
)
1149 if (put_user(len
, optlen
))
1151 if (copy_to_user(optval
, &mtuinfo
, len
))
1157 case IPV6_TRANSPARENT
:
1158 val
= inet_sk(sk
)->transparent
;
1161 case IPV6_RECVORIGDSTADDR
:
1162 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1165 case IPV6_UNICAST_HOPS
:
1166 case IPV6_MULTICAST_HOPS
:
1168 struct dst_entry
*dst
;
1170 if (optname
== IPV6_UNICAST_HOPS
)
1171 val
= np
->hop_limit
;
1173 val
= np
->mcast_hops
;
1177 dst
= __sk_dst_get(sk
);
1179 val
= ip6_dst_hoplimit(dst
);
1184 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1188 case IPV6_MULTICAST_LOOP
:
1192 case IPV6_MULTICAST_IF
:
1193 val
= np
->mcast_oif
;
1196 case IPV6_UNICAST_IF
:
1197 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1200 case IPV6_MTU_DISCOVER
:
1208 case IPV6_FLOWINFO_SEND
:
1212 case IPV6_FLOWLABEL_MGR
:
1214 struct in6_flowlabel_req freq
;
1217 if (len
< sizeof(freq
))
1220 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1223 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1227 flags
= freq
.flr_flags
;
1229 memset(&freq
, 0, sizeof(freq
));
1231 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1235 if (put_user(len
, optlen
))
1237 if (copy_to_user(optval
, &freq
, len
))
1243 case IPV6_ADDR_PREFERENCES
:
1246 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1247 val
|= IPV6_PREFER_SRC_TMP
;
1248 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1249 val
|= IPV6_PREFER_SRC_PUBLIC
;
1251 /* XXX: should we return system default? */
1252 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1255 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1256 val
|= IPV6_PREFER_SRC_COA
;
1258 val
|= IPV6_PREFER_SRC_HOME
;
1261 case IPV6_MINHOPCOUNT
:
1262 val
= np
->min_hopcount
;
1269 case IPV6_AUTOFLOWLABEL
:
1270 val
= np
->autoflowlabel
;
1274 return -ENOPROTOOPT
;
1276 len
= min_t(unsigned int, sizeof(int), len
);
1277 if (put_user(len
, optlen
))
1279 if (copy_to_user(optval
, &val
, len
))
1284 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1285 char __user
*optval
, int __user
*optlen
)
1289 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1290 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1292 if (level
!= SOL_IPV6
)
1293 return -ENOPROTOOPT
;
1295 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1296 #ifdef CONFIG_NETFILTER
1297 /* we need to exclude all possible ENOPROTOOPTs except default case */
1298 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1301 if (get_user(len
, optlen
))
1305 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1309 err
= put_user(len
, optlen
);
1314 EXPORT_SYMBOL(ipv6_getsockopt
);
1316 #ifdef CONFIG_COMPAT
1317 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1318 char __user
*optval
, int __user
*optlen
)
1322 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1323 if (udp_prot
.compat_getsockopt
!= NULL
)
1324 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1326 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1329 if (level
!= SOL_IPV6
)
1330 return -ENOPROTOOPT
;
1332 if (optname
== MCAST_MSFILTER
)
1333 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1336 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1338 #ifdef CONFIG_NETFILTER
1339 /* we need to exclude all possible ENOPROTOOPTs except default case */
1340 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1343 if (get_user(len
, optlen
))
1347 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1348 optname
, optval
, &len
);
1351 err
= put_user(len
, optlen
);
1356 EXPORT_SYMBOL(compat_ipv6_getsockopt
);