Home | History | Annotate | Line # | Download | only in netinet
in_proto.c revision 1.126.2.3
      1 /*	$NetBSD: in_proto.c,v 1.126.2.3 2018/09/06 06:56:44 pgoyette 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.126.2.3 2018/09/06 06:56:44 pgoyette 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_sctp.h"
     74 #include "opt_compat_netbsd.h"
     75 #include "opt_net_mpsafe.h"
     76 #endif
     77 
     78 #include <sys/param.h>
     79 #include <sys/socket.h>
     80 #include <sys/protosw.h>
     81 #include <sys/domain.h>
     82 #include <sys/mbuf.h>
     83 
     84 #include <net/if.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/tcp_debug.h>
    113 #include <netinet/udp.h>
    114 #include <netinet/udp_var.h>
    115 #include <netinet/ip_encap.h>
    116 
    117 #ifdef DCCP
    118 #include <netinet/dccp.h>
    119 #include <netinet/dccp_var.h>
    120 #endif
    121 
    122 #ifdef SCTP
    123 #include <netinet/sctp.h>
    124 #include <netinet/sctp_var.h>
    125 #endif
    126 
    127 /*
    128  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
    129  */
    130 
    131 #ifdef IPSEC
    132 #include <netipsec/ipsec.h>
    133 #include <netipsec/key.h>
    134 #endif	/* IPSEC */
    135 
    136 #include "carp.h"
    137 #if NCARP > 0
    138 #include <netinet/ip_carp.h>
    139 #endif
    140 
    141 #include "pfsync.h"
    142 #if NPFSYNC > 0
    143 #include <net/pfvar.h>
    144 #include <net/if_pfsync.h>
    145 #endif
    146 
    147 DOMAIN_DEFINE(inetdomain);	/* forward declare and add to link set */
    148 
    149 /* Wrappers to acquire kernel_lock. */
    150 
    151 PR_WRAP_CTLINPUT(rip_ctlinput)
    152 PR_WRAP_CTLINPUT(udp_ctlinput)
    153 PR_WRAP_CTLINPUT(tcp_ctlinput)
    154 
    155 #define	rip_ctlinput	rip_ctlinput_wrapper
    156 #define	udp_ctlinput	udp_ctlinput_wrapper
    157 #define	tcp_ctlinput	tcp_ctlinput_wrapper
    158 
    159 PR_WRAP_CTLOUTPUT(rip_ctloutput)
    160 PR_WRAP_CTLOUTPUT(udp_ctloutput)
    161 PR_WRAP_CTLOUTPUT(tcp_ctloutput)
    162 
    163 #define	rip_ctloutput	rip_ctloutput_wrapper
    164 #define	udp_ctloutput	udp_ctloutput_wrapper
    165 #define	tcp_ctloutput	tcp_ctloutput_wrapper
    166 
    167 #ifdef DCCP
    168 PR_WRAP_CTLINPUT(dccp_ctlinput)
    169 PR_WRAP_CTLOUTPUT(dccp_ctloutput)
    170 
    171 #define dccp_ctlinput	dccp_ctlinput_wrapper
    172 #define dccp_ctloutput	dccp_ctloutput_wrapper
    173 #endif
    174 
    175 #ifdef SCTP
    176 PR_WRAP_CTLINPUT(sctp_ctlinput)
    177 PR_WRAP_CTLOUTPUT(sctp_ctloutput)
    178 
    179 #define sctp_ctlinput	sctp_ctlinput_wrapper
    180 #define sctp_ctloutput	sctp_ctloutput_wrapper
    181 #endif
    182 
    183 #ifdef NET_MPSAFE
    184 PR_WRAP_INPUT(udp_input)
    185 PR_WRAP_INPUT(tcp_input)
    186 #ifdef DCCP
    187 PR_WRAP_INPUT(dccp_input)
    188 #endif
    189 #ifdef SCTP
    190 PR_WRAP_INPUT(sctp_input)
    191 #endif
    192 PR_WRAP_INPUT(rip_input)
    193 #if NPFSYNC > 0
    194 PR_WRAP_INPUT(pfsync_input)
    195 #endif
    196 PR_WRAP_INPUT(igmp_input)
    197 #ifdef PIM
    198 PR_WRAP_INPUT(pim_input)
    199 #endif
    200 
    201 #define	udp_input		udp_input_wrapper
    202 #define	tcp_input		tcp_input_wrapper
    203 #define	dccp_input		dccp_input_wrapper
    204 #define	sctp_input		sctp_input_wrapper
    205 #define	rip_input		rip_input_wrapper
    206 #define	pfsync_input		pfsync_input_wrapper
    207 #define	igmp_input		igmp_input_wrapper
    208 #define	pim_input		pim_input_wrapper
    209 #endif
    210 
    211 #if defined(IPSEC)
    212 
    213 #ifdef IPSEC_RUMPKERNEL
    214 /*
    215  * .pr_input = ipsec4_common_input won't be resolved on loading
    216  * the ipsec shared library. We need a wrapper anyway.
    217  */
    218 static void
    219 ipsec4_common_input_wrapper(struct mbuf *m, ...)
    220 {
    221 
    222 	if (ipsec_enabled) {
    223 		int off, nxt;
    224 		va_list args;
    225 		/* XXX just passing args to ipsec4_common_input doesn't work */
    226 		va_start(args, m);
    227 		off = va_arg(args, int);
    228 		nxt = va_arg(args, int);
    229 		va_end(args);
    230 		ipsec4_common_input(m, off, nxt);
    231 	} else {
    232 		m_freem(m);
    233 	}
    234 }
    235 #define	ipsec4_common_input	ipsec4_common_input_wrapper
    236 
    237 /* The ctlinput functions may not be loaded */
    238 #define	IPSEC_WRAP_CTLINPUT(name)			\
    239 static void *						\
    240 name##_wrapper(int a, const struct sockaddr *b, void *c)\
    241 {							\
    242 	void *rv;					\
    243 	KERNEL_LOCK(1, NULL);				\
    244 	if (ipsec_enabled)				\
    245 		rv = name(a, b, c);			\
    246 	else						\
    247 		rv = NULL;				\
    248 	KERNEL_UNLOCK_ONE(NULL);			\
    249 	return rv;					\
    250 }
    251 IPSEC_WRAP_CTLINPUT(ah4_ctlinput)
    252 IPSEC_WRAP_CTLINPUT(esp4_ctlinput)
    253 
    254 #else /* !IPSEC_RUMPKERNEL */
    255 
    256 PR_WRAP_CTLINPUT(ah4_ctlinput)
    257 PR_WRAP_CTLINPUT(esp4_ctlinput)
    258 
    259 #endif /* !IPSEC_RUMPKERNEL */
    260 
    261 #define	ah4_ctlinput	ah4_ctlinput_wrapper
    262 #define	esp4_ctlinput	esp4_ctlinput_wrapper
    263 
    264 #endif /* IPSEC */
    265 
    266 const struct protosw inetsw[] = {
    267 {	.pr_domain = &inetdomain,
    268 	.pr_init = ip_init,
    269 	.pr_fasttimo = ip_fasttimo,
    270 	.pr_slowtimo = ip_slowtimo,
    271 	.pr_drain = ip_drainstub,
    272 },
    273 {	.pr_type = SOCK_RAW,
    274 	.pr_domain = &inetdomain,
    275 	.pr_protocol = IPPROTO_ICMP,
    276 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    277 	.pr_input = icmp_input,
    278 	.pr_ctlinput = rip_ctlinput,
    279 	.pr_ctloutput = rip_ctloutput,
    280 	.pr_usrreqs = &rip_usrreqs,
    281 	.pr_init = icmp_init,
    282 },
    283 {	.pr_type = SOCK_DGRAM,
    284 	.pr_domain = &inetdomain,
    285 	.pr_protocol = IPPROTO_UDP,
    286 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
    287 	.pr_input = udp_input,
    288 	.pr_ctlinput = udp_ctlinput,
    289 	.pr_ctloutput = udp_ctloutput,
    290 	.pr_usrreqs = &udp_usrreqs,
    291 	.pr_init = udp_init,
    292 },
    293 {	.pr_type = SOCK_STREAM,
    294 	.pr_domain = &inetdomain,
    295 	.pr_protocol = IPPROTO_TCP,
    296 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
    297 	.pr_input = tcp_input,
    298 	.pr_ctlinput = tcp_ctlinput,
    299 	.pr_ctloutput = tcp_ctloutput,
    300 	.pr_usrreqs = &tcp_usrreqs,
    301 	.pr_init = tcp_init,
    302 	.pr_fasttimo = tcp_fasttimo,
    303 	.pr_drain = tcp_drainstub,
    304 },
    305 #ifdef DCCP
    306 {	.pr_type = SOCK_CONN_DGRAM,
    307 	.pr_domain = &inetdomain,
    308 	.pr_protocol = IPPROTO_DCCP,
    309 	.pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_ATOMIC|PR_LISTEN|PR_ABRTACPTDIS,
    310 	.pr_input = dccp_input,
    311 	.pr_ctlinput = dccp_ctlinput,
    312 	.pr_ctloutput = dccp_ctloutput,
    313 	.pr_usrreqs = &dccp_usrreqs,
    314 	.pr_init = dccp_init,
    315 },
    316 #endif
    317 #ifdef SCTP
    318 {	.pr_type = SOCK_DGRAM,
    319 	.pr_domain = &inetdomain,
    320 	.pr_protocol = IPPROTO_SCTP,
    321 	.pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
    322 	.pr_input = sctp_input,
    323 	.pr_ctlinput = sctp_ctlinput,
    324 	.pr_ctloutput = sctp_ctloutput,
    325 	.pr_usrreqs = &sctp_usrreqs,
    326 	.pr_init = sctp_init,
    327 	.pr_drain = sctp_drain
    328 },
    329 {	.pr_type = SOCK_SEQPACKET,
    330 	.pr_domain = &inetdomain,
    331 	.pr_protocol = IPPROTO_SCTP,
    332 	.pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
    333 	.pr_input = sctp_input,
    334 	.pr_ctlinput = sctp_ctlinput,
    335 	.pr_ctloutput = sctp_ctloutput,
    336 	.pr_usrreqs = &sctp_usrreqs,
    337 	.pr_drain = sctp_drain
    338 },
    339 {	.pr_type = SOCK_STREAM,
    340 	.pr_domain = &inetdomain,
    341 	.pr_protocol = IPPROTO_SCTP,
    342 	.pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD|PR_LISTEN,
    343 	.pr_input = sctp_input,
    344 	.pr_ctlinput = sctp_ctlinput,
    345 	.pr_ctloutput = sctp_ctloutput,
    346 	.pr_usrreqs = &sctp_usrreqs,
    347 	.pr_drain = sctp_drain
    348 },
    349 #endif /* SCTP */
    350 {	.pr_type = SOCK_RAW,
    351 	.pr_domain = &inetdomain,
    352 	.pr_protocol = IPPROTO_RAW,
    353 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
    354 	.pr_input = rip_input,
    355 	.pr_ctlinput = rip_ctlinput,
    356 	.pr_ctloutput = rip_ctloutput,
    357 	.pr_usrreqs = &rip_usrreqs,
    358 },
    359 #ifdef GATEWAY
    360 {	.pr_domain = &inetdomain,
    361 	.pr_protocol = IPPROTO_IP,
    362 	.pr_slowtimo = ipflow_slowtimo,
    363 	.pr_init = ipflow_poolinit,
    364 },
    365 #endif /* GATEWAY */
    366 #ifdef IPSEC
    367 {	.pr_type = SOCK_RAW,
    368 	.pr_domain = &inetdomain,
    369 	.pr_protocol = IPPROTO_AH,
    370 	.pr_flags = PR_ATOMIC|PR_ADDR,
    371 	.pr_input = ipsec4_common_input,
    372 	.pr_ctlinput = ah4_ctlinput,
    373 },
    374 {	.pr_type = SOCK_RAW,
    375 	.pr_domain = &inetdomain,
    376 	.pr_protocol = IPPROTO_ESP,
    377 	.pr_flags = PR_ATOMIC|PR_ADDR,
    378 	.pr_input = ipsec4_common_input,
    379 	.pr_ctlinput = esp4_ctlinput,
    380 },
    381 {	.pr_type = SOCK_RAW,
    382 	.pr_domain = &inetdomain,
    383 	.pr_protocol = IPPROTO_IPCOMP,
    384 	.pr_flags = PR_ATOMIC|PR_ADDR,
    385 	.pr_input = ipsec4_common_input,
    386 },
    387 #endif /* IPSEC */
    388 {	.pr_type = SOCK_RAW,
    389 	.pr_domain = &inetdomain,
    390 	.pr_protocol = IPPROTO_IPV4,
    391 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    392 	.pr_input = encap4_input,
    393 	.pr_ctlinput = rip_ctlinput,
    394 	.pr_ctloutput = rip_ctloutput,
    395 	.pr_usrreqs = &rip_usrreqs,
    396 	.pr_init = encap_init,
    397 },
    398 #ifdef INET6
    399 {	.pr_type = SOCK_RAW,
    400 	.pr_domain = &inetdomain,
    401 	.pr_protocol = IPPROTO_IPV6,
    402 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    403 	.pr_input = encap4_input,
    404 	.pr_ctlinput = rip_ctlinput,
    405 	.pr_ctloutput = rip_ctloutput,
    406 	.pr_usrreqs = &rip_usrreqs,
    407 	.pr_init = encap_init,
    408 },
    409 #endif /* INET6 */
    410 #if NCARP > 0
    411 {	.pr_type = SOCK_RAW,
    412 	.pr_domain = &inetdomain,
    413 	.pr_protocol = IPPROTO_CARP,
    414 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    415 	.pr_input = carp_proto_input,
    416 	.pr_ctloutput = rip_ctloutput,
    417 	.pr_usrreqs = &rip_usrreqs,
    418 	.pr_init = carp_init,
    419 },
    420 #endif /* NCARP > 0 */
    421 {	.pr_type = SOCK_RAW,
    422 	.pr_domain = &inetdomain,
    423 	.pr_protocol = IPPROTO_L2TP,
    424 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    425 	.pr_input = encap4_input,
    426 	.pr_ctlinput = rip_ctlinput,
    427 	.pr_ctloutput = rip_ctloutput,
    428 	.pr_usrreqs = &rip_usrreqs,	/*XXX*/
    429 	.pr_init = encap_init,
    430 },
    431 #if NPFSYNC > 0
    432 {	.pr_type = SOCK_RAW,
    433 	.pr_domain = &inetdomain,
    434 	.pr_protocol = IPPROTO_PFSYNC,
    435 	.pr_flags	 = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    436 	.pr_input	 = pfsync_input,
    437 	.pr_ctloutput = rip_ctloutput,
    438 	.pr_usrreqs	 = &rip_usrreqs,
    439 },
    440 #endif /* NPFSYNC > 0 */
    441 {	.pr_type = SOCK_RAW,
    442 	.pr_domain = &inetdomain,
    443 	.pr_protocol = IPPROTO_IGMP,
    444 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    445 	.pr_input = igmp_input,
    446 	.pr_ctloutput = rip_ctloutput,
    447 	.pr_ctlinput = rip_ctlinput,
    448 	.pr_usrreqs = &rip_usrreqs,
    449 	.pr_fasttimo = igmp_fasttimo,
    450 	.pr_slowtimo = igmp_slowtimo,
    451 	.pr_init = igmp_init,
    452 },
    453 #ifdef PIM
    454 {	.pr_type = SOCK_RAW,
    455 	.pr_domain = &inetdomain,
    456 	.pr_protocol = IPPROTO_PIM,
    457 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    458 	.pr_input = pim_input,
    459 	.pr_ctloutput = rip_ctloutput,
    460 	.pr_ctlinput = rip_ctlinput,
    461 	.pr_usrreqs = &rip_usrreqs,
    462 },
    463 #endif /* PIM */
    464 /* raw wildcard */
    465 {	.pr_type = SOCK_RAW,
    466 	.pr_domain = &inetdomain,
    467 	.pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
    468 	.pr_input = rip_input,
    469 	.pr_ctloutput = rip_ctloutput,
    470 	.pr_ctlinput = rip_ctlinput,
    471 	.pr_usrreqs = &rip_usrreqs,
    472 	.pr_init = rip_init,
    473 },
    474 };
    475 
    476 const struct sockaddr_in in_any = {
    477 	  .sin_len = sizeof(struct sockaddr_in)
    478 	, .sin_family = AF_INET
    479 	, .sin_port = 0
    480 	, .sin_addr = {.s_addr = 0 /* INADDR_ANY */}
    481 };
    482 
    483 struct domain inetdomain = {
    484 	.dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
    485 	.dom_externalize = NULL, .dom_dispose = NULL,
    486 	.dom_protosw = inetsw,
    487 	.dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)],
    488 	.dom_rtattach = rt_inithead,
    489 	.dom_rtoffset = 32,
    490 	.dom_maxrtkey = sizeof(struct ip_pack4),
    491 	.dom_if_up = in_if_up,
    492 	.dom_if_down = in_if_down,
    493 	.dom_ifattach = in_domifattach,
    494 	.dom_ifdetach = in_domifdetach,
    495 	.dom_if_link_state_change = in_if_link_state_change,
    496 	.dom_ifqueues = { NULL, NULL },
    497 	.dom_link = { NULL },
    498 	.dom_mowner = MOWNER_INIT("",""),
    499 	.dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr),
    500 	.dom_sa_cmplen = sizeof(struct in_addr),
    501 	.dom_sa_any = (const struct sockaddr *)&in_any,
    502 	.dom_sockaddr_const_addr = sockaddr_in_const_addr,
    503 	.dom_sockaddr_addr = sockaddr_in_addr,
    504 };
    505 
    506 u_char	ip_protox[IPPROTO_MAX];
    507 
    508 static void
    509 sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp)
    510 {
    511 	socklen_t slen;
    512 
    513 	if (slenp == NULL)
    514 		return;
    515 
    516 	slen = sockaddr_getlen(sa);
    517 	*slenp = (socklen_t)MIN(sizeof(struct in_addr),
    518 	    slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr)));
    519 }
    520 
    521 const void *
    522 sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp)
    523 {
    524 	const struct sockaddr_in *sin;
    525 
    526 	sockaddr_in_addrlen(sa, slenp);
    527 	sin = (const struct sockaddr_in *)sa;
    528 	return &sin->sin_addr;
    529 }
    530 
    531 void *
    532 sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp)
    533 {
    534 	struct sockaddr_in *sin;
    535 
    536 	sockaddr_in_addrlen(sa, slenp);
    537 	sin = (struct sockaddr_in *)sa;
    538 	return &sin->sin_addr;
    539 }
    540 
    541 int
    542 sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    543 {
    544 	uint_fast8_t len;
    545 	const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr),
    546 			   addrend = addrofs + sizeof(struct in_addr);
    547 	int rc;
    548 	const struct sockaddr_in *sin1, *sin2;
    549 
    550 	sin1 = satocsin(sa1);
    551 	sin2 = satocsin(sa2);
    552 
    553 	len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len));
    554 
    555 	if (len > addrofs &&
    556 	     (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr,
    557 	                  len - addrofs)) != 0)
    558 		return rc;
    559 
    560 	return sin1->sin_len - sin2->sin_len;
    561 }
    562