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