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/config.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 int 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;
118 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
119 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
126 else if (get_user(val, (int __user *) optval))
136 if (val == PF_INET) {
137 struct ipv6_txoptions *opt;
138 struct sk_buff *pktopt;
140 if (sk->sk_protocol != IPPROTO_UDP &&
141 sk->sk_protocol != IPPROTO_TCP)
144 if (sk->sk_state != TCP_ESTABLISHED) {
149 if (ipv6_only_sock(sk) ||
150 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
151 retv = -EADDRNOTAVAIL;
155 fl6_free_socklist(sk);
156 ipv6_sock_mc_close(sk);
159 * Sock is moving from IPv6 to IPv4 (sk_prot), so
160 * remove it from the refcnt debug socks count in the
163 sk_refcnt_debug_dec(sk);
165 if (sk->sk_protocol == IPPROTO_TCP) {
166 struct inet_connection_sock *icsk = inet_csk(sk);
169 sock_prot_dec_use(sk->sk_prot);
170 sock_prot_inc_use(&tcp_prot);
172 sk->sk_prot = &tcp_prot;
173 icsk->icsk_af_ops = &ipv4_specific;
174 sk->sk_socket->ops = &inet_stream_ops;
175 sk->sk_family = PF_INET;
176 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
179 sock_prot_dec_use(sk->sk_prot);
180 sock_prot_inc_use(&udp_prot);
182 sk->sk_prot = &udp_prot;
183 sk->sk_socket->ops = &inet_dgram_ops;
184 sk->sk_family = PF_INET;
186 opt = xchg(&np->opt, NULL);
188 sock_kfree_s(sk, opt, opt->tot_len);
189 pktopt = xchg(&np->pktoptions, NULL);
193 sk->sk_destruct = inet_sock_destruct;
195 * ... and add it to the refcnt debug socks count
196 * in the new family. -acme
198 sk_refcnt_debug_inc(sk);
199 module_put(THIS_MODULE);
206 if (inet_sk(sk)->num)
208 np->ipv6only = valbool;
212 case IPV6_RECVPKTINFO:
213 np->rxopt.bits.rxinfo = valbool;
217 case IPV6_2292PKTINFO:
218 np->rxopt.bits.rxoinfo = valbool;
222 case IPV6_RECVHOPLIMIT:
223 np->rxopt.bits.rxhlim = valbool;
227 case IPV6_2292HOPLIMIT:
228 np->rxopt.bits.rxohlim = valbool;
233 if (val < 0 || val > 2)
235 np->rxopt.bits.srcrt = val;
240 if (val < 0 || val > 2)
242 np->rxopt.bits.osrcrt = val;
246 case IPV6_RECVHOPOPTS:
247 np->rxopt.bits.hopopts = valbool;
251 case IPV6_2292HOPOPTS:
252 np->rxopt.bits.ohopopts = valbool;
256 case IPV6_RECVDSTOPTS:
257 np->rxopt.bits.dstopts = valbool;
261 case IPV6_2292DSTOPTS:
262 np->rxopt.bits.odstopts = valbool;
267 if (val < 0 || val > 0xff)
273 case IPV6_RECVTCLASS:
274 np->rxopt.bits.rxtclass = valbool;
279 np->rxopt.bits.rxflow = valbool;
284 case IPV6_RTHDRDSTOPTS:
288 struct ipv6_txoptions *opt;
292 /* hop-by-hop / destination options are privileged option */
294 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
298 if (optlen & 0x7 || optlen > 8 * 255)
301 opt = ipv6_renew_options(sk, np->opt, optname,
302 (struct ipv6_opt_hdr __user *)optval,
309 /* routing header option needs extra check */
310 if (optname == IPV6_RTHDR && opt->srcrt) {
311 struct ipv6_rt_hdr *rthdr = opt->srcrt;
314 if ((rthdr->hdrlen & 1) ||
315 (rthdr->hdrlen >> 1) != rthdr->segments_left)
320 if (inet_sk(sk)->is_icsk) {
322 struct inet_connection_sock *icsk = inet_csk(sk);
323 if (!((1 << sk->sk_state) &
324 (TCPF_LISTEN | TCPF_CLOSE))
325 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
326 icsk->icsk_ext_hdr_len =
327 opt->opt_flen + opt->opt_nflen;
328 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
331 opt = xchg(&np->opt, opt);
334 write_lock(&sk->sk_dst_lock);
335 opt = xchg(&np->opt, opt);
336 write_unlock(&sk->sk_dst_lock);
341 sock_kfree_s(sk, opt, opt->tot_len);
345 case IPV6_2292PKTOPTIONS:
347 struct ipv6_txoptions *opt = NULL;
352 fl.fl6_flowlabel = 0;
353 fl.oif = sk->sk_bound_dev_if;
358 /* 1K is probably excessive
359 * 1K is surely not enough, 2K per standard header is 16K.
362 if (optlen > 64*1024)
365 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
370 memset(opt, 0, sizeof(*opt));
371 opt->tot_len = sizeof(*opt) + optlen;
373 if (copy_from_user(opt+1, optval, optlen))
376 msg.msg_controllen = optlen;
377 msg.msg_control = (void*)(opt+1);
379 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
384 if (inet_sk(sk)->is_icsk) {
386 struct inet_connection_sock *icsk = inet_csk(sk);
387 if (!((1 << sk->sk_state) &
388 (TCPF_LISTEN | TCPF_CLOSE))
389 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
390 icsk->icsk_ext_hdr_len =
391 opt->opt_flen + opt->opt_nflen;
392 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
395 opt = xchg(&np->opt, opt);
398 write_lock(&sk->sk_dst_lock);
399 opt = xchg(&np->opt, opt);
400 write_unlock(&sk->sk_dst_lock);
406 sock_kfree_s(sk, opt, opt->tot_len);
409 case IPV6_UNICAST_HOPS:
410 if (val > 255 || val < -1)
416 case IPV6_MULTICAST_HOPS:
417 if (sk->sk_type == SOCK_STREAM)
419 if (val > 255 || val < -1)
421 np->mcast_hops = val;
425 case IPV6_MULTICAST_LOOP:
426 np->mc_loop = valbool;
430 case IPV6_MULTICAST_IF:
431 if (sk->sk_type == SOCK_STREAM)
433 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
436 if (__dev_get_by_index(val) == NULL) {
443 case IPV6_ADD_MEMBERSHIP:
444 case IPV6_DROP_MEMBERSHIP:
446 struct ipv6_mreq mreq;
449 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
452 if (optname == IPV6_ADD_MEMBERSHIP)
453 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
455 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
458 case IPV6_JOIN_ANYCAST:
459 case IPV6_LEAVE_ANYCAST:
461 struct ipv6_mreq mreq;
463 if (optlen != sizeof(struct ipv6_mreq))
467 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
470 if (optname == IPV6_JOIN_ANYCAST)
471 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
473 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
476 case MCAST_JOIN_GROUP:
477 case MCAST_LEAVE_GROUP:
479 struct group_req greq;
480 struct sockaddr_in6 *psin6;
483 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
485 if (greq.gr_group.ss_family != AF_INET6) {
486 retv = -EADDRNOTAVAIL;
489 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
490 if (optname == MCAST_JOIN_GROUP)
491 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
494 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
498 case MCAST_JOIN_SOURCE_GROUP:
499 case MCAST_LEAVE_SOURCE_GROUP:
500 case MCAST_BLOCK_SOURCE:
501 case MCAST_UNBLOCK_SOURCE:
503 struct group_source_req greqs;
506 if (optlen != sizeof(struct group_source_req))
508 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
512 if (greqs.gsr_group.ss_family != AF_INET6 ||
513 greqs.gsr_source.ss_family != AF_INET6) {
514 retv = -EADDRNOTAVAIL;
517 if (optname == MCAST_BLOCK_SOURCE) {
518 omode = MCAST_EXCLUDE;
520 } else if (optname == MCAST_UNBLOCK_SOURCE) {
521 omode = MCAST_EXCLUDE;
523 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
524 struct sockaddr_in6 *psin6;
526 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
527 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
529 /* prior join w/ different source is ok */
530 if (retv && retv != -EADDRINUSE)
532 omode = MCAST_INCLUDE;
534 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
535 omode = MCAST_INCLUDE;
538 retv = ip6_mc_source(add, omode, sk, &greqs);
543 extern int sysctl_mld_max_msf;
544 struct group_filter *gsf;
546 if (optlen < GROUP_FILTER_SIZE(0))
548 if (optlen > sysctl_optmem_max) {
552 gsf = (struct group_filter *)kmalloc(optlen,GFP_KERNEL);
558 if (copy_from_user(gsf, optval, optlen)) {
562 /* numsrc >= (4G-140)/128 overflow in 32 bits */
563 if (gsf->gf_numsrc >= 0x1ffffffU ||
564 gsf->gf_numsrc > sysctl_mld_max_msf) {
569 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
574 retv = ip6_mc_msfilter(sk, gsf);
579 case IPV6_ROUTER_ALERT:
580 retv = ip6_ra_control(sk, val, NULL);
582 case IPV6_MTU_DISCOVER:
589 if (val && val < IPV6_MIN_MTU)
595 np->recverr = valbool;
597 skb_queue_purge(&sk->sk_error_queue);
600 case IPV6_FLOWINFO_SEND:
601 np->sndflow = valbool;
604 case IPV6_FLOWLABEL_MGR:
605 retv = ipv6_flowlabel_opt(sk, optval, optlen);
607 case IPV6_IPSEC_POLICY:
608 case IPV6_XFRM_POLICY:
610 if (!capable(CAP_NET_ADMIN))
612 retv = xfrm_user_policy(sk, optname, optval, optlen);
615 #ifdef CONFIG_NETFILTER
617 retv = nf_setsockopt(sk, PF_INET6, optname, optval,
633 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
634 char __user *optval, int len)
638 len = min_t(int, len, ipv6_optlen(hdr));
639 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
644 int ipv6_getsockopt(struct sock *sk, int level, int optname,
645 char __user *optval, int __user *optlen)
647 struct ipv6_pinfo *np = inet6_sk(sk);
651 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
652 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
655 if (get_user(len, optlen))
659 if (sk->sk_protocol != IPPROTO_UDP &&
660 sk->sk_protocol != IPPROTO_TCP)
662 if (sk->sk_state != TCP_ESTABLISHED)
668 struct group_filter gsf;
671 if (len < GROUP_FILTER_SIZE(0))
673 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
676 err = ip6_mc_msfget(sk, &gsf,
677 (struct group_filter __user *)optval, optlen);
682 case IPV6_2292PKTOPTIONS:
687 if (sk->sk_type != SOCK_STREAM)
690 msg.msg_control = optval;
691 msg.msg_controllen = len;
695 skb = np->pktoptions;
697 atomic_inc(&skb->users);
701 int err = datagram_recv_ctl(sk, &msg, skb);
706 if (np->rxopt.bits.rxinfo) {
707 struct in6_pktinfo src_info;
708 src_info.ipi6_ifindex = np->mcast_oif;
709 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
710 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
712 if (np->rxopt.bits.rxhlim) {
713 int hlim = np->mcast_hops;
714 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
716 if (np->rxopt.bits.rxoinfo) {
717 struct in6_pktinfo src_info;
718 src_info.ipi6_ifindex = np->mcast_oif;
719 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
720 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
722 if (np->rxopt.bits.rxohlim) {
723 int hlim = np->mcast_hops;
724 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
727 len -= msg.msg_controllen;
728 return put_user(len, optlen);
732 struct dst_entry *dst;
735 dst = sk_dst_get(sk);
750 case IPV6_RECVPKTINFO:
751 val = np->rxopt.bits.rxinfo;
754 case IPV6_2292PKTINFO:
755 val = np->rxopt.bits.rxoinfo;
758 case IPV6_RECVHOPLIMIT:
759 val = np->rxopt.bits.rxhlim;
762 case IPV6_2292HOPLIMIT:
763 val = np->rxopt.bits.rxohlim;
767 val = np->rxopt.bits.srcrt;
771 val = np->rxopt.bits.osrcrt;
775 case IPV6_RTHDRDSTOPTS:
781 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
784 return put_user(len, optlen);
787 case IPV6_RECVHOPOPTS:
788 val = np->rxopt.bits.hopopts;
791 case IPV6_2292HOPOPTS:
792 val = np->rxopt.bits.ohopopts;
795 case IPV6_RECVDSTOPTS:
796 val = np->rxopt.bits.dstopts;
799 case IPV6_2292DSTOPTS:
800 val = np->rxopt.bits.odstopts;
807 case IPV6_RECVTCLASS:
808 val = np->rxopt.bits.rxtclass;
812 val = np->rxopt.bits.rxflow;
815 case IPV6_UNICAST_HOPS:
819 case IPV6_MULTICAST_HOPS:
820 val = np->mcast_hops;
823 case IPV6_MULTICAST_LOOP:
827 case IPV6_MULTICAST_IF:
831 case IPV6_MTU_DISCOVER:
839 case IPV6_FLOWINFO_SEND:
844 #ifdef CONFIG_NETFILTER
846 val = nf_getsockopt(sk, PF_INET6, optname, optval,
850 val = put_user(len, optlen);
856 len = min_t(unsigned int, sizeof(int), len);
857 if(put_user(len, optlen))
859 if(copy_to_user(optval,&val,len))
864 void __init ipv6_packet_init(void)
866 dev_add_pack(&ipv6_packet_type);
869 void ipv6_packet_cleanup(void)
871 dev_remove_pack(&ipv6_packet_type);