1139823Simp/*-
22788Sdg * Copyright (c) 1982, 1986, 1993
32788Sdg *	The Regents of the University of California.  All rights reserved.
42788Sdg *
52788Sdg * Redistribution and use in source and binary forms, with or without
62788Sdg * modification, are permitted provided that the following conditions
72788Sdg * are met:
82788Sdg * 1. Redistributions of source code must retain the above copyright
92788Sdg *    notice, this list of conditions and the following disclaimer.
102788Sdg * 2. Redistributions in binary form must reproduce the above copyright
112788Sdg *    notice, this list of conditions and the following disclaimer in the
122788Sdg *    documentation and/or other materials provided with the distribution.
132788Sdg * 4. Neither the name of the University nor the names of its contributors
142788Sdg *    may be used to endorse or promote products derived from this software
152788Sdg *    without specific prior written permission.
162788Sdg *
172788Sdg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
182788Sdg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192788Sdg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202788Sdg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
212788Sdg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222788Sdg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232788Sdg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242788Sdg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252788Sdg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262788Sdg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272788Sdg * SUCH DAMAGE.
282788Sdg *
2910941Swollman *	@(#)in_proto.c	8.2 (Berkeley) 2/9/95
302788Sdg */
311541Srgrimes
32172467Ssilby#include <sys/cdefs.h>
33172467Ssilby__FBSDID("$FreeBSD: stable/11/sys/netinet/in_proto.c 335031 2018-06-13 07:14:34Z ae $");
34172467Ssilby
35118622Shsu#include "opt_mrouting.h"
3655009Sshin#include "opt_ipsec.h"
37220746Sbz#include "opt_inet.h"
3855009Sshin#include "opt_inet6.h"
39163953Srrs#include "opt_sctp.h"
40178167Sqingli#include "opt_mpath.h"
4130966Sjoerg
422788Sdg#include <sys/param.h>
43111145Sjlemon#include <sys/systm.h>
448426Swollman#include <sys/kernel.h>
45295126Sglebius#include <sys/malloc.h>
462788Sdg#include <sys/socket.h>
472788Sdg#include <sys/domain.h>
48185895Szec#include <sys/proc.h>
4912426Sphk#include <sys/protosw.h>
5044078Sdfr#include <sys/queue.h>
5112172Sphk#include <sys/sysctl.h>
521541Srgrimes
53220746Sbz/*
54220746Sbz * While this file provides the domain and protocol switch tables for IPv4, it
55220746Sbz * also provides the sysctl node declarations for net.inet.* often shared with
56220746Sbz * IPv6 for common features or by upper layer protocols.  In case of no IPv4
57220746Sbz * support compile out everything but these sysctl nodes.
58220746Sbz */
59220746Sbz#ifdef INET
602788Sdg#include <net/if.h>
61257176Sglebius#include <net/if_var.h>
622788Sdg#include <net/route.h>
63178167Sqingli#ifdef RADIX_MPATH
64178167Sqingli#include <net/radix_mpath.h>
65178167Sqingli#endif
66196019Srwatson#include <net/vnet.h>
67221021Sbz#endif /* INET */
681541Srgrimes
69221021Sbz#if defined(INET) || defined(INET6)
702788Sdg#include <netinet/in.h>
71221021Sbz#endif
72221021Sbz
73221021Sbz#ifdef INET
742788Sdg#include <netinet/in_systm.h>
75186119Sqingli#include <netinet/in_var.h>
762788Sdg#include <netinet/ip.h>
772788Sdg#include <netinet/ip_var.h>
782788Sdg#include <netinet/ip_icmp.h>
792788Sdg#include <netinet/igmp_var.h>
802788Sdg#include <netinet/tcp.h>
812788Sdg#include <netinet/tcp_timer.h>
822788Sdg#include <netinet/tcp_var.h>
832788Sdg#include <netinet/udp.h>
842788Sdg#include <netinet/udp_var.h>
8562587Sitojun#include <netinet/ip_encap.h>
8655009Sshin
872788Sdg/*
882788Sdg * TCP/IP protocol family: IP, ICMP, UDP, TCP.
892788Sdg */
901541Srgrimes
91148918Sobrienstatic struct pr_usrreqs nousrreqs;
92148918Sobrien
93163953Srrs#ifdef SCTP
94163953Srrs#include <netinet/in_pcb.h>
95163953Srrs#include <netinet/sctp_pcb.h>
96163953Srrs#include <netinet/sctp.h>
97163953Srrs#include <netinet/sctp_var.h>
98163953Srrs#endif /* SCTP */
99163953Srrs
100222272SbzFEATURE(inet, "Internet Protocol version 4");
101222272Sbz
1022788Sdgextern	struct domain inetdomain;
1032531Swollman
104136695Sandre/* Spacer for loadable protocols. */
105152242Sru#define IPPROTOSPACER   			\
106152242Sru{						\
107152242Sru	.pr_domain =		&inetdomain,	\
108152242Sru	.pr_protocol =		PROTO_SPACER,	\
109152242Sru	.pr_usrreqs =		&nousrreqs	\
110136695Sandre}
111136695Sandre
11282884Sjulianstruct protosw inetsw[] = {
113152242Sru{
114152242Sru	.pr_type =		0,
115152242Sru	.pr_domain =		&inetdomain,
116152242Sru	.pr_protocol =		IPPROTO_IP,
117152242Sru	.pr_init =		ip_init,
118152242Sru	.pr_slowtimo =		ip_slowtimo,
119152242Sru	.pr_drain =		ip_drain,
120152242Sru	.pr_usrreqs =		&nousrreqs
1212788Sdg},
122152242Sru{
123152242Sru	.pr_type =		SOCK_DGRAM,
124152242Sru	.pr_domain =		&inetdomain,
125152242Sru	.pr_protocol =		IPPROTO_UDP,
126152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
127152242Sru	.pr_input =		udp_input,
128152242Sru	.pr_ctlinput =		udp_ctlinput,
129194062Svanhu	.pr_ctloutput =		udp_ctloutput,
130152242Sru	.pr_init =		udp_init,
131152242Sru	.pr_usrreqs =		&udp_usrreqs
1322788Sdg},
133152242Sru{
134152242Sru	.pr_type =		SOCK_STREAM,
135152242Sru	.pr_domain =		&inetdomain,
136152242Sru	.pr_protocol =		IPPROTO_TCP,
137152242Sru	.pr_flags =		PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
138152242Sru	.pr_input =		tcp_input,
139152242Sru	.pr_ctlinput =		tcp_ctlinput,
140152242Sru	.pr_ctloutput =		tcp_ctloutput,
141152242Sru	.pr_init =		tcp_init,
142152242Sru	.pr_slowtimo =		tcp_slowtimo,
143152242Sru	.pr_drain =		tcp_drain,
144152242Sru	.pr_usrreqs =		&tcp_usrreqs
1452788Sdg},
146163953Srrs#ifdef SCTP
147163953Srrs{
148223963Stuexen	.pr_type =		SOCK_SEQPACKET,
149197326Stuexen	.pr_domain =		&inetdomain,
150197326Stuexen	.pr_protocol =		IPPROTO_SCTP,
151315514Sae	.pr_flags =		PR_WANTRCVD|PR_LASTHDR,
152197326Stuexen	.pr_input =		sctp_input,
153197326Stuexen	.pr_ctlinput =		sctp_ctlinput,
154197326Stuexen	.pr_ctloutput =		sctp_ctloutput,
155197326Stuexen	.pr_init =		sctp_init,
156197326Stuexen	.pr_drain =		sctp_drain,
157197326Stuexen	.pr_usrreqs =		&sctp_usrreqs
158163953Srrs},
159163953Srrs{
160197326Stuexen	.pr_type =		SOCK_STREAM,
161197326Stuexen	.pr_domain =		&inetdomain,
162197326Stuexen	.pr_protocol =		IPPROTO_SCTP,
163315514Sae	.pr_flags =		PR_CONNREQUIRED|PR_WANTRCVD|PR_LASTHDR,
164197326Stuexen	.pr_input =		sctp_input,
165197326Stuexen	.pr_ctlinput =		sctp_ctlinput,
166197326Stuexen	.pr_ctloutput =		sctp_ctloutput,
167197326Stuexen	.pr_drain =		sctp_drain,
168197326Stuexen	.pr_usrreqs =		&sctp_usrreqs
169163953Srrs},
170163953Srrs#endif /* SCTP */
171163953Srrs{
172264212Skevlo	.pr_type =		SOCK_DGRAM,
173264212Skevlo	.pr_domain =		&inetdomain,
174264212Skevlo	.pr_protocol =		IPPROTO_UDPLITE,
175264212Skevlo	.pr_flags =		PR_ATOMIC|PR_ADDR,
176264212Skevlo	.pr_input =		udp_input,
177264212Skevlo	.pr_ctlinput =		udplite_ctlinput,
178264212Skevlo	.pr_ctloutput =		udp_ctloutput,
179264212Skevlo	.pr_init =		udplite_init,
180264212Skevlo	.pr_usrreqs =		&udp_usrreqs
181264212Skevlo},
182264212Skevlo{
183152242Sru	.pr_type =		SOCK_RAW,
184152242Sru	.pr_domain =		&inetdomain,
185152242Sru	.pr_protocol =		IPPROTO_RAW,
186152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
187152242Sru	.pr_input =		rip_input,
188152242Sru	.pr_ctlinput =		rip_ctlinput,
189152242Sru	.pr_ctloutput =		rip_ctloutput,
190152242Sru	.pr_usrreqs =		&rip_usrreqs
1912788Sdg},
192152242Sru{
193152242Sru	.pr_type =		SOCK_RAW,
194152242Sru	.pr_domain =		&inetdomain,
195152242Sru	.pr_protocol =		IPPROTO_ICMP,
196152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
197152242Sru	.pr_input =		icmp_input,
198152242Sru	.pr_ctloutput =		rip_ctloutput,
199152242Sru	.pr_usrreqs =		&rip_usrreqs
2002788Sdg},
201152242Sru{
202152242Sru	.pr_type =		SOCK_RAW,
203152242Sru	.pr_domain =		&inetdomain,
204152242Sru	.pr_protocol =		IPPROTO_IGMP,
205152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
206152242Sru	.pr_input =		igmp_input,
207152242Sru	.pr_ctloutput =		rip_ctloutput,
208152242Sru	.pr_fasttimo =		igmp_fasttimo,
209152242Sru	.pr_slowtimo =		igmp_slowtimo,
210152242Sru	.pr_usrreqs =		&rip_usrreqs
2112788Sdg},
212152242Sru{
213152242Sru	.pr_type =		SOCK_RAW,
214152242Sru	.pr_domain =		&inetdomain,
215152242Sru	.pr_protocol =		IPPROTO_RSVP,
216152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
217152242Sru	.pr_input =		rsvp_input,
218152242Sru	.pr_ctloutput =		rip_ctloutput,
219152242Sru	.pr_usrreqs =		&rip_usrreqs
2202788Sdg},
221152242Sru{
222152242Sru	.pr_type =		SOCK_RAW,
223152242Sru	.pr_domain =		&inetdomain,
224152242Sru	.pr_protocol =		IPPROTO_IPV4,
225152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
226152242Sru	.pr_input =		encap4_input,
227152242Sru	.pr_ctloutput =		rip_ctloutput,
228152242Sru	.pr_usrreqs =		&rip_usrreqs
22954263Sshin},
230152242Sru{
231152242Sru	.pr_type =		SOCK_RAW,
232152242Sru	.pr_domain =		&inetdomain,
233152242Sru	.pr_protocol =		IPPROTO_MOBILE,
234152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
235152242Sru	.pr_input =		encap4_input,
236152242Sru	.pr_ctloutput =		rip_ctloutput,
237152242Sru	.pr_usrreqs =		&rip_usrreqs
238103026Ssobomax},
239152242Sru{
240152242Sru	.pr_type =		SOCK_RAW,
241152242Sru	.pr_domain =		&inetdomain,
242153621Sthompsa	.pr_protocol =		IPPROTO_ETHERIP,
243153621Sthompsa	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
244153621Sthompsa	.pr_input =		encap4_input,
245153621Sthompsa	.pr_ctloutput =		rip_ctloutput,
246153621Sthompsa	.pr_usrreqs =		&rip_usrreqs
247153621Sthompsa},
248153621Sthompsa{
249153621Sthompsa	.pr_type =		SOCK_RAW,
250153621Sthompsa	.pr_domain =		&inetdomain,
251152242Sru	.pr_protocol =		IPPROTO_GRE,
252152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
253152242Sru	.pr_input =		encap4_input,
254152242Sru	.pr_ctloutput =		rip_ctloutput,
255152242Sru	.pr_usrreqs =		&rip_usrreqs
256103026Ssobomax},
25754263Sshin# ifdef INET6
258152242Sru{
259152242Sru	.pr_type =		SOCK_RAW,
260152242Sru	.pr_domain =		&inetdomain,
261152242Sru	.pr_protocol =		IPPROTO_IPV6,
262152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
263152242Sru	.pr_input =		encap4_input,
264152242Sru	.pr_ctloutput =		rip_ctloutput,
265152242Sru	.pr_usrreqs =		&rip_usrreqs
26654263Sshin},
26754263Sshin#endif
268152242Sru{
269152242Sru	.pr_type =		SOCK_RAW,
270152242Sru	.pr_domain =		&inetdomain,
271152242Sru	.pr_protocol =		IPPROTO_PIM,
272152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
273166622Sbms	.pr_input =		encap4_input,
274152242Sru	.pr_ctloutput =		rip_ctloutput,
275152242Sru	.pr_usrreqs =		&rip_usrreqs
276118622Shsu},
277136695Sandre/* Spacer n-times for loadable protocols. */
278136695SandreIPPROTOSPACER,
279136695SandreIPPROTOSPACER,
280136695SandreIPPROTOSPACER,
281136695SandreIPPROTOSPACER,
282136695SandreIPPROTOSPACER,
283136695SandreIPPROTOSPACER,
284136695SandreIPPROTOSPACER,
285136695SandreIPPROTOSPACER,
286136695Sandre/* raw wildcard */
287152242Sru{
288152242Sru	.pr_type =		SOCK_RAW,
289152242Sru	.pr_domain =		&inetdomain,
290152242Sru	.pr_flags =		PR_ATOMIC|PR_ADDR,
291152242Sru	.pr_input =		rip_input,
292152242Sru	.pr_ctloutput =		rip_ctloutput,
293152242Sru	.pr_init =		rip_init,
294152242Sru	.pr_usrreqs =		&rip_usrreqs
2951541Srgrimes},
2961541Srgrimes};
2971541Srgrimes
29892723Salfredextern int in_inithead(void **, int);
299193731Szecextern int in_detachhead(void **, int);
3004073Swollman
301152242Srustruct domain inetdomain = {
302152242Sru	.dom_family =		AF_INET,
303152242Sru	.dom_name =		"internet",
304152242Sru	.dom_protosw =		inetsw,
305298310Spfg	.dom_protoswNPROTOSW =	&inetsw[nitems(inetsw)],
306178167Sqingli#ifdef RADIX_MPATH
307178167Sqingli	.dom_rtattach =		rn4_mpath_inithead,
308178167Sqingli#else
309152242Sru	.dom_rtattach =		in_inithead,
310178167Sqingli#endif
311193731Szec#ifdef VIMAGE
312193731Szec	.dom_rtdetach =		in_detachhead,
313193731Szec#endif
314186119Sqingli	.dom_ifattach =		in_domifattach,
315186119Sqingli	.dom_ifdetach =		in_domifdetach
316152242Sru};
3171541Srgrimes
318195837SrwatsonVNET_DOMAIN_SET(inet);
319220746Sbz#endif /* INET */
3208426Swollman
32112942SwollmanSYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
32212942Swollman	"Internet Family");
32312172Sphk
32412942SwollmanSYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
32512942SwollmanSYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
32612942SwollmanSYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
32712942SwollmanSYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
328163953Srrs#ifdef SCTP
329163953SrrsSYSCTL_NODE(_net_inet, IPPROTO_SCTP,	sctp,	CTLFLAG_RW, 0,	"SCTP");
330163953Srrs#endif
33112942SwollmanSYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
332315514Sae#if defined(IPSEC) || defined(IPSEC_SUPPORT)
333105199Ssam/* XXX no protocol # to use, pick something "reserved" */
334105199SsamSYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
335105199SsamSYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
336105199SsamSYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
337105199SsamSYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
338105199SsamSYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
339171167Sgnn#endif /* IPSEC */
34022900SwollmanSYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
341269142SmarcelSYSCTL_NODE(_net_inet, OID_AUTO,	accf,	CTLFLAG_RW, 0,
342269142Smarcel    "Accept filters");
343