xref: /freebsd/sys/netinet/ip_output.c (revision 2e77d270c1bcf6a24dc17a8b27e844f36a8ddd54)
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"
366a800098SYoshinobu Inoue #include "opt_ipsec.h"
3753dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
38e440aed9SQing Li #include "opt_mpath.h"
3957f60867SMark Johnston #include "opt_route.h"
402f4afd21SRandall Stewart #include "opt_sctp.h"
419c423972SAdrian Chadd #include "opt_rss.h"
42fbd1372aSJoerg Wunsch 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
4426f9a767SRodney W. Grimes #include <sys/systm.h>
45f0f6d643SLuigi Rizzo #include <sys/kernel.h>
46cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49acd3428bSRobert Watson #include <sys/priv.h>
50c26fe973SBjoern A. Zeeb #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
52cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
5357f60867SMark Johnston #include <sys/sdt.h>
54df8bae1dSRodney W. Grimes #include <sys/socket.h>
55df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
569d9edc56SMike Silbersack #include <sys/sysctl.h>
57c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <net/if.h>
6076039bc8SGleb Smirnoff #include <net/if_var.h>
613ef5e21dSQing Li #include <net/if_llatbl.h>
629b932e9eSAndre Oppermann #include <net/netisr.h>
63c21fd232SAndre Oppermann #include <net/pfil.h>
64df8bae1dSRodney W. Grimes #include <net/route.h>
6565111ec7SKip Macy #include <net/flowtable.h>
66e440aed9SQing Li #ifdef RADIX_MPATH
67e440aed9SQing Li #include <net/radix_mpath.h>
68e440aed9SQing Li #endif
69b2bdc62aSAdrian Chadd #include <net/rss_config.h>
704b79449eSBjoern A. Zeeb #include <net/vnet.h>
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes #include <netinet/in.h>
7357f60867SMark Johnston #include <netinet/in_kdtrace.h>
74df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip.h>
76df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
779c423972SAdrian Chadd #include <netinet/in_rss.h>
78df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
79df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
80ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
812f4afd21SRandall Stewart #ifdef SCTP
822f4afd21SRandall Stewart #include <netinet/sctp.h>
832f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
842f4afd21SRandall Stewart #endif
85df8bae1dSRodney W. Grimes 
86b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
872cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h>
881dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
89b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/
906a800098SYoshinobu Inoue 
911dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
921dfcf0d2SAndre Oppermann 
93aed55708SRobert Watson #include <security/mac/mac_framework.h>
94aed55708SRobert Watson 
9553dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
9605b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0;
979d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
989d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
999d9edc56SMike Silbersack #endif
1009d9edc56SMike Silbersack 
101331dff07SAlexander V. Chernikov static void	ip_mloopback(struct ifnet *, const struct mbuf *, int);
102afed1b49SDarren Reed 
103afed1b49SDarren Reed 
1048b889dbbSBruce M Simpson extern int in_mcast_loop;
10593e0e116SJulian Elischer extern	struct protosw inetsw[];
10693e0e116SJulian Elischer 
107d9f2a782SErmal Luçi static inline int
1088f980c01SMark Johnston ip_output_pfil(struct mbuf **mp, struct ifnet *ifp, struct inpcb *inp,
109d9f2a782SErmal Luçi     struct sockaddr_in *dst, int *fibnum, int *error)
110d9f2a782SErmal Luçi {
111d9f2a782SErmal Luçi 	struct m_tag *fwd_tag = NULL;
1128f980c01SMark Johnston 	struct mbuf *m;
113d9f2a782SErmal Luçi 	struct in_addr odst;
114d9f2a782SErmal Luçi 	struct ip *ip;
115d9f2a782SErmal Luçi 
1168f980c01SMark Johnston 	m = *mp;
117d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
118d9f2a782SErmal Luçi 
119d9f2a782SErmal Luçi 	/* Run through list of hooks for output packets. */
120d9f2a782SErmal Luçi 	odst.s_addr = ip->ip_dst.s_addr;
1218f980c01SMark Johnston 	*error = pfil_run_hooks(&V_inet_pfil_hook, mp, ifp, PFIL_OUT, inp);
1228f980c01SMark Johnston 	m = *mp;
123d9f2a782SErmal Luçi 	if ((*error) != 0 || m == NULL)
124d9f2a782SErmal Luçi 		return 1; /* Finished */
125d9f2a782SErmal Luçi 
126d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
127d9f2a782SErmal Luçi 
128d9f2a782SErmal Luçi 	/* See if destination IP address was changed by packet filter. */
129d9f2a782SErmal Luçi 	if (odst.s_addr != ip->ip_dst.s_addr) {
130d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
131d9f2a782SErmal Luçi 		/* If destination is now ourself drop to ip_input(). */
132d9f2a782SErmal Luçi 		if (in_localip(ip->ip_dst)) {
133d9f2a782SErmal Luçi 			m->m_flags |= M_FASTFWD_OURS;
134d9f2a782SErmal Luçi 			if (m->m_pkthdr.rcvif == NULL)
135d9f2a782SErmal Luçi 				m->m_pkthdr.rcvif = V_loif;
136d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
137d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |=
138d9f2a782SErmal Luçi 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
139d9f2a782SErmal Luçi 				m->m_pkthdr.csum_data = 0xffff;
140d9f2a782SErmal Luçi 			}
141d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
142d9f2a782SErmal Luçi 				CSUM_IP_CHECKED | CSUM_IP_VALID;
143d9f2a782SErmal Luçi #ifdef SCTP
144d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
145d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
146d9f2a782SErmal Luçi #endif
147d9f2a782SErmal Luçi 			*error = netisr_queue(NETISR_IP, m);
148d9f2a782SErmal Luçi 			return 1; /* Finished */
149d9f2a782SErmal Luçi 		}
150d9f2a782SErmal Luçi 
151d9f2a782SErmal Luçi 		bzero(dst, sizeof(*dst));
152d9f2a782SErmal Luçi 		dst->sin_family = AF_INET;
153d9f2a782SErmal Luçi 		dst->sin_len = sizeof(*dst);
154d9f2a782SErmal Luçi 		dst->sin_addr = ip->ip_dst;
155d9f2a782SErmal Luçi 
156d9f2a782SErmal Luçi 		return -1; /* Reloop */
157d9f2a782SErmal Luçi 	}
158d9f2a782SErmal Luçi 	/* See if fib was changed by packet filter. */
159d9f2a782SErmal Luçi 	if ((*fibnum) != M_GETFIB(m)) {
160d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
161d9f2a782SErmal Luçi 		*fibnum = M_GETFIB(m);
162d9f2a782SErmal Luçi 		return -1; /* Reloop for FIB change */
163d9f2a782SErmal Luçi 	}
164d9f2a782SErmal Luçi 
165d9f2a782SErmal Luçi 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
166d9f2a782SErmal Luçi 	if (m->m_flags & M_FASTFWD_OURS) {
167d9f2a782SErmal Luçi 		if (m->m_pkthdr.rcvif == NULL)
168d9f2a782SErmal Luçi 			m->m_pkthdr.rcvif = V_loif;
169d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
170d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
171d9f2a782SErmal Luçi 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
172d9f2a782SErmal Luçi 			m->m_pkthdr.csum_data = 0xffff;
173d9f2a782SErmal Luçi 		}
174d9f2a782SErmal Luçi #ifdef SCTP
175d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
176d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
177d9f2a782SErmal Luçi #endif
178d9f2a782SErmal Luçi 		m->m_pkthdr.csum_flags |=
179d9f2a782SErmal Luçi 			CSUM_IP_CHECKED | CSUM_IP_VALID;
180d9f2a782SErmal Luçi 
181d9f2a782SErmal Luçi 		*error = netisr_queue(NETISR_IP, m);
182d9f2a782SErmal Luçi 		return 1; /* Finished */
183d9f2a782SErmal Luçi 	}
184d9f2a782SErmal Luçi 	/* Or forward to some other address? */
185d9f2a782SErmal Luçi 	if ((m->m_flags & M_IP_NEXTHOP) &&
186d9f2a782SErmal Luçi 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
187d9f2a782SErmal Luçi 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
188d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
189d9f2a782SErmal Luçi 		m->m_flags &= ~M_IP_NEXTHOP;
190d9f2a782SErmal Luçi 		m_tag_delete(m, fwd_tag);
191d9f2a782SErmal Luçi 
192d9f2a782SErmal Luçi 		return -1; /* Reloop for CHANGE of dst */
193d9f2a782SErmal Luçi 	}
194d9f2a782SErmal Luçi 
195d9f2a782SErmal Luçi 	return 0;
196d9f2a782SErmal Luçi }
197d9f2a782SErmal Luçi 
198df8bae1dSRodney W. Grimes /*
199df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
200df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
201df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
202df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
203bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
204bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
205bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
206bf984051SGleb Smirnoff  *
2073de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
2083de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
209df8bae1dSRodney W. Grimes  */
210df8bae1dSRodney W. Grimes int
211f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
212f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
213df8bae1dSRodney W. Grimes {
214cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
2151e78ac21SJeffrey Hsu 	struct ip *ip;
216fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
217ac9d7e26SMax Laier 	struct mbuf *m0;
21823bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
2193ae2ad08SJohn-Mark Gurney 	int mtu;
220ca8b83b0SLuigi Rizzo 	int error = 0;
221ca8b83b0SLuigi Rizzo 	struct sockaddr_in *dst;
2224c7a6059SGleb Smirnoff 	const struct sockaddr_in *gw;
2233c73180fSGleb Smirnoff 	struct in_ifaddr *ia;
22421d172a3SGleb Smirnoff 	int isbroadcast;
225078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
226e3f406b3SRuslan Ermilov 	struct route iproute;
227ec396e61SLuigi Rizzo 	struct rtentry *rte;	/* cache for ro->ro_rt */
2289c57a5b6SHiroki Sato 	uint32_t fibnum;
229fe82cbe8SGleb Smirnoff 	int have_ia_ref;
2301a499816SEdward Tomasz Napierala #ifdef IPSEC
2311a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
2321a499816SEdward Tomasz Napierala #endif
233ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
23436e8826fSMax Laier 
235bc97ba51SJulian Elischer 	if (inp != NULL) {
236baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
23762e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
238c2529042SHans Petter Selasky 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
23980cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
2409c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
24180cb9f21SKip Macy 		}
242bc97ba51SJulian Elischer 	}
24362e1ba83SRobert Watson 
24462e1ba83SRobert Watson 	if (ro == NULL) {
24562e1ba83SRobert Watson 		ro = &iproute;
24662e1ba83SRobert Watson 		bzero(ro, sizeof (*ro));
2476d768226SGeorge V. Neville-Neil 	} else
2486d768226SGeorge V. Neville-Neil 		ro->ro_flags |= RT_LLE_CACHE;
24962e1ba83SRobert Watson 
25053be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE
2510ff96b4fSGleb Smirnoff 	if (ro->ro_rt == NULL)
2520ff96b4fSGleb Smirnoff 		(void )flowtable_lookup(AF_INET, m, ro);
25353be8fcaSBjoern A. Zeeb #endif
2542b25acc1SLuigi Rizzo 
255df8bae1dSRodney W. Grimes 	if (opt) {
256ca8b83b0SLuigi Rizzo 		int len = 0;
257df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
258cb7641e8SMaxim Konovalov 		if (len != 0)
259ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
260df8bae1dSRodney W. Grimes 	}
261df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
2628f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
2638f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
2643efc3014SJulian Elischer 
265df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
26653be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
26753be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
2686d947416SGleb Smirnoff 		ip_fillid(ip);
26986425c62SRobert Watson 		IPSTAT_INC(ips_localout);
270df8bae1dSRodney W. Grimes 	} else {
271ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
27253be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
273df8bae1dSRodney W. Grimes 	}
2749c9137eaSGarrett Wollman 
275054692a4SAlexander V. Chernikov 	/*
276054692a4SAlexander V. Chernikov 	 * dst/gw handling:
277054692a4SAlexander V. Chernikov 	 *
2783c065f2fSGleb Smirnoff 	 * dst can be rewritten but always points to &ro->ro_dst.
2793c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
2803c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
281054692a4SAlexander V. Chernikov 	 */
2824c7a6059SGleb Smirnoff 	gw = dst = (struct sockaddr_in *)&ro->ro_dst;
2839c57a5b6SHiroki Sato 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
284d9f2a782SErmal Luçi 	rte = ro->ro_rt;
28584cc0778SGeorge V. Neville-Neil 	if (rte == NULL) {
286a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
287df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
288df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2899b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
290df8bae1dSRodney W. Grimes 	}
291d9f2a782SErmal Luçi again:
29284cc0778SGeorge V. Neville-Neil 	/*
29384cc0778SGeorge V. Neville-Neil 	 * Validate route against routing table additions;
29484cc0778SGeorge V. Neville-Neil 	 * a better/more specific route might have been added.
29584cc0778SGeorge V. Neville-Neil 	 */
29684cc0778SGeorge V. Neville-Neil 	if (inp)
29784cc0778SGeorge V. Neville-Neil 		RT_VALIDATE(ro, &inp->inp_rt_cookie, fibnum);
29884cc0778SGeorge V. Neville-Neil 	/*
29984cc0778SGeorge V. Neville-Neil 	 * If there is a cached route,
30084cc0778SGeorge V. Neville-Neil 	 * check that it is to the same destination
30184cc0778SGeorge V. Neville-Neil 	 * and is still up.  If not, free it and try again.
30284cc0778SGeorge V. Neville-Neil 	 * The address family should also be checked in case of sharing the
30384cc0778SGeorge V. Neville-Neil 	 * cache with IPv6.
30484cc0778SGeorge V. Neville-Neil 	 * Also check whether routing cache needs invalidation.
30584cc0778SGeorge V. Neville-Neil 	 */
30684cc0778SGeorge V. Neville-Neil 	rte = ro->ro_rt;
30784cc0778SGeorge V. Neville-Neil 	if (rte && ((rte->rt_flags & RTF_UP) == 0 ||
30884cc0778SGeorge V. Neville-Neil 		    rte->rt_ifp == NULL ||
30984cc0778SGeorge V. Neville-Neil 		    !RT_LINK_IS_UP(rte->rt_ifp) ||
31084cc0778SGeorge V. Neville-Neil 			  dst->sin_family != AF_INET ||
31184cc0778SGeorge V. Neville-Neil 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
31284cc0778SGeorge V. Neville-Neil 		RTFREE(rte);
31384cc0778SGeorge V. Neville-Neil 		rte = ro->ro_rt = (struct rtentry *)NULL;
3146d768226SGeorge V. Neville-Neil 		if (ro->ro_lle)
3156d768226SGeorge V. Neville-Neil 			LLE_FREE(ro->ro_lle);	/* zeros ro_lle */
3166d768226SGeorge V. Neville-Neil 		ro->ro_lle = (struct llentry *)NULL;
31784cc0778SGeorge V. Neville-Neil 	}
318d9f2a782SErmal Luçi 	ia = NULL;
319d9f2a782SErmal Luçi 	have_ia_ref = 0;
320df8bae1dSRodney W. Grimes 	/*
321a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
322a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
323a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
324a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
325df8bae1dSRodney W. Grimes 	 */
326a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
3274f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
32858a39d8cSAlan Somers 						      M_GETFIB(m)))) == NULL &&
3294f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
33058a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL) {
33186425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
332a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
333a3fd02d8SBruce M Simpson 			goto bad;
334a3fd02d8SBruce M Simpson 		}
335fe82cbe8SGleb Smirnoff 		have_ia_ref = 1;
336a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
337a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
338a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
339a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
340a3fd02d8SBruce M Simpson 		isbroadcast = 1;
341a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
3424f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
34358a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL &&
3444f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
34558a39d8cSAlan Somers 						M_GETFIB(m)))) == NULL) {
34686425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
347df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
348df8bae1dSRodney W. Grimes 			goto bad;
349df8bae1dSRodney W. Grimes 		}
350fe82cbe8SGleb Smirnoff 		have_ia_ref = 1;
351df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
352df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
3536c1bd558SRyan Stone 		isbroadcast = ifp->if_flags & IFF_BROADCAST ?
3546c1bd558SRyan Stone 		    in_ifaddr_broadcast(dst->sin_addr, ia) : 0;
3553afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
35638c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
3573afefa39SDaniel C. Sobral 		/*
35838c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
35938c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
3603afefa39SDaniel C. Sobral 		 */
36138c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
362cc0a3c8cSAndrey V. Elsukov 		IFP_TO_IA(ifp, ia, &in_ifa_tracker);
363fe82cbe8SGleb Smirnoff 		if (ia)
364fe82cbe8SGleb Smirnoff 			have_ia_ref = 1;
36538c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
366df8bae1dSRodney W. Grimes 	} else {
3672c17fe93SGarrett Wollman 		/*
36897d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
36997d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
37097d8d152SAndre Oppermann 		 * operation (as it is for ARP).
3712c17fe93SGarrett Wollman 		 */
372ec396e61SLuigi Rizzo 		if (rte == NULL) {
373e440aed9SQing Li #ifdef RADIX_MPATH
3748b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
3758b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
3769c57a5b6SHiroki Sato 			    fibnum);
377e440aed9SQing Li #else
3789c57a5b6SHiroki Sato 			in_rtalloc_ign(ro, 0, fibnum);
379e440aed9SQing Li #endif
380ec396e61SLuigi Rizzo 			rte = ro->ro_rt;
381ec396e61SLuigi Rizzo 		}
382c7ea0aa6SQing Li 		if (rte == NULL ||
383d9f2a782SErmal Luçi 		    (rte->rt_flags & RTF_UP) == 0 ||
384c7ea0aa6SQing Li 		    rte->rt_ifp == NULL ||
385c7ea0aa6SQing Li 		    !RT_LINK_IS_UP(rte->rt_ifp)) {
3861a499816SEdward Tomasz Napierala #ifdef IPSEC
3871a499816SEdward Tomasz Napierala 			/*
3881a499816SEdward Tomasz Napierala 			 * There is no route for this packet, but it is
3891a499816SEdward Tomasz Napierala 			 * possible that a matching SPD entry exists.
3901a499816SEdward Tomasz Napierala 			 */
3911a499816SEdward Tomasz Napierala 			no_route_but_check_spd = 1;
3921a499816SEdward Tomasz Napierala 			mtu = 0; /* Silence GCC warning. */
3931a499816SEdward Tomasz Napierala 			goto sendit;
3941a499816SEdward Tomasz Napierala #endif
39586425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
396df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
397df8bae1dSRodney W. Grimes 			goto bad;
398df8bae1dSRodney W. Grimes 		}
399ec396e61SLuigi Rizzo 		ia = ifatoia(rte->rt_ifa);
400ec396e61SLuigi Rizzo 		ifp = rte->rt_ifp;
401e3a7aa6fSGleb Smirnoff 		counter_u64_add(rte->rt_pksent, 1);
40236402a68SAlexander V. Chernikov 		rt_update_ro_flags(ro);
403ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_GATEWAY)
4044c7a6059SGleb Smirnoff 			gw = (struct sockaddr_in *)rte->rt_gateway;
405ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_HOST)
406ec396e61SLuigi Rizzo 			isbroadcast = (rte->rt_flags & RTF_BROADCAST);
4076c1bd558SRyan Stone 		else if (ifp->if_flags & IFF_BROADCAST)
40890cc51a1SRyan Stone 			isbroadcast = in_ifaddr_broadcast(gw->sin_addr, ia);
4096c1bd558SRyan Stone 		else
4106c1bd558SRyan Stone 			isbroadcast = 0;
411df8bae1dSRodney W. Grimes 	}
412d9f2a782SErmal Luçi 
4133ae2ad08SJohn-Mark Gurney 	/*
4143ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
4153ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
4163ae2ad08SJohn-Mark Gurney 	 */
4177f948f12SAlexander V. Chernikov 	if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST)))
418e3a7aa6fSGleb Smirnoff 		mtu = rte->rt_mtu;
4197f948f12SAlexander V. Chernikov 	else
4203ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
421c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
422c744cde4SBjoern A. Zeeb 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p",
423c744cde4SBjoern A. Zeeb 	    __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp));
424d9f2a782SErmal Luçi 
4259b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
426df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
427df8bae1dSRodney W. Grimes 		/*
428183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
429183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
430183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
431183e1c86SGleb Smirnoff 		 */
432183e1c86SGleb Smirnoff 		gw = dst;
433183e1c86SGleb Smirnoff 		/*
434df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
435df8bae1dSRodney W. Grimes 		 */
436df8bae1dSRodney W. Grimes 		if (imo != NULL) {
437df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
4381c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
4391c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
440bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
441bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
442bbb4330bSLuigi Rizzo 				    INADDR_ANY;
443df8bae1dSRodney W. Grimes 		} else
444df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
445df8bae1dSRodney W. Grimes 		/*
446df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
447df8bae1dSRodney W. Grimes 		 */
4481c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
449df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
45086425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
451df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
452df8bae1dSRodney W. Grimes 				goto bad;
453df8bae1dSRodney W. Grimes 			}
4541c5de19aSGarrett Wollman 		}
455df8bae1dSRodney W. Grimes 		/*
456df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
457df8bae1dSRodney W. Grimes 		 * of outgoing interface.
458df8bae1dSRodney W. Grimes 		 */
459df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
46038c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
46138c1bc35SRuslan Ermilov 			if (ia != NULL)
46238c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
463df8bae1dSRodney W. Grimes 		}
464df8bae1dSRodney W. Grimes 
4658b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
4668b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
467df8bae1dSRodney W. Grimes 			/*
4688b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
4698b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
4708b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
4718b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
4728b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
473df8bae1dSRodney W. Grimes 			 */
474331dff07SAlexander V. Chernikov 			ip_mloopback(ifp, m, hlen);
4758b889dbbSBruce M Simpson 		} else {
476df8bae1dSRodney W. Grimes 			/*
477df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
478df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
479df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
480df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
481df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
482df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
483df8bae1dSRodney W. Grimes 			 *
484df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
485df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
486df8bae1dSRodney W. Grimes 			 * if necessary.
487df8bae1dSRodney W. Grimes 			 */
488603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
489f0068c4aSGarrett Wollman 				/*
490bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
491f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
492f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
493f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
494f0068c4aSGarrett Wollman 				 */
495603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
496f0068c4aSGarrett Wollman 					imo = NULL;
497bbb4330bSLuigi Rizzo 				if (ip_mforward &&
498bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
499df8bae1dSRodney W. Grimes 					m_freem(m);
500df8bae1dSRodney W. Grimes 					goto done;
501df8bae1dSRodney W. Grimes 				}
502df8bae1dSRodney W. Grimes 			}
503df8bae1dSRodney W. Grimes 		}
5045e9ae478SGarrett Wollman 
505df8bae1dSRodney W. Grimes 		/*
506df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
507df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
508df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
509df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
5108b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
5118b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
5128b889dbbSBruce M Simpson 		 * the loopback interface.
513df8bae1dSRodney W. Grimes 		 */
514f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
515df8bae1dSRodney W. Grimes 			m_freem(m);
516df8bae1dSRodney W. Grimes 			goto done;
517df8bae1dSRodney W. Grimes 		}
518df8bae1dSRodney W. Grimes 
519df8bae1dSRodney W. Grimes 		goto sendit;
520df8bae1dSRodney W. Grimes 	}
5216a7c943cSAndre Oppermann 
522df8bae1dSRodney W. Grimes 	/*
5232b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
524cb459254SJohn-Mark Gurney 	 * of the outoing interface.
525df8bae1dSRodney W. Grimes 	 */
526f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
52738c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
52838c1bc35SRuslan Ermilov 		if (ia != NULL) {
529df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
530f9e354dfSJulian Elischer 		}
53138c1bc35SRuslan Ermilov 	}
5326a7c943cSAndre Oppermann 
533ca7a789aSMax Laier 	/*
534df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
5358c3f5566SRuslan Ermilov 	 * verify user is allowed to send
536df8bae1dSRodney W. Grimes 	 * such a packet.
537df8bae1dSRodney W. Grimes 	 */
5389f9b3dc4SGarrett Wollman 	if (isbroadcast) {
539df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
540df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
541df8bae1dSRodney W. Grimes 			goto bad;
542df8bae1dSRodney W. Grimes 		}
543df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
544df8bae1dSRodney W. Grimes 			error = EACCES;
545df8bae1dSRodney W. Grimes 			goto bad;
546df8bae1dSRodney W. Grimes 		}
547df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
5488f134647SGleb Smirnoff 		if (ip_len > mtu) {
549df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
550df8bae1dSRodney W. Grimes 			goto bad;
551df8bae1dSRodney W. Grimes 		}
552df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
5539f9b3dc4SGarrett Wollman 	} else {
554df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
5559f9b3dc4SGarrett Wollman 	}
556df8bae1dSRodney W. Grimes 
557f1743588SDarren Reed sendit:
558b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
5590275b2e3SAndrey V. Elsukov 	switch(ip_ipsec_output(&m, inp, &error)) {
5601dfcf0d2SAndre Oppermann 	case 1:
56133841545SHajimu UMEMOTO 		goto bad;
5621dfcf0d2SAndre Oppermann 	case -1:
5631dfcf0d2SAndre Oppermann 		goto done;
5641dfcf0d2SAndre Oppermann 	case 0:
56533841545SHajimu UMEMOTO 	default:
5661dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
56733841545SHajimu UMEMOTO 	}
5681a499816SEdward Tomasz Napierala 	/*
5691a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
5701a499816SEdward Tomasz Napierala 	 */
5711a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
5721a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
5731a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
5741a499816SEdward Tomasz Napierala 		goto bad;
5751a499816SEdward Tomasz Napierala 	}
5761dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
57733841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
57833841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
579b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
58033841545SHajimu UMEMOTO 
581c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
582d9f2a782SErmal Luçi 	if (PFIL_HOOKED(&V_inet_pfil_hook)) {
5838f980c01SMark Johnston 		switch (ip_output_pfil(&m, ifp, inp, dst, &fibnum, &error)) {
584d9f2a782SErmal Luçi 		case 1: /* Finished */
585c4ac87eaSDarren Reed 			goto done;
5869b932e9eSAndre Oppermann 
587d9f2a782SErmal Luçi 		case 0: /* Continue normally */
588c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
589d9f2a782SErmal Luçi 			break;
590fed1c7e9SSøren Schmidt 
591d9f2a782SErmal Luçi 		case -1: /* Need to try again */
592d9f2a782SErmal Luçi 			/* Reset everything for a new round */
5939c57a5b6SHiroki Sato 			RO_RTFREE(ro);
594fe82cbe8SGleb Smirnoff 			if (have_ia_ref)
595fe82cbe8SGleb Smirnoff 				ifa_free(&ia->ia_ifa);
5964fb3a820SAlexander V. Chernikov 			ro->ro_prepend = NULL;
597d9f2a782SErmal Luçi 			rte = NULL;
598d9f2a782SErmal Luçi 			gw = dst;
599d9f2a782SErmal Luçi 			ip = mtod(m, struct ip *);
6009b932e9eSAndre Oppermann 			goto again;
601d9f2a782SErmal Luçi 
602d9f2a782SErmal Luçi 		}
603099dd043SAndre Oppermann 	}
6042b25acc1SLuigi Rizzo 
60551c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
60651c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
60751c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
60851c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
60986425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
61051c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
61151c8ec4aSRuslan Ermilov 			goto bad;
61251c8ec4aSRuslan Ermilov 		}
61351c8ec4aSRuslan Ermilov 	}
61451c8ec4aSRuslan Ermilov 
615206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
616078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
617db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
618078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
619db4f9cc7SJonathan Lemon 	}
6202f4afd21SRandall Stewart #ifdef SCTP
621078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
6221966e5b5SRandall Stewart 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
623078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
6242f4afd21SRandall Stewart 	}
6252f4afd21SRandall Stewart #endif
626db4f9cc7SJonathan Lemon 
627e7319babSPoul-Henning Kamp 	/*
628db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
629233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
630df8bae1dSRodney W. Grimes 	 */
63121d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
632bf7dcda3SGleb Smirnoff 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
633df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
634078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
635df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
636078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
637078468edSGleb Smirnoff 		}
6385da9f8faSJosef Karthauser 
639233dcce1SAndre Oppermann 		/*
640233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
641233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
642233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
643233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
644233dcce1SAndre Oppermann 		 */
64538c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
646233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
6477caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
6487caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
649233dcce1SAndre Oppermann 			else
6507caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
6517caf4ab7SGleb Smirnoff 
6527caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
6535da9f8faSJosef Karthauser 		}
65453dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
6553390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
656eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
6579d9edc56SMike Silbersack #endif
658147f74d1SAndre Oppermann 		/*
659147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
660147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
661147f74d1SAndre Oppermann 		 */
66286bd0491SAndre Oppermann 		m_clrprotoflags(m);
66357f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
66447e8d432SGleb Smirnoff 		error = (*ifp->if_output)(ifp, m,
66547e8d432SGleb Smirnoff 		    (const struct sockaddr *)gw, ro);
666df8bae1dSRodney W. Grimes 		goto done;
667df8bae1dSRodney W. Grimes 	}
6681e78ac21SJeffrey Hsu 
669233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
67021d172a3SGleb Smirnoff 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
671df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
67286425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
673df8bae1dSRodney W. Grimes 		goto bad;
674df8bae1dSRodney W. Grimes 	}
6751e78ac21SJeffrey Hsu 
6761e78ac21SJeffrey Hsu 	/*
6771e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
6781e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
6791e78ac21SJeffrey Hsu 	 */
680078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
6811e78ac21SJeffrey Hsu 	if (error)
682df8bae1dSRodney W. Grimes 		goto bad;
6831e78ac21SJeffrey Hsu 	for (; m; m = m0) {
684df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
685b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
68607203494SDaniel C. Sobral 		if (error == 0) {
687fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
68807203494SDaniel C. Sobral 			if (ia != NULL) {
6897caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
6907caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
6917caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
69207203494SDaniel C. Sobral 			}
693147f74d1SAndre Oppermann 			/*
694147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
695147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
696147f74d1SAndre Oppermann 			 */
69786bd0491SAndre Oppermann 			m_clrprotoflags(m);
698fe937674SJosef Karthauser 
699*2e77d270SAndrey V. Elsukov 			IP_PROBE(send, NULL, NULL, mtod(m, struct ip *), ifp,
700*2e77d270SAndrey V. Elsukov 			    mtod(m, struct ip *), NULL);
701df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
70247e8d432SGleb Smirnoff 			    (const struct sockaddr *)gw, ro);
703fe937674SJosef Karthauser 		} else
704df8bae1dSRodney W. Grimes 			m_freem(m);
705df8bae1dSRodney W. Grimes 	}
706df8bae1dSRodney W. Grimes 
707df8bae1dSRodney W. Grimes 	if (error == 0)
70886425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
7091e78ac21SJeffrey Hsu 
710df8bae1dSRodney W. Grimes done:
711cc94f0c2SGleb Smirnoff 	if (ro == &iproute)
712bf984051SGleb Smirnoff 		RO_RTFREE(ro);
7133c402323SErmal Luçi 	else if (rte == NULL)
7143c402323SErmal Luçi 		/*
7153c402323SErmal Luçi 		 * If the caller supplied a route but somehow the reference
7163c402323SErmal Luçi 		 * to it has been released need to prevent the caller
7173c402323SErmal Luçi 		 * calling RTFREE on it again.
7183c402323SErmal Luçi 		 */
7193c402323SErmal Luçi 		ro->ro_rt = NULL;
720fe82cbe8SGleb Smirnoff 	if (have_ia_ref)
721fe82cbe8SGleb Smirnoff 		ifa_free(&ia->ia_ifa);
722df8bae1dSRodney W. Grimes 	return (error);
723df8bae1dSRodney W. Grimes bad:
7243528d68fSBill Paul 	m_freem(m);
725df8bae1dSRodney W. Grimes 	goto done;
726df8bae1dSRodney W. Grimes }
727df8bae1dSRodney W. Grimes 
7281e78ac21SJeffrey Hsu /*
7291e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
7301e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
7311e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
7321e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
7331e78ac21SJeffrey Hsu  *
7341e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
7351e78ac21SJeffrey Hsu  */
7361e78ac21SJeffrey Hsu int
7371e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
738078468edSGleb Smirnoff     u_long if_hwassist_flags)
7391e78ac21SJeffrey Hsu {
7401e78ac21SJeffrey Hsu 	int error = 0;
7411e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
7421e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
7431e78ac21SJeffrey Hsu 	int off;
7441e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
7451e78ac21SJeffrey Hsu 	int firstlen;
7461e78ac21SJeffrey Hsu 	struct mbuf **mnext;
7471e78ac21SJeffrey Hsu 	int nfrags;
74821d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
7491e78ac21SJeffrey Hsu 
75021d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
75121d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
75221d172a3SGleb Smirnoff 
75321d172a3SGleb Smirnoff 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
75486425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
7551e78ac21SJeffrey Hsu 		return EMSGSIZE;
7561e78ac21SJeffrey Hsu 	}
7571e78ac21SJeffrey Hsu 
7581e78ac21SJeffrey Hsu 	/*
7591e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
7601e78ac21SJeffrey Hsu 	 */
7611e78ac21SJeffrey Hsu 	if (len < 8)
7621e78ac21SJeffrey Hsu 		return EMSGSIZE;
7631e78ac21SJeffrey Hsu 
7641e78ac21SJeffrey Hsu 	/*
7651e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
7661e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
7671e78ac21SJeffrey Hsu 	 */
768da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
7691e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
7701e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
7711e78ac21SJeffrey Hsu 	}
7722f4afd21SRandall Stewart #ifdef SCTP
773da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
7741966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
7752f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
7762f4afd21SRandall Stewart 	}
7772f4afd21SRandall Stewart #endif
7781e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
7791e78ac21SJeffrey Hsu 		/*
7801e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
7811e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
7821e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
7831e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
7841e78ac21SJeffrey Hsu 		 *
7851e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
7861e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
7871e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
7881e78ac21SJeffrey Hsu 		 */
7891e78ac21SJeffrey Hsu 		int newlen;
7901e78ac21SJeffrey Hsu 
7919c0f6aa7SHans Petter Selasky 		off = MIN(mtu, m0->m_pkthdr.len);
7921e78ac21SJeffrey Hsu 
7931e78ac21SJeffrey Hsu 		/*
7941e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
7951e78ac21SJeffrey Hsu 		 * 8-byte boundary
7961e78ac21SJeffrey Hsu 		 */
7971e78ac21SJeffrey Hsu 		if (off < hlen)
7981e78ac21SJeffrey Hsu 			goto smart_frag_failure;
7991e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
8001e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
8011e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
8021e78ac21SJeffrey Hsu 			/* we failed, go back the default */
8031e78ac21SJeffrey Hsu smart_frag_failure:
8041e78ac21SJeffrey Hsu 			newlen = len;
8051e78ac21SJeffrey Hsu 			off = hlen + len;
8061e78ac21SJeffrey Hsu 		}
8071e78ac21SJeffrey Hsu 		len = newlen;
8081e78ac21SJeffrey Hsu 
8091e78ac21SJeffrey Hsu 	} else {
8101e78ac21SJeffrey Hsu 		off = hlen + len;
8111e78ac21SJeffrey Hsu 	}
8121e78ac21SJeffrey Hsu 
8131e78ac21SJeffrey Hsu 	firstlen = off - hlen;
8141e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
8151e78ac21SJeffrey Hsu 
8161e78ac21SJeffrey Hsu 	/*
8171e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
8181e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
8191e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
8201e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
8211e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
8221e78ac21SJeffrey Hsu 	 */
82321d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
8241e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
8251e78ac21SJeffrey Hsu 		struct mbuf *m;
8261e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
8271e78ac21SJeffrey Hsu 
828dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
8290b17fba7SAndre Oppermann 		if (m == NULL) {
8301e78ac21SJeffrey Hsu 			error = ENOBUFS;
83186425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8321e78ac21SJeffrey Hsu 			goto done;
8331e78ac21SJeffrey Hsu 		}
834c4c4346fSHans Petter Selasky 		/*
835c4c4346fSHans Petter Selasky 		 * Make sure the complete packet header gets copied
836c4c4346fSHans Petter Selasky 		 * from the originating mbuf to the newly created
837c4c4346fSHans Petter Selasky 		 * mbuf. This also ensures that existing firewall
838c4c4346fSHans Petter Selasky 		 * classification(s), VLAN tags and so on get copied
839c4c4346fSHans Petter Selasky 		 * to the resulting fragmented packet(s):
840c4c4346fSHans Petter Selasky 		 */
841c4c4346fSHans Petter Selasky 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
842c4c4346fSHans Petter Selasky 			m_free(m);
843c4c4346fSHans Petter Selasky 			error = ENOBUFS;
844c4c4346fSHans Petter Selasky 			IPSTAT_INC(ips_odropped);
845c4c4346fSHans Petter Selasky 			goto done;
846c4c4346fSHans Petter Selasky 		}
8471e78ac21SJeffrey Hsu 		/*
8481e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
8491e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
8508b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
8511e78ac21SJeffrey Hsu 		 */
8521e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
8531e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
8541e78ac21SJeffrey Hsu 		*mhip = *ip;
8551e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
8561e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
8571e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
8581e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
8591e78ac21SJeffrey Hsu 		}
8601e78ac21SJeffrey Hsu 		m->m_len = mhlen;
86121d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
86221d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
863fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
86421d172a3SGleb Smirnoff 			len = ip_len - off;
865fb86dfcdSAndre Oppermann 		else
8661e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
8671e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
868eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
8690b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
8701e78ac21SJeffrey Hsu 			m_free(m);
8711e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
87286425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8731e78ac21SJeffrey Hsu 			goto done;
8741e78ac21SJeffrey Hsu 		}
8751e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
8761e78ac21SJeffrey Hsu #ifdef MAC
87730d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
8781e78ac21SJeffrey Hsu #endif
8791e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
8801e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
881078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
8821e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
883078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
884078468edSGleb Smirnoff 		}
8851e78ac21SJeffrey Hsu 		*mnext = m;
8861e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
8871e78ac21SJeffrey Hsu 	}
88886425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
8891e78ac21SJeffrey Hsu 
8901e78ac21SJeffrey Hsu 	/*
8911e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
8921e78ac21SJeffrey Hsu 	 * and updating header.
8931e78ac21SJeffrey Hsu 	 */
89421d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
8951e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
8961e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
89721d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
8981e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
899078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
9001e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
901078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
902078468edSGleb Smirnoff 	}
9031e78ac21SJeffrey Hsu 
9041e78ac21SJeffrey Hsu done:
9051e78ac21SJeffrey Hsu 	*m_frag = m0;
9061e78ac21SJeffrey Hsu 	return error;
9071e78ac21SJeffrey Hsu }
9081e78ac21SJeffrey Hsu 
9091c238475SJonathan Lemon void
910db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
911db4f9cc7SJonathan Lemon {
912db4f9cc7SJonathan Lemon 	struct ip *ip;
91323e9c6dcSGleb Smirnoff 	uint16_t csum, offset, ip_len;
914db4f9cc7SJonathan Lemon 
915db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
91653be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
91723e9c6dcSGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
91823e9c6dcSGleb Smirnoff 	csum = in_cksum_skip(m, ip_len, offset);
919206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
920206a3274SRuslan Ermilov 		csum = 0xffff;
921db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
922db4f9cc7SJonathan Lemon 
9238e1d0a56SMichael Tuexen 	/* find the mbuf in the chain where the checksum starts*/
9248e1d0a56SMichael Tuexen 	while ((m != NULL) && (offset >= m->m_len)) {
9258e1d0a56SMichael Tuexen 		offset -= m->m_len;
9268e1d0a56SMichael Tuexen 		m = m->m_next;
9278e1d0a56SMichael Tuexen 	}
92826461454SMichael Tuexen 	KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain."));
92926461454SMichael Tuexen 	KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs."));
930db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
931db4f9cc7SJonathan Lemon }
932db4f9cc7SJonathan Lemon 
933df8bae1dSRodney W. Grimes /*
934df8bae1dSRodney W. Grimes  * IP socket option processing.
935df8bae1dSRodney W. Grimes  */
936df8bae1dSRodney W. Grimes int
937f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
938df8bae1dSRodney W. Grimes {
939cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
940cfe8b629SGarrett Wollman 	int	error, optval;
941dc847eb6SAdrian Chadd #ifdef	RSS
942dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
943dc847eb6SAdrian Chadd 	int retval;
944dc847eb6SAdrian Chadd #endif
945df8bae1dSRodney W. Grimes 
946cfe8b629SGarrett Wollman 	error = optval = 0;
947cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
948fc06cd42SMikolaj Golub 		error = EINVAL;
949fc06cd42SMikolaj Golub 
950fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
951fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
952fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
953fc06cd42SMikolaj Golub 			case SO_REUSEADDR:
954fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
955efdf104bSMikolaj Golub 				if ((so->so_options & SO_REUSEADDR) != 0)
956efdf104bSMikolaj Golub 					inp->inp_flags2 |= INP_REUSEADDR;
957fc06cd42SMikolaj Golub 				else
958efdf104bSMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEADDR;
959fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
960fc06cd42SMikolaj Golub 				error = 0;
961fc06cd42SMikolaj Golub 				break;
962fc06cd42SMikolaj Golub 			case SO_REUSEPORT:
963fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
964fc06cd42SMikolaj Golub 				if ((so->so_options & SO_REUSEPORT) != 0)
965fc06cd42SMikolaj Golub 					inp->inp_flags2 |= INP_REUSEPORT;
966fc06cd42SMikolaj Golub 				else
967fc06cd42SMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEPORT;
968fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
969fc06cd42SMikolaj Golub 				error = 0;
970fc06cd42SMikolaj Golub 				break;
971fc06cd42SMikolaj Golub 			case SO_SETFIB:
972fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
973fc06cd42SMikolaj Golub 				inp->inp_inc.inc_fibnum = so->so_fibnum;
974fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
975fc06cd42SMikolaj Golub 				error = 0;
976fc06cd42SMikolaj Golub 				break;
977fc06cd42SMikolaj Golub 			default:
978fc06cd42SMikolaj Golub 				break;
979fc06cd42SMikolaj Golub 			}
980fc06cd42SMikolaj Golub 		}
981fc06cd42SMikolaj Golub 		return (error);
982cfe8b629SGarrett Wollman 	}
983df8bae1dSRodney W. Grimes 
984cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
985cfe8b629SGarrett Wollman 	case SOPT_SET:
986cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
987df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
988df8bae1dSRodney W. Grimes #ifdef notyet
989df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
990df8bae1dSRodney W. Grimes #endif
991cfe8b629SGarrett Wollman 		{
992cfe8b629SGarrett Wollman 			struct mbuf *m;
993cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
994cfe8b629SGarrett Wollman 				error = EMSGSIZE;
995cfe8b629SGarrett Wollman 				break;
996cfe8b629SGarrett Wollman 			}
997dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
9980b17fba7SAndre Oppermann 			if (m == NULL) {
999cfe8b629SGarrett Wollman 				error = ENOBUFS;
1000cfe8b629SGarrett Wollman 				break;
1001cfe8b629SGarrett Wollman 			}
1002cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1003cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1004cfe8b629SGarrett Wollman 					    m->m_len);
1005635354c4SMaxim Konovalov 			if (error) {
1006635354c4SMaxim Konovalov 				m_free(m);
1007635354c4SMaxim Konovalov 				break;
1008635354c4SMaxim Konovalov 			}
10098501a69cSRobert Watson 			INP_WLOCK(inp);
1010993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
10118501a69cSRobert Watson 			INP_WUNLOCK(inp);
1012993d9505SRobert Watson 			return (error);
1013cfe8b629SGarrett Wollman 		}
1014df8bae1dSRodney W. Grimes 
1015f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
1016f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
1017f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
1018f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
1019f44270e7SPawel Jakub Dawidek 				if (error)
1020be9347e3SAdrian Chadd 					break;
1021be9347e3SAdrian Chadd 			}
1022cef27294SAdrian Chadd 			/* FALLTHROUGH */
10230a100a6fSAdrian Chadd 		case IP_BINDMULTI:
10240a100a6fSAdrian Chadd #ifdef	RSS
10250a100a6fSAdrian Chadd 		case IP_RSS_LISTEN_BUCKET:
10260a100a6fSAdrian Chadd #endif
1027df8bae1dSRodney W. Grimes 		case IP_TOS:
1028df8bae1dSRodney W. Grimes 		case IP_TTL:
1029936cd18dSAndre Oppermann 		case IP_MINTTL:
1030df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1031df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1032df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
10334957466bSMatthew N. Dodd 		case IP_RECVTTL:
103482c23ebaSBill Fenner 		case IP_RECVIF:
10358afa2304SBruce M Simpson 		case IP_ONESBCAST:
1036b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
10373cca425bSMichael Tuexen 		case IP_RECVTOS:
10389d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
10399d3ddf43SAdrian Chadd #ifdef	RSS
10409d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
10419d3ddf43SAdrian Chadd #endif
1042cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1043cfe8b629SGarrett Wollman 					    sizeof optval);
1044cfe8b629SGarrett Wollman 			if (error)
1045cfe8b629SGarrett Wollman 				break;
1046df8bae1dSRodney W. Grimes 
1047cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1048df8bae1dSRodney W. Grimes 			case IP_TOS:
1049ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1050df8bae1dSRodney W. Grimes 				break;
1051df8bae1dSRodney W. Grimes 
1052df8bae1dSRodney W. Grimes 			case IP_TTL:
1053ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1054df8bae1dSRodney W. Grimes 				break;
1055936cd18dSAndre Oppermann 
1056936cd18dSAndre Oppermann 			case IP_MINTTL:
1057a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1058936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1059936cd18dSAndre Oppermann 				else
1060936cd18dSAndre Oppermann 					error = EINVAL;
1061936cd18dSAndre Oppermann 				break;
1062936cd18dSAndre Oppermann 
1063a138d217SRobert Watson #define	OPTSET(bit) do {						\
10648501a69cSRobert Watson 	INP_WLOCK(inp);							\
1065df8bae1dSRodney W. Grimes 	if (optval)							\
1066df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1067df8bae1dSRodney W. Grimes 	else								\
1068a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
10698501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1070a138d217SRobert Watson } while (0)
1071df8bae1dSRodney W. Grimes 
10720a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
10730a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
10740a100a6fSAdrian Chadd 	if (val)							\
10750a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
10760a100a6fSAdrian Chadd 	else								\
10770a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
10780a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
10790a100a6fSAdrian Chadd } while (0)
10800a100a6fSAdrian Chadd 
1081df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1082df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1083df8bae1dSRodney W. Grimes 				break;
1084df8bae1dSRodney W. Grimes 
1085df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1086df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1087df8bae1dSRodney W. Grimes 				break;
1088df8bae1dSRodney W. Grimes 
1089df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1090df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1091df8bae1dSRodney W. Grimes 				break;
109282c23ebaSBill Fenner 
10934957466bSMatthew N. Dodd 			case IP_RECVTTL:
10944957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
10954957466bSMatthew N. Dodd 				break;
10964957466bSMatthew N. Dodd 
109782c23ebaSBill Fenner 			case IP_RECVIF:
109882c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
109982c23ebaSBill Fenner 				break;
11006a800098SYoshinobu Inoue 
11018afa2304SBruce M Simpson 			case IP_ONESBCAST:
11028afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
11038afa2304SBruce M Simpson 				break;
1104b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1105b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1106b2828ad2SAndre Oppermann 				break;
1107f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1108f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1109be9347e3SAdrian Chadd 				break;
11103cca425bSMichael Tuexen 			case IP_RECVTOS:
11113cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
11123cca425bSMichael Tuexen 				break;
11130a100a6fSAdrian Chadd 			case IP_BINDMULTI:
11140a100a6fSAdrian Chadd 				OPTSET2(INP_BINDMULTI, optval);
11150a100a6fSAdrian Chadd 				break;
11169d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
11179d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
11189d3ddf43SAdrian Chadd 				break;
11190a100a6fSAdrian Chadd #ifdef	RSS
11200a100a6fSAdrian Chadd 			case IP_RSS_LISTEN_BUCKET:
11210a100a6fSAdrian Chadd 				if ((optval >= 0) &&
11220a100a6fSAdrian Chadd 				    (optval < rss_getnumbuckets())) {
11230a100a6fSAdrian Chadd 					inp->inp_rss_listen_bucket = optval;
11240a100a6fSAdrian Chadd 					OPTSET2(INP_RSS_BUCKET_SET, 1);
11250a100a6fSAdrian Chadd 				} else {
11260a100a6fSAdrian Chadd 					error = EINVAL;
11270a100a6fSAdrian Chadd 				}
11280a100a6fSAdrian Chadd 				break;
11299d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
11309d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
11319d3ddf43SAdrian Chadd 				break;
11320a100a6fSAdrian Chadd #endif
1133df8bae1dSRodney W. Grimes 			}
1134df8bae1dSRodney W. Grimes 			break;
1135df8bae1dSRodney W. Grimes #undef OPTSET
11360a100a6fSAdrian Chadd #undef OPTSET2
1137df8bae1dSRodney W. Grimes 
113871498f30SBruce M Simpson 		/*
113971498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
114071498f30SBruce M Simpson 		 * module.
114171498f30SBruce M Simpson 		 */
1142df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1143f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1144df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1145df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1146df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1147df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
114871498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
114971498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
115071498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
115171498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
115271498f30SBruce M Simpson 		case IP_MSFILTER:
115371498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
115471498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
115571498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
115671498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
115771498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
115871498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
115971498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1160df8bae1dSRodney W. Grimes 			break;
1161df8bae1dSRodney W. Grimes 
116233b3ac06SPeter Wemm 		case IP_PORTRANGE:
1163cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1164cfe8b629SGarrett Wollman 					    sizeof optval);
1165cfe8b629SGarrett Wollman 			if (error)
1166cfe8b629SGarrett Wollman 				break;
116733b3ac06SPeter Wemm 
11688501a69cSRobert Watson 			INP_WLOCK(inp);
116933b3ac06SPeter Wemm 			switch (optval) {
117033b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
117133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
117233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
117333b3ac06SPeter Wemm 				break;
117433b3ac06SPeter Wemm 
117533b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
117633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
117733b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
117833b3ac06SPeter Wemm 				break;
117933b3ac06SPeter Wemm 
118033b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
118133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
118233b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
118333b3ac06SPeter Wemm 				break;
118433b3ac06SPeter Wemm 
118533b3ac06SPeter Wemm 			default:
118633b3ac06SPeter Wemm 				error = EINVAL;
118733b3ac06SPeter Wemm 				break;
118833b3ac06SPeter Wemm 			}
11898501a69cSRobert Watson 			INP_WUNLOCK(inp);
1190ce8c72b1SPeter Wemm 			break;
119133b3ac06SPeter Wemm 
1192b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11936a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
11946a800098SYoshinobu Inoue 		{
11956a800098SYoshinobu Inoue 			caddr_t req;
11966a800098SYoshinobu Inoue 			struct mbuf *m;
11976a800098SYoshinobu Inoue 
11986a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
11996a800098SYoshinobu Inoue 				break;
12006a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12016a800098SYoshinobu Inoue 				break;
12026a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
120397aa4a51SBjoern A. Zeeb 			error = ipsec_set_policy(inp, sopt->sopt_name, req,
1204c26fe973SBjoern A. Zeeb 			    m->m_len, (sopt->sopt_td != NULL) ?
1205c26fe973SBjoern A. Zeeb 			    sopt->sopt_td->td_ucred : NULL);
12066a800098SYoshinobu Inoue 			m_freem(m);
12076a800098SYoshinobu Inoue 			break;
12086a800098SYoshinobu Inoue 		}
1209b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
12106a800098SYoshinobu Inoue 
1211df8bae1dSRodney W. Grimes 		default:
1212df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1213df8bae1dSRodney W. Grimes 			break;
1214df8bae1dSRodney W. Grimes 		}
1215df8bae1dSRodney W. Grimes 		break;
1216df8bae1dSRodney W. Grimes 
1217cfe8b629SGarrett Wollman 	case SOPT_GET:
1218cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1219df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1220df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1221cfe8b629SGarrett Wollman 			if (inp->inp_options)
1222cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1223cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1224cfe8b629SGarrett Wollman 							  char *),
1225cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1226cfe8b629SGarrett Wollman 			else
1227cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1228df8bae1dSRodney W. Grimes 			break;
1229df8bae1dSRodney W. Grimes 
1230df8bae1dSRodney W. Grimes 		case IP_TOS:
1231df8bae1dSRodney W. Grimes 		case IP_TTL:
1232936cd18dSAndre Oppermann 		case IP_MINTTL:
1233df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1234df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1235df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
12364957466bSMatthew N. Dodd 		case IP_RECVTTL:
123782c23ebaSBill Fenner 		case IP_RECVIF:
1238cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
12398afa2304SBruce M Simpson 		case IP_ONESBCAST:
1240b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
12415f6bf451SAttilio Rao 		case IP_BINDANY:
12423cca425bSMichael Tuexen 		case IP_RECVTOS:
12430a100a6fSAdrian Chadd 		case IP_BINDMULTI:
12449c423972SAdrian Chadd 		case IP_FLOWID:
12459c423972SAdrian Chadd 		case IP_FLOWTYPE:
12469d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
12470a100a6fSAdrian Chadd #ifdef	RSS
12480a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
12499d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
12500a100a6fSAdrian Chadd #endif
1251cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1252df8bae1dSRodney W. Grimes 
1253df8bae1dSRodney W. Grimes 			case IP_TOS:
1254ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1255df8bae1dSRodney W. Grimes 				break;
1256df8bae1dSRodney W. Grimes 
1257df8bae1dSRodney W. Grimes 			case IP_TTL:
1258ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1259df8bae1dSRodney W. Grimes 				break;
1260df8bae1dSRodney W. Grimes 
1261936cd18dSAndre Oppermann 			case IP_MINTTL:
1262936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1263936cd18dSAndre Oppermann 				break;
1264936cd18dSAndre Oppermann 
1265df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
12660a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1269df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1270df8bae1dSRodney W. Grimes 				break;
1271df8bae1dSRodney W. Grimes 
1272df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1273df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1274df8bae1dSRodney W. Grimes 				break;
1275df8bae1dSRodney W. Grimes 
1276df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1277df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1278df8bae1dSRodney W. Grimes 				break;
127982c23ebaSBill Fenner 
12804957466bSMatthew N. Dodd 			case IP_RECVTTL:
12814957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
12824957466bSMatthew N. Dodd 				break;
12834957466bSMatthew N. Dodd 
128482c23ebaSBill Fenner 			case IP_RECVIF:
128582c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
128682c23ebaSBill Fenner 				break;
1287cfe8b629SGarrett Wollman 
1288cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1289cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1290cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1291cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1292cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1293cfe8b629SGarrett Wollman 				else
1294cfe8b629SGarrett Wollman 					optval = 0;
1295cfe8b629SGarrett Wollman 				break;
12966a800098SYoshinobu Inoue 
12978afa2304SBruce M Simpson 			case IP_ONESBCAST:
12988afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
12998afa2304SBruce M Simpson 				break;
1300b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1301b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1302b2828ad2SAndre Oppermann 				break;
13035f6bf451SAttilio Rao 			case IP_BINDANY:
13045f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
13055f6bf451SAttilio Rao 				break;
13063cca425bSMichael Tuexen 			case IP_RECVTOS:
13073cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
13083cca425bSMichael Tuexen 				break;
13099c423972SAdrian Chadd 			case IP_FLOWID:
13109c423972SAdrian Chadd 				optval = inp->inp_flowid;
13119c423972SAdrian Chadd 				break;
13129c423972SAdrian Chadd 			case IP_FLOWTYPE:
13139c423972SAdrian Chadd 				optval = inp->inp_flowtype;
13149c423972SAdrian Chadd 				break;
13159d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
13169d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
13179d3ddf43SAdrian Chadd 				break;
13189c423972SAdrian Chadd #ifdef	RSS
13197847796aSAdrian Chadd 			case IP_RSSBUCKETID:
13207847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
13217847796aSAdrian Chadd 				    inp->inp_flowtype,
13227847796aSAdrian Chadd 				    &rss_bucket);
13237847796aSAdrian Chadd 				if (retval == 0)
13247847796aSAdrian Chadd 					optval = rss_bucket;
13257847796aSAdrian Chadd 				else
13267847796aSAdrian Chadd 					error = EINVAL;
13279c423972SAdrian Chadd 				break;
13289d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
13299d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
13309d3ddf43SAdrian Chadd 				break;
13319c423972SAdrian Chadd #endif
13320a100a6fSAdrian Chadd 			case IP_BINDMULTI:
13330a100a6fSAdrian Chadd 				optval = OPTBIT2(INP_BINDMULTI);
13340a100a6fSAdrian Chadd 				break;
1335df8bae1dSRodney W. Grimes 			}
1336cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1337df8bae1dSRodney W. Grimes 			break;
1338df8bae1dSRodney W. Grimes 
133971498f30SBruce M Simpson 		/*
134071498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
134171498f30SBruce M Simpson 		 * module.
134271498f30SBruce M Simpson 		 */
1343df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1344f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1345df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1346df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
134771498f30SBruce M Simpson 		case IP_MSFILTER:
134871498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
134933b3ac06SPeter Wemm 			break;
135033b3ac06SPeter Wemm 
1351b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
13526a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13536a800098SYoshinobu Inoue 		{
1354f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
13556a800098SYoshinobu Inoue 			caddr_t req = NULL;
1356686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
13576a800098SYoshinobu Inoue 
135899d628d5SPedro F. Giffuni 			if (m != NULL) {
13596a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1360686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1361686cdd19SJun-ichiro itojun Hagino 			}
136297aa4a51SBjoern A. Zeeb 			error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
13636a800098SYoshinobu Inoue 			if (error == 0)
13646a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1365f63e7634SYoshinobu Inoue 			if (error == 0)
13666a800098SYoshinobu Inoue 				m_freem(m);
13676a800098SYoshinobu Inoue 			break;
13686a800098SYoshinobu Inoue 		}
1369b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
13706a800098SYoshinobu Inoue 
1371df8bae1dSRodney W. Grimes 		default:
1372df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1373df8bae1dSRodney W. Grimes 			break;
1374df8bae1dSRodney W. Grimes 		}
1375df8bae1dSRodney W. Grimes 		break;
1376df8bae1dSRodney W. Grimes 	}
1377df8bae1dSRodney W. Grimes 	return (error);
1378df8bae1dSRodney W. Grimes }
1379df8bae1dSRodney W. Grimes 
1380df8bae1dSRodney W. Grimes /*
1381df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1382df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1383df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1384f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1385f5fea3ddSPaul Traina  * replicating that code here.
1386df8bae1dSRodney W. Grimes  */
1387df8bae1dSRodney W. Grimes static void
1388331dff07SAlexander V. Chernikov ip_mloopback(struct ifnet *ifp, const struct mbuf *m, int hlen)
1389df8bae1dSRodney W. Grimes {
1390331dff07SAlexander V. Chernikov 	struct ip *ip;
1391df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1392df8bae1dSRodney W. Grimes 
1393e4762f75SGeorge V. Neville-Neil 	/*
1394e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1395e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1396e4762f75SGeorge V. Neville-Neil 	 */
1397eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
1398f0cace5dSRobert Watson 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
139986b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1400df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1401390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1402390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1403390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1404390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1405390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1406390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1407390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1408390cdc6aSRuslan Ermilov 		}
1409df8bae1dSRodney W. Grimes 		/*
1410df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1411df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1412df8bae1dSRodney W. Grimes 		 */
14138f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1414df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
141586b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1416331dff07SAlexander V. Chernikov 		if_simloop(ifp, copym, AF_INET, 0);
1417df8bae1dSRodney W. Grimes 	}
1418df8bae1dSRodney W. Grimes }
1419