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