in_proto.c revision 1.117 1 1.117 riastrad /* $NetBSD: in_proto.c,v 1.117 2016/01/21 15:27:48 riastradh 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.117 riastrad __KERNEL_RCSID(0, "$NetBSD: in_proto.c,v 1.117 2016/01/21 15:27:48 riastradh 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.113 pooka #endif
76 1.1 cgd
77 1.5 mycroft #include <sys/param.h>
78 1.5 mycroft #include <sys/socket.h>
79 1.5 mycroft #include <sys/protosw.h>
80 1.5 mycroft #include <sys/domain.h>
81 1.5 mycroft #include <sys/mbuf.h>
82 1.1 cgd
83 1.8 mycroft #include <net/if.h>
84 1.8 mycroft #include <net/radix.h>
85 1.8 mycroft #include <net/route.h>
86 1.8 mycroft
87 1.5 mycroft #include <netinet/in.h>
88 1.5 mycroft #include <netinet/in_systm.h>
89 1.110 rmind #include <netinet/in_var.h>
90 1.8 mycroft #include <netinet/ip.h>
91 1.8 mycroft #include <netinet/ip_var.h>
92 1.8 mycroft #include <netinet/ip_icmp.h>
93 1.78 dyoung #include <netinet/in_ifattach.h>
94 1.8 mycroft #include <netinet/in_pcb.h>
95 1.69 yamt #include <netinet/in_proto.h>
96 1.32 itojun
97 1.32 itojun #ifdef INET6
98 1.32 itojun #ifndef INET
99 1.32 itojun #include <netinet/in.h>
100 1.32 itojun #endif
101 1.32 itojun #include <netinet/ip6.h>
102 1.32 itojun #endif
103 1.32 itojun
104 1.8 mycroft #include <netinet/igmp_var.h>
105 1.65 manu #ifdef PIM
106 1.65 manu #include <netinet/pim_var.h>
107 1.65 manu #endif
108 1.8 mycroft #include <netinet/tcp.h>
109 1.8 mycroft #include <netinet/tcp_fsm.h>
110 1.8 mycroft #include <netinet/tcp_seq.h>
111 1.8 mycroft #include <netinet/tcp_timer.h>
112 1.8 mycroft #include <netinet/tcp_var.h>
113 1.8 mycroft #include <netinet/tcpip.h>
114 1.8 mycroft #include <netinet/tcp_debug.h>
115 1.8 mycroft #include <netinet/udp.h>
116 1.8 mycroft #include <netinet/udp_var.h>
117 1.39 itojun #include <netinet/ip_encap.h>
118 1.79 rpaulo
119 1.111 rjs #ifdef DCCP
120 1.111 rjs #include <netinet/dccp.h>
121 1.111 rjs #include <netinet/dccp_var.h>
122 1.111 rjs #endif
123 1.111 rjs
124 1.115 rjs #ifdef SCTP
125 1.115 rjs #include <netinet/sctp.h>
126 1.115 rjs #include <netinet/sctp_var.h>
127 1.115 rjs #endif
128 1.115 rjs
129 1.1 cgd /*
130 1.1 cgd * TCP/IP protocol family: IP, ICMP, UDP, TCP.
131 1.1 cgd */
132 1.1 cgd
133 1.106 christos #ifdef IPSEC
134 1.61 jonathan #include <netipsec/ipsec.h>
135 1.61 jonathan #include <netipsec/key.h>
136 1.106 christos #endif /* IPSEC */
137 1.61 jonathan
138 1.72 liamjfoy #include "carp.h"
139 1.72 liamjfoy #if NCARP > 0
140 1.72 liamjfoy #include <netinet/ip_carp.h>
141 1.72 liamjfoy #endif
142 1.72 liamjfoy
143 1.98 degroote #include "pfsync.h"
144 1.98 degroote #if NPFSYNC > 0
145 1.98 degroote #include <net/pfvar.h>
146 1.98 degroote #include <net/if_pfsync.h>
147 1.98 degroote #endif
148 1.98 degroote
149 1.79 rpaulo #include "etherip.h"
150 1.79 rpaulo #if NETHERIP > 0
151 1.79 rpaulo #include <netinet/ip_etherip.h>
152 1.79 rpaulo #endif
153 1.67 kim
154 1.66 matt DOMAIN_DEFINE(inetdomain); /* forward declare and add to link set */
155 1.66 matt
156 1.94 ad /* Wrappers to acquire kernel_lock. */
157 1.94 ad
158 1.94 ad PR_WRAP_CTLINPUT(rip_ctlinput)
159 1.94 ad PR_WRAP_CTLINPUT(udp_ctlinput)
160 1.94 ad PR_WRAP_CTLINPUT(tcp_ctlinput)
161 1.94 ad
162 1.94 ad #define rip_ctlinput rip_ctlinput_wrapper
163 1.94 ad #define udp_ctlinput udp_ctlinput_wrapper
164 1.94 ad #define tcp_ctlinput tcp_ctlinput_wrapper
165 1.94 ad
166 1.94 ad PR_WRAP_CTLOUTPUT(rip_ctloutput)
167 1.94 ad PR_WRAP_CTLOUTPUT(udp_ctloutput)
168 1.94 ad PR_WRAP_CTLOUTPUT(tcp_ctloutput)
169 1.94 ad
170 1.94 ad #define rip_ctloutput rip_ctloutput_wrapper
171 1.94 ad #define udp_ctloutput udp_ctloutput_wrapper
172 1.94 ad #define tcp_ctloutput tcp_ctloutput_wrapper
173 1.94 ad
174 1.111 rjs #ifdef DCCP
175 1.111 rjs PR_WRAP_CTLINPUT(dccp_ctlinput)
176 1.111 rjs PR_WRAP_CTLOUTPUT(dccp_ctloutput)
177 1.111 rjs
178 1.111 rjs #define dccp_ctlinput dccp_ctlinput_wrapper
179 1.111 rjs #define dccp_ctloutput dccp_ctloutput_wrapper
180 1.111 rjs #endif
181 1.111 rjs
182 1.115 rjs #ifdef SCTP
183 1.115 rjs PR_WRAP_CTLINPUT(sctp_ctlinput)
184 1.115 rjs PR_WRAP_CTLOUTPUT(sctp_ctloutput)
185 1.115 rjs
186 1.115 rjs #define sctp_ctlinput sctp_ctlinput_wrapper
187 1.115 rjs #define sctp_ctloutput sctp_ctloutput_wrapper
188 1.115 rjs #endif
189 1.115 rjs
190 1.106 christos #if defined(IPSEC)
191 1.94 ad PR_WRAP_CTLINPUT(ah4_ctlinput)
192 1.94 ad
193 1.94 ad #define ah4_ctlinput ah4_ctlinput_wrapper
194 1.94 ad PR_WRAP_CTLINPUT(esp4_ctlinput)
195 1.94 ad
196 1.94 ad #define esp4_ctlinput esp4_ctlinput_wrapper
197 1.94 ad #endif
198 1.94 ad
199 1.117 riastrad const struct ipprotosw inetsw[] = {
200 1.117 riastrad {
201 1.117 riastrad .ippr_protosw = {
202 1.117 riastrad .pr_domain = &inetdomain,
203 1.81 liamjfoy .pr_init = ip_init,
204 1.101 dyoung .pr_fasttimo = ip_fasttimo,
205 1.81 liamjfoy .pr_slowtimo = ip_slowtimo,
206 1.101 dyoung .pr_drain = ip_drainstub,
207 1.117 riastrad },
208 1.81 liamjfoy },
209 1.117 riastrad {
210 1.117 riastrad .ippr_protosw = {
211 1.117 riastrad .pr_type = SOCK_DGRAM,
212 1.81 liamjfoy .pr_domain = &inetdomain,
213 1.81 liamjfoy .pr_protocol = IPPROTO_UDP,
214 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
215 1.81 liamjfoy .pr_ctlinput = udp_ctlinput,
216 1.81 liamjfoy .pr_ctloutput = udp_ctloutput,
217 1.109 rmind .pr_usrreqs = &udp_usrreqs,
218 1.81 liamjfoy .pr_init = udp_init,
219 1.117 riastrad },
220 1.117 riastrad .ippr_input = udp_input,
221 1.81 liamjfoy },
222 1.117 riastrad {
223 1.117 riastrad .ippr_protosw = {
224 1.117 riastrad .pr_type = SOCK_STREAM,
225 1.81 liamjfoy .pr_domain = &inetdomain,
226 1.81 liamjfoy .pr_protocol = IPPROTO_TCP,
227 1.81 liamjfoy .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF,
228 1.81 liamjfoy .pr_ctlinput = tcp_ctlinput,
229 1.81 liamjfoy .pr_ctloutput = tcp_ctloutput,
230 1.109 rmind .pr_usrreqs = &tcp_usrreqs,
231 1.81 liamjfoy .pr_init = tcp_init,
232 1.101 dyoung .pr_fasttimo = tcp_fasttimo,
233 1.101 dyoung .pr_drain = tcp_drainstub,
234 1.117 riastrad },
235 1.117 riastrad .ippr_input = tcp_input,
236 1.81 liamjfoy },
237 1.111 rjs #ifdef DCCP
238 1.117 riastrad {
239 1.117 riastrad .ippr_protosw = {
240 1.117 riastrad .pr_type = SOCK_CONN_DGRAM,
241 1.111 rjs .pr_domain = &inetdomain,
242 1.111 rjs .pr_protocol = IPPROTO_DCCP,
243 1.111 rjs .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_ATOMIC|PR_LISTEN|PR_ABRTACPTDIS,
244 1.111 rjs .pr_ctlinput = dccp_ctlinput,
245 1.111 rjs .pr_ctloutput = dccp_ctloutput,
246 1.111 rjs .pr_usrreqs = &dccp_usrreqs,
247 1.111 rjs .pr_init = dccp_init,
248 1.117 riastrad },
249 1.117 riastrad .ippr_input = dccp_input,
250 1.111 rjs },
251 1.111 rjs #endif
252 1.115 rjs #ifdef SCTP
253 1.117 riastrad {
254 1.117 riastrad .ippr_protosw = {
255 1.117 riastrad .pr_type = SOCK_DGRAM,
256 1.115 rjs .pr_domain = &inetdomain,
257 1.115 rjs .pr_protocol = IPPROTO_SCTP,
258 1.115 rjs .pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
259 1.115 rjs .pr_ctlinput = sctp_ctlinput,
260 1.115 rjs .pr_ctloutput = sctp_ctloutput,
261 1.115 rjs .pr_usrreqs = &sctp_usrreqs,
262 1.115 rjs .pr_init = sctp_init,
263 1.115 rjs .pr_drain = sctp_drain
264 1.117 riastrad },
265 1.117 riastrad .ippr_input = sctp_input,
266 1.115 rjs },
267 1.117 riastrad {
268 1.117 riastrad .ippr_protosw = {
269 1.117 riastrad .pr_type = SOCK_SEQPACKET,
270 1.115 rjs .pr_domain = &inetdomain,
271 1.115 rjs .pr_protocol = IPPROTO_SCTP,
272 1.115 rjs .pr_flags = PR_ADDR_OPT|PR_WANTRCVD,
273 1.115 rjs .pr_ctlinput = sctp_ctlinput,
274 1.115 rjs .pr_ctloutput = sctp_ctloutput,
275 1.115 rjs .pr_usrreqs = &sctp_usrreqs,
276 1.115 rjs .pr_drain = sctp_drain
277 1.117 riastrad },
278 1.117 riastrad .ippr_input = sctp_input,
279 1.115 rjs },
280 1.117 riastrad {
281 1.117 riastrad .ippr_protosw = {
282 1.117 riastrad .pr_type = SOCK_STREAM,
283 1.115 rjs .pr_domain = &inetdomain,
284 1.115 rjs .pr_protocol = IPPROTO_SCTP,
285 1.115 rjs .pr_flags = PR_CONNREQUIRED|PR_ADDR_OPT|PR_WANTRCVD|PR_LISTEN,
286 1.115 rjs .pr_ctlinput = sctp_ctlinput,
287 1.115 rjs .pr_ctloutput = sctp_ctloutput,
288 1.115 rjs .pr_usrreqs = &sctp_usrreqs,
289 1.115 rjs .pr_drain = sctp_drain
290 1.117 riastrad },
291 1.117 riastrad .ippr_input = sctp_input,
292 1.115 rjs },
293 1.115 rjs #endif /* SCTP */
294 1.117 riastrad {
295 1.117 riastrad .ippr_protosw = {
296 1.117 riastrad .pr_type = SOCK_RAW,
297 1.81 liamjfoy .pr_domain = &inetdomain,
298 1.81 liamjfoy .pr_protocol = IPPROTO_RAW,
299 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
300 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
301 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
302 1.109 rmind .pr_usrreqs = &rip_usrreqs,
303 1.117 riastrad },
304 1.117 riastrad .ippr_input = rip_input,
305 1.81 liamjfoy },
306 1.117 riastrad {
307 1.117 riastrad .ippr_protosw = {
308 1.117 riastrad .pr_type = SOCK_RAW,
309 1.81 liamjfoy .pr_domain = &inetdomain,
310 1.81 liamjfoy .pr_protocol = IPPROTO_ICMP,
311 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
312 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
313 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
314 1.109 rmind .pr_usrreqs = &rip_usrreqs,
315 1.81 liamjfoy .pr_init = icmp_init,
316 1.117 riastrad },
317 1.117 riastrad .ippr_input = icmp_input,
318 1.8 mycroft },
319 1.83 liamjfoy #ifdef GATEWAY
320 1.117 riastrad {
321 1.117 riastrad .ippr_protosw = {
322 1.117 riastrad .pr_domain = &inetdomain,
323 1.83 liamjfoy .pr_protocol = IPPROTO_IP,
324 1.83 liamjfoy .pr_slowtimo = ipflow_slowtimo,
325 1.96 pooka .pr_init = ipflow_poolinit,
326 1.117 riastrad },
327 1.83 liamjfoy },
328 1.83 liamjfoy #endif /* GATEWAY */
329 1.106 christos #ifdef IPSEC
330 1.117 riastrad {
331 1.117 riastrad .ippr_protosw = {
332 1.117 riastrad .pr_type = SOCK_RAW,
333 1.81 liamjfoy .pr_domain = &inetdomain,
334 1.81 liamjfoy .pr_protocol = IPPROTO_AH,
335 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR,
336 1.81 liamjfoy .pr_ctlinput = ah4_ctlinput,
337 1.117 riastrad },
338 1.117 riastrad .ippr_input = ipsec4_common_input,
339 1.81 liamjfoy },
340 1.117 riastrad {
341 1.117 riastrad .ippr_protosw = {
342 1.117 riastrad .pr_type = SOCK_RAW,
343 1.81 liamjfoy .pr_domain = &inetdomain,
344 1.81 liamjfoy .pr_protocol = IPPROTO_ESP,
345 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR,
346 1.81 liamjfoy .pr_ctlinput = esp4_ctlinput,
347 1.117 riastrad },
348 1.117 riastrad .ippr_input = ipsec4_common_input,
349 1.81 liamjfoy },
350 1.117 riastrad {
351 1.117 riastrad .ippr_protosw = {
352 1.117 riastrad .pr_type = SOCK_RAW,
353 1.81 liamjfoy .pr_domain = &inetdomain,
354 1.81 liamjfoy .pr_protocol = IPPROTO_IPCOMP,
355 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR,
356 1.117 riastrad },
357 1.117 riastrad .ippr_input = ipsec4_common_input,
358 1.61 jonathan },
359 1.106 christos #endif /* IPSEC */
360 1.117 riastrad {
361 1.117 riastrad .ippr_protosw = {
362 1.117 riastrad .pr_type = SOCK_RAW,
363 1.81 liamjfoy .pr_domain = &inetdomain,
364 1.81 liamjfoy .pr_protocol = IPPROTO_IPV4,
365 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
366 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
367 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
368 1.109 rmind .pr_usrreqs = &rip_usrreqs,
369 1.81 liamjfoy .pr_init = encap_init,
370 1.117 riastrad },
371 1.117 riastrad .ippr_input = encap4_input,
372 1.32 itojun },
373 1.32 itojun #ifdef INET6
374 1.117 riastrad {
375 1.117 riastrad .ippr_protosw = {
376 1.117 riastrad .pr_type = SOCK_RAW,
377 1.81 liamjfoy .pr_domain = &inetdomain,
378 1.81 liamjfoy .pr_protocol = IPPROTO_IPV6,
379 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
380 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
381 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
382 1.109 rmind .pr_usrreqs = &rip_usrreqs,
383 1.81 liamjfoy .pr_init = encap_init,
384 1.117 riastrad },
385 1.117 riastrad .ippr_input = encap4_input,
386 1.32 itojun },
387 1.32 itojun #endif /* INET6 */
388 1.79 rpaulo #if NETHERIP > 0
389 1.117 riastrad {
390 1.117 riastrad .ippr_protosw = {
391 1.117 riastrad .pr_type = SOCK_RAW,
392 1.81 liamjfoy .pr_domain = &inetdomain,
393 1.81 liamjfoy .pr_protocol = IPPROTO_ETHERIP,
394 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
395 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
396 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
397 1.109 rmind .pr_usrreqs = &rip_usrreqs,
398 1.117 riastrad },
399 1.117 riastrad .ippr_inpput = ip_etherip_input,
400 1.67 kim },
401 1.81 liamjfoy #endif /* NETHERIP > 0 */
402 1.72 liamjfoy #if NCARP > 0
403 1.117 riastrad {
404 1.117 riastrad .ippr_protosw = {
405 1.117 riastrad .pr_type = SOCK_RAW,
406 1.81 liamjfoy .pr_domain = &inetdomain,
407 1.81 liamjfoy .pr_protocol = IPPROTO_CARP,
408 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR,
409 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
410 1.109 rmind .pr_usrreqs = &rip_usrreqs,
411 1.99 pooka .pr_init = carp_init,
412 1.117 riastrad },
413 1.117 riastrad .ippr_input = carp_proto_input,
414 1.72 liamjfoy },
415 1.81 liamjfoy #endif /* NCARP > 0 */
416 1.98 degroote #if NPFSYNC > 0
417 1.117 riastrad {
418 1.117 riastrad .ippr_protosw = {
419 1.117 riastrad .pr_type = SOCK_RAW,
420 1.98 degroote .pr_domain = &inetdomain,
421 1.98 degroote .pr_protocol = IPPROTO_PFSYNC,
422 1.98 degroote .pr_flags = PR_ATOMIC|PR_ADDR,
423 1.98 degroote .pr_ctloutput = rip_ctloutput,
424 1.109 rmind .pr_usrreqs = &rip_usrreqs,
425 1.117 riastrad },
426 1.117 riastrad .ippr_input = pfsync_input,
427 1.98 degroote },
428 1.98 degroote #endif /* NPFSYNC > 0 */
429 1.117 riastrad {
430 1.117 riastrad .ippr_protosw = {
431 1.117 riastrad .pr_type = SOCK_RAW,
432 1.81 liamjfoy .pr_domain = &inetdomain,
433 1.81 liamjfoy .pr_protocol = IPPROTO_IGMP,
434 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
435 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
436 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
437 1.109 rmind .pr_usrreqs = &rip_usrreqs,
438 1.81 liamjfoy .pr_fasttimo = igmp_fasttimo,
439 1.81 liamjfoy .pr_slowtimo = igmp_slowtimo,
440 1.92 thorpej .pr_init = igmp_init,
441 1.117 riastrad },
442 1.117 riastrad .ippr_input = igmp_input,
443 1.1 cgd },
444 1.65 manu #ifdef PIM
445 1.117 riastrad {
446 1.117 riastrad .ippr_protosw = {
447 1.117 riastrad .pr_type = SOCK_RAW,
448 1.81 liamjfoy .pr_domain = &inetdomain,
449 1.81 liamjfoy .pr_protocol = IPPROTO_PIM,
450 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
451 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
452 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
453 1.109 rmind .pr_usrreqs = &rip_usrreqs,
454 1.117 riastrad },
455 1.117 riastrad .ippr_input = pim_input,
456 1.65 manu },
457 1.65 manu #endif /* PIM */
458 1.11 mycroft /* raw wildcard */
459 1.117 riastrad {
460 1.117 riastrad .ippr_protosw = {
461 1.117 riastrad .pr_type = SOCK_RAW,
462 1.81 liamjfoy .pr_domain = &inetdomain,
463 1.81 liamjfoy .pr_flags = PR_ATOMIC|PR_ADDR|PR_LASTHDR,
464 1.81 liamjfoy .pr_ctloutput = rip_ctloutput,
465 1.81 liamjfoy .pr_ctlinput = rip_ctlinput,
466 1.109 rmind .pr_usrreqs = &rip_usrreqs,
467 1.81 liamjfoy .pr_init = rip_init,
468 1.117 riastrad },
469 1.117 riastrad .ippr_input = rip_input,
470 1.1 cgd },
471 1.1 cgd };
472 1.1 cgd
473 1.89 dyoung const struct sockaddr_in in_any = {
474 1.89 dyoung .sin_len = sizeof(struct sockaddr_in)
475 1.89 dyoung , .sin_family = AF_INET
476 1.89 dyoung , .sin_port = 0
477 1.90 dyoung , .sin_addr = {.s_addr = 0 /* INADDR_ANY */}
478 1.89 dyoung };
479 1.89 dyoung
480 1.117 riastrad static const struct protosw *inetprotosw[__arraycount(inetsw)];
481 1.117 riastrad
482 1.73 matt struct domain inetdomain = {
483 1.80 dyoung .dom_family = PF_INET, .dom_name = "internet", .dom_init = NULL,
484 1.80 dyoung .dom_externalize = NULL, .dom_dispose = NULL,
485 1.117 riastrad .dom_protosw = inetprotosw,
486 1.117 riastrad .dom_nprotosw = __arraycount(inetprotosw),
487 1.100 dyoung .dom_rtattach = rt_inithead,
488 1.95 pooka .dom_rtoffset = 32,
489 1.95 pooka .dom_maxrtkey = sizeof(struct ip_pack4),
490 1.112 roy .dom_if_up = in_if_up,
491 1.112 roy .dom_if_down = in_if_down,
492 1.80 dyoung .dom_ifattach = in_domifattach,
493 1.80 dyoung .dom_ifdetach = in_domifdetach,
494 1.112 roy .dom_if_link_state_change = in_if_link_state_change,
495 1.110 rmind .dom_ifqueues = { NULL, NULL },
496 1.80 dyoung .dom_link = { NULL },
497 1.80 dyoung .dom_mowner = MOWNER_INIT("",""),
498 1.86 dyoung .dom_sa_cmpofs = offsetof(struct sockaddr_in, sin_addr),
499 1.86 dyoung .dom_sa_cmplen = sizeof(struct in_addr),
500 1.89 dyoung .dom_sa_any = (const struct sockaddr *)&in_any,
501 1.89 dyoung .dom_sockaddr_const_addr = sockaddr_in_const_addr,
502 1.89 dyoung .dom_sockaddr_addr = sockaddr_in_addr,
503 1.84 dyoung .dom_rtcache = LIST_HEAD_INITIALIZER(inetdomain.dom_rtcache)
504 1.73 matt };
505 1.38 darrenr
506 1.38 darrenr u_char ip_protox[IPPROTO_MAX];
507 1.36 thorpej
508 1.40 itojun int icmperrppslim = 100; /* 100pps */
509 1.84 dyoung
510 1.89 dyoung static void
511 1.89 dyoung sockaddr_in_addrlen(const struct sockaddr *sa, socklen_t *slenp)
512 1.89 dyoung {
513 1.89 dyoung socklen_t slen;
514 1.89 dyoung
515 1.89 dyoung if (slenp == NULL)
516 1.89 dyoung return;
517 1.89 dyoung
518 1.89 dyoung slen = sockaddr_getlen(sa);
519 1.89 dyoung *slenp = (socklen_t)MIN(sizeof(struct in_addr),
520 1.89 dyoung slen - MIN(slen, offsetof(struct sockaddr_in, sin_addr)));
521 1.89 dyoung }
522 1.89 dyoung
523 1.89 dyoung const void *
524 1.89 dyoung sockaddr_in_const_addr(const struct sockaddr *sa, socklen_t *slenp)
525 1.89 dyoung {
526 1.89 dyoung const struct sockaddr_in *sin;
527 1.89 dyoung
528 1.89 dyoung sockaddr_in_addrlen(sa, slenp);
529 1.89 dyoung sin = (const struct sockaddr_in *)sa;
530 1.89 dyoung return &sin->sin_addr;
531 1.89 dyoung }
532 1.89 dyoung
533 1.89 dyoung void *
534 1.89 dyoung sockaddr_in_addr(struct sockaddr *sa, socklen_t *slenp)
535 1.89 dyoung {
536 1.89 dyoung struct sockaddr_in *sin;
537 1.89 dyoung
538 1.89 dyoung sockaddr_in_addrlen(sa, slenp);
539 1.89 dyoung sin = (struct sockaddr_in *)sa;
540 1.89 dyoung return &sin->sin_addr;
541 1.89 dyoung }
542 1.89 dyoung
543 1.84 dyoung int
544 1.84 dyoung sockaddr_in_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
545 1.84 dyoung {
546 1.84 dyoung uint_fast8_t len;
547 1.84 dyoung const uint_fast8_t addrofs = offsetof(struct sockaddr_in, sin_addr),
548 1.84 dyoung addrend = addrofs + sizeof(struct in_addr);
549 1.84 dyoung int rc;
550 1.84 dyoung const struct sockaddr_in *sin1, *sin2;
551 1.84 dyoung
552 1.84 dyoung sin1 = satocsin(sa1);
553 1.84 dyoung sin2 = satocsin(sa2);
554 1.84 dyoung
555 1.84 dyoung len = MIN(addrend, MIN(sin1->sin_len, sin2->sin_len));
556 1.84 dyoung
557 1.84 dyoung if (len > addrofs &&
558 1.84 dyoung (rc = memcmp(&sin1->sin_addr, &sin2->sin_addr,
559 1.84 dyoung len - addrofs)) != 0)
560 1.84 dyoung return rc;
561 1.84 dyoung
562 1.84 dyoung return sin1->sin_len - sin2->sin_len;
563 1.84 dyoung }
564