xref: /freebsd/sys/netinet/ip_output.c (revision 993d9505d477e46a88872d0e6abf3ae01b683fcf)
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 *);
102993d9505SRobert Watson static int	ip_pcbopts(struct inpcb *, int, 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;
665d6a8d588SMax Laier 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
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;
674f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
6759b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
6769b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
677f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
678a9c92b54SAndre Oppermann 				m->m_pkthdr.rcvif = loif;
67920c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
68020c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
68120c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
682ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
68320c822f3SJonathan Lemon 			}
68420c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
68520c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
6869b932e9eSAndre Oppermann 
6879b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
6889b932e9eSAndre Oppermann 			goto done;
6899b932e9eSAndre Oppermann 		} else
690f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
6919b932e9eSAndre Oppermann 	}
6929b932e9eSAndre Oppermann 
6939b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
6949b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
6959b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
6969b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
697a9c92b54SAndre Oppermann 			m->m_pkthdr.rcvif = loif;
6989b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
6999b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
7009b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
7019b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
7029b932e9eSAndre Oppermann 		}
7039b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
7049b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
7059b932e9eSAndre Oppermann 
7069b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
707f9e354dfSJulian Elischer 		goto done;
708f9e354dfSJulian Elischer 	}
7099b932e9eSAndre Oppermann 	/* Or forward to some other address? */
7109b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7119b932e9eSAndre Oppermann 	if (fwd_tag) {
7129b932e9eSAndre Oppermann 		if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) {
7139b932e9eSAndre Oppermann 			dst = (struct sockaddr_in *)&ro->ro_dst;
7149b932e9eSAndre Oppermann 			bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
7159b932e9eSAndre Oppermann 			m->m_flags |= M_SKIP_FIREWALL;
7169b932e9eSAndre Oppermann 			m_tag_delete(m, fwd_tag);
7179b932e9eSAndre Oppermann 			goto again;
7189b932e9eSAndre Oppermann 		} else {
7199b932e9eSAndre Oppermann 			m_tag_delete(m, fwd_tag);
7209b932e9eSAndre Oppermann 			/* Continue. */
721f9e354dfSJulian Elischer 		}
722f9e354dfSJulian Elischer 	}
7239b932e9eSAndre Oppermann #endif
7242b25acc1SLuigi Rizzo 
725c21fd232SAndre Oppermann passout:
72651c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
72751c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
72851c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
72951c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
73051c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
73151c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
73251c8ec4aSRuslan Ermilov 			goto bad;
73351c8ec4aSRuslan Ermilov 		}
73451c8ec4aSRuslan Ermilov 	}
73551c8ec4aSRuslan Ermilov 
736206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
737206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
738db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
739db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
740db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
741db4f9cc7SJonathan Lemon 	}
742206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
743db4f9cc7SJonathan Lemon 
744e7319babSPoul-Henning Kamp 	/*
745db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
746db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
747df8bae1dSRodney W. Grimes 	 */
7482683ceb6SAndre Oppermann 	if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
7492683ceb6SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0))) {
750fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
751fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
752df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
75353be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
754df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
7555da9f8faSJosef Karthauser 
7565da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
75738c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
7585da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
7595da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
7605da9f8faSJosef Karthauser 		}
7615da9f8faSJosef Karthauser 
76233841545SHajimu UMEMOTO #ifdef IPSEC
76333841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
76433841545SHajimu UMEMOTO 		ipsec_delaux(m);
76533841545SHajimu UMEMOTO #endif
76633841545SHajimu UMEMOTO 
76753dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
7683390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
7693390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
7709d9edc56SMike Silbersack #endif
771df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
772df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
773df8bae1dSRodney W. Grimes 		goto done;
774df8bae1dSRodney W. Grimes 	}
7751e78ac21SJeffrey Hsu 
776df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
777df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
7783d1f141bSGarrett Wollman 		/*
7793d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
7803d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
7813d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
7823d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
7833d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
7843d1f141bSGarrett Wollman 		 */
7851e78ac21SJeffrey Hsu 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
7861e78ac21SJeffrey Hsu 		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
7873d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
7883d1f141bSGarrett Wollman 		}
789df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
790df8bae1dSRodney W. Grimes 		goto bad;
791df8bae1dSRodney W. Grimes 	}
7921e78ac21SJeffrey Hsu 
7931e78ac21SJeffrey Hsu 	/*
7941e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
7951e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
7961e78ac21SJeffrey Hsu 	 */
7971e78ac21SJeffrey Hsu 	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
7981e78ac21SJeffrey Hsu 	if (error)
799df8bae1dSRodney W. Grimes 		goto bad;
8001e78ac21SJeffrey Hsu 	for (; m; m = m0) {
801df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
802b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
80333841545SHajimu UMEMOTO #ifdef IPSEC
80433841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
80533841545SHajimu UMEMOTO 		ipsec_delaux(m);
80633841545SHajimu UMEMOTO #endif
80707203494SDaniel C. Sobral 		if (error == 0) {
808fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
80907203494SDaniel C. Sobral 			if (ia != NULL) {
810fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
811fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
81207203494SDaniel C. Sobral 			}
813fe937674SJosef Karthauser 
814df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
815df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
816fe937674SJosef Karthauser 		} else
817df8bae1dSRodney W. Grimes 			m_freem(m);
818df8bae1dSRodney W. Grimes 	}
819df8bae1dSRodney W. Grimes 
820df8bae1dSRodney W. Grimes 	if (error == 0)
821df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
8221e78ac21SJeffrey Hsu 
823df8bae1dSRodney W. Grimes done:
8246a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
8256a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
826ac9d7e26SMax Laier 	}
82702c1c707SAndre Oppermann #ifdef IPSEC
8286a800098SYoshinobu Inoue 	if (sp != NULL) {
8296a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
8306a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
8316a800098SYoshinobu Inoue 		key_freesp(sp);
8326a800098SYoshinobu Inoue 	}
8331e78ac21SJeffrey Hsu #endif
834b9234fafSSam Leffler #ifdef FAST_IPSEC
835b9234fafSSam Leffler 	if (sp != NULL)
836b9234fafSSam Leffler 		KEY_FREESP(&sp);
8371e78ac21SJeffrey Hsu #endif
838df8bae1dSRodney W. Grimes 	return (error);
839df8bae1dSRodney W. Grimes bad:
8403528d68fSBill Paul 	m_freem(m);
841df8bae1dSRodney W. Grimes 	goto done;
842df8bae1dSRodney W. Grimes }
843df8bae1dSRodney W. Grimes 
8441e78ac21SJeffrey Hsu /*
8451e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
8461e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
8471e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
8481e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
8491e78ac21SJeffrey Hsu  *
8501e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
8511e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
8521e78ac21SJeffrey Hsu  */
8531e78ac21SJeffrey Hsu int
8541e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
8551e78ac21SJeffrey Hsu 	    u_long if_hwassist_flags, int sw_csum)
8561e78ac21SJeffrey Hsu {
8571e78ac21SJeffrey Hsu 	int error = 0;
8581e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
8591e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
8601e78ac21SJeffrey Hsu 	int off;
8611e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
8621e78ac21SJeffrey Hsu 	int firstlen;
8631e78ac21SJeffrey Hsu 	struct mbuf **mnext;
8641e78ac21SJeffrey Hsu 	int nfrags;
8651e78ac21SJeffrey Hsu 
8661e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
8671e78ac21SJeffrey Hsu 		ipstat.ips_cantfrag++;
8681e78ac21SJeffrey Hsu 		return EMSGSIZE;
8691e78ac21SJeffrey Hsu 	}
8701e78ac21SJeffrey Hsu 
8711e78ac21SJeffrey Hsu 	/*
8721e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
8731e78ac21SJeffrey Hsu 	 */
8741e78ac21SJeffrey Hsu 	if (len < 8)
8751e78ac21SJeffrey Hsu 		return EMSGSIZE;
8761e78ac21SJeffrey Hsu 
8771e78ac21SJeffrey Hsu 	/*
8781e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
8791e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
8801e78ac21SJeffrey Hsu 	 */
8811e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
8821e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
8831e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
8841e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
8851e78ac21SJeffrey Hsu 	}
8861e78ac21SJeffrey Hsu 
8871e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
8881e78ac21SJeffrey Hsu 		/*
8891e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
8901e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
8911e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
8921e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
8931e78ac21SJeffrey Hsu 		 *
8941e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
8951e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
8961e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
8971e78ac21SJeffrey Hsu 		 */
8981e78ac21SJeffrey Hsu 		int newlen;
8991e78ac21SJeffrey Hsu 		struct mbuf *m;
9001e78ac21SJeffrey Hsu 
9011e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
9021e78ac21SJeffrey Hsu 			off += m->m_len;
9031e78ac21SJeffrey Hsu 
9041e78ac21SJeffrey Hsu 		/*
9051e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
9061e78ac21SJeffrey Hsu 		 * 8-byte boundary
9071e78ac21SJeffrey Hsu 		 */
9081e78ac21SJeffrey Hsu 		if (off < hlen)
9091e78ac21SJeffrey Hsu 			goto smart_frag_failure;
9101e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
9111e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
9121e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
9131e78ac21SJeffrey Hsu 			/* we failed, go back the default */
9141e78ac21SJeffrey Hsu smart_frag_failure:
9151e78ac21SJeffrey Hsu 			newlen = len;
9161e78ac21SJeffrey Hsu 			off = hlen + len;
9171e78ac21SJeffrey Hsu 		}
9181e78ac21SJeffrey Hsu 		len = newlen;
9191e78ac21SJeffrey Hsu 
9201e78ac21SJeffrey Hsu 	} else {
9211e78ac21SJeffrey Hsu 		off = hlen + len;
9221e78ac21SJeffrey Hsu 	}
9231e78ac21SJeffrey Hsu 
9241e78ac21SJeffrey Hsu 	firstlen = off - hlen;
9251e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
9261e78ac21SJeffrey Hsu 
9271e78ac21SJeffrey Hsu 	/*
9281e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
9291e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
9301e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
9311e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
9321e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
9331e78ac21SJeffrey Hsu 	 */
9341e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
9351e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
9361e78ac21SJeffrey Hsu 		struct mbuf *m;
9371e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
9381e78ac21SJeffrey Hsu 
9391e78ac21SJeffrey Hsu 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
9400b17fba7SAndre Oppermann 		if (m == NULL) {
9411e78ac21SJeffrey Hsu 			error = ENOBUFS;
9421e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
9431e78ac21SJeffrey Hsu 			goto done;
9441e78ac21SJeffrey Hsu 		}
9451e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
9461e78ac21SJeffrey Hsu 		/*
9471e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
9481e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
9491e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
9501e78ac21SJeffrey Hsu 		 */
9511e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
9521e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
9531e78ac21SJeffrey Hsu 		*mhip = *ip;
9541e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
9551e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9561e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
9571e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
9581e78ac21SJeffrey Hsu 		}
9591e78ac21SJeffrey Hsu 		m->m_len = mhlen;
9601e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
9611e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
9621e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
9631e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
9641e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
9651e78ac21SJeffrey Hsu 		} else
9661e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
9671e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
9681e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
9690b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
9701e78ac21SJeffrey Hsu 			m_free(m);
9711e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
9721e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
9731e78ac21SJeffrey Hsu 			goto done;
9741e78ac21SJeffrey Hsu 		}
9751e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
9761e78ac21SJeffrey Hsu 		m->m_pkthdr.rcvif = (struct ifnet *)0;
9771e78ac21SJeffrey Hsu #ifdef MAC
9781e78ac21SJeffrey Hsu 		mac_create_fragment(m0, m);
9791e78ac21SJeffrey Hsu #endif
9801e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
9811e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
9821e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
9831e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
9841e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
9851e78ac21SJeffrey Hsu 		*mnext = m;
9861e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
9871e78ac21SJeffrey Hsu 	}
9881e78ac21SJeffrey Hsu 	ipstat.ips_ofragments += nfrags;
9891e78ac21SJeffrey Hsu 
9901e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
9911e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
9921e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
9931e78ac21SJeffrey Hsu 
9941e78ac21SJeffrey Hsu 	/*
9951e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
9961e78ac21SJeffrey Hsu 	 * and updating header.
9971e78ac21SJeffrey Hsu 	 */
9981e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
9991e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
10001e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
10011e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
10021e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
10031e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
10041e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
10051e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
10061e78ac21SJeffrey Hsu 
10071e78ac21SJeffrey Hsu done:
10081e78ac21SJeffrey Hsu 	*m_frag = m0;
10091e78ac21SJeffrey Hsu 	return error;
10101e78ac21SJeffrey Hsu }
10111e78ac21SJeffrey Hsu 
10121c238475SJonathan Lemon void
1013db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1014db4f9cc7SJonathan Lemon {
1015db4f9cc7SJonathan Lemon 	struct ip *ip;
1016db4f9cc7SJonathan Lemon 	u_short csum, offset;
1017db4f9cc7SJonathan Lemon 
1018db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
101953be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
1020db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1021206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1022206a3274SRuslan Ermilov 		csum = 0xffff;
1023db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1024db4f9cc7SJonathan Lemon 
1025db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1026db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1027db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1028db4f9cc7SJonathan Lemon 		/*
1029db4f9cc7SJonathan Lemon 		 * XXX
1030db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1031db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1032db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1033db4f9cc7SJonathan Lemon 		 */
1034db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1035db4f9cc7SJonathan Lemon 	}
1036db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1037db4f9cc7SJonathan Lemon }
1038db4f9cc7SJonathan Lemon 
1039df8bae1dSRodney W. Grimes /*
1040df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1041df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1042df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1043072b9b24SPaul Traina  *
1044072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1045df8bae1dSRodney W. Grimes  */
1046df8bae1dSRodney W. Grimes static struct mbuf *
1047b375c9ecSLuigi Rizzo ip_insertoptions(m, opt, phlen)
1048b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1049b375c9ecSLuigi Rizzo 	struct mbuf *opt;
1050b375c9ecSLuigi Rizzo 	int *phlen;
1051df8bae1dSRodney W. Grimes {
1052b375c9ecSLuigi Rizzo 	register struct ipoption *p = mtod(opt, struct ipoption *);
1053df8bae1dSRodney W. Grimes 	struct mbuf *n;
1054b375c9ecSLuigi Rizzo 	register struct ip *ip = mtod(m, struct ip *);
1055df8bae1dSRodney W. Grimes 	unsigned optlen;
1056df8bae1dSRodney W. Grimes 
1057df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
10581e78ac21SJeffrey Hsu 	if (optlen + ip->ip_len > IP_MAXPACKET) {
1059cb7641e8SMaxim Konovalov 		*phlen = 0;
1060df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1061cb7641e8SMaxim Konovalov 	}
1062df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1063df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1064df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1065a163d034SWarner Losh 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
10660b17fba7SAndre Oppermann 		if (n == NULL) {
1067cb7641e8SMaxim Konovalov 			*phlen = 0;
1068df8bae1dSRodney W. Grimes 			return (m);
1069cb7641e8SMaxim Konovalov 		}
10705db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
10714ed84624SRobert Watson #ifdef MAC
10724ed84624SRobert Watson 		mac_create_mbuf_from_mbuf(m, n);
10734ed84624SRobert Watson #endif
1074df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1075df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1076df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1077df8bae1dSRodney W. Grimes 		n->m_next = m;
1078df8bae1dSRodney W. Grimes 		m = n;
1079df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1080df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1081212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1082df8bae1dSRodney W. Grimes 	} else {
1083df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1084df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1085df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1086212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1087df8bae1dSRodney W. Grimes 	}
1088df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
10890453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1090df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
109153be11f6SPoul-Henning Kamp 	ip->ip_v = IPVERSION;
109253be11f6SPoul-Henning Kamp 	ip->ip_hl = *phlen >> 2;
1093df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1094df8bae1dSRodney W. Grimes 	return (m);
1095df8bae1dSRodney W. Grimes }
1096df8bae1dSRodney W. Grimes 
1097df8bae1dSRodney W. Grimes /*
1098df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1099df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1100df8bae1dSRodney W. Grimes  */
1101beec8214SDarren Reed int
1102b375c9ecSLuigi Rizzo ip_optcopy(ip, jp)
1103b375c9ecSLuigi Rizzo 	struct ip *ip, *jp;
1104df8bae1dSRodney W. Grimes {
1105b375c9ecSLuigi Rizzo 	register u_char *cp, *dp;
1106df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1107df8bae1dSRodney W. Grimes 
1108df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1109df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
111053be11f6SPoul-Henning Kamp 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1111df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1112df8bae1dSRodney W. Grimes 		opt = cp[0];
1113df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1114df8bae1dSRodney W. Grimes 			break;
1115df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1116df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1117df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1118df8bae1dSRodney W. Grimes 			optlen = 1;
1119df8bae1dSRodney W. Grimes 			continue;
1120686cdd19SJun-ichiro itojun Hagino 		}
1121db40007dSAndrew R. Reiter 
1122db40007dSAndrew R. Reiter 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1123db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1124df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1125db40007dSAndrew R. Reiter 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1126db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1127db40007dSAndrew R. Reiter 
1128df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1129df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1130df8bae1dSRodney W. Grimes 			optlen = cnt;
1131df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
11320453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1133df8bae1dSRodney W. Grimes 			dp += optlen;
1134df8bae1dSRodney W. Grimes 		}
1135df8bae1dSRodney W. Grimes 	}
1136df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1137df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1138df8bae1dSRodney W. Grimes 	return (optlen);
1139df8bae1dSRodney W. Grimes }
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes /*
1142df8bae1dSRodney W. Grimes  * IP socket option processing.
1143df8bae1dSRodney W. Grimes  */
1144df8bae1dSRodney W. Grimes int
1145b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt)
1146b375c9ecSLuigi Rizzo 	struct socket *so;
1147b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1148df8bae1dSRodney W. Grimes {
1149cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1150cfe8b629SGarrett Wollman 	int	error, optval;
1151df8bae1dSRodney W. Grimes 
1152cfe8b629SGarrett Wollman 	error = optval = 0;
1153cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1154cfe8b629SGarrett Wollman 		return (EINVAL);
1155cfe8b629SGarrett Wollman 	}
1156df8bae1dSRodney W. Grimes 
1157cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1158cfe8b629SGarrett Wollman 	case SOPT_SET:
1159cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1160df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1161df8bae1dSRodney W. Grimes #ifdef notyet
1162df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1163df8bae1dSRodney W. Grimes #endif
1164cfe8b629SGarrett Wollman 		{
1165cfe8b629SGarrett Wollman 			struct mbuf *m;
1166cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1167cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1168cfe8b629SGarrett Wollman 				break;
1169cfe8b629SGarrett Wollman 			}
1170a163d034SWarner Losh 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
11710b17fba7SAndre Oppermann 			if (m == NULL) {
1172cfe8b629SGarrett Wollman 				error = ENOBUFS;
1173cfe8b629SGarrett Wollman 				break;
1174cfe8b629SGarrett Wollman 			}
1175cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1176cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1177cfe8b629SGarrett Wollman 					    m->m_len);
1178993d9505SRobert Watson 			INP_LOCK(inp);
1179993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
1180993d9505SRobert Watson 			INP_UNLOCK(inp);
1181993d9505SRobert Watson 			return (error);
1182cfe8b629SGarrett Wollman 		}
1183df8bae1dSRodney W. Grimes 
1184df8bae1dSRodney W. Grimes 		case IP_TOS:
1185df8bae1dSRodney W. Grimes 		case IP_TTL:
1186df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1187df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1188df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
11894957466bSMatthew N. Dodd 		case IP_RECVTTL:
119082c23ebaSBill Fenner 		case IP_RECVIF:
11916a800098SYoshinobu Inoue 		case IP_FAITH:
11928afa2304SBruce M Simpson 		case IP_ONESBCAST:
1193cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1194cfe8b629SGarrett Wollman 					    sizeof optval);
1195cfe8b629SGarrett Wollman 			if (error)
1196cfe8b629SGarrett Wollman 				break;
1197df8bae1dSRodney W. Grimes 
1198cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1199df8bae1dSRodney W. Grimes 			case IP_TOS:
1200ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1201df8bae1dSRodney W. Grimes 				break;
1202df8bae1dSRodney W. Grimes 
1203df8bae1dSRodney W. Grimes 			case IP_TTL:
1204ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1205df8bae1dSRodney W. Grimes 				break;
1206a138d217SRobert Watson #define	OPTSET(bit) do {						\
1207a138d217SRobert Watson 	INP_LOCK(inp);							\
1208df8bae1dSRodney W. Grimes 	if (optval)							\
1209df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1210df8bae1dSRodney W. Grimes 	else								\
1211a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
1212a138d217SRobert Watson 	INP_UNLOCK(inp);						\
1213a138d217SRobert Watson } while (0)
1214df8bae1dSRodney W. Grimes 
1215df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1216df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1217df8bae1dSRodney W. Grimes 				break;
1218df8bae1dSRodney W. Grimes 
1219df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1220df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1221df8bae1dSRodney W. Grimes 				break;
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1224df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1225df8bae1dSRodney W. Grimes 				break;
122682c23ebaSBill Fenner 
12274957466bSMatthew N. Dodd 			case IP_RECVTTL:
12284957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
12294957466bSMatthew N. Dodd 				break;
12304957466bSMatthew N. Dodd 
123182c23ebaSBill Fenner 			case IP_RECVIF:
123282c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
123382c23ebaSBill Fenner 				break;
12346a800098SYoshinobu Inoue 
12356a800098SYoshinobu Inoue 			case IP_FAITH:
12366a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
12376a800098SYoshinobu Inoue 				break;
12388afa2304SBruce M Simpson 
12398afa2304SBruce M Simpson 			case IP_ONESBCAST:
12408afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
12418afa2304SBruce M Simpson 				break;
1242df8bae1dSRodney W. Grimes 			}
1243df8bae1dSRodney W. Grimes 			break;
1244df8bae1dSRodney W. Grimes #undef OPTSET
1245df8bae1dSRodney W. Grimes 
1246df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1247f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1248df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1249df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1250df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1251df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1252cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1253df8bae1dSRodney W. Grimes 			break;
1254df8bae1dSRodney W. Grimes 
125533b3ac06SPeter Wemm 		case IP_PORTRANGE:
1256cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1257cfe8b629SGarrett Wollman 					    sizeof optval);
1258cfe8b629SGarrett Wollman 			if (error)
1259cfe8b629SGarrett Wollman 				break;
126033b3ac06SPeter Wemm 
1261a138d217SRobert Watson 			INP_LOCK(inp);
126233b3ac06SPeter Wemm 			switch (optval) {
126333b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
126433b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
126533b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
126633b3ac06SPeter Wemm 				break;
126733b3ac06SPeter Wemm 
126833b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
126933b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
127033b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
127133b3ac06SPeter Wemm 				break;
127233b3ac06SPeter Wemm 
127333b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
127433b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
127533b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
127633b3ac06SPeter Wemm 				break;
127733b3ac06SPeter Wemm 
127833b3ac06SPeter Wemm 			default:
127933b3ac06SPeter Wemm 				error = EINVAL;
128033b3ac06SPeter Wemm 				break;
128133b3ac06SPeter Wemm 			}
1282a138d217SRobert Watson 			INP_UNLOCK(inp);
1283ce8c72b1SPeter Wemm 			break;
128433b3ac06SPeter Wemm 
1285b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
12866a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12876a800098SYoshinobu Inoue 		{
12886a800098SYoshinobu Inoue 			caddr_t req;
1289686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
12906a800098SYoshinobu Inoue 			int priv;
12916a800098SYoshinobu Inoue 			struct mbuf *m;
12926a800098SYoshinobu Inoue 			int optname;
12936a800098SYoshinobu Inoue 
12946a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
12956a800098SYoshinobu Inoue 				break;
12966a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12976a800098SYoshinobu Inoue 				break;
1298b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
129944731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
13006a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1301686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
13026a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1303686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
13046a800098SYoshinobu Inoue 			m_freem(m);
13056a800098SYoshinobu Inoue 			break;
13066a800098SYoshinobu Inoue 		}
13076a800098SYoshinobu Inoue #endif /*IPSEC*/
13086a800098SYoshinobu Inoue 
1309df8bae1dSRodney W. Grimes 		default:
1310df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1311df8bae1dSRodney W. Grimes 			break;
1312df8bae1dSRodney W. Grimes 		}
1313df8bae1dSRodney W. Grimes 		break;
1314df8bae1dSRodney W. Grimes 
1315cfe8b629SGarrett Wollman 	case SOPT_GET:
1316cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1317df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1318df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1319cfe8b629SGarrett Wollman 			if (inp->inp_options)
1320cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1321cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1322cfe8b629SGarrett Wollman 							  char *),
1323cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1324cfe8b629SGarrett Wollman 			else
1325cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1326df8bae1dSRodney W. Grimes 			break;
1327df8bae1dSRodney W. Grimes 
1328df8bae1dSRodney W. Grimes 		case IP_TOS:
1329df8bae1dSRodney W. Grimes 		case IP_TTL:
1330df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1331df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1332df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
13334957466bSMatthew N. Dodd 		case IP_RECVTTL:
133482c23ebaSBill Fenner 		case IP_RECVIF:
1335cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
13366a800098SYoshinobu Inoue 		case IP_FAITH:
13378afa2304SBruce M Simpson 		case IP_ONESBCAST:
1338cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1339df8bae1dSRodney W. Grimes 
1340df8bae1dSRodney W. Grimes 			case IP_TOS:
1341ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1342df8bae1dSRodney W. Grimes 				break;
1343df8bae1dSRodney W. Grimes 
1344df8bae1dSRodney W. Grimes 			case IP_TTL:
1345ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1346df8bae1dSRodney W. Grimes 				break;
1347df8bae1dSRodney W. Grimes 
1348df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1351df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1352df8bae1dSRodney W. Grimes 				break;
1353df8bae1dSRodney W. Grimes 
1354df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1355df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1356df8bae1dSRodney W. Grimes 				break;
1357df8bae1dSRodney W. Grimes 
1358df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1359df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1360df8bae1dSRodney W. Grimes 				break;
136182c23ebaSBill Fenner 
13624957466bSMatthew N. Dodd 			case IP_RECVTTL:
13634957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
13644957466bSMatthew N. Dodd 				break;
13654957466bSMatthew N. Dodd 
136682c23ebaSBill Fenner 			case IP_RECVIF:
136782c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
136882c23ebaSBill Fenner 				break;
1369cfe8b629SGarrett Wollman 
1370cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1371cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1372cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1373cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1374cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1375cfe8b629SGarrett Wollman 				else
1376cfe8b629SGarrett Wollman 					optval = 0;
1377cfe8b629SGarrett Wollman 				break;
13786a800098SYoshinobu Inoue 
13796a800098SYoshinobu Inoue 			case IP_FAITH:
13806a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
13816a800098SYoshinobu Inoue 				break;
13828afa2304SBruce M Simpson 
13838afa2304SBruce M Simpson 			case IP_ONESBCAST:
13848afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
13858afa2304SBruce M Simpson 				break;
1386df8bae1dSRodney W. Grimes 			}
1387cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1388df8bae1dSRodney W. Grimes 			break;
1389df8bae1dSRodney W. Grimes 
1390df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1391f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1392df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1393df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1394df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1395df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1396cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
139733b3ac06SPeter Wemm 			break;
139833b3ac06SPeter Wemm 
1399b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
14006a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
14016a800098SYoshinobu Inoue 		{
1402f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
14036a800098SYoshinobu Inoue 			caddr_t req = NULL;
1404686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
14056a800098SYoshinobu Inoue 
1406686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
14076a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1408686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1409686cdd19SJun-ichiro itojun Hagino 			}
1410686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
14116a800098SYoshinobu Inoue 			if (error == 0)
14126a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1413f63e7634SYoshinobu Inoue 			if (error == 0)
14146a800098SYoshinobu Inoue 				m_freem(m);
14156a800098SYoshinobu Inoue 			break;
14166a800098SYoshinobu Inoue 		}
14176a800098SYoshinobu Inoue #endif /*IPSEC*/
14186a800098SYoshinobu Inoue 
1419df8bae1dSRodney W. Grimes 		default:
1420df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1421df8bae1dSRodney W. Grimes 			break;
1422df8bae1dSRodney W. Grimes 		}
1423df8bae1dSRodney W. Grimes 		break;
1424df8bae1dSRodney W. Grimes 	}
1425df8bae1dSRodney W. Grimes 	return (error);
1426df8bae1dSRodney W. Grimes }
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes /*
1429df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1430df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1431df8bae1dSRodney W. Grimes  * with destination address if source routed.
1432df8bae1dSRodney W. Grimes  */
14330312fbe9SPoul-Henning Kamp static int
1434993d9505SRobert Watson ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
1435df8bae1dSRodney W. Grimes {
1436b375c9ecSLuigi Rizzo 	register int cnt, optlen;
1437b375c9ecSLuigi Rizzo 	register u_char *cp;
1438993d9505SRobert Watson 	struct mbuf **pcbopt;
1439df8bae1dSRodney W. Grimes 	u_char opt;
1440df8bae1dSRodney W. Grimes 
1441993d9505SRobert Watson 	INP_LOCK_ASSERT(inp);
1442993d9505SRobert Watson 
1443993d9505SRobert Watson 	pcbopt = &inp->inp_options;
1444993d9505SRobert Watson 
1445df8bae1dSRodney W. Grimes 	/* turn off any old options */
1446df8bae1dSRodney W. Grimes 	if (*pcbopt)
1447df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1448df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1449993d9505SRobert Watson 	if (m == NULL || m->m_len == 0) {
1450df8bae1dSRodney W. Grimes 		/*
1451df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1452df8bae1dSRodney W. Grimes 		 */
1453993d9505SRobert Watson 		if (m != NULL)
1454df8bae1dSRodney W. Grimes 			(void)m_free(m);
1455df8bae1dSRodney W. Grimes 		return (0);
1456df8bae1dSRodney W. Grimes 	}
1457df8bae1dSRodney W. Grimes 
14580c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1459df8bae1dSRodney W. Grimes 		goto bad;
1460df8bae1dSRodney W. Grimes 	/*
1461df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1462df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1463df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1464df8bae1dSRodney W. Grimes 	 */
1465df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1466df8bae1dSRodney W. Grimes 		goto bad;
1467df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1468df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1469df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1470212059bdSDag-Erling Smørgrav 	bcopy(mtod(m, void *), cp, (unsigned)cnt);
1471212059bdSDag-Erling Smørgrav 	bzero(mtod(m, void *), sizeof(struct in_addr));
1472df8bae1dSRodney W. Grimes 
1473df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1474df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1475df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1476df8bae1dSRodney W. Grimes 			break;
1477df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1478df8bae1dSRodney W. Grimes 			optlen = 1;
1479df8bae1dSRodney W. Grimes 		else {
1480707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1481707d00a3SJonathan Lemon 				goto bad;
1482df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1483707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1484df8bae1dSRodney W. Grimes 				goto bad;
1485df8bae1dSRodney W. Grimes 		}
1486df8bae1dSRodney W. Grimes 		switch (opt) {
1487df8bae1dSRodney W. Grimes 
1488df8bae1dSRodney W. Grimes 		default:
1489df8bae1dSRodney W. Grimes 			break;
1490df8bae1dSRodney W. Grimes 
1491df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1492df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1493df8bae1dSRodney W. Grimes 			/*
1494df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1495df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1496df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1497df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1498df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1499df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1500df8bae1dSRodney W. Grimes 			 */
1501df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1502df8bae1dSRodney W. Grimes 				goto bad;
1503df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1504df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1505df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1506df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1507df8bae1dSRodney W. Grimes 			/*
1508df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1509df8bae1dSRodney W. Grimes 			 */
1510df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1511df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1512df8bae1dSRodney W. Grimes 			/*
1513df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1514df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1515df8bae1dSRodney W. Grimes 			 */
1516212059bdSDag-Erling Smørgrav 			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1517212059bdSDag-Erling Smørgrav 			    &cp[IPOPT_OFFSET+1],
1518a49b2137SMaxim Konovalov 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
1519df8bae1dSRodney W. Grimes 			break;
1520df8bae1dSRodney W. Grimes 		}
1521df8bae1dSRodney W. Grimes 	}
1522df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1523df8bae1dSRodney W. Grimes 		goto bad;
1524df8bae1dSRodney W. Grimes 	*pcbopt = m;
1525df8bae1dSRodney W. Grimes 	return (0);
1526df8bae1dSRodney W. Grimes 
1527df8bae1dSRodney W. Grimes bad:
1528df8bae1dSRodney W. Grimes 	(void)m_free(m);
1529df8bae1dSRodney W. Grimes 	return (EINVAL);
1530df8bae1dSRodney W. Grimes }
1531df8bae1dSRodney W. Grimes 
1532df8bae1dSRodney W. Grimes /*
1533cfe8b629SGarrett Wollman  * XXX
1534cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1535cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1536cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1537cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1538cfe8b629SGarrett Wollman  */
153933841545SHajimu UMEMOTO 
154033841545SHajimu UMEMOTO /*
154133841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
154233841545SHajimu UMEMOTO  */
154333841545SHajimu UMEMOTO static struct ifnet *
1544b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp)
1545b375c9ecSLuigi Rizzo 	struct in_addr *a;
1546b375c9ecSLuigi Rizzo 	int *ifindexp;
154733841545SHajimu UMEMOTO {
154833841545SHajimu UMEMOTO 	int ifindex;
154933841545SHajimu UMEMOTO 	struct ifnet *ifp;
155033841545SHajimu UMEMOTO 
155133841545SHajimu UMEMOTO 	if (ifindexp)
155233841545SHajimu UMEMOTO 		*ifindexp = 0;
155333841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
155433841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
155533841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
155633841545SHajimu UMEMOTO 			return NULL;
1557f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
155833841545SHajimu UMEMOTO 		if (ifindexp)
155933841545SHajimu UMEMOTO 			*ifindexp = ifindex;
156033841545SHajimu UMEMOTO 	} else {
156133841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
156233841545SHajimu UMEMOTO 	}
156333841545SHajimu UMEMOTO 	return ifp;
156433841545SHajimu UMEMOTO }
156533841545SHajimu UMEMOTO 
1566cfe8b629SGarrett Wollman /*
1567df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1568df8bae1dSRodney W. Grimes  */
15690312fbe9SPoul-Henning Kamp static int
1570b375c9ecSLuigi Rizzo ip_setmoptions(sopt, imop)
1571b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1572b375c9ecSLuigi Rizzo 	struct ip_moptions **imop;
1573df8bae1dSRodney W. Grimes {
1574cfe8b629SGarrett Wollman 	int error = 0;
1575cfe8b629SGarrett Wollman 	int i;
1576df8bae1dSRodney W. Grimes 	struct in_addr addr;
1577cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1578cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1579cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1580df8bae1dSRodney W. Grimes 	struct route ro;
1581cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
158233841545SHajimu UMEMOTO 	int ifindex;
15839b626c29SGarrett Wollman 	int s;
1584df8bae1dSRodney W. Grimes 
1585df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1586df8bae1dSRodney W. Grimes 		/*
1587df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1588df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1589df8bae1dSRodney W. Grimes 		 */
1590df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1591a163d034SWarner Losh 		    M_WAITOK);
1592df8bae1dSRodney W. Grimes 
1593df8bae1dSRodney W. Grimes 		if (imo == NULL)
1594df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1595df8bae1dSRodney W. Grimes 		*imop = imo;
1596df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
159733841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
15981c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1599df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1600df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1601df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1602df8bae1dSRodney W. Grimes 	}
1603df8bae1dSRodney W. Grimes 
1604cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1605f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1606f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1607cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
16085e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
16095e9ae478SGarrett Wollman 			break;
16105e9ae478SGarrett Wollman 		}
1611cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1612cfe8b629SGarrett Wollman 		if (error)
1613f0068c4aSGarrett Wollman 			break;
16141c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1615f0068c4aSGarrett Wollman 			error = EINVAL;
1616f0068c4aSGarrett Wollman 			break;
1617f0068c4aSGarrett Wollman 		}
1618f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1619f0068c4aSGarrett Wollman 		break;
1620f0068c4aSGarrett Wollman 
1621df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1622df8bae1dSRodney W. Grimes 		/*
1623df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1624df8bae1dSRodney W. Grimes 		 */
1625cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1626cfe8b629SGarrett Wollman 		if (error)
1627df8bae1dSRodney W. Grimes 			break;
1628df8bae1dSRodney W. Grimes 		/*
1629df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1630df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1631df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1632df8bae1dSRodney W. Grimes 		 */
1633df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1634df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1635df8bae1dSRodney W. Grimes 			break;
1636df8bae1dSRodney W. Grimes 		}
1637df8bae1dSRodney W. Grimes 		/*
1638df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1639df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1640df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1641df8bae1dSRodney W. Grimes 		 */
164220e8807cSGarrett Wollman 		s = splimp();
164333841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1644df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1645fbc6ab00SBill Fenner 			splx(s);
1646df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1647df8bae1dSRodney W. Grimes 			break;
1648df8bae1dSRodney W. Grimes 		}
1649df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
165033841545SHajimu UMEMOTO 		if (ifindex)
165133841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
165233841545SHajimu UMEMOTO 		else
165333841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
16549b626c29SGarrett Wollman 		splx(s);
1655df8bae1dSRodney W. Grimes 		break;
1656df8bae1dSRodney W. Grimes 
1657df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1658df8bae1dSRodney W. Grimes 		/*
1659df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1660cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1661cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1662cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1663df8bae1dSRodney W. Grimes 		 */
1664cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1665cfe8b629SGarrett Wollman 			u_char ttl;
1666cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1667cfe8b629SGarrett Wollman 			if (error)
1668df8bae1dSRodney W. Grimes 				break;
1669cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1670cfe8b629SGarrett Wollman 		} else {
1671cfe8b629SGarrett Wollman 			u_int ttl;
1672cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1673cfe8b629SGarrett Wollman 					    sizeof ttl);
1674cfe8b629SGarrett Wollman 			if (error)
1675cfe8b629SGarrett Wollman 				break;
1676cfe8b629SGarrett Wollman 			if (ttl > 255)
1677cfe8b629SGarrett Wollman 				error = EINVAL;
1678cfe8b629SGarrett Wollman 			else
1679cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1680df8bae1dSRodney W. Grimes 		}
1681df8bae1dSRodney W. Grimes 		break;
1682df8bae1dSRodney W. Grimes 
1683df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1684df8bae1dSRodney W. Grimes 		/*
1685df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1686cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1687cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1688cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1689df8bae1dSRodney W. Grimes 		 */
1690cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1691cfe8b629SGarrett Wollman 			u_char loop;
1692cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1693cfe8b629SGarrett Wollman 			if (error)
1694df8bae1dSRodney W. Grimes 				break;
1695cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1696cfe8b629SGarrett Wollman 		} else {
1697cfe8b629SGarrett Wollman 			u_int loop;
1698cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1699cfe8b629SGarrett Wollman 					    sizeof loop);
1700cfe8b629SGarrett Wollman 			if (error)
1701cfe8b629SGarrett Wollman 				break;
1702cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1703df8bae1dSRodney W. Grimes 		}
1704df8bae1dSRodney W. Grimes 		break;
1705df8bae1dSRodney W. Grimes 
1706df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1707df8bae1dSRodney W. Grimes 		/*
1708df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1709df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1710df8bae1dSRodney W. Grimes 		 */
1711cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1712cfe8b629SGarrett Wollman 		if (error)
1713df8bae1dSRodney W. Grimes 			break;
1714cfe8b629SGarrett Wollman 
1715cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1716df8bae1dSRodney W. Grimes 			error = EINVAL;
1717df8bae1dSRodney W. Grimes 			break;
1718df8bae1dSRodney W. Grimes 		}
171920e8807cSGarrett Wollman 		s = splimp();
1720df8bae1dSRodney W. Grimes 		/*
1721df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1722df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1723df8bae1dSRodney W. Grimes 		 */
1724cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
17251c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1726df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1727df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1728df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1729cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
173097d8d152SAndre Oppermann 			rtalloc_ign(&ro, RTF_CLONING);
1731df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1732df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
17339b626c29SGarrett Wollman 				splx(s);
1734df8bae1dSRodney W. Grimes 				break;
1735df8bae1dSRodney W. Grimes 			}
1736df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1737d1dd20beSSam Leffler 			RTFREE(ro.ro_rt);
1738df8bae1dSRodney W. Grimes 		}
1739df8bae1dSRodney W. Grimes 		else {
174033841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1741df8bae1dSRodney W. Grimes 		}
17429b626c29SGarrett Wollman 
1743df8bae1dSRodney W. Grimes 		/*
1744df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1745df8bae1dSRodney W. Grimes 		 * supports multicast.
1746df8bae1dSRodney W. Grimes 		 */
1747df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1748df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
17499b626c29SGarrett Wollman 			splx(s);
1750df8bae1dSRodney W. Grimes 			break;
1751df8bae1dSRodney W. Grimes 		}
1752df8bae1dSRodney W. Grimes 		/*
1753df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1754df8bae1dSRodney W. Grimes 		 * membership slots are full.
1755df8bae1dSRodney W. Grimes 		 */
1756df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1757df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1758df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1759cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1760df8bae1dSRodney W. Grimes 				break;
1761df8bae1dSRodney W. Grimes 		}
1762df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1763df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
17649b626c29SGarrett Wollman 			splx(s);
1765df8bae1dSRodney W. Grimes 			break;
1766df8bae1dSRodney W. Grimes 		}
1767df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1768df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
17699b626c29SGarrett Wollman 			splx(s);
1770df8bae1dSRodney W. Grimes 			break;
1771df8bae1dSRodney W. Grimes 		}
1772df8bae1dSRodney W. Grimes 		/*
1773df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1774df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1775df8bae1dSRodney W. Grimes 		 */
1776df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1777cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1778df8bae1dSRodney W. Grimes 			error = ENOBUFS;
17799b626c29SGarrett Wollman 			splx(s);
1780df8bae1dSRodney W. Grimes 			break;
1781df8bae1dSRodney W. Grimes 		}
1782df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
17839b626c29SGarrett Wollman 		splx(s);
1784df8bae1dSRodney W. Grimes 		break;
1785df8bae1dSRodney W. Grimes 
1786df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1787df8bae1dSRodney W. Grimes 		/*
1788df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1789df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1790df8bae1dSRodney W. Grimes 		 */
1791cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1792cfe8b629SGarrett Wollman 		if (error)
1793df8bae1dSRodney W. Grimes 			break;
1794cfe8b629SGarrett Wollman 
1795cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1796df8bae1dSRodney W. Grimes 			error = EINVAL;
1797df8bae1dSRodney W. Grimes 			break;
1798df8bae1dSRodney W. Grimes 		}
17999b626c29SGarrett Wollman 
180020e8807cSGarrett Wollman 		s = splimp();
1801df8bae1dSRodney W. Grimes 		/*
1802df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1803df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1804df8bae1dSRodney W. Grimes 		 */
1805cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1806df8bae1dSRodney W. Grimes 			ifp = NULL;
1807df8bae1dSRodney W. Grimes 		else {
180833841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1809df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1810df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
18119b626c29SGarrett Wollman 				splx(s);
1812df8bae1dSRodney W. Grimes 				break;
1813df8bae1dSRodney W. Grimes 			}
1814df8bae1dSRodney W. Grimes 		}
1815df8bae1dSRodney W. Grimes 		/*
1816df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1817df8bae1dSRodney W. Grimes 		 */
1818df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1819df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1820df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1821df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1822cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1823df8bae1dSRodney W. Grimes 				break;
1824df8bae1dSRodney W. Grimes 		}
1825df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1826df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
18279b626c29SGarrett Wollman 			splx(s);
1828df8bae1dSRodney W. Grimes 			break;
1829df8bae1dSRodney W. Grimes 		}
1830df8bae1dSRodney W. Grimes 		/*
1831df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1832df8bae1dSRodney W. Grimes 		 * membership points.
1833df8bae1dSRodney W. Grimes 		 */
1834df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1835df8bae1dSRodney W. Grimes 		/*
1836df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1837df8bae1dSRodney W. Grimes 		 */
1838df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1839df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1840df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
18419b626c29SGarrett Wollman 		splx(s);
1842df8bae1dSRodney W. Grimes 		break;
1843df8bae1dSRodney W. Grimes 
1844df8bae1dSRodney W. Grimes 	default:
1845df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1846df8bae1dSRodney W. Grimes 		break;
1847df8bae1dSRodney W. Grimes 	}
1848df8bae1dSRodney W. Grimes 
1849df8bae1dSRodney W. Grimes 	/*
1850df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1851df8bae1dSRodney W. Grimes 	 */
1852df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
18531c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1854df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1855df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1856df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1857df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1858df8bae1dSRodney W. Grimes 		*imop = NULL;
1859df8bae1dSRodney W. Grimes 	}
1860df8bae1dSRodney W. Grimes 
1861df8bae1dSRodney W. Grimes 	return (error);
1862df8bae1dSRodney W. Grimes }
1863df8bae1dSRodney W. Grimes 
1864df8bae1dSRodney W. Grimes /*
1865df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1866df8bae1dSRodney W. Grimes  */
18670312fbe9SPoul-Henning Kamp static int
1868b375c9ecSLuigi Rizzo ip_getmoptions(sopt, imo)
1869b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1870b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
1871df8bae1dSRodney W. Grimes {
1872cfe8b629SGarrett Wollman 	struct in_addr addr;
1873df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1874cfe8b629SGarrett Wollman 	int error, optval;
1875cfe8b629SGarrett Wollman 	u_char coptval;
1876df8bae1dSRodney W. Grimes 
1877cfe8b629SGarrett Wollman 	error = 0;
1878cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1879f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1880f0068c4aSGarrett Wollman 		if (imo != NULL)
1881cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1882f0068c4aSGarrett Wollman 		else
1883cfe8b629SGarrett Wollman 			optval = -1;
1884cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1885cfe8b629SGarrett Wollman 		break;
1886f0068c4aSGarrett Wollman 
1887df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1888df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1889cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
189033841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
189133841545SHajimu UMEMOTO 			/* return the value user has set */
189233841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
189333841545SHajimu UMEMOTO 		} else {
1894df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1895cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1896df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1897df8bae1dSRodney W. Grimes 		}
1898cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1899cfe8b629SGarrett Wollman 		break;
1900df8bae1dSRodney W. Grimes 
1901df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1902cfe8b629SGarrett Wollman 		if (imo == 0)
1903cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1904cfe8b629SGarrett Wollman 		else
1905cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
1906cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1907cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1908cfe8b629SGarrett Wollman 		else
1909cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1910cfe8b629SGarrett Wollman 		break;
1911df8bae1dSRodney W. Grimes 
1912df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1913cfe8b629SGarrett Wollman 		if (imo == 0)
1914cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1915cfe8b629SGarrett Wollman 		else
1916cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
1917cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1918cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1919cfe8b629SGarrett Wollman 		else
1920cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1921cfe8b629SGarrett Wollman 		break;
1922df8bae1dSRodney W. Grimes 
1923df8bae1dSRodney W. Grimes 	default:
1924cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1925cfe8b629SGarrett Wollman 		break;
1926df8bae1dSRodney W. Grimes 	}
1927cfe8b629SGarrett Wollman 	return (error);
1928df8bae1dSRodney W. Grimes }
1929df8bae1dSRodney W. Grimes 
1930df8bae1dSRodney W. Grimes /*
1931df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1932df8bae1dSRodney W. Grimes  */
1933df8bae1dSRodney W. Grimes void
1934b375c9ecSLuigi Rizzo ip_freemoptions(imo)
1935b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
1936df8bae1dSRodney W. Grimes {
1937b375c9ecSLuigi Rizzo 	register int i;
1938df8bae1dSRodney W. Grimes 
1939df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1940df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1941df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1942df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1943df8bae1dSRodney W. Grimes 	}
1944df8bae1dSRodney W. Grimes }
1945df8bae1dSRodney W. Grimes 
1946df8bae1dSRodney W. Grimes /*
1947df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1948df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1949df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1950f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1951f5fea3ddSPaul Traina  * replicating that code here.
1952df8bae1dSRodney W. Grimes  */
1953df8bae1dSRodney W. Grimes static void
1954b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen)
1955b375c9ecSLuigi Rizzo 	struct ifnet *ifp;
1956b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1957b375c9ecSLuigi Rizzo 	register struct sockaddr_in *dst;
1958b375c9ecSLuigi Rizzo 	int hlen;
1959df8bae1dSRodney W. Grimes {
1960b375c9ecSLuigi Rizzo 	register struct ip *ip;
1961df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1962df8bae1dSRodney W. Grimes 
1963b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
196486b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
196586b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1966df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1967390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1968390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1969390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1970390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1971390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1972390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1973390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1974390cdc6aSRuslan Ermilov 		}
1975df8bae1dSRodney W. Grimes 		/*
1976df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1977df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1978df8bae1dSRodney W. Grimes 		 */
1979df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1980fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1981fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1982df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
198386b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
19849c9137eaSGarrett Wollman 		/*
19859c9137eaSGarrett Wollman 		 * NB:
1986e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1987e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1988e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1989e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1990e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1991e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1992e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1993ed7509acSJulian Elischer 		 * of safety by using if_simloop().
19949c9137eaSGarrett Wollman 		 */
1995201c2527SJulian Elischer #if 1 /* XXX */
1996201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
19972b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1998201c2527SJulian Elischer 						dst->sin_family);
1999201c2527SJulian Elischer 			dst->sin_family = AF_INET;
2000201c2527SJulian Elischer 		}
2001201c2527SJulian Elischer #endif
2002201c2527SJulian Elischer 
20039c9137eaSGarrett Wollman #ifdef notdef
2004e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
2005ed7509acSJulian Elischer 		ip_input(copym);
20069c9137eaSGarrett Wollman #else
200706a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
20089c9137eaSGarrett Wollman #endif
2009df8bae1dSRodney W. Grimes 	}
2010df8bae1dSRodney W. Grimes }
2011