xref: /freebsd/sys/netinet/ip_output.c (revision f3e7afe2d7b262ab55ab818445d4dfdb6e0c70a9)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
355d6d7e75SGleb Smirnoff #include "opt_inet.h"
36*f3e7afe2SHans Petter Selasky #include "opt_ratelimit.h"
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
3853dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
39e440aed9SQing Li #include "opt_mpath.h"
4057f60867SMark Johnston #include "opt_route.h"
412f4afd21SRandall Stewart #include "opt_sctp.h"
429c423972SAdrian Chadd #include "opt_rss.h"
43fbd1372aSJoerg Wunsch 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
4526f9a767SRodney W. Grimes #include <sys/systm.h>
46f0f6d643SLuigi Rizzo #include <sys/kernel.h>
47cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50acd3428bSRobert Watson #include <sys/priv.h>
51c26fe973SBjoern A. Zeeb #include <sys/proc.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
5457f60867SMark Johnston #include <sys/sdt.h>
55df8bae1dSRodney W. Grimes #include <sys/socket.h>
56df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
579d9edc56SMike Silbersack #include <sys/sysctl.h>
58c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <net/if.h>
6176039bc8SGleb Smirnoff #include <net/if_var.h>
623ef5e21dSQing Li #include <net/if_llatbl.h>
639b932e9eSAndre Oppermann #include <net/netisr.h>
64c21fd232SAndre Oppermann #include <net/pfil.h>
65df8bae1dSRodney W. Grimes #include <net/route.h>
6665111ec7SKip Macy #include <net/flowtable.h>
67e440aed9SQing Li #ifdef RADIX_MPATH
68e440aed9SQing Li #include <net/radix_mpath.h>
69e440aed9SQing Li #endif
70b2bdc62aSAdrian Chadd #include <net/rss_config.h>
714b79449eSBjoern A. Zeeb #include <net/vnet.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <netinet/in.h>
7457f60867SMark Johnston #include <netinet/in_kdtrace.h>
75df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
76df8bae1dSRodney W. Grimes #include <netinet/ip.h>
77df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
789c423972SAdrian Chadd #include <netinet/in_rss.h>
79df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
80df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
81ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
822f4afd21SRandall Stewart #ifdef SCTP
832f4afd21SRandall Stewart #include <netinet/sctp.h>
842f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
852f4afd21SRandall Stewart #endif
86df8bae1dSRodney W. Grimes 
87b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
882cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h>
891dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
90b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/
916a800098SYoshinobu Inoue 
921dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
931dfcf0d2SAndre Oppermann 
94aed55708SRobert Watson #include <security/mac/mac_framework.h>
95aed55708SRobert Watson 
9653dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
9705b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0;
989d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
999d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1009d9edc56SMike Silbersack #endif
1019d9edc56SMike Silbersack 
102331dff07SAlexander V. Chernikov static void	ip_mloopback(struct ifnet *, const struct mbuf *, int);
103afed1b49SDarren Reed 
104afed1b49SDarren Reed 
1058b889dbbSBruce M Simpson extern int in_mcast_loop;
10693e0e116SJulian Elischer extern	struct protosw inetsw[];
10793e0e116SJulian Elischer 
108d9f2a782SErmal Luçi static inline int
1098f980c01SMark Johnston ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, struct inpcb *inp,
110d9f2a782SErmal Luçi     struct sockaddr_in *dst, int *fibnum, int *error)
111d9f2a782SErmal Luçi {
112d9f2a782SErmal Luçi 	struct m_tag *fwd_tag = NULL;
1138f980c01SMark Johnston 	struct mbuf *m;
114d9f2a782SErmal Luçi 	struct in_addr odst;
115d9f2a782SErmal Luçi 	struct ip *ip;
116d9f2a782SErmal Luçi 
1178f980c01SMark Johnston 	m = *mp;
118d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
119d9f2a782SErmal Luçi 
120d9f2a782SErmal Luçi 	/* Run through list of hooks for output packets. */
121d9f2a782SErmal Luçi 	odst.s_addr = ip->ip_dst.s_addr;
1228f980c01SMark Johnston 	*error = pfil_run_hooks(&V_inet_pfil_hook, mp, ifp, PFIL_OUT, inp);
1238f980c01SMark Johnston 	m = *mp;
124d9f2a782SErmal Luçi 	if ((*error) != 0 || m == NULL)
125d9f2a782SErmal Luçi 		return 1; /* Finished */
126d9f2a782SErmal Luçi 
127d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
128d9f2a782SErmal Luçi 
129d9f2a782SErmal Luçi 	/* See if destination IP address was changed by packet filter. */
130d9f2a782SErmal Luçi 	if (odst.s_addr != ip->ip_dst.s_addr) {
131d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
132d9f2a782SErmal Luçi 		/* If destination is now ourself drop to ip_input(). */
133d9f2a782SErmal Luçi 		if (in_localip(ip->ip_dst)) {
134d9f2a782SErmal Luçi 			m->m_flags |= M_FASTFWD_OURS;
135d9f2a782SErmal Luçi 			if (m->m_pkthdr.rcvif == NULL)
136d9f2a782SErmal Luçi 				m->m_pkthdr.rcvif = V_loif;
137d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
138d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |=
139d9f2a782SErmal Luçi 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
140d9f2a782SErmal Luçi 				m->m_pkthdr.csum_data = 0xffff;
141d9f2a782SErmal Luçi 			}
142d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
143d9f2a782SErmal Luçi 				CSUM_IP_CHECKED | CSUM_IP_VALID;
144d9f2a782SErmal Luçi #ifdef SCTP
145d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
146d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
147d9f2a782SErmal Luçi #endif
148d9f2a782SErmal Luçi 			*error = netisr_queue(NETISR_IP, m);
149d9f2a782SErmal Luçi 			return 1; /* Finished */
150d9f2a782SErmal Luçi 		}
151d9f2a782SErmal Luçi 
152d9f2a782SErmal Luçi 		bzero(dst, sizeof(*dst));
153d9f2a782SErmal Luçi 		dst->sin_family = AF_INET;
154d9f2a782SErmal Luçi 		dst->sin_len = sizeof(*dst);
155d9f2a782SErmal Luçi 		dst->sin_addr = ip->ip_dst;
156d9f2a782SErmal Luçi 
157d9f2a782SErmal Luçi 		return -1; /* Reloop */
158d9f2a782SErmal Luçi 	}
159d9f2a782SErmal Luçi 	/* See if fib was changed by packet filter. */
160d9f2a782SErmal Luçi 	if ((*fibnum) != M_GETFIB(m)) {
161d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
162d9f2a782SErmal Luçi 		*fibnum = M_GETFIB(m);
163d9f2a782SErmal Luçi 		return -1; /* Reloop for FIB change */
164d9f2a782SErmal Luçi 	}
165d9f2a782SErmal Luçi 
166d9f2a782SErmal Luçi 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
167d9f2a782SErmal Luçi 	if (m->m_flags & M_FASTFWD_OURS) {
168d9f2a782SErmal Luçi 		if (m->m_pkthdr.rcvif == NULL)
169d9f2a782SErmal Luçi 			m->m_pkthdr.rcvif = V_loif;
170d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
171d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
172d9f2a782SErmal Luçi 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
173d9f2a782SErmal Luçi 			m->m_pkthdr.csum_data = 0xffff;
174d9f2a782SErmal Luçi 		}
175d9f2a782SErmal Luçi #ifdef SCTP
176d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
177d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
178d9f2a782SErmal Luçi #endif
179d9f2a782SErmal Luçi 		m->m_pkthdr.csum_flags |=
180d9f2a782SErmal Luçi 			CSUM_IP_CHECKED | CSUM_IP_VALID;
181d9f2a782SErmal Luçi 
182d9f2a782SErmal Luçi 		*error = netisr_queue(NETISR_IP, m);
183d9f2a782SErmal Luçi 		return 1; /* Finished */
184d9f2a782SErmal Luçi 	}
185d9f2a782SErmal Luçi 	/* Or forward to some other address? */
186d9f2a782SErmal Luçi 	if ((m->m_flags & M_IP_NEXTHOP) &&
187d9f2a782SErmal Luçi 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
188d9f2a782SErmal Luçi 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
189d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
190d9f2a782SErmal Luçi 		m->m_flags &= ~M_IP_NEXTHOP;
191d9f2a782SErmal Luçi 		m_tag_delete(m, fwd_tag);
192d9f2a782SErmal Luçi 
193d9f2a782SErmal Luçi 		return -1; /* Reloop for CHANGE of dst */
194d9f2a782SErmal Luçi 	}
195d9f2a782SErmal Luçi 
196d9f2a782SErmal Luçi 	return 0;
197d9f2a782SErmal Luçi }
198d9f2a782SErmal Luçi 
199df8bae1dSRodney W. Grimes /*
200df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
201df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
202df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
203df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
204bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
205bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
206bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
207bf984051SGleb Smirnoff  *
2083de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
2093de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
210df8bae1dSRodney W. Grimes  */
211df8bae1dSRodney W. Grimes int
212f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
213f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
214df8bae1dSRodney W. Grimes {
215cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
2161e78ac21SJeffrey Hsu 	struct ip *ip;
217fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
218ac9d7e26SMax Laier 	struct mbuf *m0;
21923bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
2203ae2ad08SJohn-Mark Gurney 	int mtu;
221ca8b83b0SLuigi Rizzo 	int error = 0;
222ca8b83b0SLuigi Rizzo 	struct sockaddr_in *dst;
2234c7a6059SGleb Smirnoff 	const struct sockaddr_in *gw;
2243c73180fSGleb Smirnoff 	struct in_ifaddr *ia;
22521d172a3SGleb Smirnoff 	int isbroadcast;
226078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
227e3f406b3SRuslan Ermilov 	struct route iproute;
228ec396e61SLuigi Rizzo 	struct rtentry *rte;	/* cache for ro->ro_rt */
2299c57a5b6SHiroki Sato 	uint32_t fibnum;
230fe82cbe8SGleb Smirnoff 	int have_ia_ref;
2311a499816SEdward Tomasz Napierala #ifdef IPSEC
2321a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
2331a499816SEdward Tomasz Napierala #endif
234ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
23536e8826fSMax Laier 
236bc97ba51SJulian Elischer 	if (inp != NULL) {
237baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
23862e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
239c2529042SHans Petter Selasky 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
24080cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
2419c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
24280cb9f21SKip Macy 		}
243bc97ba51SJulian Elischer 	}
24462e1ba83SRobert Watson 
24562e1ba83SRobert Watson 	if (ro == NULL) {
24662e1ba83SRobert Watson 		ro = &iproute;
24762e1ba83SRobert Watson 		bzero(ro, sizeof (*ro));
2486d768226SGeorge V. Neville-Neil 	} else
2496d768226SGeorge V. Neville-Neil 		ro->ro_flags |= RT_LLE_CACHE;
25062e1ba83SRobert Watson 
25153be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE
2520ff96b4fSGleb Smirnoff 	if (ro->ro_rt == NULL)
2530ff96b4fSGleb Smirnoff 		(void )flowtable_lookup(AF_INET, m, ro);
25453be8fcaSBjoern A. Zeeb #endif
2552b25acc1SLuigi Rizzo 
256df8bae1dSRodney W. Grimes 	if (opt) {
257ca8b83b0SLuigi Rizzo 		int len = 0;
258df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
259cb7641e8SMaxim Konovalov 		if (len != 0)
260ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
261df8bae1dSRodney W. Grimes 	}
262df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
2638f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
2648f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
2653efc3014SJulian Elischer 
266df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
26753be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
26853be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
2696d947416SGleb Smirnoff 		ip_fillid(ip);
27086425c62SRobert Watson 		IPSTAT_INC(ips_localout);
271df8bae1dSRodney W. Grimes 	} else {
272ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
27353be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
274df8bae1dSRodney W. Grimes 	}
2759c9137eaSGarrett Wollman 
276054692a4SAlexander V. Chernikov 	/*
277054692a4SAlexander V. Chernikov 	 * dst/gw handling:
278054692a4SAlexander V. Chernikov 	 *
2793c065f2fSGleb Smirnoff 	 * dst can be rewritten but always points to &ro->ro_dst.
2803c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
2813c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
282054692a4SAlexander V. Chernikov 	 */
2834c7a6059SGleb Smirnoff 	gw = dst = (struct sockaddr_in *)&ro->ro_dst;
2849c57a5b6SHiroki Sato 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
285d9f2a782SErmal Luçi 	rte = ro->ro_rt;
28684cc0778SGeorge V. Neville-Neil 	if (rte == NULL) {
287a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
288df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
289df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2909b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
291df8bae1dSRodney W. Grimes 	}
292d9f2a782SErmal Luçi again:
29384cc0778SGeorge V. Neville-Neil 	/*
29484cc0778SGeorge V. Neville-Neil 	 * Validate route against routing table additions;
29584cc0778SGeorge V. Neville-Neil 	 * a better/more specific route might have been added.
29684cc0778SGeorge V. Neville-Neil 	 */
29784cc0778SGeorge V. Neville-Neil 	if (inp)
29884cc0778SGeorge V. Neville-Neil 		RT_VALIDATE(ro, &inp->inp_rt_cookie, fibnum);
29984cc0778SGeorge V. Neville-Neil 	/*
30084cc0778SGeorge V. Neville-Neil 	 * If there is a cached route,
30184cc0778SGeorge V. Neville-Neil 	 * check that it is to the same destination
30284cc0778SGeorge V. Neville-Neil 	 * and is still up.  If not, free it and try again.
30384cc0778SGeorge V. Neville-Neil 	 * The address family should also be checked in case of sharing the
30484cc0778SGeorge V. Neville-Neil 	 * cache with IPv6.
30584cc0778SGeorge V. Neville-Neil 	 * Also check whether routing cache needs invalidation.
30684cc0778SGeorge V. Neville-Neil 	 */
30784cc0778SGeorge V. Neville-Neil 	rte = ro->ro_rt;
30884cc0778SGeorge V. Neville-Neil 	if (rte && ((rte->rt_flags & RTF_UP) == 0 ||
30984cc0778SGeorge V. Neville-Neil 		    rte->rt_ifp == NULL ||
31084cc0778SGeorge V. Neville-Neil 		    !RT_LINK_IS_UP(rte->rt_ifp) ||
31184cc0778SGeorge V. Neville-Neil 			  dst->sin_family != AF_INET ||
31284cc0778SGeorge V. Neville-Neil 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
31384cc0778SGeorge V. Neville-Neil 		RTFREE(rte);
31484cc0778SGeorge V. Neville-Neil 		rte = ro->ro_rt = (struct rtentry *)NULL;
3156d768226SGeorge V. Neville-Neil 		if (ro->ro_lle)
3166d768226SGeorge V. Neville-Neil 			LLE_FREE(ro->ro_lle);	/* zeros ro_lle */
3176d768226SGeorge V. Neville-Neil 		ro->ro_lle = (struct llentry *)NULL;
31884cc0778SGeorge V. Neville-Neil 	}
319d9f2a782SErmal Luçi 	ia = NULL;
320d9f2a782SErmal Luçi 	have_ia_ref = 0;
321df8bae1dSRodney W. Grimes 	/*
322a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
323a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
324a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
325a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
326df8bae1dSRodney W. Grimes 	 */
327a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
3284f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
32958a39d8cSAlan Somers 						      M_GETFIB(m)))) == NULL &&
3304f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
33158a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL) {
33286425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
333a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
334a3fd02d8SBruce M Simpson 			goto bad;
335a3fd02d8SBruce M Simpson 		}
336fe82cbe8SGleb Smirnoff 		have_ia_ref = 1;
337a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
338a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
339a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
340a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
341a3fd02d8SBruce M Simpson 		isbroadcast = 1;
342a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
3434f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
34458a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL &&
3454f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
34658a39d8cSAlan Somers 						M_GETFIB(m)))) == NULL) {
34786425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
348df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
349df8bae1dSRodney W. Grimes 			goto bad;
350df8bae1dSRodney W. Grimes 		}
351fe82cbe8SGleb Smirnoff 		have_ia_ref = 1;
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);
364fe82cbe8SGleb Smirnoff 		if (ia)
365fe82cbe8SGleb Smirnoff 			have_ia_ref = 1;
36638c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
367df8bae1dSRodney W. Grimes 	} else {
3682c17fe93SGarrett Wollman 		/*
36997d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
37097d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
37197d8d152SAndre Oppermann 		 * operation (as it is for ARP).
3722c17fe93SGarrett Wollman 		 */
373ec396e61SLuigi Rizzo 		if (rte == NULL) {
374e440aed9SQing Li #ifdef RADIX_MPATH
3758b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
3768b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
3779c57a5b6SHiroki Sato 			    fibnum);
378e440aed9SQing Li #else
3799c57a5b6SHiroki Sato 			in_rtalloc_ign(ro, 0, fibnum);
380e440aed9SQing Li #endif
381ec396e61SLuigi Rizzo 			rte = ro->ro_rt;
382ec396e61SLuigi Rizzo 		}
383c7ea0aa6SQing Li 		if (rte == NULL ||
384d9f2a782SErmal Luçi 		    (rte->rt_flags & RTF_UP) == 0 ||
385c7ea0aa6SQing Li 		    rte->rt_ifp == NULL ||
386c7ea0aa6SQing Li 		    !RT_LINK_IS_UP(rte->rt_ifp)) {
3871a499816SEdward Tomasz Napierala #ifdef IPSEC
3881a499816SEdward Tomasz Napierala 			/*
3891a499816SEdward Tomasz Napierala 			 * There is no route for this packet, but it is
3901a499816SEdward Tomasz Napierala 			 * possible that a matching SPD entry exists.
3911a499816SEdward Tomasz Napierala 			 */
3921a499816SEdward Tomasz Napierala 			no_route_but_check_spd = 1;
3931a499816SEdward Tomasz Napierala 			mtu = 0; /* Silence GCC warning. */
3941a499816SEdward Tomasz Napierala 			goto sendit;
3951a499816SEdward Tomasz Napierala #endif
39686425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
397df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
398df8bae1dSRodney W. Grimes 			goto bad;
399df8bae1dSRodney W. Grimes 		}
400ec396e61SLuigi Rizzo 		ia = ifatoia(rte->rt_ifa);
401ec396e61SLuigi Rizzo 		ifp = rte->rt_ifp;
402e3a7aa6fSGleb Smirnoff 		counter_u64_add(rte->rt_pksent, 1);
40336402a68SAlexander V. Chernikov 		rt_update_ro_flags(ro);
404ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_GATEWAY)
4054c7a6059SGleb Smirnoff 			gw = (struct sockaddr_in *)rte->rt_gateway;
406ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_HOST)
407ec396e61SLuigi Rizzo 			isbroadcast = (rte->rt_flags & RTF_BROADCAST);
4086c1bd558SRyan Stone 		else if (ifp->if_flags & IFF_BROADCAST)
40990cc51a1SRyan Stone 			isbroadcast = in_ifaddr_broadcast(gw->sin_addr, ia);
4106c1bd558SRyan Stone 		else
4116c1bd558SRyan Stone 			isbroadcast = 0;
412df8bae1dSRodney W. Grimes 	}
413d9f2a782SErmal Luçi 
4143ae2ad08SJohn-Mark Gurney 	/*
4153ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
4163ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
4173ae2ad08SJohn-Mark Gurney 	 */
4187f948f12SAlexander V. Chernikov 	if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST)))
419e3a7aa6fSGleb Smirnoff 		mtu = rte->rt_mtu;
4207f948f12SAlexander V. Chernikov 	else
4213ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
422c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
423c744cde4SBjoern A. Zeeb 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p",
424c744cde4SBjoern A. Zeeb 	    __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp));
425d9f2a782SErmal Luçi 
4269b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
427df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
428df8bae1dSRodney W. Grimes 		/*
429183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
430183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
431183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
432183e1c86SGleb Smirnoff 		 */
433183e1c86SGleb Smirnoff 		gw = dst;
434183e1c86SGleb Smirnoff 		/*
435df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
436df8bae1dSRodney W. Grimes 		 */
437df8bae1dSRodney W. Grimes 		if (imo != NULL) {
438df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
4391c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
4401c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
441bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
442bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
443bbb4330bSLuigi Rizzo 				    INADDR_ANY;
444df8bae1dSRodney W. Grimes 		} else
445df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
446df8bae1dSRodney W. Grimes 		/*
447df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
448df8bae1dSRodney W. Grimes 		 */
4491c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
450df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
45186425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
452df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
453df8bae1dSRodney W. Grimes 				goto bad;
454df8bae1dSRodney W. Grimes 			}
4551c5de19aSGarrett Wollman 		}
456df8bae1dSRodney W. Grimes 		/*
457df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
458df8bae1dSRodney W. Grimes 		 * of outgoing interface.
459df8bae1dSRodney W. Grimes 		 */
460df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
46138c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
46238c1bc35SRuslan Ermilov 			if (ia != NULL)
46338c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
464df8bae1dSRodney W. Grimes 		}
465df8bae1dSRodney W. Grimes 
4668b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
4678b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
468df8bae1dSRodney W. Grimes 			/*
4698b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
4708b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
4718b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
4728b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
4738b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
474df8bae1dSRodney W. Grimes 			 */
475331dff07SAlexander V. Chernikov 			ip_mloopback(ifp, m, hlen);
4768b889dbbSBruce M Simpson 		} else {
477df8bae1dSRodney W. Grimes 			/*
478df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
479df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
480df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
481df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
482df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
483df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
484df8bae1dSRodney W. Grimes 			 *
485df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
486df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
487df8bae1dSRodney W. Grimes 			 * if necessary.
488df8bae1dSRodney W. Grimes 			 */
489603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
490f0068c4aSGarrett Wollman 				/*
491bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
492f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
493f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
494f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
495f0068c4aSGarrett Wollman 				 */
496603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
497f0068c4aSGarrett Wollman 					imo = NULL;
498bbb4330bSLuigi Rizzo 				if (ip_mforward &&
499bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
500df8bae1dSRodney W. Grimes 					m_freem(m);
501df8bae1dSRodney W. Grimes 					goto done;
502df8bae1dSRodney W. Grimes 				}
503df8bae1dSRodney W. Grimes 			}
504df8bae1dSRodney W. Grimes 		}
5055e9ae478SGarrett Wollman 
506df8bae1dSRodney W. Grimes 		/*
507df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
508df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
509df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
510df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
5118b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
5128b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
5138b889dbbSBruce M Simpson 		 * the loopback interface.
514df8bae1dSRodney W. Grimes 		 */
515f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
516df8bae1dSRodney W. Grimes 			m_freem(m);
517df8bae1dSRodney W. Grimes 			goto done;
518df8bae1dSRodney W. Grimes 		}
519df8bae1dSRodney W. Grimes 
520df8bae1dSRodney W. Grimes 		goto sendit;
521df8bae1dSRodney W. Grimes 	}
5226a7c943cSAndre Oppermann 
523df8bae1dSRodney W. Grimes 	/*
5242b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
525cb459254SJohn-Mark Gurney 	 * of the outoing interface.
526df8bae1dSRodney W. Grimes 	 */
527f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
52838c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
52938c1bc35SRuslan Ermilov 		if (ia != NULL) {
530df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
531f9e354dfSJulian Elischer 		}
53238c1bc35SRuslan Ermilov 	}
5336a7c943cSAndre Oppermann 
534ca7a789aSMax Laier 	/*
535df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
5368c3f5566SRuslan Ermilov 	 * verify user is allowed to send
537df8bae1dSRodney W. Grimes 	 * such a packet.
538df8bae1dSRodney W. Grimes 	 */
5399f9b3dc4SGarrett Wollman 	if (isbroadcast) {
540df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
541df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
542df8bae1dSRodney W. Grimes 			goto bad;
543df8bae1dSRodney W. Grimes 		}
544df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
545df8bae1dSRodney W. Grimes 			error = EACCES;
546df8bae1dSRodney W. Grimes 			goto bad;
547df8bae1dSRodney W. Grimes 		}
548df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
5498f134647SGleb Smirnoff 		if (ip_len > mtu) {
550df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
551df8bae1dSRodney W. Grimes 			goto bad;
552df8bae1dSRodney W. Grimes 		}
553df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
5549f9b3dc4SGarrett Wollman 	} else {
555df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
5569f9b3dc4SGarrett Wollman 	}
557df8bae1dSRodney W. Grimes 
558f1743588SDarren Reed sendit:
559b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
5600275b2e3SAndrey V. Elsukov 	switch(ip_ipsec_output(&m, inp, &error)) {
5611dfcf0d2SAndre Oppermann 	case 1:
56233841545SHajimu UMEMOTO 		goto bad;
5631dfcf0d2SAndre Oppermann 	case -1:
5641dfcf0d2SAndre Oppermann 		goto done;
5651dfcf0d2SAndre Oppermann 	case 0:
56633841545SHajimu UMEMOTO 	default:
5671dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
56833841545SHajimu UMEMOTO 	}
5691a499816SEdward Tomasz Napierala 	/*
5701a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
5711a499816SEdward Tomasz Napierala 	 */
5721a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
5731a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
5741a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
5751a499816SEdward Tomasz Napierala 		goto bad;
5761a499816SEdward Tomasz Napierala 	}
5771dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
57833841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
57933841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
580b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
58133841545SHajimu UMEMOTO 
582c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
583d9f2a782SErmal Luçi 	if (PFIL_HOOKED(&V_inet_pfil_hook)) {
5848f980c01SMark Johnston 		switch (ip_output_pfil(&m, ifp, inp, dst, &fibnum, &error)) {
585d9f2a782SErmal Luçi 		case 1: /* Finished */
586c4ac87eaSDarren Reed 			goto done;
5879b932e9eSAndre Oppermann 
588d9f2a782SErmal Luçi 		case 0: /* Continue normally */
589c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
590d9f2a782SErmal Luçi 			break;
591fed1c7e9SSøren Schmidt 
592d9f2a782SErmal Luçi 		case -1: /* Need to try again */
593d9f2a782SErmal Luçi 			/* Reset everything for a new round */
5949c57a5b6SHiroki Sato 			RO_RTFREE(ro);
595fe82cbe8SGleb Smirnoff 			if (have_ia_ref)
596fe82cbe8SGleb Smirnoff 				ifa_free(&ia->ia_ifa);
5974fb3a820SAlexander V. Chernikov 			ro->ro_prepend = NULL;
598d9f2a782SErmal Luçi 			rte = NULL;
599d9f2a782SErmal Luçi 			gw = dst;
600d9f2a782SErmal Luçi 			ip = mtod(m, struct ip *);
6019b932e9eSAndre Oppermann 			goto again;
602d9f2a782SErmal Luçi 
603d9f2a782SErmal Luçi 		}
604099dd043SAndre Oppermann 	}
6052b25acc1SLuigi Rizzo 
60651c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
60751c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
60851c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
60951c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
61086425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
61151c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
61251c8ec4aSRuslan Ermilov 			goto bad;
61351c8ec4aSRuslan Ermilov 		}
61451c8ec4aSRuslan Ermilov 	}
61551c8ec4aSRuslan Ermilov 
616206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
617078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
618db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
619078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
620db4f9cc7SJonathan Lemon 	}
6212f4afd21SRandall Stewart #ifdef SCTP
622078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
6231966e5b5SRandall Stewart 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
624078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
6252f4afd21SRandall Stewart 	}
6262f4afd21SRandall Stewart #endif
627db4f9cc7SJonathan Lemon 
628e7319babSPoul-Henning Kamp 	/*
629db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
630233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
631df8bae1dSRodney W. Grimes 	 */
63221d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
633bf7dcda3SGleb Smirnoff 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
634df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
635078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
636df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
637078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
638078468edSGleb Smirnoff 		}
6395da9f8faSJosef Karthauser 
640233dcce1SAndre Oppermann 		/*
641233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
642233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
643233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
644233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
645233dcce1SAndre Oppermann 		 */
64638c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
647233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
6487caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
6497caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
650233dcce1SAndre Oppermann 			else
6517caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
6527caf4ab7SGleb Smirnoff 
6537caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
6545da9f8faSJosef Karthauser 		}
65553dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
6563390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
657eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
6589d9edc56SMike Silbersack #endif
659147f74d1SAndre Oppermann 		/*
660147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
661147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
662147f74d1SAndre Oppermann 		 */
66386bd0491SAndre Oppermann 		m_clrprotoflags(m);
66457f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
665*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
666*f3e7afe2SHans Petter Selasky 		if (inp != NULL) {
667*f3e7afe2SHans Petter Selasky 			if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
668*f3e7afe2SHans Petter Selasky 				in_pcboutput_txrtlmt(inp, ifp, m);
669*f3e7afe2SHans Petter Selasky 			/* stamp send tag on mbuf */
670*f3e7afe2SHans Petter Selasky 			m->m_pkthdr.snd_tag = inp->inp_snd_tag;
671*f3e7afe2SHans Petter Selasky 		} else {
672*f3e7afe2SHans Petter Selasky 			m->m_pkthdr.snd_tag = NULL;
673*f3e7afe2SHans Petter Selasky 		}
674*f3e7afe2SHans Petter Selasky #endif
67547e8d432SGleb Smirnoff 		error = (*ifp->if_output)(ifp, m,
67647e8d432SGleb Smirnoff 		    (const struct sockaddr *)gw, ro);
677*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
678*f3e7afe2SHans Petter Selasky 		/* check for route change */
679*f3e7afe2SHans Petter Selasky 		if (error == EAGAIN)
680*f3e7afe2SHans Petter Selasky 			in_pcboutput_eagain(inp);
681*f3e7afe2SHans Petter Selasky #endif
682df8bae1dSRodney W. Grimes 		goto done;
683df8bae1dSRodney W. Grimes 	}
6841e78ac21SJeffrey Hsu 
685233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
68621d172a3SGleb Smirnoff 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
687df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
68886425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
689df8bae1dSRodney W. Grimes 		goto bad;
690df8bae1dSRodney W. Grimes 	}
6911e78ac21SJeffrey Hsu 
6921e78ac21SJeffrey Hsu 	/*
6931e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
6941e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
6951e78ac21SJeffrey Hsu 	 */
696078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
6971e78ac21SJeffrey Hsu 	if (error)
698df8bae1dSRodney W. Grimes 		goto bad;
6991e78ac21SJeffrey Hsu 	for (; m; m = m0) {
700df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
701b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
70207203494SDaniel C. Sobral 		if (error == 0) {
703fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
70407203494SDaniel C. Sobral 			if (ia != NULL) {
7057caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
7067caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
7077caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
70807203494SDaniel C. Sobral 			}
709147f74d1SAndre Oppermann 			/*
710147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
711147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
712147f74d1SAndre Oppermann 			 */
71386bd0491SAndre Oppermann 			m_clrprotoflags(m);
714fe937674SJosef Karthauser 
7152e77d270SAndrey V. Elsukov 			IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp,
7162e77d270SAndrey V. Elsukov 			    mtod(m, struct ip *), NULL);
717*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
718*f3e7afe2SHans Petter Selasky 			if (inp != NULL) {
719*f3e7afe2SHans Petter Selasky 				if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
720*f3e7afe2SHans Petter Selasky 					in_pcboutput_txrtlmt(inp, ifp, m);
721*f3e7afe2SHans Petter Selasky 				/* stamp send tag on mbuf */
722*f3e7afe2SHans Petter Selasky 				m->m_pkthdr.snd_tag = inp->inp_snd_tag;
723*f3e7afe2SHans Petter Selasky 			} else {
724*f3e7afe2SHans Petter Selasky 				m->m_pkthdr.snd_tag = NULL;
725*f3e7afe2SHans Petter Selasky 			}
726*f3e7afe2SHans Petter Selasky #endif
727df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
72847e8d432SGleb Smirnoff 			    (const struct sockaddr *)gw, ro);
729*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
730*f3e7afe2SHans Petter Selasky 			/* check for route change */
731*f3e7afe2SHans Petter Selasky 			if (error == EAGAIN)
732*f3e7afe2SHans Petter Selasky 				in_pcboutput_eagain(inp);
733*f3e7afe2SHans Petter Selasky #endif
734fe937674SJosef Karthauser 		} else
735df8bae1dSRodney W. Grimes 			m_freem(m);
736df8bae1dSRodney W. Grimes 	}
737df8bae1dSRodney W. Grimes 
738df8bae1dSRodney W. Grimes 	if (error == 0)
73986425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
7401e78ac21SJeffrey Hsu 
741df8bae1dSRodney W. Grimes done:
742cc94f0c2SGleb Smirnoff 	if (ro == &iproute)
743bf984051SGleb Smirnoff 		RO_RTFREE(ro);
7443c402323SErmal Luçi 	else if (rte == NULL)
7453c402323SErmal Luçi 		/*
7463c402323SErmal Luçi 		 * If the caller supplied a route but somehow the reference
7473c402323SErmal Luçi 		 * to it has been released need to prevent the caller
7483c402323SErmal Luçi 		 * calling RTFREE on it again.
7493c402323SErmal Luçi 		 */
7503c402323SErmal Luçi 		ro->ro_rt = NULL;
751fe82cbe8SGleb Smirnoff 	if (have_ia_ref)
752fe82cbe8SGleb Smirnoff 		ifa_free(&ia->ia_ifa);
753df8bae1dSRodney W. Grimes 	return (error);
754df8bae1dSRodney W. Grimes bad:
7553528d68fSBill Paul 	m_freem(m);
756df8bae1dSRodney W. Grimes 	goto done;
757df8bae1dSRodney W. Grimes }
758df8bae1dSRodney W. Grimes 
7591e78ac21SJeffrey Hsu /*
7601e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
7611e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
7621e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
7631e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
7641e78ac21SJeffrey Hsu  *
7651e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
7661e78ac21SJeffrey Hsu  */
7671e78ac21SJeffrey Hsu int
7681e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
769078468edSGleb Smirnoff     u_long if_hwassist_flags)
7701e78ac21SJeffrey Hsu {
7711e78ac21SJeffrey Hsu 	int error = 0;
7721e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
7731e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
7741e78ac21SJeffrey Hsu 	int off;
7751e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
7761e78ac21SJeffrey Hsu 	int firstlen;
7771e78ac21SJeffrey Hsu 	struct mbuf **mnext;
7781e78ac21SJeffrey Hsu 	int nfrags;
77921d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
7801e78ac21SJeffrey Hsu 
78121d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
78221d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
78321d172a3SGleb Smirnoff 
78421d172a3SGleb Smirnoff 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
78586425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
7861e78ac21SJeffrey Hsu 		return EMSGSIZE;
7871e78ac21SJeffrey Hsu 	}
7881e78ac21SJeffrey Hsu 
7891e78ac21SJeffrey Hsu 	/*
7901e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
7911e78ac21SJeffrey Hsu 	 */
7921e78ac21SJeffrey Hsu 	if (len < 8)
7931e78ac21SJeffrey Hsu 		return EMSGSIZE;
7941e78ac21SJeffrey Hsu 
7951e78ac21SJeffrey Hsu 	/*
7961e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
7971e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
7981e78ac21SJeffrey Hsu 	 */
799da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
8001e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
8011e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
8021e78ac21SJeffrey Hsu 	}
8032f4afd21SRandall Stewart #ifdef SCTP
804da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
8051966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
8062f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
8072f4afd21SRandall Stewart 	}
8082f4afd21SRandall Stewart #endif
8091e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
8101e78ac21SJeffrey Hsu 		/*
8111e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
8121e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
8131e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
8141e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
8151e78ac21SJeffrey Hsu 		 *
8161e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
8171e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
8181e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
8191e78ac21SJeffrey Hsu 		 */
8201e78ac21SJeffrey Hsu 		int newlen;
8211e78ac21SJeffrey Hsu 
8229c0f6aa7SHans Petter Selasky 		off = MIN(mtu, m0->m_pkthdr.len);
8231e78ac21SJeffrey Hsu 
8241e78ac21SJeffrey Hsu 		/*
8251e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
8261e78ac21SJeffrey Hsu 		 * 8-byte boundary
8271e78ac21SJeffrey Hsu 		 */
8281e78ac21SJeffrey Hsu 		if (off < hlen)
8291e78ac21SJeffrey Hsu 			goto smart_frag_failure;
8301e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
8311e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
8321e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
8331e78ac21SJeffrey Hsu 			/* we failed, go back the default */
8341e78ac21SJeffrey Hsu smart_frag_failure:
8351e78ac21SJeffrey Hsu 			newlen = len;
8361e78ac21SJeffrey Hsu 			off = hlen + len;
8371e78ac21SJeffrey Hsu 		}
8381e78ac21SJeffrey Hsu 		len = newlen;
8391e78ac21SJeffrey Hsu 
8401e78ac21SJeffrey Hsu 	} else {
8411e78ac21SJeffrey Hsu 		off = hlen + len;
8421e78ac21SJeffrey Hsu 	}
8431e78ac21SJeffrey Hsu 
8441e78ac21SJeffrey Hsu 	firstlen = off - hlen;
8451e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
8461e78ac21SJeffrey Hsu 
8471e78ac21SJeffrey Hsu 	/*
8481e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
8491e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
8501e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
8511e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
8521e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
8531e78ac21SJeffrey Hsu 	 */
85421d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
8551e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
8561e78ac21SJeffrey Hsu 		struct mbuf *m;
8571e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
8581e78ac21SJeffrey Hsu 
859dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
8600b17fba7SAndre Oppermann 		if (m == NULL) {
8611e78ac21SJeffrey Hsu 			error = ENOBUFS;
86286425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8631e78ac21SJeffrey Hsu 			goto done;
8641e78ac21SJeffrey Hsu 		}
865c4c4346fSHans Petter Selasky 		/*
866c4c4346fSHans Petter Selasky 		 * Make sure the complete packet header gets copied
867c4c4346fSHans Petter Selasky 		 * from the originating mbuf to the newly created
868c4c4346fSHans Petter Selasky 		 * mbuf. This also ensures that existing firewall
869c4c4346fSHans Petter Selasky 		 * classification(s), VLAN tags and so on get copied
870c4c4346fSHans Petter Selasky 		 * to the resulting fragmented packet(s):
871c4c4346fSHans Petter Selasky 		 */
872c4c4346fSHans Petter Selasky 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
873c4c4346fSHans Petter Selasky 			m_free(m);
874c4c4346fSHans Petter Selasky 			error = ENOBUFS;
875c4c4346fSHans Petter Selasky 			IPSTAT_INC(ips_odropped);
876c4c4346fSHans Petter Selasky 			goto done;
877c4c4346fSHans Petter Selasky 		}
8781e78ac21SJeffrey Hsu 		/*
8791e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
8801e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
8818b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
8821e78ac21SJeffrey Hsu 		 */
8831e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
8841e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
8851e78ac21SJeffrey Hsu 		*mhip = *ip;
8861e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
8871e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
8881e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
8891e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
8901e78ac21SJeffrey Hsu 		}
8911e78ac21SJeffrey Hsu 		m->m_len = mhlen;
89221d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
89321d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
894fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
89521d172a3SGleb Smirnoff 			len = ip_len - off;
896fb86dfcdSAndre Oppermann 		else
8971e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
8981e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
899eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
9000b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
9011e78ac21SJeffrey Hsu 			m_free(m);
9021e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
90386425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
9041e78ac21SJeffrey Hsu 			goto done;
9051e78ac21SJeffrey Hsu 		}
9061e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
9071e78ac21SJeffrey Hsu #ifdef MAC
90830d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
9091e78ac21SJeffrey Hsu #endif
9101e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
9111e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
912078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
9131e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
914078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
915078468edSGleb Smirnoff 		}
9161e78ac21SJeffrey Hsu 		*mnext = m;
9171e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
9181e78ac21SJeffrey Hsu 	}
91986425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
9201e78ac21SJeffrey Hsu 
9211e78ac21SJeffrey Hsu 	/*
9221e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
9231e78ac21SJeffrey Hsu 	 * and updating header.
9241e78ac21SJeffrey Hsu 	 */
92521d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
9261e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
9271e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
92821d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
9291e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
930078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
9311e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
932078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
933078468edSGleb Smirnoff 	}
9341e78ac21SJeffrey Hsu 
9351e78ac21SJeffrey Hsu done:
9361e78ac21SJeffrey Hsu 	*m_frag = m0;
9371e78ac21SJeffrey Hsu 	return error;
9381e78ac21SJeffrey Hsu }
9391e78ac21SJeffrey Hsu 
9401c238475SJonathan Lemon void
941db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
942db4f9cc7SJonathan Lemon {
943db4f9cc7SJonathan Lemon 	struct ip *ip;
94423e9c6dcSGleb Smirnoff 	uint16_t csum, offset, ip_len;
945db4f9cc7SJonathan Lemon 
946db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
94753be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
94823e9c6dcSGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
94923e9c6dcSGleb Smirnoff 	csum = in_cksum_skip(m, ip_len, offset);
950206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
951206a3274SRuslan Ermilov 		csum = 0xffff;
952db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
953db4f9cc7SJonathan Lemon 
9548e1d0a56SMichael Tuexen 	/* find the mbuf in the chain where the checksum starts*/
9558e1d0a56SMichael Tuexen 	while ((m != NULL) && (offset >= m->m_len)) {
9568e1d0a56SMichael Tuexen 		offset -= m->m_len;
9578e1d0a56SMichael Tuexen 		m = m->m_next;
9588e1d0a56SMichael Tuexen 	}
95926461454SMichael Tuexen 	KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain."));
96026461454SMichael Tuexen 	KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs."));
961db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
962db4f9cc7SJonathan Lemon }
963db4f9cc7SJonathan Lemon 
964df8bae1dSRodney W. Grimes /*
965df8bae1dSRodney W. Grimes  * IP socket option processing.
966df8bae1dSRodney W. Grimes  */
967df8bae1dSRodney W. Grimes int
968f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
969df8bae1dSRodney W. Grimes {
970cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
971cfe8b629SGarrett Wollman 	int	error, optval;
972dc847eb6SAdrian Chadd #ifdef	RSS
973dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
974dc847eb6SAdrian Chadd 	int retval;
975dc847eb6SAdrian Chadd #endif
976df8bae1dSRodney W. Grimes 
977cfe8b629SGarrett Wollman 	error = optval = 0;
978cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
979fc06cd42SMikolaj Golub 		error = EINVAL;
980fc06cd42SMikolaj Golub 
981fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
982fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
983fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
984fc06cd42SMikolaj Golub 			case SO_REUSEADDR:
985fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
986efdf104bSMikolaj Golub 				if ((so->so_options & SO_REUSEADDR) != 0)
987efdf104bSMikolaj Golub 					inp->inp_flags2 |= INP_REUSEADDR;
988fc06cd42SMikolaj Golub 				else
989efdf104bSMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEADDR;
990fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
991fc06cd42SMikolaj Golub 				error = 0;
992fc06cd42SMikolaj Golub 				break;
993fc06cd42SMikolaj Golub 			case SO_REUSEPORT:
994fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
995fc06cd42SMikolaj Golub 				if ((so->so_options & SO_REUSEPORT) != 0)
996fc06cd42SMikolaj Golub 					inp->inp_flags2 |= INP_REUSEPORT;
997fc06cd42SMikolaj Golub 				else
998fc06cd42SMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEPORT;
999fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1000fc06cd42SMikolaj Golub 				error = 0;
1001fc06cd42SMikolaj Golub 				break;
1002fc06cd42SMikolaj Golub 			case SO_SETFIB:
1003fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
1004fc06cd42SMikolaj Golub 				inp->inp_inc.inc_fibnum = so->so_fibnum;
1005fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
1006fc06cd42SMikolaj Golub 				error = 0;
1007fc06cd42SMikolaj Golub 				break;
1008*f3e7afe2SHans Petter Selasky 			case SO_MAX_PACING_RATE:
1009*f3e7afe2SHans Petter Selasky #ifdef RATELIMIT
1010*f3e7afe2SHans Petter Selasky 				INP_WLOCK(inp);
1011*f3e7afe2SHans Petter Selasky 				inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1012*f3e7afe2SHans Petter Selasky 				INP_WUNLOCK(inp);
1013*f3e7afe2SHans Petter Selasky 				error = 0;
1014*f3e7afe2SHans Petter Selasky #else
1015*f3e7afe2SHans Petter Selasky 				error = EOPNOTSUPP;
1016*f3e7afe2SHans Petter Selasky #endif
1017*f3e7afe2SHans Petter Selasky 				break;
1018fc06cd42SMikolaj Golub 			default:
1019fc06cd42SMikolaj Golub 				break;
1020fc06cd42SMikolaj Golub 			}
1021fc06cd42SMikolaj Golub 		}
1022fc06cd42SMikolaj Golub 		return (error);
1023cfe8b629SGarrett Wollman 	}
1024df8bae1dSRodney W. Grimes 
1025cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1026cfe8b629SGarrett Wollman 	case SOPT_SET:
1027cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1028df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1029df8bae1dSRodney W. Grimes #ifdef notyet
1030df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1031df8bae1dSRodney W. Grimes #endif
1032cfe8b629SGarrett Wollman 		{
1033cfe8b629SGarrett Wollman 			struct mbuf *m;
1034cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1035cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1036cfe8b629SGarrett Wollman 				break;
1037cfe8b629SGarrett Wollman 			}
1038dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
10390b17fba7SAndre Oppermann 			if (m == NULL) {
1040cfe8b629SGarrett Wollman 				error = ENOBUFS;
1041cfe8b629SGarrett Wollman 				break;
1042cfe8b629SGarrett Wollman 			}
1043cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1044cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1045cfe8b629SGarrett Wollman 					    m->m_len);
1046635354c4SMaxim Konovalov 			if (error) {
1047635354c4SMaxim Konovalov 				m_free(m);
1048635354c4SMaxim Konovalov 				break;
1049635354c4SMaxim Konovalov 			}
10508501a69cSRobert Watson 			INP_WLOCK(inp);
1051993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
10528501a69cSRobert Watson 			INP_WUNLOCK(inp);
1053993d9505SRobert Watson 			return (error);
1054cfe8b629SGarrett Wollman 		}
1055df8bae1dSRodney W. Grimes 
1056f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
1057f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
1058f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
1059f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
1060f44270e7SPawel Jakub Dawidek 				if (error)
1061be9347e3SAdrian Chadd 					break;
1062be9347e3SAdrian Chadd 			}
1063cef27294SAdrian Chadd 			/* FALLTHROUGH */
10640a100a6fSAdrian Chadd 		case IP_BINDMULTI:
10650a100a6fSAdrian Chadd #ifdef	RSS
10660a100a6fSAdrian Chadd 		case IP_RSS_LISTEN_BUCKET:
10670a100a6fSAdrian Chadd #endif
1068df8bae1dSRodney W. Grimes 		case IP_TOS:
1069df8bae1dSRodney W. Grimes 		case IP_TTL:
1070936cd18dSAndre Oppermann 		case IP_MINTTL:
1071df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1072df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1073df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
10744957466bSMatthew N. Dodd 		case IP_RECVTTL:
107582c23ebaSBill Fenner 		case IP_RECVIF:
10768afa2304SBruce M Simpson 		case IP_ONESBCAST:
1077b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
10783cca425bSMichael Tuexen 		case IP_RECVTOS:
10799d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
10809d3ddf43SAdrian Chadd #ifdef	RSS
10819d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
10829d3ddf43SAdrian Chadd #endif
1083cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1084cfe8b629SGarrett Wollman 					    sizeof optval);
1085cfe8b629SGarrett Wollman 			if (error)
1086cfe8b629SGarrett Wollman 				break;
1087df8bae1dSRodney W. Grimes 
1088cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1089df8bae1dSRodney W. Grimes 			case IP_TOS:
1090ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1091df8bae1dSRodney W. Grimes 				break;
1092df8bae1dSRodney W. Grimes 
1093df8bae1dSRodney W. Grimes 			case IP_TTL:
1094ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1095df8bae1dSRodney W. Grimes 				break;
1096936cd18dSAndre Oppermann 
1097936cd18dSAndre Oppermann 			case IP_MINTTL:
1098a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1099936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1100936cd18dSAndre Oppermann 				else
1101936cd18dSAndre Oppermann 					error = EINVAL;
1102936cd18dSAndre Oppermann 				break;
1103936cd18dSAndre Oppermann 
1104a138d217SRobert Watson #define	OPTSET(bit) do {						\
11058501a69cSRobert Watson 	INP_WLOCK(inp);							\
1106df8bae1dSRodney W. Grimes 	if (optval)							\
1107df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1108df8bae1dSRodney W. Grimes 	else								\
1109a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
11108501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1111a138d217SRobert Watson } while (0)
1112df8bae1dSRodney W. Grimes 
11130a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
11140a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
11150a100a6fSAdrian Chadd 	if (val)							\
11160a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
11170a100a6fSAdrian Chadd 	else								\
11180a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
11190a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
11200a100a6fSAdrian Chadd } while (0)
11210a100a6fSAdrian Chadd 
1122df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1123df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1124df8bae1dSRodney W. Grimes 				break;
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1127df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1128df8bae1dSRodney W. Grimes 				break;
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1131df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1132df8bae1dSRodney W. Grimes 				break;
113382c23ebaSBill Fenner 
11344957466bSMatthew N. Dodd 			case IP_RECVTTL:
11354957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
11364957466bSMatthew N. Dodd 				break;
11374957466bSMatthew N. Dodd 
113882c23ebaSBill Fenner 			case IP_RECVIF:
113982c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
114082c23ebaSBill Fenner 				break;
11416a800098SYoshinobu Inoue 
11428afa2304SBruce M Simpson 			case IP_ONESBCAST:
11438afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
11448afa2304SBruce M Simpson 				break;
1145b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1146b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1147b2828ad2SAndre Oppermann 				break;
1148f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1149f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1150be9347e3SAdrian Chadd 				break;
11513cca425bSMichael Tuexen 			case IP_RECVTOS:
11523cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
11533cca425bSMichael Tuexen 				break;
11540a100a6fSAdrian Chadd 			case IP_BINDMULTI:
11550a100a6fSAdrian Chadd 				OPTSET2(INP_BINDMULTI, optval);
11560a100a6fSAdrian Chadd 				break;
11579d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
11589d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
11599d3ddf43SAdrian Chadd 				break;
11600a100a6fSAdrian Chadd #ifdef	RSS
11610a100a6fSAdrian Chadd 			case IP_RSS_LISTEN_BUCKET:
11620a100a6fSAdrian Chadd 				if ((optval >= 0) &&
11630a100a6fSAdrian Chadd 				    (optval < rss_getnumbuckets())) {
11640a100a6fSAdrian Chadd 					inp->inp_rss_listen_bucket = optval;
11650a100a6fSAdrian Chadd 					OPTSET2(INP_RSS_BUCKET_SET, 1);
11660a100a6fSAdrian Chadd 				} else {
11670a100a6fSAdrian Chadd 					error = EINVAL;
11680a100a6fSAdrian Chadd 				}
11690a100a6fSAdrian Chadd 				break;
11709d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
11719d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
11729d3ddf43SAdrian Chadd 				break;
11730a100a6fSAdrian Chadd #endif
1174df8bae1dSRodney W. Grimes 			}
1175df8bae1dSRodney W. Grimes 			break;
1176df8bae1dSRodney W. Grimes #undef OPTSET
11770a100a6fSAdrian Chadd #undef OPTSET2
1178df8bae1dSRodney W. Grimes 
117971498f30SBruce M Simpson 		/*
118071498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
118171498f30SBruce M Simpson 		 * module.
118271498f30SBruce M Simpson 		 */
1183df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1184f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1185df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1186df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1187df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1188df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
118971498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
119071498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
119171498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
119271498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
119371498f30SBruce M Simpson 		case IP_MSFILTER:
119471498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
119571498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
119671498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
119771498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
119871498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
119971498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
120071498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1201df8bae1dSRodney W. Grimes 			break;
1202df8bae1dSRodney W. Grimes 
120333b3ac06SPeter Wemm 		case IP_PORTRANGE:
1204cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1205cfe8b629SGarrett Wollman 					    sizeof optval);
1206cfe8b629SGarrett Wollman 			if (error)
1207cfe8b629SGarrett Wollman 				break;
120833b3ac06SPeter Wemm 
12098501a69cSRobert Watson 			INP_WLOCK(inp);
121033b3ac06SPeter Wemm 			switch (optval) {
121133b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
121233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
121333b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
121433b3ac06SPeter Wemm 				break;
121533b3ac06SPeter Wemm 
121633b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
121733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
121833b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
121933b3ac06SPeter Wemm 				break;
122033b3ac06SPeter Wemm 
122133b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
122233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
122333b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
122433b3ac06SPeter Wemm 				break;
122533b3ac06SPeter Wemm 
122633b3ac06SPeter Wemm 			default:
122733b3ac06SPeter Wemm 				error = EINVAL;
122833b3ac06SPeter Wemm 				break;
122933b3ac06SPeter Wemm 			}
12308501a69cSRobert Watson 			INP_WUNLOCK(inp);
1231ce8c72b1SPeter Wemm 			break;
123233b3ac06SPeter Wemm 
1233b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
12346a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12356a800098SYoshinobu Inoue 		{
12366a800098SYoshinobu Inoue 			caddr_t req;
12376a800098SYoshinobu Inoue 			struct mbuf *m;
12386a800098SYoshinobu Inoue 
12396a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
12406a800098SYoshinobu Inoue 				break;
12416a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12426a800098SYoshinobu Inoue 				break;
12436a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
124497aa4a51SBjoern A. Zeeb 			error = ipsec_set_policy(inp, sopt->sopt_name, req,
1245c26fe973SBjoern A. Zeeb 			    m->m_len, (sopt->sopt_td != NULL) ?
1246c26fe973SBjoern A. Zeeb 			    sopt->sopt_td->td_ucred : NULL);
12476a800098SYoshinobu Inoue 			m_freem(m);
12486a800098SYoshinobu Inoue 			break;
12496a800098SYoshinobu Inoue 		}
1250b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
12516a800098SYoshinobu Inoue 
1252df8bae1dSRodney W. Grimes 		default:
1253df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1254df8bae1dSRodney W. Grimes 			break;
1255df8bae1dSRodney W. Grimes 		}
1256df8bae1dSRodney W. Grimes 		break;
1257df8bae1dSRodney W. Grimes 
1258cfe8b629SGarrett Wollman 	case SOPT_GET:
1259cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1260df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1261df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1262cfe8b629SGarrett Wollman 			if (inp->inp_options)
1263cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1264cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1265cfe8b629SGarrett Wollman 							  char *),
1266cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1267cfe8b629SGarrett Wollman 			else
1268cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1269df8bae1dSRodney W. Grimes 			break;
1270df8bae1dSRodney W. Grimes 
1271df8bae1dSRodney W. Grimes 		case IP_TOS:
1272df8bae1dSRodney W. Grimes 		case IP_TTL:
1273936cd18dSAndre Oppermann 		case IP_MINTTL:
1274df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1275df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1276df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
12774957466bSMatthew N. Dodd 		case IP_RECVTTL:
127882c23ebaSBill Fenner 		case IP_RECVIF:
1279cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
12808afa2304SBruce M Simpson 		case IP_ONESBCAST:
1281b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
12825f6bf451SAttilio Rao 		case IP_BINDANY:
12833cca425bSMichael Tuexen 		case IP_RECVTOS:
12840a100a6fSAdrian Chadd 		case IP_BINDMULTI:
12859c423972SAdrian Chadd 		case IP_FLOWID:
12869c423972SAdrian Chadd 		case IP_FLOWTYPE:
12879d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
12880a100a6fSAdrian Chadd #ifdef	RSS
12890a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
12909d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
12910a100a6fSAdrian Chadd #endif
1292cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1293df8bae1dSRodney W. Grimes 
1294df8bae1dSRodney W. Grimes 			case IP_TOS:
1295ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1296df8bae1dSRodney W. Grimes 				break;
1297df8bae1dSRodney W. Grimes 
1298df8bae1dSRodney W. Grimes 			case IP_TTL:
1299ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1300df8bae1dSRodney W. Grimes 				break;
1301df8bae1dSRodney W. Grimes 
1302936cd18dSAndre Oppermann 			case IP_MINTTL:
1303936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1304936cd18dSAndre Oppermann 				break;
1305936cd18dSAndre Oppermann 
1306df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
13070a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1308df8bae1dSRodney W. Grimes 
1309df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1310df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1311df8bae1dSRodney W. Grimes 				break;
1312df8bae1dSRodney W. Grimes 
1313df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1314df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1315df8bae1dSRodney W. Grimes 				break;
1316df8bae1dSRodney W. Grimes 
1317df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1318df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1319df8bae1dSRodney W. Grimes 				break;
132082c23ebaSBill Fenner 
13214957466bSMatthew N. Dodd 			case IP_RECVTTL:
13224957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
13234957466bSMatthew N. Dodd 				break;
13244957466bSMatthew N. Dodd 
132582c23ebaSBill Fenner 			case IP_RECVIF:
132682c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
132782c23ebaSBill Fenner 				break;
1328cfe8b629SGarrett Wollman 
1329cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1330cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1331cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1332cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1333cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1334cfe8b629SGarrett Wollman 				else
1335cfe8b629SGarrett Wollman 					optval = 0;
1336cfe8b629SGarrett Wollman 				break;
13376a800098SYoshinobu Inoue 
13388afa2304SBruce M Simpson 			case IP_ONESBCAST:
13398afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
13408afa2304SBruce M Simpson 				break;
1341b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1342b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1343b2828ad2SAndre Oppermann 				break;
13445f6bf451SAttilio Rao 			case IP_BINDANY:
13455f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
13465f6bf451SAttilio Rao 				break;
13473cca425bSMichael Tuexen 			case IP_RECVTOS:
13483cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
13493cca425bSMichael Tuexen 				break;
13509c423972SAdrian Chadd 			case IP_FLOWID:
13519c423972SAdrian Chadd 				optval = inp->inp_flowid;
13529c423972SAdrian Chadd 				break;
13539c423972SAdrian Chadd 			case IP_FLOWTYPE:
13549c423972SAdrian Chadd 				optval = inp->inp_flowtype;
13559c423972SAdrian Chadd 				break;
13569d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
13579d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
13589d3ddf43SAdrian Chadd 				break;
13599c423972SAdrian Chadd #ifdef	RSS
13607847796aSAdrian Chadd 			case IP_RSSBUCKETID:
13617847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
13627847796aSAdrian Chadd 				    inp->inp_flowtype,
13637847796aSAdrian Chadd 				    &rss_bucket);
13647847796aSAdrian Chadd 				if (retval == 0)
13657847796aSAdrian Chadd 					optval = rss_bucket;
13667847796aSAdrian Chadd 				else
13677847796aSAdrian Chadd 					error = EINVAL;
13689c423972SAdrian Chadd 				break;
13699d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
13709d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
13719d3ddf43SAdrian Chadd 				break;
13729c423972SAdrian Chadd #endif
13730a100a6fSAdrian Chadd 			case IP_BINDMULTI:
13740a100a6fSAdrian Chadd 				optval = OPTBIT2(INP_BINDMULTI);
13750a100a6fSAdrian Chadd 				break;
1376df8bae1dSRodney W. Grimes 			}
1377cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1378df8bae1dSRodney W. Grimes 			break;
1379df8bae1dSRodney W. Grimes 
138071498f30SBruce M Simpson 		/*
138171498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
138271498f30SBruce M Simpson 		 * module.
138371498f30SBruce M Simpson 		 */
1384df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1385f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1386df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1387df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
138871498f30SBruce M Simpson 		case IP_MSFILTER:
138971498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
139033b3ac06SPeter Wemm 			break;
139133b3ac06SPeter Wemm 
1392b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
13936a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13946a800098SYoshinobu Inoue 		{
1395f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
13966a800098SYoshinobu Inoue 			caddr_t req = NULL;
1397686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
13986a800098SYoshinobu Inoue 
139999d628d5SPedro F. Giffuni 			if (m != NULL) {
14006a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1401686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1402686cdd19SJun-ichiro itojun Hagino 			}
140397aa4a51SBjoern A. Zeeb 			error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
14046a800098SYoshinobu Inoue 			if (error == 0)
14056a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1406f63e7634SYoshinobu Inoue 			if (error == 0)
14076a800098SYoshinobu Inoue 				m_freem(m);
14086a800098SYoshinobu Inoue 			break;
14096a800098SYoshinobu Inoue 		}
1410b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
14116a800098SYoshinobu Inoue 
1412df8bae1dSRodney W. Grimes 		default:
1413df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1414df8bae1dSRodney W. Grimes 			break;
1415df8bae1dSRodney W. Grimes 		}
1416df8bae1dSRodney W. Grimes 		break;
1417df8bae1dSRodney W. Grimes 	}
1418df8bae1dSRodney W. Grimes 	return (error);
1419df8bae1dSRodney W. Grimes }
1420df8bae1dSRodney W. Grimes 
1421df8bae1dSRodney W. Grimes /*
1422df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1423df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1424df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1425f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1426f5fea3ddSPaul Traina  * replicating that code here.
1427df8bae1dSRodney W. Grimes  */
1428df8bae1dSRodney W. Grimes static void
1429331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen)
1430df8bae1dSRodney W. Grimes {
1431331dff07SAlexander V. Chernikov 	struct ip *ip;
1432df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1433df8bae1dSRodney W. Grimes 
1434e4762f75SGeorge V. Neville-Neil 	/*
1435e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1436e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1437e4762f75SGeorge V. Neville-Neil 	 */
1438eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
1439f0cace5dSRobert Watson 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
144086b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1441df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1442390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1443390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1444390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1445390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1446390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1447390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1448390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1449390cdc6aSRuslan Ermilov 		}
1450df8bae1dSRodney W. Grimes 		/*
1451df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1452df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1453df8bae1dSRodney W. Grimes 		 */
14548f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1455df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
145686b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1457331dff07SAlexander V. Chernikov 		if_simloop(ifp, copym, AF_INET, 0);
1458df8bae1dSRodney W. Grimes 	}
1459df8bae1dSRodney W. Grimes }
1460