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