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