Home | History | Annotate | Line # | Download | only in netinet
in_proto.c revision 1.111
      1 /*	$NetBSD: in_proto.c,v 1.111 2015/02/10 19:11:52 rjs 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.111 2015/02/10 19:11:52 rjs Exp $");
     65 
     66 #include "opt_mrouting.h"
     67 #include "opt_inet.h"
     68 #include "opt_ipsec.h"
     69 #include "opt_pim.h"
     70 #include "opt_gateway.h"
     71 #include "opt_dccp.h"
     72 #include "opt_compat_netbsd.h"
     73 
     74 #include <sys/param.h>
     75 #include <sys/socket.h>
     76 #include <sys/protosw.h>
     77 #include <sys/domain.h>
     78 #include <sys/mbuf.h>
     79 
     80 #include <net/if.h>
     81 #include <net/radix.h>
     82 #include <net/route.h>
     83 
     84 #include <netinet/in.h>
     85 #include <netinet/in_systm.h>
     86 #include <netinet/in_var.h>
     87 #include <netinet/ip.h>
     88 #include <netinet/ip_var.h>
     89 #include <netinet/ip_icmp.h>
     90 #include <netinet/in_ifattach.h>
     91 #include <netinet/in_pcb.h>
     92 #include <netinet/in_proto.h>
     93 
     94 #ifdef INET6
     95 #ifndef INET
     96 #include <netinet/in.h>
     97 #endif
     98 #include <netinet/ip6.h>
     99 #endif
    100 
    101 #include <netinet/igmp_var.h>
    102 #ifdef PIM
    103 #include <netinet/pim_var.h>
    104 #endif
    105 #include <netinet/tcp.h>
    106 #include <netinet/tcp_fsm.h>
    107 #include <netinet/tcp_seq.h>
    108 #include <netinet/tcp_timer.h>
    109 #include <netinet/tcp_var.h>
    110 #include <netinet/tcpip.h>
    111 #include <netinet/tcp_debug.h>
    112 #include <netinet/udp.h>
    113 #include <netinet/udp_var.h>
    114 #include <netinet/ip_encap.h>
    115 
    116 #ifdef DCCP
    117 #include <netinet/dccp.h>
    118 #include <netinet/dccp_var.h>
    119 #endif
    120 
    121 /*
    122  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
    123  */
    124 
    125 #ifdef IPSEC
    126 #include <netipsec/ipsec.h>
    127 #include <netipsec/key.h>
    128 #endif	/* IPSEC */
    129 
    130 #include "carp.h"
    131 #if NCARP > 0
    132 #include <netinet/ip_carp.h>
    133 #endif
    134 
    135 #include "pfsync.h"
    136 #if NPFSYNC > 0
    137 #include <net/pfvar.h>
    138 #include <net/if_pfsync.h>
    139 #endif
    140 
    141 #include "etherip.h"
    142 #if NETHERIP > 0
    143 #include <netinet/ip_etherip.h>
    144 #endif
    145 
    146 DOMAIN_DEFINE(inetdomain);	/* forward declare and add to link set */
    147 
    148 /* Wrappers to acquire kernel_lock. */
    149 
    150 PR_WRAP_CTLINPUT(rip_ctlinput)
    151 PR_WRAP_CTLINPUT(udp_ctlinput)
    152 PR_WRAP_CTLINPUT(tcp_ctlinput)
    153 
    154 #define	rip_ctlinput	rip_ctlinput_wrapper
    155 #define	udp_ctlinput	udp_ctlinput_wrapper
    156 #define	tcp_ctlinput	tcp_ctlinput_wrapper
    157 
    158 PR_WRAP_CTLOUTPUT(rip_ctloutput)
    159 PR_WRAP_CTLOUTPUT(udp_ctloutput)
    160 PR_WRAP_CTLOUTPUT(tcp_ctloutput)
    161 
    162 #define	rip_ctloutput	rip_ctloutput_wrapper
    163 #define	udp_ctloutput	udp_ctloutput_wrapper
    164 #define	tcp_ctloutput	tcp_ctloutput_wrapper
    165 
    166 #ifdef DCCP
    167 PR_WRAP_CTLINPUT(dccp_ctlinput)
    168 PR_WRAP_CTLOUTPUT(dccp_ctloutput)
    169 
    170 #define dccp_ctlinput	dccp_ctlinput_wrapper
    171 #define dccp_ctloutput	dccp_ctloutput_wrapper
    172 #endif
    173 
    174 #if defined(IPSEC)
    175 PR_WRAP_CTLINPUT(ah4_ctlinput)
    176 
    177 #define	ah4_ctlinput	ah4_ctlinput_wrapper
    178 PR_WRAP_CTLINPUT(esp4_ctlinput)
    179 
    180 #define	esp4_ctlinput	esp4_ctlinput_wrapper
    181 #endif
    182 
    183 const struct protosw inetsw[] = {
    184 {	.pr_domain = &inetdomain,
    185 	.pr_init = ip_init,
    186 	.pr_output = ip_output,
    187 	.pr_fasttimo = ip_fasttimo,
    188 	.pr_slowtimo = ip_slowtimo,
    189 	.pr_drain = ip_drainstub,
    190 },
    191 {	.pr_type = SOCK_DGRAM,
    192 	.pr_domain = &inetdomain,
    193 	.pr_protocol = IPPROTO_UDP,
    194 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
    195 	.pr_input = udp_input,
    196 	.pr_ctlinput = udp_ctlinput,
    197 	.pr_ctloutput = udp_ctloutput,
    198 	.pr_usrreqs = &udp_usrreqs,
    199 	.pr_init = udp_init,
    200 },
    201 {	.pr_type = SOCK_STREAM,
    202 	.pr_domain = &inetdomain,
    203 	.pr_protocol = IPPROTO_TCP,
    204 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
    205 	.pr_input = tcp_input,
    206 	.pr_ctlinput = tcp_ctlinput,
    207 	.pr_ctloutput = tcp_ctloutput,
    208 	.pr_usrreqs = &tcp_usrreqs,
    209 	.pr_init = tcp_init,
    210 	.pr_fasttimo = tcp_fasttimo,
    211 	.pr_drain = tcp_drainstub,
    212 },
    213 #ifdef DCCP
    214 {	.pr_type = SOCK_CONN_DGRAM,
    215 	.pr_domain = &inetdomain,
    216 	.pr_protocol = IPPROTO_DCCP,
    217 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_ATOMIC|PR_LISTEN|PR_ABRTACPTDIS,
    218 	.pr_input = dccp_input,
    219 	.pr_ctlinput = dccp_ctlinput,
    220 	.pr_ctloutput = dccp_ctloutput,
    221 	.pr_usrreqs = &dccp_usrreqs,
    222 	.pr_init = dccp_init,
    223 },
    224 #endif
    225 {	.pr_type = SOCK_RAW,
    226 	.pr_domain = &inetdomain,
    227 	.pr_protocol = IPPROTO_RAW,
    228 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
    229 	.pr_input = rip_input,
    230 	.pr_output = rip_output,
    231 	.pr_ctlinput = rip_ctlinput,
    232 	.pr_ctloutput = rip_ctloutput,
    233 	.pr_usrreqs = &rip_usrreqs,
    234 },
    235 {	.pr_type = SOCK_RAW,
    236 	.pr_domain = &inetdomain,
    237 	.pr_protocol = IPPROTO_ICMP,
    238 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    239 	.pr_input = icmp_input,
    240 	.pr_output = rip_output,
    241 	.pr_ctlinput = rip_ctlinput,
    242 	.pr_ctloutput = rip_ctloutput,
    243 	.pr_usrreqs = &rip_usrreqs,
    244 	.pr_init = icmp_init,
    245 },
    246 #ifdef GATEWAY
    247 {	.pr_domain = &inetdomain,
    248 	.pr_protocol = IPPROTO_IP,
    249 	.pr_slowtimo = ipflow_slowtimo,
    250 	.pr_init = ipflow_poolinit,
    251 },
    252 #endif /* GATEWAY */
    253 #ifdef IPSEC
    254 {	.pr_type = SOCK_RAW,
    255 	.pr_domain = &inetdomain,
    256 	.pr_protocol = IPPROTO_AH,
    257 	.pr_flags = PR_ATOMIC|PR_ADDR,
    258 	.pr_input = ipsec4_common_input,
    259 	.pr_ctlinput = ah4_ctlinput,
    260 },
    261 {	.pr_type = SOCK_RAW,
    262 	.pr_domain = &inetdomain,
    263 	.pr_protocol = IPPROTO_ESP,
    264 	.pr_flags = PR_ATOMIC|PR_ADDR,
    265 	.pr_input = ipsec4_common_input,
    266 	.pr_ctlinput = esp4_ctlinput,
    267 },
    268 {	.pr_type = SOCK_RAW,
    269 	.pr_domain = &inetdomain,
    270 	.pr_protocol = IPPROTO_IPCOMP,
    271 	.pr_flags = PR_ATOMIC|PR_ADDR,
    272 	.pr_input = ipsec4_common_input,
    273 },
    274 #endif /* IPSEC */
    275 {	.pr_type = SOCK_RAW,
    276 	.pr_domain = &inetdomain,
    277 	.pr_protocol = IPPROTO_IPV4,
    278 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    279 	.pr_input = encap4_input,
    280 	.pr_output = rip_output,
    281 	.pr_ctlinput = rip_ctlinput,
    282 	.pr_ctloutput = rip_ctloutput,
    283 	.pr_usrreqs = &rip_usrreqs,
    284 	.pr_init = encap_init,
    285 },
    286 #ifdef INET6
    287 {	.pr_type = SOCK_RAW,
    288 	.pr_domain = &inetdomain,
    289 	.pr_protocol = IPPROTO_IPV6,
    290 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    291 	.pr_input = encap4_input,
    292 	.pr_output = rip_output,
    293 	.pr_ctlinput = rip_ctlinput,
    294 	.pr_ctloutput = rip_ctloutput,
    295 	.pr_usrreqs = &rip_usrreqs,
    296 	.pr_init = encap_init,
    297 },
    298 #endif /* INET6 */
    299 #if NETHERIP > 0
    300 {	.pr_type = SOCK_RAW,
    301 	.pr_domain = &inetdomain,
    302 	.pr_protocol = IPPROTO_ETHERIP,
    303 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    304 	.pr_input = ip_etherip_input,
    305 	.pr_output = rip_output,
    306 	.pr_ctlinput = rip_ctlinput,
    307 	.pr_ctloutput = rip_ctloutput,
    308 	.pr_usrreqs = &rip_usrreqs,
    309 },
    310 #endif /* NETHERIP > 0 */
    311 #if NCARP > 0
    312 {	.pr_type = SOCK_RAW,
    313 	.pr_domain = &inetdomain,
    314 	.pr_protocol = IPPROTO_CARP,
    315 	.pr_flags = PR_ATOMIC|PR_ADDR,
    316 	.pr_input = carp_proto_input,
    317 	.pr_output = rip_output,
    318 	.pr_ctloutput = rip_ctloutput,
    319 	.pr_usrreqs = &rip_usrreqs,
    320 	.pr_init = carp_init,
    321 },
    322 #endif /* NCARP > 0 */
    323 #if NPFSYNC > 0
    324 {	.pr_type = SOCK_RAW,
    325 	.pr_domain = &inetdomain,
    326 	.pr_protocol = IPPROTO_PFSYNC,
    327 	.pr_flags	 = PR_ATOMIC|PR_ADDR,
    328 	.pr_input	 = pfsync_input,
    329 	.pr_output	 = rip_output,
    330 	.pr_ctloutput = rip_ctloutput,
    331 	.pr_usrreqs	 = &rip_usrreqs,
    332 },
    333 #endif /* NPFSYNC > 0 */
    334 {	.pr_type = SOCK_RAW,
    335 	.pr_domain = &inetdomain,
    336 	.pr_protocol = IPPROTO_IGMP,
    337 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    338 	.pr_input = igmp_input,
    339 	.pr_output = rip_output,
    340 	.pr_ctloutput = rip_ctloutput,
    341 	.pr_ctlinput = rip_ctlinput,
    342 	.pr_usrreqs = &rip_usrreqs,
    343 	.pr_fasttimo = igmp_fasttimo,
    344 	.pr_slowtimo = igmp_slowtimo,
    345 	.pr_init = igmp_init,
    346 },
    347 #ifdef PIM
    348 {	.pr_type = SOCK_RAW,
    349 	.pr_domain = &inetdomain,
    350 	.pr_protocol = IPPROTO_PIM,
    351 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    352 	.pr_input = pim_input,
    353 	.pr_output = rip_output,
    354 	.pr_ctloutput = rip_ctloutput,
    355 	.pr_ctlinput = rip_ctlinput,
    356 	.pr_usrreqs = &rip_usrreqs,
    357 },
    358 #endif /* PIM */
    359 /* raw wildcard */
    360 {	.pr_type = SOCK_RAW,
    361 	.pr_domain = &inetdomain,
    362 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    363 	.pr_input = rip_input,
    364 	.pr_output = rip_output,
    365 	.pr_ctloutput = rip_ctloutput,
    366 	.pr_ctlinput = rip_ctlinput,
    367 	.pr_usrreqs = &rip_usrreqs,
    368 	.pr_init = rip_init,
    369 },
    370 };
    371 
    372 const struct sockaddr_in in_any = {
    373 	  .sin_len = sizeof(struct sockaddr_in)
    374 	, .sin_family = AF_INET
    375 	, .sin_port = 0
    376 	, .sin_addr = {.s_addr = 0 /* INADDR_ANY */}
    377 };
    378 
    379 struct domain inetdomain = {
    380 	.dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
    381 	.dom_externalize = NULL, .dom_dispose = NULL,
    382 	.dom_protosw = inetsw,
    383 	.dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)],
    384 	.dom_rtattach = rt_inithead,
    385 	.dom_rtoffset = 32,
    386 	.dom_maxrtkey = sizeof(struct ip_pack4),
    387 #ifdef IPSELSRC
    388 	.dom_ifattach = in_domifattach,
    389 	.dom_ifdetach = in_domifdetach,
    390 #else
    391 	.dom_ifattach = NULL,
    392 	.dom_ifdetach = NULL,
    393 #endif
    394 	.dom_ifqueues = { NULL, NULL },
    395 	.dom_link = { NULL },
    396 	.dom_mowner = MOWNER_INIT("",""),
    397 	.dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr),
    398 	.dom_sa_cmplen = sizeof(struct in_addr),
    399 	.dom_sa_any = (const struct sockaddr *)&in_any,
    400 	.dom_sockaddr_const_addr = sockaddr_in_const_addr,
    401 	.dom_sockaddr_addr = sockaddr_in_addr,
    402 	.dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache)
    403 };
    404 
    405 u_char	ip_protox[IPPROTO_MAX];
    406 
    407 int icmperrppslim = 100;			/* 100pps */
    408 
    409 static void
    410 sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp)
    411 {
    412 	socklen_t slen;
    413 
    414 	if (slenp == NULL)
    415 		return;
    416 
    417 	slen = sockaddr_getlen(sa);
    418 	*slenp = (socklen_t)MIN(sizeof(struct in_addr),
    419 	    slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr)));
    420 }
    421 
    422 const void *
    423 sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp)
    424 {
    425 	const struct sockaddr_in *sin;
    426 
    427 	sockaddr_in_addrlen(sa, slenp);
    428 	sin = (const struct sockaddr_in *)sa;
    429 	return &sin->sin_addr;
    430 }
    431 
    432 void *
    433 sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp)
    434 {
    435 	struct sockaddr_in *sin;
    436 
    437 	sockaddr_in_addrlen(sa, slenp);
    438 	sin = (struct sockaddr_in *)sa;
    439 	return &sin->sin_addr;
    440 }
    441 
    442 int
    443 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    444 {
    445 	uint_fast8_t len;
    446 	const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr),
    447 			   addrend = addrofs + sizeof(struct in_addr);
    448 	int rc;
    449 	const struct sockaddr_in *sin1, *sin2;
    450 
    451 	sin1 = satocsin(sa1);
    452 	sin2 = satocsin(sa2);
    453 
    454 	len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len));
    455 
    456 	if (len > addrofs &&
    457 	     (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr,
    458 	                  len - addrofs)) != 0)
    459 		return rc;
    460 
    461 	return sin1->sin_len - sin2->sin_len;
    462 }
    463