xref: /freebsd/sys/netinet/ip_output.c (revision 50575ce11cbe09fd4d7cfe1e11d8dcca209515b9)
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"
38f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
396a800098SYoshinobu Inoue #include "opt_ipsec.h"
4053dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
41e440aed9SQing Li #include "opt_mpath.h"
4257f60867SMark Johnston #include "opt_route.h"
432f4afd21SRandall Stewart #include "opt_sctp.h"
449c423972SAdrian Chadd #include "opt_rss.h"
45fbd1372aSJoerg Wunsch 
46df8bae1dSRodney W. Grimes #include <sys/param.h>
4726f9a767SRodney W. Grimes #include <sys/systm.h>
48f0f6d643SLuigi Rizzo #include <sys/kernel.h>
49cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
50df8bae1dSRodney W. Grimes #include <sys/malloc.h>
51df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
52acd3428bSRobert Watson #include <sys/priv.h>
53c26fe973SBjoern A. Zeeb #include <sys/proc.h>
54df8bae1dSRodney W. Grimes #include <sys/protosw.h>
55cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
5657f60867SMark Johnston #include <sys/sdt.h>
57df8bae1dSRodney W. Grimes #include <sys/socket.h>
58df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
599d9edc56SMike Silbersack #include <sys/sysctl.h>
60c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <net/if.h>
6376039bc8SGleb Smirnoff #include <net/if_var.h>
643ef5e21dSQing Li #include <net/if_llatbl.h>
659b932e9eSAndre Oppermann #include <net/netisr.h>
66c21fd232SAndre Oppermann #include <net/pfil.h>
67df8bae1dSRodney W. Grimes #include <net/route.h>
68e440aed9SQing Li #ifdef RADIX_MPATH
69e440aed9SQing Li #include <net/radix_mpath.h>
70e440aed9SQing Li #endif
71b2bdc62aSAdrian Chadd #include <net/rss_config.h>
724b79449eSBjoern A. Zeeb #include <net/vnet.h>
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes #include <netinet/in.h>
7557f60867SMark Johnston #include <netinet/in_kdtrace.h>
76df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
77df8bae1dSRodney W. Grimes #include <netinet/ip.h>
78df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
799c423972SAdrian Chadd #include <netinet/in_rss.h>
80df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
81df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
82ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
831fdbfb90STom Jones 
841fdbfb90STom Jones #include <netinet/udp.h>
851fdbfb90STom Jones #include <netinet/udp_var.h>
861fdbfb90STom Jones 
872f4afd21SRandall Stewart #ifdef SCTP
882f4afd21SRandall Stewart #include <netinet/sctp.h>
892f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
902f4afd21SRandall Stewart #endif
91df8bae1dSRodney W. Grimes 
92fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h>
936a800098SYoshinobu Inoue 
941dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
951dfcf0d2SAndre Oppermann 
96aed55708SRobert Watson #include <security/mac/mac_framework.h>
97aed55708SRobert Watson 
9853dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
9905b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0;
1009d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
1019d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1029d9edc56SMike Silbersack #endif
1039d9edc56SMike Silbersack 
104331dff07SAlexander V. Chernikov static void	ip_mloopback(struct ifnet *, const struct mbuf *, int);
105afed1b49SDarren Reed 
106afed1b49SDarren Reed 
1078b889dbbSBruce M Simpson extern int in_mcast_loop;
10893e0e116SJulian Elischer extern	struct protosw inetsw[];
10993e0e116SJulian Elischer 
110d9f2a782SErmal Luçi static inline int
1118f980c01SMark Johnston ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, struct inpcb *inp,
112d9f2a782SErmal Luçi     struct sockaddr_in *dst, int *fibnum, int *error)
113d9f2a782SErmal Luçi {
114d9f2a782SErmal Luçi 	struct m_tag *fwd_tag = NULL;
1158f980c01SMark Johnston 	struct mbuf *m;
116d9f2a782SErmal Luçi 	struct in_addr odst;
117d9f2a782SErmal Luçi 	struct ip *ip;
118d9f2a782SErmal Luçi 
1198f980c01SMark Johnston 	m = *mp;
120d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
121d9f2a782SErmal Luçi 
122d9f2a782SErmal Luçi 	/* Run through list of hooks for output packets. */
123d9f2a782SErmal Luçi 	odst.s_addr = ip->ip_dst.s_addr;
124b252313fSGleb Smirnoff 	switch (pfil_run_hooks(V_inet_pfil_head, mp, ifp, PFIL_OUT, inp)) {
125b252313fSGleb Smirnoff 	case PFIL_DROPPED:
126b252313fSGleb Smirnoff 		*error = EPERM;
127b252313fSGleb Smirnoff 		/* FALLTHROUGH */
128b252313fSGleb Smirnoff 	case PFIL_CONSUMED:
129d9f2a782SErmal Luçi 		return 1; /* Finished */
130b252313fSGleb Smirnoff 	case PFIL_PASS:
131b252313fSGleb Smirnoff 		*error = 0;
132b252313fSGleb Smirnoff 	}
133b252313fSGleb Smirnoff 	m = *mp;
134d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
135d9f2a782SErmal Luçi 
136d9f2a782SErmal Luçi 	/* See if destination IP address was changed by packet filter. */
137d9f2a782SErmal Luçi 	if (odst.s_addr != ip->ip_dst.s_addr) {
138d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
139d9f2a782SErmal Luçi 		/* If destination is now ourself drop to ip_input(). */
140d9f2a782SErmal Luçi 		if (in_localip(ip->ip_dst)) {
141d9f2a782SErmal Luçi 			m->m_flags |= M_FASTFWD_OURS;
142d9f2a782SErmal Luçi 			if (m->m_pkthdr.rcvif == NULL)
143d9f2a782SErmal Luçi 				m->m_pkthdr.rcvif = V_loif;
144d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
145d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |=
146d9f2a782SErmal Luçi 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
147d9f2a782SErmal Luçi 				m->m_pkthdr.csum_data = 0xffff;
148d9f2a782SErmal Luçi 			}
149d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
150d9f2a782SErmal Luçi 				CSUM_IP_CHECKED | CSUM_IP_VALID;
151d9f2a782SErmal Luçi #ifdef SCTP
152d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
153d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
154d9f2a782SErmal Luçi #endif
155d9f2a782SErmal Luçi 			*error = netisr_queue(NETISR_IP, m);
156d9f2a782SErmal Luçi 			return 1; /* Finished */
157d9f2a782SErmal Luçi 		}
158d9f2a782SErmal Luçi 
159d9f2a782SErmal Luçi 		bzero(dst, sizeof(*dst));
160d9f2a782SErmal Luçi 		dst->sin_family = AF_INET;
161d9f2a782SErmal Luçi 		dst->sin_len = sizeof(*dst);
162d9f2a782SErmal Luçi 		dst->sin_addr = ip->ip_dst;
163d9f2a782SErmal Luçi 
164d9f2a782SErmal Luçi 		return -1; /* Reloop */
165d9f2a782SErmal Luçi 	}
166d9f2a782SErmal Luçi 	/* See if fib was changed by packet filter. */
167d9f2a782SErmal Luçi 	if ((*fibnum) != M_GETFIB(m)) {
168d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
169d9f2a782SErmal Luçi 		*fibnum = M_GETFIB(m);
170d9f2a782SErmal Luçi 		return -1; /* Reloop for FIB change */
171d9f2a782SErmal Luçi 	}
172d9f2a782SErmal Luçi 
173d9f2a782SErmal Luçi 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
174d9f2a782SErmal Luçi 	if (m->m_flags & M_FASTFWD_OURS) {
175d9f2a782SErmal Luçi 		if (m->m_pkthdr.rcvif == NULL)
176d9f2a782SErmal Luçi 			m->m_pkthdr.rcvif = V_loif;
177d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
178d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
179d9f2a782SErmal Luçi 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
180d9f2a782SErmal Luçi 			m->m_pkthdr.csum_data = 0xffff;
181d9f2a782SErmal Luçi 		}
182d9f2a782SErmal Luçi #ifdef SCTP
183d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
184d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
185d9f2a782SErmal Luçi #endif
186d9f2a782SErmal Luçi 		m->m_pkthdr.csum_flags |=
187d9f2a782SErmal Luçi 			CSUM_IP_CHECKED | CSUM_IP_VALID;
188d9f2a782SErmal Luçi 
189d9f2a782SErmal Luçi 		*error = netisr_queue(NETISR_IP, m);
190d9f2a782SErmal Luçi 		return 1; /* Finished */
191d9f2a782SErmal Luçi 	}
192d9f2a782SErmal Luçi 	/* Or forward to some other address? */
193d9f2a782SErmal Luçi 	if ((m->m_flags & M_IP_NEXTHOP) &&
194d9f2a782SErmal Luçi 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
195d9f2a782SErmal Luçi 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
196d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
197d9f2a782SErmal Luçi 		m->m_flags &= ~M_IP_NEXTHOP;
198d9f2a782SErmal Luçi 		m_tag_delete(m, fwd_tag);
199d9f2a782SErmal Luçi 
200d9f2a782SErmal Luçi 		return -1; /* Reloop for CHANGE of dst */
201d9f2a782SErmal Luçi 	}
202d9f2a782SErmal Luçi 
203d9f2a782SErmal Luçi 	return 0;
204d9f2a782SErmal Luçi }
205d9f2a782SErmal Luçi 
206df8bae1dSRodney W. Grimes /*
207df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
208df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
209df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
210df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
211bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
212bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
213bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
214bf984051SGleb Smirnoff  *
2153de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
2163de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
217df8bae1dSRodney W. Grimes  */
218df8bae1dSRodney W. Grimes int
219f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
220f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
221df8bae1dSRodney W. Grimes {
222cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
223a68cc388SGleb Smirnoff 	struct epoch_tracker et;
2241e78ac21SJeffrey Hsu 	struct ip *ip;
225fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
226ac9d7e26SMax Laier 	struct mbuf *m0;
22723bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
2283ae2ad08SJohn-Mark Gurney 	int mtu;
229ca8b83b0SLuigi Rizzo 	int error = 0;
230ca8b83b0SLuigi Rizzo 	struct sockaddr_in *dst;
2314c7a6059SGleb Smirnoff 	const struct sockaddr_in *gw;
2323c73180fSGleb Smirnoff 	struct in_ifaddr *ia;
23321d172a3SGleb Smirnoff 	int isbroadcast;
234078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
235e3f406b3SRuslan Ermilov 	struct route iproute;
236ec396e61SLuigi Rizzo 	struct rtentry *rte;	/* cache for ro->ro_rt */
2379c57a5b6SHiroki Sato 	uint32_t fibnum;
238fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2391a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
2401a499816SEdward Tomasz Napierala #endif
241ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
24236e8826fSMax Laier 
243bc97ba51SJulian Elischer 	if (inp != NULL) {
244baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
24562e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
246c2529042SHans Petter Selasky 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
24780cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
2489c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
24980cb9f21SKip Macy 		}
250*50575ce1SAndrew Gallatin #ifdef NUMA
251*50575ce1SAndrew Gallatin 		m->m_pkthdr.numa_domain = inp->inp_numa_domain;
252*50575ce1SAndrew Gallatin #endif
253bc97ba51SJulian Elischer 	}
25462e1ba83SRobert Watson 
25562e1ba83SRobert Watson 	if (ro == NULL) {
25662e1ba83SRobert Watson 		ro = &iproute;
25762e1ba83SRobert Watson 		bzero(ro, sizeof (*ro));
2588c1960d5SMike Karels 	}
25962e1ba83SRobert Watson 
260df8bae1dSRodney W. Grimes 	if (opt) {
261ca8b83b0SLuigi Rizzo 		int len = 0;
262df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
263cb7641e8SMaxim Konovalov 		if (len != 0)
264ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
265df8bae1dSRodney W. Grimes 	}
266df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
2678f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
2688f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
2693efc3014SJulian Elischer 
270df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
27153be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
27253be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
2736d947416SGleb Smirnoff 		ip_fillid(ip);
274df8bae1dSRodney W. Grimes 	} else {
275ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
27653be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
277df8bae1dSRodney W. Grimes 	}
2783924dfa7SMichael Tuexen 	if ((flags & IP_FORWARDING) == 0)
2793924dfa7SMichael Tuexen 		IPSTAT_INC(ips_localout);
2809c9137eaSGarrett Wollman 
281054692a4SAlexander V. Chernikov 	/*
282054692a4SAlexander V. Chernikov 	 * dst/gw handling:
283054692a4SAlexander V. Chernikov 	 *
2843c065f2fSGleb Smirnoff 	 * dst can be rewritten but always points to &ro->ro_dst.
2853c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
2863c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
287054692a4SAlexander V. Chernikov 	 */
2884c7a6059SGleb Smirnoff 	gw = dst = (struct sockaddr_in *)&ro->ro_dst;
2899c57a5b6SHiroki Sato 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
290d9f2a782SErmal Luçi 	rte = ro->ro_rt;
29184cc0778SGeorge V. Neville-Neil 	if (rte == NULL) {
292a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
293df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
294df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2959b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
296df8bae1dSRodney W. Grimes 	}
297a68cc388SGleb Smirnoff 	NET_EPOCH_ENTER(et);
298d9f2a782SErmal Luçi again:
29984cc0778SGeorge V. Neville-Neil 	/*
30084cc0778SGeorge V. Neville-Neil 	 * Validate route against routing table additions;
30184cc0778SGeorge V. Neville-Neil 	 * a better/more specific route might have been added.
30284cc0778SGeorge V. Neville-Neil 	 */
30384cc0778SGeorge V. Neville-Neil 	if (inp)
30484cc0778SGeorge V. Neville-Neil 		RT_VALIDATE(ro, &inp->inp_rt_cookie, fibnum);
30584cc0778SGeorge V. Neville-Neil 	/*
30684cc0778SGeorge V. Neville-Neil 	 * If there is a cached route,
30784cc0778SGeorge V. Neville-Neil 	 * check that it is to the same destination
30884cc0778SGeorge V. Neville-Neil 	 * and is still up.  If not, free it and try again.
30984cc0778SGeorge V. Neville-Neil 	 * The address family should also be checked in case of sharing the
31084cc0778SGeorge V. Neville-Neil 	 * cache with IPv6.
31184cc0778SGeorge V. Neville-Neil 	 * Also check whether routing cache needs invalidation.
31284cc0778SGeorge V. Neville-Neil 	 */
31384cc0778SGeorge V. Neville-Neil 	rte = ro->ro_rt;
31484cc0778SGeorge V. Neville-Neil 	if (rte && ((rte->rt_flags & RTF_UP) == 0 ||
31584cc0778SGeorge V. Neville-Neil 		    rte->rt_ifp == NULL ||
31684cc0778SGeorge V. Neville-Neil 		    !RT_LINK_IS_UP(rte->rt_ifp) ||
31784cc0778SGeorge V. Neville-Neil 			  dst->sin_family != AF_INET ||
31884cc0778SGeorge V. Neville-Neil 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
319fc21c53fSRyan Stone 		RO_INVALIDATE_CACHE(ro);
320fc21c53fSRyan Stone 		rte = NULL;
32184cc0778SGeorge V. Neville-Neil 	}
322d9f2a782SErmal Luçi 	ia = NULL;
323df8bae1dSRodney W. Grimes 	/*
324a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
325a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
326a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
327a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
328df8bae1dSRodney W. Grimes 	 */
329a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
3304f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
33158a39d8cSAlan Somers 						      M_GETFIB(m)))) == NULL &&
3324f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
33358a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL) {
33486425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
335a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
336a3fd02d8SBruce M Simpson 			goto bad;
337a3fd02d8SBruce M Simpson 		}
338a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
339a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
340a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
341a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
342a3fd02d8SBruce M Simpson 		isbroadcast = 1;
343a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
3444f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
34558a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL &&
3464f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
34758a39d8cSAlan Somers 						M_GETFIB(m)))) == NULL) {
34886425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
349df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
350df8bae1dSRodney W. Grimes 			goto bad;
351df8bae1dSRodney W. Grimes 		}
352df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
353df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
3546c1bd558SRyan Stone 		isbroadcast = ifp->if_flags & IFF_BROADCAST ?
3556c1bd558SRyan Stone 		    in_ifaddr_broadcast(dst->sin_addr, ia) : 0;
3563afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
35738c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
3583afefa39SDaniel C. Sobral 		/*
35938c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
36038c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
3613afefa39SDaniel C. Sobral 		 */
36238c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
363cc0a3c8cSAndrey V. Elsukov 		IFP_TO_IA(ifp, ia, &in_ifa_tracker);
36438c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
365df8bae1dSRodney W. Grimes 	} else {
3662c17fe93SGarrett Wollman 		/*
36797d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
36897d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
36997d8d152SAndre Oppermann 		 * operation (as it is for ARP).
3702c17fe93SGarrett Wollman 		 */
371ec396e61SLuigi Rizzo 		if (rte == NULL) {
372e440aed9SQing Li #ifdef RADIX_MPATH
3738b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
3748b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
3759c57a5b6SHiroki Sato 			    fibnum);
376e440aed9SQing Li #else
3779c57a5b6SHiroki Sato 			in_rtalloc_ign(ro, 0, fibnum);
378e440aed9SQing Li #endif
379ec396e61SLuigi Rizzo 			rte = ro->ro_rt;
380ec396e61SLuigi Rizzo 		}
381c7ea0aa6SQing Li 		if (rte == NULL ||
382d9f2a782SErmal Luçi 		    (rte->rt_flags & RTF_UP) == 0 ||
383c7ea0aa6SQing Li 		    rte->rt_ifp == NULL ||
384c7ea0aa6SQing Li 		    !RT_LINK_IS_UP(rte->rt_ifp)) {
385fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
3861a499816SEdward Tomasz Napierala 			/*
3871a499816SEdward Tomasz Napierala 			 * There is no route for this packet, but it is
3881a499816SEdward Tomasz Napierala 			 * possible that a matching SPD entry exists.
3891a499816SEdward Tomasz Napierala 			 */
3901a499816SEdward Tomasz Napierala 			no_route_but_check_spd = 1;
3911a499816SEdward Tomasz Napierala 			mtu = 0; /* Silence GCC warning. */
3921a499816SEdward Tomasz Napierala 			goto sendit;
3931a499816SEdward Tomasz Napierala #endif
39486425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
395df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
396df8bae1dSRodney W. Grimes 			goto bad;
397df8bae1dSRodney W. Grimes 		}
398ec396e61SLuigi Rizzo 		ia = ifatoia(rte->rt_ifa);
399ec396e61SLuigi Rizzo 		ifp = rte->rt_ifp;
400e3a7aa6fSGleb Smirnoff 		counter_u64_add(rte->rt_pksent, 1);
40136402a68SAlexander V. Chernikov 		rt_update_ro_flags(ro);
402ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_GATEWAY)
4034c7a6059SGleb Smirnoff 			gw = (struct sockaddr_in *)rte->rt_gateway;
404ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_HOST)
405ec396e61SLuigi Rizzo 			isbroadcast = (rte->rt_flags & RTF_BROADCAST);
4066c1bd558SRyan Stone 		else if (ifp->if_flags & IFF_BROADCAST)
40790cc51a1SRyan Stone 			isbroadcast = in_ifaddr_broadcast(gw->sin_addr, ia);
4086c1bd558SRyan Stone 		else
4096c1bd558SRyan Stone 			isbroadcast = 0;
410df8bae1dSRodney W. Grimes 	}
411d9f2a782SErmal Luçi 
4123ae2ad08SJohn-Mark Gurney 	/*
4133ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
4143ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
4153ae2ad08SJohn-Mark Gurney 	 */
4167f948f12SAlexander V. Chernikov 	if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST)))
417e3a7aa6fSGleb Smirnoff 		mtu = rte->rt_mtu;
4187f948f12SAlexander V. Chernikov 	else
4193ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
420c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
421c744cde4SBjoern A. Zeeb 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p",
422c744cde4SBjoern A. Zeeb 	    __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp));
423d9f2a782SErmal Luçi 
4249b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
425df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
426df8bae1dSRodney W. Grimes 		/*
427183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
428183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
429183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
430183e1c86SGleb Smirnoff 		 */
431183e1c86SGleb Smirnoff 		gw = dst;
432183e1c86SGleb Smirnoff 		/*
433df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
434df8bae1dSRodney W. Grimes 		 */
435df8bae1dSRodney W. Grimes 		if (imo != NULL) {
436df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
4371c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
4381c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
439bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
440bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
441bbb4330bSLuigi Rizzo 				    INADDR_ANY;
442df8bae1dSRodney W. Grimes 		} else
443df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
444df8bae1dSRodney W. Grimes 		/*
445df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
446df8bae1dSRodney W. Grimes 		 */
4471c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
448df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
44986425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
450df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
451df8bae1dSRodney W. Grimes 				goto bad;
452df8bae1dSRodney W. Grimes 			}
4531c5de19aSGarrett Wollman 		}
454df8bae1dSRodney W. Grimes 		/*
455df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
456df8bae1dSRodney W. Grimes 		 * of outgoing interface.
457df8bae1dSRodney W. Grimes 		 */
458df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
45938c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
46038c1bc35SRuslan Ermilov 			if (ia != NULL)
46138c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
462df8bae1dSRodney W. Grimes 		}
463df8bae1dSRodney W. Grimes 
4648b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
4658b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
466df8bae1dSRodney W. Grimes 			/*
4678b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
4688b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
4698b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
4708b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
4718b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
472df8bae1dSRodney W. Grimes 			 */
473331dff07SAlexander V. Chernikov 			ip_mloopback(ifp, m, hlen);
4748b889dbbSBruce M Simpson 		} else {
475df8bae1dSRodney W. Grimes 			/*
476df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
477df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
478df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
479df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
480df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
481df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
482df8bae1dSRodney W. Grimes 			 *
483df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
484df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
485df8bae1dSRodney W. Grimes 			 * if necessary.
486df8bae1dSRodney W. Grimes 			 */
487603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
488f0068c4aSGarrett Wollman 				/*
489bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
490f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
491f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
492f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
493f0068c4aSGarrett Wollman 				 */
494603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
495f0068c4aSGarrett Wollman 					imo = NULL;
496bbb4330bSLuigi Rizzo 				if (ip_mforward &&
497bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
498df8bae1dSRodney W. Grimes 					m_freem(m);
499df8bae1dSRodney W. Grimes 					goto done;
500df8bae1dSRodney W. Grimes 				}
501df8bae1dSRodney W. Grimes 			}
502df8bae1dSRodney W. Grimes 		}
5035e9ae478SGarrett Wollman 
504df8bae1dSRodney W. Grimes 		/*
505df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
506df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
507df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
508df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
5098b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
5108b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
5118b889dbbSBruce M Simpson 		 * the loopback interface.
512df8bae1dSRodney W. Grimes 		 */
513f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
514df8bae1dSRodney W. Grimes 			m_freem(m);
515df8bae1dSRodney W. Grimes 			goto done;
516df8bae1dSRodney W. Grimes 		}
517df8bae1dSRodney W. Grimes 
518df8bae1dSRodney W. Grimes 		goto sendit;
519df8bae1dSRodney W. Grimes 	}
5206a7c943cSAndre Oppermann 
521df8bae1dSRodney W. Grimes 	/*
5222b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
523cb459254SJohn-Mark Gurney 	 * of the outoing interface.
524df8bae1dSRodney W. Grimes 	 */
525f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
52638c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
52738c1bc35SRuslan Ermilov 		if (ia != NULL) {
528df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
529f9e354dfSJulian Elischer 		}
53038c1bc35SRuslan Ermilov 	}
5316a7c943cSAndre Oppermann 
532ca7a789aSMax Laier 	/*
533df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
5348c3f5566SRuslan Ermilov 	 * verify user is allowed to send
535df8bae1dSRodney W. Grimes 	 * such a packet.
536df8bae1dSRodney W. Grimes 	 */
5379f9b3dc4SGarrett Wollman 	if (isbroadcast) {
538df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
539df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
540df8bae1dSRodney W. Grimes 			goto bad;
541df8bae1dSRodney W. Grimes 		}
542df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
543df8bae1dSRodney W. Grimes 			error = EACCES;
544df8bae1dSRodney W. Grimes 			goto bad;
545df8bae1dSRodney W. Grimes 		}
546df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
5478f134647SGleb Smirnoff 		if (ip_len > mtu) {
548df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
549df8bae1dSRodney W. Grimes 			goto bad;
550df8bae1dSRodney W. Grimes 		}
551df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
5529f9b3dc4SGarrett Wollman 	} else {
553df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
5549f9b3dc4SGarrett Wollman 	}
555df8bae1dSRodney W. Grimes 
556f1743588SDarren Reed sendit:
557fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
558fcf59617SAndrey V. Elsukov 	if (IPSEC_ENABLED(ipv4)) {
559fcf59617SAndrey V. Elsukov 		if ((error = IPSEC_OUTPUT(ipv4, m, inp)) != 0) {
560fcf59617SAndrey V. Elsukov 			if (error == EINPROGRESS)
561fcf59617SAndrey V. Elsukov 				error = 0;
5621dfcf0d2SAndre Oppermann 			goto done;
563fcf59617SAndrey V. Elsukov 		}
56433841545SHajimu UMEMOTO 	}
5651a499816SEdward Tomasz Napierala 	/*
5661a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
5671a499816SEdward Tomasz Napierala 	 */
5681a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
5691a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
5701a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
5711a499816SEdward Tomasz Napierala 		goto bad;
5721a499816SEdward Tomasz Napierala 	}
5731dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
57433841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
57533841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
576b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
57733841545SHajimu UMEMOTO 
578c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
579b252313fSGleb Smirnoff 	if (PFIL_HOOKED_OUT(V_inet_pfil_head)) {
5808f980c01SMark Johnston 		switch (ip_output_pfil(&m, ifp, inp, dst, &fibnum, &error)) {
581d9f2a782SErmal Luçi 		case 1: /* Finished */
582c4ac87eaSDarren Reed 			goto done;
5839b932e9eSAndre Oppermann 
584d9f2a782SErmal Luçi 		case 0: /* Continue normally */
585c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
586d9f2a782SErmal Luçi 			break;
587fed1c7e9SSøren Schmidt 
588d9f2a782SErmal Luçi 		case -1: /* Need to try again */
589d9f2a782SErmal Luçi 			/* Reset everything for a new round */
5909c57a5b6SHiroki Sato 			RO_RTFREE(ro);
5914fb3a820SAlexander V. Chernikov 			ro->ro_prepend = NULL;
592d9f2a782SErmal Luçi 			rte = NULL;
593d9f2a782SErmal Luçi 			gw = dst;
594d9f2a782SErmal Luçi 			ip = mtod(m, struct ip *);
5959b932e9eSAndre Oppermann 			goto again;
596d9f2a782SErmal Luçi 
597d9f2a782SErmal Luçi 		}
598099dd043SAndre Oppermann 	}
5992b25acc1SLuigi Rizzo 
6006c1c6ae5SRodney W. Grimes 	/* IN_LOOPBACK must not appear on the wire - RFC1122. */
6016c1c6ae5SRodney W. Grimes 	if (IN_LOOPBACK(ntohl(ip->ip_dst.s_addr)) ||
6026c1c6ae5SRodney W. Grimes 	    IN_LOOPBACK(ntohl(ip->ip_src.s_addr))) {
60351c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
60486425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
60551c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
60651c8ec4aSRuslan Ermilov 			goto bad;
60751c8ec4aSRuslan Ermilov 		}
60851c8ec4aSRuslan Ermilov 	}
60951c8ec4aSRuslan Ermilov 
610206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
611078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
612db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
613078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
614db4f9cc7SJonathan Lemon 	}
6152f4afd21SRandall Stewart #ifdef SCTP
616078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
6171966e5b5SRandall Stewart 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
618078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
6192f4afd21SRandall Stewart 	}
6202f4afd21SRandall Stewart #endif
621db4f9cc7SJonathan Lemon 
622e7319babSPoul-Henning Kamp 	/*
623db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
624233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
625df8bae1dSRodney W. Grimes 	 */
62621d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
627bf7dcda3SGleb Smirnoff 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
628df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
629078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
630df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
631078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
632078468edSGleb Smirnoff 		}
6335da9f8faSJosef Karthauser 
634233dcce1SAndre Oppermann 		/*
635233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
636233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
637233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
638233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
639233dcce1SAndre Oppermann 		 */
64038c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
641233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
6427caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
6437caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
644233dcce1SAndre Oppermann 			else
6457caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
6467caf4ab7SGleb Smirnoff 
6477caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
6485da9f8faSJosef Karthauser 		}
64953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
6503390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
651eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
6529d9edc56SMike Silbersack #endif
653147f74d1SAndre Oppermann 		/*
654147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
655147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
656147f74d1SAndre Oppermann 		 */
65786bd0491SAndre Oppermann 		m_clrprotoflags(m);
65857f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
659f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
660f3e7afe2SHans Petter Selasky 		if (inp != NULL) {
661f3e7afe2SHans Petter Selasky 			if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
662f3e7afe2SHans Petter Selasky 				in_pcboutput_txrtlmt(inp, ifp, m);
663f3e7afe2SHans Petter Selasky 			/* stamp send tag on mbuf */
664f3e7afe2SHans Petter Selasky 			m->m_pkthdr.snd_tag = inp->inp_snd_tag;
665f3e7afe2SHans Petter Selasky 		} else {
666f3e7afe2SHans Petter Selasky 			m->m_pkthdr.snd_tag = NULL;
667f3e7afe2SHans Petter Selasky 		}
668f3e7afe2SHans Petter Selasky #endif
66947e8d432SGleb Smirnoff 		error = (*ifp->if_output)(ifp, m,
67047e8d432SGleb Smirnoff 		    (const struct sockaddr *)gw, ro);
671f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
672f3e7afe2SHans Petter Selasky 		/* check for route change */
673f3e7afe2SHans Petter Selasky 		if (error == EAGAIN)
674f3e7afe2SHans Petter Selasky 			in_pcboutput_eagain(inp);
675f3e7afe2SHans Petter Selasky #endif
676df8bae1dSRodney W. Grimes 		goto done;
677df8bae1dSRodney W. Grimes 	}
6781e78ac21SJeffrey Hsu 
679233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
68021d172a3SGleb Smirnoff 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
681df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
68286425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
683df8bae1dSRodney W. Grimes 		goto bad;
684df8bae1dSRodney W. Grimes 	}
6851e78ac21SJeffrey Hsu 
6861e78ac21SJeffrey Hsu 	/*
6871e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
6881e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
6891e78ac21SJeffrey Hsu 	 */
690078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
6911e78ac21SJeffrey Hsu 	if (error)
692df8bae1dSRodney W. Grimes 		goto bad;
6931e78ac21SJeffrey Hsu 	for (; m; m = m0) {
694df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
695b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
69607203494SDaniel C. Sobral 		if (error == 0) {
697fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
69807203494SDaniel C. Sobral 			if (ia != NULL) {
6997caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
7007caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
7017caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
70207203494SDaniel C. Sobral 			}
703147f74d1SAndre Oppermann 			/*
704147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
705147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
706147f74d1SAndre Oppermann 			 */
70786bd0491SAndre Oppermann 			m_clrprotoflags(m);
708fe937674SJosef Karthauser 
7092e77d270SAndrey V. Elsukov 			IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp,
7102e77d270SAndrey V. Elsukov 			    mtod(m, struct ip *), NULL);
711f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
712f3e7afe2SHans Petter Selasky 			if (inp != NULL) {
713f3e7afe2SHans Petter Selasky 				if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
714f3e7afe2SHans Petter Selasky 					in_pcboutput_txrtlmt(inp, ifp, m);
715f3e7afe2SHans Petter Selasky 				/* stamp send tag on mbuf */
716f3e7afe2SHans Petter Selasky 				m->m_pkthdr.snd_tag = inp->inp_snd_tag;
717f3e7afe2SHans Petter Selasky 			} else {
718f3e7afe2SHans Petter Selasky 				m->m_pkthdr.snd_tag = NULL;
719f3e7afe2SHans Petter Selasky 			}
720f3e7afe2SHans Petter Selasky #endif
721df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
72247e8d432SGleb Smirnoff 			    (const struct sockaddr *)gw, ro);
723f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
724f3e7afe2SHans Petter Selasky 			/* check for route change */
725f3e7afe2SHans Petter Selasky 			if (error == EAGAIN)
726f3e7afe2SHans Petter Selasky 				in_pcboutput_eagain(inp);
727f3e7afe2SHans Petter Selasky #endif
728fe937674SJosef Karthauser 		} else
729df8bae1dSRodney W. Grimes 			m_freem(m);
730df8bae1dSRodney W. Grimes 	}
731df8bae1dSRodney W. Grimes 
732df8bae1dSRodney W. Grimes 	if (error == 0)
73386425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
7341e78ac21SJeffrey Hsu 
735df8bae1dSRodney W. Grimes done:
736cc94f0c2SGleb Smirnoff 	if (ro == &iproute)
737bf984051SGleb Smirnoff 		RO_RTFREE(ro);
7383c402323SErmal Luçi 	else if (rte == NULL)
7393c402323SErmal Luçi 		/*
7403c402323SErmal Luçi 		 * If the caller supplied a route but somehow the reference
7413c402323SErmal Luçi 		 * to it has been released need to prevent the caller
7423c402323SErmal Luçi 		 * calling RTFREE on it again.
7433c402323SErmal Luçi 		 */
7443c402323SErmal Luçi 		ro->ro_rt = NULL;
745a68cc388SGleb Smirnoff 	NET_EPOCH_EXIT(et);
746df8bae1dSRodney W. Grimes 	return (error);
747df8bae1dSRodney W. Grimes  bad:
7483528d68fSBill Paul 	m_freem(m);
749df8bae1dSRodney W. Grimes 	goto done;
750df8bae1dSRodney W. Grimes }
751df8bae1dSRodney W. Grimes 
7521e78ac21SJeffrey Hsu /*
7531e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
7541e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
7551e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
7561e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
7571e78ac21SJeffrey Hsu  *
7581e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
7591e78ac21SJeffrey Hsu  */
7601e78ac21SJeffrey Hsu int
7611e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
762078468edSGleb Smirnoff     u_long if_hwassist_flags)
7631e78ac21SJeffrey Hsu {
7641e78ac21SJeffrey Hsu 	int error = 0;
7651e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
7661e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
7671e78ac21SJeffrey Hsu 	int off;
7681e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
7691e78ac21SJeffrey Hsu 	int firstlen;
7701e78ac21SJeffrey Hsu 	struct mbuf **mnext;
7711e78ac21SJeffrey Hsu 	int nfrags;
77221d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
7731e78ac21SJeffrey Hsu 
77421d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
77521d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
77621d172a3SGleb Smirnoff 
77721d172a3SGleb Smirnoff 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
77886425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
7791e78ac21SJeffrey Hsu 		return EMSGSIZE;
7801e78ac21SJeffrey Hsu 	}
7811e78ac21SJeffrey Hsu 
7821e78ac21SJeffrey Hsu 	/*
7831e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
7841e78ac21SJeffrey Hsu 	 */
7851e78ac21SJeffrey Hsu 	if (len < 8)
7861e78ac21SJeffrey Hsu 		return EMSGSIZE;
7871e78ac21SJeffrey Hsu 
7881e78ac21SJeffrey Hsu 	/*
7891e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
7901e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
7911e78ac21SJeffrey Hsu 	 */
792da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
7931e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
7941e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
7951e78ac21SJeffrey Hsu 	}
7962f4afd21SRandall Stewart #ifdef SCTP
797da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
7981966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
7992f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
8002f4afd21SRandall Stewart 	}
8012f4afd21SRandall Stewart #endif
8021e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
8031e78ac21SJeffrey Hsu 		/*
8041e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
8051e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
8061e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
8071e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
8081e78ac21SJeffrey Hsu 		 *
8091e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
8101e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
8111e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
8121e78ac21SJeffrey Hsu 		 */
8131e78ac21SJeffrey Hsu 		int newlen;
8141e78ac21SJeffrey Hsu 
8159c0f6aa7SHans Petter Selasky 		off = MIN(mtu, m0->m_pkthdr.len);
8161e78ac21SJeffrey Hsu 
8171e78ac21SJeffrey Hsu 		/*
8181e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
8191e78ac21SJeffrey Hsu 		 * 8-byte boundary
8201e78ac21SJeffrey Hsu 		 */
8211e78ac21SJeffrey Hsu 		if (off < hlen)
8221e78ac21SJeffrey Hsu 			goto smart_frag_failure;
8231e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
8241e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
8251e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
8261e78ac21SJeffrey Hsu 			/* we failed, go back the default */
8271e78ac21SJeffrey Hsu smart_frag_failure:
8281e78ac21SJeffrey Hsu 			newlen = len;
8291e78ac21SJeffrey Hsu 			off = hlen + len;
8301e78ac21SJeffrey Hsu 		}
8311e78ac21SJeffrey Hsu 		len = newlen;
8321e78ac21SJeffrey Hsu 
8331e78ac21SJeffrey Hsu 	} else {
8341e78ac21SJeffrey Hsu 		off = hlen + len;
8351e78ac21SJeffrey Hsu 	}
8361e78ac21SJeffrey Hsu 
8371e78ac21SJeffrey Hsu 	firstlen = off - hlen;
8381e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
8391e78ac21SJeffrey Hsu 
8401e78ac21SJeffrey Hsu 	/*
8411e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
8421e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
8431e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
8441e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
8451e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
8461e78ac21SJeffrey Hsu 	 */
84721d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
8481e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
8491e78ac21SJeffrey Hsu 		struct mbuf *m;
8501e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
8511e78ac21SJeffrey Hsu 
852dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
8530b17fba7SAndre Oppermann 		if (m == NULL) {
8541e78ac21SJeffrey Hsu 			error = ENOBUFS;
85586425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8561e78ac21SJeffrey Hsu 			goto done;
8571e78ac21SJeffrey Hsu 		}
858c4c4346fSHans Petter Selasky 		/*
859c4c4346fSHans Petter Selasky 		 * Make sure the complete packet header gets copied
860c4c4346fSHans Petter Selasky 		 * from the originating mbuf to the newly created
861c4c4346fSHans Petter Selasky 		 * mbuf. This also ensures that existing firewall
862c4c4346fSHans Petter Selasky 		 * classification(s), VLAN tags and so on get copied
863c4c4346fSHans Petter Selasky 		 * to the resulting fragmented packet(s):
864c4c4346fSHans Petter Selasky 		 */
865c4c4346fSHans Petter Selasky 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
866c4c4346fSHans Petter Selasky 			m_free(m);
867c4c4346fSHans Petter Selasky 			error = ENOBUFS;
868c4c4346fSHans Petter Selasky 			IPSTAT_INC(ips_odropped);
869c4c4346fSHans Petter Selasky 			goto done;
870c4c4346fSHans Petter Selasky 		}
8711e78ac21SJeffrey Hsu 		/*
8721e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
8731e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
8748b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
8751e78ac21SJeffrey Hsu 		 */
8761e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
8771e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
8781e78ac21SJeffrey Hsu 		*mhip = *ip;
8791e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
8801e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
8811e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
8821e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
8831e78ac21SJeffrey Hsu 		}
8841e78ac21SJeffrey Hsu 		m->m_len = mhlen;
88521d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
88621d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
887fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
88821d172a3SGleb Smirnoff 			len = ip_len - off;
889fb86dfcdSAndre Oppermann 		else
8901e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
8911e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
892eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
8930b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
8941e78ac21SJeffrey Hsu 			m_free(m);
8951e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
89686425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8971e78ac21SJeffrey Hsu 			goto done;
8981e78ac21SJeffrey Hsu 		}
8991e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
9001e78ac21SJeffrey Hsu #ifdef MAC
90130d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
9021e78ac21SJeffrey Hsu #endif
9031e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
9041e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
905078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
9061e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
907078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
908078468edSGleb Smirnoff 		}
9091e78ac21SJeffrey Hsu 		*mnext = m;
9101e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
9111e78ac21SJeffrey Hsu 	}
91286425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
9131e78ac21SJeffrey Hsu 
9141e78ac21SJeffrey Hsu 	/*
9151e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
9161e78ac21SJeffrey Hsu 	 * and updating header.
9171e78ac21SJeffrey Hsu 	 */
91821d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
9191e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
9201e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
92121d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
9221e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
923078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
9241e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
925078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
926078468edSGleb Smirnoff 	}
9271e78ac21SJeffrey Hsu 
9281e78ac21SJeffrey Hsu done:
9291e78ac21SJeffrey Hsu 	*m_frag = m0;
9301e78ac21SJeffrey Hsu 	return error;
9311e78ac21SJeffrey Hsu }
9321e78ac21SJeffrey Hsu 
9331c238475SJonathan Lemon void
934db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
935db4f9cc7SJonathan Lemon {
936db4f9cc7SJonathan Lemon 	struct ip *ip;
9371fdbfb90STom Jones 	struct udphdr *uh;
9381fdbfb90STom Jones 	uint16_t cklen, csum, offset;
939db4f9cc7SJonathan Lemon 
940db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
94153be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
9421fdbfb90STom Jones 
9431fdbfb90STom Jones 	if (m->m_pkthdr.csum_flags & CSUM_UDP) {
9441fdbfb90STom Jones 		/* if udp header is not in the first mbuf copy udplen */
945b6e87011STom Jones 		if (offset + sizeof(struct udphdr) > m->m_len) {
946590d0a43SAndrey V. Elsukov 			m_copydata(m, offset + offsetof(struct udphdr,
947590d0a43SAndrey V. Elsukov 			    uh_ulen), sizeof(cklen), (caddr_t)&cklen);
948b6e87011STom Jones 			cklen = ntohs(cklen);
949b6e87011STom Jones 		} else {
950590d0a43SAndrey V. Elsukov 			uh = (struct udphdr *)mtodo(m, offset);
9511fdbfb90STom Jones 			cklen = ntohs(uh->uh_ulen);
9521fdbfb90STom Jones 		}
9531fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen + offset, offset);
9541fdbfb90STom Jones 		if (csum == 0)
955206a3274SRuslan Ermilov 			csum = 0xffff;
9561fdbfb90STom Jones 	} else {
9571fdbfb90STom Jones 		cklen = ntohs(ip->ip_len);
9581fdbfb90STom Jones 		csum = in_cksum_skip(m, cklen, offset);
9591fdbfb90STom Jones 	}
960db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
961db4f9cc7SJonathan Lemon 
962590d0a43SAndrey V. Elsukov 	if (offset + sizeof(csum) > m->m_len)
963590d0a43SAndrey V. Elsukov 		m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
964590d0a43SAndrey V. Elsukov 	else
965590d0a43SAndrey V. Elsukov 		*(u_short *)mtodo(m, offset) = csum;
966db4f9cc7SJonathan Lemon }
967db4f9cc7SJonathan Lemon 
968df8bae1dSRodney W. Grimes /*
969df8bae1dSRodney W. Grimes  * IP socket option processing.
970df8bae1dSRodney W. Grimes  */
971df8bae1dSRodney W. Grimes int
972f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
973df8bae1dSRodney W. Grimes {
974cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
975cfe8b629SGarrett Wollman 	int	error, optval;
976dc847eb6SAdrian Chadd #ifdef	RSS
977dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
978dc847eb6SAdrian Chadd 	int retval;
979dc847eb6SAdrian Chadd #endif
980df8bae1dSRodney W. Grimes 
981cfe8b629SGarrett Wollman 	error = optval = 0;
982cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
983fc06cd42SMikolaj Golub 		error = EINVAL;
984fc06cd42SMikolaj Golub 
985fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
986fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
987fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
988fc06cd42SMikolaj Golub 			case SO_REUSEADDR:
989fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
990efdf104bSMikolaj Golub 				if ((so->so_options & SO_REUSEADDR) != 0)
991efdf104bSMikolaj Golub 					inp->inp_flags2 |= INP_REUSEADDR;
992fc06cd42SMikolaj Golub 				else
993efdf104bSMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEADDR;
994fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
995fc06cd42SMikolaj Golub 				error = 0;
996fc06cd42SMikolaj Golub 				break;
997fc06cd42SMikolaj Golub 			case SO_REUSEPORT:
998fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
999fc06cd42SMikolaj Golub 				if ((so->so_options & SO_REUSEPORT) != 0)
1000fc06cd42SMikolaj Golub 					inp->inp_flags2 |= INP_REUSEPORT;
1001fc06cd42SMikolaj Golub 				else
1002fc06cd42SMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEPORT;
1003fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1004fc06cd42SMikolaj Golub 				error = 0;
1005fc06cd42SMikolaj Golub 				break;
10061a43cff9SSean Bruno 			case SO_REUSEPORT_LB:
10071a43cff9SSean Bruno 				INP_WLOCK(inp);
10081a43cff9SSean Bruno 				if ((so->so_options & SO_REUSEPORT_LB) != 0)
10091a43cff9SSean Bruno 					inp->inp_flags2 |= INP_REUSEPORT_LB;
10101a43cff9SSean Bruno 				else
10111a43cff9SSean Bruno 					inp->inp_flags2 &= ~INP_REUSEPORT_LB;
10121a43cff9SSean Bruno 				INP_WUNLOCK(inp);
10131a43cff9SSean Bruno 				error = 0;
10141a43cff9SSean Bruno 				break;
1015fc06cd42SMikolaj Golub 			case SO_SETFIB:
1016fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1017fc06cd42SMikolaj Golub 				inp->inp_inc.inc_fibnum = so->so_fibnum;
1018fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1019fc06cd42SMikolaj Golub 				error = 0;
1020fc06cd42SMikolaj Golub 				break;
1021f3e7afe2SHans Petter Selasky 			case SO_MAX_PACING_RATE:
1022f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1023f3e7afe2SHans Petter Selasky 				INP_WLOCK(inp);
1024f3e7afe2SHans Petter Selasky 				inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1025f3e7afe2SHans Petter Selasky 				INP_WUNLOCK(inp);
1026f3e7afe2SHans Petter Selasky 				error = 0;
1027f3e7afe2SHans Petter Selasky #else
1028f3e7afe2SHans Petter Selasky 				error = EOPNOTSUPP;
1029f3e7afe2SHans Petter Selasky #endif
1030f3e7afe2SHans Petter Selasky 				break;
1031fc06cd42SMikolaj Golub 			default:
1032fc06cd42SMikolaj Golub 				break;
1033fc06cd42SMikolaj Golub 			}
1034fc06cd42SMikolaj Golub 		}
1035fc06cd42SMikolaj Golub 		return (error);
1036cfe8b629SGarrett Wollman 	}
1037df8bae1dSRodney W. Grimes 
1038cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1039cfe8b629SGarrett Wollman 	case SOPT_SET:
1040cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1041df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1042df8bae1dSRodney W. Grimes #ifdef notyet
1043df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1044df8bae1dSRodney W. Grimes #endif
1045cfe8b629SGarrett Wollman 		{
1046cfe8b629SGarrett Wollman 			struct mbuf *m;
1047cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1048cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1049cfe8b629SGarrett Wollman 				break;
1050cfe8b629SGarrett Wollman 			}
1051dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
10520b17fba7SAndre Oppermann 			if (m == NULL) {
1053cfe8b629SGarrett Wollman 				error = ENOBUFS;
1054cfe8b629SGarrett Wollman 				break;
1055cfe8b629SGarrett Wollman 			}
1056cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1057cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1058cfe8b629SGarrett Wollman 					    m->m_len);
1059635354c4SMaxim Konovalov 			if (error) {
1060635354c4SMaxim Konovalov 				m_free(m);
1061635354c4SMaxim Konovalov 				break;
1062635354c4SMaxim Konovalov 			}
10638501a69cSRobert Watson 			INP_WLOCK(inp);
1064993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
10658501a69cSRobert Watson 			INP_WUNLOCK(inp);
1066993d9505SRobert Watson 			return (error);
1067cfe8b629SGarrett Wollman 		}
1068df8bae1dSRodney W. Grimes 
1069f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
1070f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
1071f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
1072f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
1073f44270e7SPawel Jakub Dawidek 				if (error)
1074be9347e3SAdrian Chadd 					break;
1075be9347e3SAdrian Chadd 			}
1076cef27294SAdrian Chadd 			/* FALLTHROUGH */
10770a100a6fSAdrian Chadd 		case IP_BINDMULTI:
10780a100a6fSAdrian Chadd #ifdef	RSS
10790a100a6fSAdrian Chadd 		case IP_RSS_LISTEN_BUCKET:
10800a100a6fSAdrian Chadd #endif
1081df8bae1dSRodney W. Grimes 		case IP_TOS:
1082df8bae1dSRodney W. Grimes 		case IP_TTL:
1083936cd18dSAndre Oppermann 		case IP_MINTTL:
1084df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1085df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1086dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1087df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
10884957466bSMatthew N. Dodd 		case IP_RECVTTL:
108982c23ebaSBill Fenner 		case IP_RECVIF:
10908afa2304SBruce M Simpson 		case IP_ONESBCAST:
1091b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
10923cca425bSMichael Tuexen 		case IP_RECVTOS:
10939d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
10949d3ddf43SAdrian Chadd #ifdef	RSS
10959d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
10969d3ddf43SAdrian Chadd #endif
1097cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1098cfe8b629SGarrett Wollman 					    sizeof optval);
1099cfe8b629SGarrett Wollman 			if (error)
1100cfe8b629SGarrett Wollman 				break;
1101df8bae1dSRodney W. Grimes 
1102cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1103df8bae1dSRodney W. Grimes 			case IP_TOS:
1104ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1105df8bae1dSRodney W. Grimes 				break;
1106df8bae1dSRodney W. Grimes 
1107df8bae1dSRodney W. Grimes 			case IP_TTL:
1108ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1109df8bae1dSRodney W. Grimes 				break;
1110936cd18dSAndre Oppermann 
1111936cd18dSAndre Oppermann 			case IP_MINTTL:
1112a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1113936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1114936cd18dSAndre Oppermann 				else
1115936cd18dSAndre Oppermann 					error = EINVAL;
1116936cd18dSAndre Oppermann 				break;
1117936cd18dSAndre Oppermann 
1118a138d217SRobert Watson #define	OPTSET(bit) do {						\
11198501a69cSRobert Watson 	INP_WLOCK(inp);							\
1120df8bae1dSRodney W. Grimes 	if (optval)							\
1121df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1122df8bae1dSRodney W. Grimes 	else								\
1123a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
11248501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1125a138d217SRobert Watson } while (0)
1126df8bae1dSRodney W. Grimes 
11270a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
11280a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
11290a100a6fSAdrian Chadd 	if (val)							\
11300a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
11310a100a6fSAdrian Chadd 	else								\
11320a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
11330a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
11340a100a6fSAdrian Chadd } while (0)
11350a100a6fSAdrian Chadd 
1136df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1137df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1138df8bae1dSRodney W. Grimes 				break;
1139df8bae1dSRodney W. Grimes 
1140df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1141df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1142df8bae1dSRodney W. Grimes 				break;
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1145df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1146df8bae1dSRodney W. Grimes 				break;
114782c23ebaSBill Fenner 
1148dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1149dce33a45SErmal Luçi 				OPTSET2(INP_ORIGDSTADDR, optval);
1150dce33a45SErmal Luçi 				break;
1151dce33a45SErmal Luçi 
11524957466bSMatthew N. Dodd 			case IP_RECVTTL:
11534957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
11544957466bSMatthew N. Dodd 				break;
11554957466bSMatthew N. Dodd 
115682c23ebaSBill Fenner 			case IP_RECVIF:
115782c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
115882c23ebaSBill Fenner 				break;
11596a800098SYoshinobu Inoue 
11608afa2304SBruce M Simpson 			case IP_ONESBCAST:
11618afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
11628afa2304SBruce M Simpson 				break;
1163b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1164b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1165b2828ad2SAndre Oppermann 				break;
1166f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1167f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1168be9347e3SAdrian Chadd 				break;
11693cca425bSMichael Tuexen 			case IP_RECVTOS:
11703cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
11713cca425bSMichael Tuexen 				break;
11720a100a6fSAdrian Chadd 			case IP_BINDMULTI:
11730a100a6fSAdrian Chadd 				OPTSET2(INP_BINDMULTI, optval);
11740a100a6fSAdrian Chadd 				break;
11759d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
11769d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
11779d3ddf43SAdrian Chadd 				break;
11780a100a6fSAdrian Chadd #ifdef	RSS
11790a100a6fSAdrian Chadd 			case IP_RSS_LISTEN_BUCKET:
11800a100a6fSAdrian Chadd 				if ((optval >= 0) &&
11810a100a6fSAdrian Chadd 				    (optval < rss_getnumbuckets())) {
11820a100a6fSAdrian Chadd 					inp->inp_rss_listen_bucket = optval;
11830a100a6fSAdrian Chadd 					OPTSET2(INP_RSS_BUCKET_SET, 1);
11840a100a6fSAdrian Chadd 				} else {
11850a100a6fSAdrian Chadd 					error = EINVAL;
11860a100a6fSAdrian Chadd 				}
11870a100a6fSAdrian Chadd 				break;
11889d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
11899d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
11909d3ddf43SAdrian Chadd 				break;
11910a100a6fSAdrian Chadd #endif
1192df8bae1dSRodney W. Grimes 			}
1193df8bae1dSRodney W. Grimes 			break;
1194df8bae1dSRodney W. Grimes #undef OPTSET
11950a100a6fSAdrian Chadd #undef OPTSET2
1196df8bae1dSRodney W. Grimes 
119771498f30SBruce M Simpson 		/*
119871498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
119971498f30SBruce M Simpson 		 * module.
120071498f30SBruce M Simpson 		 */
1201df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1202f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1203df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1204df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1205df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1206df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
120771498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
120871498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
120971498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
121071498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
121171498f30SBruce M Simpson 		case IP_MSFILTER:
121271498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
121371498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
121471498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
121571498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
121671498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
121771498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
121871498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1219df8bae1dSRodney W. Grimes 			break;
1220df8bae1dSRodney W. Grimes 
122133b3ac06SPeter Wemm 		case IP_PORTRANGE:
1222cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1223cfe8b629SGarrett Wollman 					    sizeof optval);
1224cfe8b629SGarrett Wollman 			if (error)
1225cfe8b629SGarrett Wollman 				break;
122633b3ac06SPeter Wemm 
12278501a69cSRobert Watson 			INP_WLOCK(inp);
122833b3ac06SPeter Wemm 			switch (optval) {
122933b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
123033b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
123133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
123233b3ac06SPeter Wemm 				break;
123333b3ac06SPeter Wemm 
123433b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
123533b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
123633b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
123733b3ac06SPeter Wemm 				break;
123833b3ac06SPeter Wemm 
123933b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
124033b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
124133b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
124233b3ac06SPeter Wemm 				break;
124333b3ac06SPeter Wemm 
124433b3ac06SPeter Wemm 			default:
124533b3ac06SPeter Wemm 				error = EINVAL;
124633b3ac06SPeter Wemm 				break;
124733b3ac06SPeter Wemm 			}
12488501a69cSRobert Watson 			INP_WUNLOCK(inp);
1249ce8c72b1SPeter Wemm 			break;
125033b3ac06SPeter Wemm 
1251fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
12526a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1253fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1254fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
12556a800098SYoshinobu Inoue 				break;
12566a800098SYoshinobu Inoue 			}
1257fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1258b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
12596a800098SYoshinobu Inoue 
1260df8bae1dSRodney W. Grimes 		default:
1261df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1262df8bae1dSRodney W. Grimes 			break;
1263df8bae1dSRodney W. Grimes 		}
1264df8bae1dSRodney W. Grimes 		break;
1265df8bae1dSRodney W. Grimes 
1266cfe8b629SGarrett Wollman 	case SOPT_GET:
1267cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1268df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1269df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1270e5e3e746SMatt Macy 			INP_RLOCK(inp);
1271e5e3e746SMatt Macy 			if (inp->inp_options) {
1272e5e3e746SMatt Macy 				struct mbuf *options;
1273e5e3e746SMatt Macy 
127410731c54SMichael Tuexen 				options = m_copym(inp->inp_options, 0,
127510731c54SMichael Tuexen 				    M_COPYALL, M_NOWAIT);
1276e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1277e5e3e746SMatt Macy 				if (options != NULL) {
1278cfe8b629SGarrett Wollman 					error = sooptcopyout(sopt,
1279e5e3e746SMatt Macy 							     mtod(options, char *),
1280e5e3e746SMatt Macy 							     options->m_len);
1281e5e3e746SMatt Macy 					m_freem(options);
1282e5e3e746SMatt Macy 				} else
1283e5e3e746SMatt Macy 					error = ENOMEM;
1284e5e3e746SMatt Macy 			} else {
1285e5e3e746SMatt Macy 				INP_RUNLOCK(inp);
1286cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1287e5e3e746SMatt Macy 			}
1288df8bae1dSRodney W. Grimes 			break;
1289df8bae1dSRodney W. Grimes 
1290df8bae1dSRodney W. Grimes 		case IP_TOS:
1291df8bae1dSRodney W. Grimes 		case IP_TTL:
1292936cd18dSAndre Oppermann 		case IP_MINTTL:
1293df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1294df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1295dce33a45SErmal Luçi 		case IP_ORIGDSTADDR:
1296df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
12974957466bSMatthew N. Dodd 		case IP_RECVTTL:
129882c23ebaSBill Fenner 		case IP_RECVIF:
1299cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
13008afa2304SBruce M Simpson 		case IP_ONESBCAST:
1301b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
13025f6bf451SAttilio Rao 		case IP_BINDANY:
13033cca425bSMichael Tuexen 		case IP_RECVTOS:
13040a100a6fSAdrian Chadd 		case IP_BINDMULTI:
13059c423972SAdrian Chadd 		case IP_FLOWID:
13069c423972SAdrian Chadd 		case IP_FLOWTYPE:
13079d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
13080a100a6fSAdrian Chadd #ifdef	RSS
13090a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
13109d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
13110a100a6fSAdrian Chadd #endif
1312cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1313df8bae1dSRodney W. Grimes 
1314df8bae1dSRodney W. Grimes 			case IP_TOS:
1315ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1316df8bae1dSRodney W. Grimes 				break;
1317df8bae1dSRodney W. Grimes 
1318df8bae1dSRodney W. Grimes 			case IP_TTL:
1319ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1320df8bae1dSRodney W. Grimes 				break;
1321df8bae1dSRodney W. Grimes 
1322936cd18dSAndre Oppermann 			case IP_MINTTL:
1323936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1324936cd18dSAndre Oppermann 				break;
1325936cd18dSAndre Oppermann 
1326df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
13270a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1330df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1331df8bae1dSRodney W. Grimes 				break;
1332df8bae1dSRodney W. Grimes 
1333df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1334df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1335df8bae1dSRodney W. Grimes 				break;
1336df8bae1dSRodney W. Grimes 
1337df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1338df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1339df8bae1dSRodney W. Grimes 				break;
134082c23ebaSBill Fenner 
1341dce33a45SErmal Luçi 			case IP_ORIGDSTADDR:
1342dce33a45SErmal Luçi 				optval = OPTBIT2(INP_ORIGDSTADDR);
1343dce33a45SErmal Luçi 				break;
1344dce33a45SErmal Luçi 
13454957466bSMatthew N. Dodd 			case IP_RECVTTL:
13464957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
13474957466bSMatthew N. Dodd 				break;
13484957466bSMatthew N. Dodd 
134982c23ebaSBill Fenner 			case IP_RECVIF:
135082c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
135182c23ebaSBill Fenner 				break;
1352cfe8b629SGarrett Wollman 
1353cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1354cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1355cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1356cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1357cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1358cfe8b629SGarrett Wollman 				else
1359cfe8b629SGarrett Wollman 					optval = 0;
1360cfe8b629SGarrett Wollman 				break;
13616a800098SYoshinobu Inoue 
13628afa2304SBruce M Simpson 			case IP_ONESBCAST:
13638afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
13648afa2304SBruce M Simpson 				break;
1365b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1366b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1367b2828ad2SAndre Oppermann 				break;
13685f6bf451SAttilio Rao 			case IP_BINDANY:
13695f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
13705f6bf451SAttilio Rao 				break;
13713cca425bSMichael Tuexen 			case IP_RECVTOS:
13723cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
13733cca425bSMichael Tuexen 				break;
13749c423972SAdrian Chadd 			case IP_FLOWID:
13759c423972SAdrian Chadd 				optval = inp->inp_flowid;
13769c423972SAdrian Chadd 				break;
13779c423972SAdrian Chadd 			case IP_FLOWTYPE:
13789c423972SAdrian Chadd 				optval = inp->inp_flowtype;
13799c423972SAdrian Chadd 				break;
13809d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
13819d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
13829d3ddf43SAdrian Chadd 				break;
13839c423972SAdrian Chadd #ifdef	RSS
13847847796aSAdrian Chadd 			case IP_RSSBUCKETID:
13857847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
13867847796aSAdrian Chadd 				    inp->inp_flowtype,
13877847796aSAdrian Chadd 				    &rss_bucket);
13887847796aSAdrian Chadd 				if (retval == 0)
13897847796aSAdrian Chadd 					optval = rss_bucket;
13907847796aSAdrian Chadd 				else
13917847796aSAdrian Chadd 					error = EINVAL;
13929c423972SAdrian Chadd 				break;
13939d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
13949d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
13959d3ddf43SAdrian Chadd 				break;
13969c423972SAdrian Chadd #endif
13970a100a6fSAdrian Chadd 			case IP_BINDMULTI:
13980a100a6fSAdrian Chadd 				optval = OPTBIT2(INP_BINDMULTI);
13990a100a6fSAdrian Chadd 				break;
1400df8bae1dSRodney W. Grimes 			}
1401cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1402df8bae1dSRodney W. Grimes 			break;
1403df8bae1dSRodney W. Grimes 
140471498f30SBruce M Simpson 		/*
140571498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
140671498f30SBruce M Simpson 		 * module.
140771498f30SBruce M Simpson 		 */
1408df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1409f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1410df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1411df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
141271498f30SBruce M Simpson 		case IP_MSFILTER:
141371498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
141433b3ac06SPeter Wemm 			break;
141533b3ac06SPeter Wemm 
1416fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT)
14176a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
1418fcf59617SAndrey V. Elsukov 			if (IPSEC_ENABLED(ipv4)) {
1419fcf59617SAndrey V. Elsukov 				error = IPSEC_PCBCTL(ipv4, inp, sopt);
14206a800098SYoshinobu Inoue 				break;
14216a800098SYoshinobu Inoue 			}
1422fcf59617SAndrey V. Elsukov 			/* FALLTHROUGH */
1423b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
14246a800098SYoshinobu Inoue 
1425df8bae1dSRodney W. Grimes 		default:
1426df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1427df8bae1dSRodney W. Grimes 			break;
1428df8bae1dSRodney W. Grimes 		}
1429df8bae1dSRodney W. Grimes 		break;
1430df8bae1dSRodney W. Grimes 	}
1431df8bae1dSRodney W. Grimes 	return (error);
1432df8bae1dSRodney W. Grimes }
1433df8bae1dSRodney W. Grimes 
1434df8bae1dSRodney W. Grimes /*
1435df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1436df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1437df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1438f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1439f5fea3ddSPaul Traina  * replicating that code here.
1440df8bae1dSRodney W. Grimes  */
1441df8bae1dSRodney W. Grimes static void
1442331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen)
1443df8bae1dSRodney W. Grimes {
1444331dff07SAlexander V. Chernikov 	struct ip *ip;
1445df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1446df8bae1dSRodney W. Grimes 
1447e4762f75SGeorge V. Neville-Neil 	/*
1448e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1449e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1450e4762f75SGeorge V. Neville-Neil 	 */
1451eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
1452f0cace5dSRobert Watson 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
145386b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1454df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1455390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1456390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1457390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1458390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1459390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1460390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1461390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1462390cdc6aSRuslan Ermilov 		}
1463df8bae1dSRodney W. Grimes 		/*
1464df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1465df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1466df8bae1dSRodney W. Grimes 		 */
14678f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1468df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
146986b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1470331dff07SAlexander V. Chernikov 		if_simloop(ifp, copym, AF_INET, 0);
1471df8bae1dSRodney W. Grimes 	}
1472df8bae1dSRodney W. Grimes }
1473