xref: /freebsd/sys/netinet/ip_output.c (revision ef39adf007d3a9125dc4a03ece5a586c1519e8b8)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
331f7e052cSJulian Elischer #include "opt_ipfw.h"
346a800098SYoshinobu Inoue #include "opt_ipsec.h"
354ed84624SRobert Watson #include "opt_mac.h"
3653dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
37fbd1372aSJoerg Wunsch 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
3926f9a767SRodney W. Grimes #include <sys/systm.h>
40f0f6d643SLuigi Rizzo #include <sys/kernel.h>
414ed84624SRobert Watson #include <sys/mac.h>
42df8bae1dSRodney W. Grimes #include <sys/malloc.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44df8bae1dSRodney W. Grimes #include <sys/protosw.h>
45df8bae1dSRodney W. Grimes #include <sys/socket.h>
46df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
479d9edc56SMike Silbersack #include <sys/sysctl.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <net/if.h>
509b932e9eSAndre Oppermann #include <net/netisr.h>
51c21fd232SAndre Oppermann #include <net/pfil.h>
52df8bae1dSRodney W. Grimes #include <net/route.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <netinet/in.h>
55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
56df8bae1dSRodney W. Grimes #include <netinet/ip.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
60ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
61df8bae1dSRodney W. Grimes 
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 ifnet *ip_multicast_if(struct in_addr *, int *);
97df8bae1dSRodney W. Grimes static void	ip_mloopback
984d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
9989924e58SRobert Watson static int	ip_getmoptions(struct inpcb *, struct sockopt *);
1005c918b56SRobert Watson static int	ip_setmoptions(struct inpcb *, struct sockopt *);
101afed1b49SDarren Reed 
102afed1b49SDarren Reed 
10393e0e116SJulian Elischer extern	struct protosw inetsw[];
10493e0e116SJulian Elischer 
105df8bae1dSRodney W. Grimes /*
106df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
107df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
108df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
109df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1103de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1113de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
112df8bae1dSRodney W. Grimes  */
113df8bae1dSRodney W. Grimes int
114ac9d7e26SMax Laier ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
1151e78ac21SJeffrey Hsu 	int flags, struct ip_moptions *imo, struct inpcb *inp)
116df8bae1dSRodney W. Grimes {
1171e78ac21SJeffrey Hsu 	struct ip *ip;
1182b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
119ac9d7e26SMax Laier 	struct mbuf *m0;
12023bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1219b932e9eSAndre Oppermann 	int len, error = 0;
1222b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1233956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
124db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
125e3f406b3SRuslan Ermilov 	struct route iproute;
1269b932e9eSAndre Oppermann 	struct in_addr odst;
1279b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1289b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
1299b932e9eSAndre Oppermann #endif
13002c1c707SAndre Oppermann #ifdef IPSEC
1316a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1326a800098SYoshinobu Inoue #endif
133b9234fafSSam Leffler #ifdef FAST_IPSEC
134b9234fafSSam Leffler 	struct secpolicy *sp = NULL;
135b9234fafSSam Leffler 	struct tdb_ident *tdbi;
1369b932e9eSAndre Oppermann 	struct m_tag *mtag;
137b9234fafSSam Leffler 	int s;
138b9234fafSSam Leffler #endif /* FAST_IPSEC */
13936e8826fSMax Laier 
140ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
14136e8826fSMax Laier 
14202c1c707SAndre Oppermann 	if (ro == NULL) {
14302c1c707SAndre Oppermann 		ro = &iproute;
14402c1c707SAndre Oppermann 		bzero(ro, sizeof (*ro));
14502c1c707SAndre Oppermann 	}
14602c1c707SAndre Oppermann 
14784843845SSam Leffler 	if (inp != NULL)
14884843845SSam Leffler 		INP_LOCK_ASSERT(inp);
1492b25acc1SLuigi Rizzo 
150df8bae1dSRodney W. Grimes 	if (opt) {
151cb7641e8SMaxim Konovalov 		len = 0;
152df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
153cb7641e8SMaxim Konovalov 		if (len != 0)
154df8bae1dSRodney W. Grimes 			hlen = len;
155df8bae1dSRodney W. Grimes 	}
156df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1573efc3014SJulian Elischer 
158df8bae1dSRodney W. Grimes 	/*
1596e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
160e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
161e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
162e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
163e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1646e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1656e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1666e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1676e49b1feSGarrett Wollman 	 * <http://www.research.att.com/~smb/papers/fnat.pdf>.
168df8bae1dSRodney W. Grimes 	 */
169df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
17053be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
17153be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1721f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
173df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
174df8bae1dSRodney W. Grimes 	} else {
17553be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
176df8bae1dSRodney W. Grimes 	}
1779c9137eaSGarrett Wollman 
178df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
1799b932e9eSAndre Oppermann again:
180df8bae1dSRodney W. Grimes 	/*
181df8bae1dSRodney W. Grimes 	 * If there is a cached route,
182df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
183df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
184a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
185a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
186df8bae1dSRodney W. Grimes 	 */
187df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
188a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
1899b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
190df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
191df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
192df8bae1dSRodney W. Grimes 	}
1939b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1949b932e9eSAndre Oppermann 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
1959b932e9eSAndre Oppermann #else
1960b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
1979b932e9eSAndre Oppermann #endif
198a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
199df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
200df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2019b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
202df8bae1dSRodney W. Grimes 	}
203df8bae1dSRodney W. Grimes 	/*
204df8bae1dSRodney W. Grimes 	 * If routing to interface only,
205df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
206df8bae1dSRodney W. Grimes 	 */
207df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
2080b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2090b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
210df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
211df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
212df8bae1dSRodney W. Grimes 			goto bad;
213df8bae1dSRodney W. Grimes 		}
214df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
215df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2169f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2173afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
21838c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2193afefa39SDaniel C. Sobral 		/*
22038c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
22138c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2223afefa39SDaniel C. Sobral 		 */
22338c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
22438c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
22538c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
226df8bae1dSRodney W. Grimes 	} else {
2272c17fe93SGarrett Wollman 		/*
22897d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
22997d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
23097d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2312c17fe93SGarrett Wollman 		 */
2320b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
233e6e51f05SLuigi Rizzo 			rtalloc_ign(ro, 0);
2340b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
235df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
236df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
237df8bae1dSRodney W. Grimes 			goto bad;
238df8bae1dSRodney W. Grimes 		}
239df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
240df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
24197d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
242df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
243f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2449f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
245f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2469f9b3dc4SGarrett Wollman 		else
2479f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
248df8bae1dSRodney W. Grimes 	}
2499b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
250df8bae1dSRodney W. Grimes 		struct in_multi *inm;
251df8bae1dSRodney W. Grimes 
252df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
253df8bae1dSRodney W. Grimes 		/*
254df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
255df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
256df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
257df8bae1dSRodney W. Grimes 		 */
258df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
259df8bae1dSRodney W. Grimes 		/*
260df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
261df8bae1dSRodney W. Grimes 		 */
262df8bae1dSRodney W. Grimes 		if (imo != NULL) {
263df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
2641c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
2651c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
266bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
267bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
268bbb4330bSLuigi Rizzo 				    INADDR_ANY;
269df8bae1dSRodney W. Grimes 		} else
270df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
271df8bae1dSRodney W. Grimes 		/*
272df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
273df8bae1dSRodney W. Grimes 		 */
2741c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
275df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
276df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
277df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
278df8bae1dSRodney W. Grimes 				goto bad;
279df8bae1dSRodney W. Grimes 			}
2801c5de19aSGarrett Wollman 		}
281df8bae1dSRodney W. Grimes 		/*
282df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
283df8bae1dSRodney W. Grimes 		 * of outgoing interface.
284df8bae1dSRodney W. Grimes 		 */
285df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
28638c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
28738c1bc35SRuslan Ermilov 			if (ia != NULL)
28838c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
289df8bae1dSRodney W. Grimes 		}
290df8bae1dSRodney W. Grimes 
291dd5a318bSRobert Watson 		IN_MULTI_LOCK();
2929b932e9eSAndre Oppermann 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
293df8bae1dSRodney W. Grimes 		if (inm != NULL &&
294df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
295dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
296df8bae1dSRodney W. Grimes 			/*
297df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
298df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
299df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
300df8bae1dSRodney W. Grimes 			 */
30186b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
302df8bae1dSRodney W. Grimes 		}
303df8bae1dSRodney W. Grimes 		else {
304dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
305df8bae1dSRodney W. Grimes 			/*
306df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
307df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
308df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
309df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
310df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
311df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
312df8bae1dSRodney W. Grimes 			 *
313df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
314df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
315df8bae1dSRodney W. Grimes 			 * if necessary.
316df8bae1dSRodney W. Grimes 			 */
317df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
318f0068c4aSGarrett Wollman 				/*
319bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
320f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
321f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
322f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
323f0068c4aSGarrett Wollman 				 */
324b124e4f2SGarrett Wollman 				if (!rsvp_on)
325f0068c4aSGarrett Wollman 					imo = NULL;
326bbb4330bSLuigi Rizzo 				if (ip_mforward &&
327bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
328df8bae1dSRodney W. Grimes 					m_freem(m);
329df8bae1dSRodney W. Grimes 					goto done;
330df8bae1dSRodney W. Grimes 				}
331df8bae1dSRodney W. Grimes 			}
332df8bae1dSRodney W. Grimes 		}
3335e9ae478SGarrett Wollman 
334df8bae1dSRodney W. Grimes 		/*
335df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
336df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
337df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
338df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
339df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
340df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
341df8bae1dSRodney W. Grimes 		 */
342f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
343df8bae1dSRodney W. Grimes 			m_freem(m);
344df8bae1dSRodney W. Grimes 			goto done;
345df8bae1dSRodney W. Grimes 		}
346df8bae1dSRodney W. Grimes 
347df8bae1dSRodney W. Grimes 		goto sendit;
348df8bae1dSRodney W. Grimes 	}
349df8bae1dSRodney W. Grimes #ifndef notdef
350df8bae1dSRodney W. Grimes 	/*
3512b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
352cb459254SJohn-Mark Gurney 	 * of the outoing interface.
353df8bae1dSRodney W. Grimes 	 */
354f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
35538c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
35638c1bc35SRuslan Ermilov 		if (ia != NULL) {
357df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
358f9e354dfSJulian Elischer 		}
35938c1bc35SRuslan Ermilov 	}
360f9e354dfSJulian Elischer #endif /* notdef */
361ca7a789aSMax Laier 	/*
362ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
363ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
364ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
365ca7a789aSMax Laier 	 */
36602b199f1SMax Laier #ifdef ALTQ
367ca7a789aSMax Laier 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
368ca7a789aSMax Laier 	    ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
369ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen))
370ca7a789aSMax Laier #else
371b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
372ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen)
373ca7a789aSMax Laier #endif /* ALTQ */
374ca7a789aSMax Laier 	{
375b5390296SDavid Greenman 		error = ENOBUFS;
37635609d45SJonathan Lemon 		ipstat.ips_odropped++;
377b5390296SDavid Greenman 		goto bad;
378b5390296SDavid Greenman 	}
379b5390296SDavid Greenman 
380b5390296SDavid Greenman 	/*
381df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
3828c3f5566SRuslan Ermilov 	 * verify user is allowed to send
383df8bae1dSRodney W. Grimes 	 * such a packet.
384df8bae1dSRodney W. Grimes 	 */
3859f9b3dc4SGarrett Wollman 	if (isbroadcast) {
386df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
387df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
388df8bae1dSRodney W. Grimes 			goto bad;
389df8bae1dSRodney W. Grimes 		}
390df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
391df8bae1dSRodney W. Grimes 			error = EACCES;
392df8bae1dSRodney W. Grimes 			goto bad;
393df8bae1dSRodney W. Grimes 		}
394df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
3951e78ac21SJeffrey Hsu 		if (ip->ip_len > ifp->if_mtu) {
396df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
397df8bae1dSRodney W. Grimes 			goto bad;
398df8bae1dSRodney W. Grimes 		}
3998afa2304SBruce M Simpson 		if (flags & IP_SENDONES)
4008afa2304SBruce M Simpson 			ip->ip_dst.s_addr = INADDR_BROADCAST;
401df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4029f9b3dc4SGarrett Wollman 	} else {
403df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4049f9b3dc4SGarrett Wollman 	}
405df8bae1dSRodney W. Grimes 
406f1743588SDarren Reed sendit:
40733841545SHajimu UMEMOTO #ifdef IPSEC
40833841545SHajimu UMEMOTO 	/* get SP for this packet */
409f073c60fSHajimu UMEMOTO 	if (inp == NULL)
4100f9ade71SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
4110f9ade71SHajimu UMEMOTO 		    flags, &error);
41233841545SHajimu UMEMOTO 	else
413f073c60fSHajimu UMEMOTO 		sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
41433841545SHajimu UMEMOTO 
41533841545SHajimu UMEMOTO 	if (sp == NULL) {
41633841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
41733841545SHajimu UMEMOTO 		goto bad;
41833841545SHajimu UMEMOTO 	}
41933841545SHajimu UMEMOTO 
42033841545SHajimu UMEMOTO 	error = 0;
42133841545SHajimu UMEMOTO 
42233841545SHajimu UMEMOTO 	/* check policy */
42333841545SHajimu UMEMOTO 	switch (sp->policy) {
42433841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
42533841545SHajimu UMEMOTO 		/*
42633841545SHajimu UMEMOTO 		 * This packet is just discarded.
42733841545SHajimu UMEMOTO 		 */
42833841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
42933841545SHajimu UMEMOTO 		goto bad;
43033841545SHajimu UMEMOTO 
43133841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
43233841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
4331cfd4b53SBruce M Simpson 	case IPSEC_POLICY_TCP:
43433841545SHajimu UMEMOTO 		/* no need to do IPsec. */
43533841545SHajimu UMEMOTO 		goto skip_ipsec;
43633841545SHajimu UMEMOTO 
43733841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
43833841545SHajimu UMEMOTO 		if (sp->req == NULL) {
43933841545SHajimu UMEMOTO 			/* acquire a policy */
44033841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
44133841545SHajimu UMEMOTO 			goto bad;
44233841545SHajimu UMEMOTO 		}
44333841545SHajimu UMEMOTO 		break;
44433841545SHajimu UMEMOTO 
44533841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
44633841545SHajimu UMEMOTO 	default:
44733841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
44833841545SHajimu UMEMOTO 	}
44933841545SHajimu UMEMOTO     {
45033841545SHajimu UMEMOTO 	struct ipsec_output_state state;
45133841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
45233841545SHajimu UMEMOTO 	state.m = m;
45333841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
45433841545SHajimu UMEMOTO 		state.ro = &iproute;
45533841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
45633841545SHajimu UMEMOTO 	} else
45733841545SHajimu UMEMOTO 		state.ro = ro;
45833841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
45933841545SHajimu UMEMOTO 
46033841545SHajimu UMEMOTO 	ip->ip_sum = 0;
46133841545SHajimu UMEMOTO 
46233841545SHajimu UMEMOTO 	/*
46333841545SHajimu UMEMOTO 	 * XXX
46433841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
46533841545SHajimu UMEMOTO 	 */
46633841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
46733841545SHajimu UMEMOTO 		in_delayed_cksum(m);
46833841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
46933841545SHajimu UMEMOTO 	}
47033841545SHajimu UMEMOTO 
471fd8e4ebcSMike Barcroft 	ip->ip_len = htons(ip->ip_len);
472fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
47333841545SHajimu UMEMOTO 
47433841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
47533841545SHajimu UMEMOTO 
47633841545SHajimu UMEMOTO 	m = state.m;
47733841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
47833841545SHajimu UMEMOTO 		/*
47933841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
48033841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
48133841545SHajimu UMEMOTO 		 */
48233841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
48333841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
48433841545SHajimu UMEMOTO 			ro = state.ro;
48533841545SHajimu UMEMOTO 		}
48633841545SHajimu UMEMOTO 	} else
48733841545SHajimu UMEMOTO 		ro = state.ro;
48833841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
48933841545SHajimu UMEMOTO 	if (error) {
49033841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
491ac9d7e26SMax Laier 		m = NULL;
49233841545SHajimu UMEMOTO 		switch (error) {
49333841545SHajimu UMEMOTO 		case EHOSTUNREACH:
49433841545SHajimu UMEMOTO 		case ENETUNREACH:
49533841545SHajimu UMEMOTO 		case EMSGSIZE:
49633841545SHajimu UMEMOTO 		case ENOBUFS:
49733841545SHajimu UMEMOTO 		case ENOMEM:
49833841545SHajimu UMEMOTO 			break;
49933841545SHajimu UMEMOTO 		default:
50033841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
50133841545SHajimu UMEMOTO 			/*fall through*/
50233841545SHajimu UMEMOTO 		case ENOENT:
50333841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
50433841545SHajimu UMEMOTO 			error = 0;
50533841545SHajimu UMEMOTO 			break;
50633841545SHajimu UMEMOTO 		}
50733841545SHajimu UMEMOTO 		goto bad;
50833841545SHajimu UMEMOTO 	}
50933841545SHajimu UMEMOTO 
51033841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
51133841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
51233841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
51333841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
51433841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
51533841545SHajimu UMEMOTO 			printf("ip_output: "
51633841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
51733841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
51833841545SHajimu UMEMOTO 			goto bad;
51933841545SHajimu UMEMOTO 		}
52033841545SHajimu UMEMOTO 	} else {
52170dbc6cbSHajimu UMEMOTO 		if (state.encap) {
52233841545SHajimu UMEMOTO 			ia = ifatoia(ro->ro_rt->rt_ifa);
52333841545SHajimu UMEMOTO 			ifp = ro->ro_rt->rt_ifp;
52433841545SHajimu UMEMOTO 		}
52570dbc6cbSHajimu UMEMOTO 	}
52670dbc6cbSHajimu UMEMOTO     }
52733841545SHajimu UMEMOTO 
52833841545SHajimu UMEMOTO 	/* make it flipped, again. */
529fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
530fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
53133841545SHajimu UMEMOTO skip_ipsec:
53233841545SHajimu UMEMOTO #endif /*IPSEC*/
533b9234fafSSam Leffler #ifdef FAST_IPSEC
534b9234fafSSam Leffler 	/*
535b9234fafSSam Leffler 	 * Check the security policy (SP) for the packet and, if
536b9234fafSSam Leffler 	 * required, do IPsec-related processing.  There are two
537b9234fafSSam Leffler 	 * cases here; the first time a packet is sent through
538b9234fafSSam Leffler 	 * it will be untagged and handled by ipsec4_checkpolicy.
539b9234fafSSam Leffler 	 * If the packet is resubmitted to ip_output (e.g. after
540b9234fafSSam Leffler 	 * AH, ESP, etc. processing), there will be a tag to bypass
541b9234fafSSam Leffler 	 * the lookup and related policy checking.
542b9234fafSSam Leffler 	 */
543b9234fafSSam Leffler 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
544b9234fafSSam Leffler 	s = splnet();
545b9234fafSSam Leffler 	if (mtag != NULL) {
546b9234fafSSam Leffler 		tdbi = (struct tdb_ident *)(mtag + 1);
547b9234fafSSam Leffler 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
548b9234fafSSam Leffler 		if (sp == NULL)
549b9234fafSSam Leffler 			error = -EINVAL;	/* force silent drop */
550b9234fafSSam Leffler 		m_tag_delete(m, mtag);
551b9234fafSSam Leffler 	} else {
552b9234fafSSam Leffler 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
553b9234fafSSam Leffler 					&error, inp);
554b9234fafSSam Leffler 	}
555b9234fafSSam Leffler 	/*
556b9234fafSSam Leffler 	 * There are four return cases:
557b9234fafSSam Leffler 	 *    sp != NULL	 	    apply IPsec policy
558b9234fafSSam Leffler 	 *    sp == NULL, error == 0	    no IPsec handling needed
559b9234fafSSam Leffler 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
560b9234fafSSam Leffler 	 *    sp == NULL, error != 0	    discard packet, report error
561b9234fafSSam Leffler 	 */
562b9234fafSSam Leffler 	if (sp != NULL) {
563b9234fafSSam Leffler 		/* Loop detection, check if ipsec processing already done */
564b9234fafSSam Leffler 		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
565b9234fafSSam Leffler 		for (mtag = m_tag_first(m); mtag != NULL;
566b9234fafSSam Leffler 		     mtag = m_tag_next(m, mtag)) {
567b9234fafSSam Leffler 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
568b9234fafSSam Leffler 				continue;
569b9234fafSSam Leffler 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
570b9234fafSSam Leffler 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
571b9234fafSSam Leffler 				continue;
572b9234fafSSam Leffler 			/*
573b9234fafSSam Leffler 			 * Check if policy has an SA associated with it.
574b9234fafSSam Leffler 			 * This can happen when an SP has yet to acquire
575b9234fafSSam Leffler 			 * an SA; e.g. on first reference.  If it occurs,
576b9234fafSSam Leffler 			 * then we let ipsec4_process_packet do its thing.
577b9234fafSSam Leffler 			 */
578b9234fafSSam Leffler 			if (sp->req->sav == NULL)
579b9234fafSSam Leffler 				break;
580b9234fafSSam Leffler 			tdbi = (struct tdb_ident *)(mtag + 1);
581b9234fafSSam Leffler 			if (tdbi->spi == sp->req->sav->spi &&
582b9234fafSSam Leffler 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
583ab94ca3cSSam Leffler 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
584b9234fafSSam Leffler 				 sizeof (union sockaddr_union)) == 0) {
585b9234fafSSam Leffler 				/*
586b9234fafSSam Leffler 				 * No IPsec processing is needed, free
587b9234fafSSam Leffler 				 * reference to SP.
588b9234fafSSam Leffler 				 *
589b9234fafSSam Leffler 				 * NB: null pointer to avoid free at
590b9234fafSSam Leffler 				 *     done: below.
591b9234fafSSam Leffler 				 */
592b9234fafSSam Leffler 				KEY_FREESP(&sp), sp = NULL;
593b9234fafSSam Leffler 				splx(s);
594b9234fafSSam Leffler 				goto spd_done;
595b9234fafSSam Leffler 			}
596b9234fafSSam Leffler 		}
597b9234fafSSam Leffler 
598b9234fafSSam Leffler 		/*
599b9234fafSSam Leffler 		 * Do delayed checksums now because we send before
600b9234fafSSam Leffler 		 * this is done in the normal processing path.
601b9234fafSSam Leffler 		 */
602b9234fafSSam Leffler 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
603b9234fafSSam Leffler 			in_delayed_cksum(m);
604b9234fafSSam Leffler 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
605b9234fafSSam Leffler 		}
606b9234fafSSam Leffler 
607b9234fafSSam Leffler 		ip->ip_len = htons(ip->ip_len);
608b9234fafSSam Leffler 		ip->ip_off = htons(ip->ip_off);
609b9234fafSSam Leffler 
610b9234fafSSam Leffler 		/* NB: callee frees mbuf */
611b9234fafSSam Leffler 		error = ipsec4_process_packet(m, sp->req, flags, 0);
6129359ad86SSam Leffler 		/*
6139359ad86SSam Leffler 		 * Preserve KAME behaviour: ENOENT can be returned
6149359ad86SSam Leffler 		 * when an SA acquire is in progress.  Don't propagate
6159359ad86SSam Leffler 		 * this to user-level; it confuses applications.
6169359ad86SSam Leffler 		 *
6179359ad86SSam Leffler 		 * XXX this will go away when the SADB is redone.
6189359ad86SSam Leffler 		 */
6199359ad86SSam Leffler 		if (error == ENOENT)
6209359ad86SSam Leffler 			error = 0;
621b9234fafSSam Leffler 		splx(s);
622b9234fafSSam Leffler 		goto done;
623b9234fafSSam Leffler 	} else {
624b9234fafSSam Leffler 		splx(s);
625b9234fafSSam Leffler 
626b9234fafSSam Leffler 		if (error != 0) {
627b9234fafSSam Leffler 			/*
628b9234fafSSam Leffler 			 * Hack: -EINVAL is used to signal that a packet
629b9234fafSSam Leffler 			 * should be silently discarded.  This is typically
630b9234fafSSam Leffler 			 * because we asked key management for an SA and
631b9234fafSSam Leffler 			 * it was delayed (e.g. kicked up to IKE).
632b9234fafSSam Leffler 			 */
633b9234fafSSam Leffler 			if (error == -EINVAL)
634b9234fafSSam Leffler 				error = 0;
635b9234fafSSam Leffler 			goto bad;
636b9234fafSSam Leffler 		} else {
637b9234fafSSam Leffler 			/* No IPsec processing for this packet. */
638b9234fafSSam Leffler 		}
639b9234fafSSam Leffler #ifdef notyet
640b9234fafSSam Leffler 		/*
641b9234fafSSam Leffler 		 * If deferred crypto processing is needed, check that
642b9234fafSSam Leffler 		 * the interface supports it.
643b9234fafSSam Leffler 		 */
644b9234fafSSam Leffler 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
645b9234fafSSam Leffler 		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
646b9234fafSSam Leffler 			/* notify IPsec to do its own crypto */
647b9234fafSSam Leffler 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
648b9234fafSSam Leffler 			error = EHOSTUNREACH;
649b9234fafSSam Leffler 			goto bad;
650b9234fafSSam Leffler 		}
651b9234fafSSam Leffler #endif
652b9234fafSSam Leffler 	}
653b9234fafSSam Leffler spd_done:
654b9234fafSSam Leffler #endif /* FAST_IPSEC */
65533841545SHajimu UMEMOTO 
656c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
657c21fd232SAndre Oppermann 	if (inet_pfil_hook.ph_busy_count == -1)
658c21fd232SAndre Oppermann 		goto passout;
659c21fd232SAndre Oppermann 
660c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
6619b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
662d6a8d588SMax Laier 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
663134ea224SSam Leffler 	if (error != 0 || m == NULL)
664c4ac87eaSDarren Reed 		goto done;
6659b932e9eSAndre Oppermann 
666c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
667fed1c7e9SSøren Schmidt 
6689b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
6699b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
6709b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
671f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
6729b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
6739b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
674f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
675a9c92b54SAndre Oppermann 				m->m_pkthdr.rcvif = loif;
67620c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
67720c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
67820c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
679ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
68020c822f3SJonathan Lemon 			}
68120c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
68220c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
6839b932e9eSAndre Oppermann 
6849b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
6859b932e9eSAndre Oppermann 			goto done;
6869b932e9eSAndre Oppermann 		} else
687f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
6889b932e9eSAndre Oppermann 	}
6899b932e9eSAndre Oppermann 
6909b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
6919b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
6929b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
6939b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
694a9c92b54SAndre Oppermann 			m->m_pkthdr.rcvif = loif;
6959b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
6969b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
6979b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
6989b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
6999b932e9eSAndre Oppermann 		}
7009b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
7019b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
7029b932e9eSAndre Oppermann 
7039b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
704f9e354dfSJulian Elischer 		goto done;
705f9e354dfSJulian Elischer 	}
7069b932e9eSAndre Oppermann 	/* Or forward to some other address? */
7079b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
7089b932e9eSAndre Oppermann 	if (fwd_tag) {
709099dd043SAndre Oppermann #ifndef IPFIREWALL_FORWARD_EXTENDED
7109b932e9eSAndre Oppermann 		if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) {
711099dd043SAndre Oppermann #endif
7129b932e9eSAndre Oppermann 			dst = (struct sockaddr_in *)&ro->ro_dst;
7139b932e9eSAndre Oppermann 			bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
7149b932e9eSAndre Oppermann 			m->m_flags |= M_SKIP_FIREWALL;
7159b932e9eSAndre Oppermann 			m_tag_delete(m, fwd_tag);
7169b932e9eSAndre Oppermann 			goto again;
717099dd043SAndre Oppermann #ifndef IPFIREWALL_FORWARD_EXTENDED
7189b932e9eSAndre Oppermann 		} else {
7199b932e9eSAndre Oppermann 			m_tag_delete(m, fwd_tag);
7209b932e9eSAndre Oppermann 			/* Continue. */
721f9e354dfSJulian Elischer 		}
7229b932e9eSAndre Oppermann #endif
723099dd043SAndre Oppermann 	}
724099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */
7252b25acc1SLuigi Rizzo 
726c21fd232SAndre Oppermann passout:
72751c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
72851c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
72951c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
73051c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
73151c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
73251c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
73351c8ec4aSRuslan Ermilov 			goto bad;
73451c8ec4aSRuslan Ermilov 		}
73551c8ec4aSRuslan Ermilov 	}
73651c8ec4aSRuslan Ermilov 
737206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
738206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
739db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
740db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
741db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
742db4f9cc7SJonathan Lemon 	}
743206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
744db4f9cc7SJonathan Lemon 
745e7319babSPoul-Henning Kamp 	/*
746db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
747db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
748df8bae1dSRodney W. Grimes 	 */
7492683ceb6SAndre Oppermann 	if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
7502683ceb6SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0))) {
751fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
752fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
753df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
75453be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
755df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
7565da9f8faSJosef Karthauser 
7575da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
75838c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
7595da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
7605da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
7615da9f8faSJosef Karthauser 		}
7625da9f8faSJosef Karthauser 
76333841545SHajimu UMEMOTO #ifdef IPSEC
76433841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
76533841545SHajimu UMEMOTO 		ipsec_delaux(m);
76633841545SHajimu UMEMOTO #endif
76733841545SHajimu UMEMOTO 
76853dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
7693390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
7703390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
7719d9edc56SMike Silbersack #endif
772147f74d1SAndre Oppermann 		/*
773147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
774147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
775147f74d1SAndre Oppermann 		 */
776147f74d1SAndre Oppermann 		m->m_flags &= ~(M_PROTOFLAGS);
777147f74d1SAndre Oppermann 
778df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
779df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
780df8bae1dSRodney W. Grimes 		goto done;
781df8bae1dSRodney W. Grimes 	}
7821e78ac21SJeffrey Hsu 
783df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
784df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
7853d1f141bSGarrett Wollman 		/*
7863d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
7873d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
7883d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
7893d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
7903d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
7913d1f141bSGarrett Wollman 		 */
7921e78ac21SJeffrey Hsu 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
7931e78ac21SJeffrey Hsu 		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
7943d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
7953d1f141bSGarrett Wollman 		}
796df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
797df8bae1dSRodney W. Grimes 		goto bad;
798df8bae1dSRodney W. Grimes 	}
7991e78ac21SJeffrey Hsu 
8001e78ac21SJeffrey Hsu 	/*
8011e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
8021e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
8031e78ac21SJeffrey Hsu 	 */
8041e78ac21SJeffrey Hsu 	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
8051e78ac21SJeffrey Hsu 	if (error)
806df8bae1dSRodney W. Grimes 		goto bad;
8071e78ac21SJeffrey Hsu 	for (; m; m = m0) {
808df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
809b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
81033841545SHajimu UMEMOTO #ifdef IPSEC
81133841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
81233841545SHajimu UMEMOTO 		ipsec_delaux(m);
81333841545SHajimu UMEMOTO #endif
81407203494SDaniel C. Sobral 		if (error == 0) {
815fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
81607203494SDaniel C. Sobral 			if (ia != NULL) {
817fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
818fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
81907203494SDaniel C. Sobral 			}
820147f74d1SAndre Oppermann 			/*
821147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
822147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
823147f74d1SAndre Oppermann 			 */
824147f74d1SAndre Oppermann 			m->m_flags &= ~(M_PROTOFLAGS);
825fe937674SJosef Karthauser 
826df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
827df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
828fe937674SJosef Karthauser 		} else
829df8bae1dSRodney W. Grimes 			m_freem(m);
830df8bae1dSRodney W. Grimes 	}
831df8bae1dSRodney W. Grimes 
832df8bae1dSRodney W. Grimes 	if (error == 0)
833df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
8341e78ac21SJeffrey Hsu 
835df8bae1dSRodney W. Grimes done:
8366a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
8376a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
838ac9d7e26SMax Laier 	}
83902c1c707SAndre Oppermann #ifdef IPSEC
8406a800098SYoshinobu Inoue 	if (sp != NULL) {
8416a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
8426a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
8436a800098SYoshinobu Inoue 		key_freesp(sp);
8446a800098SYoshinobu Inoue 	}
8451e78ac21SJeffrey Hsu #endif
846b9234fafSSam Leffler #ifdef FAST_IPSEC
847b9234fafSSam Leffler 	if (sp != NULL)
848b9234fafSSam Leffler 		KEY_FREESP(&sp);
8491e78ac21SJeffrey Hsu #endif
850df8bae1dSRodney W. Grimes 	return (error);
851df8bae1dSRodney W. Grimes bad:
8523528d68fSBill Paul 	m_freem(m);
853df8bae1dSRodney W. Grimes 	goto done;
854df8bae1dSRodney W. Grimes }
855df8bae1dSRodney W. Grimes 
8561e78ac21SJeffrey Hsu /*
8571e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
8581e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
8591e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
8601e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
8611e78ac21SJeffrey Hsu  *
8621e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
8631e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
8641e78ac21SJeffrey Hsu  */
8651e78ac21SJeffrey Hsu int
8661e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
8671e78ac21SJeffrey Hsu 	    u_long if_hwassist_flags, int sw_csum)
8681e78ac21SJeffrey Hsu {
8691e78ac21SJeffrey Hsu 	int error = 0;
8701e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
8711e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
8721e78ac21SJeffrey Hsu 	int off;
8731e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
8741e78ac21SJeffrey Hsu 	int firstlen;
8751e78ac21SJeffrey Hsu 	struct mbuf **mnext;
8761e78ac21SJeffrey Hsu 	int nfrags;
8771e78ac21SJeffrey Hsu 
8781e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
8791e78ac21SJeffrey Hsu 		ipstat.ips_cantfrag++;
8801e78ac21SJeffrey Hsu 		return EMSGSIZE;
8811e78ac21SJeffrey Hsu 	}
8821e78ac21SJeffrey Hsu 
8831e78ac21SJeffrey Hsu 	/*
8841e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
8851e78ac21SJeffrey Hsu 	 */
8861e78ac21SJeffrey Hsu 	if (len < 8)
8871e78ac21SJeffrey Hsu 		return EMSGSIZE;
8881e78ac21SJeffrey Hsu 
8891e78ac21SJeffrey Hsu 	/*
8901e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
8911e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
8921e78ac21SJeffrey Hsu 	 */
8931e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
8941e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
8951e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
8961e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
8971e78ac21SJeffrey Hsu 	}
8981e78ac21SJeffrey Hsu 
8991e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
9001e78ac21SJeffrey Hsu 		/*
9011e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
9021e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
9031e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
9041e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
9051e78ac21SJeffrey Hsu 		 *
9061e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
9071e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
9081e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
9091e78ac21SJeffrey Hsu 		 */
9101e78ac21SJeffrey Hsu 		int newlen;
9111e78ac21SJeffrey Hsu 		struct mbuf *m;
9121e78ac21SJeffrey Hsu 
9131e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
9141e78ac21SJeffrey Hsu 			off += m->m_len;
9151e78ac21SJeffrey Hsu 
9161e78ac21SJeffrey Hsu 		/*
9171e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
9181e78ac21SJeffrey Hsu 		 * 8-byte boundary
9191e78ac21SJeffrey Hsu 		 */
9201e78ac21SJeffrey Hsu 		if (off < hlen)
9211e78ac21SJeffrey Hsu 			goto smart_frag_failure;
9221e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
9231e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
9241e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
9251e78ac21SJeffrey Hsu 			/* we failed, go back the default */
9261e78ac21SJeffrey Hsu smart_frag_failure:
9271e78ac21SJeffrey Hsu 			newlen = len;
9281e78ac21SJeffrey Hsu 			off = hlen + len;
9291e78ac21SJeffrey Hsu 		}
9301e78ac21SJeffrey Hsu 		len = newlen;
9311e78ac21SJeffrey Hsu 
9321e78ac21SJeffrey Hsu 	} else {
9331e78ac21SJeffrey Hsu 		off = hlen + len;
9341e78ac21SJeffrey Hsu 	}
9351e78ac21SJeffrey Hsu 
9361e78ac21SJeffrey Hsu 	firstlen = off - hlen;
9371e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
9381e78ac21SJeffrey Hsu 
9391e78ac21SJeffrey Hsu 	/*
9401e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
9411e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
9421e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
9431e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
9441e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
9451e78ac21SJeffrey Hsu 	 */
9461e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
9471e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
9481e78ac21SJeffrey Hsu 		struct mbuf *m;
9491e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
9501e78ac21SJeffrey Hsu 
95134333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
9520b17fba7SAndre Oppermann 		if (m == NULL) {
9531e78ac21SJeffrey Hsu 			error = ENOBUFS;
9541e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
9551e78ac21SJeffrey Hsu 			goto done;
9561e78ac21SJeffrey Hsu 		}
9571e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
9581e78ac21SJeffrey Hsu 		/*
9591e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
9601e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
9611e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
9621e78ac21SJeffrey Hsu 		 */
9631e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
9641e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
9651e78ac21SJeffrey Hsu 		*mhip = *ip;
9661e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
9671e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9681e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
9691e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
9701e78ac21SJeffrey Hsu 		}
9711e78ac21SJeffrey Hsu 		m->m_len = mhlen;
9721e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
9731e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
9741e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
9751e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
9761e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
9771e78ac21SJeffrey Hsu 		} else
9781e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
9791e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
9801e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
9810b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
9821e78ac21SJeffrey Hsu 			m_free(m);
9831e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
9841e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
9851e78ac21SJeffrey Hsu 			goto done;
9861e78ac21SJeffrey Hsu 		}
9871e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
988fc74a9f9SBrooks Davis 		m->m_pkthdr.rcvif = NULL;
9891e78ac21SJeffrey Hsu #ifdef MAC
9901e78ac21SJeffrey Hsu 		mac_create_fragment(m0, m);
9911e78ac21SJeffrey Hsu #endif
9921e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
9931e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
9941e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
9951e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
9961e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
9971e78ac21SJeffrey Hsu 		*mnext = m;
9981e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
9991e78ac21SJeffrey Hsu 	}
10001e78ac21SJeffrey Hsu 	ipstat.ips_ofragments += nfrags;
10011e78ac21SJeffrey Hsu 
10021e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
10031e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
10041e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
10051e78ac21SJeffrey Hsu 
10061e78ac21SJeffrey Hsu 	/*
10071e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
10081e78ac21SJeffrey Hsu 	 * and updating header.
10091e78ac21SJeffrey Hsu 	 */
10101e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
10111e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
10121e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
10131e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
10141e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
10151e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
10161e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
10171e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
10181e78ac21SJeffrey Hsu 
10191e78ac21SJeffrey Hsu done:
10201e78ac21SJeffrey Hsu 	*m_frag = m0;
10211e78ac21SJeffrey Hsu 	return error;
10221e78ac21SJeffrey Hsu }
10231e78ac21SJeffrey Hsu 
10241c238475SJonathan Lemon void
1025db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1026db4f9cc7SJonathan Lemon {
1027db4f9cc7SJonathan Lemon 	struct ip *ip;
1028db4f9cc7SJonathan Lemon 	u_short csum, offset;
1029db4f9cc7SJonathan Lemon 
1030db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
103153be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
1032db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1033206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1034206a3274SRuslan Ermilov 		csum = 0xffff;
1035db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1036db4f9cc7SJonathan Lemon 
1037db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1038db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1039db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1040db4f9cc7SJonathan Lemon 		/*
1041db4f9cc7SJonathan Lemon 		 * XXX
1042db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1043db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1044db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1045db4f9cc7SJonathan Lemon 		 */
1046db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1047db4f9cc7SJonathan Lemon 	}
1048db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1049db4f9cc7SJonathan Lemon }
1050db4f9cc7SJonathan Lemon 
1051df8bae1dSRodney W. Grimes /*
1052df8bae1dSRodney W. Grimes  * IP socket option processing.
1053df8bae1dSRodney W. Grimes  */
1054df8bae1dSRodney W. Grimes int
1055b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt)
1056b375c9ecSLuigi Rizzo 	struct socket *so;
1057b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1058df8bae1dSRodney W. Grimes {
1059cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1060cfe8b629SGarrett Wollman 	int	error, optval;
1061df8bae1dSRodney W. Grimes 
1062cfe8b629SGarrett Wollman 	error = optval = 0;
1063cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1064cfe8b629SGarrett Wollman 		return (EINVAL);
1065cfe8b629SGarrett Wollman 	}
1066df8bae1dSRodney W. Grimes 
1067cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1068cfe8b629SGarrett Wollman 	case SOPT_SET:
1069cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1070df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1071df8bae1dSRodney W. Grimes #ifdef notyet
1072df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1073df8bae1dSRodney W. Grimes #endif
1074cfe8b629SGarrett Wollman 		{
1075cfe8b629SGarrett Wollman 			struct mbuf *m;
1076cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1077cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1078cfe8b629SGarrett Wollman 				break;
1079cfe8b629SGarrett Wollman 			}
1080e0aec682SAndre Oppermann 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
10810b17fba7SAndre Oppermann 			if (m == NULL) {
1082cfe8b629SGarrett Wollman 				error = ENOBUFS;
1083cfe8b629SGarrett Wollman 				break;
1084cfe8b629SGarrett Wollman 			}
1085cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1086cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1087cfe8b629SGarrett Wollman 					    m->m_len);
1088993d9505SRobert Watson 			INP_LOCK(inp);
1089993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
1090993d9505SRobert Watson 			INP_UNLOCK(inp);
1091993d9505SRobert Watson 			return (error);
1092cfe8b629SGarrett Wollman 		}
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes 		case IP_TOS:
1095df8bae1dSRodney W. Grimes 		case IP_TTL:
1096936cd18dSAndre Oppermann 		case IP_MINTTL:
1097df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1098df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1099df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
11004957466bSMatthew N. Dodd 		case IP_RECVTTL:
110182c23ebaSBill Fenner 		case IP_RECVIF:
11026a800098SYoshinobu Inoue 		case IP_FAITH:
11038afa2304SBruce M Simpson 		case IP_ONESBCAST:
1104b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
1105cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1106cfe8b629SGarrett Wollman 					    sizeof optval);
1107cfe8b629SGarrett Wollman 			if (error)
1108cfe8b629SGarrett Wollman 				break;
1109df8bae1dSRodney W. Grimes 
1110cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1111df8bae1dSRodney W. Grimes 			case IP_TOS:
1112ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1113df8bae1dSRodney W. Grimes 				break;
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes 			case IP_TTL:
1116ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1117df8bae1dSRodney W. Grimes 				break;
1118936cd18dSAndre Oppermann 
1119936cd18dSAndre Oppermann 			case IP_MINTTL:
1120936cd18dSAndre Oppermann 				if (optval > 0 && optval <= MAXTTL)
1121936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
1122936cd18dSAndre Oppermann 				else
1123936cd18dSAndre Oppermann 					error = EINVAL;
1124936cd18dSAndre Oppermann 				break;
1125936cd18dSAndre Oppermann 
1126a138d217SRobert Watson #define	OPTSET(bit) do {						\
1127a138d217SRobert Watson 	INP_LOCK(inp);							\
1128df8bae1dSRodney W. Grimes 	if (optval)							\
1129df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1130df8bae1dSRodney W. Grimes 	else								\
1131a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
1132a138d217SRobert Watson 	INP_UNLOCK(inp);						\
1133a138d217SRobert Watson } while (0)
1134df8bae1dSRodney W. Grimes 
1135df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1136df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1137df8bae1dSRodney W. Grimes 				break;
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1140df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1141df8bae1dSRodney W. Grimes 				break;
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1144df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1145df8bae1dSRodney W. Grimes 				break;
114682c23ebaSBill Fenner 
11474957466bSMatthew N. Dodd 			case IP_RECVTTL:
11484957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
11494957466bSMatthew N. Dodd 				break;
11504957466bSMatthew N. Dodd 
115182c23ebaSBill Fenner 			case IP_RECVIF:
115282c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
115382c23ebaSBill Fenner 				break;
11546a800098SYoshinobu Inoue 
11556a800098SYoshinobu Inoue 			case IP_FAITH:
11566a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
11576a800098SYoshinobu Inoue 				break;
11588afa2304SBruce M Simpson 
11598afa2304SBruce M Simpson 			case IP_ONESBCAST:
11608afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
11618afa2304SBruce M Simpson 				break;
1162b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1163b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1164b2828ad2SAndre Oppermann 				break;
1165df8bae1dSRodney W. Grimes 			}
1166df8bae1dSRodney W. Grimes 			break;
1167df8bae1dSRodney W. Grimes #undef OPTSET
1168df8bae1dSRodney W. Grimes 
1169df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1170f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1171df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1172df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1173df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1174df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
11755c918b56SRobert Watson 			error = ip_setmoptions(inp, sopt);
1176df8bae1dSRodney W. Grimes 			break;
1177df8bae1dSRodney W. Grimes 
117833b3ac06SPeter Wemm 		case IP_PORTRANGE:
1179cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1180cfe8b629SGarrett Wollman 					    sizeof optval);
1181cfe8b629SGarrett Wollman 			if (error)
1182cfe8b629SGarrett Wollman 				break;
118333b3ac06SPeter Wemm 
1184a138d217SRobert Watson 			INP_LOCK(inp);
118533b3ac06SPeter Wemm 			switch (optval) {
118633b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
118733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
118833b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
118933b3ac06SPeter Wemm 				break;
119033b3ac06SPeter Wemm 
119133b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
119233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
119333b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
119433b3ac06SPeter Wemm 				break;
119533b3ac06SPeter Wemm 
119633b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
119733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
119833b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
119933b3ac06SPeter Wemm 				break;
120033b3ac06SPeter Wemm 
120133b3ac06SPeter Wemm 			default:
120233b3ac06SPeter Wemm 				error = EINVAL;
120333b3ac06SPeter Wemm 				break;
120433b3ac06SPeter Wemm 			}
1205a138d217SRobert Watson 			INP_UNLOCK(inp);
1206ce8c72b1SPeter Wemm 			break;
120733b3ac06SPeter Wemm 
1208b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
12096a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12106a800098SYoshinobu Inoue 		{
12116a800098SYoshinobu Inoue 			caddr_t req;
1212686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
12136a800098SYoshinobu Inoue 			int priv;
12146a800098SYoshinobu Inoue 			struct mbuf *m;
12156a800098SYoshinobu Inoue 			int optname;
12166a800098SYoshinobu Inoue 
12176a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
12186a800098SYoshinobu Inoue 				break;
12196a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12206a800098SYoshinobu Inoue 				break;
1221b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
122244731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
12236a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1224686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
12256a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1226686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
12276a800098SYoshinobu Inoue 			m_freem(m);
12286a800098SYoshinobu Inoue 			break;
12296a800098SYoshinobu Inoue 		}
12306a800098SYoshinobu Inoue #endif /*IPSEC*/
12316a800098SYoshinobu Inoue 
1232df8bae1dSRodney W. Grimes 		default:
1233df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1234df8bae1dSRodney W. Grimes 			break;
1235df8bae1dSRodney W. Grimes 		}
1236df8bae1dSRodney W. Grimes 		break;
1237df8bae1dSRodney W. Grimes 
1238cfe8b629SGarrett Wollman 	case SOPT_GET:
1239cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1240df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1241df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1242cfe8b629SGarrett Wollman 			if (inp->inp_options)
1243cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1244cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1245cfe8b629SGarrett Wollman 							  char *),
1246cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1247cfe8b629SGarrett Wollman 			else
1248cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1249df8bae1dSRodney W. Grimes 			break;
1250df8bae1dSRodney W. Grimes 
1251df8bae1dSRodney W. Grimes 		case IP_TOS:
1252df8bae1dSRodney W. Grimes 		case IP_TTL:
1253936cd18dSAndre Oppermann 		case IP_MINTTL:
1254df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1255df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1256df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
12574957466bSMatthew N. Dodd 		case IP_RECVTTL:
125882c23ebaSBill Fenner 		case IP_RECVIF:
1259cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
12606a800098SYoshinobu Inoue 		case IP_FAITH:
12618afa2304SBruce M Simpson 		case IP_ONESBCAST:
1262b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
1263cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1264df8bae1dSRodney W. Grimes 
1265df8bae1dSRodney W. Grimes 			case IP_TOS:
1266ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1267df8bae1dSRodney W. Grimes 				break;
1268df8bae1dSRodney W. Grimes 
1269df8bae1dSRodney W. Grimes 			case IP_TTL:
1270ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1271df8bae1dSRodney W. Grimes 				break;
1272df8bae1dSRodney W. Grimes 
1273936cd18dSAndre Oppermann 			case IP_MINTTL:
1274936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1275936cd18dSAndre Oppermann 				break;
1276936cd18dSAndre Oppermann 
1277df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1278df8bae1dSRodney W. Grimes 
1279df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1280df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1281df8bae1dSRodney W. Grimes 				break;
1282df8bae1dSRodney W. Grimes 
1283df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1284df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1285df8bae1dSRodney W. Grimes 				break;
1286df8bae1dSRodney W. Grimes 
1287df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1288df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1289df8bae1dSRodney W. Grimes 				break;
129082c23ebaSBill Fenner 
12914957466bSMatthew N. Dodd 			case IP_RECVTTL:
12924957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
12934957466bSMatthew N. Dodd 				break;
12944957466bSMatthew N. Dodd 
129582c23ebaSBill Fenner 			case IP_RECVIF:
129682c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
129782c23ebaSBill Fenner 				break;
1298cfe8b629SGarrett Wollman 
1299cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1300cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1301cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1302cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1303cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1304cfe8b629SGarrett Wollman 				else
1305cfe8b629SGarrett Wollman 					optval = 0;
1306cfe8b629SGarrett Wollman 				break;
13076a800098SYoshinobu Inoue 
13086a800098SYoshinobu Inoue 			case IP_FAITH:
13096a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
13106a800098SYoshinobu Inoue 				break;
13118afa2304SBruce M Simpson 
13128afa2304SBruce M Simpson 			case IP_ONESBCAST:
13138afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
13148afa2304SBruce M Simpson 				break;
1315b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1316b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1317b2828ad2SAndre Oppermann 				break;
1318df8bae1dSRodney W. Grimes 			}
1319cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1320df8bae1dSRodney W. Grimes 			break;
1321df8bae1dSRodney W. Grimes 
1322df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1323f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1324df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1325df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1326df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1327df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
132889924e58SRobert Watson 			error = ip_getmoptions(inp, sopt);
132933b3ac06SPeter Wemm 			break;
133033b3ac06SPeter Wemm 
1331b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
13326a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13336a800098SYoshinobu Inoue 		{
1334f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
13356a800098SYoshinobu Inoue 			caddr_t req = NULL;
1336686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
13376a800098SYoshinobu Inoue 
1338686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
13396a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1340686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1341686cdd19SJun-ichiro itojun Hagino 			}
1342686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
13436a800098SYoshinobu Inoue 			if (error == 0)
13446a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1345f63e7634SYoshinobu Inoue 			if (error == 0)
13466a800098SYoshinobu Inoue 				m_freem(m);
13476a800098SYoshinobu Inoue 			break;
13486a800098SYoshinobu Inoue 		}
13496a800098SYoshinobu Inoue #endif /*IPSEC*/
13506a800098SYoshinobu Inoue 
1351df8bae1dSRodney W. Grimes 		default:
1352df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1353df8bae1dSRodney W. Grimes 			break;
1354df8bae1dSRodney W. Grimes 		}
1355df8bae1dSRodney W. Grimes 		break;
1356df8bae1dSRodney W. Grimes 	}
1357df8bae1dSRodney W. Grimes 	return (error);
1358df8bae1dSRodney W. Grimes }
1359df8bae1dSRodney W. Grimes 
1360df8bae1dSRodney W. Grimes /*
1361cfe8b629SGarrett Wollman  * XXX
1362cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1363cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1364cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1365cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1366cfe8b629SGarrett Wollman  */
136733841545SHajimu UMEMOTO 
136833841545SHajimu UMEMOTO /*
136933841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
137033841545SHajimu UMEMOTO  */
137133841545SHajimu UMEMOTO static struct ifnet *
1372b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp)
1373b375c9ecSLuigi Rizzo 	struct in_addr *a;
1374b375c9ecSLuigi Rizzo 	int *ifindexp;
137533841545SHajimu UMEMOTO {
137633841545SHajimu UMEMOTO 	int ifindex;
137733841545SHajimu UMEMOTO 	struct ifnet *ifp;
137833841545SHajimu UMEMOTO 
137933841545SHajimu UMEMOTO 	if (ifindexp)
138033841545SHajimu UMEMOTO 		*ifindexp = 0;
138133841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
138233841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
138333841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
138433841545SHajimu UMEMOTO 			return NULL;
1385f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
138633841545SHajimu UMEMOTO 		if (ifindexp)
138733841545SHajimu UMEMOTO 			*ifindexp = ifindex;
138833841545SHajimu UMEMOTO 	} else {
138933841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
139033841545SHajimu UMEMOTO 	}
139133841545SHajimu UMEMOTO 	return ifp;
139233841545SHajimu UMEMOTO }
139333841545SHajimu UMEMOTO 
1394cfe8b629SGarrett Wollman /*
1395a2dc1f50SRobert Watson  * Given an inpcb, return its multicast options structure pointer.  Accepts
1396a2dc1f50SRobert Watson  * an unlocked inpcb pointer, but will return it locked.  May sleep.
1397a2dc1f50SRobert Watson  */
1398a2dc1f50SRobert Watson static struct ip_moptions *
1399a2dc1f50SRobert Watson ip_findmoptions(struct inpcb *inp)
1400a2dc1f50SRobert Watson {
1401a2dc1f50SRobert Watson 	struct ip_moptions *imo;
1402a2dc1f50SRobert Watson 
1403a2dc1f50SRobert Watson 	INP_LOCK(inp);
1404a2dc1f50SRobert Watson 	if (inp->inp_moptions != NULL)
1405a2dc1f50SRobert Watson 		return (inp->inp_moptions);
1406a2dc1f50SRobert Watson 
1407a2dc1f50SRobert Watson 	INP_UNLOCK(inp);
1408a2dc1f50SRobert Watson 
1409a2dc1f50SRobert Watson 	imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1410a2dc1f50SRobert Watson 
1411a2dc1f50SRobert Watson 	imo->imo_multicast_ifp = NULL;
1412a2dc1f50SRobert Watson 	imo->imo_multicast_addr.s_addr = INADDR_ANY;
1413a2dc1f50SRobert Watson 	imo->imo_multicast_vif = -1;
1414a2dc1f50SRobert Watson 	imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1415a2dc1f50SRobert Watson 	imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1416a2dc1f50SRobert Watson 	imo->imo_num_memberships = 0;
1417a2dc1f50SRobert Watson 
1418a2dc1f50SRobert Watson 	INP_LOCK(inp);
1419a2dc1f50SRobert Watson 	if (inp->inp_moptions != NULL) {
1420a2dc1f50SRobert Watson 		free(imo, M_IPMOPTS);
1421a2dc1f50SRobert Watson 		return (inp->inp_moptions);
1422a2dc1f50SRobert Watson 	}
1423a2dc1f50SRobert Watson 	inp->inp_moptions = imo;
1424a2dc1f50SRobert Watson 	return (imo);
1425a2dc1f50SRobert Watson }
1426a2dc1f50SRobert Watson 
1427a2dc1f50SRobert Watson /*
1428df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1429df8bae1dSRodney W. Grimes  */
14300312fbe9SPoul-Henning Kamp static int
14315c918b56SRobert Watson ip_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1432df8bae1dSRodney W. Grimes {
1433cfe8b629SGarrett Wollman 	int error = 0;
1434cfe8b629SGarrett Wollman 	int i;
1435df8bae1dSRodney W. Grimes 	struct in_addr addr;
1436cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1437cfe8b629SGarrett Wollman 	struct ifnet *ifp;
14385c918b56SRobert Watson 	struct ip_moptions *imo;
1439df8bae1dSRodney W. Grimes 	struct route ro;
1440cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
144133841545SHajimu UMEMOTO 	int ifindex;
14429b626c29SGarrett Wollman 	int s;
1443df8bae1dSRodney W. Grimes 
1444cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1445f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1446f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1447cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
14485e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
14495e9ae478SGarrett Wollman 			break;
14505e9ae478SGarrett Wollman 		}
1451cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1452cfe8b629SGarrett Wollman 		if (error)
1453f0068c4aSGarrett Wollman 			break;
14541c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1455f0068c4aSGarrett Wollman 			error = EINVAL;
1456f0068c4aSGarrett Wollman 			break;
1457f0068c4aSGarrett Wollman 		}
1458a2dc1f50SRobert Watson 		imo = ip_findmoptions(inp);
1459f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1460a2dc1f50SRobert Watson 		INP_UNLOCK(inp);
1461f0068c4aSGarrett Wollman 		break;
1462f0068c4aSGarrett Wollman 
1463df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1464df8bae1dSRodney W. Grimes 		/*
1465df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1466df8bae1dSRodney W. Grimes 		 */
1467cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1468cfe8b629SGarrett Wollman 		if (error)
1469df8bae1dSRodney W. Grimes 			break;
1470df8bae1dSRodney W. Grimes 		/*
1471df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1472df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1473df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1474df8bae1dSRodney W. Grimes 		 */
1475a2dc1f50SRobert Watson 		imo = ip_findmoptions(inp);
1476df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1477df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1478a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1479df8bae1dSRodney W. Grimes 			break;
1480df8bae1dSRodney W. Grimes 		}
1481df8bae1dSRodney W. Grimes 		/*
1482df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1483df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1484df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1485df8bae1dSRodney W. Grimes 		 */
148620e8807cSGarrett Wollman 		s = splimp();
148733841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1488df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1489a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1490fbc6ab00SBill Fenner 			splx(s);
1491df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1492df8bae1dSRodney W. Grimes 			break;
1493df8bae1dSRodney W. Grimes 		}
1494df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
149533841545SHajimu UMEMOTO 		if (ifindex)
149633841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
149733841545SHajimu UMEMOTO 		else
149833841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1499a2dc1f50SRobert Watson 		INP_UNLOCK(inp);
15009b626c29SGarrett Wollman 		splx(s);
1501df8bae1dSRodney W. Grimes 		break;
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1504df8bae1dSRodney W. Grimes 		/*
1505df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1506cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1507cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1508cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1509df8bae1dSRodney W. Grimes 		 */
1510cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1511cfe8b629SGarrett Wollman 			u_char ttl;
1512cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1513cfe8b629SGarrett Wollman 			if (error)
1514df8bae1dSRodney W. Grimes 				break;
1515a2dc1f50SRobert Watson 			imo = ip_findmoptions(inp);
1516cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1517a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1518cfe8b629SGarrett Wollman 		} else {
1519cfe8b629SGarrett Wollman 			u_int ttl;
1520cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1521cfe8b629SGarrett Wollman 					    sizeof ttl);
1522cfe8b629SGarrett Wollman 			if (error)
1523cfe8b629SGarrett Wollman 				break;
1524cfe8b629SGarrett Wollman 			if (ttl > 255)
1525cfe8b629SGarrett Wollman 				error = EINVAL;
1526a2dc1f50SRobert Watson 			else {
1527a2dc1f50SRobert Watson 				imo = ip_findmoptions(inp);
1528cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1529a2dc1f50SRobert Watson 				INP_UNLOCK(inp);
1530a2dc1f50SRobert Watson 			}
1531df8bae1dSRodney W. Grimes 		}
1532df8bae1dSRodney W. Grimes 		break;
1533df8bae1dSRodney W. Grimes 
1534df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1535df8bae1dSRodney W. Grimes 		/*
1536df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1537cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1538cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1539cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1540df8bae1dSRodney W. Grimes 		 */
1541cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1542cfe8b629SGarrett Wollman 			u_char loop;
1543cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1544cfe8b629SGarrett Wollman 			if (error)
1545df8bae1dSRodney W. Grimes 				break;
1546a2dc1f50SRobert Watson 			imo = ip_findmoptions(inp);
1547cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1548a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1549cfe8b629SGarrett Wollman 		} else {
1550cfe8b629SGarrett Wollman 			u_int loop;
1551cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1552cfe8b629SGarrett Wollman 					    sizeof loop);
1553cfe8b629SGarrett Wollman 			if (error)
1554cfe8b629SGarrett Wollman 				break;
1555a2dc1f50SRobert Watson 			imo = ip_findmoptions(inp);
1556cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1557a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1558df8bae1dSRodney W. Grimes 		}
1559df8bae1dSRodney W. Grimes 		break;
1560df8bae1dSRodney W. Grimes 
1561df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1562df8bae1dSRodney W. Grimes 		/*
1563df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1564df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1565df8bae1dSRodney W. Grimes 		 */
1566cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1567cfe8b629SGarrett Wollman 		if (error)
1568df8bae1dSRodney W. Grimes 			break;
1569cfe8b629SGarrett Wollman 
1570cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1571df8bae1dSRodney W. Grimes 			error = EINVAL;
1572df8bae1dSRodney W. Grimes 			break;
1573df8bae1dSRodney W. Grimes 		}
157420e8807cSGarrett Wollman 		s = splimp();
1575df8bae1dSRodney W. Grimes 		/*
1576df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1577df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1578df8bae1dSRodney W. Grimes 		 */
1579cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
15801c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1581df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1582df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1583df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1584cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
158597d8d152SAndre Oppermann 			rtalloc_ign(&ro, RTF_CLONING);
1586df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1587df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
15889b626c29SGarrett Wollman 				splx(s);
1589df8bae1dSRodney W. Grimes 				break;
1590df8bae1dSRodney W. Grimes 			}
1591df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1592d1dd20beSSam Leffler 			RTFREE(ro.ro_rt);
1593df8bae1dSRodney W. Grimes 		}
1594df8bae1dSRodney W. Grimes 		else {
159533841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1596df8bae1dSRodney W. Grimes 		}
15979b626c29SGarrett Wollman 
1598df8bae1dSRodney W. Grimes 		/*
1599df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1600df8bae1dSRodney W. Grimes 		 * supports multicast.
1601df8bae1dSRodney W. Grimes 		 */
1602df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1603df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
16049b626c29SGarrett Wollman 			splx(s);
1605df8bae1dSRodney W. Grimes 			break;
1606df8bae1dSRodney W. Grimes 		}
1607df8bae1dSRodney W. Grimes 		/*
1608df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1609df8bae1dSRodney W. Grimes 		 * membership slots are full.
1610df8bae1dSRodney W. Grimes 		 */
1611a2dc1f50SRobert Watson 		imo = ip_findmoptions(inp);
1612df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1613df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1614df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1615cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1616df8bae1dSRodney W. Grimes 				break;
1617df8bae1dSRodney W. Grimes 		}
1618df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1619a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1620df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
16219b626c29SGarrett Wollman 			splx(s);
1622df8bae1dSRodney W. Grimes 			break;
1623df8bae1dSRodney W. Grimes 		}
1624df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1625a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1626df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
16279b626c29SGarrett Wollman 			splx(s);
1628df8bae1dSRodney W. Grimes 			break;
1629df8bae1dSRodney W. Grimes 		}
1630df8bae1dSRodney W. Grimes 		/*
1631df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1632df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1633df8bae1dSRodney W. Grimes 		 */
1634df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1635cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1636a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1637df8bae1dSRodney W. Grimes 			error = ENOBUFS;
16389b626c29SGarrett Wollman 			splx(s);
1639df8bae1dSRodney W. Grimes 			break;
1640df8bae1dSRodney W. Grimes 		}
1641df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
1642a2dc1f50SRobert Watson 		INP_UNLOCK(inp);
16439b626c29SGarrett Wollman 		splx(s);
1644df8bae1dSRodney W. Grimes 		break;
1645df8bae1dSRodney W. Grimes 
1646df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1647df8bae1dSRodney W. Grimes 		/*
1648df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1649df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1650df8bae1dSRodney W. Grimes 		 */
1651cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1652cfe8b629SGarrett Wollman 		if (error)
1653df8bae1dSRodney W. Grimes 			break;
1654cfe8b629SGarrett Wollman 
1655cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1656df8bae1dSRodney W. Grimes 			error = EINVAL;
1657df8bae1dSRodney W. Grimes 			break;
1658df8bae1dSRodney W. Grimes 		}
16599b626c29SGarrett Wollman 
166020e8807cSGarrett Wollman 		s = splimp();
1661df8bae1dSRodney W. Grimes 		/*
1662df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1663df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1664df8bae1dSRodney W. Grimes 		 */
1665cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1666df8bae1dSRodney W. Grimes 			ifp = NULL;
1667df8bae1dSRodney W. Grimes 		else {
166833841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1669df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1670df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
16719b626c29SGarrett Wollman 				splx(s);
1672df8bae1dSRodney W. Grimes 				break;
1673df8bae1dSRodney W. Grimes 			}
1674df8bae1dSRodney W. Grimes 		}
1675df8bae1dSRodney W. Grimes 		/*
1676df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1677df8bae1dSRodney W. Grimes 		 */
1678a2dc1f50SRobert Watson 		imo = ip_findmoptions(inp);
1679df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1680df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1681df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1682df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1683cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1684df8bae1dSRodney W. Grimes 				break;
1685df8bae1dSRodney W. Grimes 		}
1686df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1687a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1688df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
16899b626c29SGarrett Wollman 			splx(s);
1690df8bae1dSRodney W. Grimes 			break;
1691df8bae1dSRodney W. Grimes 		}
1692df8bae1dSRodney W. Grimes 		/*
1693df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1694df8bae1dSRodney W. Grimes 		 * membership points.
1695df8bae1dSRodney W. Grimes 		 */
1696df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1697df8bae1dSRodney W. Grimes 		/*
1698df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1699df8bae1dSRodney W. Grimes 		 */
1700df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1701df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1702df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
1703a2dc1f50SRobert Watson 		INP_UNLOCK(inp);
17049b626c29SGarrett Wollman 		splx(s);
1705df8bae1dSRodney W. Grimes 		break;
1706df8bae1dSRodney W. Grimes 
1707df8bae1dSRodney W. Grimes 	default:
1708df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1709df8bae1dSRodney W. Grimes 		break;
1710df8bae1dSRodney W. Grimes 	}
1711df8bae1dSRodney W. Grimes 
1712df8bae1dSRodney W. Grimes 	return (error);
1713df8bae1dSRodney W. Grimes }
1714df8bae1dSRodney W. Grimes 
1715df8bae1dSRodney W. Grimes /*
1716df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1717df8bae1dSRodney W. Grimes  */
17180312fbe9SPoul-Henning Kamp static int
171989924e58SRobert Watson ip_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1720df8bae1dSRodney W. Grimes {
172189924e58SRobert Watson 	struct ip_moptions *imo;
1722cfe8b629SGarrett Wollman 	struct in_addr addr;
1723df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1724cfe8b629SGarrett Wollman 	int error, optval;
1725cfe8b629SGarrett Wollman 	u_char coptval;
1726df8bae1dSRodney W. Grimes 
172789924e58SRobert Watson 	INP_LOCK(inp);
172889924e58SRobert Watson 	imo = inp->inp_moptions;
172989924e58SRobert Watson 
1730cfe8b629SGarrett Wollman 	error = 0;
1731cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1732f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1733f0068c4aSGarrett Wollman 		if (imo != NULL)
1734cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1735f0068c4aSGarrett Wollman 		else
1736cfe8b629SGarrett Wollman 			optval = -1;
173789924e58SRobert Watson 		INP_UNLOCK(inp);
1738cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1739cfe8b629SGarrett Wollman 		break;
1740f0068c4aSGarrett Wollman 
1741df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1742df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1743cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
174433841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
174533841545SHajimu UMEMOTO 			/* return the value user has set */
174633841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
174733841545SHajimu UMEMOTO 		} else {
1748df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1749cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1750df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1751df8bae1dSRodney W. Grimes 		}
175289924e58SRobert Watson 		INP_UNLOCK(inp);
1753cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1754cfe8b629SGarrett Wollman 		break;
1755df8bae1dSRodney W. Grimes 
1756df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1757cfe8b629SGarrett Wollman 		if (imo == 0)
1758cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1759cfe8b629SGarrett Wollman 		else
1760cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
176189924e58SRobert Watson 		INP_UNLOCK(inp);
1762cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1763cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1764cfe8b629SGarrett Wollman 		else
1765cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1766cfe8b629SGarrett Wollman 		break;
1767df8bae1dSRodney W. Grimes 
1768df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1769cfe8b629SGarrett Wollman 		if (imo == 0)
1770cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1771cfe8b629SGarrett Wollman 		else
1772cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
177389924e58SRobert Watson 		INP_UNLOCK(inp);
1774cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1775cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1776cfe8b629SGarrett Wollman 		else
1777cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1778cfe8b629SGarrett Wollman 		break;
1779df8bae1dSRodney W. Grimes 
1780df8bae1dSRodney W. Grimes 	default:
178189924e58SRobert Watson 		INP_UNLOCK(inp);
1782cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1783cfe8b629SGarrett Wollman 		break;
1784df8bae1dSRodney W. Grimes 	}
178589924e58SRobert Watson 	INP_UNLOCK_ASSERT(inp);
178689924e58SRobert Watson 
1787cfe8b629SGarrett Wollman 	return (error);
1788df8bae1dSRodney W. Grimes }
1789df8bae1dSRodney W. Grimes 
1790df8bae1dSRodney W. Grimes /*
1791df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1792df8bae1dSRodney W. Grimes  */
1793df8bae1dSRodney W. Grimes void
1794b375c9ecSLuigi Rizzo ip_freemoptions(imo)
1795b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
1796df8bae1dSRodney W. Grimes {
1797b375c9ecSLuigi Rizzo 	register int i;
1798df8bae1dSRodney W. Grimes 
1799df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1800df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1801df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1802df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1803df8bae1dSRodney W. Grimes 	}
1804df8bae1dSRodney W. Grimes }
1805df8bae1dSRodney W. Grimes 
1806df8bae1dSRodney W. Grimes /*
1807df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1808df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1809df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1810f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1811f5fea3ddSPaul Traina  * replicating that code here.
1812df8bae1dSRodney W. Grimes  */
1813df8bae1dSRodney W. Grimes static void
1814b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen)
1815b375c9ecSLuigi Rizzo 	struct ifnet *ifp;
1816b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1817b375c9ecSLuigi Rizzo 	register struct sockaddr_in *dst;
1818b375c9ecSLuigi Rizzo 	int hlen;
1819df8bae1dSRodney W. Grimes {
1820b375c9ecSLuigi Rizzo 	register struct ip *ip;
1821df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1822df8bae1dSRodney W. Grimes 
1823b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
182486b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
182586b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1826df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1827390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1828390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1829390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1830390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1831390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1832390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1833390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1834390cdc6aSRuslan Ermilov 		}
1835df8bae1dSRodney W. Grimes 		/*
1836df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1837df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1838df8bae1dSRodney W. Grimes 		 */
1839df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1840fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1841fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1842df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
184386b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
18449c9137eaSGarrett Wollman 		/*
18459c9137eaSGarrett Wollman 		 * NB:
1846e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1847e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1848e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1849e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1850e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1851e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1852e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1853ed7509acSJulian Elischer 		 * of safety by using if_simloop().
18549c9137eaSGarrett Wollman 		 */
1855201c2527SJulian Elischer #if 1 /* XXX */
1856201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
18572b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1858201c2527SJulian Elischer 						dst->sin_family);
1859201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1860201c2527SJulian Elischer 		}
1861201c2527SJulian Elischer #endif
1862201c2527SJulian Elischer 
18639c9137eaSGarrett Wollman #ifdef notdef
1864e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
1865ed7509acSJulian Elischer 		ip_input(copym);
18669c9137eaSGarrett Wollman #else
186706a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
18689c9137eaSGarrett Wollman #endif
1869df8bae1dSRodney W. Grimes 	}
1870df8bae1dSRodney W. Grimes }
1871