xref: /freebsd/sys/netinet/ip_output.c (revision 7d98cc096b995ca3bfd85277ed009b0f872c3e1b)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
344b421e2dSMike Silbersack #include <sys/cdefs.h>
354b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
364b421e2dSMike Silbersack 
375d6d7e75SGleb Smirnoff #include "opt_inet.h"
386a800098SYoshinobu Inoue #include "opt_ipsec.h"
39b2e60773SJohn Baldwin #include "opt_kern_tls.h"
4053dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
4177a01441SJohn Baldwin #include "opt_ratelimit.h"
4257f60867SMark Johnston #include "opt_route.h"
439c423972SAdrian Chadd #include "opt_rss.h"
4477a01441SJohn Baldwin #include "opt_sctp.h"
45fbd1372aSJoerg Wunsch 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
4726f9a767SRodney W. Grimes #include <sys/systm.h>
48f0f6d643SLuigi Rizzo #include <sys/kernel.h>
49b2e60773SJohn Baldwin #include <sys/ktls.h>
50cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
51df8bae1dSRodney W. Grimes #include <sys/malloc.h>
52df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
53acd3428bSRobert Watson #include <sys/priv.h>
54c26fe973SBjoern A. Zeeb #include <sys/proc.h>
55df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5657f60867SMark Johnston #include <sys/sdt.h>
57df8bae1dSRodney W. Grimes #include <sys/socket.h>
58df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
599d9edc56SMike Silbersack #include <sys/sysctl.h>
60c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <net/if.h>
6376039bc8SGleb Smirnoff #include <net/if_var.h>
64868aabb4SRichard Scheffenegger #include <net/if_vlan_var.h>
653ef5e21dSQing Li #include <net/if_llatbl.h>
66868aabb4SRichard Scheffenegger #include <net/ethernet.h>
679b932e9eSAndre Oppermann #include <net/netisr.h>
68c21fd232SAndre Oppermann #include <net/pfil.h>
69df8bae1dSRodney W. Grimes #include <net/route.h>
70983066f0SAlexander V. Chernikov #include <net/route/nhop.h>
71b2bdc62aSAdrian Chadd #include <net/rss_config.h>
724b79449eSBjoern A. Zeeb #include <net/vnet.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <netinet/in.h>
756ca363ebSGleb Smirnoff #include <netinet/in_fib.h>
7657f60867SMark Johnston #include <netinet/in_kdtrace.h>
77df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
78df8bae1dSRodney W. Grimes #include <netinet/ip.h>
79983066f0SAlexander V. Chernikov #include <netinet/in_fib.h>
80df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
819c423972SAdrian Chadd #include <netinet/in_rss.h>
82df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
83df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
84ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
851fdbfb90STom Jones 
861fdbfb90STom Jones #include <netinet/udp.h>
871fdbfb90STom Jones #include <netinet/udp_var.h>
881fdbfb90STom Jones 
8995033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
902f4afd21SRandall Stewart #include <netinet/sctp.h>
912f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
922f4afd21SRandall Stewart #endif
93df8bae1dSRodney W. Grimes 
94fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
956a800098SYoshinobu Inoue 
961dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
971dfcf0d2SAndre Oppermann 
98aed55708SRobert Watson #include <security/mac/mac_framework.h>
99aed55708SRobert Watson 
10053dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
10105b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0;
1029d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
1039d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1049d9edc56SMike Silbersack #endif
1059d9edc56SMike Silbersack 
106331dff07SAlexander V. Chernikov static void	ip_mloopback(struct ifnet *, const struct mbuf *, int);
107afed1b49SDarren Reed 
1088b889dbbSBruce M Simpson extern int in_mcast_loop;
10993e0e116SJulian Elischer extern	struct protosw inetsw[];
11093e0e116SJulian Elischer 
111d9f2a782SErmal Luçi static inline int
11205fc9d78SKristof Provost ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, int flags,
11305fc9d78SKristof Provost     struct inpcb *inp, struct sockaddr_in *dst, int *fibnum, int *error)
114d9f2a782SErmal Luçi {
115d9f2a782SErmal Luçi 	struct m_tag *fwd_tag = NULL;
1168f980c01SMark Johnston 	struct mbuf *m;
117d9f2a782SErmal Luçi 	struct in_addr odst;
118d9f2a782SErmal Luçi 	struct ip *ip;
11905fc9d78SKristof Provost 	int pflags = PFIL_OUT;
12005fc9d78SKristof Provost 
12105fc9d78SKristof Provost 	if (flags & IP_FORWARDING)
12205fc9d78SKristof Provost 		pflags |= PFIL_FWD;
123d9f2a782SErmal Luçi 
1248f980c01SMark Johnston 	m = *mp;
125d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
126d9f2a782SErmal Luçi 
127d9f2a782SErmal Luçi 	/* Run through list of hooks for output packets. */
128d9f2a782SErmal Luçi 	odst.s_addr = ip->ip_dst.s_addr;
12905fc9d78SKristof Provost 	switch (pfil_run_hooks(V_inet_pfil_head, mp, ifp, pflags, inp)) {
130b252313fSGleb Smirnoff 	case PFIL_DROPPED:
1318d5c56daSGleb Smirnoff 		*error = EACCES;
132b252313fSGleb Smirnoff 		/* FALLTHROUGH */
133b252313fSGleb Smirnoff 	case PFIL_CONSUMED:
134d9f2a782SErmal Luçi 		return 1; /* Finished */
135b252313fSGleb Smirnoff 	case PFIL_PASS:
136b252313fSGleb Smirnoff 		*error = 0;
137b252313fSGleb Smirnoff 	}
138b252313fSGleb Smirnoff 	m = *mp;
139d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
140d9f2a782SErmal Luçi 
141d9f2a782SErmal Luçi 	/* See if destination IP address was changed by packet filter. */
142d9f2a782SErmal Luçi 	if (odst.s_addr != ip->ip_dst.s_addr) {
143d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
144d9f2a782SErmal Luçi 		/* If destination is now ourself drop to ip_input(). */
145d9f2a782SErmal Luçi 		if (in_localip(ip->ip_dst)) {
146d9f2a782SErmal Luçi 			m->m_flags |= M_FASTFWD_OURS;
147d9f2a782SErmal Luçi 			if (m->m_pkthdr.rcvif == NULL)
148d9f2a782SErmal Luçi 				m->m_pkthdr.rcvif = V_loif;
149d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
150d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |=
151d9f2a782SErmal Luçi 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
152d9f2a782SErmal Luçi 				m->m_pkthdr.csum_data = 0xffff;
153d9f2a782SErmal Luçi 			}
154d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
155d9f2a782SErmal Luçi 				CSUM_IP_CHECKED | CSUM_IP_VALID;
15695033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
157d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
158d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
159d9f2a782SErmal Luçi #endif
160d9f2a782SErmal Luçi 			*error = netisr_queue(NETISR_IP, m);
161d9f2a782SErmal Luçi 			return 1; /* Finished */
162d9f2a782SErmal Luçi 		}
163d9f2a782SErmal Luçi 
164d9f2a782SErmal Luçi 		bzero(dst, sizeof(*dst));
165d9f2a782SErmal Luçi 		dst->sin_family = AF_INET;
166d9f2a782SErmal Luçi 		dst->sin_len = sizeof(*dst);
167d9f2a782SErmal Luçi 		dst->sin_addr = ip->ip_dst;
168d9f2a782SErmal Luçi 
169d9f2a782SErmal Luçi 		return -1; /* Reloop */
170d9f2a782SErmal Luçi 	}
171d9f2a782SErmal Luçi 	/* See if fib was changed by packet filter. */
172d9f2a782SErmal Luçi 	if ((*fibnum) != M_GETFIB(m)) {
173d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
174d9f2a782SErmal Luçi 		*fibnum = M_GETFIB(m);
175d9f2a782SErmal Luçi 		return -1; /* Reloop for FIB change */
176d9f2a782SErmal Luçi 	}
177d9f2a782SErmal Luçi 
178d9f2a782SErmal Luçi 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
179d9f2a782SErmal Luçi 	if (m->m_flags & M_FASTFWD_OURS) {
180d9f2a782SErmal Luçi 		if (m->m_pkthdr.rcvif == NULL)
181d9f2a782SErmal Luçi 			m->m_pkthdr.rcvif = V_loif;
182d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
183d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
184d9f2a782SErmal Luçi 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
185d9f2a782SErmal Luçi 			m->m_pkthdr.csum_data = 0xffff;
186d9f2a782SErmal Luçi 		}
18795033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
188d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
189d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
190d9f2a782SErmal Luçi #endif
191d9f2a782SErmal Luçi 		m->m_pkthdr.csum_flags |=
192d9f2a782SErmal Luçi 			CSUM_IP_CHECKED | CSUM_IP_VALID;
193d9f2a782SErmal Luçi 
194d9f2a782SErmal Luçi 		*error = netisr_queue(NETISR_IP, m);
195d9f2a782SErmal Luçi 		return 1; /* Finished */
196d9f2a782SErmal Luçi 	}
197d9f2a782SErmal Luçi 	/* Or forward to some other address? */
198d9f2a782SErmal Luçi 	if ((m->m_flags & M_IP_NEXTHOP) &&
199d9f2a782SErmal Luçi 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
200d9f2a782SErmal Luçi 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
201d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
202d9f2a782SErmal Luçi 		m->m_flags &= ~M_IP_NEXTHOP;
203d9f2a782SErmal Luçi 		m_tag_delete(m, fwd_tag);
204d9f2a782SErmal Luçi 
205d9f2a782SErmal Luçi 		return -1; /* Reloop for CHANGE of dst */
206d9f2a782SErmal Luçi 	}
207d9f2a782SErmal Luçi 
208d9f2a782SErmal Luçi 	return 0;
209d9f2a782SErmal Luçi }
210d9f2a782SErmal Luçi 
211fb3bc596SJohn Baldwin static int
212fb3bc596SJohn Baldwin ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m,
21362e1a437SZhenlei Huang     const struct sockaddr *gw, struct route *ro, bool stamp_tag)
214fb3bc596SJohn Baldwin {
215b2e60773SJohn Baldwin #ifdef KERN_TLS
216b2e60773SJohn Baldwin 	struct ktls_session *tls = NULL;
217b2e60773SJohn Baldwin #endif
218fb3bc596SJohn Baldwin 	struct m_snd_tag *mst;
219fb3bc596SJohn Baldwin 	int error;
220fb3bc596SJohn Baldwin 
221fb3bc596SJohn Baldwin 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
222fb3bc596SJohn Baldwin 	mst = NULL;
223fb3bc596SJohn Baldwin 
224b2e60773SJohn Baldwin #ifdef KERN_TLS
225b2e60773SJohn Baldwin 	/*
226b2e60773SJohn Baldwin 	 * If this is an unencrypted TLS record, save a reference to
227b2e60773SJohn Baldwin 	 * the record.  This local reference is used to call
228b2e60773SJohn Baldwin 	 * ktls_output_eagain after the mbuf has been freed (thus
229b2e60773SJohn Baldwin 	 * dropping the mbuf's reference) in if_output.
230b2e60773SJohn Baldwin 	 */
231b2e60773SJohn Baldwin 	if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
2327b6c99d0SGleb Smirnoff 		tls = ktls_hold(m->m_next->m_epg_tls);
233b2e60773SJohn Baldwin 		mst = tls->snd_tag;
234b2e60773SJohn Baldwin 
235b2e60773SJohn Baldwin 		/*
236b2e60773SJohn Baldwin 		 * If a TLS session doesn't have a valid tag, it must
237b2e60773SJohn Baldwin 		 * have had an earlier ifp mismatch, so drop this
238b2e60773SJohn Baldwin 		 * packet.
239b2e60773SJohn Baldwin 		 */
240b2e60773SJohn Baldwin 		if (mst == NULL) {
2419ba11796SAndrew Gallatin 			m_freem(m);
242b2e60773SJohn Baldwin 			error = EAGAIN;
243b2e60773SJohn Baldwin 			goto done;
244b2e60773SJohn Baldwin 		}
2456043ac20SAndrew Gallatin 		/*
2466043ac20SAndrew Gallatin 		 * Always stamp tags that include NIC ktls.
2476043ac20SAndrew Gallatin 		 */
2486043ac20SAndrew Gallatin 		stamp_tag = true;
249b2e60773SJohn Baldwin 	}
250b2e60773SJohn Baldwin #endif
251fb3bc596SJohn Baldwin #ifdef RATELIMIT
252b2e60773SJohn Baldwin 	if (inp != NULL && mst == NULL) {
253fb3bc596SJohn Baldwin 		if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
254fb3bc596SJohn Baldwin 		    (inp->inp_snd_tag != NULL &&
255fb3bc596SJohn Baldwin 		    inp->inp_snd_tag->ifp != ifp))
256fb3bc596SJohn Baldwin 			in_pcboutput_txrtlmt(inp, ifp, m);
257fb3bc596SJohn Baldwin 
258fb3bc596SJohn Baldwin 		if (inp->inp_snd_tag != NULL)
259fb3bc596SJohn Baldwin 			mst = inp->inp_snd_tag;
260fb3bc596SJohn Baldwin 	}
261fb3bc596SJohn Baldwin #endif
26235c7bb34SRandall Stewart 	if (stamp_tag && mst != NULL) {
263fb3bc596SJohn Baldwin 		KASSERT(m->m_pkthdr.rcvif == NULL,
264fb3bc596SJohn Baldwin 		    ("trying to add a send tag to a forwarded packet"));
265fb3bc596SJohn Baldwin 		if (mst->ifp != ifp) {
2669ba11796SAndrew Gallatin 			m_freem(m);
267fb3bc596SJohn Baldwin 			error = EAGAIN;
268fb3bc596SJohn Baldwin 			goto done;
269fb3bc596SJohn Baldwin 		}
270fb3bc596SJohn Baldwin 
271fb3bc596SJohn Baldwin 		/* stamp send tag on mbuf */
272fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
273fb3bc596SJohn Baldwin 		m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
274fb3bc596SJohn Baldwin 	}
275fb3bc596SJohn Baldwin 
27662e1a437SZhenlei Huang 	error = (*ifp->if_output)(ifp, m, gw, ro);
277fb3bc596SJohn Baldwin 
278fb3bc596SJohn Baldwin done:
279fb3bc596SJohn Baldwin 	/* Check for route change invalidating send tags. */
280b2e60773SJohn Baldwin #ifdef KERN_TLS
281b2e60773SJohn Baldwin 	if (tls != NULL) {
282b2e60773SJohn Baldwin 		if (error == EAGAIN)
283b2e60773SJohn Baldwin 			error = ktls_output_eagain(inp, tls);
284b2e60773SJohn Baldwin 		ktls_free(tls);
285b2e60773SJohn Baldwin 	}
286b2e60773SJohn Baldwin #endif
287fb3bc596SJohn Baldwin #ifdef RATELIMIT
288fb3bc596SJohn Baldwin 	if (error == EAGAIN)
289fb3bc596SJohn Baldwin 		in_pcboutput_eagain(inp);
290fb3bc596SJohn Baldwin #endif
291fb3bc596SJohn Baldwin 	return (error);
292fb3bc596SJohn Baldwin }
293fb3bc596SJohn Baldwin 
294983066f0SAlexander V. Chernikov /* rte<>ro_flags translation */
295983066f0SAlexander V. Chernikov static inline void
2969748eb74SAlexander V. Chernikov rt_update_ro_flags(struct route *ro, const struct nhop_object *nh)
297983066f0SAlexander V. Chernikov {
2989748eb74SAlexander V. Chernikov 	int nh_flags = nh->nh_flags;
299983066f0SAlexander V. Chernikov 
300983066f0SAlexander V. Chernikov 	ro->ro_flags &= ~ (RT_REJECT|RT_BLACKHOLE|RT_HAS_GW);
301983066f0SAlexander V. Chernikov 
302983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_REJECT) ? RT_REJECT : 0;
303983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_BLACKHOLE) ? RT_BLACKHOLE : 0;
304983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_GATEWAY) ? RT_HAS_GW : 0;
305983066f0SAlexander V. Chernikov }
306983066f0SAlexander V. Chernikov 
307df8bae1dSRodney W. Grimes /*
308df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
309df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
310df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
311df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
312bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
313bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
314bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
315bf984051SGleb Smirnoff  *
3163de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
3173de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
318df8bae1dSRodney W. Grimes  */
319df8bae1dSRodney W. Grimes int
320f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
321f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
322df8bae1dSRodney W. Grimes {
3231e78ac21SJeffrey Hsu 	struct ip *ip;
324fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
325ac9d7e26SMax Laier 	struct mbuf *m0;
32623bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
327374ce248SMitchell Horne 	int mtu = 0;
328ca8b83b0SLuigi Rizzo 	int error = 0;
329868aabb4SRichard Scheffenegger 	int vlan_pcp = -1;
33062e1a437SZhenlei Huang 	struct sockaddr_in *dst;
33162e1a437SZhenlei Huang 	const struct sockaddr *gw;
332374ce248SMitchell Horne 	struct in_ifaddr *ia = NULL;
3336ca363ebSGleb Smirnoff 	struct in_addr src;
33421d172a3SGleb Smirnoff 	int isbroadcast;
335078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
33662e1a437SZhenlei Huang 	struct route iproute;
3379c57a5b6SHiroki Sato 	uint32_t fibnum;
338fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
3391a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
3401a499816SEdward Tomasz Napierala #endif
34177a01441SJohn Baldwin 
342ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
343b9555453SGleb Smirnoff 	NET_EPOCH_ASSERT();
34436e8826fSMax Laier 
345bc97ba51SJulian Elischer 	if (inp != NULL) {
346baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
34762e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
348c2529042SHans Petter Selasky 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
34980cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
3509c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
35180cb9f21SKip Macy 		}
352868aabb4SRichard Scheffenegger 		if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
353868aabb4SRichard Scheffenegger 			vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
354868aabb4SRichard Scheffenegger 			    INP_2PCP_SHIFT;
35550575ce1SAndrew Gallatin #ifdef NUMA
35650575ce1SAndrew Gallatin 		m->m_pkthdr.numa_domain = inp->inp_numa_domain;
35750575ce1SAndrew Gallatin #endif
358bc97ba51SJulian Elischer 	}
35962e1ba83SRobert Watson 
360df8bae1dSRodney W. Grimes 	if (opt) {
361ca8b83b0SLuigi Rizzo 		int len = 0;
362df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
363cb7641e8SMaxim Konovalov 		if (len != 0)
364ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
365df8bae1dSRodney W. Grimes 	}
366df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
3678f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
3688f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
3693efc3014SJulian Elischer 
370df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
37153be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
37253be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
3736d947416SGleb Smirnoff 		ip_fillid(ip);
374df8bae1dSRodney W. Grimes 	} else {
375ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
37653be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
377df8bae1dSRodney W. Grimes 	}
3783924dfa7SMichael Tuexen 	if ((flags & IP_FORWARDING) == 0)
3793924dfa7SMichael Tuexen 		IPSTAT_INC(ips_localout);
3809c9137eaSGarrett Wollman 
381054692a4SAlexander V. Chernikov 	/*
382054692a4SAlexander V. Chernikov 	 * dst/gw handling:
383054692a4SAlexander V. Chernikov 	 *
3843c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
3853c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
386054692a4SAlexander V. Chernikov 	 */
3879c57a5b6SHiroki Sato 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
38862e1a437SZhenlei Huang 	if (ro == NULL) {
38962e1a437SZhenlei Huang 		ro = &iproute;
39062e1a437SZhenlei Huang 		bzero(ro, sizeof (*ro));
39162e1a437SZhenlei Huang 	}
3926ca363ebSGleb Smirnoff 	dst = (struct sockaddr_in *)&ro->ro_dst;
39362e1a437SZhenlei Huang 	if (ro->ro_nh == NULL) {
394df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
395df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
3969b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
397df8bae1dSRodney W. Grimes 	}
39862e1a437SZhenlei Huang 	gw = (const struct sockaddr *)dst;
399d9f2a782SErmal Luçi again:
40084cc0778SGeorge V. Neville-Neil 	/*
40184cc0778SGeorge V. Neville-Neil 	 * Validate route against routing table additions;
40284cc0778SGeorge V. Neville-Neil 	 * a better/more specific route might have been added.
40384cc0778SGeorge V. Neville-Neil 	 */
40462e1a437SZhenlei Huang 	if (inp != NULL && ro->ro_nh != NULL)
405983066f0SAlexander V. Chernikov 		NH_VALIDATE(ro, &inp->inp_rt_cookie, fibnum);
40684cc0778SGeorge V. Neville-Neil 	/*
40784cc0778SGeorge V. Neville-Neil 	 * If there is a cached route,
40884cc0778SGeorge V. Neville-Neil 	 * check that it is to the same destination
40984cc0778SGeorge V. Neville-Neil 	 * and is still up.  If not, free it and try again.
41084cc0778SGeorge V. Neville-Neil 	 * The address family should also be checked in case of sharing the
41184cc0778SGeorge V. Neville-Neil 	 * cache with IPv6.
41284cc0778SGeorge V. Neville-Neil 	 * Also check whether routing cache needs invalidation.
41384cc0778SGeorge V. Neville-Neil 	 */
41462e1a437SZhenlei Huang 	if (ro->ro_nh != NULL &&
415174fb9dbSAlexander V. Chernikov 	    ((!NH_IS_VALID(ro->ro_nh)) || dst->sin_family != AF_INET ||
4166ca363ebSGleb Smirnoff 	    dst->sin_addr.s_addr != ip->ip_dst.s_addr))
417fc21c53fSRyan Stone 		RO_INVALIDATE_CACHE(ro);
418d9f2a782SErmal Luçi 	ia = NULL;
419df8bae1dSRodney W. Grimes 	/*
420a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
421a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
422a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
423a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
424df8bae1dSRodney W. Grimes 	 */
425a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
4264f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
42758a39d8cSAlan Somers 						      M_GETFIB(m)))) == NULL &&
4284f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
42958a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL) {
43086425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
431a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
432a3fd02d8SBruce M Simpson 			goto bad;
433a3fd02d8SBruce M Simpson 		}
434a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
435a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
436a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
4376ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
438a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
439a3fd02d8SBruce M Simpson 		isbroadcast = 1;
4406ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
441a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
4424f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
44358a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL &&
4444f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
44558a39d8cSAlan Somers 						M_GETFIB(m)))) == NULL) {
44686425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
447df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
448df8bae1dSRodney W. Grimes 			goto bad;
449df8bae1dSRodney W. Grimes 		}
450df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
4516ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
452df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
4536c1bd558SRyan Stone 		isbroadcast = ifp->if_flags & IFF_BROADCAST ?
4546c1bd558SRyan Stone 		    in_ifaddr_broadcast(dst->sin_addr, ia) : 0;
4556ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
4563afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
45738c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
4583afefa39SDaniel C. Sobral 		/*
45938c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
46038c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
4613afefa39SDaniel C. Sobral 		 */
46238c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
4636ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
4642144431cSGleb Smirnoff 		IFP_TO_IA(ifp, ia);
46538c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
46654bb7ac0SGleb Smirnoff 		/* Interface may have no addresses. */
46754bb7ac0SGleb Smirnoff 		if (ia != NULL)
4686ca363ebSGleb Smirnoff 			src = IA_SIN(ia)->sin_addr;
46954bb7ac0SGleb Smirnoff 		else
47054bb7ac0SGleb Smirnoff 			src.s_addr = INADDR_ANY;
47162e1a437SZhenlei Huang 	} else if (ro != &iproute) {
472983066f0SAlexander V. Chernikov 		if (ro->ro_nh == NULL) {
4732c17fe93SGarrett Wollman 			/*
4746ca363ebSGleb Smirnoff 			 * We want to do any cloning requested by the link
4756ca363ebSGleb Smirnoff 			 * layer, as this is probably required in all cases
4766ca363ebSGleb Smirnoff 			 * for correct operation (as it is for ARP).
4772c17fe93SGarrett Wollman 			 */
4784043ee3cSAlexander V. Chernikov 			uint32_t flowid;
4794043ee3cSAlexander V. Chernikov 			flowid = m->m_pkthdr.flowid;
4804043ee3cSAlexander V. Chernikov 			ro->ro_nh = fib4_lookup(fibnum, dst->sin_addr, 0,
4814043ee3cSAlexander V. Chernikov 			    NHR_REF, flowid);
4824043ee3cSAlexander V. Chernikov 
483174fb9dbSAlexander V. Chernikov 			if (ro->ro_nh == NULL || (!NH_IS_VALID(ro->ro_nh))) {
484fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
4851a499816SEdward Tomasz Napierala 				/*
4861a499816SEdward Tomasz Napierala 				 * There is no route for this packet, but it is
4871a499816SEdward Tomasz Napierala 				 * possible that a matching SPD entry exists.
4881a499816SEdward Tomasz Napierala 				 */
4891a499816SEdward Tomasz Napierala 				no_route_but_check_spd = 1;
4901a499816SEdward Tomasz Napierala 				goto sendit;
4911a499816SEdward Tomasz Napierala #endif
49286425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
493df8bae1dSRodney W. Grimes 				error = EHOSTUNREACH;
494df8bae1dSRodney W. Grimes 				goto bad;
495df8bae1dSRodney W. Grimes 			}
4966ca363ebSGleb Smirnoff 		}
4979748eb74SAlexander V. Chernikov 		struct nhop_object *nh = ro->ro_nh;
4989748eb74SAlexander V. Chernikov 
4999748eb74SAlexander V. Chernikov 		ia = ifatoia(nh->nh_ifa);
5009748eb74SAlexander V. Chernikov 		ifp = nh->nh_ifp;
5019748eb74SAlexander V. Chernikov 		counter_u64_add(nh->nh_pksent, 1);
5029748eb74SAlexander V. Chernikov 		rt_update_ro_flags(ro, nh);
5039748eb74SAlexander V. Chernikov 		if (nh->nh_flags & NHF_GATEWAY)
50462e1a437SZhenlei Huang 			gw = &nh->gw_sa;
5059748eb74SAlexander V. Chernikov 		if (nh->nh_flags & NHF_HOST)
5069748eb74SAlexander V. Chernikov 			isbroadcast = (nh->nh_flags & NHF_BROADCAST);
50762e1a437SZhenlei Huang 		else if ((ifp->if_flags & IFF_BROADCAST) && (gw->sa_family == AF_INET))
50862e1a437SZhenlei Huang 			isbroadcast = in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia);
5096c1bd558SRyan Stone 		else
5106c1bd558SRyan Stone 			isbroadcast = 0;
5119748eb74SAlexander V. Chernikov 		mtu = nh->nh_mtu;
5126ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
5136ca363ebSGleb Smirnoff 	} else {
5143553b300SAlexander V. Chernikov 		struct nhop_object *nh;
5156ca363ebSGleb Smirnoff 
516*7d98cc09SAndrey V. Elsukov 		nh = fib4_lookup(M_GETFIB(m), dst->sin_addr, 0, NHR_NONE,
5172259a030SAlexander V. Chernikov 		    m->m_pkthdr.flowid);
5183553b300SAlexander V. Chernikov 		if (nh == NULL) {
5196ca363ebSGleb Smirnoff #if defined(IPSEC) || defined(IPSEC_SUPPORT)
5206ca363ebSGleb Smirnoff 			/*
5216ca363ebSGleb Smirnoff 			 * There is no route for this packet, but it is
5226ca363ebSGleb Smirnoff 			 * possible that a matching SPD entry exists.
5236ca363ebSGleb Smirnoff 			 */
5246ca363ebSGleb Smirnoff 			no_route_but_check_spd = 1;
5256ca363ebSGleb Smirnoff 			goto sendit;
5266ca363ebSGleb Smirnoff #endif
5276ca363ebSGleb Smirnoff 			IPSTAT_INC(ips_noroute);
5286ca363ebSGleb Smirnoff 			error = EHOSTUNREACH;
5296ca363ebSGleb Smirnoff 			goto bad;
5306ca363ebSGleb Smirnoff 		}
5313553b300SAlexander V. Chernikov 		ifp = nh->nh_ifp;
5323553b300SAlexander V. Chernikov 		mtu = nh->nh_mtu;
53362e1a437SZhenlei Huang 		rt_update_ro_flags(ro, nh);
5343553b300SAlexander V. Chernikov 		if (nh->nh_flags & NHF_GATEWAY)
53562e1a437SZhenlei Huang 			gw = &nh->gw_sa;
5363553b300SAlexander V. Chernikov 		ia = ifatoia(nh->nh_ifa);
5373553b300SAlexander V. Chernikov 		src = IA_SIN(ia)->sin_addr;
5383553b300SAlexander V. Chernikov 		isbroadcast = (((nh->nh_flags & (NHF_HOST | NHF_BROADCAST)) ==
5396ca363ebSGleb Smirnoff 		    (NHF_HOST | NHF_BROADCAST)) ||
5406ca363ebSGleb Smirnoff 		    ((ifp->if_flags & IFF_BROADCAST) &&
54162e1a437SZhenlei Huang 		    (gw->sa_family == AF_INET) &&
54262e1a437SZhenlei Huang 		    in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia)));
5436ca363ebSGleb Smirnoff 	}
5446ca363ebSGleb Smirnoff 
545c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
546983066f0SAlexander V. Chernikov 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, ro=%p (nh_flags=0x%08x) ifp=%p",
5476ca363ebSGleb Smirnoff 	    __func__, mtu, ro,
548983066f0SAlexander V. Chernikov 	    (ro != NULL && ro->ro_nh != NULL) ? ro->ro_nh->nh_flags : 0, ifp));
549d9f2a782SErmal Luçi 
5509b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
551df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
552df8bae1dSRodney W. Grimes 		/*
553183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
554183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
555183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
556183e1c86SGleb Smirnoff 		 */
55762e1a437SZhenlei Huang 		gw = (const struct sockaddr *)dst;
558183e1c86SGleb Smirnoff 		/*
559df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
560df8bae1dSRodney W. Grimes 		 */
561df8bae1dSRodney W. Grimes 		if (imo != NULL) {
562df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
5631c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
5641c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
565bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
566bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
567bbb4330bSLuigi Rizzo 				    INADDR_ANY;
568df8bae1dSRodney W. Grimes 		} else
569df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
570df8bae1dSRodney W. Grimes 		/*
571df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
572df8bae1dSRodney W. Grimes 		 */
5731c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
574df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
57586425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
576df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
577df8bae1dSRodney W. Grimes 				goto bad;
578df8bae1dSRodney W. Grimes 			}
5791c5de19aSGarrett Wollman 		}
580df8bae1dSRodney W. Grimes 		/*
581df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
582df8bae1dSRodney W. Grimes 		 * of outgoing interface.
583df8bae1dSRodney W. Grimes 		 */
5846ca363ebSGleb Smirnoff 		if (ip->ip_src.s_addr == INADDR_ANY)
5856ca363ebSGleb Smirnoff 			ip->ip_src = src;
586df8bae1dSRodney W. Grimes 
5878b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
5888b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
589df8bae1dSRodney W. Grimes 			/*
5908b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
5918b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
5928b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
5938b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
5948b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
595df8bae1dSRodney W. Grimes 			 */
596331dff07SAlexander V. Chernikov 			ip_mloopback(ifp, m, hlen);
5978b889dbbSBruce M Simpson 		} else {
598df8bae1dSRodney W. Grimes 			/*
599df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
600df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
601df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
602df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
603df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
604df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
605df8bae1dSRodney W. Grimes 			 *
606df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
607df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
608df8bae1dSRodney W. Grimes 			 * if necessary.
609df8bae1dSRodney W. Grimes 			 */
610603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
611f0068c4aSGarrett Wollman 				/*
612bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
613f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
614f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
615f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
616f0068c4aSGarrett Wollman 				 */
617603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
618f0068c4aSGarrett Wollman 					imo = NULL;
619bbb4330bSLuigi Rizzo 				if (ip_mforward &&
620bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
621df8bae1dSRodney W. Grimes 					m_freem(m);
622df8bae1dSRodney W. Grimes 					goto done;
623df8bae1dSRodney W. Grimes 				}
624df8bae1dSRodney W. Grimes 			}
625df8bae1dSRodney W. Grimes 		}
6265e9ae478SGarrett Wollman 
627df8bae1dSRodney W. Grimes 		/*
628df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
629df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
630df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
631df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
6328b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
6338b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
6348b889dbbSBruce M Simpson 		 * the loopback interface.
635df8bae1dSRodney W. Grimes 		 */
636f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
637df8bae1dSRodney W. Grimes 			m_freem(m);
638df8bae1dSRodney W. Grimes 			goto done;
639df8bae1dSRodney W. Grimes 		}
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes 		goto sendit;
642df8bae1dSRodney W. Grimes 	}
6436a7c943cSAndre Oppermann 
644df8bae1dSRodney W. Grimes 	/*
6452b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
646cb459254SJohn-Mark Gurney 	 * of the outoing interface.
647df8bae1dSRodney W. Grimes 	 */
6486ca363ebSGleb Smirnoff 	if (ip->ip_src.s_addr == INADDR_ANY)
6496ca363ebSGleb Smirnoff 		ip->ip_src = src;
6506a7c943cSAndre Oppermann 
651ca7a789aSMax Laier 	/*
652df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
6538c3f5566SRuslan Ermilov 	 * verify user is allowed to send
654df8bae1dSRodney W. Grimes 	 * such a packet.
655df8bae1dSRodney W. Grimes 	 */
6569f9b3dc4SGarrett Wollman 	if (isbroadcast) {
657df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
658df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
659df8bae1dSRodney W. Grimes 			goto bad;
660df8bae1dSRodney W. Grimes 		}
661df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
662df8bae1dSRodney W. Grimes 			error = EACCES;
663df8bae1dSRodney W. Grimes 			goto bad;
664df8bae1dSRodney W. Grimes 		}
665df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
6668f134647SGleb Smirnoff 		if (ip_len > mtu) {
667df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
668df8bae1dSRodney W. Grimes 			goto bad;
669df8bae1dSRodney W. Grimes 		}
670df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
6719f9b3dc4SGarrett Wollman 	} else {
672df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
6739f9b3dc4SGarrett Wollman 	}
674df8bae1dSRodney W. Grimes 
675f1743588SDarren Reed sendit:
676fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
677fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4)) {
678fcf59617SAndrey V. Elsukov 		if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) {
679fcf59617SAndrey V. Elsukov 			if (error == EINPROGRESS)
680fcf59617SAndrey V. Elsukov 				error = 0;
6811dfcf0d2SAndre Oppermann 			goto done;
682fcf59617SAndrey V. Elsukov 		}
68333841545SHajimu UMEMOTO 	}
6841a499816SEdward Tomasz Napierala 	/*
6851a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
6861a499816SEdward Tomasz Napierala 	 */
6871a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
6881a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
6891a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
6901a499816SEdward Tomasz Napierala 		goto bad;
6911a499816SEdward Tomasz Napierala 	}
6921dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
69333841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
69433841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
695b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
69633841545SHajimu UMEMOTO 
697c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
698b252313fSGleb Smirnoff 	if (PFIL_HOOKED_OUT(V_inet_pfil_head)) {
69905fc9d78SKristof Provost 		switch (ip_output_pfil(&m, ifp, flags, inp, dst, &fibnum,
70005fc9d78SKristof Provost 		    &error)) {
701d9f2a782SErmal Luçi 		case 1: /* Finished */
702c4ac87eaSDarren Reed 			goto done;
7039b932e9eSAndre Oppermann 
704d9f2a782SErmal Luçi 		case 0: /* Continue normally */
705c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
706d9f2a782SErmal Luçi 			break;
707fed1c7e9SSøren Schmidt 
708d9f2a782SErmal Luçi 		case -1: /* Need to try again */
709d9f2a782SErmal Luçi 			/* Reset everything for a new round */
7106ca363ebSGleb Smirnoff 			if (ro != NULL) {
711983066f0SAlexander V. Chernikov 				RO_NHFREE(ro);
7124fb3a820SAlexander V. Chernikov 				ro->ro_prepend = NULL;
7136ca363ebSGleb Smirnoff 			}
71462e1a437SZhenlei Huang 			gw = (const struct sockaddr *)dst;
715d9f2a782SErmal Luçi 			ip = mtod(m, struct ip *);
7169b932e9eSAndre Oppermann 			goto again;
717d9f2a782SErmal Luçi 		}
718099dd043SAndre Oppermann 	}
7192b25acc1SLuigi Rizzo 
720868aabb4SRichard Scheffenegger 	if (vlan_pcp > -1)
721868aabb4SRichard Scheffenegger 		EVL_APPLY_PRI(m, vlan_pcp);
722868aabb4SRichard Scheffenegger 
7236c1c6ae5SRodney W. Grimes 	/* IN_LOOPBACK must not appear on the wire - RFC1122. */
7246c1c6ae5SRodney W. Grimes 	if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) ||
7256c1c6ae5SRodney W. Grimes 	    IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) {
72651c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
72786425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
72851c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
72951c8ec4aSRuslan Ermilov 			goto bad;
73051c8ec4aSRuslan Ermilov 		}
73151c8ec4aSRuslan Ermilov 	}
73251c8ec4aSRuslan Ermilov 
73344775b16SMark Johnston 	/* Ensure the packet data is mapped if the interface requires it. */
73444775b16SMark Johnston 	if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
73544775b16SMark Johnston 		m = mb_unmapped_to_ext(m);
73644775b16SMark Johnston 		if (m == NULL) {
73744775b16SMark Johnston 			IPSTAT_INC(ips_odropped);
73844775b16SMark Johnston 			error = ENOBUFS;
73944775b16SMark Johnston 			goto bad;
74044775b16SMark Johnston 		}
74144775b16SMark Johnston 	}
74244775b16SMark Johnston 
743206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
744078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
745db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
746078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
747db4f9cc7SJonathan Lemon 	}
74895033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
749078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
7501966e5b5SRandall Stewart 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
751078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
7522f4afd21SRandall Stewart 	}
7532f4afd21SRandall Stewart #endif
754db4f9cc7SJonathan Lemon 
755e7319babSPoul-Henning Kamp 	/*
756db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
757233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
758b092fd6cSNavdeep Parhar 	 * Note that if_vxlan could have requested TSO even though the outer
759b092fd6cSNavdeep Parhar 	 * frame is UDP.  It is correct to not fragment such datagrams and
760b092fd6cSNavdeep Parhar 	 * instead just pass them on to the driver.
761df8bae1dSRodney W. Grimes 	 */
76221d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
763b092fd6cSNavdeep Parhar 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist &
764b092fd6cSNavdeep Parhar 	    (CSUM_TSO | CSUM_INNER_TSO)) != 0) {
765df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
766078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
767df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
768078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
769078468edSGleb Smirnoff 		}
7705da9f8faSJosef Karthauser 
771233dcce1SAndre Oppermann 		/*
772233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
773233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
774233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
775233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
776233dcce1SAndre Oppermann 		 */
77738c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
778b092fd6cSNavdeep Parhar 			if (m->m_pkthdr.csum_flags &
779b092fd6cSNavdeep Parhar 			    (CSUM_TSO | CSUM_INNER_TSO))
7807caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
7817caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
782233dcce1SAndre Oppermann 			else
7837caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
7847caf4ab7SGleb Smirnoff 
7857caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
7865da9f8faSJosef Karthauser 		}
78753dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
7883390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
789eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
7909d9edc56SMike Silbersack #endif
791147f74d1SAndre Oppermann 		/*
792147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
793147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
794147f74d1SAndre Oppermann 		 */
79586bd0491SAndre Oppermann 		m_clrprotoflags(m);
79657f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
79735c7bb34SRandall Stewart 		error = ip_output_send(inp, ifp, m, gw, ro,
79835c7bb34SRandall Stewart 		    (flags & IP_NO_SND_TAG_RL) ? false : true);
799df8bae1dSRodney W. Grimes 		goto done;
800df8bae1dSRodney W. Grimes 	}
8011e78ac21SJeffrey Hsu 
802233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
803b092fd6cSNavdeep Parhar 	if ((ip_off & IP_DF) ||
804b092fd6cSNavdeep Parhar 	    (m->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_INNER_TSO))) {
805df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
80686425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
807df8bae1dSRodney W. Grimes 		goto bad;
808df8bae1dSRodney W. Grimes 	}
8091e78ac21SJeffrey Hsu 
8101e78ac21SJeffrey Hsu 	/*
8111e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
8121e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
8131e78ac21SJeffrey Hsu 	 */
814078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
8151e78ac21SJeffrey Hsu 	if (error)
816df8bae1dSRodney W. Grimes 		goto bad;
8171e78ac21SJeffrey Hsu 	for (; m; m = m0) {
818df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
819b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
82007203494SDaniel C. Sobral 		if (error == 0) {
821fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
82207203494SDaniel C. Sobral 			if (ia != NULL) {
8237caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
8247caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
8257caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
82607203494SDaniel C. Sobral 			}
827147f74d1SAndre Oppermann 			/*
828147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
829147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
830147f74d1SAndre Oppermann 			 */
83186bd0491SAndre Oppermann 			m_clrprotoflags(m);
832fe937674SJosef Karthauser 
8332e77d270SAndrey V. Elsukov 			IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp,
8342e77d270SAndrey V. Elsukov 			    mtod(m, struct ip *), NULL);
83535c7bb34SRandall Stewart 			error = ip_output_send(inp, ifp, m, gw, ro, true);
836fe937674SJosef Karthauser 		} else
837df8bae1dSRodney W. Grimes 			m_freem(m);
838df8bae1dSRodney W. Grimes 	}
839df8bae1dSRodney W. Grimes 
840df8bae1dSRodney W. Grimes 	if (error == 0)
84186425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
8421e78ac21SJeffrey Hsu 
843df8bae1dSRodney W. Grimes done:
844df8bae1dSRodney W. Grimes 	return (error);
845df8bae1dSRodney W. Grimes  bad:
8463528d68fSBill Paul 	m_freem(m);
847df8bae1dSRodney W. Grimes 	goto done;
848df8bae1dSRodney W. Grimes }
849df8bae1dSRodney W. Grimes 
8501e78ac21SJeffrey Hsu /*
8511e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
8521e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
8531e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
8541e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
8551e78ac21SJeffrey Hsu  *
8561e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
8571e78ac21SJeffrey Hsu  */
8581e78ac21SJeffrey Hsu int
8591e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
860078468edSGleb Smirnoff     u_long if_hwassist_flags)
8611e78ac21SJeffrey Hsu {
8621e78ac21SJeffrey Hsu 	int error = 0;
8631e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
8641e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
8651e78ac21SJeffrey Hsu 	int off;
8661e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
8671e78ac21SJeffrey Hsu 	int firstlen;
8681e78ac21SJeffrey Hsu 	struct mbuf **mnext;
8691e78ac21SJeffrey Hsu 	int nfrags;
87021d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
8711e78ac21SJeffrey Hsu 
87221d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
87321d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
87421d172a3SGleb Smirnoff 
87521d172a3SGleb Smirnoff 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
87686425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
8771e78ac21SJeffrey Hsu 		return EMSGSIZE;
8781e78ac21SJeffrey Hsu 	}
8791e78ac21SJeffrey Hsu 
8801e78ac21SJeffrey Hsu 	/*
8811e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
8821e78ac21SJeffrey Hsu 	 */
8831e78ac21SJeffrey Hsu 	if (len < 8)
8841e78ac21SJeffrey Hsu 		return EMSGSIZE;
8851e78ac21SJeffrey Hsu 
8861e78ac21SJeffrey Hsu 	/*
8871e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
8881e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
8891e78ac21SJeffrey Hsu 	 */
890da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
8911e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
8921e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
8931e78ac21SJeffrey Hsu 	}
89495033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
895da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
8961966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
8972f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
8982f4afd21SRandall Stewart 	}
8992f4afd21SRandall Stewart #endif
9001e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
9011e78ac21SJeffrey Hsu 		/*
9021e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
9031e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
9041e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
9051e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
9061e78ac21SJeffrey Hsu 		 *
9071e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
9081e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
9091e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
9101e78ac21SJeffrey Hsu 		 */
9111e78ac21SJeffrey Hsu 		int newlen;
9121e78ac21SJeffrey Hsu 
9139c0f6aa7SHans Petter Selasky 		off = MIN(mtu, m0->m_pkthdr.len);
9141e78ac21SJeffrey Hsu 
9151e78ac21SJeffrey Hsu 		/*
9161e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
9171e78ac21SJeffrey Hsu 		 * 8-byte boundary
9181e78ac21SJeffrey Hsu 		 */
9191e78ac21SJeffrey Hsu 		if (off < hlen)
9201e78ac21SJeffrey Hsu 			goto smart_frag_failure;
9211e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
9221e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
9231e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
9241e78ac21SJeffrey Hsu 			/* we failed, go back the default */
9251e78ac21SJeffrey Hsu smart_frag_failure:
9261e78ac21SJeffrey Hsu 			newlen = len;
9271e78ac21SJeffrey Hsu 			off = hlen + len;
9281e78ac21SJeffrey Hsu 		}
9291e78ac21SJeffrey Hsu 		len = newlen;
9301e78ac21SJeffrey Hsu 
9311e78ac21SJeffrey Hsu 	} else {
9321e78ac21SJeffrey Hsu 		off = hlen + len;
9331e78ac21SJeffrey Hsu 	}
9341e78ac21SJeffrey Hsu 
9351e78ac21SJeffrey Hsu 	firstlen = off - hlen;
9361e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
9371e78ac21SJeffrey Hsu 
9381e78ac21SJeffrey Hsu 	/*
9391e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
9401e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
9411e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
9421e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
9431e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
9441e78ac21SJeffrey Hsu 	 */
94521d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
9461e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
9471e78ac21SJeffrey Hsu 		struct mbuf *m;
9481e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
9491e78ac21SJeffrey Hsu 
950dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
9510b17fba7SAndre Oppermann 		if (m == NULL) {
9521e78ac21SJeffrey Hsu 			error = ENOBUFS;
95386425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
9541e78ac21SJeffrey Hsu 			goto done;
9551e78ac21SJeffrey Hsu 		}
956c4c4346fSHans Petter Selasky 		/*
957c4c4346fSHans Petter Selasky 		 * Make sure the complete packet header gets copied
958c4c4346fSHans Petter Selasky 		 * from the originating mbuf to the newly created
959c4c4346fSHans Petter Selasky 		 * mbuf. This also ensures that existing firewall
960c4c4346fSHans Petter Selasky 		 * classification(s), VLAN tags and so on get copied
961c4c4346fSHans Petter Selasky 		 * to the resulting fragmented packet(s):
962c4c4346fSHans Petter Selasky 		 */
963c4c4346fSHans Petter Selasky 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
964c4c4346fSHans Petter Selasky 			m_free(m);
965c4c4346fSHans Petter Selasky 			error = ENOBUFS;
966c4c4346fSHans Petter Selasky 			IPSTAT_INC(ips_odropped);
967c4c4346fSHans Petter Selasky 			goto done;
968c4c4346fSHans Petter Selasky 		}
9691e78ac21SJeffrey Hsu 		/*
9701e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
9711e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
9728b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
9731e78ac21SJeffrey Hsu 		 */
9741e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
9751e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
9761e78ac21SJeffrey Hsu 		*mhip = *ip;
9771e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
9781e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9791e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
9801e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
9811e78ac21SJeffrey Hsu 		}
9821e78ac21SJeffrey Hsu 		m->m_len = mhlen;
98321d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
98421d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
985fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
98621d172a3SGleb Smirnoff 			len = ip_len - off;
987fb86dfcdSAndre Oppermann 		else
9881e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
9891e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
990eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
9910b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
9921e78ac21SJeffrey Hsu 			m_free(m);
9931e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
99486425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
9951e78ac21SJeffrey Hsu 			goto done;
9961e78ac21SJeffrey Hsu 		}
9971e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
9981e78ac21SJeffrey Hsu #ifdef MAC
99930d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
10001e78ac21SJeffrey Hsu #endif
10011e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
10021e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
1003078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
10041e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
1005078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
1006078468edSGleb Smirnoff 		}
10071e78ac21SJeffrey Hsu 		*mnext = m;
10081e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
10091e78ac21SJeffrey Hsu 	}
101086425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
10111e78ac21SJeffrey Hsu 
10121e78ac21SJeffrey Hsu 	/*
10131e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
10141e78ac21SJeffrey Hsu 	 * and updating header.
10151e78ac21SJeffrey Hsu 	 */
101621d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
10171e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
10181e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
101921d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
10201e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
1021078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
10221e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
1023078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
1024078468edSGleb Smirnoff 	}
10251e78ac21SJeffrey Hsu 
10261e78ac21SJeffrey Hsu done:
10271e78ac21SJeffrey Hsu 	*m_frag = m0;
10281e78ac21SJeffrey Hsu 	return error;
10291e78ac21SJeffrey Hsu }
10301e78ac21SJeffrey Hsu 
10311c238475SJonathan Lemon void
1032db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1033db4f9cc7SJonathan Lemon {
1034db4f9cc7SJonathan Lemon 	struct ip *ip;
10351fdbfb90STom Jones 	struct udphdr *uh;
10361fdbfb90STom Jones 	uint16_t cklen, csum, offset;
1037db4f9cc7SJonathan Lemon 
1038db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
103953be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
10401fdbfb90STom Jones 
10411fdbfb90STom Jones 	if (m->m_pkthdr.csum_flags & CSUM_UDP) {
10421fdbfb90STom Jones 		/* if udp header is not in the first mbuf copy udplen */
1043b6e87011STom Jones 		if (offset + sizeof(struct udphdr) > m->m_len) {
1044590d0a43SAndrey V. Elsukov 			m_copydata(m, offset + offsetof(struct udphdr,
1045590d0a43SAndrey V. Elsukov 			    uh_ulen), sizeof(cklen), (caddr_t)&cklen);
1046b6e87011STom Jones 			cklen = ntohs(cklen);
1047b6e87011STom Jones 		} else {
1048590d0a43SAndrey V. Elsukov 			uh = (struct udphdr *)mtodo(m, offset);
10491fdbfb90STom Jones 			cklen = ntohs(uh->uh_ulen);
10501fdbfb90STom Jones 		}
10511fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen + offset, offset);
10521fdbfb90STom Jones 		if (csum == 0)
1053206a3274SRuslan Ermilov 			csum = 0xffff;
10541fdbfb90STom Jones 	} else {
10551fdbfb90STom Jones 		cklen = ntohs(ip->ip_len);
10561fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen, offset);
10571fdbfb90STom Jones 	}
1058db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1059db4f9cc7SJonathan Lemon 
1060590d0a43SAndrey V. Elsukov 	if (offset + sizeof(csum) > m->m_len)
1061590d0a43SAndrey V. Elsukov 		m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
1062590d0a43SAndrey V. Elsukov 	else
1063590d0a43SAndrey V. Elsukov 		*(u_short *)mtodo(m, offset) = csum;
1064db4f9cc7SJonathan Lemon }
1065db4f9cc7SJonathan Lemon 
1066df8bae1dSRodney W. Grimes /*
1067df8bae1dSRodney W. Grimes  * IP socket option processing.
1068df8bae1dSRodney W. Grimes  */
1069df8bae1dSRodney W. Grimes int
1070f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
1071df8bae1dSRodney W. Grimes {
1072cfe8b629SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1073cfe8b629SGarrett Wollman 	int	error, optval;
1074dc847eb6SAdrian Chadd #ifdef	RSS
1075dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
1076dc847eb6SAdrian Chadd 	int retval;
1077dc847eb6SAdrian Chadd #endif
1078df8bae1dSRodney W. Grimes 
1079cfe8b629SGarrett Wollman 	error = optval = 0;
1080cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1081fc06cd42SMikolaj Golub 		error = EINVAL;
1082fc06cd42SMikolaj Golub 
1083fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
1084fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
1085fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
1086fc06cd42SMikolaj Golub 			case SO_REUSEADDR:
1087fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1088efdf104bSMikolaj Golub 				if ((so->so_options & SO_REUSEADDR) != 0)
1089efdf104bSMikolaj Golub 					inp->inp_flags2 |= INP_REUSEADDR;
1090fc06cd42SMikolaj Golub 				else
1091efdf104bSMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEADDR;
1092fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1093fc06cd42SMikolaj Golub 				error = 0;
1094fc06cd42SMikolaj Golub 				break;
1095fc06cd42SMikolaj Golub 			case SO_REUSEPORT:
1096fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1097fc06cd42SMikolaj Golub 				if ((so->so_options & SO_REUSEPORT) != 0)
1098fc06cd42SMikolaj Golub 					inp->inp_flags2 |= INP_REUSEPORT;
1099fc06cd42SMikolaj Golub 				else
1100fc06cd42SMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEPORT;
1101fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1102fc06cd42SMikolaj Golub 				error = 0;
1103fc06cd42SMikolaj Golub 				break;
11041a43cff9SSean Bruno 			case SO_REUSEPORT_LB:
11051a43cff9SSean Bruno 				INP_WLOCK(inp);
11061a43cff9SSean Bruno 				if ((so->so_options & SO_REUSEPORT_LB) != 0)
11071a43cff9SSean Bruno 					inp->inp_flags2 |= INP_REUSEPORT_LB;
11081a43cff9SSean Bruno 				else
11091a43cff9SSean Bruno 					inp->inp_flags2 &= ~INP_REUSEPORT_LB;
11101a43cff9SSean Bruno 				INP_WUNLOCK(inp);
11111a43cff9SSean Bruno 				error = 0;
11121a43cff9SSean Bruno 				break;
1113fc06cd42SMikolaj Golub 			case SO_SETFIB:
1114fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1115fc06cd42SMikolaj Golub 				inp->inp_inc.inc_fibnum = so->so_fibnum;
1116fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1117fc06cd42SMikolaj Golub 				error = 0;
1118fc06cd42SMikolaj Golub 				break;
1119f3e7afe2SHans Petter Selasky 			case SO_MAX_PACING_RATE:
1120f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1121f3e7afe2SHans Petter Selasky 				INP_WLOCK(inp);
1122f3e7afe2SHans Petter Selasky 				inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1123f3e7afe2SHans Petter Selasky 				INP_WUNLOCK(inp);
1124f3e7afe2SHans Petter Selasky 				error = 0;
1125f3e7afe2SHans Petter Selasky #else
1126f3e7afe2SHans Petter Selasky 				error = EOPNOTSUPP;
1127f3e7afe2SHans Petter Selasky #endif
1128f3e7afe2SHans Petter Selasky 				break;
1129fc06cd42SMikolaj Golub 			default:
1130fc06cd42SMikolaj Golub 				break;
1131fc06cd42SMikolaj Golub 			}
1132fc06cd42SMikolaj Golub 		}
1133fc06cd42SMikolaj Golub 		return (error);
1134cfe8b629SGarrett Wollman 	}
1135df8bae1dSRodney W. Grimes 
1136cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1137cfe8b629SGarrett Wollman 	case SOPT_SET:
1138cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1139df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1140df8bae1dSRodney W. Grimes #ifdef notyet
1141df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1142df8bae1dSRodney W. Grimes #endif
1143cfe8b629SGarrett Wollman 		{
1144cfe8b629SGarrett Wollman 			struct mbuf *m;
1145cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1146cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1147cfe8b629SGarrett Wollman 				break;
1148cfe8b629SGarrett Wollman 			}
1149dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
11500b17fba7SAndre Oppermann 			if (m == NULL) {
1151cfe8b629SGarrett Wollman 				error = ENOBUFS;
1152cfe8b629SGarrett Wollman 				break;
1153cfe8b629SGarrett Wollman 			}
1154cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1155cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1156cfe8b629SGarrett Wollman 					    m->m_len);
1157635354c4SMaxim Konovalov 			if (error) {
1158635354c4SMaxim Konovalov 				m_free(m);
1159635354c4SMaxim Konovalov 				break;
1160635354c4SMaxim Konovalov 			}
11618501a69cSRobert Watson 			INP_WLOCK(inp);
1162993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
11638501a69cSRobert Watson 			INP_WUNLOCK(inp);
1164993d9505SRobert Watson 			return (error);
1165cfe8b629SGarrett Wollman 		}
1166df8bae1dSRodney W. Grimes 
1167f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
1168f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
1169f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
1170f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
1171f44270e7SPawel Jakub Dawidek 				if (error)
1172be9347e3SAdrian Chadd 					break;
1173be9347e3SAdrian Chadd 			}
1174cef27294SAdrian Chadd 			/* FALLTHROUGH */
11750a100a6fSAdrian Chadd 		case IP_BINDMULTI:
11760a100a6fSAdrian Chadd #ifdef	RSS
11770a100a6fSAdrian Chadd 		case IP_RSS_LISTEN_BUCKET:
11780a100a6fSAdrian Chadd #endif
1179df8bae1dSRodney W. Grimes 		case IP_TOS:
1180df8bae1dSRodney W. Grimes 		case IP_TTL:
1181936cd18dSAndre Oppermann 		case IP_MINTTL:
1182df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1183df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1184dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1185df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
11864957466bSMatthew N. Dodd 		case IP_RECVTTL:
118782c23ebaSBill Fenner 		case IP_RECVIF:
11888afa2304SBruce M Simpson 		case IP_ONESBCAST:
1189b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
11903cca425bSMichael Tuexen 		case IP_RECVTOS:
11919d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
11929d3ddf43SAdrian Chadd #ifdef	RSS
11939d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
11949d3ddf43SAdrian Chadd #endif
1195868aabb4SRichard Scheffenegger 		case IP_VLAN_PCP:
1196cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1197cfe8b629SGarrett Wollman 					    sizeof optval);
1198cfe8b629SGarrett Wollman 			if (error)
1199cfe8b629SGarrett Wollman 				break;
1200df8bae1dSRodney W. Grimes 
1201cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1202df8bae1dSRodney W. Grimes 			case IP_TOS:
1203ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1204df8bae1dSRodney W. Grimes 				break;
1205df8bae1dSRodney W. Grimes 
1206df8bae1dSRodney W. Grimes 			case IP_TTL:
1207ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1208df8bae1dSRodney W. Grimes 				break;
1209936cd18dSAndre Oppermann 
1210936cd18dSAndre Oppermann 			case IP_MINTTL:
1211a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1212936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1213936cd18dSAndre Oppermann 				else
1214936cd18dSAndre Oppermann 					error = EINVAL;
1215936cd18dSAndre Oppermann 				break;
1216936cd18dSAndre Oppermann 
1217a138d217SRobert Watson #define	OPTSET(bit) do {						\
12188501a69cSRobert Watson 	INP_WLOCK(inp);							\
1219df8bae1dSRodney W. Grimes 	if (optval)							\
1220df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1221df8bae1dSRodney W. Grimes 	else								\
1222a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
12238501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1224a138d217SRobert Watson } while (0)
1225df8bae1dSRodney W. Grimes 
12260a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
12270a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
12280a100a6fSAdrian Chadd 	if (val)							\
12290a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
12300a100a6fSAdrian Chadd 	else								\
12310a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
12320a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
12330a100a6fSAdrian Chadd } while (0)
12340a100a6fSAdrian Chadd 
1235df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1236df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1237df8bae1dSRodney W. Grimes 				break;
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1240df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1241df8bae1dSRodney W. Grimes 				break;
1242df8bae1dSRodney W. Grimes 
1243df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1244df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1245df8bae1dSRodney W. Grimes 				break;
124682c23ebaSBill Fenner 
1247dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1248dce33a45SErmal Luçi 				OPTSET2(INP_ORIGDSTADDR, optval);
1249dce33a45SErmal Luçi 				break;
1250dce33a45SErmal Luçi 
12514957466bSMatthew N. Dodd 			case IP_RECVTTL:
12524957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
12534957466bSMatthew N. Dodd 				break;
12544957466bSMatthew N. Dodd 
125582c23ebaSBill Fenner 			case IP_RECVIF:
125682c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
125782c23ebaSBill Fenner 				break;
12586a800098SYoshinobu Inoue 
12598afa2304SBruce M Simpson 			case IP_ONESBCAST:
12608afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
12618afa2304SBruce M Simpson 				break;
1262b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1263b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1264b2828ad2SAndre Oppermann 				break;
1265f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1266f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1267be9347e3SAdrian Chadd 				break;
12683cca425bSMichael Tuexen 			case IP_RECVTOS:
12693cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
12703cca425bSMichael Tuexen 				break;
12710a100a6fSAdrian Chadd 			case IP_BINDMULTI:
12720a100a6fSAdrian Chadd 				OPTSET2(INP_BINDMULTI, optval);
12730a100a6fSAdrian Chadd 				break;
12749d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
12759d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
12769d3ddf43SAdrian Chadd 				break;
12770a100a6fSAdrian Chadd #ifdef	RSS
12780a100a6fSAdrian Chadd 			case IP_RSS_LISTEN_BUCKET:
12790a100a6fSAdrian Chadd 				if ((optval >= 0) &&
12800a100a6fSAdrian Chadd 				    (optval < rss_getnumbuckets())) {
12810a100a6fSAdrian Chadd 					inp->inp_rss_listen_bucket = optval;
12820a100a6fSAdrian Chadd 					OPTSET2(INP_RSS_BUCKET_SET, 1);
12830a100a6fSAdrian Chadd 				} else {
12840a100a6fSAdrian Chadd 					error = EINVAL;
12850a100a6fSAdrian Chadd 				}
12860a100a6fSAdrian Chadd 				break;
12879d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
12889d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
12899d3ddf43SAdrian Chadd 				break;
12900a100a6fSAdrian Chadd #endif
1291868aabb4SRichard Scheffenegger 			case IP_VLAN_PCP:
1292868aabb4SRichard Scheffenegger 				if ((optval >= -1) && (optval <=
1293868aabb4SRichard Scheffenegger 				    (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1294868aabb4SRichard Scheffenegger 					if (optval == -1) {
1295868aabb4SRichard Scheffenegger 						INP_WLOCK(inp);
1296868aabb4SRichard Scheffenegger 						inp->inp_flags2 &=
1297868aabb4SRichard Scheffenegger 						    ~(INP_2PCP_SET |
1298868aabb4SRichard Scheffenegger 						      INP_2PCP_MASK);
1299868aabb4SRichard Scheffenegger 						INP_WUNLOCK(inp);
1300868aabb4SRichard Scheffenegger 					} else {
1301868aabb4SRichard Scheffenegger 						INP_WLOCK(inp);
1302868aabb4SRichard Scheffenegger 						inp->inp_flags2 |=
1303868aabb4SRichard Scheffenegger 						    INP_2PCP_SET;
1304868aabb4SRichard Scheffenegger 						inp->inp_flags2 &=
1305868aabb4SRichard Scheffenegger 						    ~INP_2PCP_MASK;
1306868aabb4SRichard Scheffenegger 						inp->inp_flags2 |=
1307868aabb4SRichard Scheffenegger 						    optval << INP_2PCP_SHIFT;
1308868aabb4SRichard Scheffenegger 						INP_WUNLOCK(inp);
1309868aabb4SRichard Scheffenegger 					}
1310868aabb4SRichard Scheffenegger 				} else
1311868aabb4SRichard Scheffenegger 					error = EINVAL;
1312868aabb4SRichard Scheffenegger 				break;
1313df8bae1dSRodney W. Grimes 			}
1314df8bae1dSRodney W. Grimes 			break;
1315df8bae1dSRodney W. Grimes #undef OPTSET
13160a100a6fSAdrian Chadd #undef OPTSET2
1317df8bae1dSRodney W. Grimes 
131871498f30SBruce M Simpson 		/*
131971498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
132071498f30SBruce M Simpson 		 * module.
132171498f30SBruce M Simpson 		 */
1322df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1323f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1324df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1325df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1326df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1327df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
132871498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
132971498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
133071498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
133171498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
133271498f30SBruce M Simpson 		case IP_MSFILTER:
133371498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
133471498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
133571498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
133671498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
133771498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
133871498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
133971498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1340df8bae1dSRodney W. Grimes 			break;
1341df8bae1dSRodney W. Grimes 
134233b3ac06SPeter Wemm 		case IP_PORTRANGE:
1343cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1344cfe8b629SGarrett Wollman 					    sizeof optval);
1345cfe8b629SGarrett Wollman 			if (error)
1346cfe8b629SGarrett Wollman 				break;
134733b3ac06SPeter Wemm 
13488501a69cSRobert Watson 			INP_WLOCK(inp);
134933b3ac06SPeter Wemm 			switch (optval) {
135033b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
135133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
135233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
135333b3ac06SPeter Wemm 				break;
135433b3ac06SPeter Wemm 
135533b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
135633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
135733b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
135833b3ac06SPeter Wemm 				break;
135933b3ac06SPeter Wemm 
136033b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
136133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
136233b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
136333b3ac06SPeter Wemm 				break;
136433b3ac06SPeter Wemm 
136533b3ac06SPeter Wemm 			default:
136633b3ac06SPeter Wemm 				error = EINVAL;
136733b3ac06SPeter Wemm 				break;
136833b3ac06SPeter Wemm 			}
13698501a69cSRobert Watson 			INP_WUNLOCK(inp);
1370ce8c72b1SPeter Wemm 			break;
137133b3ac06SPeter Wemm 
1372fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13736a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1374fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1375fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
13766a800098SYoshinobu Inoue 				break;
13776a800098SYoshinobu Inoue 			}
1378fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1379b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
13806a800098SYoshinobu Inoue 
1381df8bae1dSRodney W. Grimes 		default:
1382df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1383df8bae1dSRodney W. Grimes 			break;
1384df8bae1dSRodney W. Grimes 		}
1385df8bae1dSRodney W. Grimes 		break;
1386df8bae1dSRodney W. Grimes 
1387cfe8b629SGarrett Wollman 	case SOPT_GET:
1388cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1389df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1390df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1391e5e3e746SMatt Macy 			INP_RLOCK(inp);
1392e5e3e746SMatt Macy 			if (inp->inp_options) {
1393e5e3e746SMatt Macy 				struct mbuf *options;
1394e5e3e746SMatt Macy 
139510731c54SMichael Tuexen 				options = m_copym(inp->inp_options, 0,
139610731c54SMichael Tuexen 				    M_COPYALL, M_NOWAIT);
1397e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1398e5e3e746SMatt Macy 				if (options != NULL) {
1399cfe8b629SGarrett Wollman 					error = sooptcopyout(sopt,
1400e5e3e746SMatt Macy 							     mtod(options, char *),
1401e5e3e746SMatt Macy 							     options->m_len);
1402e5e3e746SMatt Macy 					m_freem(options);
1403e5e3e746SMatt Macy 				} else
1404e5e3e746SMatt Macy 					error = ENOMEM;
1405e5e3e746SMatt Macy 			} else {
1406e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1407cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1408e5e3e746SMatt Macy 			}
1409df8bae1dSRodney W. Grimes 			break;
1410df8bae1dSRodney W. Grimes 
1411df8bae1dSRodney W. Grimes 		case IP_TOS:
1412df8bae1dSRodney W. Grimes 		case IP_TTL:
1413936cd18dSAndre Oppermann 		case IP_MINTTL:
1414df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1415df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1416dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1417df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
14184957466bSMatthew N. Dodd 		case IP_RECVTTL:
141982c23ebaSBill Fenner 		case IP_RECVIF:
1420cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
14218afa2304SBruce M Simpson 		case IP_ONESBCAST:
1422b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
14235f6bf451SAttilio Rao 		case IP_BINDANY:
14243cca425bSMichael Tuexen 		case IP_RECVTOS:
14250a100a6fSAdrian Chadd 		case IP_BINDMULTI:
14269c423972SAdrian Chadd 		case IP_FLOWID:
14279c423972SAdrian Chadd 		case IP_FLOWTYPE:
14289d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
14290a100a6fSAdrian Chadd #ifdef	RSS
14300a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
14319d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
14320a100a6fSAdrian Chadd #endif
1433868aabb4SRichard Scheffenegger 		case IP_VLAN_PCP:
1434cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1435df8bae1dSRodney W. Grimes 			case IP_TOS:
1436ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1437df8bae1dSRodney W. Grimes 				break;
1438df8bae1dSRodney W. Grimes 
1439df8bae1dSRodney W. Grimes 			case IP_TTL:
1440ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1441df8bae1dSRodney W. Grimes 				break;
1442df8bae1dSRodney W. Grimes 
1443936cd18dSAndre Oppermann 			case IP_MINTTL:
1444936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1445936cd18dSAndre Oppermann 				break;
1446936cd18dSAndre Oppermann 
1447df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
14480a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1451df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1452df8bae1dSRodney W. Grimes 				break;
1453df8bae1dSRodney W. Grimes 
1454df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1455df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1456df8bae1dSRodney W. Grimes 				break;
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1459df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1460df8bae1dSRodney W. Grimes 				break;
146182c23ebaSBill Fenner 
1462dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1463dce33a45SErmal Luçi 				optval = OPTBIT2(INP_ORIGDSTADDR);
1464dce33a45SErmal Luçi 				break;
1465dce33a45SErmal Luçi 
14664957466bSMatthew N. Dodd 			case IP_RECVTTL:
14674957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
14684957466bSMatthew N. Dodd 				break;
14694957466bSMatthew N. Dodd 
147082c23ebaSBill Fenner 			case IP_RECVIF:
147182c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
147282c23ebaSBill Fenner 				break;
1473cfe8b629SGarrett Wollman 
1474cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1475cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1476cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1477cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1478cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1479cfe8b629SGarrett Wollman 				else
1480cfe8b629SGarrett Wollman 					optval = 0;
1481cfe8b629SGarrett Wollman 				break;
14826a800098SYoshinobu Inoue 
14838afa2304SBruce M Simpson 			case IP_ONESBCAST:
14848afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
14858afa2304SBruce M Simpson 				break;
1486b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1487b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1488b2828ad2SAndre Oppermann 				break;
14895f6bf451SAttilio Rao 			case IP_BINDANY:
14905f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
14915f6bf451SAttilio Rao 				break;
14923cca425bSMichael Tuexen 			case IP_RECVTOS:
14933cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
14943cca425bSMichael Tuexen 				break;
14959c423972SAdrian Chadd 			case IP_FLOWID:
14969c423972SAdrian Chadd 				optval = inp->inp_flowid;
14979c423972SAdrian Chadd 				break;
14989c423972SAdrian Chadd 			case IP_FLOWTYPE:
14999c423972SAdrian Chadd 				optval = inp->inp_flowtype;
15009c423972SAdrian Chadd 				break;
15019d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
15029d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
15039d3ddf43SAdrian Chadd 				break;
15049c423972SAdrian Chadd #ifdef	RSS
15057847796aSAdrian Chadd 			case IP_RSSBUCKETID:
15067847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
15077847796aSAdrian Chadd 				    inp->inp_flowtype,
15087847796aSAdrian Chadd 				    &rss_bucket);
15097847796aSAdrian Chadd 				if (retval == 0)
15107847796aSAdrian Chadd 					optval = rss_bucket;
15117847796aSAdrian Chadd 				else
15127847796aSAdrian Chadd 					error = EINVAL;
15139c423972SAdrian Chadd 				break;
15149d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
15159d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
15169d3ddf43SAdrian Chadd 				break;
15179c423972SAdrian Chadd #endif
15180a100a6fSAdrian Chadd 			case IP_BINDMULTI:
15190a100a6fSAdrian Chadd 				optval = OPTBIT2(INP_BINDMULTI);
15200a100a6fSAdrian Chadd 				break;
1521868aabb4SRichard Scheffenegger 			case IP_VLAN_PCP:
1522868aabb4SRichard Scheffenegger 				if (OPTBIT2(INP_2PCP_SET)) {
1523868aabb4SRichard Scheffenegger 					optval = (inp->inp_flags2 &
1524868aabb4SRichard Scheffenegger 					    INP_2PCP_MASK) >> INP_2PCP_SHIFT;
1525868aabb4SRichard Scheffenegger 				} else {
1526868aabb4SRichard Scheffenegger 					optval = -1;
1527868aabb4SRichard Scheffenegger 				}
1528868aabb4SRichard Scheffenegger 				break;
1529df8bae1dSRodney W. Grimes 			}
1530cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1531df8bae1dSRodney W. Grimes 			break;
1532df8bae1dSRodney W. Grimes 
153371498f30SBruce M Simpson 		/*
153471498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
153571498f30SBruce M Simpson 		 * module.
153671498f30SBruce M Simpson 		 */
1537df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1538f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1539df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1540df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
154171498f30SBruce M Simpson 		case IP_MSFILTER:
154271498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
154333b3ac06SPeter Wemm 			break;
154433b3ac06SPeter Wemm 
1545fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
15466a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1547fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1548fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
15496a800098SYoshinobu Inoue 				break;
15506a800098SYoshinobu Inoue 			}
1551fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1552b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15536a800098SYoshinobu Inoue 
1554df8bae1dSRodney W. Grimes 		default:
1555df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1556df8bae1dSRodney W. Grimes 			break;
1557df8bae1dSRodney W. Grimes 		}
1558df8bae1dSRodney W. Grimes 		break;
1559df8bae1dSRodney W. Grimes 	}
1560df8bae1dSRodney W. Grimes 	return (error);
1561df8bae1dSRodney W. Grimes }
1562df8bae1dSRodney W. Grimes 
1563df8bae1dSRodney W. Grimes /*
1564df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1565df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1566df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1567f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1568f5fea3ddSPaul Traina  * replicating that code here.
1569df8bae1dSRodney W. Grimes  */
1570df8bae1dSRodney W. Grimes static void
1571331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen)
1572df8bae1dSRodney W. Grimes {
1573331dff07SAlexander V. Chernikov 	struct ip *ip;
1574df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1575df8bae1dSRodney W. Grimes 
1576e4762f75SGeorge V. Neville-Neil 	/*
1577e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1578e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1579e4762f75SGeorge V. Neville-Neil 	 */
1580eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
1581f0cace5dSRobert Watson 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
158286b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1583df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1584390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1585390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1586390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1587390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1588390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1589390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1590390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1591390cdc6aSRuslan Ermilov 		}
1592df8bae1dSRodney W. Grimes 		/*
1593df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1594df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1595df8bae1dSRodney W. Grimes 		 */
15968f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1597df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
159886b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1599331dff07SAlexander V. Chernikov 		if_simloop(ifp, copym, AF_INET, 0);
1600df8bae1dSRodney W. Grimes 	}
1601df8bae1dSRodney W. Grimes }
1602