xref: /freebsd/sys/netinet/ip_output.c (revision cb459254a29d679b7fbef7a60afb6b64135688a9)
1df8bae1dSRodney W. Grimes /*
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 
61134ea224SSam Leffler 
629c9137eaSGarrett Wollman #include <machine/in_cksum.h>
63df8bae1dSRodney W. Grimes 
64a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
6555166637SPoul-Henning Kamp 
666a800098SYoshinobu Inoue #ifdef IPSEC
676a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
686a800098SYoshinobu Inoue #include <netkey/key.h>
696a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG
706a800098SYoshinobu Inoue #include <netkey/key_debug.h>
716a800098SYoshinobu Inoue #else
726a800098SYoshinobu Inoue #define	KEYDEBUG(lev,arg)
736a800098SYoshinobu Inoue #endif
746a800098SYoshinobu Inoue #endif /*IPSEC*/
756a800098SYoshinobu Inoue 
76b9234fafSSam Leffler #ifdef FAST_IPSEC
77b9234fafSSam Leffler #include <netipsec/ipsec.h>
78b9234fafSSam Leffler #include <netipsec/xform.h>
79b9234fafSSam Leffler #include <netipsec/key.h>
80b9234fafSSam Leffler #endif /*FAST_IPSEC*/
81b9234fafSSam Leffler 
822b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
832b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
84f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
85f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
862b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
87f9e354dfSJulian Elischer 
88f23b4c91SGarrett Wollman u_short ip_id;
89f23b4c91SGarrett Wollman 
9053dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
919d9edc56SMike Silbersack int mbuf_frag_size = 0;
929d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
939d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
949d9edc56SMike Silbersack #endif
959d9edc56SMike Silbersack 
964d77a549SAlfred Perlstein static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
974d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *);
98df8bae1dSRodney W. Grimes static void	ip_mloopback
994d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
1000312fbe9SPoul-Henning Kamp static int	ip_getmoptions
1014d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions *);
1024d77a549SAlfred Perlstein static int	ip_pcbopts(int, struct mbuf **, struct mbuf *);
1030312fbe9SPoul-Henning Kamp static int	ip_setmoptions
1044d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions **);
105df8bae1dSRodney W. Grimes 
1064d77a549SAlfred Perlstein int	ip_optcopy(struct ip *, struct ip *);
107afed1b49SDarren Reed 
108afed1b49SDarren Reed 
10993e0e116SJulian Elischer extern	struct protosw inetsw[];
11093e0e116SJulian Elischer 
111df8bae1dSRodney W. Grimes /*
112df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
113df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
114df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
115df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1163de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1173de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
118df8bae1dSRodney W. Grimes  */
119df8bae1dSRodney W. Grimes int
120ac9d7e26SMax Laier ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
1211e78ac21SJeffrey Hsu 	int flags, struct ip_moptions *imo, struct inpcb *inp)
122df8bae1dSRodney W. Grimes {
1231e78ac21SJeffrey Hsu 	struct ip *ip;
1242b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
125ac9d7e26SMax Laier 	struct mbuf *m0;
12623bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1279b932e9eSAndre Oppermann 	int len, error = 0;
1282b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1293956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
130db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
131e3f406b3SRuslan Ermilov 	struct route iproute;
1329b932e9eSAndre Oppermann 	struct in_addr odst;
1339b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1349b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
1359b932e9eSAndre Oppermann #endif
13602c1c707SAndre Oppermann #ifdef IPSEC
1376a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1386a800098SYoshinobu Inoue #endif
139b9234fafSSam Leffler #ifdef FAST_IPSEC
140b9234fafSSam Leffler 	struct secpolicy *sp = NULL;
141b9234fafSSam Leffler 	struct tdb_ident *tdbi;
1429b932e9eSAndre Oppermann 	struct m_tag *mtag;
143b9234fafSSam Leffler 	int s;
144b9234fafSSam Leffler #endif /* FAST_IPSEC */
14536e8826fSMax Laier 
146ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
14736e8826fSMax Laier 
14802c1c707SAndre Oppermann 	if (ro == NULL) {
14902c1c707SAndre Oppermann 		ro = &iproute;
15002c1c707SAndre Oppermann 		bzero(ro, sizeof (*ro));
15102c1c707SAndre Oppermann 	}
15202c1c707SAndre Oppermann 
15384843845SSam Leffler 	if (inp != NULL)
15484843845SSam Leffler 		INP_LOCK_ASSERT(inp);
1552b25acc1SLuigi Rizzo 
156df8bae1dSRodney W. Grimes 	if (opt) {
157cb7641e8SMaxim Konovalov 		len = 0;
158df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
159cb7641e8SMaxim Konovalov 		if (len != 0)
160df8bae1dSRodney W. Grimes 			hlen = len;
161df8bae1dSRodney W. Grimes 	}
162df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1633efc3014SJulian Elischer 
164df8bae1dSRodney W. Grimes 	/*
1656e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
166e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
167e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
168e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
169e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1706e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1716e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1726e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1736e49b1feSGarrett Wollman 	 * <http://www.research.att.com/~smb/papers/fnat.pdf>.
174df8bae1dSRodney W. Grimes 	 */
175df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
17653be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
17753be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1781f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
179df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
180df8bae1dSRodney W. Grimes 	} else {
18153be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
182df8bae1dSRodney W. Grimes 	}
1839c9137eaSGarrett Wollman 
184df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
1859b932e9eSAndre Oppermann again:
186df8bae1dSRodney W. Grimes 	/*
187df8bae1dSRodney W. Grimes 	 * If there is a cached route,
188df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
189df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
190a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
191a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
192df8bae1dSRodney W. Grimes 	 */
193df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
194a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
1959b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
196df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
197df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
198df8bae1dSRodney W. Grimes 	}
1999b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
2009b932e9eSAndre Oppermann 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
2019b932e9eSAndre Oppermann #else
2020b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
2039b932e9eSAndre Oppermann #endif
204a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
205df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
206df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2079b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
208df8bae1dSRodney W. Grimes 	}
209df8bae1dSRodney W. Grimes 	/*
210df8bae1dSRodney W. Grimes 	 * If routing to interface only,
211df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
212df8bae1dSRodney W. Grimes 	 */
213df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
2140b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2150b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
216df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
217df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
218df8bae1dSRodney W. Grimes 			goto bad;
219df8bae1dSRodney W. Grimes 		}
220df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
221df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2229f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2233afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
22438c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2253afefa39SDaniel C. Sobral 		/*
22638c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
22738c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2283afefa39SDaniel C. Sobral 		 */
22938c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
23038c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
23138c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
232df8bae1dSRodney W. Grimes 	} else {
2332c17fe93SGarrett Wollman 		/*
23497d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
23597d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
23697d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2372c17fe93SGarrett Wollman 		 */
2380b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
239e6e51f05SLuigi Rizzo 			rtalloc_ign(ro, 0);
2400b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
241df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
242df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
243df8bae1dSRodney W. Grimes 			goto bad;
244df8bae1dSRodney W. Grimes 		}
245df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
246df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
24797d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
248df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
249f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2509f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
251f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2529f9b3dc4SGarrett Wollman 		else
2539f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
254df8bae1dSRodney W. Grimes 	}
2559b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
256df8bae1dSRodney W. Grimes 		struct in_multi *inm;
257df8bae1dSRodney W. Grimes 
258df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
259df8bae1dSRodney W. Grimes 		/*
260df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
261df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
262df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
263df8bae1dSRodney W. Grimes 		 */
264df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
265df8bae1dSRodney W. Grimes 		/*
266df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
267df8bae1dSRodney W. Grimes 		 */
268df8bae1dSRodney W. Grimes 		if (imo != NULL) {
269df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
2701c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
2711c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
272bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
273bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
274bbb4330bSLuigi Rizzo 				    INADDR_ANY;
275df8bae1dSRodney W. Grimes 		} else
276df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
277df8bae1dSRodney W. Grimes 		/*
278df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
279df8bae1dSRodney W. Grimes 		 */
2801c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
281df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
282df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
283df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
284df8bae1dSRodney W. Grimes 				goto bad;
285df8bae1dSRodney W. Grimes 			}
2861c5de19aSGarrett Wollman 		}
287df8bae1dSRodney W. Grimes 		/*
288df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
289df8bae1dSRodney W. Grimes 		 * of outgoing interface.
290df8bae1dSRodney W. Grimes 		 */
291df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
29238c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
29338c1bc35SRuslan Ermilov 			if (ia != NULL)
29438c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
295df8bae1dSRodney W. Grimes 		}
296df8bae1dSRodney W. Grimes 
2979b932e9eSAndre Oppermann 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
298df8bae1dSRodney W. Grimes 		if (inm != NULL &&
299df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
300df8bae1dSRodney W. Grimes 			/*
301df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
302df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
303df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
304df8bae1dSRodney W. Grimes 			 */
30586b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
306df8bae1dSRodney W. Grimes 		}
307df8bae1dSRodney W. Grimes 		else {
308df8bae1dSRodney W. Grimes 			/*
309df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
310df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
311df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
312df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
313df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
314df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
315df8bae1dSRodney W. Grimes 			 *
316df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
317df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
318df8bae1dSRodney W. Grimes 			 * if necessary.
319df8bae1dSRodney W. Grimes 			 */
320df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
321f0068c4aSGarrett Wollman 				/*
322bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
323f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
324f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
325f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
326f0068c4aSGarrett Wollman 				 */
327b124e4f2SGarrett Wollman 				if (!rsvp_on)
328f0068c4aSGarrett Wollman 					imo = NULL;
329bbb4330bSLuigi Rizzo 				if (ip_mforward &&
330bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
331df8bae1dSRodney W. Grimes 					m_freem(m);
332df8bae1dSRodney W. Grimes 					goto done;
333df8bae1dSRodney W. Grimes 				}
334df8bae1dSRodney W. Grimes 			}
335df8bae1dSRodney W. Grimes 		}
3365e9ae478SGarrett Wollman 
337df8bae1dSRodney W. Grimes 		/*
338df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
339df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
340df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
341df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
342df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
343df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
344df8bae1dSRodney W. Grimes 		 */
345f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
346df8bae1dSRodney W. Grimes 			m_freem(m);
347df8bae1dSRodney W. Grimes 			goto done;
348df8bae1dSRodney W. Grimes 		}
349df8bae1dSRodney W. Grimes 
350df8bae1dSRodney W. Grimes 		goto sendit;
351df8bae1dSRodney W. Grimes 	}
352df8bae1dSRodney W. Grimes #ifndef notdef
353df8bae1dSRodney W. Grimes 	/*
3542b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
355cb459254SJohn-Mark Gurney 	 * of the outoing interface.
356df8bae1dSRodney W. Grimes 	 */
357f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
35838c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
35938c1bc35SRuslan Ermilov 		if (ia != NULL) {
360df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
361f9e354dfSJulian Elischer 		}
36238c1bc35SRuslan Ermilov 	}
363f9e354dfSJulian Elischer #endif /* notdef */
364ca7a789aSMax Laier 	/*
365ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
366ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
367ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
368ca7a789aSMax Laier 	 */
36902b199f1SMax Laier #ifdef ALTQ
370ca7a789aSMax Laier 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
371ca7a789aSMax Laier 	    ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
372ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen))
373ca7a789aSMax Laier #else
374b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
375ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen)
376ca7a789aSMax Laier #endif /* ALTQ */
377ca7a789aSMax Laier 	{
378b5390296SDavid Greenman 		error = ENOBUFS;
37935609d45SJonathan Lemon 		ipstat.ips_odropped++;
380b5390296SDavid Greenman 		goto bad;
381b5390296SDavid Greenman 	}
382b5390296SDavid Greenman 
383b5390296SDavid Greenman 	/*
384df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
3858c3f5566SRuslan Ermilov 	 * verify user is allowed to send
386df8bae1dSRodney W. Grimes 	 * such a packet.
387df8bae1dSRodney W. Grimes 	 */
3889f9b3dc4SGarrett Wollman 	if (isbroadcast) {
389df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
390df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
391df8bae1dSRodney W. Grimes 			goto bad;
392df8bae1dSRodney W. Grimes 		}
393df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
394df8bae1dSRodney W. Grimes 			error = EACCES;
395df8bae1dSRodney W. Grimes 			goto bad;
396df8bae1dSRodney W. Grimes 		}
397df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
3981e78ac21SJeffrey Hsu 		if (ip->ip_len > ifp->if_mtu) {
399df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
400df8bae1dSRodney W. Grimes 			goto bad;
401df8bae1dSRodney W. Grimes 		}
4028afa2304SBruce M Simpson 		if (flags & IP_SENDONES)
4038afa2304SBruce M Simpson 			ip->ip_dst.s_addr = INADDR_BROADCAST;
404df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4059f9b3dc4SGarrett Wollman 	} else {
406df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4079f9b3dc4SGarrett Wollman 	}
408df8bae1dSRodney W. Grimes 
409f1743588SDarren Reed sendit:
41033841545SHajimu UMEMOTO #ifdef IPSEC
41133841545SHajimu UMEMOTO 	/* get SP for this packet */
412f073c60fSHajimu UMEMOTO 	if (inp == NULL)
4130f9ade71SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
4140f9ade71SHajimu UMEMOTO 		    flags, &error);
41533841545SHajimu UMEMOTO 	else
416f073c60fSHajimu UMEMOTO 		sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
41733841545SHajimu UMEMOTO 
41833841545SHajimu UMEMOTO 	if (sp == NULL) {
41933841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
42033841545SHajimu UMEMOTO 		goto bad;
42133841545SHajimu UMEMOTO 	}
42233841545SHajimu UMEMOTO 
42333841545SHajimu UMEMOTO 	error = 0;
42433841545SHajimu UMEMOTO 
42533841545SHajimu UMEMOTO 	/* check policy */
42633841545SHajimu UMEMOTO 	switch (sp->policy) {
42733841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
42833841545SHajimu UMEMOTO 		/*
42933841545SHajimu UMEMOTO 		 * This packet is just discarded.
43033841545SHajimu UMEMOTO 		 */
43133841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
43233841545SHajimu UMEMOTO 		goto bad;
43333841545SHajimu UMEMOTO 
43433841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
43533841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
4361cfd4b53SBruce M Simpson 	case IPSEC_POLICY_TCP:
43733841545SHajimu UMEMOTO 		/* no need to do IPsec. */
43833841545SHajimu UMEMOTO 		goto skip_ipsec;
43933841545SHajimu UMEMOTO 
44033841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
44133841545SHajimu UMEMOTO 		if (sp->req == NULL) {
44233841545SHajimu UMEMOTO 			/* acquire a policy */
44333841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
44433841545SHajimu UMEMOTO 			goto bad;
44533841545SHajimu UMEMOTO 		}
44633841545SHajimu UMEMOTO 		break;
44733841545SHajimu UMEMOTO 
44833841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
44933841545SHajimu UMEMOTO 	default:
45033841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
45133841545SHajimu UMEMOTO 	}
45233841545SHajimu UMEMOTO     {
45333841545SHajimu UMEMOTO 	struct ipsec_output_state state;
45433841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
45533841545SHajimu UMEMOTO 	state.m = m;
45633841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
45733841545SHajimu UMEMOTO 		state.ro = &iproute;
45833841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
45933841545SHajimu UMEMOTO 	} else
46033841545SHajimu UMEMOTO 		state.ro = ro;
46133841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
46233841545SHajimu UMEMOTO 
46333841545SHajimu UMEMOTO 	ip->ip_sum = 0;
46433841545SHajimu UMEMOTO 
46533841545SHajimu UMEMOTO 	/*
46633841545SHajimu UMEMOTO 	 * XXX
46733841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
46833841545SHajimu UMEMOTO 	 */
46933841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
47033841545SHajimu UMEMOTO 		in_delayed_cksum(m);
47133841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
47233841545SHajimu UMEMOTO 	}
47333841545SHajimu UMEMOTO 
474fd8e4ebcSMike Barcroft 	ip->ip_len = htons(ip->ip_len);
475fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
47633841545SHajimu UMEMOTO 
47733841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
47833841545SHajimu UMEMOTO 
47933841545SHajimu UMEMOTO 	m = state.m;
48033841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
48133841545SHajimu UMEMOTO 		/*
48233841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
48333841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
48433841545SHajimu UMEMOTO 		 */
48533841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
48633841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
48733841545SHajimu UMEMOTO 			ro = state.ro;
48833841545SHajimu UMEMOTO 		}
48933841545SHajimu UMEMOTO 	} else
49033841545SHajimu UMEMOTO 		ro = state.ro;
49133841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
49233841545SHajimu UMEMOTO 	if (error) {
49333841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
494ac9d7e26SMax Laier 		m = NULL;
49533841545SHajimu UMEMOTO 		switch (error) {
49633841545SHajimu UMEMOTO 		case EHOSTUNREACH:
49733841545SHajimu UMEMOTO 		case ENETUNREACH:
49833841545SHajimu UMEMOTO 		case EMSGSIZE:
49933841545SHajimu UMEMOTO 		case ENOBUFS:
50033841545SHajimu UMEMOTO 		case ENOMEM:
50133841545SHajimu UMEMOTO 			break;
50233841545SHajimu UMEMOTO 		default:
50333841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
50433841545SHajimu UMEMOTO 			/*fall through*/
50533841545SHajimu UMEMOTO 		case ENOENT:
50633841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
50733841545SHajimu UMEMOTO 			error = 0;
50833841545SHajimu UMEMOTO 			break;
50933841545SHajimu UMEMOTO 		}
51033841545SHajimu UMEMOTO 		goto bad;
51133841545SHajimu UMEMOTO 	}
51233841545SHajimu UMEMOTO 
51333841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
51433841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
51533841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
51633841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
51733841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
51833841545SHajimu UMEMOTO 			printf("ip_output: "
51933841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
52033841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
52133841545SHajimu UMEMOTO 			goto bad;
52233841545SHajimu UMEMOTO 		}
52333841545SHajimu UMEMOTO 	} else {
52470dbc6cbSHajimu UMEMOTO 		if (state.encap) {
52533841545SHajimu UMEMOTO 			ia = ifatoia(ro->ro_rt->rt_ifa);
52633841545SHajimu UMEMOTO 			ifp = ro->ro_rt->rt_ifp;
52733841545SHajimu UMEMOTO 		}
52870dbc6cbSHajimu UMEMOTO 	}
52970dbc6cbSHajimu UMEMOTO     }
53033841545SHajimu UMEMOTO 
53133841545SHajimu UMEMOTO 	/* make it flipped, again. */
532fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
533fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
53433841545SHajimu UMEMOTO skip_ipsec:
53533841545SHajimu UMEMOTO #endif /*IPSEC*/
536b9234fafSSam Leffler #ifdef FAST_IPSEC
537b9234fafSSam Leffler 	/*
538b9234fafSSam Leffler 	 * Check the security policy (SP) for the packet and, if
539b9234fafSSam Leffler 	 * required, do IPsec-related processing.  There are two
540b9234fafSSam Leffler 	 * cases here; the first time a packet is sent through
541b9234fafSSam Leffler 	 * it will be untagged and handled by ipsec4_checkpolicy.
542b9234fafSSam Leffler 	 * If the packet is resubmitted to ip_output (e.g. after
543b9234fafSSam Leffler 	 * AH, ESP, etc. processing), there will be a tag to bypass
544b9234fafSSam Leffler 	 * the lookup and related policy checking.
545b9234fafSSam Leffler 	 */
546b9234fafSSam Leffler 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
547b9234fafSSam Leffler 	s = splnet();
548b9234fafSSam Leffler 	if (mtag != NULL) {
549b9234fafSSam Leffler 		tdbi = (struct tdb_ident *)(mtag + 1);
550b9234fafSSam Leffler 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
551b9234fafSSam Leffler 		if (sp == NULL)
552b9234fafSSam Leffler 			error = -EINVAL;	/* force silent drop */
553b9234fafSSam Leffler 		m_tag_delete(m, mtag);
554b9234fafSSam Leffler 	} else {
555b9234fafSSam Leffler 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
556b9234fafSSam Leffler 					&error, inp);
557b9234fafSSam Leffler 	}
558b9234fafSSam Leffler 	/*
559b9234fafSSam Leffler 	 * There are four return cases:
560b9234fafSSam Leffler 	 *    sp != NULL	 	    apply IPsec policy
561b9234fafSSam Leffler 	 *    sp == NULL, error == 0	    no IPsec handling needed
562b9234fafSSam Leffler 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
563b9234fafSSam Leffler 	 *    sp == NULL, error != 0	    discard packet, report error
564b9234fafSSam Leffler 	 */
565b9234fafSSam Leffler 	if (sp != NULL) {
566b9234fafSSam Leffler 		/* Loop detection, check if ipsec processing already done */
567b9234fafSSam Leffler 		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
568b9234fafSSam Leffler 		for (mtag = m_tag_first(m); mtag != NULL;
569b9234fafSSam Leffler 		     mtag = m_tag_next(m, mtag)) {
570b9234fafSSam Leffler 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
571b9234fafSSam Leffler 				continue;
572b9234fafSSam Leffler 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
573b9234fafSSam Leffler 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
574b9234fafSSam Leffler 				continue;
575b9234fafSSam Leffler 			/*
576b9234fafSSam Leffler 			 * Check if policy has an SA associated with it.
577b9234fafSSam Leffler 			 * This can happen when an SP has yet to acquire
578b9234fafSSam Leffler 			 * an SA; e.g. on first reference.  If it occurs,
579b9234fafSSam Leffler 			 * then we let ipsec4_process_packet do its thing.
580b9234fafSSam Leffler 			 */
581b9234fafSSam Leffler 			if (sp->req->sav == NULL)
582b9234fafSSam Leffler 				break;
583b9234fafSSam Leffler 			tdbi = (struct tdb_ident *)(mtag + 1);
584b9234fafSSam Leffler 			if (tdbi->spi == sp->req->sav->spi &&
585b9234fafSSam Leffler 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
586ab94ca3cSSam Leffler 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
587b9234fafSSam Leffler 				 sizeof (union sockaddr_union)) == 0) {
588b9234fafSSam Leffler 				/*
589b9234fafSSam Leffler 				 * No IPsec processing is needed, free
590b9234fafSSam Leffler 				 * reference to SP.
591b9234fafSSam Leffler 				 *
592b9234fafSSam Leffler 				 * NB: null pointer to avoid free at
593b9234fafSSam Leffler 				 *     done: below.
594b9234fafSSam Leffler 				 */
595b9234fafSSam Leffler 				KEY_FREESP(&sp), sp = NULL;
596b9234fafSSam Leffler 				splx(s);
597b9234fafSSam Leffler 				goto spd_done;
598b9234fafSSam Leffler 			}
599b9234fafSSam Leffler 		}
600b9234fafSSam Leffler 
601b9234fafSSam Leffler 		/*
602b9234fafSSam Leffler 		 * Do delayed checksums now because we send before
603b9234fafSSam Leffler 		 * this is done in the normal processing path.
604b9234fafSSam Leffler 		 */
605b9234fafSSam Leffler 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
606b9234fafSSam Leffler 			in_delayed_cksum(m);
607b9234fafSSam Leffler 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
608b9234fafSSam Leffler 		}
609b9234fafSSam Leffler 
610b9234fafSSam Leffler 		ip->ip_len = htons(ip->ip_len);
611b9234fafSSam Leffler 		ip->ip_off = htons(ip->ip_off);
612b9234fafSSam Leffler 
613b9234fafSSam Leffler 		/* NB: callee frees mbuf */
614b9234fafSSam Leffler 		error = ipsec4_process_packet(m, sp->req, flags, 0);
6159359ad86SSam Leffler 		/*
6169359ad86SSam Leffler 		 * Preserve KAME behaviour: ENOENT can be returned
6179359ad86SSam Leffler 		 * when an SA acquire is in progress.  Don't propagate
6189359ad86SSam Leffler 		 * this to user-level; it confuses applications.
6199359ad86SSam Leffler 		 *
6209359ad86SSam Leffler 		 * XXX this will go away when the SADB is redone.
6219359ad86SSam Leffler 		 */
6229359ad86SSam Leffler 		if (error == ENOENT)
6239359ad86SSam Leffler 			error = 0;
624b9234fafSSam Leffler 		splx(s);
625b9234fafSSam Leffler 		goto done;
626b9234fafSSam Leffler 	} else {
627b9234fafSSam Leffler 		splx(s);
628b9234fafSSam Leffler 
629b9234fafSSam Leffler 		if (error != 0) {
630b9234fafSSam Leffler 			/*
631b9234fafSSam Leffler 			 * Hack: -EINVAL is used to signal that a packet
632b9234fafSSam Leffler 			 * should be silently discarded.  This is typically
633b9234fafSSam Leffler 			 * because we asked key management for an SA and
634b9234fafSSam Leffler 			 * it was delayed (e.g. kicked up to IKE).
635b9234fafSSam Leffler 			 */
636b9234fafSSam Leffler 			if (error == -EINVAL)
637b9234fafSSam Leffler 				error = 0;
638b9234fafSSam Leffler 			goto bad;
639b9234fafSSam Leffler 		} else {
640b9234fafSSam Leffler 			/* No IPsec processing for this packet. */
641b9234fafSSam Leffler 		}
642b9234fafSSam Leffler #ifdef notyet
643b9234fafSSam Leffler 		/*
644b9234fafSSam Leffler 		 * If deferred crypto processing is needed, check that
645b9234fafSSam Leffler 		 * the interface supports it.
646b9234fafSSam Leffler 		 */
647b9234fafSSam Leffler 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
648b9234fafSSam Leffler 		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
649b9234fafSSam Leffler 			/* notify IPsec to do its own crypto */
650b9234fafSSam Leffler 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
651b9234fafSSam Leffler 			error = EHOSTUNREACH;
652b9234fafSSam Leffler 			goto bad;
653b9234fafSSam Leffler 		}
654b9234fafSSam Leffler #endif
655b9234fafSSam Leffler 	}
656b9234fafSSam Leffler spd_done:
657b9234fafSSam Leffler #endif /* FAST_IPSEC */
65833841545SHajimu UMEMOTO 
659c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
660c21fd232SAndre Oppermann 	if (inet_pfil_hook.ph_busy_count == -1)
661c21fd232SAndre Oppermann 		goto passout;
662c21fd232SAndre Oppermann 
663c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
6649b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
665134ea224SSam Leffler 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT);
666134ea224SSam Leffler 	if (error != 0 || m == NULL)
667c4ac87eaSDarren Reed 		goto done;
6689b932e9eSAndre Oppermann 
669c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
670fed1c7e9SSøren Schmidt 
6719b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
6729b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
6739b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
6749b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
6759b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
676f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
677a9c92b54SAndre Oppermann 				m->m_pkthdr.rcvif = loif;
67820c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
67920c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
68020c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
681ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
68220c822f3SJonathan Lemon 			}
68320c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
68420c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
6859b932e9eSAndre Oppermann 
6869b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
6879b932e9eSAndre Oppermann 			goto done;
6889b932e9eSAndre Oppermann 		} else
6899b932e9eSAndre Oppermann 			goto again;
6909b932e9eSAndre Oppermann 	}
6919b932e9eSAndre Oppermann 
6929b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
6939b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
6949b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
6959b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
696a9c92b54SAndre Oppermann 			m->m_pkthdr.rcvif = loif;
6979b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
6989b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
6999b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
7009b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
7019b932e9eSAndre Oppermann 		}
7029b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
7039b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
7049b932e9eSAndre Oppermann 
7059b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
706f9e354dfSJulian Elischer 		goto done;
707f9e354dfSJulian Elischer 	}
7089b932e9eSAndre Oppermann 	/* Or forward to some other address? */
7099b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7109b932e9eSAndre Oppermann 	if (fwd_tag) {
7119b932e9eSAndre Oppermann 		if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) {
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;
7179b932e9eSAndre Oppermann 		} else {
7189b932e9eSAndre Oppermann 			m_tag_delete(m, fwd_tag);
7199b932e9eSAndre Oppermann 			/* Continue. */
720f9e354dfSJulian Elischer 		}
721f9e354dfSJulian Elischer 	}
7229b932e9eSAndre Oppermann #endif
7232b25acc1SLuigi Rizzo 
724c21fd232SAndre Oppermann passout:
72551c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
72651c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
72751c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
72851c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
72951c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
73051c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
73151c8ec4aSRuslan Ermilov 			goto bad;
73251c8ec4aSRuslan Ermilov 		}
73351c8ec4aSRuslan Ermilov 	}
73451c8ec4aSRuslan Ermilov 
735206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
736206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
737db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
738db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
739db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
740db4f9cc7SJonathan Lemon 	}
741206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
742db4f9cc7SJonathan Lemon 
743e7319babSPoul-Henning Kamp 	/*
744db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
745db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
746df8bae1dSRodney W. Grimes 	 */
7472683ceb6SAndre Oppermann 	if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
7482683ceb6SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0))) {
749fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
750fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
751df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
75253be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
753df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
7545da9f8faSJosef Karthauser 
7555da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
75638c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
7575da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
7585da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
7595da9f8faSJosef Karthauser 		}
7605da9f8faSJosef Karthauser 
76133841545SHajimu UMEMOTO #ifdef IPSEC
76233841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
76333841545SHajimu UMEMOTO 		ipsec_delaux(m);
76433841545SHajimu UMEMOTO #endif
76533841545SHajimu UMEMOTO 
76653dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
7673390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
7683390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
7699d9edc56SMike Silbersack #endif
770df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
771df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
772df8bae1dSRodney W. Grimes 		goto done;
773df8bae1dSRodney W. Grimes 	}
7741e78ac21SJeffrey Hsu 
775df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
776df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
7773d1f141bSGarrett Wollman 		/*
7783d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
7793d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
7803d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
7813d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
7823d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
7833d1f141bSGarrett Wollman 		 */
7841e78ac21SJeffrey Hsu 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
7851e78ac21SJeffrey Hsu 		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
7863d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
7873d1f141bSGarrett Wollman 		}
788df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
789df8bae1dSRodney W. Grimes 		goto bad;
790df8bae1dSRodney W. Grimes 	}
7911e78ac21SJeffrey Hsu 
7921e78ac21SJeffrey Hsu 	/*
7931e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
7941e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
7951e78ac21SJeffrey Hsu 	 */
7961e78ac21SJeffrey Hsu 	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
7971e78ac21SJeffrey Hsu 	if (error)
798df8bae1dSRodney W. Grimes 		goto bad;
7991e78ac21SJeffrey Hsu 	for (; m; m = m0) {
800df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
801b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
80233841545SHajimu UMEMOTO #ifdef IPSEC
80333841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
80433841545SHajimu UMEMOTO 		ipsec_delaux(m);
80533841545SHajimu UMEMOTO #endif
80607203494SDaniel C. Sobral 		if (error == 0) {
807fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
80807203494SDaniel C. Sobral 			if (ia != NULL) {
809fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
810fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
81107203494SDaniel C. Sobral 			}
812fe937674SJosef Karthauser 
813df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
814df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
815fe937674SJosef Karthauser 		} else
816df8bae1dSRodney W. Grimes 			m_freem(m);
817df8bae1dSRodney W. Grimes 	}
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes 	if (error == 0)
820df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
8211e78ac21SJeffrey Hsu 
822df8bae1dSRodney W. Grimes done:
8236a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
8246a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
825ac9d7e26SMax Laier 	}
82602c1c707SAndre Oppermann #ifdef IPSEC
8276a800098SYoshinobu Inoue 	if (sp != NULL) {
8286a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
8296a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
8306a800098SYoshinobu Inoue 		key_freesp(sp);
8316a800098SYoshinobu Inoue 	}
8321e78ac21SJeffrey Hsu #endif
833b9234fafSSam Leffler #ifdef FAST_IPSEC
834b9234fafSSam Leffler 	if (sp != NULL)
835b9234fafSSam Leffler 		KEY_FREESP(&sp);
8361e78ac21SJeffrey Hsu #endif
837df8bae1dSRodney W. Grimes 	return (error);
838df8bae1dSRodney W. Grimes bad:
8393528d68fSBill Paul 	m_freem(m);
840df8bae1dSRodney W. Grimes 	goto done;
841df8bae1dSRodney W. Grimes }
842df8bae1dSRodney W. Grimes 
8431e78ac21SJeffrey Hsu /*
8441e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
8451e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
8461e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
8471e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
8481e78ac21SJeffrey Hsu  *
8491e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
8501e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
8511e78ac21SJeffrey Hsu  */
8521e78ac21SJeffrey Hsu int
8531e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
8541e78ac21SJeffrey Hsu 	    u_long if_hwassist_flags, int sw_csum)
8551e78ac21SJeffrey Hsu {
8561e78ac21SJeffrey Hsu 	int error = 0;
8571e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
8581e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
8591e78ac21SJeffrey Hsu 	int off;
8601e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
8611e78ac21SJeffrey Hsu 	int firstlen;
8621e78ac21SJeffrey Hsu 	struct mbuf **mnext;
8631e78ac21SJeffrey Hsu 	int nfrags;
8641e78ac21SJeffrey Hsu 
8651e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
8661e78ac21SJeffrey Hsu 		ipstat.ips_cantfrag++;
8671e78ac21SJeffrey Hsu 		return EMSGSIZE;
8681e78ac21SJeffrey Hsu 	}
8691e78ac21SJeffrey Hsu 
8701e78ac21SJeffrey Hsu 	/*
8711e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
8721e78ac21SJeffrey Hsu 	 */
8731e78ac21SJeffrey Hsu 	if (len < 8)
8741e78ac21SJeffrey Hsu 		return EMSGSIZE;
8751e78ac21SJeffrey Hsu 
8761e78ac21SJeffrey Hsu 	/*
8771e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
8781e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
8791e78ac21SJeffrey Hsu 	 */
8801e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
8811e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
8821e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
8831e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
8841e78ac21SJeffrey Hsu 	}
8851e78ac21SJeffrey Hsu 
8861e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
8871e78ac21SJeffrey Hsu 		/*
8881e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
8891e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
8901e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
8911e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
8921e78ac21SJeffrey Hsu 		 *
8931e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
8941e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
8951e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
8961e78ac21SJeffrey Hsu 		 */
8971e78ac21SJeffrey Hsu 		int newlen;
8981e78ac21SJeffrey Hsu 		struct mbuf *m;
8991e78ac21SJeffrey Hsu 
9001e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
9011e78ac21SJeffrey Hsu 			off += m->m_len;
9021e78ac21SJeffrey Hsu 
9031e78ac21SJeffrey Hsu 		/*
9041e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
9051e78ac21SJeffrey Hsu 		 * 8-byte boundary
9061e78ac21SJeffrey Hsu 		 */
9071e78ac21SJeffrey Hsu 		if (off < hlen)
9081e78ac21SJeffrey Hsu 			goto smart_frag_failure;
9091e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
9101e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
9111e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
9121e78ac21SJeffrey Hsu 			/* we failed, go back the default */
9131e78ac21SJeffrey Hsu smart_frag_failure:
9141e78ac21SJeffrey Hsu 			newlen = len;
9151e78ac21SJeffrey Hsu 			off = hlen + len;
9161e78ac21SJeffrey Hsu 		}
9171e78ac21SJeffrey Hsu 		len = newlen;
9181e78ac21SJeffrey Hsu 
9191e78ac21SJeffrey Hsu 	} else {
9201e78ac21SJeffrey Hsu 		off = hlen + len;
9211e78ac21SJeffrey Hsu 	}
9221e78ac21SJeffrey Hsu 
9231e78ac21SJeffrey Hsu 	firstlen = off - hlen;
9241e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
9251e78ac21SJeffrey Hsu 
9261e78ac21SJeffrey Hsu 	/*
9271e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
9281e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
9291e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
9301e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
9311e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
9321e78ac21SJeffrey Hsu 	 */
9331e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
9341e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
9351e78ac21SJeffrey Hsu 		struct mbuf *m;
9361e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
9371e78ac21SJeffrey Hsu 
9381e78ac21SJeffrey Hsu 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
9390b17fba7SAndre Oppermann 		if (m == NULL) {
9401e78ac21SJeffrey Hsu 			error = ENOBUFS;
9411e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
9421e78ac21SJeffrey Hsu 			goto done;
9431e78ac21SJeffrey Hsu 		}
9441e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
9451e78ac21SJeffrey Hsu 		/*
9461e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
9471e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
9481e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
9491e78ac21SJeffrey Hsu 		 */
9501e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
9511e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
9521e78ac21SJeffrey Hsu 		*mhip = *ip;
9531e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
9541e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9551e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
9561e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
9571e78ac21SJeffrey Hsu 		}
9581e78ac21SJeffrey Hsu 		m->m_len = mhlen;
9591e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
9601e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
9611e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
9621e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
9631e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
9641e78ac21SJeffrey Hsu 		} else
9651e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
9661e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
9671e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
9680b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
9691e78ac21SJeffrey Hsu 			m_free(m);
9701e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
9711e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
9721e78ac21SJeffrey Hsu 			goto done;
9731e78ac21SJeffrey Hsu 		}
9741e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
9751e78ac21SJeffrey Hsu 		m->m_pkthdr.rcvif = (struct ifnet *)0;
9761e78ac21SJeffrey Hsu #ifdef MAC
9771e78ac21SJeffrey Hsu 		mac_create_fragment(m0, m);
9781e78ac21SJeffrey Hsu #endif
9791e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
9801e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
9811e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
9821e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
9831e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
9841e78ac21SJeffrey Hsu 		*mnext = m;
9851e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
9861e78ac21SJeffrey Hsu 	}
9871e78ac21SJeffrey Hsu 	ipstat.ips_ofragments += nfrags;
9881e78ac21SJeffrey Hsu 
9891e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
9901e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
9911e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
9921e78ac21SJeffrey Hsu 
9931e78ac21SJeffrey Hsu 	/*
9941e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
9951e78ac21SJeffrey Hsu 	 * and updating header.
9961e78ac21SJeffrey Hsu 	 */
9971e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
9981e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
9991e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
10001e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
10011e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
10021e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
10031e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
10041e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
10051e78ac21SJeffrey Hsu 
10061e78ac21SJeffrey Hsu done:
10071e78ac21SJeffrey Hsu 	*m_frag = m0;
10081e78ac21SJeffrey Hsu 	return error;
10091e78ac21SJeffrey Hsu }
10101e78ac21SJeffrey Hsu 
10111c238475SJonathan Lemon void
1012db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1013db4f9cc7SJonathan Lemon {
1014db4f9cc7SJonathan Lemon 	struct ip *ip;
1015db4f9cc7SJonathan Lemon 	u_short csum, offset;
1016db4f9cc7SJonathan Lemon 
1017db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
101853be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
1019db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1020206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1021206a3274SRuslan Ermilov 		csum = 0xffff;
1022db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1023db4f9cc7SJonathan Lemon 
1024db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1025db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1026db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1027db4f9cc7SJonathan Lemon 		/*
1028db4f9cc7SJonathan Lemon 		 * XXX
1029db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1030db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1031db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1032db4f9cc7SJonathan Lemon 		 */
1033db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1034db4f9cc7SJonathan Lemon 	}
1035db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1036db4f9cc7SJonathan Lemon }
1037db4f9cc7SJonathan Lemon 
1038df8bae1dSRodney W. Grimes /*
1039df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1040df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1041df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1042072b9b24SPaul Traina  *
1043072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1044df8bae1dSRodney W. Grimes  */
1045df8bae1dSRodney W. Grimes static struct mbuf *
1046b375c9ecSLuigi Rizzo ip_insertoptions(m, opt, phlen)
1047b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1048b375c9ecSLuigi Rizzo 	struct mbuf *opt;
1049b375c9ecSLuigi Rizzo 	int *phlen;
1050df8bae1dSRodney W. Grimes {
1051b375c9ecSLuigi Rizzo 	register struct ipoption *p = mtod(opt, struct ipoption *);
1052df8bae1dSRodney W. Grimes 	struct mbuf *n;
1053b375c9ecSLuigi Rizzo 	register struct ip *ip = mtod(m, struct ip *);
1054df8bae1dSRodney W. Grimes 	unsigned optlen;
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
10571e78ac21SJeffrey Hsu 	if (optlen + ip->ip_len > IP_MAXPACKET) {
1058cb7641e8SMaxim Konovalov 		*phlen = 0;
1059df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1060cb7641e8SMaxim Konovalov 	}
1061df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1062df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1063df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1064a163d034SWarner Losh 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
10650b17fba7SAndre Oppermann 		if (n == NULL) {
1066cb7641e8SMaxim Konovalov 			*phlen = 0;
1067df8bae1dSRodney W. Grimes 			return (m);
1068cb7641e8SMaxim Konovalov 		}
10695db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
10704ed84624SRobert Watson #ifdef MAC
10714ed84624SRobert Watson 		mac_create_mbuf_from_mbuf(m, n);
10724ed84624SRobert Watson #endif
1073df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1074df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1075df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1076df8bae1dSRodney W. Grimes 		n->m_next = m;
1077df8bae1dSRodney W. Grimes 		m = n;
1078df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1079df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1080212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1081df8bae1dSRodney W. Grimes 	} else {
1082df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1083df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1084df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1085212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1086df8bae1dSRodney W. Grimes 	}
1087df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
10880453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1089df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
109053be11f6SPoul-Henning Kamp 	ip->ip_v = IPVERSION;
109153be11f6SPoul-Henning Kamp 	ip->ip_hl = *phlen >> 2;
1092df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1093df8bae1dSRodney W. Grimes 	return (m);
1094df8bae1dSRodney W. Grimes }
1095df8bae1dSRodney W. Grimes 
1096df8bae1dSRodney W. Grimes /*
1097df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1098df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1099df8bae1dSRodney W. Grimes  */
1100beec8214SDarren Reed int
1101b375c9ecSLuigi Rizzo ip_optcopy(ip, jp)
1102b375c9ecSLuigi Rizzo 	struct ip *ip, *jp;
1103df8bae1dSRodney W. Grimes {
1104b375c9ecSLuigi Rizzo 	register u_char *cp, *dp;
1105df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1106df8bae1dSRodney W. Grimes 
1107df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1108df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
110953be11f6SPoul-Henning Kamp 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1110df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1111df8bae1dSRodney W. Grimes 		opt = cp[0];
1112df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1113df8bae1dSRodney W. Grimes 			break;
1114df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1115df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1116df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1117df8bae1dSRodney W. Grimes 			optlen = 1;
1118df8bae1dSRodney W. Grimes 			continue;
1119686cdd19SJun-ichiro itojun Hagino 		}
1120db40007dSAndrew R. Reiter 
1121db40007dSAndrew R. Reiter 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1122db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1123df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1124db40007dSAndrew R. Reiter 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1125db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1126db40007dSAndrew R. Reiter 
1127df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1128df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1129df8bae1dSRodney W. Grimes 			optlen = cnt;
1130df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
11310453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1132df8bae1dSRodney W. Grimes 			dp += optlen;
1133df8bae1dSRodney W. Grimes 		}
1134df8bae1dSRodney W. Grimes 	}
1135df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1136df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1137df8bae1dSRodney W. Grimes 	return (optlen);
1138df8bae1dSRodney W. Grimes }
1139df8bae1dSRodney W. Grimes 
1140df8bae1dSRodney W. Grimes /*
1141df8bae1dSRodney W. Grimes  * IP socket option processing.
1142df8bae1dSRodney W. Grimes  */
1143df8bae1dSRodney W. Grimes int
1144b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt)
1145b375c9ecSLuigi Rizzo 	struct socket *so;
1146b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1147df8bae1dSRodney W. Grimes {
1148cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1149cfe8b629SGarrett Wollman 	int	error, optval;
1150df8bae1dSRodney W. Grimes 
1151cfe8b629SGarrett Wollman 	error = optval = 0;
1152cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1153cfe8b629SGarrett Wollman 		return (EINVAL);
1154cfe8b629SGarrett Wollman 	}
1155df8bae1dSRodney W. Grimes 
1156cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1157cfe8b629SGarrett Wollman 	case SOPT_SET:
1158cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1159df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1160df8bae1dSRodney W. Grimes #ifdef notyet
1161df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1162df8bae1dSRodney W. Grimes #endif
1163cfe8b629SGarrett Wollman 		{
1164cfe8b629SGarrett Wollman 			struct mbuf *m;
1165cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1166cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1167cfe8b629SGarrett Wollman 				break;
1168cfe8b629SGarrett Wollman 			}
1169a163d034SWarner Losh 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
11700b17fba7SAndre Oppermann 			if (m == NULL) {
1171cfe8b629SGarrett Wollman 				error = ENOBUFS;
1172cfe8b629SGarrett Wollman 				break;
1173cfe8b629SGarrett Wollman 			}
1174cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1175cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1176cfe8b629SGarrett Wollman 					    m->m_len);
1177cfe8b629SGarrett Wollman 
1178cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1179cfe8b629SGarrett Wollman 					   m));
1180cfe8b629SGarrett Wollman 		}
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes 		case IP_TOS:
1183df8bae1dSRodney W. Grimes 		case IP_TTL:
1184df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1185df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1186df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
11874957466bSMatthew N. Dodd 		case IP_RECVTTL:
118882c23ebaSBill Fenner 		case IP_RECVIF:
11896a800098SYoshinobu Inoue 		case IP_FAITH:
11908afa2304SBruce M Simpson 		case IP_ONESBCAST:
1191cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1192cfe8b629SGarrett Wollman 					    sizeof optval);
1193cfe8b629SGarrett Wollman 			if (error)
1194cfe8b629SGarrett Wollman 				break;
1195df8bae1dSRodney W. Grimes 
1196cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1197df8bae1dSRodney W. Grimes 			case IP_TOS:
1198ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1199df8bae1dSRodney W. Grimes 				break;
1200df8bae1dSRodney W. Grimes 
1201df8bae1dSRodney W. Grimes 			case IP_TTL:
1202ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1203df8bae1dSRodney W. Grimes 				break;
1204a138d217SRobert Watson #define	OPTSET(bit) do {						\
1205a138d217SRobert Watson 	INP_LOCK(inp);							\
1206df8bae1dSRodney W. Grimes 	if (optval)							\
1207df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1208df8bae1dSRodney W. Grimes 	else								\
1209a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
1210a138d217SRobert Watson 	INP_UNLOCK(inp);						\
1211a138d217SRobert Watson } while (0)
1212df8bae1dSRodney W. Grimes 
1213df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1214df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1215df8bae1dSRodney W. Grimes 				break;
1216df8bae1dSRodney W. Grimes 
1217df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1218df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1219df8bae1dSRodney W. Grimes 				break;
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1222df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1223df8bae1dSRodney W. Grimes 				break;
122482c23ebaSBill Fenner 
12254957466bSMatthew N. Dodd 			case IP_RECVTTL:
12264957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
12274957466bSMatthew N. Dodd 				break;
12284957466bSMatthew N. Dodd 
122982c23ebaSBill Fenner 			case IP_RECVIF:
123082c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
123182c23ebaSBill Fenner 				break;
12326a800098SYoshinobu Inoue 
12336a800098SYoshinobu Inoue 			case IP_FAITH:
12346a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
12356a800098SYoshinobu Inoue 				break;
12368afa2304SBruce M Simpson 
12378afa2304SBruce M Simpson 			case IP_ONESBCAST:
12388afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
12398afa2304SBruce M Simpson 				break;
1240df8bae1dSRodney W. Grimes 			}
1241df8bae1dSRodney W. Grimes 			break;
1242df8bae1dSRodney W. Grimes #undef OPTSET
1243df8bae1dSRodney W. Grimes 
1244df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1245f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1246df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1247df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1248df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1249df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1250cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1251df8bae1dSRodney W. Grimes 			break;
1252df8bae1dSRodney W. Grimes 
125333b3ac06SPeter Wemm 		case IP_PORTRANGE:
1254cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1255cfe8b629SGarrett Wollman 					    sizeof optval);
1256cfe8b629SGarrett Wollman 			if (error)
1257cfe8b629SGarrett Wollman 				break;
125833b3ac06SPeter Wemm 
1259a138d217SRobert Watson 			INP_LOCK(inp);
126033b3ac06SPeter Wemm 			switch (optval) {
126133b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
126233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
126333b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
126433b3ac06SPeter Wemm 				break;
126533b3ac06SPeter Wemm 
126633b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
126733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
126833b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
126933b3ac06SPeter Wemm 				break;
127033b3ac06SPeter Wemm 
127133b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
127233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
127333b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
127433b3ac06SPeter Wemm 				break;
127533b3ac06SPeter Wemm 
127633b3ac06SPeter Wemm 			default:
127733b3ac06SPeter Wemm 				error = EINVAL;
127833b3ac06SPeter Wemm 				break;
127933b3ac06SPeter Wemm 			}
1280a138d217SRobert Watson 			INP_UNLOCK(inp);
1281ce8c72b1SPeter Wemm 			break;
128233b3ac06SPeter Wemm 
1283b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
12846a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12856a800098SYoshinobu Inoue 		{
12866a800098SYoshinobu Inoue 			caddr_t req;
1287686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
12886a800098SYoshinobu Inoue 			int priv;
12896a800098SYoshinobu Inoue 			struct mbuf *m;
12906a800098SYoshinobu Inoue 			int optname;
12916a800098SYoshinobu Inoue 
12926a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
12936a800098SYoshinobu Inoue 				break;
12946a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12956a800098SYoshinobu Inoue 				break;
1296b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
129744731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
12986a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1299686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
13006a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1301686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
13026a800098SYoshinobu Inoue 			m_freem(m);
13036a800098SYoshinobu Inoue 			break;
13046a800098SYoshinobu Inoue 		}
13056a800098SYoshinobu Inoue #endif /*IPSEC*/
13066a800098SYoshinobu Inoue 
1307df8bae1dSRodney W. Grimes 		default:
1308df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1309df8bae1dSRodney W. Grimes 			break;
1310df8bae1dSRodney W. Grimes 		}
1311df8bae1dSRodney W. Grimes 		break;
1312df8bae1dSRodney W. Grimes 
1313cfe8b629SGarrett Wollman 	case SOPT_GET:
1314cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1315df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1316df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1317cfe8b629SGarrett Wollman 			if (inp->inp_options)
1318cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1319cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1320cfe8b629SGarrett Wollman 							  char *),
1321cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1322cfe8b629SGarrett Wollman 			else
1323cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1324df8bae1dSRodney W. Grimes 			break;
1325df8bae1dSRodney W. Grimes 
1326df8bae1dSRodney W. Grimes 		case IP_TOS:
1327df8bae1dSRodney W. Grimes 		case IP_TTL:
1328df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1329df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1330df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
13314957466bSMatthew N. Dodd 		case IP_RECVTTL:
133282c23ebaSBill Fenner 		case IP_RECVIF:
1333cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
13346a800098SYoshinobu Inoue 		case IP_FAITH:
13358afa2304SBruce M Simpson 		case IP_ONESBCAST:
1336cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1337df8bae1dSRodney W. Grimes 
1338df8bae1dSRodney W. Grimes 			case IP_TOS:
1339ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1340df8bae1dSRodney W. Grimes 				break;
1341df8bae1dSRodney W. Grimes 
1342df8bae1dSRodney W. Grimes 			case IP_TTL:
1343ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1344df8bae1dSRodney W. Grimes 				break;
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1347df8bae1dSRodney W. Grimes 
1348df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1349df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1350df8bae1dSRodney W. Grimes 				break;
1351df8bae1dSRodney W. Grimes 
1352df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1353df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1354df8bae1dSRodney W. Grimes 				break;
1355df8bae1dSRodney W. Grimes 
1356df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1357df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1358df8bae1dSRodney W. Grimes 				break;
135982c23ebaSBill Fenner 
13604957466bSMatthew N. Dodd 			case IP_RECVTTL:
13614957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
13624957466bSMatthew N. Dodd 				break;
13634957466bSMatthew N. Dodd 
136482c23ebaSBill Fenner 			case IP_RECVIF:
136582c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
136682c23ebaSBill Fenner 				break;
1367cfe8b629SGarrett Wollman 
1368cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1369cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1370cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1371cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1372cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1373cfe8b629SGarrett Wollman 				else
1374cfe8b629SGarrett Wollman 					optval = 0;
1375cfe8b629SGarrett Wollman 				break;
13766a800098SYoshinobu Inoue 
13776a800098SYoshinobu Inoue 			case IP_FAITH:
13786a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
13796a800098SYoshinobu Inoue 				break;
13808afa2304SBruce M Simpson 
13818afa2304SBruce M Simpson 			case IP_ONESBCAST:
13828afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
13838afa2304SBruce M Simpson 				break;
1384df8bae1dSRodney W. Grimes 			}
1385cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1386df8bae1dSRodney W. Grimes 			break;
1387df8bae1dSRodney W. Grimes 
1388df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1389f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1390df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1391df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1392df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1393df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1394cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
139533b3ac06SPeter Wemm 			break;
139633b3ac06SPeter Wemm 
1397b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
13986a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13996a800098SYoshinobu Inoue 		{
1400f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
14016a800098SYoshinobu Inoue 			caddr_t req = NULL;
1402686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
14036a800098SYoshinobu Inoue 
1404686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
14056a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1406686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1407686cdd19SJun-ichiro itojun Hagino 			}
1408686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
14096a800098SYoshinobu Inoue 			if (error == 0)
14106a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1411f63e7634SYoshinobu Inoue 			if (error == 0)
14126a800098SYoshinobu Inoue 				m_freem(m);
14136a800098SYoshinobu Inoue 			break;
14146a800098SYoshinobu Inoue 		}
14156a800098SYoshinobu Inoue #endif /*IPSEC*/
14166a800098SYoshinobu Inoue 
1417df8bae1dSRodney W. Grimes 		default:
1418df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1419df8bae1dSRodney W. Grimes 			break;
1420df8bae1dSRodney W. Grimes 		}
1421df8bae1dSRodney W. Grimes 		break;
1422df8bae1dSRodney W. Grimes 	}
1423df8bae1dSRodney W. Grimes 	return (error);
1424df8bae1dSRodney W. Grimes }
1425df8bae1dSRodney W. Grimes 
1426df8bae1dSRodney W. Grimes /*
1427df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1428df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1429df8bae1dSRodney W. Grimes  * with destination address if source routed.
1430df8bae1dSRodney W. Grimes  */
14310312fbe9SPoul-Henning Kamp static int
1432b375c9ecSLuigi Rizzo ip_pcbopts(optname, pcbopt, m)
1433b375c9ecSLuigi Rizzo 	int optname;
1434b375c9ecSLuigi Rizzo 	struct mbuf **pcbopt;
1435b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1436df8bae1dSRodney W. Grimes {
1437b375c9ecSLuigi Rizzo 	register int cnt, optlen;
1438b375c9ecSLuigi Rizzo 	register u_char *cp;
1439df8bae1dSRodney W. Grimes 	u_char opt;
1440df8bae1dSRodney W. Grimes 
1441df8bae1dSRodney W. Grimes 	/* turn off any old options */
1442df8bae1dSRodney W. Grimes 	if (*pcbopt)
1443df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1444df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1445df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1446df8bae1dSRodney W. Grimes 		/*
1447df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1448df8bae1dSRodney W. Grimes 		 */
1449df8bae1dSRodney W. Grimes 		if (m)
1450df8bae1dSRodney W. Grimes 			(void)m_free(m);
1451df8bae1dSRodney W. Grimes 		return (0);
1452df8bae1dSRodney W. Grimes 	}
1453df8bae1dSRodney W. Grimes 
14540c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1455df8bae1dSRodney W. Grimes 		goto bad;
1456df8bae1dSRodney W. Grimes 	/*
1457df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1458df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1459df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1460df8bae1dSRodney W. Grimes 	 */
1461df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1462df8bae1dSRodney W. Grimes 		goto bad;
1463df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1464df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1465df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1466212059bdSDag-Erling Smørgrav 	bcopy(mtod(m, void *), cp, (unsigned)cnt);
1467212059bdSDag-Erling Smørgrav 	bzero(mtod(m, void *), sizeof(struct in_addr));
1468df8bae1dSRodney W. Grimes 
1469df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1470df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1471df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1472df8bae1dSRodney W. Grimes 			break;
1473df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1474df8bae1dSRodney W. Grimes 			optlen = 1;
1475df8bae1dSRodney W. Grimes 		else {
1476707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1477707d00a3SJonathan Lemon 				goto bad;
1478df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1479707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1480df8bae1dSRodney W. Grimes 				goto bad;
1481df8bae1dSRodney W. Grimes 		}
1482df8bae1dSRodney W. Grimes 		switch (opt) {
1483df8bae1dSRodney W. Grimes 
1484df8bae1dSRodney W. Grimes 		default:
1485df8bae1dSRodney W. Grimes 			break;
1486df8bae1dSRodney W. Grimes 
1487df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1488df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1489df8bae1dSRodney W. Grimes 			/*
1490df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1491df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1492df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1493df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1494df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1495df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1496df8bae1dSRodney W. Grimes 			 */
1497df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1498df8bae1dSRodney W. Grimes 				goto bad;
1499df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1500df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1501df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1502df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1503df8bae1dSRodney W. Grimes 			/*
1504df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1505df8bae1dSRodney W. Grimes 			 */
1506df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1507df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1508df8bae1dSRodney W. Grimes 			/*
1509df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1510df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1511df8bae1dSRodney W. Grimes 			 */
1512212059bdSDag-Erling Smørgrav 			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1513212059bdSDag-Erling Smørgrav 			    &cp[IPOPT_OFFSET+1],
1514a49b2137SMaxim Konovalov 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
1515df8bae1dSRodney W. Grimes 			break;
1516df8bae1dSRodney W. Grimes 		}
1517df8bae1dSRodney W. Grimes 	}
1518df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1519df8bae1dSRodney W. Grimes 		goto bad;
1520df8bae1dSRodney W. Grimes 	*pcbopt = m;
1521df8bae1dSRodney W. Grimes 	return (0);
1522df8bae1dSRodney W. Grimes 
1523df8bae1dSRodney W. Grimes bad:
1524df8bae1dSRodney W. Grimes 	(void)m_free(m);
1525df8bae1dSRodney W. Grimes 	return (EINVAL);
1526df8bae1dSRodney W. Grimes }
1527df8bae1dSRodney W. Grimes 
1528df8bae1dSRodney W. Grimes /*
1529cfe8b629SGarrett Wollman  * XXX
1530cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1531cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1532cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1533cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1534cfe8b629SGarrett Wollman  */
153533841545SHajimu UMEMOTO 
153633841545SHajimu UMEMOTO /*
153733841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
153833841545SHajimu UMEMOTO  */
153933841545SHajimu UMEMOTO static struct ifnet *
1540b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp)
1541b375c9ecSLuigi Rizzo 	struct in_addr *a;
1542b375c9ecSLuigi Rizzo 	int *ifindexp;
154333841545SHajimu UMEMOTO {
154433841545SHajimu UMEMOTO 	int ifindex;
154533841545SHajimu UMEMOTO 	struct ifnet *ifp;
154633841545SHajimu UMEMOTO 
154733841545SHajimu UMEMOTO 	if (ifindexp)
154833841545SHajimu UMEMOTO 		*ifindexp = 0;
154933841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
155033841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
155133841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
155233841545SHajimu UMEMOTO 			return NULL;
1553f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
155433841545SHajimu UMEMOTO 		if (ifindexp)
155533841545SHajimu UMEMOTO 			*ifindexp = ifindex;
155633841545SHajimu UMEMOTO 	} else {
155733841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
155833841545SHajimu UMEMOTO 	}
155933841545SHajimu UMEMOTO 	return ifp;
156033841545SHajimu UMEMOTO }
156133841545SHajimu UMEMOTO 
1562cfe8b629SGarrett Wollman /*
1563df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1564df8bae1dSRodney W. Grimes  */
15650312fbe9SPoul-Henning Kamp static int
1566b375c9ecSLuigi Rizzo ip_setmoptions(sopt, imop)
1567b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1568b375c9ecSLuigi Rizzo 	struct ip_moptions **imop;
1569df8bae1dSRodney W. Grimes {
1570cfe8b629SGarrett Wollman 	int error = 0;
1571cfe8b629SGarrett Wollman 	int i;
1572df8bae1dSRodney W. Grimes 	struct in_addr addr;
1573cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1574cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1575cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1576df8bae1dSRodney W. Grimes 	struct route ro;
1577cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
157833841545SHajimu UMEMOTO 	int ifindex;
15799b626c29SGarrett Wollman 	int s;
1580df8bae1dSRodney W. Grimes 
1581df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1582df8bae1dSRodney W. Grimes 		/*
1583df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1584df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1585df8bae1dSRodney W. Grimes 		 */
1586df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1587a163d034SWarner Losh 		    M_WAITOK);
1588df8bae1dSRodney W. Grimes 
1589df8bae1dSRodney W. Grimes 		if (imo == NULL)
1590df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1591df8bae1dSRodney W. Grimes 		*imop = imo;
1592df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
159333841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
15941c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1595df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1596df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1597df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1598df8bae1dSRodney W. Grimes 	}
1599df8bae1dSRodney W. Grimes 
1600cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1601f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1602f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1603cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
16045e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
16055e9ae478SGarrett Wollman 			break;
16065e9ae478SGarrett Wollman 		}
1607cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1608cfe8b629SGarrett Wollman 		if (error)
1609f0068c4aSGarrett Wollman 			break;
16101c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1611f0068c4aSGarrett Wollman 			error = EINVAL;
1612f0068c4aSGarrett Wollman 			break;
1613f0068c4aSGarrett Wollman 		}
1614f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1615f0068c4aSGarrett Wollman 		break;
1616f0068c4aSGarrett Wollman 
1617df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1618df8bae1dSRodney W. Grimes 		/*
1619df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1620df8bae1dSRodney W. Grimes 		 */
1621cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1622cfe8b629SGarrett Wollman 		if (error)
1623df8bae1dSRodney W. Grimes 			break;
1624df8bae1dSRodney W. Grimes 		/*
1625df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1626df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1627df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1628df8bae1dSRodney W. Grimes 		 */
1629df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1630df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1631df8bae1dSRodney W. Grimes 			break;
1632df8bae1dSRodney W. Grimes 		}
1633df8bae1dSRodney W. Grimes 		/*
1634df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1635df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1636df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1637df8bae1dSRodney W. Grimes 		 */
163820e8807cSGarrett Wollman 		s = splimp();
163933841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1640df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1641fbc6ab00SBill Fenner 			splx(s);
1642df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1643df8bae1dSRodney W. Grimes 			break;
1644df8bae1dSRodney W. Grimes 		}
1645df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
164633841545SHajimu UMEMOTO 		if (ifindex)
164733841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
164833841545SHajimu UMEMOTO 		else
164933841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
16509b626c29SGarrett Wollman 		splx(s);
1651df8bae1dSRodney W. Grimes 		break;
1652df8bae1dSRodney W. Grimes 
1653df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1654df8bae1dSRodney W. Grimes 		/*
1655df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1656cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1657cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1658cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1659df8bae1dSRodney W. Grimes 		 */
1660cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1661cfe8b629SGarrett Wollman 			u_char ttl;
1662cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1663cfe8b629SGarrett Wollman 			if (error)
1664df8bae1dSRodney W. Grimes 				break;
1665cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1666cfe8b629SGarrett Wollman 		} else {
1667cfe8b629SGarrett Wollman 			u_int ttl;
1668cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1669cfe8b629SGarrett Wollman 					    sizeof ttl);
1670cfe8b629SGarrett Wollman 			if (error)
1671cfe8b629SGarrett Wollman 				break;
1672cfe8b629SGarrett Wollman 			if (ttl > 255)
1673cfe8b629SGarrett Wollman 				error = EINVAL;
1674cfe8b629SGarrett Wollman 			else
1675cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1676df8bae1dSRodney W. Grimes 		}
1677df8bae1dSRodney W. Grimes 		break;
1678df8bae1dSRodney W. Grimes 
1679df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1680df8bae1dSRodney W. Grimes 		/*
1681df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1682cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1683cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1684cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1685df8bae1dSRodney W. Grimes 		 */
1686cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1687cfe8b629SGarrett Wollman 			u_char loop;
1688cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1689cfe8b629SGarrett Wollman 			if (error)
1690df8bae1dSRodney W. Grimes 				break;
1691cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1692cfe8b629SGarrett Wollman 		} else {
1693cfe8b629SGarrett Wollman 			u_int loop;
1694cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1695cfe8b629SGarrett Wollman 					    sizeof loop);
1696cfe8b629SGarrett Wollman 			if (error)
1697cfe8b629SGarrett Wollman 				break;
1698cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1699df8bae1dSRodney W. Grimes 		}
1700df8bae1dSRodney W. Grimes 		break;
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1703df8bae1dSRodney W. Grimes 		/*
1704df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1705df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1706df8bae1dSRodney W. Grimes 		 */
1707cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1708cfe8b629SGarrett Wollman 		if (error)
1709df8bae1dSRodney W. Grimes 			break;
1710cfe8b629SGarrett Wollman 
1711cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1712df8bae1dSRodney W. Grimes 			error = EINVAL;
1713df8bae1dSRodney W. Grimes 			break;
1714df8bae1dSRodney W. Grimes 		}
171520e8807cSGarrett Wollman 		s = splimp();
1716df8bae1dSRodney W. Grimes 		/*
1717df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1718df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1719df8bae1dSRodney W. Grimes 		 */
1720cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
17211c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1722df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1723df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1724df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1725cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
172697d8d152SAndre Oppermann 			rtalloc_ign(&ro, RTF_CLONING);
1727df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1728df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
17299b626c29SGarrett Wollman 				splx(s);
1730df8bae1dSRodney W. Grimes 				break;
1731df8bae1dSRodney W. Grimes 			}
1732df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1733d1dd20beSSam Leffler 			RTFREE(ro.ro_rt);
1734df8bae1dSRodney W. Grimes 		}
1735df8bae1dSRodney W. Grimes 		else {
173633841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1737df8bae1dSRodney W. Grimes 		}
17389b626c29SGarrett Wollman 
1739df8bae1dSRodney W. Grimes 		/*
1740df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1741df8bae1dSRodney W. Grimes 		 * supports multicast.
1742df8bae1dSRodney W. Grimes 		 */
1743df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1744df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
17459b626c29SGarrett Wollman 			splx(s);
1746df8bae1dSRodney W. Grimes 			break;
1747df8bae1dSRodney W. Grimes 		}
1748df8bae1dSRodney W. Grimes 		/*
1749df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1750df8bae1dSRodney W. Grimes 		 * membership slots are full.
1751df8bae1dSRodney W. Grimes 		 */
1752df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1753df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1754df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1755cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1756df8bae1dSRodney W. Grimes 				break;
1757df8bae1dSRodney W. Grimes 		}
1758df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1759df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
17609b626c29SGarrett Wollman 			splx(s);
1761df8bae1dSRodney W. Grimes 			break;
1762df8bae1dSRodney W. Grimes 		}
1763df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1764df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
17659b626c29SGarrett Wollman 			splx(s);
1766df8bae1dSRodney W. Grimes 			break;
1767df8bae1dSRodney W. Grimes 		}
1768df8bae1dSRodney W. Grimes 		/*
1769df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1770df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1771df8bae1dSRodney W. Grimes 		 */
1772df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1773cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1774df8bae1dSRodney W. Grimes 			error = ENOBUFS;
17759b626c29SGarrett Wollman 			splx(s);
1776df8bae1dSRodney W. Grimes 			break;
1777df8bae1dSRodney W. Grimes 		}
1778df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
17799b626c29SGarrett Wollman 		splx(s);
1780df8bae1dSRodney W. Grimes 		break;
1781df8bae1dSRodney W. Grimes 
1782df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1783df8bae1dSRodney W. Grimes 		/*
1784df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1785df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1786df8bae1dSRodney W. Grimes 		 */
1787cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1788cfe8b629SGarrett Wollman 		if (error)
1789df8bae1dSRodney W. Grimes 			break;
1790cfe8b629SGarrett Wollman 
1791cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1792df8bae1dSRodney W. Grimes 			error = EINVAL;
1793df8bae1dSRodney W. Grimes 			break;
1794df8bae1dSRodney W. Grimes 		}
17959b626c29SGarrett Wollman 
179620e8807cSGarrett Wollman 		s = splimp();
1797df8bae1dSRodney W. Grimes 		/*
1798df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1799df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1800df8bae1dSRodney W. Grimes 		 */
1801cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1802df8bae1dSRodney W. Grimes 			ifp = NULL;
1803df8bae1dSRodney W. Grimes 		else {
180433841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1805df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1806df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
18079b626c29SGarrett Wollman 				splx(s);
1808df8bae1dSRodney W. Grimes 				break;
1809df8bae1dSRodney W. Grimes 			}
1810df8bae1dSRodney W. Grimes 		}
1811df8bae1dSRodney W. Grimes 		/*
1812df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1813df8bae1dSRodney W. Grimes 		 */
1814df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1815df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1816df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1817df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1818cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1819df8bae1dSRodney W. Grimes 				break;
1820df8bae1dSRodney W. Grimes 		}
1821df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1822df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
18239b626c29SGarrett Wollman 			splx(s);
1824df8bae1dSRodney W. Grimes 			break;
1825df8bae1dSRodney W. Grimes 		}
1826df8bae1dSRodney W. Grimes 		/*
1827df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1828df8bae1dSRodney W. Grimes 		 * membership points.
1829df8bae1dSRodney W. Grimes 		 */
1830df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1831df8bae1dSRodney W. Grimes 		/*
1832df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1833df8bae1dSRodney W. Grimes 		 */
1834df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1835df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1836df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
18379b626c29SGarrett Wollman 		splx(s);
1838df8bae1dSRodney W. Grimes 		break;
1839df8bae1dSRodney W. Grimes 
1840df8bae1dSRodney W. Grimes 	default:
1841df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1842df8bae1dSRodney W. Grimes 		break;
1843df8bae1dSRodney W. Grimes 	}
1844df8bae1dSRodney W. Grimes 
1845df8bae1dSRodney W. Grimes 	/*
1846df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1847df8bae1dSRodney W. Grimes 	 */
1848df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
18491c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1850df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1851df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1852df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1853df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1854df8bae1dSRodney W. Grimes 		*imop = NULL;
1855df8bae1dSRodney W. Grimes 	}
1856df8bae1dSRodney W. Grimes 
1857df8bae1dSRodney W. Grimes 	return (error);
1858df8bae1dSRodney W. Grimes }
1859df8bae1dSRodney W. Grimes 
1860df8bae1dSRodney W. Grimes /*
1861df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1862df8bae1dSRodney W. Grimes  */
18630312fbe9SPoul-Henning Kamp static int
1864b375c9ecSLuigi Rizzo ip_getmoptions(sopt, imo)
1865b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1866b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
1867df8bae1dSRodney W. Grimes {
1868cfe8b629SGarrett Wollman 	struct in_addr addr;
1869df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1870cfe8b629SGarrett Wollman 	int error, optval;
1871cfe8b629SGarrett Wollman 	u_char coptval;
1872df8bae1dSRodney W. Grimes 
1873cfe8b629SGarrett Wollman 	error = 0;
1874cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1875f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1876f0068c4aSGarrett Wollman 		if (imo != NULL)
1877cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1878f0068c4aSGarrett Wollman 		else
1879cfe8b629SGarrett Wollman 			optval = -1;
1880cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1881cfe8b629SGarrett Wollman 		break;
1882f0068c4aSGarrett Wollman 
1883df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1884df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1885cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
188633841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
188733841545SHajimu UMEMOTO 			/* return the value user has set */
188833841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
188933841545SHajimu UMEMOTO 		} else {
1890df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1891cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1892df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1893df8bae1dSRodney W. Grimes 		}
1894cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1895cfe8b629SGarrett Wollman 		break;
1896df8bae1dSRodney W. Grimes 
1897df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1898cfe8b629SGarrett Wollman 		if (imo == 0)
1899cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1900cfe8b629SGarrett Wollman 		else
1901cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
1902cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1903cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1904cfe8b629SGarrett Wollman 		else
1905cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1906cfe8b629SGarrett Wollman 		break;
1907df8bae1dSRodney W. Grimes 
1908df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1909cfe8b629SGarrett Wollman 		if (imo == 0)
1910cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1911cfe8b629SGarrett Wollman 		else
1912cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
1913cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1914cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1915cfe8b629SGarrett Wollman 		else
1916cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1917cfe8b629SGarrett Wollman 		break;
1918df8bae1dSRodney W. Grimes 
1919df8bae1dSRodney W. Grimes 	default:
1920cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1921cfe8b629SGarrett Wollman 		break;
1922df8bae1dSRodney W. Grimes 	}
1923cfe8b629SGarrett Wollman 	return (error);
1924df8bae1dSRodney W. Grimes }
1925df8bae1dSRodney W. Grimes 
1926df8bae1dSRodney W. Grimes /*
1927df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1928df8bae1dSRodney W. Grimes  */
1929df8bae1dSRodney W. Grimes void
1930b375c9ecSLuigi Rizzo ip_freemoptions(imo)
1931b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
1932df8bae1dSRodney W. Grimes {
1933b375c9ecSLuigi Rizzo 	register int i;
1934df8bae1dSRodney W. Grimes 
1935df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1936df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1937df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1938df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1939df8bae1dSRodney W. Grimes 	}
1940df8bae1dSRodney W. Grimes }
1941df8bae1dSRodney W. Grimes 
1942df8bae1dSRodney W. Grimes /*
1943df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1944df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1945df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1946f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1947f5fea3ddSPaul Traina  * replicating that code here.
1948df8bae1dSRodney W. Grimes  */
1949df8bae1dSRodney W. Grimes static void
1950b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen)
1951b375c9ecSLuigi Rizzo 	struct ifnet *ifp;
1952b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1953b375c9ecSLuigi Rizzo 	register struct sockaddr_in *dst;
1954b375c9ecSLuigi Rizzo 	int hlen;
1955df8bae1dSRodney W. Grimes {
1956b375c9ecSLuigi Rizzo 	register struct ip *ip;
1957df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1958df8bae1dSRodney W. Grimes 
1959b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
196086b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
196186b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1962df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1963390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1964390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1965390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1966390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1967390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1968390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1969390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1970390cdc6aSRuslan Ermilov 		}
1971df8bae1dSRodney W. Grimes 		/*
1972df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1973df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1974df8bae1dSRodney W. Grimes 		 */
1975df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1976fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1977fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1978df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
197986b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
19809c9137eaSGarrett Wollman 		/*
19819c9137eaSGarrett Wollman 		 * NB:
1982e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1983e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1984e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1985e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1986e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1987e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1988e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1989ed7509acSJulian Elischer 		 * of safety by using if_simloop().
19909c9137eaSGarrett Wollman 		 */
1991201c2527SJulian Elischer #if 1 /* XXX */
1992201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
19932b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1994201c2527SJulian Elischer 						dst->sin_family);
1995201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1996201c2527SJulian Elischer 		}
1997201c2527SJulian Elischer #endif
1998201c2527SJulian Elischer 
19999c9137eaSGarrett Wollman #ifdef notdef
2000e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
2001ed7509acSJulian Elischer 		ip_input(copym);
20029c9137eaSGarrett Wollman #else
200306a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
20049c9137eaSGarrett Wollman #endif
2005df8bae1dSRodney W. Grimes 	}
2006df8bae1dSRodney W. Grimes }
2007