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.33 2000/02/27 19:42:54 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 #define __NO_VERSION__
25 #include <linux/module.h>
26 #include <linux/config.h>
27 #include <linux/errno.h>
28 #include <linux/types.h>
29 #include <linux/socket.h>
30 #include <linux/sockios.h>
31 #include <linux/sched.h>
32 #include <linux/net.h>
33 #include <linux/in6.h>
34 #include <linux/netdevice.h>
35 #include <linux/if_arp.h>
36 #include <linux/init.h>
37 #include <linux/sysctl.h>
38 #include <linux/netfilter.h>
43 #include <net/ndisc.h>
44 #include <net/protocol.h>
45 #include <net/transp_v6.h>
46 #include <net/ip6_route.h>
47 #include <net/addrconf.h>
48 #include <net/inet_common.h>
52 #include <asm/uaccess.h>
54 struct ipv6_mib ipv6_statistics
[NR_CPUS
*2];
56 struct packet_type ipv6_packet_type
=
58 __constant_htons(ETH_P_IPV6
),
59 NULL
, /* All devices */
66 * addrconf module should be notifyed of a device going up
68 static struct notifier_block ipv6_dev_notf
= {
74 struct ip6_ra_chain
*ip6_ra_chain
;
75 rwlock_t ip6_ra_lock
= RW_LOCK_UNLOCKED
;
77 int ip6_ra_control(struct sock
*sk
, int sel
, void (*destructor
)(struct sock
*))
79 struct ip6_ra_chain
*ra
, *new_ra
, **rap
;
81 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
82 if (sk
->type
!= SOCK_RAW
|| sk
->num
!= IPPROTO_RAW
)
85 new_ra
= (sel
>=0) ? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
87 write_lock_bh(&ip6_ra_lock
);
88 for (rap
= &ip6_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
91 write_unlock_bh(&ip6_ra_lock
);
98 write_unlock_bh(&ip6_ra_lock
);
107 if (new_ra
== NULL
) {
108 write_unlock_bh(&ip6_ra_lock
);
113 new_ra
->destructor
= destructor
;
117 write_unlock_bh(&ip6_ra_lock
);
122 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
, char *optval
,
125 struct ipv6_pinfo
*np
= &sk
->net_pinfo
.af_inet6
;
127 int retv
= -ENOPROTOOPT
;
129 if(level
==SOL_IP
&& sk
->type
!= SOCK_RAW
)
130 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
137 else if (get_user(val
, (int *) optval
))
147 if (val
== PF_INET
) {
148 struct ipv6_txoptions
*opt
;
149 struct sk_buff
*pktopt
;
151 if (sk
->protocol
!= IPPROTO_UDP
&&
152 sk
->protocol
!= IPPROTO_TCP
)
155 if (sk
->state
!= TCP_ESTABLISHED
) {
160 if (!(ipv6_addr_type(&np
->daddr
) & IPV6_ADDR_MAPPED
)) {
161 retv
= -EADDRNOTAVAIL
;
165 fl6_free_socklist(sk
);
166 ipv6_sock_mc_close(sk
);
168 if (sk
->protocol
== IPPROTO_TCP
) {
169 struct tcp_opt
*tp
= &(sk
->tp_pinfo
.af_tcp
);
172 sock_prot_dec_use(sk
->prot
);
173 sock_prot_inc_use(&tcp_prot
);
175 sk
->prot
= &tcp_prot
;
176 tp
->af_specific
= &ipv4_specific
;
177 sk
->socket
->ops
= &inet_stream_ops
;
178 sk
->family
= PF_INET
;
179 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
182 sock_prot_dec_use(sk
->prot
);
183 sock_prot_inc_use(&udp_prot
);
185 sk
->prot
= &udp_prot
;
186 sk
->socket
->ops
= &inet_dgram_ops
;
187 sk
->family
= PF_INET
;
189 opt
= xchg(&np
->opt
, NULL
);
191 sock_kfree_s(sk
, opt
, opt
->tot_len
);
192 pktopt
= xchg(&np
->pktoptions
, NULL
);
196 sk
->destruct
= inet_sock_destruct
;
197 #ifdef INET_REFCNT_DEBUG
198 atomic_dec(&inet6_sock_nr
);
207 np
->rxopt
.bits
.rxinfo
= valbool
;
212 np
->rxopt
.bits
.rxhlim
= valbool
;
217 if (val
< 0 || val
> 2)
219 np
->rxopt
.bits
.srcrt
= val
;
224 np
->rxopt
.bits
.hopopts
= valbool
;
229 np
->rxopt
.bits
.authhdr
= valbool
;
234 np
->rxopt
.bits
.dstopts
= valbool
;
239 np
->rxopt
.bits
.rxflow
= valbool
;
243 case IPV6_PKTOPTIONS
:
245 struct ipv6_txoptions
*opt
= NULL
;
250 fl
.fl6_flowlabel
= 0;
251 fl
.oif
= sk
->bound_dev_if
;
256 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
261 memset(opt
, 0, sizeof(*opt
));
262 opt
->tot_len
= sizeof(*opt
) + optlen
;
264 if (copy_from_user(opt
+1, optval
, optlen
))
267 msg
.msg_controllen
= optlen
;
268 msg
.msg_control
= (void*)(opt
+1);
270 retv
= datagram_send_ctl(&msg
, &fl
, opt
, &junk
);
275 if (sk
->type
== SOCK_STREAM
) {
277 struct tcp_opt
*tp
= &sk
->tp_pinfo
.af_tcp
;
278 if (!((1<<sk
->state
)&(TCPF_LISTEN
|TCPF_CLOSE
))
279 && sk
->daddr
!= LOOPBACK4_IPV6
) {
280 tp
->ext_header_len
= opt
->opt_flen
+ opt
->opt_nflen
;
281 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
284 opt
= xchg(&np
->opt
, opt
);
287 write_lock(&sk
->dst_lock
);
288 opt
= xchg(&np
->opt
, opt
);
289 write_unlock(&sk
->dst_lock
);
295 sock_kfree_s(sk
, opt
, opt
->tot_len
);
298 case IPV6_UNICAST_HOPS
:
299 if (val
> 255 || val
< -1)
305 case IPV6_MULTICAST_HOPS
:
306 if (sk
->type
== SOCK_STREAM
)
308 if (val
> 255 || val
< -1)
310 np
->mcast_hops
= val
;
314 case IPV6_MULTICAST_LOOP
:
315 np
->mc_loop
= valbool
;
319 case IPV6_MULTICAST_IF
:
320 if (sk
->type
== SOCK_STREAM
)
322 if (sk
->bound_dev_if
&& sk
->bound_dev_if
!= val
)
325 if (__dev_get_by_index(val
) == NULL
) {
332 case IPV6_ADD_MEMBERSHIP
:
333 case IPV6_DROP_MEMBERSHIP
:
335 struct ipv6_mreq mreq
;
338 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
341 if (optname
== IPV6_ADD_MEMBERSHIP
)
342 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
344 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
347 case IPV6_ROUTER_ALERT
:
348 retv
= ip6_ra_control(sk
, val
, NULL
);
350 case IPV6_MTU_DISCOVER
:
357 if (val
&& val
< IPV6_MIN_MTU
)
363 np
->recverr
= valbool
;
365 skb_queue_purge(&sk
->error_queue
);
368 case IPV6_FLOWINFO_SEND
:
369 np
->sndflow
= valbool
;
372 case IPV6_FLOWLABEL_MGR
:
373 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
376 #ifdef CONFIG_NETFILTER
378 retv
= nf_setsockopt(sk
, PF_INET6
, optname
, optval
,
394 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
, char *optval
,
397 struct ipv6_pinfo
*np
= &sk
->net_pinfo
.af_inet6
;
401 if(level
==SOL_IP
&& sk
->type
!= SOCK_RAW
)
402 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
405 if (get_user(len
, optlen
))
408 case IPV6_PKTOPTIONS
:
413 if (sk
->type
!= SOCK_STREAM
)
416 msg
.msg_control
= optval
;
417 msg
.msg_controllen
= len
;
421 skb
= np
->pktoptions
;
423 atomic_inc(&skb
->users
);
427 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
432 if (np
->rxopt
.bits
.rxinfo
) {
433 struct in6_pktinfo src_info
;
434 src_info
.ipi6_ifindex
= np
->mcast_oif
;
435 ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
);
436 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
438 if (np
->rxopt
.bits
.rxhlim
) {
439 int hlim
= np
->mcast_hops
;
440 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
443 len
-= msg
.msg_controllen
;
444 return put_user(len
, optlen
);
448 struct dst_entry
*dst
;
451 dst
= sk_dst_get(sk
);
462 #ifdef CONFIG_NETFILTER
464 val
= nf_getsockopt(sk
, PF_INET6
, optname
, optval
,
468 val
= put_user(len
, optlen
);
474 len
=min(sizeof(int),len
);
475 if(put_user(len
, optlen
))
477 if(copy_to_user(optval
,&val
,len
))
482 #if defined(MODULE) && defined(CONFIG_SYSCTL)
485 * sysctl registration functions defined in sysctl_net_ipv6.c
488 extern void ipv6_sysctl_register(void);
489 extern void ipv6_sysctl_unregister(void);
492 void __init
ipv6_packet_init(void)
494 dev_add_pack(&ipv6_packet_type
);
497 void __init
ipv6_netdev_notif_init(void)
499 register_netdevice_notifier(&ipv6_dev_notf
);
503 void ipv6_packet_cleanup(void)
505 dev_remove_pack(&ipv6_packet_type
);
508 void ipv6_netdev_notif_cleanup(void)
510 unregister_netdevice_notifier(&ipv6_dev_notf
);