xref: /freebsd/sys/netinet/ip_output.c (revision b2e60773c6b015f06fcd71510cd20a91eb43bcaa)
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"
39*b2e60773SJohn 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>
50*b2e60773SJohn 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>
70e440aed9SQing Li #ifdef RADIX_MPATH
71e440aed9SQing Li #include <net/radix_mpath.h>
72e440aed9SQing Li #endif
73b2bdc62aSAdrian Chadd #include <net/rss_config.h>
744b79449eSBjoern A. Zeeb #include <net/vnet.h>
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <netinet/in.h>
776ca363ebSGleb Smirnoff #include <netinet/in_fib.h>
7857f60867SMark Johnston #include <netinet/in_kdtrace.h>
79df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
80df8bae1dSRodney W. Grimes #include <netinet/ip.h>
81df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
829c423972SAdrian Chadd #include <netinet/in_rss.h>
83df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
84df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
85ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
861fdbfb90STom Jones 
871fdbfb90STom Jones #include <netinet/udp.h>
881fdbfb90STom Jones #include <netinet/udp_var.h>
891fdbfb90STom Jones 
902f4afd21SRandall Stewart #ifdef SCTP
912f4afd21SRandall Stewart #include <netinet/sctp.h>
922f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
932f4afd21SRandall Stewart #endif
94df8bae1dSRodney W. Grimes 
95fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
966a800098SYoshinobu Inoue 
971dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
981dfcf0d2SAndre Oppermann 
99aed55708SRobert Watson #include <security/mac/mac_framework.h>
100aed55708SRobert Watson 
10153dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
10205b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0;
1039d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
1049d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1059d9edc56SMike Silbersack #endif
1069d9edc56SMike Silbersack 
107331dff07SAlexander V. Chernikov static void	ip_mloopback(struct ifnet *, const struct mbuf *, int);
108afed1b49SDarren Reed 
109afed1b49SDarren Reed 
1108b889dbbSBruce M Simpson extern int in_mcast_loop;
11193e0e116SJulian Elischer extern	struct protosw inetsw[];
11293e0e116SJulian Elischer 
113d9f2a782SErmal Luçi static inline int
11405fc9d78SKristof Provost ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, int flags,
11505fc9d78SKristof Provost     struct inpcb *inp, struct sockaddr_in *dst, int *fibnum, int *error)
116d9f2a782SErmal Luçi {
117d9f2a782SErmal Luçi 	struct m_tag *fwd_tag = NULL;
1188f980c01SMark Johnston 	struct mbuf *m;
119d9f2a782SErmal Luçi 	struct in_addr odst;
120d9f2a782SErmal Luçi 	struct ip *ip;
12105fc9d78SKristof Provost 	int pflags = PFIL_OUT;
12205fc9d78SKristof Provost 
12305fc9d78SKristof Provost 	if (flags & IP_FORWARDING)
12405fc9d78SKristof Provost 		pflags |= PFIL_FWD;
125d9f2a782SErmal Luçi 
1268f980c01SMark Johnston 	m = *mp;
127d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
128d9f2a782SErmal Luçi 
129d9f2a782SErmal Luçi 	/* Run through list of hooks for output packets. */
130d9f2a782SErmal Luçi 	odst.s_addr = ip->ip_dst.s_addr;
13105fc9d78SKristof Provost 	switch (pfil_run_hooks(V_inet_pfil_head, mp, ifp, pflags, inp)) {
132b252313fSGleb Smirnoff 	case PFIL_DROPPED:
133b252313fSGleb Smirnoff 		*error = EPERM;
134b252313fSGleb Smirnoff 		/* FALLTHROUGH */
135b252313fSGleb Smirnoff 	case PFIL_CONSUMED:
136d9f2a782SErmal Luçi 		return 1; /* Finished */
137b252313fSGleb Smirnoff 	case PFIL_PASS:
138b252313fSGleb Smirnoff 		*error = 0;
139b252313fSGleb Smirnoff 	}
140b252313fSGleb Smirnoff 	m = *mp;
141d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
142d9f2a782SErmal Luçi 
143d9f2a782SErmal Luçi 	/* See if destination IP address was changed by packet filter. */
144d9f2a782SErmal Luçi 	if (odst.s_addr != ip->ip_dst.s_addr) {
145d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
146d9f2a782SErmal Luçi 		/* If destination is now ourself drop to ip_input(). */
147d9f2a782SErmal Luçi 		if (in_localip(ip->ip_dst)) {
148d9f2a782SErmal Luçi 			m->m_flags |= M_FASTFWD_OURS;
149d9f2a782SErmal Luçi 			if (m->m_pkthdr.rcvif == NULL)
150d9f2a782SErmal Luçi 				m->m_pkthdr.rcvif = V_loif;
151d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
152d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |=
153d9f2a782SErmal Luçi 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
154d9f2a782SErmal Luçi 				m->m_pkthdr.csum_data = 0xffff;
155d9f2a782SErmal Luçi 			}
156d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
157d9f2a782SErmal Luçi 				CSUM_IP_CHECKED | CSUM_IP_VALID;
158d9f2a782SErmal Luçi #ifdef SCTP
159d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
160d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
161d9f2a782SErmal Luçi #endif
162d9f2a782SErmal Luçi 			*error = netisr_queue(NETISR_IP, m);
163d9f2a782SErmal Luçi 			return 1; /* Finished */
164d9f2a782SErmal Luçi 		}
165d9f2a782SErmal Luçi 
166d9f2a782SErmal Luçi 		bzero(dst, sizeof(*dst));
167d9f2a782SErmal Luçi 		dst->sin_family = AF_INET;
168d9f2a782SErmal Luçi 		dst->sin_len = sizeof(*dst);
169d9f2a782SErmal Luçi 		dst->sin_addr = ip->ip_dst;
170d9f2a782SErmal Luçi 
171d9f2a782SErmal Luçi 		return -1; /* Reloop */
172d9f2a782SErmal Luçi 	}
173d9f2a782SErmal Luçi 	/* See if fib was changed by packet filter. */
174d9f2a782SErmal Luçi 	if ((*fibnum) != M_GETFIB(m)) {
175d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
176d9f2a782SErmal Luçi 		*fibnum = M_GETFIB(m);
177d9f2a782SErmal Luçi 		return -1; /* Reloop for FIB change */
178d9f2a782SErmal Luçi 	}
179d9f2a782SErmal Luçi 
180d9f2a782SErmal Luçi 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
181d9f2a782SErmal Luçi 	if (m->m_flags & M_FASTFWD_OURS) {
182d9f2a782SErmal Luçi 		if (m->m_pkthdr.rcvif == NULL)
183d9f2a782SErmal Luçi 			m->m_pkthdr.rcvif = V_loif;
184d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
185d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
186d9f2a782SErmal Luçi 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
187d9f2a782SErmal Luçi 			m->m_pkthdr.csum_data = 0xffff;
188d9f2a782SErmal Luçi 		}
189d9f2a782SErmal Luçi #ifdef SCTP
190d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
191d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
192d9f2a782SErmal Luçi #endif
193d9f2a782SErmal Luçi 		m->m_pkthdr.csum_flags |=
194d9f2a782SErmal Luçi 			CSUM_IP_CHECKED | CSUM_IP_VALID;
195d9f2a782SErmal Luçi 
196d9f2a782SErmal Luçi 		*error = netisr_queue(NETISR_IP, m);
197d9f2a782SErmal Luçi 		return 1; /* Finished */
198d9f2a782SErmal Luçi 	}
199d9f2a782SErmal Luçi 	/* Or forward to some other address? */
200d9f2a782SErmal Luçi 	if ((m->m_flags & M_IP_NEXTHOP) &&
201d9f2a782SErmal Luçi 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
202d9f2a782SErmal Luçi 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
203d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
204d9f2a782SErmal Luçi 		m->m_flags &= ~M_IP_NEXTHOP;
205d9f2a782SErmal Luçi 		m_tag_delete(m, fwd_tag);
206d9f2a782SErmal Luçi 
207d9f2a782SErmal Luçi 		return -1; /* Reloop for CHANGE of dst */
208d9f2a782SErmal Luçi 	}
209d9f2a782SErmal Luçi 
210d9f2a782SErmal Luçi 	return 0;
211d9f2a782SErmal Luçi }
212d9f2a782SErmal Luçi 
213fb3bc596SJohn Baldwin static int
214fb3bc596SJohn Baldwin ip_output_send(struct inpcb *inp, struct ifnet *ifp, struct mbuf *m,
215fb3bc596SJohn Baldwin     const struct sockaddr_in *gw, struct route *ro)
216fb3bc596SJohn Baldwin {
217*b2e60773SJohn Baldwin #ifdef KERN_TLS
218*b2e60773SJohn Baldwin 	struct ktls_session *tls = NULL;
219*b2e60773SJohn Baldwin #endif
220fb3bc596SJohn Baldwin 	struct m_snd_tag *mst;
221fb3bc596SJohn Baldwin 	int error;
222fb3bc596SJohn Baldwin 
223fb3bc596SJohn Baldwin 	MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
224fb3bc596SJohn Baldwin 	mst = NULL;
225fb3bc596SJohn Baldwin 
226*b2e60773SJohn Baldwin #ifdef KERN_TLS
227*b2e60773SJohn Baldwin 	/*
228*b2e60773SJohn Baldwin 	 * If this is an unencrypted TLS record, save a reference to
229*b2e60773SJohn Baldwin 	 * the record.  This local reference is used to call
230*b2e60773SJohn Baldwin 	 * ktls_output_eagain after the mbuf has been freed (thus
231*b2e60773SJohn Baldwin 	 * dropping the mbuf's reference) in if_output.
232*b2e60773SJohn Baldwin 	 */
233*b2e60773SJohn Baldwin 	if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
234*b2e60773SJohn Baldwin 		tls = ktls_hold(m->m_next->m_ext.ext_pgs->tls);
235*b2e60773SJohn Baldwin 		mst = tls->snd_tag;
236*b2e60773SJohn Baldwin 
237*b2e60773SJohn Baldwin 		/*
238*b2e60773SJohn Baldwin 		 * If a TLS session doesn't have a valid tag, it must
239*b2e60773SJohn Baldwin 		 * have had an earlier ifp mismatch, so drop this
240*b2e60773SJohn Baldwin 		 * packet.
241*b2e60773SJohn Baldwin 		 */
242*b2e60773SJohn Baldwin 		if (mst == NULL) {
243*b2e60773SJohn Baldwin 			error = EAGAIN;
244*b2e60773SJohn Baldwin 			goto done;
245*b2e60773SJohn Baldwin 		}
246*b2e60773SJohn Baldwin 	}
247*b2e60773SJohn Baldwin #endif
248fb3bc596SJohn Baldwin #ifdef RATELIMIT
249*b2e60773SJohn Baldwin 	if (inp != NULL && mst == NULL) {
250fb3bc596SJohn Baldwin 		if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
251fb3bc596SJohn Baldwin 		    (inp->inp_snd_tag != NULL &&
252fb3bc596SJohn Baldwin 		    inp->inp_snd_tag->ifp != ifp))
253fb3bc596SJohn Baldwin 			in_pcboutput_txrtlmt(inp, ifp, m);
254fb3bc596SJohn Baldwin 
255fb3bc596SJohn Baldwin 		if (inp->inp_snd_tag != NULL)
256fb3bc596SJohn Baldwin 			mst = inp->inp_snd_tag;
257fb3bc596SJohn Baldwin 	}
258fb3bc596SJohn Baldwin #endif
259fb3bc596SJohn Baldwin 	if (mst != NULL) {
260fb3bc596SJohn Baldwin 		KASSERT(m->m_pkthdr.rcvif == NULL,
261fb3bc596SJohn Baldwin 		    ("trying to add a send tag to a forwarded packet"));
262fb3bc596SJohn Baldwin 		if (mst->ifp != ifp) {
263fb3bc596SJohn Baldwin 			error = EAGAIN;
264fb3bc596SJohn Baldwin 			goto done;
265fb3bc596SJohn Baldwin 		}
266fb3bc596SJohn Baldwin 
267fb3bc596SJohn Baldwin 		/* stamp send tag on mbuf */
268fb3bc596SJohn Baldwin 		m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
269fb3bc596SJohn Baldwin 		m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
270fb3bc596SJohn Baldwin 	}
271fb3bc596SJohn Baldwin 
272fb3bc596SJohn Baldwin 	error = (*ifp->if_output)(ifp, m, (const struct sockaddr *)gw, ro);
273fb3bc596SJohn Baldwin 
274fb3bc596SJohn Baldwin done:
275fb3bc596SJohn Baldwin 	/* Check for route change invalidating send tags. */
276*b2e60773SJohn Baldwin #ifdef KERN_TLS
277*b2e60773SJohn Baldwin 	if (tls != NULL) {
278*b2e60773SJohn Baldwin 		if (error == EAGAIN)
279*b2e60773SJohn Baldwin 			error = ktls_output_eagain(inp, tls);
280*b2e60773SJohn Baldwin 		ktls_free(tls);
281*b2e60773SJohn Baldwin 	}
282*b2e60773SJohn Baldwin #endif
283fb3bc596SJohn Baldwin #ifdef RATELIMIT
284fb3bc596SJohn Baldwin 	if (error == EAGAIN)
285fb3bc596SJohn Baldwin 		in_pcboutput_eagain(inp);
286fb3bc596SJohn Baldwin #endif
287fb3bc596SJohn Baldwin 	return (error);
288fb3bc596SJohn Baldwin }
289fb3bc596SJohn Baldwin 
290df8bae1dSRodney W. Grimes /*
291df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
292df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
293df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
294df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
295bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
296bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
297bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
298bf984051SGleb Smirnoff  *
2993de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
3003de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
301df8bae1dSRodney W. Grimes  */
302df8bae1dSRodney W. Grimes int
303f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
304f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
305df8bae1dSRodney W. Grimes {
306cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
307a68cc388SGleb Smirnoff 	struct epoch_tracker et;
3081e78ac21SJeffrey Hsu 	struct ip *ip;
309fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
310ac9d7e26SMax Laier 	struct mbuf *m0;
31123bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
3123ae2ad08SJohn-Mark Gurney 	int mtu;
313ca8b83b0SLuigi Rizzo 	int error = 0;
3146ca363ebSGleb Smirnoff 	struct sockaddr_in *dst, sin;
3154c7a6059SGleb Smirnoff 	const struct sockaddr_in *gw;
3163c73180fSGleb Smirnoff 	struct in_ifaddr *ia;
3176ca363ebSGleb Smirnoff 	struct in_addr src;
31821d172a3SGleb Smirnoff 	int isbroadcast;
319078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
3209c57a5b6SHiroki Sato 	uint32_t fibnum;
321fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
3221a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
3231a499816SEdward Tomasz Napierala #endif
32477a01441SJohn Baldwin 
325ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
32636e8826fSMax Laier 
327bc97ba51SJulian Elischer 	if (inp != NULL) {
328baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
32962e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
330c2529042SHans Petter Selasky 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
33180cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
3329c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
33380cb9f21SKip Macy 		}
33450575ce1SAndrew Gallatin #ifdef NUMA
33550575ce1SAndrew Gallatin 		m->m_pkthdr.numa_domain = inp->inp_numa_domain;
33650575ce1SAndrew Gallatin #endif
337bc97ba51SJulian Elischer 	}
33862e1ba83SRobert Watson 
339df8bae1dSRodney W. Grimes 	if (opt) {
340ca8b83b0SLuigi Rizzo 		int len = 0;
341df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
342cb7641e8SMaxim Konovalov 		if (len != 0)
343ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
344df8bae1dSRodney W. Grimes 	}
345df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
3468f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
3478f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
3483efc3014SJulian Elischer 
349df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
35053be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
35153be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
3526d947416SGleb Smirnoff 		ip_fillid(ip);
353df8bae1dSRodney W. Grimes 	} else {
354ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
35553be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
356df8bae1dSRodney W. Grimes 	}
3573924dfa7SMichael Tuexen 	if ((flags & IP_FORWARDING) == 0)
3583924dfa7SMichael Tuexen 		IPSTAT_INC(ips_localout);
3599c9137eaSGarrett Wollman 
360054692a4SAlexander V. Chernikov 	/*
361054692a4SAlexander V. Chernikov 	 * dst/gw handling:
362054692a4SAlexander V. Chernikov 	 *
3633c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
3643c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
365054692a4SAlexander V. Chernikov 	 */
3669c57a5b6SHiroki Sato 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
3676ca363ebSGleb Smirnoff 	if (ro != NULL)
3686ca363ebSGleb Smirnoff 		dst = (struct sockaddr_in *)&ro->ro_dst;
3696ca363ebSGleb Smirnoff 	else
3706ca363ebSGleb Smirnoff 		dst = &sin;
3716ca363ebSGleb Smirnoff 	if (ro == NULL || ro->ro_rt == NULL) {
372a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
373df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
374df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
3759b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
376df8bae1dSRodney W. Grimes 	}
3776ca363ebSGleb Smirnoff 	gw = dst;
378a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
379d9f2a782SErmal Luçi again:
38084cc0778SGeorge V. Neville-Neil 	/*
38184cc0778SGeorge V. Neville-Neil 	 * Validate route against routing table additions;
38284cc0778SGeorge V. Neville-Neil 	 * a better/more specific route might have been added.
38384cc0778SGeorge V. Neville-Neil 	 */
3846ca363ebSGleb Smirnoff 	if (inp != NULL && ro != NULL && ro->ro_rt != NULL)
38584cc0778SGeorge V. Neville-Neil 		RT_VALIDATE(ro, &inp->inp_rt_cookie, fibnum);
38684cc0778SGeorge V. Neville-Neil 	/*
38784cc0778SGeorge V. Neville-Neil 	 * If there is a cached route,
38884cc0778SGeorge V. Neville-Neil 	 * check that it is to the same destination
38984cc0778SGeorge V. Neville-Neil 	 * and is still up.  If not, free it and try again.
39084cc0778SGeorge V. Neville-Neil 	 * The address family should also be checked in case of sharing the
39184cc0778SGeorge V. Neville-Neil 	 * cache with IPv6.
39284cc0778SGeorge V. Neville-Neil 	 * Also check whether routing cache needs invalidation.
39384cc0778SGeorge V. Neville-Neil 	 */
3946ca363ebSGleb Smirnoff 	if (ro != NULL && ro->ro_rt != NULL &&
3956ca363ebSGleb Smirnoff 	    ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
3966ca363ebSGleb Smirnoff 	    ro->ro_rt->rt_ifp == NULL || !RT_LINK_IS_UP(ro->ro_rt->rt_ifp) ||
39784cc0778SGeorge V. Neville-Neil 	    dst->sin_family != AF_INET ||
3986ca363ebSGleb Smirnoff 	    dst->sin_addr.s_addr != ip->ip_dst.s_addr))
399fc21c53fSRyan Stone 		RO_INVALIDATE_CACHE(ro);
400d9f2a782SErmal Luçi 	ia = NULL;
401df8bae1dSRodney W. Grimes 	/*
402a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
403a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
404a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
405a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
406df8bae1dSRodney W. Grimes 	 */
407a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
4084f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
40958a39d8cSAlan Somers 						      M_GETFIB(m)))) == NULL &&
4104f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
41158a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL) {
41286425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
413a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
414a3fd02d8SBruce M Simpson 			goto bad;
415a3fd02d8SBruce M Simpson 		}
416a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
417a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
418a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
4196ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
420a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
421a3fd02d8SBruce M Simpson 		isbroadcast = 1;
4226ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
423a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
4244f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
42558a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL &&
4264f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
42758a39d8cSAlan Somers 						M_GETFIB(m)))) == NULL) {
42886425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
429df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
430df8bae1dSRodney W. Grimes 			goto bad;
431df8bae1dSRodney W. Grimes 		}
432df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
4336ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
434df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
4356c1bd558SRyan Stone 		isbroadcast = ifp->if_flags & IFF_BROADCAST ?
4366c1bd558SRyan Stone 		    in_ifaddr_broadcast(dst->sin_addr, ia) : 0;
4376ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
4383afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
43938c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
4403afefa39SDaniel C. Sobral 		/*
44138c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
44238c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
4433afefa39SDaniel C. Sobral 		 */
44438c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
4456ca363ebSGleb Smirnoff 		mtu = ifp->if_mtu;
446cc0a3c8cSAndrey V. Elsukov 		IFP_TO_IA(ifp, ia, &in_ifa_tracker);
44738c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
44854bb7ac0SGleb Smirnoff 		/* Interface may have no addresses. */
44954bb7ac0SGleb Smirnoff 		if (ia != NULL)
4506ca363ebSGleb Smirnoff 			src = IA_SIN(ia)->sin_addr;
45154bb7ac0SGleb Smirnoff 		else
45254bb7ac0SGleb Smirnoff 			src.s_addr = INADDR_ANY;
4536ca363ebSGleb Smirnoff 	} else if (ro != NULL) {
4546ca363ebSGleb Smirnoff 		if (ro->ro_rt == NULL) {
4552c17fe93SGarrett Wollman 			/*
4566ca363ebSGleb Smirnoff 			 * We want to do any cloning requested by the link
4576ca363ebSGleb Smirnoff 			 * layer, as this is probably required in all cases
4586ca363ebSGleb Smirnoff 			 * for correct operation (as it is for ARP).
4592c17fe93SGarrett Wollman 			 */
460e440aed9SQing Li #ifdef RADIX_MPATH
4618b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
4628b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
4639c57a5b6SHiroki Sato 			    fibnum);
464e440aed9SQing Li #else
4659c57a5b6SHiroki Sato 			in_rtalloc_ign(ro, 0, fibnum);
466e440aed9SQing Li #endif
4676ca363ebSGleb Smirnoff 			if (ro->ro_rt == NULL ||
4686ca363ebSGleb Smirnoff 			    (ro->ro_rt->rt_flags & RTF_UP) == 0 ||
4696ca363ebSGleb Smirnoff 			    ro->ro_rt->rt_ifp == NULL ||
4706ca363ebSGleb Smirnoff 			    !RT_LINK_IS_UP(ro->ro_rt->rt_ifp)) {
471fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
4721a499816SEdward Tomasz Napierala 				/*
4731a499816SEdward Tomasz Napierala 				 * There is no route for this packet, but it is
4741a499816SEdward Tomasz Napierala 				 * possible that a matching SPD entry exists.
4751a499816SEdward Tomasz Napierala 				 */
4761a499816SEdward Tomasz Napierala 				no_route_but_check_spd = 1;
4771a499816SEdward Tomasz Napierala 				mtu = 0; /* Silence GCC warning. */
4781a499816SEdward Tomasz Napierala 				goto sendit;
4791a499816SEdward Tomasz Napierala #endif
48086425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
481df8bae1dSRodney W. Grimes 				error = EHOSTUNREACH;
482df8bae1dSRodney W. Grimes 				goto bad;
483df8bae1dSRodney W. Grimes 			}
4846ca363ebSGleb Smirnoff 		}
4856ca363ebSGleb Smirnoff 		ia = ifatoia(ro->ro_rt->rt_ifa);
4866ca363ebSGleb Smirnoff 		ifp = ro->ro_rt->rt_ifp;
4876ca363ebSGleb Smirnoff 		counter_u64_add(ro->ro_rt->rt_pksent, 1);
48836402a68SAlexander V. Chernikov 		rt_update_ro_flags(ro);
4896ca363ebSGleb Smirnoff 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
4906ca363ebSGleb Smirnoff 			gw = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
4916ca363ebSGleb Smirnoff 		if (ro->ro_rt->rt_flags & RTF_HOST)
4926ca363ebSGleb Smirnoff 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
4936c1bd558SRyan Stone 		else if (ifp->if_flags & IFF_BROADCAST)
49490cc51a1SRyan Stone 			isbroadcast = in_ifaddr_broadcast(gw->sin_addr, ia);
4956c1bd558SRyan Stone 		else
4966c1bd558SRyan Stone 			isbroadcast = 0;
4976ca363ebSGleb Smirnoff 		if (ro->ro_rt->rt_flags & RTF_HOST)
4986ca363ebSGleb Smirnoff 			mtu = ro->ro_rt->rt_mtu;
4997f948f12SAlexander V. Chernikov 		else
5003ae2ad08SJohn-Mark Gurney 			mtu = ifp->if_mtu;
5016ca363ebSGleb Smirnoff 		src = IA_SIN(ia)->sin_addr;
5026ca363ebSGleb Smirnoff 	} else {
5036ca363ebSGleb Smirnoff 		struct nhop4_extended nh;
5046ca363ebSGleb Smirnoff 
5056ca363ebSGleb Smirnoff 		bzero(&nh, sizeof(nh));
5066ca363ebSGleb Smirnoff 		if (fib4_lookup_nh_ext(M_GETFIB(m), ip->ip_dst, 0, 0, &nh) !=
5076ca363ebSGleb Smirnoff 		    0) {
5086ca363ebSGleb Smirnoff #if defined(IPSEC) || defined(IPSEC_SUPPORT)
5096ca363ebSGleb Smirnoff 			/*
5106ca363ebSGleb Smirnoff 			 * There is no route for this packet, but it is
5116ca363ebSGleb Smirnoff 			 * possible that a matching SPD entry exists.
5126ca363ebSGleb Smirnoff 			 */
5136ca363ebSGleb Smirnoff 			no_route_but_check_spd = 1;
5146ca363ebSGleb Smirnoff 			mtu = 0; /* Silence GCC warning. */
5156ca363ebSGleb Smirnoff 			goto sendit;
5166ca363ebSGleb Smirnoff #endif
5176ca363ebSGleb Smirnoff 			IPSTAT_INC(ips_noroute);
5186ca363ebSGleb Smirnoff 			error = EHOSTUNREACH;
5196ca363ebSGleb Smirnoff 			goto bad;
5206ca363ebSGleb Smirnoff 		}
5216ca363ebSGleb Smirnoff 		ifp = nh.nh_ifp;
5226ca363ebSGleb Smirnoff 		mtu = nh.nh_mtu;
5236ca363ebSGleb Smirnoff 		/*
5246ca363ebSGleb Smirnoff 		 * We are rewriting here dst to be gw actually, contradicting
5256ca363ebSGleb Smirnoff 		 * comment at the beginning of the function. However, in this
5266ca363ebSGleb Smirnoff 		 * case we are always dealing with on stack dst.
5276ca363ebSGleb Smirnoff 		 * In case if pfil(9) sends us back to beginning of the
5286ca363ebSGleb Smirnoff 		 * function, the dst would be rewritten by ip_output_pfil().
5296ca363ebSGleb Smirnoff 		 */
5306ca363ebSGleb Smirnoff 		MPASS(dst == &sin);
5316ca363ebSGleb Smirnoff 		dst->sin_addr = nh.nh_addr;
5326ca363ebSGleb Smirnoff 		ia = nh.nh_ia;
5336ca363ebSGleb Smirnoff 		src = nh.nh_src;
5346ca363ebSGleb Smirnoff 		isbroadcast = (((nh.nh_flags & (NHF_HOST | NHF_BROADCAST)) ==
5356ca363ebSGleb Smirnoff 		    (NHF_HOST | NHF_BROADCAST)) ||
5366ca363ebSGleb Smirnoff 		    ((ifp->if_flags & IFF_BROADCAST) &&
5376ca363ebSGleb Smirnoff 		    in_ifaddr_broadcast(dst->sin_addr, ia)));
5386ca363ebSGleb Smirnoff 	}
5396ca363ebSGleb Smirnoff 
540c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
5416ca363ebSGleb Smirnoff 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, ro=%p (rt_flags=0x%08x) ifp=%p",
5426ca363ebSGleb Smirnoff 	    __func__, mtu, ro,
5436ca363ebSGleb Smirnoff 	    (ro != NULL && ro->ro_rt != NULL) ? ro->ro_rt->rt_flags : 0, ifp));
544d9f2a782SErmal Luçi 
5459b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
546df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
547df8bae1dSRodney W. Grimes 		/*
548183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
549183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
550183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
551183e1c86SGleb Smirnoff 		 */
552183e1c86SGleb Smirnoff 		gw = dst;
553183e1c86SGleb Smirnoff 		/*
554df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
555df8bae1dSRodney W. Grimes 		 */
556df8bae1dSRodney W. Grimes 		if (imo != NULL) {
557df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
5581c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
5591c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
560bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
561bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
562bbb4330bSLuigi Rizzo 				    INADDR_ANY;
563df8bae1dSRodney W. Grimes 		} else
564df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
565df8bae1dSRodney W. Grimes 		/*
566df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
567df8bae1dSRodney W. Grimes 		 */
5681c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
569df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
57086425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
571df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
572df8bae1dSRodney W. Grimes 				goto bad;
573df8bae1dSRodney W. Grimes 			}
5741c5de19aSGarrett Wollman 		}
575df8bae1dSRodney W. Grimes 		/*
576df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
577df8bae1dSRodney W. Grimes 		 * of outgoing interface.
578df8bae1dSRodney W. Grimes 		 */
5796ca363ebSGleb Smirnoff 		if (ip->ip_src.s_addr == INADDR_ANY)
5806ca363ebSGleb Smirnoff 			ip->ip_src = src;
581df8bae1dSRodney W. Grimes 
5828b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
5838b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
584df8bae1dSRodney W. Grimes 			/*
5858b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
5868b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
5878b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
5888b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
5898b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
590df8bae1dSRodney W. Grimes 			 */
591331dff07SAlexander V. Chernikov 			ip_mloopback(ifp, m, hlen);
5928b889dbbSBruce M Simpson 		} else {
593df8bae1dSRodney W. Grimes 			/*
594df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
595df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
596df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
597df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
598df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
599df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
600df8bae1dSRodney W. Grimes 			 *
601df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
602df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
603df8bae1dSRodney W. Grimes 			 * if necessary.
604df8bae1dSRodney W. Grimes 			 */
605603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
606f0068c4aSGarrett Wollman 				/*
607bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
608f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
609f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
610f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
611f0068c4aSGarrett Wollman 				 */
612603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
613f0068c4aSGarrett Wollman 					imo = NULL;
614bbb4330bSLuigi Rizzo 				if (ip_mforward &&
615bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
616df8bae1dSRodney W. Grimes 					m_freem(m);
617df8bae1dSRodney W. Grimes 					goto done;
618df8bae1dSRodney W. Grimes 				}
619df8bae1dSRodney W. Grimes 			}
620df8bae1dSRodney W. Grimes 		}
6215e9ae478SGarrett Wollman 
622df8bae1dSRodney W. Grimes 		/*
623df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
624df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
625df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
626df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
6278b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
6288b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
6298b889dbbSBruce M Simpson 		 * the loopback interface.
630df8bae1dSRodney W. Grimes 		 */
631f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
632df8bae1dSRodney W. Grimes 			m_freem(m);
633df8bae1dSRodney W. Grimes 			goto done;
634df8bae1dSRodney W. Grimes 		}
635df8bae1dSRodney W. Grimes 
636df8bae1dSRodney W. Grimes 		goto sendit;
637df8bae1dSRodney W. Grimes 	}
6386a7c943cSAndre Oppermann 
639df8bae1dSRodney W. Grimes 	/*
6402b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
641cb459254SJohn-Mark Gurney 	 * of the outoing interface.
642df8bae1dSRodney W. Grimes 	 */
6436ca363ebSGleb Smirnoff 	if (ip->ip_src.s_addr == INADDR_ANY)
6446ca363ebSGleb Smirnoff 		ip->ip_src = src;
6456a7c943cSAndre Oppermann 
646ca7a789aSMax Laier 	/*
647df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
6488c3f5566SRuslan Ermilov 	 * verify user is allowed to send
649df8bae1dSRodney W. Grimes 	 * such a packet.
650df8bae1dSRodney W. Grimes 	 */
6519f9b3dc4SGarrett Wollman 	if (isbroadcast) {
652df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
653df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
654df8bae1dSRodney W. Grimes 			goto bad;
655df8bae1dSRodney W. Grimes 		}
656df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
657df8bae1dSRodney W. Grimes 			error = EACCES;
658df8bae1dSRodney W. Grimes 			goto bad;
659df8bae1dSRodney W. Grimes 		}
660df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
6618f134647SGleb Smirnoff 		if (ip_len > mtu) {
662df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
663df8bae1dSRodney W. Grimes 			goto bad;
664df8bae1dSRodney W. Grimes 		}
665df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
6669f9b3dc4SGarrett Wollman 	} else {
667df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
6689f9b3dc4SGarrett Wollman 	}
669df8bae1dSRodney W. Grimes 
670f1743588SDarren Reed sendit:
671fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
672fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4)) {
673fcf59617SAndrey V. Elsukov 		if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) {
674fcf59617SAndrey V. Elsukov 			if (error == EINPROGRESS)
675fcf59617SAndrey V. Elsukov 				error = 0;
6761dfcf0d2SAndre Oppermann 			goto done;
677fcf59617SAndrey V. Elsukov 		}
67833841545SHajimu UMEMOTO 	}
6791a499816SEdward Tomasz Napierala 	/*
6801a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
6811a499816SEdward Tomasz Napierala 	 */
6821a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
6831a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
6841a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
6851a499816SEdward Tomasz Napierala 		goto bad;
6861a499816SEdward Tomasz Napierala 	}
6871dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
68833841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
68933841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
690b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
69133841545SHajimu UMEMOTO 
692c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
693b252313fSGleb Smirnoff 	if (PFIL_HOOKED_OUT(V_inet_pfil_head)) {
69405fc9d78SKristof Provost 		switch (ip_output_pfil(&m, ifp, flags, inp, dst, &fibnum,
69505fc9d78SKristof Provost 		    &error)) {
696d9f2a782SErmal Luçi 		case 1: /* Finished */
697c4ac87eaSDarren Reed 			goto done;
6989b932e9eSAndre Oppermann 
699d9f2a782SErmal Luçi 		case 0: /* Continue normally */
700c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
701d9f2a782SErmal Luçi 			break;
702fed1c7e9SSøren Schmidt 
703d9f2a782SErmal Luçi 		case -1: /* Need to try again */
704d9f2a782SErmal Luçi 			/* Reset everything for a new round */
7056ca363ebSGleb Smirnoff 			if (ro != NULL) {
7069c57a5b6SHiroki Sato 				RO_RTFREE(ro);
7074fb3a820SAlexander V. Chernikov 				ro->ro_prepend = NULL;
7086ca363ebSGleb Smirnoff 			}
709d9f2a782SErmal Luçi 			gw = dst;
710d9f2a782SErmal Luçi 			ip = mtod(m, struct ip *);
7119b932e9eSAndre Oppermann 			goto again;
712d9f2a782SErmal Luçi 
713d9f2a782SErmal Luçi 		}
714099dd043SAndre Oppermann 	}
7152b25acc1SLuigi Rizzo 
7166c1c6ae5SRodney W. Grimes 	/* IN_LOOPBACK must not appear on the wire - RFC1122. */
7176c1c6ae5SRodney W. Grimes 	if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) ||
7186c1c6ae5SRodney W. Grimes 	    IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) {
71951c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
72086425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
72151c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
72251c8ec4aSRuslan Ermilov 			goto bad;
72351c8ec4aSRuslan Ermilov 		}
72451c8ec4aSRuslan Ermilov 	}
72551c8ec4aSRuslan Ermilov 
726206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
727078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
72882334850SJohn Baldwin 		m = mb_unmapped_to_ext(m);
72982334850SJohn Baldwin 		if (m == NULL) {
73082334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
73182334850SJohn Baldwin 			error = ENOBUFS;
73282334850SJohn Baldwin 			goto bad;
73382334850SJohn Baldwin 		}
734db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
735078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
73682334850SJohn Baldwin 	} else if ((ifp->if_capenable & IFCAP_NOMAP) == 0) {
73782334850SJohn Baldwin 		m = mb_unmapped_to_ext(m);
73882334850SJohn Baldwin 		if (m == NULL) {
73982334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
74082334850SJohn Baldwin 			error = ENOBUFS;
74182334850SJohn Baldwin 			goto bad;
74282334850SJohn Baldwin 		}
743db4f9cc7SJonathan Lemon 	}
7442f4afd21SRandall Stewart #ifdef SCTP
745078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
74682334850SJohn Baldwin 		m = mb_unmapped_to_ext(m);
74782334850SJohn Baldwin 		if (m == NULL) {
74882334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
74982334850SJohn Baldwin 			error = ENOBUFS;
75082334850SJohn Baldwin 			goto bad;
75182334850SJohn Baldwin 		}
7521966e5b5SRandall Stewart 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
753078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
7542f4afd21SRandall Stewart 	}
7552f4afd21SRandall Stewart #endif
756db4f9cc7SJonathan Lemon 
757e7319babSPoul-Henning Kamp 	/*
758db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
759233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
760df8bae1dSRodney W. Grimes 	 */
76121d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
762bf7dcda3SGleb Smirnoff 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
763df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
764078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
765df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
766078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
767078468edSGleb Smirnoff 		}
7685da9f8faSJosef Karthauser 
769233dcce1SAndre Oppermann 		/*
770233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
771233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
772233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
773233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
774233dcce1SAndre Oppermann 		 */
77538c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
776233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
7777caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
7787caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
779233dcce1SAndre Oppermann 			else
7807caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
7817caf4ab7SGleb Smirnoff 
7827caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
7835da9f8faSJosef Karthauser 		}
78453dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
7853390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
786eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
7879d9edc56SMike Silbersack #endif
788147f74d1SAndre Oppermann 		/*
789147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
790147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
791147f74d1SAndre Oppermann 		 */
79286bd0491SAndre Oppermann 		m_clrprotoflags(m);
79357f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
794fb3bc596SJohn Baldwin 		error = ip_output_send(inp, ifp, m, gw, ro);
795df8bae1dSRodney W. Grimes 		goto done;
796df8bae1dSRodney W. Grimes 	}
7971e78ac21SJeffrey Hsu 
798233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
79921d172a3SGleb Smirnoff 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
800df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
80186425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
802df8bae1dSRodney W. Grimes 		goto bad;
803df8bae1dSRodney W. Grimes 	}
8041e78ac21SJeffrey Hsu 
8051e78ac21SJeffrey Hsu 	/*
8061e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
8071e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
8081e78ac21SJeffrey Hsu 	 */
809078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
8101e78ac21SJeffrey Hsu 	if (error)
811df8bae1dSRodney W. Grimes 		goto bad;
8121e78ac21SJeffrey Hsu 	for (; m; m = m0) {
813df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
814b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
81507203494SDaniel C. Sobral 		if (error == 0) {
816fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
81707203494SDaniel C. Sobral 			if (ia != NULL) {
8187caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
8197caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
8207caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
82107203494SDaniel C. Sobral 			}
822147f74d1SAndre Oppermann 			/*
823147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
824147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
825147f74d1SAndre Oppermann 			 */
82686bd0491SAndre Oppermann 			m_clrprotoflags(m);
827fe937674SJosef Karthauser 
8282e77d270SAndrey V. Elsukov 			IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp,
8292e77d270SAndrey V. Elsukov 			    mtod(m, struct ip *), NULL);
830fb3bc596SJohn Baldwin 			error = ip_output_send(inp, ifp, m, gw, ro);
831fe937674SJosef Karthauser 		} else
832df8bae1dSRodney W. Grimes 			m_freem(m);
833df8bae1dSRodney W. Grimes 	}
834df8bae1dSRodney W. Grimes 
835df8bae1dSRodney W. Grimes 	if (error == 0)
83686425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
8371e78ac21SJeffrey Hsu 
838df8bae1dSRodney W. Grimes done:
839a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
840df8bae1dSRodney W. Grimes 	return (error);
841df8bae1dSRodney W. Grimes  bad:
8423528d68fSBill Paul 	m_freem(m);
843df8bae1dSRodney W. Grimes 	goto done;
844df8bae1dSRodney W. Grimes }
845df8bae1dSRodney W. Grimes 
8461e78ac21SJeffrey Hsu /*
8471e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
8481e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
8491e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
8501e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
8511e78ac21SJeffrey Hsu  *
8521e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
8531e78ac21SJeffrey Hsu  */
8541e78ac21SJeffrey Hsu int
8551e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
856078468edSGleb Smirnoff     u_long if_hwassist_flags)
8571e78ac21SJeffrey Hsu {
8581e78ac21SJeffrey Hsu 	int error = 0;
8591e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
8601e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
8611e78ac21SJeffrey Hsu 	int off;
8621e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
8631e78ac21SJeffrey Hsu 	int firstlen;
8641e78ac21SJeffrey Hsu 	struct mbuf **mnext;
8651e78ac21SJeffrey Hsu 	int nfrags;
86621d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
8671e78ac21SJeffrey Hsu 
86821d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
86921d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
87021d172a3SGleb Smirnoff 
87121d172a3SGleb Smirnoff 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
87286425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
8731e78ac21SJeffrey Hsu 		return EMSGSIZE;
8741e78ac21SJeffrey Hsu 	}
8751e78ac21SJeffrey Hsu 
8761e78ac21SJeffrey Hsu 	/*
8771e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
8781e78ac21SJeffrey Hsu 	 */
8791e78ac21SJeffrey Hsu 	if (len < 8)
8801e78ac21SJeffrey Hsu 		return EMSGSIZE;
8811e78ac21SJeffrey Hsu 
8821e78ac21SJeffrey Hsu 	/*
8831e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
8841e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
8851e78ac21SJeffrey Hsu 	 */
886da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
88782334850SJohn Baldwin 		m0 = mb_unmapped_to_ext(m0);
88882334850SJohn Baldwin 		if (m0 == NULL) {
88982334850SJohn Baldwin 			error = ENOBUFS;
89082334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
89182334850SJohn Baldwin 			goto done;
89282334850SJohn Baldwin 		}
8931e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
8941e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
8951e78ac21SJeffrey Hsu 	}
8962f4afd21SRandall Stewart #ifdef SCTP
897da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
89882334850SJohn Baldwin 		m0 = mb_unmapped_to_ext(m0);
89982334850SJohn Baldwin 		if (m0 == NULL) {
90082334850SJohn Baldwin 			error = ENOBUFS;
90182334850SJohn Baldwin 			IPSTAT_INC(ips_odropped);
90282334850SJohn Baldwin 			goto done;
90382334850SJohn Baldwin 		}
9041966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
9052f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
9062f4afd21SRandall Stewart 	}
9072f4afd21SRandall Stewart #endif
9081e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
9091e78ac21SJeffrey Hsu 		/*
9101e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
9111e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
9121e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
9131e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
9141e78ac21SJeffrey Hsu 		 *
9151e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
9161e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
9171e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
9181e78ac21SJeffrey Hsu 		 */
9191e78ac21SJeffrey Hsu 		int newlen;
9201e78ac21SJeffrey Hsu 
9219c0f6aa7SHans Petter Selasky 		off = MIN(mtu, m0->m_pkthdr.len);
9221e78ac21SJeffrey Hsu 
9231e78ac21SJeffrey Hsu 		/*
9241e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
9251e78ac21SJeffrey Hsu 		 * 8-byte boundary
9261e78ac21SJeffrey Hsu 		 */
9271e78ac21SJeffrey Hsu 		if (off < hlen)
9281e78ac21SJeffrey Hsu 			goto smart_frag_failure;
9291e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
9301e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
9311e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
9321e78ac21SJeffrey Hsu 			/* we failed, go back the default */
9331e78ac21SJeffrey Hsu smart_frag_failure:
9341e78ac21SJeffrey Hsu 			newlen = len;
9351e78ac21SJeffrey Hsu 			off = hlen + len;
9361e78ac21SJeffrey Hsu 		}
9371e78ac21SJeffrey Hsu 		len = newlen;
9381e78ac21SJeffrey Hsu 
9391e78ac21SJeffrey Hsu 	} else {
9401e78ac21SJeffrey Hsu 		off = hlen + len;
9411e78ac21SJeffrey Hsu 	}
9421e78ac21SJeffrey Hsu 
9431e78ac21SJeffrey Hsu 	firstlen = off - hlen;
9441e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
9451e78ac21SJeffrey Hsu 
9461e78ac21SJeffrey Hsu 	/*
9471e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
9481e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
9491e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
9501e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
9511e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
9521e78ac21SJeffrey Hsu 	 */
95321d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
9541e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
9551e78ac21SJeffrey Hsu 		struct mbuf *m;
9561e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
9571e78ac21SJeffrey Hsu 
958dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
9590b17fba7SAndre Oppermann 		if (m == NULL) {
9601e78ac21SJeffrey Hsu 			error = ENOBUFS;
96186425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
9621e78ac21SJeffrey Hsu 			goto done;
9631e78ac21SJeffrey Hsu 		}
964c4c4346fSHans Petter Selasky 		/*
965c4c4346fSHans Petter Selasky 		 * Make sure the complete packet header gets copied
966c4c4346fSHans Petter Selasky 		 * from the originating mbuf to the newly created
967c4c4346fSHans Petter Selasky 		 * mbuf. This also ensures that existing firewall
968c4c4346fSHans Petter Selasky 		 * classification(s), VLAN tags and so on get copied
969c4c4346fSHans Petter Selasky 		 * to the resulting fragmented packet(s):
970c4c4346fSHans Petter Selasky 		 */
971c4c4346fSHans Petter Selasky 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
972c4c4346fSHans Petter Selasky 			m_free(m);
973c4c4346fSHans Petter Selasky 			error = ENOBUFS;
974c4c4346fSHans Petter Selasky 			IPSTAT_INC(ips_odropped);
975c4c4346fSHans Petter Selasky 			goto done;
976c4c4346fSHans Petter Selasky 		}
9771e78ac21SJeffrey Hsu 		/*
9781e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
9791e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
9808b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
9811e78ac21SJeffrey Hsu 		 */
9821e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
9831e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
9841e78ac21SJeffrey Hsu 		*mhip = *ip;
9851e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
9861e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9871e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
9881e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
9891e78ac21SJeffrey Hsu 		}
9901e78ac21SJeffrey Hsu 		m->m_len = mhlen;
99121d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
99221d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
993fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
99421d172a3SGleb Smirnoff 			len = ip_len - off;
995fb86dfcdSAndre Oppermann 		else
9961e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
9971e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
998eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
9990b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
10001e78ac21SJeffrey Hsu 			m_free(m);
10011e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
100286425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
10031e78ac21SJeffrey Hsu 			goto done;
10041e78ac21SJeffrey Hsu 		}
10051e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
10061e78ac21SJeffrey Hsu #ifdef MAC
100730d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
10081e78ac21SJeffrey Hsu #endif
10091e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
10101e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
1011078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
10121e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
1013078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
1014078468edSGleb Smirnoff 		}
10151e78ac21SJeffrey Hsu 		*mnext = m;
10161e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
10171e78ac21SJeffrey Hsu 	}
101886425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
10191e78ac21SJeffrey Hsu 
10201e78ac21SJeffrey Hsu 	/*
10211e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
10221e78ac21SJeffrey Hsu 	 * and updating header.
10231e78ac21SJeffrey Hsu 	 */
102421d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
10251e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
10261e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
102721d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
10281e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
1029078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
10301e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
1031078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
1032078468edSGleb Smirnoff 	}
10331e78ac21SJeffrey Hsu 
10341e78ac21SJeffrey Hsu done:
10351e78ac21SJeffrey Hsu 	*m_frag = m0;
10361e78ac21SJeffrey Hsu 	return error;
10371e78ac21SJeffrey Hsu }
10381e78ac21SJeffrey Hsu 
10391c238475SJonathan Lemon void
1040db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1041db4f9cc7SJonathan Lemon {
1042db4f9cc7SJonathan Lemon 	struct ip *ip;
10431fdbfb90STom Jones 	struct udphdr *uh;
10441fdbfb90STom Jones 	uint16_t cklen, csum, offset;
1045db4f9cc7SJonathan Lemon 
1046db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
104753be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
10481fdbfb90STom Jones 
10491fdbfb90STom Jones 	if (m->m_pkthdr.csum_flags & CSUM_UDP) {
10501fdbfb90STom Jones 		/* if udp header is not in the first mbuf copy udplen */
1051b6e87011STom Jones 		if (offset + sizeof(struct udphdr) > m->m_len) {
1052590d0a43SAndrey V. Elsukov 			m_copydata(m, offset + offsetof(struct udphdr,
1053590d0a43SAndrey V. Elsukov 			    uh_ulen), sizeof(cklen), (caddr_t)&cklen);
1054b6e87011STom Jones 			cklen = ntohs(cklen);
1055b6e87011STom Jones 		} else {
1056590d0a43SAndrey V. Elsukov 			uh = (struct udphdr *)mtodo(m, offset);
10571fdbfb90STom Jones 			cklen = ntohs(uh->uh_ulen);
10581fdbfb90STom Jones 		}
10591fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen + offset, offset);
10601fdbfb90STom Jones 		if (csum == 0)
1061206a3274SRuslan Ermilov 			csum = 0xffff;
10621fdbfb90STom Jones 	} else {
10631fdbfb90STom Jones 		cklen = ntohs(ip->ip_len);
10641fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen, offset);
10651fdbfb90STom Jones 	}
1066db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1067db4f9cc7SJonathan Lemon 
1068590d0a43SAndrey V. Elsukov 	if (offset + sizeof(csum) > m->m_len)
1069590d0a43SAndrey V. Elsukov 		m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
1070590d0a43SAndrey V. Elsukov 	else
1071590d0a43SAndrey V. Elsukov 		*(u_short *)mtodo(m, offset) = csum;
1072db4f9cc7SJonathan Lemon }
1073db4f9cc7SJonathan Lemon 
1074df8bae1dSRodney W. Grimes /*
1075df8bae1dSRodney W. Grimes  * IP socket option processing.
1076df8bae1dSRodney W. Grimes  */
1077df8bae1dSRodney W. Grimes int
1078f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
1079df8bae1dSRodney W. Grimes {
1080cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1081cfe8b629SGarrett Wollman 	int	error, optval;
1082dc847eb6SAdrian Chadd #ifdef	RSS
1083dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
1084dc847eb6SAdrian Chadd 	int retval;
1085dc847eb6SAdrian Chadd #endif
1086df8bae1dSRodney W. Grimes 
1087cfe8b629SGarrett Wollman 	error = optval = 0;
1088cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1089fc06cd42SMikolaj Golub 		error = EINVAL;
1090fc06cd42SMikolaj Golub 
1091fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
1092fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
1093fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
1094fc06cd42SMikolaj Golub 			case SO_REUSEADDR:
1095fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1096efdf104bSMikolaj Golub 				if ((so->so_options & SO_REUSEADDR) != 0)
1097efdf104bSMikolaj Golub 					inp->inp_flags2 |= INP_REUSEADDR;
1098fc06cd42SMikolaj Golub 				else
1099efdf104bSMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEADDR;
1100fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1101fc06cd42SMikolaj Golub 				error = 0;
1102fc06cd42SMikolaj Golub 				break;
1103fc06cd42SMikolaj Golub 			case SO_REUSEPORT:
1104fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1105fc06cd42SMikolaj Golub 				if ((so->so_options & SO_REUSEPORT) != 0)
1106fc06cd42SMikolaj Golub 					inp->inp_flags2 |= INP_REUSEPORT;
1107fc06cd42SMikolaj Golub 				else
1108fc06cd42SMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEPORT;
1109fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1110fc06cd42SMikolaj Golub 				error = 0;
1111fc06cd42SMikolaj Golub 				break;
11121a43cff9SSean Bruno 			case SO_REUSEPORT_LB:
11131a43cff9SSean Bruno 				INP_WLOCK(inp);
11141a43cff9SSean Bruno 				if ((so->so_options & SO_REUSEPORT_LB) != 0)
11151a43cff9SSean Bruno 					inp->inp_flags2 |= INP_REUSEPORT_LB;
11161a43cff9SSean Bruno 				else
11171a43cff9SSean Bruno 					inp->inp_flags2 &= ~INP_REUSEPORT_LB;
11181a43cff9SSean Bruno 				INP_WUNLOCK(inp);
11191a43cff9SSean Bruno 				error = 0;
11201a43cff9SSean Bruno 				break;
1121fc06cd42SMikolaj Golub 			case SO_SETFIB:
1122fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1123fc06cd42SMikolaj Golub 				inp->inp_inc.inc_fibnum = so->so_fibnum;
1124fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1125fc06cd42SMikolaj Golub 				error = 0;
1126fc06cd42SMikolaj Golub 				break;
1127f3e7afe2SHans Petter Selasky 			case SO_MAX_PACING_RATE:
1128f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1129f3e7afe2SHans Petter Selasky 				INP_WLOCK(inp);
1130f3e7afe2SHans Petter Selasky 				inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1131f3e7afe2SHans Petter Selasky 				INP_WUNLOCK(inp);
1132f3e7afe2SHans Petter Selasky 				error = 0;
1133f3e7afe2SHans Petter Selasky #else
1134f3e7afe2SHans Petter Selasky 				error = EOPNOTSUPP;
1135f3e7afe2SHans Petter Selasky #endif
1136f3e7afe2SHans Petter Selasky 				break;
1137fc06cd42SMikolaj Golub 			default:
1138fc06cd42SMikolaj Golub 				break;
1139fc06cd42SMikolaj Golub 			}
1140fc06cd42SMikolaj Golub 		}
1141fc06cd42SMikolaj Golub 		return (error);
1142cfe8b629SGarrett Wollman 	}
1143df8bae1dSRodney W. Grimes 
1144cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1145cfe8b629SGarrett Wollman 	case SOPT_SET:
1146cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1147df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1148df8bae1dSRodney W. Grimes #ifdef notyet
1149df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1150df8bae1dSRodney W. Grimes #endif
1151cfe8b629SGarrett Wollman 		{
1152cfe8b629SGarrett Wollman 			struct mbuf *m;
1153cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1154cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1155cfe8b629SGarrett Wollman 				break;
1156cfe8b629SGarrett Wollman 			}
1157dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
11580b17fba7SAndre Oppermann 			if (m == NULL) {
1159cfe8b629SGarrett Wollman 				error = ENOBUFS;
1160cfe8b629SGarrett Wollman 				break;
1161cfe8b629SGarrett Wollman 			}
1162cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1163cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1164cfe8b629SGarrett Wollman 					    m->m_len);
1165635354c4SMaxim Konovalov 			if (error) {
1166635354c4SMaxim Konovalov 				m_free(m);
1167635354c4SMaxim Konovalov 				break;
1168635354c4SMaxim Konovalov 			}
11698501a69cSRobert Watson 			INP_WLOCK(inp);
1170993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
11718501a69cSRobert Watson 			INP_WUNLOCK(inp);
1172993d9505SRobert Watson 			return (error);
1173cfe8b629SGarrett Wollman 		}
1174df8bae1dSRodney W. Grimes 
1175f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
1176f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
1177f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
1178f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
1179f44270e7SPawel Jakub Dawidek 				if (error)
1180be9347e3SAdrian Chadd 					break;
1181be9347e3SAdrian Chadd 			}
1182cef27294SAdrian Chadd 			/* FALLTHROUGH */
11830a100a6fSAdrian Chadd 		case IP_BINDMULTI:
11840a100a6fSAdrian Chadd #ifdef	RSS
11850a100a6fSAdrian Chadd 		case IP_RSS_LISTEN_BUCKET:
11860a100a6fSAdrian Chadd #endif
1187df8bae1dSRodney W. Grimes 		case IP_TOS:
1188df8bae1dSRodney W. Grimes 		case IP_TTL:
1189936cd18dSAndre Oppermann 		case IP_MINTTL:
1190df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1191df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1192dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1193df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
11944957466bSMatthew N. Dodd 		case IP_RECVTTL:
119582c23ebaSBill Fenner 		case IP_RECVIF:
11968afa2304SBruce M Simpson 		case IP_ONESBCAST:
1197b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
11983cca425bSMichael Tuexen 		case IP_RECVTOS:
11999d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
12009d3ddf43SAdrian Chadd #ifdef	RSS
12019d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
12029d3ddf43SAdrian Chadd #endif
1203cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1204cfe8b629SGarrett Wollman 					    sizeof optval);
1205cfe8b629SGarrett Wollman 			if (error)
1206cfe8b629SGarrett Wollman 				break;
1207df8bae1dSRodney W. Grimes 
1208cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1209df8bae1dSRodney W. Grimes 			case IP_TOS:
1210ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1211df8bae1dSRodney W. Grimes 				break;
1212df8bae1dSRodney W. Grimes 
1213df8bae1dSRodney W. Grimes 			case IP_TTL:
1214ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1215df8bae1dSRodney W. Grimes 				break;
1216936cd18dSAndre Oppermann 
1217936cd18dSAndre Oppermann 			case IP_MINTTL:
1218a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1219936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1220936cd18dSAndre Oppermann 				else
1221936cd18dSAndre Oppermann 					error = EINVAL;
1222936cd18dSAndre Oppermann 				break;
1223936cd18dSAndre Oppermann 
1224a138d217SRobert Watson #define	OPTSET(bit) do {						\
12258501a69cSRobert Watson 	INP_WLOCK(inp);							\
1226df8bae1dSRodney W. Grimes 	if (optval)							\
1227df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1228df8bae1dSRodney W. Grimes 	else								\
1229a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
12308501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1231a138d217SRobert Watson } while (0)
1232df8bae1dSRodney W. Grimes 
12330a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
12340a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
12350a100a6fSAdrian Chadd 	if (val)							\
12360a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
12370a100a6fSAdrian Chadd 	else								\
12380a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
12390a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
12400a100a6fSAdrian Chadd } while (0)
12410a100a6fSAdrian Chadd 
1242df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1243df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1244df8bae1dSRodney W. Grimes 				break;
1245df8bae1dSRodney W. Grimes 
1246df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1247df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1248df8bae1dSRodney W. Grimes 				break;
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1251df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1252df8bae1dSRodney W. Grimes 				break;
125382c23ebaSBill Fenner 
1254dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1255dce33a45SErmal Luçi 				OPTSET2(INP_ORIGDSTADDR, optval);
1256dce33a45SErmal Luçi 				break;
1257dce33a45SErmal Luçi 
12584957466bSMatthew N. Dodd 			case IP_RECVTTL:
12594957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
12604957466bSMatthew N. Dodd 				break;
12614957466bSMatthew N. Dodd 
126282c23ebaSBill Fenner 			case IP_RECVIF:
126382c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
126482c23ebaSBill Fenner 				break;
12656a800098SYoshinobu Inoue 
12668afa2304SBruce M Simpson 			case IP_ONESBCAST:
12678afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
12688afa2304SBruce M Simpson 				break;
1269b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1270b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1271b2828ad2SAndre Oppermann 				break;
1272f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1273f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1274be9347e3SAdrian Chadd 				break;
12753cca425bSMichael Tuexen 			case IP_RECVTOS:
12763cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
12773cca425bSMichael Tuexen 				break;
12780a100a6fSAdrian Chadd 			case IP_BINDMULTI:
12790a100a6fSAdrian Chadd 				OPTSET2(INP_BINDMULTI, optval);
12800a100a6fSAdrian Chadd 				break;
12819d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
12829d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
12839d3ddf43SAdrian Chadd 				break;
12840a100a6fSAdrian Chadd #ifdef	RSS
12850a100a6fSAdrian Chadd 			case IP_RSS_LISTEN_BUCKET:
12860a100a6fSAdrian Chadd 				if ((optval >= 0) &&
12870a100a6fSAdrian Chadd 				    (optval < rss_getnumbuckets())) {
12880a100a6fSAdrian Chadd 					inp->inp_rss_listen_bucket = optval;
12890a100a6fSAdrian Chadd 					OPTSET2(INP_RSS_BUCKET_SET, 1);
12900a100a6fSAdrian Chadd 				} else {
12910a100a6fSAdrian Chadd 					error = EINVAL;
12920a100a6fSAdrian Chadd 				}
12930a100a6fSAdrian Chadd 				break;
12949d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
12959d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
12969d3ddf43SAdrian Chadd 				break;
12970a100a6fSAdrian Chadd #endif
1298df8bae1dSRodney W. Grimes 			}
1299df8bae1dSRodney W. Grimes 			break;
1300df8bae1dSRodney W. Grimes #undef OPTSET
13010a100a6fSAdrian Chadd #undef OPTSET2
1302df8bae1dSRodney W. Grimes 
130371498f30SBruce M Simpson 		/*
130471498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
130571498f30SBruce M Simpson 		 * module.
130671498f30SBruce M Simpson 		 */
1307df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1308f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1309df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1310df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1311df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1312df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
131371498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
131471498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
131571498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
131671498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
131771498f30SBruce M Simpson 		case IP_MSFILTER:
131871498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
131971498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
132071498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
132171498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
132271498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
132371498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
132471498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1325df8bae1dSRodney W. Grimes 			break;
1326df8bae1dSRodney W. Grimes 
132733b3ac06SPeter Wemm 		case IP_PORTRANGE:
1328cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1329cfe8b629SGarrett Wollman 					    sizeof optval);
1330cfe8b629SGarrett Wollman 			if (error)
1331cfe8b629SGarrett Wollman 				break;
133233b3ac06SPeter Wemm 
13338501a69cSRobert Watson 			INP_WLOCK(inp);
133433b3ac06SPeter Wemm 			switch (optval) {
133533b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
133633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
133733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
133833b3ac06SPeter Wemm 				break;
133933b3ac06SPeter Wemm 
134033b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
134133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
134233b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
134333b3ac06SPeter Wemm 				break;
134433b3ac06SPeter Wemm 
134533b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
134633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
134733b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
134833b3ac06SPeter Wemm 				break;
134933b3ac06SPeter Wemm 
135033b3ac06SPeter Wemm 			default:
135133b3ac06SPeter Wemm 				error = EINVAL;
135233b3ac06SPeter Wemm 				break;
135333b3ac06SPeter Wemm 			}
13548501a69cSRobert Watson 			INP_WUNLOCK(inp);
1355ce8c72b1SPeter Wemm 			break;
135633b3ac06SPeter Wemm 
1357fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
13586a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1359fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1360fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
13616a800098SYoshinobu Inoue 				break;
13626a800098SYoshinobu Inoue 			}
1363fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1364b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
13656a800098SYoshinobu Inoue 
1366df8bae1dSRodney W. Grimes 		default:
1367df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1368df8bae1dSRodney W. Grimes 			break;
1369df8bae1dSRodney W. Grimes 		}
1370df8bae1dSRodney W. Grimes 		break;
1371df8bae1dSRodney W. Grimes 
1372cfe8b629SGarrett Wollman 	case SOPT_GET:
1373cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1374df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1375df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1376e5e3e746SMatt Macy 			INP_RLOCK(inp);
1377e5e3e746SMatt Macy 			if (inp->inp_options) {
1378e5e3e746SMatt Macy 				struct mbuf *options;
1379e5e3e746SMatt Macy 
138010731c54SMichael Tuexen 				options = m_copym(inp->inp_options, 0,
138110731c54SMichael Tuexen 				    M_COPYALL, M_NOWAIT);
1382e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1383e5e3e746SMatt Macy 				if (options != NULL) {
1384cfe8b629SGarrett Wollman 					error = sooptcopyout(sopt,
1385e5e3e746SMatt Macy 							     mtod(options, char *),
1386e5e3e746SMatt Macy 							     options->m_len);
1387e5e3e746SMatt Macy 					m_freem(options);
1388e5e3e746SMatt Macy 				} else
1389e5e3e746SMatt Macy 					error = ENOMEM;
1390e5e3e746SMatt Macy 			} else {
1391e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1392cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1393e5e3e746SMatt Macy 			}
1394df8bae1dSRodney W. Grimes 			break;
1395df8bae1dSRodney W. Grimes 
1396df8bae1dSRodney W. Grimes 		case IP_TOS:
1397df8bae1dSRodney W. Grimes 		case IP_TTL:
1398936cd18dSAndre Oppermann 		case IP_MINTTL:
1399df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1400df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1401dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1402df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
14034957466bSMatthew N. Dodd 		case IP_RECVTTL:
140482c23ebaSBill Fenner 		case IP_RECVIF:
1405cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
14068afa2304SBruce M Simpson 		case IP_ONESBCAST:
1407b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
14085f6bf451SAttilio Rao 		case IP_BINDANY:
14093cca425bSMichael Tuexen 		case IP_RECVTOS:
14100a100a6fSAdrian Chadd 		case IP_BINDMULTI:
14119c423972SAdrian Chadd 		case IP_FLOWID:
14129c423972SAdrian Chadd 		case IP_FLOWTYPE:
14139d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
14140a100a6fSAdrian Chadd #ifdef	RSS
14150a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
14169d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
14170a100a6fSAdrian Chadd #endif
1418cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1419df8bae1dSRodney W. Grimes 
1420df8bae1dSRodney W. Grimes 			case IP_TOS:
1421ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1422df8bae1dSRodney W. Grimes 				break;
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes 			case IP_TTL:
1425ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1426df8bae1dSRodney W. Grimes 				break;
1427df8bae1dSRodney W. Grimes 
1428936cd18dSAndre Oppermann 			case IP_MINTTL:
1429936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1430936cd18dSAndre Oppermann 				break;
1431936cd18dSAndre Oppermann 
1432df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
14330a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1434df8bae1dSRodney W. Grimes 
1435df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1436df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1437df8bae1dSRodney W. Grimes 				break;
1438df8bae1dSRodney W. Grimes 
1439df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1440df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1441df8bae1dSRodney W. Grimes 				break;
1442df8bae1dSRodney W. Grimes 
1443df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1444df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1445df8bae1dSRodney W. Grimes 				break;
144682c23ebaSBill Fenner 
1447dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1448dce33a45SErmal Luçi 				optval = OPTBIT2(INP_ORIGDSTADDR);
1449dce33a45SErmal Luçi 				break;
1450dce33a45SErmal Luçi 
14514957466bSMatthew N. Dodd 			case IP_RECVTTL:
14524957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
14534957466bSMatthew N. Dodd 				break;
14544957466bSMatthew N. Dodd 
145582c23ebaSBill Fenner 			case IP_RECVIF:
145682c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
145782c23ebaSBill Fenner 				break;
1458cfe8b629SGarrett Wollman 
1459cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1460cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1461cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1462cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1463cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1464cfe8b629SGarrett Wollman 				else
1465cfe8b629SGarrett Wollman 					optval = 0;
1466cfe8b629SGarrett Wollman 				break;
14676a800098SYoshinobu Inoue 
14688afa2304SBruce M Simpson 			case IP_ONESBCAST:
14698afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
14708afa2304SBruce M Simpson 				break;
1471b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1472b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1473b2828ad2SAndre Oppermann 				break;
14745f6bf451SAttilio Rao 			case IP_BINDANY:
14755f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
14765f6bf451SAttilio Rao 				break;
14773cca425bSMichael Tuexen 			case IP_RECVTOS:
14783cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
14793cca425bSMichael Tuexen 				break;
14809c423972SAdrian Chadd 			case IP_FLOWID:
14819c423972SAdrian Chadd 				optval = inp->inp_flowid;
14829c423972SAdrian Chadd 				break;
14839c423972SAdrian Chadd 			case IP_FLOWTYPE:
14849c423972SAdrian Chadd 				optval = inp->inp_flowtype;
14859c423972SAdrian Chadd 				break;
14869d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
14879d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
14889d3ddf43SAdrian Chadd 				break;
14899c423972SAdrian Chadd #ifdef	RSS
14907847796aSAdrian Chadd 			case IP_RSSBUCKETID:
14917847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
14927847796aSAdrian Chadd 				    inp->inp_flowtype,
14937847796aSAdrian Chadd 				    &rss_bucket);
14947847796aSAdrian Chadd 				if (retval == 0)
14957847796aSAdrian Chadd 					optval = rss_bucket;
14967847796aSAdrian Chadd 				else
14977847796aSAdrian Chadd 					error = EINVAL;
14989c423972SAdrian Chadd 				break;
14999d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
15009d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
15019d3ddf43SAdrian Chadd 				break;
15029c423972SAdrian Chadd #endif
15030a100a6fSAdrian Chadd 			case IP_BINDMULTI:
15040a100a6fSAdrian Chadd 				optval = OPTBIT2(INP_BINDMULTI);
15050a100a6fSAdrian Chadd 				break;
1506df8bae1dSRodney W. Grimes 			}
1507cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1508df8bae1dSRodney W. Grimes 			break;
1509df8bae1dSRodney W. Grimes 
151071498f30SBruce M Simpson 		/*
151171498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
151271498f30SBruce M Simpson 		 * module.
151371498f30SBruce M Simpson 		 */
1514df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1515f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1516df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1517df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
151871498f30SBruce M Simpson 		case IP_MSFILTER:
151971498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
152033b3ac06SPeter Wemm 			break;
152133b3ac06SPeter Wemm 
1522fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
15236a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1524fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1525fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
15266a800098SYoshinobu Inoue 				break;
15276a800098SYoshinobu Inoue 			}
1528fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1529b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
15306a800098SYoshinobu Inoue 
1531df8bae1dSRodney W. Grimes 		default:
1532df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1533df8bae1dSRodney W. Grimes 			break;
1534df8bae1dSRodney W. Grimes 		}
1535df8bae1dSRodney W. Grimes 		break;
1536df8bae1dSRodney W. Grimes 	}
1537df8bae1dSRodney W. Grimes 	return (error);
1538df8bae1dSRodney W. Grimes }
1539df8bae1dSRodney W. Grimes 
1540df8bae1dSRodney W. Grimes /*
1541df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1542df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1543df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1544f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1545f5fea3ddSPaul Traina  * replicating that code here.
1546df8bae1dSRodney W. Grimes  */
1547df8bae1dSRodney W. Grimes static void
1548331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen)
1549df8bae1dSRodney W. Grimes {
1550331dff07SAlexander V. Chernikov 	struct ip *ip;
1551df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1552df8bae1dSRodney W. Grimes 
1553e4762f75SGeorge V. Neville-Neil 	/*
1554e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1555e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1556e4762f75SGeorge V. Neville-Neil 	 */
1557eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
1558f0cace5dSRobert Watson 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
155986b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1560df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1561390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1562390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1563390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1564390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1565390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1566390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1567390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1568390cdc6aSRuslan Ermilov 		}
1569df8bae1dSRodney W. Grimes 		/*
1570df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1571df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1572df8bae1dSRodney W. Grimes 		 */
15738f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1574df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
157586b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1576331dff07SAlexander V. Chernikov 		if_simloop(ifp, copym, AF_INET, 0);
1577df8bae1dSRodney W. Grimes 	}
1578df8bae1dSRodney W. Grimes }
1579