xref: /freebsd/sys/netinet/ip_output.c (revision 0b17fba7bc072f1ad8b739d53d3e957e270d9377)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
331f7e052cSJulian Elischer #include "opt_ipfw.h"
34b715f178SLuigi Rizzo #include "opt_ipdn.h"
35fbd1372aSJoerg Wunsch #include "opt_ipdivert.h"
361ee25934SPeter Wemm #include "opt_ipfilter.h"
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
384ed84624SRobert Watson #include "opt_mac.h"
39c4ac87eaSDarren Reed #include "opt_pfil_hooks.h"
4064dddc18SKris Kennaway #include "opt_random_ip_id.h"
4153dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
42fbd1372aSJoerg Wunsch 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
4426f9a767SRodney W. Grimes #include <sys/systm.h>
45f0f6d643SLuigi Rizzo #include <sys/kernel.h>
464ed84624SRobert Watson #include <sys/mac.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
529d9edc56SMike Silbersack #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <netinet/in.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
62df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
63df8bae1dSRodney W. Grimes 
64134ea224SSam Leffler #ifdef PFIL_HOOKS
65134ea224SSam Leffler #include <net/pfil.h>
66134ea224SSam Leffler #endif
67134ea224SSam Leffler 
689c9137eaSGarrett Wollman #include <machine/in_cksum.h>
69df8bae1dSRodney W. Grimes 
70a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
7155166637SPoul-Henning Kamp 
726a800098SYoshinobu Inoue #ifdef IPSEC
736a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
746a800098SYoshinobu Inoue #include <netkey/key.h>
756a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG
766a800098SYoshinobu Inoue #include <netkey/key_debug.h>
776a800098SYoshinobu Inoue #else
786a800098SYoshinobu Inoue #define	KEYDEBUG(lev,arg)
796a800098SYoshinobu Inoue #endif
806a800098SYoshinobu Inoue #endif /*IPSEC*/
816a800098SYoshinobu Inoue 
82b9234fafSSam Leffler #ifdef FAST_IPSEC
83b9234fafSSam Leffler #include <netipsec/ipsec.h>
84b9234fafSSam Leffler #include <netipsec/xform.h>
85b9234fafSSam Leffler #include <netipsec/key.h>
86b9234fafSSam Leffler #endif /*FAST_IPSEC*/
87b9234fafSSam Leffler 
88cfe8b629SGarrett Wollman #include <netinet/ip_fw.h>
89ac9d7e26SMax Laier #include <netinet/ip_divert.h>
90b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
91b715f178SLuigi Rizzo 
922b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
932b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
94f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
95f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
962b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
97f9e354dfSJulian Elischer 
98f23b4c91SGarrett Wollman u_short ip_id;
99f23b4c91SGarrett Wollman 
10053dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
1019d9edc56SMike Silbersack int mbuf_frag_size = 0;
1029d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
1039d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1049d9edc56SMike Silbersack #endif
1059d9edc56SMike Silbersack 
1064d77a549SAlfred Perlstein static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
1074d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *);
108df8bae1dSRodney W. Grimes static void	ip_mloopback
1094d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
1100312fbe9SPoul-Henning Kamp static int	ip_getmoptions
1114d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions *);
1124d77a549SAlfred Perlstein static int	ip_pcbopts(int, struct mbuf **, struct mbuf *);
1130312fbe9SPoul-Henning Kamp static int	ip_setmoptions
1144d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions **);
115df8bae1dSRodney W. Grimes 
1164d77a549SAlfred Perlstein int	ip_optcopy(struct ip *, struct ip *);
117afed1b49SDarren Reed 
118afed1b49SDarren Reed 
11993e0e116SJulian Elischer extern	struct protosw inetsw[];
12093e0e116SJulian Elischer 
121df8bae1dSRodney W. Grimes /*
122df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
123df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
124df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
125df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1263de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1273de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
128df8bae1dSRodney W. Grimes  */
129df8bae1dSRodney W. Grimes int
130ac9d7e26SMax Laier ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
1311e78ac21SJeffrey Hsu 	int flags, struct ip_moptions *imo, struct inpcb *inp)
132df8bae1dSRodney W. Grimes {
1331e78ac21SJeffrey Hsu 	struct ip *ip;
1342b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
135ac9d7e26SMax Laier 	struct mbuf *m0;
13623bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
137df8bae1dSRodney W. Grimes 	int len, off, error = 0;
1382b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1393956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
140db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
1413efc3014SJulian Elischer 	struct in_addr pkt_dst;
142e3f406b3SRuslan Ermilov 	struct route iproute;
1434672d819SMax Laier 	struct m_tag *mtag, *dummytag;
14402c1c707SAndre Oppermann #ifdef IPSEC
1456a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1466a800098SYoshinobu Inoue #endif
147b9234fafSSam Leffler #ifdef FAST_IPSEC
148b9234fafSSam Leffler 	struct secpolicy *sp = NULL;
149b9234fafSSam Leffler 	struct tdb_ident *tdbi;
150b9234fafSSam Leffler 	int s;
151b9234fafSSam Leffler #endif /* FAST_IPSEC */
1522b25acc1SLuigi Rizzo 	struct ip_fw_args args;
1532b25acc1SLuigi Rizzo 	int src_was_INADDR_ANY = 0;	/* as the name says... */
154b715f178SLuigi Rizzo 
1552b25acc1SLuigi Rizzo 	args.eh = NULL;
1562b25acc1SLuigi Rizzo 	args.rule = NULL;
15736e8826fSMax Laier 
158ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
15936e8826fSMax Laier 
1602f3f1e67SDarren Reed 	args.next_hop = m_claim_next(m, PACKET_TAG_IPFORWARD);
161ac9d7e26SMax Laier 	dummytag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
162ac9d7e26SMax Laier 	if (dummytag != NULL) {
163ac9d7e26SMax Laier 		struct dn_pkt_tag *dt = (struct dn_pkt_tag *)(dummytag+1);
164ac9d7e26SMax Laier 		/*
165ac9d7e26SMax Laier 		 * Prevent lower layers from finding the tag
166ac9d7e26SMax Laier 		 * Cleanup and free is done below
167ac9d7e26SMax Laier 		 */
168ac9d7e26SMax Laier 		m_tag_unlink(m, dummytag);
16936e8826fSMax Laier 		/*
17036e8826fSMax Laier 		 * the packet was already tagged, so part of the
17136e8826fSMax Laier 		 * processing was already done, and we need to go down.
17236e8826fSMax Laier 		 * Get parameters from the header.
17336e8826fSMax Laier 		 */
174ac9d7e26SMax Laier 		args.rule = dt->rule;
175ac9d7e26SMax Laier 		ro = &(dt->ro);
176ac9d7e26SMax Laier 		dst = dt->dn_dst;
177ac9d7e26SMax Laier 		ifp = dt->ifp;
17836e8826fSMax Laier 	}
17902c1c707SAndre Oppermann 
18002c1c707SAndre Oppermann 	if (ro == NULL) {
18102c1c707SAndre Oppermann 		ro = &iproute;
18202c1c707SAndre Oppermann 		bzero(ro, sizeof (*ro));
18302c1c707SAndre Oppermann 	}
18402c1c707SAndre Oppermann 
18584843845SSam Leffler 	if (inp != NULL)
18684843845SSam Leffler 		INP_LOCK_ASSERT(inp);
1872b25acc1SLuigi Rizzo 
18836e8826fSMax Laier 	if (args.rule != NULL) {	/* dummynet already saw us */
189b715f178SLuigi Rizzo 		ip = mtod(m, struct ip *);
19053be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2 ;
1913956b023SLuigi Rizzo 		if (ro->ro_rt)
1929a10980eSJonathan Lemon 			ia = ifatoia(ro->ro_rt->rt_ifa);
193b715f178SLuigi Rizzo 		goto sendit;
1942b25acc1SLuigi Rizzo 	}
195db40007dSAndrew R. Reiter 
196df8bae1dSRodney W. Grimes 	if (opt) {
197cb7641e8SMaxim Konovalov 		len = 0;
198df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
199cb7641e8SMaxim Konovalov 		if (len != 0)
200df8bae1dSRodney W. Grimes 			hlen = len;
201df8bae1dSRodney W. Grimes 	}
202df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
2032b25acc1SLuigi Rizzo 	pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
2043efc3014SJulian Elischer 
205df8bae1dSRodney W. Grimes 	/*
2066e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
207e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
208e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
209e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
210e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
2116e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
2126e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
2136e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
2146e49b1feSGarrett Wollman 	 * <http://www.research.att.com/~smb/papers/fnat.pdf>.
215df8bae1dSRodney W. Grimes 	 */
216df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
21753be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
21853be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
21964dddc18SKris Kennaway #ifdef RANDOM_IP_ID
22064dddc18SKris Kennaway 		ip->ip_id = ip_randomid();
22164dddc18SKris Kennaway #else
222e0f630eaSAndre Oppermann 		ip->ip_id = htons(ip_id++);
22364dddc18SKris Kennaway #endif
224df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
225df8bae1dSRodney W. Grimes 	} else {
22653be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
227df8bae1dSRodney W. Grimes 	}
2289c9137eaSGarrett Wollman 
229df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
230df8bae1dSRodney W. Grimes 	/*
231df8bae1dSRodney W. Grimes 	 * If there is a cached route,
232df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
233df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
234a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
235a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
236df8bae1dSRodney W. Grimes 	 */
237df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
238a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
2393efc3014SJulian Elischer 			  dst->sin_addr.s_addr != pkt_dst.s_addr)) {
240df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
241df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
242df8bae1dSRodney W. Grimes 	}
2430b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
244a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
245df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
246df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2473efc3014SJulian Elischer 		dst->sin_addr = pkt_dst;
248df8bae1dSRodney W. Grimes 	}
249df8bae1dSRodney W. Grimes 	/*
250df8bae1dSRodney W. Grimes 	 * If routing to interface only,
251df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
252df8bae1dSRodney W. Grimes 	 */
253df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
2540b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2550b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
256df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
257df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
258df8bae1dSRodney W. Grimes 			goto bad;
259df8bae1dSRodney W. Grimes 		}
260df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
261df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2629f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2633afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
26438c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2653afefa39SDaniel C. Sobral 		/*
26638c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
26738c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2683afefa39SDaniel C. Sobral 		 */
26938c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
27038c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
27138c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
272df8bae1dSRodney W. Grimes 	} else {
2732c17fe93SGarrett Wollman 		/*
27497d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
27597d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
27697d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2772c17fe93SGarrett Wollman 		 */
2780b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
279e6e51f05SLuigi Rizzo 			rtalloc_ign(ro, 0);
2800b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
281df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
282df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
283df8bae1dSRodney W. Grimes 			goto bad;
284df8bae1dSRodney W. Grimes 		}
285df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
286df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
28797d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
288df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
289f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2909f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
291f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2929f9b3dc4SGarrett Wollman 		else
2939f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
294df8bae1dSRodney W. Grimes 	}
2953efc3014SJulian Elischer 	if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
296df8bae1dSRodney W. Grimes 		struct in_multi *inm;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
299df8bae1dSRodney W. Grimes 		/*
300df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
301df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
302df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
303df8bae1dSRodney W. Grimes 		 */
304df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
305df8bae1dSRodney W. Grimes 		/*
306df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
307df8bae1dSRodney W. Grimes 		 */
308df8bae1dSRodney W. Grimes 		if (imo != NULL) {
309df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
3101c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3111c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
312bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
313bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
314bbb4330bSLuigi Rizzo 				    INADDR_ANY;
315df8bae1dSRodney W. Grimes 		} else
316df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
317df8bae1dSRodney W. Grimes 		/*
318df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
319df8bae1dSRodney W. Grimes 		 */
3201c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
321df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
322df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
323df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
324df8bae1dSRodney W. Grimes 				goto bad;
325df8bae1dSRodney W. Grimes 			}
3261c5de19aSGarrett Wollman 		}
327df8bae1dSRodney W. Grimes 		/*
328df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
329df8bae1dSRodney W. Grimes 		 * of outgoing interface.
330df8bae1dSRodney W. Grimes 		 */
331df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
33238c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
33338c1bc35SRuslan Ermilov 			if (ia != NULL)
33438c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
335df8bae1dSRodney W. Grimes 		}
336df8bae1dSRodney W. Grimes 
3373efc3014SJulian Elischer 		IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
338df8bae1dSRodney W. Grimes 		if (inm != NULL &&
339df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
340df8bae1dSRodney W. Grimes 			/*
341df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
342df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
343df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
344df8bae1dSRodney W. Grimes 			 */
34586b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
346df8bae1dSRodney W. Grimes 		}
347df8bae1dSRodney W. Grimes 		else {
348df8bae1dSRodney W. Grimes 			/*
349df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
350df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
351df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
352df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
353df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
354df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
355df8bae1dSRodney W. Grimes 			 *
356df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
357df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
358df8bae1dSRodney W. Grimes 			 * if necessary.
359df8bae1dSRodney W. Grimes 			 */
360df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
361f0068c4aSGarrett Wollman 				/*
362bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
363f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
364f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
365f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
366f0068c4aSGarrett Wollman 				 */
367b124e4f2SGarrett Wollman 				if (!rsvp_on)
368f0068c4aSGarrett Wollman 					imo = NULL;
369bbb4330bSLuigi Rizzo 				if (ip_mforward &&
370bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
371df8bae1dSRodney W. Grimes 					m_freem(m);
372df8bae1dSRodney W. Grimes 					goto done;
373df8bae1dSRodney W. Grimes 				}
374df8bae1dSRodney W. Grimes 			}
375df8bae1dSRodney W. Grimes 		}
3765e9ae478SGarrett Wollman 
377df8bae1dSRodney W. Grimes 		/*
378df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
379df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
380df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
381df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
382df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
383df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
384df8bae1dSRodney W. Grimes 		 */
385f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
386df8bae1dSRodney W. Grimes 			m_freem(m);
387df8bae1dSRodney W. Grimes 			goto done;
388df8bae1dSRodney W. Grimes 		}
389df8bae1dSRodney W. Grimes 
390df8bae1dSRodney W. Grimes 		goto sendit;
391df8bae1dSRodney W. Grimes 	}
392df8bae1dSRodney W. Grimes #ifndef notdef
393df8bae1dSRodney W. Grimes 	/*
3942b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
3952b25acc1SLuigi Rizzo 	 * of the outoing interface. In case, keep note we did that, so
3962b25acc1SLuigi Rizzo 	 * if the the firewall changes the next-hop causing the output
3972b25acc1SLuigi Rizzo 	 * interface to change, we can fix that.
398df8bae1dSRodney W. Grimes 	 */
399f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
40038c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
40138c1bc35SRuslan Ermilov 		if (ia != NULL) {
402df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
4032b25acc1SLuigi Rizzo 			src_was_INADDR_ANY = 1;
404f9e354dfSJulian Elischer 		}
40538c1bc35SRuslan Ermilov 	}
406f9e354dfSJulian Elischer #endif /* notdef */
40702b199f1SMax Laier #ifdef ALTQ
40802b199f1SMax Laier 	/*
40902b199f1SMax Laier 	 * disable packet drop hack.
41002b199f1SMax Laier 	 * packetdrop should be done by queueing.
41102b199f1SMax Laier 	 */
41202b199f1SMax Laier #else /* !ALTQ */
413df8bae1dSRodney W. Grimes 	/*
414b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
415b5390296SDavid Greenman 	 *      the packet or packet fragments
416b5390296SDavid Greenman 	 */
417b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
418b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
419b5390296SDavid Greenman 			error = ENOBUFS;
42035609d45SJonathan Lemon 			ipstat.ips_odropped++;
421b5390296SDavid Greenman 			goto bad;
422b5390296SDavid Greenman 	}
42302b199f1SMax Laier #endif /* !ALTQ */
424b5390296SDavid Greenman 
425b5390296SDavid Greenman 	/*
426df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4278c3f5566SRuslan Ermilov 	 * verify user is allowed to send
428df8bae1dSRodney W. Grimes 	 * such a packet.
429df8bae1dSRodney W. Grimes 	 */
4309f9b3dc4SGarrett Wollman 	if (isbroadcast) {
431df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
432df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
433df8bae1dSRodney W. Grimes 			goto bad;
434df8bae1dSRodney W. Grimes 		}
435df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
436df8bae1dSRodney W. Grimes 			error = EACCES;
437df8bae1dSRodney W. Grimes 			goto bad;
438df8bae1dSRodney W. Grimes 		}
439df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
4401e78ac21SJeffrey Hsu 		if (ip->ip_len > ifp->if_mtu) {
441df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
442df8bae1dSRodney W. Grimes 			goto bad;
443df8bae1dSRodney W. Grimes 		}
4448afa2304SBruce M Simpson 		if (flags & IP_SENDONES)
4458afa2304SBruce M Simpson 			ip->ip_dst.s_addr = INADDR_BROADCAST;
446df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4479f9b3dc4SGarrett Wollman 	} else {
448df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4499f9b3dc4SGarrett Wollman 	}
450df8bae1dSRodney W. Grimes 
451f1743588SDarren Reed sendit:
45233841545SHajimu UMEMOTO #ifdef IPSEC
45333841545SHajimu UMEMOTO 	/* get SP for this packet */
454f073c60fSHajimu UMEMOTO 	if (inp == NULL)
4550f9ade71SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
4560f9ade71SHajimu UMEMOTO 		    flags, &error);
45733841545SHajimu UMEMOTO 	else
458f073c60fSHajimu UMEMOTO 		sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
45933841545SHajimu UMEMOTO 
46033841545SHajimu UMEMOTO 	if (sp == NULL) {
46133841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
46233841545SHajimu UMEMOTO 		goto bad;
46333841545SHajimu UMEMOTO 	}
46433841545SHajimu UMEMOTO 
46533841545SHajimu UMEMOTO 	error = 0;
46633841545SHajimu UMEMOTO 
46733841545SHajimu UMEMOTO 	/* check policy */
46833841545SHajimu UMEMOTO 	switch (sp->policy) {
46933841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
47033841545SHajimu UMEMOTO 		/*
47133841545SHajimu UMEMOTO 		 * This packet is just discarded.
47233841545SHajimu UMEMOTO 		 */
47333841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
47433841545SHajimu UMEMOTO 		goto bad;
47533841545SHajimu UMEMOTO 
47633841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
47733841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
4781cfd4b53SBruce M Simpson 	case IPSEC_POLICY_TCP:
47933841545SHajimu UMEMOTO 		/* no need to do IPsec. */
48033841545SHajimu UMEMOTO 		goto skip_ipsec;
48133841545SHajimu UMEMOTO 
48233841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
48333841545SHajimu UMEMOTO 		if (sp->req == NULL) {
48433841545SHajimu UMEMOTO 			/* acquire a policy */
48533841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
48633841545SHajimu UMEMOTO 			goto bad;
48733841545SHajimu UMEMOTO 		}
48833841545SHajimu UMEMOTO 		break;
48933841545SHajimu UMEMOTO 
49033841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
49133841545SHajimu UMEMOTO 	default:
49233841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
49333841545SHajimu UMEMOTO 	}
49433841545SHajimu UMEMOTO     {
49533841545SHajimu UMEMOTO 	struct ipsec_output_state state;
49633841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
49733841545SHajimu UMEMOTO 	state.m = m;
49833841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
49933841545SHajimu UMEMOTO 		state.ro = &iproute;
50033841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
50133841545SHajimu UMEMOTO 	} else
50233841545SHajimu UMEMOTO 		state.ro = ro;
50333841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
50433841545SHajimu UMEMOTO 
50533841545SHajimu UMEMOTO 	ip->ip_sum = 0;
50633841545SHajimu UMEMOTO 
50733841545SHajimu UMEMOTO 	/*
50833841545SHajimu UMEMOTO 	 * XXX
50933841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
51033841545SHajimu UMEMOTO 	 */
51133841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
51233841545SHajimu UMEMOTO 		in_delayed_cksum(m);
51333841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
51433841545SHajimu UMEMOTO 	}
51533841545SHajimu UMEMOTO 
516fd8e4ebcSMike Barcroft 	ip->ip_len = htons(ip->ip_len);
517fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
51833841545SHajimu UMEMOTO 
51933841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
52033841545SHajimu UMEMOTO 
52133841545SHajimu UMEMOTO 	m = state.m;
52233841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
52333841545SHajimu UMEMOTO 		/*
52433841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
52533841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
52633841545SHajimu UMEMOTO 		 */
52733841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
52833841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
52933841545SHajimu UMEMOTO 			ro = state.ro;
53033841545SHajimu UMEMOTO 		}
53133841545SHajimu UMEMOTO 	} else
53233841545SHajimu UMEMOTO 		ro = state.ro;
53333841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
53433841545SHajimu UMEMOTO 	if (error) {
53533841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
536ac9d7e26SMax Laier 		m = NULL;
53733841545SHajimu UMEMOTO 		switch (error) {
53833841545SHajimu UMEMOTO 		case EHOSTUNREACH:
53933841545SHajimu UMEMOTO 		case ENETUNREACH:
54033841545SHajimu UMEMOTO 		case EMSGSIZE:
54133841545SHajimu UMEMOTO 		case ENOBUFS:
54233841545SHajimu UMEMOTO 		case ENOMEM:
54333841545SHajimu UMEMOTO 			break;
54433841545SHajimu UMEMOTO 		default:
54533841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
54633841545SHajimu UMEMOTO 			/*fall through*/
54733841545SHajimu UMEMOTO 		case ENOENT:
54833841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
54933841545SHajimu UMEMOTO 			error = 0;
55033841545SHajimu UMEMOTO 			break;
55133841545SHajimu UMEMOTO 		}
55233841545SHajimu UMEMOTO 		goto bad;
55333841545SHajimu UMEMOTO 	}
55433841545SHajimu UMEMOTO 
55533841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
55633841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
55733841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
55833841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
55933841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
56033841545SHajimu UMEMOTO 			printf("ip_output: "
56133841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
56233841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
56333841545SHajimu UMEMOTO 			goto bad;
56433841545SHajimu UMEMOTO 		}
56533841545SHajimu UMEMOTO 	} else {
56670dbc6cbSHajimu UMEMOTO 		if (state.encap) {
56733841545SHajimu UMEMOTO 			ia = ifatoia(ro->ro_rt->rt_ifa);
56833841545SHajimu UMEMOTO 			ifp = ro->ro_rt->rt_ifp;
56933841545SHajimu UMEMOTO 		}
57070dbc6cbSHajimu UMEMOTO 	}
57170dbc6cbSHajimu UMEMOTO     }
57233841545SHajimu UMEMOTO 
57333841545SHajimu UMEMOTO 	/* make it flipped, again. */
574fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
575fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
57633841545SHajimu UMEMOTO skip_ipsec:
57733841545SHajimu UMEMOTO #endif /*IPSEC*/
578b9234fafSSam Leffler #ifdef FAST_IPSEC
579b9234fafSSam Leffler 	/*
580b9234fafSSam Leffler 	 * Check the security policy (SP) for the packet and, if
581b9234fafSSam Leffler 	 * required, do IPsec-related processing.  There are two
582b9234fafSSam Leffler 	 * cases here; the first time a packet is sent through
583b9234fafSSam Leffler 	 * it will be untagged and handled by ipsec4_checkpolicy.
584b9234fafSSam Leffler 	 * If the packet is resubmitted to ip_output (e.g. after
585b9234fafSSam Leffler 	 * AH, ESP, etc. processing), there will be a tag to bypass
586b9234fafSSam Leffler 	 * the lookup and related policy checking.
587b9234fafSSam Leffler 	 */
588b9234fafSSam Leffler 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
589b9234fafSSam Leffler 	s = splnet();
590b9234fafSSam Leffler 	if (mtag != NULL) {
591b9234fafSSam Leffler 		tdbi = (struct tdb_ident *)(mtag + 1);
592b9234fafSSam Leffler 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
593b9234fafSSam Leffler 		if (sp == NULL)
594b9234fafSSam Leffler 			error = -EINVAL;	/* force silent drop */
595b9234fafSSam Leffler 		m_tag_delete(m, mtag);
596b9234fafSSam Leffler 	} else {
597b9234fafSSam Leffler 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
598b9234fafSSam Leffler 					&error, inp);
599b9234fafSSam Leffler 	}
600b9234fafSSam Leffler 	/*
601b9234fafSSam Leffler 	 * There are four return cases:
602b9234fafSSam Leffler 	 *    sp != NULL	 	    apply IPsec policy
603b9234fafSSam Leffler 	 *    sp == NULL, error == 0	    no IPsec handling needed
604b9234fafSSam Leffler 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
605b9234fafSSam Leffler 	 *    sp == NULL, error != 0	    discard packet, report error
606b9234fafSSam Leffler 	 */
607b9234fafSSam Leffler 	if (sp != NULL) {
608b9234fafSSam Leffler 		/* Loop detection, check if ipsec processing already done */
609b9234fafSSam Leffler 		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
610b9234fafSSam Leffler 		for (mtag = m_tag_first(m); mtag != NULL;
611b9234fafSSam Leffler 		     mtag = m_tag_next(m, mtag)) {
612b9234fafSSam Leffler 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
613b9234fafSSam Leffler 				continue;
614b9234fafSSam Leffler 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
615b9234fafSSam Leffler 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
616b9234fafSSam Leffler 				continue;
617b9234fafSSam Leffler 			/*
618b9234fafSSam Leffler 			 * Check if policy has an SA associated with it.
619b9234fafSSam Leffler 			 * This can happen when an SP has yet to acquire
620b9234fafSSam Leffler 			 * an SA; e.g. on first reference.  If it occurs,
621b9234fafSSam Leffler 			 * then we let ipsec4_process_packet do its thing.
622b9234fafSSam Leffler 			 */
623b9234fafSSam Leffler 			if (sp->req->sav == NULL)
624b9234fafSSam Leffler 				break;
625b9234fafSSam Leffler 			tdbi = (struct tdb_ident *)(mtag + 1);
626b9234fafSSam Leffler 			if (tdbi->spi == sp->req->sav->spi &&
627b9234fafSSam Leffler 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
628ab94ca3cSSam Leffler 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
629b9234fafSSam Leffler 				 sizeof (union sockaddr_union)) == 0) {
630b9234fafSSam Leffler 				/*
631b9234fafSSam Leffler 				 * No IPsec processing is needed, free
632b9234fafSSam Leffler 				 * reference to SP.
633b9234fafSSam Leffler 				 *
634b9234fafSSam Leffler 				 * NB: null pointer to avoid free at
635b9234fafSSam Leffler 				 *     done: below.
636b9234fafSSam Leffler 				 */
637b9234fafSSam Leffler 				KEY_FREESP(&sp), sp = NULL;
638b9234fafSSam Leffler 				splx(s);
639b9234fafSSam Leffler 				goto spd_done;
640b9234fafSSam Leffler 			}
641b9234fafSSam Leffler 		}
642b9234fafSSam Leffler 
643b9234fafSSam Leffler 		/*
644b9234fafSSam Leffler 		 * Do delayed checksums now because we send before
645b9234fafSSam Leffler 		 * this is done in the normal processing path.
646b9234fafSSam Leffler 		 */
647b9234fafSSam Leffler 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
648b9234fafSSam Leffler 			in_delayed_cksum(m);
649b9234fafSSam Leffler 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
650b9234fafSSam Leffler 		}
651b9234fafSSam Leffler 
652b9234fafSSam Leffler 		ip->ip_len = htons(ip->ip_len);
653b9234fafSSam Leffler 		ip->ip_off = htons(ip->ip_off);
654b9234fafSSam Leffler 
655b9234fafSSam Leffler 		/* NB: callee frees mbuf */
656b9234fafSSam Leffler 		error = ipsec4_process_packet(m, sp->req, flags, 0);
6579359ad86SSam Leffler 		/*
6589359ad86SSam Leffler 		 * Preserve KAME behaviour: ENOENT can be returned
6599359ad86SSam Leffler 		 * when an SA acquire is in progress.  Don't propagate
6609359ad86SSam Leffler 		 * this to user-level; it confuses applications.
6619359ad86SSam Leffler 		 *
6629359ad86SSam Leffler 		 * XXX this will go away when the SADB is redone.
6639359ad86SSam Leffler 		 */
6649359ad86SSam Leffler 		if (error == ENOENT)
6659359ad86SSam Leffler 			error = 0;
666b9234fafSSam Leffler 		splx(s);
667b9234fafSSam Leffler 		goto done;
668b9234fafSSam Leffler 	} else {
669b9234fafSSam Leffler 		splx(s);
670b9234fafSSam Leffler 
671b9234fafSSam Leffler 		if (error != 0) {
672b9234fafSSam Leffler 			/*
673b9234fafSSam Leffler 			 * Hack: -EINVAL is used to signal that a packet
674b9234fafSSam Leffler 			 * should be silently discarded.  This is typically
675b9234fafSSam Leffler 			 * because we asked key management for an SA and
676b9234fafSSam Leffler 			 * it was delayed (e.g. kicked up to IKE).
677b9234fafSSam Leffler 			 */
678b9234fafSSam Leffler 			if (error == -EINVAL)
679b9234fafSSam Leffler 				error = 0;
680b9234fafSSam Leffler 			goto bad;
681b9234fafSSam Leffler 		} else {
682b9234fafSSam Leffler 			/* No IPsec processing for this packet. */
683b9234fafSSam Leffler 		}
684b9234fafSSam Leffler #ifdef notyet
685b9234fafSSam Leffler 		/*
686b9234fafSSam Leffler 		 * If deferred crypto processing is needed, check that
687b9234fafSSam Leffler 		 * the interface supports it.
688b9234fafSSam Leffler 		 */
689b9234fafSSam Leffler 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
690b9234fafSSam Leffler 		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
691b9234fafSSam Leffler 			/* notify IPsec to do its own crypto */
692b9234fafSSam Leffler 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
693b9234fafSSam Leffler 			error = EHOSTUNREACH;
694b9234fafSSam Leffler 			goto bad;
695b9234fafSSam Leffler 		}
696b9234fafSSam Leffler #endif
697b9234fafSSam Leffler 	}
698b9234fafSSam Leffler spd_done:
699b9234fafSSam Leffler #endif /* FAST_IPSEC */
70033841545SHajimu UMEMOTO 
701fed1c7e9SSøren Schmidt 	/*
702fed1c7e9SSøren Schmidt 	 * IpHack's section.
703fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
704e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
705fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
706fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
707fed1c7e9SSøren Schmidt 	 */
708c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
709c4ac87eaSDarren Reed 	/*
710c4ac87eaSDarren Reed 	 * Run through list of hooks for output packets.
711c4ac87eaSDarren Reed 	 */
712134ea224SSam Leffler 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT);
713134ea224SSam Leffler 	if (error != 0 || m == NULL)
714c4ac87eaSDarren Reed 		goto done;
715c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
716c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
717fed1c7e9SSøren Schmidt 
718df8bae1dSRodney W. Grimes 	/*
719e7319babSPoul-Henning Kamp 	 * Check with the firewall...
7203efc3014SJulian Elischer 	 * but not if we are already being fwd'd from a firewall.
721e7319babSPoul-Henning Kamp 	 */
7222b25acc1SLuigi Rizzo 	if (fw_enable && IPFW_LOADED && !args.next_hop) {
7239de9737fSPeter Wemm 		struct sockaddr_in *old = dst;
724b715f178SLuigi Rizzo 
7252b25acc1SLuigi Rizzo 		args.m = m;
7262b25acc1SLuigi Rizzo 		args.next_hop = dst;
7272b25acc1SLuigi Rizzo 		args.oif = ifp;
7282b25acc1SLuigi Rizzo 		off = ip_fw_chk_ptr(&args);
7292b25acc1SLuigi Rizzo 		m = args.m;
7302b25acc1SLuigi Rizzo 		dst = args.next_hop;
7312b25acc1SLuigi Rizzo 
732b715f178SLuigi Rizzo                 /*
733b715f178SLuigi Rizzo 		 * On return we must do the following:
734507b4b54SLuigi Rizzo 		 * m == NULL	-> drop the pkt (old interface, deprecated)
735aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
736b715f178SLuigi Rizzo 		 * 1<=off<= 0xffff		-> DIVERT
737aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
738aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
739b715f178SLuigi Rizzo 		 * dst != old			-> IPFIREWALL_FORWARD
740b715f178SLuigi Rizzo 		 * off==0, dst==old		-> accept
741aa1f5daaSJonathan Lemon 		 * If some of the above modules are not compiled in, then
742b715f178SLuigi Rizzo 		 * we should't have to check the corresponding condition
743b715f178SLuigi Rizzo 		 * (because the ipfw control socket should not accept
744b715f178SLuigi Rizzo 		 * unsupported rules), but better play safe and drop
745b715f178SLuigi Rizzo 		 * packets in case of doubt.
746b715f178SLuigi Rizzo 		 */
747d60315beSLuigi Rizzo 		if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
748507b4b54SLuigi Rizzo 			if (m)
749507b4b54SLuigi Rizzo 				m_freem(m);
750507b4b54SLuigi Rizzo 			error = EACCES;
751507b4b54SLuigi Rizzo 			goto done;
752507b4b54SLuigi Rizzo 		}
753d60315beSLuigi Rizzo 		ip = mtod(m, struct ip *);
754b715f178SLuigi Rizzo 		if (off == 0 && dst == old)		/* common case */
755b715f178SLuigi Rizzo 			goto pass;
7567b109fa4SLuigi Rizzo                 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
757b715f178SLuigi Rizzo 			/*
758b715f178SLuigi Rizzo 			 * pass the pkt to dummynet. Need to include
759f0a53591SLuigi Rizzo 			 * pipe number, m, ifp, ro, dst because these are
760b715f178SLuigi Rizzo 			 * not recomputed in the next pass.
761b715f178SLuigi Rizzo 			 * All other parameters have been already used and
762b715f178SLuigi Rizzo 			 * so they are not needed anymore.
763b715f178SLuigi Rizzo 			 * XXX note: if the ifp or ro entry are deleted
764b715f178SLuigi Rizzo 			 * while a pkt is in dummynet, we are in trouble!
765b715f178SLuigi Rizzo 			 */
7662b25acc1SLuigi Rizzo 			args.ro = ro;
7672b25acc1SLuigi Rizzo 			args.dst = dst;
7682b25acc1SLuigi Rizzo 			args.flags = flags;
7692b25acc1SLuigi Rizzo 
7702b25acc1SLuigi Rizzo 			error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT,
7712b25acc1SLuigi Rizzo 				&args);
772b715f178SLuigi Rizzo 			goto done;
773b715f178SLuigi Rizzo 		}
774b715f178SLuigi Rizzo #ifdef IPDIVERT
7758948e4baSArchie Cobbs 		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
776ac9d7e26SMax Laier 			struct mbuf *clone;
7778948e4baSArchie Cobbs 
7788948e4baSArchie Cobbs 			/* Clone packet if we're doing a 'tee' */
7798948e4baSArchie Cobbs 			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
780ac9d7e26SMax Laier 				clone = divert_clone(m);
781ac9d7e26SMax Laier 			else
782ac9d7e26SMax Laier 				clone = NULL;
7838948e4baSArchie Cobbs 
7848948e4baSArchie Cobbs 			/* Restore packet header fields to original values */
785fd8e4ebcSMike Barcroft 			ip->ip_len = htons(ip->ip_len);
786fd8e4ebcSMike Barcroft 			ip->ip_off = htons(ip->ip_off);
7878948e4baSArchie Cobbs 
7888948e4baSArchie Cobbs 			/* Deliver packet to divert input routine */
789ac9d7e26SMax Laier 			divert_packet(m, 0);
7908948e4baSArchie Cobbs 
7918948e4baSArchie Cobbs 			/* If 'tee', continue with original packet */
7928948e4baSArchie Cobbs 			if (clone != NULL) {
7938948e4baSArchie Cobbs 				m = clone;
7948948e4baSArchie Cobbs 				ip = mtod(m, struct ip *);
7958948e4baSArchie Cobbs 				goto pass;
7968948e4baSArchie Cobbs 			}
797b715f178SLuigi Rizzo 			goto done;
798b715f178SLuigi Rizzo 		}
799b715f178SLuigi Rizzo #endif
800b715f178SLuigi Rizzo 
8012b25acc1SLuigi Rizzo 		/* IPFIREWALL_FORWARD */
8022b25acc1SLuigi Rizzo 		/*
8032b25acc1SLuigi Rizzo 		 * Check dst to make sure it is directly reachable on the
804f9e354dfSJulian Elischer 		 * interface we previously thought it was.
805f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
806f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
807f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
808f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
809f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
810f9e354dfSJulian Elischer 		 * And I'm babbling.
811f9e354dfSJulian Elischer 		 */
8122b25acc1SLuigi Rizzo 		if (off == 0 && old != dst) { /* FORWARD, dst has changed */
8132b25acc1SLuigi Rizzo #if 0
8142b25acc1SLuigi Rizzo 			/*
8152b25acc1SLuigi Rizzo 			 * XXX To improve readability, this block should be
8162b25acc1SLuigi Rizzo 			 * changed into a function call as below:
8172b25acc1SLuigi Rizzo 			 */
8182b25acc1SLuigi Rizzo 			error = ip_ipforward(&m, &dst, &ifp);
8192b25acc1SLuigi Rizzo 			if (error)
8202b25acc1SLuigi Rizzo 				goto bad;
8212b25acc1SLuigi Rizzo 			if (m == NULL) /* ip_input consumed the mbuf */
8222b25acc1SLuigi Rizzo 				goto done;
8232b25acc1SLuigi Rizzo #else
824f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
825f9e354dfSJulian Elischer 
8262b25acc1SLuigi Rizzo 			/*
8272b25acc1SLuigi Rizzo 			 * XXX sro_fwd below is static, and a pointer
8282b25acc1SLuigi Rizzo 			 * to it gets passed to routines downstream.
8292b25acc1SLuigi Rizzo 			 * This could have surprisingly bad results in
8302b25acc1SLuigi Rizzo 			 * practice, because its content is overwritten
8312b25acc1SLuigi Rizzo 			 * by subsequent packets.
832a5053398SAndre Oppermann 			 * XXX: Breaks on SMP and possibly preemption!
8332b25acc1SLuigi Rizzo 			 */
834f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
8352b25acc1SLuigi Rizzo 			static struct route sro_fwd;
8362b25acc1SLuigi Rizzo 			struct route *ro_fwd = &sro_fwd;
8372b25acc1SLuigi Rizzo 
8382b25acc1SLuigi Rizzo #if 0
8392b25acc1SLuigi Rizzo 			print_ip("IPFIREWALL_FORWARD: New dst ip: ",
8402b25acc1SLuigi Rizzo 			    dst->sin_addr, "\n");
841f9e354dfSJulian Elischer #endif
8422b25acc1SLuigi Rizzo 
843f9e354dfSJulian Elischer 			/*
844f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
8452b25acc1SLuigi Rizzo 			 * to a local socket. If so, then we should somehow
846f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
847f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
848f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
849f9e354dfSJulian Elischer 			 * you want to forward until they are being output
850f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
851f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
852f9e354dfSJulian Elischer 			 * we would not be able to control what happens
853f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
8549d5abbddSJens Schweikhardt 			 * it is done through an ISR.
855f9e354dfSJulian Elischer 			 */
856ca925d9cSJonathan Lemon 			LIST_FOREACH(ia,
857ca925d9cSJonathan Lemon 			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
858f9e354dfSJulian Elischer 				/*
859f9e354dfSJulian Elischer 				 * If the addr to forward to is one
860f9e354dfSJulian Elischer 				 * of ours, we pretend to
861f9e354dfSJulian Elischer 				 * be the destination for this packet.
862f9e354dfSJulian Elischer 				 */
863f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
864f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
865f9e354dfSJulian Elischer 					break;
866f9e354dfSJulian Elischer 			}
8672b25acc1SLuigi Rizzo 			if (ia) {	/* tell ip_input "dont filter" */
8684672d819SMax Laier 				mtag = m_tag_get(
869ac9d7e26SMax Laier 				    PACKET_TAG_IPFORWARD,
870ac9d7e26SMax Laier 				    sizeof(struct sockaddr_in *), M_NOWAIT);
871ac9d7e26SMax Laier 				if (mtag == NULL) {
872ac9d7e26SMax Laier 					error = ENOBUFS;
873ac9d7e26SMax Laier 					goto bad;
874ac9d7e26SMax Laier 				}
875ac9d7e26SMax Laier 				*(struct sockaddr_in **)(mtag+1) =
876ac9d7e26SMax Laier 				    args.next_hop;
877ac9d7e26SMax Laier 				m_tag_prepend(m, mtag);
8782b25acc1SLuigi Rizzo 
879f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
880f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
88120c822f3SJonathan Lemon 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
88220c822f3SJonathan Lemon 					m->m_pkthdr.csum_flags |=
88320c822f3SJonathan Lemon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
884ac9d7e26SMax Laier 					m->m_pkthdr.csum_data = 0xffff;
88520c822f3SJonathan Lemon 				}
88620c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
88720c822f3SJonathan Lemon 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
888fd8e4ebcSMike Barcroft 				ip->ip_len = htons(ip->ip_len);
889fd8e4ebcSMike Barcroft 				ip->ip_off = htons(ip->ip_off);
890ac9d7e26SMax Laier 				ip_input(m);
891f9e354dfSJulian Elischer 				goto done;
892f9e354dfSJulian Elischer 			}
89397d8d152SAndre Oppermann 			/*
89497d8d152SAndre Oppermann 			 * Some of the logic for this was
895f9e354dfSJulian Elischer 			 * nicked from above.
896f9e354dfSJulian Elischer 			 */
897f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
898f9e354dfSJulian Elischer 
899f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
90097d8d152SAndre Oppermann 			rtalloc_ign(ro_fwd, RTF_CLONING);
901f9e354dfSJulian Elischer 
9020b17fba7SAndre Oppermann 			if (ro_fwd->ro_rt == NULL) {
903f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
904f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
905f9e354dfSJulian Elischer 				goto bad;
906f9e354dfSJulian Elischer 			}
907f9e354dfSJulian Elischer 
908f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
909f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
91097d8d152SAndre Oppermann 			ro_fwd->ro_rt->rt_rmx.rmx_pksent++;
911f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
9122b25acc1SLuigi Rizzo 				dst = (struct sockaddr_in *)
9132b25acc1SLuigi Rizzo 					ro_fwd->ro_rt->rt_gateway;
914f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
915f9e354dfSJulian Elischer 				isbroadcast =
916f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
917f9e354dfSJulian Elischer 			else
918f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
9193f9e3122SYaroslav Tykhiy 			if (ro->ro_rt)
920f9e354dfSJulian Elischer 				RTFREE(ro->ro_rt);
921f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
922f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
923f9e354dfSJulian Elischer 
9242b25acc1SLuigi Rizzo #endif	/* ... block to be put into a function */
925f9e354dfSJulian Elischer 			/*
926f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
927f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
928f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
929f9e354dfSJulian Elischer 			 */
9302b25acc1SLuigi Rizzo 			if (src_was_INADDR_ANY)
931f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
932b715f178SLuigi Rizzo 			goto pass ;
933f9e354dfSJulian Elischer 		}
9342b25acc1SLuigi Rizzo 
935b715f178SLuigi Rizzo                 /*
936b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
937b715f178SLuigi Rizzo                  * we have to drop the pkt
938b715f178SLuigi Rizzo                  */
939b715f178SLuigi Rizzo 		m_freem(m);
940b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
941b715f178SLuigi Rizzo                 goto done;
94293e0e116SJulian Elischer 	}
943e7319babSPoul-Henning Kamp 
944b715f178SLuigi Rizzo pass:
94551c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
94651c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
94751c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
94851c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
94951c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
95051c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
95151c8ec4aSRuslan Ermilov 			goto bad;
95251c8ec4aSRuslan Ermilov 		}
95351c8ec4aSRuslan Ermilov 	}
95451c8ec4aSRuslan Ermilov 
955206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
956206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
957db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
958db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
959db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
960db4f9cc7SJonathan Lemon 	}
961206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
962db4f9cc7SJonathan Lemon 
963e7319babSPoul-Henning Kamp 	/*
964db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
965db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
966df8bae1dSRodney W. Grimes 	 */
9672683ceb6SAndre Oppermann 	if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
9682683ceb6SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0))) {
969fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
970fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
971df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
97253be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
973df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
9745da9f8faSJosef Karthauser 
9755da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
97638c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
9775da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
9785da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
9795da9f8faSJosef Karthauser 		}
9805da9f8faSJosef Karthauser 
98133841545SHajimu UMEMOTO #ifdef IPSEC
98233841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
98333841545SHajimu UMEMOTO 		ipsec_delaux(m);
98433841545SHajimu UMEMOTO #endif
98533841545SHajimu UMEMOTO 
98653dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
9873390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
9883390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
9899d9edc56SMike Silbersack #endif
990df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
991df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
992df8bae1dSRodney W. Grimes 		goto done;
993df8bae1dSRodney W. Grimes 	}
9941e78ac21SJeffrey Hsu 
995df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
996df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
9973d1f141bSGarrett Wollman 		/*
9983d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
9993d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
10003d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
10013d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
10023d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
10033d1f141bSGarrett Wollman 		 */
10041e78ac21SJeffrey Hsu 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
10051e78ac21SJeffrey Hsu 		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
10063d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
10073d1f141bSGarrett Wollman 		}
1008df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
1009df8bae1dSRodney W. Grimes 		goto bad;
1010df8bae1dSRodney W. Grimes 	}
10111e78ac21SJeffrey Hsu 
10121e78ac21SJeffrey Hsu 	/*
10131e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
10141e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
10151e78ac21SJeffrey Hsu 	 */
10161e78ac21SJeffrey Hsu 	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
10171e78ac21SJeffrey Hsu 	if (error)
1018df8bae1dSRodney W. Grimes 		goto bad;
10191e78ac21SJeffrey Hsu 	for (; m; m = m0) {
1020df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
1021b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
102233841545SHajimu UMEMOTO #ifdef IPSEC
102333841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
102433841545SHajimu UMEMOTO 		ipsec_delaux(m);
102533841545SHajimu UMEMOTO #endif
102607203494SDaniel C. Sobral 		if (error == 0) {
1027fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
102807203494SDaniel C. Sobral 			if (ia != NULL) {
1029fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
1030fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
103107203494SDaniel C. Sobral 			}
1032fe937674SJosef Karthauser 
1033df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
1034df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
1035fe937674SJosef Karthauser 		} else
1036df8bae1dSRodney W. Grimes 			m_freem(m);
1037df8bae1dSRodney W. Grimes 	}
1038df8bae1dSRodney W. Grimes 
1039df8bae1dSRodney W. Grimes 	if (error == 0)
1040df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
10411e78ac21SJeffrey Hsu 
1042df8bae1dSRodney W. Grimes done:
10436a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
10446a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
10456a800098SYoshinobu Inoue 		ro->ro_rt = NULL;
10466a800098SYoshinobu Inoue 	}
1047ac9d7e26SMax Laier 	if (dummytag) {
1048ac9d7e26SMax Laier 		struct dn_pkt_tag *dt = (struct dn_pkt_tag *)(dummytag+1);
1049ac9d7e26SMax Laier 		if (dt->ro.ro_rt)
1050ac9d7e26SMax Laier 			RTFREE(dt->ro.ro_rt);
1051ac9d7e26SMax Laier 		m_tag_free(dummytag);
1052ac9d7e26SMax Laier 	}
105302c1c707SAndre Oppermann #ifdef IPSEC
10546a800098SYoshinobu Inoue 	if (sp != NULL) {
10556a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
10566a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
10576a800098SYoshinobu Inoue 		key_freesp(sp);
10586a800098SYoshinobu Inoue 	}
10591e78ac21SJeffrey Hsu #endif
1060b9234fafSSam Leffler #ifdef FAST_IPSEC
1061b9234fafSSam Leffler 	if (sp != NULL)
1062b9234fafSSam Leffler 		KEY_FREESP(&sp);
10631e78ac21SJeffrey Hsu #endif
1064df8bae1dSRodney W. Grimes 	return (error);
1065df8bae1dSRodney W. Grimes bad:
10663528d68fSBill Paul 	m_freem(m);
1067df8bae1dSRodney W. Grimes 	goto done;
1068df8bae1dSRodney W. Grimes }
1069df8bae1dSRodney W. Grimes 
10701e78ac21SJeffrey Hsu /*
10711e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
10721e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
10731e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
10741e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
10751e78ac21SJeffrey Hsu  *
10761e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
10771e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
10781e78ac21SJeffrey Hsu  */
10791e78ac21SJeffrey Hsu int
10801e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
10811e78ac21SJeffrey Hsu 	    u_long if_hwassist_flags, int sw_csum)
10821e78ac21SJeffrey Hsu {
10831e78ac21SJeffrey Hsu 	int error = 0;
10841e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
10851e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
10861e78ac21SJeffrey Hsu 	int off;
10871e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
10881e78ac21SJeffrey Hsu 	int firstlen;
10891e78ac21SJeffrey Hsu 	struct mbuf **mnext;
10901e78ac21SJeffrey Hsu 	int nfrags;
10911e78ac21SJeffrey Hsu 
10921e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
10931e78ac21SJeffrey Hsu 		ipstat.ips_cantfrag++;
10941e78ac21SJeffrey Hsu 		return EMSGSIZE;
10951e78ac21SJeffrey Hsu 	}
10961e78ac21SJeffrey Hsu 
10971e78ac21SJeffrey Hsu 	/*
10981e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
10991e78ac21SJeffrey Hsu 	 */
11001e78ac21SJeffrey Hsu 	if (len < 8)
11011e78ac21SJeffrey Hsu 		return EMSGSIZE;
11021e78ac21SJeffrey Hsu 
11031e78ac21SJeffrey Hsu 	/*
11041e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
11051e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
11061e78ac21SJeffrey Hsu 	 */
11071e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
11081e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
11091e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
11101e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
11111e78ac21SJeffrey Hsu 	}
11121e78ac21SJeffrey Hsu 
11131e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
11141e78ac21SJeffrey Hsu 		/*
11151e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
11161e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
11171e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
11181e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
11191e78ac21SJeffrey Hsu 		 *
11201e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
11211e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
11221e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
11231e78ac21SJeffrey Hsu 		 */
11241e78ac21SJeffrey Hsu 		int newlen;
11251e78ac21SJeffrey Hsu 		struct mbuf *m;
11261e78ac21SJeffrey Hsu 
11271e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
11281e78ac21SJeffrey Hsu 			off += m->m_len;
11291e78ac21SJeffrey Hsu 
11301e78ac21SJeffrey Hsu 		/*
11311e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
11321e78ac21SJeffrey Hsu 		 * 8-byte boundary
11331e78ac21SJeffrey Hsu 		 */
11341e78ac21SJeffrey Hsu 		if (off < hlen)
11351e78ac21SJeffrey Hsu 			goto smart_frag_failure;
11361e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
11371e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
11381e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
11391e78ac21SJeffrey Hsu 			/* we failed, go back the default */
11401e78ac21SJeffrey Hsu smart_frag_failure:
11411e78ac21SJeffrey Hsu 			newlen = len;
11421e78ac21SJeffrey Hsu 			off = hlen + len;
11431e78ac21SJeffrey Hsu 		}
11441e78ac21SJeffrey Hsu 		len = newlen;
11451e78ac21SJeffrey Hsu 
11461e78ac21SJeffrey Hsu 	} else {
11471e78ac21SJeffrey Hsu 		off = hlen + len;
11481e78ac21SJeffrey Hsu 	}
11491e78ac21SJeffrey Hsu 
11501e78ac21SJeffrey Hsu 	firstlen = off - hlen;
11511e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
11521e78ac21SJeffrey Hsu 
11531e78ac21SJeffrey Hsu 	/*
11541e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
11551e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
11561e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
11571e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
11581e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
11591e78ac21SJeffrey Hsu 	 */
11601e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
11611e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
11621e78ac21SJeffrey Hsu 		struct mbuf *m;
11631e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
11641e78ac21SJeffrey Hsu 
11651e78ac21SJeffrey Hsu 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
11660b17fba7SAndre Oppermann 		if (m == NULL) {
11671e78ac21SJeffrey Hsu 			error = ENOBUFS;
11681e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
11691e78ac21SJeffrey Hsu 			goto done;
11701e78ac21SJeffrey Hsu 		}
11711e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
11721e78ac21SJeffrey Hsu 		/*
11731e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
11741e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
11751e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
11761e78ac21SJeffrey Hsu 		 */
11771e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
11781e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
11791e78ac21SJeffrey Hsu 		*mhip = *ip;
11801e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
11811e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
11821e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
11831e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
11841e78ac21SJeffrey Hsu 		}
11851e78ac21SJeffrey Hsu 		m->m_len = mhlen;
11861e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
11871e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
11881e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
11891e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
11901e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
11911e78ac21SJeffrey Hsu 		} else
11921e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
11931e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
11941e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
11950b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
11961e78ac21SJeffrey Hsu 			m_free(m);
11971e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
11981e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
11991e78ac21SJeffrey Hsu 			goto done;
12001e78ac21SJeffrey Hsu 		}
12011e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
12021e78ac21SJeffrey Hsu 		m->m_pkthdr.rcvif = (struct ifnet *)0;
12031e78ac21SJeffrey Hsu #ifdef MAC
12041e78ac21SJeffrey Hsu 		mac_create_fragment(m0, m);
12051e78ac21SJeffrey Hsu #endif
12061e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
12071e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
12081e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
12091e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
12101e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
12111e78ac21SJeffrey Hsu 		*mnext = m;
12121e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
12131e78ac21SJeffrey Hsu 	}
12141e78ac21SJeffrey Hsu 	ipstat.ips_ofragments += nfrags;
12151e78ac21SJeffrey Hsu 
12161e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
12171e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
12181e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
12191e78ac21SJeffrey Hsu 
12201e78ac21SJeffrey Hsu 	/*
12211e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
12221e78ac21SJeffrey Hsu 	 * and updating header.
12231e78ac21SJeffrey Hsu 	 */
12241e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
12251e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
12261e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
12271e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
12281e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
12291e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
12301e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
12311e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
12321e78ac21SJeffrey Hsu 
12331e78ac21SJeffrey Hsu done:
12341e78ac21SJeffrey Hsu 	*m_frag = m0;
12351e78ac21SJeffrey Hsu 	return error;
12361e78ac21SJeffrey Hsu }
12371e78ac21SJeffrey Hsu 
12381c238475SJonathan Lemon void
1239db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1240db4f9cc7SJonathan Lemon {
1241db4f9cc7SJonathan Lemon 	struct ip *ip;
1242db4f9cc7SJonathan Lemon 	u_short csum, offset;
1243db4f9cc7SJonathan Lemon 
1244db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
124553be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
1246db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1247206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1248206a3274SRuslan Ermilov 		csum = 0xffff;
1249db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1250db4f9cc7SJonathan Lemon 
1251db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1252db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1253db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1254db4f9cc7SJonathan Lemon 		/*
1255db4f9cc7SJonathan Lemon 		 * XXX
1256db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1257db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1258db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1259db4f9cc7SJonathan Lemon 		 */
1260db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1261db4f9cc7SJonathan Lemon 	}
1262db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1263db4f9cc7SJonathan Lemon }
1264db4f9cc7SJonathan Lemon 
1265df8bae1dSRodney W. Grimes /*
1266df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1267df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1268df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1269072b9b24SPaul Traina  *
1270072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1271df8bae1dSRodney W. Grimes  */
1272df8bae1dSRodney W. Grimes static struct mbuf *
1273b375c9ecSLuigi Rizzo ip_insertoptions(m, opt, phlen)
1274b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1275b375c9ecSLuigi Rizzo 	struct mbuf *opt;
1276b375c9ecSLuigi Rizzo 	int *phlen;
1277df8bae1dSRodney W. Grimes {
1278b375c9ecSLuigi Rizzo 	register struct ipoption *p = mtod(opt, struct ipoption *);
1279df8bae1dSRodney W. Grimes 	struct mbuf *n;
1280b375c9ecSLuigi Rizzo 	register struct ip *ip = mtod(m, struct ip *);
1281df8bae1dSRodney W. Grimes 	unsigned optlen;
1282df8bae1dSRodney W. Grimes 
1283df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
12841e78ac21SJeffrey Hsu 	if (optlen + ip->ip_len > IP_MAXPACKET) {
1285cb7641e8SMaxim Konovalov 		*phlen = 0;
1286df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1287cb7641e8SMaxim Konovalov 	}
1288df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1289df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1290df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1291a163d034SWarner Losh 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
12920b17fba7SAndre Oppermann 		if (n == NULL) {
1293cb7641e8SMaxim Konovalov 			*phlen = 0;
1294df8bae1dSRodney W. Grimes 			return (m);
1295cb7641e8SMaxim Konovalov 		}
12965db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
12974ed84624SRobert Watson #ifdef MAC
12984ed84624SRobert Watson 		mac_create_mbuf_from_mbuf(m, n);
12994ed84624SRobert Watson #endif
1300df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1301df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1302df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1303df8bae1dSRodney W. Grimes 		n->m_next = m;
1304df8bae1dSRodney W. Grimes 		m = n;
1305df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1306df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1307212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1308df8bae1dSRodney W. Grimes 	} else {
1309df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1310df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1311df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1312212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1313df8bae1dSRodney W. Grimes 	}
1314df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
13150453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1316df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
131753be11f6SPoul-Henning Kamp 	ip->ip_v = IPVERSION;
131853be11f6SPoul-Henning Kamp 	ip->ip_hl = *phlen >> 2;
1319df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1320df8bae1dSRodney W. Grimes 	return (m);
1321df8bae1dSRodney W. Grimes }
1322df8bae1dSRodney W. Grimes 
1323df8bae1dSRodney W. Grimes /*
1324df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1325df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1326df8bae1dSRodney W. Grimes  */
1327beec8214SDarren Reed int
1328b375c9ecSLuigi Rizzo ip_optcopy(ip, jp)
1329b375c9ecSLuigi Rizzo 	struct ip *ip, *jp;
1330df8bae1dSRodney W. Grimes {
1331b375c9ecSLuigi Rizzo 	register u_char *cp, *dp;
1332df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1333df8bae1dSRodney W. Grimes 
1334df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1335df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
133653be11f6SPoul-Henning Kamp 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1337df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1338df8bae1dSRodney W. Grimes 		opt = cp[0];
1339df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1340df8bae1dSRodney W. Grimes 			break;
1341df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1342df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1343df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1344df8bae1dSRodney W. Grimes 			optlen = 1;
1345df8bae1dSRodney W. Grimes 			continue;
1346686cdd19SJun-ichiro itojun Hagino 		}
1347db40007dSAndrew R. Reiter 
1348db40007dSAndrew R. Reiter 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1349db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1350df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1351db40007dSAndrew R. Reiter 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1352db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1353db40007dSAndrew R. Reiter 
1354df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1355df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1356df8bae1dSRodney W. Grimes 			optlen = cnt;
1357df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
13580453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1359df8bae1dSRodney W. Grimes 			dp += optlen;
1360df8bae1dSRodney W. Grimes 		}
1361df8bae1dSRodney W. Grimes 	}
1362df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1363df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1364df8bae1dSRodney W. Grimes 	return (optlen);
1365df8bae1dSRodney W. Grimes }
1366df8bae1dSRodney W. Grimes 
1367df8bae1dSRodney W. Grimes /*
1368df8bae1dSRodney W. Grimes  * IP socket option processing.
1369df8bae1dSRodney W. Grimes  */
1370df8bae1dSRodney W. Grimes int
1371b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt)
1372b375c9ecSLuigi Rizzo 	struct socket *so;
1373b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1374df8bae1dSRodney W. Grimes {
1375cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1376cfe8b629SGarrett Wollman 	int	error, optval;
1377df8bae1dSRodney W. Grimes 
1378cfe8b629SGarrett Wollman 	error = optval = 0;
1379cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1380cfe8b629SGarrett Wollman 		return (EINVAL);
1381cfe8b629SGarrett Wollman 	}
1382df8bae1dSRodney W. Grimes 
1383cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1384cfe8b629SGarrett Wollman 	case SOPT_SET:
1385cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1386df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1387df8bae1dSRodney W. Grimes #ifdef notyet
1388df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1389df8bae1dSRodney W. Grimes #endif
1390cfe8b629SGarrett Wollman 		{
1391cfe8b629SGarrett Wollman 			struct mbuf *m;
1392cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1393cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1394cfe8b629SGarrett Wollman 				break;
1395cfe8b629SGarrett Wollman 			}
1396a163d034SWarner Losh 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
13970b17fba7SAndre Oppermann 			if (m == NULL) {
1398cfe8b629SGarrett Wollman 				error = ENOBUFS;
1399cfe8b629SGarrett Wollman 				break;
1400cfe8b629SGarrett Wollman 			}
1401cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1402cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1403cfe8b629SGarrett Wollman 					    m->m_len);
1404cfe8b629SGarrett Wollman 
1405cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1406cfe8b629SGarrett Wollman 					   m));
1407cfe8b629SGarrett Wollman 		}
1408df8bae1dSRodney W. Grimes 
1409df8bae1dSRodney W. Grimes 		case IP_TOS:
1410df8bae1dSRodney W. Grimes 		case IP_TTL:
1411df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1412df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1413df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
14144957466bSMatthew N. Dodd 		case IP_RECVTTL:
141582c23ebaSBill Fenner 		case IP_RECVIF:
14166a800098SYoshinobu Inoue 		case IP_FAITH:
14178afa2304SBruce M Simpson 		case IP_ONESBCAST:
1418cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1419cfe8b629SGarrett Wollman 					    sizeof optval);
1420cfe8b629SGarrett Wollman 			if (error)
1421cfe8b629SGarrett Wollman 				break;
1422df8bae1dSRodney W. Grimes 
1423cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1424df8bae1dSRodney W. Grimes 			case IP_TOS:
1425ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1426df8bae1dSRodney W. Grimes 				break;
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes 			case IP_TTL:
1429ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1430df8bae1dSRodney W. Grimes 				break;
1431a138d217SRobert Watson #define	OPTSET(bit) do {						\
1432a138d217SRobert Watson 	INP_LOCK(inp);							\
1433df8bae1dSRodney W. Grimes 	if (optval)							\
1434df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1435df8bae1dSRodney W. Grimes 	else								\
1436a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
1437a138d217SRobert Watson 	INP_UNLOCK(inp);						\
1438a138d217SRobert Watson } while (0)
1439df8bae1dSRodney W. Grimes 
1440df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1441df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1442df8bae1dSRodney W. Grimes 				break;
1443df8bae1dSRodney W. Grimes 
1444df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1445df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1446df8bae1dSRodney W. Grimes 				break;
1447df8bae1dSRodney W. Grimes 
1448df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1449df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1450df8bae1dSRodney W. Grimes 				break;
145182c23ebaSBill Fenner 
14524957466bSMatthew N. Dodd 			case IP_RECVTTL:
14534957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
14544957466bSMatthew N. Dodd 				break;
14554957466bSMatthew N. Dodd 
145682c23ebaSBill Fenner 			case IP_RECVIF:
145782c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
145882c23ebaSBill Fenner 				break;
14596a800098SYoshinobu Inoue 
14606a800098SYoshinobu Inoue 			case IP_FAITH:
14616a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
14626a800098SYoshinobu Inoue 				break;
14638afa2304SBruce M Simpson 
14648afa2304SBruce M Simpson 			case IP_ONESBCAST:
14658afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
14668afa2304SBruce M Simpson 				break;
1467df8bae1dSRodney W. Grimes 			}
1468df8bae1dSRodney W. Grimes 			break;
1469df8bae1dSRodney W. Grimes #undef OPTSET
1470df8bae1dSRodney W. Grimes 
1471df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1472f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1473df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1474df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1475df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1476df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1477cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1478df8bae1dSRodney W. Grimes 			break;
1479df8bae1dSRodney W. Grimes 
148033b3ac06SPeter Wemm 		case IP_PORTRANGE:
1481cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1482cfe8b629SGarrett Wollman 					    sizeof optval);
1483cfe8b629SGarrett Wollman 			if (error)
1484cfe8b629SGarrett Wollman 				break;
148533b3ac06SPeter Wemm 
1486a138d217SRobert Watson 			INP_LOCK(inp);
148733b3ac06SPeter Wemm 			switch (optval) {
148833b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
148933b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
149033b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
149133b3ac06SPeter Wemm 				break;
149233b3ac06SPeter Wemm 
149333b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
149433b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
149533b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
149633b3ac06SPeter Wemm 				break;
149733b3ac06SPeter Wemm 
149833b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
149933b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
150033b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
150133b3ac06SPeter Wemm 				break;
150233b3ac06SPeter Wemm 
150333b3ac06SPeter Wemm 			default:
150433b3ac06SPeter Wemm 				error = EINVAL;
150533b3ac06SPeter Wemm 				break;
150633b3ac06SPeter Wemm 			}
1507a138d217SRobert Watson 			INP_UNLOCK(inp);
1508ce8c72b1SPeter Wemm 			break;
150933b3ac06SPeter Wemm 
1510b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
15116a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
15126a800098SYoshinobu Inoue 		{
15136a800098SYoshinobu Inoue 			caddr_t req;
1514686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
15156a800098SYoshinobu Inoue 			int priv;
15166a800098SYoshinobu Inoue 			struct mbuf *m;
15176a800098SYoshinobu Inoue 			int optname;
15186a800098SYoshinobu Inoue 
15196a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
15206a800098SYoshinobu Inoue 				break;
15216a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
15226a800098SYoshinobu Inoue 				break;
1523b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
152444731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
15256a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1526686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
15276a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1528686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
15296a800098SYoshinobu Inoue 			m_freem(m);
15306a800098SYoshinobu Inoue 			break;
15316a800098SYoshinobu Inoue 		}
15326a800098SYoshinobu Inoue #endif /*IPSEC*/
15336a800098SYoshinobu Inoue 
1534df8bae1dSRodney W. Grimes 		default:
1535df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1536df8bae1dSRodney W. Grimes 			break;
1537df8bae1dSRodney W. Grimes 		}
1538df8bae1dSRodney W. Grimes 		break;
1539df8bae1dSRodney W. Grimes 
1540cfe8b629SGarrett Wollman 	case SOPT_GET:
1541cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1542df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1543df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1544cfe8b629SGarrett Wollman 			if (inp->inp_options)
1545cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1546cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1547cfe8b629SGarrett Wollman 							  char *),
1548cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1549cfe8b629SGarrett Wollman 			else
1550cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1551df8bae1dSRodney W. Grimes 			break;
1552df8bae1dSRodney W. Grimes 
1553df8bae1dSRodney W. Grimes 		case IP_TOS:
1554df8bae1dSRodney W. Grimes 		case IP_TTL:
1555df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1556df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1557df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
15584957466bSMatthew N. Dodd 		case IP_RECVTTL:
155982c23ebaSBill Fenner 		case IP_RECVIF:
1560cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
15616a800098SYoshinobu Inoue 		case IP_FAITH:
15628afa2304SBruce M Simpson 		case IP_ONESBCAST:
1563cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1564df8bae1dSRodney W. Grimes 
1565df8bae1dSRodney W. Grimes 			case IP_TOS:
1566ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1567df8bae1dSRodney W. Grimes 				break;
1568df8bae1dSRodney W. Grimes 
1569df8bae1dSRodney W. Grimes 			case IP_TTL:
1570ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1571df8bae1dSRodney W. Grimes 				break;
1572df8bae1dSRodney W. Grimes 
1573df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1574df8bae1dSRodney W. Grimes 
1575df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1576df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1577df8bae1dSRodney W. Grimes 				break;
1578df8bae1dSRodney W. Grimes 
1579df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1580df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1581df8bae1dSRodney W. Grimes 				break;
1582df8bae1dSRodney W. Grimes 
1583df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1584df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1585df8bae1dSRodney W. Grimes 				break;
158682c23ebaSBill Fenner 
15874957466bSMatthew N. Dodd 			case IP_RECVTTL:
15884957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
15894957466bSMatthew N. Dodd 				break;
15904957466bSMatthew N. Dodd 
159182c23ebaSBill Fenner 			case IP_RECVIF:
159282c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
159382c23ebaSBill Fenner 				break;
1594cfe8b629SGarrett Wollman 
1595cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1596cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1597cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1598cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1599cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1600cfe8b629SGarrett Wollman 				else
1601cfe8b629SGarrett Wollman 					optval = 0;
1602cfe8b629SGarrett Wollman 				break;
16036a800098SYoshinobu Inoue 
16046a800098SYoshinobu Inoue 			case IP_FAITH:
16056a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
16066a800098SYoshinobu Inoue 				break;
16078afa2304SBruce M Simpson 
16088afa2304SBruce M Simpson 			case IP_ONESBCAST:
16098afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
16108afa2304SBruce M Simpson 				break;
1611df8bae1dSRodney W. Grimes 			}
1612cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1613df8bae1dSRodney W. Grimes 			break;
1614df8bae1dSRodney W. Grimes 
1615df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1616f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1617df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1618df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1619df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1620df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1621cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
162233b3ac06SPeter Wemm 			break;
162333b3ac06SPeter Wemm 
1624b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
16256a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
16266a800098SYoshinobu Inoue 		{
1627f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
16286a800098SYoshinobu Inoue 			caddr_t req = NULL;
1629686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
16306a800098SYoshinobu Inoue 
1631686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
16326a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1633686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1634686cdd19SJun-ichiro itojun Hagino 			}
1635686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
16366a800098SYoshinobu Inoue 			if (error == 0)
16376a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1638f63e7634SYoshinobu Inoue 			if (error == 0)
16396a800098SYoshinobu Inoue 				m_freem(m);
16406a800098SYoshinobu Inoue 			break;
16416a800098SYoshinobu Inoue 		}
16426a800098SYoshinobu Inoue #endif /*IPSEC*/
16436a800098SYoshinobu Inoue 
1644df8bae1dSRodney W. Grimes 		default:
1645df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1646df8bae1dSRodney W. Grimes 			break;
1647df8bae1dSRodney W. Grimes 		}
1648df8bae1dSRodney W. Grimes 		break;
1649df8bae1dSRodney W. Grimes 	}
1650df8bae1dSRodney W. Grimes 	return (error);
1651df8bae1dSRodney W. Grimes }
1652df8bae1dSRodney W. Grimes 
1653df8bae1dSRodney W. Grimes /*
1654df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1655df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1656df8bae1dSRodney W. Grimes  * with destination address if source routed.
1657df8bae1dSRodney W. Grimes  */
16580312fbe9SPoul-Henning Kamp static int
1659b375c9ecSLuigi Rizzo ip_pcbopts(optname, pcbopt, m)
1660b375c9ecSLuigi Rizzo 	int optname;
1661b375c9ecSLuigi Rizzo 	struct mbuf **pcbopt;
1662b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1663df8bae1dSRodney W. Grimes {
1664b375c9ecSLuigi Rizzo 	register int cnt, optlen;
1665b375c9ecSLuigi Rizzo 	register u_char *cp;
1666df8bae1dSRodney W. Grimes 	u_char opt;
1667df8bae1dSRodney W. Grimes 
1668df8bae1dSRodney W. Grimes 	/* turn off any old options */
1669df8bae1dSRodney W. Grimes 	if (*pcbopt)
1670df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1671df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1672df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1673df8bae1dSRodney W. Grimes 		/*
1674df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1675df8bae1dSRodney W. Grimes 		 */
1676df8bae1dSRodney W. Grimes 		if (m)
1677df8bae1dSRodney W. Grimes 			(void)m_free(m);
1678df8bae1dSRodney W. Grimes 		return (0);
1679df8bae1dSRodney W. Grimes 	}
1680df8bae1dSRodney W. Grimes 
16810c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1682df8bae1dSRodney W. Grimes 		goto bad;
1683df8bae1dSRodney W. Grimes 	/*
1684df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1685df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1686df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1687df8bae1dSRodney W. Grimes 	 */
1688df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1689df8bae1dSRodney W. Grimes 		goto bad;
1690df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1691df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1692df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1693212059bdSDag-Erling Smørgrav 	bcopy(mtod(m, void *), cp, (unsigned)cnt);
1694212059bdSDag-Erling Smørgrav 	bzero(mtod(m, void *), sizeof(struct in_addr));
1695df8bae1dSRodney W. Grimes 
1696df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1697df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1698df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1699df8bae1dSRodney W. Grimes 			break;
1700df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1701df8bae1dSRodney W. Grimes 			optlen = 1;
1702df8bae1dSRodney W. Grimes 		else {
1703707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1704707d00a3SJonathan Lemon 				goto bad;
1705df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1706707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1707df8bae1dSRodney W. Grimes 				goto bad;
1708df8bae1dSRodney W. Grimes 		}
1709df8bae1dSRodney W. Grimes 		switch (opt) {
1710df8bae1dSRodney W. Grimes 
1711df8bae1dSRodney W. Grimes 		default:
1712df8bae1dSRodney W. Grimes 			break;
1713df8bae1dSRodney W. Grimes 
1714df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1715df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1716df8bae1dSRodney W. Grimes 			/*
1717df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1718df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1719df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1720df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1721df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1722df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1723df8bae1dSRodney W. Grimes 			 */
1724df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1725df8bae1dSRodney W. Grimes 				goto bad;
1726df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1727df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1728df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1729df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1730df8bae1dSRodney W. Grimes 			/*
1731df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1732df8bae1dSRodney W. Grimes 			 */
1733df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1734df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1735df8bae1dSRodney W. Grimes 			/*
1736df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1737df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1738df8bae1dSRodney W. Grimes 			 */
1739212059bdSDag-Erling Smørgrav 			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1740212059bdSDag-Erling Smørgrav 			    &cp[IPOPT_OFFSET+1],
1741a49b2137SMaxim Konovalov 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
1742df8bae1dSRodney W. Grimes 			break;
1743df8bae1dSRodney W. Grimes 		}
1744df8bae1dSRodney W. Grimes 	}
1745df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1746df8bae1dSRodney W. Grimes 		goto bad;
1747df8bae1dSRodney W. Grimes 	*pcbopt = m;
1748df8bae1dSRodney W. Grimes 	return (0);
1749df8bae1dSRodney W. Grimes 
1750df8bae1dSRodney W. Grimes bad:
1751df8bae1dSRodney W. Grimes 	(void)m_free(m);
1752df8bae1dSRodney W. Grimes 	return (EINVAL);
1753df8bae1dSRodney W. Grimes }
1754df8bae1dSRodney W. Grimes 
1755df8bae1dSRodney W. Grimes /*
1756cfe8b629SGarrett Wollman  * XXX
1757cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1758cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1759cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1760cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1761cfe8b629SGarrett Wollman  */
176233841545SHajimu UMEMOTO 
176333841545SHajimu UMEMOTO /*
176433841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
176533841545SHajimu UMEMOTO  */
176633841545SHajimu UMEMOTO static struct ifnet *
1767b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp)
1768b375c9ecSLuigi Rizzo 	struct in_addr *a;
1769b375c9ecSLuigi Rizzo 	int *ifindexp;
177033841545SHajimu UMEMOTO {
177133841545SHajimu UMEMOTO 	int ifindex;
177233841545SHajimu UMEMOTO 	struct ifnet *ifp;
177333841545SHajimu UMEMOTO 
177433841545SHajimu UMEMOTO 	if (ifindexp)
177533841545SHajimu UMEMOTO 		*ifindexp = 0;
177633841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
177733841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
177833841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
177933841545SHajimu UMEMOTO 			return NULL;
1780f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
178133841545SHajimu UMEMOTO 		if (ifindexp)
178233841545SHajimu UMEMOTO 			*ifindexp = ifindex;
178333841545SHajimu UMEMOTO 	} else {
178433841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
178533841545SHajimu UMEMOTO 	}
178633841545SHajimu UMEMOTO 	return ifp;
178733841545SHajimu UMEMOTO }
178833841545SHajimu UMEMOTO 
1789cfe8b629SGarrett Wollman /*
1790df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1791df8bae1dSRodney W. Grimes  */
17920312fbe9SPoul-Henning Kamp static int
1793b375c9ecSLuigi Rizzo ip_setmoptions(sopt, imop)
1794b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1795b375c9ecSLuigi Rizzo 	struct ip_moptions **imop;
1796df8bae1dSRodney W. Grimes {
1797cfe8b629SGarrett Wollman 	int error = 0;
1798cfe8b629SGarrett Wollman 	int i;
1799df8bae1dSRodney W. Grimes 	struct in_addr addr;
1800cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1801cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1802cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1803df8bae1dSRodney W. Grimes 	struct route ro;
1804cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
180533841545SHajimu UMEMOTO 	int ifindex;
18069b626c29SGarrett Wollman 	int s;
1807df8bae1dSRodney W. Grimes 
1808df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1809df8bae1dSRodney W. Grimes 		/*
1810df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1811df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1812df8bae1dSRodney W. Grimes 		 */
1813df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1814a163d034SWarner Losh 		    M_WAITOK);
1815df8bae1dSRodney W. Grimes 
1816df8bae1dSRodney W. Grimes 		if (imo == NULL)
1817df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1818df8bae1dSRodney W. Grimes 		*imop = imo;
1819df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
182033841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
18211c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1822df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1823df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1824df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1825df8bae1dSRodney W. Grimes 	}
1826df8bae1dSRodney W. Grimes 
1827cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1828f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1829f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1830cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
18315e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
18325e9ae478SGarrett Wollman 			break;
18335e9ae478SGarrett Wollman 		}
1834cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1835cfe8b629SGarrett Wollman 		if (error)
1836f0068c4aSGarrett Wollman 			break;
18371c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1838f0068c4aSGarrett Wollman 			error = EINVAL;
1839f0068c4aSGarrett Wollman 			break;
1840f0068c4aSGarrett Wollman 		}
1841f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1842f0068c4aSGarrett Wollman 		break;
1843f0068c4aSGarrett Wollman 
1844df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1845df8bae1dSRodney W. Grimes 		/*
1846df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1847df8bae1dSRodney W. Grimes 		 */
1848cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1849cfe8b629SGarrett Wollman 		if (error)
1850df8bae1dSRodney W. Grimes 			break;
1851df8bae1dSRodney W. Grimes 		/*
1852df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1853df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1854df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1855df8bae1dSRodney W. Grimes 		 */
1856df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1857df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1858df8bae1dSRodney W. Grimes 			break;
1859df8bae1dSRodney W. Grimes 		}
1860df8bae1dSRodney W. Grimes 		/*
1861df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1862df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1863df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1864df8bae1dSRodney W. Grimes 		 */
186520e8807cSGarrett Wollman 		s = splimp();
186633841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1867df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1868fbc6ab00SBill Fenner 			splx(s);
1869df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1870df8bae1dSRodney W. Grimes 			break;
1871df8bae1dSRodney W. Grimes 		}
1872df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
187333841545SHajimu UMEMOTO 		if (ifindex)
187433841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
187533841545SHajimu UMEMOTO 		else
187633841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
18779b626c29SGarrett Wollman 		splx(s);
1878df8bae1dSRodney W. Grimes 		break;
1879df8bae1dSRodney W. Grimes 
1880df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1881df8bae1dSRodney W. Grimes 		/*
1882df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1883cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1884cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1885cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1886df8bae1dSRodney W. Grimes 		 */
1887cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1888cfe8b629SGarrett Wollman 			u_char ttl;
1889cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1890cfe8b629SGarrett Wollman 			if (error)
1891df8bae1dSRodney W. Grimes 				break;
1892cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1893cfe8b629SGarrett Wollman 		} else {
1894cfe8b629SGarrett Wollman 			u_int ttl;
1895cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1896cfe8b629SGarrett Wollman 					    sizeof ttl);
1897cfe8b629SGarrett Wollman 			if (error)
1898cfe8b629SGarrett Wollman 				break;
1899cfe8b629SGarrett Wollman 			if (ttl > 255)
1900cfe8b629SGarrett Wollman 				error = EINVAL;
1901cfe8b629SGarrett Wollman 			else
1902cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1903df8bae1dSRodney W. Grimes 		}
1904df8bae1dSRodney W. Grimes 		break;
1905df8bae1dSRodney W. Grimes 
1906df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1907df8bae1dSRodney W. Grimes 		/*
1908df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1909cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1910cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1911cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1912df8bae1dSRodney W. Grimes 		 */
1913cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1914cfe8b629SGarrett Wollman 			u_char loop;
1915cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1916cfe8b629SGarrett Wollman 			if (error)
1917df8bae1dSRodney W. Grimes 				break;
1918cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1919cfe8b629SGarrett Wollman 		} else {
1920cfe8b629SGarrett Wollman 			u_int loop;
1921cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1922cfe8b629SGarrett Wollman 					    sizeof loop);
1923cfe8b629SGarrett Wollman 			if (error)
1924cfe8b629SGarrett Wollman 				break;
1925cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1926df8bae1dSRodney W. Grimes 		}
1927df8bae1dSRodney W. Grimes 		break;
1928df8bae1dSRodney W. Grimes 
1929df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1930df8bae1dSRodney W. Grimes 		/*
1931df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1932df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1933df8bae1dSRodney W. Grimes 		 */
1934cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1935cfe8b629SGarrett Wollman 		if (error)
1936df8bae1dSRodney W. Grimes 			break;
1937cfe8b629SGarrett Wollman 
1938cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1939df8bae1dSRodney W. Grimes 			error = EINVAL;
1940df8bae1dSRodney W. Grimes 			break;
1941df8bae1dSRodney W. Grimes 		}
194220e8807cSGarrett Wollman 		s = splimp();
1943df8bae1dSRodney W. Grimes 		/*
1944df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1945df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1946df8bae1dSRodney W. Grimes 		 */
1947cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
19481c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1949df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1950df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1951df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1952cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
195397d8d152SAndre Oppermann 			rtalloc_ign(&ro, RTF_CLONING);
1954df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1955df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
19569b626c29SGarrett Wollman 				splx(s);
1957df8bae1dSRodney W. Grimes 				break;
1958df8bae1dSRodney W. Grimes 			}
1959df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1960d1dd20beSSam Leffler 			RTFREE(ro.ro_rt);
1961df8bae1dSRodney W. Grimes 		}
1962df8bae1dSRodney W. Grimes 		else {
196333841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1964df8bae1dSRodney W. Grimes 		}
19659b626c29SGarrett Wollman 
1966df8bae1dSRodney W. Grimes 		/*
1967df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1968df8bae1dSRodney W. Grimes 		 * supports multicast.
1969df8bae1dSRodney W. Grimes 		 */
1970df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1971df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
19729b626c29SGarrett Wollman 			splx(s);
1973df8bae1dSRodney W. Grimes 			break;
1974df8bae1dSRodney W. Grimes 		}
1975df8bae1dSRodney W. Grimes 		/*
1976df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1977df8bae1dSRodney W. Grimes 		 * membership slots are full.
1978df8bae1dSRodney W. Grimes 		 */
1979df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1980df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1981df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1982cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1983df8bae1dSRodney W. Grimes 				break;
1984df8bae1dSRodney W. Grimes 		}
1985df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1986df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
19879b626c29SGarrett Wollman 			splx(s);
1988df8bae1dSRodney W. Grimes 			break;
1989df8bae1dSRodney W. Grimes 		}
1990df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1991df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
19929b626c29SGarrett Wollman 			splx(s);
1993df8bae1dSRodney W. Grimes 			break;
1994df8bae1dSRodney W. Grimes 		}
1995df8bae1dSRodney W. Grimes 		/*
1996df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1997df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1998df8bae1dSRodney W. Grimes 		 */
1999df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
2000cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
2001df8bae1dSRodney W. Grimes 			error = ENOBUFS;
20029b626c29SGarrett Wollman 			splx(s);
2003df8bae1dSRodney W. Grimes 			break;
2004df8bae1dSRodney W. Grimes 		}
2005df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
20069b626c29SGarrett Wollman 		splx(s);
2007df8bae1dSRodney W. Grimes 		break;
2008df8bae1dSRodney W. Grimes 
2009df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
2010df8bae1dSRodney W. Grimes 		/*
2011df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
2012df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
2013df8bae1dSRodney W. Grimes 		 */
2014cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
2015cfe8b629SGarrett Wollman 		if (error)
2016df8bae1dSRodney W. Grimes 			break;
2017cfe8b629SGarrett Wollman 
2018cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
2019df8bae1dSRodney W. Grimes 			error = EINVAL;
2020df8bae1dSRodney W. Grimes 			break;
2021df8bae1dSRodney W. Grimes 		}
20229b626c29SGarrett Wollman 
202320e8807cSGarrett Wollman 		s = splimp();
2024df8bae1dSRodney W. Grimes 		/*
2025df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
2026df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
2027df8bae1dSRodney W. Grimes 		 */
2028cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
2029df8bae1dSRodney W. Grimes 			ifp = NULL;
2030df8bae1dSRodney W. Grimes 		else {
203133841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
2032df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
2033df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
20349b626c29SGarrett Wollman 				splx(s);
2035df8bae1dSRodney W. Grimes 				break;
2036df8bae1dSRodney W. Grimes 			}
2037df8bae1dSRodney W. Grimes 		}
2038df8bae1dSRodney W. Grimes 		/*
2039df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
2040df8bae1dSRodney W. Grimes 		 */
2041df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
2042df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
2043df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
2044df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
2045cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
2046df8bae1dSRodney W. Grimes 				break;
2047df8bae1dSRodney W. Grimes 		}
2048df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
2049df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
20509b626c29SGarrett Wollman 			splx(s);
2051df8bae1dSRodney W. Grimes 			break;
2052df8bae1dSRodney W. Grimes 		}
2053df8bae1dSRodney W. Grimes 		/*
2054df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
2055df8bae1dSRodney W. Grimes 		 * membership points.
2056df8bae1dSRodney W. Grimes 		 */
2057df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
2058df8bae1dSRodney W. Grimes 		/*
2059df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
2060df8bae1dSRodney W. Grimes 		 */
2061df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
2062df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
2063df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
20649b626c29SGarrett Wollman 		splx(s);
2065df8bae1dSRodney W. Grimes 		break;
2066df8bae1dSRodney W. Grimes 
2067df8bae1dSRodney W. Grimes 	default:
2068df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
2069df8bae1dSRodney W. Grimes 		break;
2070df8bae1dSRodney W. Grimes 	}
2071df8bae1dSRodney W. Grimes 
2072df8bae1dSRodney W. Grimes 	/*
2073df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
2074df8bae1dSRodney W. Grimes 	 */
2075df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
20761c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
2077df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2078df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2079df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
2080df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
2081df8bae1dSRodney W. Grimes 		*imop = NULL;
2082df8bae1dSRodney W. Grimes 	}
2083df8bae1dSRodney W. Grimes 
2084df8bae1dSRodney W. Grimes 	return (error);
2085df8bae1dSRodney W. Grimes }
2086df8bae1dSRodney W. Grimes 
2087df8bae1dSRodney W. Grimes /*
2088df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
2089df8bae1dSRodney W. Grimes  */
20900312fbe9SPoul-Henning Kamp static int
2091b375c9ecSLuigi Rizzo ip_getmoptions(sopt, imo)
2092b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
2093b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
2094df8bae1dSRodney W. Grimes {
2095cfe8b629SGarrett Wollman 	struct in_addr addr;
2096df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
2097cfe8b629SGarrett Wollman 	int error, optval;
2098cfe8b629SGarrett Wollman 	u_char coptval;
2099df8bae1dSRodney W. Grimes 
2100cfe8b629SGarrett Wollman 	error = 0;
2101cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
2102f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
2103f0068c4aSGarrett Wollman 		if (imo != NULL)
2104cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
2105f0068c4aSGarrett Wollman 		else
2106cfe8b629SGarrett Wollman 			optval = -1;
2107cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
2108cfe8b629SGarrett Wollman 		break;
2109f0068c4aSGarrett Wollman 
2110df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
2111df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
2112cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
211333841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
211433841545SHajimu UMEMOTO 			/* return the value user has set */
211533841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
211633841545SHajimu UMEMOTO 		} else {
2117df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
2118cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
2119df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
2120df8bae1dSRodney W. Grimes 		}
2121cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
2122cfe8b629SGarrett Wollman 		break;
2123df8bae1dSRodney W. Grimes 
2124df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
2125cfe8b629SGarrett Wollman 		if (imo == 0)
2126cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
2127cfe8b629SGarrett Wollman 		else
2128cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
2129cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
2130cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
2131cfe8b629SGarrett Wollman 		else
2132cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
2133cfe8b629SGarrett Wollman 		break;
2134df8bae1dSRodney W. Grimes 
2135df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
2136cfe8b629SGarrett Wollman 		if (imo == 0)
2137cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
2138cfe8b629SGarrett Wollman 		else
2139cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
2140cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
2141cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
2142cfe8b629SGarrett Wollman 		else
2143cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
2144cfe8b629SGarrett Wollman 		break;
2145df8bae1dSRodney W. Grimes 
2146df8bae1dSRodney W. Grimes 	default:
2147cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
2148cfe8b629SGarrett Wollman 		break;
2149df8bae1dSRodney W. Grimes 	}
2150cfe8b629SGarrett Wollman 	return (error);
2151df8bae1dSRodney W. Grimes }
2152df8bae1dSRodney W. Grimes 
2153df8bae1dSRodney W. Grimes /*
2154df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
2155df8bae1dSRodney W. Grimes  */
2156df8bae1dSRodney W. Grimes void
2157b375c9ecSLuigi Rizzo ip_freemoptions(imo)
2158b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
2159df8bae1dSRodney W. Grimes {
2160b375c9ecSLuigi Rizzo 	register int i;
2161df8bae1dSRodney W. Grimes 
2162df8bae1dSRodney W. Grimes 	if (imo != NULL) {
2163df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
2164df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
2165df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
2166df8bae1dSRodney W. Grimes 	}
2167df8bae1dSRodney W. Grimes }
2168df8bae1dSRodney W. Grimes 
2169df8bae1dSRodney W. Grimes /*
2170df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
2171df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
2172df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
2173f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
2174f5fea3ddSPaul Traina  * replicating that code here.
2175df8bae1dSRodney W. Grimes  */
2176df8bae1dSRodney W. Grimes static void
2177b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen)
2178b375c9ecSLuigi Rizzo 	struct ifnet *ifp;
2179b375c9ecSLuigi Rizzo 	register struct mbuf *m;
2180b375c9ecSLuigi Rizzo 	register struct sockaddr_in *dst;
2181b375c9ecSLuigi Rizzo 	int hlen;
2182df8bae1dSRodney W. Grimes {
2183b375c9ecSLuigi Rizzo 	register struct ip *ip;
2184df8bae1dSRodney W. Grimes 	struct mbuf *copym;
2185df8bae1dSRodney W. Grimes 
2186b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
218786b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
218886b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
2189df8bae1dSRodney W. Grimes 	if (copym != NULL) {
2190390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
2191390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2192390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
2193390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2194390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
2195390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2196390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
2197390cdc6aSRuslan Ermilov 		}
2198df8bae1dSRodney W. Grimes 		/*
2199df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
2200df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
2201df8bae1dSRodney W. Grimes 		 */
2202df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
2203fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
2204fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
2205df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
220686b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
22079c9137eaSGarrett Wollman 		/*
22089c9137eaSGarrett Wollman 		 * NB:
2209e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
2210e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
2211e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
2212e1596dffSBill Fenner 		 * particular, everything which modifies the packet
2213e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
2214e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
2215e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
2216ed7509acSJulian Elischer 		 * of safety by using if_simloop().
22179c9137eaSGarrett Wollman 		 */
2218201c2527SJulian Elischer #if 1 /* XXX */
2219201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
22202b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
2221201c2527SJulian Elischer 						dst->sin_family);
2222201c2527SJulian Elischer 			dst->sin_family = AF_INET;
2223201c2527SJulian Elischer 		}
2224201c2527SJulian Elischer #endif
2225201c2527SJulian Elischer 
22269c9137eaSGarrett Wollman #ifdef notdef
2227e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
2228ed7509acSJulian Elischer 		ip_input(copym);
22299c9137eaSGarrett Wollman #else
223006a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
22319c9137eaSGarrett Wollman #endif
2232df8bae1dSRodney W. Grimes 	}
2233df8bae1dSRodney W. Grimes }
2234