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