xref: /freebsd/sys/netinet/ip_output.c (revision a138d21769de1498435a908513175219a96d024f)
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 	}
243df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
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) {
254df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
255df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
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 		 */
278df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
279e6e51f05SLuigi Rizzo 			rtalloc_ign(ro, 0);
280df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
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 
784ea53ecd9SJonathan Lemon 			/*
785ea53ecd9SJonathan Lemon 			 * XXX
786ea53ecd9SJonathan Lemon 			 * delayed checksums are not currently compatible
787ea53ecd9SJonathan Lemon 			 * with divert sockets.
788ea53ecd9SJonathan Lemon 			 */
789ea53ecd9SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
790ea53ecd9SJonathan Lemon 				in_delayed_cksum(m);
791ea53ecd9SJonathan Lemon 				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
792ea53ecd9SJonathan Lemon 			}
793ea53ecd9SJonathan Lemon 
7948948e4baSArchie Cobbs 			/* Restore packet header fields to original values */
795fd8e4ebcSMike Barcroft 			ip->ip_len = htons(ip->ip_len);
796fd8e4ebcSMike Barcroft 			ip->ip_off = htons(ip->ip_off);
7978948e4baSArchie Cobbs 
7988948e4baSArchie Cobbs 			/* Deliver packet to divert input routine */
799ac9d7e26SMax Laier 			divert_packet(m, 0);
8008948e4baSArchie Cobbs 
8018948e4baSArchie Cobbs 			/* If 'tee', continue with original packet */
8028948e4baSArchie Cobbs 			if (clone != NULL) {
8038948e4baSArchie Cobbs 				m = clone;
8048948e4baSArchie Cobbs 				ip = mtod(m, struct ip *);
8058948e4baSArchie Cobbs 				goto pass;
8068948e4baSArchie Cobbs 			}
807b715f178SLuigi Rizzo 			goto done;
808b715f178SLuigi Rizzo 		}
809b715f178SLuigi Rizzo #endif
810b715f178SLuigi Rizzo 
8112b25acc1SLuigi Rizzo 		/* IPFIREWALL_FORWARD */
8122b25acc1SLuigi Rizzo 		/*
8132b25acc1SLuigi Rizzo 		 * Check dst to make sure it is directly reachable on the
814f9e354dfSJulian Elischer 		 * interface we previously thought it was.
815f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
816f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
817f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
818f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
819f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
820f9e354dfSJulian Elischer 		 * And I'm babbling.
821f9e354dfSJulian Elischer 		 */
8222b25acc1SLuigi Rizzo 		if (off == 0 && old != dst) { /* FORWARD, dst has changed */
8232b25acc1SLuigi Rizzo #if 0
8242b25acc1SLuigi Rizzo 			/*
8252b25acc1SLuigi Rizzo 			 * XXX To improve readability, this block should be
8262b25acc1SLuigi Rizzo 			 * changed into a function call as below:
8272b25acc1SLuigi Rizzo 			 */
8282b25acc1SLuigi Rizzo 			error = ip_ipforward(&m, &dst, &ifp);
8292b25acc1SLuigi Rizzo 			if (error)
8302b25acc1SLuigi Rizzo 				goto bad;
8312b25acc1SLuigi Rizzo 			if (m == NULL) /* ip_input consumed the mbuf */
8322b25acc1SLuigi Rizzo 				goto done;
8332b25acc1SLuigi Rizzo #else
834f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
835f9e354dfSJulian Elischer 
8362b25acc1SLuigi Rizzo 			/*
8372b25acc1SLuigi Rizzo 			 * XXX sro_fwd below is static, and a pointer
8382b25acc1SLuigi Rizzo 			 * to it gets passed to routines downstream.
8392b25acc1SLuigi Rizzo 			 * This could have surprisingly bad results in
8402b25acc1SLuigi Rizzo 			 * practice, because its content is overwritten
8412b25acc1SLuigi Rizzo 			 * by subsequent packets.
8422b25acc1SLuigi Rizzo 			 */
843f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
8442b25acc1SLuigi Rizzo 			static struct route sro_fwd;
8452b25acc1SLuigi Rizzo 			struct route *ro_fwd = &sro_fwd;
8462b25acc1SLuigi Rizzo 
8472b25acc1SLuigi Rizzo #if 0
8482b25acc1SLuigi Rizzo 			print_ip("IPFIREWALL_FORWARD: New dst ip: ",
8492b25acc1SLuigi Rizzo 			    dst->sin_addr, "\n");
850f9e354dfSJulian Elischer #endif
8512b25acc1SLuigi Rizzo 
852f9e354dfSJulian Elischer 			/*
853f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
8542b25acc1SLuigi Rizzo 			 * to a local socket. If so, then we should somehow
855f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
856f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
857f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
858f9e354dfSJulian Elischer 			 * you want to forward until they are being output
859f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
860f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
861f9e354dfSJulian Elischer 			 * we would not be able to control what happens
862f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
8639d5abbddSJens Schweikhardt 			 * it is done through an ISR.
864f9e354dfSJulian Elischer 			 */
865ca925d9cSJonathan Lemon 			LIST_FOREACH(ia,
866ca925d9cSJonathan Lemon 			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
867f9e354dfSJulian Elischer 				/*
868f9e354dfSJulian Elischer 				 * If the addr to forward to is one
869f9e354dfSJulian Elischer 				 * of ours, we pretend to
870f9e354dfSJulian Elischer 				 * be the destination for this packet.
871f9e354dfSJulian Elischer 				 */
872f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
873f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
874f9e354dfSJulian Elischer 					break;
875f9e354dfSJulian Elischer 			}
8762b25acc1SLuigi Rizzo 			if (ia) {	/* tell ip_input "dont filter" */
8774672d819SMax Laier 				mtag = m_tag_get(
878ac9d7e26SMax Laier 				    PACKET_TAG_IPFORWARD,
879ac9d7e26SMax Laier 				    sizeof(struct sockaddr_in *), M_NOWAIT);
880ac9d7e26SMax Laier 				if (mtag == NULL) {
881ac9d7e26SMax Laier 					error = ENOBUFS;
882ac9d7e26SMax Laier 					goto bad;
883ac9d7e26SMax Laier 				}
884ac9d7e26SMax Laier 				*(struct sockaddr_in **)(mtag+1) =
885ac9d7e26SMax Laier 				    args.next_hop;
886ac9d7e26SMax Laier 				m_tag_prepend(m, mtag);
8872b25acc1SLuigi Rizzo 
888f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
889f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
89020c822f3SJonathan Lemon 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
89120c822f3SJonathan Lemon 					m->m_pkthdr.csum_flags |=
89220c822f3SJonathan Lemon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
893ac9d7e26SMax Laier 					m->m_pkthdr.csum_data = 0xffff;
89420c822f3SJonathan Lemon 				}
89520c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
89620c822f3SJonathan Lemon 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
897fd8e4ebcSMike Barcroft 				ip->ip_len = htons(ip->ip_len);
898fd8e4ebcSMike Barcroft 				ip->ip_off = htons(ip->ip_off);
899ac9d7e26SMax Laier 				ip_input(m);
900f9e354dfSJulian Elischer 				goto done;
901f9e354dfSJulian Elischer 			}
90297d8d152SAndre Oppermann 			/*
90397d8d152SAndre Oppermann 			 * Some of the logic for this was
904f9e354dfSJulian Elischer 			 * nicked from above.
905f9e354dfSJulian Elischer 			 */
906f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
907f9e354dfSJulian Elischer 
908f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
90997d8d152SAndre Oppermann 			rtalloc_ign(ro_fwd, RTF_CLONING);
910f9e354dfSJulian Elischer 
911f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt == 0) {
912f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
913f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
914f9e354dfSJulian Elischer 				goto bad;
915f9e354dfSJulian Elischer 			}
916f9e354dfSJulian Elischer 
917f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
918f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
91997d8d152SAndre Oppermann 			ro_fwd->ro_rt->rt_rmx.rmx_pksent++;
920f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
9212b25acc1SLuigi Rizzo 				dst = (struct sockaddr_in *)
9222b25acc1SLuigi Rizzo 					ro_fwd->ro_rt->rt_gateway;
923f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
924f9e354dfSJulian Elischer 				isbroadcast =
925f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
926f9e354dfSJulian Elischer 			else
927f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
9283f9e3122SYaroslav Tykhiy 			if (ro->ro_rt)
929f9e354dfSJulian Elischer 				RTFREE(ro->ro_rt);
930f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
931f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
932f9e354dfSJulian Elischer 
9332b25acc1SLuigi Rizzo #endif	/* ... block to be put into a function */
934f9e354dfSJulian Elischer 			/*
935f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
936f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
937f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
938f9e354dfSJulian Elischer 			 */
9392b25acc1SLuigi Rizzo 			if (src_was_INADDR_ANY)
940f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
941b715f178SLuigi Rizzo 			goto pass ;
942f9e354dfSJulian Elischer 		}
9432b25acc1SLuigi Rizzo 
944b715f178SLuigi Rizzo                 /*
945b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
946b715f178SLuigi Rizzo                  * we have to drop the pkt
947b715f178SLuigi Rizzo                  */
948b715f178SLuigi Rizzo 		m_freem(m);
949b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
950b715f178SLuigi Rizzo                 goto done;
95193e0e116SJulian Elischer 	}
952e7319babSPoul-Henning Kamp 
953b715f178SLuigi Rizzo pass:
95451c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
95551c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
95651c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
95751c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
95851c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
95951c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
96051c8ec4aSRuslan Ermilov 			goto bad;
96151c8ec4aSRuslan Ermilov 		}
96251c8ec4aSRuslan Ermilov 	}
96351c8ec4aSRuslan Ermilov 
964206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
965206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
966db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
967db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
968db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
969db4f9cc7SJonathan Lemon 	}
970206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
971db4f9cc7SJonathan Lemon 
972e7319babSPoul-Henning Kamp 	/*
973db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
974db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
975df8bae1dSRodney W. Grimes 	 */
9762683ceb6SAndre Oppermann 	if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
9772683ceb6SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0))) {
978fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
979fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
980df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
98153be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
982df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
9835da9f8faSJosef Karthauser 
9845da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
98538c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
9865da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
9875da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
9885da9f8faSJosef Karthauser 		}
9895da9f8faSJosef Karthauser 
99033841545SHajimu UMEMOTO #ifdef IPSEC
99133841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
99233841545SHajimu UMEMOTO 		ipsec_delaux(m);
99333841545SHajimu UMEMOTO #endif
99433841545SHajimu UMEMOTO 
99553dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
9963390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
9973390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
9989d9edc56SMike Silbersack #endif
999df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
1000df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
1001df8bae1dSRodney W. Grimes 		goto done;
1002df8bae1dSRodney W. Grimes 	}
10031e78ac21SJeffrey Hsu 
1004df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
1005df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
10063d1f141bSGarrett Wollman 		/*
10073d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
10083d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
10093d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
10103d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
10113d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
10123d1f141bSGarrett Wollman 		 */
10131e78ac21SJeffrey Hsu 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
10141e78ac21SJeffrey Hsu 		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
10153d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
10163d1f141bSGarrett Wollman 		}
1017df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
1018df8bae1dSRodney W. Grimes 		goto bad;
1019df8bae1dSRodney W. Grimes 	}
10201e78ac21SJeffrey Hsu 
10211e78ac21SJeffrey Hsu 	/*
10221e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
10231e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
10241e78ac21SJeffrey Hsu 	 */
10251e78ac21SJeffrey Hsu 	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
10261e78ac21SJeffrey Hsu 	if (error)
1027df8bae1dSRodney W. Grimes 		goto bad;
10281e78ac21SJeffrey Hsu 	for (; m; m = m0) {
1029df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
1030b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
103133841545SHajimu UMEMOTO #ifdef IPSEC
103233841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
103333841545SHajimu UMEMOTO 		ipsec_delaux(m);
103433841545SHajimu UMEMOTO #endif
103507203494SDaniel C. Sobral 		if (error == 0) {
1036fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
103707203494SDaniel C. Sobral 			if (ia != NULL) {
1038fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
1039fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
104007203494SDaniel C. Sobral 			}
1041fe937674SJosef Karthauser 
1042df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
1043df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
1044fe937674SJosef Karthauser 		} else
1045df8bae1dSRodney W. Grimes 			m_freem(m);
1046df8bae1dSRodney W. Grimes 	}
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes 	if (error == 0)
1049df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
10501e78ac21SJeffrey Hsu 
1051df8bae1dSRodney W. Grimes done:
10526a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
10536a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
10546a800098SYoshinobu Inoue 		ro->ro_rt = NULL;
10556a800098SYoshinobu Inoue 	}
1056ac9d7e26SMax Laier 	if (dummytag) {
1057ac9d7e26SMax Laier 		struct dn_pkt_tag *dt = (struct dn_pkt_tag *)(dummytag+1);
1058ac9d7e26SMax Laier 		if (dt->ro.ro_rt)
1059ac9d7e26SMax Laier 			RTFREE(dt->ro.ro_rt);
1060ac9d7e26SMax Laier 		m_tag_free(dummytag);
1061ac9d7e26SMax Laier 	}
106202c1c707SAndre Oppermann #ifdef IPSEC
10636a800098SYoshinobu Inoue 	if (sp != NULL) {
10646a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
10656a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
10666a800098SYoshinobu Inoue 		key_freesp(sp);
10676a800098SYoshinobu Inoue 	}
10681e78ac21SJeffrey Hsu #endif
1069b9234fafSSam Leffler #ifdef FAST_IPSEC
1070b9234fafSSam Leffler 	if (sp != NULL)
1071b9234fafSSam Leffler 		KEY_FREESP(&sp);
10721e78ac21SJeffrey Hsu #endif
1073df8bae1dSRodney W. Grimes 	return (error);
1074df8bae1dSRodney W. Grimes bad:
10753528d68fSBill Paul 	m_freem(m);
1076df8bae1dSRodney W. Grimes 	goto done;
1077df8bae1dSRodney W. Grimes }
1078df8bae1dSRodney W. Grimes 
10791e78ac21SJeffrey Hsu /*
10801e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
10811e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
10821e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
10831e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
10841e78ac21SJeffrey Hsu  *
10851e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
10861e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
10871e78ac21SJeffrey Hsu  */
10881e78ac21SJeffrey Hsu int
10891e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
10901e78ac21SJeffrey Hsu 	    u_long if_hwassist_flags, int sw_csum)
10911e78ac21SJeffrey Hsu {
10921e78ac21SJeffrey Hsu 	int error = 0;
10931e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
10941e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
10951e78ac21SJeffrey Hsu 	int off;
10961e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
10971e78ac21SJeffrey Hsu 	int firstlen;
10981e78ac21SJeffrey Hsu 	struct mbuf **mnext;
10991e78ac21SJeffrey Hsu 	int nfrags;
11001e78ac21SJeffrey Hsu 
11011e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
11021e78ac21SJeffrey Hsu 		ipstat.ips_cantfrag++;
11031e78ac21SJeffrey Hsu 		return EMSGSIZE;
11041e78ac21SJeffrey Hsu 	}
11051e78ac21SJeffrey Hsu 
11061e78ac21SJeffrey Hsu 	/*
11071e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
11081e78ac21SJeffrey Hsu 	 */
11091e78ac21SJeffrey Hsu 	if (len < 8)
11101e78ac21SJeffrey Hsu 		return EMSGSIZE;
11111e78ac21SJeffrey Hsu 
11121e78ac21SJeffrey Hsu 	/*
11131e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
11141e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
11151e78ac21SJeffrey Hsu 	 */
11161e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
11171e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
11181e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
11191e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
11201e78ac21SJeffrey Hsu 	}
11211e78ac21SJeffrey Hsu 
11221e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
11231e78ac21SJeffrey Hsu 		/*
11241e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
11251e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
11261e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
11271e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
11281e78ac21SJeffrey Hsu 		 *
11291e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
11301e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
11311e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
11321e78ac21SJeffrey Hsu 		 */
11331e78ac21SJeffrey Hsu 		int newlen;
11341e78ac21SJeffrey Hsu 		struct mbuf *m;
11351e78ac21SJeffrey Hsu 
11361e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
11371e78ac21SJeffrey Hsu 			off += m->m_len;
11381e78ac21SJeffrey Hsu 
11391e78ac21SJeffrey Hsu 		/*
11401e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
11411e78ac21SJeffrey Hsu 		 * 8-byte boundary
11421e78ac21SJeffrey Hsu 		 */
11431e78ac21SJeffrey Hsu 		if (off < hlen)
11441e78ac21SJeffrey Hsu 			goto smart_frag_failure;
11451e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
11461e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
11471e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
11481e78ac21SJeffrey Hsu 			/* we failed, go back the default */
11491e78ac21SJeffrey Hsu smart_frag_failure:
11501e78ac21SJeffrey Hsu 			newlen = len;
11511e78ac21SJeffrey Hsu 			off = hlen + len;
11521e78ac21SJeffrey Hsu 		}
11531e78ac21SJeffrey Hsu 		len = newlen;
11541e78ac21SJeffrey Hsu 
11551e78ac21SJeffrey Hsu 	} else {
11561e78ac21SJeffrey Hsu 		off = hlen + len;
11571e78ac21SJeffrey Hsu 	}
11581e78ac21SJeffrey Hsu 
11591e78ac21SJeffrey Hsu 	firstlen = off - hlen;
11601e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
11611e78ac21SJeffrey Hsu 
11621e78ac21SJeffrey Hsu 	/*
11631e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
11641e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
11651e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
11661e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
11671e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
11681e78ac21SJeffrey Hsu 	 */
11691e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
11701e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
11711e78ac21SJeffrey Hsu 		struct mbuf *m;
11721e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
11731e78ac21SJeffrey Hsu 
11741e78ac21SJeffrey Hsu 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
11751e78ac21SJeffrey Hsu 		if (m == 0) {
11761e78ac21SJeffrey Hsu 			error = ENOBUFS;
11771e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
11781e78ac21SJeffrey Hsu 			goto done;
11791e78ac21SJeffrey Hsu 		}
11801e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
11811e78ac21SJeffrey Hsu 		/*
11821e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
11831e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
11841e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
11851e78ac21SJeffrey Hsu 		 */
11861e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
11871e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
11881e78ac21SJeffrey Hsu 		*mhip = *ip;
11891e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
11901e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
11911e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
11921e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
11931e78ac21SJeffrey Hsu 		}
11941e78ac21SJeffrey Hsu 		m->m_len = mhlen;
11951e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
11961e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
11971e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
11981e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
11991e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
12001e78ac21SJeffrey Hsu 		} else
12011e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
12021e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
12031e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
12041e78ac21SJeffrey Hsu 		if (m->m_next == 0) {		/* copy failed */
12051e78ac21SJeffrey Hsu 			m_free(m);
12061e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
12071e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
12081e78ac21SJeffrey Hsu 			goto done;
12091e78ac21SJeffrey Hsu 		}
12101e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
12111e78ac21SJeffrey Hsu 		m->m_pkthdr.rcvif = (struct ifnet *)0;
12121e78ac21SJeffrey Hsu #ifdef MAC
12131e78ac21SJeffrey Hsu 		mac_create_fragment(m0, m);
12141e78ac21SJeffrey Hsu #endif
12151e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
12161e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
12171e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
12181e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
12191e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
12201e78ac21SJeffrey Hsu 		*mnext = m;
12211e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
12221e78ac21SJeffrey Hsu 	}
12231e78ac21SJeffrey Hsu 	ipstat.ips_ofragments += nfrags;
12241e78ac21SJeffrey Hsu 
12251e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
12261e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
12271e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
12281e78ac21SJeffrey Hsu 
12291e78ac21SJeffrey Hsu 	/*
12301e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
12311e78ac21SJeffrey Hsu 	 * and updating header.
12321e78ac21SJeffrey Hsu 	 */
12331e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
12341e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
12351e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
12361e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
12371e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
12381e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
12391e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
12401e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
12411e78ac21SJeffrey Hsu 
12421e78ac21SJeffrey Hsu done:
12431e78ac21SJeffrey Hsu 	*m_frag = m0;
12441e78ac21SJeffrey Hsu 	return error;
12451e78ac21SJeffrey Hsu }
12461e78ac21SJeffrey Hsu 
12471c238475SJonathan Lemon void
1248db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1249db4f9cc7SJonathan Lemon {
1250db4f9cc7SJonathan Lemon 	struct ip *ip;
1251db4f9cc7SJonathan Lemon 	u_short csum, offset;
1252db4f9cc7SJonathan Lemon 
1253db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
125453be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
1255db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1256206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1257206a3274SRuslan Ermilov 		csum = 0xffff;
1258db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1259db4f9cc7SJonathan Lemon 
1260db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1261db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1262db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1263db4f9cc7SJonathan Lemon 		/*
1264db4f9cc7SJonathan Lemon 		 * XXX
1265db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1266db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1267db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1268db4f9cc7SJonathan Lemon 		 */
1269db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1270db4f9cc7SJonathan Lemon 	}
1271db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1272db4f9cc7SJonathan Lemon }
1273db4f9cc7SJonathan Lemon 
1274df8bae1dSRodney W. Grimes /*
1275df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1276df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1277df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1278072b9b24SPaul Traina  *
1279072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1280df8bae1dSRodney W. Grimes  */
1281df8bae1dSRodney W. Grimes static struct mbuf *
1282b375c9ecSLuigi Rizzo ip_insertoptions(m, opt, phlen)
1283b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1284b375c9ecSLuigi Rizzo 	struct mbuf *opt;
1285b375c9ecSLuigi Rizzo 	int *phlen;
1286df8bae1dSRodney W. Grimes {
1287b375c9ecSLuigi Rizzo 	register struct ipoption *p = mtod(opt, struct ipoption *);
1288df8bae1dSRodney W. Grimes 	struct mbuf *n;
1289b375c9ecSLuigi Rizzo 	register struct ip *ip = mtod(m, struct ip *);
1290df8bae1dSRodney W. Grimes 	unsigned optlen;
1291df8bae1dSRodney W. Grimes 
1292df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
12931e78ac21SJeffrey Hsu 	if (optlen + ip->ip_len > IP_MAXPACKET) {
1294cb7641e8SMaxim Konovalov 		*phlen = 0;
1295df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1296cb7641e8SMaxim Konovalov 	}
1297df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1298df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1299df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1300a163d034SWarner Losh 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1301cb7641e8SMaxim Konovalov 		if (n == 0) {
1302cb7641e8SMaxim Konovalov 			*phlen = 0;
1303df8bae1dSRodney W. Grimes 			return (m);
1304cb7641e8SMaxim Konovalov 		}
13055db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
13064ed84624SRobert Watson #ifdef MAC
13074ed84624SRobert Watson 		mac_create_mbuf_from_mbuf(m, n);
13084ed84624SRobert Watson #endif
1309df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1310df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1311df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1312df8bae1dSRodney W. Grimes 		n->m_next = m;
1313df8bae1dSRodney W. Grimes 		m = n;
1314df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1315df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1316212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1317df8bae1dSRodney W. Grimes 	} else {
1318df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1319df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1320df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1321212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1322df8bae1dSRodney W. Grimes 	}
1323df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
13240453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1325df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
132653be11f6SPoul-Henning Kamp 	ip->ip_v = IPVERSION;
132753be11f6SPoul-Henning Kamp 	ip->ip_hl = *phlen >> 2;
1328df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1329df8bae1dSRodney W. Grimes 	return (m);
1330df8bae1dSRodney W. Grimes }
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes /*
1333df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1334df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1335df8bae1dSRodney W. Grimes  */
1336beec8214SDarren Reed int
1337b375c9ecSLuigi Rizzo ip_optcopy(ip, jp)
1338b375c9ecSLuigi Rizzo 	struct ip *ip, *jp;
1339df8bae1dSRodney W. Grimes {
1340b375c9ecSLuigi Rizzo 	register u_char *cp, *dp;
1341df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1342df8bae1dSRodney W. Grimes 
1343df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1344df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
134553be11f6SPoul-Henning Kamp 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1346df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1347df8bae1dSRodney W. Grimes 		opt = cp[0];
1348df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1349df8bae1dSRodney W. Grimes 			break;
1350df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1351df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1352df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1353df8bae1dSRodney W. Grimes 			optlen = 1;
1354df8bae1dSRodney W. Grimes 			continue;
1355686cdd19SJun-ichiro itojun Hagino 		}
1356db40007dSAndrew R. Reiter 
1357db40007dSAndrew R. Reiter 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1358db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1359df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1360db40007dSAndrew R. Reiter 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1361db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1362db40007dSAndrew R. Reiter 
1363df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1364df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1365df8bae1dSRodney W. Grimes 			optlen = cnt;
1366df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
13670453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1368df8bae1dSRodney W. Grimes 			dp += optlen;
1369df8bae1dSRodney W. Grimes 		}
1370df8bae1dSRodney W. Grimes 	}
1371df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1372df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1373df8bae1dSRodney W. Grimes 	return (optlen);
1374df8bae1dSRodney W. Grimes }
1375df8bae1dSRodney W. Grimes 
1376df8bae1dSRodney W. Grimes /*
1377df8bae1dSRodney W. Grimes  * IP socket option processing.
1378df8bae1dSRodney W. Grimes  */
1379df8bae1dSRodney W. Grimes int
1380b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt)
1381b375c9ecSLuigi Rizzo 	struct socket *so;
1382b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1383df8bae1dSRodney W. Grimes {
1384cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1385cfe8b629SGarrett Wollman 	int	error, optval;
1386df8bae1dSRodney W. Grimes 
1387cfe8b629SGarrett Wollman 	error = optval = 0;
1388cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1389cfe8b629SGarrett Wollman 		return (EINVAL);
1390cfe8b629SGarrett Wollman 	}
1391df8bae1dSRodney W. Grimes 
1392cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1393cfe8b629SGarrett Wollman 	case SOPT_SET:
1394cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1395df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1396df8bae1dSRodney W. Grimes #ifdef notyet
1397df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1398df8bae1dSRodney W. Grimes #endif
1399cfe8b629SGarrett Wollman 		{
1400cfe8b629SGarrett Wollman 			struct mbuf *m;
1401cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1402cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1403cfe8b629SGarrett Wollman 				break;
1404cfe8b629SGarrett Wollman 			}
1405a163d034SWarner Losh 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1406cfe8b629SGarrett Wollman 			if (m == 0) {
1407cfe8b629SGarrett Wollman 				error = ENOBUFS;
1408cfe8b629SGarrett Wollman 				break;
1409cfe8b629SGarrett Wollman 			}
1410cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1411cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1412cfe8b629SGarrett Wollman 					    m->m_len);
1413cfe8b629SGarrett Wollman 
1414cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1415cfe8b629SGarrett Wollman 					   m));
1416cfe8b629SGarrett Wollman 		}
1417df8bae1dSRodney W. Grimes 
1418df8bae1dSRodney W. Grimes 		case IP_TOS:
1419df8bae1dSRodney W. Grimes 		case IP_TTL:
1420df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1421df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1422df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
14234957466bSMatthew N. Dodd 		case IP_RECVTTL:
142482c23ebaSBill Fenner 		case IP_RECVIF:
14256a800098SYoshinobu Inoue 		case IP_FAITH:
14268afa2304SBruce M Simpson 		case IP_ONESBCAST:
1427cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1428cfe8b629SGarrett Wollman 					    sizeof optval);
1429cfe8b629SGarrett Wollman 			if (error)
1430cfe8b629SGarrett Wollman 				break;
1431df8bae1dSRodney W. Grimes 
1432cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1433df8bae1dSRodney W. Grimes 			case IP_TOS:
1434ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1435df8bae1dSRodney W. Grimes 				break;
1436df8bae1dSRodney W. Grimes 
1437df8bae1dSRodney W. Grimes 			case IP_TTL:
1438ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1439df8bae1dSRodney W. Grimes 				break;
1440a138d217SRobert Watson #define	OPTSET(bit) do {						\
1441a138d217SRobert Watson 	INP_LOCK(inp);							\
1442df8bae1dSRodney W. Grimes 	if (optval)							\
1443df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
1444df8bae1dSRodney W. Grimes 	else								\
1445a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
1446a138d217SRobert Watson 	INP_UNLOCK(inp);						\
1447a138d217SRobert Watson } while (0)
1448df8bae1dSRodney W. Grimes 
1449df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1450df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1451df8bae1dSRodney W. Grimes 				break;
1452df8bae1dSRodney W. Grimes 
1453df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1454df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1455df8bae1dSRodney W. Grimes 				break;
1456df8bae1dSRodney W. Grimes 
1457df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1458df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1459df8bae1dSRodney W. Grimes 				break;
146082c23ebaSBill Fenner 
14614957466bSMatthew N. Dodd 			case IP_RECVTTL:
14624957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
14634957466bSMatthew N. Dodd 				break;
14644957466bSMatthew N. Dodd 
146582c23ebaSBill Fenner 			case IP_RECVIF:
146682c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
146782c23ebaSBill Fenner 				break;
14686a800098SYoshinobu Inoue 
14696a800098SYoshinobu Inoue 			case IP_FAITH:
14706a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
14716a800098SYoshinobu Inoue 				break;
14728afa2304SBruce M Simpson 
14738afa2304SBruce M Simpson 			case IP_ONESBCAST:
14748afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
14758afa2304SBruce M Simpson 				break;
1476df8bae1dSRodney W. Grimes 			}
1477df8bae1dSRodney W. Grimes 			break;
1478df8bae1dSRodney W. Grimes #undef OPTSET
1479df8bae1dSRodney W. Grimes 
1480df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1481f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1482df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1483df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1484df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1485df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1486cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1487df8bae1dSRodney W. Grimes 			break;
1488df8bae1dSRodney W. Grimes 
148933b3ac06SPeter Wemm 		case IP_PORTRANGE:
1490cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1491cfe8b629SGarrett Wollman 					    sizeof optval);
1492cfe8b629SGarrett Wollman 			if (error)
1493cfe8b629SGarrett Wollman 				break;
149433b3ac06SPeter Wemm 
1495a138d217SRobert Watson 			INP_LOCK(inp);
149633b3ac06SPeter Wemm 			switch (optval) {
149733b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
149833b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
149933b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
150033b3ac06SPeter Wemm 				break;
150133b3ac06SPeter Wemm 
150233b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
150333b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
150433b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
150533b3ac06SPeter Wemm 				break;
150633b3ac06SPeter Wemm 
150733b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
150833b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
150933b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
151033b3ac06SPeter Wemm 				break;
151133b3ac06SPeter Wemm 
151233b3ac06SPeter Wemm 			default:
151333b3ac06SPeter Wemm 				error = EINVAL;
151433b3ac06SPeter Wemm 				break;
151533b3ac06SPeter Wemm 			}
1516a138d217SRobert Watson 			INP_UNLOCK(inp);
1517ce8c72b1SPeter Wemm 			break;
151833b3ac06SPeter Wemm 
1519b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
15206a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
15216a800098SYoshinobu Inoue 		{
15226a800098SYoshinobu Inoue 			caddr_t req;
1523686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
15246a800098SYoshinobu Inoue 			int priv;
15256a800098SYoshinobu Inoue 			struct mbuf *m;
15266a800098SYoshinobu Inoue 			int optname;
15276a800098SYoshinobu Inoue 
15286a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
15296a800098SYoshinobu Inoue 				break;
15306a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
15316a800098SYoshinobu Inoue 				break;
1532b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
153344731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
15346a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1535686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
15366a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1537686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
15386a800098SYoshinobu Inoue 			m_freem(m);
15396a800098SYoshinobu Inoue 			break;
15406a800098SYoshinobu Inoue 		}
15416a800098SYoshinobu Inoue #endif /*IPSEC*/
15426a800098SYoshinobu Inoue 
1543df8bae1dSRodney W. Grimes 		default:
1544df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1545df8bae1dSRodney W. Grimes 			break;
1546df8bae1dSRodney W. Grimes 		}
1547df8bae1dSRodney W. Grimes 		break;
1548df8bae1dSRodney W. Grimes 
1549cfe8b629SGarrett Wollman 	case SOPT_GET:
1550cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1551df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1552df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1553cfe8b629SGarrett Wollman 			if (inp->inp_options)
1554cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1555cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1556cfe8b629SGarrett Wollman 							  char *),
1557cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1558cfe8b629SGarrett Wollman 			else
1559cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1560df8bae1dSRodney W. Grimes 			break;
1561df8bae1dSRodney W. Grimes 
1562df8bae1dSRodney W. Grimes 		case IP_TOS:
1563df8bae1dSRodney W. Grimes 		case IP_TTL:
1564df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1565df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1566df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
15674957466bSMatthew N. Dodd 		case IP_RECVTTL:
156882c23ebaSBill Fenner 		case IP_RECVIF:
1569cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
15706a800098SYoshinobu Inoue 		case IP_FAITH:
15718afa2304SBruce M Simpson 		case IP_ONESBCAST:
1572cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1573df8bae1dSRodney W. Grimes 
1574df8bae1dSRodney W. Grimes 			case IP_TOS:
1575ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1576df8bae1dSRodney W. Grimes 				break;
1577df8bae1dSRodney W. Grimes 
1578df8bae1dSRodney W. Grimes 			case IP_TTL:
1579ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1580df8bae1dSRodney W. Grimes 				break;
1581df8bae1dSRodney W. Grimes 
1582df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1583df8bae1dSRodney W. Grimes 
1584df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1585df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1586df8bae1dSRodney W. Grimes 				break;
1587df8bae1dSRodney W. Grimes 
1588df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1589df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1590df8bae1dSRodney W. Grimes 				break;
1591df8bae1dSRodney W. Grimes 
1592df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1593df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1594df8bae1dSRodney W. Grimes 				break;
159582c23ebaSBill Fenner 
15964957466bSMatthew N. Dodd 			case IP_RECVTTL:
15974957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
15984957466bSMatthew N. Dodd 				break;
15994957466bSMatthew N. Dodd 
160082c23ebaSBill Fenner 			case IP_RECVIF:
160182c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
160282c23ebaSBill Fenner 				break;
1603cfe8b629SGarrett Wollman 
1604cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1605cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1606cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1607cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1608cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1609cfe8b629SGarrett Wollman 				else
1610cfe8b629SGarrett Wollman 					optval = 0;
1611cfe8b629SGarrett Wollman 				break;
16126a800098SYoshinobu Inoue 
16136a800098SYoshinobu Inoue 			case IP_FAITH:
16146a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
16156a800098SYoshinobu Inoue 				break;
16168afa2304SBruce M Simpson 
16178afa2304SBruce M Simpson 			case IP_ONESBCAST:
16188afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
16198afa2304SBruce M Simpson 				break;
1620df8bae1dSRodney W. Grimes 			}
1621cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1622df8bae1dSRodney W. Grimes 			break;
1623df8bae1dSRodney W. Grimes 
1624df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1625f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1626df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1627df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1628df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1629df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1630cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
163133b3ac06SPeter Wemm 			break;
163233b3ac06SPeter Wemm 
1633b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
16346a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
16356a800098SYoshinobu Inoue 		{
1636f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
16376a800098SYoshinobu Inoue 			caddr_t req = NULL;
1638686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
16396a800098SYoshinobu Inoue 
1640686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
16416a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1642686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1643686cdd19SJun-ichiro itojun Hagino 			}
1644686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
16456a800098SYoshinobu Inoue 			if (error == 0)
16466a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1647f63e7634SYoshinobu Inoue 			if (error == 0)
16486a800098SYoshinobu Inoue 				m_freem(m);
16496a800098SYoshinobu Inoue 			break;
16506a800098SYoshinobu Inoue 		}
16516a800098SYoshinobu Inoue #endif /*IPSEC*/
16526a800098SYoshinobu Inoue 
1653df8bae1dSRodney W. Grimes 		default:
1654df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1655df8bae1dSRodney W. Grimes 			break;
1656df8bae1dSRodney W. Grimes 		}
1657df8bae1dSRodney W. Grimes 		break;
1658df8bae1dSRodney W. Grimes 	}
1659df8bae1dSRodney W. Grimes 	return (error);
1660df8bae1dSRodney W. Grimes }
1661df8bae1dSRodney W. Grimes 
1662df8bae1dSRodney W. Grimes /*
1663df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1664df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1665df8bae1dSRodney W. Grimes  * with destination address if source routed.
1666df8bae1dSRodney W. Grimes  */
16670312fbe9SPoul-Henning Kamp static int
1668b375c9ecSLuigi Rizzo ip_pcbopts(optname, pcbopt, m)
1669b375c9ecSLuigi Rizzo 	int optname;
1670b375c9ecSLuigi Rizzo 	struct mbuf **pcbopt;
1671b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1672df8bae1dSRodney W. Grimes {
1673b375c9ecSLuigi Rizzo 	register int cnt, optlen;
1674b375c9ecSLuigi Rizzo 	register u_char *cp;
1675df8bae1dSRodney W. Grimes 	u_char opt;
1676df8bae1dSRodney W. Grimes 
1677df8bae1dSRodney W. Grimes 	/* turn off any old options */
1678df8bae1dSRodney W. Grimes 	if (*pcbopt)
1679df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1680df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1681df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1682df8bae1dSRodney W. Grimes 		/*
1683df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1684df8bae1dSRodney W. Grimes 		 */
1685df8bae1dSRodney W. Grimes 		if (m)
1686df8bae1dSRodney W. Grimes 			(void)m_free(m);
1687df8bae1dSRodney W. Grimes 		return (0);
1688df8bae1dSRodney W. Grimes 	}
1689df8bae1dSRodney W. Grimes 
16900c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1691df8bae1dSRodney W. Grimes 		goto bad;
1692df8bae1dSRodney W. Grimes 	/*
1693df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1694df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1695df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1696df8bae1dSRodney W. Grimes 	 */
1697df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1698df8bae1dSRodney W. Grimes 		goto bad;
1699df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1700df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1701df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1702212059bdSDag-Erling Smørgrav 	bcopy(mtod(m, void *), cp, (unsigned)cnt);
1703212059bdSDag-Erling Smørgrav 	bzero(mtod(m, void *), sizeof(struct in_addr));
1704df8bae1dSRodney W. Grimes 
1705df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1706df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1707df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1708df8bae1dSRodney W. Grimes 			break;
1709df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1710df8bae1dSRodney W. Grimes 			optlen = 1;
1711df8bae1dSRodney W. Grimes 		else {
1712707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1713707d00a3SJonathan Lemon 				goto bad;
1714df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1715707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1716df8bae1dSRodney W. Grimes 				goto bad;
1717df8bae1dSRodney W. Grimes 		}
1718df8bae1dSRodney W. Grimes 		switch (opt) {
1719df8bae1dSRodney W. Grimes 
1720df8bae1dSRodney W. Grimes 		default:
1721df8bae1dSRodney W. Grimes 			break;
1722df8bae1dSRodney W. Grimes 
1723df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1724df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1725df8bae1dSRodney W. Grimes 			/*
1726df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1727df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1728df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1729df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1730df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1731df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1732df8bae1dSRodney W. Grimes 			 */
1733df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1734df8bae1dSRodney W. Grimes 				goto bad;
1735df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1736df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1737df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1738df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1739df8bae1dSRodney W. Grimes 			/*
1740df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1741df8bae1dSRodney W. Grimes 			 */
1742df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1743df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1744df8bae1dSRodney W. Grimes 			/*
1745df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1746df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1747df8bae1dSRodney W. Grimes 			 */
1748212059bdSDag-Erling Smørgrav 			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1749212059bdSDag-Erling Smørgrav 			    &cp[IPOPT_OFFSET+1],
1750a49b2137SMaxim Konovalov 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
1751df8bae1dSRodney W. Grimes 			break;
1752df8bae1dSRodney W. Grimes 		}
1753df8bae1dSRodney W. Grimes 	}
1754df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1755df8bae1dSRodney W. Grimes 		goto bad;
1756df8bae1dSRodney W. Grimes 	*pcbopt = m;
1757df8bae1dSRodney W. Grimes 	return (0);
1758df8bae1dSRodney W. Grimes 
1759df8bae1dSRodney W. Grimes bad:
1760df8bae1dSRodney W. Grimes 	(void)m_free(m);
1761df8bae1dSRodney W. Grimes 	return (EINVAL);
1762df8bae1dSRodney W. Grimes }
1763df8bae1dSRodney W. Grimes 
1764df8bae1dSRodney W. Grimes /*
1765cfe8b629SGarrett Wollman  * XXX
1766cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1767cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1768cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1769cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1770cfe8b629SGarrett Wollman  */
177133841545SHajimu UMEMOTO 
177233841545SHajimu UMEMOTO /*
177333841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
177433841545SHajimu UMEMOTO  */
177533841545SHajimu UMEMOTO static struct ifnet *
1776b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp)
1777b375c9ecSLuigi Rizzo 	struct in_addr *a;
1778b375c9ecSLuigi Rizzo 	int *ifindexp;
177933841545SHajimu UMEMOTO {
178033841545SHajimu UMEMOTO 	int ifindex;
178133841545SHajimu UMEMOTO 	struct ifnet *ifp;
178233841545SHajimu UMEMOTO 
178333841545SHajimu UMEMOTO 	if (ifindexp)
178433841545SHajimu UMEMOTO 		*ifindexp = 0;
178533841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
178633841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
178733841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
178833841545SHajimu UMEMOTO 			return NULL;
1789f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
179033841545SHajimu UMEMOTO 		if (ifindexp)
179133841545SHajimu UMEMOTO 			*ifindexp = ifindex;
179233841545SHajimu UMEMOTO 	} else {
179333841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
179433841545SHajimu UMEMOTO 	}
179533841545SHajimu UMEMOTO 	return ifp;
179633841545SHajimu UMEMOTO }
179733841545SHajimu UMEMOTO 
1798cfe8b629SGarrett Wollman /*
1799df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1800df8bae1dSRodney W. Grimes  */
18010312fbe9SPoul-Henning Kamp static int
1802b375c9ecSLuigi Rizzo ip_setmoptions(sopt, imop)
1803b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1804b375c9ecSLuigi Rizzo 	struct ip_moptions **imop;
1805df8bae1dSRodney W. Grimes {
1806cfe8b629SGarrett Wollman 	int error = 0;
1807cfe8b629SGarrett Wollman 	int i;
1808df8bae1dSRodney W. Grimes 	struct in_addr addr;
1809cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1810cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1811cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1812df8bae1dSRodney W. Grimes 	struct route ro;
1813cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
181433841545SHajimu UMEMOTO 	int ifindex;
18159b626c29SGarrett Wollman 	int s;
1816df8bae1dSRodney W. Grimes 
1817df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1818df8bae1dSRodney W. Grimes 		/*
1819df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1820df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1821df8bae1dSRodney W. Grimes 		 */
1822df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1823a163d034SWarner Losh 		    M_WAITOK);
1824df8bae1dSRodney W. Grimes 
1825df8bae1dSRodney W. Grimes 		if (imo == NULL)
1826df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1827df8bae1dSRodney W. Grimes 		*imop = imo;
1828df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
182933841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
18301c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1831df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1832df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1833df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1834df8bae1dSRodney W. Grimes 	}
1835df8bae1dSRodney W. Grimes 
1836cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1837f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1838f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1839cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
18405e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
18415e9ae478SGarrett Wollman 			break;
18425e9ae478SGarrett Wollman 		}
1843cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1844cfe8b629SGarrett Wollman 		if (error)
1845f0068c4aSGarrett Wollman 			break;
18461c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1847f0068c4aSGarrett Wollman 			error = EINVAL;
1848f0068c4aSGarrett Wollman 			break;
1849f0068c4aSGarrett Wollman 		}
1850f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1851f0068c4aSGarrett Wollman 		break;
1852f0068c4aSGarrett Wollman 
1853df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1854df8bae1dSRodney W. Grimes 		/*
1855df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1856df8bae1dSRodney W. Grimes 		 */
1857cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1858cfe8b629SGarrett Wollman 		if (error)
1859df8bae1dSRodney W. Grimes 			break;
1860df8bae1dSRodney W. Grimes 		/*
1861df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1862df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1863df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1864df8bae1dSRodney W. Grimes 		 */
1865df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1866df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1867df8bae1dSRodney W. Grimes 			break;
1868df8bae1dSRodney W. Grimes 		}
1869df8bae1dSRodney W. Grimes 		/*
1870df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1871df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1872df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1873df8bae1dSRodney W. Grimes 		 */
187420e8807cSGarrett Wollman 		s = splimp();
187533841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1876df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1877fbc6ab00SBill Fenner 			splx(s);
1878df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1879df8bae1dSRodney W. Grimes 			break;
1880df8bae1dSRodney W. Grimes 		}
1881df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
188233841545SHajimu UMEMOTO 		if (ifindex)
188333841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
188433841545SHajimu UMEMOTO 		else
188533841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
18869b626c29SGarrett Wollman 		splx(s);
1887df8bae1dSRodney W. Grimes 		break;
1888df8bae1dSRodney W. Grimes 
1889df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1890df8bae1dSRodney W. Grimes 		/*
1891df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1892cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1893cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1894cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1895df8bae1dSRodney W. Grimes 		 */
1896cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1897cfe8b629SGarrett Wollman 			u_char ttl;
1898cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1899cfe8b629SGarrett Wollman 			if (error)
1900df8bae1dSRodney W. Grimes 				break;
1901cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1902cfe8b629SGarrett Wollman 		} else {
1903cfe8b629SGarrett Wollman 			u_int ttl;
1904cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1905cfe8b629SGarrett Wollman 					    sizeof ttl);
1906cfe8b629SGarrett Wollman 			if (error)
1907cfe8b629SGarrett Wollman 				break;
1908cfe8b629SGarrett Wollman 			if (ttl > 255)
1909cfe8b629SGarrett Wollman 				error = EINVAL;
1910cfe8b629SGarrett Wollman 			else
1911cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1912df8bae1dSRodney W. Grimes 		}
1913df8bae1dSRodney W. Grimes 		break;
1914df8bae1dSRodney W. Grimes 
1915df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1916df8bae1dSRodney W. Grimes 		/*
1917df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1918cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1919cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1920cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1921df8bae1dSRodney W. Grimes 		 */
1922cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1923cfe8b629SGarrett Wollman 			u_char loop;
1924cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1925cfe8b629SGarrett Wollman 			if (error)
1926df8bae1dSRodney W. Grimes 				break;
1927cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1928cfe8b629SGarrett Wollman 		} else {
1929cfe8b629SGarrett Wollman 			u_int loop;
1930cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1931cfe8b629SGarrett Wollman 					    sizeof loop);
1932cfe8b629SGarrett Wollman 			if (error)
1933cfe8b629SGarrett Wollman 				break;
1934cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1935df8bae1dSRodney W. Grimes 		}
1936df8bae1dSRodney W. Grimes 		break;
1937df8bae1dSRodney W. Grimes 
1938df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1939df8bae1dSRodney W. Grimes 		/*
1940df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1941df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1942df8bae1dSRodney W. Grimes 		 */
1943cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1944cfe8b629SGarrett Wollman 		if (error)
1945df8bae1dSRodney W. Grimes 			break;
1946cfe8b629SGarrett Wollman 
1947cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1948df8bae1dSRodney W. Grimes 			error = EINVAL;
1949df8bae1dSRodney W. Grimes 			break;
1950df8bae1dSRodney W. Grimes 		}
195120e8807cSGarrett Wollman 		s = splimp();
1952df8bae1dSRodney W. Grimes 		/*
1953df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1954df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1955df8bae1dSRodney W. Grimes 		 */
1956cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
19571c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1958df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1959df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1960df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1961cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
196297d8d152SAndre Oppermann 			rtalloc_ign(&ro, RTF_CLONING);
1963df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1964df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
19659b626c29SGarrett Wollman 				splx(s);
1966df8bae1dSRodney W. Grimes 				break;
1967df8bae1dSRodney W. Grimes 			}
1968df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1969d1dd20beSSam Leffler 			RTFREE(ro.ro_rt);
1970df8bae1dSRodney W. Grimes 		}
1971df8bae1dSRodney W. Grimes 		else {
197233841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1973df8bae1dSRodney W. Grimes 		}
19749b626c29SGarrett Wollman 
1975df8bae1dSRodney W. Grimes 		/*
1976df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1977df8bae1dSRodney W. Grimes 		 * supports multicast.
1978df8bae1dSRodney W. Grimes 		 */
1979df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1980df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
19819b626c29SGarrett Wollman 			splx(s);
1982df8bae1dSRodney W. Grimes 			break;
1983df8bae1dSRodney W. Grimes 		}
1984df8bae1dSRodney W. Grimes 		/*
1985df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1986df8bae1dSRodney W. Grimes 		 * membership slots are full.
1987df8bae1dSRodney W. Grimes 		 */
1988df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1989df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1990df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1991cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1992df8bae1dSRodney W. Grimes 				break;
1993df8bae1dSRodney W. Grimes 		}
1994df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1995df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
19969b626c29SGarrett Wollman 			splx(s);
1997df8bae1dSRodney W. Grimes 			break;
1998df8bae1dSRodney W. Grimes 		}
1999df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
2000df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
20019b626c29SGarrett Wollman 			splx(s);
2002df8bae1dSRodney W. Grimes 			break;
2003df8bae1dSRodney W. Grimes 		}
2004df8bae1dSRodney W. Grimes 		/*
2005df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
2006df8bae1dSRodney W. Grimes 		 * address list for the given interface.
2007df8bae1dSRodney W. Grimes 		 */
2008df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
2009cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
2010df8bae1dSRodney W. Grimes 			error = ENOBUFS;
20119b626c29SGarrett Wollman 			splx(s);
2012df8bae1dSRodney W. Grimes 			break;
2013df8bae1dSRodney W. Grimes 		}
2014df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
20159b626c29SGarrett Wollman 		splx(s);
2016df8bae1dSRodney W. Grimes 		break;
2017df8bae1dSRodney W. Grimes 
2018df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
2019df8bae1dSRodney W. Grimes 		/*
2020df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
2021df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
2022df8bae1dSRodney W. Grimes 		 */
2023cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
2024cfe8b629SGarrett Wollman 		if (error)
2025df8bae1dSRodney W. Grimes 			break;
2026cfe8b629SGarrett Wollman 
2027cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
2028df8bae1dSRodney W. Grimes 			error = EINVAL;
2029df8bae1dSRodney W. Grimes 			break;
2030df8bae1dSRodney W. Grimes 		}
20319b626c29SGarrett Wollman 
203220e8807cSGarrett Wollman 		s = splimp();
2033df8bae1dSRodney W. Grimes 		/*
2034df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
2035df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
2036df8bae1dSRodney W. Grimes 		 */
2037cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
2038df8bae1dSRodney W. Grimes 			ifp = NULL;
2039df8bae1dSRodney W. Grimes 		else {
204033841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
2041df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
2042df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
20439b626c29SGarrett Wollman 				splx(s);
2044df8bae1dSRodney W. Grimes 				break;
2045df8bae1dSRodney W. Grimes 			}
2046df8bae1dSRodney W. Grimes 		}
2047df8bae1dSRodney W. Grimes 		/*
2048df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
2049df8bae1dSRodney W. Grimes 		 */
2050df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
2051df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
2052df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
2053df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
2054cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
2055df8bae1dSRodney W. Grimes 				break;
2056df8bae1dSRodney W. Grimes 		}
2057df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
2058df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
20599b626c29SGarrett Wollman 			splx(s);
2060df8bae1dSRodney W. Grimes 			break;
2061df8bae1dSRodney W. Grimes 		}
2062df8bae1dSRodney W. Grimes 		/*
2063df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
2064df8bae1dSRodney W. Grimes 		 * membership points.
2065df8bae1dSRodney W. Grimes 		 */
2066df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
2067df8bae1dSRodney W. Grimes 		/*
2068df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
2069df8bae1dSRodney W. Grimes 		 */
2070df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
2071df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
2072df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
20739b626c29SGarrett Wollman 		splx(s);
2074df8bae1dSRodney W. Grimes 		break;
2075df8bae1dSRodney W. Grimes 
2076df8bae1dSRodney W. Grimes 	default:
2077df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
2078df8bae1dSRodney W. Grimes 		break;
2079df8bae1dSRodney W. Grimes 	}
2080df8bae1dSRodney W. Grimes 
2081df8bae1dSRodney W. Grimes 	/*
2082df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
2083df8bae1dSRodney W. Grimes 	 */
2084df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
20851c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
2086df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2087df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2088df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
2089df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
2090df8bae1dSRodney W. Grimes 		*imop = NULL;
2091df8bae1dSRodney W. Grimes 	}
2092df8bae1dSRodney W. Grimes 
2093df8bae1dSRodney W. Grimes 	return (error);
2094df8bae1dSRodney W. Grimes }
2095df8bae1dSRodney W. Grimes 
2096df8bae1dSRodney W. Grimes /*
2097df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
2098df8bae1dSRodney W. Grimes  */
20990312fbe9SPoul-Henning Kamp static int
2100b375c9ecSLuigi Rizzo ip_getmoptions(sopt, imo)
2101b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
2102b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
2103df8bae1dSRodney W. Grimes {
2104cfe8b629SGarrett Wollman 	struct in_addr addr;
2105df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
2106cfe8b629SGarrett Wollman 	int error, optval;
2107cfe8b629SGarrett Wollman 	u_char coptval;
2108df8bae1dSRodney W. Grimes 
2109cfe8b629SGarrett Wollman 	error = 0;
2110cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
2111f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
2112f0068c4aSGarrett Wollman 		if (imo != NULL)
2113cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
2114f0068c4aSGarrett Wollman 		else
2115cfe8b629SGarrett Wollman 			optval = -1;
2116cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
2117cfe8b629SGarrett Wollman 		break;
2118f0068c4aSGarrett Wollman 
2119df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
2120df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
2121cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
212233841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
212333841545SHajimu UMEMOTO 			/* return the value user has set */
212433841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
212533841545SHajimu UMEMOTO 		} else {
2126df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
2127cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
2128df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
2129df8bae1dSRodney W. Grimes 		}
2130cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
2131cfe8b629SGarrett Wollman 		break;
2132df8bae1dSRodney W. Grimes 
2133df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
2134cfe8b629SGarrett Wollman 		if (imo == 0)
2135cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
2136cfe8b629SGarrett Wollman 		else
2137cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
2138cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
2139cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
2140cfe8b629SGarrett Wollman 		else
2141cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
2142cfe8b629SGarrett Wollman 		break;
2143df8bae1dSRodney W. Grimes 
2144df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
2145cfe8b629SGarrett Wollman 		if (imo == 0)
2146cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
2147cfe8b629SGarrett Wollman 		else
2148cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
2149cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
2150cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
2151cfe8b629SGarrett Wollman 		else
2152cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
2153cfe8b629SGarrett Wollman 		break;
2154df8bae1dSRodney W. Grimes 
2155df8bae1dSRodney W. Grimes 	default:
2156cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
2157cfe8b629SGarrett Wollman 		break;
2158df8bae1dSRodney W. Grimes 	}
2159cfe8b629SGarrett Wollman 	return (error);
2160df8bae1dSRodney W. Grimes }
2161df8bae1dSRodney W. Grimes 
2162df8bae1dSRodney W. Grimes /*
2163df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
2164df8bae1dSRodney W. Grimes  */
2165df8bae1dSRodney W. Grimes void
2166b375c9ecSLuigi Rizzo ip_freemoptions(imo)
2167b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
2168df8bae1dSRodney W. Grimes {
2169b375c9ecSLuigi Rizzo 	register int i;
2170df8bae1dSRodney W. Grimes 
2171df8bae1dSRodney W. Grimes 	if (imo != NULL) {
2172df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
2173df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
2174df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
2175df8bae1dSRodney W. Grimes 	}
2176df8bae1dSRodney W. Grimes }
2177df8bae1dSRodney W. Grimes 
2178df8bae1dSRodney W. Grimes /*
2179df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
2180df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
2181df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
2182f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
2183f5fea3ddSPaul Traina  * replicating that code here.
2184df8bae1dSRodney W. Grimes  */
2185df8bae1dSRodney W. Grimes static void
2186b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen)
2187b375c9ecSLuigi Rizzo 	struct ifnet *ifp;
2188b375c9ecSLuigi Rizzo 	register struct mbuf *m;
2189b375c9ecSLuigi Rizzo 	register struct sockaddr_in *dst;
2190b375c9ecSLuigi Rizzo 	int hlen;
2191df8bae1dSRodney W. Grimes {
2192b375c9ecSLuigi Rizzo 	register struct ip *ip;
2193df8bae1dSRodney W. Grimes 	struct mbuf *copym;
2194df8bae1dSRodney W. Grimes 
2195b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
219686b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
219786b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
2198df8bae1dSRodney W. Grimes 	if (copym != NULL) {
2199390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
2200390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2201390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
2202390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2203390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
2204390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2205390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
2206390cdc6aSRuslan Ermilov 		}
2207df8bae1dSRodney W. Grimes 		/*
2208df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
2209df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
2210df8bae1dSRodney W. Grimes 		 */
2211df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
2212fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
2213fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
2214df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
221586b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
22169c9137eaSGarrett Wollman 		/*
22179c9137eaSGarrett Wollman 		 * NB:
2218e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
2219e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
2220e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
2221e1596dffSBill Fenner 		 * particular, everything which modifies the packet
2222e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
2223e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
2224e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
2225ed7509acSJulian Elischer 		 * of safety by using if_simloop().
22269c9137eaSGarrett Wollman 		 */
2227201c2527SJulian Elischer #if 1 /* XXX */
2228201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
22292b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
2230201c2527SJulian Elischer 						dst->sin_family);
2231201c2527SJulian Elischer 			dst->sin_family = AF_INET;
2232201c2527SJulian Elischer 		}
2233201c2527SJulian Elischer #endif
2234201c2527SJulian Elischer 
22359c9137eaSGarrett Wollman #ifdef notdef
2236e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
2237ed7509acSJulian Elischer 		ip_input(copym);
22389c9137eaSGarrett Wollman #else
223906a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
22409c9137eaSGarrett Wollman #endif
2241df8bae1dSRodney W. Grimes 	}
2242df8bae1dSRodney W. Grimes }
2243