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);
113 opt = xchg(&inet6_sk(sk)->opt, opt);
115 spin_lock(&sk->sk_dst_lock);
116 opt = xchg(&inet6_sk(sk)->opt, opt);
117 spin_unlock(&sk->sk_dst_lock);
124 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
125 char __user *optval, unsigned int optlen)
127 struct ipv6_pinfo *np = inet6_sk(sk);
128 struct net *net = sock_net(sk);
130 int retv = -ENOPROTOOPT;
135 if (optlen >= sizeof(int)) {
136 if (get_user(val, (int __user *) optval))
144 if (ip6_mroute_opt(optname))
145 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
152 if (optlen < sizeof(int))
154 if (val == PF_INET) {
155 struct ipv6_txoptions *opt;
156 struct sk_buff *pktopt;
158 if (sk->sk_type == SOCK_RAW)
161 if (sk->sk_protocol == IPPROTO_UDP ||
162 sk->sk_protocol == IPPROTO_UDPLITE) {
163 struct udp_sock *up = udp_sk(sk);
164 if (up->pending == AF_INET6) {
168 } else if (sk->sk_protocol != IPPROTO_TCP)
171 if (sk->sk_state != TCP_ESTABLISHED) {
176 if (ipv6_only_sock(sk) ||
177 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
178 retv = -EADDRNOTAVAIL;
182 fl6_free_socklist(sk);
183 ipv6_sock_mc_close(sk);
186 * Sock is moving from IPv6 to IPv4 (sk_prot), so
187 * remove it from the refcnt debug socks count in the
190 sk_refcnt_debug_dec(sk);
192 if (sk->sk_protocol == IPPROTO_TCP) {
193 struct inet_connection_sock *icsk = inet_csk(sk);
195 sock_prot_inuse_add(net, sk->sk_prot, -1);
196 sock_prot_inuse_add(net, &tcp_prot, 1);
198 sk->sk_prot = &tcp_prot;
199 icsk->icsk_af_ops = &ipv4_specific;
200 sk->sk_socket->ops = &inet_stream_ops;
201 sk->sk_family = PF_INET;
202 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
204 struct proto *prot = &udp_prot;
206 if (sk->sk_protocol == IPPROTO_UDPLITE)
207 prot = &udplite_prot;
209 sock_prot_inuse_add(net, sk->sk_prot, -1);
210 sock_prot_inuse_add(net, prot, 1);
213 sk->sk_socket->ops = &inet_dgram_ops;
214 sk->sk_family = PF_INET;
216 opt = xchg(&np->opt, NULL);
218 sock_kfree_s(sk, opt, opt->tot_len);
219 pktopt = xchg(&np->pktoptions, NULL);
222 sk->sk_destruct = inet_sock_destruct;
224 * ... and add it to the refcnt debug socks count
225 * in the new family. -acme
227 sk_refcnt_debug_inc(sk);
228 module_put(THIS_MODULE);
235 if (optlen < sizeof(int) ||
236 inet_sk(sk)->inet_num)
238 np->ipv6only = valbool;
242 case IPV6_RECVPKTINFO:
243 if (optlen < sizeof(int))
245 np->rxopt.bits.rxinfo = valbool;
249 case IPV6_2292PKTINFO:
250 if (optlen < sizeof(int))
252 np->rxopt.bits.rxoinfo = valbool;
256 case IPV6_RECVHOPLIMIT:
257 if (optlen < sizeof(int))
259 np->rxopt.bits.rxhlim = valbool;
263 case IPV6_2292HOPLIMIT:
264 if (optlen < sizeof(int))
266 np->rxopt.bits.rxohlim = valbool;
271 if (optlen < sizeof(int))
273 np->rxopt.bits.srcrt = valbool;
278 if (optlen < sizeof(int))
280 np->rxopt.bits.osrcrt = valbool;
284 case IPV6_RECVHOPOPTS:
285 if (optlen < sizeof(int))
287 np->rxopt.bits.hopopts = valbool;
291 case IPV6_2292HOPOPTS:
292 if (optlen < sizeof(int))
294 np->rxopt.bits.ohopopts = valbool;
298 case IPV6_RECVDSTOPTS:
299 if (optlen < sizeof(int))
301 np->rxopt.bits.dstopts = valbool;
305 case IPV6_2292DSTOPTS:
306 if (optlen < sizeof(int))
308 np->rxopt.bits.odstopts = valbool;
313 if (optlen < sizeof(int))
315 if (val < -1 || val > 0xff)
317 /* RFC 3542, 6.5: default traffic class of 0x0 */
324 case IPV6_RECVTCLASS:
325 if (optlen < sizeof(int))
327 np->rxopt.bits.rxtclass = valbool;
332 if (optlen < sizeof(int))
334 np->rxopt.bits.rxflow = valbool;
338 case IPV6_RECVPATHMTU:
339 if (optlen < sizeof(int))
341 np->rxopt.bits.rxpmtu = valbool;
345 case IPV6_TRANSPARENT:
346 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
347 !ns_capable(net->user_ns, CAP_NET_RAW)) {
351 if (optlen < sizeof(int))
353 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
354 inet_sk(sk)->transparent = valbool;
358 case IPV6_RECVORIGDSTADDR:
359 if (optlen < sizeof(int))
361 np->rxopt.bits.rxorigdstaddr = valbool;
366 case IPV6_RTHDRDSTOPTS:
370 struct ipv6_txoptions *opt;
372 /* remove any sticky options header with a zero option
373 * length, per RFC3542.
377 else if (optval == NULL)
379 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
380 optlen & 0x7 || optlen > 8 * 255)
383 /* hop-by-hop / destination options are privileged option */
385 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
388 opt = ipv6_renew_options(sk, np->opt, optname,
389 (struct ipv6_opt_hdr __user *)optval,
396 /* routing header option needs extra check */
398 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
399 struct ipv6_rt_hdr *rthdr = opt->srcrt;
400 switch (rthdr->type) {
401 #if IS_ENABLED(CONFIG_IPV6_MIP6)
402 case IPV6_SRCRT_TYPE_2:
403 if (rthdr->hdrlen != 2 ||
404 rthdr->segments_left != 1)
415 opt = ipv6_update_options(sk, opt);
418 sock_kfree_s(sk, opt, opt->tot_len);
424 struct in6_pktinfo pkt;
428 else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL)
431 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
435 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
438 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
439 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
444 case IPV6_2292PKTOPTIONS:
446 struct ipv6_txoptions *opt = NULL;
451 memset(&fl6, 0, sizeof(fl6));
452 fl6.flowi6_oif = sk->sk_bound_dev_if;
453 fl6.flowi6_mark = sk->sk_mark;
458 /* 1K is probably excessive
459 * 1K is surely not enough, 2K per standard header is 16K.
462 if (optlen > 64*1024)
465 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
470 memset(opt, 0, sizeof(*opt));
471 opt->tot_len = sizeof(*opt) + optlen;
473 if (copy_from_user(opt+1, optval, optlen))
476 msg.msg_controllen = optlen;
477 msg.msg_control = (void*)(opt+1);
479 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk,
485 opt = ipv6_update_options(sk, opt);
488 sock_kfree_s(sk, opt, opt->tot_len);
491 case IPV6_UNICAST_HOPS:
492 if (optlen < sizeof(int))
494 if (val > 255 || val < -1)
500 case IPV6_MULTICAST_HOPS:
501 if (sk->sk_type == SOCK_STREAM)
503 if (optlen < sizeof(int))
505 if (val > 255 || val < -1)
507 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
511 case IPV6_MULTICAST_LOOP:
512 if (optlen < sizeof(int))
516 np->mc_loop = valbool;
520 case IPV6_UNICAST_IF:
522 struct net_device *dev = NULL;
525 if (optlen != sizeof(int))
528 ifindex = (__force int)ntohl((__force __be32)val);
535 dev = dev_get_by_index(net, ifindex);
536 retv = -EADDRNOTAVAIL;
542 if (sk->sk_bound_dev_if)
545 np->ucast_oif = ifindex;
550 case IPV6_MULTICAST_IF:
551 if (sk->sk_type == SOCK_STREAM)
553 if (optlen < sizeof(int))
557 struct net_device *dev;
559 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
562 dev = dev_get_by_index(net, val);
572 case IPV6_ADD_MEMBERSHIP:
573 case IPV6_DROP_MEMBERSHIP:
575 struct ipv6_mreq mreq;
577 if (optlen < sizeof(struct ipv6_mreq))
581 if (inet_sk(sk)->is_icsk)
585 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
588 if (optname == IPV6_ADD_MEMBERSHIP)
589 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
591 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
594 case IPV6_JOIN_ANYCAST:
595 case IPV6_LEAVE_ANYCAST:
597 struct ipv6_mreq mreq;
599 if (optlen < sizeof(struct ipv6_mreq))
603 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
606 if (optname == IPV6_JOIN_ANYCAST)
607 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
609 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
612 case MCAST_JOIN_GROUP:
613 case MCAST_LEAVE_GROUP:
615 struct group_req greq;
616 struct sockaddr_in6 *psin6;
618 if (optlen < sizeof(struct group_req))
622 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
624 if (greq.gr_group.ss_family != AF_INET6) {
625 retv = -EADDRNOTAVAIL;
628 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
629 if (optname == MCAST_JOIN_GROUP)
630 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
633 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
637 case MCAST_JOIN_SOURCE_GROUP:
638 case MCAST_LEAVE_SOURCE_GROUP:
639 case MCAST_BLOCK_SOURCE:
640 case MCAST_UNBLOCK_SOURCE:
642 struct group_source_req greqs;
645 if (optlen < sizeof(struct group_source_req))
647 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
651 if (greqs.gsr_group.ss_family != AF_INET6 ||
652 greqs.gsr_source.ss_family != AF_INET6) {
653 retv = -EADDRNOTAVAIL;
656 if (optname == MCAST_BLOCK_SOURCE) {
657 omode = MCAST_EXCLUDE;
659 } else if (optname == MCAST_UNBLOCK_SOURCE) {
660 omode = MCAST_EXCLUDE;
662 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
663 struct sockaddr_in6 *psin6;
665 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
666 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
668 /* prior join w/ different source is ok */
669 if (retv && retv != -EADDRINUSE)
671 omode = MCAST_INCLUDE;
673 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
674 omode = MCAST_INCLUDE;
677 retv = ip6_mc_source(add, omode, sk, &greqs);
682 struct group_filter *gsf;
684 if (optlen < GROUP_FILTER_SIZE(0))
686 if (optlen > sysctl_optmem_max) {
690 gsf = kmalloc(optlen,GFP_KERNEL);
696 if (copy_from_user(gsf, optval, optlen)) {
700 /* numsrc >= (4G-140)/128 overflow in 32 bits */
701 if (gsf->gf_numsrc >= 0x1ffffffU ||
702 gsf->gf_numsrc > sysctl_mld_max_msf) {
707 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
712 retv = ip6_mc_msfilter(sk, gsf);
717 case IPV6_ROUTER_ALERT:
718 if (optlen < sizeof(int))
720 retv = ip6_ra_control(sk, val);
722 case IPV6_MTU_DISCOVER:
723 if (optlen < sizeof(int))
725 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_INTERFACE)
731 if (optlen < sizeof(int))
733 if (val && val < IPV6_MIN_MTU)
739 if (optlen < sizeof(int))
741 np->recverr = valbool;
743 skb_queue_purge(&sk->sk_error_queue);
746 case IPV6_FLOWINFO_SEND:
747 if (optlen < sizeof(int))
749 np->sndflow = valbool;
752 case IPV6_FLOWLABEL_MGR:
753 retv = ipv6_flowlabel_opt(sk, optval, optlen);
755 case IPV6_IPSEC_POLICY:
756 case IPV6_XFRM_POLICY:
758 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
760 retv = xfrm_user_policy(sk, optname, optval, optlen);
763 case IPV6_ADDR_PREFERENCES:
765 unsigned int pref = 0;
766 unsigned int prefmask = ~0;
768 if (optlen < sizeof(int))
773 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
774 switch (val & (IPV6_PREFER_SRC_PUBLIC|
776 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
777 case IPV6_PREFER_SRC_PUBLIC:
778 pref |= IPV6_PREFER_SRC_PUBLIC;
780 case IPV6_PREFER_SRC_TMP:
781 pref |= IPV6_PREFER_SRC_TMP;
783 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
786 goto pref_skip_pubtmp;
791 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
792 IPV6_PREFER_SRC_TMP);
795 /* check HOME/COA conflicts */
796 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
797 case IPV6_PREFER_SRC_HOME:
799 case IPV6_PREFER_SRC_COA:
800 pref |= IPV6_PREFER_SRC_COA;
807 prefmask &= ~IPV6_PREFER_SRC_COA;
810 /* check CGA/NONCGA conflicts */
811 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
812 case IPV6_PREFER_SRC_CGA:
813 case IPV6_PREFER_SRC_NONCGA:
820 np->srcprefs = (np->srcprefs & prefmask) | pref;
825 case IPV6_MINHOPCOUNT:
826 if (optlen < sizeof(int))
828 if (val < 0 || val > 255)
830 np->min_hopcount = val;
834 np->dontfrag = 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,
873 EXPORT_SYMBOL(ipv6_setsockopt);
876 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
877 char __user *optval, unsigned int optlen)
881 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
882 if (udp_prot.compat_setsockopt != NULL)
883 return udp_prot.compat_setsockopt(sk, level, optname,
885 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
888 if (level != SOL_IPV6)
891 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
892 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
895 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
896 #ifdef CONFIG_NETFILTER
897 /* we need to exclude all possible ENOPROTOOPTs except default case */
898 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
899 optname != IPV6_XFRM_POLICY) {
901 err = compat_nf_setsockopt(sk, PF_INET6, optname,
909 EXPORT_SYMBOL(compat_ipv6_setsockopt);
912 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
913 int optname, char __user *optval, int len)
915 struct ipv6_opt_hdr *hdr;
924 case IPV6_RTHDRDSTOPTS:
928 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
934 return -EINVAL; /* should not happen */
940 len = min_t(unsigned int, len, ipv6_optlen(hdr));
941 if (copy_to_user(optval, hdr, len))
946 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
947 char __user *optval, int __user *optlen, unsigned int flags)
949 struct ipv6_pinfo *np = inet6_sk(sk);
953 if (ip6_mroute_opt(optname))
954 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
956 if (get_user(len, optlen))
960 if (sk->sk_protocol != IPPROTO_UDP &&
961 sk->sk_protocol != IPPROTO_UDPLITE &&
962 sk->sk_protocol != IPPROTO_TCP)
964 if (sk->sk_state != TCP_ESTABLISHED)
970 struct group_filter gsf;
973 if (len < GROUP_FILTER_SIZE(0))
975 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
977 if (gsf.gf_group.ss_family != AF_INET6)
978 return -EADDRNOTAVAIL;
980 err = ip6_mc_msfget(sk, &gsf,
981 (struct group_filter __user *)optval, optlen);
986 case IPV6_2292PKTOPTIONS:
991 if (sk->sk_type != SOCK_STREAM)
994 msg.msg_control = optval;
995 msg.msg_controllen = len;
996 msg.msg_flags = flags;
999 skb = np->pktoptions;
1001 atomic_inc(&skb->users);
1005 ip6_datagram_recv_ctl(sk, &msg, skb);
1008 if (np->rxopt.bits.rxinfo) {
1009 struct in6_pktinfo src_info;
1010 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1011 np->sticky_pktinfo.ipi6_ifindex;
1012 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1013 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1015 if (np->rxopt.bits.rxhlim) {
1016 int hlim = np->mcast_hops;
1017 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1019 if (np->rxopt.bits.rxtclass) {
1020 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1022 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1024 if (np->rxopt.bits.rxoinfo) {
1025 struct in6_pktinfo src_info;
1026 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1027 np->sticky_pktinfo.ipi6_ifindex;
1028 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1029 np->sticky_pktinfo.ipi6_addr;
1030 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1032 if (np->rxopt.bits.rxohlim) {
1033 int hlim = np->mcast_hops;
1034 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1036 if (np->rxopt.bits.rxflow) {
1037 __be32 flowinfo = np->rcv_flowinfo;
1039 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1042 len -= msg.msg_controllen;
1043 return put_user(len, optlen);
1047 struct dst_entry *dst;
1051 dst = __sk_dst_get(sk);
1064 case IPV6_RECVPKTINFO:
1065 val = np->rxopt.bits.rxinfo;
1068 case IPV6_2292PKTINFO:
1069 val = np->rxopt.bits.rxoinfo;
1072 case IPV6_RECVHOPLIMIT:
1073 val = np->rxopt.bits.rxhlim;
1076 case IPV6_2292HOPLIMIT:
1077 val = np->rxopt.bits.rxohlim;
1080 case IPV6_RECVRTHDR:
1081 val = np->rxopt.bits.srcrt;
1084 case IPV6_2292RTHDR:
1085 val = np->rxopt.bits.osrcrt;
1089 case IPV6_RTHDRDSTOPTS:
1095 len = ipv6_getsockopt_sticky(sk, np->opt,
1096 optname, optval, len);
1098 /* check if ipv6_getsockopt_sticky() returns err code */
1101 return put_user(len, optlen);
1104 case IPV6_RECVHOPOPTS:
1105 val = np->rxopt.bits.hopopts;
1108 case IPV6_2292HOPOPTS:
1109 val = np->rxopt.bits.ohopopts;
1112 case IPV6_RECVDSTOPTS:
1113 val = np->rxopt.bits.dstopts;
1116 case IPV6_2292DSTOPTS:
1117 val = np->rxopt.bits.odstopts;
1124 case IPV6_RECVTCLASS:
1125 val = np->rxopt.bits.rxtclass;
1129 val = np->rxopt.bits.rxflow;
1132 case IPV6_RECVPATHMTU:
1133 val = np->rxopt.bits.rxpmtu;
1138 struct dst_entry *dst;
1139 struct ip6_mtuinfo mtuinfo;
1141 if (len < sizeof(mtuinfo))
1144 len = sizeof(mtuinfo);
1145 memset(&mtuinfo, 0, sizeof(mtuinfo));
1148 dst = __sk_dst_get(sk);
1150 mtuinfo.ip6m_mtu = dst_mtu(dst);
1152 if (!mtuinfo.ip6m_mtu)
1155 if (put_user(len, optlen))
1157 if (copy_to_user(optval, &mtuinfo, len))
1164 case IPV6_TRANSPARENT:
1165 val = inet_sk(sk)->transparent;
1168 case IPV6_RECVORIGDSTADDR:
1169 val = np->rxopt.bits.rxorigdstaddr;
1172 case IPV6_UNICAST_HOPS:
1173 case IPV6_MULTICAST_HOPS:
1175 struct dst_entry *dst;
1177 if (optname == IPV6_UNICAST_HOPS)
1178 val = np->hop_limit;
1180 val = np->mcast_hops;
1184 dst = __sk_dst_get(sk);
1186 val = ip6_dst_hoplimit(dst);
1191 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1195 case IPV6_MULTICAST_LOOP:
1199 case IPV6_MULTICAST_IF:
1200 val = np->mcast_oif;
1203 case IPV6_UNICAST_IF:
1204 val = (__force int)htonl((__u32) np->ucast_oif);
1207 case IPV6_MTU_DISCOVER:
1215 case IPV6_FLOWINFO_SEND:
1219 case IPV6_FLOWLABEL_MGR:
1221 struct in6_flowlabel_req freq;
1224 if (len < sizeof(freq))
1227 if (copy_from_user(&freq, optval, sizeof(freq)))
1230 if (freq.flr_action != IPV6_FL_A_GET)
1234 flags = freq.flr_flags;
1236 memset(&freq, 0, sizeof(freq));
1238 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1242 if (put_user(len, optlen))
1244 if (copy_to_user(optval, &freq, len))
1250 case IPV6_ADDR_PREFERENCES:
1253 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1254 val |= IPV6_PREFER_SRC_TMP;
1255 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1256 val |= IPV6_PREFER_SRC_PUBLIC;
1258 /* XXX: should we return system default? */
1259 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1262 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1263 val |= IPV6_PREFER_SRC_COA;
1265 val |= IPV6_PREFER_SRC_HOME;
1268 case IPV6_MINHOPCOUNT:
1269 val = np->min_hopcount;
1277 return -ENOPROTOOPT;
1279 len = min_t(unsigned int, sizeof(int), len);
1280 if(put_user(len, optlen))
1282 if(copy_to_user(optval,&val,len))
1287 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1288 char __user *optval, int __user *optlen)
1292 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1293 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1295 if(level != SOL_IPV6)
1296 return -ENOPROTOOPT;
1298 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1299 #ifdef CONFIG_NETFILTER
1300 /* we need to exclude all possible ENOPROTOOPTs except default case */
1301 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1304 if (get_user(len, optlen))
1308 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1312 err = put_user(len, optlen);
1318 EXPORT_SYMBOL(ipv6_getsockopt);
1320 #ifdef CONFIG_COMPAT
1321 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1322 char __user *optval, int __user *optlen)
1326 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1327 if (udp_prot.compat_getsockopt != NULL)
1328 return udp_prot.compat_getsockopt(sk, level, optname,
1330 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1333 if (level != SOL_IPV6)
1334 return -ENOPROTOOPT;
1336 if (optname == MCAST_MSFILTER)
1337 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1340 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1342 #ifdef CONFIG_NETFILTER
1343 /* we need to exclude all possible ENOPROTOOPTs except default case */
1344 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1347 if (get_user(len, optlen))
1351 err = compat_nf_getsockopt(sk, PF_INET6,
1352 optname, optval, &len);
1355 err = put_user(len, optlen);
1361 EXPORT_SYMBOL(compat_ipv6_getsockopt);