xref: /openbsd-src/sys/netinet/ip_output.c (revision 824adb5411e4389b29bae28eba5c2c2bbd147f34)
1 /*	$OpenBSD: ip_output.c,v 1.374 2021/07/27 17:13:03 mvs Exp $	*/
2 /*	$NetBSD: ip_output.c,v 1.28 1996/02/13 23:43:07 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1990, 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_output.c	8.3 (Berkeley) 1/21/94
33  */
34 
35 #include "pf.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/proc.h>
44 #include <sys/kernel.h>
45 
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_enc.h>
49 #include <net/route.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/ip.h>
53 #include <netinet/in_pcb.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/ip_icmp.h>
57 #include <netinet/tcp.h>
58 #include <netinet/udp.h>
59 #include <netinet/tcp_timer.h>
60 #include <netinet/tcp_var.h>
61 #include <netinet/udp_var.h>
62 
63 #if NPF > 0
64 #include <net/pfvar.h>
65 #endif
66 
67 #ifdef IPSEC
68 #ifdef ENCDEBUG
69 #define DPRINTF(fmt, args...)						\
70 	do {								\
71 		if (encdebug)						\
72 			printf("%s: " fmt "\n", __func__, ## args);	\
73 	} while (0)
74 #else
75 #define DPRINTF(fmt, args...)						\
76 	do { } while (0)
77 #endif
78 #endif /* IPSEC */
79 
80 int ip_pcbopts(struct mbuf **, struct mbuf *);
81 int ip_multicast_if(struct ip_mreqn *, u_int, unsigned int *);
82 int ip_setmoptions(int, struct ip_moptions **, struct mbuf *, u_int);
83 void ip_mloopback(struct ifnet *, struct mbuf *, struct sockaddr_in *);
84 static __inline u_int16_t __attribute__((__unused__))
85     in_cksum_phdr(u_int32_t, u_int32_t, u_int32_t);
86 void in_delayed_cksum(struct mbuf *);
87 int in_ifcap_cksum(struct mbuf *, struct ifnet *, int);
88 
89 #ifdef IPSEC
90 struct tdb *
91 ip_output_ipsec_lookup(struct mbuf *m, int hlen, int *error, struct inpcb *inp,
92     int ipsecflowinfo);
93 int
94 ip_output_ipsec_send(struct tdb *, struct mbuf *, struct route *, int);
95 #endif /* IPSEC */
96 
97 /*
98  * IP output.  The packet in mbuf chain m contains a skeletal IP
99  * header (with len, off, ttl, proto, tos, src, dst).
100  * The mbuf chain containing the packet will be freed.
101  * The mbuf opt, if present, will not be freed.
102  */
103 int
104 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
105     struct ip_moptions *imo, struct inpcb *inp, u_int32_t ipsecflowinfo)
106 {
107 	struct ip *ip;
108 	struct ifnet *ifp = NULL;
109 	struct mbuf_list fml;
110 	int hlen = sizeof (struct ip);
111 	int error = 0;
112 	struct route iproute;
113 	struct sockaddr_in *dst;
114 	struct tdb *tdb = NULL;
115 	u_long mtu;
116 #if NPF > 0
117 	u_int orig_rtableid;
118 #endif
119 
120 	NET_ASSERT_LOCKED();
121 
122 #ifdef IPSEC
123 	if (inp && (inp->inp_flags & INP_IPV6) != 0)
124 		panic("ip_output: IPv6 pcb is passed");
125 #endif /* IPSEC */
126 
127 #ifdef	DIAGNOSTIC
128 	if ((m->m_flags & M_PKTHDR) == 0)
129 		panic("ip_output no HDR");
130 #endif
131 	if (opt)
132 		m = ip_insertoptions(m, opt, &hlen);
133 
134 	ip = mtod(m, struct ip *);
135 
136 	/*
137 	 * Fill in IP header.
138 	 */
139 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
140 		ip->ip_v = IPVERSION;
141 		ip->ip_off &= htons(IP_DF);
142 		ip->ip_id = htons(ip_randomid());
143 		ip->ip_hl = hlen >> 2;
144 		ipstat_inc(ips_localout);
145 	} else {
146 		hlen = ip->ip_hl << 2;
147 	}
148 
149 	/*
150 	 * We should not send traffic to 0/8 say both Stevens and RFCs
151 	 * 5735 section 3 and 1122 sections 3.2.1.3 and 3.3.6.
152 	 */
153 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == 0) {
154 		error = ENETUNREACH;
155 		goto bad;
156 	}
157 
158 #if NPF > 0
159 	orig_rtableid = m->m_pkthdr.ph_rtableid;
160 reroute:
161 #endif
162 
163 	/*
164 	 * Do a route lookup now in case we need the source address to
165 	 * do an SPD lookup in IPsec; for most packets, the source address
166 	 * is set at a higher level protocol. ICMPs and other packets
167 	 * though (e.g., traceroute) have a source address of zeroes.
168 	 */
169 	if (ro == NULL) {
170 		ro = &iproute;
171 		memset(ro, 0, sizeof(*ro));
172 	}
173 
174 	dst = satosin(&ro->ro_dst);
175 
176 	/*
177 	 * If there is a cached route, check that it is to the same
178 	 * destination and is still up.  If not, free it and try again.
179 	 */
180 	if (!rtisvalid(ro->ro_rt) ||
181 	    dst->sin_addr.s_addr != ip->ip_dst.s_addr ||
182 	    ro->ro_tableid != m->m_pkthdr.ph_rtableid) {
183 		rtfree(ro->ro_rt);
184 		ro->ro_rt = NULL;
185 	}
186 
187 	if (ro->ro_rt == NULL) {
188 		dst->sin_family = AF_INET;
189 		dst->sin_len = sizeof(*dst);
190 		dst->sin_addr = ip->ip_dst;
191 		ro->ro_tableid = m->m_pkthdr.ph_rtableid;
192 	}
193 
194 	if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
195 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) &&
196 	    imo != NULL && (ifp = if_get(imo->imo_ifidx)) != NULL) {
197 
198 		mtu = ifp->if_mtu;
199 		if (ip->ip_src.s_addr == INADDR_ANY) {
200 			struct in_ifaddr *ia;
201 
202 			IFP_TO_IA(ifp, ia);
203 			if (ia != NULL)
204 				ip->ip_src = ia->ia_addr.sin_addr;
205 		}
206 	} else {
207 		struct in_ifaddr *ia;
208 
209 		if (ro->ro_rt == NULL)
210 			ro->ro_rt = rtalloc_mpath(&ro->ro_dst,
211 			    &ip->ip_src.s_addr, ro->ro_tableid);
212 
213 		if (ro->ro_rt == NULL) {
214 			ipstat_inc(ips_noroute);
215 			error = EHOSTUNREACH;
216 			goto bad;
217 		}
218 
219 		ia = ifatoia(ro->ro_rt->rt_ifa);
220 		if (ISSET(ro->ro_rt->rt_flags, RTF_LOCAL))
221 			ifp = if_get(rtable_loindex(m->m_pkthdr.ph_rtableid));
222 		else
223 			ifp = if_get(ro->ro_rt->rt_ifidx);
224 		/*
225 		 * We aren't using rtisvalid() here because the UP/DOWN state
226 		 * machine is broken with some Ethernet drivers like em(4).
227 		 * As a result we might try to use an invalid cached route
228 		 * entry while an interface is being detached.
229 		 */
230 		if (ifp == NULL) {
231 			ipstat_inc(ips_noroute);
232 			error = EHOSTUNREACH;
233 			goto bad;
234 		}
235 		if ((mtu = ro->ro_rt->rt_mtu) == 0)
236 			mtu = ifp->if_mtu;
237 
238 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
239 			dst = satosin(ro->ro_rt->rt_gateway);
240 
241 		/* Set the source IP address */
242 		if (ip->ip_src.s_addr == INADDR_ANY && ia)
243 			ip->ip_src = ia->ia_addr.sin_addr;
244 	}
245 
246 #ifdef IPSEC
247 	if (ipsec_in_use || inp != NULL) {
248 		/* Do we have any pending SAs to apply ? */
249 		tdb = ip_output_ipsec_lookup(m, hlen, &error, inp,
250 		    ipsecflowinfo);
251 		if (error != 0) {
252 			/* Should silently drop packet */
253 			if (error == -EINVAL)
254 				error = 0;
255 			goto bad;
256 		}
257 		if (tdb != NULL) {
258 			/*
259 			 * If it needs TCP/UDP hardware-checksumming, do the
260 			 * computation now.
261 			 */
262 			in_proto_cksum_out(m, NULL);
263 		}
264 	}
265 #endif /* IPSEC */
266 
267 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
268 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
269 
270 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
271 			M_BCAST : M_MCAST;
272 
273 		/*
274 		 * IP destination address is multicast.  Make sure "dst"
275 		 * still points to the address in "ro".  (It may have been
276 		 * changed to point to a gateway address, above.)
277 		 */
278 		dst = satosin(&ro->ro_dst);
279 
280 		/*
281 		 * See if the caller provided any multicast options
282 		 */
283 		if (imo != NULL)
284 			ip->ip_ttl = imo->imo_ttl;
285 		else
286 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
287 
288 		/*
289 		 * if we don't know the outgoing ifp yet, we can't generate
290 		 * output
291 		 */
292 		if (!ifp) {
293 			ipstat_inc(ips_noroute);
294 			error = EHOSTUNREACH;
295 			goto bad;
296 		}
297 
298 		/*
299 		 * Confirm that the outgoing interface supports multicast,
300 		 * but only if the packet actually is going out on that
301 		 * interface (i.e., no IPsec is applied).
302 		 */
303 		if ((((m->m_flags & M_MCAST) &&
304 		      (ifp->if_flags & IFF_MULTICAST) == 0) ||
305 		     ((m->m_flags & M_BCAST) &&
306 		      (ifp->if_flags & IFF_BROADCAST) == 0)) && (tdb == NULL)) {
307 			ipstat_inc(ips_noroute);
308 			error = ENETUNREACH;
309 			goto bad;
310 		}
311 
312 		/*
313 		 * If source address not specified yet, use address
314 		 * of outgoing interface.
315 		 */
316 		if (ip->ip_src.s_addr == INADDR_ANY) {
317 			struct in_ifaddr *ia;
318 
319 			IFP_TO_IA(ifp, ia);
320 			if (ia != NULL)
321 				ip->ip_src = ia->ia_addr.sin_addr;
322 		}
323 
324 		if ((imo == NULL || imo->imo_loop) &&
325 		    in_hasmulti(&ip->ip_dst, ifp)) {
326 			/*
327 			 * If we belong to the destination multicast group
328 			 * on the outgoing interface, and the caller did not
329 			 * forbid loopback, loop back a copy.
330 			 * Can't defer TCP/UDP checksumming, do the
331 			 * computation now.
332 			 */
333 			in_proto_cksum_out(m, NULL);
334 			ip_mloopback(ifp, m, dst);
335 		}
336 #ifdef MROUTING
337 		else {
338 			/*
339 			 * If we are acting as a multicast router, perform
340 			 * multicast forwarding as if the packet had just
341 			 * arrived on the interface to which we are about
342 			 * to send.  The multicast forwarding function
343 			 * recursively calls this function, using the
344 			 * IP_FORWARDING flag to prevent infinite recursion.
345 			 *
346 			 * Multicasts that are looped back by ip_mloopback(),
347 			 * above, will be forwarded by the ip_input() routine,
348 			 * if necessary.
349 			 */
350 			if (ipmforwarding && ip_mrouter[ifp->if_rdomain] &&
351 			    (flags & IP_FORWARDING) == 0) {
352 				int rv;
353 
354 				KERNEL_LOCK();
355 				rv = ip_mforward(m, ifp);
356 				KERNEL_UNLOCK();
357 				if (rv != 0)
358 					goto bad;
359 			}
360 		}
361 #endif
362 		/*
363 		 * Multicasts with a time-to-live of zero may be looped-
364 		 * back, above, but must not be transmitted on a network.
365 		 * Also, multicasts addressed to the loopback interface
366 		 * are not sent -- the above call to ip_mloopback() will
367 		 * loop back a copy if this host actually belongs to the
368 		 * destination group on the loopback interface.
369 		 */
370 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0)
371 			goto bad;
372 
373 		goto sendit;
374 	}
375 
376 	/*
377 	 * Look for broadcast address and verify user is allowed to send
378 	 * such a packet; if the packet is going in an IPsec tunnel, skip
379 	 * this check.
380 	 */
381 	if ((tdb == NULL) && ((dst->sin_addr.s_addr == INADDR_BROADCAST) ||
382 	    (ro && ro->ro_rt && ISSET(ro->ro_rt->rt_flags, RTF_BROADCAST)))) {
383 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
384 			error = EADDRNOTAVAIL;
385 			goto bad;
386 		}
387 		if ((flags & IP_ALLOWBROADCAST) == 0) {
388 			error = EACCES;
389 			goto bad;
390 		}
391 
392 		/* Don't allow broadcast messages to be fragmented */
393 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
394 			error = EMSGSIZE;
395 			goto bad;
396 		}
397 		m->m_flags |= M_BCAST;
398 	} else
399 		m->m_flags &= ~M_BCAST;
400 
401 sendit:
402 	/*
403 	 * If we're doing Path MTU discovery, we need to set DF unless
404 	 * the route's MTU is locked.
405 	 */
406 	if ((flags & IP_MTUDISC) && ro && ro->ro_rt &&
407 	    (ro->ro_rt->rt_locks & RTV_MTU) == 0)
408 		ip->ip_off |= htons(IP_DF);
409 
410 #ifdef IPSEC
411 	/*
412 	 * Check if the packet needs encapsulation.
413 	 */
414 	if (tdb != NULL) {
415 		/* Callee frees mbuf */
416 		error = ip_output_ipsec_send(tdb, m, ro,
417 		    (flags & IP_FORWARDING) ? 1 : 0);
418 		goto done;
419 	}
420 #endif /* IPSEC */
421 
422 	/*
423 	 * Packet filter
424 	 */
425 #if NPF > 0
426 	if (pf_test(AF_INET, (flags & IP_FORWARDING) ? PF_FWD : PF_OUT,
427 	    ifp, &m) != PF_PASS) {
428 		error = EACCES;
429 		goto bad;
430 	}
431 	if (m == NULL)
432 		goto done;
433 	ip = mtod(m, struct ip *);
434 	hlen = ip->ip_hl << 2;
435 	if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) ==
436 	    (PF_TAG_REROUTE | PF_TAG_GENERATED))
437 		/* already rerun the route lookup, go on */
438 		m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE);
439 	else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) {
440 		/* tag as generated to skip over pf_test on rerun */
441 		m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
442 		ro = NULL;
443 		if_put(ifp); /* drop reference since target changed */
444 		ifp = NULL;
445 		goto reroute;
446 	}
447 #endif
448 	in_proto_cksum_out(m, ifp);
449 
450 #ifdef IPSEC
451 	if (ipsec_in_use && (flags & IP_FORWARDING) && (ipforwarding == 2) &&
452 	    (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) == NULL)) {
453 		error = EHOSTUNREACH;
454 		goto bad;
455 	}
456 #endif
457 
458 	/*
459 	 * If small enough for interface, can just send directly.
460 	 */
461 	if (ntohs(ip->ip_len) <= mtu) {
462 		ip->ip_sum = 0;
463 		if (in_ifcap_cksum(m, ifp, IFCAP_CSUM_IPv4))
464 			m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT;
465 		else {
466 			ipstat_inc(ips_outswcsum);
467 			ip->ip_sum = in_cksum(m, hlen);
468 		}
469 
470 		error = ifp->if_output(ifp, m, sintosa(dst), ro->ro_rt);
471 		goto done;
472 	}
473 
474 	/*
475 	 * Too large for interface; fragment if possible.
476 	 * Must be able to put at least 8 bytes per fragment.
477 	 */
478 	if (ip->ip_off & htons(IP_DF)) {
479 #ifdef IPSEC
480 		if (ip_mtudisc)
481 			ipsec_adjust_mtu(m, ifp->if_mtu);
482 #endif
483 		error = EMSGSIZE;
484 #if NPF > 0
485 		/* pf changed routing table, use orig rtable for path MTU */
486 		if (ro->ro_tableid != orig_rtableid) {
487 			rtfree(ro->ro_rt);
488 			ro->ro_tableid = orig_rtableid;
489 			ro->ro_rt = icmp_mtudisc_clone(
490 			    satosin(&ro->ro_dst)->sin_addr, ro->ro_tableid, 0);
491 		}
492 #endif
493 		/*
494 		 * This case can happen if the user changed the MTU
495 		 * of an interface after enabling IP on it.  Because
496 		 * most netifs don't keep track of routes pointing to
497 		 * them, there is no way for one to update all its
498 		 * routes when the MTU is changed.
499 		 */
500 		if (rtisvalid(ro->ro_rt) &&
501 		    ISSET(ro->ro_rt->rt_flags, RTF_HOST) &&
502 		    !(ro->ro_rt->rt_locks & RTV_MTU) &&
503 		    (ro->ro_rt->rt_mtu > ifp->if_mtu)) {
504 			ro->ro_rt->rt_mtu = ifp->if_mtu;
505 		}
506 		ipstat_inc(ips_cantfrag);
507 		goto bad;
508 	}
509 
510 	error = ip_fragment(m, &fml, ifp, mtu);
511 	if (error)
512 		goto done;
513 
514 	while ((m = ml_dequeue(&fml)) != NULL) {
515 		error = ifp->if_output(ifp, m, sintosa(dst), ro->ro_rt);
516 		if (error)
517 			break;
518 	}
519 	if (error)
520 		ml_purge(&fml);
521 	else
522 		ipstat_inc(ips_fragmented);
523 
524 done:
525 	if (ro == &iproute && ro->ro_rt)
526 		rtfree(ro->ro_rt);
527 	if_put(ifp);
528 	return (error);
529 
530 bad:
531 	m_freem(m);
532 	goto done;
533 }
534 
535 #ifdef IPSEC
536 struct tdb *
537 ip_output_ipsec_lookup(struct mbuf *m, int hlen, int *error, struct inpcb *inp,
538     int ipsecflowinfo)
539 {
540 	struct m_tag *mtag;
541 	struct tdb_ident *tdbi;
542 	struct tdb *tdb;
543 
544 	/* Do we have any pending SAs to apply ? */
545 	tdb = ipsp_spd_lookup(m, AF_INET, hlen, error, IPSP_DIRECTION_OUT,
546 	    NULL, inp, ipsecflowinfo);
547 	if (tdb == NULL)
548 		return NULL;
549 	/* Loop detection */
550 	for (mtag = m_tag_first(m); mtag != NULL; mtag = m_tag_next(m, mtag)) {
551 		if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE)
552 			continue;
553 		tdbi = (struct tdb_ident *)(mtag + 1);
554 		if (tdbi->spi == tdb->tdb_spi &&
555 		    tdbi->proto == tdb->tdb_sproto &&
556 		    tdbi->rdomain == tdb->tdb_rdomain &&
557 		    !memcmp(&tdbi->dst, &tdb->tdb_dst,
558 		    sizeof(union sockaddr_union))) {
559 			/* no IPsec needed */
560 			return NULL;
561 		}
562 	}
563 	return tdb;
564 }
565 
566 int
567 ip_output_ipsec_send(struct tdb *tdb, struct mbuf *m, struct route *ro, int fwd)
568 {
569 #if NPF > 0
570 	struct ifnet *encif;
571 #endif
572 	struct ip *ip;
573 	int error;
574 
575 #if NPF > 0
576 	/*
577 	 * Packet filter
578 	 */
579 	if ((encif = enc_getif(tdb->tdb_rdomain, tdb->tdb_tap)) == NULL ||
580 	    pf_test(AF_INET, fwd ? PF_FWD : PF_OUT, encif, &m) != PF_PASS) {
581 		m_freem(m);
582 		return EACCES;
583 	}
584 	if (m == NULL)
585 		return 0;
586 	/*
587 	 * PF_TAG_REROUTE handling or not...
588 	 * Packet is entering IPsec so the routing is
589 	 * already overruled by the IPsec policy.
590 	 * Until now the change was not reconsidered.
591 	 * What's the behaviour?
592 	 */
593 	in_proto_cksum_out(m, encif);
594 #endif
595 
596 	/* Check if we are allowed to fragment */
597 	ip = mtod(m, struct ip *);
598 	if (ip_mtudisc && (ip->ip_off & htons(IP_DF)) && tdb->tdb_mtu &&
599 	    ntohs(ip->ip_len) > tdb->tdb_mtu &&
600 	    tdb->tdb_mtutimeout > gettime()) {
601 		struct rtentry *rt = NULL;
602 		int rt_mtucloned = 0;
603 		int transportmode = 0;
604 
605 		transportmode = (tdb->tdb_dst.sa.sa_family == AF_INET) &&
606 		    (tdb->tdb_dst.sin.sin_addr.s_addr == ip->ip_dst.s_addr);
607 
608 		/* Find a host route to store the mtu in */
609 		if (ro != NULL)
610 			rt = ro->ro_rt;
611 		/* but don't add a PMTU route for transport mode SAs */
612 		if (transportmode)
613 			rt = NULL;
614 		else if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0) {
615 			rt = icmp_mtudisc_clone(ip->ip_dst,
616 			    m->m_pkthdr.ph_rtableid, 1);
617 			rt_mtucloned = 1;
618 		}
619 		DPRINTF("spi %08x mtu %d rt %p cloned %d",
620 		    ntohl(tdb->tdb_spi), tdb->tdb_mtu, rt, rt_mtucloned);
621 		if (rt != NULL) {
622 			rt->rt_mtu = tdb->tdb_mtu;
623 			if (ro != NULL && ro->ro_rt != NULL) {
624 				rtfree(ro->ro_rt);
625 				ro->ro_rt = rtalloc(&ro->ro_dst, RT_RESOLVE,
626 				    m->m_pkthdr.ph_rtableid);
627 			}
628 			if (rt_mtucloned)
629 				rtfree(rt);
630 		}
631 		ipsec_adjust_mtu(m, tdb->tdb_mtu);
632 		m_freem(m);
633 		return EMSGSIZE;
634 	}
635 	/* propagate IP_DF for v4-over-v6 */
636 	if (ip_mtudisc && ip->ip_off & htons(IP_DF))
637 		SET(m->m_pkthdr.csum_flags, M_IPV6_DF_OUT);
638 
639 	/*
640 	 * Clear these -- they'll be set in the recursive invocation
641 	 * as needed.
642 	 */
643 	m->m_flags &= ~(M_MCAST | M_BCAST);
644 
645 	/* Callee frees mbuf */
646 	error = ipsp_process_packet(m, tdb, AF_INET, 0);
647 	if (error) {
648 		ipsecstat_inc(ipsec_odrops);
649 		tdb->tdb_odrops++;
650 	}
651 	return error;
652 }
653 #endif /* IPSEC */
654 
655 int
656 ip_fragment(struct mbuf *m, struct mbuf_list *fml, struct ifnet *ifp,
657     u_long mtu)
658 {
659 	struct ip *ip, *mhip;
660 	struct mbuf *m0;
661 	int len, hlen, off;
662 	int mhlen, firstlen;
663 	int error;
664 
665 	ml_init(fml);
666 	ml_enqueue(fml, m);
667 
668 	ip = mtod(m, struct ip *);
669 	hlen = ip->ip_hl << 2;
670 	len = (mtu - hlen) &~ 7;
671 	if (len < 8) {
672 		error = EMSGSIZE;
673 		goto bad;
674 	}
675 
676 	/*
677 	 * If we are doing fragmentation, we can't defer TCP/UDP
678 	 * checksumming; compute the checksum and clear the flag.
679 	 */
680 	in_proto_cksum_out(m, NULL);
681 	firstlen = len;
682 
683 	/*
684 	 * Loop through length of segment after first fragment,
685 	 * make new header and copy data of each part and link onto chain.
686 	 */
687 	m0 = m;
688 	mhlen = sizeof (struct ip);
689 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
690 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
691 		if (m == NULL) {
692 			error = ENOBUFS;
693 			goto bad;
694 		}
695 		ml_enqueue(fml, m);
696 		if ((error = m_dup_pkthdr(m, m0, M_DONTWAIT)) != 0)
697 			goto bad;
698 		m->m_data += max_linkhdr;
699 		mhip = mtod(m, struct ip *);
700 		*mhip = *ip;
701 		if (hlen > sizeof (struct ip)) {
702 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
703 			mhip->ip_hl = mhlen >> 2;
704 		}
705 		m->m_len = mhlen;
706 		mhip->ip_off = ((off - hlen) >> 3) +
707 		    (ntohs(ip->ip_off) & ~IP_MF);
708 		if (ip->ip_off & htons(IP_MF))
709 			mhip->ip_off |= IP_MF;
710 		if (off + len >= ntohs(ip->ip_len))
711 			len = ntohs(ip->ip_len) - off;
712 		else
713 			mhip->ip_off |= IP_MF;
714 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
715 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
716 		if (m->m_next == NULL) {
717 			error = ENOBUFS;
718 			goto bad;
719 		}
720 		m->m_pkthdr.len = mhlen + len;
721 		m->m_pkthdr.ph_ifidx = 0;
722 		mhip->ip_off = htons((u_int16_t)mhip->ip_off);
723 		mhip->ip_sum = 0;
724 		if (in_ifcap_cksum(m, ifp, IFCAP_CSUM_IPv4))
725 			m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT;
726 		else {
727 			ipstat_inc(ips_outswcsum);
728 			mhip->ip_sum = in_cksum(m, mhlen);
729 		}
730 	}
731 	/*
732 	 * Update first fragment by trimming what's been copied out
733 	 * and updating header, then send each fragment (in order).
734 	 */
735 	m = m0;
736 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
737 	m->m_pkthdr.len = hlen + firstlen;
738 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
739 	ip->ip_off |= htons(IP_MF);
740 	ip->ip_sum = 0;
741 	if (in_ifcap_cksum(m, ifp, IFCAP_CSUM_IPv4))
742 		m->m_pkthdr.csum_flags |= M_IPV4_CSUM_OUT;
743 	else {
744 		ipstat_inc(ips_outswcsum);
745 		ip->ip_sum = in_cksum(m, hlen);
746 	}
747 
748 	ipstat_add(ips_ofragments, ml_len(fml));
749 	return (0);
750 
751 bad:
752 	ipstat_inc(ips_odropped);
753 	ml_purge(fml);
754 	return (error);
755 }
756 
757 /*
758  * Insert IP options into preformed packet.
759  * Adjust IP destination as required for IP source routing,
760  * as indicated by a non-zero in_addr at the start of the options.
761  */
762 struct mbuf *
763 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
764 {
765 	struct ipoption *p = mtod(opt, struct ipoption *);
766 	struct mbuf *n;
767 	struct ip *ip = mtod(m, struct ip *);
768 	unsigned int optlen;
769 
770 	optlen = opt->m_len - sizeof(p->ipopt_dst);
771 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
772 		return (m);		/* XXX should fail */
773 
774 	/* check if options will fit to IP header */
775 	if ((optlen + sizeof(struct ip)) > (0x0f << 2)) {
776 		*phlen = sizeof(struct ip);
777 		return (m);
778 	}
779 
780 	if (p->ipopt_dst.s_addr)
781 		ip->ip_dst = p->ipopt_dst;
782 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
783 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
784 		if (n == NULL)
785 			return (m);
786 		M_MOVE_HDR(n, m);
787 		n->m_pkthdr.len += optlen;
788 		m->m_len -= sizeof(struct ip);
789 		m->m_data += sizeof(struct ip);
790 		n->m_next = m;
791 		m = n;
792 		m->m_len = optlen + sizeof(struct ip);
793 		m->m_data += max_linkhdr;
794 		memcpy(mtod(m, caddr_t), ip, sizeof(struct ip));
795 	} else {
796 		m->m_data -= optlen;
797 		m->m_len += optlen;
798 		m->m_pkthdr.len += optlen;
799 		memmove(mtod(m, caddr_t), (caddr_t)ip, sizeof(struct ip));
800 	}
801 	ip = mtod(m, struct ip *);
802 	memcpy(ip + 1, p->ipopt_list, optlen);
803 	*phlen = sizeof(struct ip) + optlen;
804 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
805 	return (m);
806 }
807 
808 /*
809  * Copy options from ip to jp,
810  * omitting those not copied during fragmentation.
811  */
812 int
813 ip_optcopy(struct ip *ip, struct ip *jp)
814 {
815 	u_char *cp, *dp;
816 	int opt, optlen, cnt;
817 
818 	cp = (u_char *)(ip + 1);
819 	dp = (u_char *)(jp + 1);
820 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
821 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
822 		opt = cp[0];
823 		if (opt == IPOPT_EOL)
824 			break;
825 		if (opt == IPOPT_NOP) {
826 			/* Preserve for IP mcast tunnel's LSRR alignment. */
827 			*dp++ = IPOPT_NOP;
828 			optlen = 1;
829 			continue;
830 		}
831 #ifdef DIAGNOSTIC
832 		if (cnt < IPOPT_OLEN + sizeof(*cp))
833 			panic("malformed IPv4 option passed to ip_optcopy");
834 #endif
835 		optlen = cp[IPOPT_OLEN];
836 #ifdef DIAGNOSTIC
837 		if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
838 			panic("malformed IPv4 option passed to ip_optcopy");
839 #endif
840 		/* bogus lengths should have been caught by ip_dooptions */
841 		if (optlen > cnt)
842 			optlen = cnt;
843 		if (IPOPT_COPIED(opt)) {
844 			memcpy(dp, cp, optlen);
845 			dp += optlen;
846 		}
847 	}
848 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
849 		*dp++ = IPOPT_EOL;
850 	return (optlen);
851 }
852 
853 /*
854  * IP socket option processing.
855  */
856 int
857 ip_ctloutput(int op, struct socket *so, int level, int optname,
858     struct mbuf *m)
859 {
860 	struct inpcb *inp = sotoinpcb(so);
861 	int optval = 0;
862 	struct proc *p = curproc; /* XXX */
863 	int error = 0;
864 	u_int rtableid, rtid = 0;
865 
866 	if (level != IPPROTO_IP)
867 		return (EINVAL);
868 
869 	rtableid = p->p_p->ps_rtableid;
870 
871 	switch (op) {
872 	case PRCO_SETOPT:
873 		switch (optname) {
874 		case IP_OPTIONS:
875 			return (ip_pcbopts(&inp->inp_options, m));
876 
877 		case IP_TOS:
878 		case IP_TTL:
879 		case IP_MINTTL:
880 		case IP_RECVOPTS:
881 		case IP_RECVRETOPTS:
882 		case IP_RECVDSTADDR:
883 		case IP_RECVIF:
884 		case IP_RECVTTL:
885 		case IP_RECVDSTPORT:
886 		case IP_RECVRTABLE:
887 		case IP_IPSECFLOWINFO:
888 			if (m == NULL || m->m_len != sizeof(int))
889 				error = EINVAL;
890 			else {
891 				optval = *mtod(m, int *);
892 				switch (optname) {
893 
894 				case IP_TOS:
895 					inp->inp_ip.ip_tos = optval;
896 					break;
897 
898 				case IP_TTL:
899 					if (optval > 0 && optval <= MAXTTL)
900 						inp->inp_ip.ip_ttl = optval;
901 					else if (optval == -1)
902 						inp->inp_ip.ip_ttl = ip_defttl;
903 					else
904 						error = EINVAL;
905 					break;
906 
907 				case IP_MINTTL:
908 					if (optval >= 0 && optval <= MAXTTL)
909 						inp->inp_ip_minttl = optval;
910 					else
911 						error = EINVAL;
912 					break;
913 #define	OPTSET(bit) \
914 	if (optval) \
915 		inp->inp_flags |= bit; \
916 	else \
917 		inp->inp_flags &= ~bit;
918 
919 				case IP_RECVOPTS:
920 					OPTSET(INP_RECVOPTS);
921 					break;
922 
923 				case IP_RECVRETOPTS:
924 					OPTSET(INP_RECVRETOPTS);
925 					break;
926 
927 				case IP_RECVDSTADDR:
928 					OPTSET(INP_RECVDSTADDR);
929 					break;
930 				case IP_RECVIF:
931 					OPTSET(INP_RECVIF);
932 					break;
933 				case IP_RECVTTL:
934 					OPTSET(INP_RECVTTL);
935 					break;
936 				case IP_RECVDSTPORT:
937 					OPTSET(INP_RECVDSTPORT);
938 					break;
939 				case IP_RECVRTABLE:
940 					OPTSET(INP_RECVRTABLE);
941 					break;
942 				case IP_IPSECFLOWINFO:
943 					OPTSET(INP_IPSECFLOWINFO);
944 					break;
945 				}
946 			}
947 			break;
948 #undef OPTSET
949 
950 		case IP_MULTICAST_IF:
951 		case IP_MULTICAST_TTL:
952 		case IP_MULTICAST_LOOP:
953 		case IP_ADD_MEMBERSHIP:
954 		case IP_DROP_MEMBERSHIP:
955 			error = ip_setmoptions(optname, &inp->inp_moptions, m,
956 			    inp->inp_rtableid);
957 			break;
958 
959 		case IP_PORTRANGE:
960 			if (m == NULL || m->m_len != sizeof(int))
961 				error = EINVAL;
962 			else {
963 				optval = *mtod(m, int *);
964 
965 				switch (optval) {
966 
967 				case IP_PORTRANGE_DEFAULT:
968 					inp->inp_flags &= ~(INP_LOWPORT);
969 					inp->inp_flags &= ~(INP_HIGHPORT);
970 					break;
971 
972 				case IP_PORTRANGE_HIGH:
973 					inp->inp_flags &= ~(INP_LOWPORT);
974 					inp->inp_flags |= INP_HIGHPORT;
975 					break;
976 
977 				case IP_PORTRANGE_LOW:
978 					inp->inp_flags &= ~(INP_HIGHPORT);
979 					inp->inp_flags |= INP_LOWPORT;
980 					break;
981 
982 				default:
983 
984 					error = EINVAL;
985 					break;
986 				}
987 			}
988 			break;
989 		case IP_AUTH_LEVEL:
990 		case IP_ESP_TRANS_LEVEL:
991 		case IP_ESP_NETWORK_LEVEL:
992 		case IP_IPCOMP_LEVEL:
993 #ifndef IPSEC
994 			error = EOPNOTSUPP;
995 #else
996 			if (m == NULL || m->m_len != sizeof(int)) {
997 				error = EINVAL;
998 				break;
999 			}
1000 			optval = *mtod(m, int *);
1001 
1002 			if (optval < IPSEC_LEVEL_BYPASS ||
1003 			    optval > IPSEC_LEVEL_UNIQUE) {
1004 				error = EINVAL;
1005 				break;
1006 			}
1007 
1008 			switch (optname) {
1009 			case IP_AUTH_LEVEL:
1010 				if (optval < IPSEC_AUTH_LEVEL_DEFAULT &&
1011 				    suser(p)) {
1012 					error = EACCES;
1013 					break;
1014 				}
1015 				inp->inp_seclevel[SL_AUTH] = optval;
1016 				break;
1017 
1018 			case IP_ESP_TRANS_LEVEL:
1019 				if (optval < IPSEC_ESP_TRANS_LEVEL_DEFAULT &&
1020 				    suser(p)) {
1021 					error = EACCES;
1022 					break;
1023 				}
1024 				inp->inp_seclevel[SL_ESP_TRANS] = optval;
1025 				break;
1026 
1027 			case IP_ESP_NETWORK_LEVEL:
1028 				if (optval < IPSEC_ESP_NETWORK_LEVEL_DEFAULT &&
1029 				    suser(p)) {
1030 					error = EACCES;
1031 					break;
1032 				}
1033 				inp->inp_seclevel[SL_ESP_NETWORK] = optval;
1034 				break;
1035 			case IP_IPCOMP_LEVEL:
1036 				if (optval < IPSEC_IPCOMP_LEVEL_DEFAULT &&
1037 				    suser(p)) {
1038 					error = EACCES;
1039 					break;
1040 				}
1041 				inp->inp_seclevel[SL_IPCOMP] = optval;
1042 				break;
1043 			}
1044 #endif
1045 			break;
1046 
1047 		case IP_IPSEC_LOCAL_ID:
1048 		case IP_IPSEC_REMOTE_ID:
1049 			error = EOPNOTSUPP;
1050 			break;
1051 		case SO_RTABLE:
1052 			if (m == NULL || m->m_len < sizeof(u_int)) {
1053 				error = EINVAL;
1054 				break;
1055 			}
1056 			rtid = *mtod(m, u_int *);
1057 			if (inp->inp_rtableid == rtid)
1058 				break;
1059 			/* needs privileges to switch when already set */
1060 			if (rtableid != rtid && rtableid != 0 &&
1061 			    (error = suser(p)) != 0)
1062 				break;
1063 			/* table must exist */
1064 			if (!rtable_exists(rtid)) {
1065 				error = EINVAL;
1066 				break;
1067 			}
1068 			if (inp->inp_lport) {
1069 				error = EBUSY;
1070 				break;
1071 			}
1072 			inp->inp_rtableid = rtid;
1073 			in_pcbrehash(inp);
1074 			break;
1075 		case IP_PIPEX:
1076 			if (m != NULL && m->m_len == sizeof(int))
1077 				inp->inp_pipex = *mtod(m, int *);
1078 			else
1079 				error = EINVAL;
1080 			break;
1081 
1082 		default:
1083 			error = ENOPROTOOPT;
1084 			break;
1085 		}
1086 		break;
1087 
1088 	case PRCO_GETOPT:
1089 		switch (optname) {
1090 		case IP_OPTIONS:
1091 		case IP_RETOPTS:
1092 			if (inp->inp_options) {
1093 				m->m_len = inp->inp_options->m_len;
1094 				memcpy(mtod(m, caddr_t),
1095 				    mtod(inp->inp_options, caddr_t), m->m_len);
1096 			} else
1097 				m->m_len = 0;
1098 			break;
1099 
1100 		case IP_TOS:
1101 		case IP_TTL:
1102 		case IP_MINTTL:
1103 		case IP_RECVOPTS:
1104 		case IP_RECVRETOPTS:
1105 		case IP_RECVDSTADDR:
1106 		case IP_RECVIF:
1107 		case IP_RECVTTL:
1108 		case IP_RECVDSTPORT:
1109 		case IP_RECVRTABLE:
1110 		case IP_IPSECFLOWINFO:
1111 		case IP_IPDEFTTL:
1112 			m->m_len = sizeof(int);
1113 			switch (optname) {
1114 
1115 			case IP_TOS:
1116 				optval = inp->inp_ip.ip_tos;
1117 				break;
1118 
1119 			case IP_TTL:
1120 				optval = inp->inp_ip.ip_ttl;
1121 				break;
1122 
1123 			case IP_MINTTL:
1124 				optval = inp->inp_ip_minttl;
1125 				break;
1126 
1127 			case IP_IPDEFTTL:
1128 				optval = ip_defttl;
1129 				break;
1130 
1131 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1132 
1133 			case IP_RECVOPTS:
1134 				optval = OPTBIT(INP_RECVOPTS);
1135 				break;
1136 
1137 			case IP_RECVRETOPTS:
1138 				optval = OPTBIT(INP_RECVRETOPTS);
1139 				break;
1140 
1141 			case IP_RECVDSTADDR:
1142 				optval = OPTBIT(INP_RECVDSTADDR);
1143 				break;
1144 			case IP_RECVIF:
1145 				optval = OPTBIT(INP_RECVIF);
1146 				break;
1147 			case IP_RECVTTL:
1148 				optval = OPTBIT(INP_RECVTTL);
1149 				break;
1150 			case IP_RECVDSTPORT:
1151 				optval = OPTBIT(INP_RECVDSTPORT);
1152 				break;
1153 			case IP_RECVRTABLE:
1154 				optval = OPTBIT(INP_RECVRTABLE);
1155 				break;
1156 			case IP_IPSECFLOWINFO:
1157 				optval = OPTBIT(INP_IPSECFLOWINFO);
1158 				break;
1159 			}
1160 			*mtod(m, int *) = optval;
1161 			break;
1162 
1163 		case IP_MULTICAST_IF:
1164 		case IP_MULTICAST_TTL:
1165 		case IP_MULTICAST_LOOP:
1166 		case IP_ADD_MEMBERSHIP:
1167 		case IP_DROP_MEMBERSHIP:
1168 			error = ip_getmoptions(optname, inp->inp_moptions, m);
1169 			break;
1170 
1171 		case IP_PORTRANGE:
1172 			m->m_len = sizeof(int);
1173 
1174 			if (inp->inp_flags & INP_HIGHPORT)
1175 				optval = IP_PORTRANGE_HIGH;
1176 			else if (inp->inp_flags & INP_LOWPORT)
1177 				optval = IP_PORTRANGE_LOW;
1178 			else
1179 				optval = 0;
1180 
1181 			*mtod(m, int *) = optval;
1182 			break;
1183 
1184 		case IP_AUTH_LEVEL:
1185 		case IP_ESP_TRANS_LEVEL:
1186 		case IP_ESP_NETWORK_LEVEL:
1187 		case IP_IPCOMP_LEVEL:
1188 #ifndef IPSEC
1189 			m->m_len = sizeof(int);
1190 			*mtod(m, int *) = IPSEC_LEVEL_NONE;
1191 #else
1192 			m->m_len = sizeof(int);
1193 			switch (optname) {
1194 			case IP_AUTH_LEVEL:
1195 				optval = inp->inp_seclevel[SL_AUTH];
1196 				break;
1197 
1198 			case IP_ESP_TRANS_LEVEL:
1199 				optval = inp->inp_seclevel[SL_ESP_TRANS];
1200 				break;
1201 
1202 			case IP_ESP_NETWORK_LEVEL:
1203 				optval = inp->inp_seclevel[SL_ESP_NETWORK];
1204 				break;
1205 			case IP_IPCOMP_LEVEL:
1206 				optval = inp->inp_seclevel[SL_IPCOMP];
1207 				break;
1208 			}
1209 			*mtod(m, int *) = optval;
1210 #endif
1211 			break;
1212 		case IP_IPSEC_LOCAL_ID:
1213 		case IP_IPSEC_REMOTE_ID:
1214 			error = EOPNOTSUPP;
1215 			break;
1216 		case SO_RTABLE:
1217 			m->m_len = sizeof(u_int);
1218 			*mtod(m, u_int *) = inp->inp_rtableid;
1219 			break;
1220 		case IP_PIPEX:
1221 			m->m_len = sizeof(int);
1222 			*mtod(m, int *) = inp->inp_pipex;
1223 			break;
1224 		default:
1225 			error = ENOPROTOOPT;
1226 			break;
1227 		}
1228 		break;
1229 	}
1230 	return (error);
1231 }
1232 
1233 /*
1234  * Set up IP options in pcb for insertion in output packets.
1235  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1236  * with destination address if source routed.
1237  */
1238 int
1239 ip_pcbopts(struct mbuf **pcbopt, struct mbuf *m)
1240 {
1241 	struct mbuf *n;
1242 	struct ipoption *p;
1243 	int cnt, off, optlen;
1244 	u_char *cp;
1245 	u_char opt;
1246 
1247 	/* turn off any old options */
1248 	m_freem(*pcbopt);
1249 	*pcbopt = NULL;
1250 	if (m == NULL || m->m_len == 0) {
1251 		/*
1252 		 * Only turning off any previous options.
1253 		 */
1254 		return (0);
1255 	}
1256 
1257 	if (m->m_len % sizeof(int32_t) ||
1258 	    m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1259 		return (EINVAL);
1260 
1261 	/* Don't sleep because NET_LOCK() is hold. */
1262 	if ((n = m_get(M_NOWAIT, MT_SOOPTS)) == NULL)
1263 		return (ENOBUFS);
1264 	p = mtod(n, struct ipoption *);
1265 	memset(p, 0, sizeof (*p));	/* 0 = IPOPT_EOL, needed for padding */
1266 	n->m_len = sizeof(struct in_addr);
1267 
1268 	off = 0;
1269 	cnt = m->m_len;
1270 	cp = mtod(m, u_char *);
1271 
1272 	while (cnt > 0) {
1273 		opt = cp[IPOPT_OPTVAL];
1274 
1275 		if (opt == IPOPT_NOP || opt == IPOPT_EOL) {
1276 			optlen = 1;
1277 		} else {
1278 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1279 				goto bad;
1280 			optlen = cp[IPOPT_OLEN];
1281 			if (optlen < IPOPT_OLEN  + sizeof(*cp) || optlen > cnt)
1282 				goto bad;
1283 		}
1284 		switch (opt) {
1285 		default:
1286 			memcpy(p->ipopt_list + off, cp, optlen);
1287 			break;
1288 
1289 		case IPOPT_LSRR:
1290 		case IPOPT_SSRR:
1291 			/*
1292 			 * user process specifies route as:
1293 			 *	->A->B->C->D
1294 			 * D must be our final destination (but we can't
1295 			 * check that since we may not have connected yet).
1296 			 * A is first hop destination, which doesn't appear in
1297 			 * actual IP option, but is stored before the options.
1298 			 */
1299 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1300 				goto bad;
1301 
1302 			/*
1303 			 * Optlen is smaller because first address is popped.
1304 			 * Cnt and cp will be adjusted a bit later to reflect
1305 			 * this.
1306 			 */
1307 			optlen -= sizeof(struct in_addr);
1308 			p->ipopt_list[off + IPOPT_OPTVAL] = opt;
1309 			p->ipopt_list[off + IPOPT_OLEN] = optlen;
1310 
1311 			/*
1312 			 * Move first hop before start of options.
1313 			 */
1314 			memcpy(&p->ipopt_dst, cp + IPOPT_OFFSET,
1315 			    sizeof(struct in_addr));
1316 			cp += sizeof(struct in_addr);
1317 			cnt -= sizeof(struct in_addr);
1318 			/*
1319 			 * Then copy rest of options
1320 			 */
1321 			memcpy(p->ipopt_list + off + IPOPT_OFFSET,
1322 			    cp + IPOPT_OFFSET, optlen - IPOPT_OFFSET);
1323 			break;
1324 		}
1325 		off += optlen;
1326 		cp += optlen;
1327 		cnt -= optlen;
1328 
1329 		if (opt == IPOPT_EOL)
1330 			break;
1331 	}
1332 	/* pad options to next word, since p was zeroed just adjust off */
1333 	off = (off + sizeof(int32_t) - 1) & ~(sizeof(int32_t) - 1);
1334 	n->m_len += off;
1335 	if (n->m_len > sizeof(*p)) {
1336  bad:
1337 		m_freem(n);
1338 		return (EINVAL);
1339 	}
1340 
1341 	*pcbopt = n;
1342 	return (0);
1343 }
1344 
1345 /*
1346  * Lookup the interface based on the information in the ip_mreqn struct.
1347  */
1348 int
1349 ip_multicast_if(struct ip_mreqn *mreq, u_int rtableid, unsigned int *ifidx)
1350 {
1351 	struct sockaddr_in sin;
1352 	struct rtentry *rt;
1353 
1354 	/*
1355 	 * In case userland provides the imr_ifindex use this as interface.
1356 	 * If no interface address was provided, use the interface of
1357 	 * the route to the given multicast address.
1358 	 */
1359 	if (mreq->imr_ifindex != 0) {
1360 		*ifidx = mreq->imr_ifindex;
1361 	} else if (mreq->imr_address.s_addr == INADDR_ANY) {
1362 		memset(&sin, 0, sizeof(sin));
1363 		sin.sin_len = sizeof(sin);
1364 		sin.sin_family = AF_INET;
1365 		sin.sin_addr = mreq->imr_multiaddr;
1366 		rt = rtalloc(sintosa(&sin), RT_RESOLVE, rtableid);
1367 		if (!rtisvalid(rt)) {
1368 			rtfree(rt);
1369 			return EADDRNOTAVAIL;
1370 		}
1371 		*ifidx = rt->rt_ifidx;
1372 		rtfree(rt);
1373 	} else {
1374 		memset(&sin, 0, sizeof(sin));
1375 		sin.sin_len = sizeof(sin);
1376 		sin.sin_family = AF_INET;
1377 		sin.sin_addr = mreq->imr_address;
1378 		rt = rtalloc(sintosa(&sin), 0, rtableid);
1379 		if (!rtisvalid(rt) || !ISSET(rt->rt_flags, RTF_LOCAL)) {
1380 			rtfree(rt);
1381 			return EADDRNOTAVAIL;
1382 		}
1383 		*ifidx = rt->rt_ifidx;
1384 		rtfree(rt);
1385 	}
1386 
1387 	return 0;
1388 }
1389 
1390 /*
1391  * Set the IP multicast options in response to user setsockopt().
1392  */
1393 int
1394 ip_setmoptions(int optname, struct ip_moptions **imop, struct mbuf *m,
1395     u_int rtableid)
1396 {
1397 	struct in_addr addr;
1398 	struct in_ifaddr *ia;
1399 	struct ip_mreqn mreqn;
1400 	struct ifnet *ifp = NULL;
1401 	struct ip_moptions *imo = *imop;
1402 	struct in_multi **immp;
1403 	struct sockaddr_in sin;
1404 	unsigned int ifidx;
1405 	int i, error = 0;
1406 	u_char loop;
1407 
1408 	if (imo == NULL) {
1409 		/*
1410 		 * No multicast option buffer attached to the pcb;
1411 		 * allocate one and initialize to default values.
1412 		 */
1413 		imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK|M_ZERO);
1414 		immp = mallocarray(IP_MIN_MEMBERSHIPS, sizeof(*immp), M_IPMOPTS,
1415 		    M_WAITOK|M_ZERO);
1416 		*imop = imo;
1417 		imo->imo_ifidx = 0;
1418 		imo->imo_ttl = IP_DEFAULT_MULTICAST_TTL;
1419 		imo->imo_loop = IP_DEFAULT_MULTICAST_LOOP;
1420 		imo->imo_num_memberships = 0;
1421 		imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1422 		imo->imo_membership = immp;
1423 	}
1424 
1425 	switch (optname) {
1426 
1427 	case IP_MULTICAST_IF:
1428 		/*
1429 		 * Select the interface for outgoing multicast packets.
1430 		 */
1431 		if (m == NULL) {
1432 			error = EINVAL;
1433 			break;
1434 		}
1435 		if (m->m_len == sizeof(struct in_addr)) {
1436 			addr = *(mtod(m, struct in_addr *));
1437 		} else if (m->m_len == sizeof(struct ip_mreq) ||
1438 		    m->m_len == sizeof(struct ip_mreqn)) {
1439 			memset(&mreqn, 0, sizeof(mreqn));
1440 			memcpy(&mreqn, mtod(m, void *), m->m_len);
1441 
1442 			/*
1443 			 * If an interface index is given use this
1444 			 * index to set the imo_ifidx but check first
1445 			 * that the interface actually exists.
1446 			 * In the other case just set the addr to
1447 			 * the imr_address and fall through to the
1448 			 * regular code.
1449 			 */
1450 			if (mreqn.imr_ifindex != 0) {
1451 				ifp = if_get(mreqn.imr_ifindex);
1452 				if (ifp == NULL ||
1453 				    ifp->if_rdomain != rtable_l2(rtableid)) {
1454 					error = EADDRNOTAVAIL;
1455 					if_put(ifp);
1456 					break;
1457 				}
1458 				imo->imo_ifidx = ifp->if_index;
1459 				if_put(ifp);
1460 				break;
1461 			} else
1462 				addr = mreqn.imr_address;
1463 		} else {
1464 			error = EINVAL;
1465 			break;
1466 		}
1467 		/*
1468 		 * INADDR_ANY is used to remove a previous selection.
1469 		 * When no interface is selected, a default one is
1470 		 * chosen every time a multicast packet is sent.
1471 		 */
1472 		if (addr.s_addr == INADDR_ANY) {
1473 			imo->imo_ifidx = 0;
1474 			break;
1475 		}
1476 		/*
1477 		 * The selected interface is identified by its local
1478 		 * IP address.  Find the interface and confirm that
1479 		 * it supports multicasting.
1480 		 */
1481 		memset(&sin, 0, sizeof(sin));
1482 		sin.sin_len = sizeof(sin);
1483 		sin.sin_family = AF_INET;
1484 		sin.sin_addr = addr;
1485 		ia = ifatoia(ifa_ifwithaddr(sintosa(&sin), rtableid));
1486 		if (ia == NULL ||
1487 		    (ia->ia_ifp->if_flags & IFF_MULTICAST) == 0) {
1488 			error = EADDRNOTAVAIL;
1489 			break;
1490 		}
1491 		imo->imo_ifidx = ia->ia_ifp->if_index;
1492 		break;
1493 
1494 	case IP_MULTICAST_TTL:
1495 		/*
1496 		 * Set the IP time-to-live for outgoing multicast packets.
1497 		 */
1498 		if (m == NULL || m->m_len != 1) {
1499 			error = EINVAL;
1500 			break;
1501 		}
1502 		imo->imo_ttl = *(mtod(m, u_char *));
1503 		break;
1504 
1505 	case IP_MULTICAST_LOOP:
1506 		/*
1507 		 * Set the loopback flag for outgoing multicast packets.
1508 		 * Must be zero or one.
1509 		 */
1510 		if (m == NULL || m->m_len != 1 ||
1511 		   (loop = *(mtod(m, u_char *))) > 1) {
1512 			error = EINVAL;
1513 			break;
1514 		}
1515 		imo->imo_loop = loop;
1516 		break;
1517 
1518 	case IP_ADD_MEMBERSHIP:
1519 		/*
1520 		 * Add a multicast group membership.
1521 		 * Group must be a valid IP multicast address.
1522 		 */
1523 		if (m == NULL || !(m->m_len == sizeof(struct ip_mreq) ||
1524 		    m->m_len == sizeof(struct ip_mreqn))) {
1525 			error = EINVAL;
1526 			break;
1527 		}
1528 		memset(&mreqn, 0, sizeof(mreqn));
1529 		memcpy(&mreqn, mtod(m, void *), m->m_len);
1530 		if (!IN_MULTICAST(mreqn.imr_multiaddr.s_addr)) {
1531 			error = EINVAL;
1532 			break;
1533 		}
1534 
1535 		error = ip_multicast_if(&mreqn, rtableid, &ifidx);
1536 		if (error)
1537 			break;
1538 
1539 		/*
1540 		 * See if we found an interface, and confirm that it
1541 		 * supports multicast.
1542 		 */
1543 		ifp = if_get(ifidx);
1544 		if (ifp == NULL || ifp->if_rdomain != rtable_l2(rtableid) ||
1545 		    (ifp->if_flags & IFF_MULTICAST) == 0) {
1546 			error = EADDRNOTAVAIL;
1547 			if_put(ifp);
1548 			break;
1549 		}
1550 
1551 		/*
1552 		 * See if the membership already exists or if all the
1553 		 * membership slots are full.
1554 		 */
1555 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1556 			if (imo->imo_membership[i]->inm_ifidx == ifidx &&
1557 			    imo->imo_membership[i]->inm_addr.s_addr
1558 						== mreqn.imr_multiaddr.s_addr)
1559 				break;
1560 		}
1561 		if (i < imo->imo_num_memberships) {
1562 			error = EADDRINUSE;
1563 			if_put(ifp);
1564 			break;
1565 		}
1566 		if (imo->imo_num_memberships == imo->imo_max_memberships) {
1567 			struct in_multi **nmships, **omships;
1568 			size_t newmax;
1569 			/*
1570 			 * Resize the vector to next power-of-two minus 1. If
1571 			 * the size would exceed the maximum then we know we've
1572 			 * really run out of entries. Otherwise, we reallocate
1573 			 * the vector.
1574 			 */
1575 			nmships = NULL;
1576 			omships = imo->imo_membership;
1577 			newmax = ((imo->imo_max_memberships + 1) * 2) - 1;
1578 			if (newmax <= IP_MAX_MEMBERSHIPS) {
1579 				nmships = mallocarray(newmax, sizeof(*nmships),
1580 				    M_IPMOPTS, M_NOWAIT|M_ZERO);
1581 				if (nmships != NULL) {
1582 					memcpy(nmships, omships,
1583 					    sizeof(*omships) *
1584 					    imo->imo_max_memberships);
1585 					free(omships, M_IPMOPTS,
1586 					    sizeof(*omships) *
1587 					    imo->imo_max_memberships);
1588 					imo->imo_membership = nmships;
1589 					imo->imo_max_memberships = newmax;
1590 				}
1591 			}
1592 			if (nmships == NULL) {
1593 				error = ENOBUFS;
1594 				if_put(ifp);
1595 				break;
1596 			}
1597 		}
1598 		/*
1599 		 * Everything looks good; add a new record to the multicast
1600 		 * address list for the given interface.
1601 		 */
1602 		if ((imo->imo_membership[i] =
1603 		    in_addmulti(&mreqn.imr_multiaddr, ifp)) == NULL) {
1604 			error = ENOBUFS;
1605 			if_put(ifp);
1606 			break;
1607 		}
1608 		++imo->imo_num_memberships;
1609 		if_put(ifp);
1610 		break;
1611 
1612 	case IP_DROP_MEMBERSHIP:
1613 		/*
1614 		 * Drop a multicast group membership.
1615 		 * Group must be a valid IP multicast address.
1616 		 */
1617 		if (m == NULL || !(m->m_len == sizeof(struct ip_mreq) ||
1618 		    m->m_len == sizeof(struct ip_mreqn))) {
1619 			error = EINVAL;
1620 			break;
1621 		}
1622 		memset(&mreqn, 0, sizeof(mreqn));
1623 		memcpy(&mreqn, mtod(m, void *), m->m_len);
1624 		if (!IN_MULTICAST(mreqn.imr_multiaddr.s_addr)) {
1625 			error = EINVAL;
1626 			break;
1627 		}
1628 
1629 		/*
1630 		 * If an interface address was specified, get a pointer
1631 		 * to its ifnet structure.
1632 		 */
1633 		error = ip_multicast_if(&mreqn, rtableid, &ifidx);
1634 		if (error)
1635 			break;
1636 
1637 		/*
1638 		 * Find the membership in the membership array.
1639 		 */
1640 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1641 			if ((ifidx == 0 ||
1642 			    imo->imo_membership[i]->inm_ifidx == ifidx) &&
1643 			     imo->imo_membership[i]->inm_addr.s_addr ==
1644 			     mreqn.imr_multiaddr.s_addr)
1645 				break;
1646 		}
1647 		if (i == imo->imo_num_memberships) {
1648 			error = EADDRNOTAVAIL;
1649 			break;
1650 		}
1651 		/*
1652 		 * Give up the multicast address record to which the
1653 		 * membership points.
1654 		 */
1655 		in_delmulti(imo->imo_membership[i]);
1656 		/*
1657 		 * Remove the gap in the membership array.
1658 		 */
1659 		for (++i; i < imo->imo_num_memberships; ++i)
1660 			imo->imo_membership[i-1] = imo->imo_membership[i];
1661 		--imo->imo_num_memberships;
1662 		break;
1663 
1664 	default:
1665 		error = EOPNOTSUPP;
1666 		break;
1667 	}
1668 
1669 	/*
1670 	 * If all options have default values, no need to keep the data.
1671 	 */
1672 	if (imo->imo_ifidx == 0 &&
1673 	    imo->imo_ttl == IP_DEFAULT_MULTICAST_TTL &&
1674 	    imo->imo_loop == IP_DEFAULT_MULTICAST_LOOP &&
1675 	    imo->imo_num_memberships == 0) {
1676 		free(imo->imo_membership , M_IPMOPTS,
1677 		    imo->imo_max_memberships * sizeof(struct in_multi *));
1678 		free(*imop, M_IPMOPTS, sizeof(**imop));
1679 		*imop = NULL;
1680 	}
1681 
1682 	return (error);
1683 }
1684 
1685 /*
1686  * Return the IP multicast options in response to user getsockopt().
1687  */
1688 int
1689 ip_getmoptions(int optname, struct ip_moptions *imo, struct mbuf *m)
1690 {
1691 	u_char *ttl;
1692 	u_char *loop;
1693 	struct in_addr *addr;
1694 	struct in_ifaddr *ia;
1695 	struct ifnet *ifp;
1696 
1697 	switch (optname) {
1698 
1699 	case IP_MULTICAST_IF:
1700 		addr = mtod(m, struct in_addr *);
1701 		m->m_len = sizeof(struct in_addr);
1702 		if (imo == NULL || (ifp = if_get(imo->imo_ifidx)) == NULL)
1703 			addr->s_addr = INADDR_ANY;
1704 		else {
1705 			IFP_TO_IA(ifp, ia);
1706 			if_put(ifp);
1707 			addr->s_addr = (ia == NULL) ? INADDR_ANY
1708 					: ia->ia_addr.sin_addr.s_addr;
1709 		}
1710 		return (0);
1711 
1712 	case IP_MULTICAST_TTL:
1713 		ttl = mtod(m, u_char *);
1714 		m->m_len = 1;
1715 		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1716 				     : imo->imo_ttl;
1717 		return (0);
1718 
1719 	case IP_MULTICAST_LOOP:
1720 		loop = mtod(m, u_char *);
1721 		m->m_len = 1;
1722 		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1723 				      : imo->imo_loop;
1724 		return (0);
1725 
1726 	default:
1727 		return (EOPNOTSUPP);
1728 	}
1729 }
1730 
1731 /*
1732  * Discard the IP multicast options.
1733  */
1734 void
1735 ip_freemoptions(struct ip_moptions *imo)
1736 {
1737 	int i;
1738 
1739 	if (imo != NULL) {
1740 		for (i = 0; i < imo->imo_num_memberships; ++i)
1741 			in_delmulti(imo->imo_membership[i]);
1742 		free(imo->imo_membership, M_IPMOPTS,
1743 		    imo->imo_max_memberships * sizeof(struct in_multi *));
1744 		free(imo, M_IPMOPTS, sizeof(*imo));
1745 	}
1746 }
1747 
1748 /*
1749  * Routine called from ip_output() to loop back a copy of an IP multicast
1750  * packet to the input queue of a specified interface.
1751  */
1752 void
1753 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst)
1754 {
1755 	struct ip *ip;
1756 	struct mbuf *copym;
1757 
1758 	copym = m_dup_pkt(m, max_linkhdr, M_DONTWAIT);
1759 	if (copym != NULL) {
1760 		/*
1761 		 * We don't bother to fragment if the IP length is greater
1762 		 * than the interface's MTU.  Can this possibly matter?
1763 		 */
1764 		ip = mtod(copym, struct ip *);
1765 		ip->ip_sum = 0;
1766 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1767 		if_input_local(ifp, copym, dst->sin_family);
1768 	}
1769 }
1770 
1771 /*
1772  *	Compute significant parts of the IPv4 checksum pseudo-header
1773  *	for use in a delayed TCP/UDP checksum calculation.
1774  */
1775 static __inline u_int16_t __attribute__((__unused__))
1776 in_cksum_phdr(u_int32_t src, u_int32_t dst, u_int32_t lenproto)
1777 {
1778 	u_int32_t sum;
1779 
1780 	sum = lenproto +
1781 	      (u_int16_t)(src >> 16) +
1782 	      (u_int16_t)(src /*& 0xffff*/) +
1783 	      (u_int16_t)(dst >> 16) +
1784 	      (u_int16_t)(dst /*& 0xffff*/);
1785 
1786 	sum = (u_int16_t)(sum >> 16) + (u_int16_t)(sum /*& 0xffff*/);
1787 
1788 	if (sum > 0xffff)
1789 		sum -= 0xffff;
1790 
1791 	return (sum);
1792 }
1793 
1794 /*
1795  * Process a delayed payload checksum calculation.
1796  */
1797 void
1798 in_delayed_cksum(struct mbuf *m)
1799 {
1800 	struct ip *ip;
1801 	u_int16_t csum, offset;
1802 
1803 	ip = mtod(m, struct ip *);
1804 	offset = ip->ip_hl << 2;
1805 	csum = in4_cksum(m, 0, offset, m->m_pkthdr.len - offset);
1806 	if (csum == 0 && ip->ip_p == IPPROTO_UDP)
1807 		csum = 0xffff;
1808 
1809 	switch (ip->ip_p) {
1810 	case IPPROTO_TCP:
1811 		offset += offsetof(struct tcphdr, th_sum);
1812 		break;
1813 
1814 	case IPPROTO_UDP:
1815 		offset += offsetof(struct udphdr, uh_sum);
1816 		break;
1817 
1818 	case IPPROTO_ICMP:
1819 		offset += offsetof(struct icmp, icmp_cksum);
1820 		break;
1821 
1822 	default:
1823 		return;
1824 	}
1825 
1826 	if ((offset + sizeof(u_int16_t)) > m->m_len)
1827 		m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT);
1828 	else
1829 		*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
1830 }
1831 
1832 void
1833 in_proto_cksum_out(struct mbuf *m, struct ifnet *ifp)
1834 {
1835 	struct ip *ip = mtod(m, struct ip *);
1836 
1837 	/* some hw and in_delayed_cksum need the pseudo header cksum */
1838 	if (m->m_pkthdr.csum_flags &
1839 	    (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT|M_ICMP_CSUM_OUT)) {
1840 		u_int16_t csum = 0, offset;
1841 
1842 		offset = ip->ip_hl << 2;
1843 		if (m->m_pkthdr.csum_flags & (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT))
1844 			csum = in_cksum_phdr(ip->ip_src.s_addr,
1845 			    ip->ip_dst.s_addr, htonl(ntohs(ip->ip_len) -
1846 			    offset + ip->ip_p));
1847 		if (ip->ip_p == IPPROTO_TCP)
1848 			offset += offsetof(struct tcphdr, th_sum);
1849 		else if (ip->ip_p == IPPROTO_UDP)
1850 			offset += offsetof(struct udphdr, uh_sum);
1851 		else if (ip->ip_p == IPPROTO_ICMP)
1852 			offset += offsetof(struct icmp, icmp_cksum);
1853 		if ((offset + sizeof(u_int16_t)) > m->m_len)
1854 			m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT);
1855 		else
1856 			*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
1857 	}
1858 
1859 	if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) {
1860 		if (!in_ifcap_cksum(m, ifp, IFCAP_CSUM_TCPv4) ||
1861 		    ip->ip_hl != 5) {
1862 			tcpstat_inc(tcps_outswcsum);
1863 			in_delayed_cksum(m);
1864 			m->m_pkthdr.csum_flags &= ~M_TCP_CSUM_OUT; /* Clear */
1865 		}
1866 	} else if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) {
1867 		if (!in_ifcap_cksum(m, ifp, IFCAP_CSUM_UDPv4) ||
1868 		    ip->ip_hl != 5) {
1869 			udpstat_inc(udps_outswcsum);
1870 			in_delayed_cksum(m);
1871 			m->m_pkthdr.csum_flags &= ~M_UDP_CSUM_OUT; /* Clear */
1872 		}
1873 	} else if (m->m_pkthdr.csum_flags & M_ICMP_CSUM_OUT) {
1874 		in_delayed_cksum(m);
1875 		m->m_pkthdr.csum_flags &= ~M_ICMP_CSUM_OUT; /* Clear */
1876 	}
1877 }
1878 
1879 int
1880 in_ifcap_cksum(struct mbuf *m, struct ifnet *ifp, int ifcap)
1881 {
1882 	if ((ifp == NULL) ||
1883 	    !ISSET(ifp->if_capabilities, ifcap) ||
1884 	    (ifp->if_bridgeidx != 0))
1885 		return (0);
1886 	/*
1887 	 * Simplex interface sends packet back without hardware cksum.
1888 	 * Keep this check in sync with the condition where ether_resolve()
1889 	 * calls if_input_local().
1890 	 */
1891 	if (ISSET(m->m_flags, M_BCAST) &&
1892 	    ISSET(ifp->if_flags, IFF_SIMPLEX) &&
1893 	    !m->m_pkthdr.pf.routed)
1894 		return (0);
1895 	return (1);
1896 }
1897