xref: /openbsd-src/sys/netinet/in_proto.c (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1 /*	$OpenBSD: in_proto.c,v 1.91 2018/11/19 10:15:04 claudio 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 #include "mobileip.h"
176 #if NMOBILEIP > 0
177 #include <net/if_mobileip.h>
178 #endif
179 
180 u_char ip_protox[IPPROTO_MAX];
181 
182 const struct protosw inetsw[] = {
183 {
184   .pr_domain	= &inetdomain,
185   .pr_init	= ip_init,
186   .pr_slowtimo	= ip_slowtimo,
187   .pr_sysctl	= ip_sysctl
188 },
189 {
190   .pr_type	= SOCK_DGRAM,
191   .pr_domain	= &inetdomain,
192   .pr_protocol	= IPPROTO_UDP,
193   .pr_flags	= PR_ATOMIC|PR_ADDR|PR_SPLICE,
194   .pr_input	= udp_input,
195   .pr_ctlinput	= udp_ctlinput,
196   .pr_ctloutput	= ip_ctloutput,
197   .pr_usrreq	= udp_usrreq,
198   .pr_attach	= udp_attach,
199   .pr_detach	= udp_detach,
200   .pr_init	= udp_init,
201   .pr_sysctl	= udp_sysctl
202 },
203 {
204   .pr_type	= SOCK_STREAM,
205   .pr_domain	= &inetdomain,
206   .pr_protocol	= IPPROTO_TCP,
207   .pr_flags	= PR_CONNREQUIRED|PR_WANTRCVD|PR_ABRTACPTDIS|PR_SPLICE,
208   .pr_input	= tcp_input,
209   .pr_ctlinput	= tcp_ctlinput,
210   .pr_ctloutput	= tcp_ctloutput,
211   .pr_usrreq	= tcp_usrreq,
212   .pr_attach	= tcp_attach,
213   .pr_detach	= tcp_detach,
214   .pr_init	= tcp_init,
215   .pr_slowtimo	= tcp_slowtimo,
216   .pr_sysctl	= tcp_sysctl
217 },
218 {
219   .pr_type	= SOCK_RAW,
220   .pr_domain	= &inetdomain,
221   .pr_protocol	= IPPROTO_RAW,
222   .pr_flags	= PR_ATOMIC|PR_ADDR,
223   .pr_input	= rip_input,
224   .pr_ctloutput	= rip_ctloutput,
225   .pr_usrreq	= rip_usrreq,
226   .pr_attach	= rip_attach,
227   .pr_detach	= rip_detach,
228 },
229 {
230   .pr_type	= SOCK_RAW,
231   .pr_domain	= &inetdomain,
232   .pr_protocol	= IPPROTO_ICMP,
233   .pr_flags	= PR_ATOMIC|PR_ADDR,
234   .pr_input	= icmp_input,
235   .pr_ctloutput	= rip_ctloutput,
236   .pr_usrreq	= rip_usrreq,
237   .pr_attach	= rip_attach,
238   .pr_detach	= rip_detach,
239   .pr_init	= icmp_init,
240   .pr_sysctl	= icmp_sysctl
241 },
242 {
243   .pr_type	= SOCK_RAW,
244   .pr_domain	= &inetdomain,
245   .pr_protocol	= IPPROTO_IPV4,
246   .pr_flags	= PR_ATOMIC|PR_ADDR,
247 #if NGIF > 0
248   .pr_input	= in_gif_input,
249 #else
250   .pr_input	= ipip_input,
251 #endif
252   .pr_ctloutput	= rip_ctloutput,
253   .pr_usrreq	= rip_usrreq,
254   .pr_attach	= rip_attach,
255   .pr_detach	= rip_detach,
256   .pr_sysctl	= ipip_sysctl,
257   .pr_init	= ipip_init
258 },
259 #ifdef INET6
260 {
261   .pr_type	= SOCK_RAW,
262   .pr_domain	= &inetdomain,
263   .pr_protocol	= IPPROTO_IPV6,
264   .pr_flags	= PR_ATOMIC|PR_ADDR,
265 #if NGIF > 0
266   .pr_input	= in_gif_input,
267 #else
268   .pr_input	= ipip_input,
269 #endif
270   .pr_ctloutput	= rip_ctloutput,
271   .pr_usrreq	= rip_usrreq, /* XXX */
272   .pr_attach	= rip_attach,
273   .pr_detach	= rip_detach,
274 },
275 #endif
276 #if defined(MPLS) && NGIF > 0
277 {
278   .pr_type	= SOCK_RAW,
279   .pr_domain	= &inetdomain,
280   .pr_protocol	= IPPROTO_MPLS,
281   .pr_flags	= PR_ATOMIC|PR_ADDR,
282   .pr_input	= in_gif_input,
283   .pr_usrreq	= rip_usrreq,
284   .pr_attach	= rip_attach,
285   .pr_detach	= rip_detach,
286 },
287 #endif /* MPLS && GIF */
288 {
289   .pr_type	= SOCK_RAW,
290   .pr_domain	= &inetdomain,
291   .pr_protocol	= IPPROTO_IGMP,
292   .pr_flags	= PR_ATOMIC|PR_ADDR,
293   .pr_input	= igmp_input,
294   .pr_ctloutput	= rip_ctloutput,
295   .pr_usrreq	= rip_usrreq,
296   .pr_attach	= rip_attach,
297   .pr_detach	= rip_detach,
298   .pr_init	= igmp_init,
299   .pr_fasttimo	= igmp_fasttimo,
300   .pr_slowtimo	= igmp_slowtimo,
301   .pr_sysctl	= igmp_sysctl
302 },
303 #ifdef IPSEC
304 {
305   .pr_type	= SOCK_RAW,
306   .pr_domain	= &inetdomain,
307   .pr_protocol	= IPPROTO_AH,
308   .pr_flags	= PR_ATOMIC|PR_ADDR,
309   .pr_input	= ah4_input,
310   .pr_ctlinput	= ah4_ctlinput,
311   .pr_ctloutput	= rip_ctloutput,
312   .pr_usrreq	= rip_usrreq,
313   .pr_attach	= rip_attach,
314   .pr_detach	= rip_detach,
315   .pr_sysctl	= ah_sysctl
316 },
317 {
318   .pr_type	= SOCK_RAW,
319   .pr_domain	= &inetdomain,
320   .pr_protocol	= IPPROTO_ESP,
321   .pr_flags	= PR_ATOMIC|PR_ADDR,
322   .pr_input	= esp4_input,
323   .pr_ctlinput	= esp4_ctlinput,
324   .pr_ctloutput	= rip_ctloutput,
325   .pr_usrreq	= rip_usrreq,
326   .pr_attach	= rip_attach,
327   .pr_detach	= rip_detach,
328   .pr_sysctl	= esp_sysctl
329 },
330 {
331   .pr_type	= SOCK_RAW,
332   .pr_domain	= &inetdomain,
333   .pr_protocol	= IPPROTO_IPCOMP,
334   .pr_flags	= PR_ATOMIC|PR_ADDR,
335   .pr_input	= ipcomp4_input,
336   .pr_ctloutput	= rip_ctloutput,
337   .pr_usrreq	= rip_usrreq,
338   .pr_attach	= rip_attach,
339   .pr_detach	= rip_detach,
340   .pr_sysctl	= ipcomp_sysctl
341 },
342 #endif /* IPSEC */
343 #if NGRE > 0
344 {
345   .pr_type	= SOCK_RAW,
346   .pr_domain	= &inetdomain,
347   .pr_protocol	= IPPROTO_GRE,
348   .pr_flags	= PR_ATOMIC|PR_ADDR,
349   .pr_input	= gre_input,
350   .pr_ctloutput	= rip_ctloutput,
351   .pr_usrreq	= gre_usrreq,
352   .pr_attach	= rip_attach,
353   .pr_detach	= rip_detach,
354   .pr_sysctl	= gre_sysctl
355 },
356 #endif /* NGRE > 0 */
357 #if NMOBILEIP > 0
358 {
359   .pr_type	= SOCK_RAW,
360   .pr_domain	= &inetdomain,
361   .pr_protocol	= IPPROTO_MOBILE,
362   .pr_flags	= PR_ATOMIC|PR_ADDR,
363   .pr_input	= mobileip_input,
364   .pr_ctloutput	= rip_ctloutput,
365   .pr_usrreq	= rip_usrreq,
366   .pr_attach	= rip_attach,
367   .pr_detach	= rip_detach,
368   .pr_sysctl	= mobileip_sysctl
369 },
370 #endif /* NMOBILEIP > 0 */
371 #if NCARP > 0
372 {
373   .pr_type	= SOCK_RAW,
374   .pr_domain	= &inetdomain,
375   .pr_protocol	= IPPROTO_CARP,
376   .pr_flags	= PR_ATOMIC|PR_ADDR,
377   .pr_input	= carp_proto_input,
378   .pr_ctloutput	= rip_ctloutput,
379   .pr_usrreq	= rip_usrreq,
380   .pr_attach	= rip_attach,
381   .pr_detach	= rip_detach,
382   .pr_sysctl	= carp_sysctl
383 },
384 #endif /* NCARP > 0 */
385 #if NPFSYNC > 0
386 {
387   .pr_type	= SOCK_RAW,
388   .pr_domain	= &inetdomain,
389   .pr_protocol	= IPPROTO_PFSYNC,
390   .pr_flags	= PR_ATOMIC|PR_ADDR,
391   .pr_input	= pfsync_input,
392   .pr_ctloutput	= rip_ctloutput,
393   .pr_usrreq	= rip_usrreq,
394   .pr_attach	= rip_attach,
395   .pr_detach	= rip_detach,
396   .pr_sysctl	= pfsync_sysctl
397 },
398 #endif /* NPFSYNC > 0 */
399 #if NPF > 0
400 {
401   .pr_type	= SOCK_RAW,
402   .pr_domain	= &inetdomain,
403   .pr_protocol	= IPPROTO_DIVERT,
404   .pr_flags	= PR_ATOMIC|PR_ADDR,
405   .pr_ctloutput	= rip_ctloutput,
406   .pr_usrreq	= divert_usrreq,
407   .pr_attach	= divert_attach,
408   .pr_detach	= divert_detach,
409   .pr_init	= divert_init,
410   .pr_sysctl	= divert_sysctl
411 },
412 #endif /* NPF > 0 */
413 #if NETHERIP > 0
414 {
415   .pr_type	= SOCK_RAW,
416   .pr_domain	= &inetdomain,
417   .pr_protocol	= IPPROTO_ETHERIP,
418   .pr_flags	= PR_ATOMIC|PR_ADDR,
419   .pr_input	= ip_etherip_input,
420   .pr_ctloutput	= rip_ctloutput,
421   .pr_usrreq	= rip_usrreq,
422   .pr_attach	= rip_attach,
423   .pr_detach	= rip_detach,
424   .pr_sysctl	= etherip_sysctl
425 },
426 #endif /* NETHERIP */
427 {
428   /* raw wildcard */
429   .pr_type	= SOCK_RAW,
430   .pr_domain	= &inetdomain,
431   .pr_flags	= PR_ATOMIC|PR_ADDR,
432   .pr_input	= rip_input,
433   .pr_ctloutput	= rip_ctloutput,
434   .pr_usrreq	= rip_usrreq,
435   .pr_attach	= rip_attach,
436   .pr_detach	= rip_detach,
437   .pr_init	= rip_init
438 }
439 };
440 
441 struct domain inetdomain = {
442   .dom_family = AF_INET,
443   .dom_name = "internet",
444   .dom_protosw = inetsw,
445   .dom_protoswNPROTOSW = &inetsw[nitems(inetsw)],
446   .dom_rtoffset = offsetof(struct sockaddr_in, sin_addr),
447   .dom_maxplen = 32
448 };
449