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