xref: /openbsd/sys/netinet/ip_input.c (revision fc61954a)
1 /*	$OpenBSD: ip_input.c,v 1.282 2016/09/22 10:12:25 jsg 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(
1122 				    sintosa(&ipaddr),
1123 				    m->m_pkthdr.ph_rtableid))) == NULL)
1124 					ia = ifatoia(ifa_ifwithnet(
1125 					    sintosa(&ipaddr),
1126 					    m->m_pkthdr.ph_rtableid));
1127 				if (ia == NULL) {
1128 					type = ICMP_UNREACH;
1129 					code = ICMP_UNREACH_SRCFAIL;
1130 					goto bad;
1131 				}
1132 				memcpy(cp + off, &ia->ia_addr.sin_addr,
1133 				    sizeof(struct in_addr));
1134 				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1135 			} else {
1136 				/* keep packet in the virtual instance */
1137 				rt = rtalloc(sintosa(&ipaddr), RT_RESOLVE,
1138 				    rtableid);
1139 				if (!rtisvalid(rt)) {
1140 					type = ICMP_UNREACH;
1141 					code = ICMP_UNREACH_SRCFAIL;
1142 					rtfree(rt);
1143 					goto bad;
1144 				}
1145 				ia = ifatoia(rt->rt_ifa);
1146 				memcpy(cp + off, &ia->ia_addr.sin_addr,
1147 				    sizeof(struct in_addr));
1148 				rtfree(rt);
1149 				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1150 			}
1151 			ip->ip_dst = ipaddr.sin_addr;
1152 			/*
1153 			 * Let ip_intr's mcast routing check handle mcast pkts
1154 			 */
1155 			forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1156 			break;
1157 
1158 		case IPOPT_RR:
1159 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1160 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
1161 				goto bad;
1162 			}
1163 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1164 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1165 				goto bad;
1166 			}
1167 
1168 			/*
1169 			 * If no space remains, ignore.
1170 			 */
1171 			off--;			/* 0 origin */
1172 			if ((off + sizeof(struct in_addr)) > optlen)
1173 				break;
1174 			memset(&ipaddr, 0, sizeof(ipaddr));
1175 			ipaddr.sin_family = AF_INET;
1176 			ipaddr.sin_len = sizeof(ipaddr);
1177 			ipaddr.sin_addr = ip->ip_dst;
1178 			/*
1179 			 * locate outgoing interface; if we're the destination,
1180 			 * use the incoming interface (should be same).
1181 			 * Again keep the packet inside the virtual instance.
1182 			 */
1183 			rt = rtalloc(sintosa(&ipaddr), RT_RESOLVE, rtableid);
1184 			if (!rtisvalid(rt)) {
1185 				type = ICMP_UNREACH;
1186 				code = ICMP_UNREACH_HOST;
1187 				rtfree(rt);
1188 				goto bad;
1189 			}
1190 			ia = ifatoia(rt->rt_ifa);
1191 			memcpy(cp + off, &ia->ia_addr.sin_addr,
1192 			    sizeof(struct in_addr));
1193 			rtfree(rt);
1194 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1195 			break;
1196 
1197 		case IPOPT_TS:
1198 			code = cp - (u_char *)ip;
1199 			if (optlen < sizeof(struct ip_timestamp))
1200 				goto bad;
1201 			memcpy(&ipt, cp, sizeof(struct ip_timestamp));
1202 			if (ipt.ipt_ptr < 5 || ipt.ipt_len < 5)
1203 				goto bad;
1204 			if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) > ipt.ipt_len) {
1205 				if (++ipt.ipt_oflw == 0)
1206 					goto bad;
1207 				break;
1208 			}
1209 			memcpy(&sin, cp + ipt.ipt_ptr - 1, sizeof sin);
1210 			switch (ipt.ipt_flg) {
1211 
1212 			case IPOPT_TS_TSONLY:
1213 				break;
1214 
1215 			case IPOPT_TS_TSANDADDR:
1216 				if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) +
1217 				    sizeof(struct in_addr) > ipt.ipt_len)
1218 					goto bad;
1219 				memset(&ipaddr, 0, sizeof(ipaddr));
1220 				ipaddr.sin_family = AF_INET;
1221 				ipaddr.sin_len = sizeof(ipaddr);
1222 				ipaddr.sin_addr = dst;
1223 				ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1224 				    ifp));
1225 				if (ia == NULL)
1226 					continue;
1227 				memcpy(&sin, &ia->ia_addr.sin_addr,
1228 				    sizeof(struct in_addr));
1229 				ipt.ipt_ptr += sizeof(struct in_addr);
1230 				break;
1231 
1232 			case IPOPT_TS_PRESPEC:
1233 				if (ipt.ipt_ptr - 1 + sizeof(u_int32_t) +
1234 				    sizeof(struct in_addr) > ipt.ipt_len)
1235 					goto bad;
1236 				memset(&ipaddr, 0, sizeof(ipaddr));
1237 				ipaddr.sin_family = AF_INET;
1238 				ipaddr.sin_len = sizeof(ipaddr);
1239 				ipaddr.sin_addr = sin;
1240 				if (ifa_ifwithaddr(sintosa(&ipaddr),
1241 				    m->m_pkthdr.ph_rtableid) == NULL)
1242 					continue;
1243 				ipt.ipt_ptr += sizeof(struct in_addr);
1244 				break;
1245 
1246 			default:
1247 				/* XXX can't take &ipt->ipt_flg */
1248 				code = (u_char *)&ipt.ipt_ptr -
1249 				    (u_char *)ip + 1;
1250 				goto bad;
1251 			}
1252 			ntime = iptime();
1253 			memcpy(cp + ipt.ipt_ptr - 1, &ntime, sizeof(u_int32_t));
1254 			ipt.ipt_ptr += sizeof(u_int32_t);
1255 		}
1256 	}
1257 	KERNEL_UNLOCK();
1258 	if (forward && ipforwarding) {
1259 		ip_forward(m, ifp, NULL, 1);
1260 		return (1);
1261 	}
1262 	return (0);
1263 bad:
1264 	KERNEL_UNLOCK();
1265 	icmp_error(m, type, code, 0, 0);
1266 	ipstat.ips_badoptions++;
1267 	return (1);
1268 }
1269 
1270 /*
1271  * Save incoming source route for use in replies,
1272  * to be picked up later by ip_srcroute if the receiver is interested.
1273  */
1274 void
1275 save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
1276 {
1277 	struct ip_srcrt *isr;
1278 	struct m_tag *mtag;
1279 	unsigned olen;
1280 
1281 	olen = option[IPOPT_OLEN];
1282 	if (olen > sizeof(isr->isr_hdr) + sizeof(isr->isr_routes))
1283 		return;
1284 
1285 	mtag = m_tag_get(PACKET_TAG_SRCROUTE, sizeof(*isr), M_NOWAIT);
1286 	if (mtag == NULL)
1287 		return;
1288 	isr = (struct ip_srcrt *)(mtag + 1);
1289 
1290 	memcpy(isr->isr_hdr, option, olen);
1291 	isr->isr_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1292 	isr->isr_dst = dst;
1293 	m_tag_prepend(m, mtag);
1294 }
1295 
1296 /*
1297  * Retrieve incoming source route for use in replies,
1298  * in the same form used by setsockopt.
1299  * The first hop is placed before the options, will be removed later.
1300  */
1301 struct mbuf *
1302 ip_srcroute(struct mbuf *m0)
1303 {
1304 	struct in_addr *p, *q;
1305 	struct mbuf *m;
1306 	struct ip_srcrt *isr;
1307 	struct m_tag *mtag;
1308 
1309 	if (!ip_dosourceroute)
1310 		return (NULL);
1311 
1312 	mtag = m_tag_find(m0, PACKET_TAG_SRCROUTE, NULL);
1313 	if (mtag == NULL)
1314 		return (NULL);
1315 	isr = (struct ip_srcrt *)(mtag + 1);
1316 
1317 	if (isr->isr_nhops == 0)
1318 		return (NULL);
1319 	m = m_get(M_DONTWAIT, MT_SOOPTS);
1320 	if (m == NULL)
1321 		return (NULL);
1322 
1323 #define OPTSIZ	(sizeof(isr->isr_nop) + sizeof(isr->isr_hdr))
1324 
1325 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + header) */
1326 	m->m_len = (isr->isr_nhops + 1) * sizeof(struct in_addr) + OPTSIZ;
1327 
1328 	/*
1329 	 * First save first hop for return route
1330 	 */
1331 	p = &(isr->isr_routes[isr->isr_nhops - 1]);
1332 	*(mtod(m, struct in_addr *)) = *p--;
1333 
1334 	/*
1335 	 * Copy option fields and padding (nop) to mbuf.
1336 	 */
1337 	isr->isr_nop = IPOPT_NOP;
1338 	isr->isr_hdr[IPOPT_OFFSET] = IPOPT_MINOFF;
1339 	memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), &isr->isr_nop,
1340 	    OPTSIZ);
1341 	q = (struct in_addr *)(mtod(m, caddr_t) +
1342 	    sizeof(struct in_addr) + OPTSIZ);
1343 #undef OPTSIZ
1344 	/*
1345 	 * Record return path as an IP source route,
1346 	 * reversing the path (pointers are now aligned).
1347 	 */
1348 	while (p >= isr->isr_routes) {
1349 		*q++ = *p--;
1350 	}
1351 	/*
1352 	 * Last hop goes to final destination.
1353 	 */
1354 	*q = isr->isr_dst;
1355 	m_tag_delete(m0, (struct m_tag *)isr);
1356 	return (m);
1357 }
1358 
1359 /*
1360  * Strip out IP options, at higher level protocol in the kernel.
1361  */
1362 void
1363 ip_stripoptions(struct mbuf *m)
1364 {
1365 	int i;
1366 	struct ip *ip = mtod(m, struct ip *);
1367 	caddr_t opts;
1368 	int olen;
1369 
1370 	olen = (ip->ip_hl<<2) - sizeof (struct ip);
1371 	opts = (caddr_t)(ip + 1);
1372 	i = m->m_len - (sizeof (struct ip) + olen);
1373 	memmove(opts, opts  + olen, i);
1374 	m->m_len -= olen;
1375 	if (m->m_flags & M_PKTHDR)
1376 		m->m_pkthdr.len -= olen;
1377 	ip->ip_hl = sizeof(struct ip) >> 2;
1378 	ip->ip_len = htons(ntohs(ip->ip_len) - olen);
1379 }
1380 
1381 int inetctlerrmap[PRC_NCMDS] = {
1382 	0,		0,		0,		0,
1383 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
1384 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
1385 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
1386 	0,		0,		0,		0,
1387 	ENOPROTOOPT
1388 };
1389 
1390 /*
1391  * Forward a packet.  If some error occurs return the sender
1392  * an icmp packet.  Note we can't always generate a meaningful
1393  * icmp message because icmp doesn't have a large enough repertoire
1394  * of codes and types.
1395  *
1396  * If not forwarding, just drop the packet.  This could be confusing
1397  * if ipforwarding was zero but some routing protocol was advancing
1398  * us as a gateway to somewhere.  However, we must let the routing
1399  * protocol deal with that.
1400  *
1401  * The srcrt parameter indicates whether the packet is being forwarded
1402  * via a source route.
1403  */
1404 void
1405 ip_forward(struct mbuf *m, struct ifnet *ifp, struct rtentry *rt, int srcrt)
1406 {
1407 	struct mbuf mfake, *mcopy = NULL;
1408 	struct ip *ip = mtod(m, struct ip *);
1409 	struct sockaddr_in *sin;
1410 	struct route ro;
1411 	int error, type = 0, code = 0, destmtu = 0, fake = 0, len;
1412 	u_int32_t dest;
1413 
1414 	dest = 0;
1415 	if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1416 		ipstat.ips_cantforward++;
1417 		m_freem(m);
1418 		goto freecopy;
1419 	}
1420 	if (ip->ip_ttl <= IPTTLDEC) {
1421 		icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1422 		goto freecopy;
1423 	}
1424 
1425 	sin = satosin(&ro.ro_dst);
1426 	memset(sin, 0, sizeof(*sin));
1427 	sin->sin_family = AF_INET;
1428 	sin->sin_len = sizeof(*sin);
1429 	sin->sin_addr = ip->ip_dst;
1430 
1431 	if (!rtisvalid(rt)) {
1432 		rtfree(rt);
1433 		rt = rtalloc_mpath(sintosa(sin), &ip->ip_src.s_addr,
1434 		    m->m_pkthdr.ph_rtableid);
1435 		if (rt == NULL) {
1436 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1437 			return;
1438 		}
1439 	}
1440 
1441 	/*
1442 	 * Save at most 68 bytes of the packet in case
1443 	 * we need to generate an ICMP message to the src.
1444 	 * The data is saved in the mbuf on the stack that
1445 	 * acts as a temporary storage not intended to be
1446 	 * passed down the IP stack or to the mfree.
1447 	 */
1448 	memset(&mfake.m_hdr, 0, sizeof(mfake.m_hdr));
1449 	mfake.m_type = m->m_type;
1450 	if (m_dup_pkthdr(&mfake, m, M_DONTWAIT) == 0) {
1451 		mfake.m_data = mfake.m_pktdat;
1452 		len = min(ntohs(ip->ip_len), 68);
1453 		m_copydata(m, 0, len, mfake.m_pktdat);
1454 		mfake.m_pkthdr.len = mfake.m_len = len;
1455 #if NPF > 0
1456 		pf_pkt_unlink_state_key(&mfake);
1457 #endif	/* NPF > 0 */
1458 		fake = 1;
1459 	}
1460 
1461 	ip->ip_ttl -= IPTTLDEC;
1462 
1463 	/*
1464 	 * If forwarding packet using same interface that it came in on,
1465 	 * perhaps should send a redirect to sender to shortcut a hop.
1466 	 * Only send redirect if source is sending directly to us,
1467 	 * and if packet was not source routed (or has any options).
1468 	 * Also, don't send redirect if forwarding using a default route
1469 	 * or a route modified by a redirect.
1470 	 * Don't send redirect if we advertise destination's arp address
1471 	 * as ours (proxy arp).
1472 	 */
1473 	if ((rt->rt_ifidx == ifp->if_index) &&
1474 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1475 	    satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1476 	    ipsendredirects && !srcrt &&
1477 	    !arpproxy(satosin(rt_key(rt))->sin_addr, m->m_pkthdr.ph_rtableid)) {
1478 		if ((ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_netmask) ==
1479 		    ifatoia(rt->rt_ifa)->ia_net) {
1480 		    if (rt->rt_flags & RTF_GATEWAY)
1481 			dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1482 		    else
1483 			dest = ip->ip_dst.s_addr;
1484 		    /* Router requirements says to only send host redirects */
1485 		    type = ICMP_REDIRECT;
1486 		    code = ICMP_REDIRECT_HOST;
1487 		}
1488 	}
1489 
1490 	ro.ro_rt = rt;
1491 	ro.ro_tableid = m->m_pkthdr.ph_rtableid;
1492 	error = ip_output(m, NULL, &ro,
1493 	    (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)),
1494 	    NULL, NULL, 0);
1495 	rt = ro.ro_rt;
1496 	if (error)
1497 		ipstat.ips_cantforward++;
1498 	else {
1499 		ipstat.ips_forward++;
1500 		if (type)
1501 			ipstat.ips_redirectsent++;
1502 		else
1503 			goto freecopy;
1504 	}
1505 	if (!fake)
1506 		goto freecopy;
1507 
1508 	switch (error) {
1509 
1510 	case 0:				/* forwarded, but need redirect */
1511 		/* type, code set above */
1512 		break;
1513 
1514 	case ENETUNREACH:		/* shouldn't happen, checked above */
1515 	case EHOSTUNREACH:
1516 	case ENETDOWN:
1517 	case EHOSTDOWN:
1518 	default:
1519 		type = ICMP_UNREACH;
1520 		code = ICMP_UNREACH_HOST;
1521 		break;
1522 
1523 	case EMSGSIZE:
1524 		type = ICMP_UNREACH;
1525 		code = ICMP_UNREACH_NEEDFRAG;
1526 
1527 #ifdef IPSEC
1528 		if (rt != NULL) {
1529 			if (rt->rt_rmx.rmx_mtu)
1530 				destmtu = rt->rt_rmx.rmx_mtu;
1531 			else {
1532 				struct ifnet *destifp;
1533 
1534 				destifp = if_get(rt->rt_ifidx);
1535 				if (destifp != NULL)
1536 					destmtu = destifp->if_mtu;
1537 				if_put(destifp);
1538 			}
1539 		}
1540 #endif /*IPSEC*/
1541 		ipstat.ips_cantfrag++;
1542 		break;
1543 
1544 	case EACCES:
1545 		/*
1546 		 * pf(4) blocked the packet. There is no need to send an ICMP
1547 		 * packet back since pf(4) takes care of it.
1548 		 */
1549 		goto freecopy;
1550 	case ENOBUFS:
1551 		/*
1552 		 * a router should not generate ICMP_SOURCEQUENCH as
1553 		 * required in RFC1812 Requirements for IP Version 4 Routers.
1554 		 * source quench could be a big problem under DoS attacks,
1555 		 * or the underlying interface is rate-limited.
1556 		 */
1557 		goto freecopy;
1558 	}
1559 
1560 	mcopy = m_copym(&mfake, 0, len, M_DONTWAIT);
1561 	if (mcopy)
1562 		icmp_error(mcopy, type, code, dest, destmtu);
1563 
1564 freecopy:
1565 	if (fake)
1566 		m_tag_delete_chain(&mfake);
1567 	rtfree(rt);
1568 }
1569 
1570 int
1571 ip_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1572     size_t newlen)
1573 {
1574 	int s, error;
1575 #ifdef MROUTING
1576 	extern int ip_mrtproto;
1577 	extern struct mrtstat mrtstat;
1578 #endif
1579 
1580 	/* Almost all sysctl names at this level are terminal. */
1581 	if (namelen != 1 && name[0] != IPCTL_IFQUEUE)
1582 		return (ENOTDIR);
1583 
1584 	switch (name[0]) {
1585 #ifdef notyet
1586 	case IPCTL_DEFMTU:
1587 		return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1588 #endif
1589 	case IPCTL_SOURCEROUTE:
1590 		/*
1591 		 * Don't allow this to change in a secure environment.
1592 		 */
1593 		if (newp && securelevel > 0)
1594 			return (EPERM);
1595 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1596 		    &ip_dosourceroute));
1597 	case IPCTL_MTUDISC:
1598 		error = sysctl_int(oldp, oldlenp, newp, newlen,
1599 		    &ip_mtudisc);
1600 		if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1601 			ip_mtudisc_timeout_q =
1602 			    rt_timer_queue_create(ip_mtudisc_timeout);
1603 		} else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1604 			s = splsoftnet();
1605 			rt_timer_queue_destroy(ip_mtudisc_timeout_q);
1606 			ip_mtudisc_timeout_q = NULL;
1607 			splx(s);
1608 		}
1609 		return error;
1610 	case IPCTL_MTUDISCTIMEOUT:
1611 		error = sysctl_int(oldp, oldlenp, newp, newlen,
1612 		   &ip_mtudisc_timeout);
1613 		if (ip_mtudisc_timeout_q != NULL) {
1614 			s = splsoftnet();
1615 			rt_timer_queue_change(ip_mtudisc_timeout_q,
1616 					      ip_mtudisc_timeout);
1617 			splx(s);
1618 		}
1619 		return (error);
1620 	case IPCTL_IPSEC_ENC_ALGORITHM:
1621 	        return (sysctl_tstring(oldp, oldlenp, newp, newlen,
1622 				       ipsec_def_enc, sizeof(ipsec_def_enc)));
1623 	case IPCTL_IPSEC_AUTH_ALGORITHM:
1624 	        return (sysctl_tstring(oldp, oldlenp, newp, newlen,
1625 				       ipsec_def_auth,
1626 				       sizeof(ipsec_def_auth)));
1627 	case IPCTL_IPSEC_IPCOMP_ALGORITHM:
1628 	        return (sysctl_tstring(oldp, oldlenp, newp, newlen,
1629 				       ipsec_def_comp,
1630 				       sizeof(ipsec_def_comp)));
1631 	case IPCTL_IFQUEUE:
1632 	        return (sysctl_niq(name + 1, namelen - 1,
1633 		    oldp, oldlenp, newp, newlen, &ipintrq));
1634 	case IPCTL_STATS:
1635 		return (sysctl_rdstruct(oldp, oldlenp, newp,
1636 		    &ipstat, sizeof(ipstat)));
1637 #ifdef MROUTING
1638 	case IPCTL_MRTSTATS:
1639 		return (sysctl_rdstruct(oldp, oldlenp, newp,
1640 		    &mrtstat, sizeof(mrtstat)));
1641 	case IPCTL_MRTPROTO:
1642 		return (sysctl_rdint(oldp, oldlenp, newp, ip_mrtproto));
1643 	case IPCTL_MRTMFC:
1644 		if (newp)
1645 			return (EPERM);
1646 		return mrt_sysctl_mfc(oldp, oldlenp);
1647 	case IPCTL_MRTVIF:
1648 		if (newp)
1649 			return (EPERM);
1650 		return mrt_sysctl_vif(oldp, oldlenp);
1651 #else
1652 	case IPCTL_MRTPROTO:
1653 	case IPCTL_MRTSTATS:
1654 	case IPCTL_MRTMFC:
1655 	case IPCTL_MRTVIF:
1656 		return (EOPNOTSUPP);
1657 #endif
1658 	default:
1659 		if (name[0] < IPCTL_MAXID)
1660 			return (sysctl_int_arr(ipctl_vars, name, namelen,
1661 			    oldp, oldlenp, newp, newlen));
1662 		return (EOPNOTSUPP);
1663 	}
1664 	/* NOTREACHED */
1665 }
1666 
1667 void
1668 ip_savecontrol(struct inpcb *inp, struct mbuf **mp, struct ip *ip,
1669     struct mbuf *m)
1670 {
1671 #ifdef SO_TIMESTAMP
1672 	if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1673 		struct timeval tv;
1674 
1675 		microtime(&tv);
1676 		*mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1677 		    SCM_TIMESTAMP, SOL_SOCKET);
1678 		if (*mp)
1679 			mp = &(*mp)->m_next;
1680 	}
1681 #endif
1682 	if (inp->inp_flags & INP_RECVDSTADDR) {
1683 		*mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1684 		    sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1685 		if (*mp)
1686 			mp = &(*mp)->m_next;
1687 	}
1688 #ifdef notyet
1689 	/* this code is broken and will probably never be fixed. */
1690 	/* options were tossed already */
1691 	if (inp->inp_flags & INP_RECVOPTS) {
1692 		*mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1693 		    sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1694 		if (*mp)
1695 			mp = &(*mp)->m_next;
1696 	}
1697 	/* ip_srcroute doesn't do what we want here, need to fix */
1698 	if (inp->inp_flags & INP_RECVRETOPTS) {
1699 		*mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
1700 		    sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1701 		if (*mp)
1702 			mp = &(*mp)->m_next;
1703 	}
1704 #endif
1705 	if (inp->inp_flags & INP_RECVIF) {
1706 		struct sockaddr_dl sdl;
1707 		struct ifnet *ifp;
1708 
1709 		ifp = if_get(m->m_pkthdr.ph_ifidx);
1710 		if (ifp == NULL || ifp->if_sadl == NULL) {
1711 			memset(&sdl, 0, sizeof(sdl));
1712 			sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1713 			sdl.sdl_family = AF_LINK;
1714 			sdl.sdl_index = ifp != NULL ? ifp->if_index : 0;
1715 			sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1716 			*mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1717 			    IP_RECVIF, IPPROTO_IP);
1718 		} else {
1719 			*mp = sbcreatecontrol((caddr_t) ifp->if_sadl,
1720 			    ifp->if_sadl->sdl_len, IP_RECVIF, IPPROTO_IP);
1721 		}
1722 		if (*mp)
1723 			mp = &(*mp)->m_next;
1724 		if_put(ifp);
1725 	}
1726 	if (inp->inp_flags & INP_RECVTTL) {
1727 		*mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
1728 		    sizeof(u_int8_t), IP_RECVTTL, IPPROTO_IP);
1729 		if (*mp)
1730 			mp = &(*mp)->m_next;
1731 	}
1732 	if (inp->inp_flags & INP_RECVRTABLE) {
1733 		u_int rtableid = inp->inp_rtableid;
1734 #if NPF > 0
1735 		struct pf_divert *divert;
1736 
1737 		if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED &&
1738 		    (divert = pf_find_divert(m)) != NULL)
1739 			rtableid = divert->rdomain;
1740 #endif
1741 
1742 		*mp = sbcreatecontrol((caddr_t) &rtableid,
1743 		    sizeof(u_int), IP_RECVRTABLE, IPPROTO_IP);
1744 		if (*mp)
1745 			mp = &(*mp)->m_next;
1746 	}
1747 }
1748 
1749 void
1750 ip_send_dispatch(void *xmq)
1751 {
1752 	struct mbuf_queue *mq = xmq;
1753 	struct mbuf *m;
1754 	struct mbuf_list ml;
1755 	int s;
1756 
1757 	mq_delist(mq, &ml);
1758 	KERNEL_LOCK();
1759 	s = splsoftnet();
1760 	while ((m = ml_dequeue(&ml)) != NULL) {
1761 		ip_output(m, NULL, NULL, 0, NULL, NULL, 0);
1762 	}
1763 	splx(s);
1764 	KERNEL_UNLOCK();
1765 }
1766 
1767 void
1768 ip_send(struct mbuf *m)
1769 {
1770 	mq_enqueue(&ipsend_mq, m);
1771 	task_add(softnettq, &ipsend_task);
1772 }
1773