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 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 inet_connection_sock *icsk = inet_csk(sk);
170 sock_prot_dec_use(sk->sk_prot);
171 sock_prot_inc_use(&tcp_prot);
173 sk->sk_prot = &tcp_prot;
174 icsk->icsk_af_ops = &ipv4_specific;
175 sk->sk_socket->ops = &inet_stream_ops;
176 sk->sk_family = PF_INET;
177 tcp_sync_mss(sk, icsk->icsk_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 if (val < 0 || val > 0xff)
274 case IPV6_RECVTCLASS:
275 np->rxopt.bits.rxtclass = valbool;
280 np->rxopt.bits.rxflow = valbool;
285 case IPV6_RTHDRDSTOPTS:
289 struct ipv6_txoptions *opt;
293 /* hop-by-hop / destination options are privileged option */
295 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
299 if (optlen & 0x7 || optlen > 8 * 255)
302 opt = ipv6_renew_options(sk, np->opt, optname,
303 (struct ipv6_opt_hdr __user *)optval,
310 /* routing header option needs extra check */
311 if (optname == IPV6_RTHDR && opt->srcrt) {
312 struct ipv6_rt_hdr *rthdr = opt->srcrt;
315 if ((rthdr->hdrlen & 1) ||
316 (rthdr->hdrlen >> 1) != rthdr->segments_left)
321 if (inet_sk(sk)->is_icsk) {
323 struct inet_connection_sock *icsk = inet_csk(sk);
324 if (!((1 << sk->sk_state) &
325 (TCPF_LISTEN | TCPF_CLOSE))
326 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
327 icsk->icsk_ext_hdr_len =
328 opt->opt_flen + opt->opt_nflen;
329 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
332 opt = xchg(&np->opt, opt);
335 write_lock(&sk->sk_dst_lock);
336 opt = xchg(&np->opt, opt);
337 write_unlock(&sk->sk_dst_lock);
342 sock_kfree_s(sk, opt, opt->tot_len);
346 case IPV6_2292PKTOPTIONS:
348 struct ipv6_txoptions *opt = NULL;
353 fl.fl6_flowlabel = 0;
354 fl.oif = sk->sk_bound_dev_if;
359 /* 1K is probably excessive
360 * 1K is surely not enough, 2K per standard header is 16K.
363 if (optlen > 64*1024)
366 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
371 memset(opt, 0, sizeof(*opt));
372 opt->tot_len = sizeof(*opt) + optlen;
374 if (copy_from_user(opt+1, optval, optlen))
377 msg.msg_controllen = optlen;
378 msg.msg_control = (void*)(opt+1);
380 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
385 if (inet_sk(sk)->is_icsk) {
387 struct inet_connection_sock *icsk = inet_csk(sk);
388 if (!((1 << sk->sk_state) &
389 (TCPF_LISTEN | TCPF_CLOSE))
390 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
391 icsk->icsk_ext_hdr_len =
392 opt->opt_flen + opt->opt_nflen;
393 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
396 opt = xchg(&np->opt, opt);
399 write_lock(&sk->sk_dst_lock);
400 opt = xchg(&np->opt, opt);
401 write_unlock(&sk->sk_dst_lock);
407 sock_kfree_s(sk, opt, opt->tot_len);
410 case IPV6_UNICAST_HOPS:
411 if (val > 255 || val < -1)
417 case IPV6_MULTICAST_HOPS:
418 if (sk->sk_type == SOCK_STREAM)
420 if (val > 255 || val < -1)
422 np->mcast_hops = val;
426 case IPV6_MULTICAST_LOOP:
427 np->mc_loop = valbool;
431 case IPV6_MULTICAST_IF:
432 if (sk->sk_type == SOCK_STREAM)
434 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
437 if (__dev_get_by_index(val) == NULL) {
444 case IPV6_ADD_MEMBERSHIP:
445 case IPV6_DROP_MEMBERSHIP:
447 struct ipv6_mreq mreq;
450 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
453 if (optname == IPV6_ADD_MEMBERSHIP)
454 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
456 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
459 case IPV6_JOIN_ANYCAST:
460 case IPV6_LEAVE_ANYCAST:
462 struct ipv6_mreq mreq;
464 if (optlen != sizeof(struct ipv6_mreq))
468 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
471 if (optname == IPV6_JOIN_ANYCAST)
472 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
474 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
477 case MCAST_JOIN_GROUP:
478 case MCAST_LEAVE_GROUP:
480 struct group_req greq;
481 struct sockaddr_in6 *psin6;
484 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
486 if (greq.gr_group.ss_family != AF_INET6) {
487 retv = -EADDRNOTAVAIL;
490 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
491 if (optname == MCAST_JOIN_GROUP)
492 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
495 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
499 case MCAST_JOIN_SOURCE_GROUP:
500 case MCAST_LEAVE_SOURCE_GROUP:
501 case MCAST_BLOCK_SOURCE:
502 case MCAST_UNBLOCK_SOURCE:
504 struct group_source_req greqs;
507 if (optlen != sizeof(struct group_source_req))
509 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
513 if (greqs.gsr_group.ss_family != AF_INET6 ||
514 greqs.gsr_source.ss_family != AF_INET6) {
515 retv = -EADDRNOTAVAIL;
518 if (optname == MCAST_BLOCK_SOURCE) {
519 omode = MCAST_EXCLUDE;
521 } else if (optname == MCAST_UNBLOCK_SOURCE) {
522 omode = MCAST_EXCLUDE;
524 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
525 struct sockaddr_in6 *psin6;
527 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
528 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
530 /* prior join w/ different source is ok */
531 if (retv && retv != -EADDRINUSE)
533 omode = MCAST_INCLUDE;
535 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
536 omode = MCAST_INCLUDE;
539 retv = ip6_mc_source(add, omode, sk, &greqs);
544 extern int sysctl_mld_max_msf;
545 struct group_filter *gsf;
547 if (optlen < GROUP_FILTER_SIZE(0))
549 if (optlen > sysctl_optmem_max) {
553 gsf = kmalloc(optlen,GFP_KERNEL);
559 if (copy_from_user(gsf, optval, optlen)) {
563 /* numsrc >= (4G-140)/128 overflow in 32 bits */
564 if (gsf->gf_numsrc >= 0x1ffffffU ||
565 gsf->gf_numsrc > sysctl_mld_max_msf) {
570 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
575 retv = ip6_mc_msfilter(sk, gsf);
580 case IPV6_ROUTER_ALERT:
581 retv = ip6_ra_control(sk, val, NULL);
583 case IPV6_MTU_DISCOVER:
590 if (val && val < IPV6_MIN_MTU)
596 np->recverr = valbool;
598 skb_queue_purge(&sk->sk_error_queue);
601 case IPV6_FLOWINFO_SEND:
602 np->sndflow = valbool;
605 case IPV6_FLOWLABEL_MGR:
606 retv = ipv6_flowlabel_opt(sk, optval, optlen);
608 case IPV6_IPSEC_POLICY:
609 case IPV6_XFRM_POLICY:
611 if (!capable(CAP_NET_ADMIN))
613 retv = xfrm_user_policy(sk, optname, optval, optlen);
616 #ifdef CONFIG_NETFILTER
618 retv = nf_setsockopt(sk, PF_INET6, optname, optval,
634 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
635 char __user *optval, int len)
639 len = min_t(int, len, ipv6_optlen(hdr));
640 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
645 int ipv6_getsockopt(struct sock *sk, int level, int optname,
646 char __user *optval, int __user *optlen)
648 struct ipv6_pinfo *np = inet6_sk(sk);
652 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
653 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
656 if (get_user(len, optlen))
660 if (sk->sk_protocol != IPPROTO_UDP &&
661 sk->sk_protocol != IPPROTO_TCP)
663 if (sk->sk_state != TCP_ESTABLISHED)
669 struct group_filter gsf;
672 if (len < GROUP_FILTER_SIZE(0))
674 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
677 err = ip6_mc_msfget(sk, &gsf,
678 (struct group_filter __user *)optval, optlen);
683 case IPV6_2292PKTOPTIONS:
688 if (sk->sk_type != SOCK_STREAM)
691 msg.msg_control = optval;
692 msg.msg_controllen = len;
696 skb = np->pktoptions;
698 atomic_inc(&skb->users);
702 int err = datagram_recv_ctl(sk, &msg, skb);
707 if (np->rxopt.bits.rxinfo) {
708 struct in6_pktinfo src_info;
709 src_info.ipi6_ifindex = np->mcast_oif;
710 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
711 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
713 if (np->rxopt.bits.rxhlim) {
714 int hlim = np->mcast_hops;
715 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
717 if (np->rxopt.bits.rxoinfo) {
718 struct in6_pktinfo src_info;
719 src_info.ipi6_ifindex = np->mcast_oif;
720 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
721 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
723 if (np->rxopt.bits.rxohlim) {
724 int hlim = np->mcast_hops;
725 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
728 len -= msg.msg_controllen;
729 return put_user(len, optlen);
733 struct dst_entry *dst;
736 dst = sk_dst_get(sk);
751 case IPV6_RECVPKTINFO:
752 val = np->rxopt.bits.rxinfo;
755 case IPV6_2292PKTINFO:
756 val = np->rxopt.bits.rxoinfo;
759 case IPV6_RECVHOPLIMIT:
760 val = np->rxopt.bits.rxhlim;
763 case IPV6_2292HOPLIMIT:
764 val = np->rxopt.bits.rxohlim;
768 val = np->rxopt.bits.srcrt;
772 val = np->rxopt.bits.osrcrt;
776 case IPV6_RTHDRDSTOPTS:
782 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
785 return put_user(len, optlen);
788 case IPV6_RECVHOPOPTS:
789 val = np->rxopt.bits.hopopts;
792 case IPV6_2292HOPOPTS:
793 val = np->rxopt.bits.ohopopts;
796 case IPV6_RECVDSTOPTS:
797 val = np->rxopt.bits.dstopts;
800 case IPV6_2292DSTOPTS:
801 val = np->rxopt.bits.odstopts;
808 case IPV6_RECVTCLASS:
809 val = np->rxopt.bits.rxtclass;
813 val = np->rxopt.bits.rxflow;
816 case IPV6_UNICAST_HOPS:
820 case IPV6_MULTICAST_HOPS:
821 val = np->mcast_hops;
824 case IPV6_MULTICAST_LOOP:
828 case IPV6_MULTICAST_IF:
832 case IPV6_MTU_DISCOVER:
840 case IPV6_FLOWINFO_SEND:
845 #ifdef CONFIG_NETFILTER
847 val = nf_getsockopt(sk, PF_INET6, optname, optval,
851 val = put_user(len, optlen);
857 len = min_t(unsigned int, sizeof(int), len);
858 if(put_user(len, optlen))
860 if(copy_to_user(optval,&val,len))
865 void __init ipv6_packet_init(void)
867 dev_add_pack(&ipv6_packet_type);
870 void ipv6_packet_cleanup(void)
872 dev_remove_pack(&ipv6_packet_type);