- Replace 0 with NULL when pointers are tested.
[dragonfly.git] / sys / netinet6 / in6_proto.c
blob75e4cfd71f23f209b41860d1d177ece63ef196aa
1 /* $FreeBSD: src/sys/netinet6/in6_proto.c,v 1.6.2.9 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6_proto.c,v 1.10 2006/10/24 06:18:42 hsu Exp $ */
3 /* $KAME: in6_proto.c,v 1.91 2001/05/27 13:28:35 itojun Exp $ */
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
35 * Copyright (c) 1982, 1986, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
66 * @(#)in_proto.c 8.1 (Berkeley) 6/10/93
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
73 #include <sys/param.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/protosw.h>
77 #include <sys/kernel.h>
78 #include <sys/domain.h>
79 #include <sys/mbuf.h>
80 #include <sys/systm.h>
81 #include <sys/sysctl.h>
83 #include <net/if.h>
84 #include <net/radix.h>
85 #include <net/route.h>
87 #include <netinet/in.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip_encap.h>
91 #include <netinet/ip.h>
92 #include <netinet/ip_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet/icmp6.h>
97 #include <netinet/tcp.h>
98 #include <netinet/tcp_timer.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/udp.h>
101 #include <netinet/udp_var.h>
102 #include <netinet6/tcp6_var.h>
103 #include <netinet6/raw_ip6.h>
104 #include <netinet6/udp6_var.h>
105 #include <netinet6/pim6_var.h>
106 #include <netinet6/nd6.h>
107 #include <netinet6/in6_prefix.h>
109 #ifdef IPSEC
110 #include <netinet6/ipsec.h>
111 #ifdef INET6
112 #include <netinet6/ipsec6.h>
113 #endif
114 #include <netinet6/ah.h>
115 #ifdef INET6
116 #include <netinet6/ah6.h>
117 #endif
118 #ifdef IPSEC_ESP
119 #include <netinet6/esp.h>
120 #ifdef INET6
121 #include <netinet6/esp6.h>
122 #endif
123 #endif
124 #include <netinet6/ipcomp.h>
125 #ifdef INET6
126 #include <netinet6/ipcomp6.h>
127 #endif
128 #endif /* IPSEC */
130 #ifdef FAST_IPSEC
131 #include <netproto/ipsec/ipsec6.h>
132 #define IPSEC
133 #define IPSEC_ESP
134 #define ah6_input ipsec6_common_input
135 #define esp6_input ipsec6_common_input
136 #define ipcomp6_input ipsec6_common_input
137 #endif /* FAST_IPSEC */
139 #include <netinet6/ip6protosw.h>
141 #include <net/net_osdep.h>
144 * TCP/IP protocol family: IP6, ICMP6, UDP, TCP.
147 extern struct domain inet6domain;
148 static struct pr_usrreqs nousrreqs;
150 #define PR_LISTEN 0
151 #define PR_ABRTACPTDIS 0
153 struct ip6protosw inet6sw[] = {
154 { 0, &inet6domain, IPPROTO_IPV6, 0,
155 0, 0, 0, 0,
156 cpu0_soport,
157 ip6_init, 0, frag6_slowtimo, frag6_drain,
158 &nousrreqs,
160 { SOCK_DGRAM, &inet6domain, IPPROTO_UDP, PR_ATOMIC|PR_ADDR,
161 udp6_input, 0, udp6_ctlinput, ip6_ctloutput,
162 cpu0_soport,
163 0, 0, 0, 0,
164 &udp6_usrreqs,
166 { SOCK_STREAM, &inet6domain, IPPROTO_TCP, PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN,
167 tcp6_input, 0, tcp6_ctlinput, tcp_ctloutput,
168 cpu0_soport,
169 #ifdef INET /* don't call initialization and timeout routines twice */
170 0, 0, 0, tcp_drain,
171 #else
172 tcp_init, tcp_fasttimo, tcp_slowtimo, tcp_drain,
173 #endif
174 &tcp6_usrreqs,
176 { SOCK_RAW, &inet6domain, IPPROTO_RAW, PR_ATOMIC|PR_ADDR,
177 rip6_input, rip6_output, rip6_ctlinput, rip6_ctloutput,
178 cpu0_soport,
179 0, 0, 0, 0,
180 &rip6_usrreqs
182 { SOCK_RAW, &inet6domain, IPPROTO_ICMPV6, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
183 icmp6_input, rip6_output, rip6_ctlinput, rip6_ctloutput,
184 cpu0_soport,
185 icmp6_init, icmp6_fasttimo, 0, 0,
186 &rip6_usrreqs
188 { SOCK_RAW, &inet6domain, IPPROTO_DSTOPTS, PR_ATOMIC|PR_ADDR,
189 dest6_input, 0, 0, 0,
190 cpu0_soport,
191 0, 0, 0, 0,
192 &nousrreqs
194 { SOCK_RAW, &inet6domain, IPPROTO_ROUTING, PR_ATOMIC|PR_ADDR,
195 route6_input, 0, 0, 0,
196 cpu0_soport,
197 0, 0, 0, 0,
198 &nousrreqs
200 { SOCK_RAW, &inet6domain, IPPROTO_FRAGMENT, PR_ATOMIC|PR_ADDR,
201 frag6_input, 0, 0, 0,
202 cpu0_soport,
203 0, 0, 0, 0,
204 &nousrreqs
206 #ifdef IPSEC
207 { SOCK_RAW, &inet6domain, IPPROTO_AH, PR_ATOMIC|PR_ADDR,
208 ah6_input, 0, 0, 0,
209 cpu0_soport,
210 0, 0, 0, 0,
211 &nousrreqs,
213 #ifdef IPSEC_ESP
214 { SOCK_RAW, &inet6domain, IPPROTO_ESP, PR_ATOMIC|PR_ADDR,
215 esp6_input, 0,
216 esp6_ctlinput,
218 cpu0_soport,
219 0, 0, 0, 0,
220 &nousrreqs,
222 #endif
223 { SOCK_RAW, &inet6domain, IPPROTO_IPCOMP, PR_ATOMIC|PR_ADDR,
224 ipcomp6_input, 0, 0, 0,
225 cpu0_soport,
226 0, 0, 0, 0,
227 &nousrreqs,
229 #endif /* IPSEC */
230 #ifdef INET
231 { SOCK_RAW, &inet6domain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
232 encap6_input, rip6_output, 0, rip6_ctloutput,
233 cpu0_soport,
234 encap_init, 0, 0, 0,
235 &rip6_usrreqs
237 #endif /* INET */
238 { SOCK_RAW, &inet6domain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
239 encap6_input, rip6_output, 0, rip6_ctloutput,
240 cpu0_soport,
241 encap_init, 0, 0, 0,
242 &rip6_usrreqs
244 { SOCK_RAW, &inet6domain, IPPROTO_PIM, PR_ATOMIC|PR_ADDR|PR_LASTHDR,
245 pim6_input, rip6_output, 0, rip6_ctloutput,
246 cpu0_soport,
247 0, 0, 0, 0,
248 &rip6_usrreqs
250 /* raw wildcard */
251 { SOCK_RAW, &inet6domain, 0, PR_ATOMIC|PR_ADDR,
252 rip6_input, rip6_output, 0, rip6_ctloutput,
253 cpu0_soport,
254 0, 0, 0, 0,
255 &rip6_usrreqs
259 extern int in6_inithead (void **, int);
261 struct domain inet6domain = {
262 AF_INET6, "internet6", NULL, NULL, NULL,
263 (struct protosw *)inet6sw,
264 (struct protosw *)&inet6sw[sizeof(inet6sw)/sizeof(inet6sw[0])],
265 SLIST_ENTRY_INITIALIZER,
266 in6_inithead, offsetof(struct sockaddr_in6, sin6_addr) << 3,
267 sizeof(struct sockaddr_in6), in6_domifattach, in6_domifdetach
270 DOMAIN_SET(inet6);
273 * Internet configuration info
275 #ifndef IPV6FORWARDING
276 #ifdef GATEWAY6
277 #define IPV6FORWARDING 1 /* forward IP6 packets not for us */
278 #else
279 #define IPV6FORWARDING 0 /* don't forward IP6 packets not for us */
280 #endif /* GATEWAY6 */
281 #endif /* !IPV6FORWARDING */
283 #ifndef IPV6_SENDREDIRECTS
284 #define IPV6_SENDREDIRECTS 1
285 #endif
287 int ip6_forwarding = IPV6FORWARDING; /* act as router? */
288 int ip6_sendredirects = IPV6_SENDREDIRECTS;
289 int ip6_defhlim = IPV6_DEFHLIM;
290 int ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
291 int ip6_accept_rtadv = 0; /* "IPV6FORWARDING ? 0 : 1" is dangerous */
292 int ip6_maxfragpackets; /* initialized in frag6.c:frag6_init() */
293 int ip6_log_interval = 5;
294 int ip6_hdrnestlimit = 50; /* appropriate? */
295 int ip6_dad_count = 1; /* DupAddrDetectionTransmits */
296 u_int32_t ip6_flow_seq;
297 int ip6_auto_flowlabel = 1;
298 int ip6_gif_hlim = 0;
299 int ip6_use_deprecated = 1; /* allow deprecated addr (RFC2462 5.5.4) */
300 int ip6_rr_prune = 5; /* router renumbering prefix
301 * walk list every 5 sec. */
302 int ip6_v6only = 1;
304 u_int32_t ip6_id = 0UL;
305 int ip6_keepfaith = 0;
306 time_t ip6_log_time = (time_t)0L;
308 /* icmp6 */
310 * BSDI4 defines these variables in in_proto.c...
311 * XXX: what if we don't define INET? Should we define pmtu6_expire
312 * or so? (jinmei@kame.net 19990310)
314 int pmtu_expire = 60*10;
315 int pmtu_probe = 60*2;
317 /* raw IP6 parameters */
319 * Nominal space allocated to a raw ip socket.
321 #define RIPV6SNDQ 8192
322 #define RIPV6RCVQ 8192
324 u_long rip6_sendspace = RIPV6SNDQ;
325 u_long rip6_recvspace = RIPV6RCVQ;
327 /* ICMPV6 parameters */
328 int icmp6_rediraccept = 1; /* accept and process redirects */
329 int icmp6_redirtimeout = 10 * 60; /* 10 minutes */
330 int icmp6errppslim = 100; /* 100pps */
331 int icmp6_nodeinfo = 3; /* enable/disable NI response */
333 /* UDP on IP6 parameters */
334 int udp6_sendspace = 9216; /* really max datagram size */
335 int udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
336 /* 40 1K datagrams */
339 * sysctl related items.
341 SYSCTL_NODE(_net, PF_INET6, inet6, CTLFLAG_RW, 0,
342 "Internet6 Family");
344 /* net.inet6 */
345 SYSCTL_NODE(_net_inet6, IPPROTO_IPV6, ip6, CTLFLAG_RW, 0, "IP6");
346 SYSCTL_NODE(_net_inet6, IPPROTO_ICMPV6, icmp6, CTLFLAG_RW, 0, "ICMP6");
347 SYSCTL_NODE(_net_inet6, IPPROTO_UDP, udp6, CTLFLAG_RW, 0, "UDP6");
348 SYSCTL_NODE(_net_inet6, IPPROTO_TCP, tcp6, CTLFLAG_RW, 0, "TCP6");
349 #ifdef IPSEC
350 SYSCTL_NODE(_net_inet6, IPPROTO_ESP, ipsec6, CTLFLAG_RW, 0, "IPSEC6");
351 #endif /* IPSEC */
353 /* net.inet6.ip6 */
354 static int
355 sysctl_ip6_temppltime(SYSCTL_HANDLER_ARGS)
357 int error = 0;
358 int old;
360 error = SYSCTL_OUT(req, arg1, sizeof(int));
361 if (error || !req->newptr)
362 return (error);
363 old = ip6_temp_preferred_lifetime;
364 error = SYSCTL_IN(req, arg1, sizeof(int));
365 if (ip6_temp_preferred_lifetime <
366 ip6_desync_factor + ip6_temp_regen_advance) {
367 ip6_temp_preferred_lifetime = old;
368 return (EINVAL);
370 return (error);
373 static int
374 sysctl_ip6_tempvltime(SYSCTL_HANDLER_ARGS)
376 int error = 0;
377 int old;
379 error = SYSCTL_OUT(req, arg1, sizeof(int));
380 if (error || !req->newptr)
381 return (error);
382 old = ip6_temp_valid_lifetime;
383 error = SYSCTL_IN(req, arg1, sizeof(int));
384 if (ip6_temp_valid_lifetime < ip6_temp_preferred_lifetime) {
385 ip6_temp_preferred_lifetime = old;
386 return (EINVAL);
388 return (error);
391 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_FORWARDING,
392 forwarding, CTLFLAG_RW, &ip6_forwarding, 0, "");
393 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_SENDREDIRECTS,
394 redirect, CTLFLAG_RW, &ip6_sendredirects, 0, "");
395 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_DEFHLIM,
396 hlim, CTLFLAG_RW, &ip6_defhlim, 0, "");
397 SYSCTL_STRUCT(_net_inet6_ip6, IPV6CTL_STATS, stats, CTLFLAG_RD,
398 &ip6stat, ip6stat, "");
399 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS,
400 maxfragpackets, CTLFLAG_RW, &ip6_maxfragpackets, 0, "");
401 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_ACCEPT_RTADV,
402 accept_rtadv, CTLFLAG_RW, &ip6_accept_rtadv, 0, "");
403 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_KEEPFAITH,
404 keepfaith, CTLFLAG_RW, &ip6_keepfaith, 0, "");
405 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_LOG_INTERVAL,
406 log_interval, CTLFLAG_RW, &ip6_log_interval, 0, "");
407 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_HDRNESTLIMIT,
408 hdrnestlimit, CTLFLAG_RW, &ip6_hdrnestlimit, 0, "");
409 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_DAD_COUNT,
410 dad_count, CTLFLAG_RW, &ip6_dad_count, 0, "");
411 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_AUTO_FLOWLABEL,
412 auto_flowlabel, CTLFLAG_RW, &ip6_auto_flowlabel, 0, "");
413 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_DEFMCASTHLIM,
414 defmcasthlim, CTLFLAG_RW, &ip6_defmcasthlim, 0, "");
415 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM,
416 gifhlim, CTLFLAG_RW, &ip6_gif_hlim, 0, "");
417 SYSCTL_STRING(_net_inet6_ip6, IPV6CTL_KAME_VERSION,
418 kame_version, CTLFLAG_RD, __KAME_VERSION, 0, "");
419 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_USE_DEPRECATED,
420 use_deprecated, CTLFLAG_RW, &ip6_use_deprecated, 0, "");
421 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_RR_PRUNE,
422 rr_prune, CTLFLAG_RW, &ip6_rr_prune, 0, "");
423 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_USETEMPADDR,
424 use_tempaddr, CTLFLAG_RW, &ip6_use_tempaddr, 0, "");
425 SYSCTL_OID(_net_inet6_ip6, IPV6CTL_TEMPPLTIME, temppltime,
426 CTLTYPE_INT|CTLFLAG_RW, &ip6_temp_preferred_lifetime, 0,
427 sysctl_ip6_temppltime, "I", "");
428 SYSCTL_OID(_net_inet6_ip6, IPV6CTL_TEMPVLTIME, tempvltime,
429 CTLTYPE_INT|CTLFLAG_RW, &ip6_temp_valid_lifetime, 0,
430 sysctl_ip6_tempvltime, "I", "");
431 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_V6ONLY,
432 v6only, CTLFLAG_RW, &ip6_v6only, 0, "");
433 SYSCTL_INT(_net_inet6_ip6, IPV6CTL_AUTO_LINKLOCAL,
434 auto_linklocal, CTLFLAG_RW, &ip6_auto_linklocal, 0, "");
435 SYSCTL_STRUCT(_net_inet6_ip6, IPV6CTL_RIP6STATS, rip6stats, CTLFLAG_RD,
436 &rip6stat, rip6stat, "");
438 /* net.inet6.icmp6 */
439 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_REDIRACCEPT,
440 rediraccept, CTLFLAG_RW, &icmp6_rediraccept, 0, "");
441 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_REDIRTIMEOUT,
442 redirtimeout, CTLFLAG_RW, &icmp6_redirtimeout, 0, "");
443 SYSCTL_STRUCT(_net_inet6_icmp6, ICMPV6CTL_STATS, stats, CTLFLAG_RD,
444 &icmp6stat, icmp6stat, "");
445 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_PRUNE,
446 nd6_prune, CTLFLAG_RW, &nd6_prune, 0, "");
447 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_DELAY,
448 nd6_delay, CTLFLAG_RW, &nd6_delay, 0, "");
449 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_UMAXTRIES,
450 nd6_umaxtries, CTLFLAG_RW, &nd6_umaxtries, 0, "");
451 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MMAXTRIES,
452 nd6_mmaxtries, CTLFLAG_RW, &nd6_mmaxtries, 0, "");
453 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_USELOOPBACK,
454 nd6_useloopback, CTLFLAG_RW, &nd6_useloopback, 0, "");
455 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_NODEINFO,
456 nodeinfo, CTLFLAG_RW, &icmp6_nodeinfo, 0, "");
457 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ERRPPSLIMIT,
458 errppslimit, CTLFLAG_RW, &icmp6errppslim, 0, "");
459 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXNUDHINT,
460 nd6_maxnudhint, CTLFLAG_RW, &nd6_maxnudhint, 0, "");
461 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_DEBUG,
462 nd6_debug, CTLFLAG_RW, &nd6_debug, 0, "");