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