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.31 2000/01/16 05:11:38 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>
42 #include <net/ndisc.h>
43 #include <net/protocol.h>
44 #include <net/transp_v6.h>
45 #include <net/ip6_route.h>
46 #include <net/addrconf.h>
47 #include <net/inet_common.h>
51 #include <asm/uaccess.h>
53 struct ipv6_mib ipv6_statistics
[NR_CPUS
*2];
55 struct packet_type ipv6_packet_type
=
57 __constant_htons(ETH_P_IPV6
),
58 NULL
, /* All devices */
65 * addrconf module should be notifyed of a device going up
67 static struct notifier_block ipv6_dev_notf
= {
73 struct ip6_ra_chain
*ip6_ra_chain
;
74 rwlock_t ip6_ra_lock
= RW_LOCK_UNLOCKED
;
76 int ip6_ra_control(struct sock
*sk
, int sel
, void (*destructor
)(struct sock
*))
78 struct ip6_ra_chain
*ra
, *new_ra
, **rap
;
80 /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
81 if (sk
->type
!= SOCK_RAW
|| sk
->num
!= IPPROTO_RAW
)
84 new_ra
= (sel
>=0) ? kmalloc(sizeof(*new_ra
), GFP_KERNEL
) : NULL
;
86 write_lock_bh(&ip6_ra_lock
);
87 for (rap
= &ip6_ra_chain
; (ra
=*rap
) != NULL
; rap
= &ra
->next
) {
90 write_unlock_bh(&ip6_ra_lock
);
97 write_unlock_bh(&ip6_ra_lock
);
106 if (new_ra
== NULL
) {
107 write_unlock_bh(&ip6_ra_lock
);
112 new_ra
->destructor
= destructor
;
116 write_unlock_bh(&ip6_ra_lock
);
121 int ipv6_setsockopt(struct sock
*sk
, int level
, int optname
, char *optval
,
124 struct ipv6_pinfo
*np
= &sk
->net_pinfo
.af_inet6
;
126 int retv
= -ENOPROTOOPT
;
128 if(level
==SOL_IP
&& sk
->type
!= SOCK_RAW
)
129 return udp_prot
.setsockopt(sk
, level
, optname
, optval
, optlen
);
136 else if (get_user(val
, (int *) optval
))
146 if (val
== PF_INET
) {
147 struct ipv6_txoptions
*opt
;
148 struct sk_buff
*pktopt
;
150 if (sk
->protocol
!= IPPROTO_UDP
&&
151 sk
->protocol
!= IPPROTO_TCP
)
154 if (sk
->state
!= TCP_ESTABLISHED
) {
159 if (!(ipv6_addr_type(&np
->daddr
) & IPV6_ADDR_MAPPED
)) {
160 retv
= -EADDRNOTAVAIL
;
164 fl6_free_socklist(sk
);
165 ipv6_sock_mc_close(sk
);
167 if (sk
->protocol
== IPPROTO_TCP
) {
168 struct tcp_opt
*tp
= &(sk
->tp_pinfo
.af_tcp
);
171 sock_prot_dec_use(sk
->prot
);
172 sock_prot_inc_use(&tcp_prot
);
174 sk
->prot
= &tcp_prot
;
175 tp
->af_specific
= &ipv4_specific
;
176 sk
->socket
->ops
= &inet_stream_ops
;
177 sk
->family
= PF_INET
;
178 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
181 sock_prot_dec_use(sk
->prot
);
182 sock_prot_inc_use(&udp_prot
);
184 sk
->prot
= &udp_prot
;
185 sk
->socket
->ops
= &inet_dgram_ops
;
187 opt
= xchg(&np
->opt
, NULL
);
189 sock_kfree_s(sk
, opt
, opt
->tot_len
);
190 pktopt
= xchg(&np
->pktoptions
, NULL
);
194 sk
->destruct
= inet_sock_destruct
;
195 #ifdef INET_REFCNT_DEBUG
196 atomic_dec(&inet6_sock_nr
);
205 np
->rxopt
.bits
.rxinfo
= valbool
;
210 np
->rxopt
.bits
.rxhlim
= valbool
;
215 if (val
< 0 || val
> 2)
217 np
->rxopt
.bits
.srcrt
= val
;
222 np
->rxopt
.bits
.hopopts
= valbool
;
227 np
->rxopt
.bits
.authhdr
= valbool
;
232 np
->rxopt
.bits
.dstopts
= valbool
;
237 np
->rxopt
.bits
.rxflow
= valbool
;
241 case IPV6_PKTOPTIONS
:
243 struct ipv6_txoptions
*opt
= NULL
;
248 fl
.fl6_flowlabel
= 0;
249 fl
.oif
= sk
->bound_dev_if
;
254 opt
= sock_kmalloc(sk
, sizeof(*opt
) + optlen
, GFP_KERNEL
);
259 memset(opt
, 0, sizeof(*opt
));
260 opt
->tot_len
= sizeof(*opt
) + optlen
;
262 if (copy_from_user(opt
+1, optval
, optlen
))
265 msg
.msg_controllen
= optlen
;
266 msg
.msg_control
= (void*)(opt
+1);
268 retv
= datagram_send_ctl(&msg
, &fl
, opt
, &junk
);
273 if (sk
->type
== SOCK_STREAM
) {
275 struct tcp_opt
*tp
= &sk
->tp_pinfo
.af_tcp
;
276 if (!((1<<sk
->state
)&(TCPF_LISTEN
|TCPF_CLOSE
))
277 && sk
->daddr
!= LOOPBACK4_IPV6
) {
278 tp
->ext_header_len
= opt
->opt_flen
+ opt
->opt_nflen
;
279 tcp_sync_mss(sk
, tp
->pmtu_cookie
);
282 opt
= xchg(&np
->opt
, opt
);
285 write_lock(&sk
->dst_lock
);
286 opt
= xchg(&np
->opt
, opt
);
287 write_unlock(&sk
->dst_lock
);
293 sock_kfree_s(sk
, opt
, opt
->tot_len
);
296 case IPV6_UNICAST_HOPS
:
297 if (val
> 255 || val
< -1)
303 case IPV6_MULTICAST_HOPS
:
304 if (sk
->type
== SOCK_STREAM
)
306 if (val
> 255 || val
< -1)
308 np
->mcast_hops
= val
;
312 case IPV6_MULTICAST_LOOP
:
313 np
->mc_loop
= valbool
;
317 case IPV6_MULTICAST_IF
:
318 if (sk
->type
== SOCK_STREAM
)
320 if (sk
->bound_dev_if
&& sk
->bound_dev_if
!= val
)
323 if (__dev_get_by_index(val
) == NULL
) {
330 case IPV6_ADD_MEMBERSHIP
:
331 case IPV6_DROP_MEMBERSHIP
:
333 struct ipv6_mreq mreq
;
336 if (copy_from_user(&mreq
, optval
, sizeof(struct ipv6_mreq
)))
339 if (optname
== IPV6_ADD_MEMBERSHIP
)
340 retv
= ipv6_sock_mc_join(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
342 retv
= ipv6_sock_mc_drop(sk
, mreq
.ipv6mr_ifindex
, &mreq
.ipv6mr_multiaddr
);
345 case IPV6_ROUTER_ALERT
:
346 retv
= ip6_ra_control(sk
, val
, NULL
);
348 case IPV6_MTU_DISCOVER
:
355 if (val
&& val
< IPV6_MIN_MTU
)
361 np
->recverr
= valbool
;
363 skb_queue_purge(&sk
->error_queue
);
366 case IPV6_FLOWINFO_SEND
:
367 np
->sndflow
= valbool
;
370 case IPV6_FLOWLABEL_MGR
:
371 retv
= ipv6_flowlabel_opt(sk
, optval
, optlen
);
384 int ipv6_getsockopt(struct sock
*sk
, int level
, int optname
, char *optval
,
387 struct ipv6_pinfo
*np
= &sk
->net_pinfo
.af_inet6
;
391 if(level
==SOL_IP
&& sk
->type
!= SOCK_RAW
)
392 return udp_prot
.getsockopt(sk
, level
, optname
, optval
, optlen
);
395 if (get_user(len
, optlen
))
398 case IPV6_PKTOPTIONS
:
403 if (sk
->type
!= SOCK_STREAM
)
406 msg
.msg_control
= optval
;
407 msg
.msg_controllen
= len
;
411 skb
= np
->pktoptions
;
413 atomic_inc(&skb
->users
);
417 int err
= datagram_recv_ctl(sk
, &msg
, skb
);
422 if (np
->rxopt
.bits
.rxinfo
) {
423 struct in6_pktinfo src_info
;
424 src_info
.ipi6_ifindex
= np
->mcast_oif
;
425 ipv6_addr_copy(&src_info
.ipi6_addr
, &np
->daddr
);
426 put_cmsg(&msg
, SOL_IPV6
, IPV6_PKTINFO
, sizeof(src_info
), &src_info
);
428 if (np
->rxopt
.bits
.rxhlim
) {
429 int hlim
= np
->mcast_hops
;
430 put_cmsg(&msg
, SOL_IPV6
, IPV6_HOPLIMIT
, sizeof(hlim
), &hlim
);
433 len
-= msg
.msg_controllen
;
434 return put_user(len
, optlen
);
438 struct dst_entry
*dst
;
441 dst
= sk_dst_get(sk
);
454 len
=min(sizeof(int),len
);
455 if(put_user(len
, optlen
))
457 if(copy_to_user(optval
,&val
,len
))
462 #if defined(MODULE) && defined(CONFIG_SYSCTL)
465 * sysctl registration functions defined in sysctl_net_ipv6.c
468 extern void ipv6_sysctl_register(void);
469 extern void ipv6_sysctl_unregister(void);
472 void __init
ipv6_packet_init(void)
474 dev_add_pack(&ipv6_packet_type
);
477 void __init
ipv6_netdev_notif_init(void)
479 register_netdevice_notifier(&ipv6_dev_notf
);
483 void ipv6_packet_cleanup(void)
485 dev_remove_pack(&ipv6_packet_type
);
488 void ipv6_netdev_notif_cleanup(void)
490 unregister_netdevice_notifier(&ipv6_dev_notf
);