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