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 * $Id: ipv6_sockglue.c,v 1.41 2002/02/01 22:01:04 davem Exp $
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * FIXME: Make the setsockopt code POSIX compliant: That is
19 * o Return -EINVAL for setsockopt of short lengths
20 * o Truncate getsockopt returns
21 * o Return an optlen of the truncated length if need be
24 * David L Stevens <dlstevens@us.ibm.com>:
25 * - added multicast source filtering API for MLDv2
28 #include <linux/module.h>
29 #include <linux/capability.h>
30 #include <linux/config.h>
31 #include <linux/errno.h>
32 #include <linux/types.h>
33 #include <linux/socket.h>
34 #include <linux/sockios.h>
35 #include <linux/sched.h>
36 #include <linux/net.h>
37 #include <linux/in6.h>
38 #include <linux/netdevice.h>
39 #include <linux/if_arp.h>
40 #include <linux/init.h>
41 #include <linux/sysctl.h>
42 #include <linux/netfilter.h>
47 #include <net/ndisc.h>
48 #include <net/protocol.h>
49 #include <net/transp_v6.h>
50 #include <net/ip6_route.h>
51 #include <net/addrconf.h>
52 #include <net/inet_common.h>
57 #include <asm/uaccess.h>
59 DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
61 static struct packet_type ipv6_packet_type = {
62 .type = __constant_htons(ETH_P_IPV6),
66 struct ip6_ra_chain *ip6_ra_chain;
67 DEFINE_RWLOCK(ip6_ra_lock);
69 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
71 struct ip6_ra_chain *ra, *new_ra, **rap;
73 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
74 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
77 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
79 write_lock_bh(&ip6_ra_lock);
80 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
83 write_unlock_bh(&ip6_ra_lock);
89 write_unlock_bh(&ip6_ra_lock);
99 write_unlock_bh(&ip6_ra_lock);
104 new_ra->destructor = destructor;
108 write_unlock_bh(&ip6_ra_lock);
112 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
113 char __user *optval, int optlen)
115 struct ipv6_pinfo *np = inet6_sk(sk);
117 int retv = -ENOPROTOOPT;
121 else if (get_user(val, (int __user *) optval))
131 if (val == PF_INET) {
132 struct ipv6_txoptions *opt;
133 struct sk_buff *pktopt;
135 if (sk->sk_protocol != IPPROTO_UDP &&
136 sk->sk_protocol != IPPROTO_TCP)
139 if (sk->sk_state != TCP_ESTABLISHED) {
144 if (ipv6_only_sock(sk) ||
145 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
146 retv = -EADDRNOTAVAIL;
150 fl6_free_socklist(sk);
151 ipv6_sock_mc_close(sk);
154 * Sock is moving from IPv6 to IPv4 (sk_prot), so
155 * remove it from the refcnt debug socks count in the
158 sk_refcnt_debug_dec(sk);
160 if (sk->sk_protocol == IPPROTO_TCP) {
161 struct inet_connection_sock *icsk = inet_csk(sk);
164 sock_prot_dec_use(sk->sk_prot);
165 sock_prot_inc_use(&tcp_prot);
167 sk->sk_prot = &tcp_prot;
168 icsk->icsk_af_ops = &ipv4_specific;
169 sk->sk_socket->ops = &inet_stream_ops;
170 sk->sk_family = PF_INET;
171 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
174 sock_prot_dec_use(sk->sk_prot);
175 sock_prot_inc_use(&udp_prot);
177 sk->sk_prot = &udp_prot;
178 sk->sk_socket->ops = &inet_dgram_ops;
179 sk->sk_family = PF_INET;
181 opt = xchg(&np->opt, NULL);
183 sock_kfree_s(sk, opt, opt->tot_len);
184 pktopt = xchg(&np->pktoptions, NULL);
188 sk->sk_destruct = inet_sock_destruct;
190 * ... and add it to the refcnt debug socks count
191 * in the new family. -acme
193 sk_refcnt_debug_inc(sk);
194 module_put(THIS_MODULE);
201 if (inet_sk(sk)->num)
203 np->ipv6only = valbool;
207 case IPV6_RECVPKTINFO:
208 np->rxopt.bits.rxinfo = valbool;
212 case IPV6_2292PKTINFO:
213 np->rxopt.bits.rxoinfo = valbool;
217 case IPV6_RECVHOPLIMIT:
218 np->rxopt.bits.rxhlim = valbool;
222 case IPV6_2292HOPLIMIT:
223 np->rxopt.bits.rxohlim = valbool;
228 if (val < 0 || val > 2)
230 np->rxopt.bits.srcrt = val;
235 if (val < 0 || val > 2)
237 np->rxopt.bits.osrcrt = val;
241 case IPV6_RECVHOPOPTS:
242 np->rxopt.bits.hopopts = valbool;
246 case IPV6_2292HOPOPTS:
247 np->rxopt.bits.ohopopts = valbool;
251 case IPV6_RECVDSTOPTS:
252 np->rxopt.bits.dstopts = valbool;
256 case IPV6_2292DSTOPTS:
257 np->rxopt.bits.odstopts = valbool;
262 if (val < 0 || val > 0xff)
268 case IPV6_RECVTCLASS:
269 np->rxopt.bits.rxtclass = valbool;
274 np->rxopt.bits.rxflow = valbool;
279 case IPV6_RTHDRDSTOPTS:
283 struct ipv6_txoptions *opt;
287 /* hop-by-hop / destination options are privileged option */
289 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
293 if (optlen & 0x7 || optlen > 8 * 255)
296 opt = ipv6_renew_options(sk, np->opt, optname,
297 (struct ipv6_opt_hdr __user *)optval,
304 /* routing header option needs extra check */
305 if (optname == IPV6_RTHDR && opt->srcrt) {
306 struct ipv6_rt_hdr *rthdr = opt->srcrt;
309 if ((rthdr->hdrlen & 1) ||
310 (rthdr->hdrlen >> 1) != rthdr->segments_left)
315 if (inet_sk(sk)->is_icsk) {
317 struct inet_connection_sock *icsk = inet_csk(sk);
318 if (!((1 << sk->sk_state) &
319 (TCPF_LISTEN | TCPF_CLOSE))
320 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
321 icsk->icsk_ext_hdr_len =
322 opt->opt_flen + opt->opt_nflen;
323 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
326 opt = xchg(&np->opt, opt);
329 write_lock(&sk->sk_dst_lock);
330 opt = xchg(&np->opt, opt);
331 write_unlock(&sk->sk_dst_lock);
336 sock_kfree_s(sk, opt, opt->tot_len);
340 case IPV6_2292PKTOPTIONS:
342 struct ipv6_txoptions *opt = NULL;
347 fl.fl6_flowlabel = 0;
348 fl.oif = sk->sk_bound_dev_if;
353 /* 1K is probably excessive
354 * 1K is surely not enough, 2K per standard header is 16K.
357 if (optlen > 64*1024)
360 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
365 memset(opt, 0, sizeof(*opt));
366 opt->tot_len = sizeof(*opt) + optlen;
368 if (copy_from_user(opt+1, optval, optlen))
371 msg.msg_controllen = optlen;
372 msg.msg_control = (void*)(opt+1);
374 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
379 if (inet_sk(sk)->is_icsk) {
381 struct inet_connection_sock *icsk = inet_csk(sk);
382 if (!((1 << sk->sk_state) &
383 (TCPF_LISTEN | TCPF_CLOSE))
384 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
385 icsk->icsk_ext_hdr_len =
386 opt->opt_flen + opt->opt_nflen;
387 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
390 opt = xchg(&np->opt, opt);
393 write_lock(&sk->sk_dst_lock);
394 opt = xchg(&np->opt, opt);
395 write_unlock(&sk->sk_dst_lock);
401 sock_kfree_s(sk, opt, opt->tot_len);
404 case IPV6_UNICAST_HOPS:
405 if (val > 255 || val < -1)
411 case IPV6_MULTICAST_HOPS:
412 if (sk->sk_type == SOCK_STREAM)
414 if (val > 255 || val < -1)
416 np->mcast_hops = val;
420 case IPV6_MULTICAST_LOOP:
421 np->mc_loop = valbool;
425 case IPV6_MULTICAST_IF:
426 if (sk->sk_type == SOCK_STREAM)
428 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
431 if (__dev_get_by_index(val) == NULL) {
438 case IPV6_ADD_MEMBERSHIP:
439 case IPV6_DROP_MEMBERSHIP:
441 struct ipv6_mreq mreq;
444 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
447 if (optname == IPV6_ADD_MEMBERSHIP)
448 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
450 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
453 case IPV6_JOIN_ANYCAST:
454 case IPV6_LEAVE_ANYCAST:
456 struct ipv6_mreq mreq;
458 if (optlen != sizeof(struct ipv6_mreq))
462 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
465 if (optname == IPV6_JOIN_ANYCAST)
466 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
468 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
471 case MCAST_JOIN_GROUP:
472 case MCAST_LEAVE_GROUP:
474 struct group_req greq;
475 struct sockaddr_in6 *psin6;
478 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
480 if (greq.gr_group.ss_family != AF_INET6) {
481 retv = -EADDRNOTAVAIL;
484 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
485 if (optname == MCAST_JOIN_GROUP)
486 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
489 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
493 case MCAST_JOIN_SOURCE_GROUP:
494 case MCAST_LEAVE_SOURCE_GROUP:
495 case MCAST_BLOCK_SOURCE:
496 case MCAST_UNBLOCK_SOURCE:
498 struct group_source_req greqs;
501 if (optlen != sizeof(struct group_source_req))
503 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
507 if (greqs.gsr_group.ss_family != AF_INET6 ||
508 greqs.gsr_source.ss_family != AF_INET6) {
509 retv = -EADDRNOTAVAIL;
512 if (optname == MCAST_BLOCK_SOURCE) {
513 omode = MCAST_EXCLUDE;
515 } else if (optname == MCAST_UNBLOCK_SOURCE) {
516 omode = MCAST_EXCLUDE;
518 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
519 struct sockaddr_in6 *psin6;
521 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
522 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
524 /* prior join w/ different source is ok */
525 if (retv && retv != -EADDRINUSE)
527 omode = MCAST_INCLUDE;
529 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
530 omode = MCAST_INCLUDE;
533 retv = ip6_mc_source(add, omode, sk, &greqs);
538 extern int sysctl_mld_max_msf;
539 struct group_filter *gsf;
541 if (optlen < GROUP_FILTER_SIZE(0))
543 if (optlen > sysctl_optmem_max) {
547 gsf = kmalloc(optlen,GFP_KERNEL);
553 if (copy_from_user(gsf, optval, optlen)) {
557 /* numsrc >= (4G-140)/128 overflow in 32 bits */
558 if (gsf->gf_numsrc >= 0x1ffffffU ||
559 gsf->gf_numsrc > sysctl_mld_max_msf) {
564 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
569 retv = ip6_mc_msfilter(sk, gsf);
574 case IPV6_ROUTER_ALERT:
575 retv = ip6_ra_control(sk, val, NULL);
577 case IPV6_MTU_DISCOVER:
584 if (val && val < IPV6_MIN_MTU)
590 np->recverr = valbool;
592 skb_queue_purge(&sk->sk_error_queue);
595 case IPV6_FLOWINFO_SEND:
596 np->sndflow = valbool;
599 case IPV6_FLOWLABEL_MGR:
600 retv = ipv6_flowlabel_opt(sk, optval, optlen);
602 case IPV6_IPSEC_POLICY:
603 case IPV6_XFRM_POLICY:
605 if (!capable(CAP_NET_ADMIN))
607 retv = xfrm_user_policy(sk, optname, optval, optlen);
620 int ipv6_setsockopt(struct sock *sk, int level, int optname,
621 char __user *optval, int optlen)
625 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
626 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
628 if (level != SOL_IPV6)
631 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
632 #ifdef CONFIG_NETFILTER
633 /* we need to exclude all possible ENOPROTOOPTs except default case */
634 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
635 optname != IPV6_XFRM_POLICY) {
637 err = nf_setsockopt(sk, PF_INET6, optname, optval,
647 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
648 char __user *optval, int optlen)
652 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
653 if (udp_prot.compat_setsockopt != NULL)
654 return udp_prot.compat_setsockopt(sk, level, optname,
656 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
659 if (level != SOL_IPV6)
662 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
663 #ifdef CONFIG_NETFILTER
664 /* we need to exclude all possible ENOPROTOOPTs except default case */
665 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
666 optname != IPV6_XFRM_POLICY) {
668 err = compat_nf_setsockopt(sk, PF_INET6, optname,
676 EXPORT_SYMBOL(compat_ipv6_setsockopt);
679 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
680 char __user *optval, int len)
684 len = min_t(int, len, ipv6_optlen(hdr));
685 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
690 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
691 char __user *optval, int __user *optlen)
693 struct ipv6_pinfo *np = inet6_sk(sk);
697 if (get_user(len, optlen))
701 if (sk->sk_protocol != IPPROTO_UDP &&
702 sk->sk_protocol != IPPROTO_TCP)
704 if (sk->sk_state != TCP_ESTABLISHED)
710 struct group_filter gsf;
713 if (len < GROUP_FILTER_SIZE(0))
715 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
718 err = ip6_mc_msfget(sk, &gsf,
719 (struct group_filter __user *)optval, optlen);
724 case IPV6_2292PKTOPTIONS:
729 if (sk->sk_type != SOCK_STREAM)
732 msg.msg_control = optval;
733 msg.msg_controllen = len;
737 skb = np->pktoptions;
739 atomic_inc(&skb->users);
743 int err = datagram_recv_ctl(sk, &msg, skb);
748 if (np->rxopt.bits.rxinfo) {
749 struct in6_pktinfo src_info;
750 src_info.ipi6_ifindex = np->mcast_oif;
751 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
752 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
754 if (np->rxopt.bits.rxhlim) {
755 int hlim = np->mcast_hops;
756 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
758 if (np->rxopt.bits.rxoinfo) {
759 struct in6_pktinfo src_info;
760 src_info.ipi6_ifindex = np->mcast_oif;
761 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
762 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
764 if (np->rxopt.bits.rxohlim) {
765 int hlim = np->mcast_hops;
766 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
769 len -= msg.msg_controllen;
770 return put_user(len, optlen);
774 struct dst_entry *dst;
777 dst = sk_dst_get(sk);
792 case IPV6_RECVPKTINFO:
793 val = np->rxopt.bits.rxinfo;
796 case IPV6_2292PKTINFO:
797 val = np->rxopt.bits.rxoinfo;
800 case IPV6_RECVHOPLIMIT:
801 val = np->rxopt.bits.rxhlim;
804 case IPV6_2292HOPLIMIT:
805 val = np->rxopt.bits.rxohlim;
809 val = np->rxopt.bits.srcrt;
813 val = np->rxopt.bits.osrcrt;
817 case IPV6_RTHDRDSTOPTS:
823 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
826 return put_user(len, optlen);
829 case IPV6_RECVHOPOPTS:
830 val = np->rxopt.bits.hopopts;
833 case IPV6_2292HOPOPTS:
834 val = np->rxopt.bits.ohopopts;
837 case IPV6_RECVDSTOPTS:
838 val = np->rxopt.bits.dstopts;
841 case IPV6_2292DSTOPTS:
842 val = np->rxopt.bits.odstopts;
849 case IPV6_RECVTCLASS:
850 val = np->rxopt.bits.rxtclass;
854 val = np->rxopt.bits.rxflow;
857 case IPV6_UNICAST_HOPS:
861 case IPV6_MULTICAST_HOPS:
862 val = np->mcast_hops;
865 case IPV6_MULTICAST_LOOP:
869 case IPV6_MULTICAST_IF:
873 case IPV6_MTU_DISCOVER:
881 case IPV6_FLOWINFO_SEND:
888 len = min_t(unsigned int, sizeof(int), len);
889 if(put_user(len, optlen))
891 if(copy_to_user(optval,&val,len))
896 int ipv6_getsockopt(struct sock *sk, int level, int optname,
897 char __user *optval, int __user *optlen)
901 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
902 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
904 if(level != SOL_IPV6)
907 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
908 #ifdef CONFIG_NETFILTER
909 /* we need to exclude all possible EINVALs except default case */
910 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
911 optname != MCAST_MSFILTER) {
914 if (get_user(len, optlen))
918 err = nf_getsockopt(sk, PF_INET6, optname, optval,
922 err = put_user(len, optlen);
929 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
930 char __user *optval, int __user *optlen)
934 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
935 if (udp_prot.compat_getsockopt != NULL)
936 return udp_prot.compat_getsockopt(sk, level, optname,
938 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
941 if (level != SOL_IPV6)
944 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
945 #ifdef CONFIG_NETFILTER
946 /* we need to exclude all possible EINVALs except default case */
947 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
948 optname != MCAST_MSFILTER) {
951 if (get_user(len, optlen))
955 err = compat_nf_getsockopt(sk, PF_INET6,
956 optname, optval, &len);
959 err = put_user(len, optlen);
965 EXPORT_SYMBOL(compat_ipv6_getsockopt);
968 void __init ipv6_packet_init(void)
970 dev_add_pack(&ipv6_packet_type);
973 void ipv6_packet_cleanup(void)
975 dev_remove_pack(&ipv6_packet_type);