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