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