xref: /openbsd/sys/netinet/in_proto.c (revision 09467b48)
1 /*	$OpenBSD: in_proto.c,v 1.94 2019/11/04 23:52:28 dlg Exp $	*/
2 /*	$NetBSD: in_proto.c,v 1.14 1996/02/18 18:58:32 christos Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
62  *
63  * NRL grants permission for redistribution and use in source and binary
64  * forms, with or without modification, of the software and documentation
65  * created at NRL provided that the following conditions are met:
66  *
67  * 1. Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  * 2. Redistributions in binary form must reproduce the above copyright
70  *    notice, this list of conditions and the following disclaimer in the
71  *    documentation and/or other materials provided with the distribution.
72  * 3. All advertising materials mentioning features or use of this software
73  *    must display the following acknowledgements:
74  * 	This product includes software developed by the University of
75  * 	California, Berkeley and its contributors.
76  * 	This product includes software developed at the Information
77  * 	Technology Division, US Naval Research Laboratory.
78  * 4. Neither the name of the NRL nor the names of its contributors
79  *    may be used to endorse or promote products derived from this software
80  *    without specific prior written permission.
81  *
82  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
83  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
84  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
85  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
86  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
87  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
88  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
89  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
91  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
92  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93  *
94  * The views and conclusions contained in the software and documentation
95  * are those of the authors and should not be interpreted as representing
96  * official policies, either expressed or implied, of the US Naval
97  * Research Laboratory (NRL).
98  */
99 
100 #include <sys/param.h>
101 #include <sys/socket.h>
102 #include <sys/protosw.h>
103 #include <sys/domain.h>
104 #include <sys/mbuf.h>
105 
106 #include <net/if.h>
107 #include <net/if_var.h>
108 #include <net/route.h>
109 #include <net/rtable.h>
110 
111 #include <netinet/in.h>
112 #include <netinet/ip.h>
113 #include <netinet/ip_var.h>
114 #include <netinet/ip_icmp.h>
115 #include <netinet/in_pcb.h>
116 
117 #ifdef INET6
118 #include <netinet/ip6.h>
119 #endif
120 
121 #include <netinet/igmp_var.h>
122 #include <netinet/tcp.h>
123 #include <netinet/tcp_timer.h>
124 #include <netinet/tcp_var.h>
125 #include <netinet/udp.h>
126 #include <netinet/udp_var.h>
127 
128 /*
129  * TCP/IP protocol family: IP, ICMP, UDP, TCP.
130  */
131 
132 #include "gif.h"
133 #if NGIF > 0
134 #include <net/if_gif.h>
135 #endif
136 
137 #ifdef INET6
138 #include <netinet6/ip6_var.h>
139 #endif /* INET6 */
140 
141 #ifdef IPSEC
142 #include <netinet/ip_ipsp.h>
143 #endif
144 
145 #include <netinet/ip_ether.h>
146 #include <netinet/ip_ipip.h>
147 
148 #include "gre.h"
149 #if NGRE > 0
150 #include <netinet/ip_gre.h>
151 #include <net/if_gre.h>
152 #endif
153 
154 #include "carp.h"
155 #if NCARP > 0
156 #include <netinet/ip_carp.h>
157 #endif
158 
159 #include "pfsync.h"
160 #if NPFSYNC > 0
161 #include <net/pfvar.h>
162 #include <net/if_pfsync.h>
163 #endif
164 
165 #include "pf.h"
166 #if NPF > 0
167 #include <netinet/ip_divert.h>
168 #endif
169 
170 #include "etherip.h"
171 #if NETHERIP > 0
172 #include <net/if_etherip.h>
173 #endif
174 
175 u_char ip_protox[IPPROTO_MAX];
176 
177 const struct protosw inetsw[] = {
178 {
179   .pr_domain	= &inetdomain,
180   .pr_init	= ip_init,
181   .pr_slowtimo	= ip_slowtimo,
182   .pr_sysctl	= ip_sysctl
183 },
184 {
185   .pr_type	= SOCK_DGRAM,
186   .pr_domain	= &inetdomain,
187   .pr_protocol	= IPPROTO_UDP,
188   .pr_flags	= PR_ATOMIC|PR_ADDR|PR_SPLICE,
189   .pr_input	= udp_input,
190   .pr_ctlinput	= udp_ctlinput,
191   .pr_ctloutput	= ip_ctloutput,
192   .pr_usrreq	= udp_usrreq,
193   .pr_attach	= udp_attach,
194   .pr_detach	= udp_detach,
195   .pr_init	= udp_init,
196   .pr_sysctl	= udp_sysctl
197 },
198 {
199   .pr_type	= SOCK_STREAM,
200   .pr_domain	= &inetdomain,
201   .pr_protocol	= IPPROTO_TCP,
202   .pr_flags	= PR_CONNREQUIRED|PR_WANTRCVD|PR_ABRTACPTDIS|PR_SPLICE,
203   .pr_input	= tcp_input,
204   .pr_ctlinput	= tcp_ctlinput,
205   .pr_ctloutput	= tcp_ctloutput,
206   .pr_usrreq	= tcp_usrreq,
207   .pr_attach	= tcp_attach,
208   .pr_detach	= tcp_detach,
209   .pr_init	= tcp_init,
210   .pr_slowtimo	= tcp_slowtimo,
211   .pr_sysctl	= tcp_sysctl
212 },
213 {
214   .pr_type	= SOCK_RAW,
215   .pr_domain	= &inetdomain,
216   .pr_protocol	= IPPROTO_RAW,
217   .pr_flags	= PR_ATOMIC|PR_ADDR,
218   .pr_input	= rip_input,
219   .pr_ctloutput	= rip_ctloutput,
220   .pr_usrreq	= rip_usrreq,
221   .pr_attach	= rip_attach,
222   .pr_detach	= rip_detach,
223 },
224 {
225   .pr_type	= SOCK_RAW,
226   .pr_domain	= &inetdomain,
227   .pr_protocol	= IPPROTO_ICMP,
228   .pr_flags	= PR_ATOMIC|PR_ADDR,
229   .pr_input	= icmp_input,
230   .pr_ctloutput	= rip_ctloutput,
231   .pr_usrreq	= rip_usrreq,
232   .pr_attach	= rip_attach,
233   .pr_detach	= rip_detach,
234   .pr_init	= icmp_init,
235   .pr_sysctl	= icmp_sysctl
236 },
237 {
238   .pr_type	= SOCK_RAW,
239   .pr_domain	= &inetdomain,
240   .pr_protocol	= IPPROTO_IPV4,
241   .pr_flags	= PR_ATOMIC|PR_ADDR,
242 #if NGIF > 0
243   .pr_input	= in_gif_input,
244 #else
245   .pr_input	= ipip_input,
246 #endif
247   .pr_ctloutput	= rip_ctloutput,
248   .pr_usrreq	= rip_usrreq,
249   .pr_attach	= rip_attach,
250   .pr_detach	= rip_detach,
251   .pr_sysctl	= ipip_sysctl,
252   .pr_init	= ipip_init
253 },
254 #ifdef INET6
255 {
256   .pr_type	= SOCK_RAW,
257   .pr_domain	= &inetdomain,
258   .pr_protocol	= IPPROTO_IPV6,
259   .pr_flags	= PR_ATOMIC|PR_ADDR,
260 #if NGIF > 0
261   .pr_input	= in_gif_input,
262 #else
263   .pr_input	= ipip_input,
264 #endif
265   .pr_ctloutput	= rip_ctloutput,
266   .pr_usrreq	= rip_usrreq, /* XXX */
267   .pr_attach	= rip_attach,
268   .pr_detach	= rip_detach,
269 },
270 #endif
271 #if defined(MPLS) && NGIF > 0
272 {
273   .pr_type	= SOCK_RAW,
274   .pr_domain	= &inetdomain,
275   .pr_protocol	= IPPROTO_MPLS,
276   .pr_flags	= PR_ATOMIC|PR_ADDR,
277   .pr_input	= in_gif_input,
278   .pr_usrreq	= rip_usrreq,
279   .pr_attach	= rip_attach,
280   .pr_detach	= rip_detach,
281 },
282 #endif /* MPLS && GIF */
283 {
284   .pr_type	= SOCK_RAW,
285   .pr_domain	= &inetdomain,
286   .pr_protocol	= IPPROTO_IGMP,
287   .pr_flags	= PR_ATOMIC|PR_ADDR,
288   .pr_input	= igmp_input,
289   .pr_ctloutput	= rip_ctloutput,
290   .pr_usrreq	= rip_usrreq,
291   .pr_attach	= rip_attach,
292   .pr_detach	= rip_detach,
293   .pr_init	= igmp_init,
294   .pr_fasttimo	= igmp_fasttimo,
295   .pr_slowtimo	= igmp_slowtimo,
296   .pr_sysctl	= igmp_sysctl
297 },
298 #ifdef IPSEC
299 {
300   .pr_type	= SOCK_RAW,
301   .pr_domain	= &inetdomain,
302   .pr_protocol	= IPPROTO_AH,
303   .pr_flags	= PR_ATOMIC|PR_ADDR,
304   .pr_input	= ah4_input,
305   .pr_ctlinput	= ah4_ctlinput,
306   .pr_ctloutput	= rip_ctloutput,
307   .pr_usrreq	= rip_usrreq,
308   .pr_attach	= rip_attach,
309   .pr_detach	= rip_detach,
310   .pr_sysctl	= ah_sysctl
311 },
312 {
313   .pr_type	= SOCK_RAW,
314   .pr_domain	= &inetdomain,
315   .pr_protocol	= IPPROTO_ESP,
316   .pr_flags	= PR_ATOMIC|PR_ADDR,
317   .pr_input	= esp4_input,
318   .pr_ctlinput	= esp4_ctlinput,
319   .pr_ctloutput	= rip_ctloutput,
320   .pr_usrreq	= rip_usrreq,
321   .pr_attach	= rip_attach,
322   .pr_detach	= rip_detach,
323   .pr_sysctl	= esp_sysctl
324 },
325 {
326   .pr_type	= SOCK_RAW,
327   .pr_domain	= &inetdomain,
328   .pr_protocol	= IPPROTO_IPCOMP,
329   .pr_flags	= PR_ATOMIC|PR_ADDR,
330   .pr_input	= ipcomp4_input,
331   .pr_ctloutput	= rip_ctloutput,
332   .pr_usrreq	= rip_usrreq,
333   .pr_attach	= rip_attach,
334   .pr_detach	= rip_detach,
335   .pr_sysctl	= ipcomp_sysctl
336 },
337 #endif /* IPSEC */
338 #if NGRE > 0
339 {
340   .pr_type	= SOCK_RAW,
341   .pr_domain	= &inetdomain,
342   .pr_protocol	= IPPROTO_GRE,
343   .pr_flags	= PR_ATOMIC|PR_ADDR,
344   .pr_input	= gre_input,
345   .pr_ctloutput	= rip_ctloutput,
346   .pr_usrreq	= gre_usrreq,
347   .pr_attach	= rip_attach,
348   .pr_detach	= rip_detach,
349   .pr_sysctl	= gre_sysctl
350 },
351 #endif /* NGRE > 0 */
352 #if NCARP > 0
353 {
354   .pr_type	= SOCK_RAW,
355   .pr_domain	= &inetdomain,
356   .pr_protocol	= IPPROTO_CARP,
357   .pr_flags	= PR_ATOMIC|PR_ADDR,
358   .pr_input	= carp_proto_input,
359   .pr_ctloutput	= rip_ctloutput,
360   .pr_usrreq	= rip_usrreq,
361   .pr_attach	= rip_attach,
362   .pr_detach	= rip_detach,
363   .pr_sysctl	= carp_sysctl
364 },
365 #endif /* NCARP > 0 */
366 #if NPFSYNC > 0
367 {
368   .pr_type	= SOCK_RAW,
369   .pr_domain	= &inetdomain,
370   .pr_protocol	= IPPROTO_PFSYNC,
371   .pr_flags	= PR_ATOMIC|PR_ADDR,
372   .pr_input	= pfsync_input,
373   .pr_ctloutput	= rip_ctloutput,
374   .pr_usrreq	= rip_usrreq,
375   .pr_attach	= rip_attach,
376   .pr_detach	= rip_detach,
377   .pr_sysctl	= pfsync_sysctl
378 },
379 #endif /* NPFSYNC > 0 */
380 #if NPF > 0
381 {
382   .pr_type	= SOCK_RAW,
383   .pr_domain	= &inetdomain,
384   .pr_protocol	= IPPROTO_DIVERT,
385   .pr_flags	= PR_ATOMIC|PR_ADDR,
386   .pr_ctloutput	= rip_ctloutput,
387   .pr_usrreq	= divert_usrreq,
388   .pr_attach	= divert_attach,
389   .pr_detach	= divert_detach,
390   .pr_init	= divert_init,
391   .pr_sysctl	= divert_sysctl
392 },
393 #endif /* NPF > 0 */
394 #if NETHERIP > 0
395 {
396   .pr_type	= SOCK_RAW,
397   .pr_domain	= &inetdomain,
398   .pr_protocol	= IPPROTO_ETHERIP,
399   .pr_flags	= PR_ATOMIC|PR_ADDR,
400   .pr_input	= ip_etherip_input,
401   .pr_ctloutput	= rip_ctloutput,
402   .pr_usrreq	= rip_usrreq,
403   .pr_attach	= rip_attach,
404   .pr_detach	= rip_detach,
405   .pr_sysctl	= etherip_sysctl
406 },
407 #endif /* NETHERIP */
408 {
409   /* raw wildcard */
410   .pr_type	= SOCK_RAW,
411   .pr_domain	= &inetdomain,
412   .pr_flags	= PR_ATOMIC|PR_ADDR,
413   .pr_input	= rip_input,
414   .pr_ctloutput	= rip_ctloutput,
415   .pr_usrreq	= rip_usrreq,
416   .pr_attach	= rip_attach,
417   .pr_detach	= rip_detach,
418   .pr_init	= rip_init
419 }
420 };
421 
422 struct domain inetdomain = {
423   .dom_family = AF_INET,
424   .dom_name = "internet",
425   .dom_init = in_init,
426   .dom_protosw = inetsw,
427   .dom_protoswNPROTOSW = &inetsw[nitems(inetsw)],
428   .dom_sasize = sizeof(struct sockaddr_in),
429   .dom_rtoffset = offsetof(struct sockaddr_in, sin_addr),
430   .dom_maxplen = 32
431 };
432