xref: /freebsd/sys/netinet/ip_output.c (revision bc97ba51001ddef32d8ac1e4d286f50c362956cb)
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 
351f7e052cSJulian Elischer #include "opt_ipfw.h"
366a800098SYoshinobu Inoue #include "opt_ipsec.h"
374ed84624SRobert Watson #include "opt_mac.h"
3853dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
39e440aed9SQing Li #include "opt_mpath.h"
40fbd1372aSJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
4226f9a767SRodney W. Grimes #include <sys/systm.h>
43f0f6d643SLuigi Rizzo #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46acd3428bSRobert Watson #include <sys/priv.h>
47c26fe973SBjoern A. Zeeb #include <sys/proc.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
519d9edc56SMike Silbersack #include <sys/sysctl.h>
52c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
53603724d3SBjoern A. Zeeb #include <sys/vimage.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/if.h>
569b932e9eSAndre Oppermann #include <net/netisr.h>
57c21fd232SAndre Oppermann #include <net/pfil.h>
58df8bae1dSRodney W. Grimes #include <net/route.h>
59e440aed9SQing Li #ifdef RADIX_MPATH
60e440aed9SQing Li #include <net/radix_mpath.h>
61e440aed9SQing Li #endif
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <netinet/in.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
65df8bae1dSRodney W. Grimes #include <netinet/ip.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
69ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
70df8bae1dSRodney W. Grimes 
71b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
722cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h>
731dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
74b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/
756a800098SYoshinobu Inoue 
761dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
771dfcf0d2SAndre Oppermann 
78aed55708SRobert Watson #include <security/mac/mac_framework.h>
79aed55708SRobert Watson 
802b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
812b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
82f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
83f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
842b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
85f9e354dfSJulian Elischer 
8644e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
87f23b4c91SGarrett Wollman u_short ip_id;
8844e33a07SMarko Zec #endif
89f23b4c91SGarrett Wollman 
9053dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
919d9edc56SMike Silbersack int mbuf_frag_size = 0;
929d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
939d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
949d9edc56SMike Silbersack #endif
959d9edc56SMike Silbersack 
96df8bae1dSRodney W. Grimes static void	ip_mloopback
974d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
98afed1b49SDarren Reed 
99afed1b49SDarren Reed 
10093e0e116SJulian Elischer extern	struct protosw inetsw[];
10193e0e116SJulian Elischer 
102df8bae1dSRodney W. Grimes /*
103df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
104df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
105df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
106df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1073de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1083de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
109df8bae1dSRodney W. Grimes  */
110df8bae1dSRodney W. Grimes int
111f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
112f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
113df8bae1dSRodney W. Grimes {
1148b615593SMarko Zec 	INIT_VNET_NET(curvnet);
1158b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1161e78ac21SJeffrey Hsu 	struct ip *ip;
1172b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
118ac9d7e26SMax Laier 	struct mbuf *m0;
11923bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1203ae2ad08SJohn-Mark Gurney 	int mtu;
1219b932e9eSAndre Oppermann 	int len, error = 0;
1222b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1233956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
124db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
125e3f406b3SRuslan Ermilov 	struct route iproute;
1269b932e9eSAndre Oppermann 	struct in_addr odst;
1279b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1289b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
1299b932e9eSAndre Oppermann #endif
130ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
13136e8826fSMax Laier 
13202c1c707SAndre Oppermann 	if (ro == NULL) {
13302c1c707SAndre Oppermann 		ro = &iproute;
13402c1c707SAndre Oppermann 		bzero(ro, sizeof (*ro));
13502c1c707SAndre Oppermann 	}
13602c1c707SAndre Oppermann 
137bc97ba51SJulian Elischer 	if (inp != NULL) {
138bc97ba51SJulian Elischer 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
139baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
140bc97ba51SJulian Elischer 	}
1412b25acc1SLuigi Rizzo 
142df8bae1dSRodney W. Grimes 	if (opt) {
143cb7641e8SMaxim Konovalov 		len = 0;
144df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
145cb7641e8SMaxim Konovalov 		if (len != 0)
146df8bae1dSRodney W. Grimes 			hlen = len;
147df8bae1dSRodney W. Grimes 	}
148df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1493efc3014SJulian Elischer 
150df8bae1dSRodney W. Grimes 	/*
1516e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
152e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
153e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
154e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
155e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1566e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1576e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1586e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1594d09f5a0SGleb Smirnoff 	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
160df8bae1dSRodney W. Grimes 	 */
161df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
16253be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
16353be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1641f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
165603724d3SBjoern A. Zeeb 		V_ipstat.ips_localout++;
166df8bae1dSRodney W. Grimes 	} else {
16753be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
168df8bae1dSRodney W. Grimes 	}
1699c9137eaSGarrett Wollman 
170df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
1719b932e9eSAndre Oppermann again:
172df8bae1dSRodney W. Grimes 	/*
173df8bae1dSRodney W. Grimes 	 * If there is a cached route,
174df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
175df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
176a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
177a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
178df8bae1dSRodney W. Grimes 	 */
179df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
180a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
1819b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
182df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
1833ae2ad08SJohn-Mark Gurney 		ro->ro_rt = (struct rtentry *)NULL;
184df8bae1dSRodney W. Grimes 	}
1859b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1869b932e9eSAndre Oppermann 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
1879b932e9eSAndre Oppermann #else
1880b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
1899b932e9eSAndre Oppermann #endif
190a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
191df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
192df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
1939b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
194df8bae1dSRodney W. Grimes 	}
195df8bae1dSRodney W. Grimes 	/*
196a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
197a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
198a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
199a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
200df8bae1dSRodney W. Grimes 	 */
201a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
202a3fd02d8SBruce M Simpson 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
203a3fd02d8SBruce M Simpson 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
204603724d3SBjoern A. Zeeb 			V_ipstat.ips_noroute++;
205a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
206a3fd02d8SBruce M Simpson 			goto bad;
207a3fd02d8SBruce M Simpson 		}
208a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
209a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
210a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
211a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
212a3fd02d8SBruce M Simpson 		isbroadcast = 1;
213a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
2140b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2150b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
216603724d3SBjoern A. Zeeb 			V_ipstat.ips_noroute++;
217df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
218df8bae1dSRodney W. Grimes 			goto bad;
219df8bae1dSRodney W. Grimes 		}
220df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
221df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2229f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2233afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
22438c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2253afefa39SDaniel C. Sobral 		/*
22638c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
22738c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2283afefa39SDaniel C. Sobral 		 */
22938c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
23038c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
23138c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
232df8bae1dSRodney W. Grimes 	} else {
2332c17fe93SGarrett Wollman 		/*
23497d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
23597d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
23697d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2372c17fe93SGarrett Wollman 		 */
2380b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
239e440aed9SQing Li #ifdef RADIX_MPATH
2408b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
2418b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
2428b07e49aSJulian Elischer 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
243e440aed9SQing Li #else
2448b07e49aSJulian Elischer 			in_rtalloc_ign(ro, 0,
2458b07e49aSJulian Elischer 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
246e440aed9SQing Li #endif
2470b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
248603724d3SBjoern A. Zeeb 			V_ipstat.ips_noroute++;
249df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
250df8bae1dSRodney W. Grimes 			goto bad;
251df8bae1dSRodney W. Grimes 		}
252df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
253df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
25497d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
255df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
256f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2579f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
258f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2599f9b3dc4SGarrett Wollman 		else
2609f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
261df8bae1dSRodney W. Grimes 	}
2623ae2ad08SJohn-Mark Gurney 	/*
2633ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
2643ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
2653ae2ad08SJohn-Mark Gurney 	 */
266384a05bfSAndre Oppermann 	if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) {
2673ae2ad08SJohn-Mark Gurney 		/*
2683ae2ad08SJohn-Mark Gurney 		 * This case can happen if the user changed the MTU
2693ae2ad08SJohn-Mark Gurney 		 * of an interface after enabling IP on it.  Because
2703ae2ad08SJohn-Mark Gurney 		 * most netifs don't keep track of routes pointing to
2713ae2ad08SJohn-Mark Gurney 		 * them, there is no way for one to update all its
2723ae2ad08SJohn-Mark Gurney 		 * routes when the MTU is changed.
2733ae2ad08SJohn-Mark Gurney 		 */
2743ae2ad08SJohn-Mark Gurney 		if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)
2753ae2ad08SJohn-Mark Gurney 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
2763ae2ad08SJohn-Mark Gurney 		mtu = ro->ro_rt->rt_rmx.rmx_mtu;
2773ae2ad08SJohn-Mark Gurney 	} else {
2783ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
2793ae2ad08SJohn-Mark Gurney 	}
2809b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
281df8bae1dSRodney W. Grimes 		struct in_multi *inm;
282df8bae1dSRodney W. Grimes 
283df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
284df8bae1dSRodney W. Grimes 		/*
285df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
286df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
287df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
288df8bae1dSRodney W. Grimes 		 */
289df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
290df8bae1dSRodney W. Grimes 		/*
291df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
292df8bae1dSRodney W. Grimes 		 */
293df8bae1dSRodney W. Grimes 		if (imo != NULL) {
294df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
2951c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
2961c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
297bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
298bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
299bbb4330bSLuigi Rizzo 				    INADDR_ANY;
300df8bae1dSRodney W. Grimes 		} else
301df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
302df8bae1dSRodney W. Grimes 		/*
303df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
304df8bae1dSRodney W. Grimes 		 */
3051c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
306df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
307603724d3SBjoern A. Zeeb 				V_ipstat.ips_noroute++;
308df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
309df8bae1dSRodney W. Grimes 				goto bad;
310df8bae1dSRodney W. Grimes 			}
3111c5de19aSGarrett Wollman 		}
312df8bae1dSRodney W. Grimes 		/*
313df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
314df8bae1dSRodney W. Grimes 		 * of outgoing interface.
315df8bae1dSRodney W. Grimes 		 */
316df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
31738c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
31838c1bc35SRuslan Ermilov 			if (ia != NULL)
31938c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
320df8bae1dSRodney W. Grimes 		}
321df8bae1dSRodney W. Grimes 
322dd5a318bSRobert Watson 		IN_MULTI_LOCK();
3239b932e9eSAndre Oppermann 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
324df8bae1dSRodney W. Grimes 		if (inm != NULL &&
325df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
326dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
327df8bae1dSRodney W. Grimes 			/*
328df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
329df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
330df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
331df8bae1dSRodney W. Grimes 			 */
33286b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
333df8bae1dSRodney W. Grimes 		}
334df8bae1dSRodney W. Grimes 		else {
335dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
336df8bae1dSRodney W. Grimes 			/*
337df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
338df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
339df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
340df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
341df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
342df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
343df8bae1dSRodney W. Grimes 			 *
344df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
345df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
346df8bae1dSRodney W. Grimes 			 * if necessary.
347df8bae1dSRodney W. Grimes 			 */
348603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
349f0068c4aSGarrett Wollman 				/*
350bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
351f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
352f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
353f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
354f0068c4aSGarrett Wollman 				 */
355603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
356f0068c4aSGarrett Wollman 					imo = NULL;
357bbb4330bSLuigi Rizzo 				if (ip_mforward &&
358bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
359df8bae1dSRodney W. Grimes 					m_freem(m);
360df8bae1dSRodney W. Grimes 					goto done;
361df8bae1dSRodney W. Grimes 				}
362df8bae1dSRodney W. Grimes 			}
363df8bae1dSRodney W. Grimes 		}
3645e9ae478SGarrett Wollman 
365df8bae1dSRodney W. Grimes 		/*
366df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
367df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
368df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
369df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
370df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
371df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
372df8bae1dSRodney W. Grimes 		 */
373f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
374df8bae1dSRodney W. Grimes 			m_freem(m);
375df8bae1dSRodney W. Grimes 			goto done;
376df8bae1dSRodney W. Grimes 		}
377df8bae1dSRodney W. Grimes 
378df8bae1dSRodney W. Grimes 		goto sendit;
379df8bae1dSRodney W. Grimes 	}
3806a7c943cSAndre Oppermann 
381df8bae1dSRodney W. Grimes 	/*
3822b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
383cb459254SJohn-Mark Gurney 	 * of the outoing interface.
384df8bae1dSRodney W. Grimes 	 */
385f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
38638c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
38738c1bc35SRuslan Ermilov 		if (ia != NULL) {
388df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
389f9e354dfSJulian Elischer 		}
39038c1bc35SRuslan Ermilov 	}
3916a7c943cSAndre Oppermann 
392ca7a789aSMax Laier 	/*
393ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
394ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
395ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
396ca7a789aSMax Laier 	 */
39702b199f1SMax Laier #ifdef ALTQ
398ca7a789aSMax Laier 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
3993ae2ad08SJohn-Mark Gurney 	    ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
400ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen))
401ca7a789aSMax Laier #else
4023ae2ad08SJohn-Mark Gurney 	if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
403ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen)
404ca7a789aSMax Laier #endif /* ALTQ */
405ca7a789aSMax Laier 	{
406b5390296SDavid Greenman 		error = ENOBUFS;
407603724d3SBjoern A. Zeeb 		V_ipstat.ips_odropped++;
408bbce982bSGleb Smirnoff 		ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
409b5390296SDavid Greenman 		goto bad;
410b5390296SDavid Greenman 	}
411b5390296SDavid Greenman 
412b5390296SDavid Greenman 	/*
413df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4148c3f5566SRuslan Ermilov 	 * verify user is allowed to send
415df8bae1dSRodney W. Grimes 	 * such a packet.
416df8bae1dSRodney W. Grimes 	 */
4179f9b3dc4SGarrett Wollman 	if (isbroadcast) {
418df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
419df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
420df8bae1dSRodney W. Grimes 			goto bad;
421df8bae1dSRodney W. Grimes 		}
422df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
423df8bae1dSRodney W. Grimes 			error = EACCES;
424df8bae1dSRodney W. Grimes 			goto bad;
425df8bae1dSRodney W. Grimes 		}
426df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
4273ae2ad08SJohn-Mark Gurney 		if (ip->ip_len > mtu) {
428df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
429df8bae1dSRodney W. Grimes 			goto bad;
430df8bae1dSRodney W. Grimes 		}
431df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4329f9b3dc4SGarrett Wollman 	} else {
433df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4349f9b3dc4SGarrett Wollman 	}
435df8bae1dSRodney W. Grimes 
436f1743588SDarren Reed sendit:
437b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
4381dfcf0d2SAndre Oppermann 	switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
4391dfcf0d2SAndre Oppermann 	case 1:
44033841545SHajimu UMEMOTO 		goto bad;
4411dfcf0d2SAndre Oppermann 	case -1:
4421dfcf0d2SAndre Oppermann 		goto done;
4431dfcf0d2SAndre Oppermann 	case 0:
44433841545SHajimu UMEMOTO 	default:
4451dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
44633841545SHajimu UMEMOTO 	}
4471dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
44833841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
44933841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
450b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
45133841545SHajimu UMEMOTO 
452c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
453604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
454c21fd232SAndre Oppermann 		goto passout;
455c21fd232SAndre Oppermann 
456c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
4579b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
458d6a8d588SMax Laier 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
459134ea224SSam Leffler 	if (error != 0 || m == NULL)
460c4ac87eaSDarren Reed 		goto done;
4619b932e9eSAndre Oppermann 
462c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
463fed1c7e9SSøren Schmidt 
4649b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
4659b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
4669b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
467f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
4689b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
4699b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
470f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
471603724d3SBjoern A. Zeeb 				m->m_pkthdr.rcvif = V_loif;
47220c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
47320c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
47420c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
475ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
47620c822f3SJonathan Lemon 			}
47720c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
47820c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
4799b932e9eSAndre Oppermann 
4809b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
4819b932e9eSAndre Oppermann 			goto done;
4829b932e9eSAndre Oppermann 		} else
483f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
4849b932e9eSAndre Oppermann 	}
4859b932e9eSAndre Oppermann 
4869b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4879b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
4889b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4899b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
490603724d3SBjoern A. Zeeb 			m->m_pkthdr.rcvif = V_loif;
4919b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
4929b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
4939b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
4949b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
4959b932e9eSAndre Oppermann 		}
4969b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
4979b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
4989b932e9eSAndre Oppermann 
4999b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
500f9e354dfSJulian Elischer 		goto done;
501f9e354dfSJulian Elischer 	}
5029b932e9eSAndre Oppermann 	/* Or forward to some other address? */
5039b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
5049b932e9eSAndre Oppermann 	if (fwd_tag) {
5059b932e9eSAndre Oppermann 		dst = (struct sockaddr_in *)&ro->ro_dst;
5069b932e9eSAndre Oppermann 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
5079b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
5089b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
5099b932e9eSAndre Oppermann 		goto again;
510099dd043SAndre Oppermann 	}
511099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5122b25acc1SLuigi Rizzo 
513c21fd232SAndre Oppermann passout:
51451c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
51551c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
51651c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
51751c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
518603724d3SBjoern A. Zeeb 			V_ipstat.ips_badaddr++;
51951c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
52051c8ec4aSRuslan Ermilov 			goto bad;
52151c8ec4aSRuslan Ermilov 		}
52251c8ec4aSRuslan Ermilov 	}
52351c8ec4aSRuslan Ermilov 
524206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
525206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
526db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
527db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
528db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
529db4f9cc7SJonathan Lemon 	}
530206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
531db4f9cc7SJonathan Lemon 
532e7319babSPoul-Henning Kamp 	/*
533db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
534233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
535df8bae1dSRodney W. Grimes 	 */
5363ae2ad08SJohn-Mark Gurney 	if (ip->ip_len <= mtu ||
537233dcce1SAndre Oppermann 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
538233dcce1SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
539fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
540fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
541df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
54253be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
543df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
5445da9f8faSJosef Karthauser 
545233dcce1SAndre Oppermann 		/*
546233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
547233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
548233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
549233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
550233dcce1SAndre Oppermann 		 */
55138c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
552233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
5533dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets +=
554233dcce1SAndre Oppermann 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
555233dcce1SAndre Oppermann 			else
5563dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
5573dbee59bSBruce M Simpson 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
5585da9f8faSJosef Karthauser 		}
55953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
5603390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
5613390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
5629d9edc56SMike Silbersack #endif
563147f74d1SAndre Oppermann 		/*
564147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
565147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
566147f74d1SAndre Oppermann 		 */
567147f74d1SAndre Oppermann 		m->m_flags &= ~(M_PROTOFLAGS);
568147f74d1SAndre Oppermann 
569df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
570df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
571df8bae1dSRodney W. Grimes 		goto done;
572df8bae1dSRodney W. Grimes 	}
5731e78ac21SJeffrey Hsu 
574233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
575233dcce1SAndre Oppermann 	if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
576df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
577603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantfrag++;
578df8bae1dSRodney W. Grimes 		goto bad;
579df8bae1dSRodney W. Grimes 	}
5801e78ac21SJeffrey Hsu 
5811e78ac21SJeffrey Hsu 	/*
5821e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
5831e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
5841e78ac21SJeffrey Hsu 	 */
5853ae2ad08SJohn-Mark Gurney 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum);
5861e78ac21SJeffrey Hsu 	if (error)
587df8bae1dSRodney W. Grimes 		goto bad;
5881e78ac21SJeffrey Hsu 	for (; m; m = m0) {
589df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
590b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
59107203494SDaniel C. Sobral 		if (error == 0) {
592fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
59307203494SDaniel C. Sobral 			if (ia != NULL) {
5943dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
5953dbee59bSBruce M Simpson 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
59607203494SDaniel C. Sobral 			}
597147f74d1SAndre Oppermann 			/*
598147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
599147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
600147f74d1SAndre Oppermann 			 */
601147f74d1SAndre Oppermann 			m->m_flags &= ~(M_PROTOFLAGS);
602fe937674SJosef Karthauser 
603df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
604df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
605fe937674SJosef Karthauser 		} else
606df8bae1dSRodney W. Grimes 			m_freem(m);
607df8bae1dSRodney W. Grimes 	}
608df8bae1dSRodney W. Grimes 
609df8bae1dSRodney W. Grimes 	if (error == 0)
610603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragmented++;
6111e78ac21SJeffrey Hsu 
612df8bae1dSRodney W. Grimes done:
6136a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
6146a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
615ac9d7e26SMax Laier 	}
616df8bae1dSRodney W. Grimes 	return (error);
617df8bae1dSRodney W. Grimes bad:
6183528d68fSBill Paul 	m_freem(m);
619df8bae1dSRodney W. Grimes 	goto done;
620df8bae1dSRodney W. Grimes }
621df8bae1dSRodney W. Grimes 
6221e78ac21SJeffrey Hsu /*
6231e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
6241e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
6251e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
6261e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
6271e78ac21SJeffrey Hsu  *
6281e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
6291e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
6301e78ac21SJeffrey Hsu  */
6311e78ac21SJeffrey Hsu int
6321e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
6331e78ac21SJeffrey Hsu     u_long if_hwassist_flags, int sw_csum)
6341e78ac21SJeffrey Hsu {
6358b615593SMarko Zec 	INIT_VNET_INET(curvnet);
6361e78ac21SJeffrey Hsu 	int error = 0;
6371e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
6381e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
6391e78ac21SJeffrey Hsu 	int off;
6401e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
6411e78ac21SJeffrey Hsu 	int firstlen;
6421e78ac21SJeffrey Hsu 	struct mbuf **mnext;
6431e78ac21SJeffrey Hsu 	int nfrags;
6441e78ac21SJeffrey Hsu 
6451e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
646603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantfrag++;
6471e78ac21SJeffrey Hsu 		return EMSGSIZE;
6481e78ac21SJeffrey Hsu 	}
6491e78ac21SJeffrey Hsu 
6501e78ac21SJeffrey Hsu 	/*
6511e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
6521e78ac21SJeffrey Hsu 	 */
6531e78ac21SJeffrey Hsu 	if (len < 8)
6541e78ac21SJeffrey Hsu 		return EMSGSIZE;
6551e78ac21SJeffrey Hsu 
6561e78ac21SJeffrey Hsu 	/*
6571e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
6581e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
6591e78ac21SJeffrey Hsu 	 */
6601e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
6611e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
6621e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
6631e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
6641e78ac21SJeffrey Hsu 	}
6651e78ac21SJeffrey Hsu 
6661e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
6671e78ac21SJeffrey Hsu 		/*
6681e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
6691e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
6701e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
6711e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
6721e78ac21SJeffrey Hsu 		 *
6731e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
6741e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
6751e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
6761e78ac21SJeffrey Hsu 		 */
6771e78ac21SJeffrey Hsu 		int newlen;
6781e78ac21SJeffrey Hsu 		struct mbuf *m;
6791e78ac21SJeffrey Hsu 
6801e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
6811e78ac21SJeffrey Hsu 			off += m->m_len;
6821e78ac21SJeffrey Hsu 
6831e78ac21SJeffrey Hsu 		/*
6841e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
6851e78ac21SJeffrey Hsu 		 * 8-byte boundary
6861e78ac21SJeffrey Hsu 		 */
6871e78ac21SJeffrey Hsu 		if (off < hlen)
6881e78ac21SJeffrey Hsu 			goto smart_frag_failure;
6891e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
6901e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
6911e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
6921e78ac21SJeffrey Hsu 			/* we failed, go back the default */
6931e78ac21SJeffrey Hsu smart_frag_failure:
6941e78ac21SJeffrey Hsu 			newlen = len;
6951e78ac21SJeffrey Hsu 			off = hlen + len;
6961e78ac21SJeffrey Hsu 		}
6971e78ac21SJeffrey Hsu 		len = newlen;
6981e78ac21SJeffrey Hsu 
6991e78ac21SJeffrey Hsu 	} else {
7001e78ac21SJeffrey Hsu 		off = hlen + len;
7011e78ac21SJeffrey Hsu 	}
7021e78ac21SJeffrey Hsu 
7031e78ac21SJeffrey Hsu 	firstlen = off - hlen;
7041e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
7051e78ac21SJeffrey Hsu 
7061e78ac21SJeffrey Hsu 	/*
7071e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
7081e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
7091e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
7101e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
7111e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
7121e78ac21SJeffrey Hsu 	 */
7131e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
7141e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
7151e78ac21SJeffrey Hsu 		struct mbuf *m;
7161e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
7171e78ac21SJeffrey Hsu 
71834333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
7190b17fba7SAndre Oppermann 		if (m == NULL) {
7201e78ac21SJeffrey Hsu 			error = ENOBUFS;
721603724d3SBjoern A. Zeeb 			V_ipstat.ips_odropped++;
7221e78ac21SJeffrey Hsu 			goto done;
7231e78ac21SJeffrey Hsu 		}
7241e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
7251e78ac21SJeffrey Hsu 		/*
7261e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
7271e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
7281e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
7291e78ac21SJeffrey Hsu 		 */
7301e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
7311e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
7321e78ac21SJeffrey Hsu 		*mhip = *ip;
7331e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
7341e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
7351e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
7361e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
7371e78ac21SJeffrey Hsu 		}
7381e78ac21SJeffrey Hsu 		m->m_len = mhlen;
7391e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
7401e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
7411e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
7421e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
7431e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
7441e78ac21SJeffrey Hsu 		} else
7451e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
7461e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
7471e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
7480b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
7491e78ac21SJeffrey Hsu 			m_free(m);
7501e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
751603724d3SBjoern A. Zeeb 			V_ipstat.ips_odropped++;
7521e78ac21SJeffrey Hsu 			goto done;
7531e78ac21SJeffrey Hsu 		}
7541e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
755fc74a9f9SBrooks Davis 		m->m_pkthdr.rcvif = NULL;
7561e78ac21SJeffrey Hsu #ifdef MAC
75730d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
7581e78ac21SJeffrey Hsu #endif
7591e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
7601e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
7611e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
7621e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
7631e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
7641e78ac21SJeffrey Hsu 		*mnext = m;
7651e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
7661e78ac21SJeffrey Hsu 	}
767603724d3SBjoern A. Zeeb 	V_ipstat.ips_ofragments += nfrags;
7681e78ac21SJeffrey Hsu 
7691e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
7701e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
7711e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
7721e78ac21SJeffrey Hsu 
7731e78ac21SJeffrey Hsu 	/*
7741e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
7751e78ac21SJeffrey Hsu 	 * and updating header.
7761e78ac21SJeffrey Hsu 	 */
7771e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
7781e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
7791e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
7801e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
7811e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
7821e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
7831e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
7841e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
7851e78ac21SJeffrey Hsu 
7861e78ac21SJeffrey Hsu done:
7871e78ac21SJeffrey Hsu 	*m_frag = m0;
7881e78ac21SJeffrey Hsu 	return error;
7891e78ac21SJeffrey Hsu }
7901e78ac21SJeffrey Hsu 
7911c238475SJonathan Lemon void
792db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
793db4f9cc7SJonathan Lemon {
794db4f9cc7SJonathan Lemon 	struct ip *ip;
795db4f9cc7SJonathan Lemon 	u_short csum, offset;
796db4f9cc7SJonathan Lemon 
797db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
79853be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
799db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
800206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
801206a3274SRuslan Ermilov 		csum = 0xffff;
802db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
803db4f9cc7SJonathan Lemon 
804db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
805db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
806db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
807db4f9cc7SJonathan Lemon 		/*
808db4f9cc7SJonathan Lemon 		 * XXX
809db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
810db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
8118f8d29f6SAndre Oppermann 		 * in the appropriate next mbuf in the chain.
812db4f9cc7SJonathan Lemon 		 */
8138f8d29f6SAndre Oppermann 		return;
814db4f9cc7SJonathan Lemon 	}
815db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
816db4f9cc7SJonathan Lemon }
817db4f9cc7SJonathan Lemon 
818df8bae1dSRodney W. Grimes /*
819df8bae1dSRodney W. Grimes  * IP socket option processing.
820df8bae1dSRodney W. Grimes  */
821df8bae1dSRodney W. Grimes int
822f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
823df8bae1dSRodney W. Grimes {
824cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
825cfe8b629SGarrett Wollman 	int	error, optval;
826df8bae1dSRodney W. Grimes 
827cfe8b629SGarrett Wollman 	error = optval = 0;
828cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
829bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
830bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
831bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
832bc97ba51SJulian Elischer 			return (0);
833bc97ba51SJulian Elischer 		}
834cfe8b629SGarrett Wollman 		return (EINVAL);
835cfe8b629SGarrett Wollman 	}
836df8bae1dSRodney W. Grimes 
837cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
838cfe8b629SGarrett Wollman 	case SOPT_SET:
839cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
840df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
841df8bae1dSRodney W. Grimes #ifdef notyet
842df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
843df8bae1dSRodney W. Grimes #endif
844cfe8b629SGarrett Wollman 		{
845cfe8b629SGarrett Wollman 			struct mbuf *m;
846cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
847cfe8b629SGarrett Wollman 				error = EMSGSIZE;
848cfe8b629SGarrett Wollman 				break;
849cfe8b629SGarrett Wollman 			}
850ea26d587SRuslan Ermilov 			MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
8510b17fba7SAndre Oppermann 			if (m == NULL) {
852cfe8b629SGarrett Wollman 				error = ENOBUFS;
853cfe8b629SGarrett Wollman 				break;
854cfe8b629SGarrett Wollman 			}
855cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
856cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
857cfe8b629SGarrett Wollman 					    m->m_len);
858635354c4SMaxim Konovalov 			if (error) {
859635354c4SMaxim Konovalov 				m_free(m);
860635354c4SMaxim Konovalov 				break;
861635354c4SMaxim Konovalov 			}
8628501a69cSRobert Watson 			INP_WLOCK(inp);
863993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
8648501a69cSRobert Watson 			INP_WUNLOCK(inp);
865993d9505SRobert Watson 			return (error);
866cfe8b629SGarrett Wollman 		}
867df8bae1dSRodney W. Grimes 
868df8bae1dSRodney W. Grimes 		case IP_TOS:
869df8bae1dSRodney W. Grimes 		case IP_TTL:
870936cd18dSAndre Oppermann 		case IP_MINTTL:
871df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
872df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
873df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
8744957466bSMatthew N. Dodd 		case IP_RECVTTL:
87582c23ebaSBill Fenner 		case IP_RECVIF:
8766a800098SYoshinobu Inoue 		case IP_FAITH:
8778afa2304SBruce M Simpson 		case IP_ONESBCAST:
878b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
879cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
880cfe8b629SGarrett Wollman 					    sizeof optval);
881cfe8b629SGarrett Wollman 			if (error)
882cfe8b629SGarrett Wollman 				break;
883df8bae1dSRodney W. Grimes 
884cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
885df8bae1dSRodney W. Grimes 			case IP_TOS:
886ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
887df8bae1dSRodney W. Grimes 				break;
888df8bae1dSRodney W. Grimes 
889df8bae1dSRodney W. Grimes 			case IP_TTL:
890ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
891df8bae1dSRodney W. Grimes 				break;
892936cd18dSAndre Oppermann 
893936cd18dSAndre Oppermann 			case IP_MINTTL:
894936cd18dSAndre Oppermann 				if (optval > 0 && optval <= MAXTTL)
895936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
896936cd18dSAndre Oppermann 				else
897936cd18dSAndre Oppermann 					error = EINVAL;
898936cd18dSAndre Oppermann 				break;
899936cd18dSAndre Oppermann 
900a138d217SRobert Watson #define	OPTSET(bit) do {						\
9018501a69cSRobert Watson 	INP_WLOCK(inp);							\
902df8bae1dSRodney W. Grimes 	if (optval)							\
903df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
904df8bae1dSRodney W. Grimes 	else								\
905a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
9068501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
907a138d217SRobert Watson } while (0)
908df8bae1dSRodney W. Grimes 
909df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
910df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
911df8bae1dSRodney W. Grimes 				break;
912df8bae1dSRodney W. Grimes 
913df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
914df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
915df8bae1dSRodney W. Grimes 				break;
916df8bae1dSRodney W. Grimes 
917df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
918df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
919df8bae1dSRodney W. Grimes 				break;
92082c23ebaSBill Fenner 
9214957466bSMatthew N. Dodd 			case IP_RECVTTL:
9224957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
9234957466bSMatthew N. Dodd 				break;
9244957466bSMatthew N. Dodd 
92582c23ebaSBill Fenner 			case IP_RECVIF:
92682c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
92782c23ebaSBill Fenner 				break;
9286a800098SYoshinobu Inoue 
9296a800098SYoshinobu Inoue 			case IP_FAITH:
9306a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
9316a800098SYoshinobu Inoue 				break;
9328afa2304SBruce M Simpson 
9338afa2304SBruce M Simpson 			case IP_ONESBCAST:
9348afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
9358afa2304SBruce M Simpson 				break;
936b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
937b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
938b2828ad2SAndre Oppermann 				break;
939df8bae1dSRodney W. Grimes 			}
940df8bae1dSRodney W. Grimes 			break;
941df8bae1dSRodney W. Grimes #undef OPTSET
942df8bae1dSRodney W. Grimes 
94371498f30SBruce M Simpson 		/*
94471498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
94571498f30SBruce M Simpson 		 * module.
94671498f30SBruce M Simpson 		 */
947df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
948f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
949df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
950df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
951df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
952df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
95371498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
95471498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
95571498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
95671498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
95771498f30SBruce M Simpson 		case IP_MSFILTER:
95871498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
95971498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
96071498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
96171498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
96271498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
96371498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
96471498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
965df8bae1dSRodney W. Grimes 			break;
966df8bae1dSRodney W. Grimes 
96733b3ac06SPeter Wemm 		case IP_PORTRANGE:
968cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
969cfe8b629SGarrett Wollman 					    sizeof optval);
970cfe8b629SGarrett Wollman 			if (error)
971cfe8b629SGarrett Wollman 				break;
97233b3ac06SPeter Wemm 
9738501a69cSRobert Watson 			INP_WLOCK(inp);
97433b3ac06SPeter Wemm 			switch (optval) {
97533b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
97633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
97733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
97833b3ac06SPeter Wemm 				break;
97933b3ac06SPeter Wemm 
98033b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
98133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
98233b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
98333b3ac06SPeter Wemm 				break;
98433b3ac06SPeter Wemm 
98533b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
98633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
98733b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
98833b3ac06SPeter Wemm 				break;
98933b3ac06SPeter Wemm 
99033b3ac06SPeter Wemm 			default:
99133b3ac06SPeter Wemm 				error = EINVAL;
99233b3ac06SPeter Wemm 				break;
99333b3ac06SPeter Wemm 			}
9948501a69cSRobert Watson 			INP_WUNLOCK(inp);
995ce8c72b1SPeter Wemm 			break;
99633b3ac06SPeter Wemm 
997b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
9986a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
9996a800098SYoshinobu Inoue 		{
10006a800098SYoshinobu Inoue 			caddr_t req;
10016a800098SYoshinobu Inoue 			struct mbuf *m;
10026a800098SYoshinobu Inoue 
10036a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
10046a800098SYoshinobu Inoue 				break;
10056a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
10066a800098SYoshinobu Inoue 				break;
10076a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1008c26fe973SBjoern A. Zeeb 			error = ipsec4_set_policy(inp, sopt->sopt_name, req,
1009c26fe973SBjoern A. Zeeb 			    m->m_len, (sopt->sopt_td != NULL) ?
1010c26fe973SBjoern A. Zeeb 			    sopt->sopt_td->td_ucred : NULL);
10116a800098SYoshinobu Inoue 			m_freem(m);
10126a800098SYoshinobu Inoue 			break;
10136a800098SYoshinobu Inoue 		}
1014b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
10156a800098SYoshinobu Inoue 
1016df8bae1dSRodney W. Grimes 		default:
1017df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1018df8bae1dSRodney W. Grimes 			break;
1019df8bae1dSRodney W. Grimes 		}
1020df8bae1dSRodney W. Grimes 		break;
1021df8bae1dSRodney W. Grimes 
1022cfe8b629SGarrett Wollman 	case SOPT_GET:
1023cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1024df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1025df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1026cfe8b629SGarrett Wollman 			if (inp->inp_options)
1027cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1028cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1029cfe8b629SGarrett Wollman 							  char *),
1030cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1031cfe8b629SGarrett Wollman 			else
1032cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1033df8bae1dSRodney W. Grimes 			break;
1034df8bae1dSRodney W. Grimes 
1035df8bae1dSRodney W. Grimes 		case IP_TOS:
1036df8bae1dSRodney W. Grimes 		case IP_TTL:
1037936cd18dSAndre Oppermann 		case IP_MINTTL:
1038df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1039df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1040df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
10414957466bSMatthew N. Dodd 		case IP_RECVTTL:
104282c23ebaSBill Fenner 		case IP_RECVIF:
1043cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
10446a800098SYoshinobu Inoue 		case IP_FAITH:
10458afa2304SBruce M Simpson 		case IP_ONESBCAST:
1046b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
1047cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1048df8bae1dSRodney W. Grimes 
1049df8bae1dSRodney W. Grimes 			case IP_TOS:
1050ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1051df8bae1dSRodney W. Grimes 				break;
1052df8bae1dSRodney W. Grimes 
1053df8bae1dSRodney W. Grimes 			case IP_TTL:
1054ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1055df8bae1dSRodney W. Grimes 				break;
1056df8bae1dSRodney W. Grimes 
1057936cd18dSAndre Oppermann 			case IP_MINTTL:
1058936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1059936cd18dSAndre Oppermann 				break;
1060936cd18dSAndre Oppermann 
1061df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1062df8bae1dSRodney W. Grimes 
1063df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1064df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1065df8bae1dSRodney W. Grimes 				break;
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1068df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1069df8bae1dSRodney W. Grimes 				break;
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1072df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1073df8bae1dSRodney W. Grimes 				break;
107482c23ebaSBill Fenner 
10754957466bSMatthew N. Dodd 			case IP_RECVTTL:
10764957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
10774957466bSMatthew N. Dodd 				break;
10784957466bSMatthew N. Dodd 
107982c23ebaSBill Fenner 			case IP_RECVIF:
108082c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
108182c23ebaSBill Fenner 				break;
1082cfe8b629SGarrett Wollman 
1083cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1084cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1085cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1086cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1087cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1088cfe8b629SGarrett Wollman 				else
1089cfe8b629SGarrett Wollman 					optval = 0;
1090cfe8b629SGarrett Wollman 				break;
10916a800098SYoshinobu Inoue 
10926a800098SYoshinobu Inoue 			case IP_FAITH:
10936a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
10946a800098SYoshinobu Inoue 				break;
10958afa2304SBruce M Simpson 
10968afa2304SBruce M Simpson 			case IP_ONESBCAST:
10978afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
10988afa2304SBruce M Simpson 				break;
1099b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1100b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1101b2828ad2SAndre Oppermann 				break;
1102df8bae1dSRodney W. Grimes 			}
1103cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1104df8bae1dSRodney W. Grimes 			break;
1105df8bae1dSRodney W. Grimes 
110671498f30SBruce M Simpson 		/*
110771498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
110871498f30SBruce M Simpson 		 * module.
110971498f30SBruce M Simpson 		 */
1110df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1111f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1112df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1113df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
111471498f30SBruce M Simpson 		case IP_MSFILTER:
111571498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
111633b3ac06SPeter Wemm 			break;
111733b3ac06SPeter Wemm 
1118b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11196a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
11206a800098SYoshinobu Inoue 		{
1121f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
11226a800098SYoshinobu Inoue 			caddr_t req = NULL;
1123686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
11246a800098SYoshinobu Inoue 
1125686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
11266a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1127686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1128686cdd19SJun-ichiro itojun Hagino 			}
1129686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
11306a800098SYoshinobu Inoue 			if (error == 0)
11316a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1132f63e7634SYoshinobu Inoue 			if (error == 0)
11336a800098SYoshinobu Inoue 				m_freem(m);
11346a800098SYoshinobu Inoue 			break;
11356a800098SYoshinobu Inoue 		}
1136b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
11376a800098SYoshinobu Inoue 
1138df8bae1dSRodney W. Grimes 		default:
1139df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1140df8bae1dSRodney W. Grimes 			break;
1141df8bae1dSRodney W. Grimes 		}
1142df8bae1dSRodney W. Grimes 		break;
1143df8bae1dSRodney W. Grimes 	}
1144df8bae1dSRodney W. Grimes 	return (error);
1145df8bae1dSRodney W. Grimes }
1146df8bae1dSRodney W. Grimes 
1147df8bae1dSRodney W. Grimes /*
1148df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1149df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1150df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1151f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1152f5fea3ddSPaul Traina  * replicating that code here.
1153df8bae1dSRodney W. Grimes  */
1154df8bae1dSRodney W. Grimes static void
1155f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1156f2565d68SRobert Watson     int hlen)
1157df8bae1dSRodney W. Grimes {
1158b375c9ecSLuigi Rizzo 	register struct ip *ip;
1159df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1160df8bae1dSRodney W. Grimes 
1161e4762f75SGeorge V. Neville-Neil 	/*
1162e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1163e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1164e4762f75SGeorge V. Neville-Neil 	 */
1165e4762f75SGeorge V. Neville-Neil 	copym = m_dup(m, M_DONTWAIT);
116686b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
116786b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1168df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1169390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1170390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1171390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1172390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1173390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1174390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1175390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1176390cdc6aSRuslan Ermilov 		}
1177df8bae1dSRodney W. Grimes 		/*
1178df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1179df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1180df8bae1dSRodney W. Grimes 		 */
1181df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1182fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1183fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1184df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
118586b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1186201c2527SJulian Elischer #if 1 /* XXX */
1187201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
11882b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1189201c2527SJulian Elischer 						dst->sin_family);
1190201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1191201c2527SJulian Elischer 		}
1192201c2527SJulian Elischer #endif
119306a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
1194df8bae1dSRodney W. Grimes 	}
1195df8bae1dSRodney W. Grimes }
1196