xref: /freebsd/sys/netinet/ip_output.c (revision 0572ccaa4543b0abef8ef81e384c1d04de9f3da1)
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 #ifdef IPSEC
140 	int no_route_but_check_spd = 0;
141 #endif
142 	M_ASSERTPKTHDR(m);
143 
144 	if (inp != NULL) {
145 		INP_LOCK_ASSERT(inp);
146 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
147 		if (inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) {
148 			m->m_pkthdr.flowid = inp->inp_flowid;
149 			M_HASHTYPE_SET(m, inp->inp_flowtype);
150 			m->m_flags |= M_FLOWID;
151 		}
152 	}
153 
154 	if (ro == NULL) {
155 		ro = &iproute;
156 		bzero(ro, sizeof (*ro));
157 	}
158 
159 #ifdef FLOWTABLE
160 	if (ro->ro_rt == NULL)
161 		(void )flowtable_lookup(AF_INET, m, ro);
162 #endif
163 
164 	if (opt) {
165 		int len = 0;
166 		m = ip_insertoptions(m, opt, &len);
167 		if (len != 0)
168 			hlen = len; /* ip->ip_hl is updated above */
169 	}
170 	ip = mtod(m, struct ip *);
171 	ip_len = ntohs(ip->ip_len);
172 	ip_off = ntohs(ip->ip_off);
173 
174 	/*
175 	 * Fill in IP header.  If we are not allowing fragmentation,
176 	 * then the ip_id field is meaningless, but we don't set it
177 	 * to zero.  Doing so causes various problems when devices along
178 	 * the path (routers, load balancers, firewalls, etc.) illegally
179 	 * disable DF on our packet.  Note that a 16-bit counter
180 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
181 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
182 	 * for Counting NATted Hosts", Proc. IMW'02, available at
183 	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
184 	 */
185 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
186 		ip->ip_v = IPVERSION;
187 		ip->ip_hl = hlen >> 2;
188 		ip->ip_id = ip_newid();
189 		IPSTAT_INC(ips_localout);
190 	} else {
191 		/* Header already set, fetch hlen from there */
192 		hlen = ip->ip_hl << 2;
193 	}
194 
195 	/*
196 	 * dst/gw handling:
197 	 *
198 	 * dst can be rewritten but always points to &ro->ro_dst.
199 	 * gw is readonly but can point either to dst OR rt_gateway,
200 	 * therefore we need restore gw if we're redoing lookup.
201 	 */
202 	gw = dst = (struct sockaddr_in *)&ro->ro_dst;
203 again:
204 	ia = NULL;
205 	/*
206 	 * If there is a cached route, check that it is to the same
207 	 * destination and is still up.  If not, free it and try again.
208 	 * The address family should also be checked in case of sharing
209 	 * the cache with IPv6.
210 	 */
211 	rte = ro->ro_rt;
212 	if (rte && ((rte->rt_flags & RTF_UP) == 0 ||
213 		    rte->rt_ifp == NULL ||
214 		    !RT_LINK_IS_UP(rte->rt_ifp) ||
215 			  dst->sin_family != AF_INET ||
216 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
217 		RO_RTFREE(ro);
218 		ro->ro_lle = NULL;
219 		rte = NULL;
220 		gw = dst;
221 	}
222 	if (rte == NULL && fwd_tag == NULL) {
223 		bzero(dst, sizeof(*dst));
224 		dst->sin_family = AF_INET;
225 		dst->sin_len = sizeof(*dst);
226 		dst->sin_addr = ip->ip_dst;
227 	}
228 	/*
229 	 * If routing to interface only, short circuit routing lookup.
230 	 * The use of an all-ones broadcast address implies this; an
231 	 * interface is specified by the broadcast address of an interface,
232 	 * or the destination address of a ptp interface.
233 	 */
234 	if (flags & IP_SENDONES) {
235 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
236 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
237 						    RT_DEFAULT_FIB))) == NULL) {
238 			IPSTAT_INC(ips_noroute);
239 			error = ENETUNREACH;
240 			goto bad;
241 		}
242 		ip->ip_dst.s_addr = INADDR_BROADCAST;
243 		dst->sin_addr = ip->ip_dst;
244 		ifp = ia->ia_ifp;
245 		ip->ip_ttl = 1;
246 		isbroadcast = 1;
247 	} else if (flags & IP_ROUTETOIF) {
248 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
249 		    				    RT_DEFAULT_FIB))) == NULL &&
250 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
251 		    				RT_DEFAULT_FIB))) == NULL) {
252 			IPSTAT_INC(ips_noroute);
253 			error = ENETUNREACH;
254 			goto bad;
255 		}
256 		ifp = ia->ia_ifp;
257 		ip->ip_ttl = 1;
258 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
259 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
260 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
261 		/*
262 		 * Bypass the normal routing lookup for multicast
263 		 * packets if the interface is specified.
264 		 */
265 		ifp = imo->imo_multicast_ifp;
266 		IFP_TO_IA(ifp, ia);
267 		isbroadcast = 0;	/* fool gcc */
268 	} else {
269 		/*
270 		 * We want to do any cloning requested by the link layer,
271 		 * as this is probably required in all cases for correct
272 		 * operation (as it is for ARP).
273 		 */
274 		if (rte == NULL) {
275 #ifdef RADIX_MPATH
276 			rtalloc_mpath_fib(ro,
277 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
278 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
279 #else
280 			in_rtalloc_ign(ro, 0,
281 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
282 #endif
283 			rte = ro->ro_rt;
284 		}
285 		if (rte == NULL ||
286 		    rte->rt_ifp == NULL ||
287 		    !RT_LINK_IS_UP(rte->rt_ifp)) {
288 #ifdef IPSEC
289 			/*
290 			 * There is no route for this packet, but it is
291 			 * possible that a matching SPD entry exists.
292 			 */
293 			no_route_but_check_spd = 1;
294 			mtu = 0; /* Silence GCC warning. */
295 			goto sendit;
296 #endif
297 			IPSTAT_INC(ips_noroute);
298 			error = EHOSTUNREACH;
299 			goto bad;
300 		}
301 		ia = ifatoia(rte->rt_ifa);
302 		ifp = rte->rt_ifp;
303 		counter_u64_add(rte->rt_pksent, 1);
304 		if (rte->rt_flags & RTF_GATEWAY)
305 			gw = (struct sockaddr_in *)rte->rt_gateway;
306 		if (rte->rt_flags & RTF_HOST)
307 			isbroadcast = (rte->rt_flags & RTF_BROADCAST);
308 		else
309 			isbroadcast = in_broadcast(gw->sin_addr, ifp);
310 	}
311 	/*
312 	 * Calculate MTU.  If we have a route that is up, use that,
313 	 * otherwise use the interface's MTU.
314 	 */
315 	if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) {
316 		/*
317 		 * This case can happen if the user changed the MTU
318 		 * of an interface after enabling IP on it.  Because
319 		 * most netifs don't keep track of routes pointing to
320 		 * them, there is no way for one to update all its
321 		 * routes when the MTU is changed.
322 		 */
323 		if (rte->rt_mtu > ifp->if_mtu)
324 			rte->rt_mtu = ifp->if_mtu;
325 		mtu = rte->rt_mtu;
326 	} else {
327 		mtu = ifp->if_mtu;
328 	}
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 	 * Both in the SMP world, pre-emption world if_transmit() world,
442 	 * the following code doesn't really function as intended any further.
443 	 *
444 	 * + There can and will be multiple CPUs running this code path
445 	 *   in parallel, and we do no lock holding when checking the
446 	 *   queue depth;
447 	 * + And since other threads can be running concurrently, even if
448 	 *   we do pass this check, another thread may queue some frames
449 	 *   before this thread does and it will end up partially or fully
450 	 *   failing to send anyway;
451 	 * + if_transmit() based drivers don't necessarily set ifq_len
452 	 *   at all.
453 	 *
454 	 * This should be replaced with a method of pushing an entire list
455 	 * of fragment frames to the driver and have the driver decide
456 	 * whether it can queue or not queue the entire set.
457 	 */
458 #if 0
459 	/*
460 	 * Verify that we have any chance at all of being able to queue the
461 	 * packet or packet fragments, unless ALTQ is enabled on the given
462 	 * interface in which case packetdrop should be done by queueing.
463 	 */
464 	n = ip_len / mtu + 1; /* how many fragments ? */
465 	if (
466 #ifdef ALTQ
467 	    (!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
468 #endif /* ALTQ */
469 	    (ifp->if_snd.ifq_len + n) >= ifp->if_snd.ifq_maxlen ) {
470 		error = ENOBUFS;
471 		IPSTAT_INC(ips_odropped);
472 		ifp->if_snd.ifq_drops += n;
473 		goto bad;
474 	}
475 #endif
476 
477 	/*
478 	 * Look for broadcast address and
479 	 * verify user is allowed to send
480 	 * such a packet.
481 	 */
482 	if (isbroadcast) {
483 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
484 			error = EADDRNOTAVAIL;
485 			goto bad;
486 		}
487 		if ((flags & IP_ALLOWBROADCAST) == 0) {
488 			error = EACCES;
489 			goto bad;
490 		}
491 		/* don't allow broadcast messages to be fragmented */
492 		if (ip_len > mtu) {
493 			error = EMSGSIZE;
494 			goto bad;
495 		}
496 		m->m_flags |= M_BCAST;
497 	} else {
498 		m->m_flags &= ~M_BCAST;
499 	}
500 
501 sendit:
502 #ifdef IPSEC
503 	switch(ip_ipsec_output(&m, inp, &flags, &error)) {
504 	case 1:
505 		goto bad;
506 	case -1:
507 		goto done;
508 	case 0:
509 	default:
510 		break;	/* Continue with packet processing. */
511 	}
512 	/*
513 	 * Check if there was a route for this packet; return error if not.
514 	 */
515 	if (no_route_but_check_spd) {
516 		IPSTAT_INC(ips_noroute);
517 		error = EHOSTUNREACH;
518 		goto bad;
519 	}
520 	/* Update variables that are affected by ipsec4_output(). */
521 	ip = mtod(m, struct ip *);
522 	hlen = ip->ip_hl << 2;
523 #endif /* IPSEC */
524 
525 	/* Jump over all PFIL processing if hooks are not active. */
526 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
527 		goto passout;
528 
529 	/* Run through list of hooks for output packets. */
530 	odst.s_addr = ip->ip_dst.s_addr;
531 	error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
532 	if (error != 0 || m == NULL)
533 		goto done;
534 
535 	ip = mtod(m, struct ip *);
536 
537 	/* See if destination IP address was changed by packet filter. */
538 	if (odst.s_addr != ip->ip_dst.s_addr) {
539 		m->m_flags |= M_SKIP_FIREWALL;
540 		/* If destination is now ourself drop to ip_input(). */
541 		if (in_localip(ip->ip_dst)) {
542 			m->m_flags |= M_FASTFWD_OURS;
543 			if (m->m_pkthdr.rcvif == NULL)
544 				m->m_pkthdr.rcvif = V_loif;
545 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
546 				m->m_pkthdr.csum_flags |=
547 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
548 				m->m_pkthdr.csum_data = 0xffff;
549 			}
550 			m->m_pkthdr.csum_flags |=
551 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
552 #ifdef SCTP
553 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
554 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
555 #endif
556 			error = netisr_queue(NETISR_IP, m);
557 			goto done;
558 		} else
559 			goto again;	/* Redo the routing table lookup. */
560 	}
561 
562 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
563 	if (m->m_flags & M_FASTFWD_OURS) {
564 		if (m->m_pkthdr.rcvif == NULL)
565 			m->m_pkthdr.rcvif = V_loif;
566 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
567 			m->m_pkthdr.csum_flags |=
568 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
569 			m->m_pkthdr.csum_data = 0xffff;
570 		}
571 #ifdef SCTP
572 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
573 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
574 #endif
575 		m->m_pkthdr.csum_flags |=
576 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
577 
578 		error = netisr_queue(NETISR_IP, m);
579 		goto done;
580 	}
581 	/* Or forward to some other address? */
582 	if ((m->m_flags & M_IP_NEXTHOP) &&
583 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
584 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
585 		m->m_flags |= M_SKIP_FIREWALL;
586 		m->m_flags &= ~M_IP_NEXTHOP;
587 		m_tag_delete(m, fwd_tag);
588 		goto again;
589 	}
590 
591 passout:
592 	/* 127/8 must not appear on wire - RFC1122. */
593 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
594 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
595 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
596 			IPSTAT_INC(ips_badaddr);
597 			error = EADDRNOTAVAIL;
598 			goto bad;
599 		}
600 	}
601 
602 	m->m_pkthdr.csum_flags |= CSUM_IP;
603 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
604 		in_delayed_cksum(m);
605 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
606 	}
607 #ifdef SCTP
608 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
609 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
610 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
611 	}
612 #endif
613 
614 	/*
615 	 * If small enough for interface, or the interface will take
616 	 * care of the fragmentation for us, we can just send directly.
617 	 */
618 	if (ip_len <= mtu ||
619 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
620 	    ((ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
621 		ip->ip_sum = 0;
622 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
623 			ip->ip_sum = in_cksum(m, hlen);
624 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
625 		}
626 
627 		/*
628 		 * Record statistics for this interface address.
629 		 * With CSUM_TSO the byte/packet count will be slightly
630 		 * incorrect because we count the IP+TCP headers only
631 		 * once instead of for every generated packet.
632 		 */
633 		if (!(flags & IP_FORWARDING) && ia) {
634 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
635 				counter_u64_add(ia->ia_ifa.ifa_opackets,
636 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
637 			else
638 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
639 
640 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
641 		}
642 #ifdef MBUF_STRESS_TEST
643 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
644 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
645 #endif
646 		/*
647 		 * Reset layer specific mbuf flags
648 		 * to avoid confusing lower layers.
649 		 */
650 		m_clrprotoflags(m);
651 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
652 		error = (*ifp->if_output)(ifp, m,
653 		    (const struct sockaddr *)gw, ro);
654 		goto done;
655 	}
656 
657 	/* Balk when DF bit is set or the interface didn't support TSO. */
658 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
659 		error = EMSGSIZE;
660 		IPSTAT_INC(ips_cantfrag);
661 		goto bad;
662 	}
663 
664 	/*
665 	 * Too large for interface; fragment if possible. If successful,
666 	 * on return, m will point to a list of packets to be sent.
667 	 */
668 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
669 	if (error)
670 		goto bad;
671 	for (; m; m = m0) {
672 		m0 = m->m_nextpkt;
673 		m->m_nextpkt = 0;
674 		if (error == 0) {
675 			/* Record statistics for this interface address. */
676 			if (ia != NULL) {
677 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
678 				counter_u64_add(ia->ia_ifa.ifa_obytes,
679 				    m->m_pkthdr.len);
680 			}
681 			/*
682 			 * Reset layer specific mbuf flags
683 			 * to avoid confusing upper layers.
684 			 */
685 			m_clrprotoflags(m);
686 
687 			IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
688 			error = (*ifp->if_output)(ifp, m,
689 			    (const struct sockaddr *)gw, ro);
690 		} else
691 			m_freem(m);
692 	}
693 
694 	if (error == 0)
695 		IPSTAT_INC(ips_fragmented);
696 
697 done:
698 	if (ro == &iproute)
699 		RO_RTFREE(ro);
700 	return (error);
701 bad:
702 	m_freem(m);
703 	goto done;
704 }
705 
706 /*
707  * Create a chain of fragments which fit the given mtu. m_frag points to the
708  * mbuf to be fragmented; on return it points to the chain with the fragments.
709  * Return 0 if no error. If error, m_frag may contain a partially built
710  * chain of fragments that should be freed by the caller.
711  *
712  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
713  */
714 int
715 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
716     u_long if_hwassist_flags)
717 {
718 	int error = 0;
719 	int hlen = ip->ip_hl << 2;
720 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
721 	int off;
722 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
723 	int firstlen;
724 	struct mbuf **mnext;
725 	int nfrags;
726 	uint16_t ip_len, ip_off;
727 
728 	ip_len = ntohs(ip->ip_len);
729 	ip_off = ntohs(ip->ip_off);
730 
731 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
732 		IPSTAT_INC(ips_cantfrag);
733 		return EMSGSIZE;
734 	}
735 
736 	/*
737 	 * Must be able to put at least 8 bytes per fragment.
738 	 */
739 	if (len < 8)
740 		return EMSGSIZE;
741 
742 	/*
743 	 * If the interface will not calculate checksums on
744 	 * fragmented packets, then do it here.
745 	 */
746 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
747 		in_delayed_cksum(m0);
748 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
749 	}
750 #ifdef SCTP
751 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
752 		sctp_delayed_cksum(m0, hlen);
753 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
754 	}
755 #endif
756 	if (len > PAGE_SIZE) {
757 		/*
758 		 * Fragment large datagrams such that each segment
759 		 * contains a multiple of PAGE_SIZE amount of data,
760 		 * plus headers. This enables a receiver to perform
761 		 * page-flipping zero-copy optimizations.
762 		 *
763 		 * XXX When does this help given that sender and receiver
764 		 * could have different page sizes, and also mtu could
765 		 * be less than the receiver's page size ?
766 		 */
767 		int newlen;
768 		struct mbuf *m;
769 
770 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
771 			off += m->m_len;
772 
773 		/*
774 		 * firstlen (off - hlen) must be aligned on an
775 		 * 8-byte boundary
776 		 */
777 		if (off < hlen)
778 			goto smart_frag_failure;
779 		off = ((off - hlen) & ~7) + hlen;
780 		newlen = (~PAGE_MASK) & mtu;
781 		if ((newlen + sizeof (struct ip)) > mtu) {
782 			/* we failed, go back the default */
783 smart_frag_failure:
784 			newlen = len;
785 			off = hlen + len;
786 		}
787 		len = newlen;
788 
789 	} else {
790 		off = hlen + len;
791 	}
792 
793 	firstlen = off - hlen;
794 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
795 
796 	/*
797 	 * Loop through length of segment after first fragment,
798 	 * make new header and copy data of each part and link onto chain.
799 	 * Here, m0 is the original packet, m is the fragment being created.
800 	 * The fragments are linked off the m_nextpkt of the original
801 	 * packet, which after processing serves as the first fragment.
802 	 */
803 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
804 		struct ip *mhip;	/* ip header on the fragment */
805 		struct mbuf *m;
806 		int mhlen = sizeof (struct ip);
807 
808 		m = m_gethdr(M_NOWAIT, MT_DATA);
809 		if (m == NULL) {
810 			error = ENOBUFS;
811 			IPSTAT_INC(ips_odropped);
812 			goto done;
813 		}
814 		m->m_flags |= (m0->m_flags & M_MCAST);
815 		/*
816 		 * In the first mbuf, leave room for the link header, then
817 		 * copy the original IP header including options. The payload
818 		 * goes into an additional mbuf chain returned by m_copym().
819 		 */
820 		m->m_data += max_linkhdr;
821 		mhip = mtod(m, struct ip *);
822 		*mhip = *ip;
823 		if (hlen > sizeof (struct ip)) {
824 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
825 			mhip->ip_v = IPVERSION;
826 			mhip->ip_hl = mhlen >> 2;
827 		}
828 		m->m_len = mhlen;
829 		/* XXX do we need to add ip_off below ? */
830 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
831 		if (off + len >= ip_len)
832 			len = ip_len - off;
833 		else
834 			mhip->ip_off |= IP_MF;
835 		mhip->ip_len = htons((u_short)(len + mhlen));
836 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
837 		if (m->m_next == NULL) {	/* copy failed */
838 			m_free(m);
839 			error = ENOBUFS;	/* ??? */
840 			IPSTAT_INC(ips_odropped);
841 			goto done;
842 		}
843 		m->m_pkthdr.len = mhlen + len;
844 		m->m_pkthdr.rcvif = NULL;
845 #ifdef MAC
846 		mac_netinet_fragment(m0, m);
847 #endif
848 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
849 		mhip->ip_off = htons(mhip->ip_off);
850 		mhip->ip_sum = 0;
851 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
852 			mhip->ip_sum = in_cksum(m, mhlen);
853 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
854 		}
855 		*mnext = m;
856 		mnext = &m->m_nextpkt;
857 	}
858 	IPSTAT_ADD(ips_ofragments, nfrags);
859 
860 	/*
861 	 * Update first fragment by trimming what's been copied out
862 	 * and updating header.
863 	 */
864 	m_adj(m0, hlen + firstlen - ip_len);
865 	m0->m_pkthdr.len = hlen + firstlen;
866 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
867 	ip->ip_off = htons(ip_off | IP_MF);
868 	ip->ip_sum = 0;
869 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
870 		ip->ip_sum = in_cksum(m0, hlen);
871 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
872 	}
873 
874 done:
875 	*m_frag = m0;
876 	return error;
877 }
878 
879 void
880 in_delayed_cksum(struct mbuf *m)
881 {
882 	struct ip *ip;
883 	uint16_t csum, offset, ip_len;
884 
885 	ip = mtod(m, struct ip *);
886 	offset = ip->ip_hl << 2 ;
887 	ip_len = ntohs(ip->ip_len);
888 	csum = in_cksum_skip(m, ip_len, offset);
889 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
890 		csum = 0xffff;
891 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
892 
893 	/* find the mbuf in the chain where the checksum starts*/
894 	while ((m != NULL) && (offset >= m->m_len)) {
895 		offset -= m->m_len;
896 		m = m->m_next;
897 	}
898 	KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain."));
899 	KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs."));
900 	*(u_short *)(m->m_data + offset) = csum;
901 }
902 
903 /*
904  * IP socket option processing.
905  */
906 int
907 ip_ctloutput(struct socket *so, struct sockopt *sopt)
908 {
909 	struct	inpcb *inp = sotoinpcb(so);
910 	int	error, optval;
911 
912 	error = optval = 0;
913 	if (sopt->sopt_level != IPPROTO_IP) {
914 		error = EINVAL;
915 
916 		if (sopt->sopt_level == SOL_SOCKET &&
917 		    sopt->sopt_dir == SOPT_SET) {
918 			switch (sopt->sopt_name) {
919 			case SO_REUSEADDR:
920 				INP_WLOCK(inp);
921 				if ((so->so_options & SO_REUSEADDR) != 0)
922 					inp->inp_flags2 |= INP_REUSEADDR;
923 				else
924 					inp->inp_flags2 &= ~INP_REUSEADDR;
925 				INP_WUNLOCK(inp);
926 				error = 0;
927 				break;
928 			case SO_REUSEPORT:
929 				INP_WLOCK(inp);
930 				if ((so->so_options & SO_REUSEPORT) != 0)
931 					inp->inp_flags2 |= INP_REUSEPORT;
932 				else
933 					inp->inp_flags2 &= ~INP_REUSEPORT;
934 				INP_WUNLOCK(inp);
935 				error = 0;
936 				break;
937 			case SO_SETFIB:
938 				INP_WLOCK(inp);
939 				inp->inp_inc.inc_fibnum = so->so_fibnum;
940 				INP_WUNLOCK(inp);
941 				error = 0;
942 				break;
943 			default:
944 				break;
945 			}
946 		}
947 		return (error);
948 	}
949 
950 	switch (sopt->sopt_dir) {
951 	case SOPT_SET:
952 		switch (sopt->sopt_name) {
953 		case IP_OPTIONS:
954 #ifdef notyet
955 		case IP_RETOPTS:
956 #endif
957 		{
958 			struct mbuf *m;
959 			if (sopt->sopt_valsize > MLEN) {
960 				error = EMSGSIZE;
961 				break;
962 			}
963 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
964 			if (m == NULL) {
965 				error = ENOBUFS;
966 				break;
967 			}
968 			m->m_len = sopt->sopt_valsize;
969 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
970 					    m->m_len);
971 			if (error) {
972 				m_free(m);
973 				break;
974 			}
975 			INP_WLOCK(inp);
976 			error = ip_pcbopts(inp, sopt->sopt_name, m);
977 			INP_WUNLOCK(inp);
978 			return (error);
979 		}
980 
981 		case IP_BINDANY:
982 			if (sopt->sopt_td != NULL) {
983 				error = priv_check(sopt->sopt_td,
984 				    PRIV_NETINET_BINDANY);
985 				if (error)
986 					break;
987 			}
988 			/* FALLTHROUGH */
989 		case IP_TOS:
990 		case IP_TTL:
991 		case IP_MINTTL:
992 		case IP_RECVOPTS:
993 		case IP_RECVRETOPTS:
994 		case IP_RECVDSTADDR:
995 		case IP_RECVTTL:
996 		case IP_RECVIF:
997 		case IP_FAITH:
998 		case IP_ONESBCAST:
999 		case IP_DONTFRAG:
1000 		case IP_RECVTOS:
1001 			error = sooptcopyin(sopt, &optval, sizeof optval,
1002 					    sizeof optval);
1003 			if (error)
1004 				break;
1005 
1006 			switch (sopt->sopt_name) {
1007 			case IP_TOS:
1008 				inp->inp_ip_tos = optval;
1009 				break;
1010 
1011 			case IP_TTL:
1012 				inp->inp_ip_ttl = optval;
1013 				break;
1014 
1015 			case IP_MINTTL:
1016 				if (optval >= 0 && optval <= MAXTTL)
1017 					inp->inp_ip_minttl = optval;
1018 				else
1019 					error = EINVAL;
1020 				break;
1021 
1022 #define	OPTSET(bit) do {						\
1023 	INP_WLOCK(inp);							\
1024 	if (optval)							\
1025 		inp->inp_flags |= bit;					\
1026 	else								\
1027 		inp->inp_flags &= ~bit;					\
1028 	INP_WUNLOCK(inp);						\
1029 } while (0)
1030 
1031 			case IP_RECVOPTS:
1032 				OPTSET(INP_RECVOPTS);
1033 				break;
1034 
1035 			case IP_RECVRETOPTS:
1036 				OPTSET(INP_RECVRETOPTS);
1037 				break;
1038 
1039 			case IP_RECVDSTADDR:
1040 				OPTSET(INP_RECVDSTADDR);
1041 				break;
1042 
1043 			case IP_RECVTTL:
1044 				OPTSET(INP_RECVTTL);
1045 				break;
1046 
1047 			case IP_RECVIF:
1048 				OPTSET(INP_RECVIF);
1049 				break;
1050 
1051 			case IP_FAITH:
1052 				OPTSET(INP_FAITH);
1053 				break;
1054 
1055 			case IP_ONESBCAST:
1056 				OPTSET(INP_ONESBCAST);
1057 				break;
1058 			case IP_DONTFRAG:
1059 				OPTSET(INP_DONTFRAG);
1060 				break;
1061 			case IP_BINDANY:
1062 				OPTSET(INP_BINDANY);
1063 				break;
1064 			case IP_RECVTOS:
1065 				OPTSET(INP_RECVTOS);
1066 				break;
1067 			}
1068 			break;
1069 #undef OPTSET
1070 
1071 		/*
1072 		 * Multicast socket options are processed by the in_mcast
1073 		 * module.
1074 		 */
1075 		case IP_MULTICAST_IF:
1076 		case IP_MULTICAST_VIF:
1077 		case IP_MULTICAST_TTL:
1078 		case IP_MULTICAST_LOOP:
1079 		case IP_ADD_MEMBERSHIP:
1080 		case IP_DROP_MEMBERSHIP:
1081 		case IP_ADD_SOURCE_MEMBERSHIP:
1082 		case IP_DROP_SOURCE_MEMBERSHIP:
1083 		case IP_BLOCK_SOURCE:
1084 		case IP_UNBLOCK_SOURCE:
1085 		case IP_MSFILTER:
1086 		case MCAST_JOIN_GROUP:
1087 		case MCAST_LEAVE_GROUP:
1088 		case MCAST_JOIN_SOURCE_GROUP:
1089 		case MCAST_LEAVE_SOURCE_GROUP:
1090 		case MCAST_BLOCK_SOURCE:
1091 		case MCAST_UNBLOCK_SOURCE:
1092 			error = inp_setmoptions(inp, sopt);
1093 			break;
1094 
1095 		case IP_PORTRANGE:
1096 			error = sooptcopyin(sopt, &optval, sizeof optval,
1097 					    sizeof optval);
1098 			if (error)
1099 				break;
1100 
1101 			INP_WLOCK(inp);
1102 			switch (optval) {
1103 			case IP_PORTRANGE_DEFAULT:
1104 				inp->inp_flags &= ~(INP_LOWPORT);
1105 				inp->inp_flags &= ~(INP_HIGHPORT);
1106 				break;
1107 
1108 			case IP_PORTRANGE_HIGH:
1109 				inp->inp_flags &= ~(INP_LOWPORT);
1110 				inp->inp_flags |= INP_HIGHPORT;
1111 				break;
1112 
1113 			case IP_PORTRANGE_LOW:
1114 				inp->inp_flags &= ~(INP_HIGHPORT);
1115 				inp->inp_flags |= INP_LOWPORT;
1116 				break;
1117 
1118 			default:
1119 				error = EINVAL;
1120 				break;
1121 			}
1122 			INP_WUNLOCK(inp);
1123 			break;
1124 
1125 #ifdef IPSEC
1126 		case IP_IPSEC_POLICY:
1127 		{
1128 			caddr_t req;
1129 			struct mbuf *m;
1130 
1131 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1132 				break;
1133 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1134 				break;
1135 			req = mtod(m, caddr_t);
1136 			error = ipsec_set_policy(inp, sopt->sopt_name, req,
1137 			    m->m_len, (sopt->sopt_td != NULL) ?
1138 			    sopt->sopt_td->td_ucred : NULL);
1139 			m_freem(m);
1140 			break;
1141 		}
1142 #endif /* IPSEC */
1143 
1144 		default:
1145 			error = ENOPROTOOPT;
1146 			break;
1147 		}
1148 		break;
1149 
1150 	case SOPT_GET:
1151 		switch (sopt->sopt_name) {
1152 		case IP_OPTIONS:
1153 		case IP_RETOPTS:
1154 			if (inp->inp_options)
1155 				error = sooptcopyout(sopt,
1156 						     mtod(inp->inp_options,
1157 							  char *),
1158 						     inp->inp_options->m_len);
1159 			else
1160 				sopt->sopt_valsize = 0;
1161 			break;
1162 
1163 		case IP_TOS:
1164 		case IP_TTL:
1165 		case IP_MINTTL:
1166 		case IP_RECVOPTS:
1167 		case IP_RECVRETOPTS:
1168 		case IP_RECVDSTADDR:
1169 		case IP_RECVTTL:
1170 		case IP_RECVIF:
1171 		case IP_PORTRANGE:
1172 		case IP_FAITH:
1173 		case IP_ONESBCAST:
1174 		case IP_DONTFRAG:
1175 		case IP_BINDANY:
1176 		case IP_RECVTOS:
1177 		case IP_FLOWID:
1178 		case IP_FLOWTYPE:
1179 #ifdef	RSS
1180 		case IP_RSSCPUID:
1181 #endif
1182 			switch (sopt->sopt_name) {
1183 
1184 			case IP_TOS:
1185 				optval = inp->inp_ip_tos;
1186 				break;
1187 
1188 			case IP_TTL:
1189 				optval = inp->inp_ip_ttl;
1190 				break;
1191 
1192 			case IP_MINTTL:
1193 				optval = inp->inp_ip_minttl;
1194 				break;
1195 
1196 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1197 
1198 			case IP_RECVOPTS:
1199 				optval = OPTBIT(INP_RECVOPTS);
1200 				break;
1201 
1202 			case IP_RECVRETOPTS:
1203 				optval = OPTBIT(INP_RECVRETOPTS);
1204 				break;
1205 
1206 			case IP_RECVDSTADDR:
1207 				optval = OPTBIT(INP_RECVDSTADDR);
1208 				break;
1209 
1210 			case IP_RECVTTL:
1211 				optval = OPTBIT(INP_RECVTTL);
1212 				break;
1213 
1214 			case IP_RECVIF:
1215 				optval = OPTBIT(INP_RECVIF);
1216 				break;
1217 
1218 			case IP_PORTRANGE:
1219 				if (inp->inp_flags & INP_HIGHPORT)
1220 					optval = IP_PORTRANGE_HIGH;
1221 				else if (inp->inp_flags & INP_LOWPORT)
1222 					optval = IP_PORTRANGE_LOW;
1223 				else
1224 					optval = 0;
1225 				break;
1226 
1227 			case IP_FAITH:
1228 				optval = OPTBIT(INP_FAITH);
1229 				break;
1230 
1231 			case IP_ONESBCAST:
1232 				optval = OPTBIT(INP_ONESBCAST);
1233 				break;
1234 			case IP_DONTFRAG:
1235 				optval = OPTBIT(INP_DONTFRAG);
1236 				break;
1237 			case IP_BINDANY:
1238 				optval = OPTBIT(INP_BINDANY);
1239 				break;
1240 			case IP_RECVTOS:
1241 				optval = OPTBIT(INP_RECVTOS);
1242 				break;
1243 			case IP_FLOWID:
1244 				optval = inp->inp_flowid;
1245 				break;
1246 			case IP_FLOWTYPE:
1247 				optval = inp->inp_flowtype;
1248 				break;
1249 #ifdef	RSS
1250 			case IP_RSSCPUID:
1251 				optval = rss_hash2cpuid(inp->inp_flowid,
1252 				    inp->inp_flowtype);
1253 				break;
1254 #endif
1255 			}
1256 			error = sooptcopyout(sopt, &optval, sizeof optval);
1257 			break;
1258 
1259 		/*
1260 		 * Multicast socket options are processed by the in_mcast
1261 		 * module.
1262 		 */
1263 		case IP_MULTICAST_IF:
1264 		case IP_MULTICAST_VIF:
1265 		case IP_MULTICAST_TTL:
1266 		case IP_MULTICAST_LOOP:
1267 		case IP_MSFILTER:
1268 			error = inp_getmoptions(inp, sopt);
1269 			break;
1270 
1271 #ifdef IPSEC
1272 		case IP_IPSEC_POLICY:
1273 		{
1274 			struct mbuf *m = NULL;
1275 			caddr_t req = NULL;
1276 			size_t len = 0;
1277 
1278 			if (m != 0) {
1279 				req = mtod(m, caddr_t);
1280 				len = m->m_len;
1281 			}
1282 			error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
1283 			if (error == 0)
1284 				error = soopt_mcopyout(sopt, m); /* XXX */
1285 			if (error == 0)
1286 				m_freem(m);
1287 			break;
1288 		}
1289 #endif /* IPSEC */
1290 
1291 		default:
1292 			error = ENOPROTOOPT;
1293 			break;
1294 		}
1295 		break;
1296 	}
1297 	return (error);
1298 }
1299 
1300 /*
1301  * Routine called from ip_output() to loop back a copy of an IP multicast
1302  * packet to the input queue of a specified interface.  Note that this
1303  * calls the output routine of the loopback "driver", but with an interface
1304  * pointer that might NOT be a loopback interface -- evil, but easier than
1305  * replicating that code here.
1306  */
1307 static void
1308 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1309     int hlen)
1310 {
1311 	register struct ip *ip;
1312 	struct mbuf *copym;
1313 
1314 	/*
1315 	 * Make a deep copy of the packet because we're going to
1316 	 * modify the pack in order to generate checksums.
1317 	 */
1318 	copym = m_dup(m, M_NOWAIT);
1319 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1320 		copym = m_pullup(copym, hlen);
1321 	if (copym != NULL) {
1322 		/* If needed, compute the checksum and mark it as valid. */
1323 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1324 			in_delayed_cksum(copym);
1325 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1326 			copym->m_pkthdr.csum_flags |=
1327 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1328 			copym->m_pkthdr.csum_data = 0xffff;
1329 		}
1330 		/*
1331 		 * We don't bother to fragment if the IP length is greater
1332 		 * than the interface's MTU.  Can this possibly matter?
1333 		 */
1334 		ip = mtod(copym, struct ip *);
1335 		ip->ip_sum = 0;
1336 		ip->ip_sum = in_cksum(copym, hlen);
1337 #if 1 /* XXX */
1338 		if (dst->sin_family != AF_INET) {
1339 			printf("ip_mloopback: bad address family %d\n",
1340 						dst->sin_family);
1341 			dst->sin_family = AF_INET;
1342 		}
1343 #endif
1344 		if_simloop(ifp, copym, dst->sin_family, 0);
1345 	}
1346 }
1347