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 sk_buff *ipv6_gso_segment(struct sk_buff *skb, int features)
62 struct sk_buff *segs = ERR_PTR(-EINVAL);
63 struct ipv6hdr *ipv6h;
64 struct inet6_protocol *ops;
67 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
70 ipv6h = skb->nh.ipv6h;
71 proto = ipv6h->nexthdr;
72 __skb_pull(skb, sizeof(*ipv6h));
76 struct ipv6_opt_hdr *opth;
79 if (proto != NEXTHDR_HOP) {
80 ops = rcu_dereference(inet6_protos[proto]);
85 if (!(ops->flags & INET6_PROTO_GSO_EXTHDR))
89 if (unlikely(!pskb_may_pull(skb, 8)))
92 opth = (void *)skb->data;
93 len = opth->hdrlen * 8 + 8;
95 if (unlikely(!pskb_may_pull(skb, len)))
98 proto = opth->nexthdr;
102 skb->h.raw = skb->data;
103 if (likely(ops->gso_segment))
104 segs = ops->gso_segment(skb, features);
109 if (unlikely(IS_ERR(segs)))
112 for (skb = segs; skb; skb = skb->next) {
113 ipv6h = skb->nh.ipv6h;
114 ipv6h->payload_len = htons(skb->len - skb->mac_len);
121 static struct packet_type ipv6_packet_type = {
122 .type = __constant_htons(ETH_P_IPV6),
124 .gso_segment = ipv6_gso_segment,
127 struct ip6_ra_chain *ip6_ra_chain;
128 DEFINE_RWLOCK(ip6_ra_lock);
130 int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
132 struct ip6_ra_chain *ra, *new_ra, **rap;
134 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
135 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->num != IPPROTO_RAW)
138 new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
140 write_lock_bh(&ip6_ra_lock);
141 for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
144 write_unlock_bh(&ip6_ra_lock);
150 write_unlock_bh(&ip6_ra_lock);
159 if (new_ra == NULL) {
160 write_unlock_bh(&ip6_ra_lock);
165 new_ra->destructor = destructor;
169 write_unlock_bh(&ip6_ra_lock);
173 static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
174 char __user *optval, int optlen)
176 struct ipv6_pinfo *np = inet6_sk(sk);
178 int retv = -ENOPROTOOPT;
182 else if (get_user(val, (int __user *) optval))
192 if (val == PF_INET) {
193 struct ipv6_txoptions *opt;
194 struct sk_buff *pktopt;
196 if (sk->sk_protocol != IPPROTO_UDP &&
197 sk->sk_protocol != IPPROTO_TCP)
200 if (sk->sk_state != TCP_ESTABLISHED) {
205 if (ipv6_only_sock(sk) ||
206 !(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
207 retv = -EADDRNOTAVAIL;
211 fl6_free_socklist(sk);
212 ipv6_sock_mc_close(sk);
215 * Sock is moving from IPv6 to IPv4 (sk_prot), so
216 * remove it from the refcnt debug socks count in the
219 sk_refcnt_debug_dec(sk);
221 if (sk->sk_protocol == IPPROTO_TCP) {
222 struct inet_connection_sock *icsk = inet_csk(sk);
225 sock_prot_dec_use(sk->sk_prot);
226 sock_prot_inc_use(&tcp_prot);
228 sk->sk_prot = &tcp_prot;
229 icsk->icsk_af_ops = &ipv4_specific;
230 sk->sk_socket->ops = &inet_stream_ops;
231 sk->sk_family = PF_INET;
232 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
235 sock_prot_dec_use(sk->sk_prot);
236 sock_prot_inc_use(&udp_prot);
238 sk->sk_prot = &udp_prot;
239 sk->sk_socket->ops = &inet_dgram_ops;
240 sk->sk_family = PF_INET;
242 opt = xchg(&np->opt, NULL);
244 sock_kfree_s(sk, opt, opt->tot_len);
245 pktopt = xchg(&np->pktoptions, NULL);
249 sk->sk_destruct = inet_sock_destruct;
251 * ... and add it to the refcnt debug socks count
252 * in the new family. -acme
254 sk_refcnt_debug_inc(sk);
255 module_put(THIS_MODULE);
262 if (inet_sk(sk)->num)
264 np->ipv6only = valbool;
268 case IPV6_RECVPKTINFO:
269 np->rxopt.bits.rxinfo = valbool;
273 case IPV6_2292PKTINFO:
274 np->rxopt.bits.rxoinfo = valbool;
278 case IPV6_RECVHOPLIMIT:
279 np->rxopt.bits.rxhlim = valbool;
283 case IPV6_2292HOPLIMIT:
284 np->rxopt.bits.rxohlim = valbool;
289 if (val < 0 || val > 2)
291 np->rxopt.bits.srcrt = val;
296 if (val < 0 || val > 2)
298 np->rxopt.bits.osrcrt = val;
302 case IPV6_RECVHOPOPTS:
303 np->rxopt.bits.hopopts = valbool;
307 case IPV6_2292HOPOPTS:
308 np->rxopt.bits.ohopopts = valbool;
312 case IPV6_RECVDSTOPTS:
313 np->rxopt.bits.dstopts = valbool;
317 case IPV6_2292DSTOPTS:
318 np->rxopt.bits.odstopts = valbool;
323 if (val < 0 || val > 0xff)
329 case IPV6_RECVTCLASS:
330 np->rxopt.bits.rxtclass = valbool;
335 np->rxopt.bits.rxflow = valbool;
340 case IPV6_RTHDRDSTOPTS:
344 struct ipv6_txoptions *opt;
348 /* hop-by-hop / destination options are privileged option */
350 if (optname != IPV6_RTHDR && !capable(CAP_NET_RAW))
354 if (optlen & 0x7 || optlen > 8 * 255)
357 opt = ipv6_renew_options(sk, np->opt, optname,
358 (struct ipv6_opt_hdr __user *)optval,
365 /* routing header option needs extra check */
366 if (optname == IPV6_RTHDR && opt->srcrt) {
367 struct ipv6_rt_hdr *rthdr = opt->srcrt;
370 if ((rthdr->hdrlen & 1) ||
371 (rthdr->hdrlen >> 1) != rthdr->segments_left)
376 if (inet_sk(sk)->is_icsk) {
378 struct inet_connection_sock *icsk = inet_csk(sk);
379 if (!((1 << sk->sk_state) &
380 (TCPF_LISTEN | TCPF_CLOSE))
381 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
382 icsk->icsk_ext_hdr_len =
383 opt->opt_flen + opt->opt_nflen;
384 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
387 opt = xchg(&np->opt, opt);
390 write_lock(&sk->sk_dst_lock);
391 opt = xchg(&np->opt, opt);
392 write_unlock(&sk->sk_dst_lock);
397 sock_kfree_s(sk, opt, opt->tot_len);
401 case IPV6_2292PKTOPTIONS:
403 struct ipv6_txoptions *opt = NULL;
408 fl.fl6_flowlabel = 0;
409 fl.oif = sk->sk_bound_dev_if;
414 /* 1K is probably excessive
415 * 1K is surely not enough, 2K per standard header is 16K.
418 if (optlen > 64*1024)
421 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
426 memset(opt, 0, sizeof(*opt));
427 opt->tot_len = sizeof(*opt) + optlen;
429 if (copy_from_user(opt+1, optval, optlen))
432 msg.msg_controllen = optlen;
433 msg.msg_control = (void*)(opt+1);
435 retv = datagram_send_ctl(&msg, &fl, opt, &junk, &junk);
440 if (inet_sk(sk)->is_icsk) {
442 struct inet_connection_sock *icsk = inet_csk(sk);
443 if (!((1 << sk->sk_state) &
444 (TCPF_LISTEN | TCPF_CLOSE))
445 && inet_sk(sk)->daddr != LOOPBACK4_IPV6) {
446 icsk->icsk_ext_hdr_len =
447 opt->opt_flen + opt->opt_nflen;
448 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
451 opt = xchg(&np->opt, opt);
454 write_lock(&sk->sk_dst_lock);
455 opt = xchg(&np->opt, opt);
456 write_unlock(&sk->sk_dst_lock);
462 sock_kfree_s(sk, opt, opt->tot_len);
465 case IPV6_UNICAST_HOPS:
466 if (val > 255 || val < -1)
472 case IPV6_MULTICAST_HOPS:
473 if (sk->sk_type == SOCK_STREAM)
475 if (val > 255 || val < -1)
477 np->mcast_hops = val;
481 case IPV6_MULTICAST_LOOP:
482 np->mc_loop = valbool;
486 case IPV6_MULTICAST_IF:
487 if (sk->sk_type == SOCK_STREAM)
489 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != val)
492 if (__dev_get_by_index(val) == NULL) {
499 case IPV6_ADD_MEMBERSHIP:
500 case IPV6_DROP_MEMBERSHIP:
502 struct ipv6_mreq mreq;
505 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
508 if (optname == IPV6_ADD_MEMBERSHIP)
509 retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
511 retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
514 case IPV6_JOIN_ANYCAST:
515 case IPV6_LEAVE_ANYCAST:
517 struct ipv6_mreq mreq;
519 if (optlen != sizeof(struct ipv6_mreq))
523 if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
526 if (optname == IPV6_JOIN_ANYCAST)
527 retv = ipv6_sock_ac_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
529 retv = ipv6_sock_ac_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_acaddr);
532 case MCAST_JOIN_GROUP:
533 case MCAST_LEAVE_GROUP:
535 struct group_req greq;
536 struct sockaddr_in6 *psin6;
539 if (copy_from_user(&greq, optval, sizeof(struct group_req)))
541 if (greq.gr_group.ss_family != AF_INET6) {
542 retv = -EADDRNOTAVAIL;
545 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
546 if (optname == MCAST_JOIN_GROUP)
547 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
550 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
554 case MCAST_JOIN_SOURCE_GROUP:
555 case MCAST_LEAVE_SOURCE_GROUP:
556 case MCAST_BLOCK_SOURCE:
557 case MCAST_UNBLOCK_SOURCE:
559 struct group_source_req greqs;
562 if (optlen != sizeof(struct group_source_req))
564 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
568 if (greqs.gsr_group.ss_family != AF_INET6 ||
569 greqs.gsr_source.ss_family != AF_INET6) {
570 retv = -EADDRNOTAVAIL;
573 if (optname == MCAST_BLOCK_SOURCE) {
574 omode = MCAST_EXCLUDE;
576 } else if (optname == MCAST_UNBLOCK_SOURCE) {
577 omode = MCAST_EXCLUDE;
579 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
580 struct sockaddr_in6 *psin6;
582 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
583 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
585 /* prior join w/ different source is ok */
586 if (retv && retv != -EADDRINUSE)
588 omode = MCAST_INCLUDE;
590 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
591 omode = MCAST_INCLUDE;
594 retv = ip6_mc_source(add, omode, sk, &greqs);
599 extern int sysctl_mld_max_msf;
600 struct group_filter *gsf;
602 if (optlen < GROUP_FILTER_SIZE(0))
604 if (optlen > sysctl_optmem_max) {
608 gsf = kmalloc(optlen,GFP_KERNEL);
614 if (copy_from_user(gsf, optval, optlen)) {
618 /* numsrc >= (4G-140)/128 overflow in 32 bits */
619 if (gsf->gf_numsrc >= 0x1ffffffU ||
620 gsf->gf_numsrc > sysctl_mld_max_msf) {
625 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
630 retv = ip6_mc_msfilter(sk, gsf);
635 case IPV6_ROUTER_ALERT:
636 retv = ip6_ra_control(sk, val, NULL);
638 case IPV6_MTU_DISCOVER:
645 if (val && val < IPV6_MIN_MTU)
651 np->recverr = valbool;
653 skb_queue_purge(&sk->sk_error_queue);
656 case IPV6_FLOWINFO_SEND:
657 np->sndflow = valbool;
660 case IPV6_FLOWLABEL_MGR:
661 retv = ipv6_flowlabel_opt(sk, optval, optlen);
663 case IPV6_IPSEC_POLICY:
664 case IPV6_XFRM_POLICY:
666 if (!capable(CAP_NET_ADMIN))
668 retv = xfrm_user_policy(sk, optname, optval, optlen);
681 int ipv6_setsockopt(struct sock *sk, int level, int optname,
682 char __user *optval, int optlen)
686 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
687 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
689 if (level != SOL_IPV6)
692 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
693 #ifdef CONFIG_NETFILTER
694 /* we need to exclude all possible ENOPROTOOPTs except default case */
695 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
696 optname != IPV6_XFRM_POLICY) {
698 err = nf_setsockopt(sk, PF_INET6, optname, optval,
708 int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
709 char __user *optval, int optlen)
713 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
714 if (udp_prot.compat_setsockopt != NULL)
715 return udp_prot.compat_setsockopt(sk, level, optname,
717 return udp_prot.setsockopt(sk, level, optname, optval, optlen);
720 if (level != SOL_IPV6)
723 err = do_ipv6_setsockopt(sk, level, optname, optval, optlen);
724 #ifdef CONFIG_NETFILTER
725 /* we need to exclude all possible ENOPROTOOPTs except default case */
726 if (err == -ENOPROTOOPT && optname != IPV6_IPSEC_POLICY &&
727 optname != IPV6_XFRM_POLICY) {
729 err = compat_nf_setsockopt(sk, PF_INET6, optname,
737 EXPORT_SYMBOL(compat_ipv6_setsockopt);
740 static int ipv6_getsockopt_sticky(struct sock *sk, struct ipv6_opt_hdr *hdr,
741 char __user *optval, int len)
745 len = min_t(int, len, ipv6_optlen(hdr));
746 if (copy_to_user(optval, hdr, ipv6_optlen(hdr)))
751 static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
752 char __user *optval, int __user *optlen)
754 struct ipv6_pinfo *np = inet6_sk(sk);
758 if (get_user(len, optlen))
762 if (sk->sk_protocol != IPPROTO_UDP &&
763 sk->sk_protocol != IPPROTO_TCP)
765 if (sk->sk_state != TCP_ESTABLISHED)
771 struct group_filter gsf;
774 if (len < GROUP_FILTER_SIZE(0))
776 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0)))
779 err = ip6_mc_msfget(sk, &gsf,
780 (struct group_filter __user *)optval, optlen);
785 case IPV6_2292PKTOPTIONS:
790 if (sk->sk_type != SOCK_STREAM)
793 msg.msg_control = optval;
794 msg.msg_controllen = len;
798 skb = np->pktoptions;
800 atomic_inc(&skb->users);
804 int err = datagram_recv_ctl(sk, &msg, skb);
809 if (np->rxopt.bits.rxinfo) {
810 struct in6_pktinfo src_info;
811 src_info.ipi6_ifindex = np->mcast_oif;
812 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
813 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
815 if (np->rxopt.bits.rxhlim) {
816 int hlim = np->mcast_hops;
817 put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
819 if (np->rxopt.bits.rxoinfo) {
820 struct in6_pktinfo src_info;
821 src_info.ipi6_ifindex = np->mcast_oif;
822 ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
823 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
825 if (np->rxopt.bits.rxohlim) {
826 int hlim = np->mcast_hops;
827 put_cmsg(&msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
830 len -= msg.msg_controllen;
831 return put_user(len, optlen);
835 struct dst_entry *dst;
838 dst = sk_dst_get(sk);
853 case IPV6_RECVPKTINFO:
854 val = np->rxopt.bits.rxinfo;
857 case IPV6_2292PKTINFO:
858 val = np->rxopt.bits.rxoinfo;
861 case IPV6_RECVHOPLIMIT:
862 val = np->rxopt.bits.rxhlim;
865 case IPV6_2292HOPLIMIT:
866 val = np->rxopt.bits.rxohlim;
870 val = np->rxopt.bits.srcrt;
874 val = np->rxopt.bits.osrcrt;
878 case IPV6_RTHDRDSTOPTS:
884 len = ipv6_getsockopt_sticky(sk, np->opt->hopopt,
887 return put_user(len, optlen);
890 case IPV6_RECVHOPOPTS:
891 val = np->rxopt.bits.hopopts;
894 case IPV6_2292HOPOPTS:
895 val = np->rxopt.bits.ohopopts;
898 case IPV6_RECVDSTOPTS:
899 val = np->rxopt.bits.dstopts;
902 case IPV6_2292DSTOPTS:
903 val = np->rxopt.bits.odstopts;
910 case IPV6_RECVTCLASS:
911 val = np->rxopt.bits.rxtclass;
915 val = np->rxopt.bits.rxflow;
918 case IPV6_UNICAST_HOPS:
922 case IPV6_MULTICAST_HOPS:
923 val = np->mcast_hops;
926 case IPV6_MULTICAST_LOOP:
930 case IPV6_MULTICAST_IF:
934 case IPV6_MTU_DISCOVER:
942 case IPV6_FLOWINFO_SEND:
949 len = min_t(unsigned int, sizeof(int), len);
950 if(put_user(len, optlen))
952 if(copy_to_user(optval,&val,len))
957 int ipv6_getsockopt(struct sock *sk, int level, int optname,
958 char __user *optval, int __user *optlen)
962 if (level == SOL_IP && sk->sk_type != SOCK_RAW)
963 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
965 if(level != SOL_IPV6)
968 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
969 #ifdef CONFIG_NETFILTER
970 /* we need to exclude all possible EINVALs except default case */
971 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
972 optname != MCAST_MSFILTER) {
975 if (get_user(len, optlen))
979 err = nf_getsockopt(sk, PF_INET6, optname, optval,
983 err = put_user(len, optlen);
990 int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
991 char __user *optval, int __user *optlen)
995 if (level == SOL_IP && sk->sk_type != SOCK_RAW) {
996 if (udp_prot.compat_getsockopt != NULL)
997 return udp_prot.compat_getsockopt(sk, level, optname,
999 return udp_prot.getsockopt(sk, level, optname, optval, optlen);
1002 if (level != SOL_IPV6)
1003 return -ENOPROTOOPT;
1005 err = do_ipv6_getsockopt(sk, level, optname, optval, optlen);
1006 #ifdef CONFIG_NETFILTER
1007 /* we need to exclude all possible EINVALs except default case */
1008 if (err == -EINVAL && optname != IPV6_ADDRFORM &&
1009 optname != MCAST_MSFILTER) {
1012 if (get_user(len, optlen))
1016 err = compat_nf_getsockopt(sk, PF_INET6,
1017 optname, optval, &len);
1020 err = put_user(len, optlen);
1026 EXPORT_SYMBOL(compat_ipv6_getsockopt);
1029 void __init ipv6_packet_init(void)
1031 dev_add_pack(&ipv6_packet_type);
1034 void ipv6_packet_cleanup(void)
1036 dev_remove_pack(&ipv6_packet_type);