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>
57 #include <linux/uaccess.h>
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
)->inet_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
);
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((__force
struct ipv6_txoptions
**)&inet6_sk(sk
)->opt
,
121 static bool setsockopt_needs_rtnl(int optname
)
125 case IPV6_ADD_MEMBERSHIP
:
126 case IPV6_DROP_MEMBERSHIP
:
127 case IPV6_JOIN_ANYCAST
:
128 case IPV6_LEAVE_ANYCAST
:
129 case MCAST_JOIN_GROUP
:
130 case MCAST_LEAVE_GROUP
:
131 case MCAST_JOIN_SOURCE_GROUP
:
132 case MCAST_LEAVE_SOURCE_GROUP
:
133 case MCAST_BLOCK_SOURCE
:
134 case MCAST_UNBLOCK_SOURCE
:
141 static int do_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
142 char __user
*optval
, unsigned int optlen
)
144 struct ipv6_pinfo
*np
= inet6_sk(sk
);
145 struct net
*net
= sock_net(sk
);
147 int retv
= -ENOPROTOOPT
;
148 bool needs_rtnl
= setsockopt_needs_rtnl(optname
);
153 if (optlen
>= sizeof(int)) {
154 if (get_user(val
, (int __user
*) optval
))
160 valbool
= (val
!= 0);
162 if (ip6_mroute_opt(optname
))
163 return ip6_mroute_setsockopt(sk
, optname
, optval
, optlen
);
172 if (optlen
< sizeof(int))
174 if (val
== PF_INET
) {
175 struct ipv6_txoptions
*opt
;
176 struct sk_buff
*pktopt
;
178 if (sk
->sk_type
== SOCK_RAW
)
181 if (sk
->sk_protocol
== IPPROTO_UDP
||
182 sk
->sk_protocol
== IPPROTO_UDPLITE
) {
183 struct udp_sock
*up
= udp_sk(sk
);
184 if (up
->pending
== AF_INET6
) {
188 } else if (sk
->sk_protocol
!= IPPROTO_TCP
)
191 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
196 if (ipv6_only_sock(sk
) ||
197 !ipv6_addr_v4mapped(&sk
->sk_v6_daddr
)) {
198 retv
= -EADDRNOTAVAIL
;
202 fl6_free_socklist(sk
);
203 __ipv6_sock_mc_close(sk
);
206 * Sock is moving from IPv6 to IPv4 (sk_prot), so
207 * remove it from the refcnt debug socks count in the
210 sk_refcnt_debug_dec(sk
);
212 if (sk
->sk_protocol
== IPPROTO_TCP
) {
213 struct inet_connection_sock
*icsk
= inet_csk(sk
);
215 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
216 sock_prot_inuse_add(net
, &tcp_prot
, 1);
218 sk
->sk_prot
= &tcp_prot
;
219 icsk
->icsk_af_ops
= &ipv4_specific
;
220 sk
->sk_socket
->ops
= &inet_stream_ops
;
221 sk
->sk_family
= PF_INET
;
222 tcp_sync_mss(sk
, icsk
->icsk_pmtu_cookie
);
224 struct proto
*prot
= &udp_prot
;
226 if (sk
->sk_protocol
== IPPROTO_UDPLITE
)
227 prot
= &udplite_prot
;
229 sock_prot_inuse_add(net
, sk
->sk_prot
, -1);
230 sock_prot_inuse_add(net
, prot
, 1);
233 sk
->sk_socket
->ops
= &inet_dgram_ops
;
234 sk
->sk_family
= PF_INET
;
236 opt
= xchg((__force
struct ipv6_txoptions
**)&np
->opt
,
239 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
242 pktopt
= xchg(&np
->pktoptions
, NULL
);
245 sk
->sk_destruct
= inet_sock_destruct
;
247 * ... and add it to the refcnt debug socks count
248 * in the new family. -acme
250 sk_refcnt_debug_inc(sk
);
251 module_put(THIS_MODULE
);
258 if (optlen
< sizeof(int) ||
259 inet_sk(sk
)->inet_num
)
261 sk
->sk_ipv6only
= valbool
;
265 case IPV6_RECVPKTINFO
:
266 if (optlen
< sizeof(int))
268 np
->rxopt
.bits
.rxinfo
= valbool
;
272 case IPV6_2292PKTINFO
:
273 if (optlen
< sizeof(int))
275 np
->rxopt
.bits
.rxoinfo
= valbool
;
279 case IPV6_RECVHOPLIMIT
:
280 if (optlen
< sizeof(int))
282 np
->rxopt
.bits
.rxhlim
= valbool
;
286 case IPV6_2292HOPLIMIT
:
287 if (optlen
< sizeof(int))
289 np
->rxopt
.bits
.rxohlim
= valbool
;
294 if (optlen
< sizeof(int))
296 np
->rxopt
.bits
.srcrt
= valbool
;
301 if (optlen
< sizeof(int))
303 np
->rxopt
.bits
.osrcrt
= valbool
;
307 case IPV6_RECVHOPOPTS
:
308 if (optlen
< sizeof(int))
310 np
->rxopt
.bits
.hopopts
= valbool
;
314 case IPV6_2292HOPOPTS
:
315 if (optlen
< sizeof(int))
317 np
->rxopt
.bits
.ohopopts
= valbool
;
321 case IPV6_RECVDSTOPTS
:
322 if (optlen
< sizeof(int))
324 np
->rxopt
.bits
.dstopts
= valbool
;
328 case IPV6_2292DSTOPTS
:
329 if (optlen
< sizeof(int))
331 np
->rxopt
.bits
.odstopts
= valbool
;
336 if (optlen
< sizeof(int))
338 if (val
< -1 || val
> 0xff)
340 /* RFC 3542, 6.5: default traffic class of 0x0 */
347 case IPV6_RECVTCLASS
:
348 if (optlen
< sizeof(int))
350 np
->rxopt
.bits
.rxtclass
= valbool
;
355 if (optlen
< sizeof(int))
357 np
->rxopt
.bits
.rxflow
= valbool
;
361 case IPV6_RECVPATHMTU
:
362 if (optlen
< sizeof(int))
364 np
->rxopt
.bits
.rxpmtu
= valbool
;
368 case IPV6_TRANSPARENT
:
369 if (valbool
&& !ns_capable(net
->user_ns
, CAP_NET_ADMIN
) &&
370 !ns_capable(net
->user_ns
, CAP_NET_RAW
)) {
374 if (optlen
< sizeof(int))
376 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
377 inet_sk(sk
)->transparent
= valbool
;
381 case IPV6_RECVORIGDSTADDR
:
382 if (optlen
< sizeof(int))
384 np
->rxopt
.bits
.rxorigdstaddr
= valbool
;
389 case IPV6_RTHDRDSTOPTS
:
393 struct ipv6_txoptions
*opt
;
395 /* remove any sticky options header with a zero option
396 * length, per RFC3542.
402 else if (optlen
< sizeof(struct ipv6_opt_hdr
) ||
403 optlen
& 0x7 || optlen
> 8 * 255)
406 /* hop-by-hop / destination options are privileged option */
408 if (optname
!= IPV6_RTHDR
&& !ns_capable(net
->user_ns
, CAP_NET_RAW
))
411 opt
= rcu_dereference_protected(np
->opt
,
412 lockdep_sock_is_held(sk
));
413 opt
= ipv6_renew_options(sk
, opt
, optname
,
414 (struct ipv6_opt_hdr __user
*)optval
,
421 /* routing header option needs extra check */
423 if (optname
== IPV6_RTHDR
&& opt
&& opt
->srcrt
) {
424 struct ipv6_rt_hdr
*rthdr
= opt
->srcrt
;
425 switch (rthdr
->type
) {
426 #if IS_ENABLED(CONFIG_IPV6_MIP6)
427 case IPV6_SRCRT_TYPE_2
:
428 if (rthdr
->hdrlen
!= 2 ||
429 rthdr
->segments_left
!= 1)
434 case IPV6_SRCRT_TYPE_4
:
436 struct ipv6_sr_hdr
*srh
= (struct ipv6_sr_hdr
*)
439 if (!seg6_validate_srh(srh
, optlen
))
449 opt
= ipv6_update_options(sk
, opt
);
452 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
460 struct in6_pktinfo pkt
;
464 else if (optlen
< sizeof(struct in6_pktinfo
) || !optval
)
467 if (copy_from_user(&pkt
, optval
, sizeof(struct in6_pktinfo
))) {
471 if (sk
->sk_bound_dev_if
&& pkt
.ipi6_ifindex
!= sk
->sk_bound_dev_if
)
474 np
->sticky_pktinfo
.ipi6_ifindex
= pkt
.ipi6_ifindex
;
475 np
->sticky_pktinfo
.ipi6_addr
= pkt
.ipi6_addr
;
480 case IPV6_2292PKTOPTIONS
:
482 struct ipv6_txoptions
*opt
= NULL
;
485 struct sockcm_cookie sockc_junk
;
486 struct ipcm6_cookie ipc6
;
488 memset(&fl6
, 0, sizeof(fl6
));
489 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
490 fl6
.flowi6_mark
= sk
->sk_mark
;
495 /* 1K is probably excessive
496 * 1K is surely not enough, 2K per standard header is 16K.
499 if (optlen
> 64*1024)
502 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
507 memset(opt
, 0, sizeof(*opt
));
508 atomic_set(&opt
->refcnt
, 1);
509 opt
->tot_len
= sizeof(*opt
) + optlen
;
511 if (copy_from_user(opt
+1, optval
, optlen
))
514 msg
.msg_controllen
= optlen
;
515 msg
.msg_control
= (void *)(opt
+1);
518 retv
= ip6_datagram_send_ctl(net
, sk
, &msg
, &fl6
, &ipc6
, &sockc_junk
);
523 opt
= ipv6_update_options(sk
, opt
);
526 atomic_sub(opt
->tot_len
, &sk
->sk_omem_alloc
);
531 case IPV6_UNICAST_HOPS
:
532 if (optlen
< sizeof(int))
534 if (val
> 255 || val
< -1)
540 case IPV6_MULTICAST_HOPS
:
541 if (sk
->sk_type
== SOCK_STREAM
)
543 if (optlen
< sizeof(int))
545 if (val
> 255 || val
< -1)
547 np
->mcast_hops
= (val
== -1 ? IPV6_DEFAULT_MCASTHOPS
: val
);
551 case IPV6_MULTICAST_LOOP
:
552 if (optlen
< sizeof(int))
556 np
->mc_loop
= valbool
;
560 case IPV6_UNICAST_IF
:
562 struct net_device
*dev
= NULL
;
565 if (optlen
!= sizeof(int))
568 ifindex
= (__force
int)ntohl((__force __be32
)val
);
575 dev
= dev_get_by_index(net
, ifindex
);
576 retv
= -EADDRNOTAVAIL
;
582 if (sk
->sk_bound_dev_if
)
585 np
->ucast_oif
= ifindex
;
590 case IPV6_MULTICAST_IF
:
591 if (sk
->sk_type
== SOCK_STREAM
)
593 if (optlen
< sizeof(int))
597 struct net_device
*dev
;
602 dev
= dev_get_by_index_rcu(net
, val
);
608 midx
= l3mdev_master_ifindex_rcu(dev
);
612 if (sk
->sk_bound_dev_if
&&
613 sk
->sk_bound_dev_if
!= val
&&
614 (!midx
|| midx
!= sk
->sk_bound_dev_if
))
620 case IPV6_ADD_MEMBERSHIP
:
621 case IPV6_DROP_MEMBERSHIP
:
623 struct ipv6_mreq mreq
;
625 if (optlen
< sizeof(struct ipv6_mreq
))
629 if (inet_sk(sk
)->is_icsk
)
633 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
636 if (optname
== IPV6_ADD_MEMBERSHIP
)
637 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
639 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
642 case IPV6_JOIN_ANYCAST
:
643 case IPV6_LEAVE_ANYCAST
:
645 struct ipv6_mreq mreq
;
647 if (optlen
< sizeof(struct ipv6_mreq
))
651 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
654 if (optname
== IPV6_JOIN_ANYCAST
)
655 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
657 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
660 case MCAST_JOIN_GROUP
:
661 case MCAST_LEAVE_GROUP
:
663 struct group_req greq
;
664 struct sockaddr_in6
*psin6
;
666 if (optlen
< sizeof(struct group_req
))
670 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
672 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
673 retv
= -EADDRNOTAVAIL
;
676 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
677 if (optname
== MCAST_JOIN_GROUP
)
678 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
681 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
685 case MCAST_JOIN_SOURCE_GROUP
:
686 case MCAST_LEAVE_SOURCE_GROUP
:
687 case MCAST_BLOCK_SOURCE
:
688 case MCAST_UNBLOCK_SOURCE
:
690 struct group_source_req greqs
;
693 if (optlen
< sizeof(struct group_source_req
))
695 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
699 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
700 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
701 retv
= -EADDRNOTAVAIL
;
704 if (optname
== MCAST_BLOCK_SOURCE
) {
705 omode
= MCAST_EXCLUDE
;
707 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
708 omode
= MCAST_EXCLUDE
;
710 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
711 struct sockaddr_in6
*psin6
;
713 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
714 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
716 /* prior join w/ different source is ok */
717 if (retv
&& retv
!= -EADDRINUSE
)
719 omode
= MCAST_INCLUDE
;
721 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
722 omode
= MCAST_INCLUDE
;
725 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
730 struct group_filter
*gsf
;
732 if (optlen
< GROUP_FILTER_SIZE(0))
734 if (optlen
> sysctl_optmem_max
) {
738 gsf
= kmalloc(optlen
, GFP_KERNEL
);
744 if (copy_from_user(gsf
, optval
, optlen
)) {
748 /* numsrc >= (4G-140)/128 overflow in 32 bits */
749 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
750 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
755 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
760 retv
= ip6_mc_msfilter(sk
, gsf
);
765 case IPV6_ROUTER_ALERT
:
766 if (optlen
< sizeof(int))
768 retv
= ip6_ra_control(sk
, val
);
770 case IPV6_MTU_DISCOVER
:
771 if (optlen
< sizeof(int))
773 if (val
< IPV6_PMTUDISC_DONT
|| val
> IPV6_PMTUDISC_OMIT
)
779 if (optlen
< sizeof(int))
781 if (val
&& val
< IPV6_MIN_MTU
)
787 if (optlen
< sizeof(int))
789 np
->recverr
= valbool
;
791 skb_queue_purge(&sk
->sk_error_queue
);
794 case IPV6_FLOWINFO_SEND
:
795 if (optlen
< sizeof(int))
797 np
->sndflow
= valbool
;
800 case IPV6_FLOWLABEL_MGR
:
801 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
803 case IPV6_IPSEC_POLICY
:
804 case IPV6_XFRM_POLICY
:
806 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
808 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
811 case IPV6_ADDR_PREFERENCES
:
813 unsigned int pref
= 0;
814 unsigned int prefmask
= ~0;
816 if (optlen
< sizeof(int))
821 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
822 switch (val
& (IPV6_PREFER_SRC_PUBLIC
|
824 IPV6_PREFER_SRC_PUBTMP_DEFAULT
)) {
825 case IPV6_PREFER_SRC_PUBLIC
:
826 pref
|= IPV6_PREFER_SRC_PUBLIC
;
828 case IPV6_PREFER_SRC_TMP
:
829 pref
|= IPV6_PREFER_SRC_TMP
;
831 case IPV6_PREFER_SRC_PUBTMP_DEFAULT
:
834 goto pref_skip_pubtmp
;
839 prefmask
&= ~(IPV6_PREFER_SRC_PUBLIC
|
840 IPV6_PREFER_SRC_TMP
);
843 /* check HOME/COA conflicts */
844 switch (val
& (IPV6_PREFER_SRC_HOME
|IPV6_PREFER_SRC_COA
)) {
845 case IPV6_PREFER_SRC_HOME
:
847 case IPV6_PREFER_SRC_COA
:
848 pref
|= IPV6_PREFER_SRC_COA
;
855 prefmask
&= ~IPV6_PREFER_SRC_COA
;
858 /* check CGA/NONCGA conflicts */
859 switch (val
& (IPV6_PREFER_SRC_CGA
|IPV6_PREFER_SRC_NONCGA
)) {
860 case IPV6_PREFER_SRC_CGA
:
861 case IPV6_PREFER_SRC_NONCGA
:
868 np
->srcprefs
= (np
->srcprefs
& prefmask
) | pref
;
873 case IPV6_MINHOPCOUNT
:
874 if (optlen
< sizeof(int))
876 if (val
< 0 || val
> 255)
878 np
->min_hopcount
= val
;
882 np
->dontfrag
= valbool
;
885 case IPV6_AUTOFLOWLABEL
:
886 np
->autoflowlabel
= valbool
;
889 case IPV6_RECVFRAGSIZE
:
890 np
->rxopt
.bits
.recvfragsize
= valbool
;
908 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
909 char __user
*optval
, unsigned int optlen
)
913 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
914 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
916 if (level
!= SOL_IPV6
)
919 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
920 #ifdef CONFIG_NETFILTER
921 /* we need to exclude all possible ENOPROTOOPTs except default case */
922 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
923 optname
!= IPV6_XFRM_POLICY
) {
925 err
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
932 EXPORT_SYMBOL(ipv6_setsockopt
);
935 int compat_ipv6_setsockopt(struct sock
*sk
, int level
, int optname
,
936 char __user
*optval
, unsigned int optlen
)
940 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
941 if (udp_prot
.compat_setsockopt
!= NULL
)
942 return udp_prot
.compat_setsockopt(sk
, level
, optname
,
944 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
947 if (level
!= SOL_IPV6
)
950 if (optname
>= MCAST_JOIN_GROUP
&& optname
<= MCAST_MSFILTER
)
951 return compat_mc_setsockopt(sk
, level
, optname
, optval
, optlen
,
954 err
= do_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
955 #ifdef CONFIG_NETFILTER
956 /* we need to exclude all possible ENOPROTOOPTs except default case */
957 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_IPSEC_POLICY
&&
958 optname
!= IPV6_XFRM_POLICY
) {
960 err
= compat_nf_setsockopt(sk
, PF_INET6
, optname
,
967 EXPORT_SYMBOL(compat_ipv6_setsockopt
);
970 static int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_txoptions
*opt
,
971 int optname
, char __user
*optval
, int len
)
973 struct ipv6_opt_hdr
*hdr
;
982 case IPV6_RTHDRDSTOPTS
:
986 hdr
= (struct ipv6_opt_hdr
*)opt
->srcrt
;
992 return -EINVAL
; /* should not happen */
998 len
= min_t(unsigned int, len
, ipv6_optlen(hdr
));
999 if (copy_to_user(optval
, hdr
, len
))
1004 static int do_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1005 char __user
*optval
, int __user
*optlen
, unsigned int flags
)
1007 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1011 if (ip6_mroute_opt(optname
))
1012 return ip6_mroute_getsockopt(sk
, optname
, optval
, optlen
);
1014 if (get_user(len
, optlen
))
1018 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
1019 sk
->sk_protocol
!= IPPROTO_UDPLITE
&&
1020 sk
->sk_protocol
!= IPPROTO_TCP
)
1021 return -ENOPROTOOPT
;
1022 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1024 val
= sk
->sk_family
;
1026 case MCAST_MSFILTER
:
1028 struct group_filter gsf
;
1031 if (len
< GROUP_FILTER_SIZE(0))
1033 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
1035 if (gsf
.gf_group
.ss_family
!= AF_INET6
)
1036 return -EADDRNOTAVAIL
;
1038 err
= ip6_mc_msfget(sk
, &gsf
,
1039 (struct group_filter __user
*)optval
, optlen
);
1044 case IPV6_2292PKTOPTIONS
:
1047 struct sk_buff
*skb
;
1049 if (sk
->sk_type
!= SOCK_STREAM
)
1050 return -ENOPROTOOPT
;
1052 msg
.msg_control
= optval
;
1053 msg
.msg_controllen
= len
;
1054 msg
.msg_flags
= flags
;
1057 skb
= np
->pktoptions
;
1059 ip6_datagram_recv_ctl(sk
, &msg
, skb
);
1062 if (np
->rxopt
.bits
.rxinfo
) {
1063 struct in6_pktinfo src_info
;
1064 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1065 np
->sticky_pktinfo
.ipi6_ifindex
;
1066 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
: np
->sticky_pktinfo
.ipi6_addr
;
1067 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
1069 if (np
->rxopt
.bits
.rxhlim
) {
1070 int hlim
= np
->mcast_hops
;
1071 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
1073 if (np
->rxopt
.bits
.rxtclass
) {
1074 int tclass
= (int)ip6_tclass(np
->rcv_flowinfo
);
1076 put_cmsg(&msg
, SOL_IPV6
, IPV6_TCLASS
, sizeof(tclass
), &tclass
);
1078 if (np
->rxopt
.bits
.rxoinfo
) {
1079 struct in6_pktinfo src_info
;
1080 src_info
.ipi6_ifindex
= np
->mcast_oif
? np
->mcast_oif
:
1081 np
->sticky_pktinfo
.ipi6_ifindex
;
1082 src_info
.ipi6_addr
= np
->mcast_oif
? sk
->sk_v6_daddr
:
1083 np
->sticky_pktinfo
.ipi6_addr
;
1084 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
1086 if (np
->rxopt
.bits
.rxohlim
) {
1087 int hlim
= np
->mcast_hops
;
1088 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
1090 if (np
->rxopt
.bits
.rxflow
) {
1091 __be32 flowinfo
= np
->rcv_flowinfo
;
1093 put_cmsg(&msg
, SOL_IPV6
, IPV6_FLOWINFO
, sizeof(flowinfo
), &flowinfo
);
1096 len
-= msg
.msg_controllen
;
1097 return put_user(len
, optlen
);
1101 struct dst_entry
*dst
;
1105 dst
= __sk_dst_get(sk
);
1115 val
= sk
->sk_ipv6only
;
1118 case IPV6_RECVPKTINFO
:
1119 val
= np
->rxopt
.bits
.rxinfo
;
1122 case IPV6_2292PKTINFO
:
1123 val
= np
->rxopt
.bits
.rxoinfo
;
1126 case IPV6_RECVHOPLIMIT
:
1127 val
= np
->rxopt
.bits
.rxhlim
;
1130 case IPV6_2292HOPLIMIT
:
1131 val
= np
->rxopt
.bits
.rxohlim
;
1134 case IPV6_RECVRTHDR
:
1135 val
= np
->rxopt
.bits
.srcrt
;
1138 case IPV6_2292RTHDR
:
1139 val
= np
->rxopt
.bits
.osrcrt
;
1143 case IPV6_RTHDRDSTOPTS
:
1147 struct ipv6_txoptions
*opt
;
1150 opt
= rcu_dereference_protected(np
->opt
,
1151 lockdep_sock_is_held(sk
));
1152 len
= ipv6_getsockopt_sticky(sk
, opt
, optname
, optval
, len
);
1154 /* check if ipv6_getsockopt_sticky() returns err code */
1157 return put_user(len
, optlen
);
1160 case IPV6_RECVHOPOPTS
:
1161 val
= np
->rxopt
.bits
.hopopts
;
1164 case IPV6_2292HOPOPTS
:
1165 val
= np
->rxopt
.bits
.ohopopts
;
1168 case IPV6_RECVDSTOPTS
:
1169 val
= np
->rxopt
.bits
.dstopts
;
1172 case IPV6_2292DSTOPTS
:
1173 val
= np
->rxopt
.bits
.odstopts
;
1180 case IPV6_RECVTCLASS
:
1181 val
= np
->rxopt
.bits
.rxtclass
;
1185 val
= np
->rxopt
.bits
.rxflow
;
1188 case IPV6_RECVPATHMTU
:
1189 val
= np
->rxopt
.bits
.rxpmtu
;
1194 struct dst_entry
*dst
;
1195 struct ip6_mtuinfo mtuinfo
;
1197 if (len
< sizeof(mtuinfo
))
1200 len
= sizeof(mtuinfo
);
1201 memset(&mtuinfo
, 0, sizeof(mtuinfo
));
1204 dst
= __sk_dst_get(sk
);
1206 mtuinfo
.ip6m_mtu
= dst_mtu(dst
);
1208 if (!mtuinfo
.ip6m_mtu
)
1211 if (put_user(len
, optlen
))
1213 if (copy_to_user(optval
, &mtuinfo
, len
))
1219 case IPV6_TRANSPARENT
:
1220 val
= inet_sk(sk
)->transparent
;
1223 case IPV6_RECVORIGDSTADDR
:
1224 val
= np
->rxopt
.bits
.rxorigdstaddr
;
1227 case IPV6_UNICAST_HOPS
:
1228 case IPV6_MULTICAST_HOPS
:
1230 struct dst_entry
*dst
;
1232 if (optname
== IPV6_UNICAST_HOPS
)
1233 val
= np
->hop_limit
;
1235 val
= np
->mcast_hops
;
1239 dst
= __sk_dst_get(sk
);
1241 val
= ip6_dst_hoplimit(dst
);
1246 val
= sock_net(sk
)->ipv6
.devconf_all
->hop_limit
;
1250 case IPV6_MULTICAST_LOOP
:
1254 case IPV6_MULTICAST_IF
:
1255 val
= np
->mcast_oif
;
1258 case IPV6_UNICAST_IF
:
1259 val
= (__force
int)htonl((__u32
) np
->ucast_oif
);
1262 case IPV6_MTU_DISCOVER
:
1270 case IPV6_FLOWINFO_SEND
:
1274 case IPV6_FLOWLABEL_MGR
:
1276 struct in6_flowlabel_req freq
;
1279 if (len
< sizeof(freq
))
1282 if (copy_from_user(&freq
, optval
, sizeof(freq
)))
1285 if (freq
.flr_action
!= IPV6_FL_A_GET
)
1289 flags
= freq
.flr_flags
;
1291 memset(&freq
, 0, sizeof(freq
));
1293 val
= ipv6_flowlabel_opt_get(sk
, &freq
, flags
);
1297 if (put_user(len
, optlen
))
1299 if (copy_to_user(optval
, &freq
, len
))
1305 case IPV6_ADDR_PREFERENCES
:
1308 if (np
->srcprefs
& IPV6_PREFER_SRC_TMP
)
1309 val
|= IPV6_PREFER_SRC_TMP
;
1310 else if (np
->srcprefs
& IPV6_PREFER_SRC_PUBLIC
)
1311 val
|= IPV6_PREFER_SRC_PUBLIC
;
1313 /* XXX: should we return system default? */
1314 val
|= IPV6_PREFER_SRC_PUBTMP_DEFAULT
;
1317 if (np
->srcprefs
& IPV6_PREFER_SRC_COA
)
1318 val
|= IPV6_PREFER_SRC_COA
;
1320 val
|= IPV6_PREFER_SRC_HOME
;
1323 case IPV6_MINHOPCOUNT
:
1324 val
= np
->min_hopcount
;
1331 case IPV6_AUTOFLOWLABEL
:
1332 val
= np
->autoflowlabel
;
1335 case IPV6_RECVFRAGSIZE
:
1336 val
= np
->rxopt
.bits
.recvfragsize
;
1340 return -ENOPROTOOPT
;
1342 len
= min_t(unsigned int, sizeof(int), len
);
1343 if (put_user(len
, optlen
))
1345 if (copy_to_user(optval
, &val
, len
))
1350 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1351 char __user
*optval
, int __user
*optlen
)
1355 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
1356 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1358 if (level
!= SOL_IPV6
)
1359 return -ENOPROTOOPT
;
1361 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
, 0);
1362 #ifdef CONFIG_NETFILTER
1363 /* we need to exclude all possible ENOPROTOOPTs except default case */
1364 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1367 if (get_user(len
, optlen
))
1371 err
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
1375 err
= put_user(len
, optlen
);
1380 EXPORT_SYMBOL(ipv6_getsockopt
);
1382 #ifdef CONFIG_COMPAT
1383 int compat_ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1384 char __user
*optval
, int __user
*optlen
)
1388 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
) {
1389 if (udp_prot
.compat_getsockopt
!= NULL
)
1390 return udp_prot
.compat_getsockopt(sk
, level
, optname
,
1392 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
1395 if (level
!= SOL_IPV6
)
1396 return -ENOPROTOOPT
;
1398 if (optname
== MCAST_MSFILTER
)
1399 return compat_mc_getsockopt(sk
, level
, optname
, optval
, optlen
,
1402 err
= do_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
,
1404 #ifdef CONFIG_NETFILTER
1405 /* we need to exclude all possible ENOPROTOOPTs except default case */
1406 if (err
== -ENOPROTOOPT
&& optname
!= IPV6_2292PKTOPTIONS
) {
1409 if (get_user(len
, optlen
))
1413 err
= compat_nf_getsockopt(sk
, PF_INET6
,
1414 optname
, optval
, &len
);
1417 err
= put_user(len
, optlen
);
1422 EXPORT_SYMBOL(compat_ipv6_getsockopt
);