]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/in_proto.c
xnu-6153.81.5.tar.gz
[apple/xnu.git] / bsd / netinet / in_proto.c
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)in_proto.c 8.2 (Berkeley) 2/9/95
61 */
62
1c79356b
A
63#include <sys/param.h>
64#include <sys/kernel.h>
65#include <sys/socket.h>
66#include <sys/domain.h>
67#include <sys/protosw.h>
9bccf70c 68#include <sys/queue.h>
1c79356b 69#include <sys/sysctl.h>
91447636 70#include <sys/mbuf.h>
1c79356b 71
39236c6e
A
72#include <kern/debug.h>
73
1c79356b
A
74#include <net/if.h>
75#include <net/route.h>
39037602 76#include <net/kpi_protocol.h>
1c79356b
A
77
78#include <netinet/in.h>
39236c6e 79#include <netinet/in_var.h>
1c79356b
A
80#include <netinet/in_systm.h>
81#include <netinet/ip.h>
82#include <netinet/ip_var.h>
1c79356b
A
83#include <netinet/ip_icmp.h>
84#include <netinet/igmp_var.h>
85#include <netinet/tcp.h>
86#include <netinet/tcp_timer.h>
87#include <netinet/tcp_var.h>
88#include <netinet/tcpip.h>
89#include <netinet/udp.h>
90#include <netinet/udp_var.h>
9bccf70c 91#include <netinet/ip_encap.h>
91447636 92#include <netinet/ip_divert.h>
9bccf70c
A
93
94
1c79356b
A
95/*
96 * TCP/IP protocol family: IP, ICMP, UDP, TCP.
97 */
98
99#if IPSEC
100#include <netinet6/ipsec.h>
101#include <netinet6/ah.h>
102#if IPSEC_ESP
103#include <netinet6/esp.h>
104#endif
1c79356b
A
105#endif /* IPSEC */
106
39236c6e
A
107static void in_dinit(struct domain *);
108static void ip_proto_input(protocol_family_t, mbuf_t);
1c79356b 109
39236c6e
A
110extern struct domain inetdomain_s;
111static struct pr_usrreqs nousrreqs;
112extern struct pr_usrreqs icmp_dgram_usrreqs;
9bccf70c 113extern int icmp_dgram_ctloutput(struct socket *, struct sockopt *);
1c79356b 114
39236c6e
A
115struct domain *inetdomain = NULL;
116
117/* Thanks to PPP, this still needs to be exported */
0a7de745 118lck_mtx_t *inet_domain_mutex;
91447636 119
39236c6e 120static struct protosw inetsw[] = {
0a7de745
A
121 {
122 .pr_type = 0,
123 .pr_protocol = 0,
124 .pr_init = ip_init,
125 .pr_drain = ip_drain,
126 .pr_usrreqs = &nousrreqs,
127 },
128 {
129 .pr_type = SOCK_DGRAM,
130 .pr_protocol = IPPROTO_UDP,
131 .pr_flags = PR_ATOMIC | PR_ADDR | PR_PROTOLOCK | PR_PCBLOCK |
132 PR_EVCONNINFO | PR_PRECONN_WRITE,
133 .pr_input = udp_input,
134 .pr_ctlinput = udp_ctlinput,
135 .pr_ctloutput = udp_ctloutput,
136 .pr_init = udp_init,
137 .pr_usrreqs = &udp_usrreqs,
138 .pr_lock = udp_lock,
139 .pr_unlock = udp_unlock,
140 .pr_getlock = udp_getlock,
cb323159
A
141 .pr_update_last_owner = inp_update_last_owner,
142 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745
A
143 },
144 {
145 .pr_type = SOCK_STREAM,
146 .pr_protocol = IPPROTO_TCP,
147 .pr_flags = PR_CONNREQUIRED | PR_WANTRCVD | PR_PCBLOCK |
148 PR_PROTOLOCK | PR_DISPOSE | PR_EVCONNINFO |
149 PR_PRECONN_WRITE | PR_DATA_IDEMPOTENT,
150 .pr_input = tcp_input,
151 .pr_ctlinput = tcp_ctlinput,
152 .pr_ctloutput = tcp_ctloutput,
153 .pr_init = tcp_init,
154 .pr_drain = tcp_drain,
155 .pr_usrreqs = &tcp_usrreqs,
156 .pr_lock = tcp_lock,
157 .pr_unlock = tcp_unlock,
158 .pr_getlock = tcp_getlock,
cb323159
A
159 .pr_update_last_owner = inp_update_last_owner,
160 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745
A
161 },
162 {
163 .pr_type = SOCK_RAW,
164 .pr_protocol = IPPROTO_RAW,
165 .pr_flags = PR_ATOMIC | PR_ADDR,
166 .pr_input = rip_input,
167 .pr_ctlinput = rip_ctlinput,
168 .pr_ctloutput = rip_ctloutput,
169 .pr_usrreqs = &rip_usrreqs,
170 .pr_unlock = rip_unlock,
cb323159
A
171 .pr_update_last_owner = inp_update_last_owner,
172 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745
A
173 },
174 {
175 .pr_type = SOCK_RAW,
176 .pr_protocol = IPPROTO_ICMP,
177 .pr_flags = PR_ATOMIC | PR_ADDR | PR_LASTHDR,
178 .pr_input = icmp_input,
179 .pr_ctloutput = rip_ctloutput,
180 .pr_usrreqs = &rip_usrreqs,
181 .pr_unlock = rip_unlock,
cb323159
A
182 .pr_update_last_owner = inp_update_last_owner,
183 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745
A
184 },
185 {
186 .pr_type = SOCK_DGRAM,
187 .pr_protocol = IPPROTO_ICMP,
188 .pr_flags = PR_ATOMIC | PR_ADDR | PR_LASTHDR,
189 .pr_input = icmp_input,
190 .pr_ctloutput = icmp_dgram_ctloutput,
191 .pr_usrreqs = &icmp_dgram_usrreqs,
192 .pr_unlock = rip_unlock,
cb323159
A
193 .pr_update_last_owner = inp_update_last_owner,
194 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745
A
195 },
196 {
197 .pr_type = SOCK_RAW,
198 .pr_protocol = IPPROTO_IGMP,
199 .pr_flags = PR_ATOMIC | PR_ADDR | PR_LASTHDR,
200 .pr_input = igmp_input,
201 .pr_ctloutput = rip_ctloutput,
202 .pr_init = igmp_init,
203 .pr_usrreqs = &rip_usrreqs,
204 .pr_unlock = rip_unlock,
cb323159
A
205 .pr_update_last_owner = inp_update_last_owner,
206 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745
A
207 },
208 {
209 .pr_type = SOCK_RAW,
210 .pr_protocol = IPPROTO_GRE,
211 .pr_flags = PR_ATOMIC | PR_ADDR,
212 .pr_input = gre_input,
213 .pr_ctlinput = rip_ctlinput,
214 .pr_ctloutput = rip_ctloutput,
215 .pr_usrreqs = &rip_usrreqs,
216 .pr_unlock = rip_unlock,
cb323159
A
217 .pr_update_last_owner = inp_update_last_owner,
218 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745 219 },
1c79356b 220#if IPSEC
0a7de745
A
221 {
222 .pr_type = SOCK_RAW,
223 .pr_protocol = IPPROTO_AH,
224 .pr_flags = PR_ATOMIC | PR_ADDR | PR_PROTOLOCK,
225 .pr_input = ah4_input,
226 .pr_usrreqs = &nousrreqs,
227 },
1c79356b 228#if IPSEC_ESP
0a7de745
A
229 {
230 .pr_type = SOCK_RAW,
231 .pr_protocol = IPPROTO_ESP,
232 .pr_flags = PR_ATOMIC | PR_ADDR | PR_PROTOLOCK,
233 .pr_input = esp4_input,
234 .pr_usrreqs = &nousrreqs,
235 },
39236c6e 236#endif /* IPSEC_ESP */
1c79356b 237#endif /* IPSEC */
0a7de745
A
238 {
239 .pr_type = SOCK_RAW,
240 .pr_protocol = IPPROTO_IPV4,
241 .pr_flags = PR_ATOMIC | PR_ADDR | PR_LASTHDR,
242 .pr_input = encap4_input,
243 .pr_ctloutput = rip_ctloutput,
244 .pr_init = encap4_init,
245 .pr_usrreqs = &rip_usrreqs,
246 .pr_unlock = rip_unlock,
cb323159
A
247 .pr_update_last_owner = inp_update_last_owner,
248 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745 249 },
39236c6e 250#if INET6
0a7de745
A
251 {
252 .pr_type = SOCK_RAW,
253 .pr_protocol = IPPROTO_IPV6,
254 .pr_flags = PR_ATOMIC | PR_ADDR | PR_LASTHDR,
255 .pr_input = encap4_input,
256 .pr_ctloutput = rip_ctloutput,
257 .pr_init = encap4_init,
258 .pr_usrreqs = &rip_usrreqs,
259 .pr_unlock = rip_unlock,
cb323159
A
260 .pr_update_last_owner = inp_update_last_owner,
261 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745 262 },
39236c6e 263#endif /* INET6 */
1c79356b 264#if IPDIVERT
0a7de745
A
265 {
266 .pr_type = SOCK_RAW,
267 .pr_protocol = IPPROTO_DIVERT,
268 .pr_flags = PR_ATOMIC | PR_ADDR | PR_PCBLOCK,
269 .pr_input = div_input,
270 .pr_ctloutput = ip_ctloutput,
271 .pr_init = div_init,
272 .pr_usrreqs = &div_usrreqs,
273 .pr_lock = div_lock,
274 .pr_unlock = div_unlock,
275 .pr_getlock = div_getlock,
276 },
39236c6e
A
277#endif /* IPDIVERT */
278/* raw wildcard */
0a7de745
A
279 {
280 .pr_type = SOCK_RAW,
281 .pr_flags = PR_ATOMIC | PR_ADDR | PR_LASTHDR,
282 .pr_input = rip_input,
283 .pr_ctloutput = rip_ctloutput,
284 .pr_init = rip_init,
285 .pr_usrreqs = &rip_usrreqs,
286 .pr_unlock = rip_unlock,
cb323159
A
287 .pr_update_last_owner = inp_update_last_owner,
288 .pr_copy_last_owner = inp_copy_last_owner,
0a7de745 289 },
1c79356b
A
290};
291
0a7de745 292static int in_proto_count = (sizeof(inetsw) / sizeof(struct protosw));
39236c6e
A
293
294struct domain inetdomain_s = {
0a7de745
A
295 .dom_family = PF_INET,
296 .dom_flags = DOM_REENTRANT,
297 .dom_name = "internet",
298 .dom_init = in_dinit,
299 .dom_rtattach = in_inithead,
300 .dom_rtoffset = 32,
301 .dom_maxrtkey = sizeof(struct sockaddr_in),
302 .dom_protohdrlen = sizeof(struct tcpiphdr),
39236c6e
A
303};
304
305/* Initialize the PF_INET domain, and add in the pre-defined protos */
306void
307in_dinit(struct domain *dp)
308{
309 struct protosw *pr;
310 int i;
311 domain_unguard_t unguard;
312
313 VERIFY(!(dp->dom_flags & DOM_INITIALIZED));
314 VERIFY(inetdomain == NULL);
1c79356b 315
39236c6e 316 inetdomain = dp;
1c79356b 317
39037602
A
318 /*
319 * Verify that the maximum possible tcp/ip header will still
320 * fit in a small mbuf because m_pullup only puls into 256
321 * byte mbuf
322 */
323 _CASSERT((sizeof(struct tcpiphdr) + TCP_MAXOLEN) <= _MHLEN);
324
39236c6e
A
325 /*
326 * Attach first, then initialize; ip_init() needs raw IP handler.
327 */
0a7de745 328 for (i = 0, pr = &inetsw[0]; i < in_proto_count; i++, pr++) {
39236c6e 329 net_add_proto(pr, dp, 0);
0a7de745
A
330 }
331 for (i = 0, pr = &inetsw[0]; i < in_proto_count; i++, pr++) {
39236c6e 332 net_init_proto(pr, dp);
0a7de745 333 }
1c79356b 334
39236c6e 335 inet_domain_mutex = dp->dom_mtx;
1c79356b 336
39236c6e
A
337 unguard = domain_unguard_deploy();
338 i = proto_register_input(PF_INET, ip_proto_input, NULL, 1);
339 if (i != 0) {
340 panic("%s: failed to register PF_INET protocol: %d\n",
341 __func__, i);
342 /* NOTREACHED */
343 }
344 domain_unguard_release(unguard);
345}
1c79356b 346
39236c6e
A
347static void
348ip_proto_input(protocol_family_t protocol, mbuf_t packet_list)
349{
350#pragma unused(protocol)
39236c6e 351
3e170ce0
A
352 if (packet_list->m_nextpkt != NULL) {
353 ip_input_process_list(packet_list);
354 } else {
355 /*
356 * XXX remove this path if ip_input_process_list is proven
357 * to be stable and has minimum overhead on most platforms.
358 */
359 ip_input(packet_list);
39236c6e
A
360 }
361}
362
363SYSCTL_NODE(_net, PF_INET, inet,
0a7de745 364 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "Internet Family");
39236c6e
A
365
366SYSCTL_NODE(_net_inet, IPPROTO_IP, ip,
0a7de745 367 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IP");
39236c6e 368SYSCTL_NODE(_net_inet, IPPROTO_ICMP, icmp,
0a7de745 369 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "ICMP");
39236c6e 370SYSCTL_NODE(_net_inet, IPPROTO_UDP, udp,
0a7de745 371 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "UDP");
39236c6e 372SYSCTL_NODE(_net_inet, IPPROTO_TCP, tcp,
0a7de745 373 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "TCP");
39236c6e 374SYSCTL_NODE(_net_inet, IPPROTO_IGMP, igmp,
0a7de745 375 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IGMP");
1c79356b 376#if IPSEC
39236c6e 377SYSCTL_NODE(_net_inet, IPPROTO_AH, ipsec,
0a7de745 378 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IPSEC");
1c79356b 379#endif /* IPSEC */
39236c6e 380SYSCTL_NODE(_net_inet, IPPROTO_RAW, raw,
0a7de745 381 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "RAW");
1c79356b 382#if IPDIVERT
39236c6e 383SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, div,
0a7de745 384 CTLFLAG_RW | CTLFLAG_LOCKED, 0, "DIVERT");
39236c6e 385#endif /* IPDIVERT */