xref: /freebsd/sys/netinet/ip_output.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1988, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
32  */
33 
34 #include <sys/cdefs.h>
35 #include "opt_inet.h"
36 #include "opt_ipsec.h"
37 #include "opt_kern_tls.h"
38 #include "opt_mbuf_stress_test.h"
39 #include "opt_ratelimit.h"
40 #include "opt_route.h"
41 #include "opt_rss.h"
42 #include "opt_sctp.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/ktls.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/protosw.h>
54 #include <sys/sdt.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/ucred.h>
59 
60 #include <net/if.h>
61 #include <net/if_var.h>
62 #include <net/if_private.h>
63 #include <net/if_vlan_var.h>
64 #include <net/if_llatbl.h>
65 #include <net/ethernet.h>
66 #include <net/netisr.h>
67 #include <net/pfil.h>
68 #include <net/route.h>
69 #include <net/route/nhop.h>
70 #include <net/rss_config.h>
71 #include <net/vnet.h>
72 
73 #include <netinet/in.h>
74 #include <netinet/in_fib.h>
75 #include <netinet/in_kdtrace.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/in_fib.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/in_rss.h>
81 #include <netinet/in_var.h>
82 #include <netinet/ip_var.h>
83 #include <netinet/ip_options.h>
84 
85 #include <netinet/udp.h>
86 #include <netinet/udp_var.h>
87 
88 #if defined(SCTP) || defined(SCTP_SUPPORT)
89 #include <netinet/sctp.h>
90 #include <netinet/sctp_crc32.h>
91 #endif
92 
93 #include <netipsec/ipsec_support.h>
94 
95 #include <machine/in_cksum.h>
96 
97 #include <security/mac/mac_framework.h>
98 
99 #ifdef MBUF_STRESS_TEST
100 static int mbuf_frag_size = 0;
101 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
102 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
103 #endif
104 
105 static void	ip_mloopback(struct ifnet *, const struct mbuf *, int);
106 
107 extern int in_mcast_loop;
108 
109 static inline int
110 ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, int flags,
111     struct inpcb *inp, struct sockaddr_in *dst, int *fibnum, int *error)
112 {
113 	struct m_tag *fwd_tag = NULL;
114 	struct mbuf *m;
115 	struct in_addr odst;
116 	struct ip *ip;
117 
118 	m = *mp;
119 	ip = mtod(m, struct ip *);
120 
121 	/* Run through list of hooks for output packets. */
122 	odst.s_addr = ip->ip_dst.s_addr;
123 	switch (pfil_mbuf_out(V_inet_pfil_head, mp, ifp, inp)) {
124 	case PFIL_DROPPED:
125 		*error = EACCES;
126 		/* FALLTHROUGH */
127 	case PFIL_CONSUMED:
128 		return 1; /* Finished */
129 	case PFIL_PASS:
130 		*error = 0;
131 	}
132 	m = *mp;
133 	ip = mtod(m, struct ip *);
134 
135 	/* See if destination IP address was changed by packet filter. */
136 	if (odst.s_addr != ip->ip_dst.s_addr) {
137 		m->m_flags |= M_SKIP_FIREWALL;
138 		/* If destination is now ourself drop to ip_input(). */
139 		if (in_localip(ip->ip_dst)) {
140 			m->m_flags |= M_FASTFWD_OURS;
141 			if (m->m_pkthdr.rcvif == NULL)
142 				m->m_pkthdr.rcvif = V_loif;
143 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
144 				m->m_pkthdr.csum_flags |=
145 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
146 				m->m_pkthdr.csum_data = 0xffff;
147 			}
148 			m->m_pkthdr.csum_flags |=
149 				CSUM_IP_CHECKED | CSUM_IP_VALID;
150 #if defined(SCTP) || defined(SCTP_SUPPORT)
151 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
152 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
153 #endif
154 			*error = netisr_queue(NETISR_IP, m);
155 			return 1; /* Finished */
156 		}
157 
158 		bzero(dst, sizeof(*dst));
159 		dst->sin_family = AF_INET;
160 		dst->sin_len = sizeof(*dst);
161 		dst->sin_addr = ip->ip_dst;
162 
163 		return -1; /* Reloop */
164 	}
165 	/* See if fib was changed by packet filter. */
166 	if ((*fibnum) != M_GETFIB(m)) {
167 		m->m_flags |= M_SKIP_FIREWALL;
168 		*fibnum = M_GETFIB(m);
169 		return -1; /* Reloop for FIB change */
170 	}
171 
172 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
173 	if (m->m_flags & M_FASTFWD_OURS) {
174 		if (m->m_pkthdr.rcvif == NULL)
175 			m->m_pkthdr.rcvif = V_loif;
176 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
177 			m->m_pkthdr.csum_flags |=
178 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
179 			m->m_pkthdr.csum_data = 0xffff;
180 		}
181 #if defined(SCTP) || defined(SCTP_SUPPORT)
182 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
183 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
184 #endif
185 		m->m_pkthdr.csum_flags |=
186 			CSUM_IP_CHECKED | CSUM_IP_VALID;
187 
188 		*error = netisr_queue(NETISR_IP, m);
189 		return 1; /* Finished */
190 	}
191 	/* Or forward to some other address? */
192 	if ((m->m_flags & M_IP_NEXTHOP) &&
193 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
194 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
195 		m->m_flags |= M_SKIP_FIREWALL;
196 		m->m_flags &= ~M_IP_NEXTHOP;
197 		m_tag_delete(m, fwd_tag);
198 
199 		return -1; /* Reloop for CHANGE of dst */
200 	}
201 
202 	return 0;
203 }
204 
205 static int
206 ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m,
207     const struct sockaddr *gw, struct route *ro, bool stamp_tag)
208 {
209 #ifdef KERN_TLS
210 	struct ktls_session *tls = NULL;
211 #endif
212 	struct m_snd_tag *mst;
213 	int error;
214 
215 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
216 	mst = NULL;
217 
218 #ifdef KERN_TLS
219 	/*
220 	 * If this is an unencrypted TLS record, save a reference to
221 	 * the record.  This local reference is used to call
222 	 * ktls_output_eagain after the mbuf has been freed (thus
223 	 * dropping the mbuf's reference) in if_output.
224 	 */
225 	if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
226 		tls = ktls_hold(m->m_next->m_epg_tls);
227 		mst = tls->snd_tag;
228 
229 		/*
230 		 * If a TLS session doesn't have a valid tag, it must
231 		 * have had an earlier ifp mismatch, so drop this
232 		 * packet.
233 		 */
234 		if (mst == NULL) {
235 			m_freem(m);
236 			error = EAGAIN;
237 			goto done;
238 		}
239 		/*
240 		 * Always stamp tags that include NIC ktls.
241 		 */
242 		stamp_tag = true;
243 	}
244 #endif
245 #ifdef RATELIMIT
246 	if (inp != NULL && mst == NULL) {
247 		if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
248 		    (inp->inp_snd_tag != NULL &&
249 		    inp->inp_snd_tag->ifp != ifp))
250 			in_pcboutput_txrtlmt(inp, ifp, m);
251 
252 		if (inp->inp_snd_tag != NULL)
253 			mst = inp->inp_snd_tag;
254 	}
255 #endif
256 	if (stamp_tag && mst != NULL) {
257 		KASSERT(m->m_pkthdr.rcvif == NULL,
258 		    ("trying to add a send tag to a forwarded packet"));
259 		if (mst->ifp != ifp) {
260 			m_freem(m);
261 			error = EAGAIN;
262 			goto done;
263 		}
264 
265 		/* stamp send tag on mbuf */
266 		m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
267 		m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
268 	}
269 
270 	error = (*ifp->if_output)(ifp, m, gw, ro);
271 
272 done:
273 	/* Check for route change invalidating send tags. */
274 #ifdef KERN_TLS
275 	if (tls != NULL) {
276 		if (error == EAGAIN)
277 			error = ktls_output_eagain(inp, tls);
278 		ktls_free(tls);
279 	}
280 #endif
281 #ifdef RATELIMIT
282 	if (error == EAGAIN)
283 		in_pcboutput_eagain(inp);
284 #endif
285 	return (error);
286 }
287 
288 /* rte<>ro_flags translation */
289 static inline void
290 rt_update_ro_flags(struct route *ro, const struct nhop_object *nh)
291 {
292 	int nh_flags = nh->nh_flags;
293 
294 	ro->ro_flags &= ~ (RT_REJECT|RT_BLACKHOLE|RT_HAS_GW);
295 
296 	ro->ro_flags |= (nh_flags & NHF_REJECT) ? RT_REJECT : 0;
297 	ro->ro_flags |= (nh_flags & NHF_BLACKHOLE) ? RT_BLACKHOLE : 0;
298 	ro->ro_flags |= (nh_flags & NHF_GATEWAY) ? RT_HAS_GW : 0;
299 }
300 
301 /*
302  * IP output.  The packet in mbuf chain m contains a skeletal IP
303  * header (with len, off, ttl, proto, tos, src, dst).
304  * The mbuf chain containing the packet will be freed.
305  * The mbuf opt, if present, will not be freed.
306  * If route ro is present and has ro_rt initialized, route lookup would be
307  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
308  * then result of route lookup is stored in ro->ro_rt.
309  *
310  * In the IP forwarding case, the packet will arrive with options already
311  * inserted, so must have a NULL opt pointer.
312  */
313 int
314 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
315     struct ip_moptions *imo, struct inpcb *inp)
316 {
317 	struct ip *ip;
318 	struct ifnet *ifp = NULL;	/* keep compiler happy */
319 	struct mbuf *m0;
320 	int hlen = sizeof (struct ip);
321 	int mtu = 0;
322 	int error = 0;
323 	int vlan_pcp = -1;
324 	struct sockaddr_in *dst;
325 	const struct sockaddr *gw;
326 	struct in_ifaddr *ia = NULL;
327 	struct in_addr src;
328 	int isbroadcast;
329 	uint16_t ip_len, ip_off;
330 	struct route iproute;
331 	uint32_t fibnum;
332 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
333 	int no_route_but_check_spd = 0;
334 #endif
335 
336 	M_ASSERTPKTHDR(m);
337 	NET_EPOCH_ASSERT();
338 
339 	if (inp != NULL) {
340 		INP_LOCK_ASSERT(inp);
341 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
342 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
343 			m->m_pkthdr.flowid = inp->inp_flowid;
344 			M_HASHTYPE_SET(m, inp->inp_flowtype);
345 		}
346 		if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
347 			vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
348 			    INP_2PCP_SHIFT;
349 #ifdef NUMA
350 		m->m_pkthdr.numa_domain = inp->inp_numa_domain;
351 #endif
352 	}
353 
354 	if (opt) {
355 		int len = 0;
356 		m = ip_insertoptions(m, opt, &len);
357 		if (len != 0)
358 			hlen = len; /* ip->ip_hl is updated above */
359 	}
360 	ip = mtod(m, struct ip *);
361 	ip_len = ntohs(ip->ip_len);
362 	ip_off = ntohs(ip->ip_off);
363 
364 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
365 		ip->ip_v = IPVERSION;
366 		ip->ip_hl = hlen >> 2;
367 		ip_fillid(ip);
368 	} else {
369 		/* Header already set, fetch hlen from there */
370 		hlen = ip->ip_hl << 2;
371 	}
372 	if ((flags & IP_FORWARDING) == 0)
373 		IPSTAT_INC(ips_localout);
374 
375 	/*
376 	 * dst/gw handling:
377 	 *
378 	 * gw is readonly but can point either to dst OR rt_gateway,
379 	 * therefore we need restore gw if we're redoing lookup.
380 	 */
381 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
382 	if (ro == NULL) {
383 		ro = &iproute;
384 		bzero(ro, sizeof (*ro));
385 	}
386 	dst = (struct sockaddr_in *)&ro->ro_dst;
387 	if (ro->ro_nh == NULL) {
388 		dst->sin_family = AF_INET;
389 		dst->sin_len = sizeof(*dst);
390 		dst->sin_addr = ip->ip_dst;
391 	}
392 	gw = (const struct sockaddr *)dst;
393 again:
394 	/*
395 	 * Validate route against routing table additions;
396 	 * a better/more specific route might have been added.
397 	 */
398 	if (inp != NULL && ro->ro_nh != NULL)
399 		NH_VALIDATE(ro, &inp->inp_rt_cookie, fibnum);
400 	/*
401 	 * If there is a cached route,
402 	 * check that it is to the same destination
403 	 * and is still up.  If not, free it and try again.
404 	 * The address family should also be checked in case of sharing the
405 	 * cache with IPv6.
406 	 * Also check whether routing cache needs invalidation.
407 	 */
408 	if (ro->ro_nh != NULL &&
409 	    ((!NH_IS_VALID(ro->ro_nh)) || dst->sin_family != AF_INET ||
410 	    dst->sin_addr.s_addr != ip->ip_dst.s_addr))
411 		RO_INVALIDATE_CACHE(ro);
412 	ia = NULL;
413 	/*
414 	 * If routing to interface only, short circuit routing lookup.
415 	 * The use of an all-ones broadcast address implies this; an
416 	 * interface is specified by the broadcast address of an interface,
417 	 * or the destination address of a ptp interface.
418 	 */
419 	if (flags & IP_SENDONES) {
420 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
421 						      M_GETFIB(m)))) == NULL &&
422 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
423 						    M_GETFIB(m)))) == NULL) {
424 			IPSTAT_INC(ips_noroute);
425 			error = ENETUNREACH;
426 			goto bad;
427 		}
428 		ip->ip_dst.s_addr = INADDR_BROADCAST;
429 		dst->sin_addr = ip->ip_dst;
430 		ifp = ia->ia_ifp;
431 		mtu = ifp->if_mtu;
432 		ip->ip_ttl = 1;
433 		isbroadcast = 1;
434 		src = IA_SIN(ia)->sin_addr;
435 	} else if (flags & IP_ROUTETOIF) {
436 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
437 						    M_GETFIB(m)))) == NULL &&
438 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
439 						M_GETFIB(m)))) == NULL) {
440 			IPSTAT_INC(ips_noroute);
441 			error = ENETUNREACH;
442 			goto bad;
443 		}
444 		ifp = ia->ia_ifp;
445 		mtu = ifp->if_mtu;
446 		ip->ip_ttl = 1;
447 		isbroadcast = ifp->if_flags & IFF_BROADCAST ?
448 		    in_ifaddr_broadcast(dst->sin_addr, ia) : 0;
449 		src = IA_SIN(ia)->sin_addr;
450 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
451 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
452 		/*
453 		 * Bypass the normal routing lookup for multicast
454 		 * packets if the interface is specified.
455 		 */
456 		ifp = imo->imo_multicast_ifp;
457 		mtu = ifp->if_mtu;
458 		IFP_TO_IA(ifp, ia);
459 		isbroadcast = 0;	/* fool gcc */
460 		/* Interface may have no addresses. */
461 		if (ia != NULL)
462 			src = IA_SIN(ia)->sin_addr;
463 		else
464 			src.s_addr = INADDR_ANY;
465 	} else if (ro != &iproute) {
466 		if (ro->ro_nh == NULL) {
467 			/*
468 			 * We want to do any cloning requested by the link
469 			 * layer, as this is probably required in all cases
470 			 * for correct operation (as it is for ARP).
471 			 */
472 			uint32_t flowid;
473 			flowid = m->m_pkthdr.flowid;
474 			ro->ro_nh = fib4_lookup(fibnum, dst->sin_addr, 0,
475 			    NHR_REF, flowid);
476 
477 			if (ro->ro_nh == NULL || (!NH_IS_VALID(ro->ro_nh))) {
478 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
479 				/*
480 				 * There is no route for this packet, but it is
481 				 * possible that a matching SPD entry exists.
482 				 */
483 				no_route_but_check_spd = 1;
484 				goto sendit;
485 #endif
486 				IPSTAT_INC(ips_noroute);
487 				error = EHOSTUNREACH;
488 				goto bad;
489 			}
490 		}
491 		struct nhop_object *nh = ro->ro_nh;
492 
493 		ia = ifatoia(nh->nh_ifa);
494 		ifp = nh->nh_ifp;
495 		counter_u64_add(nh->nh_pksent, 1);
496 		rt_update_ro_flags(ro, nh);
497 		if (nh->nh_flags & NHF_GATEWAY)
498 			gw = &nh->gw_sa;
499 		if (nh->nh_flags & NHF_HOST)
500 			isbroadcast = (nh->nh_flags & NHF_BROADCAST);
501 		else if ((ifp->if_flags & IFF_BROADCAST) && (gw->sa_family == AF_INET))
502 			isbroadcast = in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia);
503 		else
504 			isbroadcast = 0;
505 		mtu = nh->nh_mtu;
506 		src = IA_SIN(ia)->sin_addr;
507 	} else {
508 		struct nhop_object *nh;
509 
510 		nh = fib4_lookup(M_GETFIB(m), dst->sin_addr, 0, NHR_NONE,
511 		    m->m_pkthdr.flowid);
512 		if (nh == NULL) {
513 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
514 			/*
515 			 * There is no route for this packet, but it is
516 			 * possible that a matching SPD entry exists.
517 			 */
518 			no_route_but_check_spd = 1;
519 			goto sendit;
520 #endif
521 			IPSTAT_INC(ips_noroute);
522 			error = EHOSTUNREACH;
523 			goto bad;
524 		}
525 		ifp = nh->nh_ifp;
526 		mtu = nh->nh_mtu;
527 		rt_update_ro_flags(ro, nh);
528 		if (nh->nh_flags & NHF_GATEWAY)
529 			gw = &nh->gw_sa;
530 		ia = ifatoia(nh->nh_ifa);
531 		src = IA_SIN(ia)->sin_addr;
532 		isbroadcast = (((nh->nh_flags & (NHF_HOST | NHF_BROADCAST)) ==
533 		    (NHF_HOST | NHF_BROADCAST)) ||
534 		    ((ifp->if_flags & IFF_BROADCAST) &&
535 		    (gw->sa_family == AF_INET) &&
536 		    in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia)));
537 	}
538 
539 	/* Catch a possible divide by zero later. */
540 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, ro=%p (nh_flags=0x%08x) ifp=%p",
541 	    __func__, mtu, ro,
542 	    (ro != NULL && ro->ro_nh != NULL) ? ro->ro_nh->nh_flags : 0, ifp));
543 
544 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
545 		m->m_flags |= M_MCAST;
546 		/*
547 		 * IP destination address is multicast.  Make sure "gw"
548 		 * still points to the address in "ro".  (It may have been
549 		 * changed to point to a gateway address, above.)
550 		 */
551 		gw = (const struct sockaddr *)dst;
552 		/*
553 		 * See if the caller provided any multicast options
554 		 */
555 		if (imo != NULL) {
556 			ip->ip_ttl = imo->imo_multicast_ttl;
557 			if (imo->imo_multicast_vif != -1)
558 				ip->ip_src.s_addr =
559 				    ip_mcast_src ?
560 				    ip_mcast_src(imo->imo_multicast_vif) :
561 				    INADDR_ANY;
562 		} else
563 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
564 		/*
565 		 * Confirm that the outgoing interface supports multicast.
566 		 */
567 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
568 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
569 				IPSTAT_INC(ips_noroute);
570 				error = ENETUNREACH;
571 				goto bad;
572 			}
573 		}
574 		/*
575 		 * If source address not specified yet, use address
576 		 * of outgoing interface.
577 		 */
578 		if (ip->ip_src.s_addr == INADDR_ANY)
579 			ip->ip_src = src;
580 
581 		if ((imo == NULL && in_mcast_loop) ||
582 		    (imo && imo->imo_multicast_loop)) {
583 			/*
584 			 * Loop back multicast datagram if not expressly
585 			 * forbidden to do so, even if we are not a member
586 			 * of the group; ip_input() will filter it later,
587 			 * thus deferring a hash lookup and mutex acquisition
588 			 * at the expense of a cheap copy using m_copym().
589 			 */
590 			ip_mloopback(ifp, m, hlen);
591 		} else {
592 			/*
593 			 * If we are acting as a multicast router, perform
594 			 * multicast forwarding as if the packet had just
595 			 * arrived on the interface to which we are about
596 			 * to send.  The multicast forwarding function
597 			 * recursively calls this function, using the
598 			 * IP_FORWARDING flag to prevent infinite recursion.
599 			 *
600 			 * Multicasts that are looped back by ip_mloopback(),
601 			 * above, will be forwarded by the ip_input() routine,
602 			 * if necessary.
603 			 */
604 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
605 				/*
606 				 * If rsvp daemon is not running, do not
607 				 * set ip_moptions. This ensures that the packet
608 				 * is multicast and not just sent down one link
609 				 * as prescribed by rsvpd.
610 				 */
611 				if (!V_rsvp_on)
612 					imo = NULL;
613 				if (ip_mforward &&
614 				    ip_mforward(ip, ifp, m, imo) != 0) {
615 					m_freem(m);
616 					goto done;
617 				}
618 			}
619 		}
620 
621 		/*
622 		 * Multicasts with a time-to-live of zero may be looped-
623 		 * back, above, but must not be transmitted on a network.
624 		 * Also, multicasts addressed to the loopback interface
625 		 * are not sent -- the above call to ip_mloopback() will
626 		 * loop back a copy. ip_input() will drop the copy if
627 		 * this host does not belong to the destination group on
628 		 * the loopback interface.
629 		 */
630 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
631 			m_freem(m);
632 			goto done;
633 		}
634 
635 		goto sendit;
636 	}
637 
638 	/*
639 	 * If the source address is not specified yet, use the address
640 	 * of the outoing interface.
641 	 */
642 	if (ip->ip_src.s_addr == INADDR_ANY)
643 		ip->ip_src = src;
644 
645 	/*
646 	 * Look for broadcast address and
647 	 * verify user is allowed to send
648 	 * such a packet.
649 	 */
650 	if (isbroadcast) {
651 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
652 			error = EADDRNOTAVAIL;
653 			goto bad;
654 		}
655 		if ((flags & IP_ALLOWBROADCAST) == 0) {
656 			error = EACCES;
657 			goto bad;
658 		}
659 		/* don't allow broadcast messages to be fragmented */
660 		if (ip_len > mtu) {
661 			error = EMSGSIZE;
662 			goto bad;
663 		}
664 		m->m_flags |= M_BCAST;
665 	} else {
666 		m->m_flags &= ~M_BCAST;
667 	}
668 
669 sendit:
670 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
671 	if (IPSEC_ENABLED(ipv4)) {
672 		m = mb_unmapped_to_ext(m);
673 		if (m == NULL) {
674 			IPSTAT_INC(ips_odropped);
675 			error = ENOBUFS;
676 			goto bad;
677 		}
678 		if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) {
679 			if (error == EINPROGRESS)
680 				error = 0;
681 			goto done;
682 		}
683 	}
684 	/*
685 	 * Check if there was a route for this packet; return error if not.
686 	 */
687 	if (no_route_but_check_spd) {
688 		IPSTAT_INC(ips_noroute);
689 		error = EHOSTUNREACH;
690 		goto bad;
691 	}
692 	/* Update variables that are affected by ipsec4_output(). */
693 	ip = mtod(m, struct ip *);
694 	hlen = ip->ip_hl << 2;
695 #endif /* IPSEC */
696 
697 	/* Jump over all PFIL processing if hooks are not active. */
698 	if (PFIL_HOOKED_OUT(V_inet_pfil_head)) {
699 		switch (ip_output_pfil(&m, ifp, flags, inp, dst, &fibnum,
700 		    &error)) {
701 		case 1: /* Finished */
702 			goto done;
703 
704 		case 0: /* Continue normally */
705 			ip = mtod(m, struct ip *);
706 			ip_len = ntohs(ip->ip_len);
707 			break;
708 
709 		case -1: /* Need to try again */
710 			/* Reset everything for a new round */
711 			if (ro != NULL) {
712 				RO_NHFREE(ro);
713 				ro->ro_prepend = NULL;
714 			}
715 			gw = (const struct sockaddr *)dst;
716 			ip = mtod(m, struct ip *);
717 			goto again;
718 		}
719 	}
720 
721 	if (vlan_pcp > -1)
722 		EVL_APPLY_PRI(m, vlan_pcp);
723 
724 	/* IN_LOOPBACK must not appear on the wire - RFC1122. */
725 	if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) ||
726 	    IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) {
727 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
728 			IPSTAT_INC(ips_badaddr);
729 			error = EADDRNOTAVAIL;
730 			goto bad;
731 		}
732 	}
733 
734 	/* Ensure the packet data is mapped if the interface requires it. */
735 	if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
736 		m = mb_unmapped_to_ext(m);
737 		if (m == NULL) {
738 			IPSTAT_INC(ips_odropped);
739 			error = ENOBUFS;
740 			goto bad;
741 		}
742 	}
743 
744 	m->m_pkthdr.csum_flags |= CSUM_IP;
745 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
746 		in_delayed_cksum(m);
747 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
748 	}
749 #if defined(SCTP) || defined(SCTP_SUPPORT)
750 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
751 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
752 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
753 	}
754 #endif
755 
756 	/*
757 	 * If small enough for interface, or the interface will take
758 	 * care of the fragmentation for us, we can just send directly.
759 	 * Note that if_vxlan could have requested TSO even though the outer
760 	 * frame is UDP.  It is correct to not fragment such datagrams and
761 	 * instead just pass them on to the driver.
762 	 */
763 	if (ip_len <= mtu ||
764 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist &
765 	    (CSUM_TSO | CSUM_INNER_TSO)) != 0) {
766 		ip->ip_sum = 0;
767 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
768 			ip->ip_sum = in_cksum(m, hlen);
769 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
770 		}
771 
772 		/*
773 		 * Record statistics for this interface address.
774 		 * With CSUM_TSO the byte/packet count will be slightly
775 		 * incorrect because we count the IP+TCP headers only
776 		 * once instead of for every generated packet.
777 		 */
778 		if (!(flags & IP_FORWARDING) && ia) {
779 			if (m->m_pkthdr.csum_flags &
780 			    (CSUM_TSO | CSUM_INNER_TSO))
781 				counter_u64_add(ia->ia_ifa.ifa_opackets,
782 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
783 			else
784 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
785 
786 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
787 		}
788 #ifdef MBUF_STRESS_TEST
789 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
790 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
791 #endif
792 		/*
793 		 * Reset layer specific mbuf flags
794 		 * to avoid confusing lower layers.
795 		 */
796 		m_clrprotoflags(m);
797 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
798 		error = ip_output_send(inp, ifp, m, gw, ro,
799 		    (flags & IP_NO_SND_TAG_RL) ? false : true);
800 		goto done;
801 	}
802 
803 	/* Balk when DF bit is set or the interface didn't support TSO. */
804 	if ((ip_off & IP_DF) ||
805 	    (m->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_INNER_TSO))) {
806 		error = EMSGSIZE;
807 		IPSTAT_INC(ips_cantfrag);
808 		goto bad;
809 	}
810 
811 	/*
812 	 * Too large for interface; fragment if possible. If successful,
813 	 * on return, m will point to a list of packets to be sent.
814 	 */
815 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
816 	if (error)
817 		goto bad;
818 	for (; m; m = m0) {
819 		m0 = m->m_nextpkt;
820 		m->m_nextpkt = 0;
821 		if (error == 0) {
822 			/* Record statistics for this interface address. */
823 			if (ia != NULL) {
824 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
825 				counter_u64_add(ia->ia_ifa.ifa_obytes,
826 				    m->m_pkthdr.len);
827 			}
828 			/*
829 			 * Reset layer specific mbuf flags
830 			 * to avoid confusing upper layers.
831 			 */
832 			m_clrprotoflags(m);
833 
834 			IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp,
835 			    mtod(m, struct ip *), NULL);
836 			error = ip_output_send(inp, ifp, m, gw, ro, true);
837 		} else
838 			m_freem(m);
839 	}
840 
841 	if (error == 0)
842 		IPSTAT_INC(ips_fragmented);
843 
844 done:
845 	return (error);
846  bad:
847 	m_freem(m);
848 	goto done;
849 }
850 
851 /*
852  * Create a chain of fragments which fit the given mtu. m_frag points to the
853  * mbuf to be fragmented; on return it points to the chain with the fragments.
854  * Return 0 if no error. If error, m_frag may contain a partially built
855  * chain of fragments that should be freed by the caller.
856  *
857  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
858  */
859 int
860 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
861     u_long if_hwassist_flags)
862 {
863 	int error = 0;
864 	int hlen = ip->ip_hl << 2;
865 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
866 	int off;
867 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
868 	int firstlen;
869 	struct mbuf **mnext;
870 	int nfrags;
871 	uint16_t ip_len, ip_off;
872 
873 	ip_len = ntohs(ip->ip_len);
874 	ip_off = ntohs(ip->ip_off);
875 
876 	/*
877 	 * Packet shall not have "Don't Fragment" flag and have at least 8
878 	 * bytes of payload.
879 	 */
880 	if (__predict_false((ip_off & IP_DF) || len < 8)) {
881 		IPSTAT_INC(ips_cantfrag);
882 		return (EMSGSIZE);
883 	}
884 
885 	/*
886 	 * If the interface will not calculate checksums on
887 	 * fragmented packets, then do it here.
888 	 */
889 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
890 		in_delayed_cksum(m0);
891 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
892 	}
893 #if defined(SCTP) || defined(SCTP_SUPPORT)
894 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
895 		sctp_delayed_cksum(m0, hlen);
896 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
897 	}
898 #endif
899 	if (len > PAGE_SIZE) {
900 		/*
901 		 * Fragment large datagrams such that each segment
902 		 * contains a multiple of PAGE_SIZE amount of data,
903 		 * plus headers. This enables a receiver to perform
904 		 * page-flipping zero-copy optimizations.
905 		 *
906 		 * XXX When does this help given that sender and receiver
907 		 * could have different page sizes, and also mtu could
908 		 * be less than the receiver's page size ?
909 		 */
910 		int newlen;
911 
912 		off = MIN(mtu, m0->m_pkthdr.len);
913 
914 		/*
915 		 * firstlen (off - hlen) must be aligned on an
916 		 * 8-byte boundary
917 		 */
918 		if (off < hlen)
919 			goto smart_frag_failure;
920 		off = ((off - hlen) & ~7) + hlen;
921 		newlen = (~PAGE_MASK) & mtu;
922 		if ((newlen + sizeof (struct ip)) > mtu) {
923 			/* we failed, go back the default */
924 smart_frag_failure:
925 			newlen = len;
926 			off = hlen + len;
927 		}
928 		len = newlen;
929 
930 	} else {
931 		off = hlen + len;
932 	}
933 
934 	firstlen = off - hlen;
935 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
936 
937 	/*
938 	 * Loop through length of segment after first fragment,
939 	 * make new header and copy data of each part and link onto chain.
940 	 * Here, m0 is the original packet, m is the fragment being created.
941 	 * The fragments are linked off the m_nextpkt of the original
942 	 * packet, which after processing serves as the first fragment.
943 	 */
944 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
945 		struct ip *mhip;	/* ip header on the fragment */
946 		struct mbuf *m;
947 		int mhlen = sizeof (struct ip);
948 
949 		m = m_gethdr(M_NOWAIT, MT_DATA);
950 		if (m == NULL) {
951 			error = ENOBUFS;
952 			IPSTAT_INC(ips_odropped);
953 			goto done;
954 		}
955 		/*
956 		 * Make sure the complete packet header gets copied
957 		 * from the originating mbuf to the newly created
958 		 * mbuf. This also ensures that existing firewall
959 		 * classification(s), VLAN tags and so on get copied
960 		 * to the resulting fragmented packet(s):
961 		 */
962 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
963 			m_free(m);
964 			error = ENOBUFS;
965 			IPSTAT_INC(ips_odropped);
966 			goto done;
967 		}
968 		/*
969 		 * In the first mbuf, leave room for the link header, then
970 		 * copy the original IP header including options. The payload
971 		 * goes into an additional mbuf chain returned by m_copym().
972 		 */
973 		m->m_data += max_linkhdr;
974 		mhip = mtod(m, struct ip *);
975 		*mhip = *ip;
976 		if (hlen > sizeof (struct ip)) {
977 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
978 			mhip->ip_v = IPVERSION;
979 			mhip->ip_hl = mhlen >> 2;
980 		}
981 		m->m_len = mhlen;
982 		/* XXX do we need to add ip_off below ? */
983 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
984 		if (off + len >= ip_len)
985 			len = ip_len - off;
986 		else
987 			mhip->ip_off |= IP_MF;
988 		mhip->ip_len = htons((u_short)(len + mhlen));
989 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
990 		if (m->m_next == NULL) {	/* copy failed */
991 			m_free(m);
992 			error = ENOBUFS;	/* ??? */
993 			IPSTAT_INC(ips_odropped);
994 			goto done;
995 		}
996 		m->m_pkthdr.len = mhlen + len;
997 #ifdef MAC
998 		mac_netinet_fragment(m0, m);
999 #endif
1000 		mhip->ip_off = htons(mhip->ip_off);
1001 		mhip->ip_sum = 0;
1002 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
1003 			mhip->ip_sum = in_cksum(m, mhlen);
1004 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
1005 		}
1006 		*mnext = m;
1007 		mnext = &m->m_nextpkt;
1008 	}
1009 	IPSTAT_ADD(ips_ofragments, nfrags);
1010 
1011 	/*
1012 	 * Update first fragment by trimming what's been copied out
1013 	 * and updating header.
1014 	 */
1015 	m_adj(m0, hlen + firstlen - ip_len);
1016 	m0->m_pkthdr.len = hlen + firstlen;
1017 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
1018 	ip->ip_off = htons(ip_off | IP_MF);
1019 	ip->ip_sum = 0;
1020 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
1021 		ip->ip_sum = in_cksum(m0, hlen);
1022 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
1023 	}
1024 
1025 done:
1026 	*m_frag = m0;
1027 	return error;
1028 }
1029 
1030 void
1031 in_delayed_cksum(struct mbuf *m)
1032 {
1033 	struct ip *ip;
1034 	struct udphdr *uh;
1035 	uint16_t cklen, csum, offset;
1036 
1037 	ip = mtod(m, struct ip *);
1038 	offset = ip->ip_hl << 2 ;
1039 
1040 	if (m->m_pkthdr.csum_flags & CSUM_UDP) {
1041 		/* if udp header is not in the first mbuf copy udplen */
1042 		if (offset + sizeof(struct udphdr) > m->m_len) {
1043 			m_copydata(m, offset + offsetof(struct udphdr,
1044 			    uh_ulen), sizeof(cklen), (caddr_t)&cklen);
1045 			cklen = ntohs(cklen);
1046 		} else {
1047 			uh = (struct udphdr *)mtodo(m, offset);
1048 			cklen = ntohs(uh->uh_ulen);
1049 		}
1050 		csum = in_cksum_skip(m, cklen + offset, offset);
1051 		if (csum == 0)
1052 			csum = 0xffff;
1053 	} else {
1054 		cklen = ntohs(ip->ip_len);
1055 		csum = in_cksum_skip(m, cklen, offset);
1056 	}
1057 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1058 
1059 	if (offset + sizeof(csum) > m->m_len)
1060 		m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
1061 	else
1062 		*(u_short *)mtodo(m, offset) = csum;
1063 }
1064 
1065 /*
1066  * IP socket option processing.
1067  */
1068 int
1069 ip_ctloutput(struct socket *so, struct sockopt *sopt)
1070 {
1071 	struct inpcb *inp = sotoinpcb(so);
1072 	int	error, optval;
1073 #ifdef	RSS
1074 	uint32_t rss_bucket;
1075 	int retval;
1076 #endif
1077 
1078 	error = optval = 0;
1079 	if (sopt->sopt_level != IPPROTO_IP) {
1080 		error = EINVAL;
1081 
1082 		if (sopt->sopt_level == SOL_SOCKET &&
1083 		    sopt->sopt_dir == SOPT_SET) {
1084 			switch (sopt->sopt_name) {
1085 			case SO_SETFIB:
1086 				INP_WLOCK(inp);
1087 				inp->inp_inc.inc_fibnum = so->so_fibnum;
1088 				INP_WUNLOCK(inp);
1089 				error = 0;
1090 				break;
1091 			case SO_MAX_PACING_RATE:
1092 #ifdef RATELIMIT
1093 				INP_WLOCK(inp);
1094 				inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1095 				INP_WUNLOCK(inp);
1096 				error = 0;
1097 #else
1098 				error = EOPNOTSUPP;
1099 #endif
1100 				break;
1101 			default:
1102 				break;
1103 			}
1104 		}
1105 		return (error);
1106 	}
1107 
1108 	switch (sopt->sopt_dir) {
1109 	case SOPT_SET:
1110 		switch (sopt->sopt_name) {
1111 		case IP_OPTIONS:
1112 #ifdef notyet
1113 		case IP_RETOPTS:
1114 #endif
1115 		{
1116 			struct mbuf *m;
1117 			if (sopt->sopt_valsize > MLEN) {
1118 				error = EMSGSIZE;
1119 				break;
1120 			}
1121 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
1122 			if (m == NULL) {
1123 				error = ENOBUFS;
1124 				break;
1125 			}
1126 			m->m_len = sopt->sopt_valsize;
1127 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1128 					    m->m_len);
1129 			if (error) {
1130 				m_free(m);
1131 				break;
1132 			}
1133 			INP_WLOCK(inp);
1134 			error = ip_pcbopts(inp, sopt->sopt_name, m);
1135 			INP_WUNLOCK(inp);
1136 			return (error);
1137 		}
1138 
1139 		case IP_BINDANY:
1140 			if (sopt->sopt_td != NULL) {
1141 				error = priv_check(sopt->sopt_td,
1142 				    PRIV_NETINET_BINDANY);
1143 				if (error)
1144 					break;
1145 			}
1146 			/* FALLTHROUGH */
1147 		case IP_TOS:
1148 		case IP_TTL:
1149 		case IP_MINTTL:
1150 		case IP_RECVOPTS:
1151 		case IP_RECVRETOPTS:
1152 		case IP_ORIGDSTADDR:
1153 		case IP_RECVDSTADDR:
1154 		case IP_RECVTTL:
1155 		case IP_RECVIF:
1156 		case IP_ONESBCAST:
1157 		case IP_DONTFRAG:
1158 		case IP_RECVTOS:
1159 		case IP_RECVFLOWID:
1160 #ifdef	RSS
1161 		case IP_RECVRSSBUCKETID:
1162 #endif
1163 		case IP_VLAN_PCP:
1164 			error = sooptcopyin(sopt, &optval, sizeof optval,
1165 					    sizeof optval);
1166 			if (error)
1167 				break;
1168 
1169 			switch (sopt->sopt_name) {
1170 			case IP_TOS:
1171 				inp->inp_ip_tos = optval;
1172 				break;
1173 
1174 			case IP_TTL:
1175 				inp->inp_ip_ttl = optval;
1176 				break;
1177 
1178 			case IP_MINTTL:
1179 				if (optval >= 0 && optval <= MAXTTL)
1180 					inp->inp_ip_minttl = optval;
1181 				else
1182 					error = EINVAL;
1183 				break;
1184 
1185 #define	OPTSET(bit) do {						\
1186 	INP_WLOCK(inp);							\
1187 	if (optval)							\
1188 		inp->inp_flags |= bit;					\
1189 	else								\
1190 		inp->inp_flags &= ~bit;					\
1191 	INP_WUNLOCK(inp);						\
1192 } while (0)
1193 
1194 #define	OPTSET2(bit, val) do {						\
1195 	INP_WLOCK(inp);							\
1196 	if (val)							\
1197 		inp->inp_flags2 |= bit;					\
1198 	else								\
1199 		inp->inp_flags2 &= ~bit;				\
1200 	INP_WUNLOCK(inp);						\
1201 } while (0)
1202 
1203 			case IP_RECVOPTS:
1204 				OPTSET(INP_RECVOPTS);
1205 				break;
1206 
1207 			case IP_RECVRETOPTS:
1208 				OPTSET(INP_RECVRETOPTS);
1209 				break;
1210 
1211 			case IP_RECVDSTADDR:
1212 				OPTSET(INP_RECVDSTADDR);
1213 				break;
1214 
1215 			case IP_ORIGDSTADDR:
1216 				OPTSET2(INP_ORIGDSTADDR, optval);
1217 				break;
1218 
1219 			case IP_RECVTTL:
1220 				OPTSET(INP_RECVTTL);
1221 				break;
1222 
1223 			case IP_RECVIF:
1224 				OPTSET(INP_RECVIF);
1225 				break;
1226 
1227 			case IP_ONESBCAST:
1228 				OPTSET(INP_ONESBCAST);
1229 				break;
1230 			case IP_DONTFRAG:
1231 				OPTSET(INP_DONTFRAG);
1232 				break;
1233 			case IP_BINDANY:
1234 				OPTSET(INP_BINDANY);
1235 				break;
1236 			case IP_RECVTOS:
1237 				OPTSET(INP_RECVTOS);
1238 				break;
1239 			case IP_RECVFLOWID:
1240 				OPTSET2(INP_RECVFLOWID, optval);
1241 				break;
1242 #ifdef RSS
1243 			case IP_RECVRSSBUCKETID:
1244 				OPTSET2(INP_RECVRSSBUCKETID, optval);
1245 				break;
1246 #endif
1247 			case IP_VLAN_PCP:
1248 				if ((optval >= -1) && (optval <=
1249 				    (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1250 					if (optval == -1) {
1251 						INP_WLOCK(inp);
1252 						inp->inp_flags2 &=
1253 						    ~(INP_2PCP_SET |
1254 						      INP_2PCP_MASK);
1255 						INP_WUNLOCK(inp);
1256 					} else {
1257 						INP_WLOCK(inp);
1258 						inp->inp_flags2 |=
1259 						    INP_2PCP_SET;
1260 						inp->inp_flags2 &=
1261 						    ~INP_2PCP_MASK;
1262 						inp->inp_flags2 |=
1263 						    optval << INP_2PCP_SHIFT;
1264 						INP_WUNLOCK(inp);
1265 					}
1266 				} else
1267 					error = EINVAL;
1268 				break;
1269 			}
1270 			break;
1271 #undef OPTSET
1272 #undef OPTSET2
1273 
1274 		/*
1275 		 * Multicast socket options are processed by the in_mcast
1276 		 * module.
1277 		 */
1278 		case IP_MULTICAST_IF:
1279 		case IP_MULTICAST_VIF:
1280 		case IP_MULTICAST_TTL:
1281 		case IP_MULTICAST_LOOP:
1282 		case IP_ADD_MEMBERSHIP:
1283 		case IP_DROP_MEMBERSHIP:
1284 		case IP_ADD_SOURCE_MEMBERSHIP:
1285 		case IP_DROP_SOURCE_MEMBERSHIP:
1286 		case IP_BLOCK_SOURCE:
1287 		case IP_UNBLOCK_SOURCE:
1288 		case IP_MSFILTER:
1289 		case MCAST_JOIN_GROUP:
1290 		case MCAST_LEAVE_GROUP:
1291 		case MCAST_JOIN_SOURCE_GROUP:
1292 		case MCAST_LEAVE_SOURCE_GROUP:
1293 		case MCAST_BLOCK_SOURCE:
1294 		case MCAST_UNBLOCK_SOURCE:
1295 			error = inp_setmoptions(inp, sopt);
1296 			break;
1297 
1298 		case IP_PORTRANGE:
1299 			error = sooptcopyin(sopt, &optval, sizeof optval,
1300 					    sizeof optval);
1301 			if (error)
1302 				break;
1303 
1304 			INP_WLOCK(inp);
1305 			switch (optval) {
1306 			case IP_PORTRANGE_DEFAULT:
1307 				inp->inp_flags &= ~(INP_LOWPORT);
1308 				inp->inp_flags &= ~(INP_HIGHPORT);
1309 				break;
1310 
1311 			case IP_PORTRANGE_HIGH:
1312 				inp->inp_flags &= ~(INP_LOWPORT);
1313 				inp->inp_flags |= INP_HIGHPORT;
1314 				break;
1315 
1316 			case IP_PORTRANGE_LOW:
1317 				inp->inp_flags &= ~(INP_HIGHPORT);
1318 				inp->inp_flags |= INP_LOWPORT;
1319 				break;
1320 
1321 			default:
1322 				error = EINVAL;
1323 				break;
1324 			}
1325 			INP_WUNLOCK(inp);
1326 			break;
1327 
1328 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1329 		case IP_IPSEC_POLICY:
1330 			if (IPSEC_ENABLED(ipv4)) {
1331 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
1332 				break;
1333 			}
1334 			/* FALLTHROUGH */
1335 #endif /* IPSEC */
1336 
1337 		default:
1338 			error = ENOPROTOOPT;
1339 			break;
1340 		}
1341 		break;
1342 
1343 	case SOPT_GET:
1344 		switch (sopt->sopt_name) {
1345 		case IP_OPTIONS:
1346 		case IP_RETOPTS:
1347 			INP_RLOCK(inp);
1348 			if (inp->inp_options) {
1349 				struct mbuf *options;
1350 
1351 				options = m_copym(inp->inp_options, 0,
1352 				    M_COPYALL, M_NOWAIT);
1353 				INP_RUNLOCK(inp);
1354 				if (options != NULL) {
1355 					error = sooptcopyout(sopt,
1356 							     mtod(options, char *),
1357 							     options->m_len);
1358 					m_freem(options);
1359 				} else
1360 					error = ENOMEM;
1361 			} else {
1362 				INP_RUNLOCK(inp);
1363 				sopt->sopt_valsize = 0;
1364 			}
1365 			break;
1366 
1367 		case IP_TOS:
1368 		case IP_TTL:
1369 		case IP_MINTTL:
1370 		case IP_RECVOPTS:
1371 		case IP_RECVRETOPTS:
1372 		case IP_ORIGDSTADDR:
1373 		case IP_RECVDSTADDR:
1374 		case IP_RECVTTL:
1375 		case IP_RECVIF:
1376 		case IP_PORTRANGE:
1377 		case IP_ONESBCAST:
1378 		case IP_DONTFRAG:
1379 		case IP_BINDANY:
1380 		case IP_RECVTOS:
1381 		case IP_FLOWID:
1382 		case IP_FLOWTYPE:
1383 		case IP_RECVFLOWID:
1384 #ifdef	RSS
1385 		case IP_RSSBUCKETID:
1386 		case IP_RECVRSSBUCKETID:
1387 #endif
1388 		case IP_VLAN_PCP:
1389 			switch (sopt->sopt_name) {
1390 			case IP_TOS:
1391 				optval = inp->inp_ip_tos;
1392 				break;
1393 
1394 			case IP_TTL:
1395 				optval = inp->inp_ip_ttl;
1396 				break;
1397 
1398 			case IP_MINTTL:
1399 				optval = inp->inp_ip_minttl;
1400 				break;
1401 
1402 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1403 #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1404 
1405 			case IP_RECVOPTS:
1406 				optval = OPTBIT(INP_RECVOPTS);
1407 				break;
1408 
1409 			case IP_RECVRETOPTS:
1410 				optval = OPTBIT(INP_RECVRETOPTS);
1411 				break;
1412 
1413 			case IP_RECVDSTADDR:
1414 				optval = OPTBIT(INP_RECVDSTADDR);
1415 				break;
1416 
1417 			case IP_ORIGDSTADDR:
1418 				optval = OPTBIT2(INP_ORIGDSTADDR);
1419 				break;
1420 
1421 			case IP_RECVTTL:
1422 				optval = OPTBIT(INP_RECVTTL);
1423 				break;
1424 
1425 			case IP_RECVIF:
1426 				optval = OPTBIT(INP_RECVIF);
1427 				break;
1428 
1429 			case IP_PORTRANGE:
1430 				if (inp->inp_flags & INP_HIGHPORT)
1431 					optval = IP_PORTRANGE_HIGH;
1432 				else if (inp->inp_flags & INP_LOWPORT)
1433 					optval = IP_PORTRANGE_LOW;
1434 				else
1435 					optval = 0;
1436 				break;
1437 
1438 			case IP_ONESBCAST:
1439 				optval = OPTBIT(INP_ONESBCAST);
1440 				break;
1441 			case IP_DONTFRAG:
1442 				optval = OPTBIT(INP_DONTFRAG);
1443 				break;
1444 			case IP_BINDANY:
1445 				optval = OPTBIT(INP_BINDANY);
1446 				break;
1447 			case IP_RECVTOS:
1448 				optval = OPTBIT(INP_RECVTOS);
1449 				break;
1450 			case IP_FLOWID:
1451 				optval = inp->inp_flowid;
1452 				break;
1453 			case IP_FLOWTYPE:
1454 				optval = inp->inp_flowtype;
1455 				break;
1456 			case IP_RECVFLOWID:
1457 				optval = OPTBIT2(INP_RECVFLOWID);
1458 				break;
1459 #ifdef	RSS
1460 			case IP_RSSBUCKETID:
1461 				retval = rss_hash2bucket(inp->inp_flowid,
1462 				    inp->inp_flowtype,
1463 				    &rss_bucket);
1464 				if (retval == 0)
1465 					optval = rss_bucket;
1466 				else
1467 					error = EINVAL;
1468 				break;
1469 			case IP_RECVRSSBUCKETID:
1470 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
1471 				break;
1472 #endif
1473 			case IP_VLAN_PCP:
1474 				if (OPTBIT2(INP_2PCP_SET)) {
1475 					optval = (inp->inp_flags2 &
1476 					    INP_2PCP_MASK) >> INP_2PCP_SHIFT;
1477 				} else {
1478 					optval = -1;
1479 				}
1480 				break;
1481 			}
1482 			error = sooptcopyout(sopt, &optval, sizeof optval);
1483 			break;
1484 
1485 		/*
1486 		 * Multicast socket options are processed by the in_mcast
1487 		 * module.
1488 		 */
1489 		case IP_MULTICAST_IF:
1490 		case IP_MULTICAST_VIF:
1491 		case IP_MULTICAST_TTL:
1492 		case IP_MULTICAST_LOOP:
1493 		case IP_MSFILTER:
1494 			error = inp_getmoptions(inp, sopt);
1495 			break;
1496 
1497 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1498 		case IP_IPSEC_POLICY:
1499 			if (IPSEC_ENABLED(ipv4)) {
1500 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
1501 				break;
1502 			}
1503 			/* FALLTHROUGH */
1504 #endif /* IPSEC */
1505 
1506 		default:
1507 			error = ENOPROTOOPT;
1508 			break;
1509 		}
1510 		break;
1511 	}
1512 	return (error);
1513 }
1514 
1515 /*
1516  * Routine called from ip_output() to loop back a copy of an IP multicast
1517  * packet to the input queue of a specified interface.  Note that this
1518  * calls the output routine of the loopback "driver", but with an interface
1519  * pointer that might NOT be a loopback interface -- evil, but easier than
1520  * replicating that code here.
1521  */
1522 static void
1523 ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen)
1524 {
1525 	struct ip *ip;
1526 	struct mbuf *copym;
1527 
1528 	/*
1529 	 * Make a deep copy of the packet because we're going to
1530 	 * modify the pack in order to generate checksums.
1531 	 */
1532 	copym = m_dup(m, M_NOWAIT);
1533 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
1534 		copym = m_pullup(copym, hlen);
1535 	if (copym != NULL) {
1536 		/* If needed, compute the checksum and mark it as valid. */
1537 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1538 			in_delayed_cksum(copym);
1539 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1540 			copym->m_pkthdr.csum_flags |=
1541 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1542 			copym->m_pkthdr.csum_data = 0xffff;
1543 		}
1544 		/*
1545 		 * We don't bother to fragment if the IP length is greater
1546 		 * than the interface's MTU.  Can this possibly matter?
1547 		 */
1548 		ip = mtod(copym, struct ip *);
1549 		ip->ip_sum = 0;
1550 		ip->ip_sum = in_cksum(copym, hlen);
1551 		if_simloop(ifp, copym, AF_INET, 0);
1552 	}
1553 }
1554