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