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/errno.h>
31 #include <linux/types.h>
32 #include <linux/socket.h>
33 #include <linux/sockios.h>
34 #include <linux/sched.h>
35 #include <linux/net.h>
36 #include <linux/in6.h>
37 #include <linux/netdevice.h>
38 #include <linux/if_arp.h>
39 #include <linux/init.h>
40 #include <linux/sysctl.h>
41 #include <linux/netfilter.h>
46 #include <net/ndisc.h>
47 #include <net/protocol.h>
48 #include <net/transp_v6.h>
49 #include <net/ip6_route.h>
50 #include <net/addrconf.h>
51 #include <net/inet_common.h>
56 #include <asm/uaccess.h>
58 DEFINE_SNMP_STAT(struct ipstats_mib, ipv6_statistics) __read_mostly;
60 static struct packet_type ipv6_packet_type = {
61 .type = __constant_htons(ETH_P_IPV6),
65 struct ip6_ra_chain *ip6_ra_chain;
66 DEFINE_RWLOCK(ip6_ra_lock);
68 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
70 struct ip6_ra_chain *ra, *new_ra, **rap;
72 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
73 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
76 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
78 write_lock_bh(&ip6_ra_lock);
79 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
82 write_unlock_bh(&ip6_ra_lock);
88 write_unlock_bh(&ip6_ra_lock);
98 write_unlock_bh(&ip6_ra_lock);
103 new_ra->destructor = destructor;
107 write_unlock_bh(&ip6_ra_lock);
111 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
112 char __user *optval, int optlen)
114 struct ipv6_pinfo *np = inet6_sk(sk);
116 int retv = -ENOPROTOOPT;
120 else if (get_user(val, (int __user *) optval))
130 if (val == PF_INET) {
131 struct ipv6_txoptions *opt;
132 struct sk_buff *pktopt;
134 if (sk->sk_protocol != IPPROTO_UDP &&
135 sk->sk_protocol != IPPROTO_TCP)
138 if (sk->sk_state != TCP_ESTABLISHED) {
143 if (ipv6_only_sock(sk) ||
144 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
145 retv = -EADDRNOTAVAIL;
149 fl6_free_socklist(sk);
150 ipv6_sock_mc_close(sk);
153 * Sock is moving from IPv6 to IPv4 (sk_prot), so
154 * remove it from the refcnt debug socks count in the
157 sk_refcnt_debug_dec(sk);
159 if (sk->sk_protocol == IPPROTO_TCP) {
160 struct inet_connection_sock *icsk = inet_csk(sk);
163 sock_prot_dec_use(sk->sk_prot);
164 sock_prot_inc_use(&tcp_prot);
166 sk->sk_prot = &tcp_prot;
167 icsk->icsk_af_ops = &ipv4_specific;
168 sk->sk_socket->ops = &inet_stream_ops;
169 sk->sk_family = PF_INET;
170 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
173 sock_prot_dec_use(sk->sk_prot);
174 sock_prot_inc_use(&udp_prot);
176 sk->sk_prot = &udp_prot;
177 sk->sk_socket->ops = &inet_dgram_ops;
178 sk->sk_family = PF_INET;
180 opt = xchg(&np->opt, NULL);
182 sock_kfree_s(sk, opt, opt->tot_len);
183 pktopt = xchg(&np->pktoptions, NULL);
187 sk->sk_destruct = inet_sock_destruct;
189 * ... and add it to the refcnt debug socks count
190 * in the new family. -acme
192 sk_refcnt_debug_inc(sk);
193 module_put(THIS_MODULE);
200 if (inet_sk(sk)->num)
202 np->ipv6only = valbool;
206 case IPV6_RECVPKTINFO:
207 np->rxopt.bits.rxinfo = valbool;
211 case IPV6_2292PKTINFO:
212 np->rxopt.bits.rxoinfo = valbool;
216 case IPV6_RECVHOPLIMIT:
217 np->rxopt.bits.rxhlim = valbool;
221 case IPV6_2292HOPLIMIT:
222 np->rxopt.bits.rxohlim = valbool;
227 if (val < 0 || val > 2)
229 np->rxopt.bits.srcrt = val;
234 if (val < 0 || val > 2)
236 np->rxopt.bits.osrcrt = val;
240 case IPV6_RECVHOPOPTS:
241 np->rxopt.bits.hopopts = valbool;
245 case IPV6_2292HOPOPTS:
246 np->rxopt.bits.ohopopts = valbool;
250 case IPV6_RECVDSTOPTS:
251 np->rxopt.bits.dstopts = valbool;
255 case IPV6_2292DSTOPTS:
256 np->rxopt.bits.odstopts = valbool;
261 if (val < 0 || val > 0xff)
267 case IPV6_RECVTCLASS:
268 np->rxopt.bits.rxtclass = valbool;
273 np->rxopt.bits.rxflow = valbool;
278 case IPV6_RTHDRDSTOPTS:
282 struct ipv6_txoptions *opt;
286 /* hop-by-hop / destination options are privileged option */
288 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
292 if (optlen & 0x7 || optlen > 8 * 255)
295 opt = ipv6_renew_options(sk, np->opt, optname,
296 (struct ipv6_opt_hdr __user *)optval,
303 /* routing header option needs extra check */
304 if (optname == IPV6_RTHDR && opt->srcrt) {
305 struct ipv6_rt_hdr *rthdr = opt->srcrt;
308 if ((rthdr->hdrlen & 1) ||
309 (rthdr->hdrlen >> 1) != rthdr->segments_left)
314 if (inet_sk(sk)->is_icsk) {
316 struct inet_connection_sock *icsk = inet_csk(sk);
317 if (!((1 << sk->sk_state) &
318 (TCPF_LISTEN | TCPF_CLOSE))
319 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
320 icsk->icsk_ext_hdr_len =
321 opt->opt_flen + opt->opt_nflen;
322 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
325 opt = xchg(&np->opt, opt);
328 write_lock(&sk->sk_dst_lock);
329 opt = xchg(&np->opt, opt);
330 write_unlock(&sk->sk_dst_lock);
335 sock_kfree_s(sk, opt, opt->tot_len);
339 case IPV6_2292PKTOPTIONS:
341 struct ipv6_txoptions *opt = NULL;
346 fl.fl6_flowlabel = 0;
347 fl.oif = sk->sk_bound_dev_if;
352 /* 1K is probably excessive
353 * 1K is surely not enough, 2K per standard header is 16K.
356 if (optlen > 64*1024)
359 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
364 memset(opt, 0, sizeof(*opt));
365 opt->tot_len = sizeof(*opt) + optlen;
367 if (copy_from_user(opt+1, optval, optlen))
370 msg.msg_controllen = optlen;
371 msg.msg_control = (void*)(opt+1);
373 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
378 if (inet_sk(sk)->is_icsk) {
380 struct inet_connection_sock *icsk = inet_csk(sk);
381 if (!((1 << sk->sk_state) &
382 (TCPF_LISTEN | TCPF_CLOSE))
383 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
384 icsk->icsk_ext_hdr_len =
385 opt->opt_flen + opt->opt_nflen;
386 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
389 opt = xchg(&np->opt, opt);
392 write_lock(&sk->sk_dst_lock);
393 opt = xchg(&np->opt, opt);
394 write_unlock(&sk->sk_dst_lock);
400 sock_kfree_s(sk, opt, opt->tot_len);
403 case IPV6_UNICAST_HOPS:
404 if (val > 255 || val < -1)
410 case IPV6_MULTICAST_HOPS:
411 if (sk->sk_type == SOCK_STREAM)
413 if (val > 255 || val < -1)
415 np->mcast_hops = val;
419 case IPV6_MULTICAST_LOOP:
420 np->mc_loop = valbool;
424 case IPV6_MULTICAST_IF:
425 if (sk->sk_type == SOCK_STREAM)
427 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
430 if (__dev_get_by_index(val) == NULL) {
437 case IPV6_ADD_MEMBERSHIP:
438 case IPV6_DROP_MEMBERSHIP:
440 struct ipv6_mreq mreq;
443 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
446 if (optname == IPV6_ADD_MEMBERSHIP)
447 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
449 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
452 case IPV6_JOIN_ANYCAST:
453 case IPV6_LEAVE_ANYCAST:
455 struct ipv6_mreq mreq;
457 if (optlen != sizeof(struct ipv6_mreq))
461 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
464 if (optname == IPV6_JOIN_ANYCAST)
465 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
467 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
470 case MCAST_JOIN_GROUP:
471 case MCAST_LEAVE_GROUP:
473 struct group_req greq;
474 struct sockaddr_in6 *psin6;
477 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
479 if (greq.gr_group.ss_family != AF_INET6) {
480 retv = -EADDRNOTAVAIL;
483 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
484 if (optname == MCAST_JOIN_GROUP)
485 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
488 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
492 case MCAST_JOIN_SOURCE_GROUP:
493 case MCAST_LEAVE_SOURCE_GROUP:
494 case MCAST_BLOCK_SOURCE:
495 case MCAST_UNBLOCK_SOURCE:
497 struct group_source_req greqs;
500 if (optlen != sizeof(struct group_source_req))
502 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
506 if (greqs.gsr_group.ss_family != AF_INET6 ||
507 greqs.gsr_source.ss_family != AF_INET6) {
508 retv = -EADDRNOTAVAIL;
511 if (optname == MCAST_BLOCK_SOURCE) {
512 omode = MCAST_EXCLUDE;
514 } else if (optname == MCAST_UNBLOCK_SOURCE) {
515 omode = MCAST_EXCLUDE;
517 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
518 struct sockaddr_in6 *psin6;
520 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
521 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
523 /* prior join w/ different source is ok */
524 if (retv && retv != -EADDRINUSE)
526 omode = MCAST_INCLUDE;
528 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
529 omode = MCAST_INCLUDE;
532 retv = ip6_mc_source(add, omode, sk, &greqs);
537 extern int sysctl_mld_max_msf;
538 struct group_filter *gsf;
540 if (optlen < GROUP_FILTER_SIZE(0))
542 if (optlen > sysctl_optmem_max) {
546 gsf = kmalloc(optlen,GFP_KERNEL);
552 if (copy_from_user(gsf, optval, optlen)) {
556 /* numsrc >= (4G-140)/128 overflow in 32 bits */
557 if (gsf->gf_numsrc >= 0x1ffffffU ||
558 gsf->gf_numsrc > sysctl_mld_max_msf) {
563 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
568 retv = ip6_mc_msfilter(sk, gsf);
573 case IPV6_ROUTER_ALERT:
574 retv = ip6_ra_control(sk, val, NULL);
576 case IPV6_MTU_DISCOVER:
583 if (val && val < IPV6_MIN_MTU)
589 np->recverr = valbool;
591 skb_queue_purge(&sk->sk_error_queue);
594 case IPV6_FLOWINFO_SEND:
595 np->sndflow = valbool;
598 case IPV6_FLOWLABEL_MGR:
599 retv = ipv6_flowlabel_opt(sk, optval, optlen);
601 case IPV6_IPSEC_POLICY:
602 case IPV6_XFRM_POLICY:
604 if (!capable(CAP_NET_ADMIN))
606 retv = xfrm_user_policy(sk, optname, optval, optlen);
619 int ipv6_setsockopt(struct sock *sk, int level, int optname,
620 char __user *optval, int optlen)
624 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
625 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
627 if (level != SOL_IPV6)
630 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
631 #ifdef CONFIG_NETFILTER
632 /* we need to exclude all possible ENOPROTOOPTs except default case */
633 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
634 optname != IPV6_XFRM_POLICY) {
636 err = nf_setsockopt(sk, PF_INET6, optname, optval,
646 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
647 char __user *optval, int optlen)
651 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
652 if (udp_prot.compat_setsockopt != NULL)
653 return udp_prot.compat_setsockopt(sk, level, optname,
655 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
658 if (level != SOL_IPV6)
661 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
662 #ifdef CONFIG_NETFILTER
663 /* we need to exclude all possible ENOPROTOOPTs except default case */
664 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
665 optname != IPV6_XFRM_POLICY) {
667 err = compat_nf_setsockopt(sk, PF_INET6, optname,
675 EXPORT_SYMBOL(compat_ipv6_setsockopt);
678 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
679 char __user *optval, int len)
683 len = min_t(int, len, ipv6_optlen(hdr));
684 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
689 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
690 char __user *optval, int __user *optlen)
692 struct ipv6_pinfo *np = inet6_sk(sk);
696 if (get_user(len, optlen))
700 if (sk->sk_protocol != IPPROTO_UDP &&
701 sk->sk_protocol != IPPROTO_TCP)
703 if (sk->sk_state != TCP_ESTABLISHED)
709 struct group_filter gsf;
712 if (len < GROUP_FILTER_SIZE(0))
714 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
717 err = ip6_mc_msfget(sk, &gsf,
718 (struct group_filter __user *)optval, optlen);
723 case IPV6_2292PKTOPTIONS:
728 if (sk->sk_type != SOCK_STREAM)
731 msg.msg_control = optval;
732 msg.msg_controllen = len;
736 skb = np->pktoptions;
738 atomic_inc(&skb->users);
742 int err = datagram_recv_ctl(sk, &msg, skb);
747 if (np->rxopt.bits.rxinfo) {
748 struct in6_pktinfo src_info;
749 src_info.ipi6_ifindex = np->mcast_oif;
750 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
751 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
753 if (np->rxopt.bits.rxhlim) {
754 int hlim = np->mcast_hops;
755 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
757 if (np->rxopt.bits.rxoinfo) {
758 struct in6_pktinfo src_info;
759 src_info.ipi6_ifindex = np->mcast_oif;
760 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
761 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
763 if (np->rxopt.bits.rxohlim) {
764 int hlim = np->mcast_hops;
765 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
768 len -= msg.msg_controllen;
769 return put_user(len, optlen);
773 struct dst_entry *dst;
776 dst = sk_dst_get(sk);
791 case IPV6_RECVPKTINFO:
792 val = np->rxopt.bits.rxinfo;
795 case IPV6_2292PKTINFO:
796 val = np->rxopt.bits.rxoinfo;
799 case IPV6_RECVHOPLIMIT:
800 val = np->rxopt.bits.rxhlim;
803 case IPV6_2292HOPLIMIT:
804 val = np->rxopt.bits.rxohlim;
808 val = np->rxopt.bits.srcrt;
812 val = np->rxopt.bits.osrcrt;
816 case IPV6_RTHDRDSTOPTS:
822 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
825 return put_user(len, optlen);
828 case IPV6_RECVHOPOPTS:
829 val = np->rxopt.bits.hopopts;
832 case IPV6_2292HOPOPTS:
833 val = np->rxopt.bits.ohopopts;
836 case IPV6_RECVDSTOPTS:
837 val = np->rxopt.bits.dstopts;
840 case IPV6_2292DSTOPTS:
841 val = np->rxopt.bits.odstopts;
848 case IPV6_RECVTCLASS:
849 val = np->rxopt.bits.rxtclass;
853 val = np->rxopt.bits.rxflow;
856 case IPV6_UNICAST_HOPS:
860 case IPV6_MULTICAST_HOPS:
861 val = np->mcast_hops;
864 case IPV6_MULTICAST_LOOP:
868 case IPV6_MULTICAST_IF:
872 case IPV6_MTU_DISCOVER:
880 case IPV6_FLOWINFO_SEND:
887 len = min_t(unsigned int, sizeof(int), len);
888 if(put_user(len, optlen))
890 if(copy_to_user(optval,&val,len))
895 int ipv6_getsockopt(struct sock *sk, int level, int optname,
896 char __user *optval, int __user *optlen)
900 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
901 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
903 if(level != SOL_IPV6)
906 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
907 #ifdef CONFIG_NETFILTER
908 /* we need to exclude all possible EINVALs except default case */
909 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
910 optname != MCAST_MSFILTER) {
913 if (get_user(len, optlen))
917 err = nf_getsockopt(sk, PF_INET6, optname, optval,
921 err = put_user(len, optlen);
928 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
929 char __user *optval, int __user *optlen)
933 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
934 if (udp_prot.compat_getsockopt != NULL)
935 return udp_prot.compat_getsockopt(sk, level, optname,
937 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
940 if (level != SOL_IPV6)
943 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
944 #ifdef CONFIG_NETFILTER
945 /* we need to exclude all possible EINVALs except default case */
946 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
947 optname != MCAST_MSFILTER) {
950 if (get_user(len, optlen))
954 err = compat_nf_getsockopt(sk, PF_INET6,
955 optname, optval, &len);
958 err = put_user(len, optlen);
964 EXPORT_SYMBOL(compat_ipv6_getsockopt);
967 void __init ipv6_packet_init(void)
969 dev_add_pack(&ipv6_packet_type);
972 void ipv6_packet_cleanup(void)
974 dev_remove_pack(&ipv6_packet_type);