xref: /freebsd/sys/netinet/ip_output.c (revision dcf58f92e2c19a32fc171f763698e711c719badc)
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 		mtu = rte->rt_mtu;
327 	else
328 		mtu = ifp->if_mtu;
329 	/* Catch a possible divide by zero later. */
330 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p",
331 	    __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp));
332 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
333 		m->m_flags |= M_MCAST;
334 		/*
335 		 * IP destination address is multicast.  Make sure "gw"
336 		 * still points to the address in "ro".  (It may have been
337 		 * changed to point to a gateway address, above.)
338 		 */
339 		gw = dst;
340 		/*
341 		 * See if the caller provided any multicast options
342 		 */
343 		if (imo != NULL) {
344 			ip->ip_ttl = imo->imo_multicast_ttl;
345 			if (imo->imo_multicast_vif != -1)
346 				ip->ip_src.s_addr =
347 				    ip_mcast_src ?
348 				    ip_mcast_src(imo->imo_multicast_vif) :
349 				    INADDR_ANY;
350 		} else
351 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
352 		/*
353 		 * Confirm that the outgoing interface supports multicast.
354 		 */
355 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
356 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
357 				IPSTAT_INC(ips_noroute);
358 				error = ENETUNREACH;
359 				goto bad;
360 			}
361 		}
362 		/*
363 		 * If source address not specified yet, use address
364 		 * of outgoing interface.
365 		 */
366 		if (ip->ip_src.s_addr == INADDR_ANY) {
367 			/* Interface may have no addresses. */
368 			if (ia != NULL)
369 				ip->ip_src = IA_SIN(ia)->sin_addr;
370 		}
371 
372 		if ((imo == NULL && in_mcast_loop) ||
373 		    (imo && imo->imo_multicast_loop)) {
374 			/*
375 			 * Loop back multicast datagram if not expressly
376 			 * forbidden to do so, even if we are not a member
377 			 * of the group; ip_input() will filter it later,
378 			 * thus deferring a hash lookup and mutex acquisition
379 			 * at the expense of a cheap copy using m_copym().
380 			 */
381 			ip_mloopback(ifp, m, dst, hlen);
382 		} else {
383 			/*
384 			 * If we are acting as a multicast router, perform
385 			 * multicast forwarding as if the packet had just
386 			 * arrived on the interface to which we are about
387 			 * to send.  The multicast forwarding function
388 			 * recursively calls this function, using the
389 			 * IP_FORWARDING flag to prevent infinite recursion.
390 			 *
391 			 * Multicasts that are looped back by ip_mloopback(),
392 			 * above, will be forwarded by the ip_input() routine,
393 			 * if necessary.
394 			 */
395 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
396 				/*
397 				 * If rsvp daemon is not running, do not
398 				 * set ip_moptions. This ensures that the packet
399 				 * is multicast and not just sent down one link
400 				 * as prescribed by rsvpd.
401 				 */
402 				if (!V_rsvp_on)
403 					imo = NULL;
404 				if (ip_mforward &&
405 				    ip_mforward(ip, ifp, m, imo) != 0) {
406 					m_freem(m);
407 					goto done;
408 				}
409 			}
410 		}
411 
412 		/*
413 		 * Multicasts with a time-to-live of zero may be looped-
414 		 * back, above, but must not be transmitted on a network.
415 		 * Also, multicasts addressed to the loopback interface
416 		 * are not sent -- the above call to ip_mloopback() will
417 		 * loop back a copy. ip_input() will drop the copy if
418 		 * this host does not belong to the destination group on
419 		 * the loopback interface.
420 		 */
421 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
422 			m_freem(m);
423 			goto done;
424 		}
425 
426 		goto sendit;
427 	}
428 
429 	/*
430 	 * If the source address is not specified yet, use the address
431 	 * of the outoing 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 
440 	/*
441 	 * Look for broadcast address and
442 	 * verify user is allowed to send
443 	 * such a packet.
444 	 */
445 	if (isbroadcast) {
446 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
447 			error = EADDRNOTAVAIL;
448 			goto bad;
449 		}
450 		if ((flags & IP_ALLOWBROADCAST) == 0) {
451 			error = EACCES;
452 			goto bad;
453 		}
454 		/* don't allow broadcast messages to be fragmented */
455 		if (ip_len > mtu) {
456 			error = EMSGSIZE;
457 			goto bad;
458 		}
459 		m->m_flags |= M_BCAST;
460 	} else {
461 		m->m_flags &= ~M_BCAST;
462 	}
463 
464 sendit:
465 #ifdef IPSEC
466 	switch(ip_ipsec_output(&m, inp, &flags, &error)) {
467 	case 1:
468 		goto bad;
469 	case -1:
470 		goto done;
471 	case 0:
472 	default:
473 		break;	/* Continue with packet processing. */
474 	}
475 	/*
476 	 * Check if there was a route for this packet; return error if not.
477 	 */
478 	if (no_route_but_check_spd) {
479 		IPSTAT_INC(ips_noroute);
480 		error = EHOSTUNREACH;
481 		goto bad;
482 	}
483 	/* Update variables that are affected by ipsec4_output(). */
484 	ip = mtod(m, struct ip *);
485 	hlen = ip->ip_hl << 2;
486 #endif /* IPSEC */
487 
488 	/* Jump over all PFIL processing if hooks are not active. */
489 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
490 		goto passout;
491 
492 	/* Run through list of hooks for output packets. */
493 	odst.s_addr = ip->ip_dst.s_addr;
494 	error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
495 	if (error != 0 || m == NULL)
496 		goto done;
497 
498 	ip = mtod(m, struct ip *);
499 	needfiblookup = 0;
500 
501 	/* See if destination IP address was changed by packet filter. */
502 	if (odst.s_addr != ip->ip_dst.s_addr) {
503 		m->m_flags |= M_SKIP_FIREWALL;
504 		/* If destination is now ourself drop to ip_input(). */
505 		if (in_localip(ip->ip_dst)) {
506 			m->m_flags |= M_FASTFWD_OURS;
507 			if (m->m_pkthdr.rcvif == NULL)
508 				m->m_pkthdr.rcvif = V_loif;
509 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
510 				m->m_pkthdr.csum_flags |=
511 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
512 				m->m_pkthdr.csum_data = 0xffff;
513 			}
514 			m->m_pkthdr.csum_flags |=
515 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
516 #ifdef SCTP
517 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
518 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
519 #endif
520 			error = netisr_queue(NETISR_IP, m);
521 			goto done;
522 		} else {
523 			if (have_ia_ref)
524 				ifa_free(&ia->ia_ifa);
525 			needfiblookup = 1; /* Redo the routing table lookup. */
526 		}
527 	}
528 	/* See if fib was changed by packet filter. */
529 	if (fibnum != M_GETFIB(m)) {
530 		m->m_flags |= M_SKIP_FIREWALL;
531 		fibnum = M_GETFIB(m);
532 		RO_RTFREE(ro);
533 		needfiblookup = 1;
534 	}
535 	if (needfiblookup)
536 		goto again;
537 
538 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
539 	if (m->m_flags & M_FASTFWD_OURS) {
540 		if (m->m_pkthdr.rcvif == NULL)
541 			m->m_pkthdr.rcvif = V_loif;
542 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
543 			m->m_pkthdr.csum_flags |=
544 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
545 			m->m_pkthdr.csum_data = 0xffff;
546 		}
547 #ifdef SCTP
548 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
549 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
550 #endif
551 		m->m_pkthdr.csum_flags |=
552 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
553 
554 		error = netisr_queue(NETISR_IP, m);
555 		goto done;
556 	}
557 	/* Or forward to some other address? */
558 	if ((m->m_flags & M_IP_NEXTHOP) &&
559 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
560 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
561 		m->m_flags |= M_SKIP_FIREWALL;
562 		m->m_flags &= ~M_IP_NEXTHOP;
563 		m_tag_delete(m, fwd_tag);
564 		if (have_ia_ref)
565 			ifa_free(&ia->ia_ifa);
566 		goto again;
567 	}
568 
569 passout:
570 	/* 127/8 must not appear on wire - RFC1122. */
571 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
572 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
573 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
574 			IPSTAT_INC(ips_badaddr);
575 			error = EADDRNOTAVAIL;
576 			goto bad;
577 		}
578 	}
579 
580 	m->m_pkthdr.csum_flags |= CSUM_IP;
581 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
582 		in_delayed_cksum(m);
583 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
584 	}
585 #ifdef SCTP
586 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
587 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
588 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
589 	}
590 #endif
591 
592 	/*
593 	 * If small enough for interface, or the interface will take
594 	 * care of the fragmentation for us, we can just send directly.
595 	 */
596 	if (ip_len <= mtu ||
597 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
598 		ip->ip_sum = 0;
599 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
600 			ip->ip_sum = in_cksum(m, hlen);
601 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
602 		}
603 
604 		/*
605 		 * Record statistics for this interface address.
606 		 * With CSUM_TSO the byte/packet count will be slightly
607 		 * incorrect because we count the IP+TCP headers only
608 		 * once instead of for every generated packet.
609 		 */
610 		if (!(flags & IP_FORWARDING) && ia) {
611 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
612 				counter_u64_add(ia->ia_ifa.ifa_opackets,
613 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
614 			else
615 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
616 
617 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
618 		}
619 #ifdef MBUF_STRESS_TEST
620 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
621 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
622 #endif
623 		/*
624 		 * Reset layer specific mbuf flags
625 		 * to avoid confusing lower layers.
626 		 */
627 		m_clrprotoflags(m);
628 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
629 		error = (*ifp->if_output)(ifp, m,
630 		    (const struct sockaddr *)gw, ro);
631 		goto done;
632 	}
633 
634 	/* Balk when DF bit is set or the interface didn't support TSO. */
635 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
636 		error = EMSGSIZE;
637 		IPSTAT_INC(ips_cantfrag);
638 		goto bad;
639 	}
640 
641 	/*
642 	 * Too large for interface; fragment if possible. If successful,
643 	 * on return, m will point to a list of packets to be sent.
644 	 */
645 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
646 	if (error)
647 		goto bad;
648 	for (; m; m = m0) {
649 		m0 = m->m_nextpkt;
650 		m->m_nextpkt = 0;
651 		if (error == 0) {
652 			/* Record statistics for this interface address. */
653 			if (ia != NULL) {
654 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
655 				counter_u64_add(ia->ia_ifa.ifa_obytes,
656 				    m->m_pkthdr.len);
657 			}
658 			/*
659 			 * Reset layer specific mbuf flags
660 			 * to avoid confusing upper layers.
661 			 */
662 			m_clrprotoflags(m);
663 
664 			IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
665 			error = (*ifp->if_output)(ifp, m,
666 			    (const struct sockaddr *)gw, ro);
667 		} else
668 			m_freem(m);
669 	}
670 
671 	if (error == 0)
672 		IPSTAT_INC(ips_fragmented);
673 
674 done:
675 	if (ro == &iproute)
676 		RO_RTFREE(ro);
677 	if (have_ia_ref)
678 		ifa_free(&ia->ia_ifa);
679 	return (error);
680 bad:
681 	m_freem(m);
682 	goto done;
683 }
684 
685 /*
686  * Create a chain of fragments which fit the given mtu. m_frag points to the
687  * mbuf to be fragmented; on return it points to the chain with the fragments.
688  * Return 0 if no error. If error, m_frag may contain a partially built
689  * chain of fragments that should be freed by the caller.
690  *
691  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
692  */
693 int
694 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
695     u_long if_hwassist_flags)
696 {
697 	int error = 0;
698 	int hlen = ip->ip_hl << 2;
699 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
700 	int off;
701 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
702 	int firstlen;
703 	struct mbuf **mnext;
704 	int nfrags;
705 	uint16_t ip_len, ip_off;
706 
707 	ip_len = ntohs(ip->ip_len);
708 	ip_off = ntohs(ip->ip_off);
709 
710 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
711 		IPSTAT_INC(ips_cantfrag);
712 		return EMSGSIZE;
713 	}
714 
715 	/*
716 	 * Must be able to put at least 8 bytes per fragment.
717 	 */
718 	if (len < 8)
719 		return EMSGSIZE;
720 
721 	/*
722 	 * If the interface will not calculate checksums on
723 	 * fragmented packets, then do it here.
724 	 */
725 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
726 		in_delayed_cksum(m0);
727 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
728 	}
729 #ifdef SCTP
730 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
731 		sctp_delayed_cksum(m0, hlen);
732 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
733 	}
734 #endif
735 	if (len > PAGE_SIZE) {
736 		/*
737 		 * Fragment large datagrams such that each segment
738 		 * contains a multiple of PAGE_SIZE amount of data,
739 		 * plus headers. This enables a receiver to perform
740 		 * page-flipping zero-copy optimizations.
741 		 *
742 		 * XXX When does this help given that sender and receiver
743 		 * could have different page sizes, and also mtu could
744 		 * be less than the receiver's page size ?
745 		 */
746 		int newlen;
747 		struct mbuf *m;
748 
749 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
750 			off += m->m_len;
751 
752 		/*
753 		 * firstlen (off - hlen) must be aligned on an
754 		 * 8-byte boundary
755 		 */
756 		if (off < hlen)
757 			goto smart_frag_failure;
758 		off = ((off - hlen) & ~7) + hlen;
759 		newlen = (~PAGE_MASK) & mtu;
760 		if ((newlen + sizeof (struct ip)) > mtu) {
761 			/* we failed, go back the default */
762 smart_frag_failure:
763 			newlen = len;
764 			off = hlen + len;
765 		}
766 		len = newlen;
767 
768 	} else {
769 		off = hlen + len;
770 	}
771 
772 	firstlen = off - hlen;
773 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
774 
775 	/*
776 	 * Loop through length of segment after first fragment,
777 	 * make new header and copy data of each part and link onto chain.
778 	 * Here, m0 is the original packet, m is the fragment being created.
779 	 * The fragments are linked off the m_nextpkt of the original
780 	 * packet, which after processing serves as the first fragment.
781 	 */
782 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
783 		struct ip *mhip;	/* ip header on the fragment */
784 		struct mbuf *m;
785 		int mhlen = sizeof (struct ip);
786 
787 		m = m_gethdr(M_NOWAIT, MT_DATA);
788 		if (m == NULL) {
789 			error = ENOBUFS;
790 			IPSTAT_INC(ips_odropped);
791 			goto done;
792 		}
793 		m->m_flags |= (m0->m_flags & M_MCAST);
794 		/*
795 		 * In the first mbuf, leave room for the link header, then
796 		 * copy the original IP header including options. The payload
797 		 * goes into an additional mbuf chain returned by m_copym().
798 		 */
799 		m->m_data += max_linkhdr;
800 		mhip = mtod(m, struct ip *);
801 		*mhip = *ip;
802 		if (hlen > sizeof (struct ip)) {
803 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
804 			mhip->ip_v = IPVERSION;
805 			mhip->ip_hl = mhlen >> 2;
806 		}
807 		m->m_len = mhlen;
808 		/* XXX do we need to add ip_off below ? */
809 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
810 		if (off + len >= ip_len)
811 			len = ip_len - off;
812 		else
813 			mhip->ip_off |= IP_MF;
814 		mhip->ip_len = htons((u_short)(len + mhlen));
815 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
816 		if (m->m_next == NULL) {	/* copy failed */
817 			m_free(m);
818 			error = ENOBUFS;	/* ??? */
819 			IPSTAT_INC(ips_odropped);
820 			goto done;
821 		}
822 		m->m_pkthdr.len = mhlen + len;
823 		m->m_pkthdr.rcvif = NULL;
824 #ifdef MAC
825 		mac_netinet_fragment(m0, m);
826 #endif
827 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
828 		mhip->ip_off = htons(mhip->ip_off);
829 		mhip->ip_sum = 0;
830 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
831 			mhip->ip_sum = in_cksum(m, mhlen);
832 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
833 		}
834 		*mnext = m;
835 		mnext = &m->m_nextpkt;
836 	}
837 	IPSTAT_ADD(ips_ofragments, nfrags);
838 
839 	/*
840 	 * Update first fragment by trimming what's been copied out
841 	 * and updating header.
842 	 */
843 	m_adj(m0, hlen + firstlen - ip_len);
844 	m0->m_pkthdr.len = hlen + firstlen;
845 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
846 	ip->ip_off = htons(ip_off | IP_MF);
847 	ip->ip_sum = 0;
848 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
849 		ip->ip_sum = in_cksum(m0, hlen);
850 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
851 	}
852 
853 done:
854 	*m_frag = m0;
855 	return error;
856 }
857 
858 void
859 in_delayed_cksum(struct mbuf *m)
860 {
861 	struct ip *ip;
862 	uint16_t csum, offset, ip_len;
863 
864 	ip = mtod(m, struct ip *);
865 	offset = ip->ip_hl << 2 ;
866 	ip_len = ntohs(ip->ip_len);
867 	csum = in_cksum_skip(m, ip_len, offset);
868 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
869 		csum = 0xffff;
870 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
871 
872 	/* find the mbuf in the chain where the checksum starts*/
873 	while ((m != NULL) && (offset >= m->m_len)) {
874 		offset -= m->m_len;
875 		m = m->m_next;
876 	}
877 	KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain."));
878 	KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs."));
879 	*(u_short *)(m->m_data + offset) = csum;
880 }
881 
882 /*
883  * IP socket option processing.
884  */
885 int
886 ip_ctloutput(struct socket *so, struct sockopt *sopt)
887 {
888 	struct	inpcb *inp = sotoinpcb(so);
889 	int	error, optval;
890 #ifdef	RSS
891 	uint32_t rss_bucket;
892 	int retval;
893 #endif
894 
895 	error = optval = 0;
896 	if (sopt->sopt_level != IPPROTO_IP) {
897 		error = EINVAL;
898 
899 		if (sopt->sopt_level == SOL_SOCKET &&
900 		    sopt->sopt_dir == SOPT_SET) {
901 			switch (sopt->sopt_name) {
902 			case SO_REUSEADDR:
903 				INP_WLOCK(inp);
904 				if ((so->so_options & SO_REUSEADDR) != 0)
905 					inp->inp_flags2 |= INP_REUSEADDR;
906 				else
907 					inp->inp_flags2 &= ~INP_REUSEADDR;
908 				INP_WUNLOCK(inp);
909 				error = 0;
910 				break;
911 			case SO_REUSEPORT:
912 				INP_WLOCK(inp);
913 				if ((so->so_options & SO_REUSEPORT) != 0)
914 					inp->inp_flags2 |= INP_REUSEPORT;
915 				else
916 					inp->inp_flags2 &= ~INP_REUSEPORT;
917 				INP_WUNLOCK(inp);
918 				error = 0;
919 				break;
920 			case SO_SETFIB:
921 				INP_WLOCK(inp);
922 				inp->inp_inc.inc_fibnum = so->so_fibnum;
923 				INP_WUNLOCK(inp);
924 				error = 0;
925 				break;
926 			default:
927 				break;
928 			}
929 		}
930 		return (error);
931 	}
932 
933 	switch (sopt->sopt_dir) {
934 	case SOPT_SET:
935 		switch (sopt->sopt_name) {
936 		case IP_OPTIONS:
937 #ifdef notyet
938 		case IP_RETOPTS:
939 #endif
940 		{
941 			struct mbuf *m;
942 			if (sopt->sopt_valsize > MLEN) {
943 				error = EMSGSIZE;
944 				break;
945 			}
946 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
947 			if (m == NULL) {
948 				error = ENOBUFS;
949 				break;
950 			}
951 			m->m_len = sopt->sopt_valsize;
952 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
953 					    m->m_len);
954 			if (error) {
955 				m_free(m);
956 				break;
957 			}
958 			INP_WLOCK(inp);
959 			error = ip_pcbopts(inp, sopt->sopt_name, m);
960 			INP_WUNLOCK(inp);
961 			return (error);
962 		}
963 
964 		case IP_BINDANY:
965 			if (sopt->sopt_td != NULL) {
966 				error = priv_check(sopt->sopt_td,
967 				    PRIV_NETINET_BINDANY);
968 				if (error)
969 					break;
970 			}
971 			/* FALLTHROUGH */
972 		case IP_BINDMULTI:
973 #ifdef	RSS
974 		case IP_RSS_LISTEN_BUCKET:
975 #endif
976 		case IP_TOS:
977 		case IP_TTL:
978 		case IP_MINTTL:
979 		case IP_RECVOPTS:
980 		case IP_RECVRETOPTS:
981 		case IP_RECVDSTADDR:
982 		case IP_RECVTTL:
983 		case IP_RECVIF:
984 		case IP_ONESBCAST:
985 		case IP_DONTFRAG:
986 		case IP_RECVTOS:
987 		case IP_RECVFLOWID:
988 #ifdef	RSS
989 		case IP_RECVRSSBUCKETID:
990 #endif
991 			error = sooptcopyin(sopt, &optval, sizeof optval,
992 					    sizeof optval);
993 			if (error)
994 				break;
995 
996 			switch (sopt->sopt_name) {
997 			case IP_TOS:
998 				inp->inp_ip_tos = optval;
999 				break;
1000 
1001 			case IP_TTL:
1002 				inp->inp_ip_ttl = optval;
1003 				break;
1004 
1005 			case IP_MINTTL:
1006 				if (optval >= 0 && optval <= MAXTTL)
1007 					inp->inp_ip_minttl = optval;
1008 				else
1009 					error = EINVAL;
1010 				break;
1011 
1012 #define	OPTSET(bit) do {						\
1013 	INP_WLOCK(inp);							\
1014 	if (optval)							\
1015 		inp->inp_flags |= bit;					\
1016 	else								\
1017 		inp->inp_flags &= ~bit;					\
1018 	INP_WUNLOCK(inp);						\
1019 } while (0)
1020 
1021 #define	OPTSET2(bit, val) do {						\
1022 	INP_WLOCK(inp);							\
1023 	if (val)							\
1024 		inp->inp_flags2 |= bit;					\
1025 	else								\
1026 		inp->inp_flags2 &= ~bit;				\
1027 	INP_WUNLOCK(inp);						\
1028 } while (0)
1029 
1030 			case IP_RECVOPTS:
1031 				OPTSET(INP_RECVOPTS);
1032 				break;
1033 
1034 			case IP_RECVRETOPTS:
1035 				OPTSET(INP_RECVRETOPTS);
1036 				break;
1037 
1038 			case IP_RECVDSTADDR:
1039 				OPTSET(INP_RECVDSTADDR);
1040 				break;
1041 
1042 			case IP_RECVTTL:
1043 				OPTSET(INP_RECVTTL);
1044 				break;
1045 
1046 			case IP_RECVIF:
1047 				OPTSET(INP_RECVIF);
1048 				break;
1049 
1050 			case IP_ONESBCAST:
1051 				OPTSET(INP_ONESBCAST);
1052 				break;
1053 			case IP_DONTFRAG:
1054 				OPTSET(INP_DONTFRAG);
1055 				break;
1056 			case IP_BINDANY:
1057 				OPTSET(INP_BINDANY);
1058 				break;
1059 			case IP_RECVTOS:
1060 				OPTSET(INP_RECVTOS);
1061 				break;
1062 			case IP_BINDMULTI:
1063 				OPTSET2(INP_BINDMULTI, optval);
1064 				break;
1065 			case IP_RECVFLOWID:
1066 				OPTSET2(INP_RECVFLOWID, optval);
1067 				break;
1068 #ifdef	RSS
1069 			case IP_RSS_LISTEN_BUCKET:
1070 				if ((optval >= 0) &&
1071 				    (optval < rss_getnumbuckets())) {
1072 					inp->inp_rss_listen_bucket = optval;
1073 					OPTSET2(INP_RSS_BUCKET_SET, 1);
1074 				} else {
1075 					error = EINVAL;
1076 				}
1077 				break;
1078 			case IP_RECVRSSBUCKETID:
1079 				OPTSET2(INP_RECVRSSBUCKETID, optval);
1080 				break;
1081 #endif
1082 			}
1083 			break;
1084 #undef OPTSET
1085 #undef OPTSET2
1086 
1087 		/*
1088 		 * Multicast socket options are processed by the in_mcast
1089 		 * module.
1090 		 */
1091 		case IP_MULTICAST_IF:
1092 		case IP_MULTICAST_VIF:
1093 		case IP_MULTICAST_TTL:
1094 		case IP_MULTICAST_LOOP:
1095 		case IP_ADD_MEMBERSHIP:
1096 		case IP_DROP_MEMBERSHIP:
1097 		case IP_ADD_SOURCE_MEMBERSHIP:
1098 		case IP_DROP_SOURCE_MEMBERSHIP:
1099 		case IP_BLOCK_SOURCE:
1100 		case IP_UNBLOCK_SOURCE:
1101 		case IP_MSFILTER:
1102 		case MCAST_JOIN_GROUP:
1103 		case MCAST_LEAVE_GROUP:
1104 		case MCAST_JOIN_SOURCE_GROUP:
1105 		case MCAST_LEAVE_SOURCE_GROUP:
1106 		case MCAST_BLOCK_SOURCE:
1107 		case MCAST_UNBLOCK_SOURCE:
1108 			error = inp_setmoptions(inp, sopt);
1109 			break;
1110 
1111 		case IP_PORTRANGE:
1112 			error = sooptcopyin(sopt, &optval, sizeof optval,
1113 					    sizeof optval);
1114 			if (error)
1115 				break;
1116 
1117 			INP_WLOCK(inp);
1118 			switch (optval) {
1119 			case IP_PORTRANGE_DEFAULT:
1120 				inp->inp_flags &= ~(INP_LOWPORT);
1121 				inp->inp_flags &= ~(INP_HIGHPORT);
1122 				break;
1123 
1124 			case IP_PORTRANGE_HIGH:
1125 				inp->inp_flags &= ~(INP_LOWPORT);
1126 				inp->inp_flags |= INP_HIGHPORT;
1127 				break;
1128 
1129 			case IP_PORTRANGE_LOW:
1130 				inp->inp_flags &= ~(INP_HIGHPORT);
1131 				inp->inp_flags |= INP_LOWPORT;
1132 				break;
1133 
1134 			default:
1135 				error = EINVAL;
1136 				break;
1137 			}
1138 			INP_WUNLOCK(inp);
1139 			break;
1140 
1141 #ifdef IPSEC
1142 		case IP_IPSEC_POLICY:
1143 		{
1144 			caddr_t req;
1145 			struct mbuf *m;
1146 
1147 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1148 				break;
1149 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1150 				break;
1151 			req = mtod(m, caddr_t);
1152 			error = ipsec_set_policy(inp, sopt->sopt_name, req,
1153 			    m->m_len, (sopt->sopt_td != NULL) ?
1154 			    sopt->sopt_td->td_ucred : NULL);
1155 			m_freem(m);
1156 			break;
1157 		}
1158 #endif /* IPSEC */
1159 
1160 		default:
1161 			error = ENOPROTOOPT;
1162 			break;
1163 		}
1164 		break;
1165 
1166 	case SOPT_GET:
1167 		switch (sopt->sopt_name) {
1168 		case IP_OPTIONS:
1169 		case IP_RETOPTS:
1170 			if (inp->inp_options)
1171 				error = sooptcopyout(sopt,
1172 						     mtod(inp->inp_options,
1173 							  char *),
1174 						     inp->inp_options->m_len);
1175 			else
1176 				sopt->sopt_valsize = 0;
1177 			break;
1178 
1179 		case IP_TOS:
1180 		case IP_TTL:
1181 		case IP_MINTTL:
1182 		case IP_RECVOPTS:
1183 		case IP_RECVRETOPTS:
1184 		case IP_RECVDSTADDR:
1185 		case IP_RECVTTL:
1186 		case IP_RECVIF:
1187 		case IP_PORTRANGE:
1188 		case IP_ONESBCAST:
1189 		case IP_DONTFRAG:
1190 		case IP_BINDANY:
1191 		case IP_RECVTOS:
1192 		case IP_BINDMULTI:
1193 		case IP_FLOWID:
1194 		case IP_FLOWTYPE:
1195 		case IP_RECVFLOWID:
1196 #ifdef	RSS
1197 		case IP_RSSBUCKETID:
1198 		case IP_RECVRSSBUCKETID:
1199 #endif
1200 			switch (sopt->sopt_name) {
1201 
1202 			case IP_TOS:
1203 				optval = inp->inp_ip_tos;
1204 				break;
1205 
1206 			case IP_TTL:
1207 				optval = inp->inp_ip_ttl;
1208 				break;
1209 
1210 			case IP_MINTTL:
1211 				optval = inp->inp_ip_minttl;
1212 				break;
1213 
1214 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1215 #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1216 
1217 			case IP_RECVOPTS:
1218 				optval = OPTBIT(INP_RECVOPTS);
1219 				break;
1220 
1221 			case IP_RECVRETOPTS:
1222 				optval = OPTBIT(INP_RECVRETOPTS);
1223 				break;
1224 
1225 			case IP_RECVDSTADDR:
1226 				optval = OPTBIT(INP_RECVDSTADDR);
1227 				break;
1228 
1229 			case IP_RECVTTL:
1230 				optval = OPTBIT(INP_RECVTTL);
1231 				break;
1232 
1233 			case IP_RECVIF:
1234 				optval = OPTBIT(INP_RECVIF);
1235 				break;
1236 
1237 			case IP_PORTRANGE:
1238 				if (inp->inp_flags & INP_HIGHPORT)
1239 					optval = IP_PORTRANGE_HIGH;
1240 				else if (inp->inp_flags & INP_LOWPORT)
1241 					optval = IP_PORTRANGE_LOW;
1242 				else
1243 					optval = 0;
1244 				break;
1245 
1246 			case IP_ONESBCAST:
1247 				optval = OPTBIT(INP_ONESBCAST);
1248 				break;
1249 			case IP_DONTFRAG:
1250 				optval = OPTBIT(INP_DONTFRAG);
1251 				break;
1252 			case IP_BINDANY:
1253 				optval = OPTBIT(INP_BINDANY);
1254 				break;
1255 			case IP_RECVTOS:
1256 				optval = OPTBIT(INP_RECVTOS);
1257 				break;
1258 			case IP_FLOWID:
1259 				optval = inp->inp_flowid;
1260 				break;
1261 			case IP_FLOWTYPE:
1262 				optval = inp->inp_flowtype;
1263 				break;
1264 			case IP_RECVFLOWID:
1265 				optval = OPTBIT2(INP_RECVFLOWID);
1266 				break;
1267 #ifdef	RSS
1268 			case IP_RSSBUCKETID:
1269 				retval = rss_hash2bucket(inp->inp_flowid,
1270 				    inp->inp_flowtype,
1271 				    &rss_bucket);
1272 				if (retval == 0)
1273 					optval = rss_bucket;
1274 				else
1275 					error = EINVAL;
1276 				break;
1277 			case IP_RECVRSSBUCKETID:
1278 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
1279 				break;
1280 #endif
1281 			case IP_BINDMULTI:
1282 				optval = OPTBIT2(INP_BINDMULTI);
1283 				break;
1284 			}
1285 			error = sooptcopyout(sopt, &optval, sizeof optval);
1286 			break;
1287 
1288 		/*
1289 		 * Multicast socket options are processed by the in_mcast
1290 		 * module.
1291 		 */
1292 		case IP_MULTICAST_IF:
1293 		case IP_MULTICAST_VIF:
1294 		case IP_MULTICAST_TTL:
1295 		case IP_MULTICAST_LOOP:
1296 		case IP_MSFILTER:
1297 			error = inp_getmoptions(inp, sopt);
1298 			break;
1299 
1300 #ifdef IPSEC
1301 		case IP_IPSEC_POLICY:
1302 		{
1303 			struct mbuf *m = NULL;
1304 			caddr_t req = NULL;
1305 			size_t len = 0;
1306 
1307 			if (m != 0) {
1308 				req = mtod(m, caddr_t);
1309 				len = m->m_len;
1310 			}
1311 			error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
1312 			if (error == 0)
1313 				error = soopt_mcopyout(sopt, m); /* XXX */
1314 			if (error == 0)
1315 				m_freem(m);
1316 			break;
1317 		}
1318 #endif /* IPSEC */
1319 
1320 		default:
1321 			error = ENOPROTOOPT;
1322 			break;
1323 		}
1324 		break;
1325 	}
1326 	return (error);
1327 }
1328 
1329 /*
1330  * Routine called from ip_output() to loop back a copy of an IP multicast
1331  * packet to the input queue of a specified interface.  Note that this
1332  * calls the output routine of the loopback "driver", but with an interface
1333  * pointer that might NOT be a loopback interface -- evil, but easier than
1334  * replicating that code here.
1335  */
1336 static void
1337 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1338     int hlen)
1339 {
1340 	register struct ip *ip;
1341 	struct mbuf *copym;
1342 
1343 	/*
1344 	 * Make a deep copy of the packet because we're going to
1345 	 * modify the pack in order to generate checksums.
1346 	 */
1347 	copym = m_dup(m, M_NOWAIT);
1348 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
1349 		copym = m_pullup(copym, hlen);
1350 	if (copym != NULL) {
1351 		/* If needed, compute the checksum and mark it as valid. */
1352 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1353 			in_delayed_cksum(copym);
1354 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1355 			copym->m_pkthdr.csum_flags |=
1356 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1357 			copym->m_pkthdr.csum_data = 0xffff;
1358 		}
1359 		/*
1360 		 * We don't bother to fragment if the IP length is greater
1361 		 * than the interface's MTU.  Can this possibly matter?
1362 		 */
1363 		ip = mtod(copym, struct ip *);
1364 		ip->ip_sum = 0;
1365 		ip->ip_sum = in_cksum(copym, hlen);
1366 #if 1 /* XXX */
1367 		if (dst->sin_family != AF_INET) {
1368 			printf("ip_mloopback: bad address family %d\n",
1369 						dst->sin_family);
1370 			dst->sin_family = AF_INET;
1371 		}
1372 #endif
1373 		if_simloop(ifp, copym, dst->sin_family, 0);
1374 	}
1375 }
1376