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