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