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