Home | History | Annotate | Line # | Download | only in netinet
in_proto.c revision 1.93
      1 /*	$NetBSD: in_proto.c,v 1.93 2008/04/23 06:09:04 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)in_proto.c	8.2 (Berkeley) 2/9/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: in_proto.c,v 1.93 2008/04/23 06:09:04 thorpej Exp $");
     65 
     66 #include "opt_mrouting.h"
     67 #include "opt_eon.h"			/* ISO CLNL over IP */
     68 #include "opt_iso.h"			/* ISO TP tunneled over IP */
     69 #include "opt_inet.h"
     70 #include "opt_ipsec.h"
     71 #include "opt_pim.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/socket.h>
     75 #include <sys/protosw.h>
     76 #include <sys/domain.h>
     77 #include <sys/mbuf.h>
     78 
     79 #include <net/if.h>
     80 #include <net/radix.h>
     81 #include <net/route.h>
     82 
     83 #include <netinet/in.h>
     84 #include <netinet/in_systm.h>
     85 #include <netinet/ip.h>
     86 #include <netinet/ip_var.h>
     87 #include <netinet/ip_icmp.h>
     88 #include <netinet/in_ifattach.h>
     89 #include <netinet/in_pcb.h>
     90 #include <netinet/in_proto.h>
     91 
     92 #ifdef INET6
     93 #ifndef INET
     94 #include <netinet/in.h>
     95 #endif
     96 #include <netinet/ip6.h>
     97 #endif
     98 
     99 #include <netinet/igmp_var.h>
    100 #ifdef PIM
    101 #include <netinet/pim_var.h>
    102 #endif
    103 #include <netinet/tcp.h>
    104 #include <netinet/tcp_fsm.h>
    105 #include <netinet/tcp_seq.h>
    106 #include <netinet/tcp_timer.h>
    107 #include <netinet/tcp_var.h>
    108 #include <netinet/tcpip.h>
    109 #include <netinet/tcp_debug.h>
    110 #include <netinet/udp.h>
    111 #include <netinet/udp_var.h>
    112 #include <netinet/ip_encap.h>
    113 
    114 /*
    115  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
    116  */
    117 
    118 #ifdef IPSEC
    119 #include <netinet6/ipsec.h>
    120 #include <netinet6/ah.h>
    121 #ifdef IPSEC_ESP
    122 #include <netinet6/esp.h>
    123 #endif
    124 #include <netinet6/ipcomp.h>
    125 #endif /* IPSEC */
    126 
    127 #ifdef FAST_IPSEC
    128 #include <netipsec/ipsec.h>
    129 #include <netipsec/key.h>
    130 #endif	/* FAST_IPSEC */
    131 
    132 #ifdef TPIP
    133 #include <netiso/tp_param.h>
    134 #include <netiso/tp_var.h>
    135 #endif /* TPIP */
    136 
    137 #ifdef EON
    138 #include <netiso/eonvar.h>
    139 #endif /* EON */
    140 
    141 #include "carp.h"
    142 #if NCARP > 0
    143 #include <netinet/ip_carp.h>
    144 #endif
    145 
    146 #include "etherip.h"
    147 #if NETHERIP > 0
    148 #include <netinet/ip_etherip.h>
    149 #endif
    150 
    151 DOMAIN_DEFINE(inetdomain);	/* forward declare and add to link set */
    152 
    153 const struct protosw inetsw[] = {
    154 {	.pr_domain = &inetdomain,
    155 	.pr_init = ip_init,
    156 	.pr_output = ip_output,
    157 	.pr_slowtimo = ip_slowtimo,
    158 	.pr_drain = ip_drain,
    159 },
    160 {	.pr_type = SOCK_DGRAM,
    161 	.pr_domain = &inetdomain,
    162 	.pr_protocol = IPPROTO_UDP,
    163 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
    164 	.pr_input = udp_input,
    165 	.pr_ctlinput = udp_ctlinput,
    166 	.pr_ctloutput = udp_ctloutput,
    167 	.pr_usrreq = udp_usrreq,
    168 	.pr_init = udp_init,
    169 },
    170 {	.pr_type = SOCK_STREAM,
    171 	.pr_domain = &inetdomain,
    172 	.pr_protocol = IPPROTO_TCP,
    173 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
    174 	.pr_input = tcp_input,
    175 	.pr_ctlinput = tcp_ctlinput,
    176 	.pr_ctloutput = tcp_ctloutput,
    177 	.pr_usrreq = tcp_usrreq,
    178 	.pr_init = tcp_init,
    179 	.pr_slowtimo = tcp_slowtimo,
    180 	.pr_drain = tcp_drain,
    181 },
    182 {	.pr_type = SOCK_RAW,
    183 	.pr_domain = &inetdomain,
    184 	.pr_protocol = IPPROTO_RAW,
    185 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
    186 	.pr_input = rip_input,
    187 	.pr_output = rip_output,
    188 	.pr_ctlinput = rip_ctlinput,
    189 	.pr_ctloutput = rip_ctloutput,
    190 	.pr_usrreq = rip_usrreq,
    191 },
    192 {	.pr_type = SOCK_RAW,
    193 	.pr_domain = &inetdomain,
    194 	.pr_protocol = IPPROTO_ICMP,
    195 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    196 	.pr_input = icmp_input,
    197 	.pr_output = rip_output,
    198 	.pr_ctlinput = rip_ctlinput,
    199 	.pr_ctloutput = rip_ctloutput,
    200 	.pr_usrreq = rip_usrreq,
    201 	.pr_init = icmp_init,
    202 },
    203 #ifdef GATEWAY
    204 {	.pr_domain = &inetdomain,
    205 	.pr_protocol = IPPROTO_IP,
    206 	.pr_slowtimo = ipflow_slowtimo,
    207 },
    208 #endif /* GATEWAY */
    209 #ifdef IPSEC
    210 {	.pr_type = SOCK_RAW,
    211 	.pr_domain = &inetdomain,
    212 	.pr_protocol = IPPROTO_AH,
    213 	.pr_flags = PR_ATOMIC|PR_ADDR,
    214 	.pr_input = ah4_input,
    215 	.pr_ctlinput = ah4_ctlinput,
    216 	.pr_init = ah4_init,
    217 },
    218 #ifdef IPSEC_ESP
    219 {	.pr_type = SOCK_RAW,
    220 	.pr_domain = &inetdomain,
    221 	.pr_protocol = IPPROTO_ESP,
    222 	.pr_flags = PR_ATOMIC|PR_ADDR,
    223 	.pr_input = esp4_input,
    224 	.pr_ctlinput = esp4_ctlinput,
    225 	.pr_init = esp4_init,
    226 },
    227 #endif /* IPSEC_ESP */
    228 {	.pr_type = SOCK_RAW,
    229 	.pr_domain = &inetdomain,
    230 	.pr_protocol = IPPROTO_IPCOMP,
    231 	.pr_flags = PR_ATOMIC|PR_ADDR,
    232 	.pr_input = ipcomp4_input,
    233 	.pr_init = ipcomp4_init,
    234 },
    235 #endif /* IPSEC */
    236 #ifdef FAST_IPSEC
    237 {	.pr_type = SOCK_RAW,
    238 	.pr_domain = &inetdomain,
    239 	.pr_protocol = IPPROTO_AH,
    240 	.pr_flags = PR_ATOMIC|PR_ADDR,
    241 	.pr_input = ipsec4_common_input,
    242 	.pr_ctlinput = ah4_ctlinput,
    243 },
    244 {	.pr_type = SOCK_RAW,
    245 	.pr_domain = &inetdomain,
    246 	.pr_protocol = IPPROTO_ESP,
    247 	.pr_flags = PR_ATOMIC|PR_ADDR,
    248 	.pr_input = ipsec4_common_input,
    249 	.pr_ctlinput = esp4_ctlinput,
    250 },
    251 {	.pr_type = SOCK_RAW,
    252 	.pr_domain = &inetdomain,
    253 	.pr_protocol = IPPROTO_IPCOMP,
    254 	.pr_flags = PR_ATOMIC|PR_ADDR,
    255 	.pr_input = ipsec4_common_input,
    256 },
    257 #endif /* FAST_IPSEC */
    258 {	.pr_type = SOCK_RAW,
    259 	.pr_domain = &inetdomain,
    260 	.pr_protocol = IPPROTO_IPV4,
    261 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    262 	.pr_input = encap4_input,
    263 	.pr_output = rip_output,
    264 	.pr_ctlinput = rip_ctlinput,
    265 	.pr_ctloutput = rip_ctloutput,
    266 	.pr_usrreq = rip_usrreq,
    267 	.pr_init = encap_init,
    268 },
    269 #ifdef INET6
    270 {	.pr_type = SOCK_RAW,
    271 	.pr_domain = &inetdomain,
    272 	.pr_protocol = IPPROTO_IPV6,
    273 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    274 	.pr_input = encap4_input,
    275 	.pr_output = rip_output,
    276 	.pr_ctlinput = rip_ctlinput,
    277 	.pr_ctloutput = rip_ctloutput,
    278 	.pr_usrreq = rip_usrreq,
    279 	.pr_init = encap_init,
    280 },
    281 #endif /* INET6 */
    282 #if NETHERIP > 0
    283 {	.pr_type = SOCK_RAW,
    284 	.pr_domain = &inetdomain,
    285 	.pr_protocol = IPPROTO_ETHERIP,
    286 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    287 	.pr_input = ip_etherip_input,
    288 	.pr_output = rip_output,
    289 	.pr_ctlinput = rip_ctlinput,
    290 	.pr_ctloutput = rip_ctloutput,
    291 	.pr_usrreq = rip_usrreq,
    292 },
    293 #endif /* NETHERIP > 0 */
    294 #if NCARP > 0
    295 {	.pr_type = SOCK_RAW,
    296 	.pr_domain = &inetdomain,
    297 	.pr_protocol = IPPROTO_CARP,
    298 	.pr_flags = PR_ATOMIC|PR_ADDR,
    299 	.pr_input = carp_proto_input,
    300 	.pr_output = rip_output,
    301 	.pr_ctloutput = rip_ctloutput,
    302 	.pr_usrreq = rip_usrreq,
    303 },
    304 #endif /* NCARP > 0 */
    305 {	.pr_type = SOCK_RAW,
    306 	.pr_domain = &inetdomain,
    307 	.pr_protocol = IPPROTO_IGMP,
    308 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    309 	.pr_input = igmp_input,
    310 	.pr_output = rip_output,
    311 	.pr_ctloutput = rip_ctloutput,
    312 	.pr_ctlinput = rip_ctlinput,
    313 	.pr_usrreq = rip_usrreq,
    314 	.pr_fasttimo = igmp_fasttimo,
    315 	.pr_slowtimo = igmp_slowtimo,
    316 	.pr_init = igmp_init,
    317 },
    318 #ifdef PIM
    319 {	.pr_type = SOCK_RAW,
    320 	.pr_domain = &inetdomain,
    321 	.pr_protocol = IPPROTO_PIM,
    322 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    323 	.pr_input = pim_input,
    324 	.pr_output = rip_output,
    325 	.pr_ctloutput = rip_ctloutput,
    326 	.pr_ctlinput = rip_ctlinput,
    327 	.pr_usrreq = rip_usrreq,
    328 },
    329 #endif /* PIM */
    330 #ifdef TPIP
    331 {	.pr_type = SOCK_SEQPACKET,
    332 	.pr_domain = &inetdomain,
    333 	.pr_protocol = IPPROTO_TP,
    334 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_LASTHDR|PR_ABRTACPTDIS,
    335 	.pr_input = tpip_input,
    336 	.pr_ctloutput = tp_ctloutput,
    337 	.pr_ctlinput = tpip_ctlinput,
    338 	.pr_usrreq = tp_usrreq,
    339 	.pr_init = tp_init,
    340 	.pr_slowtimo = tp_slowtimo,
    341 	.pr_drain = tp_drain,
    342 },
    343 #endif /* TPIP */
    344 #ifdef ISO
    345 /* EON (ISO CLNL over IP) */
    346 #ifdef EON
    347 {	.pr_type = SOCK_RAW,
    348 	.pr_domain = &inetdomain,
    349 	.pr_protocol = IPPROTO_EON,
    350 	.pr_flags = PR_LASTHDR,
    351 	.pr_input = eoninput,
    352 	.pr_ctlinput = eonctlinput,
    353 	.pr_init = eonprotoinit,
    354 },
    355 #else
    356 {	.pr_type = SOCK_RAW,
    357 	.pr_domain = &inetdomain,
    358 	.pr_protocol = IPPROTO_EON,
    359 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    360 	.pr_input = encap4_input,
    361 	.pr_output = rip_output,
    362 	.pr_ctloutput = rip_ctloutput,
    363 	.pr_ctlinput = rip_ctlinput,
    364 	.pr_usrreq = rip_usrreq,
    365 	.pr_init = encap_init,
    366 },
    367 #endif /* EON */
    368 #endif /* ISO */
    369 /* raw wildcard */
    370 {	.pr_type = SOCK_RAW,
    371 	.pr_domain = &inetdomain,
    372 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    373 	.pr_input = rip_input,
    374 	.pr_output = rip_output,
    375 	.pr_ctloutput = rip_ctloutput,
    376 	.pr_ctlinput = rip_ctlinput,
    377 	.pr_usrreq = rip_usrreq,
    378 	.pr_init = rip_init,
    379 },
    380 };
    381 
    382 extern struct ifqueue ipintrq;
    383 
    384 const struct sockaddr_in in_any = {
    385 	  .sin_len = sizeof(struct sockaddr_in)
    386 	, .sin_family = AF_INET
    387 	, .sin_port = 0
    388 	, .sin_addr = {.s_addr = 0 /* INADDR_ANY */}
    389 };
    390 
    391 struct domain inetdomain = {
    392 	.dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
    393 	.dom_externalize = NULL, .dom_dispose = NULL,
    394 	.dom_protosw = inetsw,
    395 	.dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)],
    396 	.dom_rtattach = rn_inithead,
    397 	.dom_rtoffset = 32, .dom_maxrtkey = sizeof(struct sockaddr_in),
    398 #ifdef IPSELSRC
    399 	.dom_ifattach = in_domifattach,
    400 	.dom_ifdetach = in_domifdetach,
    401 #else
    402 	.dom_ifattach = NULL,
    403 	.dom_ifdetach = NULL,
    404 #endif
    405 	.dom_ifqueues = { &ipintrq, NULL },
    406 	.dom_link = { NULL },
    407 	.dom_mowner = MOWNER_INIT("",""),
    408 	.dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr),
    409 	.dom_sa_cmplen = sizeof(struct in_addr),
    410 	.dom_sa_any = (const struct sockaddr *)&in_any,
    411 	.dom_sockaddr_const_addr = sockaddr_in_const_addr,
    412 	.dom_sockaddr_addr = sockaddr_in_addr,
    413 	.dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache)
    414 };
    415 
    416 u_char	ip_protox[IPPROTO_MAX];
    417 
    418 int icmperrppslim = 100;			/* 100pps */
    419 
    420 static void
    421 sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp)
    422 {
    423 	socklen_t slen;
    424 
    425 	if (slenp == NULL)
    426 		return;
    427 
    428 	slen = sockaddr_getlen(sa);
    429 	*slenp = (socklen_t)MIN(sizeof(struct in_addr),
    430 	    slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr)));
    431 }
    432 
    433 const void *
    434 sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp)
    435 {
    436 	const struct sockaddr_in *sin;
    437 
    438 	sockaddr_in_addrlen(sa, slenp);
    439 	sin = (const struct sockaddr_in *)sa;
    440 	return &sin->sin_addr;
    441 }
    442 
    443 void *
    444 sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp)
    445 {
    446 	struct sockaddr_in *sin;
    447 
    448 	sockaddr_in_addrlen(sa, slenp);
    449 	sin = (struct sockaddr_in *)sa;
    450 	return &sin->sin_addr;
    451 }
    452 
    453 int
    454 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    455 {
    456 	uint_fast8_t len;
    457 	const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr),
    458 			   addrend = addrofs + sizeof(struct in_addr);
    459 	int rc;
    460 	const struct sockaddr_in *sin1, *sin2;
    461 
    462 	sin1 = satocsin(sa1);
    463 	sin2 = satocsin(sa2);
    464 
    465 	len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len));
    466 
    467 	if (len > addrofs &&
    468 	     (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr,
    469 	                  len - addrofs)) != 0)
    470 		return rc;
    471 
    472 	return sin1->sin_len - sin2->sin_len;
    473 }
    474