xref: /freebsd/sys/netinet/ip_output.c (revision 9d3ddf438402c8e711dd639be6052d51cb45b680)
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"
361f7e052cSJulian Elischer #include "opt_ipfw.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>
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>
5257f60867SMark Johnston #include <sys/sdt.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
559d9edc56SMike Silbersack #include <sys/sysctl.h>
56c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <net/if.h>
5976039bc8SGleb Smirnoff #include <net/if_var.h>
603ef5e21dSQing Li #include <net/if_llatbl.h>
619b932e9eSAndre Oppermann #include <net/netisr.h>
62c21fd232SAndre Oppermann #include <net/pfil.h>
63df8bae1dSRodney W. Grimes #include <net/route.h>
6465111ec7SKip Macy #include <net/flowtable.h>
65e440aed9SQing Li #ifdef RADIX_MPATH
66e440aed9SQing Li #include <net/radix_mpath.h>
67e440aed9SQing Li #endif
684b79449eSBjoern A. Zeeb #include <net/vnet.h>
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes #include <netinet/in.h>
7157f60867SMark Johnston #include <netinet/in_kdtrace.h>
72df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
73df8bae1dSRodney W. Grimes #include <netinet/ip.h>
74df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
759c423972SAdrian Chadd #include <netinet/in_rss.h>
76df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
77df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
78ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
792f4afd21SRandall Stewart #ifdef SCTP
802f4afd21SRandall Stewart #include <netinet/sctp.h>
812f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
822f4afd21SRandall Stewart #endif
83df8bae1dSRodney W. Grimes 
84b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
852cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h>
861dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
87b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/
886a800098SYoshinobu Inoue 
891dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
901dfcf0d2SAndre Oppermann 
91aed55708SRobert Watson #include <security/mac/mac_framework.h>
92aed55708SRobert Watson 
93eddfbb76SRobert Watson VNET_DEFINE(u_short, ip_id);
94f23b4c91SGarrett Wollman 
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 
101df8bae1dSRodney W. Grimes static void	ip_mloopback
1024d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
103afed1b49SDarren Reed 
104afed1b49SDarren Reed 
1058b889dbbSBruce M Simpson extern int in_mcast_loop;
10693e0e116SJulian Elischer extern	struct protosw inetsw[];
10793e0e116SJulian Elischer 
108df8bae1dSRodney W. Grimes /*
109df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
110df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
111df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
112df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
113bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
114bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
115bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
116bf984051SGleb Smirnoff  *
1173de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1183de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
119df8bae1dSRodney W. Grimes  */
120df8bae1dSRodney W. Grimes int
121f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
122f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
123df8bae1dSRodney W. Grimes {
1241e78ac21SJeffrey Hsu 	struct ip *ip;
125fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
126ac9d7e26SMax Laier 	struct mbuf *m0;
12723bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1283ae2ad08SJohn-Mark Gurney 	int mtu;
129ca8b83b0SLuigi Rizzo 	int error = 0;
130ca8b83b0SLuigi Rizzo 	struct sockaddr_in *dst;
1314c7a6059SGleb Smirnoff 	const struct sockaddr_in *gw;
1323c73180fSGleb Smirnoff 	struct in_ifaddr *ia;
13321d172a3SGleb Smirnoff 	int isbroadcast;
134078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
135e3f406b3SRuslan Ermilov 	struct route iproute;
136ec396e61SLuigi Rizzo 	struct rtentry *rte;	/* cache for ro->ro_rt */
1379b932e9eSAndre Oppermann 	struct in_addr odst;
1389b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
139fe82cbe8SGleb Smirnoff 	int have_ia_ref;
1401a499816SEdward Tomasz Napierala #ifdef IPSEC
1411a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
1421a499816SEdward Tomasz Napierala #endif
143ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
14436e8826fSMax Laier 
145bc97ba51SJulian Elischer 	if (inp != NULL) {
146baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
14762e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
148061a4b4cSAdrian Chadd 		if (((flags & IP_NODEFAULTFLOWID) == 0) &&
149061a4b4cSAdrian Chadd 		    inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) {
15080cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
1519c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
15280cb9f21SKip Macy 			m->m_flags |= M_FLOWID;
15380cb9f21SKip Macy 		}
154bc97ba51SJulian Elischer 	}
15562e1ba83SRobert Watson 
15662e1ba83SRobert Watson 	if (ro == NULL) {
15762e1ba83SRobert Watson 		ro = &iproute;
15862e1ba83SRobert Watson 		bzero(ro, sizeof (*ro));
159bf984051SGleb Smirnoff 	}
16062e1ba83SRobert Watson 
16153be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE
1620ff96b4fSGleb Smirnoff 	if (ro->ro_rt == NULL)
1630ff96b4fSGleb Smirnoff 		(void )flowtable_lookup(AF_INET, m, ro);
16453be8fcaSBjoern A. Zeeb #endif
1652b25acc1SLuigi Rizzo 
166df8bae1dSRodney W. Grimes 	if (opt) {
167ca8b83b0SLuigi Rizzo 		int len = 0;
168df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
169cb7641e8SMaxim Konovalov 		if (len != 0)
170ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
171df8bae1dSRodney W. Grimes 	}
172df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1738f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
1748f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
1753efc3014SJulian Elischer 
176df8bae1dSRodney W. Grimes 	/*
1776e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
178e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
179e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
180e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
181e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1826e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1836e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1846e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1854d09f5a0SGleb Smirnoff 	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
186df8bae1dSRodney W. Grimes 	 */
187df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
18853be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
18953be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1901f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
19186425c62SRobert Watson 		IPSTAT_INC(ips_localout);
192df8bae1dSRodney W. Grimes 	} else {
193ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
19453be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
195df8bae1dSRodney W. Grimes 	}
1969c9137eaSGarrett Wollman 
197054692a4SAlexander V. Chernikov 	/*
198054692a4SAlexander V. Chernikov 	 * dst/gw handling:
199054692a4SAlexander V. Chernikov 	 *
2003c065f2fSGleb Smirnoff 	 * dst can be rewritten but always points to &ro->ro_dst.
2013c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
2023c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
203054692a4SAlexander V. Chernikov 	 */
2044c7a6059SGleb Smirnoff 	gw = dst = (struct sockaddr_in *)&ro->ro_dst;
2059b932e9eSAndre Oppermann again:
2063c73180fSGleb Smirnoff 	ia = NULL;
207fe82cbe8SGleb Smirnoff 	have_ia_ref = 0;
208df8bae1dSRodney W. Grimes 	/*
2093c065f2fSGleb Smirnoff 	 * If there is a cached route, check that it is to the same
2103c065f2fSGleb Smirnoff 	 * destination and is still up.  If not, free it and try again.
2113c065f2fSGleb Smirnoff 	 * The address family should also be checked in case of sharing
2123c065f2fSGleb Smirnoff 	 * the cache with IPv6.
213df8bae1dSRodney W. Grimes 	 */
214ec396e61SLuigi Rizzo 	rte = ro->ro_rt;
215ec396e61SLuigi Rizzo 	if (rte && ((rte->rt_flags & RTF_UP) == 0 ||
216c7ea0aa6SQing Li 		    rte->rt_ifp == NULL ||
217c7ea0aa6SQing Li 		    !RT_LINK_IS_UP(rte->rt_ifp) ||
218a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
2199b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
220bf984051SGleb Smirnoff 		RO_RTFREE(ro);
221bf984051SGleb Smirnoff 		ro->ro_lle = NULL;
222bf984051SGleb Smirnoff 		rte = NULL;
223054692a4SAlexander V. Chernikov 		gw = dst;
224df8bae1dSRodney W. Grimes 	}
225ec396e61SLuigi Rizzo 	if (rte == NULL && fwd_tag == NULL) {
226a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
227df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
228df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2299b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
230df8bae1dSRodney W. Grimes 	}
231df8bae1dSRodney W. Grimes 	/*
232a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
233a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
234a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
235a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
236df8bae1dSRodney W. Grimes 	 */
237a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
238a3fd02d8SBruce M Simpson 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
2392f308a34SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
24086425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
241a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
242a3fd02d8SBruce M Simpson 			goto bad;
243a3fd02d8SBruce M Simpson 		}
244fe82cbe8SGleb Smirnoff 		have_ia_ref = 1;
245a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
246a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
247a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
248a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
249a3fd02d8SBruce M Simpson 		isbroadcast = 1;
250a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
2512f308a34SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2522f308a34SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0))) == NULL) {
25386425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
254df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
255df8bae1dSRodney W. Grimes 			goto bad;
256df8bae1dSRodney W. Grimes 		}
257fe82cbe8SGleb Smirnoff 		have_ia_ref = 1;
258df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
259df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2609f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2613afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
26238c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2633afefa39SDaniel C. Sobral 		/*
26438c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
26538c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2663afefa39SDaniel C. Sobral 		 */
26738c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
26838c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
269fe82cbe8SGleb Smirnoff 		if (ia)
270fe82cbe8SGleb Smirnoff 			have_ia_ref = 1;
27138c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
272df8bae1dSRodney W. Grimes 	} else {
2732c17fe93SGarrett Wollman 		/*
27497d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
27597d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
27697d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2772c17fe93SGarrett Wollman 		 */
278ec396e61SLuigi Rizzo 		if (rte == NULL) {
279e440aed9SQing Li #ifdef RADIX_MPATH
2808b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
2818b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
2828b07e49aSJulian Elischer 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
283e440aed9SQing Li #else
2848b07e49aSJulian Elischer 			in_rtalloc_ign(ro, 0,
2858b07e49aSJulian Elischer 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
286e440aed9SQing Li #endif
287ec396e61SLuigi Rizzo 			rte = ro->ro_rt;
288ec396e61SLuigi Rizzo 		}
289c7ea0aa6SQing Li 		if (rte == NULL ||
290c7ea0aa6SQing Li 		    rte->rt_ifp == NULL ||
291c7ea0aa6SQing Li 		    !RT_LINK_IS_UP(rte->rt_ifp)) {
2921a499816SEdward Tomasz Napierala #ifdef IPSEC
2931a499816SEdward Tomasz Napierala 			/*
2941a499816SEdward Tomasz Napierala 			 * There is no route for this packet, but it is
2951a499816SEdward Tomasz Napierala 			 * possible that a matching SPD entry exists.
2961a499816SEdward Tomasz Napierala 			 */
2971a499816SEdward Tomasz Napierala 			no_route_but_check_spd = 1;
2981a499816SEdward Tomasz Napierala 			mtu = 0; /* Silence GCC warning. */
2991a499816SEdward Tomasz Napierala 			goto sendit;
3001a499816SEdward Tomasz Napierala #endif
30186425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
302df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
303df8bae1dSRodney W. Grimes 			goto bad;
304df8bae1dSRodney W. Grimes 		}
305ec396e61SLuigi Rizzo 		ia = ifatoia(rte->rt_ifa);
306ec396e61SLuigi Rizzo 		ifp = rte->rt_ifp;
307e3a7aa6fSGleb Smirnoff 		counter_u64_add(rte->rt_pksent, 1);
308ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_GATEWAY)
3094c7a6059SGleb Smirnoff 			gw = (struct sockaddr_in *)rte->rt_gateway;
310ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_HOST)
311ec396e61SLuigi Rizzo 			isbroadcast = (rte->rt_flags & RTF_BROADCAST);
3129f9b3dc4SGarrett Wollman 		else
3134c7a6059SGleb Smirnoff 			isbroadcast = in_broadcast(gw->sin_addr, ifp);
314df8bae1dSRodney W. Grimes 	}
3153ae2ad08SJohn-Mark Gurney 	/*
3163ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
3173ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
3183ae2ad08SJohn-Mark Gurney 	 */
319ec396e61SLuigi Rizzo 	if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) {
3203ae2ad08SJohn-Mark Gurney 		/*
3213ae2ad08SJohn-Mark Gurney 		 * This case can happen if the user changed the MTU
3223ae2ad08SJohn-Mark Gurney 		 * of an interface after enabling IP on it.  Because
3233ae2ad08SJohn-Mark Gurney 		 * most netifs don't keep track of routes pointing to
3243ae2ad08SJohn-Mark Gurney 		 * them, there is no way for one to update all its
3253ae2ad08SJohn-Mark Gurney 		 * routes when the MTU is changed.
3263ae2ad08SJohn-Mark Gurney 		 */
327e3a7aa6fSGleb Smirnoff 		if (rte->rt_mtu > ifp->if_mtu)
328e3a7aa6fSGleb Smirnoff 			rte->rt_mtu = ifp->if_mtu;
329e3a7aa6fSGleb Smirnoff 		mtu = rte->rt_mtu;
3303ae2ad08SJohn-Mark Gurney 	} else {
3313ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
3323ae2ad08SJohn-Mark Gurney 	}
333c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
334c744cde4SBjoern A. Zeeb 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p",
335c744cde4SBjoern A. Zeeb 	    __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp));
3369b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
337df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
338df8bae1dSRodney W. Grimes 		/*
339183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
340183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
341183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
342183e1c86SGleb Smirnoff 		 */
343183e1c86SGleb Smirnoff 		gw = dst;
344183e1c86SGleb Smirnoff 		/*
345df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
346df8bae1dSRodney W. Grimes 		 */
347df8bae1dSRodney W. Grimes 		if (imo != NULL) {
348df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
3491c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3501c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
351bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
352bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
353bbb4330bSLuigi Rizzo 				    INADDR_ANY;
354df8bae1dSRodney W. Grimes 		} else
355df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
356df8bae1dSRodney W. Grimes 		/*
357df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
358df8bae1dSRodney W. Grimes 		 */
3591c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
360df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
36186425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
362df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
363df8bae1dSRodney W. Grimes 				goto bad;
364df8bae1dSRodney W. Grimes 			}
3651c5de19aSGarrett Wollman 		}
366df8bae1dSRodney W. Grimes 		/*
367df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
368df8bae1dSRodney W. Grimes 		 * of outgoing interface.
369df8bae1dSRodney W. Grimes 		 */
370df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
37138c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
37238c1bc35SRuslan Ermilov 			if (ia != NULL)
37338c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
374df8bae1dSRodney W. Grimes 		}
375df8bae1dSRodney W. Grimes 
3768b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
3778b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
378df8bae1dSRodney W. Grimes 			/*
3798b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
3808b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
3818b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
3828b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
3838b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
384df8bae1dSRodney W. Grimes 			 */
38586b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
3868b889dbbSBruce M Simpson 		} else {
387df8bae1dSRodney W. Grimes 			/*
388df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
389df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
390df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
391df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
392df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
393df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
394df8bae1dSRodney W. Grimes 			 *
395df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
396df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
397df8bae1dSRodney W. Grimes 			 * if necessary.
398df8bae1dSRodney W. Grimes 			 */
399603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
400f0068c4aSGarrett Wollman 				/*
401bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
402f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
403f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
404f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
405f0068c4aSGarrett Wollman 				 */
406603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
407f0068c4aSGarrett Wollman 					imo = NULL;
408bbb4330bSLuigi Rizzo 				if (ip_mforward &&
409bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
410df8bae1dSRodney W. Grimes 					m_freem(m);
411df8bae1dSRodney W. Grimes 					goto done;
412df8bae1dSRodney W. Grimes 				}
413df8bae1dSRodney W. Grimes 			}
414df8bae1dSRodney W. Grimes 		}
4155e9ae478SGarrett Wollman 
416df8bae1dSRodney W. Grimes 		/*
417df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
418df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
419df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
420df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
4218b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
4228b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
4238b889dbbSBruce M Simpson 		 * the loopback interface.
424df8bae1dSRodney W. Grimes 		 */
425f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
426df8bae1dSRodney W. Grimes 			m_freem(m);
427df8bae1dSRodney W. Grimes 			goto done;
428df8bae1dSRodney W. Grimes 		}
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 		goto sendit;
431df8bae1dSRodney W. Grimes 	}
4326a7c943cSAndre Oppermann 
433df8bae1dSRodney W. Grimes 	/*
4342b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
435cb459254SJohn-Mark Gurney 	 * of the outoing interface.
436df8bae1dSRodney W. Grimes 	 */
437f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
43838c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
43938c1bc35SRuslan Ermilov 		if (ia != NULL) {
440df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
441f9e354dfSJulian Elischer 		}
44238c1bc35SRuslan Ermilov 	}
4436a7c943cSAndre Oppermann 
444ca7a789aSMax Laier 	/*
445ac7e1212SAdrian Chadd 	 * Both in the SMP world, pre-emption world if_transmit() world,
446ac7e1212SAdrian Chadd 	 * the following code doesn't really function as intended any further.
447ac7e1212SAdrian Chadd 	 *
448ac7e1212SAdrian Chadd 	 * + There can and will be multiple CPUs running this code path
449ac7e1212SAdrian Chadd 	 *   in parallel, and we do no lock holding when checking the
450ac7e1212SAdrian Chadd 	 *   queue depth;
451ac7e1212SAdrian Chadd 	 * + And since other threads can be running concurrently, even if
452ac7e1212SAdrian Chadd 	 *   we do pass this check, another thread may queue some frames
453ac7e1212SAdrian Chadd 	 *   before this thread does and it will end up partially or fully
454ac7e1212SAdrian Chadd 	 *   failing to send anyway;
455ac7e1212SAdrian Chadd 	 * + if_transmit() based drivers don't necessarily set ifq_len
456ac7e1212SAdrian Chadd 	 *   at all.
457ac7e1212SAdrian Chadd 	 *
458ac7e1212SAdrian Chadd 	 * This should be replaced with a method of pushing an entire list
459ac7e1212SAdrian Chadd 	 * of fragment frames to the driver and have the driver decide
460ac7e1212SAdrian Chadd 	 * whether it can queue or not queue the entire set.
461ac7e1212SAdrian Chadd 	 */
462ac7e1212SAdrian Chadd #if 0
463ac7e1212SAdrian Chadd 	/*
464ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
465ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
466ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
467ca7a789aSMax Laier 	 */
4688f134647SGleb Smirnoff 	n = ip_len / mtu + 1; /* how many fragments ? */
469ca8b83b0SLuigi Rizzo 	if (
47002b199f1SMax Laier #ifdef ALTQ
471ca8b83b0SLuigi Rizzo 	    (!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
472ca7a789aSMax Laier #endif /* ALTQ */
473ca8b83b0SLuigi Rizzo 	    (ifp->if_snd.ifq_len + n) >= ifp->if_snd.ifq_maxlen ) {
474b5390296SDavid Greenman 		error = ENOBUFS;
47586425c62SRobert Watson 		IPSTAT_INC(ips_odropped);
476ca8b83b0SLuigi Rizzo 		ifp->if_snd.ifq_drops += n;
477b5390296SDavid Greenman 		goto bad;
478b5390296SDavid Greenman 	}
479ac7e1212SAdrian Chadd #endif
480b5390296SDavid Greenman 
481b5390296SDavid Greenman 	/*
482df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4838c3f5566SRuslan Ermilov 	 * verify user is allowed to send
484df8bae1dSRodney W. Grimes 	 * such a packet.
485df8bae1dSRodney W. Grimes 	 */
4869f9b3dc4SGarrett Wollman 	if (isbroadcast) {
487df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
488df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
489df8bae1dSRodney W. Grimes 			goto bad;
490df8bae1dSRodney W. Grimes 		}
491df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
492df8bae1dSRodney W. Grimes 			error = EACCES;
493df8bae1dSRodney W. Grimes 			goto bad;
494df8bae1dSRodney W. Grimes 		}
495df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
4968f134647SGleb Smirnoff 		if (ip_len > mtu) {
497df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
498df8bae1dSRodney W. Grimes 			goto bad;
499df8bae1dSRodney W. Grimes 		}
500df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
5019f9b3dc4SGarrett Wollman 	} else {
502df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
5039f9b3dc4SGarrett Wollman 	}
504df8bae1dSRodney W. Grimes 
505f1743588SDarren Reed sendit:
506b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
5074f7418a0SEdward Tomasz Napierala 	switch(ip_ipsec_output(&m, inp, &flags, &error)) {
5081dfcf0d2SAndre Oppermann 	case 1:
50933841545SHajimu UMEMOTO 		goto bad;
5101dfcf0d2SAndre Oppermann 	case -1:
5111dfcf0d2SAndre Oppermann 		goto done;
5121dfcf0d2SAndre Oppermann 	case 0:
51333841545SHajimu UMEMOTO 	default:
5141dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
51533841545SHajimu UMEMOTO 	}
5161a499816SEdward Tomasz Napierala 	/*
5171a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
5181a499816SEdward Tomasz Napierala 	 */
5191a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
5201a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
5211a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
5221a499816SEdward Tomasz Napierala 		goto bad;
5231a499816SEdward Tomasz Napierala 	}
5241dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
52533841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
52633841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
527b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
52833841545SHajimu UMEMOTO 
529c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
5300b4b0b0fSJulian Elischer 	if (!PFIL_HOOKED(&V_inet_pfil_hook))
531c21fd232SAndre Oppermann 		goto passout;
532c21fd232SAndre Oppermann 
533c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
5349b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
5350b4b0b0fSJulian Elischer 	error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
536134ea224SSam Leffler 	if (error != 0 || m == NULL)
537c4ac87eaSDarren Reed 		goto done;
5389b932e9eSAndre Oppermann 
539c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
540fed1c7e9SSøren Schmidt 
5419b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
5429b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
5439b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
544f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
5459b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
5469b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
547f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
548603724d3SBjoern A. Zeeb 				m->m_pkthdr.rcvif = V_loif;
54920c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
55020c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
55120c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
552ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
55320c822f3SJonathan Lemon 			}
55420c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
55520c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
5562f4afd21SRandall Stewart #ifdef SCTP
5572f4afd21SRandall Stewart 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
5582f4afd21SRandall Stewart 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
5592f4afd21SRandall Stewart #endif
5609b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
5619b932e9eSAndre Oppermann 			goto done;
562fe82cbe8SGleb Smirnoff 		} else {
563fe82cbe8SGleb Smirnoff 			if (have_ia_ref)
564fe82cbe8SGleb Smirnoff 				ifa_free(&ia->ia_ifa);
565f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
5669b932e9eSAndre Oppermann 		}
567fe82cbe8SGleb Smirnoff 	}
5689b932e9eSAndre Oppermann 
5699b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
5709b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5719b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
572603724d3SBjoern A. Zeeb 			m->m_pkthdr.rcvif = V_loif;
5739b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
5749b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
5759b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
5769b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
5779b932e9eSAndre Oppermann 		}
5782f4afd21SRandall Stewart #ifdef SCTP
5792f4afd21SRandall Stewart 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
5802f4afd21SRandall Stewart 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
5812f4afd21SRandall Stewart #endif
5829b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
5839b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
5849b932e9eSAndre Oppermann 
5859b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
586f9e354dfSJulian Elischer 		goto done;
587f9e354dfSJulian Elischer 	}
5889b932e9eSAndre Oppermann 	/* Or forward to some other address? */
589ffdbf9daSAndrey V. Elsukov 	if ((m->m_flags & M_IP_NEXTHOP) &&
590ffdbf9daSAndrey V. Elsukov 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
5919b932e9eSAndre Oppermann 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
5929b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
593ffdbf9daSAndrey V. Elsukov 		m->m_flags &= ~M_IP_NEXTHOP;
5949b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
595fe82cbe8SGleb Smirnoff 		if (have_ia_ref)
596fe82cbe8SGleb Smirnoff 			ifa_free(&ia->ia_ifa);
5979b932e9eSAndre Oppermann 		goto again;
598099dd043SAndre Oppermann 	}
5992b25acc1SLuigi Rizzo 
600c21fd232SAndre Oppermann passout:
60151c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
60251c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
60351c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
60451c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
60586425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
60651c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
60751c8ec4aSRuslan Ermilov 			goto bad;
60851c8ec4aSRuslan Ermilov 		}
60951c8ec4aSRuslan Ermilov 	}
61051c8ec4aSRuslan Ermilov 
611206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
612078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
613db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
614078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
615db4f9cc7SJonathan Lemon 	}
6162f4afd21SRandall Stewart #ifdef SCTP
617078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
6181966e5b5SRandall Stewart 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
619078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
6202f4afd21SRandall Stewart 	}
6212f4afd21SRandall Stewart #endif
622db4f9cc7SJonathan Lemon 
623e7319babSPoul-Henning Kamp 	/*
624db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
625233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
626df8bae1dSRodney W. Grimes 	 */
62721d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
628bf7dcda3SGleb Smirnoff 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
629df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
630078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
631df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
632078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
633078468edSGleb Smirnoff 		}
6345da9f8faSJosef Karthauser 
635233dcce1SAndre Oppermann 		/*
636233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
637233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
638233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
639233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
640233dcce1SAndre Oppermann 		 */
64138c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
642233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
6437caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
6447caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
645233dcce1SAndre Oppermann 			else
6467caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
6477caf4ab7SGleb Smirnoff 
6487caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
6495da9f8faSJosef Karthauser 		}
65053dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
6513390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
652eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
6539d9edc56SMike Silbersack #endif
654147f74d1SAndre Oppermann 		/*
655147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
656147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
657147f74d1SAndre Oppermann 		 */
65886bd0491SAndre Oppermann 		m_clrprotoflags(m);
65957f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
66047e8d432SGleb Smirnoff 		error = (*ifp->if_output)(ifp, m,
66147e8d432SGleb Smirnoff 		    (const struct sockaddr *)gw, ro);
662df8bae1dSRodney W. Grimes 		goto done;
663df8bae1dSRodney W. Grimes 	}
6641e78ac21SJeffrey Hsu 
665233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
66621d172a3SGleb Smirnoff 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
667df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
66886425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
669df8bae1dSRodney W. Grimes 		goto bad;
670df8bae1dSRodney W. Grimes 	}
6711e78ac21SJeffrey Hsu 
6721e78ac21SJeffrey Hsu 	/*
6731e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
6741e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
6751e78ac21SJeffrey Hsu 	 */
676078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
6771e78ac21SJeffrey Hsu 	if (error)
678df8bae1dSRodney W. Grimes 		goto bad;
6791e78ac21SJeffrey Hsu 	for (; m; m = m0) {
680df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
681b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
68207203494SDaniel C. Sobral 		if (error == 0) {
683fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
68407203494SDaniel C. Sobral 			if (ia != NULL) {
6857caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
6867caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
6877caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
68807203494SDaniel C. Sobral 			}
689147f74d1SAndre Oppermann 			/*
690147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
691147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
692147f74d1SAndre Oppermann 			 */
69386bd0491SAndre Oppermann 			m_clrprotoflags(m);
694fe937674SJosef Karthauser 
69557f60867SMark Johnston 			IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
696df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
69747e8d432SGleb Smirnoff 			    (const struct sockaddr *)gw, ro);
698fe937674SJosef Karthauser 		} else
699df8bae1dSRodney W. Grimes 			m_freem(m);
700df8bae1dSRodney W. Grimes 	}
701df8bae1dSRodney W. Grimes 
702df8bae1dSRodney W. Grimes 	if (error == 0)
70386425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
7041e78ac21SJeffrey Hsu 
705df8bae1dSRodney W. Grimes done:
706bf984051SGleb Smirnoff 	if (ro == &iproute)
707bf984051SGleb Smirnoff 		RO_RTFREE(ro);
708fe82cbe8SGleb Smirnoff 	if (have_ia_ref)
709fe82cbe8SGleb Smirnoff 		ifa_free(&ia->ia_ifa);
710df8bae1dSRodney W. Grimes 	return (error);
711df8bae1dSRodney W. Grimes bad:
7123528d68fSBill Paul 	m_freem(m);
713df8bae1dSRodney W. Grimes 	goto done;
714df8bae1dSRodney W. Grimes }
715df8bae1dSRodney W. Grimes 
7161e78ac21SJeffrey Hsu /*
7171e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
7181e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
7191e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
7201e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
7211e78ac21SJeffrey Hsu  *
7221e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
7231e78ac21SJeffrey Hsu  */
7241e78ac21SJeffrey Hsu int
7251e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
726078468edSGleb Smirnoff     u_long if_hwassist_flags)
7271e78ac21SJeffrey Hsu {
7281e78ac21SJeffrey Hsu 	int error = 0;
7291e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
7301e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
7311e78ac21SJeffrey Hsu 	int off;
7321e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
7331e78ac21SJeffrey Hsu 	int firstlen;
7341e78ac21SJeffrey Hsu 	struct mbuf **mnext;
7351e78ac21SJeffrey Hsu 	int nfrags;
73621d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
7371e78ac21SJeffrey Hsu 
73821d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
73921d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
74021d172a3SGleb Smirnoff 
74121d172a3SGleb Smirnoff 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
74286425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
7431e78ac21SJeffrey Hsu 		return EMSGSIZE;
7441e78ac21SJeffrey Hsu 	}
7451e78ac21SJeffrey Hsu 
7461e78ac21SJeffrey Hsu 	/*
7471e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
7481e78ac21SJeffrey Hsu 	 */
7491e78ac21SJeffrey Hsu 	if (len < 8)
7501e78ac21SJeffrey Hsu 		return EMSGSIZE;
7511e78ac21SJeffrey Hsu 
7521e78ac21SJeffrey Hsu 	/*
7531e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
7541e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
7551e78ac21SJeffrey Hsu 	 */
756da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
7571e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
7581e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
7591e78ac21SJeffrey Hsu 	}
7602f4afd21SRandall Stewart #ifdef SCTP
761da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
7621966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
7632f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
7642f4afd21SRandall Stewart 	}
7652f4afd21SRandall Stewart #endif
7661e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
7671e78ac21SJeffrey Hsu 		/*
7681e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
7691e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
7701e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
7711e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
7721e78ac21SJeffrey Hsu 		 *
7731e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
7741e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
7751e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
7761e78ac21SJeffrey Hsu 		 */
7771e78ac21SJeffrey Hsu 		int newlen;
7781e78ac21SJeffrey Hsu 		struct mbuf *m;
7791e78ac21SJeffrey Hsu 
7801e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
7811e78ac21SJeffrey Hsu 			off += m->m_len;
7821e78ac21SJeffrey Hsu 
7831e78ac21SJeffrey Hsu 		/*
7841e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
7851e78ac21SJeffrey Hsu 		 * 8-byte boundary
7861e78ac21SJeffrey Hsu 		 */
7871e78ac21SJeffrey Hsu 		if (off < hlen)
7881e78ac21SJeffrey Hsu 			goto smart_frag_failure;
7891e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
7901e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
7911e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
7921e78ac21SJeffrey Hsu 			/* we failed, go back the default */
7931e78ac21SJeffrey Hsu smart_frag_failure:
7941e78ac21SJeffrey Hsu 			newlen = len;
7951e78ac21SJeffrey Hsu 			off = hlen + len;
7961e78ac21SJeffrey Hsu 		}
7971e78ac21SJeffrey Hsu 		len = newlen;
7981e78ac21SJeffrey Hsu 
7991e78ac21SJeffrey Hsu 	} else {
8001e78ac21SJeffrey Hsu 		off = hlen + len;
8011e78ac21SJeffrey Hsu 	}
8021e78ac21SJeffrey Hsu 
8031e78ac21SJeffrey Hsu 	firstlen = off - hlen;
8041e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
8051e78ac21SJeffrey Hsu 
8061e78ac21SJeffrey Hsu 	/*
8071e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
8081e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
8091e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
8101e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
8111e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
8121e78ac21SJeffrey Hsu 	 */
81321d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
8141e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
8151e78ac21SJeffrey Hsu 		struct mbuf *m;
8161e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
8171e78ac21SJeffrey Hsu 
818dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
8190b17fba7SAndre Oppermann 		if (m == NULL) {
8201e78ac21SJeffrey Hsu 			error = ENOBUFS;
82186425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8221e78ac21SJeffrey Hsu 			goto done;
8231e78ac21SJeffrey Hsu 		}
824fb86dfcdSAndre Oppermann 		m->m_flags |= (m0->m_flags & M_MCAST);
8251e78ac21SJeffrey Hsu 		/*
8261e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
8271e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
8288b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
8291e78ac21SJeffrey Hsu 		 */
8301e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
8311e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
8321e78ac21SJeffrey Hsu 		*mhip = *ip;
8331e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
8341e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
8351e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
8361e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
8371e78ac21SJeffrey Hsu 		}
8381e78ac21SJeffrey Hsu 		m->m_len = mhlen;
83921d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
84021d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
841fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
84221d172a3SGleb Smirnoff 			len = ip_len - off;
843fb86dfcdSAndre Oppermann 		else
8441e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
8451e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
846eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
8470b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
8481e78ac21SJeffrey Hsu 			m_free(m);
8491e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
85086425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8511e78ac21SJeffrey Hsu 			goto done;
8521e78ac21SJeffrey Hsu 		}
8531e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
854fc74a9f9SBrooks Davis 		m->m_pkthdr.rcvif = NULL;
8551e78ac21SJeffrey Hsu #ifdef MAC
85630d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
8571e78ac21SJeffrey Hsu #endif
8581e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
8591e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
8601e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
861078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
8621e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
863078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
864078468edSGleb Smirnoff 		}
8651e78ac21SJeffrey Hsu 		*mnext = m;
8661e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
8671e78ac21SJeffrey Hsu 	}
86886425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
8691e78ac21SJeffrey Hsu 
8701e78ac21SJeffrey Hsu 	/*
8711e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
8721e78ac21SJeffrey Hsu 	 * and updating header.
8731e78ac21SJeffrey Hsu 	 */
87421d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
8751e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
8761e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
87721d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
8781e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
879078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
8801e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
881078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
882078468edSGleb Smirnoff 	}
8831e78ac21SJeffrey Hsu 
8841e78ac21SJeffrey Hsu done:
8851e78ac21SJeffrey Hsu 	*m_frag = m0;
8861e78ac21SJeffrey Hsu 	return error;
8871e78ac21SJeffrey Hsu }
8881e78ac21SJeffrey Hsu 
8891c238475SJonathan Lemon void
890db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
891db4f9cc7SJonathan Lemon {
892db4f9cc7SJonathan Lemon 	struct ip *ip;
89323e9c6dcSGleb Smirnoff 	uint16_t csum, offset, ip_len;
894db4f9cc7SJonathan Lemon 
895db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
89653be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
89723e9c6dcSGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
89823e9c6dcSGleb Smirnoff 	csum = in_cksum_skip(m, ip_len, offset);
899206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
900206a3274SRuslan Ermilov 		csum = 0xffff;
901db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
902db4f9cc7SJonathan Lemon 
9038e1d0a56SMichael Tuexen 	/* find the mbuf in the chain where the checksum starts*/
9048e1d0a56SMichael Tuexen 	while ((m != NULL) && (offset >= m->m_len)) {
9058e1d0a56SMichael Tuexen 		offset -= m->m_len;
9068e1d0a56SMichael Tuexen 		m = m->m_next;
9078e1d0a56SMichael Tuexen 	}
90826461454SMichael Tuexen 	KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain."));
90926461454SMichael Tuexen 	KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs."));
910db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
911db4f9cc7SJonathan Lemon }
912db4f9cc7SJonathan Lemon 
913df8bae1dSRodney W. Grimes /*
914df8bae1dSRodney W. Grimes  * IP socket option processing.
915df8bae1dSRodney W. Grimes  */
916df8bae1dSRodney W. Grimes int
917f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
918df8bae1dSRodney W. Grimes {
919cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
920cfe8b629SGarrett Wollman 	int	error, optval;
921dc847eb6SAdrian Chadd #ifdef	RSS
922dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
923dc847eb6SAdrian Chadd 	int retval;
924dc847eb6SAdrian Chadd #endif
925df8bae1dSRodney W. Grimes 
926cfe8b629SGarrett Wollman 	error = optval = 0;
927cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
928fc06cd42SMikolaj Golub 		error = EINVAL;
929fc06cd42SMikolaj Golub 
930fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
931fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
932fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
933fc06cd42SMikolaj Golub 			case SO_REUSEADDR:
934fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
935efdf104bSMikolaj Golub 				if ((so->so_options & SO_REUSEADDR) != 0)
936efdf104bSMikolaj Golub 					inp->inp_flags2 |= INP_REUSEADDR;
937fc06cd42SMikolaj Golub 				else
938efdf104bSMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEADDR;
939fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
940fc06cd42SMikolaj Golub 				error = 0;
941fc06cd42SMikolaj Golub 				break;
942fc06cd42SMikolaj Golub 			case SO_REUSEPORT:
943fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
944fc06cd42SMikolaj Golub 				if ((so->so_options & SO_REUSEPORT) != 0)
945fc06cd42SMikolaj Golub 					inp->inp_flags2 |= INP_REUSEPORT;
946fc06cd42SMikolaj Golub 				else
947fc06cd42SMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEPORT;
948fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
949fc06cd42SMikolaj Golub 				error = 0;
950fc06cd42SMikolaj Golub 				break;
951fc06cd42SMikolaj Golub 			case SO_SETFIB:
952fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
953fc06cd42SMikolaj Golub 				inp->inp_inc.inc_fibnum = so->so_fibnum;
954fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
955fc06cd42SMikolaj Golub 				error = 0;
956fc06cd42SMikolaj Golub 				break;
957fc06cd42SMikolaj Golub 			default:
958fc06cd42SMikolaj Golub 				break;
959fc06cd42SMikolaj Golub 			}
960fc06cd42SMikolaj Golub 		}
961fc06cd42SMikolaj Golub 		return (error);
962cfe8b629SGarrett Wollman 	}
963df8bae1dSRodney W. Grimes 
964cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
965cfe8b629SGarrett Wollman 	case SOPT_SET:
966cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
967df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
968df8bae1dSRodney W. Grimes #ifdef notyet
969df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
970df8bae1dSRodney W. Grimes #endif
971cfe8b629SGarrett Wollman 		{
972cfe8b629SGarrett Wollman 			struct mbuf *m;
973cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
974cfe8b629SGarrett Wollman 				error = EMSGSIZE;
975cfe8b629SGarrett Wollman 				break;
976cfe8b629SGarrett Wollman 			}
977dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
9780b17fba7SAndre Oppermann 			if (m == NULL) {
979cfe8b629SGarrett Wollman 				error = ENOBUFS;
980cfe8b629SGarrett Wollman 				break;
981cfe8b629SGarrett Wollman 			}
982cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
983cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
984cfe8b629SGarrett Wollman 					    m->m_len);
985635354c4SMaxim Konovalov 			if (error) {
986635354c4SMaxim Konovalov 				m_free(m);
987635354c4SMaxim Konovalov 				break;
988635354c4SMaxim Konovalov 			}
9898501a69cSRobert Watson 			INP_WLOCK(inp);
990993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
9918501a69cSRobert Watson 			INP_WUNLOCK(inp);
992993d9505SRobert Watson 			return (error);
993cfe8b629SGarrett Wollman 		}
994df8bae1dSRodney W. Grimes 
995f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
996f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
997f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
998f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
999f44270e7SPawel Jakub Dawidek 				if (error)
1000be9347e3SAdrian Chadd 					break;
1001be9347e3SAdrian Chadd 			}
1002cef27294SAdrian Chadd 			/* FALLTHROUGH */
10030a100a6fSAdrian Chadd 		case IP_BINDMULTI:
10040a100a6fSAdrian Chadd #ifdef	RSS
10050a100a6fSAdrian Chadd 		case IP_RSS_LISTEN_BUCKET:
10060a100a6fSAdrian Chadd #endif
1007df8bae1dSRodney W. Grimes 		case IP_TOS:
1008df8bae1dSRodney W. Grimes 		case IP_TTL:
1009936cd18dSAndre Oppermann 		case IP_MINTTL:
1010df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1011df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1012df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
10134957466bSMatthew N. Dodd 		case IP_RECVTTL:
101482c23ebaSBill Fenner 		case IP_RECVIF:
10156a800098SYoshinobu Inoue 		case IP_FAITH:
10168afa2304SBruce M Simpson 		case IP_ONESBCAST:
1017b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
10183cca425bSMichael Tuexen 		case IP_RECVTOS:
1019*9d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
1020*9d3ddf43SAdrian Chadd #ifdef	RSS
1021*9d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
1022*9d3ddf43SAdrian Chadd #endif
1023cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1024cfe8b629SGarrett Wollman 					    sizeof optval);
1025cfe8b629SGarrett Wollman 			if (error)
1026cfe8b629SGarrett Wollman 				break;
1027df8bae1dSRodney W. Grimes 
1028cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1029df8bae1dSRodney W. Grimes 			case IP_TOS:
1030ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1031df8bae1dSRodney W. Grimes 				break;
1032df8bae1dSRodney W. Grimes 
1033df8bae1dSRodney W. Grimes 			case IP_TTL:
1034ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1035df8bae1dSRodney W. Grimes 				break;
1036936cd18dSAndre Oppermann 
1037936cd18dSAndre Oppermann 			case IP_MINTTL:
1038a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1039936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1040936cd18dSAndre Oppermann 				else
1041936cd18dSAndre Oppermann 					error = EINVAL;
1042936cd18dSAndre Oppermann 				break;
1043936cd18dSAndre Oppermann 
1044a138d217SRobert Watson #define	OPTSET(bit) do {						\
10458501a69cSRobert Watson 	INP_WLOCK(inp);							\
1046df8bae1dSRodney W. Grimes 	if (optval)							\
1047df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1048df8bae1dSRodney W. Grimes 	else								\
1049a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
10508501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1051a138d217SRobert Watson } while (0)
1052df8bae1dSRodney W. Grimes 
10530a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
10540a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
10550a100a6fSAdrian Chadd 	if (val)							\
10560a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
10570a100a6fSAdrian Chadd 	else								\
10580a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
10590a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
10600a100a6fSAdrian Chadd } while (0)
10610a100a6fSAdrian Chadd 
1062df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1063df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1064df8bae1dSRodney W. Grimes 				break;
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1067df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1068df8bae1dSRodney W. Grimes 				break;
1069df8bae1dSRodney W. Grimes 
1070df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1071df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1072df8bae1dSRodney W. Grimes 				break;
107382c23ebaSBill Fenner 
10744957466bSMatthew N. Dodd 			case IP_RECVTTL:
10754957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
10764957466bSMatthew N. Dodd 				break;
10774957466bSMatthew N. Dodd 
107882c23ebaSBill Fenner 			case IP_RECVIF:
107982c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
108082c23ebaSBill Fenner 				break;
10816a800098SYoshinobu Inoue 
10826a800098SYoshinobu Inoue 			case IP_FAITH:
10836a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
10846a800098SYoshinobu Inoue 				break;
10858afa2304SBruce M Simpson 
10868afa2304SBruce M Simpson 			case IP_ONESBCAST:
10878afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
10888afa2304SBruce M Simpson 				break;
1089b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1090b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1091b2828ad2SAndre Oppermann 				break;
1092f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1093f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1094be9347e3SAdrian Chadd 				break;
10953cca425bSMichael Tuexen 			case IP_RECVTOS:
10963cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
10973cca425bSMichael Tuexen 				break;
10980a100a6fSAdrian Chadd 			case IP_BINDMULTI:
10990a100a6fSAdrian Chadd 				OPTSET2(INP_BINDMULTI, optval);
11000a100a6fSAdrian Chadd 				break;
1101*9d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
1102*9d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
1103*9d3ddf43SAdrian Chadd 				break;
11040a100a6fSAdrian Chadd #ifdef	RSS
11050a100a6fSAdrian Chadd 			case IP_RSS_LISTEN_BUCKET:
11060a100a6fSAdrian Chadd 				if ((optval >= 0) &&
11070a100a6fSAdrian Chadd 				    (optval < rss_getnumbuckets())) {
11080a100a6fSAdrian Chadd 					inp->inp_rss_listen_bucket = optval;
11090a100a6fSAdrian Chadd 					OPTSET2(INP_RSS_BUCKET_SET, 1);
11100a100a6fSAdrian Chadd 				} else {
11110a100a6fSAdrian Chadd 					error = EINVAL;
11120a100a6fSAdrian Chadd 				}
11130a100a6fSAdrian Chadd 				break;
1114*9d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
1115*9d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
1116*9d3ddf43SAdrian Chadd 				break;
11170a100a6fSAdrian Chadd #endif
1118df8bae1dSRodney W. Grimes 			}
1119df8bae1dSRodney W. Grimes 			break;
1120df8bae1dSRodney W. Grimes #undef OPTSET
11210a100a6fSAdrian Chadd #undef OPTSET2
1122df8bae1dSRodney W. Grimes 
112371498f30SBruce M Simpson 		/*
112471498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
112571498f30SBruce M Simpson 		 * module.
112671498f30SBruce M Simpson 		 */
1127df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1128f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1129df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1130df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1131df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1132df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
113371498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
113471498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
113571498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
113671498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
113771498f30SBruce M Simpson 		case IP_MSFILTER:
113871498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
113971498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
114071498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
114171498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
114271498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
114371498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
114471498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1145df8bae1dSRodney W. Grimes 			break;
1146df8bae1dSRodney W. Grimes 
114733b3ac06SPeter Wemm 		case IP_PORTRANGE:
1148cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1149cfe8b629SGarrett Wollman 					    sizeof optval);
1150cfe8b629SGarrett Wollman 			if (error)
1151cfe8b629SGarrett Wollman 				break;
115233b3ac06SPeter Wemm 
11538501a69cSRobert Watson 			INP_WLOCK(inp);
115433b3ac06SPeter Wemm 			switch (optval) {
115533b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
115633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
115733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
115833b3ac06SPeter Wemm 				break;
115933b3ac06SPeter Wemm 
116033b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
116133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
116233b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
116333b3ac06SPeter Wemm 				break;
116433b3ac06SPeter Wemm 
116533b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
116633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
116733b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
116833b3ac06SPeter Wemm 				break;
116933b3ac06SPeter Wemm 
117033b3ac06SPeter Wemm 			default:
117133b3ac06SPeter Wemm 				error = EINVAL;
117233b3ac06SPeter Wemm 				break;
117333b3ac06SPeter Wemm 			}
11748501a69cSRobert Watson 			INP_WUNLOCK(inp);
1175ce8c72b1SPeter Wemm 			break;
117633b3ac06SPeter Wemm 
1177b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11786a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
11796a800098SYoshinobu Inoue 		{
11806a800098SYoshinobu Inoue 			caddr_t req;
11816a800098SYoshinobu Inoue 			struct mbuf *m;
11826a800098SYoshinobu Inoue 
11836a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
11846a800098SYoshinobu Inoue 				break;
11856a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
11866a800098SYoshinobu Inoue 				break;
11876a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
118897aa4a51SBjoern A. Zeeb 			error = ipsec_set_policy(inp, sopt->sopt_name, req,
1189c26fe973SBjoern A. Zeeb 			    m->m_len, (sopt->sopt_td != NULL) ?
1190c26fe973SBjoern A. Zeeb 			    sopt->sopt_td->td_ucred : NULL);
11916a800098SYoshinobu Inoue 			m_freem(m);
11926a800098SYoshinobu Inoue 			break;
11936a800098SYoshinobu Inoue 		}
1194b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
11956a800098SYoshinobu Inoue 
1196df8bae1dSRodney W. Grimes 		default:
1197df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1198df8bae1dSRodney W. Grimes 			break;
1199df8bae1dSRodney W. Grimes 		}
1200df8bae1dSRodney W. Grimes 		break;
1201df8bae1dSRodney W. Grimes 
1202cfe8b629SGarrett Wollman 	case SOPT_GET:
1203cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1204df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1205df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1206cfe8b629SGarrett Wollman 			if (inp->inp_options)
1207cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1208cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1209cfe8b629SGarrett Wollman 							  char *),
1210cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1211cfe8b629SGarrett Wollman 			else
1212cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1213df8bae1dSRodney W. Grimes 			break;
1214df8bae1dSRodney W. Grimes 
1215df8bae1dSRodney W. Grimes 		case IP_TOS:
1216df8bae1dSRodney W. Grimes 		case IP_TTL:
1217936cd18dSAndre Oppermann 		case IP_MINTTL:
1218df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1219df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1220df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
12214957466bSMatthew N. Dodd 		case IP_RECVTTL:
122282c23ebaSBill Fenner 		case IP_RECVIF:
1223cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
12246a800098SYoshinobu Inoue 		case IP_FAITH:
12258afa2304SBruce M Simpson 		case IP_ONESBCAST:
1226b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
12275f6bf451SAttilio Rao 		case IP_BINDANY:
12283cca425bSMichael Tuexen 		case IP_RECVTOS:
12290a100a6fSAdrian Chadd 		case IP_BINDMULTI:
12309c423972SAdrian Chadd 		case IP_FLOWID:
12319c423972SAdrian Chadd 		case IP_FLOWTYPE:
1232*9d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
12330a100a6fSAdrian Chadd #ifdef	RSS
12340a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
1235*9d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
12360a100a6fSAdrian Chadd #endif
1237cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 			case IP_TOS:
1240ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1241df8bae1dSRodney W. Grimes 				break;
1242df8bae1dSRodney W. Grimes 
1243df8bae1dSRodney W. Grimes 			case IP_TTL:
1244ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1245df8bae1dSRodney W. Grimes 				break;
1246df8bae1dSRodney W. Grimes 
1247936cd18dSAndre Oppermann 			case IP_MINTTL:
1248936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1249936cd18dSAndre Oppermann 				break;
1250936cd18dSAndre Oppermann 
1251df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
12520a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1253df8bae1dSRodney W. Grimes 
1254df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1255df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1256df8bae1dSRodney W. Grimes 				break;
1257df8bae1dSRodney W. Grimes 
1258df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1259df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1260df8bae1dSRodney W. Grimes 				break;
1261df8bae1dSRodney W. Grimes 
1262df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1263df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1264df8bae1dSRodney W. Grimes 				break;
126582c23ebaSBill Fenner 
12664957466bSMatthew N. Dodd 			case IP_RECVTTL:
12674957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
12684957466bSMatthew N. Dodd 				break;
12694957466bSMatthew N. Dodd 
127082c23ebaSBill Fenner 			case IP_RECVIF:
127182c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
127282c23ebaSBill Fenner 				break;
1273cfe8b629SGarrett Wollman 
1274cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1275cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1276cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1277cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1278cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1279cfe8b629SGarrett Wollman 				else
1280cfe8b629SGarrett Wollman 					optval = 0;
1281cfe8b629SGarrett Wollman 				break;
12826a800098SYoshinobu Inoue 
12836a800098SYoshinobu Inoue 			case IP_FAITH:
12846a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
12856a800098SYoshinobu Inoue 				break;
12868afa2304SBruce M Simpson 
12878afa2304SBruce M Simpson 			case IP_ONESBCAST:
12888afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
12898afa2304SBruce M Simpson 				break;
1290b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1291b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1292b2828ad2SAndre Oppermann 				break;
12935f6bf451SAttilio Rao 			case IP_BINDANY:
12945f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
12955f6bf451SAttilio Rao 				break;
12963cca425bSMichael Tuexen 			case IP_RECVTOS:
12973cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
12983cca425bSMichael Tuexen 				break;
12999c423972SAdrian Chadd 			case IP_FLOWID:
13009c423972SAdrian Chadd 				optval = inp->inp_flowid;
13019c423972SAdrian Chadd 				break;
13029c423972SAdrian Chadd 			case IP_FLOWTYPE:
13039c423972SAdrian Chadd 				optval = inp->inp_flowtype;
13049c423972SAdrian Chadd 				break;
1305*9d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
1306*9d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
1307*9d3ddf43SAdrian Chadd 				break;
13089c423972SAdrian Chadd #ifdef	RSS
13097847796aSAdrian Chadd 			case IP_RSSBUCKETID:
13107847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
13117847796aSAdrian Chadd 				    inp->inp_flowtype,
13127847796aSAdrian Chadd 				    &rss_bucket);
13137847796aSAdrian Chadd 				if (retval == 0)
13147847796aSAdrian Chadd 					optval = rss_bucket;
13157847796aSAdrian Chadd 				else
13167847796aSAdrian Chadd 					error = EINVAL;
13179c423972SAdrian Chadd 				break;
1318*9d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
1319*9d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
1320*9d3ddf43SAdrian Chadd 				break;
13219c423972SAdrian Chadd #endif
13220a100a6fSAdrian Chadd 			case IP_BINDMULTI:
13230a100a6fSAdrian Chadd 				optval = OPTBIT2(INP_BINDMULTI);
13240a100a6fSAdrian Chadd 				break;
1325df8bae1dSRodney W. Grimes 			}
1326cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1327df8bae1dSRodney W. Grimes 			break;
1328df8bae1dSRodney W. Grimes 
132971498f30SBruce M Simpson 		/*
133071498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
133171498f30SBruce M Simpson 		 * module.
133271498f30SBruce M Simpson 		 */
1333df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1334f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1335df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1336df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
133771498f30SBruce M Simpson 		case IP_MSFILTER:
133871498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
133933b3ac06SPeter Wemm 			break;
134033b3ac06SPeter Wemm 
1341b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
13426a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13436a800098SYoshinobu Inoue 		{
1344f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
13456a800098SYoshinobu Inoue 			caddr_t req = NULL;
1346686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
13476a800098SYoshinobu Inoue 
1348686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
13496a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1350686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1351686cdd19SJun-ichiro itojun Hagino 			}
135297aa4a51SBjoern A. Zeeb 			error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
13536a800098SYoshinobu Inoue 			if (error == 0)
13546a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1355f63e7634SYoshinobu Inoue 			if (error == 0)
13566a800098SYoshinobu Inoue 				m_freem(m);
13576a800098SYoshinobu Inoue 			break;
13586a800098SYoshinobu Inoue 		}
1359b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
13606a800098SYoshinobu Inoue 
1361df8bae1dSRodney W. Grimes 		default:
1362df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1363df8bae1dSRodney W. Grimes 			break;
1364df8bae1dSRodney W. Grimes 		}
1365df8bae1dSRodney W. Grimes 		break;
1366df8bae1dSRodney W. Grimes 	}
1367df8bae1dSRodney W. Grimes 	return (error);
1368df8bae1dSRodney W. Grimes }
1369df8bae1dSRodney W. Grimes 
1370df8bae1dSRodney W. Grimes /*
1371df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1372df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1373df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1374f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1375f5fea3ddSPaul Traina  * replicating that code here.
1376df8bae1dSRodney W. Grimes  */
1377df8bae1dSRodney W. Grimes static void
1378f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1379f2565d68SRobert Watson     int hlen)
1380df8bae1dSRodney W. Grimes {
1381b375c9ecSLuigi Rizzo 	register struct ip *ip;
1382df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1383df8bae1dSRodney W. Grimes 
1384e4762f75SGeorge V. Neville-Neil 	/*
1385e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1386e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1387e4762f75SGeorge V. Neville-Neil 	 */
1388eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
138986b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
139086b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1391df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1392390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1393390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1394390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1395390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1396390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1397390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1398390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1399390cdc6aSRuslan Ermilov 		}
1400df8bae1dSRodney W. Grimes 		/*
1401df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1402df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1403df8bae1dSRodney W. Grimes 		 */
14048f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1405df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
140686b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1407201c2527SJulian Elischer #if 1 /* XXX */
1408201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
14092b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1410201c2527SJulian Elischer 						dst->sin_family);
1411201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1412201c2527SJulian Elischer 		}
1413201c2527SJulian Elischer #endif
141406a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
1415df8bae1dSRodney W. Grimes 	}
1416df8bae1dSRodney W. Grimes }
1417