in_proto.c revision 1.125 1 1.125 ozaki /* $NetBSD: in_proto.c,v 1.125 2017/09/27 10:05:04 ozaki-r Exp $ */
2 1.32 itojun
3 1.32 itojun /*
4 1.32 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.32 itojun * All rights reserved.
6 1.56 itojun *
7 1.32 itojun * Redistribution and use in source and binary forms, with or without
8 1.32 itojun * modification, are permitted provided that the following conditions
9 1.32 itojun * are met:
10 1.32 itojun * 1. Redistributions of source code must retain the above copyright
11 1.32 itojun * notice, this list of conditions and the following disclaimer.
12 1.32 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.32 itojun * notice, this list of conditions and the following disclaimer in the
14 1.32 itojun * documentation and/or other materials provided with the distribution.
15 1.32 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.32 itojun * may be used to endorse or promote products derived from this software
17 1.32 itojun * without specific prior written permission.
18 1.56 itojun *
19 1.32 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.32 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.32 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.32 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.32 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.32 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.32 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.32 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.32 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.32 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.32 itojun * SUCH DAMAGE.
30 1.32 itojun */
31 1.9 cgd
32 1.1 cgd /*
33 1.8 mycroft * Copyright (c) 1982, 1986, 1993
34 1.8 mycroft * The Regents of the University of California. All rights reserved.
35 1.1 cgd *
36 1.1 cgd * Redistribution and use in source and binary forms, with or without
37 1.1 cgd * modification, are permitted provided that the following conditions
38 1.1 cgd * are met:
39 1.1 cgd * 1. Redistributions of source code must retain the above copyright
40 1.1 cgd * notice, this list of conditions and the following disclaimer.
41 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 cgd * notice, this list of conditions and the following disclaimer in the
43 1.1 cgd * documentation and/or other materials provided with the distribution.
44 1.59 agc * 3. Neither the name of the University nor the names of its contributors
45 1.1 cgd * may be used to endorse or promote products derived from this software
46 1.1 cgd * without specific prior written permission.
47 1.1 cgd *
48 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 cgd * SUCH DAMAGE.
59 1.1 cgd *
60 1.18 thorpej * @(#)in_proto.c 8.2 (Berkeley) 2/9/95
61 1.1 cgd */
62 1.51 lukem
63 1.51 lukem #include <sys/cdefs.h>
64 1.125 ozaki __KERNEL_RCSID(0, "$NetBSD: in_proto.c,v 1.125 2017/09/27 10:05:04 ozaki-r Exp $");
65 1.19 scottr
66 1.113 pooka #ifdef _KERNEL_OPT
67 1.19 scottr #include "opt_mrouting.h"
68 1.32 itojun #include "opt_inet.h"
69 1.33 thorpej #include "opt_ipsec.h"
70 1.65 manu #include "opt_pim.h"
71 1.97 pooka #include "opt_gateway.h"
72 1.111 rjs #include "opt_dccp.h"
73 1.115 rjs #include "opt_sctp.h"
74 1.108 christos #include "opt_compat_netbsd.h"
75 1.125 ozaki #include "opt_net_mpsafe.h"
76 1.113 pooka #endif
77 1.1 cgd
78 1.5 mycroft #include <sys/param.h>
79 1.5 mycroft #include <sys/socket.h>
80 1.5 mycroft #include <sys/protosw.h>
81 1.5 mycroft #include <sys/domain.h>
82 1.5 mycroft #include <sys/mbuf.h>
83 1.1 cgd
84 1.8 mycroft #include <net/if.h>
85 1.8 mycroft
86 1.5 mycroft #include <netinet/in.h>
87 1.5 mycroft #include <netinet/in_systm.h>
88 1.110 rmind #include <netinet/in_var.h>
89 1.8 mycroft #include <netinet/ip.h>
90 1.8 mycroft #include <netinet/ip_var.h>
91 1.8 mycroft #include <netinet/ip_icmp.h>
92 1.78 dyoung #include <netinet/in_ifattach.h>
93 1.8 mycroft #include <netinet/in_pcb.h>
94 1.69 yamt #include <netinet/in_proto.h>
95 1.32 itojun
96 1.32 itojun #ifdef INET6
97 1.32 itojun #ifndef INET
98 1.32 itojun #include <netinet/in.h>
99 1.32 itojun #endif
100 1.32 itojun #include <netinet/ip6.h>
101 1.32 itojun #endif
102 1.32 itojun
103 1.8 mycroft #include <netinet/igmp_var.h>
104 1.65 manu #ifdef PIM
105 1.65 manu #include <netinet/pim_var.h>
106 1.65 manu #endif
107 1.8 mycroft #include <netinet/tcp.h>
108 1.8 mycroft #include <netinet/tcp_fsm.h>
109 1.8 mycroft #include <netinet/tcp_seq.h>
110 1.8 mycroft #include <netinet/tcp_timer.h>
111 1.8 mycroft #include <netinet/tcp_var.h>
112 1.8 mycroft #include <netinet/tcpip.h>
113 1.8 mycroft #include <netinet/tcp_debug.h>
114 1.8 mycroft #include <netinet/udp.h>
115 1.8 mycroft #include <netinet/udp_var.h>
116 1.39 itojun #include <netinet/ip_encap.h>
117 1.79 rpaulo
118 1.111 rjs #ifdef DCCP
119 1.111 rjs #include <netinet/dccp.h>
120 1.111 rjs #include <netinet/dccp_var.h>
121 1.111 rjs #endif
122 1.111 rjs
123 1.115 rjs #ifdef SCTP
124 1.115 rjs #include <netinet/sctp.h>
125 1.115 rjs #include <netinet/sctp_var.h>
126 1.115 rjs #endif
127 1.115 rjs
128 1.1 cgd /*
129 1.1 cgd * TCP/IP protocol family: IP, ICMP, UDP, TCP.
130 1.1 cgd */
131 1.1 cgd
132 1.106 christos #ifdef IPSEC
133 1.61 jonathan #include <netipsec/ipsec.h>
134 1.61 jonathan #include <netipsec/key.h>
135 1.106 christos #endif /* IPSEC */
136 1.61 jonathan
137 1.72 liamjfoy #include "carp.h"
138 1.72 liamjfoy #if NCARP > 0
139 1.72 liamjfoy #include <netinet/ip_carp.h>
140 1.72 liamjfoy #endif
141 1.72 liamjfoy
142 1.98 degroote #include "pfsync.h"
143 1.98 degroote #if NPFSYNC > 0
144 1.98 degroote #include <net/pfvar.h>
145 1.98 degroote #include <net/if_pfsync.h>
146 1.98 degroote #endif
147 1.98 degroote
148 1.79 rpaulo #include "etherip.h"
149 1.79 rpaulo #if NETHERIP > 0
150 1.79 rpaulo #include <netinet/ip_etherip.h>
151 1.79 rpaulo #endif
152 1.67 kim
153 1.66 matt DOMAIN_DEFINE(inetdomain); /* forward declare and add to link set */
154 1.66 matt
155 1.94 ad /* Wrappers to acquire kernel_lock. */
156 1.94 ad
157 1.94 ad PR_WRAP_CTLINPUT(rip_ctlinput)
158 1.94 ad PR_WRAP_CTLINPUT(udp_ctlinput)
159 1.94 ad PR_WRAP_CTLINPUT(tcp_ctlinput)
160 1.94 ad
161 1.94 ad #define rip_ctlinput rip_ctlinput_wrapper
162 1.94 ad #define udp_ctlinput udp_ctlinput_wrapper
163 1.94 ad #define tcp_ctlinput tcp_ctlinput_wrapper
164 1.94 ad
165 1.94 ad PR_WRAP_CTLOUTPUT(rip_ctloutput)
166 1.94 ad PR_WRAP_CTLOUTPUT(udp_ctloutput)
167 1.94 ad PR_WRAP_CTLOUTPUT(tcp_ctloutput)
168 1.94 ad
169 1.94 ad #define rip_ctloutput rip_ctloutput_wrapper
170 1.94 ad #define udp_ctloutput udp_ctloutput_wrapper
171 1.94 ad #define tcp_ctloutput tcp_ctloutput_wrapper
172 1.94 ad
173 1.111 rjs #ifdef DCCP
174 1.111 rjs PR_WRAP_CTLINPUT(dccp_ctlinput)
175 1.111 rjs PR_WRAP_CTLOUTPUT(dccp_ctloutput)
176 1.111 rjs
177 1.111 rjs #define dccp_ctlinput dccp_ctlinput_wrapper
178 1.111 rjs #define dccp_ctloutput dccp_ctloutput_wrapper
179 1.111 rjs #endif
180 1.111 rjs
181 1.115 rjs #ifdef SCTP
182 1.115 rjs PR_WRAP_CTLINPUT(sctp_ctlinput)
183 1.115 rjs PR_WRAP_CTLOUTPUT(sctp_ctloutput)
184 1.115 rjs
185 1.115 rjs #define sctp_ctlinput sctp_ctlinput_wrapper
186 1.115 rjs #define sctp_ctloutput sctp_ctloutput_wrapper
187 1.115 rjs #endif
188 1.115 rjs
189 1.125 ozaki #ifdef NET_MPSAFE
190 1.125 ozaki PR_WRAP_INPUT(udp_input)
191 1.125 ozaki PR_WRAP_INPUT(tcp_input)
192 1.125 ozaki #ifdef DCCP
193 1.125 ozaki PR_WRAP_INPUT(dccp_input)
194 1.125 ozaki #endif
195 1.125 ozaki #ifdef SCTP
196 1.125 ozaki PR_WRAP_INPUT(sctp_input)
197 1.125 ozaki #endif
198 1.125 ozaki PR_WRAP_INPUT(rip_input)
199 1.125 ozaki #if NETHERIP > 0
200 1.125 ozaki PR_WRAP_INPUT(ip_etherip_input)
201 1.125 ozaki #endif
202 1.125 ozaki #if NPFSYNC > 0
203 1.125 ozaki PR_WRAP_INPUT(pfsync_input)
204 1.125 ozaki #endif
205 1.125 ozaki PR_WRAP_INPUT(igmp_input)
206 1.125 ozaki #ifdef PIM
207 1.125 ozaki PR_WRAP_INPUT(pim_input)
208 1.125 ozaki #endif
209 1.125 ozaki
210 1.125 ozaki #define udp_input udp_input_wrapper
211 1.125 ozaki #define tcp_input tcp_input_wrapper
212 1.125 ozaki #define dccp_input dccp_input_wrapper
213 1.125 ozaki #define sctp_input sctp_input_wrapper
214 1.125 ozaki #define rip_input rip_input_wrapper
215 1.125 ozaki #define ip_etherip_input ip_etherip_input_wrapper
216 1.125 ozaki #define pfsync_input pfsync_input_wrapper
217 1.125 ozaki #define igmp_input igmp_input_wrapper
218 1.125 ozaki #define pim_input pim_input_wrapper
219 1.125 ozaki #endif
220 1.125 ozaki
221 1.106 christos #if defined(IPSEC)
222 1.123 ozaki
223 1.123 ozaki #ifdef IPSEC_RUMPKERNEL
224 1.123 ozaki /*
225 1.123 ozaki * .pr_input = ipsec4_common_input won't be resolved on loading
226 1.123 ozaki * the ipsec shared library. We need a wrapper anyway.
227 1.123 ozaki */
228 1.123 ozaki static void
229 1.123 ozaki ipsec4_common_input_wrapper(struct mbuf *m, ...)
230 1.123 ozaki {
231 1.123 ozaki
232 1.123 ozaki if (ipsec_enabled) {
233 1.123 ozaki int off, nxt;
234 1.123 ozaki va_list args;
235 1.123 ozaki /* XXX just passing args to ipsec4_common_input doesn't work */
236 1.123 ozaki va_start(args, m);
237 1.123 ozaki off = va_arg(args, int);
238 1.123 ozaki nxt = va_arg(args, int);
239 1.123 ozaki va_end(args);
240 1.123 ozaki ipsec4_common_input(m, off, nxt);
241 1.123 ozaki } else {
242 1.123 ozaki m_freem(m);
243 1.123 ozaki }
244 1.123 ozaki }
245 1.123 ozaki #define ipsec4_common_input ipsec4_common_input_wrapper
246 1.123 ozaki
247 1.123 ozaki /* The ctlinput functions may not be loaded */
248 1.123 ozaki #define IPSEC_WRAP_CTLINPUT(name) \
249 1.123 ozaki static void * \
250 1.123 ozaki name##_wrapper(int a, const struct sockaddr *b, void *c)\
251 1.123 ozaki { \
252 1.123 ozaki void *rv; \
253 1.123 ozaki KERNEL_LOCK(1, NULL); \
254 1.123 ozaki if (ipsec_enabled) \
255 1.123 ozaki rv = name(a, b, c); \
256 1.123 ozaki else \
257 1.123 ozaki rv = NULL; \
258 1.123 ozaki KERNEL_UNLOCK_ONE(NULL); \
259 1.123 ozaki return rv; \
260 1.123 ozaki }
261 1.123 ozaki IPSEC_WRAP_CTLINPUT(ah4_ctlinput)
262 1.123 ozaki IPSEC_WRAP_CTLINPUT(esp4_ctlinput)
263 1.123 ozaki
264 1.123 ozaki #else /* !IPSEC_RUMPKERNEL */
265 1.123 ozaki
266 1.94 ad PR_WRAP_CTLINPUT(ah4_ctlinput)
267 1.123 ozaki PR_WRAP_CTLINPUT(esp4_ctlinput)
268 1.123 ozaki
269 1.123 ozaki #endif /* !IPSEC_RUMPKERNEL */
270 1.94 ad
271 1.94 ad #define ah4_ctlinput ah4_ctlinput_wrapper
272 1.123 ozaki #define esp4_ctlinput esp4_ctlinput_wrapper
273 1.94 ad
274 1.123 ozaki #endif /* IPSEC */
275 1.94 ad
276 1.118 riastrad const struct protosw inetsw[] = {
277 1.118 riastrad { .pr_domain = &inetdomain,
278 1.81 liamjfoy .pr_init = ip_init,
279 1.101 dyoung .pr_fasttimo = ip_fasttimo,
280 1.81 liamjfoy .pr_slowtimo = ip_slowtimo,
281 1.101 dyoung .pr_drain = ip_drainstub,
282 1.81 liamjfoy },
283 1.121 ozaki { .pr_type = SOCK_RAW,
284 1.121 ozaki .pr_domain = &inetdomain,
285 1.121 ozaki .pr_protocol = IPPROTO_ICMP,
286 1.121 ozaki .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
287 1.121 ozaki .pr_input = icmp_input,
288 1.121 ozaki .pr_ctlinput = rip_ctlinput,
289 1.121 ozaki .pr_ctloutput = rip_ctloutput,
290 1.121 ozaki .pr_usrreqs = &rip_usrreqs,
291 1.121 ozaki .pr_init = icmp_init,
292 1.121 ozaki },
293 1.118 riastrad { .pr_type = SOCK_DGRAM,
294 1.81 liamjfoy .pr_domain = &inetdomain,
295 1.81 liamjfoy .pr_protocol = IPPROTO_UDP,
296 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
297 1.118 riastrad .pr_input = udp_input,
298 1.81 liamjfoy .pr_ctlinput = udp_ctlinput,
299 1.81 liamjfoy .pr_ctloutput = udp_ctloutput,
300 1.109 rmind .pr_usrreqs = &udp_usrreqs,
301 1.81 liamjfoy .pr_init = udp_init,
302 1.81 liamjfoy },
303 1.118 riastrad { .pr_type = SOCK_STREAM,
304 1.81 liamjfoy .pr_domain = &inetdomain,
305 1.81 liamjfoy .pr_protocol = IPPROTO_TCP,
306 1.81 liamjfoy .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
307 1.118 riastrad .pr_input = tcp_input,
308 1.81 liamjfoy .pr_ctlinput = tcp_ctlinput,
309 1.81 liamjfoy .pr_ctloutput = tcp_ctloutput,
310 1.109 rmind .pr_usrreqs = &tcp_usrreqs,
311 1.81 liamjfoy .pr_init = tcp_init,
312 1.101 dyoung .pr_fasttimo = tcp_fasttimo,
313 1.101 dyoung .pr_drain = tcp_drainstub,
314 1.81 liamjfoy },
315 1.111 rjs #ifdef DCCP
316 1.118 riastrad { .pr_type = SOCK_CONN_DGRAM,
317 1.111 rjs .pr_domain = &inetdomain,
318 1.111 rjs .pr_protocol = IPPROTO_DCCP,
319 1.111 rjs .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_ATOMIC|PR_LISTEN|PR_ABRTACPTDIS,
320 1.118 riastrad .pr_input = dccp_input,
321 1.111 rjs .pr_ctlinput = dccp_ctlinput,
322 1.111 rjs .pr_ctloutput = dccp_ctloutput,
323 1.111 rjs .pr_usrreqs = &dccp_usrreqs,
324 1.111 rjs .pr_init = dccp_init,
325 1.111 rjs },
326 1.111 rjs #endif
327 1.115 rjs #ifdef SCTP
328 1.118 riastrad { .pr_type = SOCK_DGRAM,
329 1.115 rjs .pr_domain = &inetdomain,
330 1.115 rjs .pr_protocol = IPPROTO_SCTP,
331 1.115 rjs .pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
332 1.118 riastrad .pr_input = sctp_input,
333 1.115 rjs .pr_ctlinput = sctp_ctlinput,
334 1.115 rjs .pr_ctloutput = sctp_ctloutput,
335 1.115 rjs .pr_usrreqs = &sctp_usrreqs,
336 1.115 rjs .pr_init = sctp_init,
337 1.115 rjs .pr_drain = sctp_drain
338 1.115 rjs },
339 1.118 riastrad { .pr_type = SOCK_SEQPACKET,
340 1.115 rjs .pr_domain = &inetdomain,
341 1.115 rjs .pr_protocol = IPPROTO_SCTP,
342 1.115 rjs .pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
343 1.118 riastrad .pr_input = sctp_input,
344 1.115 rjs .pr_ctlinput = sctp_ctlinput,
345 1.115 rjs .pr_ctloutput = sctp_ctloutput,
346 1.115 rjs .pr_usrreqs = &sctp_usrreqs,
347 1.115 rjs .pr_drain = sctp_drain
348 1.115 rjs },
349 1.118 riastrad { .pr_type = SOCK_STREAM,
350 1.115 rjs .pr_domain = &inetdomain,
351 1.115 rjs .pr_protocol = IPPROTO_SCTP,
352 1.115 rjs .pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD|PR_LISTEN,
353 1.118 riastrad .pr_input = sctp_input,
354 1.115 rjs .pr_ctlinput = sctp_ctlinput,
355 1.115 rjs .pr_ctloutput = sctp_ctloutput,
356 1.115 rjs .pr_usrreqs = &sctp_usrreqs,
357 1.115 rjs .pr_drain = sctp_drain
358 1.115 rjs },
359 1.115 rjs #endif /* SCTP */
360 1.118 riastrad { .pr_type = SOCK_RAW,
361 1.81 liamjfoy .pr_domain = &inetdomain,
362 1.81 liamjfoy .pr_protocol = IPPROTO_RAW,
363 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
364 1.118 riastrad .pr_input = rip_input,
365 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
366 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
367 1.109 rmind .pr_usrreqs = &rip_usrreqs,
368 1.81 liamjfoy },
369 1.83 liamjfoy #ifdef GATEWAY
370 1.118 riastrad { .pr_domain = &inetdomain,
371 1.83 liamjfoy .pr_protocol = IPPROTO_IP,
372 1.83 liamjfoy .pr_slowtimo = ipflow_slowtimo,
373 1.96 pooka .pr_init = ipflow_poolinit,
374 1.83 liamjfoy },
375 1.83 liamjfoy #endif /* GATEWAY */
376 1.106 christos #ifdef IPSEC
377 1.118 riastrad { .pr_type = SOCK_RAW,
378 1.81 liamjfoy .pr_domain = &inetdomain,
379 1.81 liamjfoy .pr_protocol = IPPROTO_AH,
380 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR,
381 1.118 riastrad .pr_input = ipsec4_common_input,
382 1.81 liamjfoy .pr_ctlinput = ah4_ctlinput,
383 1.81 liamjfoy },
384 1.118 riastrad { .pr_type = SOCK_RAW,
385 1.81 liamjfoy .pr_domain = &inetdomain,
386 1.81 liamjfoy .pr_protocol = IPPROTO_ESP,
387 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR,
388 1.118 riastrad .pr_input = ipsec4_common_input,
389 1.81 liamjfoy .pr_ctlinput = esp4_ctlinput,
390 1.81 liamjfoy },
391 1.118 riastrad { .pr_type = SOCK_RAW,
392 1.81 liamjfoy .pr_domain = &inetdomain,
393 1.81 liamjfoy .pr_protocol = IPPROTO_IPCOMP,
394 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR,
395 1.118 riastrad .pr_input = ipsec4_common_input,
396 1.61 jonathan },
397 1.106 christos #endif /* IPSEC */
398 1.118 riastrad { .pr_type = SOCK_RAW,
399 1.81 liamjfoy .pr_domain = &inetdomain,
400 1.81 liamjfoy .pr_protocol = IPPROTO_IPV4,
401 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
402 1.118 riastrad .pr_input = encap4_input,
403 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
404 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
405 1.109 rmind .pr_usrreqs = &rip_usrreqs,
406 1.81 liamjfoy .pr_init = encap_init,
407 1.32 itojun },
408 1.32 itojun #ifdef INET6
409 1.118 riastrad { .pr_type = SOCK_RAW,
410 1.81 liamjfoy .pr_domain = &inetdomain,
411 1.81 liamjfoy .pr_protocol = IPPROTO_IPV6,
412 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
413 1.118 riastrad .pr_input = encap4_input,
414 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
415 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
416 1.109 rmind .pr_usrreqs = &rip_usrreqs,
417 1.81 liamjfoy .pr_init = encap_init,
418 1.32 itojun },
419 1.32 itojun #endif /* INET6 */
420 1.79 rpaulo #if NETHERIP > 0
421 1.118 riastrad { .pr_type = SOCK_RAW,
422 1.81 liamjfoy .pr_domain = &inetdomain,
423 1.81 liamjfoy .pr_protocol = IPPROTO_ETHERIP,
424 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
425 1.118 riastrad .pr_input = ip_etherip_input,
426 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
427 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
428 1.109 rmind .pr_usrreqs = &rip_usrreqs,
429 1.67 kim },
430 1.81 liamjfoy #endif /* NETHERIP > 0 */
431 1.72 liamjfoy #if NCARP > 0
432 1.118 riastrad { .pr_type = SOCK_RAW,
433 1.81 liamjfoy .pr_domain = &inetdomain,
434 1.81 liamjfoy .pr_protocol = IPPROTO_CARP,
435 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR,
436 1.118 riastrad .pr_input = carp_proto_input,
437 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
438 1.109 rmind .pr_usrreqs = &rip_usrreqs,
439 1.99 pooka .pr_init = carp_init,
440 1.72 liamjfoy },
441 1.81 liamjfoy #endif /* NCARP > 0 */
442 1.122 knakahar { .pr_type = SOCK_RAW,
443 1.122 knakahar .pr_domain = &inetdomain,
444 1.122 knakahar .pr_protocol = IPPROTO_L2TP,
445 1.122 knakahar .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
446 1.122 knakahar .pr_input = encap4_input,
447 1.122 knakahar .pr_ctlinput = rip_ctlinput,
448 1.122 knakahar .pr_ctloutput = rip_ctloutput,
449 1.122 knakahar .pr_usrreqs = &rip_usrreqs, /*XXX*/
450 1.122 knakahar .pr_init = encap_init,
451 1.122 knakahar },
452 1.98 degroote #if NPFSYNC > 0
453 1.118 riastrad { .pr_type = SOCK_RAW,
454 1.98 degroote .pr_domain = &inetdomain,
455 1.98 degroote .pr_protocol = IPPROTO_PFSYNC,
456 1.98 degroote .pr_flags = PR_ATOMIC|PR_ADDR,
457 1.118 riastrad .pr_input = pfsync_input,
458 1.98 degroote .pr_ctloutput = rip_ctloutput,
459 1.109 rmind .pr_usrreqs = &rip_usrreqs,
460 1.98 degroote },
461 1.98 degroote #endif /* NPFSYNC > 0 */
462 1.118 riastrad { .pr_type = SOCK_RAW,
463 1.81 liamjfoy .pr_domain = &inetdomain,
464 1.81 liamjfoy .pr_protocol = IPPROTO_IGMP,
465 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
466 1.118 riastrad .pr_input = igmp_input,
467 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
468 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
469 1.109 rmind .pr_usrreqs = &rip_usrreqs,
470 1.81 liamjfoy .pr_fasttimo = igmp_fasttimo,
471 1.81 liamjfoy .pr_slowtimo = igmp_slowtimo,
472 1.92 thorpej .pr_init = igmp_init,
473 1.1 cgd },
474 1.65 manu #ifdef PIM
475 1.118 riastrad { .pr_type = SOCK_RAW,
476 1.81 liamjfoy .pr_domain = &inetdomain,
477 1.81 liamjfoy .pr_protocol = IPPROTO_PIM,
478 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
479 1.118 riastrad .pr_input = pim_input,
480 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
481 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
482 1.109 rmind .pr_usrreqs = &rip_usrreqs,
483 1.65 manu },
484 1.65 manu #endif /* PIM */
485 1.11 mycroft /* raw wildcard */
486 1.118 riastrad { .pr_type = SOCK_RAW,
487 1.81 liamjfoy .pr_domain = &inetdomain,
488 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
489 1.118 riastrad .pr_input = rip_input,
490 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
491 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
492 1.109 rmind .pr_usrreqs = &rip_usrreqs,
493 1.81 liamjfoy .pr_init = rip_init,
494 1.1 cgd },
495 1.1 cgd };
496 1.1 cgd
497 1.89 dyoung const struct sockaddr_in in_any = {
498 1.89 dyoung .sin_len = sizeof(struct sockaddr_in)
499 1.89 dyoung , .sin_family = AF_INET
500 1.89 dyoung , .sin_port = 0
501 1.90 dyoung , .sin_addr = {.s_addr = 0 /* INADDR_ANY */}
502 1.89 dyoung };
503 1.89 dyoung
504 1.73 matt struct domain inetdomain = {
505 1.80 dyoung .dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
506 1.80 dyoung .dom_externalize = NULL, .dom_dispose = NULL,
507 1.118 riastrad .dom_protosw = inetsw,
508 1.118 riastrad .dom_protoswNPROTOSW = &inetsw[__arraycount(inetsw)],
509 1.100 dyoung .dom_rtattach = rt_inithead,
510 1.95 pooka .dom_rtoffset = 32,
511 1.95 pooka .dom_maxrtkey = sizeof(struct ip_pack4),
512 1.112 roy .dom_if_up = in_if_up,
513 1.112 roy .dom_if_down = in_if_down,
514 1.80 dyoung .dom_ifattach = in_domifattach,
515 1.80 dyoung .dom_ifdetach = in_domifdetach,
516 1.112 roy .dom_if_link_state_change = in_if_link_state_change,
517 1.110 rmind .dom_ifqueues = { NULL, NULL },
518 1.80 dyoung .dom_link = { NULL },
519 1.80 dyoung .dom_mowner = MOWNER_INIT("",""),
520 1.86 dyoung .dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr),
521 1.86 dyoung .dom_sa_cmplen = sizeof(struct in_addr),
522 1.89 dyoung .dom_sa_any = (const struct sockaddr *)&in_any,
523 1.89 dyoung .dom_sockaddr_const_addr = sockaddr_in_const_addr,
524 1.89 dyoung .dom_sockaddr_addr = sockaddr_in_addr,
525 1.73 matt };
526 1.38 darrenr
527 1.38 darrenr u_char ip_protox[IPPROTO_MAX];
528 1.36 thorpej
529 1.40 itojun int icmperrppslim = 100; /* 100pps */
530 1.84 dyoung
531 1.89 dyoung static void
532 1.89 dyoung sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp)
533 1.89 dyoung {
534 1.89 dyoung socklen_t slen;
535 1.89 dyoung
536 1.89 dyoung if (slenp == NULL)
537 1.89 dyoung return;
538 1.89 dyoung
539 1.89 dyoung slen = sockaddr_getlen(sa);
540 1.89 dyoung *slenp = (socklen_t)MIN(sizeof(struct in_addr),
541 1.89 dyoung slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr)));
542 1.89 dyoung }
543 1.89 dyoung
544 1.89 dyoung const void *
545 1.89 dyoung sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp)
546 1.89 dyoung {
547 1.89 dyoung const struct sockaddr_in *sin;
548 1.89 dyoung
549 1.89 dyoung sockaddr_in_addrlen(sa, slenp);
550 1.89 dyoung sin = (const struct sockaddr_in *)sa;
551 1.89 dyoung return &sin->sin_addr;
552 1.89 dyoung }
553 1.89 dyoung
554 1.89 dyoung void *
555 1.89 dyoung sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp)
556 1.89 dyoung {
557 1.89 dyoung struct sockaddr_in *sin;
558 1.89 dyoung
559 1.89 dyoung sockaddr_in_addrlen(sa, slenp);
560 1.89 dyoung sin = (struct sockaddr_in *)sa;
561 1.89 dyoung return &sin->sin_addr;
562 1.89 dyoung }
563 1.89 dyoung
564 1.84 dyoung int
565 1.84 dyoung sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
566 1.84 dyoung {
567 1.84 dyoung uint_fast8_t len;
568 1.84 dyoung const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr),
569 1.84 dyoung addrend = addrofs + sizeof(struct in_addr);
570 1.84 dyoung int rc;
571 1.84 dyoung const struct sockaddr_in *sin1, *sin2;
572 1.84 dyoung
573 1.84 dyoung sin1 = satocsin(sa1);
574 1.84 dyoung sin2 = satocsin(sa2);
575 1.84 dyoung
576 1.84 dyoung len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len));
577 1.84 dyoung
578 1.84 dyoung if (len > addrofs &&
579 1.84 dyoung (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr,
580 1.84 dyoung len - addrofs)) != 0)
581 1.84 dyoung return rc;
582 1.84 dyoung
583 1.84 dyoung return sin1->sin_len - sin2->sin_len;
584 1.84 dyoung }
585