]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 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 | * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 | |
9bccf70c | 61 | * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $ |
1c79356b | 62 | */ |
2d21ac55 A |
63 | /* |
64 | * NOTICE: This file was modified by SPARTA, Inc. in 2007 to introduce | |
65 | * support for mandatory and extensible security protections. This notice | |
66 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
67 | * Version 2.0. | |
68 | */ | |
1c79356b A |
69 | |
70 | #define _IP_VHL | |
71 | ||
1c79356b A |
72 | #include <sys/param.h> |
73 | #include <sys/systm.h> | |
74 | #include <sys/mbuf.h> | |
75 | #include <sys/malloc.h> | |
76 | #include <sys/domain.h> | |
77 | #include <sys/protosw.h> | |
78 | #include <sys/socket.h> | |
79 | #include <sys/time.h> | |
80 | #include <sys/kernel.h> | |
81 | #include <sys/syslog.h> | |
82 | #include <sys/sysctl.h> | |
83 | ||
84 | #include <kern/queue.h> | |
91447636 | 85 | #include <kern/locks.h> |
1c79356b | 86 | |
2d21ac55 A |
87 | #include <pexpert/pexpert.h> |
88 | ||
1c79356b A |
89 | #include <net/if.h> |
90 | #include <net/if_var.h> | |
91 | #include <net/if_dl.h> | |
92 | #include <net/route.h> | |
91447636 | 93 | #include <net/kpi_protocol.h> |
1c79356b A |
94 | |
95 | #include <netinet/in.h> | |
96 | #include <netinet/in_systm.h> | |
97 | #include <netinet/in_var.h> | |
98 | #include <netinet/ip.h> | |
1c79356b A |
99 | #include <netinet/in_pcb.h> |
100 | #include <netinet/ip_var.h> | |
101 | #include <netinet/ip_icmp.h> | |
102 | #include <sys/socketvar.h> | |
103 | ||
9bccf70c | 104 | #include <netinet/ip_fw.h> |
91447636 A |
105 | #include <netinet/ip_divert.h> |
106 | ||
107 | #include <netinet/kpi_ipfilter_var.h> | |
1c79356b | 108 | |
9bccf70c A |
109 | /* needed for AUTOCONFIGURING: */ |
110 | #include <netinet/udp.h> | |
111 | #include <netinet/udp_var.h> | |
112 | #include <netinet/bootp.h> | |
113 | ||
2d21ac55 A |
114 | #if CONFIG_MACF_NET |
115 | #include <security/mac_framework.h> | |
116 | #endif | |
117 | ||
9bccf70c | 118 | #include <sys/kdebug.h> |
2d21ac55 | 119 | #include <libkern/OSAtomic.h> |
1c79356b A |
120 | |
121 | #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0) | |
122 | #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2) | |
123 | #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8)) | |
124 | ||
125 | ||
1c79356b A |
126 | #if IPSEC |
127 | #include <netinet6/ipsec.h> | |
128 | #include <netkey/key.h> | |
1c79356b A |
129 | #endif |
130 | ||
131 | #include "faith.h" | |
132 | #if defined(NFAITH) && NFAITH > 0 | |
133 | #include <net/if_types.h> | |
134 | #endif | |
135 | ||
136 | #if DUMMYNET | |
137 | #include <netinet/ip_dummynet.h> | |
138 | #endif | |
139 | ||
9bccf70c A |
140 | #if IPSEC |
141 | extern int ipsec_bypass; | |
91447636 | 142 | extern lck_mtx_t *sadb_mutex; |
9bccf70c A |
143 | #endif |
144 | ||
1c79356b A |
145 | int rsvp_on = 0; |
146 | static int ip_rsvp_on; | |
147 | struct socket *ip_rsvpd; | |
148 | ||
149 | int ipforwarding = 0; | |
150 | SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, | |
9bccf70c | 151 | &ipforwarding, 0, "Enable IP forwarding between interfaces"); |
1c79356b A |
152 | |
153 | static int ipsendredirects = 1; /* XXX */ | |
154 | SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, | |
9bccf70c | 155 | &ipsendredirects, 0, "Enable sending IP redirects"); |
1c79356b A |
156 | |
157 | int ip_defttl = IPDEFTTL; | |
158 | SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, | |
9bccf70c | 159 | &ip_defttl, 0, "Maximum TTL on IP packets"); |
1c79356b A |
160 | |
161 | static int ip_dosourceroute = 0; | |
162 | SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, | |
9bccf70c | 163 | &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); |
1c79356b A |
164 | |
165 | static int ip_acceptsourceroute = 0; | |
9bccf70c A |
166 | SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, |
167 | CTLFLAG_RW, &ip_acceptsourceroute, 0, | |
168 | "Enable accepting source routed IP packets"); | |
1c79356b A |
169 | |
170 | static int ip_keepfaith = 0; | |
171 | SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, | |
9bccf70c A |
172 | &ip_keepfaith, 0, |
173 | "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); | |
174 | ||
483a1d10 | 175 | static int nipq = 0; /* total # of reass queues */ |
91447636 | 176 | static int maxnipq; |
9bccf70c | 177 | SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW, |
483a1d10 | 178 | &maxnipq, 0, |
9bccf70c A |
179 | "Maximum number of IPv4 fragment reassembly queue entries"); |
180 | ||
483a1d10 A |
181 | static int maxfragsperpacket; |
182 | SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW, | |
183 | &maxfragsperpacket, 0, | |
184 | "Maximum number of IPv4 fragments allowed per packet"); | |
185 | ||
91447636 A |
186 | static int maxfrags; |
187 | SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfrags, CTLFLAG_RW, | |
188 | &maxfrags, 0, "Maximum number of IPv4 fragments allowed"); | |
189 | ||
190 | static int currentfrags = 0; | |
191 | ||
9bccf70c A |
192 | /* |
193 | * XXX - Setting ip_checkinterface mostly implements the receive side of | |
194 | * the Strong ES model described in RFC 1122, but since the routing table | |
195 | * and transmit implementation do not implement the Strong ES model, | |
196 | * setting this to 1 results in an odd hybrid. | |
197 | * | |
198 | * XXX - ip_checkinterface currently must be disabled if you use ipnat | |
199 | * to translate the destination address to another local interface. | |
200 | * | |
201 | * XXX - ip_checkinterface must be disabled if you add IP aliases | |
202 | * to the loopback interface instead of the interface where the | |
203 | * packets for those addresses are received. | |
204 | */ | |
205 | static int ip_checkinterface = 0; | |
206 | SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW, | |
207 | &ip_checkinterface, 0, "Verify packet arrives on correct interface"); | |
1c79356b | 208 | |
2d21ac55 | 209 | |
1c79356b A |
210 | #if DIAGNOSTIC |
211 | static int ipprintfs = 0; | |
212 | #endif | |
213 | ||
2d21ac55 | 214 | extern int in_proto_count; |
1c79356b A |
215 | extern struct domain inetdomain; |
216 | extern struct protosw inetsw[]; | |
217 | struct protosw *ip_protox[IPPROTO_MAX]; | |
218 | static int ipqmaxlen = IFQ_MAXLEN; | |
219 | struct in_ifaddrhead in_ifaddrhead; /* first inet address */ | |
220 | struct ifqueue ipintrq; | |
9bccf70c A |
221 | SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW, |
222 | &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); | |
1c79356b | 223 | SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, |
9bccf70c | 224 | &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue"); |
1c79356b A |
225 | |
226 | struct ipstat ipstat; | |
227 | SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD, | |
9bccf70c | 228 | &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); |
1c79356b A |
229 | |
230 | /* Packet reassembly stuff */ | |
231 | #define IPREASS_NHASH_LOG2 6 | |
232 | #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) | |
233 | #define IPREASS_HMASK (IPREASS_NHASH - 1) | |
234 | #define IPREASS_HASH(x,y) \ | |
9bccf70c | 235 | (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) |
1c79356b A |
236 | |
237 | static struct ipq ipq[IPREASS_NHASH]; | |
91447636 A |
238 | static TAILQ_HEAD(ipq_list, ipq) ipq_list = |
239 | TAILQ_HEAD_INITIALIZER(ipq_list); | |
9bccf70c | 240 | const int ipintrq_present = 1; |
2d21ac55 | 241 | lck_mtx_t *ip_mutex; |
91447636 | 242 | lck_attr_t *ip_mutex_attr; |
2d21ac55 A |
243 | lck_grp_t *ip_mutex_grp; |
244 | lck_grp_attr_t *ip_mutex_grp_attr; | |
91447636 | 245 | lck_mtx_t *inet_domain_mutex; |
2d21ac55 | 246 | extern lck_mtx_t *domain_proto_mtx; |
1c79356b A |
247 | |
248 | #if IPCTL_DEFMTU | |
249 | SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, | |
9bccf70c | 250 | &ip_mtu, 0, "Default MTU"); |
1c79356b A |
251 | #endif |
252 | ||
9bccf70c A |
253 | #if IPSTEALTH |
254 | static int ipstealth = 0; | |
255 | SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, | |
256 | &ipstealth, 0, ""); | |
1c79356b A |
257 | #endif |
258 | ||
1c79356b A |
259 | |
260 | /* Firewall hooks */ | |
4a3eedf9 | 261 | #if IPFIREWALL |
1c79356b | 262 | ip_fw_chk_t *ip_fw_chk_ptr; |
2d21ac55 A |
263 | int fw_enable = 1; |
264 | int fw_bypass = 1; | |
265 | int fw_one_pass = 0; | |
1c79356b A |
266 | |
267 | #if DUMMYNET | |
91447636 | 268 | ip_dn_io_t *ip_dn_io_ptr; |
1c79356b A |
269 | #endif |
270 | ||
91447636 | 271 | int (*fr_checkp)(struct ip *, int, struct ifnet *, int, struct mbuf **) = NULL; |
4a3eedf9 | 272 | #endif /* IPFIREWALL */ |
9bccf70c | 273 | |
2d21ac55 | 274 | SYSCTL_NODE(_net_inet_ip, OID_AUTO, linklocal, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "link local"); |
9bccf70c A |
275 | |
276 | struct ip_linklocal_stat ip_linklocal_stat; | |
277 | SYSCTL_STRUCT(_net_inet_ip_linklocal, OID_AUTO, stat, CTLFLAG_RD, | |
278 | &ip_linklocal_stat, ip_linklocal_stat, | |
279 | "Number of link local packets with TTL less than 255"); | |
280 | ||
2d21ac55 | 281 | SYSCTL_NODE(_net_inet_ip_linklocal, OID_AUTO, in, CTLFLAG_RW|CTLFLAG_LOCKED, 0, "link local input"); |
9bccf70c | 282 | |
91447636 | 283 | int ip_linklocal_in_allowbadttl = 1; |
9bccf70c A |
284 | SYSCTL_INT(_net_inet_ip_linklocal_in, OID_AUTO, allowbadttl, CTLFLAG_RW, |
285 | &ip_linklocal_in_allowbadttl, 0, | |
286 | "Allow incoming link local packets with TTL less than 255"); | |
287 | ||
1c79356b | 288 | |
1c79356b A |
289 | /* |
290 | * We need to save the IP options in case a protocol wants to respond | |
291 | * to an incoming packet over the same route if the packet got here | |
292 | * using IP source routing. This allows connection establishment and | |
293 | * maintenance when the remote end is on a network that is not known | |
294 | * to us. | |
295 | */ | |
296 | static int ip_nhops = 0; | |
297 | static struct ip_srcrt { | |
298 | struct in_addr dst; /* final destination */ | |
299 | char nop; /* one NOP to align */ | |
300 | char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ | |
301 | struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; | |
302 | } ip_srcrt; | |
303 | ||
1c79356b | 304 | |
91447636 A |
305 | static void save_rte(u_char *, struct in_addr); |
306 | static int ip_dooptions(struct mbuf *, int, struct sockaddr_in *, struct route *ipforward_rt); | |
307 | static void ip_forward(struct mbuf *, int, struct sockaddr_in *, struct route *ipforward_rt); | |
308 | static void ip_freef(struct ipq *); | |
9bccf70c A |
309 | #if IPDIVERT |
310 | #ifdef IPDIVERT_44 | |
91447636 A |
311 | static struct mbuf *ip_reass(struct mbuf *, |
312 | struct ipq *, struct ipq *, u_int32_t *, u_int16_t *); | |
9bccf70c | 313 | #else |
91447636 A |
314 | static struct mbuf *ip_reass(struct mbuf *, |
315 | struct ipq *, struct ipq *, u_int16_t *, u_int16_t *); | |
1c79356b | 316 | #endif |
9bccf70c | 317 | #else |
91447636 | 318 | static struct mbuf *ip_reass(struct mbuf *, struct ipq *, struct ipq *); |
9bccf70c | 319 | #endif |
91447636 | 320 | void ipintr(void); |
2d21ac55 | 321 | void in_dinit(void); |
1c79356b | 322 | |
9bccf70c A |
323 | #if RANDOM_IP_ID |
324 | extern u_short ip_id; | |
2d21ac55 A |
325 | |
326 | int ip_use_randomid = 1; | |
327 | SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW, | |
328 | &ip_use_randomid, 0, "Randomize IP packets IDs"); | |
9bccf70c | 329 | #endif |
1c79356b | 330 | |
55e303ae | 331 | extern u_long route_generation; |
55e303ae | 332 | |
1c79356b A |
333 | /* |
334 | * IP initialization: fill in IP protocol switch table. | |
335 | * All protocols not implemented in kernel go to raw IP protocol handler. | |
336 | */ | |
337 | void | |
2d21ac55 | 338 | ip_init(void) |
1c79356b | 339 | { |
2d21ac55 A |
340 | struct protosw *pr; |
341 | int i; | |
342 | static int ip_initialized = 0; | |
91447636 | 343 | |
1c79356b A |
344 | |
345 | if (!ip_initialized) | |
346 | { | |
347 | TAILQ_INIT(&in_ifaddrhead); | |
91447636 | 348 | pr = pffindproto_locked(PF_INET, IPPROTO_RAW, SOCK_RAW); |
1c79356b A |
349 | if (pr == 0) |
350 | panic("ip_init"); | |
351 | for (i = 0; i < IPPROTO_MAX; i++) | |
352 | ip_protox[i] = pr; | |
353 | for (pr = inetdomain.dom_protosw; pr; pr = pr->pr_next) | |
354 | { if(!((unsigned int)pr->pr_domain)) continue; /* If uninitialized, skip */ | |
355 | if (pr->pr_domain->dom_family == PF_INET && | |
356 | pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) | |
357 | ip_protox[pr->pr_protocol] = pr; | |
358 | } | |
359 | for (i = 0; i < IPREASS_NHASH; i++) | |
360 | ipq[i].next = ipq[i].prev = &ipq[i]; | |
361 | ||
483a1d10 | 362 | maxnipq = nmbclusters / 32; |
91447636 A |
363 | maxfrags = maxnipq * 2; |
364 | maxfragsperpacket = 128; /* enough for 64k in 512 byte fragments */ | |
1c79356b | 365 | |
9bccf70c | 366 | #if RANDOM_IP_ID |
2d21ac55 A |
367 | { |
368 | struct timeval timenow; | |
369 | getmicrotime(&timenow); | |
370 | ip_id = timenow.tv_sec & 0xffff; | |
371 | } | |
1c79356b | 372 | #endif |
9bccf70c | 373 | ipintrq.ifq_maxlen = ipqmaxlen; |
91447636 A |
374 | |
375 | ipf_init(); | |
376 | ||
377 | ip_mutex_grp_attr = lck_grp_attr_alloc_init(); | |
91447636 A |
378 | |
379 | ip_mutex_grp = lck_grp_alloc_init("ip", ip_mutex_grp_attr); | |
380 | ||
381 | ip_mutex_attr = lck_attr_alloc_init(); | |
382 | ||
91447636 A |
383 | if ((ip_mutex = lck_mtx_alloc_init(ip_mutex_grp, ip_mutex_attr)) == NULL) { |
384 | printf("ip_init: can't alloc ip_mutex\n"); | |
385 | return; | |
386 | } | |
387 | ||
2d21ac55 A |
388 | #if IPSEC |
389 | ||
390 | sadb_stat_mutex_grp_attr = lck_grp_attr_alloc_init(); | |
391 | sadb_stat_mutex_grp = lck_grp_alloc_init("sadb_stat", sadb_stat_mutex_grp_attr); | |
392 | sadb_stat_mutex_attr = lck_attr_alloc_init(); | |
393 | ||
394 | if ((sadb_stat_mutex = lck_mtx_alloc_init(sadb_stat_mutex_grp, sadb_stat_mutex_attr)) == NULL) { | |
395 | printf("ip_init: can't alloc sadb_stat_mutex\n"); | |
396 | return; | |
397 | } | |
398 | ||
399 | #endif | |
1c79356b A |
400 | ip_initialized = 1; |
401 | } | |
402 | } | |
403 | ||
91447636 A |
404 | static void |
405 | ip_proto_input( | |
2d21ac55 A |
406 | protocol_family_t __unused protocol, |
407 | mbuf_t packet_list) | |
91447636 | 408 | { |
2d21ac55 A |
409 | mbuf_t packet; |
410 | int how_many = 0 ; | |
411 | ||
412 | /* ip_input should handle a list of packets but does not yet */ | |
413 | ||
414 | for (packet = packet_list; packet; packet = packet_list) { | |
415 | how_many++; | |
416 | packet_list = mbuf_nextpkt(packet); | |
417 | mbuf_setnextpkt(packet, NULL); | |
418 | ip_input(packet); | |
419 | } | |
91447636 A |
420 | } |
421 | ||
1c79356b A |
422 | /* Initialize the PF_INET domain, and add in the pre-defined protos */ |
423 | void | |
2d21ac55 A |
424 | in_dinit(void) |
425 | { | |
426 | int i; | |
427 | struct protosw *pr; | |
428 | struct domain *dp; | |
429 | static int inetdomain_initted = 0; | |
1c79356b A |
430 | |
431 | if (!inetdomain_initted) | |
9bccf70c | 432 | { |
2d21ac55 | 433 | #if 0 |
9bccf70c | 434 | kprintf("Initing %d protosw entries\n", in_proto_count); |
2d21ac55 | 435 | #endif |
1c79356b | 436 | dp = &inetdomain; |
91447636 | 437 | dp->dom_flags = DOM_REENTRANT; |
1c79356b A |
438 | |
439 | for (i=0, pr = &inetsw[0]; i<in_proto_count; i++, pr++) | |
440 | net_add_proto(pr, dp); | |
91447636 | 441 | inet_domain_mutex = dp->dom_mtx; |
1c79356b | 442 | inetdomain_initted = 1; |
91447636 A |
443 | |
444 | lck_mtx_unlock(domain_proto_mtx); | |
2d21ac55 | 445 | proto_register_input(PF_INET, ip_proto_input, NULL, 1); |
91447636 | 446 | lck_mtx_lock(domain_proto_mtx); |
1c79356b A |
447 | } |
448 | } | |
449 | ||
91447636 A |
450 | __private_extern__ void |
451 | ip_proto_dispatch_in( | |
452 | struct mbuf *m, | |
453 | int hlen, | |
454 | u_int8_t proto, | |
455 | ipfilter_t inject_ipfref) | |
456 | { | |
457 | struct ipfilter *filter; | |
458 | int seen = (inject_ipfref == 0); | |
459 | int changed_header = 0; | |
460 | struct ip *ip; | |
461 | ||
462 | if (!TAILQ_EMPTY(&ipv4_filters)) { | |
463 | ipf_ref(); | |
464 | TAILQ_FOREACH(filter, &ipv4_filters, ipf_link) { | |
465 | if (seen == 0) { | |
466 | if ((struct ipfilter *)inject_ipfref == filter) | |
467 | seen = 1; | |
468 | } else if (filter->ipf_filter.ipf_input) { | |
469 | errno_t result; | |
470 | ||
471 | if (changed_header == 0) { | |
472 | changed_header = 1; | |
473 | ip = mtod(m, struct ip *); | |
474 | ip->ip_len = htons(ip->ip_len + hlen); | |
475 | ip->ip_off = htons(ip->ip_off); | |
476 | ip->ip_sum = 0; | |
477 | ip->ip_sum = in_cksum(m, hlen); | |
478 | } | |
479 | result = filter->ipf_filter.ipf_input( | |
480 | filter->ipf_filter.cookie, (mbuf_t*)&m, hlen, proto); | |
481 | if (result == EJUSTRETURN) { | |
482 | ipf_unref(); | |
483 | return; | |
484 | } | |
485 | if (result != 0) { | |
486 | ipf_unref(); | |
487 | m_freem(m); | |
488 | return; | |
489 | } | |
490 | } | |
491 | } | |
492 | ipf_unref(); | |
493 | } | |
494 | /* | |
495 | * If there isn't a specific lock for the protocol | |
496 | * we're about to call, use the generic lock for AF_INET. | |
497 | * otherwise let the protocol deal with its own locking | |
498 | */ | |
499 | ip = mtod(m, struct ip *); | |
500 | ||
501 | if (changed_header) { | |
502 | ip->ip_len = ntohs(ip->ip_len) - hlen; | |
503 | ip->ip_off = ntohs(ip->ip_off); | |
504 | } | |
505 | ||
506 | if (!(ip_protox[ip->ip_p]->pr_flags & PR_PROTOLOCK)) { | |
507 | lck_mtx_lock(inet_domain_mutex); | |
508 | (*ip_protox[ip->ip_p]->pr_input)(m, hlen); | |
509 | lck_mtx_unlock(inet_domain_mutex); | |
510 | } | |
511 | else | |
512 | (*ip_protox[ip->ip_p]->pr_input)(m, hlen); | |
513 | ||
514 | } | |
515 | ||
516 | /* | |
517 | * ipforward_rt cleared in in_addroute() | |
518 | * when a new route is successfully created. | |
519 | */ | |
2d21ac55 | 520 | static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET , 0 , {0}, {0,0,0,0,0,0,0,0} }; |
1c79356b A |
521 | |
522 | /* | |
523 | * Ip input routine. Checksum and byte swap header. If fragmented | |
524 | * try to reassemble. Process options. Pass to next level. | |
525 | */ | |
526 | void | |
527 | ip_input(struct mbuf *m) | |
528 | { | |
529 | struct ip *ip; | |
530 | struct ipq *fp; | |
9bccf70c | 531 | struct in_ifaddr *ia = NULL; |
2d21ac55 | 532 | int i, hlen, checkif; |
1c79356b | 533 | u_short sum; |
9bccf70c | 534 | struct in_addr pkt_dst; |
91447636 | 535 | u_int32_t div_info = 0; /* packet divert/tee info */ |
4a3eedf9 | 536 | #if IPFIREWALL |
91447636 | 537 | struct ip_fw_args args; |
4a3eedf9 | 538 | #endif |
91447636 A |
539 | ipfilter_t inject_filter_ref = 0; |
540 | struct m_tag *tag; | |
2d21ac55 | 541 | struct route ipforward_rt; |
91447636 | 542 | |
2d21ac55 A |
543 | bzero(&ipforward_rt, sizeof(struct route)); |
544 | ||
545 | #if IPFIREWALL | |
91447636 A |
546 | args.eh = NULL; |
547 | args.oif = NULL; | |
548 | args.rule = NULL; | |
549 | args.divert_rule = 0; /* divert cookie */ | |
550 | args.next_hop = NULL; | |
551 | ||
552 | /* Grab info from mtags prepended to the chain */ | |
553 | #if DUMMYNET | |
554 | if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DUMMYNET, NULL)) != NULL) { | |
555 | struct dn_pkt_tag *dn_tag; | |
556 | ||
557 | dn_tag = (struct dn_pkt_tag *)(tag+1); | |
558 | args.rule = dn_tag->rule; | |
559 | ||
560 | m_tag_delete(m, tag); | |
561 | } | |
562 | #endif /* DUMMYNET */ | |
9bccf70c | 563 | |
4a3eedf9 | 564 | #if IPDIVERT |
91447636 A |
565 | if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_DIVERT, NULL)) != NULL) { |
566 | struct divert_tag *div_tag; | |
567 | ||
568 | div_tag = (struct divert_tag *)(tag+1); | |
569 | args.divert_rule = div_tag->cookie; | |
1c79356b | 570 | |
91447636 A |
571 | m_tag_delete(m, tag); |
572 | } | |
4a3eedf9 A |
573 | #endif |
574 | ||
91447636 A |
575 | if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_IPFORWARD, NULL)) != NULL) { |
576 | struct ip_fwd_tag *ipfwd_tag; | |
577 | ||
578 | ipfwd_tag = (struct ip_fwd_tag *)(tag+1); | |
579 | args.next_hop = ipfwd_tag->next_hop; | |
1c79356b | 580 | |
91447636 A |
581 | m_tag_delete(m, tag); |
582 | } | |
583 | ||
1c79356b A |
584 | #if DIAGNOSTIC |
585 | if (m == NULL || (m->m_flags & M_PKTHDR) == 0) | |
586 | panic("ip_input no HDR"); | |
587 | #endif | |
91447636 A |
588 | |
589 | if (args.rule) { /* dummynet already filtered us */ | |
590 | ip = mtod(m, struct ip *); | |
591 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
592 | inject_filter_ref = ipf_get_inject_filter(m); | |
593 | goto iphack ; | |
594 | } | |
2d21ac55 | 595 | #endif /* IPFIREWALL */ |
91447636 A |
596 | |
597 | /* | |
598 | * No need to proccess packet twice if we've | |
599 | * already seen it | |
600 | */ | |
601 | inject_filter_ref = ipf_get_inject_filter(m); | |
602 | if (inject_filter_ref != 0) { | |
91447636 A |
603 | ip = mtod(m, struct ip *); |
604 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
605 | ip->ip_len = ntohs(ip->ip_len) - hlen; | |
606 | ip->ip_off = ntohs(ip->ip_off); | |
607 | ip_proto_dispatch_in(m, hlen, ip->ip_p, inject_filter_ref); | |
608 | return; | |
609 | } | |
610 | ||
2d21ac55 | 611 | OSAddAtomic(1, (SInt32*)&ipstat.ips_total); |
1c79356b A |
612 | |
613 | if (m->m_pkthdr.len < sizeof(struct ip)) | |
614 | goto tooshort; | |
615 | ||
616 | if (m->m_len < sizeof (struct ip) && | |
617 | (m = m_pullup(m, sizeof (struct ip))) == 0) { | |
2d21ac55 | 618 | OSAddAtomic(1, (SInt32*)&ipstat.ips_toosmall); |
1c79356b A |
619 | return; |
620 | } | |
621 | ip = mtod(m, struct ip *); | |
622 | ||
623 | KERNEL_DEBUG(DBG_LAYER_BEG, ip->ip_dst.s_addr, | |
624 | ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len); | |
625 | ||
626 | if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { | |
2d21ac55 | 627 | OSAddAtomic(1, (SInt32*)&ipstat.ips_badvers); |
1c79356b A |
628 | goto bad; |
629 | } | |
630 | ||
631 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
632 | if (hlen < sizeof(struct ip)) { /* minimum header length */ | |
2d21ac55 | 633 | OSAddAtomic(1, (SInt32*)&ipstat.ips_badhlen); |
1c79356b A |
634 | goto bad; |
635 | } | |
636 | if (hlen > m->m_len) { | |
637 | if ((m = m_pullup(m, hlen)) == 0) { | |
2d21ac55 | 638 | OSAddAtomic(1, (SInt32*)&ipstat.ips_badhlen); |
1c79356b A |
639 | return; |
640 | } | |
641 | ip = mtod(m, struct ip *); | |
642 | } | |
643 | ||
9bccf70c A |
644 | /* 127/8 must not appear on wire - RFC1122 */ |
645 | if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || | |
646 | (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { | |
647 | if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { | |
2d21ac55 | 648 | OSAddAtomic(1, (SInt32*)&ipstat.ips_badaddr); |
9bccf70c A |
649 | goto bad; |
650 | } | |
651 | } | |
652 | ||
653 | /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */ | |
654 | if ((IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || | |
655 | IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)))) { | |
656 | ip_linklocal_stat.iplls_in_total++; | |
657 | if (ip->ip_ttl != MAXTTL) { | |
2d21ac55 | 658 | OSAddAtomic(1, (SInt32*)&ip_linklocal_stat.iplls_in_badttl); |
9bccf70c | 659 | /* Silently drop link local traffic with bad TTL */ |
91447636 | 660 | if (!ip_linklocal_in_allowbadttl) |
9bccf70c A |
661 | goto bad; |
662 | } | |
663 | } | |
4a249263 A |
664 | if ((IF_HWASSIST_CSUM_FLAGS(m->m_pkthdr.rcvif->if_hwassist) == 0) |
665 | || (apple_hwcksum_rx == 0) || | |
91447636 A |
666 | ((m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) && ip->ip_p != IPPROTO_TCP)) { |
667 | m->m_pkthdr.csum_flags = 0; /* invalidate HW generated checksum flags */ | |
668 | } | |
1c79356b | 669 | |
9bccf70c A |
670 | if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { |
671 | sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); | |
2d21ac55 A |
672 | } else if (!(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || |
673 | apple_hwcksum_tx == 0) { | |
674 | /* | |
675 | * Either this is not loopback packet coming from an interface | |
676 | * that does not support checksum offloading, or it is loopback | |
677 | * packet that has undergone software checksumming at the send | |
678 | * side because apple_hwcksum_tx was set to 0. In this case, | |
679 | * calculate the checksum in software to validate the packet. | |
680 | */ | |
9bccf70c | 681 | sum = in_cksum(m, hlen); |
2d21ac55 A |
682 | } else { |
683 | /* | |
684 | * This is a loopback packet without any valid checksum since | |
685 | * the send side has bypassed it (apple_hwcksum_tx set to 1). | |
686 | * We get here because apple_hwcksum_rx was set to 0, and so | |
687 | * we pretend that all is well. | |
688 | */ | |
689 | sum = 0; | |
690 | m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR | | |
691 | CSUM_IP_CHECKED | CSUM_IP_VALID; | |
692 | m->m_pkthdr.csum_data = 0xffff; | |
9bccf70c | 693 | } |
1c79356b | 694 | if (sum) { |
2d21ac55 | 695 | OSAddAtomic(1, (SInt32*)&ipstat.ips_badsum); |
1c79356b A |
696 | goto bad; |
697 | } | |
698 | ||
699 | /* | |
700 | * Convert fields to host representation. | |
701 | */ | |
702 | NTOHS(ip->ip_len); | |
703 | if (ip->ip_len < hlen) { | |
2d21ac55 | 704 | OSAddAtomic(1, (SInt32*)&ipstat.ips_badlen); |
1c79356b A |
705 | goto bad; |
706 | } | |
1c79356b A |
707 | NTOHS(ip->ip_off); |
708 | ||
709 | /* | |
710 | * Check that the amount of data in the buffers | |
711 | * is as at least much as the IP header would have us expect. | |
712 | * Trim mbufs if longer than we expect. | |
713 | * Drop packet if shorter than we expect. | |
714 | */ | |
715 | if (m->m_pkthdr.len < ip->ip_len) { | |
716 | tooshort: | |
2d21ac55 | 717 | OSAddAtomic(1, (SInt32*)&ipstat.ips_tooshort); |
1c79356b A |
718 | goto bad; |
719 | } | |
720 | if (m->m_pkthdr.len > ip->ip_len) { | |
765c9de3 A |
721 | /* Invalidate hwcksuming */ |
722 | m->m_pkthdr.csum_flags = 0; | |
723 | m->m_pkthdr.csum_data = 0; | |
724 | ||
1c79356b A |
725 | if (m->m_len == m->m_pkthdr.len) { |
726 | m->m_len = ip->ip_len; | |
727 | m->m_pkthdr.len = ip->ip_len; | |
728 | } else | |
729 | m_adj(m, ip->ip_len - m->m_pkthdr.len); | |
730 | } | |
9bccf70c A |
731 | |
732 | #if IPSEC | |
733 | if (ipsec_bypass == 0 && ipsec_gethist(m, NULL)) | |
734 | goto pass; | |
735 | #endif | |
736 | ||
1c79356b A |
737 | /* |
738 | * IpHack's section. | |
739 | * Right now when no processing on packet has done | |
740 | * and it is still fresh out of network we do our black | |
741 | * deals with it. | |
742 | * - Firewall: deny/allow/divert | |
743 | * - Xlate: translate packet's addr/port (NAT). | |
744 | * - Pipe: pass pkt through dummynet. | |
745 | * - Wrap: fake packet's addr/port <unimpl.> | |
746 | * - Encapsulate: put it in another IP and send out. <unimp.> | |
747 | */ | |
748 | ||
2d21ac55 A |
749 | #if IPFIREWALL |
750 | #if DUMMYNET | |
1c79356b | 751 | iphack: |
2d21ac55 | 752 | #endif /* DUMMYNET */ |
9bccf70c A |
753 | /* |
754 | * Check if we want to allow this packet to be processed. | |
755 | * Consider it to be bad if not. | |
756 | */ | |
757 | if (fr_checkp) { | |
758 | struct mbuf *m1 = m; | |
759 | ||
3a60a9f5 | 760 | if (fr_checkp(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1) { |
9bccf70c | 761 | return; |
3a60a9f5 | 762 | } |
9bccf70c A |
763 | ip = mtod(m = m1, struct ip *); |
764 | } | |
91447636 | 765 | if (fw_enable && IPFW_LOADED) { |
1c79356b A |
766 | #if IPFIREWALL_FORWARD |
767 | /* | |
768 | * If we've been forwarded from the output side, then | |
769 | * skip the firewall a second time | |
770 | */ | |
91447636 | 771 | if (args.next_hop) |
1c79356b A |
772 | goto ours; |
773 | #endif /* IPFIREWALL_FORWARD */ | |
91447636 A |
774 | |
775 | args.m = m; | |
3a60a9f5 | 776 | |
91447636 A |
777 | i = ip_fw_chk_ptr(&args); |
778 | m = args.m; | |
779 | ||
9bccf70c | 780 | if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */ |
91447636 | 781 | if (m) |
3a60a9f5 | 782 | m_freem(m); |
9bccf70c | 783 | return; |
91447636 | 784 | } |
9bccf70c | 785 | ip = mtod(m, struct ip *); /* just in case m changed */ |
2d21ac55 | 786 | |
3a60a9f5 | 787 | if (i == 0 && args.next_hop == NULL) { /* common case */ |
9bccf70c | 788 | goto pass; |
3a60a9f5 | 789 | } |
1c79356b | 790 | #if DUMMYNET |
91447636 A |
791 | if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG) != 0) { |
792 | /* Send packet to the appropriate pipe */ | |
91447636 | 793 | ip_dn_io_ptr(m, i&0xffff, DN_TO_IP_IN, &args); |
9bccf70c | 794 | return; |
1c79356b | 795 | } |
91447636 | 796 | #endif /* DUMMYNET */ |
1c79356b | 797 | #if IPDIVERT |
9bccf70c A |
798 | if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) { |
799 | /* Divert or tee packet */ | |
91447636 | 800 | div_info = i; |
1c79356b A |
801 | goto ours; |
802 | } | |
803 | #endif | |
804 | #if IPFIREWALL_FORWARD | |
3a60a9f5 | 805 | if (i == 0 && args.next_hop != NULL) { |
9bccf70c | 806 | goto pass; |
3a60a9f5 | 807 | } |
1c79356b A |
808 | #endif |
809 | /* | |
810 | * if we get here, the packet must be dropped | |
811 | */ | |
1c79356b | 812 | m_freem(m); |
9bccf70c | 813 | return; |
1c79356b | 814 | } |
2d21ac55 | 815 | #endif /* IPFIREWALL */ |
9bccf70c | 816 | pass: |
1c79356b A |
817 | |
818 | /* | |
819 | * Process options and, if not destined for us, | |
820 | * ship it on. ip_dooptions returns 1 when an | |
821 | * error was detected (causing an icmp message | |
822 | * to be sent and the original packet to be freed). | |
823 | */ | |
824 | ip_nhops = 0; /* for source routed packets */ | |
4a3eedf9 | 825 | #if IPFIREWALL |
91447636 | 826 | if (hlen > sizeof (struct ip) && ip_dooptions(m, 0, args.next_hop, &ipforward_rt)) { |
4a3eedf9 A |
827 | #else |
828 | if (hlen > sizeof (struct ip) && ip_dooptions(m, 0, NULL, &ipforward_rt)) { | |
829 | #endif | |
1c79356b A |
830 | return; |
831 | } | |
832 | ||
833 | /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no | |
834 | * matter if it is destined to another node, or whether it is | |
835 | * a multicast one, RSVP wants it! and prevents it from being forwarded | |
836 | * anywhere else. Also checks if the rsvp daemon is running before | |
837 | * grabbing the packet. | |
838 | */ | |
839 | if (rsvp_on && ip->ip_p==IPPROTO_RSVP) | |
840 | goto ours; | |
841 | ||
842 | /* | |
843 | * Check our list of addresses, to see if the packet is for us. | |
9bccf70c A |
844 | * If we don't have any addresses, assume any unicast packet |
845 | * we receive might be for us (and let the upper layers deal | |
846 | * with it). | |
1c79356b | 847 | */ |
9bccf70c A |
848 | if (TAILQ_EMPTY(&in_ifaddrhead) && |
849 | (m->m_flags & (M_MCAST|M_BCAST)) == 0) | |
850 | goto ours; | |
1c79356b | 851 | |
9bccf70c A |
852 | /* |
853 | * Cache the destination address of the packet; this may be | |
854 | * changed by use of 'ipfw fwd'. | |
855 | */ | |
4a3eedf9 | 856 | #if IPFIREWALL |
91447636 A |
857 | pkt_dst = args.next_hop == NULL ? |
858 | ip->ip_dst : args.next_hop->sin_addr; | |
4a3eedf9 A |
859 | #else |
860 | pkt_dst = ip->ip_dst; | |
861 | #endif | |
9bccf70c A |
862 | |
863 | /* | |
864 | * Enable a consistency check between the destination address | |
865 | * and the arrival interface for a unicast packet (the RFC 1122 | |
866 | * strong ES model) if IP forwarding is disabled and the packet | |
867 | * is not locally generated and the packet is not subject to | |
868 | * 'ipfw fwd'. | |
869 | * | |
870 | * XXX - Checking also should be disabled if the destination | |
871 | * address is ipnat'ed to a different interface. | |
872 | * | |
873 | * XXX - Checking is incompatible with IP aliases added | |
874 | * to the loopback interface instead of the interface where | |
875 | * the packets are received. | |
876 | */ | |
877 | checkif = ip_checkinterface && (ipforwarding == 0) && | |
4a3eedf9 A |
878 | ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) |
879 | #if IPFIREWALL | |
880 | && (args.next_hop == NULL); | |
881 | #else | |
882 | ; | |
883 | #endif | |
9bccf70c | 884 | |
91447636 | 885 | lck_mtx_lock(rt_mtx); |
9bccf70c A |
886 | TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { |
887 | #define satosin(sa) ((struct sockaddr_in *)(sa)) | |
1c79356b | 888 | |
91447636 A |
889 | if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) { |
890 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 891 | goto ours; |
91447636 | 892 | } |
9bccf70c | 893 | |
1c79356b | 894 | /* |
9bccf70c A |
895 | * If the address matches, verify that the packet |
896 | * arrived via the correct interface if checking is | |
897 | * enabled. | |
1c79356b | 898 | */ |
9bccf70c | 899 | if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr && |
91447636 A |
900 | (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) { |
901 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 902 | goto ours; |
91447636 | 903 | } |
9bccf70c A |
904 | /* |
905 | * Only accept broadcast packets that arrive via the | |
906 | * matching interface. Reception of forwarded directed | |
907 | * broadcasts would be handled via ip_forward() and | |
908 | * ether_output() with the loopback into the stack for | |
909 | * SIMPLEX interfaces handled by ether_output(). | |
910 | */ | |
55e303ae | 911 | if ((!checkif || ia->ia_ifp == m->m_pkthdr.rcvif) && |
9bccf70c | 912 | ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) { |
1c79356b | 913 | if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == |
91447636 A |
914 | pkt_dst.s_addr) { |
915 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 916 | goto ours; |
91447636 A |
917 | } |
918 | if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr) { | |
919 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 920 | goto ours; |
91447636 | 921 | } |
1c79356b A |
922 | } |
923 | } | |
91447636 | 924 | lck_mtx_unlock(rt_mtx); |
1c79356b A |
925 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { |
926 | struct in_multi *inm; | |
2d21ac55 | 927 | #if MROUTING |
1c79356b A |
928 | if (ip_mrouter) { |
929 | /* | |
930 | * If we are acting as a multicast router, all | |
931 | * incoming multicast packets are passed to the | |
932 | * kernel-level multicast forwarding function. | |
933 | * The packet is returned (relatively) intact; if | |
934 | * ip_mforward() returns a non-zero value, the packet | |
935 | * must be discarded, else it may be accepted below. | |
1c79356b | 936 | */ |
2d21ac55 | 937 | lck_mtx_lock(ip_mutex); |
91447636 A |
938 | if (ip_mforward && |
939 | ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { | |
2d21ac55 | 940 | OSAddAtomic(1, (SInt32*)&ipstat.ips_cantforward); |
1c79356b | 941 | m_freem(m); |
91447636 | 942 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
943 | return; |
944 | } | |
1c79356b A |
945 | |
946 | /* | |
55e303ae | 947 | * The process-level routing daemon needs to receive |
1c79356b A |
948 | * all multicast IGMP packets, whether or not this |
949 | * host belongs to their destination groups. | |
950 | */ | |
951 | if (ip->ip_p == IPPROTO_IGMP) | |
952 | goto ours; | |
2d21ac55 | 953 | OSAddAtomic(1, (SInt32*)&ipstat.ips_forward); |
1c79356b | 954 | } |
2d21ac55 | 955 | #endif /* MROUTING */ |
1c79356b A |
956 | /* |
957 | * See if we belong to the destination multicast group on the | |
958 | * arrival interface. | |
959 | */ | |
960 | IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); | |
961 | if (inm == NULL) { | |
2d21ac55 | 962 | OSAddAtomic(1, (SInt32*)&ipstat.ips_notmember); |
1c79356b A |
963 | m_freem(m); |
964 | return; | |
965 | } | |
966 | goto ours; | |
967 | } | |
968 | if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) | |
969 | goto ours; | |
970 | if (ip->ip_dst.s_addr == INADDR_ANY) | |
971 | goto ours; | |
972 | ||
9bccf70c A |
973 | /* Allow DHCP/BootP responses through */ |
974 | if (m->m_pkthdr.rcvif != NULL | |
0b4e3aa0 | 975 | && (m->m_pkthdr.rcvif->if_eflags & IFEF_AUTOCONFIGURING) |
9bccf70c | 976 | && hlen == sizeof(struct ip) |
0b4e3aa0 | 977 | && ip->ip_p == IPPROTO_UDP) { |
9bccf70c A |
978 | struct udpiphdr *ui; |
979 | if (m->m_len < sizeof(struct udpiphdr) | |
980 | && (m = m_pullup(m, sizeof(struct udpiphdr))) == 0) { | |
2d21ac55 | 981 | OSAddAtomic(1, (SInt32*)&udpstat.udps_hdrops); |
9bccf70c A |
982 | return; |
983 | } | |
984 | ui = mtod(m, struct udpiphdr *); | |
985 | if (ntohs(ui->ui_dport) == IPPORT_BOOTPC) { | |
986 | goto ours; | |
987 | } | |
988 | ip = mtod(m, struct ip *); /* in case it changed */ | |
0b4e3aa0 A |
989 | } |
990 | ||
9bccf70c | 991 | #if defined(NFAITH) && 0 < NFAITH |
1c79356b A |
992 | /* |
993 | * FAITH(Firewall Aided Internet Translator) | |
994 | */ | |
995 | if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { | |
996 | if (ip_keepfaith) { | |
997 | if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) | |
998 | goto ours; | |
999 | } | |
1000 | m_freem(m); | |
1001 | return; | |
1002 | } | |
1003 | #endif | |
1004 | /* | |
1005 | * Not for us; forward if possible and desirable. | |
1006 | */ | |
1007 | if (ipforwarding == 0) { | |
2d21ac55 | 1008 | OSAddAtomic(1, (SInt32*)&ipstat.ips_cantforward); |
1c79356b | 1009 | m_freem(m); |
91447636 | 1010 | } else { |
4a3eedf9 | 1011 | #if IPFIREWALL |
91447636 | 1012 | ip_forward(m, 0, args.next_hop, &ipforward_rt); |
4a3eedf9 A |
1013 | #else |
1014 | ip_forward(m, 0, NULL, &ipforward_rt); | |
1015 | #endif | |
2d21ac55 A |
1016 | if (ipforward_rt.ro_rt != NULL) { |
1017 | rtfree(ipforward_rt.ro_rt); | |
1018 | ipforward_rt.ro_rt = NULL; | |
1019 | } | |
91447636 | 1020 | } |
1c79356b A |
1021 | return; |
1022 | ||
1023 | ours: | |
1c79356b A |
1024 | /* |
1025 | * If offset or IP_MF are set, must reassemble. | |
1026 | * Otherwise, nothing need be done. | |
1027 | * (We could look in the reassembly queue to see | |
1028 | * if the packet was previously fragmented, | |
1029 | * but it's not worth the time; just let them time out.) | |
1030 | */ | |
1031 | if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) { | |
9bccf70c | 1032 | |
483a1d10 A |
1033 | /* If maxnipq is 0, never accept fragments. */ |
1034 | if (maxnipq == 0) { | |
2d21ac55 A |
1035 | |
1036 | OSAddAtomic(1, (SInt32*)&ipstat.ips_fragments); | |
1037 | OSAddAtomic(1, (SInt32*)&ipstat.ips_fragdropped); | |
483a1d10 | 1038 | goto bad; |
91447636 A |
1039 | } |
1040 | ||
1041 | /* | |
1042 | * If we will exceed the number of fragments in queues, timeout the | |
1043 | * oldest fragemented packet to make space. | |
1044 | */ | |
2d21ac55 | 1045 | lck_mtx_lock(ip_mutex); |
91447636 A |
1046 | if (currentfrags >= maxfrags) { |
1047 | fp = TAILQ_LAST(&ipq_list, ipq_list); | |
2d21ac55 | 1048 | OSAddAtomic(fp->ipq_nfrags, (SInt32*)&ipstat.ips_fragtimeout); |
91447636 A |
1049 | |
1050 | if (ip->ip_id == fp->ipq_id && | |
1051 | ip->ip_src.s_addr == fp->ipq_src.s_addr && | |
1052 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr && | |
1053 | ip->ip_p == fp->ipq_p) { | |
1054 | /* | |
1055 | * If we match the fragment queue we were going to | |
1056 | * discard, drop this packet too. | |
1057 | */ | |
2d21ac55 | 1058 | OSAddAtomic(1, (SInt32*)&ipstat.ips_fragdropped); |
91447636 | 1059 | ip_freef(fp); |
2d21ac55 | 1060 | lck_mtx_unlock(ip_mutex); |
91447636 | 1061 | goto bad; |
1c79356b | 1062 | } |
91447636 A |
1063 | |
1064 | ip_freef(fp); | |
1065 | } | |
483a1d10 | 1066 | |
1c79356b A |
1067 | sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); |
1068 | /* | |
1069 | * Look for queue of fragments | |
1070 | * of this datagram. | |
1071 | */ | |
1072 | for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next) | |
1073 | if (ip->ip_id == fp->ipq_id && | |
1074 | ip->ip_src.s_addr == fp->ipq_src.s_addr && | |
1075 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr && | |
2d21ac55 A |
1076 | #if CONFIG_MACF_NET |
1077 | mac_ipq_label_compare(m, fp) && | |
1078 | #endif | |
1c79356b A |
1079 | ip->ip_p == fp->ipq_p) |
1080 | goto found; | |
1081 | ||
483a1d10 A |
1082 | /* |
1083 | * Enforce upper bound on number of fragmented packets | |
1084 | * for which we attempt reassembly; | |
1085 | * If maxnipq is -1, accept all fragments without limitation. | |
1086 | */ | |
1087 | if ((nipq > maxnipq) && (maxnipq > 0)) { | |
1c79356b | 1088 | /* |
91447636 | 1089 | * drop the oldest fragment before proceeding further |
1c79356b | 1090 | */ |
91447636 | 1091 | fp = TAILQ_LAST(&ipq_list, ipq_list); |
2d21ac55 | 1092 | OSAddAtomic(fp->ipq_nfrags, (SInt32*)&ipstat.ips_fragtimeout); |
91447636 | 1093 | ip_freef(fp); |
483a1d10 | 1094 | } |
91447636 A |
1095 | |
1096 | fp = NULL; | |
1097 | ||
1c79356b A |
1098 | found: |
1099 | /* | |
1100 | * Adjust ip_len to not reflect header, | |
1c79356b A |
1101 | * convert offset of this to bytes. |
1102 | */ | |
1103 | ip->ip_len -= hlen; | |
483a1d10 | 1104 | if (ip->ip_off & IP_MF) { |
1c79356b A |
1105 | /* |
1106 | * Make sure that fragments have a data length | |
91447636 | 1107 | * that's a non-zero multiple of 8 bytes. |
1c79356b A |
1108 | */ |
1109 | if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { | |
2d21ac55 A |
1110 | OSAddAtomic(1, (SInt32*)&ipstat.ips_toosmall); |
1111 | lck_mtx_unlock(ip_mutex); | |
1c79356b A |
1112 | goto bad; |
1113 | } | |
1114 | m->m_flags |= M_FRAG; | |
91447636 A |
1115 | } else { |
1116 | /* Clear the flag in case packet comes from loopback */ | |
55e303ae | 1117 | m->m_flags &= ~M_FRAG; |
91447636 | 1118 | } |
1c79356b A |
1119 | ip->ip_off <<= 3; |
1120 | ||
1121 | /* | |
483a1d10 A |
1122 | * Attempt reassembly; if it succeeds, proceed. |
1123 | * ip_reass() will return a different mbuf, and update | |
91447636 | 1124 | * the divert info in div_info and args.divert_rule. |
1c79356b | 1125 | */ |
2d21ac55 | 1126 | OSAddAtomic(1, (SInt32*)&ipstat.ips_fragments); |
1c79356b | 1127 | m->m_pkthdr.header = ip; |
9bccf70c A |
1128 | #if IPDIVERT |
1129 | m = ip_reass(m, | |
91447636 | 1130 | fp, &ipq[sum], &div_info, &args.divert_rule); |
9bccf70c A |
1131 | #else |
1132 | m = ip_reass(m, fp, &ipq[sum]); | |
1133 | #endif | |
1134 | if (m == 0) { | |
91447636 | 1135 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
1136 | return; |
1137 | } | |
2d21ac55 | 1138 | OSAddAtomic(1, (SInt32*)&ipstat.ips_reassembled); |
9bccf70c A |
1139 | ip = mtod(m, struct ip *); |
1140 | /* Get the header length of the reassembled packet */ | |
1141 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
1c79356b | 1142 | #if IPDIVERT |
9bccf70c | 1143 | /* Restore original checksum before diverting packet */ |
91447636 | 1144 | if (div_info != 0) { |
1c79356b A |
1145 | ip->ip_len += hlen; |
1146 | HTONS(ip->ip_len); | |
1147 | HTONS(ip->ip_off); | |
1c79356b | 1148 | ip->ip_sum = 0; |
9bccf70c | 1149 | ip->ip_sum = in_cksum(m, hlen); |
1c79356b A |
1150 | NTOHS(ip->ip_off); |
1151 | NTOHS(ip->ip_len); | |
1152 | ip->ip_len -= hlen; | |
1153 | } | |
1154 | #endif | |
2d21ac55 | 1155 | lck_mtx_unlock(ip_mutex); |
1c79356b | 1156 | } else |
1c79356b A |
1157 | ip->ip_len -= hlen; |
1158 | ||
1159 | #if IPDIVERT | |
1160 | /* | |
9bccf70c A |
1161 | * Divert or tee packet to the divert protocol if required. |
1162 | * | |
91447636 | 1163 | * If div_info is zero then cookie should be too, so we shouldn't |
9bccf70c | 1164 | * need to clear them here. Assume divert_packet() does so also. |
1c79356b | 1165 | */ |
91447636 | 1166 | if (div_info != 0) { |
9bccf70c A |
1167 | struct mbuf *clone = NULL; |
1168 | ||
1169 | /* Clone packet if we're doing a 'tee' */ | |
91447636 | 1170 | if ((div_info & IP_FW_PORT_TEE_FLAG) != 0) |
9bccf70c A |
1171 | clone = m_dup(m, M_DONTWAIT); |
1172 | ||
1173 | /* Restore packet header fields to original values */ | |
1174 | ip->ip_len += hlen; | |
1175 | HTONS(ip->ip_len); | |
1176 | HTONS(ip->ip_off); | |
1177 | ||
1178 | /* Deliver packet to divert input routine */ | |
2d21ac55 | 1179 | OSAddAtomic(1, (SInt32*)&ipstat.ips_delivered); |
91447636 | 1180 | divert_packet(m, 1, div_info & 0xffff, args.divert_rule); |
9bccf70c A |
1181 | |
1182 | /* If 'tee', continue with original packet */ | |
91447636 | 1183 | if (clone == NULL) { |
9bccf70c | 1184 | return; |
91447636 | 1185 | } |
9bccf70c A |
1186 | m = clone; |
1187 | ip = mtod(m, struct ip *); | |
1c79356b | 1188 | } |
9bccf70c | 1189 | #endif |
1c79356b | 1190 | |
9bccf70c A |
1191 | #if IPSEC |
1192 | /* | |
1193 | * enforce IPsec policy checking if we are seeing last header. | |
1194 | * note that we do not visit this with protocols with pcb layer | |
1195 | * code - like udp/tcp/raw ip. | |
1196 | */ | |
91447636 | 1197 | if (ipsec_bypass == 0 && (ip_protox[ip->ip_p]->pr_flags & PR_LASTHDR) != 0) { |
91447636 | 1198 | if (ipsec4_in_reject(m, NULL)) { |
2d21ac55 A |
1199 | IPSEC_STAT_INCREMENT(ipsecstat.in_polvio); |
1200 | goto bad; | |
91447636 | 1201 | } |
1c79356b | 1202 | } |
1c79356b A |
1203 | #endif |
1204 | ||
1205 | /* | |
1206 | * Switch out to protocol's input routine. | |
1207 | */ | |
2d21ac55 | 1208 | OSAddAtomic(1, (SInt32*)&ipstat.ips_delivered); |
9bccf70c | 1209 | { |
4a3eedf9 | 1210 | #if IPFIREWALL |
91447636 A |
1211 | if (args.next_hop && ip->ip_p == IPPROTO_TCP) { |
1212 | /* TCP needs IPFORWARD info if available */ | |
1213 | struct m_tag *fwd_tag; | |
1214 | struct ip_fwd_tag *ipfwd_tag; | |
1215 | ||
1216 | fwd_tag = m_tag_alloc(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_IPFORWARD, | |
1217 | sizeof(struct sockaddr_in), M_NOWAIT); | |
1218 | if (fwd_tag == NULL) { | |
1219 | goto bad; | |
1220 | } | |
1221 | ||
1222 | ipfwd_tag = (struct ip_fwd_tag *)(fwd_tag+1); | |
1223 | ipfwd_tag->next_hop = args.next_hop; | |
1224 | ||
1225 | m_tag_prepend(m, fwd_tag); | |
1226 | ||
1227 | KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr, | |
1228 | ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len); | |
1229 | ||
91447636 A |
1230 | |
1231 | /* TCP deals with its own locking */ | |
1232 | ip_proto_dispatch_in(m, hlen, ip->ip_p, 0); | |
1233 | } else { | |
1234 | KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr, | |
1235 | ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len); | |
1236 | ||
91447636 A |
1237 | ip_proto_dispatch_in(m, hlen, ip->ip_p, 0); |
1238 | } | |
4a3eedf9 A |
1239 | #else |
1240 | ip_proto_dispatch_in(m, hlen, ip->ip_p, 0); | |
1241 | #endif | |
91447636 | 1242 | |
9bccf70c A |
1243 | return; |
1244 | } | |
1c79356b | 1245 | bad: |
1c79356b A |
1246 | KERNEL_DEBUG(DBG_LAYER_END, 0,0,0,0,0); |
1247 | m_freem(m); | |
1248 | } | |
1249 | ||
1c79356b | 1250 | /* |
9bccf70c A |
1251 | * Take incoming datagram fragment and try to reassemble it into |
1252 | * whole datagram. If a chain for reassembly of this datagram already | |
1253 | * exists, then it is given as fp; otherwise have to make a chain. | |
1254 | * | |
1255 | * When IPDIVERT enabled, keep additional state with each packet that | |
1256 | * tells us if we need to divert or tee the packet we're building. | |
1c79356b | 1257 | */ |
9bccf70c A |
1258 | |
1259 | static struct mbuf * | |
1260 | #if IPDIVERT | |
2d21ac55 | 1261 | ip_reass(struct mbuf *m, struct ipq *fp, struct ipq *where, |
9bccf70c | 1262 | #ifdef IPDIVERT_44 |
2d21ac55 A |
1263 | u_int32_t *divinfo, |
1264 | #else /* IPDIVERT_44 */ | |
1265 | u_int16_t *divinfo, | |
1266 | #endif /* IPDIVERT_44 */ | |
1267 | u_int16_t *divcookie) | |
1268 | #else /* IPDIVERT */ | |
1269 | ip_reass(struct mbuf *m, struct ipq *fp, struct ipq *where) | |
1270 | #endif /* IPDIVERT */ | |
1c79356b A |
1271 | { |
1272 | struct ip *ip = mtod(m, struct ip *); | |
2d21ac55 | 1273 | struct mbuf *p = 0, *q, *nq; |
1c79356b A |
1274 | struct mbuf *t; |
1275 | int hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
1276 | int i, next; | |
2d21ac55 | 1277 | u_int8_t ecn, ecn0; |
1c79356b | 1278 | |
2d21ac55 | 1279 | lck_mtx_assert(ip_mutex, LCK_MTX_ASSERT_OWNED); |
1c79356b A |
1280 | /* |
1281 | * Presence of header sizes in mbufs | |
1282 | * would confuse code below. | |
1283 | */ | |
1284 | m->m_data += hlen; | |
1285 | m->m_len -= hlen; | |
1286 | ||
0b4e3aa0 A |
1287 | if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) |
1288 | m->m_pkthdr.csum_flags = 0; | |
1c79356b A |
1289 | /* |
1290 | * If first fragment to arrive, create a reassembly queue. | |
1291 | */ | |
1292 | if (fp == 0) { | |
1293 | if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) | |
1294 | goto dropfrag; | |
1295 | fp = mtod(t, struct ipq *); | |
2d21ac55 A |
1296 | #if CONFIG_MACF_NET |
1297 | if (mac_ipq_label_init(fp, M_NOWAIT) != 0) { | |
1298 | m_free(t); | |
1299 | fp = NULL; | |
1300 | goto dropfrag; | |
1301 | } | |
1302 | mac_ipq_label_associate(m, fp); | |
1303 | #endif | |
9bccf70c | 1304 | insque((void*)fp, (void*)where); |
1c79356b | 1305 | nipq++; |
483a1d10 | 1306 | fp->ipq_nfrags = 1; |
1c79356b A |
1307 | fp->ipq_ttl = IPFRAGTTL; |
1308 | fp->ipq_p = ip->ip_p; | |
1309 | fp->ipq_id = ip->ip_id; | |
1310 | fp->ipq_src = ip->ip_src; | |
1311 | fp->ipq_dst = ip->ip_dst; | |
1312 | fp->ipq_frags = m; | |
1313 | m->m_nextpkt = NULL; | |
1314 | #if IPDIVERT | |
9bccf70c A |
1315 | #ifdef IPDIVERT_44 |
1316 | fp->ipq_div_info = 0; | |
1317 | #else | |
1c79356b | 1318 | fp->ipq_divert = 0; |
9bccf70c | 1319 | #endif |
1c79356b A |
1320 | fp->ipq_div_cookie = 0; |
1321 | #endif | |
91447636 | 1322 | TAILQ_INSERT_HEAD(&ipq_list, fp, ipq_list); |
1c79356b | 1323 | goto inserted; |
483a1d10 A |
1324 | } else { |
1325 | fp->ipq_nfrags++; | |
2d21ac55 A |
1326 | #if CONFIG_MACF_NET |
1327 | mac_ipq_label_update(m, fp); | |
1328 | #endif | |
1c79356b A |
1329 | } |
1330 | ||
1331 | #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) | |
1332 | ||
2d21ac55 A |
1333 | /* |
1334 | * Handle ECN by comparing this segment with the first one; | |
1335 | * if CE is set, do not lose CE. | |
1336 | * drop if CE and not-ECT are mixed for the same packet. | |
1337 | */ | |
1338 | ecn = ip->ip_tos & IPTOS_ECN_MASK; | |
1339 | ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; | |
1340 | if (ecn == IPTOS_ECN_CE) { | |
1341 | if (ecn0 == IPTOS_ECN_NOTECT) | |
1342 | goto dropfrag; | |
1343 | if (ecn0 != IPTOS_ECN_CE) | |
1344 | GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; | |
1345 | } | |
1346 | if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) | |
1347 | goto dropfrag; | |
1348 | ||
1c79356b A |
1349 | /* |
1350 | * Find a segment which begins after this one does. | |
1351 | */ | |
1352 | for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) | |
1353 | if (GETIP(q)->ip_off > ip->ip_off) | |
1354 | break; | |
1355 | ||
1356 | /* | |
1357 | * If there is a preceding segment, it may provide some of | |
1358 | * our data already. If so, drop the data from the incoming | |
1359 | * segment. If it provides all of our data, drop us, otherwise | |
1360 | * stick new segment in the proper place. | |
9bccf70c A |
1361 | * |
1362 | * If some of the data is dropped from the the preceding | |
1363 | * segment, then it's checksum is invalidated. | |
1c79356b A |
1364 | */ |
1365 | if (p) { | |
1366 | i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; | |
1367 | if (i > 0) { | |
1368 | if (i >= ip->ip_len) | |
1369 | goto dropfrag; | |
9bccf70c A |
1370 | m_adj(m, i); |
1371 | m->m_pkthdr.csum_flags = 0; | |
1c79356b A |
1372 | ip->ip_off += i; |
1373 | ip->ip_len -= i; | |
1374 | } | |
1375 | m->m_nextpkt = p->m_nextpkt; | |
1376 | p->m_nextpkt = m; | |
1377 | } else { | |
1378 | m->m_nextpkt = fp->ipq_frags; | |
1379 | fp->ipq_frags = m; | |
1380 | } | |
1381 | ||
1382 | /* | |
1383 | * While we overlap succeeding segments trim them or, | |
1384 | * if they are completely covered, dequeue them. | |
1385 | */ | |
1386 | for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; | |
1387 | q = nq) { | |
1388 | i = (ip->ip_off + ip->ip_len) - | |
1389 | GETIP(q)->ip_off; | |
1390 | if (i < GETIP(q)->ip_len) { | |
1391 | GETIP(q)->ip_len -= i; | |
1392 | GETIP(q)->ip_off += i; | |
1393 | m_adj(q, i); | |
9bccf70c | 1394 | q->m_pkthdr.csum_flags = 0; |
1c79356b A |
1395 | break; |
1396 | } | |
1397 | nq = q->m_nextpkt; | |
1398 | m->m_nextpkt = nq; | |
2d21ac55 | 1399 | OSAddAtomic(1, (SInt32*)&ipstat.ips_fragdropped); |
483a1d10 | 1400 | fp->ipq_nfrags--; |
1c79356b A |
1401 | m_freem(q); |
1402 | } | |
1403 | ||
1404 | inserted: | |
91447636 | 1405 | currentfrags++; |
1c79356b A |
1406 | |
1407 | #if IPDIVERT | |
1408 | /* | |
9bccf70c | 1409 | * Transfer firewall instructions to the fragment structure. |
483a1d10 | 1410 | * Only trust info in the fragment at offset 0. |
1c79356b | 1411 | */ |
483a1d10 | 1412 | if (ip->ip_off == 0) { |
9bccf70c A |
1413 | #ifdef IPDIVERT_44 |
1414 | fp->ipq_div_info = *divinfo; | |
1415 | #else | |
1416 | fp->ipq_divert = *divinfo; | |
1417 | #endif | |
1418 | fp->ipq_div_cookie = *divcookie; | |
483a1d10 | 1419 | } |
9bccf70c A |
1420 | *divinfo = 0; |
1421 | *divcookie = 0; | |
1c79356b A |
1422 | #endif |
1423 | ||
1424 | /* | |
483a1d10 A |
1425 | * Check for complete reassembly and perform frag per packet |
1426 | * limiting. | |
1427 | * | |
1428 | * Frag limiting is performed here so that the nth frag has | |
1429 | * a chance to complete the packet before we drop the packet. | |
1430 | * As a result, n+1 frags are actually allowed per packet, but | |
1431 | * only n will ever be stored. (n = maxfragsperpacket.) | |
1432 | * | |
1c79356b A |
1433 | */ |
1434 | next = 0; | |
1435 | for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { | |
483a1d10 A |
1436 | if (GETIP(q)->ip_off != next) { |
1437 | if (fp->ipq_nfrags > maxfragsperpacket) { | |
2d21ac55 | 1438 | OSAddAtomic(fp->ipq_nfrags, (SInt32*)&ipstat.ips_fragdropped); |
483a1d10 A |
1439 | ip_freef(fp); |
1440 | } | |
1c79356b | 1441 | return (0); |
483a1d10 | 1442 | } |
1c79356b A |
1443 | next += GETIP(q)->ip_len; |
1444 | } | |
1445 | /* Make sure the last packet didn't have the IP_MF flag */ | |
483a1d10 A |
1446 | if (p->m_flags & M_FRAG) { |
1447 | if (fp->ipq_nfrags > maxfragsperpacket) { | |
2d21ac55 | 1448 | OSAddAtomic(fp->ipq_nfrags, (SInt32*)&ipstat.ips_fragdropped); |
483a1d10 A |
1449 | ip_freef(fp); |
1450 | } | |
1c79356b | 1451 | return (0); |
483a1d10 | 1452 | } |
1c79356b A |
1453 | |
1454 | /* | |
1455 | * Reassembly is complete. Make sure the packet is a sane size. | |
1456 | */ | |
1457 | q = fp->ipq_frags; | |
1458 | ip = GETIP(q); | |
1459 | if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { | |
2d21ac55 A |
1460 | OSAddAtomic(1, (SInt32*)&ipstat.ips_toolong); |
1461 | OSAddAtomic(fp->ipq_nfrags, (SInt32*)&ipstat.ips_fragdropped); | |
1c79356b A |
1462 | ip_freef(fp); |
1463 | return (0); | |
1464 | } | |
1465 | ||
1466 | /* | |
1467 | * Concatenate fragments. | |
1468 | */ | |
1469 | m = q; | |
1470 | t = m->m_next; | |
1471 | m->m_next = 0; | |
1472 | m_cat(m, t); | |
1473 | nq = q->m_nextpkt; | |
1474 | q->m_nextpkt = 0; | |
1475 | for (q = nq; q != NULL; q = nq) { | |
1476 | nq = q->m_nextpkt; | |
1477 | q->m_nextpkt = NULL; | |
91447636 A |
1478 | if (q->m_pkthdr.csum_flags & CSUM_TCP_SUM16) |
1479 | m->m_pkthdr.csum_flags = 0; | |
1480 | else { | |
9bccf70c A |
1481 | m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; |
1482 | m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; | |
91447636 | 1483 | } |
1c79356b A |
1484 | m_cat(m, q); |
1485 | } | |
1486 | ||
1487 | #if IPDIVERT | |
1488 | /* | |
9bccf70c | 1489 | * Extract firewall instructions from the fragment structure. |
1c79356b | 1490 | */ |
9bccf70c A |
1491 | #ifdef IPDIVERT_44 |
1492 | *divinfo = fp->ipq_div_info; | |
1493 | #else | |
1494 | *divinfo = fp->ipq_divert; | |
1495 | #endif | |
1496 | *divcookie = fp->ipq_div_cookie; | |
1c79356b A |
1497 | #endif |
1498 | ||
2d21ac55 A |
1499 | #if CONFIG_MACF_NET |
1500 | mac_mbuf_label_associate_ipq(fp, m); | |
1501 | mac_ipq_label_destroy(fp); | |
1502 | #endif | |
1c79356b A |
1503 | /* |
1504 | * Create header for new ip packet by | |
1505 | * modifying header of first packet; | |
1506 | * dequeue and discard fragment reassembly header. | |
1507 | * Make header visible. | |
1508 | */ | |
1509 | ip->ip_len = next; | |
1510 | ip->ip_src = fp->ipq_src; | |
1511 | ip->ip_dst = fp->ipq_dst; | |
9bccf70c | 1512 | remque((void*)fp); |
91447636 A |
1513 | TAILQ_REMOVE(&ipq_list, fp, ipq_list); |
1514 | currentfrags -= fp->ipq_nfrags; | |
1c79356b A |
1515 | nipq--; |
1516 | (void) m_free(dtom(fp)); | |
1517 | m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); | |
1518 | m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); | |
1519 | /* some debugging cruft by sklower, below, will go away soon */ | |
1520 | if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ | |
2d21ac55 | 1521 | int plen = 0; |
9bccf70c A |
1522 | for (t = m; t; t = t->m_next) |
1523 | plen += t->m_len; | |
1524 | m->m_pkthdr.len = plen; | |
1c79356b | 1525 | } |
9bccf70c | 1526 | return (m); |
1c79356b A |
1527 | |
1528 | dropfrag: | |
1529 | #if IPDIVERT | |
9bccf70c A |
1530 | *divinfo = 0; |
1531 | *divcookie = 0; | |
1c79356b | 1532 | #endif |
2d21ac55 | 1533 | OSAddAtomic(1, (SInt32*)&ipstat.ips_fragdropped); |
483a1d10 A |
1534 | if (fp != 0) |
1535 | fp->ipq_nfrags--; | |
1c79356b A |
1536 | m_freem(m); |
1537 | return (0); | |
1538 | ||
1539 | #undef GETIP | |
1540 | } | |
1541 | ||
1542 | /* | |
1543 | * Free a fragment reassembly header and all | |
1544 | * associated datagrams. | |
1545 | */ | |
1546 | static void | |
2d21ac55 | 1547 | ip_freef(struct ipq *fp) |
1c79356b | 1548 | { |
2d21ac55 | 1549 | lck_mtx_assert(ip_mutex, LCK_MTX_ASSERT_OWNED); |
91447636 A |
1550 | currentfrags -= fp->ipq_nfrags; |
1551 | m_freem_list(fp->ipq_frags); | |
9bccf70c | 1552 | remque((void*)fp); |
91447636 | 1553 | TAILQ_REMOVE(&ipq_list, fp, ipq_list); |
1c79356b A |
1554 | (void) m_free(dtom(fp)); |
1555 | nipq--; | |
1556 | } | |
1557 | ||
1558 | /* | |
1559 | * IP timer processing; | |
1560 | * if a timer expires on a reassembly | |
1561 | * queue, discard it. | |
1562 | */ | |
1563 | void | |
2d21ac55 | 1564 | ip_slowtimo(void) |
1c79356b | 1565 | { |
2d21ac55 | 1566 | struct ipq *fp; |
1c79356b | 1567 | int i; |
91447636 | 1568 | lck_mtx_lock(ip_mutex); |
1c79356b A |
1569 | for (i = 0; i < IPREASS_NHASH; i++) { |
1570 | fp = ipq[i].next; | |
1571 | if (fp == 0) | |
1572 | continue; | |
1573 | while (fp != &ipq[i]) { | |
1574 | --fp->ipq_ttl; | |
1575 | fp = fp->next; | |
1576 | if (fp->prev->ipq_ttl == 0) { | |
2d21ac55 | 1577 | OSAddAtomic(fp->ipq_nfrags, (SInt32*)&ipstat.ips_fragtimeout); |
1c79356b A |
1578 | ip_freef(fp->prev); |
1579 | } | |
1580 | } | |
1581 | } | |
9bccf70c A |
1582 | /* |
1583 | * If we are over the maximum number of fragments | |
1584 | * (due to the limit being lowered), drain off | |
1585 | * enough to get down to the new limit. | |
1586 | */ | |
483a1d10 | 1587 | if (maxnipq >= 0 && nipq > maxnipq) { |
9bccf70c | 1588 | for (i = 0; i < IPREASS_NHASH; i++) { |
483a1d10 | 1589 | while (nipq > maxnipq && |
9bccf70c | 1590 | (ipq[i].next != &ipq[i])) { |
2d21ac55 | 1591 | OSAddAtomic(ipq[i].next->ipq_nfrags, (SInt32*)&ipstat.ips_fragdropped); |
9bccf70c A |
1592 | ip_freef(ipq[i].next); |
1593 | } | |
1594 | } | |
1595 | } | |
1c79356b | 1596 | ipflow_slowtimo(); |
91447636 | 1597 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
1598 | } |
1599 | ||
1600 | /* | |
1601 | * Drain off all datagram fragments. | |
1602 | */ | |
1603 | void | |
2d21ac55 | 1604 | ip_drain(void) |
1c79356b A |
1605 | { |
1606 | int i; | |
1607 | ||
91447636 | 1608 | lck_mtx_lock(ip_mutex); |
1c79356b A |
1609 | for (i = 0; i < IPREASS_NHASH; i++) { |
1610 | while (ipq[i].next != &ipq[i]) { | |
2d21ac55 | 1611 | OSAddAtomic(ipq[i].next->ipq_nfrags, (SInt32*)&ipstat.ips_fragdropped); |
1c79356b A |
1612 | ip_freef(ipq[i].next); |
1613 | } | |
1614 | } | |
91447636 | 1615 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
1616 | in_rtqdrain(); |
1617 | } | |
1618 | ||
1619 | /* | |
1620 | * Do option processing on a datagram, | |
1621 | * possibly discarding it if bad options are encountered, | |
1622 | * or forwarding it if source-routed. | |
91447636 A |
1623 | * The pass argument is used when operating in the IPSTEALTH |
1624 | * mode to tell what options to process: | |
1625 | * [LS]SRR (pass 0) or the others (pass 1). | |
1626 | * The reason for as many as two passes is that when doing IPSTEALTH, | |
1627 | * non-routing options should be processed only if the packet is for us. | |
1c79356b A |
1628 | * Returns 1 if packet has been forwarded/freed, |
1629 | * 0 if the packet should be processed further. | |
1630 | */ | |
1631 | static int | |
2d21ac55 | 1632 | ip_dooptions(struct mbuf *m, __unused int pass, struct sockaddr_in *next_hop, struct route *ipforward_rt) |
1c79356b | 1633 | { |
2d21ac55 A |
1634 | struct ip *ip = mtod(m, struct ip *); |
1635 | u_char *cp; | |
1636 | struct ip_timestamp *ipt; | |
1637 | struct in_ifaddr *ia; | |
1c79356b A |
1638 | int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; |
1639 | struct in_addr *sin, dst; | |
1640 | n_time ntime; | |
1641 | ||
1642 | dst = ip->ip_dst; | |
1643 | cp = (u_char *)(ip + 1); | |
1644 | cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); | |
1645 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
1646 | opt = cp[IPOPT_OPTVAL]; | |
1647 | if (opt == IPOPT_EOL) | |
1648 | break; | |
1649 | if (opt == IPOPT_NOP) | |
1650 | optlen = 1; | |
1651 | else { | |
1652 | if (cnt < IPOPT_OLEN + sizeof(*cp)) { | |
9bccf70c | 1653 | code = &cp[IPOPT_OLEN] - (u_char *)ip; |
1c79356b A |
1654 | goto bad; |
1655 | } | |
1656 | optlen = cp[IPOPT_OLEN]; | |
1657 | if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { | |
1658 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
1659 | goto bad; | |
1660 | } | |
1661 | } | |
1662 | switch (opt) { | |
1663 | ||
1664 | default: | |
1665 | break; | |
1666 | ||
1667 | /* | |
1668 | * Source routing with record. | |
1669 | * Find interface with current destination address. | |
1670 | * If none on this machine then drop if strictly routed, | |
1671 | * or do nothing if loosely routed. | |
1672 | * Record interface address and bring up next address | |
1673 | * component. If strictly routed make sure next | |
1674 | * address is on directly accessible net. | |
1675 | */ | |
1676 | case IPOPT_LSRR: | |
1677 | case IPOPT_SSRR: | |
9bccf70c A |
1678 | if (optlen < IPOPT_OFFSET + sizeof(*cp)) { |
1679 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
1680 | goto bad; | |
1681 | } | |
1c79356b A |
1682 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { |
1683 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1684 | goto bad; | |
1685 | } | |
1686 | ipaddr.sin_addr = ip->ip_dst; | |
1687 | ia = (struct in_ifaddr *) | |
1688 | ifa_ifwithaddr((struct sockaddr *)&ipaddr); | |
1689 | if (ia == 0) { | |
1690 | if (opt == IPOPT_SSRR) { | |
1691 | type = ICMP_UNREACH; | |
1692 | code = ICMP_UNREACH_SRCFAIL; | |
1693 | goto bad; | |
1694 | } | |
1695 | if (!ip_dosourceroute) | |
1696 | goto nosourcerouting; | |
1697 | /* | |
1698 | * Loose routing, and not at next destination | |
1699 | * yet; nothing to do except forward. | |
1700 | */ | |
1701 | break; | |
1702 | } | |
91447636 A |
1703 | else { |
1704 | ifafree(&ia->ia_ifa); | |
1705 | ia = NULL; | |
1706 | } | |
1c79356b | 1707 | off--; /* 0 origin */ |
9bccf70c | 1708 | if (off > optlen - (int)sizeof(struct in_addr)) { |
1c79356b A |
1709 | /* |
1710 | * End of source route. Should be for us. | |
1711 | */ | |
1712 | if (!ip_acceptsourceroute) | |
1713 | goto nosourcerouting; | |
1714 | save_rte(cp, ip->ip_src); | |
1715 | break; | |
1716 | } | |
1717 | ||
1718 | if (!ip_dosourceroute) { | |
1719 | if (ipforwarding) { | |
91447636 A |
1720 | char buf[MAX_IPv4_STR_LEN]; |
1721 | char buf2[MAX_IPv4_STR_LEN]; | |
1c79356b A |
1722 | /* |
1723 | * Acting as a router, so generate ICMP | |
1724 | */ | |
1725 | nosourcerouting: | |
91447636 | 1726 | log(LOG_WARNING, |
1c79356b | 1727 | "attempted source route from %s to %s\n", |
91447636 A |
1728 | inet_ntop(AF_INET, &ip->ip_src, buf, sizeof(buf)), |
1729 | inet_ntop(AF_INET, &ip->ip_dst, buf2, sizeof(buf2))); | |
1c79356b A |
1730 | type = ICMP_UNREACH; |
1731 | code = ICMP_UNREACH_SRCFAIL; | |
1732 | goto bad; | |
1733 | } else { | |
1734 | /* | |
1735 | * Not acting as a router, so silently drop. | |
1736 | */ | |
2d21ac55 | 1737 | OSAddAtomic(1, (SInt32*)&ipstat.ips_cantforward); |
1c79356b A |
1738 | m_freem(m); |
1739 | return (1); | |
1740 | } | |
1741 | } | |
1742 | ||
1743 | /* | |
1744 | * locate outgoing interface | |
1745 | */ | |
1746 | (void)memcpy(&ipaddr.sin_addr, cp + off, | |
1747 | sizeof(ipaddr.sin_addr)); | |
1748 | ||
1749 | if (opt == IPOPT_SSRR) { | |
1750 | #define INA struct in_ifaddr * | |
1751 | #define SA struct sockaddr * | |
91447636 A |
1752 | if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) { |
1753 | ia = (INA)ifa_ifwithnet((SA)&ipaddr); | |
1754 | } | |
1755 | } else { | |
1756 | ia = ip_rtaddr(ipaddr.sin_addr, ipforward_rt); | |
1757 | } | |
1c79356b A |
1758 | if (ia == 0) { |
1759 | type = ICMP_UNREACH; | |
1760 | code = ICMP_UNREACH_SRCFAIL; | |
1761 | goto bad; | |
1762 | } | |
1763 | ip->ip_dst = ipaddr.sin_addr; | |
1764 | (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), | |
1765 | sizeof(struct in_addr)); | |
91447636 A |
1766 | ifafree(&ia->ia_ifa); |
1767 | ia = NULL; | |
1c79356b A |
1768 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); |
1769 | /* | |
1770 | * Let ip_intr's mcast routing check handle mcast pkts | |
1771 | */ | |
1772 | forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); | |
1773 | break; | |
1774 | ||
1775 | case IPOPT_RR: | |
1776 | if (optlen < IPOPT_OFFSET + sizeof(*cp)) { | |
1777 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1778 | goto bad; | |
1779 | } | |
1780 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
1781 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1782 | goto bad; | |
1783 | } | |
1784 | /* | |
1785 | * If no space remains, ignore. | |
1786 | */ | |
1787 | off--; /* 0 origin */ | |
9bccf70c | 1788 | if (off > optlen - (int)sizeof(struct in_addr)) |
1c79356b A |
1789 | break; |
1790 | (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, | |
1791 | sizeof(ipaddr.sin_addr)); | |
1792 | /* | |
1793 | * locate outgoing interface; if we're the destination, | |
1794 | * use the incoming interface (should be same). | |
1795 | */ | |
cc9f6e38 | 1796 | if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0) { |
91447636 A |
1797 | if ((ia = ip_rtaddr(ipaddr.sin_addr, ipforward_rt)) == 0) { |
1798 | type = ICMP_UNREACH; | |
1799 | code = ICMP_UNREACH_HOST; | |
1800 | goto bad; | |
1801 | } | |
1c79356b A |
1802 | } |
1803 | (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), | |
1804 | sizeof(struct in_addr)); | |
91447636 A |
1805 | ifafree(&ia->ia_ifa); |
1806 | ia = NULL; | |
1c79356b A |
1807 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); |
1808 | break; | |
1809 | ||
1810 | case IPOPT_TS: | |
1811 | code = cp - (u_char *)ip; | |
1812 | ipt = (struct ip_timestamp *)cp; | |
9bccf70c A |
1813 | if (ipt->ipt_len < 4 || ipt->ipt_len > 40) { |
1814 | code = (u_char *)&ipt->ipt_len - (u_char *)ip; | |
1c79356b | 1815 | goto bad; |
9bccf70c A |
1816 | } |
1817 | if (ipt->ipt_ptr < 5) { | |
1818 | code = (u_char *)&ipt->ipt_ptr - (u_char *)ip; | |
1819 | goto bad; | |
1820 | } | |
1821 | if (ipt->ipt_ptr > | |
1822 | ipt->ipt_len - (int)sizeof(int32_t)) { | |
1823 | if (++ipt->ipt_oflw == 0) { | |
1824 | code = (u_char *)&ipt->ipt_ptr - | |
1825 | (u_char *)ip; | |
1c79356b | 1826 | goto bad; |
9bccf70c | 1827 | } |
1c79356b A |
1828 | break; |
1829 | } | |
1830 | sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); | |
1831 | switch (ipt->ipt_flg) { | |
1832 | ||
1833 | case IPOPT_TS_TSONLY: | |
1834 | break; | |
1835 | ||
1836 | case IPOPT_TS_TSANDADDR: | |
1837 | if (ipt->ipt_ptr - 1 + sizeof(n_time) + | |
9bccf70c A |
1838 | sizeof(struct in_addr) > ipt->ipt_len) { |
1839 | code = (u_char *)&ipt->ipt_ptr - | |
1840 | (u_char *)ip; | |
1c79356b | 1841 | goto bad; |
9bccf70c | 1842 | } |
1c79356b A |
1843 | ipaddr.sin_addr = dst; |
1844 | ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, | |
1845 | m->m_pkthdr.rcvif); | |
1846 | if (ia == 0) | |
1847 | continue; | |
1848 | (void)memcpy(sin, &IA_SIN(ia)->sin_addr, | |
1849 | sizeof(struct in_addr)); | |
1850 | ipt->ipt_ptr += sizeof(struct in_addr); | |
91447636 A |
1851 | ifafree(&ia->ia_ifa); |
1852 | ia = NULL; | |
1c79356b A |
1853 | break; |
1854 | ||
1855 | case IPOPT_TS_PRESPEC: | |
1856 | if (ipt->ipt_ptr - 1 + sizeof(n_time) + | |
9bccf70c A |
1857 | sizeof(struct in_addr) > ipt->ipt_len) { |
1858 | code = (u_char *)&ipt->ipt_ptr - | |
1859 | (u_char *)ip; | |
1c79356b | 1860 | goto bad; |
9bccf70c | 1861 | } |
1c79356b A |
1862 | (void)memcpy(&ipaddr.sin_addr, sin, |
1863 | sizeof(struct in_addr)); | |
91447636 | 1864 | if ((ia = (struct in_ifaddr*)ifa_ifwithaddr((SA)&ipaddr)) == 0) |
1c79356b | 1865 | continue; |
91447636 A |
1866 | ifafree(&ia->ia_ifa); |
1867 | ia = NULL; | |
1c79356b A |
1868 | ipt->ipt_ptr += sizeof(struct in_addr); |
1869 | break; | |
1870 | ||
1871 | default: | |
9bccf70c A |
1872 | /* XXX can't take &ipt->ipt_flg */ |
1873 | code = (u_char *)&ipt->ipt_ptr - | |
1874 | (u_char *)ip + 1; | |
1c79356b A |
1875 | goto bad; |
1876 | } | |
1877 | ntime = iptime(); | |
1878 | (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, | |
1879 | sizeof(n_time)); | |
1880 | ipt->ipt_ptr += sizeof(n_time); | |
1881 | } | |
1882 | } | |
1883 | if (forward && ipforwarding) { | |
91447636 | 1884 | ip_forward(m, 1, next_hop, ipforward_rt); |
2d21ac55 A |
1885 | if (ipforward_rt->ro_rt != NULL) { |
1886 | rtfree(ipforward_rt->ro_rt); | |
1887 | ipforward_rt->ro_rt = NULL; | |
1888 | } | |
1c79356b A |
1889 | return (1); |
1890 | } | |
1891 | return (0); | |
1892 | bad: | |
1893 | ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */ | |
1894 | icmp_error(m, type, code, 0, 0); | |
2d21ac55 | 1895 | OSAddAtomic(1, (SInt32*)&ipstat.ips_badoptions); |
1c79356b A |
1896 | return (1); |
1897 | } | |
1898 | ||
1899 | /* | |
1900 | * Given address of next destination (final or next hop), | |
1901 | * return internet address info of interface to be used to get there. | |
1902 | */ | |
91447636 | 1903 | struct in_ifaddr * |
2d21ac55 | 1904 | ip_rtaddr(struct in_addr dst, struct route *rt) |
1c79356b | 1905 | { |
2d21ac55 | 1906 | struct sockaddr_in *sin; |
1c79356b | 1907 | |
91447636 | 1908 | sin = (struct sockaddr_in *)&rt->ro_dst; |
1c79356b | 1909 | |
91447636 A |
1910 | lck_mtx_lock(rt_mtx); |
1911 | if (rt->ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr || | |
1912 | rt->ro_rt->generation_id != route_generation) { | |
1913 | if (rt->ro_rt) { | |
1914 | rtfree_locked(rt->ro_rt); | |
1915 | rt->ro_rt = 0; | |
1c79356b A |
1916 | } |
1917 | sin->sin_family = AF_INET; | |
1918 | sin->sin_len = sizeof(*sin); | |
1919 | sin->sin_addr = dst; | |
1920 | ||
91447636 | 1921 | rtalloc_ign_locked(rt, RTF_PRCLONING); |
1c79356b | 1922 | } |
91447636 A |
1923 | if (rt->ro_rt == 0) { |
1924 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 1925 | return ((struct in_ifaddr *)0); |
91447636 A |
1926 | } |
1927 | ||
1928 | if (rt->ro_rt->rt_ifa) | |
1929 | ifaref(rt->ro_rt->rt_ifa); | |
1930 | lck_mtx_unlock(rt_mtx); | |
1931 | return ((struct in_ifaddr *) rt->ro_rt->rt_ifa); | |
1c79356b A |
1932 | } |
1933 | ||
1934 | /* | |
1935 | * Save incoming source route for use in replies, | |
1936 | * to be picked up later by ip_srcroute if the receiver is interested. | |
1937 | */ | |
1938 | void | |
2d21ac55 | 1939 | save_rte(u_char *option, struct in_addr dst) |
1c79356b A |
1940 | { |
1941 | unsigned olen; | |
1942 | ||
1943 | olen = option[IPOPT_OLEN]; | |
1944 | #if DIAGNOSTIC | |
1945 | if (ipprintfs) | |
1946 | printf("save_rte: olen %d\n", olen); | |
1947 | #endif | |
1948 | if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) | |
1949 | return; | |
1950 | bcopy(option, ip_srcrt.srcopt, olen); | |
1951 | ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); | |
1952 | ip_srcrt.dst = dst; | |
1953 | } | |
1954 | ||
1955 | /* | |
1956 | * Retrieve incoming source route for use in replies, | |
1957 | * in the same form used by setsockopt. | |
1958 | * The first hop is placed before the options, will be removed later. | |
1959 | */ | |
1960 | struct mbuf * | |
2d21ac55 | 1961 | ip_srcroute(void) |
1c79356b | 1962 | { |
2d21ac55 A |
1963 | struct in_addr *p, *q; |
1964 | struct mbuf *m; | |
1c79356b A |
1965 | |
1966 | if (ip_nhops == 0) | |
1967 | return ((struct mbuf *)0); | |
1968 | m = m_get(M_DONTWAIT, MT_HEADER); | |
1969 | if (m == 0) | |
1970 | return ((struct mbuf *)0); | |
1971 | ||
1972 | #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) | |
1973 | ||
1974 | /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ | |
1975 | m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + | |
1976 | OPTSIZ; | |
1977 | #if DIAGNOSTIC | |
1978 | if (ipprintfs) | |
1979 | printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); | |
1980 | #endif | |
1981 | ||
1982 | /* | |
1983 | * First save first hop for return route | |
1984 | */ | |
1985 | p = &ip_srcrt.route[ip_nhops - 1]; | |
1986 | *(mtod(m, struct in_addr *)) = *p--; | |
1987 | #if DIAGNOSTIC | |
1988 | if (ipprintfs) | |
1989 | printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); | |
1990 | #endif | |
1991 | ||
1992 | /* | |
1993 | * Copy option fields and padding (nop) to mbuf. | |
1994 | */ | |
1995 | ip_srcrt.nop = IPOPT_NOP; | |
1996 | ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; | |
1997 | (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), | |
1998 | &ip_srcrt.nop, OPTSIZ); | |
1999 | q = (struct in_addr *)(mtod(m, caddr_t) + | |
2000 | sizeof(struct in_addr) + OPTSIZ); | |
2001 | #undef OPTSIZ | |
2002 | /* | |
2003 | * Record return path as an IP source route, | |
2004 | * reversing the path (pointers are now aligned). | |
2005 | */ | |
2006 | while (p >= ip_srcrt.route) { | |
2007 | #if DIAGNOSTIC | |
2008 | if (ipprintfs) | |
2009 | printf(" %lx", (u_long)ntohl(q->s_addr)); | |
2010 | #endif | |
2011 | *q++ = *p--; | |
2012 | } | |
2013 | /* | |
2014 | * Last hop goes to final destination. | |
2015 | */ | |
2016 | *q = ip_srcrt.dst; | |
2017 | #if DIAGNOSTIC | |
2018 | if (ipprintfs) | |
2019 | printf(" %lx\n", (u_long)ntohl(q->s_addr)); | |
2020 | #endif | |
2021 | return (m); | |
2022 | } | |
2023 | ||
2024 | /* | |
2025 | * Strip out IP options, at higher | |
2026 | * level protocol in the kernel. | |
2027 | * Second argument is buffer to which options | |
2028 | * will be moved, and return value is their length. | |
2029 | * XXX should be deleted; last arg currently ignored. | |
2030 | */ | |
2031 | void | |
2d21ac55 | 2032 | ip_stripoptions(struct mbuf *m, __unused struct mbuf *mopt) |
1c79356b | 2033 | { |
2d21ac55 | 2034 | int i; |
1c79356b | 2035 | struct ip *ip = mtod(m, struct ip *); |
2d21ac55 | 2036 | caddr_t opts; |
1c79356b A |
2037 | int olen; |
2038 | ||
2039 | olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); | |
2040 | opts = (caddr_t)(ip + 1); | |
2041 | i = m->m_len - (sizeof (struct ip) + olen); | |
2042 | bcopy(opts + olen, opts, (unsigned)i); | |
2043 | m->m_len -= olen; | |
2044 | if (m->m_flags & M_PKTHDR) | |
2045 | m->m_pkthdr.len -= olen; | |
2046 | ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); | |
2047 | } | |
2048 | ||
2049 | u_char inetctlerrmap[PRC_NCMDS] = { | |
2050 | 0, 0, 0, 0, | |
2051 | 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, | |
2d21ac55 | 2052 | ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, |
1c79356b A |
2053 | EMSGSIZE, EHOSTUNREACH, 0, 0, |
2054 | 0, 0, 0, 0, | |
9bccf70c | 2055 | ENOPROTOOPT, ECONNREFUSED |
1c79356b A |
2056 | }; |
2057 | ||
2058 | /* | |
2059 | * Forward a packet. If some error occurs return the sender | |
2060 | * an icmp packet. Note we can't always generate a meaningful | |
2061 | * icmp message because icmp doesn't have a large enough repertoire | |
2062 | * of codes and types. | |
2063 | * | |
2064 | * If not forwarding, just drop the packet. This could be confusing | |
2065 | * if ipforwarding was zero but some routing protocol was advancing | |
2066 | * us as a gateway to somewhere. However, we must let the routing | |
2067 | * protocol deal with that. | |
2068 | * | |
2069 | * The srcrt parameter indicates whether the packet is being forwarded | |
2070 | * via a source route. | |
2071 | */ | |
9bccf70c | 2072 | static void |
91447636 | 2073 | ip_forward(struct mbuf *m, int srcrt, struct sockaddr_in *next_hop, struct route *ipforward_rt) |
1c79356b | 2074 | { |
2d21ac55 A |
2075 | struct ip *ip = mtod(m, struct ip *); |
2076 | struct sockaddr_in *sin; | |
2077 | struct rtentry *rt; | |
1c79356b A |
2078 | int error, type = 0, code = 0; |
2079 | struct mbuf *mcopy; | |
2080 | n_long dest; | |
91447636 | 2081 | struct in_addr pkt_dst; |
1c79356b | 2082 | struct ifnet *destifp; |
2d21ac55 | 2083 | struct ifnet *rcvif = m->m_pkthdr.rcvif; |
1c79356b A |
2084 | #if IPSEC |
2085 | struct ifnet dummyifp; | |
2086 | #endif | |
2087 | ||
2d21ac55 A |
2088 | m->m_pkthdr.rcvif = NULL; |
2089 | ||
1c79356b | 2090 | dest = 0; |
91447636 A |
2091 | /* |
2092 | * Cache the destination address of the packet; this may be | |
2093 | * changed by use of 'ipfw fwd'. | |
2094 | */ | |
2095 | pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; | |
2096 | ||
1c79356b A |
2097 | #if DIAGNOSTIC |
2098 | if (ipprintfs) | |
2099 | printf("forward: src %lx dst %lx ttl %x\n", | |
91447636 | 2100 | (u_long)ip->ip_src.s_addr, (u_long)pkt_dst.s_addr, |
1c79356b A |
2101 | ip->ip_ttl); |
2102 | #endif | |
2103 | ||
2104 | ||
91447636 | 2105 | if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(pkt_dst) == 0) { |
2d21ac55 | 2106 | OSAddAtomic(1, (SInt32*)&ipstat.ips_cantforward); |
1c79356b A |
2107 | m_freem(m); |
2108 | return; | |
2109 | } | |
9bccf70c A |
2110 | #if IPSTEALTH |
2111 | if (!ipstealth) { | |
2112 | #endif | |
2113 | if (ip->ip_ttl <= IPTTLDEC) { | |
2114 | icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, | |
2115 | dest, 0); | |
2116 | return; | |
2117 | } | |
2118 | #if IPSTEALTH | |
1c79356b A |
2119 | } |
2120 | #endif | |
2121 | ||
91447636 A |
2122 | sin = (struct sockaddr_in *)&ipforward_rt->ro_dst; |
2123 | if ((rt = ipforward_rt->ro_rt) == 0 || | |
2124 | pkt_dst.s_addr != sin->sin_addr.s_addr || | |
2125 | ipforward_rt->ro_rt->generation_id != route_generation) { | |
2126 | if (ipforward_rt->ro_rt) { | |
2127 | rtfree(ipforward_rt->ro_rt); | |
2128 | ipforward_rt->ro_rt = 0; | |
1c79356b A |
2129 | } |
2130 | sin->sin_family = AF_INET; | |
2131 | sin->sin_len = sizeof(*sin); | |
91447636 | 2132 | sin->sin_addr = pkt_dst; |
1c79356b | 2133 | |
91447636 A |
2134 | rtalloc_ign(ipforward_rt, RTF_PRCLONING); |
2135 | if (ipforward_rt->ro_rt == 0) { | |
1c79356b A |
2136 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); |
2137 | return; | |
2138 | } | |
91447636 | 2139 | rt = ipforward_rt->ro_rt; |
1c79356b A |
2140 | } |
2141 | ||
2142 | /* | |
9bccf70c A |
2143 | * Save the IP header and at most 8 bytes of the payload, |
2144 | * in case we need to generate an ICMP message to the src. | |
2145 | * | |
2146 | * We don't use m_copy() because it might return a reference | |
2147 | * to a shared cluster. Both this function and ip_output() | |
2148 | * assume exclusive access to the IP header in `m', so any | |
2149 | * data in a cluster may change before we reach icmp_error(). | |
1c79356b | 2150 | */ |
9bccf70c A |
2151 | MGET(mcopy, M_DONTWAIT, m->m_type); |
2152 | if (mcopy != NULL) { | |
2153 | M_COPY_PKTHDR(mcopy, m); | |
2154 | mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8, | |
2155 | (int)ip->ip_len); | |
2156 | m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); | |
2157 | } | |
2158 | ||
2159 | #if IPSTEALTH | |
2160 | if (!ipstealth) { | |
2161 | #endif | |
2162 | ip->ip_ttl -= IPTTLDEC; | |
2163 | #if IPSTEALTH | |
2164 | } | |
2165 | #endif | |
1c79356b A |
2166 | |
2167 | /* | |
2168 | * If forwarding packet using same interface that it came in on, | |
2169 | * perhaps should send a redirect to sender to shortcut a hop. | |
2170 | * Only send redirect if source is sending directly to us, | |
2171 | * and if packet was not source routed (or has any options). | |
2172 | * Also, don't send redirect if forwarding using a default route | |
2173 | * or a route modified by a redirect. | |
2174 | */ | |
2175 | #define satosin(sa) ((struct sockaddr_in *)(sa)) | |
2176 | if (rt->rt_ifp == m->m_pkthdr.rcvif && | |
2177 | (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && | |
2178 | satosin(rt_key(rt))->sin_addr.s_addr != 0 && | |
2179 | ipsendredirects && !srcrt) { | |
2180 | #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) | |
2181 | u_long src = ntohl(ip->ip_src.s_addr); | |
2182 | ||
2183 | if (RTA(rt) && | |
2184 | (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { | |
2185 | if (rt->rt_flags & RTF_GATEWAY) | |
2186 | dest = satosin(rt->rt_gateway)->sin_addr.s_addr; | |
2187 | else | |
91447636 | 2188 | dest = pkt_dst.s_addr; |
1c79356b A |
2189 | /* Router requirements says to only send host redirects */ |
2190 | type = ICMP_REDIRECT; | |
2191 | code = ICMP_REDIRECT_HOST; | |
2192 | #if DIAGNOSTIC | |
2193 | if (ipprintfs) | |
2194 | printf("redirect (%d) to %lx\n", code, (u_long)dest); | |
2195 | #endif | |
2196 | } | |
2197 | } | |
2198 | ||
91447636 A |
2199 | { |
2200 | if (next_hop) { | |
2201 | /* Pass IPFORWARD info if available */ | |
2202 | struct m_tag *tag; | |
2203 | struct ip_fwd_tag *ipfwd_tag; | |
2204 | ||
2205 | tag = m_tag_alloc(KERNEL_MODULE_TAG_ID, KERNEL_TAG_TYPE_IPFORWARD, | |
2206 | sizeof(struct sockaddr_in), M_NOWAIT); | |
2207 | if (tag == NULL) { | |
2208 | error = ENOBUFS; | |
2209 | m_freem(m); | |
2210 | return; | |
2211 | } | |
2212 | ||
2213 | ipfwd_tag = (struct ip_fwd_tag *)(tag+1); | |
2214 | ipfwd_tag->next_hop = next_hop; | |
2215 | ||
2216 | m_tag_prepend(m, tag); | |
2217 | } | |
2218 | error = ip_output_list(m, 0, (struct mbuf *)0, ipforward_rt, | |
2d21ac55 | 2219 | IP_FORWARDING, 0, NULL); |
91447636 | 2220 | } |
1c79356b | 2221 | if (error) |
2d21ac55 | 2222 | OSAddAtomic(1, (SInt32*)&ipstat.ips_cantforward); |
1c79356b | 2223 | else { |
2d21ac55 | 2224 | OSAddAtomic(1, (SInt32*)&ipstat.ips_forward); |
1c79356b | 2225 | if (type) |
2d21ac55 | 2226 | OSAddAtomic(1, (SInt32*)&ipstat.ips_redirectsent); |
1c79356b A |
2227 | else { |
2228 | if (mcopy) { | |
91447636 | 2229 | ipflow_create(ipforward_rt, mcopy); |
1c79356b A |
2230 | m_freem(mcopy); |
2231 | } | |
2232 | return; | |
2233 | } | |
2234 | } | |
2235 | if (mcopy == NULL) | |
2236 | return; | |
2237 | destifp = NULL; | |
2238 | ||
2239 | switch (error) { | |
2240 | ||
2241 | case 0: /* forwarded, but need redirect */ | |
2242 | /* type, code set above */ | |
2243 | break; | |
2244 | ||
2245 | case ENETUNREACH: /* shouldn't happen, checked above */ | |
2246 | case EHOSTUNREACH: | |
2247 | case ENETDOWN: | |
2248 | case EHOSTDOWN: | |
2249 | default: | |
2250 | type = ICMP_UNREACH; | |
2251 | code = ICMP_UNREACH_HOST; | |
2252 | break; | |
2253 | ||
2254 | case EMSGSIZE: | |
2255 | type = ICMP_UNREACH; | |
2256 | code = ICMP_UNREACH_NEEDFRAG; | |
2257 | #ifndef IPSEC | |
91447636 A |
2258 | if (ipforward_rt->ro_rt) |
2259 | destifp = ipforward_rt->ro_rt->rt_ifp; | |
1c79356b A |
2260 | #else |
2261 | /* | |
2262 | * If the packet is routed over IPsec tunnel, tell the | |
2263 | * originator the tunnel MTU. | |
2264 | * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz | |
2265 | * XXX quickhack!!! | |
2266 | */ | |
91447636 | 2267 | if (ipforward_rt->ro_rt) { |
1c79356b A |
2268 | struct secpolicy *sp = NULL; |
2269 | int ipsecerror; | |
2270 | int ipsechdr; | |
2271 | struct route *ro; | |
2272 | ||
9bccf70c | 2273 | if (ipsec_bypass) { |
91447636 | 2274 | destifp = ipforward_rt->ro_rt->rt_ifp; |
2d21ac55 | 2275 | OSAddAtomic(1, (SInt32*)&ipstat.ips_cantfrag); |
9bccf70c A |
2276 | break; |
2277 | } | |
1c79356b | 2278 | sp = ipsec4_getpolicybyaddr(mcopy, |
9bccf70c | 2279 | IPSEC_DIR_OUTBOUND, |
1c79356b A |
2280 | IP_FORWARDING, |
2281 | &ipsecerror); | |
2282 | ||
2283 | if (sp == NULL) | |
91447636 | 2284 | destifp = ipforward_rt->ro_rt->rt_ifp; |
1c79356b A |
2285 | else { |
2286 | /* count IPsec header size */ | |
2d21ac55 | 2287 | ipsechdr = ipsec_hdrsiz(sp); |
1c79356b A |
2288 | |
2289 | /* | |
2290 | * find the correct route for outer IPv4 | |
2291 | * header, compute tunnel MTU. | |
2292 | * | |
2293 | * XXX BUG ALERT | |
2294 | * The "dummyifp" code relies upon the fact | |
2295 | * that icmp_error() touches only ifp->if_mtu. | |
2296 | */ | |
2297 | /*XXX*/ | |
2298 | destifp = NULL; | |
2d21ac55 A |
2299 | |
2300 | if (sp->req != NULL) { | |
2301 | if (sp->req->saidx.mode == IPSEC_MODE_TUNNEL) { | |
2302 | struct secasindex saidx; | |
2303 | struct ip *ipm; | |
2304 | struct secasvar *sav; | |
2305 | ||
2306 | ipm = mtod(mcopy, struct ip *); | |
2307 | bcopy(&sp->req->saidx, &saidx, sizeof(saidx)); | |
2308 | saidx.mode = sp->req->saidx.mode; | |
2309 | saidx.reqid = sp->req->saidx.reqid; | |
2310 | sin = (struct sockaddr_in *)&saidx.src; | |
2311 | if (sin->sin_len == 0) { | |
2312 | sin->sin_len = sizeof(*sin); | |
2313 | sin->sin_family = AF_INET; | |
2314 | sin->sin_port = IPSEC_PORT_ANY; | |
2315 | bcopy(&ipm->ip_src, &sin->sin_addr, | |
2316 | sizeof(sin->sin_addr)); | |
2317 | } | |
2318 | sin = (struct sockaddr_in *)&saidx.dst; | |
2319 | if (sin->sin_len == 0) { | |
2320 | sin->sin_len = sizeof(*sin); | |
2321 | sin->sin_family = AF_INET; | |
2322 | sin->sin_port = IPSEC_PORT_ANY; | |
2323 | bcopy(&ipm->ip_dst, &sin->sin_addr, | |
2324 | sizeof(sin->sin_addr)); | |
2325 | } | |
2326 | sav = key_allocsa_policy(&saidx); | |
2327 | if (sav != NULL) { | |
2328 | if (sav->sah != NULL) { | |
2329 | ro = &sav->sah->sa_route; | |
2330 | if (ro->ro_rt && ro->ro_rt->rt_ifp) { | |
2331 | dummyifp.if_mtu = | |
2332 | ro->ro_rt->rt_ifp->if_mtu; | |
2333 | dummyifp.if_mtu -= ipsechdr; | |
2334 | destifp = &dummyifp; | |
2335 | } | |
2336 | } | |
2337 | key_freesav(sav, KEY_SADB_UNLOCKED); | |
2338 | } | |
1c79356b A |
2339 | } |
2340 | } | |
2d21ac55 | 2341 | key_freesp(sp, KEY_SADB_UNLOCKED); |
1c79356b A |
2342 | } |
2343 | } | |
2344 | #endif /*IPSEC*/ | |
2d21ac55 | 2345 | OSAddAtomic(1, (SInt32*)&ipstat.ips_cantfrag); |
1c79356b A |
2346 | break; |
2347 | ||
2348 | case ENOBUFS: | |
2349 | type = ICMP_SOURCEQUENCH; | |
2350 | code = 0; | |
2351 | break; | |
9bccf70c A |
2352 | |
2353 | case EACCES: /* ipfw denied packet */ | |
2354 | m_freem(mcopy); | |
2355 | return; | |
1c79356b A |
2356 | } |
2357 | icmp_error(mcopy, type, code, dest, destifp); | |
2358 | } | |
2359 | ||
2360 | void | |
91447636 | 2361 | ip_savecontrol( |
2d21ac55 A |
2362 | struct inpcb *inp, |
2363 | struct mbuf **mp, | |
2364 | struct ip *ip, | |
2365 | struct mbuf *m) | |
1c79356b A |
2366 | { |
2367 | if (inp->inp_socket->so_options & SO_TIMESTAMP) { | |
2368 | struct timeval tv; | |
2369 | ||
2370 | microtime(&tv); | |
2371 | *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), | |
2372 | SCM_TIMESTAMP, SOL_SOCKET); | |
2373 | if (*mp) | |
2374 | mp = &(*mp)->m_next; | |
2375 | } | |
2376 | if (inp->inp_flags & INP_RECVDSTADDR) { | |
2377 | *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, | |
2378 | sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); | |
2379 | if (*mp) | |
2380 | mp = &(*mp)->m_next; | |
2381 | } | |
2382 | #ifdef notyet | |
2383 | /* XXX | |
2384 | * Moving these out of udp_input() made them even more broken | |
2385 | * than they already were. | |
2386 | */ | |
2387 | /* options were tossed already */ | |
2388 | if (inp->inp_flags & INP_RECVOPTS) { | |
2389 | *mp = sbcreatecontrol((caddr_t) opts_deleted_above, | |
2390 | sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); | |
2391 | if (*mp) | |
2392 | mp = &(*mp)->m_next; | |
2393 | } | |
2394 | /* ip_srcroute doesn't do what we want here, need to fix */ | |
2395 | if (inp->inp_flags & INP_RECVRETOPTS) { | |
2396 | *mp = sbcreatecontrol((caddr_t) ip_srcroute(), | |
2397 | sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); | |
2398 | if (*mp) | |
2399 | mp = &(*mp)->m_next; | |
2400 | } | |
2401 | #endif | |
2402 | if (inp->inp_flags & INP_RECVIF) { | |
2403 | struct ifnet *ifp; | |
2404 | struct sdlbuf { | |
2405 | struct sockaddr_dl sdl; | |
2406 | u_char pad[32]; | |
2407 | } sdlbuf; | |
2408 | struct sockaddr_dl *sdp; | |
2409 | struct sockaddr_dl *sdl2 = &sdlbuf.sdl; | |
2410 | ||
91447636 | 2411 | ifnet_head_lock_shared(); |
1c79356b A |
2412 | if (((ifp = m->m_pkthdr.rcvif)) |
2413 | && ( ifp->if_index && (ifp->if_index <= if_index))) { | |
13fec989 | 2414 | struct ifaddr *ifa = ifnet_addrs[ifp->if_index - 1]; |
2d21ac55 | 2415 | |
13fec989 A |
2416 | if (!ifa || !ifa->ifa_addr) |
2417 | goto makedummy; | |
2d21ac55 | 2418 | |
13fec989 | 2419 | sdp = (struct sockaddr_dl *)ifa->ifa_addr; |
1c79356b A |
2420 | /* |
2421 | * Change our mind and don't try copy. | |
2422 | */ | |
2423 | if ((sdp->sdl_family != AF_LINK) | |
2424 | || (sdp->sdl_len > sizeof(sdlbuf))) { | |
2425 | goto makedummy; | |
2426 | } | |
2427 | bcopy(sdp, sdl2, sdp->sdl_len); | |
2428 | } else { | |
2429 | makedummy: | |
2430 | sdl2->sdl_len | |
2431 | = offsetof(struct sockaddr_dl, sdl_data[0]); | |
2432 | sdl2->sdl_family = AF_LINK; | |
2433 | sdl2->sdl_index = 0; | |
2434 | sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; | |
2435 | } | |
91447636 | 2436 | ifnet_head_done(); |
1c79356b A |
2437 | *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, |
2438 | IP_RECVIF, IPPROTO_IP); | |
2439 | if (*mp) | |
2440 | mp = &(*mp)->m_next; | |
2441 | } | |
55e303ae A |
2442 | if (inp->inp_flags & INP_RECVTTL) { |
2443 | *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl, sizeof(ip->ip_ttl), IP_RECVTTL, IPPROTO_IP); | |
2444 | if (*mp) mp = &(*mp)->m_next; | |
2445 | } | |
1c79356b A |
2446 | } |
2447 | ||
2448 | int | |
2449 | ip_rsvp_init(struct socket *so) | |
2450 | { | |
2451 | if (so->so_type != SOCK_RAW || | |
2452 | so->so_proto->pr_protocol != IPPROTO_RSVP) | |
2453 | return EOPNOTSUPP; | |
2454 | ||
2455 | if (ip_rsvpd != NULL) | |
2456 | return EADDRINUSE; | |
2457 | ||
2458 | ip_rsvpd = so; | |
2459 | /* | |
2460 | * This may seem silly, but we need to be sure we don't over-increment | |
2461 | * the RSVP counter, in case something slips up. | |
2462 | */ | |
2463 | if (!ip_rsvp_on) { | |
2464 | ip_rsvp_on = 1; | |
2465 | rsvp_on++; | |
2466 | } | |
2467 | ||
2468 | return 0; | |
2469 | } | |
2470 | ||
2471 | int | |
2472 | ip_rsvp_done(void) | |
2473 | { | |
2474 | ip_rsvpd = NULL; | |
2475 | /* | |
2476 | * This may seem silly, but we need to be sure we don't over-decrement | |
2477 | * the RSVP counter, in case something slips up. | |
2478 | */ | |
2479 | if (ip_rsvp_on) { | |
2480 | ip_rsvp_on = 0; | |
2481 | rsvp_on--; | |
2482 | } | |
2483 | return 0; | |
2484 | } |