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