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 bool setsockopt_needs_rtnl(int optname)
123 case IPV6_ADD_MEMBERSHIP:
124 case IPV6_DROP_MEMBERSHIP:
125 case MCAST_JOIN_GROUP:
126 case MCAST_LEAVE_GROUP:
132 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
133 char __user *optval, unsigned int optlen)
135 struct ipv6_pinfo *np = inet6_sk(sk);
136 struct net *net = sock_net(sk);
138 int retv = -ENOPROTOOPT;
139 bool needs_rtnl = setsockopt_needs_rtnl(optname);
144 if (optlen >= sizeof(int)) {
145 if (get_user(val, (int __user *) optval))
151 valbool = (val != 0);
153 if (ip6_mroute_opt(optname))
154 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
163 if (optlen < sizeof(int))
165 if (val == PF_INET) {
166 struct ipv6_txoptions *opt;
167 struct sk_buff *pktopt;
169 if (sk->sk_type == SOCK_RAW)
172 if (sk->sk_protocol == IPPROTO_UDP ||
173 sk->sk_protocol == IPPROTO_UDPLITE) {
174 struct udp_sock *up = udp_sk(sk);
175 if (up->pending == AF_INET6) {
179 } else if (sk->sk_protocol != IPPROTO_TCP)
182 if (sk->sk_state != TCP_ESTABLISHED) {
187 if (ipv6_only_sock(sk) ||
188 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
189 retv = -EADDRNOTAVAIL;
193 fl6_free_socklist(sk);
194 ipv6_sock_mc_close(sk);
197 * Sock is moving from IPv6 to IPv4 (sk_prot), so
198 * remove it from the refcnt debug socks count in the
201 sk_refcnt_debug_dec(sk);
203 if (sk->sk_protocol == IPPROTO_TCP) {
204 struct inet_connection_sock *icsk = inet_csk(sk);
206 sock_prot_inuse_add(net, sk->sk_prot, -1);
207 sock_prot_inuse_add(net, &tcp_prot, 1);
209 sk->sk_prot = &tcp_prot;
210 icsk->icsk_af_ops = &ipv4_specific;
211 sk->sk_socket->ops = &inet_stream_ops;
212 sk->sk_family = PF_INET;
213 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
215 struct proto *prot = &udp_prot;
217 if (sk->sk_protocol == IPPROTO_UDPLITE)
218 prot = &udplite_prot;
220 sock_prot_inuse_add(net, sk->sk_prot, -1);
221 sock_prot_inuse_add(net, prot, 1);
224 sk->sk_socket->ops = &inet_dgram_ops;
225 sk->sk_family = PF_INET;
227 opt = xchg(&np->opt, NULL);
229 sock_kfree_s(sk, opt, opt->tot_len);
230 pktopt = xchg(&np->pktoptions, NULL);
233 sk->sk_destruct = inet_sock_destruct;
235 * ... and add it to the refcnt debug socks count
236 * in the new family. -acme
238 sk_refcnt_debug_inc(sk);
239 module_put(THIS_MODULE);
246 if (optlen < sizeof(int) ||
247 inet_sk(sk)->inet_num)
249 sk->sk_ipv6only = valbool;
253 case IPV6_RECVPKTINFO:
254 if (optlen < sizeof(int))
256 np->rxopt.bits.rxinfo = valbool;
260 case IPV6_2292PKTINFO:
261 if (optlen < sizeof(int))
263 np->rxopt.bits.rxoinfo = valbool;
267 case IPV6_RECVHOPLIMIT:
268 if (optlen < sizeof(int))
270 np->rxopt.bits.rxhlim = valbool;
274 case IPV6_2292HOPLIMIT:
275 if (optlen < sizeof(int))
277 np->rxopt.bits.rxohlim = valbool;
282 if (optlen < sizeof(int))
284 np->rxopt.bits.srcrt = valbool;
289 if (optlen < sizeof(int))
291 np->rxopt.bits.osrcrt = valbool;
295 case IPV6_RECVHOPOPTS:
296 if (optlen < sizeof(int))
298 np->rxopt.bits.hopopts = valbool;
302 case IPV6_2292HOPOPTS:
303 if (optlen < sizeof(int))
305 np->rxopt.bits.ohopopts = valbool;
309 case IPV6_RECVDSTOPTS:
310 if (optlen < sizeof(int))
312 np->rxopt.bits.dstopts = valbool;
316 case IPV6_2292DSTOPTS:
317 if (optlen < sizeof(int))
319 np->rxopt.bits.odstopts = valbool;
324 if (optlen < sizeof(int))
326 if (val < -1 || val > 0xff)
328 /* RFC 3542, 6.5: default traffic class of 0x0 */
335 case IPV6_RECVTCLASS:
336 if (optlen < sizeof(int))
338 np->rxopt.bits.rxtclass = valbool;
343 if (optlen < sizeof(int))
345 np->rxopt.bits.rxflow = valbool;
349 case IPV6_RECVPATHMTU:
350 if (optlen < sizeof(int))
352 np->rxopt.bits.rxpmtu = valbool;
356 case IPV6_TRANSPARENT:
357 if (valbool && !ns_capable(net->user_ns, CAP_NET_ADMIN) &&
358 !ns_capable(net->user_ns, CAP_NET_RAW)) {
362 if (optlen < sizeof(int))
364 /* we don't have a separate transparent bit for IPV6 we use the one in the IPv4 socket */
365 inet_sk(sk)->transparent = valbool;
369 case IPV6_RECVORIGDSTADDR:
370 if (optlen < sizeof(int))
372 np->rxopt.bits.rxorigdstaddr = valbool;
377 case IPV6_RTHDRDSTOPTS:
381 struct ipv6_txoptions *opt;
383 /* remove any sticky options header with a zero option
384 * length, per RFC3542.
388 else if (optval == NULL)
390 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
391 optlen & 0x7 || optlen > 8 * 255)
394 /* hop-by-hop / destination options are privileged option */
396 if (optname != IPV6_RTHDR && !ns_capable(net->user_ns, CAP_NET_RAW))
399 opt = ipv6_renew_options(sk, np->opt, optname,
400 (struct ipv6_opt_hdr __user *)optval,
407 /* routing header option needs extra check */
409 if (optname == IPV6_RTHDR && opt && opt->srcrt) {
410 struct ipv6_rt_hdr *rthdr = opt->srcrt;
411 switch (rthdr->type) {
412 #if IS_ENABLED(CONFIG_IPV6_MIP6)
413 case IPV6_SRCRT_TYPE_2:
414 if (rthdr->hdrlen != 2 ||
415 rthdr->segments_left != 1)
426 opt = ipv6_update_options(sk, opt);
429 sock_kfree_s(sk, opt, opt->tot_len);
435 struct in6_pktinfo pkt;
439 else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL)
442 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
446 if (sk->sk_bound_dev_if && pkt.ipi6_ifindex != sk->sk_bound_dev_if)
449 np->sticky_pktinfo.ipi6_ifindex = pkt.ipi6_ifindex;
450 np->sticky_pktinfo.ipi6_addr = pkt.ipi6_addr;
455 case IPV6_2292PKTOPTIONS:
457 struct ipv6_txoptions *opt = NULL;
462 memset(&fl6, 0, sizeof(fl6));
463 fl6.flowi6_oif = sk->sk_bound_dev_if;
464 fl6.flowi6_mark = sk->sk_mark;
469 /* 1K is probably excessive
470 * 1K is surely not enough, 2K per standard header is 16K.
473 if (optlen > 64*1024)
476 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
481 memset(opt, 0, sizeof(*opt));
482 opt->tot_len = sizeof(*opt) + optlen;
484 if (copy_from_user(opt+1, optval, optlen))
487 msg.msg_controllen = optlen;
488 msg.msg_control = (void *)(opt+1);
490 retv = ip6_datagram_send_ctl(net, sk, &msg, &fl6, opt, &junk,
496 opt = ipv6_update_options(sk, opt);
499 sock_kfree_s(sk, opt, opt->tot_len);
502 case IPV6_UNICAST_HOPS:
503 if (optlen < sizeof(int))
505 if (val > 255 || val < -1)
511 case IPV6_MULTICAST_HOPS:
512 if (sk->sk_type == SOCK_STREAM)
514 if (optlen < sizeof(int))
516 if (val > 255 || val < -1)
518 np->mcast_hops = (val == -1 ? IPV6_DEFAULT_MCASTHOPS : val);
522 case IPV6_MULTICAST_LOOP:
523 if (optlen < sizeof(int))
527 np->mc_loop = valbool;
531 case IPV6_UNICAST_IF:
533 struct net_device *dev = NULL;
536 if (optlen != sizeof(int))
539 ifindex = (__force int)ntohl((__force __be32)val);
546 dev = dev_get_by_index(net, ifindex);
547 retv = -EADDRNOTAVAIL;
553 if (sk->sk_bound_dev_if)
556 np->ucast_oif = ifindex;
561 case IPV6_MULTICAST_IF:
562 if (sk->sk_type == SOCK_STREAM)
564 if (optlen < sizeof(int))
568 struct net_device *dev;
570 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
573 dev = dev_get_by_index(net, val);
583 case IPV6_ADD_MEMBERSHIP:
584 case IPV6_DROP_MEMBERSHIP:
586 struct ipv6_mreq mreq;
588 if (optlen < sizeof(struct ipv6_mreq))
592 if (inet_sk(sk)->is_icsk)
596 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
599 if (optname == IPV6_ADD_MEMBERSHIP)
600 retv = __ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
602 retv = __ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
605 case IPV6_JOIN_ANYCAST:
606 case IPV6_LEAVE_ANYCAST:
608 struct ipv6_mreq mreq;
610 if (optlen < sizeof(struct ipv6_mreq))
614 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
617 if (optname == IPV6_JOIN_ANYCAST)
618 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
620 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
623 case MCAST_JOIN_GROUP:
624 case MCAST_LEAVE_GROUP:
626 struct group_req greq;
627 struct sockaddr_in6 *psin6;
629 if (optlen < sizeof(struct group_req))
633 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
635 if (greq.gr_group.ss_family != AF_INET6) {
636 retv = -EADDRNOTAVAIL;
639 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
640 if (optname == MCAST_JOIN_GROUP)
641 retv = __ipv6_sock_mc_join(sk, greq.gr_interface,
644 retv = __ipv6_sock_mc_drop(sk, greq.gr_interface,
648 case MCAST_JOIN_SOURCE_GROUP:
649 case MCAST_LEAVE_SOURCE_GROUP:
650 case MCAST_BLOCK_SOURCE:
651 case MCAST_UNBLOCK_SOURCE:
653 struct group_source_req greqs;
656 if (optlen < sizeof(struct group_source_req))
658 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
662 if (greqs.gsr_group.ss_family != AF_INET6 ||
663 greqs.gsr_source.ss_family != AF_INET6) {
664 retv = -EADDRNOTAVAIL;
667 if (optname == MCAST_BLOCK_SOURCE) {
668 omode = MCAST_EXCLUDE;
670 } else if (optname == MCAST_UNBLOCK_SOURCE) {
671 omode = MCAST_EXCLUDE;
673 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
674 struct sockaddr_in6 *psin6;
676 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
677 retv = __ipv6_sock_mc_join(sk, greqs.gsr_interface,
679 /* prior join w/ different source is ok */
680 if (retv && retv != -EADDRINUSE)
682 omode = MCAST_INCLUDE;
684 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
685 omode = MCAST_INCLUDE;
688 retv = ip6_mc_source(add, omode, sk, &greqs);
693 struct group_filter *gsf;
695 if (optlen < GROUP_FILTER_SIZE(0))
697 if (optlen > sysctl_optmem_max) {
701 gsf = kmalloc(optlen, GFP_KERNEL);
707 if (copy_from_user(gsf, optval, optlen)) {
711 /* numsrc >= (4G-140)/128 overflow in 32 bits */
712 if (gsf->gf_numsrc >= 0x1ffffffU ||
713 gsf->gf_numsrc > sysctl_mld_max_msf) {
718 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
723 retv = ip6_mc_msfilter(sk, gsf);
728 case IPV6_ROUTER_ALERT:
729 if (optlen < sizeof(int))
731 retv = ip6_ra_control(sk, val);
733 case IPV6_MTU_DISCOVER:
734 if (optlen < sizeof(int))
736 if (val < IPV6_PMTUDISC_DONT || val > IPV6_PMTUDISC_OMIT)
742 if (optlen < sizeof(int))
744 if (val && val < IPV6_MIN_MTU)
750 if (optlen < sizeof(int))
752 np->recverr = valbool;
754 skb_queue_purge(&sk->sk_error_queue);
757 case IPV6_FLOWINFO_SEND:
758 if (optlen < sizeof(int))
760 np->sndflow = valbool;
763 case IPV6_FLOWLABEL_MGR:
764 retv = ipv6_flowlabel_opt(sk, optval, optlen);
766 case IPV6_IPSEC_POLICY:
767 case IPV6_XFRM_POLICY:
769 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
771 retv = xfrm_user_policy(sk, optname, optval, optlen);
774 case IPV6_ADDR_PREFERENCES:
776 unsigned int pref = 0;
777 unsigned int prefmask = ~0;
779 if (optlen < sizeof(int))
784 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
785 switch (val & (IPV6_PREFER_SRC_PUBLIC|
787 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
788 case IPV6_PREFER_SRC_PUBLIC:
789 pref |= IPV6_PREFER_SRC_PUBLIC;
791 case IPV6_PREFER_SRC_TMP:
792 pref |= IPV6_PREFER_SRC_TMP;
794 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
797 goto pref_skip_pubtmp;
802 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC|
803 IPV6_PREFER_SRC_TMP);
806 /* check HOME/COA conflicts */
807 switch (val & (IPV6_PREFER_SRC_HOME|IPV6_PREFER_SRC_COA)) {
808 case IPV6_PREFER_SRC_HOME:
810 case IPV6_PREFER_SRC_COA:
811 pref |= IPV6_PREFER_SRC_COA;
818 prefmask &= ~IPV6_PREFER_SRC_COA;
821 /* check CGA/NONCGA conflicts */
822 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
823 case IPV6_PREFER_SRC_CGA:
824 case IPV6_PREFER_SRC_NONCGA:
831 np->srcprefs = (np->srcprefs & prefmask) | pref;
836 case IPV6_MINHOPCOUNT:
837 if (optlen < sizeof(int))
839 if (val < 0 || val > 255)
841 np->min_hopcount = val;
845 np->dontfrag = valbool;
848 case IPV6_AUTOFLOWLABEL:
849 np->autoflowlabel = valbool;
867 int ipv6_setsockopt(struct sock *sk, int level, int optname,
868 char __user *optval, unsigned int optlen)
872 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
873 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
875 if (level != SOL_IPV6)
878 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
879 #ifdef CONFIG_NETFILTER
880 /* we need to exclude all possible ENOPROTOOPTs except default case */
881 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
882 optname != IPV6_XFRM_POLICY) {
884 err = nf_setsockopt(sk, PF_INET6, optname, optval,
891 EXPORT_SYMBOL(ipv6_setsockopt);
894 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
895 char __user *optval, unsigned int optlen)
899 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
900 if (udp_prot.compat_setsockopt != NULL)
901 return udp_prot.compat_setsockopt(sk, level, optname,
903 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
906 if (level != SOL_IPV6)
909 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
910 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
913 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
914 #ifdef CONFIG_NETFILTER
915 /* we need to exclude all possible ENOPROTOOPTs except default case */
916 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
917 optname != IPV6_XFRM_POLICY) {
919 err = compat_nf_setsockopt(sk, PF_INET6, optname,
926 EXPORT_SYMBOL(compat_ipv6_setsockopt);
929 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_txoptions *opt,
930 int optname, char __user *optval, int len)
932 struct ipv6_opt_hdr *hdr;
941 case IPV6_RTHDRDSTOPTS:
945 hdr = (struct ipv6_opt_hdr *)opt->srcrt;
951 return -EINVAL; /* should not happen */
957 len = min_t(unsigned int, len, ipv6_optlen(hdr));
958 if (copy_to_user(optval, hdr, len))
963 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
964 char __user *optval, int __user *optlen, unsigned int flags)
966 struct ipv6_pinfo *np = inet6_sk(sk);
970 if (ip6_mroute_opt(optname))
971 return ip6_mroute_getsockopt(sk, optname, optval, optlen);
973 if (get_user(len, optlen))
977 if (sk->sk_protocol != IPPROTO_UDP &&
978 sk->sk_protocol != IPPROTO_UDPLITE &&
979 sk->sk_protocol != IPPROTO_TCP)
981 if (sk->sk_state != TCP_ESTABLISHED)
987 struct group_filter gsf;
990 if (len < GROUP_FILTER_SIZE(0))
992 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
994 if (gsf.gf_group.ss_family != AF_INET6)
995 return -EADDRNOTAVAIL;
997 err = ip6_mc_msfget(sk, &gsf,
998 (struct group_filter __user *)optval, optlen);
1003 case IPV6_2292PKTOPTIONS:
1006 struct sk_buff *skb;
1008 if (sk->sk_type != SOCK_STREAM)
1009 return -ENOPROTOOPT;
1011 msg.msg_control = optval;
1012 msg.msg_controllen = len;
1013 msg.msg_flags = flags;
1016 skb = np->pktoptions;
1018 ip6_datagram_recv_ctl(sk, &msg, skb);
1021 if (np->rxopt.bits.rxinfo) {
1022 struct in6_pktinfo src_info;
1023 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1024 np->sticky_pktinfo.ipi6_ifindex;
1025 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1026 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1028 if (np->rxopt.bits.rxhlim) {
1029 int hlim = np->mcast_hops;
1030 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
1032 if (np->rxopt.bits.rxtclass) {
1033 int tclass = (int)ip6_tclass(np->rcv_flowinfo);
1035 put_cmsg(&msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
1037 if (np->rxopt.bits.rxoinfo) {
1038 struct in6_pktinfo src_info;
1039 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1040 np->sticky_pktinfo.ipi6_ifindex;
1041 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1042 np->sticky_pktinfo.ipi6_addr;
1043 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1045 if (np->rxopt.bits.rxohlim) {
1046 int hlim = np->mcast_hops;
1047 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
1049 if (np->rxopt.bits.rxflow) {
1050 __be32 flowinfo = np->rcv_flowinfo;
1052 put_cmsg(&msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
1055 len -= msg.msg_controllen;
1056 return put_user(len, optlen);
1060 struct dst_entry *dst;
1064 dst = __sk_dst_get(sk);
1074 val = sk->sk_ipv6only;
1077 case IPV6_RECVPKTINFO:
1078 val = np->rxopt.bits.rxinfo;
1081 case IPV6_2292PKTINFO:
1082 val = np->rxopt.bits.rxoinfo;
1085 case IPV6_RECVHOPLIMIT:
1086 val = np->rxopt.bits.rxhlim;
1089 case IPV6_2292HOPLIMIT:
1090 val = np->rxopt.bits.rxohlim;
1093 case IPV6_RECVRTHDR:
1094 val = np->rxopt.bits.srcrt;
1097 case IPV6_2292RTHDR:
1098 val = np->rxopt.bits.osrcrt;
1102 case IPV6_RTHDRDSTOPTS:
1108 len = ipv6_getsockopt_sticky(sk, np->opt,
1109 optname, optval, len);
1111 /* check if ipv6_getsockopt_sticky() returns err code */
1114 return put_user(len, optlen);
1117 case IPV6_RECVHOPOPTS:
1118 val = np->rxopt.bits.hopopts;
1121 case IPV6_2292HOPOPTS:
1122 val = np->rxopt.bits.ohopopts;
1125 case IPV6_RECVDSTOPTS:
1126 val = np->rxopt.bits.dstopts;
1129 case IPV6_2292DSTOPTS:
1130 val = np->rxopt.bits.odstopts;
1137 case IPV6_RECVTCLASS:
1138 val = np->rxopt.bits.rxtclass;
1142 val = np->rxopt.bits.rxflow;
1145 case IPV6_RECVPATHMTU:
1146 val = np->rxopt.bits.rxpmtu;
1151 struct dst_entry *dst;
1152 struct ip6_mtuinfo mtuinfo;
1154 if (len < sizeof(mtuinfo))
1157 len = sizeof(mtuinfo);
1158 memset(&mtuinfo, 0, sizeof(mtuinfo));
1161 dst = __sk_dst_get(sk);
1163 mtuinfo.ip6m_mtu = dst_mtu(dst);
1165 if (!mtuinfo.ip6m_mtu)
1168 if (put_user(len, optlen))
1170 if (copy_to_user(optval, &mtuinfo, len))
1176 case IPV6_TRANSPARENT:
1177 val = inet_sk(sk)->transparent;
1180 case IPV6_RECVORIGDSTADDR:
1181 val = np->rxopt.bits.rxorigdstaddr;
1184 case IPV6_UNICAST_HOPS:
1185 case IPV6_MULTICAST_HOPS:
1187 struct dst_entry *dst;
1189 if (optname == IPV6_UNICAST_HOPS)
1190 val = np->hop_limit;
1192 val = np->mcast_hops;
1196 dst = __sk_dst_get(sk);
1198 val = ip6_dst_hoplimit(dst);
1203 val = sock_net(sk)->ipv6.devconf_all->hop_limit;
1207 case IPV6_MULTICAST_LOOP:
1211 case IPV6_MULTICAST_IF:
1212 val = np->mcast_oif;
1215 case IPV6_UNICAST_IF:
1216 val = (__force int)htonl((__u32) np->ucast_oif);
1219 case IPV6_MTU_DISCOVER:
1227 case IPV6_FLOWINFO_SEND:
1231 case IPV6_FLOWLABEL_MGR:
1233 struct in6_flowlabel_req freq;
1236 if (len < sizeof(freq))
1239 if (copy_from_user(&freq, optval, sizeof(freq)))
1242 if (freq.flr_action != IPV6_FL_A_GET)
1246 flags = freq.flr_flags;
1248 memset(&freq, 0, sizeof(freq));
1250 val = ipv6_flowlabel_opt_get(sk, &freq, flags);
1254 if (put_user(len, optlen))
1256 if (copy_to_user(optval, &freq, len))
1262 case IPV6_ADDR_PREFERENCES:
1265 if (np->srcprefs & IPV6_PREFER_SRC_TMP)
1266 val |= IPV6_PREFER_SRC_TMP;
1267 else if (np->srcprefs & IPV6_PREFER_SRC_PUBLIC)
1268 val |= IPV6_PREFER_SRC_PUBLIC;
1270 /* XXX: should we return system default? */
1271 val |= IPV6_PREFER_SRC_PUBTMP_DEFAULT;
1274 if (np->srcprefs & IPV6_PREFER_SRC_COA)
1275 val |= IPV6_PREFER_SRC_COA;
1277 val |= IPV6_PREFER_SRC_HOME;
1280 case IPV6_MINHOPCOUNT:
1281 val = np->min_hopcount;
1288 case IPV6_AUTOFLOWLABEL:
1289 val = np->autoflowlabel;
1293 return -ENOPROTOOPT;
1295 len = min_t(unsigned int, sizeof(int), len);
1296 if (put_user(len, optlen))
1298 if (copy_to_user(optval, &val, len))
1303 int ipv6_getsockopt(struct sock *sk, int level, int optname,
1304 char __user *optval, int __user *optlen)
1308 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
1309 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1311 if (level != SOL_IPV6)
1312 return -ENOPROTOOPT;
1314 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen, 0);
1315 #ifdef CONFIG_NETFILTER
1316 /* we need to exclude all possible ENOPROTOOPTs except default case */
1317 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1320 if (get_user(len, optlen))
1324 err = nf_getsockopt(sk, PF_INET6, optname, optval,
1328 err = put_user(len, optlen);
1333 EXPORT_SYMBOL(ipv6_getsockopt);
1335 #ifdef CONFIG_COMPAT
1336 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
1337 char __user *optval, int __user *optlen)
1341 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
1342 if (udp_prot.compat_getsockopt != NULL)
1343 return udp_prot.compat_getsockopt(sk, level, optname,
1345 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1348 if (level != SOL_IPV6)
1349 return -ENOPROTOOPT;
1351 if (optname == MCAST_MSFILTER)
1352 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1355 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen,
1357 #ifdef CONFIG_NETFILTER
1358 /* we need to exclude all possible ENOPROTOOPTs except default case */
1359 if (err == -ENOPROTOOPT && optname != IPV6_2292PKTOPTIONS) {
1362 if (get_user(len, optlen))
1366 err = compat_nf_getsockopt(sk, PF_INET6,
1367 optname, optval, &len);
1370 err = put_user(len, optlen);
1375 EXPORT_SYMBOL(compat_ipv6_getsockopt);