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