Home | History | Annotate | Line # | Download | only in netinet6
in6_proto.c revision 1.21
      1 /*	$NetBSD: in6_proto.c,v 1.21 2000/10/10 16:26:44 itojun Exp $	*/
      2 /*	$KAME: in6_proto.c,v 1.66 2000/10/10 15:35:47 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)in_proto.c	8.1 (Berkeley) 6/10/93
     66  */
     67 
     68 #include "opt_inet.h"
     69 #include "opt_ipsec.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/socket.h>
     73 #include <sys/protosw.h>
     74 #include <sys/kernel.h>
     75 #include <sys/domain.h>
     76 #include <sys/mbuf.h>
     77 
     78 #include <net/if.h>
     79 #include <net/radix.h>
     80 #include <net/route.h>
     81 
     82 #include <netinet/in.h>
     83 #include <netinet/in_systm.h>
     84 #include <netinet/in_var.h>
     85 #include <netinet/ip_encap.h>
     86 #ifndef TCP6
     87 #include <netinet/ip.h>
     88 #include <netinet/ip_var.h>
     89 #endif
     90 #ifndef TCP6
     91 #include <netinet/in_pcb.h>
     92 #endif
     93 #include <netinet/ip6.h>
     94 #include <netinet6/ip6_var.h>
     95 #include <netinet/icmp6.h>
     96 #include <netinet6/in6_pcb.h>
     97 
     98 #ifndef TCP6
     99 #include <netinet/tcp.h>
    100 #include <netinet/tcp_fsm.h>
    101 #include <netinet/tcp_seq.h>
    102 #include <netinet/tcp_timer.h>
    103 #include <netinet/tcp_var.h>
    104 #include <netinet/tcpip.h>
    105 #include <netinet/tcp_debug.h>
    106 #else
    107 #include <netinet6/tcp6.h>
    108 #include <netinet6/tcp6_fsm.h>
    109 #include <netinet6/tcp6_seq.h>
    110 #include <netinet6/tcp6_timer.h>
    111 #include <netinet6/tcp6_var.h>
    112 #endif
    113 
    114 #include <netinet6/udp6.h>
    115 #include <netinet6/udp6_var.h>
    116 
    117 #include <netinet6/pim6_var.h>
    118 
    119 #include <netinet6/nd6.h>
    120 
    121 
    122 #ifdef IPSEC
    123 #include <netinet6/ipsec.h>
    124 #include <netinet6/ah.h>
    125 #ifdef IPSEC_ESP
    126 #include <netinet6/esp.h>
    127 #endif
    128 #include <netinet6/ipcomp.h>
    129 #endif /*IPSEC*/
    130 
    131 #include <netinet6/ip6protosw.h>
    132 
    133 #include "gif.h"
    134 #if NGIF > 0
    135 #include <netinet6/in6_gif.h>
    136 #endif
    137 
    138 #include <net/net_osdep.h>
    139 
    140 #define	offsetof(type, member)	((size_t)(&((type *)0)->member))
    141 
    142 /*
    143  * TCP/IP protocol family: IP6, ICMP6, UDP, TCP.
    144  */
    145 
    146 extern	struct domain inet6domain;
    147 
    148 struct ip6protosw inet6sw[] = {
    149 { 0,		&inet6domain,	IPPROTO_IPV6,	0,
    150   0,		0,		0,		0,
    151   0,
    152   ip6_init,	0,		frag6_slowtimo,	frag6_drain,
    153   ip6_sysctl,
    154 },
    155 { SOCK_DGRAM,	&inet6domain,	IPPROTO_UDP,	PR_ATOMIC | PR_ADDR,
    156   udp6_input,	0,		udp6_ctlinput,	ip6_ctloutput,
    157   udp6_usrreq,	udp6_init,
    158   0,		0,		0,
    159   udp6_sysctl,
    160 },
    161 #ifdef TCP6
    162 { SOCK_STREAM,	&inet6domain,	IPPROTO_TCP,	PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN,
    163   tcp6_input,	0,		tcp6_ctlinput,	tcp6_ctloutput,
    164   tcp6_usrreq,
    165   tcp6_init,	tcp6_fasttimo,	tcp6_slowtimo,	tcp6_drain,
    166   tcp6_sysctl,
    167 },
    168 #else
    169 { SOCK_STREAM,	&inet6domain,	IPPROTO_TCP,	PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN,
    170   tcp6_input,	0,		tcp6_ctlinput,	tcp_ctloutput,
    171   tcp_usrreq,
    172 #ifdef INET	/* don't call initialization and timeout routines twice */
    173   0,		0,		0,		tcp_drain,
    174 #else
    175   tcp_init,	tcp_fasttimo,	tcp_slowtimo,	tcp_drain,
    176 #endif
    177   tcp_sysctl,
    178 },
    179 #endif /*TCP6*/
    180 { SOCK_RAW,	&inet6domain,	IPPROTO_RAW,	PR_ATOMIC | PR_ADDR,
    181   rip6_input,	rip6_output,	rip6_ctlinput,	rip6_ctloutput,
    182   rip6_usrreq,
    183   0,		0,		0,		0,
    184 },
    185 { SOCK_RAW,	&inet6domain,	IPPROTO_ICMPV6,	PR_ATOMIC | PR_ADDR,
    186   icmp6_input,	rip6_output,	0,		rip6_ctloutput,
    187   rip6_usrreq,
    188   icmp6_init,	icmp6_fasttimo,	0,		0,
    189   icmp6_sysctl,
    190 },
    191 { SOCK_RAW,	&inet6domain,	IPPROTO_DSTOPTS,PR_ATOMIC|PR_ADDR,
    192   dest6_input,	0,	 	0,		0,
    193   0,
    194   0,		0,		0,		0,
    195 },
    196 { SOCK_RAW,	&inet6domain,	IPPROTO_ROUTING,PR_ATOMIC|PR_ADDR,
    197   route6_input,	0,	 	0,		0,
    198   0,
    199   0,		0,		0,		0,
    200 },
    201 { SOCK_RAW,	&inet6domain,	IPPROTO_FRAGMENT,PR_ATOMIC|PR_ADDR,
    202   frag6_input,	0,	 	0,		0,
    203   0,
    204   0,		0,		0,		0,
    205 },
    206 #ifdef IPSEC
    207 { SOCK_RAW,	&inet6domain,	IPPROTO_AH,	PR_ATOMIC|PR_ADDR,
    208   ah6_input,	0,	 	0,		0,
    209   0,
    210   0,		0,		0,		0,
    211   ipsec6_sysctl,
    212 },
    213 #ifdef IPSEC_ESP
    214 { SOCK_RAW,	&inet6domain,	IPPROTO_ESP,	PR_ATOMIC|PR_ADDR,
    215   esp6_input,	0,	 	0,		0,
    216   0,
    217   0,		0,		0,		0,
    218   ipsec6_sysctl,
    219 },
    220 #endif
    221 { SOCK_RAW,	&inet6domain,	IPPROTO_IPCOMP,	PR_ATOMIC|PR_ADDR,
    222   ipcomp6_input, 0,	 	0,		0,
    223   0,
    224   0,		0,		0,		0,
    225   ipsec6_sysctl,
    226 },
    227 #endif /* IPSEC */
    228 #ifdef INET
    229 { SOCK_RAW,	&inet6domain,	IPPROTO_IPV4,	PR_ATOMIC|PR_ADDR,
    230   encap6_input,	rip6_output, 	0,		rip6_ctloutput,
    231   rip6_usrreq,
    232   0,		0,		0,		0,
    233 },
    234 #endif
    235 { SOCK_RAW,	&inet6domain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR,
    236   encap6_input, rip6_output,	 0,		rip6_ctloutput,
    237   rip6_usrreq,
    238 #ifndef INET6
    239   0,		0,		0,		0,
    240 #else
    241   encap_init,	0,		0,		0,
    242 #endif
    243 },
    244 { SOCK_RAW,     &inet6domain,	IPPROTO_PIM,	PR_ATOMIC|PR_ADDR,
    245   pim6_input,    rip6_output,	0,              rip6_ctloutput,
    246   rip6_usrreq,
    247   0,            0,              0,              0,
    248 },
    249 /* raw wildcard */
    250 { SOCK_RAW,	&inet6domain,	0,		PR_ATOMIC | PR_ADDR,
    251   rip6_input,	rip6_output,	0,		rip6_ctloutput,
    252   rip6_usrreq,
    253   rip6_init,	0,		0,		0,
    254 },
    255 };
    256 
    257 #if NGIF > 0
    258 struct ip6protosw in6_gif_protosw =
    259 { SOCK_RAW,	&inet6domain,	0/*IPPROTO_IPV[46]*/,	PR_ATOMIC|PR_ADDR,
    260   in6_gif_input, rip6_output,	0,		rip6_ctloutput,
    261   rip6_usrreq,
    262   0,            0,              0,              0,
    263 };
    264 #endif /*NGIF*/
    265 
    266 struct domain inet6domain =
    267     { AF_INET6, "internet6", 0, 0, 0,
    268       (struct protosw *)inet6sw,
    269       (struct protosw *)&inet6sw[sizeof(inet6sw)/sizeof(inet6sw[0])], 0,
    270       rn_inithead,
    271       offsetof(struct sockaddr_in6, sin6_addr) << 3,
    272       sizeof(struct sockaddr_in6) };
    273 
    274 /*
    275  * Internet configuration info
    276  */
    277 #ifndef	IPV6FORWARDING
    278 #ifdef GATEWAY6
    279 #define	IPV6FORWARDING	1	/* forward IP6 packets not for us */
    280 #else
    281 #define	IPV6FORWARDING	0	/* don't forward IP6 packets not for us */
    282 #endif /* GATEWAY6 */
    283 #endif /* !IPV6FORWARDING */
    284 
    285 #ifndef	IPV6_SENDREDIRECTS
    286 #define	IPV6_SENDREDIRECTS	1
    287 #endif
    288 
    289 int	ip6_forwarding = IPV6FORWARDING;	/* act as router? */
    290 int	ip6_sendredirects = IPV6_SENDREDIRECTS;
    291 int	ip6_defhlim = IPV6_DEFHLIM;
    292 int	ip6_defmcasthlim = IPV6_DEFAULT_MULTICAST_HOPS;
    293 int	ip6_accept_rtadv = 0;	/* "IPV6FORWARDING ? 0 : 1" is dangerous */
    294 int	ip6_maxfragpackets = 200;
    295 int	ip6_log_interval = 5;
    296 int	ip6_hdrnestlimit = 50;	/* appropriate? */
    297 int	ip6_dad_count = 1;	/* DupAddrDetectionTransmits */
    298 u_int32_t ip6_flow_seq;
    299 int	ip6_auto_flowlabel = 1;
    300 #if NGIF > 0
    301 int	ip6_gif_hlim = GIF_HLIM;
    302 #else
    303 int	ip6_gif_hlim = 0;
    304 #endif
    305 int	ip6_use_deprecated = 1;	/* allow deprecated addr (RFC2462 5.5.4) */
    306 int	ip6_rr_prune = 5;	/* router renumbering prefix
    307 				 * walk list every 5 sec.    */
    308 #ifndef INET6_BINDV6ONLY
    309 int	ip6_bindv6only = 1;
    310 #endif
    311 
    312 u_int32_t ip6_id = 0UL;
    313 int	ip6_keepfaith = 0;
    314 time_t	ip6_log_time = (time_t)0L;
    315 
    316 /* icmp6 */
    317 /*
    318  * BSDI4 defines these variables in in_proto.c...
    319  * XXX: what if we don't define INET? Should we define pmtu6_expire
    320  * or so? (jinmei (at) kame.net 19990310)
    321  */
    322 int pmtu_expire = 60*10;
    323 int pmtu_probe = 60*2;
    324 
    325 /* raw IP6 parameters */
    326 /*
    327  * Nominal space allocated to a raw ip socket.
    328  */
    329 #define	RIPV6SNDQ	8192
    330 #define	RIPV6RCVQ	8192
    331 
    332 u_long	rip6_sendspace = RIPV6SNDQ;
    333 u_long	rip6_recvspace = RIPV6RCVQ;
    334 
    335 /* ICMPV6 parameters */
    336 int	icmp6_rediraccept = 1;		/* accept and process redirects */
    337 int	icmp6_redirtimeout = 10 * 60;	/* 10 minutes */
    338 int	icmp6errppslim = 100;		/* 100pps */
    339 int	icmp6_nodeinfo = 1;		/* enable/disable NI response */
    340 
    341 #ifdef TCP6
    342 /* TCP on IP6 parameters */
    343 int	tcp6_sendspace = 1024 * 8;
    344 int	tcp6_recvspace = 1024 * 8;
    345 int 	tcp6_mssdflt = TCP6_MSS;
    346 int 	tcp6_rttdflt = TCP6TV_SRTTDFLT / PR_SLOWHZ;
    347 int	tcp6_do_rfc1323 = 1;
    348 int	tcp6_conntimeo = TCP6TV_KEEP_INIT;	/* initial connection timeout */
    349 int	tcp6_43maxseg = 0;
    350 int	tcp6_pmtu = 0;
    351 
    352 /*
    353  * Parameters for keepalive option.
    354  * Connections for which SO_KEEPALIVE is set will be probed
    355  * after being idle for a time of tcp6_keepidle (in units of PR_SLOWHZ).
    356  * Starting at that time, the connection is probed at intervals
    357  * of tcp6_keepintvl (same units) until a response is received
    358  * or until tcp6_keepcnt probes have been made, at which time
    359  * the connection is dropped.  Note that a tcp6_keepidle value
    360  * under 2 hours is nonconformant with RFC-1122, Internet Host Requirements.
    361  */
    362 int	tcp6_keepidle = TCP6TV_KEEP_IDLE;	/* time before probing idle */
    363 int	tcp6_keepintvl = TCP6TV_KEEPINTVL;	/* interval betwn idle probes */
    364 int	tcp6_keepcnt = TCP6TV_KEEPCNT;		/* max idle probes */
    365 int	tcp6_maxpersistidle = TCP6TV_KEEP_IDLE;	/* max idle time in persist */
    366 
    367 #ifndef INET_SERVER
    368 #define	TCP6_LISTEN_HASH_SIZE	17
    369 #define	TCP6_CONN_HASH_SIZE	97
    370 #define	TCP6_SYN_HASH_SIZE	293
    371 #define	TCP6_SYN_BUCKET_SIZE	35
    372 #else
    373 #define	TCP6_LISTEN_HASH_SIZE	97
    374 #define	TCP6_CONN_HASH_SIZE	9973
    375 #define	TCP6_SYN_HASH_SIZE	997
    376 #define	TCP6_SYN_BUCKET_SIZE	35
    377 #endif
    378 int	tcp6_listen_hash_size = TCP6_LISTEN_HASH_SIZE;
    379 int	tcp6_conn_hash_size = TCP6_CONN_HASH_SIZE;
    380 struct	tcp6_hash_list tcp6_listen_hash[TCP6_LISTEN_HASH_SIZE],
    381 	tcp6_conn_hash[TCP6_CONN_HASH_SIZE];
    382 
    383 int	tcp6_syn_cache_size = TCP6_SYN_HASH_SIZE;
    384 int	tcp6_syn_cache_limit = TCP6_SYN_HASH_SIZE*TCP6_SYN_BUCKET_SIZE;
    385 int	tcp6_syn_bucket_limit = 3*TCP6_SYN_BUCKET_SIZE;
    386 struct	syn_cache_head6 tcp6_syn_cache[TCP6_SYN_HASH_SIZE];
    387 struct	syn_cache_head6 *tcp6_syn_cache_first;
    388 int	tcp6_syn_cache_interval = 8;	/* runs timer every 4 seconds */
    389 int	tcp6_syn_cache_timeo = TCP6TV_KEEP_INIT;
    390 
    391 /*
    392  * Parameters for computing a desirable data segment size
    393  * given an upper bound (either interface MTU, or peer's MSS option)_.
    394  * As applications tend to use a buffer size that is a multiple
    395  * of kilobytes, try for something that divides evenly. However,
    396  * do not round down too much.
    397  *
    398  * Round segment size down to a multiple of TCP6_ROUNDSIZE if this
    399  * does not result in lowering by more than (size/TCP6_ROUNDFRAC).
    400  * For example, round 536 to 512.  Older versions of the system
    401  * effectively used MCLBYTES (1K or 2K) as TCP6_ROUNDSIZE, with
    402  * a value of 1 for TCP6_ROUNDFRAC (eliminating its effect).
    403  * We round to a multiple of 256 for SLIP.
    404  */
    405 #ifndef	TCP6_ROUNDSIZE
    406 #define	TCP6_ROUNDSIZE	256	/* round to multiple of 256 */
    407 #endif
    408 #ifndef	TCP6_ROUNDFRAC
    409 #define	TCP6_ROUNDFRAC	10	/* round down at most N/10, or 10% */
    410 #endif
    411 
    412 int	tcp6_roundsize = TCP6_ROUNDSIZE;
    413 int	tcp6_roundfrac = TCP6_ROUNDFRAC;
    414 #endif /*TCP6*/
    415 
    416 /* UDP on IP6 parameters */
    417 int	udp6_sendspace = 9216;		/* really max datagram size */
    418 int	udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
    419 					/* 40 1K datagrams */
    420