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