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 tcp_sock
*tp
= tcp_sk(sk
);
169 sock_prot_dec_use(sk
->sk_prot
);
170 sock_prot_inc_use(&tcp_prot
);
172 sk
->sk_prot
= &tcp_prot
;
173 tp
->af_specific
= &ipv4_specific
;
174 sk
->sk_socket
->ops
= &inet_stream_ops
;
175 sk
->sk_family
= PF_INET
;
176 tcp_sync_mss(sk
, tp
->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 (sk
->sk_type
== SOCK_STREAM
) {
322 struct tcp_sock
*tp
= tcp_sk(sk
);
323 if (!((1 << sk
->sk_state
) &
324 (TCPF_LISTEN
| TCPF_CLOSE
))
325 && inet_sk(sk
)->daddr
!= LOOPBACK4_IPV6
) {
326 tp
->ext_header_len
= opt
->opt_flen
+ opt
->opt_nflen
;
327 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
330 opt
= xchg(&np
->opt
, opt
);
333 write_lock(&sk
->sk_dst_lock
);
334 opt
= xchg(&np
->opt
, opt
);
335 write_unlock(&sk
->sk_dst_lock
);
340 sock_kfree_s(sk
, opt
, opt
->tot_len
);
344 case IPV6_2292PKTOPTIONS
:
346 struct ipv6_txoptions
*opt
= NULL
;
351 fl
.fl6_flowlabel
= 0;
352 fl
.oif
= sk
->sk_bound_dev_if
;
357 /* 1K is probably excessive
358 * 1K is surely not enough, 2K per standard header is 16K.
361 if (optlen
> 64*1024)
364 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
369 memset(opt
, 0, sizeof(*opt
));
370 opt
->tot_len
= sizeof(*opt
) + optlen
;
372 if (copy_from_user(opt
+1, optval
, optlen
))
375 msg
.msg_controllen
= optlen
;
376 msg
.msg_control
= (void*)(opt
+1);
378 retv
= datagram_send_ctl(&msg
, &fl
, opt
, &junk
, &junk
);
383 if (sk
->sk_type
== SOCK_STREAM
) {
385 struct tcp_sock
*tp
= tcp_sk(sk
);
386 if (!((1 << sk
->sk_state
) &
387 (TCPF_LISTEN
| TCPF_CLOSE
))
388 && inet_sk(sk
)->daddr
!= LOOPBACK4_IPV6
) {
389 tp
->ext_header_len
= opt
->opt_flen
+ opt
->opt_nflen
;
390 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
393 opt
= xchg(&np
->opt
, opt
);
396 write_lock(&sk
->sk_dst_lock
);
397 opt
= xchg(&np
->opt
, opt
);
398 write_unlock(&sk
->sk_dst_lock
);
404 sock_kfree_s(sk
, opt
, opt
->tot_len
);
407 case IPV6_UNICAST_HOPS
:
408 if (val
> 255 || val
< -1)
414 case IPV6_MULTICAST_HOPS
:
415 if (sk
->sk_type
== SOCK_STREAM
)
417 if (val
> 255 || val
< -1)
419 np
->mcast_hops
= val
;
423 case IPV6_MULTICAST_LOOP
:
424 np
->mc_loop
= valbool
;
428 case IPV6_MULTICAST_IF
:
429 if (sk
->sk_type
== SOCK_STREAM
)
431 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
434 if (__dev_get_by_index(val
) == NULL
) {
441 case IPV6_ADD_MEMBERSHIP
:
442 case IPV6_DROP_MEMBERSHIP
:
444 struct ipv6_mreq mreq
;
447 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
450 if (optname
== IPV6_ADD_MEMBERSHIP
)
451 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
453 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
456 case IPV6_JOIN_ANYCAST
:
457 case IPV6_LEAVE_ANYCAST
:
459 struct ipv6_mreq mreq
;
461 if (optlen
!= sizeof(struct ipv6_mreq
))
465 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
468 if (optname
== IPV6_JOIN_ANYCAST
)
469 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
471 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
474 case MCAST_JOIN_GROUP
:
475 case MCAST_LEAVE_GROUP
:
477 struct group_req greq
;
478 struct sockaddr_in6
*psin6
;
481 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
483 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
484 retv
= -EADDRNOTAVAIL
;
487 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
488 if (optname
== MCAST_JOIN_GROUP
)
489 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
492 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
496 case MCAST_JOIN_SOURCE_GROUP
:
497 case MCAST_LEAVE_SOURCE_GROUP
:
498 case MCAST_BLOCK_SOURCE
:
499 case MCAST_UNBLOCK_SOURCE
:
501 struct group_source_req greqs
;
504 if (optlen
!= sizeof(struct group_source_req
))
506 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
510 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
511 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
512 retv
= -EADDRNOTAVAIL
;
515 if (optname
== MCAST_BLOCK_SOURCE
) {
516 omode
= MCAST_EXCLUDE
;
518 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
519 omode
= MCAST_EXCLUDE
;
521 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
522 struct sockaddr_in6
*psin6
;
524 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
525 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
527 /* prior join w/ different source is ok */
528 if (retv
&& retv
!= -EADDRINUSE
)
530 omode
= MCAST_INCLUDE
;
532 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
533 omode
= MCAST_INCLUDE
;
536 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
541 extern int sysctl_mld_max_msf
;
542 struct group_filter
*gsf
;
544 if (optlen
< GROUP_FILTER_SIZE(0))
546 if (optlen
> sysctl_optmem_max
) {
550 gsf
= (struct group_filter
*)kmalloc(optlen
,GFP_KERNEL
);
556 if (copy_from_user(gsf
, optval
, optlen
)) {
560 /* numsrc >= (4G-140)/128 overflow in 32 bits */
561 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
562 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
567 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
572 retv
= ip6_mc_msfilter(sk
, gsf
);
577 case IPV6_ROUTER_ALERT
:
578 retv
= ip6_ra_control(sk
, val
, NULL
);
580 case IPV6_MTU_DISCOVER
:
587 if (val
&& val
< IPV6_MIN_MTU
)
593 np
->recverr
= valbool
;
595 skb_queue_purge(&sk
->sk_error_queue
);
598 case IPV6_FLOWINFO_SEND
:
599 np
->sndflow
= valbool
;
602 case IPV6_FLOWLABEL_MGR
:
603 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
605 case IPV6_IPSEC_POLICY
:
606 case IPV6_XFRM_POLICY
:
608 if (!capable(CAP_NET_ADMIN
))
610 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
613 #ifdef CONFIG_NETFILTER
615 retv
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
631 int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_opt_hdr
*hdr
,
632 char __user
*optval
, int len
)
636 len
= min_t(int, len
, ipv6_optlen(hdr
));
637 if (copy_to_user(optval
, hdr
, ipv6_optlen(hdr
)))
642 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
643 char __user
*optval
, int __user
*optlen
)
645 struct ipv6_pinfo
*np
= inet6_sk(sk
);
649 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
650 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
653 if (get_user(len
, optlen
))
657 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
658 sk
->sk_protocol
!= IPPROTO_TCP
)
660 if (sk
->sk_state
!= TCP_ESTABLISHED
)
666 struct group_filter gsf
;
669 if (len
< GROUP_FILTER_SIZE(0))
671 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
674 err
= ip6_mc_msfget(sk
, &gsf
,
675 (struct group_filter __user
*)optval
, optlen
);
680 case IPV6_2292PKTOPTIONS
:
685 if (sk
->sk_type
!= SOCK_STREAM
)
688 msg
.msg_control
= optval
;
689 msg
.msg_controllen
= len
;
693 skb
= np
->pktoptions
;
695 atomic_inc(&skb
->users
);
699 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
704 if (np
->rxopt
.bits
.rxinfo
) {
705 struct in6_pktinfo src_info
;
706 src_info
.ipi6_ifindex
= np
->mcast_oif
;
707 ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
);
708 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
710 if (np
->rxopt
.bits
.rxhlim
) {
711 int hlim
= np
->mcast_hops
;
712 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
714 if (np
->rxopt
.bits
.rxoinfo
) {
715 struct in6_pktinfo src_info
;
716 src_info
.ipi6_ifindex
= np
->mcast_oif
;
717 ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
);
718 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
720 if (np
->rxopt
.bits
.rxohlim
) {
721 int hlim
= np
->mcast_hops
;
722 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
725 len
-= msg
.msg_controllen
;
726 return put_user(len
, optlen
);
730 struct dst_entry
*dst
;
733 dst
= sk_dst_get(sk
);
748 case IPV6_RECVPKTINFO
:
749 val
= np
->rxopt
.bits
.rxinfo
;
752 case IPV6_2292PKTINFO
:
753 val
= np
->rxopt
.bits
.rxoinfo
;
756 case IPV6_RECVHOPLIMIT
:
757 val
= np
->rxopt
.bits
.rxhlim
;
760 case IPV6_2292HOPLIMIT
:
761 val
= np
->rxopt
.bits
.rxohlim
;
765 val
= np
->rxopt
.bits
.srcrt
;
769 val
= np
->rxopt
.bits
.osrcrt
;
773 case IPV6_RTHDRDSTOPTS
:
779 len
= ipv6_getsockopt_sticky(sk
, np
->opt
->hopopt
,
782 return put_user(len
, optlen
);
785 case IPV6_RECVHOPOPTS
:
786 val
= np
->rxopt
.bits
.hopopts
;
789 case IPV6_2292HOPOPTS
:
790 val
= np
->rxopt
.bits
.ohopopts
;
793 case IPV6_RECVDSTOPTS
:
794 val
= np
->rxopt
.bits
.dstopts
;
797 case IPV6_2292DSTOPTS
:
798 val
= np
->rxopt
.bits
.odstopts
;
805 case IPV6_RECVTCLASS
:
806 val
= np
->rxopt
.bits
.rxtclass
;
810 val
= np
->rxopt
.bits
.rxflow
;
813 case IPV6_UNICAST_HOPS
:
817 case IPV6_MULTICAST_HOPS
:
818 val
= np
->mcast_hops
;
821 case IPV6_MULTICAST_LOOP
:
825 case IPV6_MULTICAST_IF
:
829 case IPV6_MTU_DISCOVER
:
837 case IPV6_FLOWINFO_SEND
:
842 #ifdef CONFIG_NETFILTER
844 val
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
848 val
= put_user(len
, optlen
);
854 len
= min_t(unsigned int, sizeof(int), len
);
855 if(put_user(len
, optlen
))
857 if(copy_to_user(optval
,&val
,len
))
862 void __init
ipv6_packet_init(void)
864 dev_add_pack(&ipv6_packet_type
);
867 void ipv6_packet_cleanup(void)
869 dev_remove_pack(&ipv6_packet_type
);