xref: /freebsd/sys/netinet/ip_output.c (revision d9f2a782491ca34d4939659f89b50786da5be1bb)
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>
47cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50acd3428bSRobert Watson #include <sys/priv.h>
51c26fe973SBjoern A. Zeeb #include <sys/proc.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
5457f60867SMark Johnston #include <sys/sdt.h>
55df8bae1dSRodney W. Grimes #include <sys/socket.h>
56df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
579d9edc56SMike Silbersack #include <sys/sysctl.h>
58c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <net/if.h>
6176039bc8SGleb Smirnoff #include <net/if_var.h>
623ef5e21dSQing Li #include <net/if_llatbl.h>
639b932e9eSAndre Oppermann #include <net/netisr.h>
64c21fd232SAndre Oppermann #include <net/pfil.h>
65df8bae1dSRodney W. Grimes #include <net/route.h>
6665111ec7SKip Macy #include <net/flowtable.h>
67e440aed9SQing Li #ifdef RADIX_MPATH
68e440aed9SQing Li #include <net/radix_mpath.h>
69e440aed9SQing Li #endif
70b2bdc62aSAdrian Chadd #include <net/rss_config.h>
714b79449eSBjoern A. Zeeb #include <net/vnet.h>
72df8bae1dSRodney W. Grimes 
73df8bae1dSRodney W. Grimes #include <netinet/in.h>
7457f60867SMark Johnston #include <netinet/in_kdtrace.h>
75df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
76df8bae1dSRodney W. Grimes #include <netinet/ip.h>
77df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
789c423972SAdrian Chadd #include <netinet/in_rss.h>
79df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
80df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
81ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
822f4afd21SRandall Stewart #ifdef SCTP
832f4afd21SRandall Stewart #include <netinet/sctp.h>
842f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
852f4afd21SRandall Stewart #endif
86df8bae1dSRodney W. Grimes 
87b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
882cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h>
891dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
90b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/
916a800098SYoshinobu Inoue 
921dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
931dfcf0d2SAndre Oppermann 
94aed55708SRobert Watson #include <security/mac/mac_framework.h>
95aed55708SRobert Watson 
9653dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
9705b9d121SBjoern A. Zeeb static int mbuf_frag_size = 0;
989d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
999d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1009d9edc56SMike Silbersack #endif
1019d9edc56SMike Silbersack 
102df8bae1dSRodney W. Grimes static void	ip_mloopback
1034d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
104afed1b49SDarren Reed 
105afed1b49SDarren Reed 
1068b889dbbSBruce M Simpson extern int in_mcast_loop;
10793e0e116SJulian Elischer extern	struct protosw inetsw[];
10893e0e116SJulian Elischer 
109*d9f2a782SErmal Luçi static inline int
110*d9f2a782SErmal Luçi ip_output_pfil(struct mbuf *m, struct ifnet *ifp, struct inpcb *inp,
111*d9f2a782SErmal Luçi 	struct sockaddr_in *dst, int *fibnum, int *error)
112*d9f2a782SErmal Luçi {
113*d9f2a782SErmal Luçi 	struct m_tag *fwd_tag = NULL;
114*d9f2a782SErmal Luçi 	struct in_addr odst;
115*d9f2a782SErmal Luçi 	struct ip *ip;
116*d9f2a782SErmal Luçi 
117*d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
118*d9f2a782SErmal Luçi 
119*d9f2a782SErmal Luçi 	/* Run through list of hooks for output packets. */
120*d9f2a782SErmal Luçi 	odst.s_addr = ip->ip_dst.s_addr;
121*d9f2a782SErmal Luçi 	*error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
122*d9f2a782SErmal Luçi 	if ((*error) != 0 || m == NULL)
123*d9f2a782SErmal Luçi 		return 1; /* Finished */
124*d9f2a782SErmal Luçi 
125*d9f2a782SErmal Luçi 	ip = mtod(m, struct ip *);
126*d9f2a782SErmal Luçi 
127*d9f2a782SErmal Luçi 	/* See if destination IP address was changed by packet filter. */
128*d9f2a782SErmal Luçi 	if (odst.s_addr != ip->ip_dst.s_addr) {
129*d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
130*d9f2a782SErmal Luçi 		/* If destination is now ourself drop to ip_input(). */
131*d9f2a782SErmal Luçi 		if (in_localip(ip->ip_dst)) {
132*d9f2a782SErmal Luçi 			m->m_flags |= M_FASTFWD_OURS;
133*d9f2a782SErmal Luçi 			if (m->m_pkthdr.rcvif == NULL)
134*d9f2a782SErmal Luçi 				m->m_pkthdr.rcvif = V_loif;
135*d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
136*d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |=
137*d9f2a782SErmal Luçi 					CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
138*d9f2a782SErmal Luçi 				m->m_pkthdr.csum_data = 0xffff;
139*d9f2a782SErmal Luçi 			}
140*d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
141*d9f2a782SErmal Luçi 				CSUM_IP_CHECKED | CSUM_IP_VALID;
142*d9f2a782SErmal Luçi #ifdef SCTP
143*d9f2a782SErmal Luçi 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
144*d9f2a782SErmal Luçi 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
145*d9f2a782SErmal Luçi #endif
146*d9f2a782SErmal Luçi 			*error = netisr_queue(NETISR_IP, m);
147*d9f2a782SErmal Luçi 			return 1; /* Finished */
148*d9f2a782SErmal Luçi 		}
149*d9f2a782SErmal Luçi 
150*d9f2a782SErmal Luçi 		bzero(dst, sizeof(*dst));
151*d9f2a782SErmal Luçi 		dst->sin_family = AF_INET;
152*d9f2a782SErmal Luçi 		dst->sin_len = sizeof(*dst);
153*d9f2a782SErmal Luçi 		dst->sin_addr = ip->ip_dst;
154*d9f2a782SErmal Luçi 
155*d9f2a782SErmal Luçi 		return -1; /* Reloop */
156*d9f2a782SErmal Luçi 	}
157*d9f2a782SErmal Luçi 	/* See if fib was changed by packet filter. */
158*d9f2a782SErmal Luçi 	if ((*fibnum) != M_GETFIB(m)) {
159*d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
160*d9f2a782SErmal Luçi 		*fibnum = M_GETFIB(m);
161*d9f2a782SErmal Luçi 		return -1; /* Reloop for FIB change */
162*d9f2a782SErmal Luçi 	}
163*d9f2a782SErmal Luçi 
164*d9f2a782SErmal Luçi 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
165*d9f2a782SErmal Luçi 	if (m->m_flags & M_FASTFWD_OURS) {
166*d9f2a782SErmal Luçi 		if (m->m_pkthdr.rcvif == NULL)
167*d9f2a782SErmal Luçi 			m->m_pkthdr.rcvif = V_loif;
168*d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
169*d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |=
170*d9f2a782SErmal Luçi 				CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
171*d9f2a782SErmal Luçi 			m->m_pkthdr.csum_data = 0xffff;
172*d9f2a782SErmal Luçi 		}
173*d9f2a782SErmal Luçi #ifdef SCTP
174*d9f2a782SErmal Luçi 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
175*d9f2a782SErmal Luçi 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
176*d9f2a782SErmal Luçi #endif
177*d9f2a782SErmal Luçi 		m->m_pkthdr.csum_flags |=
178*d9f2a782SErmal Luçi 			CSUM_IP_CHECKED | CSUM_IP_VALID;
179*d9f2a782SErmal Luçi 
180*d9f2a782SErmal Luçi 		*error = netisr_queue(NETISR_IP, m);
181*d9f2a782SErmal Luçi 		return 1; /* Finished */
182*d9f2a782SErmal Luçi 	}
183*d9f2a782SErmal Luçi 	/* Or forward to some other address? */
184*d9f2a782SErmal Luçi 	if ((m->m_flags & M_IP_NEXTHOP) &&
185*d9f2a782SErmal Luçi 	    ((fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL)) {
186*d9f2a782SErmal Luçi 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
187*d9f2a782SErmal Luçi 		m->m_flags |= M_SKIP_FIREWALL;
188*d9f2a782SErmal Luçi 		m->m_flags &= ~M_IP_NEXTHOP;
189*d9f2a782SErmal Luçi 		m_tag_delete(m, fwd_tag);
190*d9f2a782SErmal Luçi 
191*d9f2a782SErmal Luçi 		return -1; /* Reloop for CHANGE of dst */
192*d9f2a782SErmal Luçi 	}
193*d9f2a782SErmal Luçi 
194*d9f2a782SErmal Luçi 	return 0;
195*d9f2a782SErmal Luçi }
196*d9f2a782SErmal Luçi 
197df8bae1dSRodney W. Grimes /*
198df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
199df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
200df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
201df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
202bf984051SGleb Smirnoff  * If route ro is present and has ro_rt initialized, route lookup would be
203bf984051SGleb Smirnoff  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
204bf984051SGleb Smirnoff  * then result of route lookup is stored in ro->ro_rt.
205bf984051SGleb Smirnoff  *
2063de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
2073de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
208df8bae1dSRodney W. Grimes  */
209df8bae1dSRodney W. Grimes int
210f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
211f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
212df8bae1dSRodney W. Grimes {
213cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
2141e78ac21SJeffrey Hsu 	struct ip *ip;
215fc74d005SBjoern A. Zeeb 	struct ifnet *ifp = NULL;	/* keep compiler happy */
216ac9d7e26SMax Laier 	struct mbuf *m0;
21723bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
2183ae2ad08SJohn-Mark Gurney 	int mtu;
219ca8b83b0SLuigi Rizzo 	int error = 0;
220ca8b83b0SLuigi Rizzo 	struct sockaddr_in *dst;
2214c7a6059SGleb Smirnoff 	const struct sockaddr_in *gw;
2223c73180fSGleb Smirnoff 	struct in_ifaddr *ia;
22321d172a3SGleb Smirnoff 	int isbroadcast;
224078468edSGleb Smirnoff 	uint16_t ip_len, ip_off;
225e3f406b3SRuslan Ermilov 	struct route iproute;
226ec396e61SLuigi Rizzo 	struct rtentry *rte;	/* cache for ro->ro_rt */
2279c57a5b6SHiroki Sato 	uint32_t fibnum;
228fe82cbe8SGleb Smirnoff 	int have_ia_ref;
2291a499816SEdward Tomasz Napierala #ifdef IPSEC
2301a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
2311a499816SEdward Tomasz Napierala #endif
232ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
23336e8826fSMax Laier 
234bc97ba51SJulian Elischer 	if (inp != NULL) {
235baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
23662e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
237c2529042SHans Petter Selasky 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
23880cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
2399c423972SAdrian Chadd 			M_HASHTYPE_SET(m, inp->inp_flowtype);
24080cb9f21SKip Macy 		}
241bc97ba51SJulian Elischer 	}
24262e1ba83SRobert Watson 
24362e1ba83SRobert Watson 	if (ro == NULL) {
24462e1ba83SRobert Watson 		ro = &iproute;
24562e1ba83SRobert Watson 		bzero(ro, sizeof (*ro));
246bf984051SGleb Smirnoff 	}
24762e1ba83SRobert Watson 
24853be8fcaSBjoern A. Zeeb #ifdef FLOWTABLE
2490ff96b4fSGleb Smirnoff 	if (ro->ro_rt == NULL)
2500ff96b4fSGleb Smirnoff 		(void )flowtable_lookup(AF_INET, m, ro);
25153be8fcaSBjoern A. Zeeb #endif
2522b25acc1SLuigi Rizzo 
253df8bae1dSRodney W. Grimes 	if (opt) {
254ca8b83b0SLuigi Rizzo 		int len = 0;
255df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
256cb7641e8SMaxim Konovalov 		if (len != 0)
257ca8b83b0SLuigi Rizzo 			hlen = len; /* ip->ip_hl is updated above */
258df8bae1dSRodney W. Grimes 	}
259df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
2608f134647SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
2618f134647SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
2623efc3014SJulian Elischer 
263df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
26453be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
26553be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
2666d947416SGleb Smirnoff 		ip_fillid(ip);
26786425c62SRobert Watson 		IPSTAT_INC(ips_localout);
268df8bae1dSRodney W. Grimes 	} else {
269ca8b83b0SLuigi Rizzo 		/* Header already set, fetch hlen from there */
27053be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
271df8bae1dSRodney W. Grimes 	}
2729c9137eaSGarrett Wollman 
273054692a4SAlexander V. Chernikov 	/*
274054692a4SAlexander V. Chernikov 	 * dst/gw handling:
275054692a4SAlexander V. Chernikov 	 *
2763c065f2fSGleb Smirnoff 	 * dst can be rewritten but always points to &ro->ro_dst.
2773c065f2fSGleb Smirnoff 	 * gw is readonly but can point either to dst OR rt_gateway,
2783c065f2fSGleb Smirnoff 	 * therefore we need restore gw if we're redoing lookup.
279054692a4SAlexander V. Chernikov 	 */
2804c7a6059SGleb Smirnoff 	gw = dst = (struct sockaddr_in *)&ro->ro_dst;
2819c57a5b6SHiroki Sato 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
282*d9f2a782SErmal Luçi 	rte = ro->ro_rt;
283df8bae1dSRodney W. Grimes 	/*
2843c065f2fSGleb Smirnoff 	 * The address family should also be checked in case of sharing
2853c065f2fSGleb Smirnoff 	 * the cache with IPv6.
286df8bae1dSRodney W. Grimes 	 */
287*d9f2a782SErmal Luçi 	if (rte == NULL || dst->sin_family != AF_INET) {
288a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
289df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
290df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2919b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
292df8bae1dSRodney W. Grimes 	}
293*d9f2a782SErmal Luçi again:
294*d9f2a782SErmal Luçi 	ia = NULL;
295*d9f2a782SErmal Luçi 	have_ia_ref = 0;
296df8bae1dSRodney W. Grimes 	/*
297a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
298a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
299a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
300a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
301df8bae1dSRodney W. Grimes 	 */
302a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
3034f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
30458a39d8cSAlan Somers 						      M_GETFIB(m)))) == NULL &&
3054f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
30658a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL) {
30786425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
308a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
309a3fd02d8SBruce M Simpson 			goto bad;
310a3fd02d8SBruce M Simpson 		}
311fe82cbe8SGleb Smirnoff 		have_ia_ref = 1;
312a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
313a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
314a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
315a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
316a3fd02d8SBruce M Simpson 		isbroadcast = 1;
317a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
3184f8585e0SAlan Somers 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
31958a39d8cSAlan Somers 						    M_GETFIB(m)))) == NULL &&
3204f8585e0SAlan Somers 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
32158a39d8cSAlan Somers 						M_GETFIB(m)))) == NULL) {
32286425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
323df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
324df8bae1dSRodney W. Grimes 			goto bad;
325df8bae1dSRodney W. Grimes 		}
326fe82cbe8SGleb Smirnoff 		have_ia_ref = 1;
327df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
328df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
3299f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
3303afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
33138c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
3323afefa39SDaniel C. Sobral 		/*
33338c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
33438c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
3353afefa39SDaniel C. Sobral 		 */
33638c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
337cc0a3c8cSAndrey V. Elsukov 		IFP_TO_IA(ifp, ia, &in_ifa_tracker);
338fe82cbe8SGleb Smirnoff 		if (ia)
339fe82cbe8SGleb Smirnoff 			have_ia_ref = 1;
34038c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
341df8bae1dSRodney W. Grimes 	} else {
3422c17fe93SGarrett Wollman 		/*
34397d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
34497d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
34597d8d152SAndre Oppermann 		 * operation (as it is for ARP).
3462c17fe93SGarrett Wollman 		 */
347ec396e61SLuigi Rizzo 		if (rte == NULL) {
348e440aed9SQing Li #ifdef RADIX_MPATH
3498b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
3508b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
3519c57a5b6SHiroki Sato 			    fibnum);
352e440aed9SQing Li #else
3539c57a5b6SHiroki Sato 			in_rtalloc_ign(ro, 0, fibnum);
354e440aed9SQing Li #endif
355ec396e61SLuigi Rizzo 			rte = ro->ro_rt;
356ec396e61SLuigi Rizzo 		}
357c7ea0aa6SQing Li 		if (rte == NULL ||
358*d9f2a782SErmal Luçi 		    (rte->rt_flags & RTF_UP) == 0 ||
359c7ea0aa6SQing Li 		    rte->rt_ifp == NULL ||
360c7ea0aa6SQing Li 		    !RT_LINK_IS_UP(rte->rt_ifp)) {
3611a499816SEdward Tomasz Napierala #ifdef IPSEC
3621a499816SEdward Tomasz Napierala 			/*
3631a499816SEdward Tomasz Napierala 			 * There is no route for this packet, but it is
3641a499816SEdward Tomasz Napierala 			 * possible that a matching SPD entry exists.
3651a499816SEdward Tomasz Napierala 			 */
3661a499816SEdward Tomasz Napierala 			no_route_but_check_spd = 1;
3671a499816SEdward Tomasz Napierala 			mtu = 0; /* Silence GCC warning. */
3681a499816SEdward Tomasz Napierala 			goto sendit;
3691a499816SEdward Tomasz Napierala #endif
37086425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
371df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
372df8bae1dSRodney W. Grimes 			goto bad;
373df8bae1dSRodney W. Grimes 		}
374ec396e61SLuigi Rizzo 		ia = ifatoia(rte->rt_ifa);
375ec396e61SLuigi Rizzo 		ifp = rte->rt_ifp;
376e3a7aa6fSGleb Smirnoff 		counter_u64_add(rte->rt_pksent, 1);
377ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_GATEWAY)
3784c7a6059SGleb Smirnoff 			gw = (struct sockaddr_in *)rte->rt_gateway;
379ec396e61SLuigi Rizzo 		if (rte->rt_flags & RTF_HOST)
380ec396e61SLuigi Rizzo 			isbroadcast = (rte->rt_flags & RTF_BROADCAST);
3819f9b3dc4SGarrett Wollman 		else
3824c7a6059SGleb Smirnoff 			isbroadcast = in_broadcast(gw->sin_addr, ifp);
383df8bae1dSRodney W. Grimes 	}
384*d9f2a782SErmal Luçi 
3853ae2ad08SJohn-Mark Gurney 	/*
3863ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
3873ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
3883ae2ad08SJohn-Mark Gurney 	 */
3897f948f12SAlexander V. Chernikov 	if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST)))
390e3a7aa6fSGleb Smirnoff 		mtu = rte->rt_mtu;
3917f948f12SAlexander V. Chernikov 	else
3923ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
393c744cde4SBjoern A. Zeeb 	/* Catch a possible divide by zero later. */
394c744cde4SBjoern A. Zeeb 	KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p",
395c744cde4SBjoern A. Zeeb 	    __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp));
396*d9f2a782SErmal Luçi 
3979b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
398df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
399df8bae1dSRodney W. Grimes 		/*
400183e1c86SGleb Smirnoff 		 * IP destination address is multicast.  Make sure "gw"
401183e1c86SGleb Smirnoff 		 * still points to the address in "ro".  (It may have been
402183e1c86SGleb Smirnoff 		 * changed to point to a gateway address, above.)
403183e1c86SGleb Smirnoff 		 */
404183e1c86SGleb Smirnoff 		gw = dst;
405183e1c86SGleb Smirnoff 		/*
406df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
407df8bae1dSRodney W. Grimes 		 */
408df8bae1dSRodney W. Grimes 		if (imo != NULL) {
409df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
4101c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
4111c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
412bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
413bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
414bbb4330bSLuigi Rizzo 				    INADDR_ANY;
415df8bae1dSRodney W. Grimes 		} else
416df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
417df8bae1dSRodney W. Grimes 		/*
418df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
419df8bae1dSRodney W. Grimes 		 */
4201c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
421df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
42286425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
423df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
424df8bae1dSRodney W. Grimes 				goto bad;
425df8bae1dSRodney W. Grimes 			}
4261c5de19aSGarrett Wollman 		}
427df8bae1dSRodney W. Grimes 		/*
428df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
429df8bae1dSRodney W. Grimes 		 * of outgoing interface.
430df8bae1dSRodney W. Grimes 		 */
431df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
43238c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
43338c1bc35SRuslan Ermilov 			if (ia != NULL)
43438c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
435df8bae1dSRodney W. Grimes 		}
436df8bae1dSRodney W. Grimes 
4378b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
4388b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
439df8bae1dSRodney W. Grimes 			/*
4408b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
4418b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
4428b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
4438b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
4448b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
445df8bae1dSRodney W. Grimes 			 */
44686b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
4478b889dbbSBruce M Simpson 		} else {
448df8bae1dSRodney W. Grimes 			/*
449df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
450df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
451df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
452df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
453df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
454df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
455df8bae1dSRodney W. Grimes 			 *
456df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
457df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
458df8bae1dSRodney W. Grimes 			 * if necessary.
459df8bae1dSRodney W. Grimes 			 */
460603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
461f0068c4aSGarrett Wollman 				/*
462bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
463f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
464f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
465f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
466f0068c4aSGarrett Wollman 				 */
467603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
468f0068c4aSGarrett Wollman 					imo = NULL;
469bbb4330bSLuigi Rizzo 				if (ip_mforward &&
470bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
471df8bae1dSRodney W. Grimes 					m_freem(m);
472df8bae1dSRodney W. Grimes 					goto done;
473df8bae1dSRodney W. Grimes 				}
474df8bae1dSRodney W. Grimes 			}
475df8bae1dSRodney W. Grimes 		}
4765e9ae478SGarrett Wollman 
477df8bae1dSRodney W. Grimes 		/*
478df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
479df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
480df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
481df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
4828b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
4838b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
4848b889dbbSBruce M Simpson 		 * the loopback interface.
485df8bae1dSRodney W. Grimes 		 */
486f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
487df8bae1dSRodney W. Grimes 			m_freem(m);
488df8bae1dSRodney W. Grimes 			goto done;
489df8bae1dSRodney W. Grimes 		}
490df8bae1dSRodney W. Grimes 
491df8bae1dSRodney W. Grimes 		goto sendit;
492df8bae1dSRodney W. Grimes 	}
4936a7c943cSAndre Oppermann 
494df8bae1dSRodney W. Grimes 	/*
4952b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
496cb459254SJohn-Mark Gurney 	 * of the outoing interface.
497df8bae1dSRodney W. Grimes 	 */
498f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
49938c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
50038c1bc35SRuslan Ermilov 		if (ia != NULL) {
501df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
502f9e354dfSJulian Elischer 		}
50338c1bc35SRuslan Ermilov 	}
5046a7c943cSAndre Oppermann 
505ca7a789aSMax Laier 	/*
506df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
5078c3f5566SRuslan Ermilov 	 * verify user is allowed to send
508df8bae1dSRodney W. Grimes 	 * such a packet.
509df8bae1dSRodney W. Grimes 	 */
5109f9b3dc4SGarrett Wollman 	if (isbroadcast) {
511df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
512df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
513df8bae1dSRodney W. Grimes 			goto bad;
514df8bae1dSRodney W. Grimes 		}
515df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
516df8bae1dSRodney W. Grimes 			error = EACCES;
517df8bae1dSRodney W. Grimes 			goto bad;
518df8bae1dSRodney W. Grimes 		}
519df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
5208f134647SGleb Smirnoff 		if (ip_len > mtu) {
521df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
522df8bae1dSRodney W. Grimes 			goto bad;
523df8bae1dSRodney W. Grimes 		}
524df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
5259f9b3dc4SGarrett Wollman 	} else {
526df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
5279f9b3dc4SGarrett Wollman 	}
528df8bae1dSRodney W. Grimes 
529f1743588SDarren Reed sendit:
530b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
5310275b2e3SAndrey V. Elsukov 	switch(ip_ipsec_output(&m, inp, &error)) {
5321dfcf0d2SAndre Oppermann 	case 1:
53333841545SHajimu UMEMOTO 		goto bad;
5341dfcf0d2SAndre Oppermann 	case -1:
5351dfcf0d2SAndre Oppermann 		goto done;
5361dfcf0d2SAndre Oppermann 	case 0:
53733841545SHajimu UMEMOTO 	default:
5381dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
53933841545SHajimu UMEMOTO 	}
5401a499816SEdward Tomasz Napierala 	/*
5411a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
5421a499816SEdward Tomasz Napierala 	 */
5431a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
5441a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
5451a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
5461a499816SEdward Tomasz Napierala 		goto bad;
5471a499816SEdward Tomasz Napierala 	}
5481dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
54933841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
55033841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
551b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
55233841545SHajimu UMEMOTO 
553c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
554*d9f2a782SErmal Luçi 	if (PFIL_HOOKED(&V_inet_pfil_hook)) {
555*d9f2a782SErmal Luçi 		switch (ip_output_pfil(m, ifp, inp, dst, &fibnum, &error)) {
556*d9f2a782SErmal Luçi 		case 1: /* Finished */
557c4ac87eaSDarren Reed 			goto done;
5589b932e9eSAndre Oppermann 
559*d9f2a782SErmal Luçi 		case 0: /* Continue normally */
560c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
561*d9f2a782SErmal Luçi 			break;
562fed1c7e9SSøren Schmidt 
563*d9f2a782SErmal Luçi 		case -1: /* Need to try again */
564*d9f2a782SErmal Luçi 			/* Reset everything for a new round */
5659c57a5b6SHiroki Sato 			RO_RTFREE(ro);
566fe82cbe8SGleb Smirnoff 			if (have_ia_ref)
567fe82cbe8SGleb Smirnoff 				ifa_free(&ia->ia_ifa);
568*d9f2a782SErmal Luçi 			ro->ro_lle = NULL;
569*d9f2a782SErmal Luçi 			rte = NULL;
570*d9f2a782SErmal Luçi 			gw = dst;
571*d9f2a782SErmal Luçi 			ip = mtod(m, struct ip *);
5729b932e9eSAndre Oppermann 			goto again;
573*d9f2a782SErmal Luçi 
574*d9f2a782SErmal Luçi 		}
575099dd043SAndre Oppermann 	}
5762b25acc1SLuigi Rizzo 
57751c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
57851c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
57951c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
58051c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
58186425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
58251c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
58351c8ec4aSRuslan Ermilov 			goto bad;
58451c8ec4aSRuslan Ermilov 		}
58551c8ec4aSRuslan Ermilov 	}
58651c8ec4aSRuslan Ermilov 
587206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
588078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
589db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
590078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
591db4f9cc7SJonathan Lemon 	}
5922f4afd21SRandall Stewart #ifdef SCTP
593078468edSGleb Smirnoff 	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
5941966e5b5SRandall Stewart 		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
595078468edSGleb Smirnoff 		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
5962f4afd21SRandall Stewart 	}
5972f4afd21SRandall Stewart #endif
598db4f9cc7SJonathan Lemon 
599e7319babSPoul-Henning Kamp 	/*
600db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
601233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
602df8bae1dSRodney W. Grimes 	 */
60321d172a3SGleb Smirnoff 	if (ip_len <= mtu ||
604bf7dcda3SGleb Smirnoff 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
605df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
606078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
607df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
608078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
609078468edSGleb Smirnoff 		}
6105da9f8faSJosef Karthauser 
611233dcce1SAndre Oppermann 		/*
612233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
613233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
614233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
615233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
616233dcce1SAndre Oppermann 		 */
61738c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
618233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
6197caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets,
6207caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
621233dcce1SAndre Oppermann 			else
6227caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
6237caf4ab7SGleb Smirnoff 
6247caf4ab7SGleb Smirnoff 			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
6255da9f8faSJosef Karthauser 		}
62653dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
6273390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
628eb1b1807SGleb Smirnoff 			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
6299d9edc56SMike Silbersack #endif
630147f74d1SAndre Oppermann 		/*
631147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
632147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
633147f74d1SAndre Oppermann 		 */
63486bd0491SAndre Oppermann 		m_clrprotoflags(m);
63557f60867SMark Johnston 		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
63647e8d432SGleb Smirnoff 		error = (*ifp->if_output)(ifp, m,
63747e8d432SGleb Smirnoff 		    (const struct sockaddr *)gw, ro);
638df8bae1dSRodney W. Grimes 		goto done;
639df8bae1dSRodney W. Grimes 	}
6401e78ac21SJeffrey Hsu 
641233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
64221d172a3SGleb Smirnoff 	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
643df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
64486425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
645df8bae1dSRodney W. Grimes 		goto bad;
646df8bae1dSRodney W. Grimes 	}
6471e78ac21SJeffrey Hsu 
6481e78ac21SJeffrey Hsu 	/*
6491e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
6501e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
6511e78ac21SJeffrey Hsu 	 */
652078468edSGleb Smirnoff 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
6531e78ac21SJeffrey Hsu 	if (error)
654df8bae1dSRodney W. Grimes 		goto bad;
6551e78ac21SJeffrey Hsu 	for (; m; m = m0) {
656df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
657b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
65807203494SDaniel C. Sobral 		if (error == 0) {
659fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
66007203494SDaniel C. Sobral 			if (ia != NULL) {
6617caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
6627caf4ab7SGleb Smirnoff 				counter_u64_add(ia->ia_ifa.ifa_obytes,
6637caf4ab7SGleb Smirnoff 				    m->m_pkthdr.len);
66407203494SDaniel C. Sobral 			}
665147f74d1SAndre Oppermann 			/*
666147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
667147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
668147f74d1SAndre Oppermann 			 */
66986bd0491SAndre Oppermann 			m_clrprotoflags(m);
670fe937674SJosef Karthauser 
67157f60867SMark Johnston 			IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
672df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
67347e8d432SGleb Smirnoff 			    (const struct sockaddr *)gw, ro);
674fe937674SJosef Karthauser 		} else
675df8bae1dSRodney W. Grimes 			m_freem(m);
676df8bae1dSRodney W. Grimes 	}
677df8bae1dSRodney W. Grimes 
678df8bae1dSRodney W. Grimes 	if (error == 0)
67986425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
6801e78ac21SJeffrey Hsu 
681df8bae1dSRodney W. Grimes done:
682bf984051SGleb Smirnoff 	if (ro == &iproute)
683bf984051SGleb Smirnoff 		RO_RTFREE(ro);
684fe82cbe8SGleb Smirnoff 	if (have_ia_ref)
685fe82cbe8SGleb Smirnoff 		ifa_free(&ia->ia_ifa);
686df8bae1dSRodney W. Grimes 	return (error);
687df8bae1dSRodney W. Grimes bad:
6883528d68fSBill Paul 	m_freem(m);
689df8bae1dSRodney W. Grimes 	goto done;
690df8bae1dSRodney W. Grimes }
691df8bae1dSRodney W. Grimes 
6921e78ac21SJeffrey Hsu /*
6931e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
6941e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
6951e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
6961e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
6971e78ac21SJeffrey Hsu  *
6981e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
6991e78ac21SJeffrey Hsu  */
7001e78ac21SJeffrey Hsu int
7011e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
702078468edSGleb Smirnoff     u_long if_hwassist_flags)
7031e78ac21SJeffrey Hsu {
7041e78ac21SJeffrey Hsu 	int error = 0;
7051e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
7061e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
7071e78ac21SJeffrey Hsu 	int off;
7081e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
7091e78ac21SJeffrey Hsu 	int firstlen;
7101e78ac21SJeffrey Hsu 	struct mbuf **mnext;
7111e78ac21SJeffrey Hsu 	int nfrags;
71221d172a3SGleb Smirnoff 	uint16_t ip_len, ip_off;
7131e78ac21SJeffrey Hsu 
71421d172a3SGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
71521d172a3SGleb Smirnoff 	ip_off = ntohs(ip->ip_off);
71621d172a3SGleb Smirnoff 
71721d172a3SGleb Smirnoff 	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
71886425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
7191e78ac21SJeffrey Hsu 		return EMSGSIZE;
7201e78ac21SJeffrey Hsu 	}
7211e78ac21SJeffrey Hsu 
7221e78ac21SJeffrey Hsu 	/*
7231e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
7241e78ac21SJeffrey Hsu 	 */
7251e78ac21SJeffrey Hsu 	if (len < 8)
7261e78ac21SJeffrey Hsu 		return EMSGSIZE;
7271e78ac21SJeffrey Hsu 
7281e78ac21SJeffrey Hsu 	/*
7291e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
7301e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
7311e78ac21SJeffrey Hsu 	 */
732da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
7331e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
7341e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
7351e78ac21SJeffrey Hsu 	}
7362f4afd21SRandall Stewart #ifdef SCTP
737da2299c5SAndre Oppermann 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
7381966e5b5SRandall Stewart 		sctp_delayed_cksum(m0, hlen);
7392f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
7402f4afd21SRandall Stewart 	}
7412f4afd21SRandall Stewart #endif
7421e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
7431e78ac21SJeffrey Hsu 		/*
7441e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
7451e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
7461e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
7471e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
7481e78ac21SJeffrey Hsu 		 *
7491e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
7501e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
7511e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
7521e78ac21SJeffrey Hsu 		 */
7531e78ac21SJeffrey Hsu 		int newlen;
7541e78ac21SJeffrey Hsu 
7559c0f6aa7SHans Petter Selasky 		off = MIN(mtu, m0->m_pkthdr.len);
7561e78ac21SJeffrey Hsu 
7571e78ac21SJeffrey Hsu 		/*
7581e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
7591e78ac21SJeffrey Hsu 		 * 8-byte boundary
7601e78ac21SJeffrey Hsu 		 */
7611e78ac21SJeffrey Hsu 		if (off < hlen)
7621e78ac21SJeffrey Hsu 			goto smart_frag_failure;
7631e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
7641e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
7651e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
7661e78ac21SJeffrey Hsu 			/* we failed, go back the default */
7671e78ac21SJeffrey Hsu smart_frag_failure:
7681e78ac21SJeffrey Hsu 			newlen = len;
7691e78ac21SJeffrey Hsu 			off = hlen + len;
7701e78ac21SJeffrey Hsu 		}
7711e78ac21SJeffrey Hsu 		len = newlen;
7721e78ac21SJeffrey Hsu 
7731e78ac21SJeffrey Hsu 	} else {
7741e78ac21SJeffrey Hsu 		off = hlen + len;
7751e78ac21SJeffrey Hsu 	}
7761e78ac21SJeffrey Hsu 
7771e78ac21SJeffrey Hsu 	firstlen = off - hlen;
7781e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
7791e78ac21SJeffrey Hsu 
7801e78ac21SJeffrey Hsu 	/*
7811e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
7821e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
7831e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
7841e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
7851e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
7861e78ac21SJeffrey Hsu 	 */
78721d172a3SGleb Smirnoff 	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
7881e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
7891e78ac21SJeffrey Hsu 		struct mbuf *m;
7901e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
7911e78ac21SJeffrey Hsu 
792dc4ad05eSGleb Smirnoff 		m = m_gethdr(M_NOWAIT, MT_DATA);
7930b17fba7SAndre Oppermann 		if (m == NULL) {
7941e78ac21SJeffrey Hsu 			error = ENOBUFS;
79586425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
7961e78ac21SJeffrey Hsu 			goto done;
7971e78ac21SJeffrey Hsu 		}
798c4c4346fSHans Petter Selasky 		/*
799c4c4346fSHans Petter Selasky 		 * Make sure the complete packet header gets copied
800c4c4346fSHans Petter Selasky 		 * from the originating mbuf to the newly created
801c4c4346fSHans Petter Selasky 		 * mbuf. This also ensures that existing firewall
802c4c4346fSHans Petter Selasky 		 * classification(s), VLAN tags and so on get copied
803c4c4346fSHans Petter Selasky 		 * to the resulting fragmented packet(s):
804c4c4346fSHans Petter Selasky 		 */
805c4c4346fSHans Petter Selasky 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
806c4c4346fSHans Petter Selasky 			m_free(m);
807c4c4346fSHans Petter Selasky 			error = ENOBUFS;
808c4c4346fSHans Petter Selasky 			IPSTAT_INC(ips_odropped);
809c4c4346fSHans Petter Selasky 			goto done;
810c4c4346fSHans Petter Selasky 		}
8111e78ac21SJeffrey Hsu 		/*
8121e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
8131e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
8148b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
8151e78ac21SJeffrey Hsu 		 */
8161e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
8171e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
8181e78ac21SJeffrey Hsu 		*mhip = *ip;
8191e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
8201e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
8211e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
8221e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
8231e78ac21SJeffrey Hsu 		}
8241e78ac21SJeffrey Hsu 		m->m_len = mhlen;
82521d172a3SGleb Smirnoff 		/* XXX do we need to add ip_off below ? */
82621d172a3SGleb Smirnoff 		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
827fb86dfcdSAndre Oppermann 		if (off + len >= ip_len)
82821d172a3SGleb Smirnoff 			len = ip_len - off;
829fb86dfcdSAndre Oppermann 		else
8301e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
8311e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
832eb1b1807SGleb Smirnoff 		m->m_next = m_copym(m0, off, len, M_NOWAIT);
8330b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
8341e78ac21SJeffrey Hsu 			m_free(m);
8351e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
83686425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8371e78ac21SJeffrey Hsu 			goto done;
8381e78ac21SJeffrey Hsu 		}
8391e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
8401e78ac21SJeffrey Hsu #ifdef MAC
84130d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
8421e78ac21SJeffrey Hsu #endif
8431e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
8441e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
845078468edSGleb Smirnoff 		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
8461e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
847078468edSGleb Smirnoff 			m->m_pkthdr.csum_flags &= ~CSUM_IP;
848078468edSGleb Smirnoff 		}
8491e78ac21SJeffrey Hsu 		*mnext = m;
8501e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
8511e78ac21SJeffrey Hsu 	}
85286425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
8531e78ac21SJeffrey Hsu 
8541e78ac21SJeffrey Hsu 	/*
8551e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
8561e78ac21SJeffrey Hsu 	 * and updating header.
8571e78ac21SJeffrey Hsu 	 */
85821d172a3SGleb Smirnoff 	m_adj(m0, hlen + firstlen - ip_len);
8591e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
8601e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
86121d172a3SGleb Smirnoff 	ip->ip_off = htons(ip_off | IP_MF);
8621e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
863078468edSGleb Smirnoff 	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
8641e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
865078468edSGleb Smirnoff 		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
866078468edSGleb Smirnoff 	}
8671e78ac21SJeffrey Hsu 
8681e78ac21SJeffrey Hsu done:
8691e78ac21SJeffrey Hsu 	*m_frag = m0;
8701e78ac21SJeffrey Hsu 	return error;
8711e78ac21SJeffrey Hsu }
8721e78ac21SJeffrey Hsu 
8731c238475SJonathan Lemon void
874db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
875db4f9cc7SJonathan Lemon {
876db4f9cc7SJonathan Lemon 	struct ip *ip;
87723e9c6dcSGleb Smirnoff 	uint16_t csum, offset, ip_len;
878db4f9cc7SJonathan Lemon 
879db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
88053be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
88123e9c6dcSGleb Smirnoff 	ip_len = ntohs(ip->ip_len);
88223e9c6dcSGleb Smirnoff 	csum = in_cksum_skip(m, ip_len, offset);
883206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
884206a3274SRuslan Ermilov 		csum = 0xffff;
885db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
886db4f9cc7SJonathan Lemon 
8878e1d0a56SMichael Tuexen 	/* find the mbuf in the chain where the checksum starts*/
8888e1d0a56SMichael Tuexen 	while ((m != NULL) && (offset >= m->m_len)) {
8898e1d0a56SMichael Tuexen 		offset -= m->m_len;
8908e1d0a56SMichael Tuexen 		m = m->m_next;
8918e1d0a56SMichael Tuexen 	}
89226461454SMichael Tuexen 	KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain."));
89326461454SMichael Tuexen 	KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs."));
894db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
895db4f9cc7SJonathan Lemon }
896db4f9cc7SJonathan Lemon 
897df8bae1dSRodney W. Grimes /*
898df8bae1dSRodney W. Grimes  * IP socket option processing.
899df8bae1dSRodney W. Grimes  */
900df8bae1dSRodney W. Grimes int
901f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
902df8bae1dSRodney W. Grimes {
903cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
904cfe8b629SGarrett Wollman 	int	error, optval;
905dc847eb6SAdrian Chadd #ifdef	RSS
906dc847eb6SAdrian Chadd 	uint32_t rss_bucket;
907dc847eb6SAdrian Chadd 	int retval;
908dc847eb6SAdrian Chadd #endif
909df8bae1dSRodney W. Grimes 
910cfe8b629SGarrett Wollman 	error = optval = 0;
911cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
912fc06cd42SMikolaj Golub 		error = EINVAL;
913fc06cd42SMikolaj Golub 
914fc06cd42SMikolaj Golub 		if (sopt->sopt_level == SOL_SOCKET &&
915fc06cd42SMikolaj Golub 		    sopt->sopt_dir == SOPT_SET) {
916fc06cd42SMikolaj Golub 			switch (sopt->sopt_name) {
917fc06cd42SMikolaj Golub 			case SO_REUSEADDR:
918fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
919efdf104bSMikolaj Golub 				if ((so->so_options & SO_REUSEADDR) != 0)
920efdf104bSMikolaj Golub 					inp->inp_flags2 |= INP_REUSEADDR;
921fc06cd42SMikolaj Golub 				else
922efdf104bSMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEADDR;
923fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
924fc06cd42SMikolaj Golub 				error = 0;
925fc06cd42SMikolaj Golub 				break;
926fc06cd42SMikolaj Golub 			case SO_REUSEPORT:
927fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
928fc06cd42SMikolaj Golub 				if ((so->so_options & SO_REUSEPORT) != 0)
929fc06cd42SMikolaj Golub 					inp->inp_flags2 |= INP_REUSEPORT;
930fc06cd42SMikolaj Golub 				else
931fc06cd42SMikolaj Golub 					inp->inp_flags2 &= ~INP_REUSEPORT;
932fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
933fc06cd42SMikolaj Golub 				error = 0;
934fc06cd42SMikolaj Golub 				break;
935fc06cd42SMikolaj Golub 			case SO_SETFIB:
936fc06cd42SMikolaj Golub 				INP_WLOCK(inp);
937fc06cd42SMikolaj Golub 				inp->inp_inc.inc_fibnum = so->so_fibnum;
938fc06cd42SMikolaj Golub 				INP_WUNLOCK(inp);
939fc06cd42SMikolaj Golub 				error = 0;
940fc06cd42SMikolaj Golub 				break;
941fc06cd42SMikolaj Golub 			default:
942fc06cd42SMikolaj Golub 				break;
943fc06cd42SMikolaj Golub 			}
944fc06cd42SMikolaj Golub 		}
945fc06cd42SMikolaj Golub 		return (error);
946cfe8b629SGarrett Wollman 	}
947df8bae1dSRodney W. Grimes 
948cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
949cfe8b629SGarrett Wollman 	case SOPT_SET:
950cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
951df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
952df8bae1dSRodney W. Grimes #ifdef notyet
953df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
954df8bae1dSRodney W. Grimes #endif
955cfe8b629SGarrett Wollman 		{
956cfe8b629SGarrett Wollman 			struct mbuf *m;
957cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
958cfe8b629SGarrett Wollman 				error = EMSGSIZE;
959cfe8b629SGarrett Wollman 				break;
960cfe8b629SGarrett Wollman 			}
961dc4ad05eSGleb Smirnoff 			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
9620b17fba7SAndre Oppermann 			if (m == NULL) {
963cfe8b629SGarrett Wollman 				error = ENOBUFS;
964cfe8b629SGarrett Wollman 				break;
965cfe8b629SGarrett Wollman 			}
966cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
967cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
968cfe8b629SGarrett Wollman 					    m->m_len);
969635354c4SMaxim Konovalov 			if (error) {
970635354c4SMaxim Konovalov 				m_free(m);
971635354c4SMaxim Konovalov 				break;
972635354c4SMaxim Konovalov 			}
9738501a69cSRobert Watson 			INP_WLOCK(inp);
974993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
9758501a69cSRobert Watson 			INP_WUNLOCK(inp);
976993d9505SRobert Watson 			return (error);
977cfe8b629SGarrett Wollman 		}
978df8bae1dSRodney W. Grimes 
979f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
980f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
981f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
982f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
983f44270e7SPawel Jakub Dawidek 				if (error)
984be9347e3SAdrian Chadd 					break;
985be9347e3SAdrian Chadd 			}
986cef27294SAdrian Chadd 			/* FALLTHROUGH */
9870a100a6fSAdrian Chadd 		case IP_BINDMULTI:
9880a100a6fSAdrian Chadd #ifdef	RSS
9890a100a6fSAdrian Chadd 		case IP_RSS_LISTEN_BUCKET:
9900a100a6fSAdrian Chadd #endif
991df8bae1dSRodney W. Grimes 		case IP_TOS:
992df8bae1dSRodney W. Grimes 		case IP_TTL:
993936cd18dSAndre Oppermann 		case IP_MINTTL:
994df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
995df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
996df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
9974957466bSMatthew N. Dodd 		case IP_RECVTTL:
99882c23ebaSBill Fenner 		case IP_RECVIF:
9998afa2304SBruce M Simpson 		case IP_ONESBCAST:
1000b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
10013cca425bSMichael Tuexen 		case IP_RECVTOS:
10029d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
10039d3ddf43SAdrian Chadd #ifdef	RSS
10049d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
10059d3ddf43SAdrian Chadd #endif
1006cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1007cfe8b629SGarrett Wollman 					    sizeof optval);
1008cfe8b629SGarrett Wollman 			if (error)
1009cfe8b629SGarrett Wollman 				break;
1010df8bae1dSRodney W. Grimes 
1011cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1012df8bae1dSRodney W. Grimes 			case IP_TOS:
1013ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1014df8bae1dSRodney W. Grimes 				break;
1015df8bae1dSRodney W. Grimes 
1016df8bae1dSRodney W. Grimes 			case IP_TTL:
1017ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1018df8bae1dSRodney W. Grimes 				break;
1019936cd18dSAndre Oppermann 
1020936cd18dSAndre Oppermann 			case IP_MINTTL:
1021a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
1022936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1023936cd18dSAndre Oppermann 				else
1024936cd18dSAndre Oppermann 					error = EINVAL;
1025936cd18dSAndre Oppermann 				break;
1026936cd18dSAndre Oppermann 
1027a138d217SRobert Watson #define	OPTSET(bit) do {						\
10288501a69cSRobert Watson 	INP_WLOCK(inp);							\
1029df8bae1dSRodney W. Grimes 	if (optval)							\
1030df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1031df8bae1dSRodney W. Grimes 	else								\
1032a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
10338501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
1034a138d217SRobert Watson } while (0)
1035df8bae1dSRodney W. Grimes 
10360a100a6fSAdrian Chadd #define	OPTSET2(bit, val) do {						\
10370a100a6fSAdrian Chadd 	INP_WLOCK(inp);							\
10380a100a6fSAdrian Chadd 	if (val)							\
10390a100a6fSAdrian Chadd 		inp->inp_flags2 |= bit;					\
10400a100a6fSAdrian Chadd 	else								\
10410a100a6fSAdrian Chadd 		inp->inp_flags2 &= ~bit;				\
10420a100a6fSAdrian Chadd 	INP_WUNLOCK(inp);						\
10430a100a6fSAdrian Chadd } while (0)
10440a100a6fSAdrian Chadd 
1045df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1046df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1047df8bae1dSRodney W. Grimes 				break;
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1050df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1051df8bae1dSRodney W. Grimes 				break;
1052df8bae1dSRodney W. Grimes 
1053df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1054df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1055df8bae1dSRodney W. Grimes 				break;
105682c23ebaSBill Fenner 
10574957466bSMatthew N. Dodd 			case IP_RECVTTL:
10584957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
10594957466bSMatthew N. Dodd 				break;
10604957466bSMatthew N. Dodd 
106182c23ebaSBill Fenner 			case IP_RECVIF:
106282c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
106382c23ebaSBill Fenner 				break;
10646a800098SYoshinobu Inoue 
10658afa2304SBruce M Simpson 			case IP_ONESBCAST:
10668afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
10678afa2304SBruce M Simpson 				break;
1068b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1069b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1070b2828ad2SAndre Oppermann 				break;
1071f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1072f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1073be9347e3SAdrian Chadd 				break;
10743cca425bSMichael Tuexen 			case IP_RECVTOS:
10753cca425bSMichael Tuexen 				OPTSET(INP_RECVTOS);
10763cca425bSMichael Tuexen 				break;
10770a100a6fSAdrian Chadd 			case IP_BINDMULTI:
10780a100a6fSAdrian Chadd 				OPTSET2(INP_BINDMULTI, optval);
10790a100a6fSAdrian Chadd 				break;
10809d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
10819d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVFLOWID, optval);
10829d3ddf43SAdrian Chadd 				break;
10830a100a6fSAdrian Chadd #ifdef	RSS
10840a100a6fSAdrian Chadd 			case IP_RSS_LISTEN_BUCKET:
10850a100a6fSAdrian Chadd 				if ((optval >= 0) &&
10860a100a6fSAdrian Chadd 				    (optval < rss_getnumbuckets())) {
10870a100a6fSAdrian Chadd 					inp->inp_rss_listen_bucket = optval;
10880a100a6fSAdrian Chadd 					OPTSET2(INP_RSS_BUCKET_SET, 1);
10890a100a6fSAdrian Chadd 				} else {
10900a100a6fSAdrian Chadd 					error = EINVAL;
10910a100a6fSAdrian Chadd 				}
10920a100a6fSAdrian Chadd 				break;
10939d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
10949d3ddf43SAdrian Chadd 				OPTSET2(INP_RECVRSSBUCKETID, optval);
10959d3ddf43SAdrian Chadd 				break;
10960a100a6fSAdrian Chadd #endif
1097df8bae1dSRodney W. Grimes 			}
1098df8bae1dSRodney W. Grimes 			break;
1099df8bae1dSRodney W. Grimes #undef OPTSET
11000a100a6fSAdrian Chadd #undef OPTSET2
1101df8bae1dSRodney W. Grimes 
110271498f30SBruce M Simpson 		/*
110371498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
110471498f30SBruce M Simpson 		 * module.
110571498f30SBruce M Simpson 		 */
1106df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1107f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1108df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1109df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1110df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1111df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
111271498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
111371498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
111471498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
111571498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
111671498f30SBruce M Simpson 		case IP_MSFILTER:
111771498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
111871498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
111971498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
112071498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
112171498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
112271498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
112371498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1124df8bae1dSRodney W. Grimes 			break;
1125df8bae1dSRodney W. Grimes 
112633b3ac06SPeter Wemm 		case IP_PORTRANGE:
1127cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1128cfe8b629SGarrett Wollman 					    sizeof optval);
1129cfe8b629SGarrett Wollman 			if (error)
1130cfe8b629SGarrett Wollman 				break;
113133b3ac06SPeter Wemm 
11328501a69cSRobert Watson 			INP_WLOCK(inp);
113333b3ac06SPeter Wemm 			switch (optval) {
113433b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
113533b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
113633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
113733b3ac06SPeter Wemm 				break;
113833b3ac06SPeter Wemm 
113933b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
114033b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
114133b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
114233b3ac06SPeter Wemm 				break;
114333b3ac06SPeter Wemm 
114433b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
114533b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
114633b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
114733b3ac06SPeter Wemm 				break;
114833b3ac06SPeter Wemm 
114933b3ac06SPeter Wemm 			default:
115033b3ac06SPeter Wemm 				error = EINVAL;
115133b3ac06SPeter Wemm 				break;
115233b3ac06SPeter Wemm 			}
11538501a69cSRobert Watson 			INP_WUNLOCK(inp);
1154ce8c72b1SPeter Wemm 			break;
115533b3ac06SPeter Wemm 
1156b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11576a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
11586a800098SYoshinobu Inoue 		{
11596a800098SYoshinobu Inoue 			caddr_t req;
11606a800098SYoshinobu Inoue 			struct mbuf *m;
11616a800098SYoshinobu Inoue 
11626a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
11636a800098SYoshinobu Inoue 				break;
11646a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
11656a800098SYoshinobu Inoue 				break;
11666a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
116797aa4a51SBjoern A. Zeeb 			error = ipsec_set_policy(inp, sopt->sopt_name, req,
1168c26fe973SBjoern A. Zeeb 			    m->m_len, (sopt->sopt_td != NULL) ?
1169c26fe973SBjoern A. Zeeb 			    sopt->sopt_td->td_ucred : NULL);
11706a800098SYoshinobu Inoue 			m_freem(m);
11716a800098SYoshinobu Inoue 			break;
11726a800098SYoshinobu Inoue 		}
1173b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
11746a800098SYoshinobu Inoue 
1175df8bae1dSRodney W. Grimes 		default:
1176df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1177df8bae1dSRodney W. Grimes 			break;
1178df8bae1dSRodney W. Grimes 		}
1179df8bae1dSRodney W. Grimes 		break;
1180df8bae1dSRodney W. Grimes 
1181cfe8b629SGarrett Wollman 	case SOPT_GET:
1182cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1183df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1184df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1185cfe8b629SGarrett Wollman 			if (inp->inp_options)
1186cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1187cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1188cfe8b629SGarrett Wollman 							  char *),
1189cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1190cfe8b629SGarrett Wollman 			else
1191cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1192df8bae1dSRodney W. Grimes 			break;
1193df8bae1dSRodney W. Grimes 
1194df8bae1dSRodney W. Grimes 		case IP_TOS:
1195df8bae1dSRodney W. Grimes 		case IP_TTL:
1196936cd18dSAndre Oppermann 		case IP_MINTTL:
1197df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1198df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1199df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
12004957466bSMatthew N. Dodd 		case IP_RECVTTL:
120182c23ebaSBill Fenner 		case IP_RECVIF:
1202cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
12038afa2304SBruce M Simpson 		case IP_ONESBCAST:
1204b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
12055f6bf451SAttilio Rao 		case IP_BINDANY:
12063cca425bSMichael Tuexen 		case IP_RECVTOS:
12070a100a6fSAdrian Chadd 		case IP_BINDMULTI:
12089c423972SAdrian Chadd 		case IP_FLOWID:
12099c423972SAdrian Chadd 		case IP_FLOWTYPE:
12109d3ddf43SAdrian Chadd 		case IP_RECVFLOWID:
12110a100a6fSAdrian Chadd #ifdef	RSS
12120a100a6fSAdrian Chadd 		case IP_RSSBUCKETID:
12139d3ddf43SAdrian Chadd 		case IP_RECVRSSBUCKETID:
12140a100a6fSAdrian Chadd #endif
1215cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1216df8bae1dSRodney W. Grimes 
1217df8bae1dSRodney W. Grimes 			case IP_TOS:
1218ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1219df8bae1dSRodney W. Grimes 				break;
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes 			case IP_TTL:
1222ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1223df8bae1dSRodney W. Grimes 				break;
1224df8bae1dSRodney W. Grimes 
1225936cd18dSAndre Oppermann 			case IP_MINTTL:
1226936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1227936cd18dSAndre Oppermann 				break;
1228936cd18dSAndre Oppermann 
1229df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
12300a100a6fSAdrian Chadd #define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1233df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1234df8bae1dSRodney W. Grimes 				break;
1235df8bae1dSRodney W. Grimes 
1236df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1237df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1238df8bae1dSRodney W. Grimes 				break;
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1241df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1242df8bae1dSRodney W. Grimes 				break;
124382c23ebaSBill Fenner 
12444957466bSMatthew N. Dodd 			case IP_RECVTTL:
12454957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
12464957466bSMatthew N. Dodd 				break;
12474957466bSMatthew N. Dodd 
124882c23ebaSBill Fenner 			case IP_RECVIF:
124982c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
125082c23ebaSBill Fenner 				break;
1251cfe8b629SGarrett Wollman 
1252cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1253cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1254cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1255cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1256cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1257cfe8b629SGarrett Wollman 				else
1258cfe8b629SGarrett Wollman 					optval = 0;
1259cfe8b629SGarrett Wollman 				break;
12606a800098SYoshinobu Inoue 
12618afa2304SBruce M Simpson 			case IP_ONESBCAST:
12628afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
12638afa2304SBruce M Simpson 				break;
1264b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1265b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1266b2828ad2SAndre Oppermann 				break;
12675f6bf451SAttilio Rao 			case IP_BINDANY:
12685f6bf451SAttilio Rao 				optval = OPTBIT(INP_BINDANY);
12695f6bf451SAttilio Rao 				break;
12703cca425bSMichael Tuexen 			case IP_RECVTOS:
12713cca425bSMichael Tuexen 				optval = OPTBIT(INP_RECVTOS);
12723cca425bSMichael Tuexen 				break;
12739c423972SAdrian Chadd 			case IP_FLOWID:
12749c423972SAdrian Chadd 				optval = inp->inp_flowid;
12759c423972SAdrian Chadd 				break;
12769c423972SAdrian Chadd 			case IP_FLOWTYPE:
12779c423972SAdrian Chadd 				optval = inp->inp_flowtype;
12789c423972SAdrian Chadd 				break;
12799d3ddf43SAdrian Chadd 			case IP_RECVFLOWID:
12809d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVFLOWID);
12819d3ddf43SAdrian Chadd 				break;
12829c423972SAdrian Chadd #ifdef	RSS
12837847796aSAdrian Chadd 			case IP_RSSBUCKETID:
12847847796aSAdrian Chadd 				retval = rss_hash2bucket(inp->inp_flowid,
12857847796aSAdrian Chadd 				    inp->inp_flowtype,
12867847796aSAdrian Chadd 				    &rss_bucket);
12877847796aSAdrian Chadd 				if (retval == 0)
12887847796aSAdrian Chadd 					optval = rss_bucket;
12897847796aSAdrian Chadd 				else
12907847796aSAdrian Chadd 					error = EINVAL;
12919c423972SAdrian Chadd 				break;
12929d3ddf43SAdrian Chadd 			case IP_RECVRSSBUCKETID:
12939d3ddf43SAdrian Chadd 				optval = OPTBIT2(INP_RECVRSSBUCKETID);
12949d3ddf43SAdrian Chadd 				break;
12959c423972SAdrian Chadd #endif
12960a100a6fSAdrian Chadd 			case IP_BINDMULTI:
12970a100a6fSAdrian Chadd 				optval = OPTBIT2(INP_BINDMULTI);
12980a100a6fSAdrian Chadd 				break;
1299df8bae1dSRodney W. Grimes 			}
1300cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1301df8bae1dSRodney W. Grimes 			break;
1302df8bae1dSRodney W. Grimes 
130371498f30SBruce M Simpson 		/*
130471498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
130571498f30SBruce M Simpson 		 * module.
130671498f30SBruce M Simpson 		 */
1307df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1308f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1309df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1310df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
131171498f30SBruce M Simpson 		case IP_MSFILTER:
131271498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
131333b3ac06SPeter Wemm 			break;
131433b3ac06SPeter Wemm 
1315b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
13166a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13176a800098SYoshinobu Inoue 		{
1318f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
13196a800098SYoshinobu Inoue 			caddr_t req = NULL;
1320686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
13216a800098SYoshinobu Inoue 
1322686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
13236a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1324686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1325686cdd19SJun-ichiro itojun Hagino 			}
132697aa4a51SBjoern A. Zeeb 			error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
13276a800098SYoshinobu Inoue 			if (error == 0)
13286a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1329f63e7634SYoshinobu Inoue 			if (error == 0)
13306a800098SYoshinobu Inoue 				m_freem(m);
13316a800098SYoshinobu Inoue 			break;
13326a800098SYoshinobu Inoue 		}
1333b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
13346a800098SYoshinobu Inoue 
1335df8bae1dSRodney W. Grimes 		default:
1336df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1337df8bae1dSRodney W. Grimes 			break;
1338df8bae1dSRodney W. Grimes 		}
1339df8bae1dSRodney W. Grimes 		break;
1340df8bae1dSRodney W. Grimes 	}
1341df8bae1dSRodney W. Grimes 	return (error);
1342df8bae1dSRodney W. Grimes }
1343df8bae1dSRodney W. Grimes 
1344df8bae1dSRodney W. Grimes /*
1345df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1346df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1347df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1348f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1349f5fea3ddSPaul Traina  * replicating that code here.
1350df8bae1dSRodney W. Grimes  */
1351df8bae1dSRodney W. Grimes static void
1352f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1353f2565d68SRobert Watson     int hlen)
1354df8bae1dSRodney W. Grimes {
1355b375c9ecSLuigi Rizzo 	register struct ip *ip;
1356df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1357df8bae1dSRodney W. Grimes 
1358e4762f75SGeorge V. Neville-Neil 	/*
1359e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1360e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1361e4762f75SGeorge V. Neville-Neil 	 */
1362eb1b1807SGleb Smirnoff 	copym = m_dup(m, M_NOWAIT);
1363f0cace5dSRobert Watson 	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
136486b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1365df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1366390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1367390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1368390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1369390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1370390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1371390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1372390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1373390cdc6aSRuslan Ermilov 		}
1374df8bae1dSRodney W. Grimes 		/*
1375df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1376df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1377df8bae1dSRodney W. Grimes 		 */
13788f134647SGleb Smirnoff 		ip = mtod(copym, struct ip *);
1379df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
138086b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1381201c2527SJulian Elischer #if 1 /* XXX */
1382201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
13832b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1384201c2527SJulian Elischer 						dst->sin_family);
1385201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1386201c2527SJulian Elischer 		}
1387201c2527SJulian Elischer #endif
138806a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
1389df8bae1dSRodney W. Grimes 	}
1390df8bae1dSRodney W. Grimes }
1391