xref: /freebsd/sys/netinet/ip_output.c (revision 8f1d5cf5b56a83d3fd39e1feaa88b8f913b13c9e)
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 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
335d6d7e75SGleb Smirnoff #include "opt_inet.h"
346a800098SYoshinobu Inoue #include "opt_ipsec.h"
35b2e60773SJohn Baldwin #include "opt_kern_tls.h"
3653dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
3777a01441SJohn Baldwin #include "opt_ratelimit.h"
3857f60867SMark Johnston #include "opt_route.h"
399c423972SAdrian Chadd #include "opt_rss.h"
4077a01441SJohn Baldwin #include "opt_sctp.h"
41fbd1372aSJoerg Wunsch 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
4326f9a767SRodney W. Grimes #include <sys/systm.h>
44f0f6d643SLuigi Rizzo #include <sys/kernel.h>
45b2e60773SJohn Baldwin #include <sys/ktls.h>
46cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49acd3428bSRobert Watson #include <sys/priv.h>
50c26fe973SBjoern A. Zeeb #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
5257f60867SMark Johnston #include <sys/sdt.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
559d9edc56SMike Silbersack #include <sys/sysctl.h>
56c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <net/if.h>
5976039bc8SGleb Smirnoff #include <net/if_var.h>
603d0d5b21SJustin Hibbits #include <net/if_private.h>
61868aabb4SRichard Scheffenegger #include <net/if_vlan_var.h>
623ef5e21dSQing Li #include <net/if_llatbl.h>
63868aabb4SRichard Scheffenegger #include <net/ethernet.h>
649b932e9eSAndre Oppermann #include <net/netisr.h>
65c21fd232SAndre Oppermann #include <net/pfil.h>
66df8bae1dSRodney W. Grimes #include <net/route.h>
67983066f0SAlexander V. Chernikov #include <net/route/nhop.h>
68b2bdc62aSAdrian Chadd #include <net/rss_config.h>
694b79449eSBjoern A. Zeeb #include <net/vnet.h>
70df8bae1dSRodney W. Grimes 
71df8bae1dSRodney W. Grimes #include <netinet/in.h>
726ca363ebSGleb Smirnoff #include <netinet/in_fib.h>
7357f60867SMark Johnston #include <netinet/in_kdtrace.h>
74df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip.h>
76983066f0SAlexander V. Chernikov #include <netinet/in_fib.h>
77df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
789c423972SAdrian Chadd #include <netinet/in_rss.h>
79df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
80df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
81ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
821fdbfb90STom Jones 
831fdbfb90STom Jones #include <netinet/udp.h>
841fdbfb90STom Jones #include <netinet/udp_var.h>
851fdbfb90STom Jones 
8695033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
872f4afd21SRandall Stewart #include <netinet/sctp.h>
882f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
892f4afd21SRandall Stewart #endif
90df8bae1dSRodney W. Grimes 
91fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
926a800098SYoshinobu Inoue 
931dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
941dfcf0d2SAndre Oppermann 
95aed55708SRobert Watson #include <security/mac/mac_framework.h>
96aed55708SRobert Watson 
9753dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
9805b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0;
999d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
1009d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1019d9edc56SMike Silbersack #endif
1029d9edc56SMike Silbersack 
103331dff07SAlexander V. Chernikov static void	ip_mloopback(struct ifnet *, const struct mbuf *, int);
104afed1b49SDarren Reed 
1058b889dbbSBruce M Simpson extern int in_mcast_loop;
10693e0e116SJulian Elischer 
107d9f2a782SErmal Luçi static inline int
10805fc9d78SKristof Provost ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, int flags,
10905fc9d78SKristof Provost     struct inpcb *inp, struct sockaddr_in *dst, int *fibnum, int *error)
110d9f2a782SErmal Luçi {
111d9f2a782SErmal Luçi 	struct m_tag *fwd_tag = NULL;
1128f980c01SMark Johnston 	struct mbuf *m;
113d9f2a782SErmal Luçi 	struct in_addr odst;
114d9f2a782SErmal Luçi 	struct ip *ip;
1151f4c3887SKristof Provost 	int ret;
11605fc9d78SKristof Provost 
1178f980c01SMark Johnston 	m = *mp;
118d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
119d9f2a782SErmal Luçi 
120d9f2a782SErmal Luçi 	/* Run through list of hooks for output packets. */
121d9f2a782SErmal Luçi 	odst.s_addr = ip->ip_dst.s_addr;
1221f4c3887SKristof Provost 	if (flags & IP_FORWARDING)
1231f4c3887SKristof Provost 		ret = pfil_mbuf_fwd(V_inet_pfil_head, mp, ifp, inp);
1241f4c3887SKristof Provost 	else
1251f4c3887SKristof Provost 		ret = pfil_mbuf_out(V_inet_pfil_head, mp, ifp, inp);
1261f4c3887SKristof Provost 
1271f4c3887SKristof Provost 	switch (ret) {
128b252313fSGleb Smirnoff 	case PFIL_DROPPED:
1298d5c56daSGleb Smirnoff 		*error = EACCES;
130b252313fSGleb Smirnoff 		/* FALLTHROUGH */
131b252313fSGleb Smirnoff 	case PFIL_CONSUMED:
132d9f2a782SErmal Luçi 		return 1; /* Finished */
133b252313fSGleb Smirnoff 	case PFIL_PASS:
134b252313fSGleb Smirnoff 		*error = 0;
135b252313fSGleb Smirnoff 	}
136b252313fSGleb Smirnoff 	m = *mp;
137d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
138d9f2a782SErmal Luçi 
139d9f2a782SErmal Luçi 	/* See if destination IP address was changed by packet filter. */
140d9f2a782SErmal Luçi 	if (odst.s_addr != ip->ip_dst.s_addr) {
141d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
142d9f2a782SErmal Luçi 		/* If destination is now ourself drop to ip_input(). */
143d9f2a782SErmal Luçi 		if (in_localip(ip->ip_dst)) {
144d9f2a782SErmal Luçi 			m->m_flags |= M_FASTFWD_OURS;
145d9f2a782SErmal Luçi 			if (m->m_pkthdr.rcvif == NULL)
146d9f2a782SErmal Luçi 				m->m_pkthdr.rcvif = V_loif;
147d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
148d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |=
149d9f2a782SErmal Luçi 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
150d9f2a782SErmal Luçi 				m->m_pkthdr.csum_data = 0xffff;
151d9f2a782SErmal Luçi 			}
152d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
153d9f2a782SErmal Luçi 				CSUM_IP_CHECKED | CSUM_IP_VALID;
15495033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
155d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
156d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
157d9f2a782SErmal Luçi #endif
158d9f2a782SErmal Luçi 			*error = netisr_queue(NETISR_IP, m);
159d9f2a782SErmal Luçi 			return 1; /* Finished */
160d9f2a782SErmal Luçi 		}
161d9f2a782SErmal Luçi 
162d9f2a782SErmal Luçi 		bzero(dst, sizeof(*dst));
163d9f2a782SErmal Luçi 		dst->sin_family = AF_INET;
164d9f2a782SErmal Luçi 		dst->sin_len = sizeof(*dst);
165d9f2a782SErmal Luçi 		dst->sin_addr = ip->ip_dst;
166d9f2a782SErmal Luçi 
167d9f2a782SErmal Luçi 		return -1; /* Reloop */
168d9f2a782SErmal Luçi 	}
169d9f2a782SErmal Luçi 	/* See if fib was changed by packet filter. */
170d9f2a782SErmal Luçi 	if ((*fibnum) != M_GETFIB(m)) {
171d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
172d9f2a782SErmal Luçi 		*fibnum = M_GETFIB(m);
173d9f2a782SErmal Luçi 		return -1; /* Reloop for FIB change */
174d9f2a782SErmal Luçi 	}
175d9f2a782SErmal Luçi 
176d9f2a782SErmal Luçi 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
177d9f2a782SErmal Luçi 	if (m->m_flags & M_FASTFWD_OURS) {
178d9f2a782SErmal Luçi 		if (m->m_pkthdr.rcvif == NULL)
179d9f2a782SErmal Luçi 			m->m_pkthdr.rcvif = V_loif;
180d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
181d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
182d9f2a782SErmal Luçi 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
183d9f2a782SErmal Luçi 			m->m_pkthdr.csum_data = 0xffff;
184d9f2a782SErmal Luçi 		}
18595033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
186d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
187d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
188d9f2a782SErmal Luçi #endif
189d9f2a782SErmal Luçi 		m->m_pkthdr.csum_flags |=
190d9f2a782SErmal Luçi 			CSUM_IP_CHECKED | CSUM_IP_VALID;
191d9f2a782SErmal Luçi 
192d9f2a782SErmal Luçi 		*error = netisr_queue(NETISR_IP, m);
193d9f2a782SErmal Luçi 		return 1; /* Finished */
194d9f2a782SErmal Luçi 	}
195d9f2a782SErmal Luçi 	/* Or forward to some other address? */
196d9f2a782SErmal Luçi 	if ((m->m_flags & M_IP_NEXTHOP) &&
197d9f2a782SErmal Luçi 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
198d9f2a782SErmal Luçi 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
199d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
200d9f2a782SErmal Luçi 		m->m_flags &= ~M_IP_NEXTHOP;
201d9f2a782SErmal Luçi 		m_tag_delete(m, fwd_tag);
202d9f2a782SErmal Luçi 
203d9f2a782SErmal Luçi 		return -1; /* Reloop for CHANGE of dst */
204d9f2a782SErmal Luçi 	}
205d9f2a782SErmal Luçi 
206d9f2a782SErmal Luçi 	return 0;
207d9f2a782SErmal Luçi }
208d9f2a782SErmal Luçi 
209fb3bc596SJohn Baldwin static int
210fb3bc596SJohn Baldwin ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m,
21162e1a437SZhenlei Huang     const struct sockaddr *gw, struct route *ro, bool stamp_tag)
212fb3bc596SJohn Baldwin {
213b2e60773SJohn Baldwin #ifdef KERN_TLS
214b2e60773SJohn Baldwin 	struct ktls_session *tls = NULL;
215b2e60773SJohn Baldwin #endif
216fb3bc596SJohn Baldwin 	struct m_snd_tag *mst;
217fb3bc596SJohn Baldwin 	int error;
218fb3bc596SJohn Baldwin 
219fb3bc596SJohn Baldwin 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
220fb3bc596SJohn Baldwin 	mst = NULL;
221fb3bc596SJohn Baldwin 
222b2e60773SJohn Baldwin #ifdef KERN_TLS
223b2e60773SJohn Baldwin 	/*
224b2e60773SJohn Baldwin 	 * If this is an unencrypted TLS record, save a reference to
225b2e60773SJohn Baldwin 	 * the record.  This local reference is used to call
226b2e60773SJohn Baldwin 	 * ktls_output_eagain after the mbuf has been freed (thus
227b2e60773SJohn Baldwin 	 * dropping the mbuf's reference) in if_output.
228b2e60773SJohn Baldwin 	 */
229b2e60773SJohn Baldwin 	if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
2307b6c99d0SGleb Smirnoff 		tls = ktls_hold(m->m_next->m_epg_tls);
231b2e60773SJohn Baldwin 		mst = tls->snd_tag;
232b2e60773SJohn Baldwin 
233b2e60773SJohn Baldwin 		/*
234b2e60773SJohn Baldwin 		 * If a TLS session doesn't have a valid tag, it must
235b2e60773SJohn Baldwin 		 * have had an earlier ifp mismatch, so drop this
236b2e60773SJohn Baldwin 		 * packet.
237b2e60773SJohn Baldwin 		 */
238b2e60773SJohn Baldwin 		if (mst == NULL) {
2399ba11796SAndrew Gallatin 			m_freem(m);
240b2e60773SJohn Baldwin 			error = EAGAIN;
241b2e60773SJohn Baldwin 			goto done;
242b2e60773SJohn Baldwin 		}
2436043ac20SAndrew Gallatin 		/*
2446043ac20SAndrew Gallatin 		 * Always stamp tags that include NIC ktls.
2456043ac20SAndrew Gallatin 		 */
2466043ac20SAndrew Gallatin 		stamp_tag = true;
247b2e60773SJohn Baldwin 	}
248b2e60773SJohn Baldwin #endif
249fb3bc596SJohn Baldwin #ifdef RATELIMIT
250b2e60773SJohn Baldwin 	if (inp != NULL && mst == NULL) {
251fb3bc596SJohn Baldwin 		if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
252fb3bc596SJohn Baldwin 		    (inp->inp_snd_tag != NULL &&
253fb3bc596SJohn Baldwin 		    inp->inp_snd_tag->ifp != ifp))
254fb3bc596SJohn Baldwin 			in_pcboutput_txrtlmt(inp, ifp, m);
255fb3bc596SJohn Baldwin 
256fb3bc596SJohn Baldwin 		if (inp->inp_snd_tag != NULL)
257fb3bc596SJohn Baldwin 			mst = inp->inp_snd_tag;
258fb3bc596SJohn Baldwin 	}
259fb3bc596SJohn Baldwin #endif
26035c7bb34SRandall Stewart 	if (stamp_tag && mst != NULL) {
261fb3bc596SJohn Baldwin 		KASSERT(m->m_pkthdr.rcvif == NULL,
262fb3bc596SJohn Baldwin 		    ("trying to add a send tag to a forwarded packet"));
263fb3bc596SJohn Baldwin 		if (mst->ifp != ifp) {
2649ba11796SAndrew Gallatin 			m_freem(m);
265fb3bc596SJohn Baldwin 			error = EAGAIN;
266fb3bc596SJohn Baldwin 			goto done;
267fb3bc596SJohn Baldwin 		}
268fb3bc596SJohn Baldwin 
269fb3bc596SJohn Baldwin 		/* stamp send tag on mbuf */
270fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
271fb3bc596SJohn Baldwin 		m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
272fb3bc596SJohn Baldwin 	}
273fb3bc596SJohn Baldwin 
27462e1a437SZhenlei Huang 	error = (*ifp->if_output)(ifp, m, gw, ro);
275fb3bc596SJohn Baldwin 
276fb3bc596SJohn Baldwin done:
277fb3bc596SJohn Baldwin 	/* Check for route change invalidating send tags. */
278b2e60773SJohn Baldwin #ifdef KERN_TLS
279b2e60773SJohn Baldwin 	if (tls != NULL) {
280b2e60773SJohn Baldwin 		if (error == EAGAIN)
281b2e60773SJohn Baldwin 			error = ktls_output_eagain(inp, tls);
282b2e60773SJohn Baldwin 		ktls_free(tls);
283b2e60773SJohn Baldwin 	}
284b2e60773SJohn Baldwin #endif
285fb3bc596SJohn Baldwin #ifdef RATELIMIT
286fb3bc596SJohn Baldwin 	if (error == EAGAIN)
287fb3bc596SJohn Baldwin 		in_pcboutput_eagain(inp);
288fb3bc596SJohn Baldwin #endif
289fb3bc596SJohn Baldwin 	return (error);
290fb3bc596SJohn Baldwin }
291fb3bc596SJohn Baldwin 
292983066f0SAlexander V. Chernikov /* rte<>ro_flags translation */
293983066f0SAlexander V. Chernikov static inline void
2949748eb74SAlexander V. Chernikov rt_update_ro_flags(struct route *ro, const struct nhop_object *nh)
295983066f0SAlexander V. Chernikov {
2969748eb74SAlexander V. Chernikov 	int nh_flags = nh->nh_flags;
297983066f0SAlexander V. Chernikov 
298983066f0SAlexander V. Chernikov 	ro->ro_flags &= ~ (RT_REJECT|RT_BLACKHOLE|RT_HAS_GW);
299983066f0SAlexander V. Chernikov 
300983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_REJECT) ? RT_REJECT : 0;
301983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_BLACKHOLE) ? RT_BLACKHOLE : 0;
302983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_GATEWAY) ? RT_HAS_GW : 0;
303983066f0SAlexander V. Chernikov }
304983066f0SAlexander V. Chernikov 
305df8bae1dSRodney W. Grimes /*
306df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
307df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
308df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
309df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
310bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
311bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
312bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
313bf984051SGleb Smirnoff  *
3143de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
3153de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
316df8bae1dSRodney W. Grimes  */
317df8bae1dSRodney W. Grimes int
318f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
319f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
320df8bae1dSRodney W. Grimes {
3211e78ac21SJeffrey Hsu 	struct ip *ip;
322fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
323ac9d7e26SMax Laier 	struct mbuf *m0;
32423bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
325374ce248SMitchell Horne 	int mtu = 0;
326ca8b83b0SLuigi Rizzo 	int error = 0;
327868aabb4SRichard Scheffenegger 	int vlan_pcp = -1;
32862e1a437SZhenlei Huang 	struct sockaddr_in *dst;
32962e1a437SZhenlei Huang 	const struct sockaddr *gw;
330374ce248SMitchell Horne 	struct in_ifaddr *ia = NULL;
3316ca363ebSGleb Smirnoff 	struct in_addr src;
332*8f1d5cf5SGleb Smirnoff 	bool isbroadcast;
333078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
33462e1a437SZhenlei Huang 	struct route iproute;
3359c57a5b6SHiroki Sato 	uint32_t fibnum;
336fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
3371a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
3381a499816SEdward Tomasz Napierala #endif
33977a01441SJohn Baldwin 
340ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
341b9555453SGleb Smirnoff 	NET_EPOCH_ASSERT();
34236e8826fSMax Laier 
343bc97ba51SJulian Elischer 	if (inp != NULL) {
344baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
34562e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
346c2529042SHans Petter Selasky 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
34780cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
3489c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
34980cb9f21SKip Macy 		}
350868aabb4SRichard Scheffenegger 		if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
351868aabb4SRichard Scheffenegger 			vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
352868aabb4SRichard Scheffenegger 			    INP_2PCP_SHIFT;
35350575ce1SAndrew Gallatin #ifdef NUMA
35450575ce1SAndrew Gallatin 		m->m_pkthdr.numa_domain = inp->inp_numa_domain;
35550575ce1SAndrew Gallatin #endif
356bc97ba51SJulian Elischer 	}
35762e1ba83SRobert Watson 
358df8bae1dSRodney W. Grimes 	if (opt) {
359ca8b83b0SLuigi Rizzo 		int len = 0;
360df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
361cb7641e8SMaxim Konovalov 		if (len != 0)
362ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
363df8bae1dSRodney W. Grimes 	}
364df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
3658f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
3668f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
3673efc3014SJulian Elischer 
368df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
36953be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
37053be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
3716d947416SGleb Smirnoff 		ip_fillid(ip);
372df8bae1dSRodney W. Grimes 	} else {
373ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
37453be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
375df8bae1dSRodney W. Grimes 	}
3763924dfa7SMichael Tuexen 	if ((flags & IP_FORWARDING) == 0)
3773924dfa7SMichael Tuexen 		IPSTAT_INC(ips_localout);
3789c9137eaSGarrett Wollman 
379054692a4SAlexander V. Chernikov 	/*
380054692a4SAlexander V. Chernikov 	 * dst/gw handling:
381054692a4SAlexander V. Chernikov 	 *
3823c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
3833c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
384054692a4SAlexander V. Chernikov 	 */
3859c57a5b6SHiroki Sato 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
38662e1a437SZhenlei Huang 	if (ro == NULL) {
38762e1a437SZhenlei Huang 		ro = &iproute;
38862e1a437SZhenlei Huang 		bzero(ro, sizeof (*ro));
38962e1a437SZhenlei Huang 	}
3906ca363ebSGleb Smirnoff 	dst = (struct sockaddr_in *)&ro->ro_dst;
39162e1a437SZhenlei Huang 	if (ro->ro_nh == NULL) {
392df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
393df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
3949b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
395df8bae1dSRodney W. Grimes 	}
39662e1a437SZhenlei Huang 	gw = (const struct sockaddr *)dst;
397d9f2a782SErmal Luçi again:
39884cc0778SGeorge V. Neville-Neil 	/*
39984cc0778SGeorge V. Neville-Neil 	 * Validate route against routing table additions;
40084cc0778SGeorge V. Neville-Neil 	 * a better/more specific route might have been added.
40184cc0778SGeorge V. Neville-Neil 	 */
40262e1a437SZhenlei Huang 	if (inp != NULL && ro->ro_nh != NULL)
403983066f0SAlexander V. Chernikov 		NH_VALIDATE(ro, &inp->inp_rt_cookie, fibnum);
40484cc0778SGeorge V. Neville-Neil 	/*
40584cc0778SGeorge V. Neville-Neil 	 * If there is a cached route,
40684cc0778SGeorge V. Neville-Neil 	 * check that it is to the same destination
40784cc0778SGeorge V. Neville-Neil 	 * and is still up.  If not, free it and try again.
40884cc0778SGeorge V. Neville-Neil 	 * The address family should also be checked in case of sharing the
40984cc0778SGeorge V. Neville-Neil 	 * cache with IPv6.
41084cc0778SGeorge V. Neville-Neil 	 * Also check whether routing cache needs invalidation.
41184cc0778SGeorge V. Neville-Neil 	 */
41262e1a437SZhenlei Huang 	if (ro->ro_nh != NULL &&
413174fb9dbSAlexander V. Chernikov 	    ((!NH_IS_VALID(ro->ro_nh)) || dst->sin_family != AF_INET ||
4146ca363ebSGleb Smirnoff 	    dst->sin_addr.s_addr != ip->ip_dst.s_addr))
415fc21c53fSRyan Stone 		RO_INVALIDATE_CACHE(ro);
416d9f2a782SErmal Luçi 	ia = NULL;
417df8bae1dSRodney W. Grimes 	/*
418a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
419a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
420a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
421a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
422df8bae1dSRodney W. Grimes 	 */
423a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
4244f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
42558a39d8cSAlan Somers 						      M_GETFIB(m)))) == NULL &&
4264f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
42758a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL) {
42886425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
429a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
430a3fd02d8SBruce M Simpson 			goto bad;
431a3fd02d8SBruce M Simpson 		}
432a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
433a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
434a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
4356ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
436a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
437*8f1d5cf5SGleb Smirnoff 		isbroadcast = true;
4386ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
439a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
4404f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
44158a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL &&
4424f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
44358a39d8cSAlan Somers 						M_GETFIB(m)))) == NULL) {
44486425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
445df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
446df8bae1dSRodney W. Grimes 			goto bad;
447df8bae1dSRodney W. Grimes 		}
448df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
4496ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
450df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
4516c1bd558SRyan Stone 		isbroadcast = ifp->if_flags & IFF_BROADCAST ?
4526c1bd558SRyan Stone 		    in_ifaddr_broadcast(dst->sin_addr, ia) : 0;
4536ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
4543afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
45538c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
4563afefa39SDaniel C. Sobral 		/*
45738c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
45838c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
4593afefa39SDaniel C. Sobral 		 */
46038c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
4616ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
4622144431cSGleb Smirnoff 		IFP_TO_IA(ifp, ia);
463*8f1d5cf5SGleb Smirnoff 		isbroadcast = false;
46454bb7ac0SGleb Smirnoff 		/* Interface may have no addresses. */
46554bb7ac0SGleb Smirnoff 		if (ia != NULL)
4666ca363ebSGleb Smirnoff 			src = IA_SIN(ia)->sin_addr;
46754bb7ac0SGleb Smirnoff 		else
46854bb7ac0SGleb Smirnoff 			src.s_addr = INADDR_ANY;
46962e1a437SZhenlei Huang 	} else if (ro != &iproute) {
470983066f0SAlexander V. Chernikov 		if (ro->ro_nh == NULL) {
4712c17fe93SGarrett Wollman 			/*
4726ca363ebSGleb Smirnoff 			 * We want to do any cloning requested by the link
4736ca363ebSGleb Smirnoff 			 * layer, as this is probably required in all cases
4746ca363ebSGleb Smirnoff 			 * for correct operation (as it is for ARP).
4752c17fe93SGarrett Wollman 			 */
4764043ee3cSAlexander V. Chernikov 			uint32_t flowid;
4774043ee3cSAlexander V. Chernikov 			flowid = m->m_pkthdr.flowid;
4784043ee3cSAlexander V. Chernikov 			ro->ro_nh = fib4_lookup(fibnum, dst->sin_addr, 0,
4794043ee3cSAlexander V. Chernikov 			    NHR_REF, flowid);
4804043ee3cSAlexander V. Chernikov 
481174fb9dbSAlexander V. Chernikov 			if (ro->ro_nh == NULL || (!NH_IS_VALID(ro->ro_nh))) {
482fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
4831a499816SEdward Tomasz Napierala 				/*
4841a499816SEdward Tomasz Napierala 				 * There is no route for this packet, but it is
4851a499816SEdward Tomasz Napierala 				 * possible that a matching SPD entry exists.
4861a499816SEdward Tomasz Napierala 				 */
4871a499816SEdward Tomasz Napierala 				no_route_but_check_spd = 1;
4881a499816SEdward Tomasz Napierala 				goto sendit;
4891a499816SEdward Tomasz Napierala #endif
49086425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
491df8bae1dSRodney W. Grimes 				error = EHOSTUNREACH;
492df8bae1dSRodney W. Grimes 				goto bad;
493df8bae1dSRodney W. Grimes 			}
4946ca363ebSGleb Smirnoff 		}
4959748eb74SAlexander V. Chernikov 		struct nhop_object *nh = ro->ro_nh;
4969748eb74SAlexander V. Chernikov 
4979748eb74SAlexander V. Chernikov 		ia = ifatoia(nh->nh_ifa);
4989748eb74SAlexander V. Chernikov 		ifp = nh->nh_ifp;
4999748eb74SAlexander V. Chernikov 		counter_u64_add(nh->nh_pksent, 1);
5009748eb74SAlexander V. Chernikov 		rt_update_ro_flags(ro, nh);
5019748eb74SAlexander V. Chernikov 		if (nh->nh_flags & NHF_GATEWAY)
50262e1a437SZhenlei Huang 			gw = &nh->gw_sa;
5039748eb74SAlexander V. Chernikov 		if (nh->nh_flags & NHF_HOST)
5049748eb74SAlexander V. Chernikov 			isbroadcast = (nh->nh_flags & NHF_BROADCAST);
50562e1a437SZhenlei Huang 		else if ((ifp->if_flags & IFF_BROADCAST) && (gw->sa_family == AF_INET))
50662e1a437SZhenlei Huang 			isbroadcast = in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia);
5076c1bd558SRyan Stone 		else
508*8f1d5cf5SGleb Smirnoff 			isbroadcast = false;
5099748eb74SAlexander V. Chernikov 		mtu = nh->nh_mtu;
5106ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
5116ca363ebSGleb Smirnoff 	} else {
5123553b300SAlexander V. Chernikov 		struct nhop_object *nh;
5136ca363ebSGleb Smirnoff 
5147d98cc09SAndrey V. Elsukov 		nh = fib4_lookup(M_GETFIB(m), dst->sin_addr, 0, NHR_NONE,
5152259a030SAlexander V. Chernikov 		    m->m_pkthdr.flowid);
5163553b300SAlexander V. Chernikov 		if (nh == NULL) {
5176ca363ebSGleb Smirnoff #if defined(IPSEC) || defined(IPSEC_SUPPORT)
5186ca363ebSGleb Smirnoff 			/*
5196ca363ebSGleb Smirnoff 			 * There is no route for this packet, but it is
5206ca363ebSGleb Smirnoff 			 * possible that a matching SPD entry exists.
5216ca363ebSGleb Smirnoff 			 */
5226ca363ebSGleb Smirnoff 			no_route_but_check_spd = 1;
5236ca363ebSGleb Smirnoff 			goto sendit;
5246ca363ebSGleb Smirnoff #endif
5256ca363ebSGleb Smirnoff 			IPSTAT_INC(ips_noroute);
5266ca363ebSGleb Smirnoff 			error = EHOSTUNREACH;
5276ca363ebSGleb Smirnoff 			goto bad;
5286ca363ebSGleb Smirnoff 		}
5293553b300SAlexander V. Chernikov 		ifp = nh->nh_ifp;
5303553b300SAlexander V. Chernikov 		mtu = nh->nh_mtu;
53162e1a437SZhenlei Huang 		rt_update_ro_flags(ro, nh);
5323553b300SAlexander V. Chernikov 		if (nh->nh_flags & NHF_GATEWAY)
53362e1a437SZhenlei Huang 			gw = &nh->gw_sa;
5343553b300SAlexander V. Chernikov 		ia = ifatoia(nh->nh_ifa);
5353553b300SAlexander V. Chernikov 		src = IA_SIN(ia)->sin_addr;
5363553b300SAlexander V. Chernikov 		isbroadcast = (((nh->nh_flags & (NHF_HOST | NHF_BROADCAST)) ==
5376ca363ebSGleb Smirnoff 		    (NHF_HOST | NHF_BROADCAST)) ||
5386ca363ebSGleb Smirnoff 		    ((ifp->if_flags & IFF_BROADCAST) &&
53962e1a437SZhenlei Huang 		    (gw->sa_family == AF_INET) &&
54062e1a437SZhenlei Huang 		    in_ifaddr_broadcast(((const struct sockaddr_in *)gw)->sin_addr, ia)));
5416ca363ebSGleb Smirnoff 	}
5426ca363ebSGleb Smirnoff 
543c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
544983066f0SAlexander V. Chernikov 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, ro=%p (nh_flags=0x%08x) ifp=%p",
5456ca363ebSGleb Smirnoff 	    __func__, mtu, ro,
546983066f0SAlexander V. Chernikov 	    (ro != NULL && ro->ro_nh != NULL) ? ro->ro_nh->nh_flags : 0, ifp));
547d9f2a782SErmal Luçi 
5489b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
549df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
550df8bae1dSRodney W. Grimes 		/*
551183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
552183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
553183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
554183e1c86SGleb Smirnoff 		 */
55562e1a437SZhenlei Huang 		gw = (const struct sockaddr *)dst;
556183e1c86SGleb Smirnoff 		/*
557df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
558df8bae1dSRodney W. Grimes 		 */
559df8bae1dSRodney W. Grimes 		if (imo != NULL) {
560df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
5611c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
5621c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
563bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
564bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
565bbb4330bSLuigi Rizzo 				    INADDR_ANY;
566df8bae1dSRodney W. Grimes 		} else
567df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
568df8bae1dSRodney W. Grimes 		/*
569df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
570df8bae1dSRodney W. Grimes 		 */
5711c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
572df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
57386425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
574df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
575df8bae1dSRodney W. Grimes 				goto bad;
576df8bae1dSRodney W. Grimes 			}
5771c5de19aSGarrett Wollman 		}
578df8bae1dSRodney W. Grimes 		/*
579df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
580df8bae1dSRodney W. Grimes 		 * of outgoing interface.
581df8bae1dSRodney W. Grimes 		 */
5826ca363ebSGleb Smirnoff 		if (ip->ip_src.s_addr == INADDR_ANY)
5836ca363ebSGleb Smirnoff 			ip->ip_src = src;
584df8bae1dSRodney W. Grimes 
5858b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
5868b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
587df8bae1dSRodney W. Grimes 			/*
5888b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
5898b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
5908b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
5918b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
5928b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
593df8bae1dSRodney W. Grimes 			 */
594331dff07SAlexander V. Chernikov 			ip_mloopback(ifp, m, hlen);
5958b889dbbSBruce M Simpson 		} else {
596df8bae1dSRodney W. Grimes 			/*
597df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
598df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
599df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
600df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
601df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
602df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
603df8bae1dSRodney W. Grimes 			 *
604df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
605df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
606df8bae1dSRodney W. Grimes 			 * if necessary.
607df8bae1dSRodney W. Grimes 			 */
608603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
609f0068c4aSGarrett Wollman 				/*
610bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
611f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
612f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
613f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
614f0068c4aSGarrett Wollman 				 */
615603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
616f0068c4aSGarrett Wollman 					imo = NULL;
617bbb4330bSLuigi Rizzo 				if (ip_mforward &&
618bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
619df8bae1dSRodney W. Grimes 					m_freem(m);
620df8bae1dSRodney W. Grimes 					goto done;
621df8bae1dSRodney W. Grimes 				}
622df8bae1dSRodney W. Grimes 			}
623df8bae1dSRodney W. Grimes 		}
6245e9ae478SGarrett Wollman 
625df8bae1dSRodney W. Grimes 		/*
626df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
627df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
628df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
629df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
6308b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
6318b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
6328b889dbbSBruce M Simpson 		 * the loopback interface.
633df8bae1dSRodney W. Grimes 		 */
634f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
635df8bae1dSRodney W. Grimes 			m_freem(m);
636df8bae1dSRodney W. Grimes 			goto done;
637df8bae1dSRodney W. Grimes 		}
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 		goto sendit;
640df8bae1dSRodney W. Grimes 	}
6416a7c943cSAndre Oppermann 
642df8bae1dSRodney W. Grimes 	/*
6432b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
644cb459254SJohn-Mark Gurney 	 * of the outoing interface.
645df8bae1dSRodney W. Grimes 	 */
6466ca363ebSGleb Smirnoff 	if (ip->ip_src.s_addr == INADDR_ANY)
6476ca363ebSGleb Smirnoff 		ip->ip_src = src;
6486a7c943cSAndre Oppermann 
649ca7a789aSMax Laier 	/*
650df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
6518c3f5566SRuslan Ermilov 	 * verify user is allowed to send
652df8bae1dSRodney W. Grimes 	 * such a packet.
653df8bae1dSRodney W. Grimes 	 */
6549f9b3dc4SGarrett Wollman 	if (isbroadcast) {
655df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
656df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
657df8bae1dSRodney W. Grimes 			goto bad;
658df8bae1dSRodney W. Grimes 		}
659df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
660df8bae1dSRodney W. Grimes 			error = EACCES;
661df8bae1dSRodney W. Grimes 			goto bad;
662df8bae1dSRodney W. Grimes 		}
663df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
6648f134647SGleb Smirnoff 		if (ip_len > mtu) {
665df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
666df8bae1dSRodney W. Grimes 			goto bad;
667df8bae1dSRodney W. Grimes 		}
668df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
6699f9b3dc4SGarrett Wollman 	} else {
670df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
6719f9b3dc4SGarrett Wollman 	}
672df8bae1dSRodney W. Grimes 
673f1743588SDarren Reed sendit:
674fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
675fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4)) {
6760ff2d00dSKonstantin Belousov 		struct ip ip_hdr;
677b0e02076SKonstantin Belousov 
67800524fd4SKonstantin Belousov 		if ((error = IPSEC_OUTPUT(ipv4, ifp, m, inp, mtu)) != 0) {
679fcf59617SAndrey V. Elsukov 			if (error == EINPROGRESS)
680fcf59617SAndrey V. Elsukov 				error = 0;
6811dfcf0d2SAndre Oppermann 			goto done;
682fcf59617SAndrey V. Elsukov 		}
6830ff2d00dSKonstantin Belousov 
6840ff2d00dSKonstantin Belousov 		/* Update variables that are affected by ipsec4_output(). */
6850ff2d00dSKonstantin Belousov 		m_copydata(m, 0, sizeof(ip_hdr), (char *)&ip_hdr);
6860ff2d00dSKonstantin Belousov 		hlen = ip_hdr.ip_hl << 2;
68733841545SHajimu UMEMOTO 	}
6880ff2d00dSKonstantin Belousov 
6891a499816SEdward Tomasz Napierala 	/*
6901a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
6911a499816SEdward Tomasz Napierala 	 */
6921a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
6931a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
6941a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
6951a499816SEdward Tomasz Napierala 		goto bad;
6961a499816SEdward Tomasz Napierala 	}
697b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
69833841545SHajimu UMEMOTO 
699c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
700b252313fSGleb Smirnoff 	if (PFIL_HOOKED_OUT(V_inet_pfil_head)) {
70105fc9d78SKristof Provost 		switch (ip_output_pfil(&m, ifp, flags, inp, dst, &fibnum,
70205fc9d78SKristof Provost 		    &error)) {
703d9f2a782SErmal Luçi 		case 1: /* Finished */
704c4ac87eaSDarren Reed 			goto done;
7059b932e9eSAndre Oppermann 
706d9f2a782SErmal Luçi 		case 0: /* Continue normally */
707c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
708185c1cddSKristof Provost 			ip_len = ntohs(ip->ip_len);
709d9f2a782SErmal Luçi 			break;
710fed1c7e9SSøren Schmidt 
711d9f2a782SErmal Luçi 		case -1: /* Need to try again */
712d9f2a782SErmal Luçi 			/* Reset everything for a new round */
7136ca363ebSGleb Smirnoff 			if (ro != NULL) {
714983066f0SAlexander V. Chernikov 				RO_NHFREE(ro);
7154fb3a820SAlexander V. Chernikov 				ro->ro_prepend = NULL;
7166ca363ebSGleb Smirnoff 			}
71762e1a437SZhenlei Huang 			gw = (const struct sockaddr *)dst;
718d9f2a782SErmal Luçi 			ip = mtod(m, struct ip *);
7199b932e9eSAndre Oppermann 			goto again;
720d9f2a782SErmal Luçi 		}
721099dd043SAndre Oppermann 	}
7222b25acc1SLuigi Rizzo 
723868aabb4SRichard Scheffenegger 	if (vlan_pcp > -1)
724868aabb4SRichard Scheffenegger 		EVL_APPLY_PRI(m, vlan_pcp);
725868aabb4SRichard Scheffenegger 
7266c1c6ae5SRodney W. Grimes 	/* IN_LOOPBACK must not appear on the wire - RFC1122. */
7276c1c6ae5SRodney W. Grimes 	if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) ||
7286c1c6ae5SRodney W. Grimes 	    IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) {
72951c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
73086425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
73151c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
73251c8ec4aSRuslan Ermilov 			goto bad;
73351c8ec4aSRuslan Ermilov 		}
73451c8ec4aSRuslan Ermilov 	}
73551c8ec4aSRuslan Ermilov 
73644775b16SMark Johnston 	/* Ensure the packet data is mapped if the interface requires it. */
73744775b16SMark Johnston 	if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
738b0e02076SKonstantin Belousov 		struct mbuf *m1;
739b0e02076SKonstantin Belousov 
740b0e02076SKonstantin Belousov 		error = mb_unmapped_to_ext(m, &m1);
741b0e02076SKonstantin Belousov 		if (error != 0) {
742b0e02076SKonstantin Belousov 			if (error == EINVAL) {
743b0e02076SKonstantin Belousov 				if_printf(ifp, "TLS packet\n");
744b0e02076SKonstantin Belousov 				/* XXXKIB */
745b0e02076SKonstantin Belousov 			} else if (error == ENOMEM) {
74644775b16SMark Johnston 				error = ENOBUFS;
747b0e02076SKonstantin Belousov 			}
748b0e02076SKonstantin Belousov 			IPSTAT_INC(ips_odropped);
74944775b16SMark Johnston 			goto bad;
750b0e02076SKonstantin Belousov 		} else {
751b0e02076SKonstantin Belousov 			m = m1;
75244775b16SMark Johnston 		}
75344775b16SMark Johnston 	}
75444775b16SMark Johnston 
755206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
756078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
757db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
758078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
759db4f9cc7SJonathan Lemon 	}
76095033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
761078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
7621966e5b5SRandall Stewart 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
763078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
7642f4afd21SRandall Stewart 	}
7652f4afd21SRandall Stewart #endif
766db4f9cc7SJonathan Lemon 
767e7319babSPoul-Henning Kamp 	/*
768db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
769233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
770b092fd6cSNavdeep Parhar 	 * Note that if_vxlan could have requested TSO even though the outer
771b092fd6cSNavdeep Parhar 	 * frame is UDP.  It is correct to not fragment such datagrams and
772b092fd6cSNavdeep Parhar 	 * instead just pass them on to the driver.
773df8bae1dSRodney W. Grimes 	 */
77421d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
775b092fd6cSNavdeep Parhar 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist &
776b092fd6cSNavdeep Parhar 	    (CSUM_TSO | CSUM_INNER_TSO)) != 0) {
777df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
778078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
779df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
780078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
781078468edSGleb Smirnoff 		}
7825da9f8faSJosef Karthauser 
783233dcce1SAndre Oppermann 		/*
784233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
785233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
786233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
787233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
788233dcce1SAndre Oppermann 		 */
78938c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
790b092fd6cSNavdeep Parhar 			if (m->m_pkthdr.csum_flags &
791b092fd6cSNavdeep Parhar 			    (CSUM_TSO | CSUM_INNER_TSO))
7927caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
7937caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
794233dcce1SAndre Oppermann 			else
7957caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
7967caf4ab7SGleb Smirnoff 
7977caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
7985da9f8faSJosef Karthauser 		}
79953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
8003390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
801eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
8029d9edc56SMike Silbersack #endif
803147f74d1SAndre Oppermann 		/*
804147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
805147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
806147f74d1SAndre Oppermann 		 */
80786bd0491SAndre Oppermann 		m_clrprotoflags(m);
80857f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
80935c7bb34SRandall Stewart 		error = ip_output_send(inp, ifp, m, gw, ro,
81035c7bb34SRandall Stewart 		    (flags & IP_NO_SND_TAG_RL) ? false : true);
811df8bae1dSRodney W. Grimes 		goto done;
812df8bae1dSRodney W. Grimes 	}
8131e78ac21SJeffrey Hsu 
814233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
815b092fd6cSNavdeep Parhar 	if ((ip_off & IP_DF) ||
816b092fd6cSNavdeep Parhar 	    (m->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_INNER_TSO))) {
817df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
81886425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
819df8bae1dSRodney W. Grimes 		goto bad;
820df8bae1dSRodney W. Grimes 	}
8211e78ac21SJeffrey Hsu 
8221e78ac21SJeffrey Hsu 	/*
8231e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
8241e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
8251e78ac21SJeffrey Hsu 	 */
826078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
8271e78ac21SJeffrey Hsu 	if (error)
828df8bae1dSRodney W. Grimes 		goto bad;
8291e78ac21SJeffrey Hsu 	for (; m; m = m0) {
830df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
831b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
83207203494SDaniel C. Sobral 		if (error == 0) {
833fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
83407203494SDaniel C. Sobral 			if (ia != NULL) {
8357caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
8367caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
8377caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
83807203494SDaniel C. Sobral 			}
839147f74d1SAndre Oppermann 			/*
840147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
841147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
842147f74d1SAndre Oppermann 			 */
84386bd0491SAndre Oppermann 			m_clrprotoflags(m);
844fe937674SJosef Karthauser 
8452e77d270SAndrey V. Elsukov 			IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp,
8462e77d270SAndrey V. Elsukov 			    mtod(m, struct ip *), NULL);
84735c7bb34SRandall Stewart 			error = ip_output_send(inp, ifp, m, gw, ro, true);
848fe937674SJosef Karthauser 		} else
849df8bae1dSRodney W. Grimes 			m_freem(m);
850df8bae1dSRodney W. Grimes 	}
851df8bae1dSRodney W. Grimes 
852df8bae1dSRodney W. Grimes 	if (error == 0)
85386425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
8541e78ac21SJeffrey Hsu 
855df8bae1dSRodney W. Grimes done:
856df8bae1dSRodney W. Grimes 	return (error);
857df8bae1dSRodney W. Grimes  bad:
8583528d68fSBill Paul 	m_freem(m);
859df8bae1dSRodney W. Grimes 	goto done;
860df8bae1dSRodney W. Grimes }
861df8bae1dSRodney W. Grimes 
8621e78ac21SJeffrey Hsu /*
8631e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
8641e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
8651e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
8661e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
8671e78ac21SJeffrey Hsu  *
8681e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
8691e78ac21SJeffrey Hsu  */
8701e78ac21SJeffrey Hsu int
8711e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
872078468edSGleb Smirnoff     u_long if_hwassist_flags)
8731e78ac21SJeffrey Hsu {
8741e78ac21SJeffrey Hsu 	int error = 0;
8751e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
8761e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
8771e78ac21SJeffrey Hsu 	int off;
8781e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
8791e78ac21SJeffrey Hsu 	int firstlen;
8801e78ac21SJeffrey Hsu 	struct mbuf **mnext;
8811e78ac21SJeffrey Hsu 	int nfrags;
88221d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
8831e78ac21SJeffrey Hsu 
88421d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
88521d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
88621d172a3SGleb Smirnoff 
8871e78ac21SJeffrey Hsu 	/*
888da6715bbSGleb Smirnoff 	 * Packet shall not have "Don't Fragment" flag and have at least 8
889da6715bbSGleb Smirnoff 	 * bytes of payload.
8901e78ac21SJeffrey Hsu 	 */
891da6715bbSGleb Smirnoff 	if (__predict_false((ip_off & IP_DF) || len < 8)) {
892da6715bbSGleb Smirnoff 		IPSTAT_INC(ips_cantfrag);
893da6715bbSGleb Smirnoff 		return (EMSGSIZE);
894da6715bbSGleb Smirnoff 	}
8951e78ac21SJeffrey Hsu 
8961e78ac21SJeffrey Hsu 	/*
8971e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
8981e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
8991e78ac21SJeffrey Hsu 	 */
900da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
9011e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
9021e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
9031e78ac21SJeffrey Hsu 	}
90495033af9SMark Johnston #if defined(SCTP) || defined(SCTP_SUPPORT)
905da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
9061966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
9072f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
9082f4afd21SRandall Stewart 	}
9092f4afd21SRandall Stewart #endif
9101e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
9111e78ac21SJeffrey Hsu 		/*
9121e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
9131e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
9141e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
9151e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
9161e78ac21SJeffrey Hsu 		 *
9171e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
9181e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
9191e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
9201e78ac21SJeffrey Hsu 		 */
9211e78ac21SJeffrey Hsu 		int newlen;
9221e78ac21SJeffrey Hsu 
9239c0f6aa7SHans Petter Selasky 		off = MIN(mtu, m0->m_pkthdr.len);
9241e78ac21SJeffrey Hsu 
9251e78ac21SJeffrey Hsu 		/*
9261e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
9271e78ac21SJeffrey Hsu 		 * 8-byte boundary
9281e78ac21SJeffrey Hsu 		 */
9291e78ac21SJeffrey Hsu 		if (off < hlen)
9301e78ac21SJeffrey Hsu 			goto smart_frag_failure;
9311e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
9321e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
9331e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
9341e78ac21SJeffrey Hsu 			/* we failed, go back the default */
9351e78ac21SJeffrey Hsu smart_frag_failure:
9361e78ac21SJeffrey Hsu 			newlen = len;
9371e78ac21SJeffrey Hsu 			off = hlen + len;
9381e78ac21SJeffrey Hsu 		}
9391e78ac21SJeffrey Hsu 		len = newlen;
9401e78ac21SJeffrey Hsu 
9411e78ac21SJeffrey Hsu 	} else {
9421e78ac21SJeffrey Hsu 		off = hlen + len;
9431e78ac21SJeffrey Hsu 	}
9441e78ac21SJeffrey Hsu 
9451e78ac21SJeffrey Hsu 	firstlen = off - hlen;
9461e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
9471e78ac21SJeffrey Hsu 
9481e78ac21SJeffrey Hsu 	/*
9491e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
9501e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
9511e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
9521e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
9531e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
9541e78ac21SJeffrey Hsu 	 */
95521d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
9561e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
9571e78ac21SJeffrey Hsu 		struct mbuf *m;
9581e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
9591e78ac21SJeffrey Hsu 
960dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
9610b17fba7SAndre Oppermann 		if (m == NULL) {
9621e78ac21SJeffrey Hsu 			error = ENOBUFS;
96386425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
9641e78ac21SJeffrey Hsu 			goto done;
9651e78ac21SJeffrey Hsu 		}
966c4c4346fSHans Petter Selasky 		/*
967c4c4346fSHans Petter Selasky 		 * Make sure the complete packet header gets copied
968c4c4346fSHans Petter Selasky 		 * from the originating mbuf to the newly created
969c4c4346fSHans Petter Selasky 		 * mbuf. This also ensures that existing firewall
970c4c4346fSHans Petter Selasky 		 * classification(s), VLAN tags and so on get copied
971c4c4346fSHans Petter Selasky 		 * to the resulting fragmented packet(s):
972c4c4346fSHans Petter Selasky 		 */
973c4c4346fSHans Petter Selasky 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
974c4c4346fSHans Petter Selasky 			m_free(m);
975c4c4346fSHans Petter Selasky 			error = ENOBUFS;
976c4c4346fSHans Petter Selasky 			IPSTAT_INC(ips_odropped);
977c4c4346fSHans Petter Selasky 			goto done;
978c4c4346fSHans Petter Selasky 		}
9791e78ac21SJeffrey Hsu 		/*
9801e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
9811e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
9828b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
9831e78ac21SJeffrey Hsu 		 */
9841e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
9851e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
9861e78ac21SJeffrey Hsu 		*mhip = *ip;
9871e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
9881e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9891e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
9901e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
9911e78ac21SJeffrey Hsu 		}
9921e78ac21SJeffrey Hsu 		m->m_len = mhlen;
99321d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
99421d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
995fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
99621d172a3SGleb Smirnoff 			len = ip_len - off;
997fb86dfcdSAndre Oppermann 		else
9981e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
9991e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
1000eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
10010b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
10021e78ac21SJeffrey Hsu 			m_free(m);
10031e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
100486425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
10051e78ac21SJeffrey Hsu 			goto done;
10061e78ac21SJeffrey Hsu 		}
10071e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
10081e78ac21SJeffrey Hsu #ifdef MAC
100930d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
10101e78ac21SJeffrey Hsu #endif
10111e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
10121e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
1013078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
10141e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
1015078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
1016078468edSGleb Smirnoff 		}
10171e78ac21SJeffrey Hsu 		*mnext = m;
10181e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
10191e78ac21SJeffrey Hsu 	}
102086425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
10211e78ac21SJeffrey Hsu 
10221e78ac21SJeffrey Hsu 	/*
10231e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
10241e78ac21SJeffrey Hsu 	 * and updating header.
10251e78ac21SJeffrey Hsu 	 */
102621d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
10271e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
10281e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
102921d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
10301e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
1031078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
10321e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
1033078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
1034078468edSGleb Smirnoff 	}
10351e78ac21SJeffrey Hsu 
10361e78ac21SJeffrey Hsu done:
10371e78ac21SJeffrey Hsu 	*m_frag = m0;
10381e78ac21SJeffrey Hsu 	return error;
10391e78ac21SJeffrey Hsu }
10401e78ac21SJeffrey Hsu 
10411c238475SJonathan Lemon void
1042db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1043db4f9cc7SJonathan Lemon {
1044db4f9cc7SJonathan Lemon 	struct ip *ip;
10451fdbfb90STom Jones 	struct udphdr *uh;
10461fdbfb90STom Jones 	uint16_t cklen, csum, offset;
1047db4f9cc7SJonathan Lemon 
1048db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
104953be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
10501fdbfb90STom Jones 
10511fdbfb90STom Jones 	if (m->m_pkthdr.csum_flags & CSUM_UDP) {
10521fdbfb90STom Jones 		/* if udp header is not in the first mbuf copy udplen */
1053b6e87011STom Jones 		if (offset + sizeof(struct udphdr) > m->m_len) {
1054590d0a43SAndrey V. Elsukov 			m_copydata(m, offset + offsetof(struct udphdr,
1055590d0a43SAndrey V. Elsukov 			    uh_ulen), sizeof(cklen), (caddr_t)&cklen);
1056b6e87011STom Jones 			cklen = ntohs(cklen);
1057b6e87011STom Jones 		} else {
1058590d0a43SAndrey V. Elsukov 			uh = (struct udphdr *)mtodo(m, offset);
10591fdbfb90STom Jones 			cklen = ntohs(uh->uh_ulen);
10601fdbfb90STom Jones 		}
10611fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen + offset, offset);
10621fdbfb90STom Jones 		if (csum == 0)
1063206a3274SRuslan Ermilov 			csum = 0xffff;
10641fdbfb90STom Jones 	} else {
10651fdbfb90STom Jones 		cklen = ntohs(ip->ip_len);
10661fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen, offset);
10671fdbfb90STom Jones 	}
1068db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1069db4f9cc7SJonathan Lemon 
1070590d0a43SAndrey V. Elsukov 	if (offset + sizeof(csum) > m->m_len)
1071590d0a43SAndrey V. Elsukov 		m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
1072590d0a43SAndrey V. Elsukov 	else
1073590d0a43SAndrey V. Elsukov 		*(u_short *)mtodo(m, offset) = csum;
1074db4f9cc7SJonathan Lemon }
1075db4f9cc7SJonathan Lemon 
1076df8bae1dSRodney W. Grimes /*
1077df8bae1dSRodney W. Grimes  * IP socket option processing.
1078df8bae1dSRodney W. Grimes  */
1079df8bae1dSRodney W. Grimes int
1080f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
1081df8bae1dSRodney W. Grimes {
1082cfe8b629SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1083cfe8b629SGarrett Wollman 	int	error, optval;
1084dc847eb6SAdrian Chadd #ifdef	RSS
1085dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
1086dc847eb6SAdrian Chadd 	int retval;
1087dc847eb6SAdrian Chadd #endif
1088df8bae1dSRodney W. Grimes 
1089cfe8b629SGarrett Wollman 	error = optval = 0;
1090cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1091fc06cd42SMikolaj Golub 		error = EINVAL;
1092fc06cd42SMikolaj Golub 
1093fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
1094fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
1095fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
1096fc06cd42SMikolaj Golub 			case SO_SETFIB:
1097caccbaefSMark Johnston 				error = sooptcopyin(sopt, &optval,
1098caccbaefSMark Johnston 				    sizeof(optval), sizeof(optval));
1099caccbaefSMark Johnston 				if (error != 0)
1100caccbaefSMark Johnston 					break;
1101caccbaefSMark Johnston 
1102fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1103caccbaefSMark Johnston 				if ((inp->inp_flags & INP_BOUNDFIB) != 0 &&
1104caccbaefSMark Johnston 				    optval != so->so_fibnum) {
1105fc06cd42SMikolaj Golub 					INP_WUNLOCK(inp);
1106caccbaefSMark Johnston 					error = EISCONN;
1107caccbaefSMark Johnston 					break;
1108caccbaefSMark Johnston 				}
1109caccbaefSMark Johnston 				error = sosetfib(inp->inp_socket, optval);
1110caccbaefSMark Johnston 				if (error == 0)
1111caccbaefSMark Johnston 					inp->inp_inc.inc_fibnum = optval;
1112caccbaefSMark Johnston 				INP_WUNLOCK(inp);
1113fc06cd42SMikolaj Golub 				break;
1114f3e7afe2SHans Petter Selasky 			case SO_MAX_PACING_RATE:
1115f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1116f3e7afe2SHans Petter Selasky 				INP_WLOCK(inp);
1117f3e7afe2SHans Petter Selasky 				inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1118f3e7afe2SHans Petter Selasky 				INP_WUNLOCK(inp);
1119f3e7afe2SHans Petter Selasky 				error = 0;
1120f3e7afe2SHans Petter Selasky #else
1121f3e7afe2SHans Petter Selasky 				error = EOPNOTSUPP;
1122f3e7afe2SHans Petter Selasky #endif
1123f3e7afe2SHans Petter Selasky 				break;
1124fc06cd42SMikolaj Golub 			default:
1125fc06cd42SMikolaj Golub 				break;
1126fc06cd42SMikolaj Golub 			}
1127fc06cd42SMikolaj Golub 		}
1128fc06cd42SMikolaj Golub 		return (error);
1129cfe8b629SGarrett Wollman 	}
1130df8bae1dSRodney W. Grimes 
1131cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1132cfe8b629SGarrett Wollman 	case SOPT_SET:
1133cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1134df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1135df8bae1dSRodney W. Grimes #ifdef notyet
1136df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1137df8bae1dSRodney W. Grimes #endif
1138cfe8b629SGarrett Wollman 		{
1139cfe8b629SGarrett Wollman 			struct mbuf *m;
1140cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1141cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1142cfe8b629SGarrett Wollman 				break;
1143cfe8b629SGarrett Wollman 			}
1144dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
11450b17fba7SAndre Oppermann 			if (m == NULL) {
1146cfe8b629SGarrett Wollman 				error = ENOBUFS;
1147cfe8b629SGarrett Wollman 				break;
1148cfe8b629SGarrett Wollman 			}
1149cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1150cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1151cfe8b629SGarrett Wollman 					    m->m_len);
1152635354c4SMaxim Konovalov 			if (error) {
1153635354c4SMaxim Konovalov 				m_free(m);
1154635354c4SMaxim Konovalov 				break;
1155635354c4SMaxim Konovalov 			}
11568501a69cSRobert Watson 			INP_WLOCK(inp);
1157993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
11588501a69cSRobert Watson 			INP_WUNLOCK(inp);
1159993d9505SRobert Watson 			return (error);
1160cfe8b629SGarrett Wollman 		}
1161df8bae1dSRodney W. Grimes 
1162f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
1163f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
1164f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
1165f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
1166f44270e7SPawel Jakub Dawidek 				if (error)
1167be9347e3SAdrian Chadd 					break;
1168be9347e3SAdrian Chadd 			}
1169cef27294SAdrian Chadd 			/* FALLTHROUGH */
1170df8bae1dSRodney W. Grimes 		case IP_TOS:
1171df8bae1dSRodney W. Grimes 		case IP_TTL:
1172936cd18dSAndre Oppermann 		case IP_MINTTL:
1173df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1174df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1175dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1176df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
11774957466bSMatthew N. Dodd 		case IP_RECVTTL:
117882c23ebaSBill Fenner 		case IP_RECVIF:
11798afa2304SBruce M Simpson 		case IP_ONESBCAST:
1180b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
11813cca425bSMichael Tuexen 		case IP_RECVTOS:
11829d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
11839d3ddf43SAdrian Chadd #ifdef	RSS
11849d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
11859d3ddf43SAdrian Chadd #endif
1186868aabb4SRichard Scheffenegger 		case IP_VLAN_PCP:
1187cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1188cfe8b629SGarrett Wollman 					    sizeof optval);
1189cfe8b629SGarrett Wollman 			if (error)
1190cfe8b629SGarrett Wollman 				break;
1191df8bae1dSRodney W. Grimes 
1192cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1193df8bae1dSRodney W. Grimes 			case IP_TOS:
1194ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1195df8bae1dSRodney W. Grimes 				break;
1196df8bae1dSRodney W. Grimes 
1197df8bae1dSRodney W. Grimes 			case IP_TTL:
1198ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1199df8bae1dSRodney W. Grimes 				break;
1200936cd18dSAndre Oppermann 
1201936cd18dSAndre Oppermann 			case IP_MINTTL:
1202a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1203936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1204936cd18dSAndre Oppermann 				else
1205936cd18dSAndre Oppermann 					error = EINVAL;
1206936cd18dSAndre Oppermann 				break;
1207936cd18dSAndre Oppermann 
1208a138d217SRobert Watson #define	OPTSET(bit) do {						\
12098501a69cSRobert Watson 	INP_WLOCK(inp);							\
1210df8bae1dSRodney W. Grimes 	if (optval)							\
1211df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1212df8bae1dSRodney W. Grimes 	else								\
1213a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
12148501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1215a138d217SRobert Watson } while (0)
1216df8bae1dSRodney W. Grimes 
12170a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
12180a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
12190a100a6fSAdrian Chadd 	if (val)							\
12200a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
12210a100a6fSAdrian Chadd 	else								\
12220a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
12230a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
12240a100a6fSAdrian Chadd } while (0)
12250a100a6fSAdrian Chadd 
1226df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1227df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1228df8bae1dSRodney W. Grimes 				break;
1229df8bae1dSRodney W. Grimes 
1230df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1231df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1232df8bae1dSRodney W. Grimes 				break;
1233df8bae1dSRodney W. Grimes 
1234df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1235df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1236df8bae1dSRodney W. Grimes 				break;
123782c23ebaSBill Fenner 
1238dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1239dce33a45SErmal Luçi 				OPTSET2(INP_ORIGDSTADDR, optval);
1240dce33a45SErmal Luçi 				break;
1241dce33a45SErmal Luçi 
12424957466bSMatthew N. Dodd 			case IP_RECVTTL:
12434957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
12444957466bSMatthew N. Dodd 				break;
12454957466bSMatthew N. Dodd 
124682c23ebaSBill Fenner 			case IP_RECVIF:
124782c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
124882c23ebaSBill Fenner 				break;
12496a800098SYoshinobu Inoue 
12508afa2304SBruce M Simpson 			case IP_ONESBCAST:
12518afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
12528afa2304SBruce M Simpson 				break;
1253b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1254b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1255b2828ad2SAndre Oppermann 				break;
1256f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1257f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1258be9347e3SAdrian Chadd 				break;
12593cca425bSMichael Tuexen 			case IP_RECVTOS:
12603cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
12613cca425bSMichael Tuexen 				break;
12629d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
12639d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
12649d3ddf43SAdrian Chadd 				break;
12650a100a6fSAdrian Chadd #ifdef RSS
12669d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
12679d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
12689d3ddf43SAdrian Chadd 				break;
12690a100a6fSAdrian Chadd #endif
1270868aabb4SRichard Scheffenegger 			case IP_VLAN_PCP:
1271868aabb4SRichard Scheffenegger 				if ((optval >= -1) && (optval <=
1272868aabb4SRichard Scheffenegger 				    (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1273868aabb4SRichard Scheffenegger 					if (optval == -1) {
1274868aabb4SRichard Scheffenegger 						INP_WLOCK(inp);
1275868aabb4SRichard Scheffenegger 						inp->inp_flags2 &=
1276868aabb4SRichard Scheffenegger 						    ~(INP_2PCP_SET |
1277868aabb4SRichard Scheffenegger 						      INP_2PCP_MASK);
1278868aabb4SRichard Scheffenegger 						INP_WUNLOCK(inp);
1279868aabb4SRichard Scheffenegger 					} else {
1280868aabb4SRichard Scheffenegger 						INP_WLOCK(inp);
1281868aabb4SRichard Scheffenegger 						inp->inp_flags2 |=
1282868aabb4SRichard Scheffenegger 						    INP_2PCP_SET;
1283868aabb4SRichard Scheffenegger 						inp->inp_flags2 &=
1284868aabb4SRichard Scheffenegger 						    ~INP_2PCP_MASK;
1285868aabb4SRichard Scheffenegger 						inp->inp_flags2 |=
1286868aabb4SRichard Scheffenegger 						    optval << INP_2PCP_SHIFT;
1287868aabb4SRichard Scheffenegger 						INP_WUNLOCK(inp);
1288868aabb4SRichard Scheffenegger 					}
1289868aabb4SRichard Scheffenegger 				} else
1290868aabb4SRichard Scheffenegger 					error = EINVAL;
1291868aabb4SRichard Scheffenegger 				break;
1292df8bae1dSRodney W. Grimes 			}
1293df8bae1dSRodney W. Grimes 			break;
1294df8bae1dSRodney W. Grimes #undef OPTSET
12950a100a6fSAdrian Chadd #undef OPTSET2
1296df8bae1dSRodney W. Grimes 
129771498f30SBruce M Simpson 		/*
129871498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
129971498f30SBruce M Simpson 		 * module.
130071498f30SBruce M Simpson 		 */
1301df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1302f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1303df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1304df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1305df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1306df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
130771498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
130871498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
130971498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
131071498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
131171498f30SBruce M Simpson 		case IP_MSFILTER:
131271498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
131371498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
131471498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
131571498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
131671498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
131771498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
131871498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1319df8bae1dSRodney W. Grimes 			break;
1320df8bae1dSRodney W. Grimes 
132133b3ac06SPeter Wemm 		case IP_PORTRANGE:
1322cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1323cfe8b629SGarrett Wollman 					    sizeof optval);
1324cfe8b629SGarrett Wollman 			if (error)
1325cfe8b629SGarrett Wollman 				break;
132633b3ac06SPeter Wemm 
13278501a69cSRobert Watson 			INP_WLOCK(inp);
132833b3ac06SPeter Wemm 			switch (optval) {
132933b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
133033b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
133133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
133233b3ac06SPeter Wemm 				break;
133333b3ac06SPeter Wemm 
133433b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
133533b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
133633b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
133733b3ac06SPeter Wemm 				break;
133833b3ac06SPeter Wemm 
133933b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
134033b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
134133b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
134233b3ac06SPeter Wemm 				break;
134333b3ac06SPeter Wemm 
134433b3ac06SPeter Wemm 			default:
134533b3ac06SPeter Wemm 				error = EINVAL;
134633b3ac06SPeter Wemm 				break;
134733b3ac06SPeter Wemm 			}
13488501a69cSRobert Watson 			INP_WUNLOCK(inp);
1349ce8c72b1SPeter Wemm 			break;
135033b3ac06SPeter Wemm 
1351fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13526a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1353fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1354fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
13556a800098SYoshinobu Inoue 				break;
13566a800098SYoshinobu Inoue 			}
1357fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1358b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
13596a800098SYoshinobu Inoue 
1360df8bae1dSRodney W. Grimes 		default:
1361df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1362df8bae1dSRodney W. Grimes 			break;
1363df8bae1dSRodney W. Grimes 		}
1364df8bae1dSRodney W. Grimes 		break;
1365df8bae1dSRodney W. Grimes 
1366cfe8b629SGarrett Wollman 	case SOPT_GET:
1367cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1368df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1369df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1370e5e3e746SMatt Macy 			INP_RLOCK(inp);
1371e5e3e746SMatt Macy 			if (inp->inp_options) {
1372e5e3e746SMatt Macy 				struct mbuf *options;
1373e5e3e746SMatt Macy 
137410731c54SMichael Tuexen 				options = m_copym(inp->inp_options, 0,
137510731c54SMichael Tuexen 				    M_COPYALL, M_NOWAIT);
1376e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1377e5e3e746SMatt Macy 				if (options != NULL) {
1378cfe8b629SGarrett Wollman 					error = sooptcopyout(sopt,
1379e5e3e746SMatt Macy 							     mtod(options, char *),
1380e5e3e746SMatt Macy 							     options->m_len);
1381e5e3e746SMatt Macy 					m_freem(options);
1382e5e3e746SMatt Macy 				} else
1383e5e3e746SMatt Macy 					error = ENOMEM;
1384e5e3e746SMatt Macy 			} else {
1385e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1386cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1387e5e3e746SMatt Macy 			}
1388df8bae1dSRodney W. Grimes 			break;
1389df8bae1dSRodney W. Grimes 
1390df8bae1dSRodney W. Grimes 		case IP_TOS:
1391df8bae1dSRodney W. Grimes 		case IP_TTL:
1392936cd18dSAndre Oppermann 		case IP_MINTTL:
1393df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1394df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1395dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1396df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
13974957466bSMatthew N. Dodd 		case IP_RECVTTL:
139882c23ebaSBill Fenner 		case IP_RECVIF:
1399cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
14008afa2304SBruce M Simpson 		case IP_ONESBCAST:
1401b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
14025f6bf451SAttilio Rao 		case IP_BINDANY:
14033cca425bSMichael Tuexen 		case IP_RECVTOS:
14049c423972SAdrian Chadd 		case IP_FLOWID:
14059c423972SAdrian Chadd 		case IP_FLOWTYPE:
14069d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
14070a100a6fSAdrian Chadd #ifdef	RSS
14080a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
14099d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
14100a100a6fSAdrian Chadd #endif
1411868aabb4SRichard Scheffenegger 		case IP_VLAN_PCP:
1412cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1413df8bae1dSRodney W. Grimes 			case IP_TOS:
1414ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1415df8bae1dSRodney W. Grimes 				break;
1416df8bae1dSRodney W. Grimes 
1417df8bae1dSRodney W. Grimes 			case IP_TTL:
1418ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1419df8bae1dSRodney W. Grimes 				break;
1420df8bae1dSRodney W. Grimes 
1421936cd18dSAndre Oppermann 			case IP_MINTTL:
1422936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1423936cd18dSAndre Oppermann 				break;
1424936cd18dSAndre Oppermann 
1425df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
14260a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1429df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1430df8bae1dSRodney W. Grimes 				break;
1431df8bae1dSRodney W. Grimes 
1432df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1433df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1434df8bae1dSRodney W. Grimes 				break;
1435df8bae1dSRodney W. Grimes 
1436df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1437df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1438df8bae1dSRodney W. Grimes 				break;
143982c23ebaSBill Fenner 
1440dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1441dce33a45SErmal Luçi 				optval = OPTBIT2(INP_ORIGDSTADDR);
1442dce33a45SErmal Luçi 				break;
1443dce33a45SErmal Luçi 
14444957466bSMatthew N. Dodd 			case IP_RECVTTL:
14454957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
14464957466bSMatthew N. Dodd 				break;
14474957466bSMatthew N. Dodd 
144882c23ebaSBill Fenner 			case IP_RECVIF:
144982c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
145082c23ebaSBill Fenner 				break;
1451cfe8b629SGarrett Wollman 
1452cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1453cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1454cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1455cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1456cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1457cfe8b629SGarrett Wollman 				else
1458cfe8b629SGarrett Wollman 					optval = 0;
1459cfe8b629SGarrett Wollman 				break;
14606a800098SYoshinobu Inoue 
14618afa2304SBruce M Simpson 			case IP_ONESBCAST:
14628afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
14638afa2304SBruce M Simpson 				break;
1464b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1465b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1466b2828ad2SAndre Oppermann 				break;
14675f6bf451SAttilio Rao 			case IP_BINDANY:
14685f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
14695f6bf451SAttilio Rao 				break;
14703cca425bSMichael Tuexen 			case IP_RECVTOS:
14713cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
14723cca425bSMichael Tuexen 				break;
14739c423972SAdrian Chadd 			case IP_FLOWID:
14749c423972SAdrian Chadd 				optval = inp->inp_flowid;
14759c423972SAdrian Chadd 				break;
14769c423972SAdrian Chadd 			case IP_FLOWTYPE:
14779c423972SAdrian Chadd 				optval = inp->inp_flowtype;
14789c423972SAdrian Chadd 				break;
14799d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
14809d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
14819d3ddf43SAdrian Chadd 				break;
14829c423972SAdrian Chadd #ifdef	RSS
14837847796aSAdrian Chadd 			case IP_RSSBUCKETID:
14847847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
14857847796aSAdrian Chadd 				    inp->inp_flowtype,
14867847796aSAdrian Chadd 				    &rss_bucket);
14877847796aSAdrian Chadd 				if (retval == 0)
14887847796aSAdrian Chadd 					optval = rss_bucket;
14897847796aSAdrian Chadd 				else
14907847796aSAdrian Chadd 					error = EINVAL;
14919c423972SAdrian Chadd 				break;
14929d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
14939d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
14949d3ddf43SAdrian Chadd 				break;
14959c423972SAdrian Chadd #endif
1496868aabb4SRichard Scheffenegger 			case IP_VLAN_PCP:
1497868aabb4SRichard Scheffenegger 				if (OPTBIT2(INP_2PCP_SET)) {
1498868aabb4SRichard Scheffenegger 					optval = (inp->inp_flags2 &
1499868aabb4SRichard Scheffenegger 					    INP_2PCP_MASK) >> INP_2PCP_SHIFT;
1500868aabb4SRichard Scheffenegger 				} else {
1501868aabb4SRichard Scheffenegger 					optval = -1;
1502868aabb4SRichard Scheffenegger 				}
1503868aabb4SRichard Scheffenegger 				break;
1504df8bae1dSRodney W. Grimes 			}
1505cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1506df8bae1dSRodney W. Grimes 			break;
1507df8bae1dSRodney W. Grimes 
150871498f30SBruce M Simpson 		/*
150971498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
151071498f30SBruce M Simpson 		 * module.
151171498f30SBruce M Simpson 		 */
1512df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1513f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1514df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1515df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
151671498f30SBruce M Simpson 		case IP_MSFILTER:
151771498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
151833b3ac06SPeter Wemm 			break;
151933b3ac06SPeter Wemm 
1520fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
15216a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1522fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1523fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
15246a800098SYoshinobu Inoue 				break;
15256a800098SYoshinobu Inoue 			}
1526fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1527b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15286a800098SYoshinobu Inoue 
1529df8bae1dSRodney W. Grimes 		default:
1530df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1531df8bae1dSRodney W. Grimes 			break;
1532df8bae1dSRodney W. Grimes 		}
1533df8bae1dSRodney W. Grimes 		break;
1534df8bae1dSRodney W. Grimes 	}
1535df8bae1dSRodney W. Grimes 	return (error);
1536df8bae1dSRodney W. Grimes }
1537df8bae1dSRodney W. Grimes 
1538df8bae1dSRodney W. Grimes /*
1539df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1540df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1541df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1542f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1543f5fea3ddSPaul Traina  * replicating that code here.
1544df8bae1dSRodney W. Grimes  */
1545df8bae1dSRodney W. Grimes static void
1546331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen)
1547df8bae1dSRodney W. Grimes {
1548331dff07SAlexander V. Chernikov 	struct ip *ip;
1549df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1550df8bae1dSRodney W. Grimes 
1551e4762f75SGeorge V. Neville-Neil 	/*
1552e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1553e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1554e4762f75SGeorge V. Neville-Neil 	 */
1555eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
1556f0cace5dSRobert Watson 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
155786b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1558df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1559390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1560390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1561390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1562390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1563390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1564390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1565390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1566390cdc6aSRuslan Ermilov 		}
1567df8bae1dSRodney W. Grimes 		/*
1568df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1569df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1570df8bae1dSRodney W. Grimes 		 */
15718f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1572df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
157386b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1574331dff07SAlexander V. Chernikov 		if_simloop(ifp, copym, AF_INET, 0);
1575df8bae1dSRodney W. Grimes 	}
1576df8bae1dSRodney W. Grimes }
1577