in_proto.c revision 152242
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 * $FreeBSD: head/sys/netinet/in_proto.c 152242 2005-11-09 13:29:16Z ru $
31 */
32
33#include "opt_ipx.h"
34#include "opt_mrouting.h"
35#include "opt_ipsec.h"
36#include "opt_inet6.h"
37#include "opt_pf.h"
38#include "opt_carp.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/socket.h>
44#include <sys/domain.h>
45#include <sys/protosw.h>
46#include <sys/queue.h>
47#include <sys/sysctl.h>
48
49#include <net/if.h>
50#include <net/route.h>
51
52#include <netinet/in.h>
53#include <netinet/in_systm.h>
54#include <netinet/ip.h>
55#include <netinet/ip_var.h>
56#include <netinet/ip_icmp.h>
57#include <netinet/igmp_var.h>
58#ifdef PIM
59#include <netinet/pim_var.h>
60#endif
61#include <netinet/tcp.h>
62#include <netinet/tcp_timer.h>
63#include <netinet/tcp_var.h>
64#include <netinet/udp.h>
65#include <netinet/udp_var.h>
66#include <netinet/ip_encap.h>
67
68/*
69 * TCP/IP protocol family: IP, ICMP, UDP, TCP.
70 */
71
72static struct pr_usrreqs nousrreqs;
73
74#ifdef IPSEC
75#include <netinet6/ipsec.h>
76#include <netinet6/ah.h>
77#ifdef IPSEC_ESP
78#include <netinet6/esp.h>
79#endif
80#include <netinet6/ipcomp.h>
81#endif /* IPSEC */
82
83#ifdef FAST_IPSEC
84#include <netipsec/ipsec.h>
85#endif /* FAST_IPSEC */
86
87#ifdef IPXIP
88#include <netipx/ipx_ip.h>
89#endif
90
91#ifdef DEV_PFSYNC
92#include <net/pfvar.h>
93#include <net/if_pfsync.h>
94#endif
95
96#ifdef DEV_CARP
97#include <netinet/ip_carp.h>
98#endif
99
100extern	struct domain inetdomain;
101
102/* Spacer for loadable protocols. */
103#define IPPROTOSPACER   			\
104{						\
105	.pr_domain =		&inetdomain,	\
106	.pr_protocol =		PROTO_SPACER,	\
107	.pr_usrreqs =		&nousrreqs	\
108}
109
110struct protosw inetsw[] = {
111{
112	.pr_type =		0,
113	.pr_domain =		&inetdomain,
114	.pr_protocol =		IPPROTO_IP,
115	.pr_init =		ip_init,
116	.pr_slowtimo =		ip_slowtimo,
117	.pr_drain =		ip_drain,
118	.pr_usrreqs =		&nousrreqs
119},
120{
121	.pr_type =		SOCK_DGRAM,
122	.pr_domain =		&inetdomain,
123	.pr_protocol =		IPPROTO_UDP,
124	.pr_flags =		PR_ATOMIC|PR_ADDR,
125	.pr_input =		udp_input,
126	.pr_ctlinput =		udp_ctlinput,
127	.pr_ctloutput =		ip_ctloutput,
128	.pr_init =		udp_init,
129	.pr_usrreqs =		&udp_usrreqs
130},
131{
132	.pr_type =		SOCK_STREAM,
133	.pr_domain =		&inetdomain,
134	.pr_protocol =		IPPROTO_TCP,
135	.pr_flags =		PR_CONNREQUIRED|PR_IMPLOPCL|PR_WANTRCVD,
136	.pr_input =		tcp_input,
137	.pr_ctlinput =		tcp_ctlinput,
138	.pr_ctloutput =		tcp_ctloutput,
139	.pr_init =		tcp_init,
140	.pr_slowtimo =		tcp_slowtimo,
141	.pr_drain =		tcp_drain,
142	.pr_usrreqs =		&tcp_usrreqs
143},
144{
145	.pr_type =		SOCK_RAW,
146	.pr_domain =		&inetdomain,
147	.pr_protocol =		IPPROTO_RAW,
148	.pr_flags =		PR_ATOMIC|PR_ADDR,
149	.pr_input =		rip_input,
150	.pr_ctlinput =		rip_ctlinput,
151	.pr_ctloutput =		rip_ctloutput,
152	.pr_usrreqs =		&rip_usrreqs
153},
154{
155	.pr_type =		SOCK_RAW,
156	.pr_domain =		&inetdomain,
157	.pr_protocol =		IPPROTO_ICMP,
158	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
159	.pr_input =		icmp_input,
160	.pr_ctloutput =		rip_ctloutput,
161	.pr_usrreqs =		&rip_usrreqs
162},
163{
164	.pr_type =		SOCK_RAW,
165	.pr_domain =		&inetdomain,
166	.pr_protocol =		IPPROTO_IGMP,
167	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
168	.pr_input =		igmp_input,
169	.pr_ctloutput =		rip_ctloutput,
170	.pr_init =		igmp_init,
171	.pr_fasttimo =		igmp_fasttimo,
172	.pr_slowtimo =		igmp_slowtimo,
173	.pr_usrreqs =		&rip_usrreqs
174},
175{
176	.pr_type =		SOCK_RAW,
177	.pr_domain =		&inetdomain,
178	.pr_protocol =		IPPROTO_RSVP,
179	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
180	.pr_input =		rsvp_input,
181	.pr_ctloutput =		rip_ctloutput,
182	.pr_usrreqs =		&rip_usrreqs
183},
184#ifdef IPSEC
185{
186	.pr_type =		SOCK_RAW,
187	.pr_domain =		&inetdomain,
188	.pr_protocol =		IPPROTO_AH,
189	.pr_flags =		PR_ATOMIC|PR_ADDR,
190	.pr_input =		ah4_input,
191	.pr_usrreqs =		&nousrreqs
192},
193#ifdef IPSEC_ESP
194{
195	.pr_type =		SOCK_RAW,
196	.pr_domain =		&inetdomain,
197	.pr_protocol =		IPPROTO_ESP,
198	.pr_flags =		PR_ATOMIC|PR_ADDR,
199	.pr_input =		esp4_input,
200	.pr_usrreqs =		&nousrreqs
201},
202#endif
203{
204	.pr_type =		SOCK_RAW,
205	.pr_domain =		&inetdomain,
206	.pr_protocol =		IPPROTO_IPCOMP,
207	.pr_flags =		PR_ATOMIC|PR_ADDR,
208	.pr_input =		ipcomp4_input,
209	.pr_usrreqs =		&nousrreqs
210},
211#endif /* IPSEC */
212#ifdef FAST_IPSEC
213{
214	.pr_type =		SOCK_RAW,
215	.pr_domain =		&inetdomain,
216	.pr_protocol =		IPPROTO_AH,
217	.pr_flags =		PR_ATOMIC|PR_ADDR,
218	.pr_input =		ah4_input,
219	.pr_ctlinput =		ah4_ctlinput,
220	.pr_usrreqs =		&nousrreqs
221},
222{
223	.pr_type =		SOCK_RAW,
224	.pr_domain =		&inetdomain,
225	.pr_protocol =		IPPROTO_ESP,
226	.pr_flags =		PR_ATOMIC|PR_ADDR,
227	.pr_input =		esp4_input,
228	.pr_ctlinput =		esp4_ctlinput,
229	.pr_usrreqs =		&nousrreqs
230},
231{
232	.pr_type =		SOCK_RAW,
233	.pr_domain =		&inetdomain,
234	.pr_protocol =		IPPROTO_IPCOMP,
235	.pr_flags =		PR_ATOMIC|PR_ADDR,
236	.pr_input =		ipcomp4_input,
237	.pr_usrreqs =		&nousrreqs
238},
239#endif /* FAST_IPSEC */
240{
241	.pr_type =		SOCK_RAW,
242	.pr_domain =		&inetdomain,
243	.pr_protocol =		IPPROTO_IPV4,
244	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
245	.pr_input =		encap4_input,
246	.pr_ctloutput =		rip_ctloutput,
247	.pr_init =		encap_init,
248	.pr_usrreqs =		&rip_usrreqs
249},
250{
251	.pr_type =		SOCK_RAW,
252	.pr_domain =		&inetdomain,
253	.pr_protocol =		IPPROTO_MOBILE,
254	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
255	.pr_input =		encap4_input,
256	.pr_ctloutput =		rip_ctloutput,
257	.pr_init =		encap_init,
258	.pr_usrreqs =		&rip_usrreqs
259},
260{
261	.pr_type =		SOCK_RAW,
262	.pr_domain =		&inetdomain,
263	.pr_protocol =		IPPROTO_GRE,
264	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
265	.pr_input =		encap4_input,
266	.pr_ctloutput =		rip_ctloutput,
267	.pr_init =		encap_init,
268	.pr_usrreqs =		&rip_usrreqs
269},
270# ifdef INET6
271{
272	.pr_type =		SOCK_RAW,
273	.pr_domain =		&inetdomain,
274	.pr_protocol =		IPPROTO_IPV6,
275	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
276	.pr_input =		encap4_input,
277	.pr_ctloutput =		rip_ctloutput,
278	.pr_init =		encap_init,
279	.pr_usrreqs =		&rip_usrreqs
280},
281#endif
282#ifdef IPXIP
283{
284	.pr_type =		SOCK_RAW,
285	.pr_domain =		&inetdomain,
286	.pr_protocol =		IPPROTO_IDP,
287	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
288	.pr_input =		ipxip_input,
289	.pr_ctlinput =		ipxip_ctlinput,
290	.pr_usrreqs =		&rip_usrreqs
291},
292#endif
293#ifdef PIM
294{
295	.pr_type =		SOCK_RAW,
296	.pr_domain =		&inetdomain,
297	.pr_protocol =		IPPROTO_PIM,
298	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_LASTHDR,
299	.pr_input =		pim_input,
300	.pr_ctloutput =		rip_ctloutput,
301	.pr_usrreqs =		&rip_usrreqs
302},
303#endif	/* PIM */
304#ifdef DEV_PFSYNC
305{
306	.pr_type =		SOCK_RAW,
307	.pr_domain =		&inetdomain,
308	.pr_protocol =		IPPROTO_PFSYNC,
309	.pr_flags =		PR_ATOMIC|PR_ADDR,
310	.pr_input =		pfsync_input,
311	.pr_ctloutput =		rip_ctloutput,
312	.pr_usrreqs =		&rip_usrreqs
313},
314#endif	/* DEV_PFSYNC */
315#ifdef DEV_CARP
316{
317	.pr_type =		SOCK_RAW,
318	.pr_domain =		&inetdomain,
319	.pr_protocol =		IPPROTO_CARP,
320	.pr_flags =		PR_ATOMIC|PR_ADDR,
321	.pr_input =		carp_input,
322	.pr_output =		(pr_output_t*)rip_output,
323	.pr_ctloutput =		rip_ctloutput,
324	.pr_usrreqs =		&rip_usrreqs
325},
326#endif /* DEV_CARP */
327/* Spacer n-times for loadable protocols. */
328IPPROTOSPACER,
329IPPROTOSPACER,
330IPPROTOSPACER,
331IPPROTOSPACER,
332IPPROTOSPACER,
333IPPROTOSPACER,
334IPPROTOSPACER,
335IPPROTOSPACER,
336/* raw wildcard */
337{
338	.pr_type =		SOCK_RAW,
339	.pr_domain =		&inetdomain,
340	.pr_flags =		PR_ATOMIC|PR_ADDR,
341	.pr_input =		rip_input,
342	.pr_ctloutput =		rip_ctloutput,
343	.pr_init =		rip_init,
344	.pr_usrreqs =		&rip_usrreqs
345},
346};
347
348extern int in_inithead(void **, int);
349
350struct domain inetdomain = {
351	.dom_family =		AF_INET,
352	.dom_name =		"internet",
353	.dom_protosw =		inetsw,
354	.dom_protoswNPROTOSW =	&inetsw[sizeof(inetsw)/sizeof(inetsw[0])],
355	.dom_rtattach =		in_inithead,
356	.dom_rtoffset =		32,
357	.dom_maxrtkey =		sizeof(struct sockaddr_in)
358};
359
360DOMAIN_SET(inet);
361
362SYSCTL_NODE(_net,      PF_INET,		inet,	CTLFLAG_RW, 0,
363	"Internet Family");
364
365SYSCTL_NODE(_net_inet, IPPROTO_IP,	ip,	CTLFLAG_RW, 0,	"IP");
366SYSCTL_NODE(_net_inet, IPPROTO_ICMP,	icmp,	CTLFLAG_RW, 0,	"ICMP");
367SYSCTL_NODE(_net_inet, IPPROTO_UDP,	udp,	CTLFLAG_RW, 0,	"UDP");
368SYSCTL_NODE(_net_inet, IPPROTO_TCP,	tcp,	CTLFLAG_RW, 0,	"TCP");
369SYSCTL_NODE(_net_inet, IPPROTO_IGMP,	igmp,	CTLFLAG_RW, 0,	"IGMP");
370#ifdef FAST_IPSEC
371/* XXX no protocol # to use, pick something "reserved" */
372SYSCTL_NODE(_net_inet, 253,		ipsec,	CTLFLAG_RW, 0,	"IPSEC");
373SYSCTL_NODE(_net_inet, IPPROTO_AH,	ah,	CTLFLAG_RW, 0,	"AH");
374SYSCTL_NODE(_net_inet, IPPROTO_ESP,	esp,	CTLFLAG_RW, 0,	"ESP");
375SYSCTL_NODE(_net_inet, IPPROTO_IPCOMP,	ipcomp,	CTLFLAG_RW, 0,	"IPCOMP");
376SYSCTL_NODE(_net_inet, IPPROTO_IPIP,	ipip,	CTLFLAG_RW, 0,	"IPIP");
377#else
378#ifdef IPSEC
379SYSCTL_NODE(_net_inet, IPPROTO_AH,	ipsec,	CTLFLAG_RW, 0,	"IPSEC");
380#endif /* IPSEC */
381#endif /* !FAST_IPSEC */
382SYSCTL_NODE(_net_inet, IPPROTO_RAW,	raw,	CTLFLAG_RW, 0,	"RAW");
383#ifdef PIM
384SYSCTL_NODE(_net_inet, IPPROTO_PIM,	pim,	CTLFLAG_RW, 0,	"PIM");
385#endif
386#ifdef DEV_PFSYNC
387SYSCTL_NODE(_net_inet, IPPROTO_PFSYNC,	pfsync,	CTLFLAG_RW, 0,	"PFSYNC");
388#endif
389#ifdef DEV_CARP
390SYSCTL_NODE(_net_inet, IPPROTO_CARP,	carp,	CTLFLAG_RW, 0,	"CARP");
391#endif
392