xref: /freebsd/sys/netinet/ip_output.c (revision 7b6c99d08d57bd6aeee333734b801208914fd788)
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"
41e440aed9SQing Li #include "opt_mpath.h"
4277a01441SJohn Baldwin #include "opt_ratelimit.h"
4357f60867SMark Johnston #include "opt_route.h"
449c423972SAdrian Chadd #include "opt_rss.h"
4577a01441SJohn Baldwin #include "opt_sctp.h"
46fbd1372aSJoerg Wunsch 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
4826f9a767SRodney W. Grimes #include <sys/systm.h>
49f0f6d643SLuigi Rizzo #include <sys/kernel.h>
50b2e60773SJohn Baldwin #include <sys/ktls.h>
51cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
52df8bae1dSRodney W. Grimes #include <sys/malloc.h>
53df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
54acd3428bSRobert Watson #include <sys/priv.h>
55c26fe973SBjoern A. Zeeb #include <sys/proc.h>
56df8bae1dSRodney W. Grimes #include <sys/protosw.h>
57cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
5857f60867SMark Johnston #include <sys/sdt.h>
59df8bae1dSRodney W. Grimes #include <sys/socket.h>
60df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
619d9edc56SMike Silbersack #include <sys/sysctl.h>
62c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes #include <net/if.h>
6576039bc8SGleb Smirnoff #include <net/if_var.h>
663ef5e21dSQing Li #include <net/if_llatbl.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 
892f4afd21SRandall Stewart #ifdef SCTP
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 
108afed1b49SDarren Reed 
1098b889dbbSBruce M Simpson extern int in_mcast_loop;
11093e0e116SJulian Elischer extern	struct protosw inetsw[];
11193e0e116SJulian Elischer 
112d9f2a782SErmal Luçi static inline int
11305fc9d78SKristof Provost ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, int flags,
11405fc9d78SKristof Provost     struct inpcb *inp, struct sockaddr_in *dst, int *fibnum, int *error)
115d9f2a782SErmal Luçi {
116d9f2a782SErmal Luçi 	struct m_tag *fwd_tag = NULL;
1178f980c01SMark Johnston 	struct mbuf *m;
118d9f2a782SErmal Luçi 	struct in_addr odst;
119d9f2a782SErmal Luçi 	struct ip *ip;
12005fc9d78SKristof Provost 	int pflags = PFIL_OUT;
12105fc9d78SKristof Provost 
12205fc9d78SKristof Provost 	if (flags & IP_FORWARDING)
12305fc9d78SKristof Provost 		pflags |= PFIL_FWD;
124d9f2a782SErmal Luçi 
1258f980c01SMark Johnston 	m = *mp;
126d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
127d9f2a782SErmal Luçi 
128d9f2a782SErmal Luçi 	/* Run through list of hooks for output packets. */
129d9f2a782SErmal Luçi 	odst.s_addr = ip->ip_dst.s_addr;
13005fc9d78SKristof Provost 	switch (pfil_run_hooks(V_inet_pfil_head, mp, ifp, pflags, inp)) {
131b252313fSGleb Smirnoff 	case PFIL_DROPPED:
1328d5c56daSGleb Smirnoff 		*error = EACCES;
133b252313fSGleb Smirnoff 		/* FALLTHROUGH */
134b252313fSGleb Smirnoff 	case PFIL_CONSUMED:
135d9f2a782SErmal Luçi 		return 1; /* Finished */
136b252313fSGleb Smirnoff 	case PFIL_PASS:
137b252313fSGleb Smirnoff 		*error = 0;
138b252313fSGleb Smirnoff 	}
139b252313fSGleb Smirnoff 	m = *mp;
140d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
141d9f2a782SErmal Luçi 
142d9f2a782SErmal Luçi 	/* See if destination IP address was changed by packet filter. */
143d9f2a782SErmal Luçi 	if (odst.s_addr != ip->ip_dst.s_addr) {
144d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
145d9f2a782SErmal Luçi 		/* If destination is now ourself drop to ip_input(). */
146d9f2a782SErmal Luçi 		if (in_localip(ip->ip_dst)) {
147d9f2a782SErmal Luçi 			m->m_flags |= M_FASTFWD_OURS;
148d9f2a782SErmal Luçi 			if (m->m_pkthdr.rcvif == NULL)
149d9f2a782SErmal Luçi 				m->m_pkthdr.rcvif = V_loif;
150d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
151d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |=
152d9f2a782SErmal Luçi 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
153d9f2a782SErmal Luçi 				m->m_pkthdr.csum_data = 0xffff;
154d9f2a782SErmal Luçi 			}
155d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
156d9f2a782SErmal Luçi 				CSUM_IP_CHECKED | CSUM_IP_VALID;
157d9f2a782SErmal Luçi #ifdef SCTP
158d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
159d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
160d9f2a782SErmal Luçi #endif
161d9f2a782SErmal Luçi 			*error = netisr_queue(NETISR_IP, m);
162d9f2a782SErmal Luçi 			return 1; /* Finished */
163d9f2a782SErmal Luçi 		}
164d9f2a782SErmal Luçi 
165d9f2a782SErmal Luçi 		bzero(dst, sizeof(*dst));
166d9f2a782SErmal Luçi 		dst->sin_family = AF_INET;
167d9f2a782SErmal Luçi 		dst->sin_len = sizeof(*dst);
168d9f2a782SErmal Luçi 		dst->sin_addr = ip->ip_dst;
169d9f2a782SErmal Luçi 
170d9f2a782SErmal Luçi 		return -1; /* Reloop */
171d9f2a782SErmal Luçi 	}
172d9f2a782SErmal Luçi 	/* See if fib was changed by packet filter. */
173d9f2a782SErmal Luçi 	if ((*fibnum) != M_GETFIB(m)) {
174d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
175d9f2a782SErmal Luçi 		*fibnum = M_GETFIB(m);
176d9f2a782SErmal Luçi 		return -1; /* Reloop for FIB change */
177d9f2a782SErmal Luçi 	}
178d9f2a782SErmal Luçi 
179d9f2a782SErmal Luçi 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
180d9f2a782SErmal Luçi 	if (m->m_flags & M_FASTFWD_OURS) {
181d9f2a782SErmal Luçi 		if (m->m_pkthdr.rcvif == NULL)
182d9f2a782SErmal Luçi 			m->m_pkthdr.rcvif = V_loif;
183d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
184d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
185d9f2a782SErmal Luçi 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
186d9f2a782SErmal Luçi 			m->m_pkthdr.csum_data = 0xffff;
187d9f2a782SErmal Luçi 		}
188d9f2a782SErmal Luçi #ifdef SCTP
189d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
190d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
191d9f2a782SErmal Luçi #endif
192d9f2a782SErmal Luçi 		m->m_pkthdr.csum_flags |=
193d9f2a782SErmal Luçi 			CSUM_IP_CHECKED | CSUM_IP_VALID;
194d9f2a782SErmal Luçi 
195d9f2a782SErmal Luçi 		*error = netisr_queue(NETISR_IP, m);
196d9f2a782SErmal Luçi 		return 1; /* Finished */
197d9f2a782SErmal Luçi 	}
198d9f2a782SErmal Luçi 	/* Or forward to some other address? */
199d9f2a782SErmal Luçi 	if ((m->m_flags & M_IP_NEXTHOP) &&
200d9f2a782SErmal Luçi 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
201d9f2a782SErmal Luçi 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
202d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
203d9f2a782SErmal Luçi 		m->m_flags &= ~M_IP_NEXTHOP;
204d9f2a782SErmal Luçi 		m_tag_delete(m, fwd_tag);
205d9f2a782SErmal Luçi 
206d9f2a782SErmal Luçi 		return -1; /* Reloop for CHANGE of dst */
207d9f2a782SErmal Luçi 	}
208d9f2a782SErmal Luçi 
209d9f2a782SErmal Luçi 	return 0;
210d9f2a782SErmal Luçi }
211d9f2a782SErmal Luçi 
212fb3bc596SJohn Baldwin static int
213fb3bc596SJohn Baldwin ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m,
21435c7bb34SRandall Stewart     const struct sockaddr_in *gw, struct route *ro, bool stamp_tag)
215fb3bc596SJohn Baldwin {
216b2e60773SJohn Baldwin #ifdef KERN_TLS
217b2e60773SJohn Baldwin 	struct ktls_session *tls = NULL;
218b2e60773SJohn Baldwin #endif
219fb3bc596SJohn Baldwin 	struct m_snd_tag *mst;
220fb3bc596SJohn Baldwin 	int error;
221fb3bc596SJohn Baldwin 
222fb3bc596SJohn Baldwin 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
223fb3bc596SJohn Baldwin 	mst = NULL;
224fb3bc596SJohn Baldwin 
225b2e60773SJohn Baldwin #ifdef KERN_TLS
226b2e60773SJohn Baldwin 	/*
227b2e60773SJohn Baldwin 	 * If this is an unencrypted TLS record, save a reference to
228b2e60773SJohn Baldwin 	 * the record.  This local reference is used to call
229b2e60773SJohn Baldwin 	 * ktls_output_eagain after the mbuf has been freed (thus
230b2e60773SJohn Baldwin 	 * dropping the mbuf's reference) in if_output.
231b2e60773SJohn Baldwin 	 */
232b2e60773SJohn Baldwin 	if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
233*7b6c99d0SGleb Smirnoff 		tls = ktls_hold(m->m_next->m_epg_tls);
234b2e60773SJohn Baldwin 		mst = tls->snd_tag;
235b2e60773SJohn Baldwin 
236b2e60773SJohn Baldwin 		/*
237b2e60773SJohn Baldwin 		 * If a TLS session doesn't have a valid tag, it must
238b2e60773SJohn Baldwin 		 * have had an earlier ifp mismatch, so drop this
239b2e60773SJohn Baldwin 		 * packet.
240b2e60773SJohn Baldwin 		 */
241b2e60773SJohn Baldwin 		if (mst == NULL) {
242b2e60773SJohn Baldwin 			error = EAGAIN;
243b2e60773SJohn Baldwin 			goto done;
244b2e60773SJohn Baldwin 		}
245b2e60773SJohn Baldwin 	}
246b2e60773SJohn Baldwin #endif
247fb3bc596SJohn Baldwin #ifdef RATELIMIT
248b2e60773SJohn Baldwin 	if (inp != NULL && mst == NULL) {
249fb3bc596SJohn Baldwin 		if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
250fb3bc596SJohn Baldwin 		    (inp->inp_snd_tag != NULL &&
251fb3bc596SJohn Baldwin 		    inp->inp_snd_tag->ifp != ifp))
252fb3bc596SJohn Baldwin 			in_pcboutput_txrtlmt(inp, ifp, m);
253fb3bc596SJohn Baldwin 
254fb3bc596SJohn Baldwin 		if (inp->inp_snd_tag != NULL)
255fb3bc596SJohn Baldwin 			mst = inp->inp_snd_tag;
256fb3bc596SJohn Baldwin 	}
257fb3bc596SJohn Baldwin #endif
25835c7bb34SRandall Stewart 	if (stamp_tag && mst != NULL) {
259fb3bc596SJohn Baldwin 		KASSERT(m->m_pkthdr.rcvif == NULL,
260fb3bc596SJohn Baldwin 		    ("trying to add a send tag to a forwarded packet"));
261fb3bc596SJohn Baldwin 		if (mst->ifp != ifp) {
262fb3bc596SJohn Baldwin 			error = EAGAIN;
263fb3bc596SJohn Baldwin 			goto done;
264fb3bc596SJohn Baldwin 		}
265fb3bc596SJohn Baldwin 
266fb3bc596SJohn Baldwin 		/* stamp send tag on mbuf */
267fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
268fb3bc596SJohn Baldwin 		m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
269fb3bc596SJohn Baldwin 	}
270fb3bc596SJohn Baldwin 
271fb3bc596SJohn Baldwin 	error = (*ifp->if_output)(ifp, m, (const struct sockaddr *)gw, ro);
272fb3bc596SJohn Baldwin 
273fb3bc596SJohn Baldwin done:
274fb3bc596SJohn Baldwin 	/* Check for route change invalidating send tags. */
275b2e60773SJohn Baldwin #ifdef KERN_TLS
276b2e60773SJohn Baldwin 	if (tls != NULL) {
277b2e60773SJohn Baldwin 		if (error == EAGAIN)
278b2e60773SJohn Baldwin 			error = ktls_output_eagain(inp, tls);
279b2e60773SJohn Baldwin 		ktls_free(tls);
280b2e60773SJohn Baldwin 	}
281b2e60773SJohn Baldwin #endif
282fb3bc596SJohn Baldwin #ifdef RATELIMIT
283fb3bc596SJohn Baldwin 	if (error == EAGAIN)
284fb3bc596SJohn Baldwin 		in_pcboutput_eagain(inp);
285fb3bc596SJohn Baldwin #endif
286fb3bc596SJohn Baldwin 	return (error);
287fb3bc596SJohn Baldwin }
288fb3bc596SJohn Baldwin 
289983066f0SAlexander V. Chernikov /* rte<>ro_flags translation */
290983066f0SAlexander V. Chernikov static inline void
291983066f0SAlexander V. Chernikov rt_update_ro_flags(struct route *ro)
292983066f0SAlexander V. Chernikov {
293983066f0SAlexander V. Chernikov 	int nh_flags = ro->ro_nh->nh_flags;
294983066f0SAlexander V. Chernikov 
295983066f0SAlexander V. Chernikov 	ro->ro_flags &= ~ (RT_REJECT|RT_BLACKHOLE|RT_HAS_GW);
296983066f0SAlexander V. Chernikov 
297983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_REJECT) ? RT_REJECT : 0;
298983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_BLACKHOLE) ? RT_BLACKHOLE : 0;
299983066f0SAlexander V. Chernikov 	ro->ro_flags |= (nh_flags & NHF_GATEWAY) ? RT_HAS_GW : 0;
300983066f0SAlexander V. Chernikov }
301983066f0SAlexander V. Chernikov 
302df8bae1dSRodney W. Grimes /*
303df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
304df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
305df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
306df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
307bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
308bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
309bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
310bf984051SGleb Smirnoff  *
3113de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
3123de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
313df8bae1dSRodney W. Grimes  */
314df8bae1dSRodney W. Grimes int
315f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
316f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
317df8bae1dSRodney W. Grimes {
318cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
3191e78ac21SJeffrey Hsu 	struct ip *ip;
320fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
321ac9d7e26SMax Laier 	struct mbuf *m0;
32223bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
3233ae2ad08SJohn-Mark Gurney 	int mtu;
324ca8b83b0SLuigi Rizzo 	int error = 0;
3256ca363ebSGleb Smirnoff 	struct sockaddr_in *dst, sin;
3264c7a6059SGleb Smirnoff 	const struct sockaddr_in *gw;
3273c73180fSGleb Smirnoff 	struct in_ifaddr *ia;
3286ca363ebSGleb Smirnoff 	struct in_addr src;
32921d172a3SGleb Smirnoff 	int isbroadcast;
330078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
3319c57a5b6SHiroki Sato 	uint32_t fibnum;
332fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
3331a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
3341a499816SEdward Tomasz Napierala #endif
33577a01441SJohn Baldwin 
336ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
337b9555453SGleb Smirnoff 	NET_EPOCH_ASSERT();
33836e8826fSMax Laier 
339bc97ba51SJulian Elischer 	if (inp != NULL) {
340baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
34162e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
342c2529042SHans Petter Selasky 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
34380cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
3449c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
34580cb9f21SKip Macy 		}
34650575ce1SAndrew Gallatin #ifdef NUMA
34750575ce1SAndrew Gallatin 		m->m_pkthdr.numa_domain = inp->inp_numa_domain;
34850575ce1SAndrew Gallatin #endif
349bc97ba51SJulian Elischer 	}
35062e1ba83SRobert Watson 
351df8bae1dSRodney W. Grimes 	if (opt) {
352ca8b83b0SLuigi Rizzo 		int len = 0;
353df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
354cb7641e8SMaxim Konovalov 		if (len != 0)
355ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
356df8bae1dSRodney W. Grimes 	}
357df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
3588f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
3598f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
3603efc3014SJulian Elischer 
361df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
36253be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
36353be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
3646d947416SGleb Smirnoff 		ip_fillid(ip);
365df8bae1dSRodney W. Grimes 	} else {
366ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
36753be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
368df8bae1dSRodney W. Grimes 	}
3693924dfa7SMichael Tuexen 	if ((flags & IP_FORWARDING) == 0)
3703924dfa7SMichael Tuexen 		IPSTAT_INC(ips_localout);
3719c9137eaSGarrett Wollman 
372054692a4SAlexander V. Chernikov 	/*
373054692a4SAlexander V. Chernikov 	 * dst/gw handling:
374054692a4SAlexander V. Chernikov 	 *
3753c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
3763c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
377054692a4SAlexander V. Chernikov 	 */
3789c57a5b6SHiroki Sato 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
3796ca363ebSGleb Smirnoff 	if (ro != NULL)
3806ca363ebSGleb Smirnoff 		dst = (struct sockaddr_in *)&ro->ro_dst;
3816ca363ebSGleb Smirnoff 	else
3826ca363ebSGleb Smirnoff 		dst = &sin;
383983066f0SAlexander V. Chernikov 	if (ro == NULL || ro->ro_nh == NULL) {
384a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
385df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
386df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
3879b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
388df8bae1dSRodney W. Grimes 	}
3896ca363ebSGleb Smirnoff 	gw = dst;
390d9f2a782SErmal Luçi again:
39184cc0778SGeorge V. Neville-Neil 	/*
39284cc0778SGeorge V. Neville-Neil 	 * Validate route against routing table additions;
39384cc0778SGeorge V. Neville-Neil 	 * a better/more specific route might have been added.
39484cc0778SGeorge V. Neville-Neil 	 */
395983066f0SAlexander V. Chernikov 	if (inp != NULL && ro != NULL && ro->ro_nh != NULL)
396983066f0SAlexander V. Chernikov 		NH_VALIDATE(ro, &inp->inp_rt_cookie, fibnum);
39784cc0778SGeorge V. Neville-Neil 	/*
39884cc0778SGeorge V. Neville-Neil 	 * If there is a cached route,
39984cc0778SGeorge V. Neville-Neil 	 * check that it is to the same destination
40084cc0778SGeorge V. Neville-Neil 	 * and is still up.  If not, free it and try again.
40184cc0778SGeorge V. Neville-Neil 	 * The address family should also be checked in case of sharing the
40284cc0778SGeorge V. Neville-Neil 	 * cache with IPv6.
40384cc0778SGeorge V. Neville-Neil 	 * Also check whether routing cache needs invalidation.
40484cc0778SGeorge V. Neville-Neil 	 */
405983066f0SAlexander V. Chernikov 	if (ro != NULL && ro->ro_nh != NULL &&
406983066f0SAlexander V. Chernikov 	    ((!NH_IS_VALID(ro->ro_nh)) || !RT_LINK_IS_UP(ro->ro_nh->nh_ifp) ||
40784cc0778SGeorge V. Neville-Neil 	    dst->sin_family != AF_INET ||
4086ca363ebSGleb Smirnoff 	    dst->sin_addr.s_addr != ip->ip_dst.s_addr))
409fc21c53fSRyan Stone 		RO_INVALIDATE_CACHE(ro);
410d9f2a782SErmal Luçi 	ia = NULL;
411df8bae1dSRodney W. Grimes 	/*
412a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
413a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
414a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
415a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
416df8bae1dSRodney W. Grimes 	 */
417a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
4184f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
41958a39d8cSAlan Somers 						      M_GETFIB(m)))) == NULL &&
4204f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
42158a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL) {
42286425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
423a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
424a3fd02d8SBruce M Simpson 			goto bad;
425a3fd02d8SBruce M Simpson 		}
426a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
427a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
428a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
4296ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
430a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
431a3fd02d8SBruce M Simpson 		isbroadcast = 1;
4326ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
433a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
4344f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
43558a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL &&
4364f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
43758a39d8cSAlan Somers 						M_GETFIB(m)))) == NULL) {
43886425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
439df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
440df8bae1dSRodney W. Grimes 			goto bad;
441df8bae1dSRodney W. Grimes 		}
442df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
4436ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
444df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
4456c1bd558SRyan Stone 		isbroadcast = ifp->if_flags & IFF_BROADCAST ?
4466c1bd558SRyan Stone 		    in_ifaddr_broadcast(dst->sin_addr, ia) : 0;
4476ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
4483afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
44938c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
4503afefa39SDaniel C. Sobral 		/*
45138c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
45238c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
4533afefa39SDaniel C. Sobral 		 */
45438c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
4556ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
456cc0a3c8cSAndrey V. Elsukov 		IFP_TO_IA(ifp, ia, &in_ifa_tracker);
45738c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
45854bb7ac0SGleb Smirnoff 		/* Interface may have no addresses. */
45954bb7ac0SGleb Smirnoff 		if (ia != NULL)
4606ca363ebSGleb Smirnoff 			src = IA_SIN(ia)->sin_addr;
46154bb7ac0SGleb Smirnoff 		else
46254bb7ac0SGleb Smirnoff 			src.s_addr = INADDR_ANY;
4636ca363ebSGleb Smirnoff 	} else if (ro != NULL) {
464983066f0SAlexander V. Chernikov 		if (ro->ro_nh == NULL) {
4652c17fe93SGarrett Wollman 			/*
4666ca363ebSGleb Smirnoff 			 * We want to do any cloning requested by the link
4676ca363ebSGleb Smirnoff 			 * layer, as this is probably required in all cases
4686ca363ebSGleb Smirnoff 			 * for correct operation (as it is for ARP).
4692c17fe93SGarrett Wollman 			 */
4704043ee3cSAlexander V. Chernikov 			uint32_t flowid;
471e440aed9SQing Li #ifdef RADIX_MPATH
4724043ee3cSAlexander V. Chernikov 			flowid = ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr);
473e440aed9SQing Li #else
4744043ee3cSAlexander V. Chernikov 			flowid = m->m_pkthdr.flowid;
475e440aed9SQing Li #endif
4764043ee3cSAlexander V. Chernikov 			ro->ro_nh = fib4_lookup(fibnum, dst->sin_addr, 0,
4774043ee3cSAlexander V. Chernikov 			    NHR_REF, flowid);
4784043ee3cSAlexander V. Chernikov 
479983066f0SAlexander V. Chernikov 			if (ro->ro_nh == NULL || (!NH_IS_VALID(ro->ro_nh)) ||
480983066f0SAlexander V. Chernikov 			    !RT_LINK_IS_UP(ro->ro_nh->nh_ifp)) {
481fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
4821a499816SEdward Tomasz Napierala 				/*
4831a499816SEdward Tomasz Napierala 				 * There is no route for this packet, but it is
4841a499816SEdward Tomasz Napierala 				 * possible that a matching SPD entry exists.
4851a499816SEdward Tomasz Napierala 				 */
4861a499816SEdward Tomasz Napierala 				no_route_but_check_spd = 1;
4871a499816SEdward Tomasz Napierala 				mtu = 0; /* Silence GCC warning. */
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 		}
495983066f0SAlexander V. Chernikov 		ia = ifatoia(ro->ro_nh->nh_ifa);
496983066f0SAlexander V. Chernikov 		ifp = ro->ro_nh->nh_ifp;
497983066f0SAlexander V. Chernikov 		counter_u64_add(ro->ro_nh->nh_pksent, 1);
49836402a68SAlexander V. Chernikov 		rt_update_ro_flags(ro);
499983066f0SAlexander V. Chernikov 		if (ro->ro_nh->nh_flags & NHF_GATEWAY)
500983066f0SAlexander V. Chernikov 			gw = &ro->ro_nh->gw4_sa;
501983066f0SAlexander V. Chernikov 		if (ro->ro_nh->nh_flags & NHF_HOST)
502983066f0SAlexander V. Chernikov 			isbroadcast = (ro->ro_nh->nh_flags & NHF_BROADCAST);
5036c1bd558SRyan Stone 		else if (ifp->if_flags & IFF_BROADCAST)
50490cc51a1SRyan Stone 			isbroadcast = in_ifaddr_broadcast(gw->sin_addr, ia);
5056c1bd558SRyan Stone 		else
5066c1bd558SRyan Stone 			isbroadcast = 0;
507983066f0SAlexander V. Chernikov 		if (ro->ro_nh->nh_flags & NHF_HOST)
508983066f0SAlexander V. Chernikov 			mtu = ro->ro_nh->nh_mtu;
5097f948f12SAlexander V. Chernikov 		else
5103ae2ad08SJohn-Mark Gurney 			mtu = ifp->if_mtu;
5116ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
5126ca363ebSGleb Smirnoff 	} else {
5136ca363ebSGleb Smirnoff 		struct nhop4_extended nh;
5146ca363ebSGleb Smirnoff 
5156ca363ebSGleb Smirnoff 		bzero(&nh, sizeof(nh));
5166ca363ebSGleb Smirnoff 		if (fib4_lookup_nh_ext(M_GETFIB(m), ip->ip_dst, 0, 0, &nh) !=
5176ca363ebSGleb Smirnoff 		    0) {
5186ca363ebSGleb Smirnoff #if defined(IPSEC) || defined(IPSEC_SUPPORT)
5196ca363ebSGleb Smirnoff 			/*
5206ca363ebSGleb Smirnoff 			 * There is no route for this packet, but it is
5216ca363ebSGleb Smirnoff 			 * possible that a matching SPD entry exists.
5226ca363ebSGleb Smirnoff 			 */
5236ca363ebSGleb Smirnoff 			no_route_but_check_spd = 1;
5246ca363ebSGleb Smirnoff 			mtu = 0; /* Silence GCC warning. */
5256ca363ebSGleb Smirnoff 			goto sendit;
5266ca363ebSGleb Smirnoff #endif
5276ca363ebSGleb Smirnoff 			IPSTAT_INC(ips_noroute);
5286ca363ebSGleb Smirnoff 			error = EHOSTUNREACH;
5296ca363ebSGleb Smirnoff 			goto bad;
5306ca363ebSGleb Smirnoff 		}
5316ca363ebSGleb Smirnoff 		ifp = nh.nh_ifp;
5326ca363ebSGleb Smirnoff 		mtu = nh.nh_mtu;
5336ca363ebSGleb Smirnoff 		/*
5346ca363ebSGleb Smirnoff 		 * We are rewriting here dst to be gw actually, contradicting
5356ca363ebSGleb Smirnoff 		 * comment at the beginning of the function. However, in this
5366ca363ebSGleb Smirnoff 		 * case we are always dealing with on stack dst.
5376ca363ebSGleb Smirnoff 		 * In case if pfil(9) sends us back to beginning of the
5386ca363ebSGleb Smirnoff 		 * function, the dst would be rewritten by ip_output_pfil().
5396ca363ebSGleb Smirnoff 		 */
5406ca363ebSGleb Smirnoff 		MPASS(dst == &sin);
5416ca363ebSGleb Smirnoff 		dst->sin_addr = nh.nh_addr;
5426ca363ebSGleb Smirnoff 		ia = nh.nh_ia;
5436ca363ebSGleb Smirnoff 		src = nh.nh_src;
5446ca363ebSGleb Smirnoff 		isbroadcast = (((nh.nh_flags & (NHF_HOST | NHF_BROADCAST)) ==
5456ca363ebSGleb Smirnoff 		    (NHF_HOST | NHF_BROADCAST)) ||
5466ca363ebSGleb Smirnoff 		    ((ifp->if_flags & IFF_BROADCAST) &&
5476ca363ebSGleb Smirnoff 		    in_ifaddr_broadcast(dst->sin_addr, ia)));
5486ca363ebSGleb Smirnoff 	}
5496ca363ebSGleb Smirnoff 
550c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
551983066f0SAlexander V. Chernikov 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, ro=%p (nh_flags=0x%08x) ifp=%p",
5526ca363ebSGleb Smirnoff 	    __func__, mtu, ro,
553983066f0SAlexander V. Chernikov 	    (ro != NULL && ro->ro_nh != NULL) ? ro->ro_nh->nh_flags : 0, ifp));
554d9f2a782SErmal Luçi 
5559b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
556df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
557df8bae1dSRodney W. Grimes 		/*
558183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
559183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
560183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
561183e1c86SGleb Smirnoff 		 */
562183e1c86SGleb Smirnoff 		gw = dst;
563183e1c86SGleb Smirnoff 		/*
564df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
565df8bae1dSRodney W. Grimes 		 */
566df8bae1dSRodney W. Grimes 		if (imo != NULL) {
567df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
5681c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
5691c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
570bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
571bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
572bbb4330bSLuigi Rizzo 				    INADDR_ANY;
573df8bae1dSRodney W. Grimes 		} else
574df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
575df8bae1dSRodney W. Grimes 		/*
576df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
577df8bae1dSRodney W. Grimes 		 */
5781c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
579df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
58086425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
581df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
582df8bae1dSRodney W. Grimes 				goto bad;
583df8bae1dSRodney W. Grimes 			}
5841c5de19aSGarrett Wollman 		}
585df8bae1dSRodney W. Grimes 		/*
586df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
587df8bae1dSRodney W. Grimes 		 * of outgoing interface.
588df8bae1dSRodney W. Grimes 		 */
5896ca363ebSGleb Smirnoff 		if (ip->ip_src.s_addr == INADDR_ANY)
5906ca363ebSGleb Smirnoff 			ip->ip_src = src;
591df8bae1dSRodney W. Grimes 
5928b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
5938b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
594df8bae1dSRodney W. Grimes 			/*
5958b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
5968b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
5978b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
5988b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
5998b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
600df8bae1dSRodney W. Grimes 			 */
601331dff07SAlexander V. Chernikov 			ip_mloopback(ifp, m, hlen);
6028b889dbbSBruce M Simpson 		} else {
603df8bae1dSRodney W. Grimes 			/*
604df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
605df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
606df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
607df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
608df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
609df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
610df8bae1dSRodney W. Grimes 			 *
611df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
612df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
613df8bae1dSRodney W. Grimes 			 * if necessary.
614df8bae1dSRodney W. Grimes 			 */
615603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
616f0068c4aSGarrett Wollman 				/*
617bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
618f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
619f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
620f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
621f0068c4aSGarrett Wollman 				 */
622603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
623f0068c4aSGarrett Wollman 					imo = NULL;
624bbb4330bSLuigi Rizzo 				if (ip_mforward &&
625bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
626df8bae1dSRodney W. Grimes 					m_freem(m);
627df8bae1dSRodney W. Grimes 					goto done;
628df8bae1dSRodney W. Grimes 				}
629df8bae1dSRodney W. Grimes 			}
630df8bae1dSRodney W. Grimes 		}
6315e9ae478SGarrett Wollman 
632df8bae1dSRodney W. Grimes 		/*
633df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
634df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
635df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
636df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
6378b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
6388b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
6398b889dbbSBruce M Simpson 		 * the loopback interface.
640df8bae1dSRodney W. Grimes 		 */
641f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
642df8bae1dSRodney W. Grimes 			m_freem(m);
643df8bae1dSRodney W. Grimes 			goto done;
644df8bae1dSRodney W. Grimes 		}
645df8bae1dSRodney W. Grimes 
646df8bae1dSRodney W. Grimes 		goto sendit;
647df8bae1dSRodney W. Grimes 	}
6486a7c943cSAndre Oppermann 
649df8bae1dSRodney W. Grimes 	/*
6502b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
651cb459254SJohn-Mark Gurney 	 * of the outoing interface.
652df8bae1dSRodney W. Grimes 	 */
6536ca363ebSGleb Smirnoff 	if (ip->ip_src.s_addr == INADDR_ANY)
6546ca363ebSGleb Smirnoff 		ip->ip_src = src;
6556a7c943cSAndre Oppermann 
656ca7a789aSMax Laier 	/*
657df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
6588c3f5566SRuslan Ermilov 	 * verify user is allowed to send
659df8bae1dSRodney W. Grimes 	 * such a packet.
660df8bae1dSRodney W. Grimes 	 */
6619f9b3dc4SGarrett Wollman 	if (isbroadcast) {
662df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
663df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
664df8bae1dSRodney W. Grimes 			goto bad;
665df8bae1dSRodney W. Grimes 		}
666df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
667df8bae1dSRodney W. Grimes 			error = EACCES;
668df8bae1dSRodney W. Grimes 			goto bad;
669df8bae1dSRodney W. Grimes 		}
670df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
6718f134647SGleb Smirnoff 		if (ip_len > mtu) {
672df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
673df8bae1dSRodney W. Grimes 			goto bad;
674df8bae1dSRodney W. Grimes 		}
675df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
6769f9b3dc4SGarrett Wollman 	} else {
677df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
6789f9b3dc4SGarrett Wollman 	}
679df8bae1dSRodney W. Grimes 
680f1743588SDarren Reed sendit:
681fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
682fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4)) {
683fcf59617SAndrey V. Elsukov 		if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) {
684fcf59617SAndrey V. Elsukov 			if (error == EINPROGRESS)
685fcf59617SAndrey V. Elsukov 				error = 0;
6861dfcf0d2SAndre Oppermann 			goto done;
687fcf59617SAndrey V. Elsukov 		}
68833841545SHajimu UMEMOTO 	}
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 	}
6971dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
69833841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
69933841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
700b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
70133841545SHajimu UMEMOTO 
702c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
703b252313fSGleb Smirnoff 	if (PFIL_HOOKED_OUT(V_inet_pfil_head)) {
70405fc9d78SKristof Provost 		switch (ip_output_pfil(&m, ifp, flags, inp, dst, &fibnum,
70505fc9d78SKristof Provost 		    &error)) {
706d9f2a782SErmal Luçi 		case 1: /* Finished */
707c4ac87eaSDarren Reed 			goto done;
7089b932e9eSAndre Oppermann 
709d9f2a782SErmal Luçi 		case 0: /* Continue normally */
710c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
711d9f2a782SErmal Luçi 			break;
712fed1c7e9SSøren Schmidt 
713d9f2a782SErmal Luçi 		case -1: /* Need to try again */
714d9f2a782SErmal Luçi 			/* Reset everything for a new round */
7156ca363ebSGleb Smirnoff 			if (ro != NULL) {
716983066f0SAlexander V. Chernikov 				RO_NHFREE(ro);
7174fb3a820SAlexander V. Chernikov 				ro->ro_prepend = NULL;
7186ca363ebSGleb Smirnoff 			}
719d9f2a782SErmal Luçi 			gw = dst;
720d9f2a782SErmal Luçi 			ip = mtod(m, struct ip *);
7219b932e9eSAndre Oppermann 			goto again;
722d9f2a782SErmal Luçi 
723d9f2a782SErmal Luçi 		}
724099dd043SAndre Oppermann 	}
7252b25acc1SLuigi Rizzo 
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 
736206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
737078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
73882334850SJohn Baldwin 		m = mb_unmapped_to_ext(m);
73982334850SJohn Baldwin 		if (m == NULL) {
74082334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
74182334850SJohn Baldwin 			error = ENOBUFS;
74282334850SJohn Baldwin 			goto bad;
74382334850SJohn Baldwin 		}
744db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
745078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
74682334850SJohn Baldwin 	} else if ((ifp->if_capenable & IFCAP_NOMAP) == 0) {
74782334850SJohn Baldwin 		m = mb_unmapped_to_ext(m);
74882334850SJohn Baldwin 		if (m == NULL) {
74982334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
75082334850SJohn Baldwin 			error = ENOBUFS;
75182334850SJohn Baldwin 			goto bad;
75282334850SJohn Baldwin 		}
753db4f9cc7SJonathan Lemon 	}
7542f4afd21SRandall Stewart #ifdef SCTP
755078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
75682334850SJohn Baldwin 		m = mb_unmapped_to_ext(m);
75782334850SJohn Baldwin 		if (m == NULL) {
75882334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
75982334850SJohn Baldwin 			error = ENOBUFS;
76082334850SJohn Baldwin 			goto bad;
76182334850SJohn Baldwin 		}
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.
770df8bae1dSRodney W. Grimes 	 */
77121d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
772bf7dcda3SGleb Smirnoff 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
773df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
774078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
775df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
776078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
777078468edSGleb Smirnoff 		}
7785da9f8faSJosef Karthauser 
779233dcce1SAndre Oppermann 		/*
780233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
781233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
782233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
783233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
784233dcce1SAndre Oppermann 		 */
78538c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
786233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
7877caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
7887caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
789233dcce1SAndre Oppermann 			else
7907caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
7917caf4ab7SGleb Smirnoff 
7927caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
7935da9f8faSJosef Karthauser 		}
79453dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
7953390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
796eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
7979d9edc56SMike Silbersack #endif
798147f74d1SAndre Oppermann 		/*
799147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
800147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
801147f74d1SAndre Oppermann 		 */
80286bd0491SAndre Oppermann 		m_clrprotoflags(m);
80357f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
80435c7bb34SRandall Stewart 		error = ip_output_send(inp, ifp, m, gw, ro,
80535c7bb34SRandall Stewart 		    (flags & IP_NO_SND_TAG_RL) ? false : true);
806df8bae1dSRodney W. Grimes 		goto done;
807df8bae1dSRodney W. Grimes 	}
8081e78ac21SJeffrey Hsu 
809233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
81021d172a3SGleb Smirnoff 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
811df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
81286425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
813df8bae1dSRodney W. Grimes 		goto bad;
814df8bae1dSRodney W. Grimes 	}
8151e78ac21SJeffrey Hsu 
8161e78ac21SJeffrey Hsu 	/*
8171e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
8181e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
8191e78ac21SJeffrey Hsu 	 */
820078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
8211e78ac21SJeffrey Hsu 	if (error)
822df8bae1dSRodney W. Grimes 		goto bad;
8231e78ac21SJeffrey Hsu 	for (; m; m = m0) {
824df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
825b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
82607203494SDaniel C. Sobral 		if (error == 0) {
827fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
82807203494SDaniel C. Sobral 			if (ia != NULL) {
8297caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
8307caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
8317caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
83207203494SDaniel C. Sobral 			}
833147f74d1SAndre Oppermann 			/*
834147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
835147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
836147f74d1SAndre Oppermann 			 */
83786bd0491SAndre Oppermann 			m_clrprotoflags(m);
838fe937674SJosef Karthauser 
8392e77d270SAndrey V. Elsukov 			IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp,
8402e77d270SAndrey V. Elsukov 			    mtod(m, struct ip *), NULL);
84135c7bb34SRandall Stewart 			error = ip_output_send(inp, ifp, m, gw, ro, true);
842fe937674SJosef Karthauser 		} else
843df8bae1dSRodney W. Grimes 			m_freem(m);
844df8bae1dSRodney W. Grimes 	}
845df8bae1dSRodney W. Grimes 
846df8bae1dSRodney W. Grimes 	if (error == 0)
84786425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
8481e78ac21SJeffrey Hsu 
849df8bae1dSRodney W. Grimes done:
850df8bae1dSRodney W. Grimes 	return (error);
851df8bae1dSRodney W. Grimes  bad:
8523528d68fSBill Paul 	m_freem(m);
853df8bae1dSRodney W. Grimes 	goto done;
854df8bae1dSRodney W. Grimes }
855df8bae1dSRodney W. Grimes 
8561e78ac21SJeffrey Hsu /*
8571e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
8581e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
8591e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
8601e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
8611e78ac21SJeffrey Hsu  *
8621e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
8631e78ac21SJeffrey Hsu  */
8641e78ac21SJeffrey Hsu int
8651e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
866078468edSGleb Smirnoff     u_long if_hwassist_flags)
8671e78ac21SJeffrey Hsu {
8681e78ac21SJeffrey Hsu 	int error = 0;
8691e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
8701e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
8711e78ac21SJeffrey Hsu 	int off;
8721e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
8731e78ac21SJeffrey Hsu 	int firstlen;
8741e78ac21SJeffrey Hsu 	struct mbuf **mnext;
8751e78ac21SJeffrey Hsu 	int nfrags;
87621d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
8771e78ac21SJeffrey Hsu 
87821d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
87921d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
88021d172a3SGleb Smirnoff 
88121d172a3SGleb Smirnoff 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
88286425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
8831e78ac21SJeffrey Hsu 		return EMSGSIZE;
8841e78ac21SJeffrey Hsu 	}
8851e78ac21SJeffrey Hsu 
8861e78ac21SJeffrey Hsu 	/*
8871e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
8881e78ac21SJeffrey Hsu 	 */
8891e78ac21SJeffrey Hsu 	if (len < 8)
8901e78ac21SJeffrey Hsu 		return EMSGSIZE;
8911e78ac21SJeffrey Hsu 
8921e78ac21SJeffrey Hsu 	/*
8931e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
8941e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
8951e78ac21SJeffrey Hsu 	 */
896da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
89782334850SJohn Baldwin 		m0 = mb_unmapped_to_ext(m0);
89882334850SJohn Baldwin 		if (m0 == NULL) {
89982334850SJohn Baldwin 			error = ENOBUFS;
90082334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
90182334850SJohn Baldwin 			goto done;
90282334850SJohn Baldwin 		}
9031e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
9041e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
9051e78ac21SJeffrey Hsu 	}
9062f4afd21SRandall Stewart #ifdef SCTP
907da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
90882334850SJohn Baldwin 		m0 = mb_unmapped_to_ext(m0);
90982334850SJohn Baldwin 		if (m0 == NULL) {
91082334850SJohn Baldwin 			error = ENOBUFS;
91182334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
91282334850SJohn Baldwin 			goto done;
91382334850SJohn Baldwin 		}
9141966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
9152f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
9162f4afd21SRandall Stewart 	}
9172f4afd21SRandall Stewart #endif
9181e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
9191e78ac21SJeffrey Hsu 		/*
9201e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
9211e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
9221e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
9231e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
9241e78ac21SJeffrey Hsu 		 *
9251e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
9261e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
9271e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
9281e78ac21SJeffrey Hsu 		 */
9291e78ac21SJeffrey Hsu 		int newlen;
9301e78ac21SJeffrey Hsu 
9319c0f6aa7SHans Petter Selasky 		off = MIN(mtu, m0->m_pkthdr.len);
9321e78ac21SJeffrey Hsu 
9331e78ac21SJeffrey Hsu 		/*
9341e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
9351e78ac21SJeffrey Hsu 		 * 8-byte boundary
9361e78ac21SJeffrey Hsu 		 */
9371e78ac21SJeffrey Hsu 		if (off < hlen)
9381e78ac21SJeffrey Hsu 			goto smart_frag_failure;
9391e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
9401e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
9411e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
9421e78ac21SJeffrey Hsu 			/* we failed, go back the default */
9431e78ac21SJeffrey Hsu smart_frag_failure:
9441e78ac21SJeffrey Hsu 			newlen = len;
9451e78ac21SJeffrey Hsu 			off = hlen + len;
9461e78ac21SJeffrey Hsu 		}
9471e78ac21SJeffrey Hsu 		len = newlen;
9481e78ac21SJeffrey Hsu 
9491e78ac21SJeffrey Hsu 	} else {
9501e78ac21SJeffrey Hsu 		off = hlen + len;
9511e78ac21SJeffrey Hsu 	}
9521e78ac21SJeffrey Hsu 
9531e78ac21SJeffrey Hsu 	firstlen = off - hlen;
9541e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
9551e78ac21SJeffrey Hsu 
9561e78ac21SJeffrey Hsu 	/*
9571e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
9581e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
9591e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
9601e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
9611e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
9621e78ac21SJeffrey Hsu 	 */
96321d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
9641e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
9651e78ac21SJeffrey Hsu 		struct mbuf *m;
9661e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
9671e78ac21SJeffrey Hsu 
968dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
9690b17fba7SAndre Oppermann 		if (m == NULL) {
9701e78ac21SJeffrey Hsu 			error = ENOBUFS;
97186425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
9721e78ac21SJeffrey Hsu 			goto done;
9731e78ac21SJeffrey Hsu 		}
974c4c4346fSHans Petter Selasky 		/*
975c4c4346fSHans Petter Selasky 		 * Make sure the complete packet header gets copied
976c4c4346fSHans Petter Selasky 		 * from the originating mbuf to the newly created
977c4c4346fSHans Petter Selasky 		 * mbuf. This also ensures that existing firewall
978c4c4346fSHans Petter Selasky 		 * classification(s), VLAN tags and so on get copied
979c4c4346fSHans Petter Selasky 		 * to the resulting fragmented packet(s):
980c4c4346fSHans Petter Selasky 		 */
981c4c4346fSHans Petter Selasky 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
982c4c4346fSHans Petter Selasky 			m_free(m);
983c4c4346fSHans Petter Selasky 			error = ENOBUFS;
984c4c4346fSHans Petter Selasky 			IPSTAT_INC(ips_odropped);
985c4c4346fSHans Petter Selasky 			goto done;
986c4c4346fSHans Petter Selasky 		}
9871e78ac21SJeffrey Hsu 		/*
9881e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
9891e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
9908b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
9911e78ac21SJeffrey Hsu 		 */
9921e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
9931e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
9941e78ac21SJeffrey Hsu 		*mhip = *ip;
9951e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
9961e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9971e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
9981e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
9991e78ac21SJeffrey Hsu 		}
10001e78ac21SJeffrey Hsu 		m->m_len = mhlen;
100121d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
100221d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
1003fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
100421d172a3SGleb Smirnoff 			len = ip_len - off;
1005fb86dfcdSAndre Oppermann 		else
10061e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
10071e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
1008eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
10090b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
10101e78ac21SJeffrey Hsu 			m_free(m);
10111e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
101286425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
10131e78ac21SJeffrey Hsu 			goto done;
10141e78ac21SJeffrey Hsu 		}
10151e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
10161e78ac21SJeffrey Hsu #ifdef MAC
101730d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
10181e78ac21SJeffrey Hsu #endif
10191e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
10201e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
1021078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
10221e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
1023078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
1024078468edSGleb Smirnoff 		}
10251e78ac21SJeffrey Hsu 		*mnext = m;
10261e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
10271e78ac21SJeffrey Hsu 	}
102886425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
10291e78ac21SJeffrey Hsu 
10301e78ac21SJeffrey Hsu 	/*
10311e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
10321e78ac21SJeffrey Hsu 	 * and updating header.
10331e78ac21SJeffrey Hsu 	 */
103421d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
10351e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
10361e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
103721d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
10381e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
1039078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
10401e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
1041078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
1042078468edSGleb Smirnoff 	}
10431e78ac21SJeffrey Hsu 
10441e78ac21SJeffrey Hsu done:
10451e78ac21SJeffrey Hsu 	*m_frag = m0;
10461e78ac21SJeffrey Hsu 	return error;
10471e78ac21SJeffrey Hsu }
10481e78ac21SJeffrey Hsu 
10491c238475SJonathan Lemon void
1050db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1051db4f9cc7SJonathan Lemon {
1052db4f9cc7SJonathan Lemon 	struct ip *ip;
10531fdbfb90STom Jones 	struct udphdr *uh;
10541fdbfb90STom Jones 	uint16_t cklen, csum, offset;
1055db4f9cc7SJonathan Lemon 
1056db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
105753be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
10581fdbfb90STom Jones 
10591fdbfb90STom Jones 	if (m->m_pkthdr.csum_flags & CSUM_UDP) {
10601fdbfb90STom Jones 		/* if udp header is not in the first mbuf copy udplen */
1061b6e87011STom Jones 		if (offset + sizeof(struct udphdr) > m->m_len) {
1062590d0a43SAndrey V. Elsukov 			m_copydata(m, offset + offsetof(struct udphdr,
1063590d0a43SAndrey V. Elsukov 			    uh_ulen), sizeof(cklen), (caddr_t)&cklen);
1064b6e87011STom Jones 			cklen = ntohs(cklen);
1065b6e87011STom Jones 		} else {
1066590d0a43SAndrey V. Elsukov 			uh = (struct udphdr *)mtodo(m, offset);
10671fdbfb90STom Jones 			cklen = ntohs(uh->uh_ulen);
10681fdbfb90STom Jones 		}
10691fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen + offset, offset);
10701fdbfb90STom Jones 		if (csum == 0)
1071206a3274SRuslan Ermilov 			csum = 0xffff;
10721fdbfb90STom Jones 	} else {
10731fdbfb90STom Jones 		cklen = ntohs(ip->ip_len);
10741fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen, offset);
10751fdbfb90STom Jones 	}
1076db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1077db4f9cc7SJonathan Lemon 
1078590d0a43SAndrey V. Elsukov 	if (offset + sizeof(csum) > m->m_len)
1079590d0a43SAndrey V. Elsukov 		m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
1080590d0a43SAndrey V. Elsukov 	else
1081590d0a43SAndrey V. Elsukov 		*(u_short *)mtodo(m, offset) = csum;
1082db4f9cc7SJonathan Lemon }
1083db4f9cc7SJonathan Lemon 
1084df8bae1dSRodney W. Grimes /*
1085df8bae1dSRodney W. Grimes  * IP socket option processing.
1086df8bae1dSRodney W. Grimes  */
1087df8bae1dSRodney W. Grimes int
1088f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
1089df8bae1dSRodney W. Grimes {
1090cfe8b629SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
1091cfe8b629SGarrett Wollman 	int	error, optval;
1092dc847eb6SAdrian Chadd #ifdef	RSS
1093dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
1094dc847eb6SAdrian Chadd 	int retval;
1095dc847eb6SAdrian Chadd #endif
1096df8bae1dSRodney W. Grimes 
1097cfe8b629SGarrett Wollman 	error = optval = 0;
1098cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1099fc06cd42SMikolaj Golub 		error = EINVAL;
1100fc06cd42SMikolaj Golub 
1101fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
1102fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
1103fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
1104fc06cd42SMikolaj Golub 			case SO_REUSEADDR:
1105fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1106efdf104bSMikolaj Golub 				if ((so->so_options & SO_REUSEADDR) != 0)
1107efdf104bSMikolaj Golub 					inp->inp_flags2 |= INP_REUSEADDR;
1108fc06cd42SMikolaj Golub 				else
1109efdf104bSMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEADDR;
1110fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1111fc06cd42SMikolaj Golub 				error = 0;
1112fc06cd42SMikolaj Golub 				break;
1113fc06cd42SMikolaj Golub 			case SO_REUSEPORT:
1114fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1115fc06cd42SMikolaj Golub 				if ((so->so_options & SO_REUSEPORT) != 0)
1116fc06cd42SMikolaj Golub 					inp->inp_flags2 |= INP_REUSEPORT;
1117fc06cd42SMikolaj Golub 				else
1118fc06cd42SMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEPORT;
1119fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1120fc06cd42SMikolaj Golub 				error = 0;
1121fc06cd42SMikolaj Golub 				break;
11221a43cff9SSean Bruno 			case SO_REUSEPORT_LB:
11231a43cff9SSean Bruno 				INP_WLOCK(inp);
11241a43cff9SSean Bruno 				if ((so->so_options & SO_REUSEPORT_LB) != 0)
11251a43cff9SSean Bruno 					inp->inp_flags2 |= INP_REUSEPORT_LB;
11261a43cff9SSean Bruno 				else
11271a43cff9SSean Bruno 					inp->inp_flags2 &= ~INP_REUSEPORT_LB;
11281a43cff9SSean Bruno 				INP_WUNLOCK(inp);
11291a43cff9SSean Bruno 				error = 0;
11301a43cff9SSean Bruno 				break;
1131fc06cd42SMikolaj Golub 			case SO_SETFIB:
1132fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1133fc06cd42SMikolaj Golub 				inp->inp_inc.inc_fibnum = so->so_fibnum;
1134fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1135fc06cd42SMikolaj Golub 				error = 0;
1136fc06cd42SMikolaj Golub 				break;
1137f3e7afe2SHans Petter Selasky 			case SO_MAX_PACING_RATE:
1138f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1139f3e7afe2SHans Petter Selasky 				INP_WLOCK(inp);
1140f3e7afe2SHans Petter Selasky 				inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1141f3e7afe2SHans Petter Selasky 				INP_WUNLOCK(inp);
1142f3e7afe2SHans Petter Selasky 				error = 0;
1143f3e7afe2SHans Petter Selasky #else
1144f3e7afe2SHans Petter Selasky 				error = EOPNOTSUPP;
1145f3e7afe2SHans Petter Selasky #endif
1146f3e7afe2SHans Petter Selasky 				break;
1147fc06cd42SMikolaj Golub 			default:
1148fc06cd42SMikolaj Golub 				break;
1149fc06cd42SMikolaj Golub 			}
1150fc06cd42SMikolaj Golub 		}
1151fc06cd42SMikolaj Golub 		return (error);
1152cfe8b629SGarrett Wollman 	}
1153df8bae1dSRodney W. Grimes 
1154cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1155cfe8b629SGarrett Wollman 	case SOPT_SET:
1156cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1157df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1158df8bae1dSRodney W. Grimes #ifdef notyet
1159df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1160df8bae1dSRodney W. Grimes #endif
1161cfe8b629SGarrett Wollman 		{
1162cfe8b629SGarrett Wollman 			struct mbuf *m;
1163cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1164cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1165cfe8b629SGarrett Wollman 				break;
1166cfe8b629SGarrett Wollman 			}
1167dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
11680b17fba7SAndre Oppermann 			if (m == NULL) {
1169cfe8b629SGarrett Wollman 				error = ENOBUFS;
1170cfe8b629SGarrett Wollman 				break;
1171cfe8b629SGarrett Wollman 			}
1172cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1173cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1174cfe8b629SGarrett Wollman 					    m->m_len);
1175635354c4SMaxim Konovalov 			if (error) {
1176635354c4SMaxim Konovalov 				m_free(m);
1177635354c4SMaxim Konovalov 				break;
1178635354c4SMaxim Konovalov 			}
11798501a69cSRobert Watson 			INP_WLOCK(inp);
1180993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
11818501a69cSRobert Watson 			INP_WUNLOCK(inp);
1182993d9505SRobert Watson 			return (error);
1183cfe8b629SGarrett Wollman 		}
1184df8bae1dSRodney W. Grimes 
1185f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
1186f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
1187f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
1188f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
1189f44270e7SPawel Jakub Dawidek 				if (error)
1190be9347e3SAdrian Chadd 					break;
1191be9347e3SAdrian Chadd 			}
1192cef27294SAdrian Chadd 			/* FALLTHROUGH */
11930a100a6fSAdrian Chadd 		case IP_BINDMULTI:
11940a100a6fSAdrian Chadd #ifdef	RSS
11950a100a6fSAdrian Chadd 		case IP_RSS_LISTEN_BUCKET:
11960a100a6fSAdrian Chadd #endif
1197df8bae1dSRodney W. Grimes 		case IP_TOS:
1198df8bae1dSRodney W. Grimes 		case IP_TTL:
1199936cd18dSAndre Oppermann 		case IP_MINTTL:
1200df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1201df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1202dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1203df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
12044957466bSMatthew N. Dodd 		case IP_RECVTTL:
120582c23ebaSBill Fenner 		case IP_RECVIF:
12068afa2304SBruce M Simpson 		case IP_ONESBCAST:
1207b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
12083cca425bSMichael Tuexen 		case IP_RECVTOS:
12099d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
12109d3ddf43SAdrian Chadd #ifdef	RSS
12119d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
12129d3ddf43SAdrian Chadd #endif
1213cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1214cfe8b629SGarrett Wollman 					    sizeof optval);
1215cfe8b629SGarrett Wollman 			if (error)
1216cfe8b629SGarrett Wollman 				break;
1217df8bae1dSRodney W. Grimes 
1218cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1219df8bae1dSRodney W. Grimes 			case IP_TOS:
1220ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1221df8bae1dSRodney W. Grimes 				break;
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 			case IP_TTL:
1224ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1225df8bae1dSRodney W. Grimes 				break;
1226936cd18dSAndre Oppermann 
1227936cd18dSAndre Oppermann 			case IP_MINTTL:
1228a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1229936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1230936cd18dSAndre Oppermann 				else
1231936cd18dSAndre Oppermann 					error = EINVAL;
1232936cd18dSAndre Oppermann 				break;
1233936cd18dSAndre Oppermann 
1234a138d217SRobert Watson #define	OPTSET(bit) do {						\
12358501a69cSRobert Watson 	INP_WLOCK(inp);							\
1236df8bae1dSRodney W. Grimes 	if (optval)							\
1237df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1238df8bae1dSRodney W. Grimes 	else								\
1239a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
12408501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1241a138d217SRobert Watson } while (0)
1242df8bae1dSRodney W. Grimes 
12430a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
12440a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
12450a100a6fSAdrian Chadd 	if (val)							\
12460a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
12470a100a6fSAdrian Chadd 	else								\
12480a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
12490a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
12500a100a6fSAdrian Chadd } while (0)
12510a100a6fSAdrian Chadd 
1252df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1253df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1254df8bae1dSRodney W. Grimes 				break;
1255df8bae1dSRodney W. Grimes 
1256df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1257df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1258df8bae1dSRodney W. Grimes 				break;
1259df8bae1dSRodney W. Grimes 
1260df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1261df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1262df8bae1dSRodney W. Grimes 				break;
126382c23ebaSBill Fenner 
1264dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1265dce33a45SErmal Luçi 				OPTSET2(INP_ORIGDSTADDR, optval);
1266dce33a45SErmal Luçi 				break;
1267dce33a45SErmal Luçi 
12684957466bSMatthew N. Dodd 			case IP_RECVTTL:
12694957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
12704957466bSMatthew N. Dodd 				break;
12714957466bSMatthew N. Dodd 
127282c23ebaSBill Fenner 			case IP_RECVIF:
127382c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
127482c23ebaSBill Fenner 				break;
12756a800098SYoshinobu Inoue 
12768afa2304SBruce M Simpson 			case IP_ONESBCAST:
12778afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
12788afa2304SBruce M Simpson 				break;
1279b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1280b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1281b2828ad2SAndre Oppermann 				break;
1282f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1283f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1284be9347e3SAdrian Chadd 				break;
12853cca425bSMichael Tuexen 			case IP_RECVTOS:
12863cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
12873cca425bSMichael Tuexen 				break;
12880a100a6fSAdrian Chadd 			case IP_BINDMULTI:
12890a100a6fSAdrian Chadd 				OPTSET2(INP_BINDMULTI, optval);
12900a100a6fSAdrian Chadd 				break;
12919d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
12929d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
12939d3ddf43SAdrian Chadd 				break;
12940a100a6fSAdrian Chadd #ifdef	RSS
12950a100a6fSAdrian Chadd 			case IP_RSS_LISTEN_BUCKET:
12960a100a6fSAdrian Chadd 				if ((optval >= 0) &&
12970a100a6fSAdrian Chadd 				    (optval < rss_getnumbuckets())) {
12980a100a6fSAdrian Chadd 					inp->inp_rss_listen_bucket = optval;
12990a100a6fSAdrian Chadd 					OPTSET2(INP_RSS_BUCKET_SET, 1);
13000a100a6fSAdrian Chadd 				} else {
13010a100a6fSAdrian Chadd 					error = EINVAL;
13020a100a6fSAdrian Chadd 				}
13030a100a6fSAdrian Chadd 				break;
13049d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
13059d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
13069d3ddf43SAdrian Chadd 				break;
13070a100a6fSAdrian Chadd #endif
1308df8bae1dSRodney W. Grimes 			}
1309df8bae1dSRodney W. Grimes 			break;
1310df8bae1dSRodney W. Grimes #undef OPTSET
13110a100a6fSAdrian Chadd #undef OPTSET2
1312df8bae1dSRodney W. Grimes 
131371498f30SBruce M Simpson 		/*
131471498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
131571498f30SBruce M Simpson 		 * module.
131671498f30SBruce M Simpson 		 */
1317df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1318f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1319df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1320df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1321df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1322df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
132371498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
132471498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
132571498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
132671498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
132771498f30SBruce M Simpson 		case IP_MSFILTER:
132871498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
132971498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
133071498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
133171498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
133271498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
133371498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
133471498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1335df8bae1dSRodney W. Grimes 			break;
1336df8bae1dSRodney W. Grimes 
133733b3ac06SPeter Wemm 		case IP_PORTRANGE:
1338cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1339cfe8b629SGarrett Wollman 					    sizeof optval);
1340cfe8b629SGarrett Wollman 			if (error)
1341cfe8b629SGarrett Wollman 				break;
134233b3ac06SPeter Wemm 
13438501a69cSRobert Watson 			INP_WLOCK(inp);
134433b3ac06SPeter Wemm 			switch (optval) {
134533b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
134633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
134733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
134833b3ac06SPeter Wemm 				break;
134933b3ac06SPeter Wemm 
135033b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
135133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
135233b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
135333b3ac06SPeter Wemm 				break;
135433b3ac06SPeter Wemm 
135533b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
135633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
135733b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
135833b3ac06SPeter Wemm 				break;
135933b3ac06SPeter Wemm 
136033b3ac06SPeter Wemm 			default:
136133b3ac06SPeter Wemm 				error = EINVAL;
136233b3ac06SPeter Wemm 				break;
136333b3ac06SPeter Wemm 			}
13648501a69cSRobert Watson 			INP_WUNLOCK(inp);
1365ce8c72b1SPeter Wemm 			break;
136633b3ac06SPeter Wemm 
1367fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13686a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1369fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1370fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
13716a800098SYoshinobu Inoue 				break;
13726a800098SYoshinobu Inoue 			}
1373fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1374b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
13756a800098SYoshinobu Inoue 
1376df8bae1dSRodney W. Grimes 		default:
1377df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1378df8bae1dSRodney W. Grimes 			break;
1379df8bae1dSRodney W. Grimes 		}
1380df8bae1dSRodney W. Grimes 		break;
1381df8bae1dSRodney W. Grimes 
1382cfe8b629SGarrett Wollman 	case SOPT_GET:
1383cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1384df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1385df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1386e5e3e746SMatt Macy 			INP_RLOCK(inp);
1387e5e3e746SMatt Macy 			if (inp->inp_options) {
1388e5e3e746SMatt Macy 				struct mbuf *options;
1389e5e3e746SMatt Macy 
139010731c54SMichael Tuexen 				options = m_copym(inp->inp_options, 0,
139110731c54SMichael Tuexen 				    M_COPYALL, M_NOWAIT);
1392e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1393e5e3e746SMatt Macy 				if (options != NULL) {
1394cfe8b629SGarrett Wollman 					error = sooptcopyout(sopt,
1395e5e3e746SMatt Macy 							     mtod(options, char *),
1396e5e3e746SMatt Macy 							     options->m_len);
1397e5e3e746SMatt Macy 					m_freem(options);
1398e5e3e746SMatt Macy 				} else
1399e5e3e746SMatt Macy 					error = ENOMEM;
1400e5e3e746SMatt Macy 			} else {
1401e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1402cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1403e5e3e746SMatt Macy 			}
1404df8bae1dSRodney W. Grimes 			break;
1405df8bae1dSRodney W. Grimes 
1406df8bae1dSRodney W. Grimes 		case IP_TOS:
1407df8bae1dSRodney W. Grimes 		case IP_TTL:
1408936cd18dSAndre Oppermann 		case IP_MINTTL:
1409df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1410df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1411dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1412df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
14134957466bSMatthew N. Dodd 		case IP_RECVTTL:
141482c23ebaSBill Fenner 		case IP_RECVIF:
1415cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
14168afa2304SBruce M Simpson 		case IP_ONESBCAST:
1417b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
14185f6bf451SAttilio Rao 		case IP_BINDANY:
14193cca425bSMichael Tuexen 		case IP_RECVTOS:
14200a100a6fSAdrian Chadd 		case IP_BINDMULTI:
14219c423972SAdrian Chadd 		case IP_FLOWID:
14229c423972SAdrian Chadd 		case IP_FLOWTYPE:
14239d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
14240a100a6fSAdrian Chadd #ifdef	RSS
14250a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
14269d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
14270a100a6fSAdrian Chadd #endif
1428cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1429df8bae1dSRodney W. Grimes 
1430df8bae1dSRodney W. Grimes 			case IP_TOS:
1431ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1432df8bae1dSRodney W. Grimes 				break;
1433df8bae1dSRodney W. Grimes 
1434df8bae1dSRodney W. Grimes 			case IP_TTL:
1435ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1436df8bae1dSRodney W. Grimes 				break;
1437df8bae1dSRodney W. Grimes 
1438936cd18dSAndre Oppermann 			case IP_MINTTL:
1439936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1440936cd18dSAndre Oppermann 				break;
1441936cd18dSAndre Oppermann 
1442df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
14430a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1444df8bae1dSRodney W. Grimes 
1445df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1446df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1447df8bae1dSRodney W. Grimes 				break;
1448df8bae1dSRodney W. Grimes 
1449df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1450df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1451df8bae1dSRodney W. Grimes 				break;
1452df8bae1dSRodney W. Grimes 
1453df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1454df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1455df8bae1dSRodney W. Grimes 				break;
145682c23ebaSBill Fenner 
1457dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1458dce33a45SErmal Luçi 				optval = OPTBIT2(INP_ORIGDSTADDR);
1459dce33a45SErmal Luçi 				break;
1460dce33a45SErmal Luçi 
14614957466bSMatthew N. Dodd 			case IP_RECVTTL:
14624957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
14634957466bSMatthew N. Dodd 				break;
14644957466bSMatthew N. Dodd 
146582c23ebaSBill Fenner 			case IP_RECVIF:
146682c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
146782c23ebaSBill Fenner 				break;
1468cfe8b629SGarrett Wollman 
1469cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1470cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1471cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1472cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1473cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1474cfe8b629SGarrett Wollman 				else
1475cfe8b629SGarrett Wollman 					optval = 0;
1476cfe8b629SGarrett Wollman 				break;
14776a800098SYoshinobu Inoue 
14788afa2304SBruce M Simpson 			case IP_ONESBCAST:
14798afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
14808afa2304SBruce M Simpson 				break;
1481b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1482b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1483b2828ad2SAndre Oppermann 				break;
14845f6bf451SAttilio Rao 			case IP_BINDANY:
14855f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
14865f6bf451SAttilio Rao 				break;
14873cca425bSMichael Tuexen 			case IP_RECVTOS:
14883cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
14893cca425bSMichael Tuexen 				break;
14909c423972SAdrian Chadd 			case IP_FLOWID:
14919c423972SAdrian Chadd 				optval = inp->inp_flowid;
14929c423972SAdrian Chadd 				break;
14939c423972SAdrian Chadd 			case IP_FLOWTYPE:
14949c423972SAdrian Chadd 				optval = inp->inp_flowtype;
14959c423972SAdrian Chadd 				break;
14969d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
14979d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
14989d3ddf43SAdrian Chadd 				break;
14999c423972SAdrian Chadd #ifdef	RSS
15007847796aSAdrian Chadd 			case IP_RSSBUCKETID:
15017847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
15027847796aSAdrian Chadd 				    inp->inp_flowtype,
15037847796aSAdrian Chadd 				    &rss_bucket);
15047847796aSAdrian Chadd 				if (retval == 0)
15057847796aSAdrian Chadd 					optval = rss_bucket;
15067847796aSAdrian Chadd 				else
15077847796aSAdrian Chadd 					error = EINVAL;
15089c423972SAdrian Chadd 				break;
15099d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
15109d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
15119d3ddf43SAdrian Chadd 				break;
15129c423972SAdrian Chadd #endif
15130a100a6fSAdrian Chadd 			case IP_BINDMULTI:
15140a100a6fSAdrian Chadd 				optval = OPTBIT2(INP_BINDMULTI);
15150a100a6fSAdrian Chadd 				break;
1516df8bae1dSRodney W. Grimes 			}
1517cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1518df8bae1dSRodney W. Grimes 			break;
1519df8bae1dSRodney W. Grimes 
152071498f30SBruce M Simpson 		/*
152171498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
152271498f30SBruce M Simpson 		 * module.
152371498f30SBruce M Simpson 		 */
1524df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1525f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1526df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1527df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
152871498f30SBruce M Simpson 		case IP_MSFILTER:
152971498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
153033b3ac06SPeter Wemm 			break;
153133b3ac06SPeter Wemm 
1532fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
15336a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1534fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1535fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
15366a800098SYoshinobu Inoue 				break;
15376a800098SYoshinobu Inoue 			}
1538fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1539b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15406a800098SYoshinobu Inoue 
1541df8bae1dSRodney W. Grimes 		default:
1542df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1543df8bae1dSRodney W. Grimes 			break;
1544df8bae1dSRodney W. Grimes 		}
1545df8bae1dSRodney W. Grimes 		break;
1546df8bae1dSRodney W. Grimes 	}
1547df8bae1dSRodney W. Grimes 	return (error);
1548df8bae1dSRodney W. Grimes }
1549df8bae1dSRodney W. Grimes 
1550df8bae1dSRodney W. Grimes /*
1551df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1552df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1553df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1554f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1555f5fea3ddSPaul Traina  * replicating that code here.
1556df8bae1dSRodney W. Grimes  */
1557df8bae1dSRodney W. Grimes static void
1558331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen)
1559df8bae1dSRodney W. Grimes {
1560331dff07SAlexander V. Chernikov 	struct ip *ip;
1561df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1562df8bae1dSRodney W. Grimes 
1563e4762f75SGeorge V. Neville-Neil 	/*
1564e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1565e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1566e4762f75SGeorge V. Neville-Neil 	 */
1567eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
1568f0cace5dSRobert Watson 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
156986b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1570df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1571390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1572390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1573390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1574390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1575390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1576390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1577390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1578390cdc6aSRuslan Ermilov 		}
1579df8bae1dSRodney W. Grimes 		/*
1580df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1581df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1582df8bae1dSRodney W. Grimes 		 */
15838f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1584df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
158586b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1586331dff07SAlexander V. Chernikov 		if_simloop(ifp, copym, AF_INET, 0);
1587df8bae1dSRodney W. Grimes 	}
1588df8bae1dSRodney W. Grimes }
1589