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 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 (sk
->sk_type
== SOCK_STREAM
) {
323 struct tcp_sock
*tp
= tcp_sk(sk
);
324 if (!((1 << sk
->sk_state
) &
325 (TCPF_LISTEN
| TCPF_CLOSE
))
326 && inet_sk(sk
)->daddr
!= LOOPBACK4_IPV6
) {
327 tp
->ext_header_len
= opt
->opt_flen
+ opt
->opt_nflen
;
328 tcp_sync_mss(sk
, tp
->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 (sk
->sk_type
== SOCK_STREAM
) {
386 struct tcp_sock
*tp
= tcp_sk(sk
);
387 if (!((1 << sk
->sk_state
) &
388 (TCPF_LISTEN
| TCPF_CLOSE
))
389 && inet_sk(sk
)->daddr
!= LOOPBACK4_IPV6
) {
390 tp
->ext_header_len
= opt
->opt_flen
+ opt
->opt_nflen
;
391 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
394 opt
= xchg(&np
->opt
, opt
);
397 write_lock(&sk
->sk_dst_lock
);
398 opt
= xchg(&np
->opt
, opt
);
399 write_unlock(&sk
->sk_dst_lock
);
405 sock_kfree_s(sk
, opt
, opt
->tot_len
);
408 case IPV6_UNICAST_HOPS
:
409 if (val
> 255 || val
< -1)
415 case IPV6_MULTICAST_HOPS
:
416 if (sk
->sk_type
== SOCK_STREAM
)
418 if (val
> 255 || val
< -1)
420 np
->mcast_hops
= val
;
424 case IPV6_MULTICAST_LOOP
:
425 np
->mc_loop
= valbool
;
429 case IPV6_MULTICAST_IF
:
430 if (sk
->sk_type
== SOCK_STREAM
)
432 if (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= val
)
435 if (__dev_get_by_index(val
) == NULL
) {
442 case IPV6_ADD_MEMBERSHIP
:
443 case IPV6_DROP_MEMBERSHIP
:
445 struct ipv6_mreq mreq
;
448 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
451 if (optname
== IPV6_ADD_MEMBERSHIP
)
452 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
454 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
457 case IPV6_JOIN_ANYCAST
:
458 case IPV6_LEAVE_ANYCAST
:
460 struct ipv6_mreq mreq
;
462 if (optlen
!= sizeof(struct ipv6_mreq
))
466 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
469 if (optname
== IPV6_JOIN_ANYCAST
)
470 retv
= ipv6_sock_ac_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
472 retv
= ipv6_sock_ac_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_acaddr
);
475 case MCAST_JOIN_GROUP
:
476 case MCAST_LEAVE_GROUP
:
478 struct group_req greq
;
479 struct sockaddr_in6
*psin6
;
482 if (copy_from_user(&greq
, optval
, sizeof(struct group_req
)))
484 if (greq
.gr_group
.ss_family
!= AF_INET6
) {
485 retv
= -EADDRNOTAVAIL
;
488 psin6
= (struct sockaddr_in6
*)&greq
.gr_group
;
489 if (optname
== MCAST_JOIN_GROUP
)
490 retv
= ipv6_sock_mc_join(sk
, greq
.gr_interface
,
493 retv
= ipv6_sock_mc_drop(sk
, greq
.gr_interface
,
497 case MCAST_JOIN_SOURCE_GROUP
:
498 case MCAST_LEAVE_SOURCE_GROUP
:
499 case MCAST_BLOCK_SOURCE
:
500 case MCAST_UNBLOCK_SOURCE
:
502 struct group_source_req greqs
;
505 if (optlen
!= sizeof(struct group_source_req
))
507 if (copy_from_user(&greqs
, optval
, sizeof(greqs
))) {
511 if (greqs
.gsr_group
.ss_family
!= AF_INET6
||
512 greqs
.gsr_source
.ss_family
!= AF_INET6
) {
513 retv
= -EADDRNOTAVAIL
;
516 if (optname
== MCAST_BLOCK_SOURCE
) {
517 omode
= MCAST_EXCLUDE
;
519 } else if (optname
== MCAST_UNBLOCK_SOURCE
) {
520 omode
= MCAST_EXCLUDE
;
522 } else if (optname
== MCAST_JOIN_SOURCE_GROUP
) {
523 struct sockaddr_in6
*psin6
;
525 psin6
= (struct sockaddr_in6
*)&greqs
.gsr_group
;
526 retv
= ipv6_sock_mc_join(sk
, greqs
.gsr_interface
,
528 /* prior join w/ different source is ok */
529 if (retv
&& retv
!= -EADDRINUSE
)
531 omode
= MCAST_INCLUDE
;
533 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
534 omode
= MCAST_INCLUDE
;
537 retv
= ip6_mc_source(add
, omode
, sk
, &greqs
);
542 extern int sysctl_mld_max_msf
;
543 struct group_filter
*gsf
;
545 if (optlen
< GROUP_FILTER_SIZE(0))
547 if (optlen
> sysctl_optmem_max
) {
551 gsf
= (struct group_filter
*)kmalloc(optlen
,GFP_KERNEL
);
557 if (copy_from_user(gsf
, optval
, optlen
)) {
561 /* numsrc >= (4G-140)/128 overflow in 32 bits */
562 if (gsf
->gf_numsrc
>= 0x1ffffffU
||
563 gsf
->gf_numsrc
> sysctl_mld_max_msf
) {
568 if (GROUP_FILTER_SIZE(gsf
->gf_numsrc
) > optlen
) {
573 retv
= ip6_mc_msfilter(sk
, gsf
);
578 case IPV6_ROUTER_ALERT
:
579 retv
= ip6_ra_control(sk
, val
, NULL
);
581 case IPV6_MTU_DISCOVER
:
588 if (val
&& val
< IPV6_MIN_MTU
)
594 np
->recverr
= valbool
;
596 skb_queue_purge(&sk
->sk_error_queue
);
599 case IPV6_FLOWINFO_SEND
:
600 np
->sndflow
= valbool
;
603 case IPV6_FLOWLABEL_MGR
:
604 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
606 case IPV6_IPSEC_POLICY
:
607 case IPV6_XFRM_POLICY
:
609 if (!capable(CAP_NET_ADMIN
))
611 retv
= xfrm_user_policy(sk
, optname
, optval
, optlen
);
614 #ifdef CONFIG_NETFILTER
616 retv
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
632 int ipv6_getsockopt_sticky(struct sock
*sk
, struct ipv6_opt_hdr
*hdr
,
633 char __user
*optval
, int len
)
637 len
= min_t(int, len
, ipv6_optlen(hdr
));
638 if (copy_to_user(optval
, hdr
, ipv6_optlen(hdr
)))
643 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
,
644 char __user
*optval
, int __user
*optlen
)
646 struct ipv6_pinfo
*np
= inet6_sk(sk
);
650 if (level
== SOL_IP
&& sk
->sk_type
!= SOCK_RAW
)
651 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
654 if (get_user(len
, optlen
))
658 if (sk
->sk_protocol
!= IPPROTO_UDP
&&
659 sk
->sk_protocol
!= IPPROTO_TCP
)
661 if (sk
->sk_state
!= TCP_ESTABLISHED
)
667 struct group_filter gsf
;
670 if (len
< GROUP_FILTER_SIZE(0))
672 if (copy_from_user(&gsf
, optval
, GROUP_FILTER_SIZE(0)))
675 err
= ip6_mc_msfget(sk
, &gsf
,
676 (struct group_filter __user
*)optval
, optlen
);
681 case IPV6_2292PKTOPTIONS
:
686 if (sk
->sk_type
!= SOCK_STREAM
)
689 msg
.msg_control
= optval
;
690 msg
.msg_controllen
= len
;
694 skb
= np
->pktoptions
;
696 atomic_inc(&skb
->users
);
700 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
705 if (np
->rxopt
.bits
.rxinfo
) {
706 struct in6_pktinfo src_info
;
707 src_info
.ipi6_ifindex
= np
->mcast_oif
;
708 ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
);
709 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
711 if (np
->rxopt
.bits
.rxhlim
) {
712 int hlim
= np
->mcast_hops
;
713 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
715 if (np
->rxopt
.bits
.rxoinfo
) {
716 struct in6_pktinfo src_info
;
717 src_info
.ipi6_ifindex
= np
->mcast_oif
;
718 ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
);
719 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292PKTINFO
, sizeof(src_info
), &src_info
);
721 if (np
->rxopt
.bits
.rxohlim
) {
722 int hlim
= np
->mcast_hops
;
723 put_cmsg(&msg
, SOL_IPV6
, IPV6_2292HOPLIMIT
, sizeof(hlim
), &hlim
);
726 len
-= msg
.msg_controllen
;
727 return put_user(len
, optlen
);
731 struct dst_entry
*dst
;
734 dst
= sk_dst_get(sk
);
749 case IPV6_RECVPKTINFO
:
750 val
= np
->rxopt
.bits
.rxinfo
;
753 case IPV6_2292PKTINFO
:
754 val
= np
->rxopt
.bits
.rxoinfo
;
757 case IPV6_RECVHOPLIMIT
:
758 val
= np
->rxopt
.bits
.rxhlim
;
761 case IPV6_2292HOPLIMIT
:
762 val
= np
->rxopt
.bits
.rxohlim
;
766 val
= np
->rxopt
.bits
.srcrt
;
770 val
= np
->rxopt
.bits
.osrcrt
;
774 case IPV6_RTHDRDSTOPTS
:
780 len
= ipv6_getsockopt_sticky(sk
, np
->opt
->hopopt
,
783 return put_user(len
, optlen
);
786 case IPV6_RECVHOPOPTS
:
787 val
= np
->rxopt
.bits
.hopopts
;
790 case IPV6_2292HOPOPTS
:
791 val
= np
->rxopt
.bits
.ohopopts
;
794 case IPV6_RECVDSTOPTS
:
795 val
= np
->rxopt
.bits
.dstopts
;
798 case IPV6_2292DSTOPTS
:
799 val
= np
->rxopt
.bits
.odstopts
;
806 case IPV6_RECVTCLASS
:
807 val
= np
->rxopt
.bits
.rxtclass
;
811 val
= np
->rxopt
.bits
.rxflow
;
814 case IPV6_UNICAST_HOPS
:
818 case IPV6_MULTICAST_HOPS
:
819 val
= np
->mcast_hops
;
822 case IPV6_MULTICAST_LOOP
:
826 case IPV6_MULTICAST_IF
:
830 case IPV6_MTU_DISCOVER
:
838 case IPV6_FLOWINFO_SEND
:
843 #ifdef CONFIG_NETFILTER
845 val
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
849 val
= put_user(len
, optlen
);
855 len
= min_t(unsigned int, sizeof(int), len
);
856 if(put_user(len
, optlen
))
858 if(copy_to_user(optval
,&val
,len
))
863 void __init
ipv6_packet_init(void)
865 dev_add_pack(&ipv6_packet_type
);
868 void ipv6_packet_cleanup(void)
870 dev_remove_pack(&ipv6_packet_type
);