xref: /freebsd/sys/netinet/ip_output.c (revision 099dd0430b6d2f75cad64e00b7283f4e7ab9602c)
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
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
331f7e052cSJulian Elischer #include "opt_ipfw.h"
346a800098SYoshinobu Inoue #include "opt_ipsec.h"
354ed84624SRobert Watson #include "opt_mac.h"
3653dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
37fbd1372aSJoerg Wunsch 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
3926f9a767SRodney W. Grimes #include <sys/systm.h>
40f0f6d643SLuigi Rizzo #include <sys/kernel.h>
414ed84624SRobert Watson #include <sys/mac.h>
42df8bae1dSRodney W. Grimes #include <sys/malloc.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44df8bae1dSRodney W. Grimes #include <sys/protosw.h>
45df8bae1dSRodney W. Grimes #include <sys/socket.h>
46df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
479d9edc56SMike Silbersack #include <sys/sysctl.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <net/if.h>
509b932e9eSAndre Oppermann #include <net/netisr.h>
51c21fd232SAndre Oppermann #include <net/pfil.h>
52df8bae1dSRodney W. Grimes #include <net/route.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <netinet/in.h>
55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
56df8bae1dSRodney W. Grimes #include <netinet/ip.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
60df8bae1dSRodney W. Grimes 
619c9137eaSGarrett Wollman #include <machine/in_cksum.h>
62df8bae1dSRodney W. Grimes 
63a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
6455166637SPoul-Henning Kamp 
656a800098SYoshinobu Inoue #ifdef IPSEC
666a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
676a800098SYoshinobu Inoue #include <netkey/key.h>
686a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG
696a800098SYoshinobu Inoue #include <netkey/key_debug.h>
706a800098SYoshinobu Inoue #else
716a800098SYoshinobu Inoue #define	KEYDEBUG(lev,arg)
726a800098SYoshinobu Inoue #endif
736a800098SYoshinobu Inoue #endif /*IPSEC*/
746a800098SYoshinobu Inoue 
75b9234fafSSam Leffler #ifdef FAST_IPSEC
76b9234fafSSam Leffler #include <netipsec/ipsec.h>
77b9234fafSSam Leffler #include <netipsec/xform.h>
78b9234fafSSam Leffler #include <netipsec/key.h>
79b9234fafSSam Leffler #endif /*FAST_IPSEC*/
80b9234fafSSam Leffler 
812b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
822b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
83f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
84f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
852b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
86f9e354dfSJulian Elischer 
87f23b4c91SGarrett Wollman u_short ip_id;
88f23b4c91SGarrett Wollman 
8953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
909d9edc56SMike Silbersack int mbuf_frag_size = 0;
919d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
929d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
939d9edc56SMike Silbersack #endif
949d9edc56SMike Silbersack 
954d77a549SAlfred Perlstein static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
964d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *);
97df8bae1dSRodney W. Grimes static void	ip_mloopback
984d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
9989924e58SRobert Watson static int	ip_getmoptions(struct inpcb *, struct sockopt *);
100993d9505SRobert Watson static int	ip_pcbopts(struct inpcb *, int, struct mbuf *);
1015c918b56SRobert Watson static int	ip_setmoptions(struct inpcb *, struct sockopt *);
102df8bae1dSRodney W. Grimes 
1034d77a549SAlfred Perlstein int	ip_optcopy(struct ip *, struct ip *);
104afed1b49SDarren Reed 
105afed1b49SDarren Reed 
10693e0e116SJulian Elischer extern	struct protosw inetsw[];
10793e0e116SJulian Elischer 
108df8bae1dSRodney W. Grimes /*
109df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
110df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
111df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
112df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1133de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1143de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
115df8bae1dSRodney W. Grimes  */
116df8bae1dSRodney W. Grimes int
117ac9d7e26SMax Laier ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
1181e78ac21SJeffrey Hsu 	int flags, struct ip_moptions *imo, struct inpcb *inp)
119df8bae1dSRodney W. Grimes {
1201e78ac21SJeffrey Hsu 	struct ip *ip;
1212b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
122ac9d7e26SMax Laier 	struct mbuf *m0;
12323bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1249b932e9eSAndre Oppermann 	int len, error = 0;
1252b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1263956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
127db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
128e3f406b3SRuslan Ermilov 	struct route iproute;
1299b932e9eSAndre Oppermann 	struct in_addr odst;
1309b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1319b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
1329b932e9eSAndre Oppermann #endif
13302c1c707SAndre Oppermann #ifdef IPSEC
1346a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1356a800098SYoshinobu Inoue #endif
136b9234fafSSam Leffler #ifdef FAST_IPSEC
137b9234fafSSam Leffler 	struct secpolicy *sp = NULL;
138b9234fafSSam Leffler 	struct tdb_ident *tdbi;
1399b932e9eSAndre Oppermann 	struct m_tag *mtag;
140b9234fafSSam Leffler 	int s;
141b9234fafSSam Leffler #endif /* FAST_IPSEC */
14236e8826fSMax Laier 
143ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
14436e8826fSMax Laier 
14502c1c707SAndre Oppermann 	if (ro == NULL) {
14602c1c707SAndre Oppermann 		ro = &iproute;
14702c1c707SAndre Oppermann 		bzero(ro, sizeof (*ro));
14802c1c707SAndre Oppermann 	}
14902c1c707SAndre Oppermann 
15084843845SSam Leffler 	if (inp != NULL)
15184843845SSam Leffler 		INP_LOCK_ASSERT(inp);
1522b25acc1SLuigi Rizzo 
153df8bae1dSRodney W. Grimes 	if (opt) {
154cb7641e8SMaxim Konovalov 		len = 0;
155df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
156cb7641e8SMaxim Konovalov 		if (len != 0)
157df8bae1dSRodney W. Grimes 			hlen = len;
158df8bae1dSRodney W. Grimes 	}
159df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1603efc3014SJulian Elischer 
161df8bae1dSRodney W. Grimes 	/*
1626e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
163e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
164e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
165e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
166e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1676e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1686e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1696e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1706e49b1feSGarrett Wollman 	 * <http://www.research.att.com/~smb/papers/fnat.pdf>.
171df8bae1dSRodney W. Grimes 	 */
172df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
17353be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
17453be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1751f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
176df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
177df8bae1dSRodney W. Grimes 	} else {
17853be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
179df8bae1dSRodney W. Grimes 	}
1809c9137eaSGarrett Wollman 
181df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
1829b932e9eSAndre Oppermann again:
183df8bae1dSRodney W. Grimes 	/*
184df8bae1dSRodney W. Grimes 	 * If there is a cached route,
185df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
186df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
187a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
188a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
189df8bae1dSRodney W. Grimes 	 */
190df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
191a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
1929b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
193df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
194df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
195df8bae1dSRodney W. Grimes 	}
1969b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1979b932e9eSAndre Oppermann 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
1989b932e9eSAndre Oppermann #else
1990b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
2009b932e9eSAndre Oppermann #endif
201a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
202df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
203df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2049b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
205df8bae1dSRodney W. Grimes 	}
206df8bae1dSRodney W. Grimes 	/*
207df8bae1dSRodney W. Grimes 	 * If routing to interface only,
208df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
209df8bae1dSRodney W. Grimes 	 */
210df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
2110b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2120b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
213df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
214df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
215df8bae1dSRodney W. Grimes 			goto bad;
216df8bae1dSRodney W. Grimes 		}
217df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
218df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2199f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2203afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
22138c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2223afefa39SDaniel C. Sobral 		/*
22338c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
22438c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2253afefa39SDaniel C. Sobral 		 */
22638c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
22738c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
22838c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
229df8bae1dSRodney W. Grimes 	} else {
2302c17fe93SGarrett Wollman 		/*
23197d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
23297d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
23397d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2342c17fe93SGarrett Wollman 		 */
2350b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
236e6e51f05SLuigi Rizzo 			rtalloc_ign(ro, 0);
2370b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
238df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
239df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
240df8bae1dSRodney W. Grimes 			goto bad;
241df8bae1dSRodney W. Grimes 		}
242df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
243df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
24497d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
245df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
246f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2479f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
248f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2499f9b3dc4SGarrett Wollman 		else
2509f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
251df8bae1dSRodney W. Grimes 	}
2529b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
253df8bae1dSRodney W. Grimes 		struct in_multi *inm;
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
256df8bae1dSRodney W. Grimes 		/*
257df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
258df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
259df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
260df8bae1dSRodney W. Grimes 		 */
261df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
262df8bae1dSRodney W. Grimes 		/*
263df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
264df8bae1dSRodney W. Grimes 		 */
265df8bae1dSRodney W. Grimes 		if (imo != NULL) {
266df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
2671c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
2681c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
269bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
270bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
271bbb4330bSLuigi Rizzo 				    INADDR_ANY;
272df8bae1dSRodney W. Grimes 		} else
273df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
274df8bae1dSRodney W. Grimes 		/*
275df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
276df8bae1dSRodney W. Grimes 		 */
2771c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
278df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
279df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
280df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
281df8bae1dSRodney W. Grimes 				goto bad;
282df8bae1dSRodney W. Grimes 			}
2831c5de19aSGarrett Wollman 		}
284df8bae1dSRodney W. Grimes 		/*
285df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
286df8bae1dSRodney W. Grimes 		 * of outgoing interface.
287df8bae1dSRodney W. Grimes 		 */
288df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
28938c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
29038c1bc35SRuslan Ermilov 			if (ia != NULL)
29138c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
292df8bae1dSRodney W. Grimes 		}
293df8bae1dSRodney W. Grimes 
2949b932e9eSAndre Oppermann 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
295df8bae1dSRodney W. Grimes 		if (inm != NULL &&
296df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
297df8bae1dSRodney W. Grimes 			/*
298df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
299df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
300df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
301df8bae1dSRodney W. Grimes 			 */
30286b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
303df8bae1dSRodney W. Grimes 		}
304df8bae1dSRodney W. Grimes 		else {
305df8bae1dSRodney W. Grimes 			/*
306df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
307df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
308df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
309df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
310df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
311df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
312df8bae1dSRodney W. Grimes 			 *
313df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
314df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
315df8bae1dSRodney W. Grimes 			 * if necessary.
316df8bae1dSRodney W. Grimes 			 */
317df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
318f0068c4aSGarrett Wollman 				/*
319bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
320f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
321f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
322f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
323f0068c4aSGarrett Wollman 				 */
324b124e4f2SGarrett Wollman 				if (!rsvp_on)
325f0068c4aSGarrett Wollman 					imo = NULL;
326bbb4330bSLuigi Rizzo 				if (ip_mforward &&
327bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
328df8bae1dSRodney W. Grimes 					m_freem(m);
329df8bae1dSRodney W. Grimes 					goto done;
330df8bae1dSRodney W. Grimes 				}
331df8bae1dSRodney W. Grimes 			}
332df8bae1dSRodney W. Grimes 		}
3335e9ae478SGarrett Wollman 
334df8bae1dSRodney W. Grimes 		/*
335df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
336df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
337df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
338df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
339df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
340df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
341df8bae1dSRodney W. Grimes 		 */
342f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
343df8bae1dSRodney W. Grimes 			m_freem(m);
344df8bae1dSRodney W. Grimes 			goto done;
345df8bae1dSRodney W. Grimes 		}
346df8bae1dSRodney W. Grimes 
347df8bae1dSRodney W. Grimes 		goto sendit;
348df8bae1dSRodney W. Grimes 	}
349df8bae1dSRodney W. Grimes #ifndef notdef
350df8bae1dSRodney W. Grimes 	/*
3512b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
352cb459254SJohn-Mark Gurney 	 * of the outoing interface.
353df8bae1dSRodney W. Grimes 	 */
354f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
35538c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
35638c1bc35SRuslan Ermilov 		if (ia != NULL) {
357df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
358f9e354dfSJulian Elischer 		}
35938c1bc35SRuslan Ermilov 	}
360f9e354dfSJulian Elischer #endif /* notdef */
361ca7a789aSMax Laier 	/*
362ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
363ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
364ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
365ca7a789aSMax Laier 	 */
36602b199f1SMax Laier #ifdef ALTQ
367ca7a789aSMax Laier 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
368ca7a789aSMax Laier 	    ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
369ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen))
370ca7a789aSMax Laier #else
371b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
372ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen)
373ca7a789aSMax Laier #endif /* ALTQ */
374ca7a789aSMax Laier 	{
375b5390296SDavid Greenman 		error = ENOBUFS;
37635609d45SJonathan Lemon 		ipstat.ips_odropped++;
377b5390296SDavid Greenman 		goto bad;
378b5390296SDavid Greenman 	}
379b5390296SDavid Greenman 
380b5390296SDavid Greenman 	/*
381df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
3828c3f5566SRuslan Ermilov 	 * verify user is allowed to send
383df8bae1dSRodney W. Grimes 	 * such a packet.
384df8bae1dSRodney W. Grimes 	 */
3859f9b3dc4SGarrett Wollman 	if (isbroadcast) {
386df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
387df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
388df8bae1dSRodney W. Grimes 			goto bad;
389df8bae1dSRodney W. Grimes 		}
390df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
391df8bae1dSRodney W. Grimes 			error = EACCES;
392df8bae1dSRodney W. Grimes 			goto bad;
393df8bae1dSRodney W. Grimes 		}
394df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
3951e78ac21SJeffrey Hsu 		if (ip->ip_len > ifp->if_mtu) {
396df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
397df8bae1dSRodney W. Grimes 			goto bad;
398df8bae1dSRodney W. Grimes 		}
3998afa2304SBruce M Simpson 		if (flags & IP_SENDONES)
4008afa2304SBruce M Simpson 			ip->ip_dst.s_addr = INADDR_BROADCAST;
401df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4029f9b3dc4SGarrett Wollman 	} else {
403df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4049f9b3dc4SGarrett Wollman 	}
405df8bae1dSRodney W. Grimes 
406f1743588SDarren Reed sendit:
40733841545SHajimu UMEMOTO #ifdef IPSEC
40833841545SHajimu UMEMOTO 	/* get SP for this packet */
409f073c60fSHajimu UMEMOTO 	if (inp == NULL)
4100f9ade71SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
4110f9ade71SHajimu UMEMOTO 		    flags, &error);
41233841545SHajimu UMEMOTO 	else
413f073c60fSHajimu UMEMOTO 		sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
41433841545SHajimu UMEMOTO 
41533841545SHajimu UMEMOTO 	if (sp == NULL) {
41633841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
41733841545SHajimu UMEMOTO 		goto bad;
41833841545SHajimu UMEMOTO 	}
41933841545SHajimu UMEMOTO 
42033841545SHajimu UMEMOTO 	error = 0;
42133841545SHajimu UMEMOTO 
42233841545SHajimu UMEMOTO 	/* check policy */
42333841545SHajimu UMEMOTO 	switch (sp->policy) {
42433841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
42533841545SHajimu UMEMOTO 		/*
42633841545SHajimu UMEMOTO 		 * This packet is just discarded.
42733841545SHajimu UMEMOTO 		 */
42833841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
42933841545SHajimu UMEMOTO 		goto bad;
43033841545SHajimu UMEMOTO 
43133841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
43233841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
4331cfd4b53SBruce M Simpson 	case IPSEC_POLICY_TCP:
43433841545SHajimu UMEMOTO 		/* no need to do IPsec. */
43533841545SHajimu UMEMOTO 		goto skip_ipsec;
43633841545SHajimu UMEMOTO 
43733841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
43833841545SHajimu UMEMOTO 		if (sp->req == NULL) {
43933841545SHajimu UMEMOTO 			/* acquire a policy */
44033841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
44133841545SHajimu UMEMOTO 			goto bad;
44233841545SHajimu UMEMOTO 		}
44333841545SHajimu UMEMOTO 		break;
44433841545SHajimu UMEMOTO 
44533841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
44633841545SHajimu UMEMOTO 	default:
44733841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
44833841545SHajimu UMEMOTO 	}
44933841545SHajimu UMEMOTO     {
45033841545SHajimu UMEMOTO 	struct ipsec_output_state state;
45133841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
45233841545SHajimu UMEMOTO 	state.m = m;
45333841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
45433841545SHajimu UMEMOTO 		state.ro = &iproute;
45533841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
45633841545SHajimu UMEMOTO 	} else
45733841545SHajimu UMEMOTO 		state.ro = ro;
45833841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
45933841545SHajimu UMEMOTO 
46033841545SHajimu UMEMOTO 	ip->ip_sum = 0;
46133841545SHajimu UMEMOTO 
46233841545SHajimu UMEMOTO 	/*
46333841545SHajimu UMEMOTO 	 * XXX
46433841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
46533841545SHajimu UMEMOTO 	 */
46633841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
46733841545SHajimu UMEMOTO 		in_delayed_cksum(m);
46833841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
46933841545SHajimu UMEMOTO 	}
47033841545SHajimu UMEMOTO 
471fd8e4ebcSMike Barcroft 	ip->ip_len = htons(ip->ip_len);
472fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
47333841545SHajimu UMEMOTO 
47433841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
47533841545SHajimu UMEMOTO 
47633841545SHajimu UMEMOTO 	m = state.m;
47733841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
47833841545SHajimu UMEMOTO 		/*
47933841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
48033841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
48133841545SHajimu UMEMOTO 		 */
48233841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
48333841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
48433841545SHajimu UMEMOTO 			ro = state.ro;
48533841545SHajimu UMEMOTO 		}
48633841545SHajimu UMEMOTO 	} else
48733841545SHajimu UMEMOTO 		ro = state.ro;
48833841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
48933841545SHajimu UMEMOTO 	if (error) {
49033841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
491ac9d7e26SMax Laier 		m = NULL;
49233841545SHajimu UMEMOTO 		switch (error) {
49333841545SHajimu UMEMOTO 		case EHOSTUNREACH:
49433841545SHajimu UMEMOTO 		case ENETUNREACH:
49533841545SHajimu UMEMOTO 		case EMSGSIZE:
49633841545SHajimu UMEMOTO 		case ENOBUFS:
49733841545SHajimu UMEMOTO 		case ENOMEM:
49833841545SHajimu UMEMOTO 			break;
49933841545SHajimu UMEMOTO 		default:
50033841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
50133841545SHajimu UMEMOTO 			/*fall through*/
50233841545SHajimu UMEMOTO 		case ENOENT:
50333841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
50433841545SHajimu UMEMOTO 			error = 0;
50533841545SHajimu UMEMOTO 			break;
50633841545SHajimu UMEMOTO 		}
50733841545SHajimu UMEMOTO 		goto bad;
50833841545SHajimu UMEMOTO 	}
50933841545SHajimu UMEMOTO 
51033841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
51133841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
51233841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
51333841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
51433841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
51533841545SHajimu UMEMOTO 			printf("ip_output: "
51633841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
51733841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
51833841545SHajimu UMEMOTO 			goto bad;
51933841545SHajimu UMEMOTO 		}
52033841545SHajimu UMEMOTO 	} else {
52170dbc6cbSHajimu UMEMOTO 		if (state.encap) {
52233841545SHajimu UMEMOTO 			ia = ifatoia(ro->ro_rt->rt_ifa);
52333841545SHajimu UMEMOTO 			ifp = ro->ro_rt->rt_ifp;
52433841545SHajimu UMEMOTO 		}
52570dbc6cbSHajimu UMEMOTO 	}
52670dbc6cbSHajimu UMEMOTO     }
52733841545SHajimu UMEMOTO 
52833841545SHajimu UMEMOTO 	/* make it flipped, again. */
529fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
530fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
53133841545SHajimu UMEMOTO skip_ipsec:
53233841545SHajimu UMEMOTO #endif /*IPSEC*/
533b9234fafSSam Leffler #ifdef FAST_IPSEC
534b9234fafSSam Leffler 	/*
535b9234fafSSam Leffler 	 * Check the security policy (SP) for the packet and, if
536b9234fafSSam Leffler 	 * required, do IPsec-related processing.  There are two
537b9234fafSSam Leffler 	 * cases here; the first time a packet is sent through
538b9234fafSSam Leffler 	 * it will be untagged and handled by ipsec4_checkpolicy.
539b9234fafSSam Leffler 	 * If the packet is resubmitted to ip_output (e.g. after
540b9234fafSSam Leffler 	 * AH, ESP, etc. processing), there will be a tag to bypass
541b9234fafSSam Leffler 	 * the lookup and related policy checking.
542b9234fafSSam Leffler 	 */
543b9234fafSSam Leffler 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
544b9234fafSSam Leffler 	s = splnet();
545b9234fafSSam Leffler 	if (mtag != NULL) {
546b9234fafSSam Leffler 		tdbi = (struct tdb_ident *)(mtag + 1);
547b9234fafSSam Leffler 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
548b9234fafSSam Leffler 		if (sp == NULL)
549b9234fafSSam Leffler 			error = -EINVAL;	/* force silent drop */
550b9234fafSSam Leffler 		m_tag_delete(m, mtag);
551b9234fafSSam Leffler 	} else {
552b9234fafSSam Leffler 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
553b9234fafSSam Leffler 					&error, inp);
554b9234fafSSam Leffler 	}
555b9234fafSSam Leffler 	/*
556b9234fafSSam Leffler 	 * There are four return cases:
557b9234fafSSam Leffler 	 *    sp != NULL	 	    apply IPsec policy
558b9234fafSSam Leffler 	 *    sp == NULL, error == 0	    no IPsec handling needed
559b9234fafSSam Leffler 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
560b9234fafSSam Leffler 	 *    sp == NULL, error != 0	    discard packet, report error
561b9234fafSSam Leffler 	 */
562b9234fafSSam Leffler 	if (sp != NULL) {
563b9234fafSSam Leffler 		/* Loop detection, check if ipsec processing already done */
564b9234fafSSam Leffler 		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
565b9234fafSSam Leffler 		for (mtag = m_tag_first(m); mtag != NULL;
566b9234fafSSam Leffler 		     mtag = m_tag_next(m, mtag)) {
567b9234fafSSam Leffler 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
568b9234fafSSam Leffler 				continue;
569b9234fafSSam Leffler 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
570b9234fafSSam Leffler 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
571b9234fafSSam Leffler 				continue;
572b9234fafSSam Leffler 			/*
573b9234fafSSam Leffler 			 * Check if policy has an SA associated with it.
574b9234fafSSam Leffler 			 * This can happen when an SP has yet to acquire
575b9234fafSSam Leffler 			 * an SA; e.g. on first reference.  If it occurs,
576b9234fafSSam Leffler 			 * then we let ipsec4_process_packet do its thing.
577b9234fafSSam Leffler 			 */
578b9234fafSSam Leffler 			if (sp->req->sav == NULL)
579b9234fafSSam Leffler 				break;
580b9234fafSSam Leffler 			tdbi = (struct tdb_ident *)(mtag + 1);
581b9234fafSSam Leffler 			if (tdbi->spi == sp->req->sav->spi &&
582b9234fafSSam Leffler 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
583ab94ca3cSSam Leffler 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
584b9234fafSSam Leffler 				 sizeof (union sockaddr_union)) == 0) {
585b9234fafSSam Leffler 				/*
586b9234fafSSam Leffler 				 * No IPsec processing is needed, free
587b9234fafSSam Leffler 				 * reference to SP.
588b9234fafSSam Leffler 				 *
589b9234fafSSam Leffler 				 * NB: null pointer to avoid free at
590b9234fafSSam Leffler 				 *     done: below.
591b9234fafSSam Leffler 				 */
592b9234fafSSam Leffler 				KEY_FREESP(&sp), sp = NULL;
593b9234fafSSam Leffler 				splx(s);
594b9234fafSSam Leffler 				goto spd_done;
595b9234fafSSam Leffler 			}
596b9234fafSSam Leffler 		}
597b9234fafSSam Leffler 
598b9234fafSSam Leffler 		/*
599b9234fafSSam Leffler 		 * Do delayed checksums now because we send before
600b9234fafSSam Leffler 		 * this is done in the normal processing path.
601b9234fafSSam Leffler 		 */
602b9234fafSSam Leffler 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
603b9234fafSSam Leffler 			in_delayed_cksum(m);
604b9234fafSSam Leffler 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
605b9234fafSSam Leffler 		}
606b9234fafSSam Leffler 
607b9234fafSSam Leffler 		ip->ip_len = htons(ip->ip_len);
608b9234fafSSam Leffler 		ip->ip_off = htons(ip->ip_off);
609b9234fafSSam Leffler 
610b9234fafSSam Leffler 		/* NB: callee frees mbuf */
611b9234fafSSam Leffler 		error = ipsec4_process_packet(m, sp->req, flags, 0);
6129359ad86SSam Leffler 		/*
6139359ad86SSam Leffler 		 * Preserve KAME behaviour: ENOENT can be returned
6149359ad86SSam Leffler 		 * when an SA acquire is in progress.  Don't propagate
6159359ad86SSam Leffler 		 * this to user-level; it confuses applications.
6169359ad86SSam Leffler 		 *
6179359ad86SSam Leffler 		 * XXX this will go away when the SADB is redone.
6189359ad86SSam Leffler 		 */
6199359ad86SSam Leffler 		if (error == ENOENT)
6209359ad86SSam Leffler 			error = 0;
621b9234fafSSam Leffler 		splx(s);
622b9234fafSSam Leffler 		goto done;
623b9234fafSSam Leffler 	} else {
624b9234fafSSam Leffler 		splx(s);
625b9234fafSSam Leffler 
626b9234fafSSam Leffler 		if (error != 0) {
627b9234fafSSam Leffler 			/*
628b9234fafSSam Leffler 			 * Hack: -EINVAL is used to signal that a packet
629b9234fafSSam Leffler 			 * should be silently discarded.  This is typically
630b9234fafSSam Leffler 			 * because we asked key management for an SA and
631b9234fafSSam Leffler 			 * it was delayed (e.g. kicked up to IKE).
632b9234fafSSam Leffler 			 */
633b9234fafSSam Leffler 			if (error == -EINVAL)
634b9234fafSSam Leffler 				error = 0;
635b9234fafSSam Leffler 			goto bad;
636b9234fafSSam Leffler 		} else {
637b9234fafSSam Leffler 			/* No IPsec processing for this packet. */
638b9234fafSSam Leffler 		}
639b9234fafSSam Leffler #ifdef notyet
640b9234fafSSam Leffler 		/*
641b9234fafSSam Leffler 		 * If deferred crypto processing is needed, check that
642b9234fafSSam Leffler 		 * the interface supports it.
643b9234fafSSam Leffler 		 */
644b9234fafSSam Leffler 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
645b9234fafSSam Leffler 		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
646b9234fafSSam Leffler 			/* notify IPsec to do its own crypto */
647b9234fafSSam Leffler 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
648b9234fafSSam Leffler 			error = EHOSTUNREACH;
649b9234fafSSam Leffler 			goto bad;
650b9234fafSSam Leffler 		}
651b9234fafSSam Leffler #endif
652b9234fafSSam Leffler 	}
653b9234fafSSam Leffler spd_done:
654b9234fafSSam Leffler #endif /* FAST_IPSEC */
65533841545SHajimu UMEMOTO 
656c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
657c21fd232SAndre Oppermann 	if (inet_pfil_hook.ph_busy_count == -1)
658c21fd232SAndre Oppermann 		goto passout;
659c21fd232SAndre Oppermann 
660c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
6619b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
662d6a8d588SMax Laier 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
663134ea224SSam Leffler 	if (error != 0 || m == NULL)
664c4ac87eaSDarren Reed 		goto done;
6659b932e9eSAndre Oppermann 
666c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
667fed1c7e9SSøren Schmidt 
6689b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
6699b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
6709b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
671f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
6729b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
6739b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
674f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
675a9c92b54SAndre Oppermann 				m->m_pkthdr.rcvif = loif;
67620c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
67720c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
67820c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
679ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
68020c822f3SJonathan Lemon 			}
68120c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
68220c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
6839b932e9eSAndre Oppermann 
6849b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
6859b932e9eSAndre Oppermann 			goto done;
6869b932e9eSAndre Oppermann 		} else
687f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
6889b932e9eSAndre Oppermann 	}
6899b932e9eSAndre Oppermann 
6909b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
6919b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
6929b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
6939b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
694a9c92b54SAndre Oppermann 			m->m_pkthdr.rcvif = loif;
6959b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
6969b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
6979b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
6989b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
6999b932e9eSAndre Oppermann 		}
7009b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
7019b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
7029b932e9eSAndre Oppermann 
7039b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
704f9e354dfSJulian Elischer 		goto done;
705f9e354dfSJulian Elischer 	}
7069b932e9eSAndre Oppermann 	/* Or forward to some other address? */
7079b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7089b932e9eSAndre Oppermann 	if (fwd_tag) {
709099dd043SAndre Oppermann #ifndef IPFIREWALL_FORWARD_EXTENDED
7109b932e9eSAndre Oppermann 		if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) {
711099dd043SAndre Oppermann #endif
7129b932e9eSAndre Oppermann 			dst = (struct sockaddr_in *)&ro->ro_dst;
7139b932e9eSAndre Oppermann 			bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
7149b932e9eSAndre Oppermann 			m->m_flags |= M_SKIP_FIREWALL;
7159b932e9eSAndre Oppermann 			m_tag_delete(m, fwd_tag);
7169b932e9eSAndre Oppermann 			goto again;
717099dd043SAndre Oppermann #ifndef IPFIREWALL_FORWARD_EXTENDED
7189b932e9eSAndre Oppermann 		} else {
7199b932e9eSAndre Oppermann 			m_tag_delete(m, fwd_tag);
7209b932e9eSAndre Oppermann 			/* Continue. */
721f9e354dfSJulian Elischer 		}
7229b932e9eSAndre Oppermann #endif
723099dd043SAndre Oppermann 	}
724099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */
7252b25acc1SLuigi Rizzo 
726c21fd232SAndre Oppermann passout:
72751c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
72851c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
72951c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
73051c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
73151c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
73251c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
73351c8ec4aSRuslan Ermilov 			goto bad;
73451c8ec4aSRuslan Ermilov 		}
73551c8ec4aSRuslan Ermilov 	}
73651c8ec4aSRuslan Ermilov 
737206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
738206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
739db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
740db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
741db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
742db4f9cc7SJonathan Lemon 	}
743206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
744db4f9cc7SJonathan Lemon 
745e7319babSPoul-Henning Kamp 	/*
746db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
747db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
748df8bae1dSRodney W. Grimes 	 */
7492683ceb6SAndre Oppermann 	if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
7502683ceb6SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0))) {
751fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
752fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
753df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
75453be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
755df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
7565da9f8faSJosef Karthauser 
7575da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
75838c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
7595da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
7605da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
7615da9f8faSJosef Karthauser 		}
7625da9f8faSJosef Karthauser 
76333841545SHajimu UMEMOTO #ifdef IPSEC
76433841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
76533841545SHajimu UMEMOTO 		ipsec_delaux(m);
76633841545SHajimu UMEMOTO #endif
76733841545SHajimu UMEMOTO 
76853dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
7693390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
7703390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
7719d9edc56SMike Silbersack #endif
772df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
773df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
774df8bae1dSRodney W. Grimes 		goto done;
775df8bae1dSRodney W. Grimes 	}
7761e78ac21SJeffrey Hsu 
777df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
778df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
7793d1f141bSGarrett Wollman 		/*
7803d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
7813d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
7823d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
7833d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
7843d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
7853d1f141bSGarrett Wollman 		 */
7861e78ac21SJeffrey Hsu 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
7871e78ac21SJeffrey Hsu 		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
7883d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
7893d1f141bSGarrett Wollman 		}
790df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
791df8bae1dSRodney W. Grimes 		goto bad;
792df8bae1dSRodney W. Grimes 	}
7931e78ac21SJeffrey Hsu 
7941e78ac21SJeffrey Hsu 	/*
7951e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
7961e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
7971e78ac21SJeffrey Hsu 	 */
7981e78ac21SJeffrey Hsu 	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
7991e78ac21SJeffrey Hsu 	if (error)
800df8bae1dSRodney W. Grimes 		goto bad;
8011e78ac21SJeffrey Hsu 	for (; m; m = m0) {
802df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
803b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
80433841545SHajimu UMEMOTO #ifdef IPSEC
80533841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
80633841545SHajimu UMEMOTO 		ipsec_delaux(m);
80733841545SHajimu UMEMOTO #endif
80807203494SDaniel C. Sobral 		if (error == 0) {
809fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
81007203494SDaniel C. Sobral 			if (ia != NULL) {
811fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
812fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
81307203494SDaniel C. Sobral 			}
814fe937674SJosef Karthauser 
815df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
816df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
817fe937674SJosef Karthauser 		} else
818df8bae1dSRodney W. Grimes 			m_freem(m);
819df8bae1dSRodney W. Grimes 	}
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes 	if (error == 0)
822df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
8231e78ac21SJeffrey Hsu 
824df8bae1dSRodney W. Grimes done:
8256a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
8266a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
827ac9d7e26SMax Laier 	}
82802c1c707SAndre Oppermann #ifdef IPSEC
8296a800098SYoshinobu Inoue 	if (sp != NULL) {
8306a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
8316a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
8326a800098SYoshinobu Inoue 		key_freesp(sp);
8336a800098SYoshinobu Inoue 	}
8341e78ac21SJeffrey Hsu #endif
835b9234fafSSam Leffler #ifdef FAST_IPSEC
836b9234fafSSam Leffler 	if (sp != NULL)
837b9234fafSSam Leffler 		KEY_FREESP(&sp);
8381e78ac21SJeffrey Hsu #endif
839df8bae1dSRodney W. Grimes 	return (error);
840df8bae1dSRodney W. Grimes bad:
8413528d68fSBill Paul 	m_freem(m);
842df8bae1dSRodney W. Grimes 	goto done;
843df8bae1dSRodney W. Grimes }
844df8bae1dSRodney W. Grimes 
8451e78ac21SJeffrey Hsu /*
8461e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
8471e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
8481e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
8491e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
8501e78ac21SJeffrey Hsu  *
8511e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
8521e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
8531e78ac21SJeffrey Hsu  */
8541e78ac21SJeffrey Hsu int
8551e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
8561e78ac21SJeffrey Hsu 	    u_long if_hwassist_flags, int sw_csum)
8571e78ac21SJeffrey Hsu {
8581e78ac21SJeffrey Hsu 	int error = 0;
8591e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
8601e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
8611e78ac21SJeffrey Hsu 	int off;
8621e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
8631e78ac21SJeffrey Hsu 	int firstlen;
8641e78ac21SJeffrey Hsu 	struct mbuf **mnext;
8651e78ac21SJeffrey Hsu 	int nfrags;
8661e78ac21SJeffrey Hsu 
8671e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
8681e78ac21SJeffrey Hsu 		ipstat.ips_cantfrag++;
8691e78ac21SJeffrey Hsu 		return EMSGSIZE;
8701e78ac21SJeffrey Hsu 	}
8711e78ac21SJeffrey Hsu 
8721e78ac21SJeffrey Hsu 	/*
8731e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
8741e78ac21SJeffrey Hsu 	 */
8751e78ac21SJeffrey Hsu 	if (len < 8)
8761e78ac21SJeffrey Hsu 		return EMSGSIZE;
8771e78ac21SJeffrey Hsu 
8781e78ac21SJeffrey Hsu 	/*
8791e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
8801e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
8811e78ac21SJeffrey Hsu 	 */
8821e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
8831e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
8841e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
8851e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
8861e78ac21SJeffrey Hsu 	}
8871e78ac21SJeffrey Hsu 
8881e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
8891e78ac21SJeffrey Hsu 		/*
8901e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
8911e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
8921e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
8931e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
8941e78ac21SJeffrey Hsu 		 *
8951e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
8961e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
8971e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
8981e78ac21SJeffrey Hsu 		 */
8991e78ac21SJeffrey Hsu 		int newlen;
9001e78ac21SJeffrey Hsu 		struct mbuf *m;
9011e78ac21SJeffrey Hsu 
9021e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
9031e78ac21SJeffrey Hsu 			off += m->m_len;
9041e78ac21SJeffrey Hsu 
9051e78ac21SJeffrey Hsu 		/*
9061e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
9071e78ac21SJeffrey Hsu 		 * 8-byte boundary
9081e78ac21SJeffrey Hsu 		 */
9091e78ac21SJeffrey Hsu 		if (off < hlen)
9101e78ac21SJeffrey Hsu 			goto smart_frag_failure;
9111e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
9121e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
9131e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
9141e78ac21SJeffrey Hsu 			/* we failed, go back the default */
9151e78ac21SJeffrey Hsu smart_frag_failure:
9161e78ac21SJeffrey Hsu 			newlen = len;
9171e78ac21SJeffrey Hsu 			off = hlen + len;
9181e78ac21SJeffrey Hsu 		}
9191e78ac21SJeffrey Hsu 		len = newlen;
9201e78ac21SJeffrey Hsu 
9211e78ac21SJeffrey Hsu 	} else {
9221e78ac21SJeffrey Hsu 		off = hlen + len;
9231e78ac21SJeffrey Hsu 	}
9241e78ac21SJeffrey Hsu 
9251e78ac21SJeffrey Hsu 	firstlen = off - hlen;
9261e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
9271e78ac21SJeffrey Hsu 
9281e78ac21SJeffrey Hsu 	/*
9291e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
9301e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
9311e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
9321e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
9331e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
9341e78ac21SJeffrey Hsu 	 */
9351e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
9361e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
9371e78ac21SJeffrey Hsu 		struct mbuf *m;
9381e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
9391e78ac21SJeffrey Hsu 
9401e78ac21SJeffrey Hsu 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
9410b17fba7SAndre Oppermann 		if (m == NULL) {
9421e78ac21SJeffrey Hsu 			error = ENOBUFS;
9431e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
9441e78ac21SJeffrey Hsu 			goto done;
9451e78ac21SJeffrey Hsu 		}
9461e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
9471e78ac21SJeffrey Hsu 		/*
9481e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
9491e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
9501e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
9511e78ac21SJeffrey Hsu 		 */
9521e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
9531e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
9541e78ac21SJeffrey Hsu 		*mhip = *ip;
9551e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
9561e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9571e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
9581e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
9591e78ac21SJeffrey Hsu 		}
9601e78ac21SJeffrey Hsu 		m->m_len = mhlen;
9611e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
9621e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
9631e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
9641e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
9651e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
9661e78ac21SJeffrey Hsu 		} else
9671e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
9681e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
9691e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
9700b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
9711e78ac21SJeffrey Hsu 			m_free(m);
9721e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
9731e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
9741e78ac21SJeffrey Hsu 			goto done;
9751e78ac21SJeffrey Hsu 		}
9761e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
9771e78ac21SJeffrey Hsu 		m->m_pkthdr.rcvif = (struct ifnet *)0;
9781e78ac21SJeffrey Hsu #ifdef MAC
9791e78ac21SJeffrey Hsu 		mac_create_fragment(m0, m);
9801e78ac21SJeffrey Hsu #endif
9811e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
9821e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
9831e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
9841e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
9851e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
9861e78ac21SJeffrey Hsu 		*mnext = m;
9871e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
9881e78ac21SJeffrey Hsu 	}
9891e78ac21SJeffrey Hsu 	ipstat.ips_ofragments += nfrags;
9901e78ac21SJeffrey Hsu 
9911e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
9921e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
9931e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
9941e78ac21SJeffrey Hsu 
9951e78ac21SJeffrey Hsu 	/*
9961e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
9971e78ac21SJeffrey Hsu 	 * and updating header.
9981e78ac21SJeffrey Hsu 	 */
9991e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
10001e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
10011e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
10021e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
10031e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
10041e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
10051e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
10061e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
10071e78ac21SJeffrey Hsu 
10081e78ac21SJeffrey Hsu done:
10091e78ac21SJeffrey Hsu 	*m_frag = m0;
10101e78ac21SJeffrey Hsu 	return error;
10111e78ac21SJeffrey Hsu }
10121e78ac21SJeffrey Hsu 
10131c238475SJonathan Lemon void
1014db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1015db4f9cc7SJonathan Lemon {
1016db4f9cc7SJonathan Lemon 	struct ip *ip;
1017db4f9cc7SJonathan Lemon 	u_short csum, offset;
1018db4f9cc7SJonathan Lemon 
1019db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
102053be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
1021db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1022206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1023206a3274SRuslan Ermilov 		csum = 0xffff;
1024db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1025db4f9cc7SJonathan Lemon 
1026db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1027db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1028db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1029db4f9cc7SJonathan Lemon 		/*
1030db4f9cc7SJonathan Lemon 		 * XXX
1031db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1032db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1033db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1034db4f9cc7SJonathan Lemon 		 */
1035db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1036db4f9cc7SJonathan Lemon 	}
1037db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1038db4f9cc7SJonathan Lemon }
1039db4f9cc7SJonathan Lemon 
1040df8bae1dSRodney W. Grimes /*
1041df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1042df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1043df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1044072b9b24SPaul Traina  *
1045072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1046df8bae1dSRodney W. Grimes  */
1047df8bae1dSRodney W. Grimes static struct mbuf *
1048b375c9ecSLuigi Rizzo ip_insertoptions(m, opt, phlen)
1049b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1050b375c9ecSLuigi Rizzo 	struct mbuf *opt;
1051b375c9ecSLuigi Rizzo 	int *phlen;
1052df8bae1dSRodney W. Grimes {
1053b375c9ecSLuigi Rizzo 	register struct ipoption *p = mtod(opt, struct ipoption *);
1054df8bae1dSRodney W. Grimes 	struct mbuf *n;
1055b375c9ecSLuigi Rizzo 	register struct ip *ip = mtod(m, struct ip *);
1056df8bae1dSRodney W. Grimes 	unsigned optlen;
1057df8bae1dSRodney W. Grimes 
1058df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
10591e78ac21SJeffrey Hsu 	if (optlen + ip->ip_len > IP_MAXPACKET) {
1060cb7641e8SMaxim Konovalov 		*phlen = 0;
1061df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1062cb7641e8SMaxim Konovalov 	}
1063df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1064df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1065df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1066a163d034SWarner Losh 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
10670b17fba7SAndre Oppermann 		if (n == NULL) {
1068cb7641e8SMaxim Konovalov 			*phlen = 0;
1069df8bae1dSRodney W. Grimes 			return (m);
1070cb7641e8SMaxim Konovalov 		}
10717258e968SAlan Cox 		M_MOVE_PKTHDR(n, m);
10725db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
10734ed84624SRobert Watson #ifdef MAC
10744ed84624SRobert Watson 		mac_create_mbuf_from_mbuf(m, n);
10754ed84624SRobert Watson #endif
10767258e968SAlan Cox 		n->m_pkthdr.len += optlen;
1077df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1078df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1079df8bae1dSRodney W. Grimes 		n->m_next = m;
1080df8bae1dSRodney W. Grimes 		m = n;
1081df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1082df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1083212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1084df8bae1dSRodney W. Grimes 	} else {
1085df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1086df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1087df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1088212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1089df8bae1dSRodney W. Grimes 	}
1090df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
10910453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1092df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
109353be11f6SPoul-Henning Kamp 	ip->ip_v = IPVERSION;
109453be11f6SPoul-Henning Kamp 	ip->ip_hl = *phlen >> 2;
1095df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1096df8bae1dSRodney W. Grimes 	return (m);
1097df8bae1dSRodney W. Grimes }
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes /*
1100df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1101df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1102df8bae1dSRodney W. Grimes  */
1103beec8214SDarren Reed int
1104b375c9ecSLuigi Rizzo ip_optcopy(ip, jp)
1105b375c9ecSLuigi Rizzo 	struct ip *ip, *jp;
1106df8bae1dSRodney W. Grimes {
1107b375c9ecSLuigi Rizzo 	register u_char *cp, *dp;
1108df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1111df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
111253be11f6SPoul-Henning Kamp 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1113df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1114df8bae1dSRodney W. Grimes 		opt = cp[0];
1115df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1116df8bae1dSRodney W. Grimes 			break;
1117df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1118df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1119df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1120df8bae1dSRodney W. Grimes 			optlen = 1;
1121df8bae1dSRodney W. Grimes 			continue;
1122686cdd19SJun-ichiro itojun Hagino 		}
1123db40007dSAndrew R. Reiter 
1124db40007dSAndrew R. Reiter 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1125db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1126df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1127db40007dSAndrew R. Reiter 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1128db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1129db40007dSAndrew R. Reiter 
1130df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1131df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1132df8bae1dSRodney W. Grimes 			optlen = cnt;
1133df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
11340453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1135df8bae1dSRodney W. Grimes 			dp += optlen;
1136df8bae1dSRodney W. Grimes 		}
1137df8bae1dSRodney W. Grimes 	}
1138df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1139df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1140df8bae1dSRodney W. Grimes 	return (optlen);
1141df8bae1dSRodney W. Grimes }
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes /*
1144df8bae1dSRodney W. Grimes  * IP socket option processing.
1145df8bae1dSRodney W. Grimes  */
1146df8bae1dSRodney W. Grimes int
1147b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt)
1148b375c9ecSLuigi Rizzo 	struct socket *so;
1149b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1150df8bae1dSRodney W. Grimes {
1151cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1152cfe8b629SGarrett Wollman 	int	error, optval;
1153df8bae1dSRodney W. Grimes 
1154cfe8b629SGarrett Wollman 	error = optval = 0;
1155cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1156cfe8b629SGarrett Wollman 		return (EINVAL);
1157cfe8b629SGarrett Wollman 	}
1158df8bae1dSRodney W. Grimes 
1159cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1160cfe8b629SGarrett Wollman 	case SOPT_SET:
1161cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1162df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1163df8bae1dSRodney W. Grimes #ifdef notyet
1164df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1165df8bae1dSRodney W. Grimes #endif
1166cfe8b629SGarrett Wollman 		{
1167cfe8b629SGarrett Wollman 			struct mbuf *m;
1168cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1169cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1170cfe8b629SGarrett Wollman 				break;
1171cfe8b629SGarrett Wollman 			}
1172a163d034SWarner Losh 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
11730b17fba7SAndre Oppermann 			if (m == NULL) {
1174cfe8b629SGarrett Wollman 				error = ENOBUFS;
1175cfe8b629SGarrett Wollman 				break;
1176cfe8b629SGarrett Wollman 			}
1177cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1178cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1179cfe8b629SGarrett Wollman 					    m->m_len);
1180993d9505SRobert Watson 			INP_LOCK(inp);
1181993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
1182993d9505SRobert Watson 			INP_UNLOCK(inp);
1183993d9505SRobert Watson 			return (error);
1184cfe8b629SGarrett Wollman 		}
1185df8bae1dSRodney W. Grimes 
1186df8bae1dSRodney W. Grimes 		case IP_TOS:
1187df8bae1dSRodney W. Grimes 		case IP_TTL:
1188df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1189df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1190df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
11914957466bSMatthew N. Dodd 		case IP_RECVTTL:
119282c23ebaSBill Fenner 		case IP_RECVIF:
11936a800098SYoshinobu Inoue 		case IP_FAITH:
11948afa2304SBruce M Simpson 		case IP_ONESBCAST:
1195cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1196cfe8b629SGarrett Wollman 					    sizeof optval);
1197cfe8b629SGarrett Wollman 			if (error)
1198cfe8b629SGarrett Wollman 				break;
1199df8bae1dSRodney W. Grimes 
1200cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1201df8bae1dSRodney W. Grimes 			case IP_TOS:
1202ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1203df8bae1dSRodney W. Grimes 				break;
1204df8bae1dSRodney W. Grimes 
1205df8bae1dSRodney W. Grimes 			case IP_TTL:
1206ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1207df8bae1dSRodney W. Grimes 				break;
1208a138d217SRobert Watson #define	OPTSET(bit) do {						\
1209a138d217SRobert Watson 	INP_LOCK(inp);							\
1210df8bae1dSRodney W. Grimes 	if (optval)							\
1211df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1212df8bae1dSRodney W. Grimes 	else								\
1213a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
1214a138d217SRobert Watson 	INP_UNLOCK(inp);						\
1215a138d217SRobert Watson } while (0)
1216df8bae1dSRodney W. Grimes 
1217df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1218df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1219df8bae1dSRodney W. Grimes 				break;
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1222df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1223df8bae1dSRodney W. Grimes 				break;
1224df8bae1dSRodney W. Grimes 
1225df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1226df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1227df8bae1dSRodney W. Grimes 				break;
122882c23ebaSBill Fenner 
12294957466bSMatthew N. Dodd 			case IP_RECVTTL:
12304957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
12314957466bSMatthew N. Dodd 				break;
12324957466bSMatthew N. Dodd 
123382c23ebaSBill Fenner 			case IP_RECVIF:
123482c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
123582c23ebaSBill Fenner 				break;
12366a800098SYoshinobu Inoue 
12376a800098SYoshinobu Inoue 			case IP_FAITH:
12386a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
12396a800098SYoshinobu Inoue 				break;
12408afa2304SBruce M Simpson 
12418afa2304SBruce M Simpson 			case IP_ONESBCAST:
12428afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
12438afa2304SBruce M Simpson 				break;
1244df8bae1dSRodney W. Grimes 			}
1245df8bae1dSRodney W. Grimes 			break;
1246df8bae1dSRodney W. Grimes #undef OPTSET
1247df8bae1dSRodney W. Grimes 
1248df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1249f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1250df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1251df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1252df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1253df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
12545c918b56SRobert Watson 			error = ip_setmoptions(inp, sopt);
1255df8bae1dSRodney W. Grimes 			break;
1256df8bae1dSRodney W. Grimes 
125733b3ac06SPeter Wemm 		case IP_PORTRANGE:
1258cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1259cfe8b629SGarrett Wollman 					    sizeof optval);
1260cfe8b629SGarrett Wollman 			if (error)
1261cfe8b629SGarrett Wollman 				break;
126233b3ac06SPeter Wemm 
1263a138d217SRobert Watson 			INP_LOCK(inp);
126433b3ac06SPeter Wemm 			switch (optval) {
126533b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
126633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
126733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
126833b3ac06SPeter Wemm 				break;
126933b3ac06SPeter Wemm 
127033b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
127133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
127233b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
127333b3ac06SPeter Wemm 				break;
127433b3ac06SPeter Wemm 
127533b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
127633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
127733b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
127833b3ac06SPeter Wemm 				break;
127933b3ac06SPeter Wemm 
128033b3ac06SPeter Wemm 			default:
128133b3ac06SPeter Wemm 				error = EINVAL;
128233b3ac06SPeter Wemm 				break;
128333b3ac06SPeter Wemm 			}
1284a138d217SRobert Watson 			INP_UNLOCK(inp);
1285ce8c72b1SPeter Wemm 			break;
128633b3ac06SPeter Wemm 
1287b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
12886a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12896a800098SYoshinobu Inoue 		{
12906a800098SYoshinobu Inoue 			caddr_t req;
1291686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
12926a800098SYoshinobu Inoue 			int priv;
12936a800098SYoshinobu Inoue 			struct mbuf *m;
12946a800098SYoshinobu Inoue 			int optname;
12956a800098SYoshinobu Inoue 
12966a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
12976a800098SYoshinobu Inoue 				break;
12986a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12996a800098SYoshinobu Inoue 				break;
1300b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
130144731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
13026a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1303686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
13046a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1305686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
13066a800098SYoshinobu Inoue 			m_freem(m);
13076a800098SYoshinobu Inoue 			break;
13086a800098SYoshinobu Inoue 		}
13096a800098SYoshinobu Inoue #endif /*IPSEC*/
13106a800098SYoshinobu Inoue 
1311df8bae1dSRodney W. Grimes 		default:
1312df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1313df8bae1dSRodney W. Grimes 			break;
1314df8bae1dSRodney W. Grimes 		}
1315df8bae1dSRodney W. Grimes 		break;
1316df8bae1dSRodney W. Grimes 
1317cfe8b629SGarrett Wollman 	case SOPT_GET:
1318cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1319df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1320df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1321cfe8b629SGarrett Wollman 			if (inp->inp_options)
1322cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1323cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1324cfe8b629SGarrett Wollman 							  char *),
1325cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1326cfe8b629SGarrett Wollman 			else
1327cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1328df8bae1dSRodney W. Grimes 			break;
1329df8bae1dSRodney W. Grimes 
1330df8bae1dSRodney W. Grimes 		case IP_TOS:
1331df8bae1dSRodney W. Grimes 		case IP_TTL:
1332df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1333df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1334df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
13354957466bSMatthew N. Dodd 		case IP_RECVTTL:
133682c23ebaSBill Fenner 		case IP_RECVIF:
1337cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
13386a800098SYoshinobu Inoue 		case IP_FAITH:
13398afa2304SBruce M Simpson 		case IP_ONESBCAST:
1340cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1341df8bae1dSRodney W. Grimes 
1342df8bae1dSRodney W. Grimes 			case IP_TOS:
1343ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1344df8bae1dSRodney W. Grimes 				break;
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes 			case IP_TTL:
1347ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1348df8bae1dSRodney W. Grimes 				break;
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1351df8bae1dSRodney W. Grimes 
1352df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1353df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1354df8bae1dSRodney W. Grimes 				break;
1355df8bae1dSRodney W. Grimes 
1356df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1357df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1358df8bae1dSRodney W. Grimes 				break;
1359df8bae1dSRodney W. Grimes 
1360df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1361df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1362df8bae1dSRodney W. Grimes 				break;
136382c23ebaSBill Fenner 
13644957466bSMatthew N. Dodd 			case IP_RECVTTL:
13654957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
13664957466bSMatthew N. Dodd 				break;
13674957466bSMatthew N. Dodd 
136882c23ebaSBill Fenner 			case IP_RECVIF:
136982c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
137082c23ebaSBill Fenner 				break;
1371cfe8b629SGarrett Wollman 
1372cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1373cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1374cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1375cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1376cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1377cfe8b629SGarrett Wollman 				else
1378cfe8b629SGarrett Wollman 					optval = 0;
1379cfe8b629SGarrett Wollman 				break;
13806a800098SYoshinobu Inoue 
13816a800098SYoshinobu Inoue 			case IP_FAITH:
13826a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
13836a800098SYoshinobu Inoue 				break;
13848afa2304SBruce M Simpson 
13858afa2304SBruce M Simpson 			case IP_ONESBCAST:
13868afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
13878afa2304SBruce M Simpson 				break;
1388df8bae1dSRodney W. Grimes 			}
1389cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1390df8bae1dSRodney W. Grimes 			break;
1391df8bae1dSRodney W. Grimes 
1392df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1393f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1394df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1395df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1396df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1397df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
139889924e58SRobert Watson 			error = ip_getmoptions(inp, sopt);
139933b3ac06SPeter Wemm 			break;
140033b3ac06SPeter Wemm 
1401b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
14026a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
14036a800098SYoshinobu Inoue 		{
1404f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
14056a800098SYoshinobu Inoue 			caddr_t req = NULL;
1406686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
14076a800098SYoshinobu Inoue 
1408686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
14096a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1410686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1411686cdd19SJun-ichiro itojun Hagino 			}
1412686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
14136a800098SYoshinobu Inoue 			if (error == 0)
14146a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1415f63e7634SYoshinobu Inoue 			if (error == 0)
14166a800098SYoshinobu Inoue 				m_freem(m);
14176a800098SYoshinobu Inoue 			break;
14186a800098SYoshinobu Inoue 		}
14196a800098SYoshinobu Inoue #endif /*IPSEC*/
14206a800098SYoshinobu Inoue 
1421df8bae1dSRodney W. Grimes 		default:
1422df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1423df8bae1dSRodney W. Grimes 			break;
1424df8bae1dSRodney W. Grimes 		}
1425df8bae1dSRodney W. Grimes 		break;
1426df8bae1dSRodney W. Grimes 	}
1427df8bae1dSRodney W. Grimes 	return (error);
1428df8bae1dSRodney W. Grimes }
1429df8bae1dSRodney W. Grimes 
1430df8bae1dSRodney W. Grimes /*
1431df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1432df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1433df8bae1dSRodney W. Grimes  * with destination address if source routed.
1434df8bae1dSRodney W. Grimes  */
14350312fbe9SPoul-Henning Kamp static int
1436993d9505SRobert Watson ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
1437df8bae1dSRodney W. Grimes {
1438b375c9ecSLuigi Rizzo 	register int cnt, optlen;
1439b375c9ecSLuigi Rizzo 	register u_char *cp;
1440993d9505SRobert Watson 	struct mbuf **pcbopt;
1441df8bae1dSRodney W. Grimes 	u_char opt;
1442df8bae1dSRodney W. Grimes 
1443993d9505SRobert Watson 	INP_LOCK_ASSERT(inp);
1444993d9505SRobert Watson 
1445993d9505SRobert Watson 	pcbopt = &inp->inp_options;
1446993d9505SRobert Watson 
1447df8bae1dSRodney W. Grimes 	/* turn off any old options */
1448df8bae1dSRodney W. Grimes 	if (*pcbopt)
1449df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1450df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1451993d9505SRobert Watson 	if (m == NULL || m->m_len == 0) {
1452df8bae1dSRodney W. Grimes 		/*
1453df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1454df8bae1dSRodney W. Grimes 		 */
1455993d9505SRobert Watson 		if (m != NULL)
1456df8bae1dSRodney W. Grimes 			(void)m_free(m);
1457df8bae1dSRodney W. Grimes 		return (0);
1458df8bae1dSRodney W. Grimes 	}
1459df8bae1dSRodney W. Grimes 
14600c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1461df8bae1dSRodney W. Grimes 		goto bad;
1462df8bae1dSRodney W. Grimes 	/*
1463df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1464df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1465df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1466df8bae1dSRodney W. Grimes 	 */
1467df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1468df8bae1dSRodney W. Grimes 		goto bad;
1469df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1470df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1471df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1472212059bdSDag-Erling Smørgrav 	bcopy(mtod(m, void *), cp, (unsigned)cnt);
1473212059bdSDag-Erling Smørgrav 	bzero(mtod(m, void *), sizeof(struct in_addr));
1474df8bae1dSRodney W. Grimes 
1475df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1476df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1477df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1478df8bae1dSRodney W. Grimes 			break;
1479df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1480df8bae1dSRodney W. Grimes 			optlen = 1;
1481df8bae1dSRodney W. Grimes 		else {
1482707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1483707d00a3SJonathan Lemon 				goto bad;
1484df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1485707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1486df8bae1dSRodney W. Grimes 				goto bad;
1487df8bae1dSRodney W. Grimes 		}
1488df8bae1dSRodney W. Grimes 		switch (opt) {
1489df8bae1dSRodney W. Grimes 
1490df8bae1dSRodney W. Grimes 		default:
1491df8bae1dSRodney W. Grimes 			break;
1492df8bae1dSRodney W. Grimes 
1493df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1494df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1495df8bae1dSRodney W. Grimes 			/*
1496df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1497df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1498df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1499df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1500df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1501df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1502df8bae1dSRodney W. Grimes 			 */
1503df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1504df8bae1dSRodney W. Grimes 				goto bad;
1505df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1506df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1507df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1508df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1509df8bae1dSRodney W. Grimes 			/*
1510df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1511df8bae1dSRodney W. Grimes 			 */
1512df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1513df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1514df8bae1dSRodney W. Grimes 			/*
1515df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1516df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1517df8bae1dSRodney W. Grimes 			 */
1518212059bdSDag-Erling Smørgrav 			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1519212059bdSDag-Erling Smørgrav 			    &cp[IPOPT_OFFSET+1],
1520a49b2137SMaxim Konovalov 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
1521df8bae1dSRodney W. Grimes 			break;
1522df8bae1dSRodney W. Grimes 		}
1523df8bae1dSRodney W. Grimes 	}
1524df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1525df8bae1dSRodney W. Grimes 		goto bad;
1526df8bae1dSRodney W. Grimes 	*pcbopt = m;
1527df8bae1dSRodney W. Grimes 	return (0);
1528df8bae1dSRodney W. Grimes 
1529df8bae1dSRodney W. Grimes bad:
1530df8bae1dSRodney W. Grimes 	(void)m_free(m);
1531df8bae1dSRodney W. Grimes 	return (EINVAL);
1532df8bae1dSRodney W. Grimes }
1533df8bae1dSRodney W. Grimes 
1534df8bae1dSRodney W. Grimes /*
1535cfe8b629SGarrett Wollman  * XXX
1536cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1537cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1538cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1539cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1540cfe8b629SGarrett Wollman  */
154133841545SHajimu UMEMOTO 
154233841545SHajimu UMEMOTO /*
154333841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
154433841545SHajimu UMEMOTO  */
154533841545SHajimu UMEMOTO static struct ifnet *
1546b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp)
1547b375c9ecSLuigi Rizzo 	struct in_addr *a;
1548b375c9ecSLuigi Rizzo 	int *ifindexp;
154933841545SHajimu UMEMOTO {
155033841545SHajimu UMEMOTO 	int ifindex;
155133841545SHajimu UMEMOTO 	struct ifnet *ifp;
155233841545SHajimu UMEMOTO 
155333841545SHajimu UMEMOTO 	if (ifindexp)
155433841545SHajimu UMEMOTO 		*ifindexp = 0;
155533841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
155633841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
155733841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
155833841545SHajimu UMEMOTO 			return NULL;
1559f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
156033841545SHajimu UMEMOTO 		if (ifindexp)
156133841545SHajimu UMEMOTO 			*ifindexp = ifindex;
156233841545SHajimu UMEMOTO 	} else {
156333841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
156433841545SHajimu UMEMOTO 	}
156533841545SHajimu UMEMOTO 	return ifp;
156633841545SHajimu UMEMOTO }
156733841545SHajimu UMEMOTO 
1568cfe8b629SGarrett Wollman /*
1569df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1570df8bae1dSRodney W. Grimes  */
15710312fbe9SPoul-Henning Kamp static int
15725c918b56SRobert Watson ip_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1573df8bae1dSRodney W. Grimes {
1574cfe8b629SGarrett Wollman 	int error = 0;
1575cfe8b629SGarrett Wollman 	int i;
1576df8bae1dSRodney W. Grimes 	struct in_addr addr;
1577cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1578cfe8b629SGarrett Wollman 	struct ifnet *ifp;
15795c918b56SRobert Watson 	struct ip_moptions *imo;
1580df8bae1dSRodney W. Grimes 	struct route ro;
1581cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
158233841545SHajimu UMEMOTO 	int ifindex;
15839b626c29SGarrett Wollman 	int s;
1584df8bae1dSRodney W. Grimes 
15855c918b56SRobert Watson 	imo = inp->inp_moptions;
1586df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1587df8bae1dSRodney W. Grimes 		/*
1588df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1589df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1590df8bae1dSRodney W. Grimes 		 */
1591df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1592a163d034SWarner Losh 		    M_WAITOK);
1593df8bae1dSRodney W. Grimes 
1594df8bae1dSRodney W. Grimes 		if (imo == NULL)
1595df8bae1dSRodney W. Grimes 			return (ENOBUFS);
15965c918b56SRobert Watson 		inp->inp_moptions = imo;
1597df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
159833841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
15991c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1600df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1601df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1602df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1603df8bae1dSRodney W. Grimes 	}
1604df8bae1dSRodney W. Grimes 
1605cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1606f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1607f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1608cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
16095e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
16105e9ae478SGarrett Wollman 			break;
16115e9ae478SGarrett Wollman 		}
1612cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1613cfe8b629SGarrett Wollman 		if (error)
1614f0068c4aSGarrett Wollman 			break;
16151c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1616f0068c4aSGarrett Wollman 			error = EINVAL;
1617f0068c4aSGarrett Wollman 			break;
1618f0068c4aSGarrett Wollman 		}
1619f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1620f0068c4aSGarrett Wollman 		break;
1621f0068c4aSGarrett Wollman 
1622df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1623df8bae1dSRodney W. Grimes 		/*
1624df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1625df8bae1dSRodney W. Grimes 		 */
1626cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1627cfe8b629SGarrett Wollman 		if (error)
1628df8bae1dSRodney W. Grimes 			break;
1629df8bae1dSRodney W. Grimes 		/*
1630df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1631df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1632df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1633df8bae1dSRodney W. Grimes 		 */
1634df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1635df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1636df8bae1dSRodney W. Grimes 			break;
1637df8bae1dSRodney W. Grimes 		}
1638df8bae1dSRodney W. Grimes 		/*
1639df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1640df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1641df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1642df8bae1dSRodney W. Grimes 		 */
164320e8807cSGarrett Wollman 		s = splimp();
164433841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1645df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1646fbc6ab00SBill Fenner 			splx(s);
1647df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1648df8bae1dSRodney W. Grimes 			break;
1649df8bae1dSRodney W. Grimes 		}
1650df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
165133841545SHajimu UMEMOTO 		if (ifindex)
165233841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
165333841545SHajimu UMEMOTO 		else
165433841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
16559b626c29SGarrett Wollman 		splx(s);
1656df8bae1dSRodney W. Grimes 		break;
1657df8bae1dSRodney W. Grimes 
1658df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1659df8bae1dSRodney W. Grimes 		/*
1660df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1661cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1662cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1663cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1664df8bae1dSRodney W. Grimes 		 */
1665cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1666cfe8b629SGarrett Wollman 			u_char ttl;
1667cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1668cfe8b629SGarrett Wollman 			if (error)
1669df8bae1dSRodney W. Grimes 				break;
1670cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1671cfe8b629SGarrett Wollman 		} else {
1672cfe8b629SGarrett Wollman 			u_int ttl;
1673cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1674cfe8b629SGarrett Wollman 					    sizeof ttl);
1675cfe8b629SGarrett Wollman 			if (error)
1676cfe8b629SGarrett Wollman 				break;
1677cfe8b629SGarrett Wollman 			if (ttl > 255)
1678cfe8b629SGarrett Wollman 				error = EINVAL;
1679cfe8b629SGarrett Wollman 			else
1680cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1681df8bae1dSRodney W. Grimes 		}
1682df8bae1dSRodney W. Grimes 		break;
1683df8bae1dSRodney W. Grimes 
1684df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1685df8bae1dSRodney W. Grimes 		/*
1686df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1687cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1688cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1689cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1690df8bae1dSRodney W. Grimes 		 */
1691cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1692cfe8b629SGarrett Wollman 			u_char loop;
1693cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1694cfe8b629SGarrett Wollman 			if (error)
1695df8bae1dSRodney W. Grimes 				break;
1696cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1697cfe8b629SGarrett Wollman 		} else {
1698cfe8b629SGarrett Wollman 			u_int loop;
1699cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1700cfe8b629SGarrett Wollman 					    sizeof loop);
1701cfe8b629SGarrett Wollman 			if (error)
1702cfe8b629SGarrett Wollman 				break;
1703cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1704df8bae1dSRodney W. Grimes 		}
1705df8bae1dSRodney W. Grimes 		break;
1706df8bae1dSRodney W. Grimes 
1707df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1708df8bae1dSRodney W. Grimes 		/*
1709df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1710df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1711df8bae1dSRodney W. Grimes 		 */
1712cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1713cfe8b629SGarrett Wollman 		if (error)
1714df8bae1dSRodney W. Grimes 			break;
1715cfe8b629SGarrett Wollman 
1716cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1717df8bae1dSRodney W. Grimes 			error = EINVAL;
1718df8bae1dSRodney W. Grimes 			break;
1719df8bae1dSRodney W. Grimes 		}
172020e8807cSGarrett Wollman 		s = splimp();
1721df8bae1dSRodney W. Grimes 		/*
1722df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1723df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1724df8bae1dSRodney W. Grimes 		 */
1725cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
17261c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1727df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1728df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1729df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1730cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
173197d8d152SAndre Oppermann 			rtalloc_ign(&ro, RTF_CLONING);
1732df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1733df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
17349b626c29SGarrett Wollman 				splx(s);
1735df8bae1dSRodney W. Grimes 				break;
1736df8bae1dSRodney W. Grimes 			}
1737df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1738d1dd20beSSam Leffler 			RTFREE(ro.ro_rt);
1739df8bae1dSRodney W. Grimes 		}
1740df8bae1dSRodney W. Grimes 		else {
174133841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1742df8bae1dSRodney W. Grimes 		}
17439b626c29SGarrett Wollman 
1744df8bae1dSRodney W. Grimes 		/*
1745df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1746df8bae1dSRodney W. Grimes 		 * supports multicast.
1747df8bae1dSRodney W. Grimes 		 */
1748df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1749df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
17509b626c29SGarrett Wollman 			splx(s);
1751df8bae1dSRodney W. Grimes 			break;
1752df8bae1dSRodney W. Grimes 		}
1753df8bae1dSRodney W. Grimes 		/*
1754df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1755df8bae1dSRodney W. Grimes 		 * membership slots are full.
1756df8bae1dSRodney W. Grimes 		 */
1757df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1758df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1759df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1760cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1761df8bae1dSRodney W. Grimes 				break;
1762df8bae1dSRodney W. Grimes 		}
1763df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1764df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
17659b626c29SGarrett Wollman 			splx(s);
1766df8bae1dSRodney W. Grimes 			break;
1767df8bae1dSRodney W. Grimes 		}
1768df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1769df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
17709b626c29SGarrett Wollman 			splx(s);
1771df8bae1dSRodney W. Grimes 			break;
1772df8bae1dSRodney W. Grimes 		}
1773df8bae1dSRodney W. Grimes 		/*
1774df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1775df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1776df8bae1dSRodney W. Grimes 		 */
1777df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1778cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1779df8bae1dSRodney W. Grimes 			error = ENOBUFS;
17809b626c29SGarrett Wollman 			splx(s);
1781df8bae1dSRodney W. Grimes 			break;
1782df8bae1dSRodney W. Grimes 		}
1783df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
17849b626c29SGarrett Wollman 		splx(s);
1785df8bae1dSRodney W. Grimes 		break;
1786df8bae1dSRodney W. Grimes 
1787df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1788df8bae1dSRodney W. Grimes 		/*
1789df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1790df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1791df8bae1dSRodney W. Grimes 		 */
1792cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1793cfe8b629SGarrett Wollman 		if (error)
1794df8bae1dSRodney W. Grimes 			break;
1795cfe8b629SGarrett Wollman 
1796cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1797df8bae1dSRodney W. Grimes 			error = EINVAL;
1798df8bae1dSRodney W. Grimes 			break;
1799df8bae1dSRodney W. Grimes 		}
18009b626c29SGarrett Wollman 
180120e8807cSGarrett Wollman 		s = splimp();
1802df8bae1dSRodney W. Grimes 		/*
1803df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1804df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1805df8bae1dSRodney W. Grimes 		 */
1806cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1807df8bae1dSRodney W. Grimes 			ifp = NULL;
1808df8bae1dSRodney W. Grimes 		else {
180933841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1810df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1811df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
18129b626c29SGarrett Wollman 				splx(s);
1813df8bae1dSRodney W. Grimes 				break;
1814df8bae1dSRodney W. Grimes 			}
1815df8bae1dSRodney W. Grimes 		}
1816df8bae1dSRodney W. Grimes 		/*
1817df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1818df8bae1dSRodney W. Grimes 		 */
1819df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1820df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1821df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1822df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1823cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1824df8bae1dSRodney W. Grimes 				break;
1825df8bae1dSRodney W. Grimes 		}
1826df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1827df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
18289b626c29SGarrett Wollman 			splx(s);
1829df8bae1dSRodney W. Grimes 			break;
1830df8bae1dSRodney W. Grimes 		}
1831df8bae1dSRodney W. Grimes 		/*
1832df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1833df8bae1dSRodney W. Grimes 		 * membership points.
1834df8bae1dSRodney W. Grimes 		 */
1835df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1836df8bae1dSRodney W. Grimes 		/*
1837df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1838df8bae1dSRodney W. Grimes 		 */
1839df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1840df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1841df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
18429b626c29SGarrett Wollman 		splx(s);
1843df8bae1dSRodney W. Grimes 		break;
1844df8bae1dSRodney W. Grimes 
1845df8bae1dSRodney W. Grimes 	default:
1846df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1847df8bae1dSRodney W. Grimes 		break;
1848df8bae1dSRodney W. Grimes 	}
1849df8bae1dSRodney W. Grimes 
1850df8bae1dSRodney W. Grimes 	/*
1851df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1852df8bae1dSRodney W. Grimes 	 */
1853df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
18541c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1855df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1856df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1857df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
18585c918b56SRobert Watson 		free(inp->inp_moptions, M_IPMOPTS);
18595c918b56SRobert Watson 		inp->inp_moptions = NULL;
1860df8bae1dSRodney W. Grimes 	}
1861df8bae1dSRodney W. Grimes 
1862df8bae1dSRodney W. Grimes 	return (error);
1863df8bae1dSRodney W. Grimes }
1864df8bae1dSRodney W. Grimes 
1865df8bae1dSRodney W. Grimes /*
1866df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1867df8bae1dSRodney W. Grimes  */
18680312fbe9SPoul-Henning Kamp static int
186989924e58SRobert Watson ip_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1870df8bae1dSRodney W. Grimes {
187189924e58SRobert Watson 	struct ip_moptions *imo;
1872cfe8b629SGarrett Wollman 	struct in_addr addr;
1873df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1874cfe8b629SGarrett Wollman 	int error, optval;
1875cfe8b629SGarrett Wollman 	u_char coptval;
1876df8bae1dSRodney W. Grimes 
187789924e58SRobert Watson 	INP_LOCK(inp);
187889924e58SRobert Watson 	imo = inp->inp_moptions;
187989924e58SRobert Watson 
1880cfe8b629SGarrett Wollman 	error = 0;
1881cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1882f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1883f0068c4aSGarrett Wollman 		if (imo != NULL)
1884cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1885f0068c4aSGarrett Wollman 		else
1886cfe8b629SGarrett Wollman 			optval = -1;
188789924e58SRobert Watson 		INP_UNLOCK(inp);
1888cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1889cfe8b629SGarrett Wollman 		break;
1890f0068c4aSGarrett Wollman 
1891df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1892df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1893cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
189433841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
189533841545SHajimu UMEMOTO 			/* return the value user has set */
189633841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
189733841545SHajimu UMEMOTO 		} else {
1898df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1899cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1900df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1901df8bae1dSRodney W. Grimes 		}
190289924e58SRobert Watson 		INP_UNLOCK(inp);
1903cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1904cfe8b629SGarrett Wollman 		break;
1905df8bae1dSRodney W. Grimes 
1906df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1907cfe8b629SGarrett Wollman 		if (imo == 0)
1908cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1909cfe8b629SGarrett Wollman 		else
1910cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
191189924e58SRobert Watson 		INP_UNLOCK(inp);
1912cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1913cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1914cfe8b629SGarrett Wollman 		else
1915cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1916cfe8b629SGarrett Wollman 		break;
1917df8bae1dSRodney W. Grimes 
1918df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1919cfe8b629SGarrett Wollman 		if (imo == 0)
1920cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1921cfe8b629SGarrett Wollman 		else
1922cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
192389924e58SRobert Watson 		INP_UNLOCK(inp);
1924cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1925cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1926cfe8b629SGarrett Wollman 		else
1927cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1928cfe8b629SGarrett Wollman 		break;
1929df8bae1dSRodney W. Grimes 
1930df8bae1dSRodney W. Grimes 	default:
193189924e58SRobert Watson 		INP_UNLOCK(inp);
1932cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1933cfe8b629SGarrett Wollman 		break;
1934df8bae1dSRodney W. Grimes 	}
193589924e58SRobert Watson 	INP_UNLOCK_ASSERT(inp);
193689924e58SRobert Watson 
1937cfe8b629SGarrett Wollman 	return (error);
1938df8bae1dSRodney W. Grimes }
1939df8bae1dSRodney W. Grimes 
1940df8bae1dSRodney W. Grimes /*
1941df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1942df8bae1dSRodney W. Grimes  */
1943df8bae1dSRodney W. Grimes void
1944b375c9ecSLuigi Rizzo ip_freemoptions(imo)
1945b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
1946df8bae1dSRodney W. Grimes {
1947b375c9ecSLuigi Rizzo 	register int i;
1948df8bae1dSRodney W. Grimes 
1949df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1950df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1951df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1952df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1953df8bae1dSRodney W. Grimes 	}
1954df8bae1dSRodney W. Grimes }
1955df8bae1dSRodney W. Grimes 
1956df8bae1dSRodney W. Grimes /*
1957df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1958df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1959df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1960f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1961f5fea3ddSPaul Traina  * replicating that code here.
1962df8bae1dSRodney W. Grimes  */
1963df8bae1dSRodney W. Grimes static void
1964b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen)
1965b375c9ecSLuigi Rizzo 	struct ifnet *ifp;
1966b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1967b375c9ecSLuigi Rizzo 	register struct sockaddr_in *dst;
1968b375c9ecSLuigi Rizzo 	int hlen;
1969df8bae1dSRodney W. Grimes {
1970b375c9ecSLuigi Rizzo 	register struct ip *ip;
1971df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1972df8bae1dSRodney W. Grimes 
1973b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
197486b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
197586b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1976df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1977390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1978390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1979390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1980390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1981390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1982390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1983390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1984390cdc6aSRuslan Ermilov 		}
1985df8bae1dSRodney W. Grimes 		/*
1986df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1987df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1988df8bae1dSRodney W. Grimes 		 */
1989df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1990fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1991fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1992df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
199386b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
19949c9137eaSGarrett Wollman 		/*
19959c9137eaSGarrett Wollman 		 * NB:
1996e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1997e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1998e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1999e1596dffSBill Fenner 		 * particular, everything which modifies the packet
2000e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
2001e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
2002e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
2003ed7509acSJulian Elischer 		 * of safety by using if_simloop().
20049c9137eaSGarrett Wollman 		 */
2005201c2527SJulian Elischer #if 1 /* XXX */
2006201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
20072b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
2008201c2527SJulian Elischer 						dst->sin_family);
2009201c2527SJulian Elischer 			dst->sin_family = AF_INET;
2010201c2527SJulian Elischer 		}
2011201c2527SJulian Elischer #endif
2012201c2527SJulian Elischer 
20139c9137eaSGarrett Wollman #ifdef notdef
2014e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
2015ed7509acSJulian Elischer 		ip_input(copym);
20169c9137eaSGarrett Wollman #else
201706a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
20189c9137eaSGarrett Wollman #endif
2019df8bae1dSRodney W. Grimes 	}
2020df8bae1dSRodney W. Grimes }
2021