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);
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 int 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;
119 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
120 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
127 else if (get_user(val, (int __user *) optval))
137 if (val == PF_INET) {
138 struct ipv6_txoptions *opt;
139 struct sk_buff *pktopt;
141 if (sk->sk_protocol != IPPROTO_UDP &&
142 sk->sk_protocol != IPPROTO_TCP)
145 if (sk->sk_state != TCP_ESTABLISHED) {
150 if (ipv6_only_sock(sk) ||
151 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
152 retv = -EADDRNOTAVAIL;
156 fl6_free_socklist(sk);
157 ipv6_sock_mc_close(sk);
160 * Sock is moving from IPv6 to IPv4 (sk_prot), so
161 * remove it from the refcnt debug socks count in the
164 sk_refcnt_debug_dec(sk);
166 if (sk->sk_protocol == IPPROTO_TCP) {
167 struct tcp_sock *tp = tcp_sk(sk);
170 sock_prot_dec_use(sk->sk_prot);
171 sock_prot_inc_use(&tcp_prot);
173 sk->sk_prot = &tcp_prot;
174 tp->af_specific = &ipv4_specific;
175 sk->sk_socket->ops = &inet_stream_ops;
176 sk->sk_family = PF_INET;
177 tcp_sync_mss(sk, tp->pmtu_cookie);
180 sock_prot_dec_use(sk->sk_prot);
181 sock_prot_inc_use(&udp_prot);
183 sk->sk_prot = &udp_prot;
184 sk->sk_socket->ops = &inet_dgram_ops;
185 sk->sk_family = PF_INET;
187 opt = xchg(&np->opt, NULL);
189 sock_kfree_s(sk, opt, opt->tot_len);
190 pktopt = xchg(&np->pktoptions, NULL);
194 sk->sk_destruct = inet_sock_destruct;
196 * ... and add it to the refcnt debug socks count
197 * in the new family. -acme
199 sk_refcnt_debug_inc(sk);
200 module_put(THIS_MODULE);
207 if (inet_sk(sk)->num)
209 np->ipv6only = valbool;
213 case IPV6_RECVPKTINFO:
214 np->rxopt.bits.rxinfo = valbool;
218 case IPV6_2292PKTINFO:
219 np->rxopt.bits.rxoinfo = valbool;
223 case IPV6_RECVHOPLIMIT:
224 np->rxopt.bits.rxhlim = valbool;
228 case IPV6_2292HOPLIMIT:
229 np->rxopt.bits.rxohlim = valbool;
234 if (val < 0 || val > 2)
236 np->rxopt.bits.srcrt = val;
241 if (val < 0 || val > 2)
243 np->rxopt.bits.osrcrt = val;
247 case IPV6_RECVHOPOPTS:
248 np->rxopt.bits.hopopts = valbool;
252 case IPV6_2292HOPOPTS:
253 np->rxopt.bits.ohopopts = valbool;
257 case IPV6_RECVDSTOPTS:
258 np->rxopt.bits.dstopts = valbool;
262 case IPV6_2292DSTOPTS:
263 np->rxopt.bits.odstopts = valbool;
268 np->rxopt.bits.rxflow = valbool;
273 case IPV6_RTHDRDSTOPTS:
277 struct ipv6_txoptions *opt;
281 /* hop-by-hop / destination options are privileged option */
283 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
287 if (optlen & 0x7 || optlen > 8 * 255)
290 opt = ipv6_renew_options(sk, np->opt, optname,
291 (struct ipv6_opt_hdr __user *)optval,
298 /* routing header option needs extra check */
299 if (optname == IPV6_RTHDR && opt->srcrt) {
300 struct ipv6_rt_hdr *rthdr = opt->srcrt;
303 if ((rthdr->hdrlen & 1) ||
304 (rthdr->hdrlen >> 1) != rthdr->segments_left)
309 if (sk->sk_type == SOCK_STREAM) {
311 struct tcp_sock *tp = tcp_sk(sk);
312 if (!((1 << sk->sk_state) &
313 (TCPF_LISTEN | TCPF_CLOSE))
314 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
315 tp->ext_header_len = opt->opt_flen + opt->opt_nflen;
316 tcp_sync_mss(sk, tp->pmtu_cookie);
319 opt = xchg(&np->opt, opt);
322 write_lock(&sk->sk_dst_lock);
323 opt = xchg(&np->opt, opt);
324 write_unlock(&sk->sk_dst_lock);
329 sock_kfree_s(sk, opt, opt->tot_len);
333 case IPV6_2292PKTOPTIONS:
335 struct ipv6_txoptions *opt = NULL;
340 fl.fl6_flowlabel = 0;
341 fl.oif = sk->sk_bound_dev_if;
346 /* 1K is probably excessive
347 * 1K is surely not enough, 2K per standard header is 16K.
350 if (optlen > 64*1024)
353 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
358 memset(opt, 0, sizeof(*opt));
359 opt->tot_len = sizeof(*opt) + optlen;
361 if (copy_from_user(opt+1, optval, optlen))
364 msg.msg_controllen = optlen;
365 msg.msg_control = (void*)(opt+1);
367 retv = datagram_send_ctl(&msg, &fl, opt, &junk);
372 if (sk->sk_type == SOCK_STREAM) {
374 struct tcp_sock *tp = tcp_sk(sk);
375 if (!((1 << sk->sk_state) &
376 (TCPF_LISTEN | TCPF_CLOSE))
377 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
378 tp->ext_header_len = opt->opt_flen + opt->opt_nflen;
379 tcp_sync_mss(sk, tp->pmtu_cookie);
382 opt = xchg(&np->opt, opt);
385 write_lock(&sk->sk_dst_lock);
386 opt = xchg(&np->opt, opt);
387 write_unlock(&sk->sk_dst_lock);
393 sock_kfree_s(sk, opt, opt->tot_len);
396 case IPV6_UNICAST_HOPS:
397 if (val > 255 || val < -1)
403 case IPV6_MULTICAST_HOPS:
404 if (sk->sk_type == SOCK_STREAM)
406 if (val > 255 || val < -1)
408 np->mcast_hops = val;
412 case IPV6_MULTICAST_LOOP:
413 np->mc_loop = valbool;
417 case IPV6_MULTICAST_IF:
418 if (sk->sk_type == SOCK_STREAM)
420 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
423 if (__dev_get_by_index(val) == NULL) {
430 case IPV6_ADD_MEMBERSHIP:
431 case IPV6_DROP_MEMBERSHIP:
433 struct ipv6_mreq mreq;
436 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
439 if (optname == IPV6_ADD_MEMBERSHIP)
440 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
442 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
445 case IPV6_JOIN_ANYCAST:
446 case IPV6_LEAVE_ANYCAST:
448 struct ipv6_mreq mreq;
450 if (optlen != sizeof(struct ipv6_mreq))
454 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
457 if (optname == IPV6_JOIN_ANYCAST)
458 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
460 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
463 case MCAST_JOIN_GROUP:
464 case MCAST_LEAVE_GROUP:
466 struct group_req greq;
467 struct sockaddr_in6 *psin6;
470 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
472 if (greq.gr_group.ss_family != AF_INET6) {
473 retv = -EADDRNOTAVAIL;
476 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
477 if (optname == MCAST_JOIN_GROUP)
478 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
481 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
485 case MCAST_JOIN_SOURCE_GROUP:
486 case MCAST_LEAVE_SOURCE_GROUP:
487 case MCAST_BLOCK_SOURCE:
488 case MCAST_UNBLOCK_SOURCE:
490 struct group_source_req greqs;
493 if (optlen != sizeof(struct group_source_req))
495 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
499 if (greqs.gsr_group.ss_family != AF_INET6 ||
500 greqs.gsr_source.ss_family != AF_INET6) {
501 retv = -EADDRNOTAVAIL;
504 if (optname == MCAST_BLOCK_SOURCE) {
505 omode = MCAST_EXCLUDE;
507 } else if (optname == MCAST_UNBLOCK_SOURCE) {
508 omode = MCAST_EXCLUDE;
510 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
511 struct sockaddr_in6 *psin6;
513 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
514 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
516 /* prior join w/ different source is ok */
517 if (retv && retv != -EADDRINUSE)
519 omode = MCAST_INCLUDE;
521 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
522 omode = MCAST_INCLUDE;
525 retv = ip6_mc_source(add, omode, sk, &greqs);
530 extern int sysctl_mld_max_msf;
531 struct group_filter *gsf;
533 if (optlen < GROUP_FILTER_SIZE(0))
535 if (optlen > sysctl_optmem_max) {
539 gsf = (struct group_filter *)kmalloc(optlen,GFP_KERNEL);
545 if (copy_from_user(gsf, optval, optlen)) {
549 /* numsrc >= (4G-140)/128 overflow in 32 bits */
550 if (gsf->gf_numsrc >= 0x1ffffffU ||
551 gsf->gf_numsrc > sysctl_mld_max_msf) {
556 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
561 retv = ip6_mc_msfilter(sk, gsf);
566 case IPV6_ROUTER_ALERT:
567 retv = ip6_ra_control(sk, val, NULL);
569 case IPV6_MTU_DISCOVER:
576 if (val && val < IPV6_MIN_MTU)
582 np->recverr = valbool;
584 skb_queue_purge(&sk->sk_error_queue);
587 case IPV6_FLOWINFO_SEND:
588 np->sndflow = valbool;
591 case IPV6_FLOWLABEL_MGR:
592 retv = ipv6_flowlabel_opt(sk, optval, optlen);
594 case IPV6_IPSEC_POLICY:
595 case IPV6_XFRM_POLICY:
597 if (!capable(CAP_NET_ADMIN))
599 retv = xfrm_user_policy(sk, optname, optval, optlen);
602 #ifdef CONFIG_NETFILTER
604 retv = nf_setsockopt(sk, PF_INET6, optname, optval,
620 int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
621 char __user *optval, int len)
625 len = min_t(int, len, ipv6_optlen(hdr));
626 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
631 int ipv6_getsockopt(struct sock *sk, int level, int optname,
632 char __user *optval, int __user *optlen)
634 struct ipv6_pinfo *np = inet6_sk(sk);
638 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
639 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
642 if (get_user(len, optlen))
646 if (sk->sk_protocol != IPPROTO_UDP &&
647 sk->sk_protocol != IPPROTO_TCP)
649 if (sk->sk_state != TCP_ESTABLISHED)
655 struct group_filter gsf;
658 if (len < GROUP_FILTER_SIZE(0))
660 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
663 err = ip6_mc_msfget(sk, &gsf,
664 (struct group_filter __user *)optval, optlen);
669 case IPV6_2292PKTOPTIONS:
674 if (sk->sk_type != SOCK_STREAM)
677 msg.msg_control = optval;
678 msg.msg_controllen = len;
682 skb = np->pktoptions;
684 atomic_inc(&skb->users);
688 int err = datagram_recv_ctl(sk, &msg, skb);
693 if (np->rxopt.bits.rxinfo) {
694 struct in6_pktinfo src_info;
695 src_info.ipi6_ifindex = np->mcast_oif;
696 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
697 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
699 if (np->rxopt.bits.rxhlim) {
700 int hlim = np->mcast_hops;
701 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
703 if (np->rxopt.bits.rxoinfo) {
704 struct in6_pktinfo src_info;
705 src_info.ipi6_ifindex = np->mcast_oif;
706 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
707 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
709 if (np->rxopt.bits.rxohlim) {
710 int hlim = np->mcast_hops;
711 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
714 len -= msg.msg_controllen;
715 return put_user(len, optlen);
719 struct dst_entry *dst;
722 dst = sk_dst_get(sk);
737 case IPV6_RECVPKTINFO:
738 val = np->rxopt.bits.rxinfo;
741 case IPV6_2292PKTINFO:
742 val = np->rxopt.bits.rxoinfo;
745 case IPV6_RECVHOPLIMIT:
746 val = np->rxopt.bits.rxhlim;
749 case IPV6_2292HOPLIMIT:
750 val = np->rxopt.bits.rxohlim;
754 val = np->rxopt.bits.srcrt;
758 val = np->rxopt.bits.osrcrt;
762 case IPV6_RTHDRDSTOPTS:
768 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
771 return put_user(len, optlen);
774 case IPV6_RECVHOPOPTS:
775 val = np->rxopt.bits.hopopts;
778 case IPV6_2292HOPOPTS:
779 val = np->rxopt.bits.ohopopts;
782 case IPV6_RECVDSTOPTS:
783 val = np->rxopt.bits.dstopts;
786 case IPV6_2292DSTOPTS:
787 val = np->rxopt.bits.odstopts;
791 val = np->rxopt.bits.rxflow;
794 case IPV6_UNICAST_HOPS:
798 case IPV6_MULTICAST_HOPS:
799 val = np->mcast_hops;
802 case IPV6_MULTICAST_LOOP:
806 case IPV6_MULTICAST_IF:
810 case IPV6_MTU_DISCOVER:
818 case IPV6_FLOWINFO_SEND:
823 #ifdef CONFIG_NETFILTER
825 val = nf_getsockopt(sk, PF_INET6, optname, optval,
829 val = put_user(len, optlen);
835 len = min_t(unsigned int, sizeof(int), len);
836 if(put_user(len, optlen))
838 if(copy_to_user(optval,&val,len))
843 void __init ipv6_packet_init(void)
845 dev_add_pack(&ipv6_packet_type);
848 void ipv6_packet_cleanup(void)
850 dev_remove_pack(&ipv6_packet_type);