xref: /openbsd-src/sys/netinet/ip_input.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: ip_input.c,v 1.281 2016/09/15 02:00:18 dlg Exp $	*/
2 /*	$NetBSD: ip_input.c,v 1.30 1996/03/16 23:53:58 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
33  */
34 
35 #include "pf.h"
36 #include "carp.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/pool.h>
47 #include <sys/task.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_dl.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/if_ether.h>
58 #include <netinet/ip.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_icmp.h>
63 
64 #if NPF > 0
65 #include <net/pfvar.h>
66 #endif
67 
68 #ifdef MROUTING
69 #include <netinet/ip_mroute.h>
70 #endif
71 
72 #ifdef IPSEC
73 #include <netinet/ip_ipsp.h>
74 #endif /* IPSEC */
75 
76 #if NCARP > 0
77 #include <net/if_types.h>
78 #include <netinet/ip_carp.h>
79 #endif
80 
81 struct ipqhead ipq;
82 
83 int encdebug = 0;
84 int ipsec_keep_invalid = IPSEC_DEFAULT_EMBRYONIC_SA_TIMEOUT;
85 int ipsec_require_pfs = IPSEC_DEFAULT_PFS;
86 int ipsec_soft_allocations = IPSEC_DEFAULT_SOFT_ALLOCATIONS;
87 int ipsec_exp_allocations = IPSEC_DEFAULT_EXP_ALLOCATIONS;
88 int ipsec_soft_bytes = IPSEC_DEFAULT_SOFT_BYTES;
89 int ipsec_exp_bytes = IPSEC_DEFAULT_EXP_BYTES;
90 int ipsec_soft_timeout = IPSEC_DEFAULT_SOFT_TIMEOUT;
91 int ipsec_exp_timeout = IPSEC_DEFAULT_EXP_TIMEOUT;
92 int ipsec_soft_first_use = IPSEC_DEFAULT_SOFT_FIRST_USE;
93 int ipsec_exp_first_use = IPSEC_DEFAULT_EXP_FIRST_USE;
94 int ipsec_expire_acquire = IPSEC_DEFAULT_EXPIRE_ACQUIRE;
95 char ipsec_def_enc[20];
96 char ipsec_def_auth[20];
97 char ipsec_def_comp[20];
98 
99 /* values controllable via sysctl */
100 int	ipforwarding = 0;
101 int	ipmforwarding = 0;
102 int	ipmultipath = 0;
103 int	ipsendredirects = 1;
104 int	ip_dosourceroute = 0;
105 int	ip_defttl = IPDEFTTL;
106 int	ip_mtudisc = 1;
107 u_int	ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
108 int	ip_directedbcast = 0;
109 
110 struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
111 
112 /* Keep track of memory used for reassembly */
113 int	ip_maxqueue = 300;
114 int	ip_frags = 0;
115 
116 int *ipctl_vars[IPCTL_MAXID] = IPCTL_VARS;
117 
118 struct niqueue ipintrq = NIQUEUE_INITIALIZER(IFQ_MAXLEN, NETISR_IP);
119 
120 struct pool ipqent_pool;
121 struct pool ipq_pool;
122 
123 struct ipstat ipstat;
124 
125 static struct mbuf_queue	ipsend_mq;
126 
127 void	ip_ours(struct mbuf *);
128 int	ip_dooptions(struct mbuf *, struct ifnet *);
129 int	in_ouraddr(struct mbuf *, struct ifnet *, struct rtentry **);
130 void	ip_forward(struct mbuf *, struct ifnet *, struct rtentry *, int);
131 #ifdef IPSEC
132 int	ip_input_ipsec_fwd_check(struct mbuf *, int);
133 int	ip_input_ipsec_ours_check(struct mbuf *, int);
134 #endif /* IPSEC */
135 
136 static void ip_send_dispatch(void *);
137 static struct task ipsend_task = TASK_INITIALIZER(ip_send_dispatch, &ipsend_mq);
138 /*
139  * Used to save the IP options in case a protocol wants to respond
140  * to an incoming packet over the same route if the packet got here
141  * using IP source routing.  This allows connection establishment and
142  * maintenance when the remote end is on a network that is not known
143  * to us.
144  */
145 struct ip_srcrt {
146 	int		isr_nhops;		   /* number of hops */
147 	struct in_addr	isr_dst;		   /* final destination */
148 	char		isr_nop;		   /* one NOP to align */
149 	char		isr_hdr[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN & OFFSET */
150 	struct in_addr	isr_routes[MAX_IPOPTLEN/sizeof(struct in_addr)];
151 };
152 
153 void save_rte(struct mbuf *, u_char *, struct in_addr);
154 
155 /*
156  * IP initialization: fill in IP protocol switch table.
157  * All protocols not implemented in kernel go to raw IP protocol handler.
158  */
159 void
160 ip_init(void)
161 {
162 	struct protosw *pr;
163 	int i;
164 	const u_int16_t defbaddynamicports_tcp[] = DEFBADDYNAMICPORTS_TCP;
165 	const u_int16_t defbaddynamicports_udp[] = DEFBADDYNAMICPORTS_UDP;
166 	const u_int16_t defrootonlyports_tcp[] = DEFROOTONLYPORTS_TCP;
167 	const u_int16_t defrootonlyports_udp[] = DEFROOTONLYPORTS_UDP;
168 
169 	pool_init(&ipqent_pool, sizeof(struct ipqent), 0,
170 	    IPL_SOFTNET, 0, "ipqe",  NULL);
171 	pool_init(&ipq_pool, sizeof(struct ipq), 0,
172 	    IPL_SOFTNET, 0, "ipq", NULL);
173 
174 	pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
175 	if (pr == NULL)
176 		panic("ip_init");
177 	for (i = 0; i < IPPROTO_MAX; i++)
178 		ip_protox[i] = pr - inetsw;
179 	for (pr = inetdomain.dom_protosw;
180 	    pr < inetdomain.dom_protoswNPROTOSW; pr++)
181 		if (pr->pr_domain->dom_family == PF_INET &&
182 		    pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW &&
183 		    pr->pr_protocol < IPPROTO_MAX)
184 			ip_protox[pr->pr_protocol] = pr - inetsw;
185 	LIST_INIT(&ipq);
186 	if (ip_mtudisc != 0)
187 		ip_mtudisc_timeout_q =
188 		    rt_timer_queue_create(ip_mtudisc_timeout);
189 
190 	/* Fill in list of ports not to allocate dynamically. */
191 	memset(&baddynamicports, 0, sizeof(baddynamicports));
192 	for (i = 0; defbaddynamicports_tcp[i] != 0; i++)
193 		DP_SET(baddynamicports.tcp, defbaddynamicports_tcp[i]);
194 	for (i = 0; defbaddynamicports_udp[i] != 0; i++)
195 		DP_SET(baddynamicports.udp, defbaddynamicports_udp[i]);
196 
197 	/* Fill in list of ports only root can bind to. */
198 	memset(&rootonlyports, 0, sizeof(rootonlyports));
199 	for (i = 0; defrootonlyports_tcp[i] != 0; i++)
200 		DP_SET(rootonlyports.tcp, defrootonlyports_tcp[i]);
201 	for (i = 0; defrootonlyports_udp[i] != 0; i++)
202 		DP_SET(rootonlyports.udp, defrootonlyports_udp[i]);
203 
204 	strlcpy(ipsec_def_enc, IPSEC_DEFAULT_DEF_ENC, sizeof(ipsec_def_enc));
205 	strlcpy(ipsec_def_auth, IPSEC_DEFAULT_DEF_AUTH, sizeof(ipsec_def_auth));
206 	strlcpy(ipsec_def_comp, IPSEC_DEFAULT_DEF_COMP, sizeof(ipsec_def_comp));
207 
208 	mq_init(&ipsend_mq, 64, IPL_SOFTNET);
209 }
210 
211 void
212 ipintr(void)
213 {
214 	struct mbuf *m;
215 
216 	/*
217 	 * Get next datagram off input queue and get IP header
218 	 * in first mbuf.
219 	 */
220 	while ((m = niq_dequeue(&ipintrq)) != NULL) {
221 #ifdef	DIAGNOSTIC
222 		if ((m->m_flags & M_PKTHDR) == 0)
223 			panic("ipintr no HDR");
224 #endif
225 		ipv4_input(m);
226 	}
227 }
228 
229 /*
230  * IPv4 input routine.
231  *
232  * Checksum and byte swap header.  Process options. Forward or deliver.
233  */
234 void
235 ipv4_input(struct mbuf *m)
236 {
237 	struct ifnet	*ifp;
238 	struct rtentry	*rt = NULL;
239 	struct ip	*ip;
240 	int hlen, len;
241 #if defined(MROUTING) || defined(IPSEC)
242 	int rv;
243 #endif
244 	in_addr_t pfrdr = 0;
245 
246 	ifp = if_get(m->m_pkthdr.ph_ifidx);
247 	if (ifp == NULL)
248 		goto bad;
249 
250 	ipstat.ips_total++;
251 	if (m->m_len < sizeof (struct ip) &&
252 	    (m = m_pullup(m, sizeof (struct ip))) == NULL) {
253 		ipstat.ips_toosmall++;
254 		goto out;
255 	}
256 	ip = mtod(m, struct ip *);
257 	if (ip->ip_v != IPVERSION) {
258 		ipstat.ips_badvers++;
259 		goto bad;
260 	}
261 	hlen = ip->ip_hl << 2;
262 	if (hlen < sizeof(struct ip)) {	/* minimum header length */
263 		ipstat.ips_badhlen++;
264 		goto bad;
265 	}
266 	if (hlen > m->m_len) {
267 		if ((m = m_pullup(m, hlen)) == NULL) {
268 			ipstat.ips_badhlen++;
269 			goto out;
270 		}
271 		ip = mtod(m, struct ip *);
272 	}
273 
274 	/* 127/8 must not appear on wire - RFC1122 */
275 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
276 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
277 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
278 			ipstat.ips_badaddr++;
279 			goto bad;
280 		}
281 	}
282 
283 	if ((m->m_pkthdr.csum_flags & M_IPV4_CSUM_IN_OK) == 0) {
284 		if (m->m_pkthdr.csum_flags & M_IPV4_CSUM_IN_BAD) {
285 			ipstat.ips_badsum++;
286 			goto bad;
287 		}
288 
289 		ipstat.ips_inswcsum++;
290 		if (in_cksum(m, hlen) != 0) {
291 			ipstat.ips_badsum++;
292 			goto bad;
293 		}
294 	}
295 
296 	/* Retrieve the packet length. */
297 	len = ntohs(ip->ip_len);
298 
299 	/*
300 	 * Convert fields to host representation.
301 	 */
302 	if (len < hlen) {
303 		ipstat.ips_badlen++;
304 		goto bad;
305 	}
306 
307 	/*
308 	 * Check that the amount of data in the buffers
309 	 * is at least as much as the IP header would have us expect.
310 	 * Trim mbufs if longer than we expect.
311 	 * Drop packet if shorter than we expect.
312 	 */
313 	if (m->m_pkthdr.len < len) {
314 		ipstat.ips_tooshort++;
315 		goto bad;
316 	}
317 	if (m->m_pkthdr.len > len) {
318 		if (m->m_len == m->m_pkthdr.len) {
319 			m->m_len = len;
320 			m->m_pkthdr.len = len;
321 		} else
322 			m_adj(m, len - m->m_pkthdr.len);
323 	}
324 
325 #if NCARP > 0
326 	if (ifp->if_type == IFT_CARP && ip->ip_p != IPPROTO_ICMP &&
327 	    carp_lsdrop(m, AF_INET, &ip->ip_src.s_addr, &ip->ip_dst.s_addr))
328 		goto bad;
329 #endif
330 
331 #if NPF > 0
332 	/*
333 	 * Packet filter
334 	 */
335 	pfrdr = ip->ip_dst.s_addr;
336 	if (pf_test(AF_INET, PF_IN, ifp, &m) != PF_PASS)
337 		goto bad;
338 	if (m == NULL)
339 		goto out;
340 
341 	ip = mtod(m, struct ip *);
342 	hlen = ip->ip_hl << 2;
343 	pfrdr = (pfrdr != ip->ip_dst.s_addr);
344 #endif
345 
346 	/*
347 	 * Process options and, if not destined for us,
348 	 * ship it on.  ip_dooptions returns 1 when an
349 	 * error was detected (causing an icmp message
350 	 * to be sent and the original packet to be freed).
351 	 */
352 	if (hlen > sizeof (struct ip) && ip_dooptions(m, ifp)) {
353 	        goto out;
354 	}
355 
356 	if (in_ouraddr(m, ifp, &rt)) {
357 		ip_ours(m);
358 		goto out;
359 	}
360 
361 	if (IN_MULTICAST(ip->ip_dst.s_addr)) {
362 		/*
363 		 * Make sure M_MCAST is set.  It should theoretically
364 		 * already be there, but let's play safe because upper
365 		 * layers check for this flag.
366 		 */
367 		m->m_flags |= M_MCAST;
368 
369 #ifdef MROUTING
370 		if (ipmforwarding && ip_mrouter) {
371 			if (m->m_flags & M_EXT) {
372 				if ((m = m_pullup(m, hlen)) == NULL) {
373 					ipstat.ips_toosmall++;
374 					goto out;
375 				}
376 				ip = mtod(m, struct ip *);
377 			}
378 			/*
379 			 * If we are acting as a multicast router, all
380 			 * incoming multicast packets are passed to the
381 			 * kernel-level multicast forwarding function.
382 			 * The packet is returned (relatively) intact; if
383 			 * ip_mforward() returns a non-zero value, the packet
384 			 * must be discarded, else it may be accepted below.
385 			 *
386 			 * (The IP ident field is put in the same byte order
387 			 * as expected when ip_mforward() is called from
388 			 * ip_output().)
389 			 */
390 			KERNEL_LOCK();
391 			rv = ip_mforward(m, ifp);
392 			KERNEL_UNLOCK();
393 			if (rv != 0) {
394 				ipstat.ips_cantforward++;
395 				goto bad;
396 			}
397 
398 			/*
399 			 * The process-level routing daemon needs to receive
400 			 * all multicast IGMP packets, whether or not this
401 			 * host belongs to their destination groups.
402 			 */
403 			if (ip->ip_p == IPPROTO_IGMP) {
404 				ip_ours(m);
405 				goto out;
406 			}
407 			ipstat.ips_forward++;
408 		}
409 #endif
410 		/*
411 		 * See if we belong to the destination multicast group on the
412 		 * arrival interface.
413 		 */
414 		if (!in_hasmulti(&ip->ip_dst, ifp)) {
415 			ipstat.ips_notmember++;
416 			if (!IN_LOCAL_GROUP(ip->ip_dst.s_addr))
417 				ipstat.ips_cantforward++;
418 			goto bad;
419 		}
420 		ip_ours(m);
421 		goto out;
422 	}
423 
424 	if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
425 	    ip->ip_dst.s_addr == INADDR_ANY) {
426 		ip_ours(m);
427 		goto out;
428 	}
429 
430 #if NCARP > 0
431 	if (ifp->if_type == IFT_CARP && ip->ip_p == IPPROTO_ICMP &&
432 	    carp_lsdrop(m, AF_INET, &ip->ip_src.s_addr, &ip->ip_dst.s_addr))
433 		goto bad;
434 #endif
435 	/*
436 	 * Not for us; forward if possible and desirable.
437 	 */
438 	if (ipforwarding == 0) {
439 		ipstat.ips_cantforward++;
440 		goto bad;
441 	}
442 #ifdef IPSEC
443 	if (ipsec_in_use) {
444 		KERNEL_LOCK();
445 		rv = ip_input_ipsec_fwd_check(m, hlen);
446 		KERNEL_UNLOCK();
447 		if (rv != 0) {
448 			ipstat.ips_cantforward++;
449 			goto bad;
450 		}
451 		/*
452 		 * Fall through, forward packet. Outbound IPsec policy
453 		 * checking will occur in ip_output().
454 		 */
455 	}
456 #endif /* IPSEC */
457 
458 	ip_forward(m, ifp, rt, pfrdr);
459 	if_put(ifp);
460 	return;
461 bad:
462 	m_freem(m);
463 out:
464 	rtfree(rt);
465 	if_put(ifp);
466 }
467 
468 /*
469  * IPv4 local-delivery routine.
470  *
471  * If fragmented try to reassemble.  Pass to next level.
472  */
473 void
474 ip_ours(struct mbuf *m)
475 {
476 	struct ip *ip = mtod(m, struct ip *);
477 	struct ipq *fp;
478 	struct ipqent *ipqe;
479 	int mff, hlen;
480 
481 	hlen = ip->ip_hl << 2;
482 
483 	/* pf might have modified stuff, might have to chksum */
484 	in_proto_cksum_out(m, NULL);
485 
486 	/*
487 	 * If offset or IP_MF are set, must reassemble.
488 	 * Otherwise, nothing need be done.
489 	 * (We could look in the reassembly queue to see
490 	 * if the packet was previously fragmented,
491 	 * but it's not worth the time; just let them time out.)
492 	 */
493 	if (ip->ip_off &~ htons(IP_DF | IP_RF)) {
494 		if (m->m_flags & M_EXT) {		/* XXX */
495 			if ((m = m_pullup(m, hlen)) == NULL) {
496 				ipstat.ips_toosmall++;
497 				return;
498 			}
499 			ip = mtod(m, struct ip *);
500 		}
501 
502 		/*
503 		 * Look for queue of fragments
504 		 * of this datagram.
505 		 */
506 		LIST_FOREACH(fp, &ipq, ipq_q)
507 			if (ip->ip_id == fp->ipq_id &&
508 			    ip->ip_src.s_addr == fp->ipq_src.s_addr &&
509 			    ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
510 			    ip->ip_p == fp->ipq_p)
511 				goto found;
512 		fp = 0;
513 found:
514 
515 		/*
516 		 * Adjust ip_len to not reflect header,
517 		 * set ipqe_mff if more fragments are expected,
518 		 * convert offset of this to bytes.
519 		 */
520 		ip->ip_len = htons(ntohs(ip->ip_len) - hlen);
521 		mff = (ip->ip_off & htons(IP_MF)) != 0;
522 		if (mff) {
523 			/*
524 			 * Make sure that fragments have a data length
525 			 * that's a non-zero multiple of 8 bytes.
526 			 */
527 			if (ntohs(ip->ip_len) == 0 ||
528 			    (ntohs(ip->ip_len) & 0x7) != 0) {
529 				ipstat.ips_badfrags++;
530 				goto bad;
531 			}
532 		}
533 		ip->ip_off = htons(ntohs(ip->ip_off) << 3);
534 
535 		/*
536 		 * If datagram marked as having more fragments
537 		 * or if this is not the first fragment,
538 		 * attempt reassembly; if it succeeds, proceed.
539 		 */
540 		if (mff || ip->ip_off) {
541 			ipstat.ips_fragments++;
542 			if (ip_frags + 1 > ip_maxqueue) {
543 				ip_flush();
544 				ipstat.ips_rcvmemdrop++;
545 				goto bad;
546 			}
547 
548 			ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
549 			if (ipqe == NULL) {
550 				ipstat.ips_rcvmemdrop++;
551 				goto bad;
552 			}
553 			ip_frags++;
554 			ipqe->ipqe_mff = mff;
555 			ipqe->ipqe_m = m;
556 			ipqe->ipqe_ip = ip;
557 			m = ip_reass(ipqe, fp);
558 			if (m == NULL) {
559 				return;
560 			}
561 			ipstat.ips_reassembled++;
562 			ip = mtod(m, struct ip *);
563 			hlen = ip->ip_hl << 2;
564 			ip->ip_len = htons(ntohs(ip->ip_len) + hlen);
565 		} else
566 			if (fp)
567 				ip_freef(fp);
568 	}
569 
570 #ifdef IPSEC
571 	if (ipsec_in_use) {
572 		if (ip_input_ipsec_ours_check(m, hlen) != 0) {
573 			ipstat.ips_cantforward++;
574 			goto bad;
575 		}
576 	}
577 	/* Otherwise, just fall through and deliver the packet */
578 #endif /* IPSEC */
579 
580 	/*
581 	 * Switch out to protocol's input routine.
582 	 */
583 	ipstat.ips_delivered++;
584 	(*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen, NULL, 0);
585 	return;
586 bad:
587 	m_freem(m);
588 }
589 
590 int
591 in_ouraddr(struct mbuf *m, struct ifnet *ifp, struct rtentry **prt)
592 {
593 	struct rtentry		*rt;
594 	struct ip		*ip;
595 	struct sockaddr_in	 sin;
596 	int			 match = 0;
597 
598 #if NPF > 0
599 	switch (pf_ouraddr(m)) {
600 	case 0:
601 		return (0);
602 	case 1:
603 		return (1);
604 	default:
605 		/* pf does not know it */
606 		break;
607 	}
608 #endif
609 
610 	ip = mtod(m, struct ip *);
611 
612 	memset(&sin, 0, sizeof(sin));
613 	sin.sin_len = sizeof(sin);
614 	sin.sin_family = AF_INET;
615 	sin.sin_addr = ip->ip_dst;
616 	rt = rtalloc_mpath(sintosa(&sin), &ip->ip_src.s_addr,
617 	    m->m_pkthdr.ph_rtableid);
618 	if (rtisvalid(rt)) {
619 		if (ISSET(rt->rt_flags, RTF_LOCAL))
620 			match = 1;
621 
622 		/*
623 		 * If directedbcast is enabled we only consider it local
624 		 * if it is received on the interface with that address.
625 		 */
626 		if (ISSET(rt->rt_flags, RTF_BROADCAST) &&
627 		    (!ip_directedbcast || rt->rt_ifidx == ifp->if_index)) {
628 			match = 1;
629 
630 			/* Make sure M_BCAST is set */
631 			m->m_flags |= M_BCAST;
632 		}
633 	}
634 	*prt = rt;
635 
636 	if (!match) {
637 		struct ifaddr *ifa;
638 
639 		/*
640 		 * No local address or broadcast address found, so check for
641 		 * ancient classful broadcast addresses.
642 		 * It must have been broadcast on the link layer, and for an
643 		 * address on the interface it was received on.
644 		 */
645 		if (!ISSET(m->m_flags, M_BCAST) ||
646 		    !IN_CLASSFULBROADCAST(ip->ip_dst.s_addr, ip->ip_dst.s_addr))
647 			return (0);
648 
649 		if (ifp->if_rdomain != rtable_l2(m->m_pkthdr.ph_rtableid))
650 			return (0);
651 		/*
652 		 * The check in the loop assumes you only rx a packet on an UP
653 		 * interface, and that M_BCAST will only be set on a BROADCAST
654 		 * interface.
655 		 */
656 		KERNEL_LOCK();
657 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
658 			if (ifa->ifa_addr->sa_family != AF_INET)
659 				continue;
660 
661 			if (IN_CLASSFULBROADCAST(ip->ip_dst.s_addr,
662 			    ifatoia(ifa)->ia_addr.sin_addr.s_addr)) {
663 			    	match = 1;
664 			    	break;
665 			}
666 		}
667 		KERNEL_UNLOCK();
668 	}
669 
670 	return (match);
671 }
672 
673 #ifdef IPSEC
674 int
675 ip_input_ipsec_fwd_check(struct mbuf *m, int hlen)
676 {
677 	struct tdb *tdb;
678 	struct tdb_ident *tdbi;
679 	struct m_tag *mtag;
680 	int error = 0;
681 
682 	/*
683 	 * IPsec policy check for forwarded packets. Look at
684 	 * inner-most IPsec SA used.
685 	 */
686 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
687 	if (mtag != NULL) {
688 		tdbi = (struct tdb_ident *)(mtag + 1);
689 		tdb = gettdb(tdbi->rdomain, tdbi->spi, &tdbi->dst, tdbi->proto);
690 	} else
691 		tdb = NULL;
692 	ipsp_spd_lookup(m, AF_INET, hlen, &error, IPSP_DIRECTION_IN, tdb, NULL,
693 	    0);
694 
695 	return error;
696 }
697 
698 int
699 ip_input_ipsec_ours_check(struct mbuf *m, int hlen)
700 {
701 	struct ip *ip = mtod(m, struct ip *);
702 	struct tdb *tdb;
703 	struct tdb_ident *tdbi;
704 	struct m_tag *mtag;
705 	int error = 0;
706 
707 	/*
708 	 * If it's a protected packet for us, skip the policy check.
709 	 * That's because we really only care about the properties of
710 	 * the protected packet, and not the intermediate versions.
711 	 * While this is not the most paranoid setting, it allows
712 	 * some flexibility in handling nested tunnels (in setting up
713 	 * the policies).
714 	 */
715 	if ((ip->ip_p == IPPROTO_ESP) || (ip->ip_p == IPPROTO_AH) ||
716 	    (ip->ip_p == IPPROTO_IPCOMP))
717 		return 0;
718 
719 	/*
720 	 * If the protected packet was tunneled, then we need to
721 	 * verify the protected packet's information, not the
722 	 * external headers. Thus, skip the policy lookup for the
723 	 * external packet, and keep the IPsec information linked on
724 	 * the packet header (the encapsulation routines know how
725 	 * to deal with that).
726 	 */
727 	if ((ip->ip_p == IPPROTO_IPIP) || (ip->ip_p == IPPROTO_IPV6))
728 		return 0;
729 
730 	/*
731 	 * If the protected packet is TCP or UDP, we'll do the
732 	 * policy check in the respective input routine, so we can
733 	 * check for bypass sockets.
734 	 */
735 	if ((ip->ip_p == IPPROTO_TCP) || (ip->ip_p == IPPROTO_UDP))
736 		return 0;
737 
738 	/*
739 	 * IPsec policy check for local-delivery packets. Look at the
740 	 * inner-most SA that protected the packet. This is in fact
741 	 * a bit too restrictive (it could end up causing packets to
742 	 * be dropped that semantically follow the policy, e.g., in
743 	 * certain SA-bundle configurations); but the alternative is
744 	 * very complicated (and requires keeping track of what
745 	 * kinds of tunneling headers have been seen in-between the
746 	 * IPsec headers), and I don't think we lose much functionality
747 	 * that's needed in the real world (who uses bundles anyway ?).
748 	 */
749 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
750 	if (mtag) {
751 		tdbi = (struct tdb_ident *)(mtag + 1);
752 		tdb = gettdb(tdbi->rdomain, tdbi->spi, &tdbi->dst,
753 		    tdbi->proto);
754 	} else
755 		tdb = NULL;
756 	ipsp_spd_lookup(m, AF_INET, hlen, &error, IPSP_DIRECTION_IN,
757 	    tdb, NULL, 0);
758 
759 	return error;
760 }
761 #endif /* IPSEC */
762 
763 /*
764  * Take incoming datagram fragment and try to
765  * reassemble it into whole datagram.  If a chain for
766  * reassembly of this datagram already exists, then it
767  * is given as fp; otherwise have to make a chain.
768  */
769 struct mbuf *
770 ip_reass(struct ipqent *ipqe, struct ipq *fp)
771 {
772 	struct mbuf *m = ipqe->ipqe_m;
773 	struct ipqent *nq, *p, *q;
774 	struct ip *ip;
775 	struct mbuf *t;
776 	int hlen = ipqe->ipqe_ip->ip_hl << 2;
777 	int i, next;
778 	u_int8_t ecn, ecn0;
779 
780 	/*
781 	 * Presence of header sizes in mbufs
782 	 * would confuse code below.
783 	 */
784 	m->m_data += hlen;
785 	m->m_len -= hlen;
786 
787 	/*
788 	 * If first fragment to arrive, create a reassembly queue.
789 	 */
790 	if (fp == NULL) {
791 		fp = pool_get(&ipq_pool, PR_NOWAIT);
792 		if (fp == NULL)
793 			goto dropfrag;
794 		LIST_INSERT_HEAD(&ipq, fp, ipq_q);
795 		fp->ipq_ttl = IPFRAGTTL;
796 		fp->ipq_p = ipqe->ipqe_ip->ip_p;
797 		fp->ipq_id = ipqe->ipqe_ip->ip_id;
798 		LIST_INIT(&fp->ipq_fragq);
799 		fp->ipq_src = ipqe->ipqe_ip->ip_src;
800 		fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
801 		p = NULL;
802 		goto insert;
803 	}
804 
805 	/*
806 	 * Handle ECN by comparing this segment with the first one;
807 	 * if CE is set, do not lose CE.
808 	 * drop if CE and not-ECT are mixed for the same packet.
809 	 */
810 	ecn = ipqe->ipqe_ip->ip_tos & IPTOS_ECN_MASK;
811 	ecn0 = LIST_FIRST(&fp->ipq_fragq)->ipqe_ip->ip_tos & IPTOS_ECN_MASK;
812 	if (ecn == IPTOS_ECN_CE) {
813 		if (ecn0 == IPTOS_ECN_NOTECT)
814 			goto dropfrag;
815 		if (ecn0 != IPTOS_ECN_CE)
816 			LIST_FIRST(&fp->ipq_fragq)->ipqe_ip->ip_tos |= IPTOS_ECN_CE;
817 	}
818 	if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
819 		goto dropfrag;
820 
821 	/*
822 	 * Find a segment which begins after this one does.
823 	 */
824 	for (p = NULL, q = LIST_FIRST(&fp->ipq_fragq); q != NULL;
825 	    p = q, q = LIST_NEXT(q, ipqe_q))
826 		if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off))
827 			break;
828 
829 	/*
830 	 * If there is a preceding segment, it may provide some of
831 	 * our data already.  If so, drop the data from the incoming
832 	 * segment.  If it provides all of our data, drop us.
833 	 */
834 	if (p != NULL) {
835 		i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) -
836 		    ntohs(ipqe->ipqe_ip->ip_off);
837 		if (i > 0) {
838 			if (i >= ntohs(ipqe->ipqe_ip->ip_len))
839 				goto dropfrag;
840 			m_adj(ipqe->ipqe_m, i);
841 			ipqe->ipqe_ip->ip_off =
842 			    htons(ntohs(ipqe->ipqe_ip->ip_off) + i);
843 			ipqe->ipqe_ip->ip_len =
844 			    htons(ntohs(ipqe->ipqe_ip->ip_len) - i);
845 		}
846 	}
847 
848 	/*
849 	 * While we overlap succeeding segments trim them or,
850 	 * if they are completely covered, dequeue them.
851 	 */
852 	for (; q != NULL &&
853 	    ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) >
854 	    ntohs(q->ipqe_ip->ip_off); q = nq) {
855 		i = (ntohs(ipqe->ipqe_ip->ip_off) +
856 		    ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off);
857 		if (i < ntohs(q->ipqe_ip->ip_len)) {
858 			q->ipqe_ip->ip_len =
859 			    htons(ntohs(q->ipqe_ip->ip_len) - i);
860 			q->ipqe_ip->ip_off =
861 			    htons(ntohs(q->ipqe_ip->ip_off) + i);
862 			m_adj(q->ipqe_m, i);
863 			break;
864 		}
865 		nq = LIST_NEXT(q, ipqe_q);
866 		m_freem(q->ipqe_m);
867 		LIST_REMOVE(q, ipqe_q);
868 		pool_put(&ipqent_pool, q);
869 		ip_frags--;
870 	}
871 
872 insert:
873 	/*
874 	 * Stick new segment in its place;
875 	 * check for complete reassembly.
876 	 */
877 	if (p == NULL) {
878 		LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
879 	} else {
880 		LIST_INSERT_AFTER(p, ipqe, ipqe_q);
881 	}
882 	next = 0;
883 	for (p = NULL, q = LIST_FIRST(&fp->ipq_fragq); q != NULL;
884 	    p = q, q = LIST_NEXT(q, ipqe_q)) {
885 		if (ntohs(q->ipqe_ip->ip_off) != next)
886 			return (0);
887 		next += ntohs(q->ipqe_ip->ip_len);
888 	}
889 	if (p->ipqe_mff)
890 		return (0);
891 
892 	/*
893 	 * Reassembly is complete.  Check for a bogus message size and
894 	 * concatenate fragments.
895 	 */
896 	q = LIST_FIRST(&fp->ipq_fragq);
897 	ip = q->ipqe_ip;
898 	if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
899 		ipstat.ips_toolong++;
900 		ip_freef(fp);
901 		return (0);
902 	}
903 	m = q->ipqe_m;
904 	t = m->m_next;
905 	m->m_next = 0;
906 	m_cat(m, t);
907 	nq = LIST_NEXT(q, ipqe_q);
908 	pool_put(&ipqent_pool, q);
909 	ip_frags--;
910 	for (q = nq; q != NULL; q = nq) {
911 		t = q->ipqe_m;
912 		nq = LIST_NEXT(q, ipqe_q);
913 		pool_put(&ipqent_pool, q);
914 		ip_frags--;
915 		m_cat(m, t);
916 	}
917 
918 	/*
919 	 * Create header for new ip packet by
920 	 * modifying header of first packet;
921 	 * dequeue and discard fragment reassembly header.
922 	 * Make header visible.
923 	 */
924 	ip->ip_len = htons(next);
925 	ip->ip_src = fp->ipq_src;
926 	ip->ip_dst = fp->ipq_dst;
927 	LIST_REMOVE(fp, ipq_q);
928 	pool_put(&ipq_pool, fp);
929 	m->m_len += (ip->ip_hl << 2);
930 	m->m_data -= (ip->ip_hl << 2);
931 	/* some debugging cruft by sklower, below, will go away soon */
932 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
933 		int plen = 0;
934 		for (t = m; t; t = t->m_next)
935 			plen += t->m_len;
936 		m->m_pkthdr.len = plen;
937 	}
938 	return (m);
939 
940 dropfrag:
941 	ipstat.ips_fragdropped++;
942 	m_freem(m);
943 	pool_put(&ipqent_pool, ipqe);
944 	ip_frags--;
945 	return (0);
946 }
947 
948 /*
949  * Free a fragment reassembly header and all
950  * associated datagrams.
951  */
952 void
953 ip_freef(struct ipq *fp)
954 {
955 	struct ipqent *q, *p;
956 
957 	for (q = LIST_FIRST(&fp->ipq_fragq); q != NULL; q = p) {
958 		p = LIST_NEXT(q, ipqe_q);
959 		m_freem(q->ipqe_m);
960 		LIST_REMOVE(q, ipqe_q);
961 		pool_put(&ipqent_pool, q);
962 		ip_frags--;
963 	}
964 	LIST_REMOVE(fp, ipq_q);
965 	pool_put(&ipq_pool, fp);
966 }
967 
968 /*
969  * IP timer processing;
970  * if a timer expires on a reassembly queue, discard it.
971  * clear the forwarding cache, there might be a better route.
972  */
973 void
974 ip_slowtimo(void)
975 {
976 	struct ipq *fp, *nfp;
977 	int s = splsoftnet();
978 
979 	for (fp = LIST_FIRST(&ipq); fp != NULL; fp = nfp) {
980 		nfp = LIST_NEXT(fp, ipq_q);
981 		if (--fp->ipq_ttl == 0) {
982 			ipstat.ips_fragtimeout++;
983 			ip_freef(fp);
984 		}
985 	}
986 	splx(s);
987 }
988 
989 /*
990  * Drain off all datagram fragments.
991  */
992 void
993 ip_drain(void)
994 {
995 	while (!LIST_EMPTY(&ipq)) {
996 		ipstat.ips_fragdropped++;
997 		ip_freef(LIST_FIRST(&ipq));
998 	}
999 }
1000 
1001 /*
1002  * Flush a bunch of datagram fragments, till we are down to 75%.
1003  */
1004 void
1005 ip_flush(void)
1006 {
1007 	int max = 50;
1008 
1009 	/* ipq already locked */
1010 	while (!LIST_EMPTY(&ipq) && ip_frags > ip_maxqueue * 3 / 4 && --max) {
1011 		ipstat.ips_fragdropped++;
1012 		ip_freef(LIST_FIRST(&ipq));
1013 	}
1014 }
1015 
1016 /*
1017  * Do option processing on a datagram,
1018  * possibly discarding it if bad options are encountered,
1019  * or forwarding it if source-routed.
1020  * Returns 1 if packet has been forwarded/freed,
1021  * 0 if the packet should be processed further.
1022  */
1023 int
1024 ip_dooptions(struct mbuf *m, struct ifnet *ifp)
1025 {
1026 	struct ip *ip = mtod(m, struct ip *);
1027 	unsigned int rtableid = m->m_pkthdr.ph_rtableid;
1028 	struct rtentry *rt;
1029 	struct sockaddr_in ipaddr;
1030 	u_char *cp;
1031 	struct ip_timestamp ipt;
1032 	struct in_ifaddr *ia;
1033 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1034 	struct in_addr sin, dst;
1035 	u_int32_t ntime;
1036 
1037 	dst = ip->ip_dst;
1038 	cp = (u_char *)(ip + 1);
1039 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1040 
1041 	KERNEL_LOCK();
1042 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1043 		opt = cp[IPOPT_OPTVAL];
1044 		if (opt == IPOPT_EOL)
1045 			break;
1046 		if (opt == IPOPT_NOP)
1047 			optlen = 1;
1048 		else {
1049 			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1050 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1051 				goto bad;
1052 			}
1053 			optlen = cp[IPOPT_OLEN];
1054 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1055 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1056 				goto bad;
1057 			}
1058 		}
1059 
1060 		switch (opt) {
1061 
1062 		default:
1063 			break;
1064 
1065 		/*
1066 		 * Source routing with record.
1067 		 * Find interface with current destination address.
1068 		 * If none on this machine then drop if strictly routed,
1069 		 * or do nothing if loosely routed.
1070 		 * Record interface address and bring up next address
1071 		 * component.  If strictly routed make sure next
1072 		 * address is on directly accessible net.
1073 		 */
1074 		case IPOPT_LSRR:
1075 		case IPOPT_SSRR:
1076 			if (!ip_dosourceroute) {
1077 				type = ICMP_UNREACH;
1078 				code = ICMP_UNREACH_SRCFAIL;
1079 				goto bad;
1080 			}
1081 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1082 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1083 				goto bad;
1084 			}
1085 			memset(&ipaddr, 0, sizeof(ipaddr));
1086 			ipaddr.sin_family = AF_INET;
1087 			ipaddr.sin_len = sizeof(ipaddr);
1088 			ipaddr.sin_addr = ip->ip_dst;
1089 			ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr),
1090 			    m->m_pkthdr.ph_rtableid));
1091 			if (ia == NULL) {
1092 				if (opt == IPOPT_SSRR) {
1093 					type = ICMP_UNREACH;
1094 					code = ICMP_UNREACH_SRCFAIL;
1095 					goto bad;
1096 				}
1097 				/*
1098 				 * Loose routing, and not at next destination
1099 				 * yet; nothing to do except forward.
1100 				 */
1101 				break;
1102 			}
1103 			off--;			/* 0 origin */
1104 			if ((off + sizeof(struct in_addr)) > optlen) {
1105 				/*
1106 				 * End of source route.  Should be for us.
1107 				 */
1108 				save_rte(m, cp, ip->ip_src);
1109 				break;
1110 			}
1111 
1112 			/*
1113 			 * locate outgoing interface
1114 			 */
1115 			memset(&ipaddr, 0, sizeof(ipaddr));
1116 			ipaddr.sin_family = AF_INET;
1117 			ipaddr.sin_len = sizeof(ipaddr);
1118 			memcpy(&ipaddr.sin_addr, cp + off,
1119 			    sizeof(ipaddr.sin_addr));
1120 			if (opt == IPOPT_SSRR) {
1121 			    if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(&ipaddr),
1122 				m->m_pkthdr.ph_rtableid))) == NULL)
1123 				ia = ifatoia(ifa_ifwithnet(sintosa(&ipaddr),
1124 				    m->m_pkthdr.ph_rtableid));
1125 				if (ia == NULL) {
1126 					type = ICMP_UNREACH;
1127 					code = ICMP_UNREACH_SRCFAIL;
1128 					goto bad;
1129 				}
1130 				memcpy(cp + off, &ia->ia_addr.sin_addr,
1131 				    sizeof(struct in_addr));
1132 				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1133 			} else {
1134 				/* keep packet in the virtual instance */
1135 				rt = rtalloc(sintosa(&ipaddr), RT_RESOLVE,
1136 				    rtableid);
1137 				if (!rtisvalid(rt)) {
1138 					type = ICMP_UNREACH;
1139 					code = ICMP_UNREACH_SRCFAIL;
1140 					rtfree(rt);
1141 					goto bad;
1142 				}
1143 				ia = ifatoia(rt->rt_ifa);
1144 				memcpy(cp + off, &ia->ia_addr.sin_addr,
1145 				    sizeof(struct in_addr));
1146 				rtfree(rt);
1147 				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1148 			}
1149 			ip->ip_dst = ipaddr.sin_addr;
1150 			/*
1151 			 * Let ip_intr's mcast routing check handle mcast pkts
1152 			 */
1153 			forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1154 			break;
1155 
1156 		case IPOPT_RR:
1157 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1158 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1159 				goto bad;
1160 			}
1161 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1162 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1163 				goto bad;
1164 			}
1165 
1166 			/*
1167 			 * If no space remains, ignore.
1168 			 */
1169 			off--;			/* 0 origin */
1170 			if ((off + sizeof(struct in_addr)) > optlen)
1171 				break;
1172 			memset(&ipaddr, 0, sizeof(ipaddr));
1173 			ipaddr.sin_family = AF_INET;
1174 			ipaddr.sin_len = sizeof(ipaddr);
1175 			ipaddr.sin_addr = ip->ip_dst;
1176 			/*
1177 			 * locate outgoing interface; if we're the destination,
1178 			 * use the incoming interface (should be same).
1179 			 * Again keep the packet inside the virtual instance.
1180 			 */
1181 			rt = rtalloc(sintosa(&ipaddr), RT_RESOLVE, rtableid);
1182 			if (!rtisvalid(rt)) {
1183 				type = ICMP_UNREACH;
1184 				code = ICMP_UNREACH_HOST;
1185 				rtfree(rt);
1186 				goto bad;
1187 			}
1188 			ia = ifatoia(rt->rt_ifa);
1189 			memcpy(cp + off, &ia->ia_addr.sin_addr,
1190 			    sizeof(struct in_addr));
1191 			rtfree(rt);
1192 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1193 			break;
1194 
1195 		case IPOPT_TS:
1196 			code = cp - (u_char *)ip;
1197 			if (optlen < sizeof(struct ip_timestamp))
1198 				goto bad;
1199 			memcpy(&ipt, cp, sizeof(struct ip_timestamp));
1200 			if (ipt.ipt_ptr < 5 || ipt.ipt_len < 5)
1201 				goto bad;
1202 			if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) > ipt.ipt_len) {
1203 				if (++ipt.ipt_oflw == 0)
1204 					goto bad;
1205 				break;
1206 			}
1207 			memcpy(&sin, cp + ipt.ipt_ptr - 1, sizeof sin);
1208 			switch (ipt.ipt_flg) {
1209 
1210 			case IPOPT_TS_TSONLY:
1211 				break;
1212 
1213 			case IPOPT_TS_TSANDADDR:
1214 				if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) +
1215 				    sizeof(struct in_addr) > ipt.ipt_len)
1216 					goto bad;
1217 				memset(&ipaddr, 0, sizeof(ipaddr));
1218 				ipaddr.sin_family = AF_INET;
1219 				ipaddr.sin_len = sizeof(ipaddr);
1220 				ipaddr.sin_addr = dst;
1221 				ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1222 				    ifp));
1223 				if (ia == NULL)
1224 					continue;
1225 				memcpy(&sin, &ia->ia_addr.sin_addr,
1226 				    sizeof(struct in_addr));
1227 				ipt.ipt_ptr += sizeof(struct in_addr);
1228 				break;
1229 
1230 			case IPOPT_TS_PRESPEC:
1231 				if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) +
1232 				    sizeof(struct in_addr) > ipt.ipt_len)
1233 					goto bad;
1234 				memset(&ipaddr, 0, sizeof(ipaddr));
1235 				ipaddr.sin_family = AF_INET;
1236 				ipaddr.sin_len = sizeof(ipaddr);
1237 				ipaddr.sin_addr = sin;
1238 				if (ifa_ifwithaddr(sintosa(&ipaddr),
1239 				    m->m_pkthdr.ph_rtableid) == NULL)
1240 					continue;
1241 				ipt.ipt_ptr += sizeof(struct in_addr);
1242 				break;
1243 
1244 			default:
1245 				/* XXX can't take &ipt->ipt_flg */
1246 				code = (u_char *)&ipt.ipt_ptr -
1247 				    (u_char *)ip + 1;
1248 				goto bad;
1249 			}
1250 			ntime = iptime();
1251 			memcpy(cp + ipt.ipt_ptr - 1, &ntime, sizeof(u_int32_t));
1252 			ipt.ipt_ptr += sizeof(u_int32_t);
1253 		}
1254 	}
1255 	KERNEL_UNLOCK();
1256 	if (forward && ipforwarding) {
1257 		ip_forward(m, ifp, NULL, 1);
1258 		return (1);
1259 	}
1260 	return (0);
1261 bad:
1262 	KERNEL_UNLOCK();
1263 	icmp_error(m, type, code, 0, 0);
1264 	ipstat.ips_badoptions++;
1265 	return (1);
1266 }
1267 
1268 /*
1269  * Save incoming source route for use in replies,
1270  * to be picked up later by ip_srcroute if the receiver is interested.
1271  */
1272 void
1273 save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
1274 {
1275 	struct ip_srcrt *isr;
1276 	struct m_tag *mtag;
1277 	unsigned olen;
1278 
1279 	olen = option[IPOPT_OLEN];
1280 	if (olen > sizeof(isr->isr_hdr) + sizeof(isr->isr_routes))
1281 		return;
1282 
1283 	mtag = m_tag_get(PACKET_TAG_SRCROUTE, sizeof(*isr), M_NOWAIT);
1284 	if (mtag == NULL)
1285 		return;
1286 	isr = (struct ip_srcrt *)(mtag + 1);
1287 
1288 	memcpy(isr->isr_hdr, option, olen);
1289 	isr->isr_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1290 	isr->isr_dst = dst;
1291 	m_tag_prepend(m, mtag);
1292 }
1293 
1294 /*
1295  * Retrieve incoming source route for use in replies,
1296  * in the same form used by setsockopt.
1297  * The first hop is placed before the options, will be removed later.
1298  */
1299 struct mbuf *
1300 ip_srcroute(struct mbuf *m0)
1301 {
1302 	struct in_addr *p, *q;
1303 	struct mbuf *m;
1304 	struct ip_srcrt *isr;
1305 	struct m_tag *mtag;
1306 
1307 	if (!ip_dosourceroute)
1308 		return (NULL);
1309 
1310 	mtag = m_tag_find(m0, PACKET_TAG_SRCROUTE, NULL);
1311 	if (mtag == NULL)
1312 		return (NULL);
1313 	isr = (struct ip_srcrt *)(mtag + 1);
1314 
1315 	if (isr->isr_nhops == 0)
1316 		return (NULL);
1317 	m = m_get(M_DONTWAIT, MT_SOOPTS);
1318 	if (m == NULL)
1319 		return (NULL);
1320 
1321 #define OPTSIZ	(sizeof(isr->isr_nop) + sizeof(isr->isr_hdr))
1322 
1323 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + header) */
1324 	m->m_len = (isr->isr_nhops + 1) * sizeof(struct in_addr) + OPTSIZ;
1325 
1326 	/*
1327 	 * First save first hop for return route
1328 	 */
1329 	p = &(isr->isr_routes[isr->isr_nhops - 1]);
1330 	*(mtod(m, struct in_addr *)) = *p--;
1331 
1332 	/*
1333 	 * Copy option fields and padding (nop) to mbuf.
1334 	 */
1335 	isr->isr_nop = IPOPT_NOP;
1336 	isr->isr_hdr[IPOPT_OFFSET] = IPOPT_MINOFF;
1337 	memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), &isr->isr_nop,
1338 	    OPTSIZ);
1339 	q = (struct in_addr *)(mtod(m, caddr_t) +
1340 	    sizeof(struct in_addr) + OPTSIZ);
1341 #undef OPTSIZ
1342 	/*
1343 	 * Record return path as an IP source route,
1344 	 * reversing the path (pointers are now aligned).
1345 	 */
1346 	while (p >= isr->isr_routes) {
1347 		*q++ = *p--;
1348 	}
1349 	/*
1350 	 * Last hop goes to final destination.
1351 	 */
1352 	*q = isr->isr_dst;
1353 	m_tag_delete(m0, (struct m_tag *)isr);
1354 	return (m);
1355 }
1356 
1357 /*
1358  * Strip out IP options, at higher level protocol in the kernel.
1359  */
1360 void
1361 ip_stripoptions(struct mbuf *m)
1362 {
1363 	int i;
1364 	struct ip *ip = mtod(m, struct ip *);
1365 	caddr_t opts;
1366 	int olen;
1367 
1368 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
1369 	opts = (caddr_t)(ip + 1);
1370 	i = m->m_len - (sizeof (struct ip) + olen);
1371 	memmove(opts, opts  + olen, i);
1372 	m->m_len -= olen;
1373 	if (m->m_flags & M_PKTHDR)
1374 		m->m_pkthdr.len -= olen;
1375 	ip->ip_hl = sizeof(struct ip) >> 2;
1376 	ip->ip_len = htons(ntohs(ip->ip_len) - olen);
1377 }
1378 
1379 int inetctlerrmap[PRC_NCMDS] = {
1380 	0,		0,		0,		0,
1381 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1382 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1383 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1384 	0,		0,		0,		0,
1385 	ENOPROTOOPT
1386 };
1387 
1388 /*
1389  * Forward a packet.  If some error occurs return the sender
1390  * an icmp packet.  Note we can't always generate a meaningful
1391  * icmp message because icmp doesn't have a large enough repertoire
1392  * of codes and types.
1393  *
1394  * If not forwarding, just drop the packet.  This could be confusing
1395  * if ipforwarding was zero but some routing protocol was advancing
1396  * us as a gateway to somewhere.  However, we must let the routing
1397  * protocol deal with that.
1398  *
1399  * The srcrt parameter indicates whether the packet is being forwarded
1400  * via a source route.
1401  */
1402 void
1403 ip_forward(struct mbuf *m, struct ifnet *ifp, struct rtentry *rt, int srcrt)
1404 {
1405 	struct mbuf mfake, *mcopy = NULL;
1406 	struct ip *ip = mtod(m, struct ip *);
1407 	struct sockaddr_in *sin;
1408 	struct route ro;
1409 	int error, type = 0, code = 0, destmtu = 0, fake = 0, len;
1410 	u_int32_t dest;
1411 
1412 	dest = 0;
1413 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1414 		ipstat.ips_cantforward++;
1415 		m_freem(m);
1416 		goto freecopy;
1417 	}
1418 	if (ip->ip_ttl <= IPTTLDEC) {
1419 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1420 		goto freecopy;
1421 	}
1422 
1423 	sin = satosin(&ro.ro_dst);
1424 	memset(sin, 0, sizeof(*sin));
1425 	sin->sin_family = AF_INET;
1426 	sin->sin_len = sizeof(*sin);
1427 	sin->sin_addr = ip->ip_dst;
1428 
1429 	if (!rtisvalid(rt)) {
1430 		rtfree(rt);
1431 		rt = rtalloc_mpath(sintosa(sin), &ip->ip_src.s_addr,
1432 		    m->m_pkthdr.ph_rtableid);
1433 		if (rt == NULL) {
1434 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1435 			return;
1436 		}
1437 	}
1438 
1439 	/*
1440 	 * Save at most 68 bytes of the packet in case
1441 	 * we need to generate an ICMP message to the src.
1442 	 * The data is saved in the mbuf on the stack that
1443 	 * acts as a temporary storage not intended to be
1444 	 * passed down the IP stack or to the mfree.
1445 	 */
1446 	memset(&mfake.m_hdr, 0, sizeof(mfake.m_hdr));
1447 	mfake.m_type = m->m_type;
1448 	if (m_dup_pkthdr(&mfake, m, M_DONTWAIT) == 0) {
1449 		mfake.m_data = mfake.m_pktdat;
1450 		len = min(ntohs(ip->ip_len), 68);
1451 		m_copydata(m, 0, len, mfake.m_pktdat);
1452 		mfake.m_pkthdr.len = mfake.m_len = len;
1453 #if NPF > 0
1454 		pf_pkt_unlink_state_key(&mfake);
1455 #endif	/* NPF > 0 */
1456 		fake = 1;
1457 	}
1458 
1459 	ip->ip_ttl -= IPTTLDEC;
1460 
1461 	/*
1462 	 * If forwarding packet using same interface that it came in on,
1463 	 * perhaps should send a redirect to sender to shortcut a hop.
1464 	 * Only send redirect if source is sending directly to us,
1465 	 * and if packet was not source routed (or has any options).
1466 	 * Also, don't send redirect if forwarding using a default route
1467 	 * or a route modified by a redirect.
1468 	 * Don't send redirect if we advertise destination's arp address
1469 	 * as ours (proxy arp).
1470 	 */
1471 	if ((rt->rt_ifidx == ifp->if_index) &&
1472 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1473 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1474 	    ipsendredirects && !srcrt &&
1475 	    !arpproxy(satosin(rt_key(rt))->sin_addr, m->m_pkthdr.ph_rtableid)) {
1476 		if ((ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_netmask) ==
1477 		    ifatoia(rt->rt_ifa)->ia_net) {
1478 		    if (rt->rt_flags & RTF_GATEWAY)
1479 			dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1480 		    else
1481 			dest = ip->ip_dst.s_addr;
1482 		    /* Router requirements says to only send host redirects */
1483 		    type = ICMP_REDIRECT;
1484 		    code = ICMP_REDIRECT_HOST;
1485 		}
1486 	}
1487 
1488 	ro.ro_rt = rt;
1489 	ro.ro_tableid = m->m_pkthdr.ph_rtableid;
1490 	error = ip_output(m, NULL, &ro,
1491 	    (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
1492 	    NULL, NULL, 0);
1493 	rt = ro.ro_rt;
1494 	if (error)
1495 		ipstat.ips_cantforward++;
1496 	else {
1497 		ipstat.ips_forward++;
1498 		if (type)
1499 			ipstat.ips_redirectsent++;
1500 		else
1501 			goto freecopy;
1502 	}
1503 	if (!fake)
1504 		goto freecopy;
1505 
1506 	switch (error) {
1507 
1508 	case 0:				/* forwarded, but need redirect */
1509 		/* type, code set above */
1510 		break;
1511 
1512 	case ENETUNREACH:		/* shouldn't happen, checked above */
1513 	case EHOSTUNREACH:
1514 	case ENETDOWN:
1515 	case EHOSTDOWN:
1516 	default:
1517 		type = ICMP_UNREACH;
1518 		code = ICMP_UNREACH_HOST;
1519 		break;
1520 
1521 	case EMSGSIZE:
1522 		type = ICMP_UNREACH;
1523 		code = ICMP_UNREACH_NEEDFRAG;
1524 
1525 #ifdef IPSEC
1526 		if (rt != NULL) {
1527 			if (rt->rt_rmx.rmx_mtu)
1528 				destmtu = rt->rt_rmx.rmx_mtu;
1529 			else {
1530 				struct ifnet *destifp;
1531 
1532 				destifp = if_get(rt->rt_ifidx);
1533 				if (destifp != NULL)
1534 					destmtu = destifp->if_mtu;
1535 				if_put(destifp);
1536 			}
1537 		}
1538 #endif /*IPSEC*/
1539 		ipstat.ips_cantfrag++;
1540 		break;
1541 
1542 	case EACCES:
1543 		/*
1544 		 * pf(4) blocked the packet. There is no need to send an ICMP
1545 		 * packet back since pf(4) takes care of it.
1546 		 */
1547 		goto freecopy;
1548 	case ENOBUFS:
1549 		/*
1550 		 * a router should not generate ICMP_SOURCEQUENCH as
1551 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1552 		 * source quench could be a big problem under DoS attacks,
1553 		 * or the underlying interface is rate-limited.
1554 		 */
1555 		goto freecopy;
1556 	}
1557 
1558 	mcopy = m_copym(&mfake, 0, len, M_DONTWAIT);
1559 	if (mcopy)
1560 		icmp_error(mcopy, type, code, dest, destmtu);
1561 
1562 freecopy:
1563 	if (fake)
1564 		m_tag_delete_chain(&mfake);
1565 	rtfree(rt);
1566 }
1567 
1568 int
1569 ip_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1570     size_t newlen)
1571 {
1572 	int s, error;
1573 #ifdef MROUTING
1574 	extern int ip_mrtproto;
1575 	extern struct mrtstat mrtstat;
1576 #endif
1577 
1578 	/* Almost all sysctl names at this level are terminal. */
1579 	if (namelen != 1 && name[0] != IPCTL_IFQUEUE)
1580 		return (ENOTDIR);
1581 
1582 	switch (name[0]) {
1583 #ifdef notyet
1584 	case IPCTL_DEFMTU:
1585 		return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1586 #endif
1587 	case IPCTL_SOURCEROUTE:
1588 		/*
1589 		 * Don't allow this to change in a secure environment.
1590 		 */
1591 		if (newp && securelevel > 0)
1592 			return (EPERM);
1593 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1594 		    &ip_dosourceroute));
1595 	case IPCTL_MTUDISC:
1596 		error = sysctl_int(oldp, oldlenp, newp, newlen,
1597 		    &ip_mtudisc);
1598 		if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1599 			ip_mtudisc_timeout_q =
1600 			    rt_timer_queue_create(ip_mtudisc_timeout);
1601 		} else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1602 			s = splsoftnet();
1603 			rt_timer_queue_destroy(ip_mtudisc_timeout_q);
1604 			ip_mtudisc_timeout_q = NULL;
1605 			splx(s);
1606 		}
1607 		return error;
1608 	case IPCTL_MTUDISCTIMEOUT:
1609 		error = sysctl_int(oldp, oldlenp, newp, newlen,
1610 		   &ip_mtudisc_timeout);
1611 		if (ip_mtudisc_timeout_q != NULL) {
1612 			s = splsoftnet();
1613 			rt_timer_queue_change(ip_mtudisc_timeout_q,
1614 					      ip_mtudisc_timeout);
1615 			splx(s);
1616 		}
1617 		return (error);
1618 	case IPCTL_IPSEC_ENC_ALGORITHM:
1619 	        return (sysctl_tstring(oldp, oldlenp, newp, newlen,
1620 				       ipsec_def_enc, sizeof(ipsec_def_enc)));
1621 	case IPCTL_IPSEC_AUTH_ALGORITHM:
1622 	        return (sysctl_tstring(oldp, oldlenp, newp, newlen,
1623 				       ipsec_def_auth,
1624 				       sizeof(ipsec_def_auth)));
1625 	case IPCTL_IPSEC_IPCOMP_ALGORITHM:
1626 	        return (sysctl_tstring(oldp, oldlenp, newp, newlen,
1627 				       ipsec_def_comp,
1628 				       sizeof(ipsec_def_comp)));
1629 	case IPCTL_IFQUEUE:
1630 	        return (sysctl_niq(name + 1, namelen - 1,
1631 		    oldp, oldlenp, newp, newlen, &ipintrq));
1632 	case IPCTL_STATS:
1633 		return (sysctl_rdstruct(oldp, oldlenp, newp,
1634 		    &ipstat, sizeof(ipstat)));
1635 #ifdef MROUTING
1636 	case IPCTL_MRTSTATS:
1637 		return (sysctl_rdstruct(oldp, oldlenp, newp,
1638 		    &mrtstat, sizeof(mrtstat)));
1639 	case IPCTL_MRTPROTO:
1640 		return (sysctl_rdint(oldp, oldlenp, newp, ip_mrtproto));
1641 	case IPCTL_MRTMFC:
1642 		if (newp)
1643 			return (EPERM);
1644 		return mrt_sysctl_mfc(oldp, oldlenp);
1645 	case IPCTL_MRTVIF:
1646 		if (newp)
1647 			return (EPERM);
1648 		return mrt_sysctl_vif(oldp, oldlenp);
1649 #else
1650 	case IPCTL_MRTPROTO:
1651 	case IPCTL_MRTSTATS:
1652 	case IPCTL_MRTMFC:
1653 	case IPCTL_MRTVIF:
1654 		return (EOPNOTSUPP);
1655 #endif
1656 	default:
1657 		if (name[0] < IPCTL_MAXID)
1658 			return (sysctl_int_arr(ipctl_vars, name, namelen,
1659 			    oldp, oldlenp, newp, newlen));
1660 		return (EOPNOTSUPP);
1661 	}
1662 	/* NOTREACHED */
1663 }
1664 
1665 void
1666 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1667     struct mbuf *m)
1668 {
1669 #ifdef SO_TIMESTAMP
1670 	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1671 		struct timeval tv;
1672 
1673 		microtime(&tv);
1674 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1675 		    SCM_TIMESTAMP, SOL_SOCKET);
1676 		if (*mp)
1677 			mp = &(*mp)->m_next;
1678 	}
1679 #endif
1680 	if (inp->inp_flags & INP_RECVDSTADDR) {
1681 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1682 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1683 		if (*mp)
1684 			mp = &(*mp)->m_next;
1685 	}
1686 #ifdef notyet
1687 	/* this code is broken and will probably never be fixed. */
1688 	/* options were tossed already */
1689 	if (inp->inp_flags & INP_RECVOPTS) {
1690 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1691 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1692 		if (*mp)
1693 			mp = &(*mp)->m_next;
1694 	}
1695 	/* ip_srcroute doesn't do what we want here, need to fix */
1696 	if (inp->inp_flags & INP_RECVRETOPTS) {
1697 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
1698 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1699 		if (*mp)
1700 			mp = &(*mp)->m_next;
1701 	}
1702 #endif
1703 	if (inp->inp_flags & INP_RECVIF) {
1704 		struct sockaddr_dl sdl;
1705 		struct ifnet *ifp;
1706 
1707 		ifp = if_get(m->m_pkthdr.ph_ifidx);
1708 		if (ifp == NULL || ifp->if_sadl == NULL) {
1709 			memset(&sdl, 0, sizeof(sdl));
1710 			sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1711 			sdl.sdl_family = AF_LINK;
1712 			sdl.sdl_index = ifp != NULL ? ifp->if_index : 0;
1713 			sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1714 			*mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1715 			    IP_RECVIF, IPPROTO_IP);
1716 		} else {
1717 			*mp = sbcreatecontrol((caddr_t) ifp->if_sadl,
1718 			    ifp->if_sadl->sdl_len, IP_RECVIF, IPPROTO_IP);
1719 		}
1720 		if (*mp)
1721 			mp = &(*mp)->m_next;
1722 		if_put(ifp);
1723 	}
1724 	if (inp->inp_flags & INP_RECVTTL) {
1725 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
1726 		    sizeof(u_int8_t), IP_RECVTTL, IPPROTO_IP);
1727 		if (*mp)
1728 			mp = &(*mp)->m_next;
1729 	}
1730 	if (inp->inp_flags & INP_RECVRTABLE) {
1731 		u_int rtableid = inp->inp_rtableid;
1732 #if NPF > 0
1733 		struct pf_divert *divert;
1734 
1735 		if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED &&
1736 		    (divert = pf_find_divert(m)) != NULL)
1737 			rtableid = divert->rdomain;
1738 #endif
1739 
1740 		*mp = sbcreatecontrol((caddr_t) &rtableid,
1741 		    sizeof(u_int), IP_RECVRTABLE, IPPROTO_IP);
1742 		if (*mp)
1743 			mp = &(*mp)->m_next;
1744 	}
1745 }
1746 
1747 void
1748 ip_send_dispatch(void *xmq)
1749 {
1750 	struct mbuf_queue *mq = xmq;
1751 	struct mbuf *m;
1752 	struct mbuf_list ml;
1753 	int s;
1754 
1755 	mq_delist(mq, &ml);
1756 	KERNEL_LOCK();
1757 	s = splsoftnet();
1758 	while ((m = ml_dequeue(&ml)) != NULL) {
1759 		ip_output(m, NULL, NULL, 0, NULL, NULL, 0);
1760 	}
1761 	splx(s);
1762 	KERNEL_UNLOCK();
1763 }
1764 
1765 void
1766 ip_send(struct mbuf *m)
1767 {
1768 	mq_enqueue(&ipsend_mq, m);
1769 	task_add(softnettq, &ipsend_task);
1770 }
1771