1/*-
2 * Copyright (c) 1982, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)in_proto.c	8.2 (Berkeley) 2/9/95
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include "opt_ipx.h"
36#include "opt_mrouting.h"
37#include "opt_ipsec.h"
38#include "opt_inet.h"
39#include "opt_inet6.h"
40#include "opt_sctp.h"
41#include "opt_mpath.h"
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#include <sys/socket.h>
47#include <sys/domain.h>
48#include <sys/proc.h>
49#include <sys/protosw.h>
50#include <sys/queue.h>
51#include <sys/sysctl.h>
52
53/*
54 * While this file provides the domain and protocol switch tables for IPv4, it
55 * also provides the sysctl node declarations for net.inet.* often shared with
56 * IPv6 for common features or by upper layer protocols.  In case of no IPv4
57 * support compile out everything but these sysctl nodes.
58 */
59#ifdef INET
60#include <net/if.h>
61#include <net/route.h>
62#ifdef RADIX_MPATH
63#include <net/radix_mpath.h>
64#endif
65#include <net/vnet.h>
66#endif /* INET */
67
68#if defined(INET) || defined(INET6)
69#include <netinet/in.h>
70#endif
71
72#ifdef INET
73#include <netinet/in_systm.h>
74#include <netinet/in_var.h>
75#include <netinet/ip.h>
76#include <netinet/ip_var.h>
77#include <netinet/ip_icmp.h>
78#include <netinet/igmp_var.h>
79#include <netinet/tcp.h>
80#include <netinet/tcp_timer.h>
81#include <netinet/tcp_var.h>
82#include <netinet/udp.h>
83#include <netinet/udp_var.h>
84#include <netinet/ip_encap.h>
85
86/*
87 * TCP/IP protocol family: IP, ICMP, UDP, TCP.
88 */
89
90static struct pr_usrreqs nousrreqs;
91
92#ifdef IPSEC
93#include <netipsec/ipsec.h>
94#endif /* IPSEC */
95
96#ifdef SCTP
97#include <netinet/in_pcb.h>
98#include <netinet/sctp_pcb.h>
99#include <netinet/sctp.h>
100#include <netinet/sctp_var.h>
101#endif /* SCTP */
102
103FEATURE(inet, "Internet Protocol version 4");
104
105extern	struct domain inetdomain;
106
107/* Spacer for loadable protocols. */
108#define IPPROTOSPACER   			\
109{						\
110	.pr_domain =		&inetdomain,	\
111	.pr_protocol =		PROTO_SPACER,	\
112	.pr_usrreqs =		&nousrreqs	\
113}
114
115struct protosw inetsw[] = {
116{
117	.pr_type =		0,
118	.pr_domain =		&inetdomain,
119	.pr_protocol =		IPPROTO_IP,
120	.pr_init =		ip_init,
121#ifdef VIMAGE
122	.pr_destroy =		ip_destroy,
123#endif
124	.pr_slowtimo =		ip_slowtimo,
125	.pr_drain =		ip_drain,
126	.pr_usrreqs =		&nousrreqs
127},
128{
129	.pr_type =		SOCK_DGRAM,
130	.pr_domain =		&inetdomain,
131	.pr_protocol =		IPPROTO_UDP,
132	.pr_flags =		PR_ATOMIC|PR_ADDR,
133	.pr_input =		udp_input,
134	.pr_ctlinput =		udp_ctlinput,
135	.pr_ctloutput =		udp_ctloutput,
136	.pr_init =		udp_init,
137#ifdef VIMAGE
138	.pr_destroy =		udp_destroy,
139#endif
140	.pr_usrreqs =		&udp_usrreqs
141},
142{
143	.pr_type =		SOCK_STREAM,
144	.pr_domain =		&inetdomain,
145	.pr_protocol =		IPPROTO_TCP,
146	.pr_flags =		PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
147	.pr_input =		tcp_input,
148	.pr_ctlinput =		tcp_ctlinput,
149	.pr_ctloutput =		tcp_ctloutput,
150	.pr_init =		tcp_init,
151#ifdef VIMAGE
152	.pr_destroy =		tcp_destroy,
153#endif
154	.pr_slowtimo =		tcp_slowtimo,
155	.pr_drain =		tcp_drain,
156	.pr_usrreqs =		&tcp_usrreqs
157},
158#ifdef SCTP
159{
160	.pr_type =		SOCK_SEQPACKET,
161	.pr_domain =		&inetdomain,
162	.pr_protocol =		IPPROTO_SCTP,
163	.pr_flags =		PR_WANTRCVD,
164	.pr_input =		sctp_input,
165	.pr_ctlinput =		sctp_ctlinput,
166	.pr_ctloutput =		sctp_ctloutput,
167	.pr_init =		sctp_init,
168#ifdef VIMAGE
169	.pr_destroy =		sctp_finish,
170#endif
171	.pr_drain =		sctp_drain,
172	.pr_usrreqs =		&sctp_usrreqs
173},
174{
175	.pr_type =		SOCK_STREAM,
176	.pr_domain =		&inetdomain,
177	.pr_protocol =		IPPROTO_SCTP,
178	.pr_flags =		PR_WANTRCVD,
179	.pr_input =		sctp_input,
180	.pr_ctlinput =		sctp_ctlinput,
181	.pr_ctloutput =		sctp_ctloutput,
182	.pr_drain =		sctp_drain,
183	.pr_usrreqs =		&sctp_usrreqs
184},
185#endif /* SCTP */
186{
187	.pr_type =		SOCK_RAW,
188	.pr_domain =		&inetdomain,
189	.pr_protocol =		IPPROTO_RAW,
190	.pr_flags =		PR_ATOMIC|PR_ADDR,
191	.pr_input =		rip_input,
192	.pr_ctlinput =		rip_ctlinput,
193	.pr_ctloutput =		rip_ctloutput,
194	.pr_usrreqs =		&rip_usrreqs
195},
196{
197	.pr_type =		SOCK_RAW,
198	.pr_domain =		&inetdomain,
199	.pr_protocol =		IPPROTO_ICMP,
200	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
201	.pr_input =		icmp_input,
202	.pr_ctloutput =		rip_ctloutput,
203	.pr_usrreqs =		&rip_usrreqs
204},
205{
206	.pr_type =		SOCK_RAW,
207	.pr_domain =		&inetdomain,
208	.pr_protocol =		IPPROTO_IGMP,
209	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
210	.pr_input =		igmp_input,
211	.pr_ctloutput =		rip_ctloutput,
212	.pr_fasttimo =		igmp_fasttimo,
213	.pr_slowtimo =		igmp_slowtimo,
214	.pr_usrreqs =		&rip_usrreqs
215},
216{
217	.pr_type =		SOCK_RAW,
218	.pr_domain =		&inetdomain,
219	.pr_protocol =		IPPROTO_RSVP,
220	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
221	.pr_input =		rsvp_input,
222	.pr_ctloutput =		rip_ctloutput,
223	.pr_usrreqs =		&rip_usrreqs
224},
225#ifdef IPSEC
226{
227	.pr_type =		SOCK_RAW,
228	.pr_domain =		&inetdomain,
229	.pr_protocol =		IPPROTO_AH,
230	.pr_flags =		PR_ATOMIC|PR_ADDR,
231	.pr_input =		ah4_input,
232	.pr_ctlinput =		ah4_ctlinput,
233	.pr_usrreqs =		&nousrreqs
234},
235{
236	.pr_type =		SOCK_RAW,
237	.pr_domain =		&inetdomain,
238	.pr_protocol =		IPPROTO_ESP,
239	.pr_flags =		PR_ATOMIC|PR_ADDR,
240	.pr_input =		esp4_input,
241	.pr_ctlinput =		esp4_ctlinput,
242	.pr_usrreqs =		&nousrreqs
243},
244{
245	.pr_type =		SOCK_RAW,
246	.pr_domain =		&inetdomain,
247	.pr_protocol =		IPPROTO_IPCOMP,
248	.pr_flags =		PR_ATOMIC|PR_ADDR,
249	.pr_input =		ipcomp4_input,
250	.pr_usrreqs =		&nousrreqs
251},
252#endif /* IPSEC */
253{
254	.pr_type =		SOCK_RAW,
255	.pr_domain =		&inetdomain,
256	.pr_protocol =		IPPROTO_IPV4,
257	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
258	.pr_input =		encap4_input,
259	.pr_ctloutput =		rip_ctloutput,
260	.pr_init =		encap_init,
261	.pr_usrreqs =		&rip_usrreqs
262},
263{
264	.pr_type =		SOCK_RAW,
265	.pr_domain =		&inetdomain,
266	.pr_protocol =		IPPROTO_MOBILE,
267	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
268	.pr_input =		encap4_input,
269	.pr_ctloutput =		rip_ctloutput,
270	.pr_init =		encap_init,
271	.pr_usrreqs =		&rip_usrreqs
272},
273{
274	.pr_type =		SOCK_RAW,
275	.pr_domain =		&inetdomain,
276	.pr_protocol =		IPPROTO_ETHERIP,
277	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
278	.pr_input =		encap4_input,
279	.pr_ctloutput =		rip_ctloutput,
280	.pr_init =		encap_init,
281	.pr_usrreqs =		&rip_usrreqs
282},
283{
284	.pr_type =		SOCK_RAW,
285	.pr_domain =		&inetdomain,
286	.pr_protocol =		IPPROTO_GRE,
287	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
288	.pr_input =		encap4_input,
289	.pr_ctloutput =		rip_ctloutput,
290	.pr_init =		encap_init,
291	.pr_usrreqs =		&rip_usrreqs
292},
293# ifdef INET6
294{
295	.pr_type =		SOCK_RAW,
296	.pr_domain =		&inetdomain,
297	.pr_protocol =		IPPROTO_IPV6,
298	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
299	.pr_input =		encap4_input,
300	.pr_ctloutput =		rip_ctloutput,
301	.pr_init =		encap_init,
302	.pr_usrreqs =		&rip_usrreqs
303},
304#endif
305{
306	.pr_type =		SOCK_RAW,
307	.pr_domain =		&inetdomain,
308	.pr_protocol =		IPPROTO_PIM,
309	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
310	.pr_input =		encap4_input,
311	.pr_ctloutput =		rip_ctloutput,
312	.pr_usrreqs =		&rip_usrreqs
313},
314/* Spacer n-times for loadable protocols. */
315IPPROTOSPACER,
316IPPROTOSPACER,
317IPPROTOSPACER,
318IPPROTOSPACER,
319IPPROTOSPACER,
320IPPROTOSPACER,
321IPPROTOSPACER,
322IPPROTOSPACER,
323/* raw wildcard */
324{
325	.pr_type =		SOCK_RAW,
326	.pr_domain =		&inetdomain,
327	.pr_flags =		PR_ATOMIC|PR_ADDR,
328	.pr_input =		rip_input,
329	.pr_ctloutput =		rip_ctloutput,
330	.pr_init =		rip_init,
331#ifdef VIMAGE
332	.pr_destroy =		rip_destroy,
333#endif
334	.pr_usrreqs =		&rip_usrreqs
335},
336};
337
338extern int in_inithead(void **, int);
339extern int in_detachhead(void **, int);
340
341struct domain inetdomain = {
342	.dom_family =		AF_INET,
343	.dom_name =		"internet",
344	.dom_protosw =		inetsw,
345	.dom_protoswNPROTOSW =	&inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
346#ifdef RADIX_MPATH
347	.dom_rtattach =		rn4_mpath_inithead,
348#else
349	.dom_rtattach =		in_inithead,
350#endif
351#ifdef VIMAGE
352	.dom_rtdetach =		in_detachhead,
353#endif
354	.dom_rtoffset =		32,
355	.dom_maxrtkey =		sizeof(struct sockaddr_in),
356	.dom_ifattach =		in_domifattach,
357	.dom_ifdetach =		in_domifdetach
358};
359
360VNET_DOMAIN_SET(inet);
361#endif /* INET */
362
363SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
364	"Internet Family");
365
366SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
367SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
368SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
369SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
370#ifdef SCTP
371SYSCTL_NODE(_net_inet, IPPROTO_SCTP,	sctp,	CTLFLAG_RW, 0,	"SCTP");
372#endif
373SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
374#ifdef IPSEC
375/* XXX no protocol # to use, pick something "reserved" */
376SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
377SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
378SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
379SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
380SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
381#endif /* IPSEC */
382SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
383