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