xref: /freebsd/sys/netinet/ip_output.c (revision 53dcc544a8b9e8ffee2ad67705cd0050fb3c36f0)
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  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
371f7e052cSJulian Elischer #include "opt_ipfw.h"
38b715f178SLuigi Rizzo #include "opt_ipdn.h"
39fbd1372aSJoerg Wunsch #include "opt_ipdivert.h"
401ee25934SPeter Wemm #include "opt_ipfilter.h"
416a800098SYoshinobu Inoue #include "opt_ipsec.h"
424ed84624SRobert Watson #include "opt_mac.h"
43c4ac87eaSDarren Reed #include "opt_pfil_hooks.h"
4464dddc18SKris Kennaway #include "opt_random_ip_id.h"
4553dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
46fbd1372aSJoerg Wunsch 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
4826f9a767SRodney W. Grimes #include <sys/systm.h>
49f0f6d643SLuigi Rizzo #include <sys/kernel.h>
504ed84624SRobert Watson #include <sys/mac.h>
51df8bae1dSRodney W. Grimes #include <sys/malloc.h>
52df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
53df8bae1dSRodney W. Grimes #include <sys/protosw.h>
54df8bae1dSRodney W. Grimes #include <sys/socket.h>
55df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
569d9edc56SMike Silbersack #include <sys/sysctl.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <net/if.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <netinet/in.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
63df8bae1dSRodney W. Grimes #include <netinet/ip.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
67df8bae1dSRodney W. Grimes 
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>
89b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
90b715f178SLuigi Rizzo 
912b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
922b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
93f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
94f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
952b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
96f9e354dfSJulian Elischer 
97f23b4c91SGarrett Wollman u_short ip_id;
98f23b4c91SGarrett Wollman 
9953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
1009d9edc56SMike Silbersack int mbuf_frag_size = 0;
1019d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
1029d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1039d9edc56SMike Silbersack #endif
1049d9edc56SMike Silbersack 
1054d77a549SAlfred Perlstein static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
1064d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *);
107df8bae1dSRodney W. Grimes static void	ip_mloopback
1084d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
1090312fbe9SPoul-Henning Kamp static int	ip_getmoptions
1104d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions *);
1114d77a549SAlfred Perlstein static int	ip_pcbopts(int, struct mbuf **, struct mbuf *);
1120312fbe9SPoul-Henning Kamp static int	ip_setmoptions
1134d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions **);
114df8bae1dSRodney W. Grimes 
1154d77a549SAlfred Perlstein int	ip_optcopy(struct ip *, struct ip *);
116afed1b49SDarren Reed 
117afed1b49SDarren Reed 
11893e0e116SJulian Elischer extern	struct protosw inetsw[];
11993e0e116SJulian Elischer 
120df8bae1dSRodney W. Grimes /*
121df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
122df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
123df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
124df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
125df8bae1dSRodney W. Grimes  */
126df8bae1dSRodney W. Grimes int
127b375c9ecSLuigi Rizzo ip_output(m0, opt, ro, flags, imo, inp)
128b375c9ecSLuigi Rizzo 	struct mbuf *m0;
129b375c9ecSLuigi Rizzo 	struct mbuf *opt;
130b375c9ecSLuigi Rizzo 	struct route *ro;
131b375c9ecSLuigi Rizzo 	int flags;
132b375c9ecSLuigi Rizzo 	struct ip_moptions *imo;
133b375c9ecSLuigi Rizzo 	struct inpcb *inp;
134df8bae1dSRodney W. Grimes {
135b375c9ecSLuigi Rizzo 	struct ip *ip, *mhip;
1362b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
1372b25acc1SLuigi Rizzo 	struct mbuf *m;
13823bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
139df8bae1dSRodney W. Grimes 	int len, off, error = 0;
1402b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1413956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
142db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
1433efc3014SJulian Elischer 	struct in_addr pkt_dst;
1446a800098SYoshinobu Inoue #ifdef IPSEC
145e3f406b3SRuslan Ermilov 	struct route iproute;
1466a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1476a800098SYoshinobu Inoue #endif
148b9234fafSSam Leffler #ifdef FAST_IPSEC
149b9234fafSSam Leffler 	struct route iproute;
150b9234fafSSam Leffler 	struct m_tag *mtag;
151b9234fafSSam Leffler 	struct secpolicy *sp = NULL;
152b9234fafSSam Leffler 	struct tdb_ident *tdbi;
153b9234fafSSam Leffler 	int s;
154b9234fafSSam Leffler #endif /* FAST_IPSEC */
1552b25acc1SLuigi Rizzo 	struct ip_fw_args args;
1562b25acc1SLuigi Rizzo 	int src_was_INADDR_ANY = 0;	/* as the name says... */
157c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
158c4ac87eaSDarren Reed 	struct packet_filter_hook *pfh;
159c4ac87eaSDarren Reed 	struct mbuf *m1;
160c4ac87eaSDarren Reed 	int rv;
161c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
162b715f178SLuigi Rizzo 
1632b25acc1SLuigi Rizzo 	args.eh = NULL;
1642b25acc1SLuigi Rizzo 	args.rule = NULL;
1652b25acc1SLuigi Rizzo 	args.next_hop = NULL;
1662b25acc1SLuigi Rizzo 	args.divert_rule = 0;			/* divert cookie */
1678948e4baSArchie Cobbs 
1682b25acc1SLuigi Rizzo 	/* Grab info from MT_TAG mbufs prepended to the chain. */
1692b25acc1SLuigi Rizzo 	for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) {
1705d846453SSam Leffler 		switch(m0->_m_tag_id) {
1712b25acc1SLuigi Rizzo 		default:
1722b25acc1SLuigi Rizzo 			printf("ip_output: unrecognised MT_TAG tag %d\n",
1735d846453SSam Leffler 			    m0->_m_tag_id);
1742b25acc1SLuigi Rizzo 			break;
1752b25acc1SLuigi Rizzo 
1762b25acc1SLuigi Rizzo 		case PACKET_TAG_DUMMYNET:
177b715f178SLuigi Rizzo 			/*
178b715f178SLuigi Rizzo 			 * the packet was already tagged, so part of the
179b715f178SLuigi Rizzo 			 * processing was already done, and we need to go down.
1806bc748b0SLuigi Rizzo 			 * Get parameters from the header.
181b715f178SLuigi Rizzo 			 */
1822b25acc1SLuigi Rizzo 			args.rule = ((struct dn_pkt *)m0)->rule;
183d1f04b29SLuigi Rizzo 			opt = NULL ;
1842b25acc1SLuigi Rizzo 			ro = & ( ((struct dn_pkt *)m0)->ro ) ;
185d1f04b29SLuigi Rizzo 			imo = NULL ;
1862b25acc1SLuigi Rizzo 			dst = ((struct dn_pkt *)m0)->dn_dst ;
1872b25acc1SLuigi Rizzo 			ifp = ((struct dn_pkt *)m0)->ifp ;
1882b25acc1SLuigi Rizzo 			flags = ((struct dn_pkt *)m0)->flags ;
1892b25acc1SLuigi Rizzo 			break;
190d1f04b29SLuigi Rizzo 
1912b25acc1SLuigi Rizzo 		case PACKET_TAG_DIVERT:
1927627c6cbSMaxime Henrion 			args.divert_rule = (intptr_t)m0->m_data & 0xffff;
1932b25acc1SLuigi Rizzo 			break;
1942b25acc1SLuigi Rizzo 
1952b25acc1SLuigi Rizzo 		case PACKET_TAG_IPFORWARD:
1962b25acc1SLuigi Rizzo 			args.next_hop = (struct sockaddr_in *)m0->m_data;
1972b25acc1SLuigi Rizzo 			break;
1982b25acc1SLuigi Rizzo 		}
1992b25acc1SLuigi Rizzo 	}
2002b25acc1SLuigi Rizzo 	m = m0;
2012b25acc1SLuigi Rizzo 
202fe584538SDag-Erling Smørgrav 	M_ASSERTPKTHDR(m);
203b9234fafSSam Leffler #ifndef FAST_IPSEC
204b9234fafSSam Leffler 	KASSERT(ro != NULL, ("ip_output: no route, proto %d",
205b9234fafSSam Leffler 	    mtod(m, struct ip *)->ip_p));
206b9234fafSSam Leffler #endif
2072b25acc1SLuigi Rizzo 
2082b25acc1SLuigi Rizzo 	if (args.rule != NULL) {	/* dummynet already saw us */
209b715f178SLuigi Rizzo 		ip = mtod(m, struct ip *);
21053be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2 ;
2113956b023SLuigi Rizzo 		if (ro->ro_rt)
2129a10980eSJonathan Lemon 			ia = ifatoia(ro->ro_rt->rt_ifa);
213b715f178SLuigi Rizzo 		goto sendit;
2142b25acc1SLuigi Rizzo 	}
215db40007dSAndrew R. Reiter 
216df8bae1dSRodney W. Grimes 	if (opt) {
217cb7641e8SMaxim Konovalov 		len = 0;
218df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
219cb7641e8SMaxim Konovalov 		if (len != 0)
220df8bae1dSRodney W. Grimes 			hlen = len;
221df8bae1dSRodney W. Grimes 	}
222df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
2232b25acc1SLuigi Rizzo 	pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
2243efc3014SJulian Elischer 
225df8bae1dSRodney W. Grimes 	/*
226df8bae1dSRodney W. Grimes 	 * Fill in IP header.
227df8bae1dSRodney W. Grimes 	 */
228df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
22953be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
23053be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
2312c56e246SMatthew N. Dodd 		ip->ip_off &= IP_DF;
23264dddc18SKris Kennaway #ifdef RANDOM_IP_ID
23364dddc18SKris Kennaway 		ip->ip_id = ip_randomid();
23464dddc18SKris Kennaway #else
235e30177e0SRuslan Ermilov 		ip->ip_id = htons(ip_id++);
23664dddc18SKris Kennaway #endif
237df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
238df8bae1dSRodney W. Grimes 	} else {
23953be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
240df8bae1dSRodney W. Grimes 	}
2419c9137eaSGarrett Wollman 
242b9234fafSSam Leffler #ifdef FAST_IPSEC
243b9234fafSSam Leffler 	if (ro == NULL) {
244b9234fafSSam Leffler 		ro = &iproute;
245b9234fafSSam Leffler 		bzero(ro, sizeof (*ro));
246b9234fafSSam Leffler 	}
247b9234fafSSam Leffler #endif /* FAST_IPSEC */
248df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
249df8bae1dSRodney W. Grimes 	/*
250df8bae1dSRodney W. Grimes 	 * If there is a cached route,
251df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
252df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
253a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
254a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
255df8bae1dSRodney W. Grimes 	 */
256df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
257a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
2583efc3014SJulian Elischer 			  dst->sin_addr.s_addr != pkt_dst.s_addr)) {
259df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
260df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
261df8bae1dSRodney W. Grimes 	}
262df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
263a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
264df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
265df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2663efc3014SJulian Elischer 		dst->sin_addr = pkt_dst;
267df8bae1dSRodney W. Grimes 	}
268df8bae1dSRodney W. Grimes 	/*
269df8bae1dSRodney W. Grimes 	 * If routing to interface only,
270df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
271df8bae1dSRodney W. Grimes 	 */
272df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
273df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
274df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
275df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
276df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
277df8bae1dSRodney W. Grimes 			goto bad;
278df8bae1dSRodney W. Grimes 		}
279df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
280df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2819f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2823afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
28338c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2843afefa39SDaniel C. Sobral 		/*
28538c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
28638c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2873afefa39SDaniel C. Sobral 		 */
28838c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
28938c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
29038c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
291df8bae1dSRodney W. Grimes 	} else {
2922c17fe93SGarrett Wollman 		/*
293f2c2962eSRuslan Ermilov 		 * If this is the case, we probably don't want to allocate
294f2c2962eSRuslan Ermilov 		 * a protocol-cloned route since we didn't get one from the
295f2c2962eSRuslan Ermilov 		 * ULP.  This lets TCP do its thing, while not burdening
296f2c2962eSRuslan Ermilov 		 * forwarding or ICMP with the overhead of cloning a route.
297f2c2962eSRuslan Ermilov 		 * Of course, we still want to do any cloning requested by
298f2c2962eSRuslan Ermilov 		 * the link layer, as this is probably required in all cases
299f2c2962eSRuslan Ermilov 		 * for correct operation (as it is for ARP).
3002c17fe93SGarrett Wollman 		 */
301df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
3022c17fe93SGarrett Wollman 			rtalloc_ign(ro, RTF_PRCLONING);
303df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
304df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
305df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
306df8bae1dSRodney W. Grimes 			goto bad;
307df8bae1dSRodney W. Grimes 		}
308df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
309df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
310df8bae1dSRodney W. Grimes 		ro->ro_rt->rt_use++;
311df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
312f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
3139f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
314f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
3159f9b3dc4SGarrett Wollman 		else
3169f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
317df8bae1dSRodney W. Grimes 	}
3183efc3014SJulian Elischer 	if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
319df8bae1dSRodney W. Grimes 		struct in_multi *inm;
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
322df8bae1dSRodney W. Grimes 		/*
323df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
324df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
325df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
326df8bae1dSRodney W. Grimes 		 */
327df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
328df8bae1dSRodney W. Grimes 		/*
329df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
330df8bae1dSRodney W. Grimes 		 */
331df8bae1dSRodney W. Grimes 		if (imo != NULL) {
332df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
3331c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3341c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
335bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
336bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
337bbb4330bSLuigi Rizzo 				    INADDR_ANY;
338df8bae1dSRodney W. Grimes 		} else
339df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
340df8bae1dSRodney W. Grimes 		/*
341df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
342df8bae1dSRodney W. Grimes 		 */
3431c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
344df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
345df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
346df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
347df8bae1dSRodney W. Grimes 				goto bad;
348df8bae1dSRodney W. Grimes 			}
3491c5de19aSGarrett Wollman 		}
350df8bae1dSRodney W. Grimes 		/*
351df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
352df8bae1dSRodney W. Grimes 		 * of outgoing interface.
353df8bae1dSRodney W. Grimes 		 */
354df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
35538c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
35638c1bc35SRuslan Ermilov 			if (ia != NULL)
35738c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
358df8bae1dSRodney W. Grimes 		}
359df8bae1dSRodney W. Grimes 
36092bdb2faSBill Fenner 		if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
36192bdb2faSBill Fenner 			/*
36292bdb2faSBill Fenner 			 * XXX
36392bdb2faSBill Fenner 			 * delayed checksums are not currently
36492bdb2faSBill Fenner 			 * compatible with IP multicast routing
36592bdb2faSBill Fenner 			 */
36692bdb2faSBill Fenner 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
36792bdb2faSBill Fenner 				in_delayed_cksum(m);
36892bdb2faSBill Fenner 				m->m_pkthdr.csum_flags &=
36992bdb2faSBill Fenner 					~CSUM_DELAY_DATA;
37092bdb2faSBill Fenner 			}
37192bdb2faSBill Fenner 		}
3723efc3014SJulian Elischer 		IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
373df8bae1dSRodney W. Grimes 		if (inm != NULL &&
374df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
375df8bae1dSRodney W. Grimes 			/*
376df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
377df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
378df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
379df8bae1dSRodney W. Grimes 			 */
38086b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
381df8bae1dSRodney W. Grimes 		}
382df8bae1dSRodney W. Grimes 		else {
383df8bae1dSRodney W. Grimes 			/*
384df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
385df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
386df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
387df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
388df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
389df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
390df8bae1dSRodney W. Grimes 			 *
391df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
392df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
393df8bae1dSRodney W. Grimes 			 * if necessary.
394df8bae1dSRodney W. Grimes 			 */
395df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
396f0068c4aSGarrett Wollman 				/*
397bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
398f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
399f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
400f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
401f0068c4aSGarrett Wollman 				 */
402b124e4f2SGarrett Wollman 				if (!rsvp_on)
403f0068c4aSGarrett Wollman 					imo = NULL;
404bbb4330bSLuigi Rizzo 				if (ip_mforward &&
405bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
406df8bae1dSRodney W. Grimes 					m_freem(m);
407df8bae1dSRodney W. Grimes 					goto done;
408df8bae1dSRodney W. Grimes 				}
409df8bae1dSRodney W. Grimes 			}
410df8bae1dSRodney W. Grimes 		}
4115e9ae478SGarrett Wollman 
412df8bae1dSRodney W. Grimes 		/*
413df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
414df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
415df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
416df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
417df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
418df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
419df8bae1dSRodney W. Grimes 		 */
420f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
421df8bae1dSRodney W. Grimes 			m_freem(m);
422df8bae1dSRodney W. Grimes 			goto done;
423df8bae1dSRodney W. Grimes 		}
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes 		goto sendit;
426df8bae1dSRodney W. Grimes 	}
427df8bae1dSRodney W. Grimes #ifndef notdef
428df8bae1dSRodney W. Grimes 	/*
4292b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
4302b25acc1SLuigi Rizzo 	 * of the outoing interface. In case, keep note we did that, so
4312b25acc1SLuigi Rizzo 	 * if the the firewall changes the next-hop causing the output
4322b25acc1SLuigi Rizzo 	 * interface to change, we can fix that.
433df8bae1dSRodney W. Grimes 	 */
434f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
43538c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
43638c1bc35SRuslan Ermilov 		if (ia != NULL) {
437df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
4382b25acc1SLuigi Rizzo 			src_was_INADDR_ANY = 1;
439f9e354dfSJulian Elischer 		}
44038c1bc35SRuslan Ermilov 	}
441f9e354dfSJulian Elischer #endif /* notdef */
442df8bae1dSRodney W. Grimes 	/*
443b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
444b5390296SDavid Greenman 	 *      the packet or packet fragments
445b5390296SDavid Greenman 	 */
446b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
447b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
448b5390296SDavid Greenman 			error = ENOBUFS;
44935609d45SJonathan Lemon 			ipstat.ips_odropped++;
450b5390296SDavid Greenman 			goto bad;
451b5390296SDavid Greenman 	}
452b5390296SDavid Greenman 
453b5390296SDavid Greenman 	/*
454df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4558c3f5566SRuslan Ermilov 	 * verify user is allowed to send
456df8bae1dSRodney W. Grimes 	 * such a packet.
457df8bae1dSRodney W. Grimes 	 */
4589f9b3dc4SGarrett Wollman 	if (isbroadcast) {
459df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
460df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
461df8bae1dSRodney W. Grimes 			goto bad;
462df8bae1dSRodney W. Grimes 		}
463df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
464df8bae1dSRodney W. Grimes 			error = EACCES;
465df8bae1dSRodney W. Grimes 			goto bad;
466df8bae1dSRodney W. Grimes 		}
467df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
468b375c9ecSLuigi Rizzo 		if ((u_short)ip->ip_len > ifp->if_mtu) {
469df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
470df8bae1dSRodney W. Grimes 			goto bad;
471df8bae1dSRodney W. Grimes 		}
472df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4739f9b3dc4SGarrett Wollman 	} else {
474df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4759f9b3dc4SGarrett Wollman 	}
476df8bae1dSRodney W. Grimes 
477f1743588SDarren Reed sendit:
47833841545SHajimu UMEMOTO #ifdef IPSEC
47933841545SHajimu UMEMOTO 	/* get SP for this packet */
480340c35deSJonathan Lemon 	if (inp == NULL)
48133841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
48233841545SHajimu UMEMOTO 	else
483340c35deSJonathan Lemon 		sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
48433841545SHajimu UMEMOTO 
48533841545SHajimu UMEMOTO 	if (sp == NULL) {
48633841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
48733841545SHajimu UMEMOTO 		goto bad;
48833841545SHajimu UMEMOTO 	}
48933841545SHajimu UMEMOTO 
49033841545SHajimu UMEMOTO 	error = 0;
49133841545SHajimu UMEMOTO 
49233841545SHajimu UMEMOTO 	/* check policy */
49333841545SHajimu UMEMOTO 	switch (sp->policy) {
49433841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
49533841545SHajimu UMEMOTO 		/*
49633841545SHajimu UMEMOTO 		 * This packet is just discarded.
49733841545SHajimu UMEMOTO 		 */
49833841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
49933841545SHajimu UMEMOTO 		goto bad;
50033841545SHajimu UMEMOTO 
50133841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
50233841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
50333841545SHajimu UMEMOTO 		/* no need to do IPsec. */
50433841545SHajimu UMEMOTO 		goto skip_ipsec;
50533841545SHajimu UMEMOTO 
50633841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
50733841545SHajimu UMEMOTO 		if (sp->req == NULL) {
50833841545SHajimu UMEMOTO 			/* acquire a policy */
50933841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
51033841545SHajimu UMEMOTO 			goto bad;
51133841545SHajimu UMEMOTO 		}
51233841545SHajimu UMEMOTO 		break;
51333841545SHajimu UMEMOTO 
51433841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
51533841545SHajimu UMEMOTO 	default:
51633841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
51733841545SHajimu UMEMOTO 	}
51833841545SHajimu UMEMOTO     {
51933841545SHajimu UMEMOTO 	struct ipsec_output_state state;
52033841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
52133841545SHajimu UMEMOTO 	state.m = m;
52233841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
52333841545SHajimu UMEMOTO 		state.ro = &iproute;
52433841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
52533841545SHajimu UMEMOTO 	} else
52633841545SHajimu UMEMOTO 		state.ro = ro;
52733841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
52833841545SHajimu UMEMOTO 
52933841545SHajimu UMEMOTO 	ip->ip_sum = 0;
53033841545SHajimu UMEMOTO 
53133841545SHajimu UMEMOTO 	/*
53233841545SHajimu UMEMOTO 	 * XXX
53333841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
53433841545SHajimu UMEMOTO 	 */
53533841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
53633841545SHajimu UMEMOTO 		in_delayed_cksum(m);
53733841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
53833841545SHajimu UMEMOTO 	}
53933841545SHajimu UMEMOTO 
540fd8e4ebcSMike Barcroft 	ip->ip_len = htons(ip->ip_len);
541fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
54233841545SHajimu UMEMOTO 
54333841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
54433841545SHajimu UMEMOTO 
54533841545SHajimu UMEMOTO 	m = state.m;
54633841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
54733841545SHajimu UMEMOTO 		/*
54833841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
54933841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
55033841545SHajimu UMEMOTO 		 */
55133841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
55233841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
55333841545SHajimu UMEMOTO 			ro = state.ro;
55433841545SHajimu UMEMOTO 		}
55533841545SHajimu UMEMOTO 	} else
55633841545SHajimu UMEMOTO 		ro = state.ro;
55733841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
55833841545SHajimu UMEMOTO 	if (error) {
55933841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
56033841545SHajimu UMEMOTO 		m0 = NULL;
56133841545SHajimu UMEMOTO 		switch (error) {
56233841545SHajimu UMEMOTO 		case EHOSTUNREACH:
56333841545SHajimu UMEMOTO 		case ENETUNREACH:
56433841545SHajimu UMEMOTO 		case EMSGSIZE:
56533841545SHajimu UMEMOTO 		case ENOBUFS:
56633841545SHajimu UMEMOTO 		case ENOMEM:
56733841545SHajimu UMEMOTO 			break;
56833841545SHajimu UMEMOTO 		default:
56933841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
57033841545SHajimu UMEMOTO 			/*fall through*/
57133841545SHajimu UMEMOTO 		case ENOENT:
57233841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
57333841545SHajimu UMEMOTO 			error = 0;
57433841545SHajimu UMEMOTO 			break;
57533841545SHajimu UMEMOTO 		}
57633841545SHajimu UMEMOTO 		goto bad;
57733841545SHajimu UMEMOTO 	}
57833841545SHajimu UMEMOTO     }
57933841545SHajimu UMEMOTO 
58033841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
58133841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
58233841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
58333841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
58433841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
58533841545SHajimu UMEMOTO 			printf("ip_output: "
58633841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
58733841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
58833841545SHajimu UMEMOTO 			goto bad;
58933841545SHajimu UMEMOTO 		}
59033841545SHajimu UMEMOTO 	} else {
59133841545SHajimu UMEMOTO 		ia = ifatoia(ro->ro_rt->rt_ifa);
59233841545SHajimu UMEMOTO 		ifp = ro->ro_rt->rt_ifp;
59333841545SHajimu UMEMOTO 	}
59433841545SHajimu UMEMOTO 
59533841545SHajimu UMEMOTO 	/* make it flipped, again. */
596fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
597fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
59833841545SHajimu UMEMOTO skip_ipsec:
59933841545SHajimu UMEMOTO #endif /*IPSEC*/
600b9234fafSSam Leffler #ifdef FAST_IPSEC
601b9234fafSSam Leffler 	/*
602b9234fafSSam Leffler 	 * Check the security policy (SP) for the packet and, if
603b9234fafSSam Leffler 	 * required, do IPsec-related processing.  There are two
604b9234fafSSam Leffler 	 * cases here; the first time a packet is sent through
605b9234fafSSam Leffler 	 * it will be untagged and handled by ipsec4_checkpolicy.
606b9234fafSSam Leffler 	 * If the packet is resubmitted to ip_output (e.g. after
607b9234fafSSam Leffler 	 * AH, ESP, etc. processing), there will be a tag to bypass
608b9234fafSSam Leffler 	 * the lookup and related policy checking.
609b9234fafSSam Leffler 	 */
610b9234fafSSam Leffler 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
611b9234fafSSam Leffler 	s = splnet();
612b9234fafSSam Leffler 	if (mtag != NULL) {
613b9234fafSSam Leffler 		tdbi = (struct tdb_ident *)(mtag + 1);
614b9234fafSSam Leffler 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
615b9234fafSSam Leffler 		if (sp == NULL)
616b9234fafSSam Leffler 			error = -EINVAL;	/* force silent drop */
617b9234fafSSam Leffler 		m_tag_delete(m, mtag);
618b9234fafSSam Leffler 	} else {
619b9234fafSSam Leffler 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
620b9234fafSSam Leffler 					&error, inp);
621b9234fafSSam Leffler 	}
622b9234fafSSam Leffler 	/*
623b9234fafSSam Leffler 	 * There are four return cases:
624b9234fafSSam Leffler 	 *    sp != NULL	 	    apply IPsec policy
625b9234fafSSam Leffler 	 *    sp == NULL, error == 0	    no IPsec handling needed
626b9234fafSSam Leffler 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
627b9234fafSSam Leffler 	 *    sp == NULL, error != 0	    discard packet, report error
628b9234fafSSam Leffler 	 */
629b9234fafSSam Leffler 	if (sp != NULL) {
630b9234fafSSam Leffler 		/* Loop detection, check if ipsec processing already done */
631b9234fafSSam Leffler 		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
632b9234fafSSam Leffler 		for (mtag = m_tag_first(m); mtag != NULL;
633b9234fafSSam Leffler 		     mtag = m_tag_next(m, mtag)) {
634b9234fafSSam Leffler 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
635b9234fafSSam Leffler 				continue;
636b9234fafSSam Leffler 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
637b9234fafSSam Leffler 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
638b9234fafSSam Leffler 				continue;
639b9234fafSSam Leffler 			/*
640b9234fafSSam Leffler 			 * Check if policy has an SA associated with it.
641b9234fafSSam Leffler 			 * This can happen when an SP has yet to acquire
642b9234fafSSam Leffler 			 * an SA; e.g. on first reference.  If it occurs,
643b9234fafSSam Leffler 			 * then we let ipsec4_process_packet do its thing.
644b9234fafSSam Leffler 			 */
645b9234fafSSam Leffler 			if (sp->req->sav == NULL)
646b9234fafSSam Leffler 				break;
647b9234fafSSam Leffler 			tdbi = (struct tdb_ident *)(mtag + 1);
648b9234fafSSam Leffler 			if (tdbi->spi == sp->req->sav->spi &&
649b9234fafSSam Leffler 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
650ab94ca3cSSam Leffler 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
651b9234fafSSam Leffler 				 sizeof (union sockaddr_union)) == 0) {
652b9234fafSSam Leffler 				/*
653b9234fafSSam Leffler 				 * No IPsec processing is needed, free
654b9234fafSSam Leffler 				 * reference to SP.
655b9234fafSSam Leffler 				 *
656b9234fafSSam Leffler 				 * NB: null pointer to avoid free at
657b9234fafSSam Leffler 				 *     done: below.
658b9234fafSSam Leffler 				 */
659b9234fafSSam Leffler 				KEY_FREESP(&sp), sp = NULL;
660b9234fafSSam Leffler 				splx(s);
661b9234fafSSam Leffler 				goto spd_done;
662b9234fafSSam Leffler 			}
663b9234fafSSam Leffler 		}
664b9234fafSSam Leffler 
665b9234fafSSam Leffler 		/*
666b9234fafSSam Leffler 		 * Do delayed checksums now because we send before
667b9234fafSSam Leffler 		 * this is done in the normal processing path.
668b9234fafSSam Leffler 		 */
669b9234fafSSam Leffler 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
670b9234fafSSam Leffler 			in_delayed_cksum(m);
671b9234fafSSam Leffler 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
672b9234fafSSam Leffler 		}
673b9234fafSSam Leffler 
674b9234fafSSam Leffler 		ip->ip_len = htons(ip->ip_len);
675b9234fafSSam Leffler 		ip->ip_off = htons(ip->ip_off);
676b9234fafSSam Leffler 
677b9234fafSSam Leffler 		/* NB: callee frees mbuf */
678b9234fafSSam Leffler 		error = ipsec4_process_packet(m, sp->req, flags, 0);
6799359ad86SSam Leffler 		/*
6809359ad86SSam Leffler 		 * Preserve KAME behaviour: ENOENT can be returned
6819359ad86SSam Leffler 		 * when an SA acquire is in progress.  Don't propagate
6829359ad86SSam Leffler 		 * this to user-level; it confuses applications.
6839359ad86SSam Leffler 		 *
6849359ad86SSam Leffler 		 * XXX this will go away when the SADB is redone.
6859359ad86SSam Leffler 		 */
6869359ad86SSam Leffler 		if (error == ENOENT)
6879359ad86SSam Leffler 			error = 0;
688b9234fafSSam Leffler 		splx(s);
689b9234fafSSam Leffler 		goto done;
690b9234fafSSam Leffler 	} else {
691b9234fafSSam Leffler 		splx(s);
692b9234fafSSam Leffler 
693b9234fafSSam Leffler 		if (error != 0) {
694b9234fafSSam Leffler 			/*
695b9234fafSSam Leffler 			 * Hack: -EINVAL is used to signal that a packet
696b9234fafSSam Leffler 			 * should be silently discarded.  This is typically
697b9234fafSSam Leffler 			 * because we asked key management for an SA and
698b9234fafSSam Leffler 			 * it was delayed (e.g. kicked up to IKE).
699b9234fafSSam Leffler 			 */
700b9234fafSSam Leffler 			if (error == -EINVAL)
701b9234fafSSam Leffler 				error = 0;
702b9234fafSSam Leffler 			goto bad;
703b9234fafSSam Leffler 		} else {
704b9234fafSSam Leffler 			/* No IPsec processing for this packet. */
705b9234fafSSam Leffler 		}
706b9234fafSSam Leffler #ifdef notyet
707b9234fafSSam Leffler 		/*
708b9234fafSSam Leffler 		 * If deferred crypto processing is needed, check that
709b9234fafSSam Leffler 		 * the interface supports it.
710b9234fafSSam Leffler 		 */
711b9234fafSSam Leffler 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
712b9234fafSSam Leffler 		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
713b9234fafSSam Leffler 			/* notify IPsec to do its own crypto */
714b9234fafSSam Leffler 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
715b9234fafSSam Leffler 			error = EHOSTUNREACH;
716b9234fafSSam Leffler 			goto bad;
717b9234fafSSam Leffler 		}
718b9234fafSSam Leffler #endif
719b9234fafSSam Leffler 	}
720b9234fafSSam Leffler spd_done:
721b9234fafSSam Leffler #endif /* FAST_IPSEC */
72233841545SHajimu UMEMOTO 
723fed1c7e9SSøren Schmidt 	/*
724fed1c7e9SSøren Schmidt 	 * IpHack's section.
725fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
726e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
727fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
728fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
729fed1c7e9SSøren Schmidt 	 */
730c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
731c4ac87eaSDarren Reed 	/*
732c4ac87eaSDarren Reed 	 * Run through list of hooks for output packets.
733c4ac87eaSDarren Reed 	 */
734c4ac87eaSDarren Reed 	m1 = m;
735c4ac87eaSDarren Reed 	pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
736fc2ffbe6SPoul-Henning Kamp 	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
737c4ac87eaSDarren Reed 		if (pfh->pfil_func) {
738c4ac87eaSDarren Reed 			rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
739c4ac87eaSDarren Reed 			if (rv) {
740c4ac87eaSDarren Reed 				error = EHOSTUNREACH;
7411ee25934SPeter Wemm 				goto done;
7421ee25934SPeter Wemm 			}
743c4ac87eaSDarren Reed 			m = m1;
744c4ac87eaSDarren Reed 			if (m == NULL)
745c4ac87eaSDarren Reed 				goto done;
746c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
747c4ac87eaSDarren Reed 		}
748c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
749fed1c7e9SSøren Schmidt 
750df8bae1dSRodney W. Grimes 	/*
751e7319babSPoul-Henning Kamp 	 * Check with the firewall...
7523efc3014SJulian Elischer 	 * but not if we are already being fwd'd from a firewall.
753e7319babSPoul-Henning Kamp 	 */
7542b25acc1SLuigi Rizzo 	if (fw_enable && IPFW_LOADED && !args.next_hop) {
7559de9737fSPeter Wemm 		struct sockaddr_in *old = dst;
756b715f178SLuigi Rizzo 
7572b25acc1SLuigi Rizzo 		args.m = m;
7582b25acc1SLuigi Rizzo 		args.next_hop = dst;
7592b25acc1SLuigi Rizzo 		args.oif = ifp;
7602b25acc1SLuigi Rizzo 		off = ip_fw_chk_ptr(&args);
7612b25acc1SLuigi Rizzo 		m = args.m;
7622b25acc1SLuigi Rizzo 		dst = args.next_hop;
7632b25acc1SLuigi Rizzo 
764b715f178SLuigi Rizzo                 /*
765b715f178SLuigi Rizzo 		 * On return we must do the following:
766507b4b54SLuigi Rizzo 		 * m == NULL	-> drop the pkt (old interface, deprecated)
767aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
768b715f178SLuigi Rizzo 		 * 1<=off<= 0xffff		-> DIVERT
769aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
770aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
771b715f178SLuigi Rizzo 		 * dst != old			-> IPFIREWALL_FORWARD
772b715f178SLuigi Rizzo 		 * off==0, dst==old		-> accept
773aa1f5daaSJonathan Lemon 		 * If some of the above modules are not compiled in, then
774b715f178SLuigi Rizzo 		 * we should't have to check the corresponding condition
775b715f178SLuigi Rizzo 		 * (because the ipfw control socket should not accept
776b715f178SLuigi Rizzo 		 * unsupported rules), but better play safe and drop
777b715f178SLuigi Rizzo 		 * packets in case of doubt.
778b715f178SLuigi Rizzo 		 */
779d60315beSLuigi Rizzo 		if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
780507b4b54SLuigi Rizzo 			if (m)
781507b4b54SLuigi Rizzo 				m_freem(m);
782507b4b54SLuigi Rizzo 			error = EACCES;
783507b4b54SLuigi Rizzo 			goto done;
784507b4b54SLuigi Rizzo 		}
785d60315beSLuigi Rizzo 		ip = mtod(m, struct ip *);
786b715f178SLuigi Rizzo 		if (off == 0 && dst == old)		/* common case */
787b715f178SLuigi Rizzo 			goto pass;
7887b109fa4SLuigi Rizzo                 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
789b715f178SLuigi Rizzo 			/*
790b715f178SLuigi Rizzo 			 * pass the pkt to dummynet. Need to include
791f0a53591SLuigi Rizzo 			 * pipe number, m, ifp, ro, dst because these are
792b715f178SLuigi Rizzo 			 * not recomputed in the next pass.
793b715f178SLuigi Rizzo 			 * All other parameters have been already used and
794b715f178SLuigi Rizzo 			 * so they are not needed anymore.
795b715f178SLuigi Rizzo 			 * XXX note: if the ifp or ro entry are deleted
796b715f178SLuigi Rizzo 			 * while a pkt is in dummynet, we are in trouble!
797b715f178SLuigi Rizzo 			 */
7982b25acc1SLuigi Rizzo 			args.ro = ro;
7992b25acc1SLuigi Rizzo 			args.dst = dst;
8002b25acc1SLuigi Rizzo 			args.flags = flags;
8012b25acc1SLuigi Rizzo 
8022b25acc1SLuigi Rizzo 			error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT,
8032b25acc1SLuigi Rizzo 				&args);
804b715f178SLuigi Rizzo 			goto done;
805b715f178SLuigi Rizzo 		}
806b715f178SLuigi Rizzo #ifdef IPDIVERT
8078948e4baSArchie Cobbs 		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
8088948e4baSArchie Cobbs 			struct mbuf *clone = NULL;
8098948e4baSArchie Cobbs 
8108948e4baSArchie Cobbs 			/* Clone packet if we're doing a 'tee' */
8118948e4baSArchie Cobbs 			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
812a163d034SWarner Losh 				clone = m_dup(m, M_DONTWAIT);
8138948e4baSArchie Cobbs 
814ea53ecd9SJonathan Lemon 			/*
815ea53ecd9SJonathan Lemon 			 * XXX
816ea53ecd9SJonathan Lemon 			 * delayed checksums are not currently compatible
817ea53ecd9SJonathan Lemon 			 * with divert sockets.
818ea53ecd9SJonathan Lemon 			 */
819ea53ecd9SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
820ea53ecd9SJonathan Lemon 				in_delayed_cksum(m);
821ea53ecd9SJonathan Lemon 				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
822ea53ecd9SJonathan Lemon 			}
823ea53ecd9SJonathan Lemon 
8248948e4baSArchie Cobbs 			/* Restore packet header fields to original values */
825fd8e4ebcSMike Barcroft 			ip->ip_len = htons(ip->ip_len);
826fd8e4ebcSMike Barcroft 			ip->ip_off = htons(ip->ip_off);
8278948e4baSArchie Cobbs 
8288948e4baSArchie Cobbs 			/* Deliver packet to divert input routine */
8292b25acc1SLuigi Rizzo 			divert_packet(m, 0, off & 0xffff, args.divert_rule);
8308948e4baSArchie Cobbs 
8318948e4baSArchie Cobbs 			/* If 'tee', continue with original packet */
8328948e4baSArchie Cobbs 			if (clone != NULL) {
8338948e4baSArchie Cobbs 				m = clone;
8348948e4baSArchie Cobbs 				ip = mtod(m, struct ip *);
8358948e4baSArchie Cobbs 				goto pass;
8368948e4baSArchie Cobbs 			}
837b715f178SLuigi Rizzo 			goto done;
838b715f178SLuigi Rizzo 		}
839b715f178SLuigi Rizzo #endif
840b715f178SLuigi Rizzo 
8412b25acc1SLuigi Rizzo 		/* IPFIREWALL_FORWARD */
8422b25acc1SLuigi Rizzo 		/*
8432b25acc1SLuigi Rizzo 		 * Check dst to make sure it is directly reachable on the
844f9e354dfSJulian Elischer 		 * interface we previously thought it was.
845f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
846f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
847f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
848f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
849f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
850f9e354dfSJulian Elischer 		 * And I'm babbling.
851f9e354dfSJulian Elischer 		 */
8522b25acc1SLuigi Rizzo 		if (off == 0 && old != dst) { /* FORWARD, dst has changed */
8532b25acc1SLuigi Rizzo #if 0
8542b25acc1SLuigi Rizzo 			/*
8552b25acc1SLuigi Rizzo 			 * XXX To improve readability, this block should be
8562b25acc1SLuigi Rizzo 			 * changed into a function call as below:
8572b25acc1SLuigi Rizzo 			 */
8582b25acc1SLuigi Rizzo 			error = ip_ipforward(&m, &dst, &ifp);
8592b25acc1SLuigi Rizzo 			if (error)
8602b25acc1SLuigi Rizzo 				goto bad;
8612b25acc1SLuigi Rizzo 			if (m == NULL) /* ip_input consumed the mbuf */
8622b25acc1SLuigi Rizzo 				goto done;
8632b25acc1SLuigi Rizzo #else
864f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
865f9e354dfSJulian Elischer 
8662b25acc1SLuigi Rizzo 			/*
8672b25acc1SLuigi Rizzo 			 * XXX sro_fwd below is static, and a pointer
8682b25acc1SLuigi Rizzo 			 * to it gets passed to routines downstream.
8692b25acc1SLuigi Rizzo 			 * This could have surprisingly bad results in
8702b25acc1SLuigi Rizzo 			 * practice, because its content is overwritten
8712b25acc1SLuigi Rizzo 			 * by subsequent packets.
8722b25acc1SLuigi Rizzo 			 */
873f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
8742b25acc1SLuigi Rizzo 			static struct route sro_fwd;
8752b25acc1SLuigi Rizzo 			struct route *ro_fwd = &sro_fwd;
8762b25acc1SLuigi Rizzo 
8772b25acc1SLuigi Rizzo #if 0
8782b25acc1SLuigi Rizzo 			print_ip("IPFIREWALL_FORWARD: New dst ip: ",
8792b25acc1SLuigi Rizzo 			    dst->sin_addr, "\n");
880f9e354dfSJulian Elischer #endif
8812b25acc1SLuigi Rizzo 
882f9e354dfSJulian Elischer 			/*
883f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
8842b25acc1SLuigi Rizzo 			 * to a local socket. If so, then we should somehow
885f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
886f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
887f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
888f9e354dfSJulian Elischer 			 * you want to forward until they are being output
889f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
890f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
891f9e354dfSJulian Elischer 			 * we would not be able to control what happens
892f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
8939d5abbddSJens Schweikhardt 			 * it is done through an ISR.
894f9e354dfSJulian Elischer 			 */
895ca925d9cSJonathan Lemon 			LIST_FOREACH(ia,
896ca925d9cSJonathan Lemon 			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
897f9e354dfSJulian Elischer 				/*
898f9e354dfSJulian Elischer 				 * If the addr to forward to is one
899f9e354dfSJulian Elischer 				 * of ours, we pretend to
900f9e354dfSJulian Elischer 				 * be the destination for this packet.
901f9e354dfSJulian Elischer 				 */
902f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
903f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
904f9e354dfSJulian Elischer 					break;
905f9e354dfSJulian Elischer 			}
9062b25acc1SLuigi Rizzo 			if (ia) {	/* tell ip_input "dont filter" */
9072b25acc1SLuigi Rizzo 				struct m_hdr tag;
9082b25acc1SLuigi Rizzo 
9092b25acc1SLuigi Rizzo 				tag.mh_type = MT_TAG;
9102b25acc1SLuigi Rizzo 				tag.mh_flags = PACKET_TAG_IPFORWARD;
9112b25acc1SLuigi Rizzo 				tag.mh_data = (caddr_t)args.next_hop;
9122b25acc1SLuigi Rizzo 				tag.mh_next = m;
9132b25acc1SLuigi Rizzo 
914f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
915f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
91620c822f3SJonathan Lemon 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
91720c822f3SJonathan Lemon 					m->m_pkthdr.csum_flags |=
91820c822f3SJonathan Lemon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
91920c822f3SJonathan Lemon 					m0->m_pkthdr.csum_data = 0xffff;
92020c822f3SJonathan Lemon 				}
92120c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
92220c822f3SJonathan Lemon 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
923fd8e4ebcSMike Barcroft 				ip->ip_len = htons(ip->ip_len);
924fd8e4ebcSMike Barcroft 				ip->ip_off = htons(ip->ip_off);
9252b25acc1SLuigi Rizzo 				ip_input((struct mbuf *)&tag);
926f9e354dfSJulian Elischer 				goto done;
927f9e354dfSJulian Elischer 			}
928f9e354dfSJulian Elischer 			/* Some of the logic for this was
929f9e354dfSJulian Elischer 			 * nicked from above.
930f9e354dfSJulian Elischer 			 *
931f9e354dfSJulian Elischer 			 * This rewrites the cached route in a local PCB.
932f9e354dfSJulian Elischer 			 * Is this what we want to do?
933f9e354dfSJulian Elischer 			 */
934f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
935f9e354dfSJulian Elischer 
936f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
937f9e354dfSJulian Elischer 			rtalloc_ign(ro_fwd, RTF_PRCLONING);
938f9e354dfSJulian Elischer 
939f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt == 0) {
940f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
941f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
942f9e354dfSJulian Elischer 				goto bad;
943f9e354dfSJulian Elischer 			}
944f9e354dfSJulian Elischer 
945f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
946f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
947f9e354dfSJulian Elischer 			ro_fwd->ro_rt->rt_use++;
948f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
9492b25acc1SLuigi Rizzo 				dst = (struct sockaddr_in *)
9502b25acc1SLuigi Rizzo 					ro_fwd->ro_rt->rt_gateway;
951f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
952f9e354dfSJulian Elischer 				isbroadcast =
953f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
954f9e354dfSJulian Elischer 			else
955f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
9563f9e3122SYaroslav Tykhiy 			if (ro->ro_rt)
957f9e354dfSJulian Elischer 				RTFREE(ro->ro_rt);
958f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
959f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
960f9e354dfSJulian Elischer 
9612b25acc1SLuigi Rizzo #endif	/* ... block to be put into a function */
962f9e354dfSJulian Elischer 			/*
963f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
964f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
965f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
966f9e354dfSJulian Elischer 			 */
9672b25acc1SLuigi Rizzo 			if (src_was_INADDR_ANY)
968f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
969b715f178SLuigi Rizzo 			goto pass ;
970f9e354dfSJulian Elischer 		}
9712b25acc1SLuigi Rizzo 
972b715f178SLuigi Rizzo                 /*
973b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
974b715f178SLuigi Rizzo                  * we have to drop the pkt
975b715f178SLuigi Rizzo                  */
976b715f178SLuigi Rizzo 		m_freem(m);
977b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
978b715f178SLuigi Rizzo                 goto done;
97993e0e116SJulian Elischer 	}
980e7319babSPoul-Henning Kamp 
981b715f178SLuigi Rizzo pass:
98251c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
98351c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
98451c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
98551c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
98651c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
98751c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
98851c8ec4aSRuslan Ermilov 			goto bad;
98951c8ec4aSRuslan Ermilov 		}
99051c8ec4aSRuslan Ermilov 	}
99151c8ec4aSRuslan Ermilov 
992206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
993206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
994db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
995db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
996db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
997db4f9cc7SJonathan Lemon 	}
998206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
999db4f9cc7SJonathan Lemon 
1000e7319babSPoul-Henning Kamp 	/*
1001db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
1002db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
1003df8bae1dSRodney W. Grimes 	 */
1004b375c9ecSLuigi Rizzo 	if ((u_short)ip->ip_len <= ifp->if_mtu ||
1005b375c9ecSLuigi Rizzo 	    ifp->if_hwassist & CSUM_FRAGMENT) {
1006fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1007fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1008df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
100953be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
1010df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
10115da9f8faSJosef Karthauser 
10125da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
101338c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
10145da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
10155da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
10165da9f8faSJosef Karthauser 		}
10175da9f8faSJosef Karthauser 
101833841545SHajimu UMEMOTO #ifdef IPSEC
101933841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
102033841545SHajimu UMEMOTO 		ipsec_delaux(m);
102133841545SHajimu UMEMOTO #endif
102233841545SHajimu UMEMOTO 
102353dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
1024511e01e2SMaxime Henrion 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) {
10259d9edc56SMike Silbersack 			struct mbuf *m1, *m2;
1026511e01e2SMaxime Henrion 			int length, tmp;
10279d9edc56SMike Silbersack 
1028511e01e2SMaxime Henrion 			tmp = length = m->m_pkthdr.len;
10299d9edc56SMike Silbersack 
1030511e01e2SMaxime Henrion 			while ((length -= mbuf_frag_size) >= 1) {
10319d9edc56SMike Silbersack 				m1 = m_split(m, length, M_DONTWAIT);
1032511e01e2SMaxime Henrion 				if (m1 == NULL)
10339d9edc56SMike Silbersack 					break;
1034511e01e2SMaxime Henrion 				m1->m_flags &= ~M_PKTHDR;
1035511e01e2SMaxime Henrion 				m2 = m;
1036511e01e2SMaxime Henrion 				while (m2->m_next != NULL)
10379d9edc56SMike Silbersack 					m2 = m2->m_next;
10389d9edc56SMike Silbersack 				m2->m_next = m1;
10399d9edc56SMike Silbersack 			}
1040511e01e2SMaxime Henrion 			m->m_pkthdr.len = tmp;
10419d9edc56SMike Silbersack 		}
10429d9edc56SMike Silbersack #endif
1043df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
1044df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
1045df8bae1dSRodney W. Grimes 		goto done;
1046df8bae1dSRodney W. Grimes 	}
1047b375c9ecSLuigi Rizzo 	/*
1048b375c9ecSLuigi Rizzo 	 * Too large for interface; fragment if possible.
1049b375c9ecSLuigi Rizzo 	 * Must be able to put at least 8 bytes per fragment.
1050b375c9ecSLuigi Rizzo 	 */
1051df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
1052df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
10533d1f141bSGarrett Wollman 		/*
10543d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
10553d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
10563d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
10573d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
10583d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
10593d1f141bSGarrett Wollman 		 */
10603d1f141bSGarrett Wollman 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
10613d1f141bSGarrett Wollman 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
10623d1f141bSGarrett Wollman 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
10633d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
10643d1f141bSGarrett Wollman 		}
1065df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
1066df8bae1dSRodney W. Grimes 		goto bad;
1067df8bae1dSRodney W. Grimes 	}
1068b375c9ecSLuigi Rizzo 	len = (ifp->if_mtu - hlen) &~ 7;
1069b375c9ecSLuigi Rizzo 	if (len < 8) {
1070b375c9ecSLuigi Rizzo 		error = EMSGSIZE;
1071df8bae1dSRodney W. Grimes 		goto bad;
1072b375c9ecSLuigi Rizzo 	}
1073b375c9ecSLuigi Rizzo 
1074b375c9ecSLuigi Rizzo 	/*
1075b375c9ecSLuigi Rizzo 	 * if the interface will not calculate checksums on
1076b375c9ecSLuigi Rizzo 	 * fragmented packets, then do it here.
1077b375c9ecSLuigi Rizzo 	 */
1078b375c9ecSLuigi Rizzo 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
1079b375c9ecSLuigi Rizzo 	    (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
1080b375c9ecSLuigi Rizzo 		in_delayed_cksum(m);
1081b375c9ecSLuigi Rizzo 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1082b375c9ecSLuigi Rizzo 	}
1083b375c9ecSLuigi Rizzo 
1084b375c9ecSLuigi Rizzo 	if (len > PAGE_SIZE) {
1085b375c9ecSLuigi Rizzo 		/*
1086b375c9ecSLuigi Rizzo 		 * Fragement large datagrams such that each segment
1087b375c9ecSLuigi Rizzo 		 * contains a multiple of PAGE_SIZE amount of data,
1088b375c9ecSLuigi Rizzo 		 * plus headers. This enables a receiver to perform
1089b375c9ecSLuigi Rizzo 		 * page-flipping zero-copy optimizations.
1090b375c9ecSLuigi Rizzo 		 */
1091b375c9ecSLuigi Rizzo 
1092b375c9ecSLuigi Rizzo 		int newlen;
1093b375c9ecSLuigi Rizzo 		struct mbuf *mtmp;
1094b375c9ecSLuigi Rizzo 
1095b375c9ecSLuigi Rizzo 		for (mtmp = m, off = 0;
1096b375c9ecSLuigi Rizzo 		     mtmp && ((off + mtmp->m_len) <= ifp->if_mtu);
1097b375c9ecSLuigi Rizzo 		     mtmp = mtmp->m_next) {
1098b375c9ecSLuigi Rizzo 			off += mtmp->m_len;
1099b375c9ecSLuigi Rizzo 		}
1100b375c9ecSLuigi Rizzo 		/*
1101b375c9ecSLuigi Rizzo 		 * firstlen (off - hlen) must be aligned on an
1102b375c9ecSLuigi Rizzo 		 * 8-byte boundary
1103b375c9ecSLuigi Rizzo 		 */
1104b375c9ecSLuigi Rizzo 		if (off < hlen)
1105b375c9ecSLuigi Rizzo 			goto smart_frag_failure;
1106b375c9ecSLuigi Rizzo 		off = ((off - hlen) & ~7) + hlen;
1107b375c9ecSLuigi Rizzo 		newlen = (~PAGE_MASK) & ifp->if_mtu;
1108b375c9ecSLuigi Rizzo 		if ((newlen + sizeof (struct ip)) > ifp->if_mtu) {
1109b375c9ecSLuigi Rizzo 			/* we failed, go back the default */
1110b375c9ecSLuigi Rizzo smart_frag_failure:
1111b375c9ecSLuigi Rizzo 			newlen = len;
1112b375c9ecSLuigi Rizzo 			off = hlen + len;
1113b375c9ecSLuigi Rizzo 		}
1114b375c9ecSLuigi Rizzo 
1115b375c9ecSLuigi Rizzo /*		printf("ipfrag: len = %d, hlen = %d, mhlen = %d, newlen = %d, off = %d\n",
1116b375c9ecSLuigi Rizzo 		len, hlen, sizeof (struct ip), newlen, off);*/
1117b375c9ecSLuigi Rizzo 
1118b375c9ecSLuigi Rizzo 		len = newlen;
1119b375c9ecSLuigi Rizzo 
1120b375c9ecSLuigi Rizzo 	} else {
1121b375c9ecSLuigi Rizzo 		off = hlen + len;
1122b375c9ecSLuigi Rizzo 	}
1123b375c9ecSLuigi Rizzo 
1124b375c9ecSLuigi Rizzo 
1125b375c9ecSLuigi Rizzo 
1126b375c9ecSLuigi Rizzo     {
1127b375c9ecSLuigi Rizzo 	int mhlen, firstlen = off - hlen;
1128b375c9ecSLuigi Rizzo 	struct mbuf **mnext = &m->m_nextpkt;
1129b375c9ecSLuigi Rizzo 	int nfrags = 1;
1130b375c9ecSLuigi Rizzo 
1131b375c9ecSLuigi Rizzo 	/*
1132b375c9ecSLuigi Rizzo 	 * Loop through length of segment after first fragment,
1133b375c9ecSLuigi Rizzo 	 * make new header and copy data of each part and link onto chain.
1134b375c9ecSLuigi Rizzo 	 */
1135b375c9ecSLuigi Rizzo 	m0 = m;
1136b375c9ecSLuigi Rizzo 	mhlen = sizeof (struct ip);
1137b375c9ecSLuigi Rizzo 	for (; off < (u_short)ip->ip_len; off += len) {
1138a163d034SWarner Losh 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
1139b375c9ecSLuigi Rizzo 		if (m == 0) {
1140b375c9ecSLuigi Rizzo 			error = ENOBUFS;
1141b375c9ecSLuigi Rizzo 			ipstat.ips_odropped++;
1142b375c9ecSLuigi Rizzo 			goto sendorfree;
1143b375c9ecSLuigi Rizzo 		}
1144b375c9ecSLuigi Rizzo 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
1145b375c9ecSLuigi Rizzo 		m->m_data += max_linkhdr;
1146b375c9ecSLuigi Rizzo 		mhip = mtod(m, struct ip *);
1147b375c9ecSLuigi Rizzo 		*mhip = *ip;
1148b375c9ecSLuigi Rizzo 		if (hlen > sizeof (struct ip)) {
1149b375c9ecSLuigi Rizzo 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1150b375c9ecSLuigi Rizzo 			mhip->ip_v = IPVERSION;
1151b375c9ecSLuigi Rizzo 			mhip->ip_hl = mhlen >> 2;
1152b375c9ecSLuigi Rizzo 		}
1153b375c9ecSLuigi Rizzo 		m->m_len = mhlen;
1154b375c9ecSLuigi Rizzo 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
1155b375c9ecSLuigi Rizzo 		if (off + len >= (u_short)ip->ip_len)
1156b375c9ecSLuigi Rizzo 			len = (u_short)ip->ip_len - off;
1157b375c9ecSLuigi Rizzo 		else
1158b375c9ecSLuigi Rizzo 			mhip->ip_off |= IP_MF;
1159b375c9ecSLuigi Rizzo 		mhip->ip_len = htons((u_short)(len + mhlen));
1160b375c9ecSLuigi Rizzo 		m->m_next = m_copy(m0, off, len);
1161b375c9ecSLuigi Rizzo 		if (m->m_next == 0) {
1162b375c9ecSLuigi Rizzo 			(void) m_free(m);
1163b375c9ecSLuigi Rizzo 			error = ENOBUFS;	/* ??? */
1164b375c9ecSLuigi Rizzo 			ipstat.ips_odropped++;
1165b375c9ecSLuigi Rizzo 			goto sendorfree;
1166b375c9ecSLuigi Rizzo 		}
1167b375c9ecSLuigi Rizzo 		m->m_pkthdr.len = mhlen + len;
1168b375c9ecSLuigi Rizzo 		m->m_pkthdr.rcvif = (struct ifnet *)0;
1169b375c9ecSLuigi Rizzo #ifdef MAC
1170b375c9ecSLuigi Rizzo 		mac_create_fragment(m0, m);
1171b375c9ecSLuigi Rizzo #endif
1172b375c9ecSLuigi Rizzo 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
1173b375c9ecSLuigi Rizzo 		mhip->ip_off = htons(mhip->ip_off);
1174b375c9ecSLuigi Rizzo 		mhip->ip_sum = 0;
1175b375c9ecSLuigi Rizzo 		if (sw_csum & CSUM_DELAY_IP)
1176b375c9ecSLuigi Rizzo 			mhip->ip_sum = in_cksum(m, mhlen);
1177b375c9ecSLuigi Rizzo 		*mnext = m;
1178b375c9ecSLuigi Rizzo 		mnext = &m->m_nextpkt;
1179b375c9ecSLuigi Rizzo 		nfrags++;
1180b375c9ecSLuigi Rizzo 	}
1181b375c9ecSLuigi Rizzo 	ipstat.ips_ofragments += nfrags;
1182b375c9ecSLuigi Rizzo 
1183b375c9ecSLuigi Rizzo 	/* set first/last markers for fragment chain */
1184b375c9ecSLuigi Rizzo 	m->m_flags |= M_LASTFRAG;
1185b375c9ecSLuigi Rizzo 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1186b375c9ecSLuigi Rizzo 	m0->m_pkthdr.csum_data = nfrags;
1187b375c9ecSLuigi Rizzo 
1188b375c9ecSLuigi Rizzo 	/*
1189b375c9ecSLuigi Rizzo 	 * Update first fragment by trimming what's been copied out
1190b375c9ecSLuigi Rizzo 	 * and updating header, then send each fragment (in order).
1191b375c9ecSLuigi Rizzo 	 */
1192b375c9ecSLuigi Rizzo 	m = m0;
1193b375c9ecSLuigi Rizzo 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
1194b375c9ecSLuigi Rizzo 	m->m_pkthdr.len = hlen + firstlen;
1195b375c9ecSLuigi Rizzo 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
1196b375c9ecSLuigi Rizzo 	ip->ip_off |= IP_MF;
1197b375c9ecSLuigi Rizzo 	ip->ip_off = htons(ip->ip_off);
1198b375c9ecSLuigi Rizzo 	ip->ip_sum = 0;
1199b375c9ecSLuigi Rizzo 	if (sw_csum & CSUM_DELAY_IP)
1200b375c9ecSLuigi Rizzo 		ip->ip_sum = in_cksum(m, hlen);
1201b375c9ecSLuigi Rizzo sendorfree:
1202b375c9ecSLuigi Rizzo 	for (m = m0; m; m = m0) {
1203df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
1204b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
120533841545SHajimu UMEMOTO #ifdef IPSEC
120633841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
120733841545SHajimu UMEMOTO 		ipsec_delaux(m);
120833841545SHajimu UMEMOTO #endif
120907203494SDaniel C. Sobral 		if (error == 0) {
1210fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
121107203494SDaniel C. Sobral 			if (ia != NULL) {
1212fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
1213fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
121407203494SDaniel C. Sobral 			}
1215fe937674SJosef Karthauser 
1216df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
1217df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
1218fe937674SJosef Karthauser 		} else
1219df8bae1dSRodney W. Grimes 			m_freem(m);
1220df8bae1dSRodney W. Grimes 	}
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes 	if (error == 0)
1223df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
1224b375c9ecSLuigi Rizzo     }
1225df8bae1dSRodney W. Grimes done:
12266a800098SYoshinobu Inoue #ifdef IPSEC
12276a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
12286a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
12296a800098SYoshinobu Inoue 		ro->ro_rt = NULL;
12306a800098SYoshinobu Inoue 	}
12316a800098SYoshinobu Inoue 	if (sp != NULL) {
12326a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
12336a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
12346a800098SYoshinobu Inoue 		key_freesp(sp);
12356a800098SYoshinobu Inoue 	}
12366a800098SYoshinobu Inoue #endif /* IPSEC */
1237b9234fafSSam Leffler #ifdef FAST_IPSEC
1238b9234fafSSam Leffler 	if (ro == &iproute && ro->ro_rt) {
1239b9234fafSSam Leffler 		RTFREE(ro->ro_rt);
1240b9234fafSSam Leffler 		ro->ro_rt = NULL;
1241b9234fafSSam Leffler 	}
1242b9234fafSSam Leffler 	if (sp != NULL)
1243b9234fafSSam Leffler 		KEY_FREESP(&sp);
1244b9234fafSSam Leffler #endif /* FAST_IPSEC */
1245df8bae1dSRodney W. Grimes 	return (error);
1246df8bae1dSRodney W. Grimes bad:
12473528d68fSBill Paul 	m_freem(m);
1248df8bae1dSRodney W. Grimes 	goto done;
1249df8bae1dSRodney W. Grimes }
1250df8bae1dSRodney W. Grimes 
12511c238475SJonathan Lemon void
1252db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1253db4f9cc7SJonathan Lemon {
1254db4f9cc7SJonathan Lemon 	struct ip *ip;
1255db4f9cc7SJonathan Lemon 	u_short csum, offset;
1256db4f9cc7SJonathan Lemon 
1257db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
125853be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
1259db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1260206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1261206a3274SRuslan Ermilov 		csum = 0xffff;
1262db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1263db4f9cc7SJonathan Lemon 
1264db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1265db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1266db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1267db4f9cc7SJonathan Lemon 		/*
1268db4f9cc7SJonathan Lemon 		 * XXX
1269db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1270db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1271db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1272db4f9cc7SJonathan Lemon 		 */
1273db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1274db4f9cc7SJonathan Lemon 	}
1275db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1276db4f9cc7SJonathan Lemon }
1277db4f9cc7SJonathan Lemon 
1278df8bae1dSRodney W. Grimes /*
1279df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1280df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1281df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1282072b9b24SPaul Traina  *
1283072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1284df8bae1dSRodney W. Grimes  */
1285df8bae1dSRodney W. Grimes static struct mbuf *
1286b375c9ecSLuigi Rizzo ip_insertoptions(m, opt, phlen)
1287b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1288b375c9ecSLuigi Rizzo 	struct mbuf *opt;
1289b375c9ecSLuigi Rizzo 	int *phlen;
1290df8bae1dSRodney W. Grimes {
1291b375c9ecSLuigi Rizzo 	register struct ipoption *p = mtod(opt, struct ipoption *);
1292df8bae1dSRodney W. Grimes 	struct mbuf *n;
1293b375c9ecSLuigi Rizzo 	register struct ip *ip = mtod(m, struct ip *);
1294df8bae1dSRodney W. Grimes 	unsigned optlen;
1295df8bae1dSRodney W. Grimes 
1296df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
1297b375c9ecSLuigi Rizzo 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) {
1298cb7641e8SMaxim Konovalov 		*phlen = 0;
1299df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1300cb7641e8SMaxim Konovalov 	}
1301df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1302df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1303df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1304a163d034SWarner Losh 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1305cb7641e8SMaxim Konovalov 		if (n == 0) {
1306cb7641e8SMaxim Konovalov 			*phlen = 0;
1307df8bae1dSRodney W. Grimes 			return (m);
1308cb7641e8SMaxim Konovalov 		}
13095db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
13104ed84624SRobert Watson #ifdef MAC
13114ed84624SRobert Watson 		mac_create_mbuf_from_mbuf(m, n);
13124ed84624SRobert Watson #endif
1313df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1314df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1315df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1316df8bae1dSRodney W. Grimes 		n->m_next = m;
1317df8bae1dSRodney W. Grimes 		m = n;
1318df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1319df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
1320212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1321df8bae1dSRodney W. Grimes 	} else {
1322df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1323df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1324df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1325212059bdSDag-Erling Smørgrav 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1326df8bae1dSRodney W. Grimes 	}
1327df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
13280453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1329df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
133053be11f6SPoul-Henning Kamp 	ip->ip_v = IPVERSION;
133153be11f6SPoul-Henning Kamp 	ip->ip_hl = *phlen >> 2;
1332df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1333df8bae1dSRodney W. Grimes 	return (m);
1334df8bae1dSRodney W. Grimes }
1335df8bae1dSRodney W. Grimes 
1336df8bae1dSRodney W. Grimes /*
1337df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1338df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1339df8bae1dSRodney W. Grimes  */
1340beec8214SDarren Reed int
1341b375c9ecSLuigi Rizzo ip_optcopy(ip, jp)
1342b375c9ecSLuigi Rizzo 	struct ip *ip, *jp;
1343df8bae1dSRodney W. Grimes {
1344b375c9ecSLuigi Rizzo 	register u_char *cp, *dp;
1345df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1346df8bae1dSRodney W. Grimes 
1347df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1348df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
134953be11f6SPoul-Henning Kamp 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1350df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1351df8bae1dSRodney W. Grimes 		opt = cp[0];
1352df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1353df8bae1dSRodney W. Grimes 			break;
1354df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1355df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1356df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1357df8bae1dSRodney W. Grimes 			optlen = 1;
1358df8bae1dSRodney W. Grimes 			continue;
1359686cdd19SJun-ichiro itojun Hagino 		}
1360db40007dSAndrew R. Reiter 
1361db40007dSAndrew R. Reiter 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1362db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1363df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1364db40007dSAndrew R. Reiter 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1365db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1366db40007dSAndrew R. Reiter 
1367df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1368df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1369df8bae1dSRodney W. Grimes 			optlen = cnt;
1370df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
13710453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1372df8bae1dSRodney W. Grimes 			dp += optlen;
1373df8bae1dSRodney W. Grimes 		}
1374df8bae1dSRodney W. Grimes 	}
1375df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1376df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1377df8bae1dSRodney W. Grimes 	return (optlen);
1378df8bae1dSRodney W. Grimes }
1379df8bae1dSRodney W. Grimes 
1380df8bae1dSRodney W. Grimes /*
1381df8bae1dSRodney W. Grimes  * IP socket option processing.
1382df8bae1dSRodney W. Grimes  */
1383df8bae1dSRodney W. Grimes int
1384b375c9ecSLuigi Rizzo ip_ctloutput(so, sopt)
1385b375c9ecSLuigi Rizzo 	struct socket *so;
1386b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1387df8bae1dSRodney W. Grimes {
1388cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1389cfe8b629SGarrett Wollman 	int	error, optval;
1390df8bae1dSRodney W. Grimes 
1391cfe8b629SGarrett Wollman 	error = optval = 0;
1392cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1393cfe8b629SGarrett Wollman 		return (EINVAL);
1394cfe8b629SGarrett Wollman 	}
1395df8bae1dSRodney W. Grimes 
1396cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1397cfe8b629SGarrett Wollman 	case SOPT_SET:
1398cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1399df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1400df8bae1dSRodney W. Grimes #ifdef notyet
1401df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1402df8bae1dSRodney W. Grimes #endif
1403cfe8b629SGarrett Wollman 		{
1404cfe8b629SGarrett Wollman 			struct mbuf *m;
1405cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1406cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1407cfe8b629SGarrett Wollman 				break;
1408cfe8b629SGarrett Wollman 			}
1409a163d034SWarner Losh 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1410cfe8b629SGarrett Wollman 			if (m == 0) {
1411cfe8b629SGarrett Wollman 				error = ENOBUFS;
1412cfe8b629SGarrett Wollman 				break;
1413cfe8b629SGarrett Wollman 			}
1414cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1415cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1416cfe8b629SGarrett Wollman 					    m->m_len);
1417cfe8b629SGarrett Wollman 
1418cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1419cfe8b629SGarrett Wollman 					   m));
1420cfe8b629SGarrett Wollman 		}
1421df8bae1dSRodney W. Grimes 
1422df8bae1dSRodney W. Grimes 		case IP_TOS:
1423df8bae1dSRodney W. Grimes 		case IP_TTL:
1424df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1425df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1426df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
142782c23ebaSBill Fenner 		case IP_RECVIF:
14286a800098SYoshinobu Inoue 		case IP_FAITH:
1429cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1430cfe8b629SGarrett Wollman 					    sizeof optval);
1431cfe8b629SGarrett Wollman 			if (error)
1432cfe8b629SGarrett Wollman 				break;
1433df8bae1dSRodney W. Grimes 
1434cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1435df8bae1dSRodney W. Grimes 			case IP_TOS:
1436ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1437df8bae1dSRodney W. Grimes 				break;
1438df8bae1dSRodney W. Grimes 
1439df8bae1dSRodney W. Grimes 			case IP_TTL:
1440ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1441df8bae1dSRodney W. Grimes 				break;
1442df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
1443df8bae1dSRodney W. Grimes 	if (optval) \
1444df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
1445df8bae1dSRodney W. Grimes 	else \
1446df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
1447df8bae1dSRodney W. Grimes 
1448df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1449df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1450df8bae1dSRodney W. Grimes 				break;
1451df8bae1dSRodney W. Grimes 
1452df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1453df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1454df8bae1dSRodney W. Grimes 				break;
1455df8bae1dSRodney W. Grimes 
1456df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1457df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1458df8bae1dSRodney W. Grimes 				break;
145982c23ebaSBill Fenner 
146082c23ebaSBill Fenner 			case IP_RECVIF:
146182c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
146282c23ebaSBill Fenner 				break;
14636a800098SYoshinobu Inoue 
14646a800098SYoshinobu Inoue 			case IP_FAITH:
14656a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
14666a800098SYoshinobu Inoue 				break;
1467df8bae1dSRodney W. Grimes 			}
1468df8bae1dSRodney W. Grimes 			break;
1469df8bae1dSRodney W. Grimes #undef OPTSET
1470df8bae1dSRodney W. Grimes 
1471df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1472f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1473df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1474df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1475df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1476df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1477cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1478df8bae1dSRodney W. Grimes 			break;
1479df8bae1dSRodney W. Grimes 
148033b3ac06SPeter Wemm 		case IP_PORTRANGE:
1481cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1482cfe8b629SGarrett Wollman 					    sizeof optval);
1483cfe8b629SGarrett Wollman 			if (error)
1484cfe8b629SGarrett Wollman 				break;
148533b3ac06SPeter Wemm 
148633b3ac06SPeter Wemm 			switch (optval) {
148733b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
148833b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
148933b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
149033b3ac06SPeter Wemm 				break;
149133b3ac06SPeter Wemm 
149233b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
149333b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
149433b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
149533b3ac06SPeter Wemm 				break;
149633b3ac06SPeter Wemm 
149733b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
149833b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
149933b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
150033b3ac06SPeter Wemm 				break;
150133b3ac06SPeter Wemm 
150233b3ac06SPeter Wemm 			default:
150333b3ac06SPeter Wemm 				error = EINVAL;
150433b3ac06SPeter Wemm 				break;
150533b3ac06SPeter Wemm 			}
1506ce8c72b1SPeter Wemm 			break;
150733b3ac06SPeter Wemm 
1508b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
15096a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
15106a800098SYoshinobu Inoue 		{
15116a800098SYoshinobu Inoue 			caddr_t req;
1512686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
15136a800098SYoshinobu Inoue 			int priv;
15146a800098SYoshinobu Inoue 			struct mbuf *m;
15156a800098SYoshinobu Inoue 			int optname;
15166a800098SYoshinobu Inoue 
15176a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
15186a800098SYoshinobu Inoue 				break;
15196a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
15206a800098SYoshinobu Inoue 				break;
1521b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
152244731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
15236a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1524686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
15256a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1526686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
15276a800098SYoshinobu Inoue 			m_freem(m);
15286a800098SYoshinobu Inoue 			break;
15296a800098SYoshinobu Inoue 		}
15306a800098SYoshinobu Inoue #endif /*IPSEC*/
15316a800098SYoshinobu Inoue 
1532df8bae1dSRodney W. Grimes 		default:
1533df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1534df8bae1dSRodney W. Grimes 			break;
1535df8bae1dSRodney W. Grimes 		}
1536df8bae1dSRodney W. Grimes 		break;
1537df8bae1dSRodney W. Grimes 
1538cfe8b629SGarrett Wollman 	case SOPT_GET:
1539cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1540df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1541df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1542cfe8b629SGarrett Wollman 			if (inp->inp_options)
1543cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1544cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1545cfe8b629SGarrett Wollman 							  char *),
1546cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1547cfe8b629SGarrett Wollman 			else
1548cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1549df8bae1dSRodney W. Grimes 			break;
1550df8bae1dSRodney W. Grimes 
1551df8bae1dSRodney W. Grimes 		case IP_TOS:
1552df8bae1dSRodney W. Grimes 		case IP_TTL:
1553df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1554df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1555df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
155682c23ebaSBill Fenner 		case IP_RECVIF:
1557cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
15586a800098SYoshinobu Inoue 		case IP_FAITH:
1559cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1560df8bae1dSRodney W. Grimes 
1561df8bae1dSRodney W. Grimes 			case IP_TOS:
1562ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1563df8bae1dSRodney W. Grimes 				break;
1564df8bae1dSRodney W. Grimes 
1565df8bae1dSRodney W. Grimes 			case IP_TTL:
1566ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1567df8bae1dSRodney W. Grimes 				break;
1568df8bae1dSRodney W. Grimes 
1569df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1570df8bae1dSRodney W. Grimes 
1571df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1572df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1573df8bae1dSRodney W. Grimes 				break;
1574df8bae1dSRodney W. Grimes 
1575df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1576df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1577df8bae1dSRodney W. Grimes 				break;
1578df8bae1dSRodney W. Grimes 
1579df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1580df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1581df8bae1dSRodney W. Grimes 				break;
158282c23ebaSBill Fenner 
158382c23ebaSBill Fenner 			case IP_RECVIF:
158482c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
158582c23ebaSBill Fenner 				break;
1586cfe8b629SGarrett Wollman 
1587cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1588cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1589cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1590cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1591cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1592cfe8b629SGarrett Wollman 				else
1593cfe8b629SGarrett Wollman 					optval = 0;
1594cfe8b629SGarrett Wollman 				break;
15956a800098SYoshinobu Inoue 
15966a800098SYoshinobu Inoue 			case IP_FAITH:
15976a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
15986a800098SYoshinobu Inoue 				break;
1599df8bae1dSRodney W. Grimes 			}
1600cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1601df8bae1dSRodney W. Grimes 			break;
1602df8bae1dSRodney W. Grimes 
1603df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1604f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1605df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1606df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1607df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1608df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1609cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
161033b3ac06SPeter Wemm 			break;
161133b3ac06SPeter Wemm 
1612b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
16136a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
16146a800098SYoshinobu Inoue 		{
1615f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
16166a800098SYoshinobu Inoue 			caddr_t req = NULL;
1617686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
16186a800098SYoshinobu Inoue 
1619686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
16206a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1621686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1622686cdd19SJun-ichiro itojun Hagino 			}
1623686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
16246a800098SYoshinobu Inoue 			if (error == 0)
16256a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1626f63e7634SYoshinobu Inoue 			if (error == 0)
16276a800098SYoshinobu Inoue 				m_freem(m);
16286a800098SYoshinobu Inoue 			break;
16296a800098SYoshinobu Inoue 		}
16306a800098SYoshinobu Inoue #endif /*IPSEC*/
16316a800098SYoshinobu Inoue 
1632df8bae1dSRodney W. Grimes 		default:
1633df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1634df8bae1dSRodney W. Grimes 			break;
1635df8bae1dSRodney W. Grimes 		}
1636df8bae1dSRodney W. Grimes 		break;
1637df8bae1dSRodney W. Grimes 	}
1638df8bae1dSRodney W. Grimes 	return (error);
1639df8bae1dSRodney W. Grimes }
1640df8bae1dSRodney W. Grimes 
1641df8bae1dSRodney W. Grimes /*
1642df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1643df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1644df8bae1dSRodney W. Grimes  * with destination address if source routed.
1645df8bae1dSRodney W. Grimes  */
16460312fbe9SPoul-Henning Kamp static int
1647b375c9ecSLuigi Rizzo ip_pcbopts(optname, pcbopt, m)
1648b375c9ecSLuigi Rizzo 	int optname;
1649b375c9ecSLuigi Rizzo 	struct mbuf **pcbopt;
1650b375c9ecSLuigi Rizzo 	register struct mbuf *m;
1651df8bae1dSRodney W. Grimes {
1652b375c9ecSLuigi Rizzo 	register int cnt, optlen;
1653b375c9ecSLuigi Rizzo 	register u_char *cp;
1654df8bae1dSRodney W. Grimes 	u_char opt;
1655df8bae1dSRodney W. Grimes 
1656df8bae1dSRodney W. Grimes 	/* turn off any old options */
1657df8bae1dSRodney W. Grimes 	if (*pcbopt)
1658df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1659df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1660df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1661df8bae1dSRodney W. Grimes 		/*
1662df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1663df8bae1dSRodney W. Grimes 		 */
1664df8bae1dSRodney W. Grimes 		if (m)
1665df8bae1dSRodney W. Grimes 			(void)m_free(m);
1666df8bae1dSRodney W. Grimes 		return (0);
1667df8bae1dSRodney W. Grimes 	}
1668df8bae1dSRodney W. Grimes 
16690c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1670df8bae1dSRodney W. Grimes 		goto bad;
1671df8bae1dSRodney W. Grimes 	/*
1672df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1673df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1674df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1675df8bae1dSRodney W. Grimes 	 */
1676df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1677df8bae1dSRodney W. Grimes 		goto bad;
1678df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1679df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1680df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1681212059bdSDag-Erling Smørgrav 	bcopy(mtod(m, void *), cp, (unsigned)cnt);
1682212059bdSDag-Erling Smørgrav 	bzero(mtod(m, void *), sizeof(struct in_addr));
1683df8bae1dSRodney W. Grimes 
1684df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1685df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1686df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1687df8bae1dSRodney W. Grimes 			break;
1688df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1689df8bae1dSRodney W. Grimes 			optlen = 1;
1690df8bae1dSRodney W. Grimes 		else {
1691707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1692707d00a3SJonathan Lemon 				goto bad;
1693df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1694707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1695df8bae1dSRodney W. Grimes 				goto bad;
1696df8bae1dSRodney W. Grimes 		}
1697df8bae1dSRodney W. Grimes 		switch (opt) {
1698df8bae1dSRodney W. Grimes 
1699df8bae1dSRodney W. Grimes 		default:
1700df8bae1dSRodney W. Grimes 			break;
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1703df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1704df8bae1dSRodney W. Grimes 			/*
1705df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1706df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1707df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1708df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1709df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1710df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1711df8bae1dSRodney W. Grimes 			 */
1712df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1713df8bae1dSRodney W. Grimes 				goto bad;
1714df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1715df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1716df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1717df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1718df8bae1dSRodney W. Grimes 			/*
1719df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1720df8bae1dSRodney W. Grimes 			 */
1721df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1722df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1723df8bae1dSRodney W. Grimes 			/*
1724df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1725df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1726df8bae1dSRodney W. Grimes 			 */
1727212059bdSDag-Erling Smørgrav 			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1728212059bdSDag-Erling Smørgrav 			    &cp[IPOPT_OFFSET+1],
1729df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
1730df8bae1dSRodney W. Grimes 			break;
1731df8bae1dSRodney W. Grimes 		}
1732df8bae1dSRodney W. Grimes 	}
1733df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1734df8bae1dSRodney W. Grimes 		goto bad;
1735df8bae1dSRodney W. Grimes 	*pcbopt = m;
1736df8bae1dSRodney W. Grimes 	return (0);
1737df8bae1dSRodney W. Grimes 
1738df8bae1dSRodney W. Grimes bad:
1739df8bae1dSRodney W. Grimes 	(void)m_free(m);
1740df8bae1dSRodney W. Grimes 	return (EINVAL);
1741df8bae1dSRodney W. Grimes }
1742df8bae1dSRodney W. Grimes 
1743df8bae1dSRodney W. Grimes /*
1744cfe8b629SGarrett Wollman  * XXX
1745cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1746cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1747cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1748cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1749cfe8b629SGarrett Wollman  */
175033841545SHajimu UMEMOTO 
175133841545SHajimu UMEMOTO /*
175233841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
175333841545SHajimu UMEMOTO  */
175433841545SHajimu UMEMOTO static struct ifnet *
1755b375c9ecSLuigi Rizzo ip_multicast_if(a, ifindexp)
1756b375c9ecSLuigi Rizzo 	struct in_addr *a;
1757b375c9ecSLuigi Rizzo 	int *ifindexp;
175833841545SHajimu UMEMOTO {
175933841545SHajimu UMEMOTO 	int ifindex;
176033841545SHajimu UMEMOTO 	struct ifnet *ifp;
176133841545SHajimu UMEMOTO 
176233841545SHajimu UMEMOTO 	if (ifindexp)
176333841545SHajimu UMEMOTO 		*ifindexp = 0;
176433841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
176533841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
176633841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
176733841545SHajimu UMEMOTO 			return NULL;
1768f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
176933841545SHajimu UMEMOTO 		if (ifindexp)
177033841545SHajimu UMEMOTO 			*ifindexp = ifindex;
177133841545SHajimu UMEMOTO 	} else {
177233841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
177333841545SHajimu UMEMOTO 	}
177433841545SHajimu UMEMOTO 	return ifp;
177533841545SHajimu UMEMOTO }
177633841545SHajimu UMEMOTO 
1777cfe8b629SGarrett Wollman /*
1778df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1779df8bae1dSRodney W. Grimes  */
17800312fbe9SPoul-Henning Kamp static int
1781b375c9ecSLuigi Rizzo ip_setmoptions(sopt, imop)
1782b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
1783b375c9ecSLuigi Rizzo 	struct ip_moptions **imop;
1784df8bae1dSRodney W. Grimes {
1785cfe8b629SGarrett Wollman 	int error = 0;
1786cfe8b629SGarrett Wollman 	int i;
1787df8bae1dSRodney W. Grimes 	struct in_addr addr;
1788cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1789cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1790cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1791df8bae1dSRodney W. Grimes 	struct route ro;
1792cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
179333841545SHajimu UMEMOTO 	int ifindex;
17949b626c29SGarrett Wollman 	int s;
1795df8bae1dSRodney W. Grimes 
1796df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1797df8bae1dSRodney W. Grimes 		/*
1798df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1799df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1800df8bae1dSRodney W. Grimes 		 */
1801df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1802a163d034SWarner Losh 		    M_WAITOK);
1803df8bae1dSRodney W. Grimes 
1804df8bae1dSRodney W. Grimes 		if (imo == NULL)
1805df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1806df8bae1dSRodney W. Grimes 		*imop = imo;
1807df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
180833841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
18091c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1810df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1811df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1812df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1813df8bae1dSRodney W. Grimes 	}
1814df8bae1dSRodney W. Grimes 
1815cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1816f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1817f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1818cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
18195e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
18205e9ae478SGarrett Wollman 			break;
18215e9ae478SGarrett Wollman 		}
1822cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1823cfe8b629SGarrett Wollman 		if (error)
1824f0068c4aSGarrett Wollman 			break;
18251c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1826f0068c4aSGarrett Wollman 			error = EINVAL;
1827f0068c4aSGarrett Wollman 			break;
1828f0068c4aSGarrett Wollman 		}
1829f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1830f0068c4aSGarrett Wollman 		break;
1831f0068c4aSGarrett Wollman 
1832df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1833df8bae1dSRodney W. Grimes 		/*
1834df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1835df8bae1dSRodney W. Grimes 		 */
1836cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1837cfe8b629SGarrett Wollman 		if (error)
1838df8bae1dSRodney W. Grimes 			break;
1839df8bae1dSRodney W. Grimes 		/*
1840df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1841df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1842df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1843df8bae1dSRodney W. Grimes 		 */
1844df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1845df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1846df8bae1dSRodney W. Grimes 			break;
1847df8bae1dSRodney W. Grimes 		}
1848df8bae1dSRodney W. Grimes 		/*
1849df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1850df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1851df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1852df8bae1dSRodney W. Grimes 		 */
185320e8807cSGarrett Wollman 		s = splimp();
185433841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1855df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1856fbc6ab00SBill Fenner 			splx(s);
1857df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1858df8bae1dSRodney W. Grimes 			break;
1859df8bae1dSRodney W. Grimes 		}
1860df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
186133841545SHajimu UMEMOTO 		if (ifindex)
186233841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
186333841545SHajimu UMEMOTO 		else
186433841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
18659b626c29SGarrett Wollman 		splx(s);
1866df8bae1dSRodney W. Grimes 		break;
1867df8bae1dSRodney W. Grimes 
1868df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1869df8bae1dSRodney W. Grimes 		/*
1870df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1871cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1872cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1873cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1874df8bae1dSRodney W. Grimes 		 */
1875cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1876cfe8b629SGarrett Wollman 			u_char ttl;
1877cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1878cfe8b629SGarrett Wollman 			if (error)
1879df8bae1dSRodney W. Grimes 				break;
1880cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1881cfe8b629SGarrett Wollman 		} else {
1882cfe8b629SGarrett Wollman 			u_int ttl;
1883cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1884cfe8b629SGarrett Wollman 					    sizeof ttl);
1885cfe8b629SGarrett Wollman 			if (error)
1886cfe8b629SGarrett Wollman 				break;
1887cfe8b629SGarrett Wollman 			if (ttl > 255)
1888cfe8b629SGarrett Wollman 				error = EINVAL;
1889cfe8b629SGarrett Wollman 			else
1890cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1891df8bae1dSRodney W. Grimes 		}
1892df8bae1dSRodney W. Grimes 		break;
1893df8bae1dSRodney W. Grimes 
1894df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1895df8bae1dSRodney W. Grimes 		/*
1896df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1897cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1898cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1899cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1900df8bae1dSRodney W. Grimes 		 */
1901cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1902cfe8b629SGarrett Wollman 			u_char loop;
1903cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1904cfe8b629SGarrett Wollman 			if (error)
1905df8bae1dSRodney W. Grimes 				break;
1906cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1907cfe8b629SGarrett Wollman 		} else {
1908cfe8b629SGarrett Wollman 			u_int loop;
1909cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1910cfe8b629SGarrett Wollman 					    sizeof loop);
1911cfe8b629SGarrett Wollman 			if (error)
1912cfe8b629SGarrett Wollman 				break;
1913cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1914df8bae1dSRodney W. Grimes 		}
1915df8bae1dSRodney W. Grimes 		break;
1916df8bae1dSRodney W. Grimes 
1917df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1918df8bae1dSRodney W. Grimes 		/*
1919df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1920df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1921df8bae1dSRodney W. Grimes 		 */
1922cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1923cfe8b629SGarrett Wollman 		if (error)
1924df8bae1dSRodney W. Grimes 			break;
1925cfe8b629SGarrett Wollman 
1926cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1927df8bae1dSRodney W. Grimes 			error = EINVAL;
1928df8bae1dSRodney W. Grimes 			break;
1929df8bae1dSRodney W. Grimes 		}
193020e8807cSGarrett Wollman 		s = splimp();
1931df8bae1dSRodney W. Grimes 		/*
1932df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1933df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1934df8bae1dSRodney W. Grimes 		 */
1935cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
19361c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1937df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1938df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1939df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1940cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
1941df8bae1dSRodney W. Grimes 			rtalloc(&ro);
1942df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1943df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
19449b626c29SGarrett Wollman 				splx(s);
1945df8bae1dSRodney W. Grimes 				break;
1946df8bae1dSRodney W. Grimes 			}
1947df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1948df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
1949df8bae1dSRodney W. Grimes 		}
1950df8bae1dSRodney W. Grimes 		else {
195133841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1952df8bae1dSRodney W. Grimes 		}
19539b626c29SGarrett Wollman 
1954df8bae1dSRodney W. Grimes 		/*
1955df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1956df8bae1dSRodney W. Grimes 		 * supports multicast.
1957df8bae1dSRodney W. Grimes 		 */
1958df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1959df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
19609b626c29SGarrett Wollman 			splx(s);
1961df8bae1dSRodney W. Grimes 			break;
1962df8bae1dSRodney W. Grimes 		}
1963df8bae1dSRodney W. Grimes 		/*
1964df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1965df8bae1dSRodney W. Grimes 		 * membership slots are full.
1966df8bae1dSRodney W. Grimes 		 */
1967df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1968df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1969df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1970cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1971df8bae1dSRodney W. Grimes 				break;
1972df8bae1dSRodney W. Grimes 		}
1973df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1974df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
19759b626c29SGarrett Wollman 			splx(s);
1976df8bae1dSRodney W. Grimes 			break;
1977df8bae1dSRodney W. Grimes 		}
1978df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1979df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
19809b626c29SGarrett Wollman 			splx(s);
1981df8bae1dSRodney W. Grimes 			break;
1982df8bae1dSRodney W. Grimes 		}
1983df8bae1dSRodney W. Grimes 		/*
1984df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1985df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1986df8bae1dSRodney W. Grimes 		 */
1987df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1988cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1989df8bae1dSRodney W. Grimes 			error = ENOBUFS;
19909b626c29SGarrett Wollman 			splx(s);
1991df8bae1dSRodney W. Grimes 			break;
1992df8bae1dSRodney W. Grimes 		}
1993df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
19949b626c29SGarrett Wollman 		splx(s);
1995df8bae1dSRodney W. Grimes 		break;
1996df8bae1dSRodney W. Grimes 
1997df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1998df8bae1dSRodney W. Grimes 		/*
1999df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
2000df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
2001df8bae1dSRodney W. Grimes 		 */
2002cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
2003cfe8b629SGarrett Wollman 		if (error)
2004df8bae1dSRodney W. Grimes 			break;
2005cfe8b629SGarrett Wollman 
2006cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
2007df8bae1dSRodney W. Grimes 			error = EINVAL;
2008df8bae1dSRodney W. Grimes 			break;
2009df8bae1dSRodney W. Grimes 		}
20109b626c29SGarrett Wollman 
201120e8807cSGarrett Wollman 		s = splimp();
2012df8bae1dSRodney W. Grimes 		/*
2013df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
2014df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
2015df8bae1dSRodney W. Grimes 		 */
2016cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
2017df8bae1dSRodney W. Grimes 			ifp = NULL;
2018df8bae1dSRodney W. Grimes 		else {
201933841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
2020df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
2021df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
20229b626c29SGarrett Wollman 				splx(s);
2023df8bae1dSRodney W. Grimes 				break;
2024df8bae1dSRodney W. Grimes 			}
2025df8bae1dSRodney W. Grimes 		}
2026df8bae1dSRodney W. Grimes 		/*
2027df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
2028df8bae1dSRodney W. Grimes 		 */
2029df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
2030df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
2031df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
2032df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
2033cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
2034df8bae1dSRodney W. Grimes 				break;
2035df8bae1dSRodney W. Grimes 		}
2036df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
2037df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
20389b626c29SGarrett Wollman 			splx(s);
2039df8bae1dSRodney W. Grimes 			break;
2040df8bae1dSRodney W. Grimes 		}
2041df8bae1dSRodney W. Grimes 		/*
2042df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
2043df8bae1dSRodney W. Grimes 		 * membership points.
2044df8bae1dSRodney W. Grimes 		 */
2045df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
2046df8bae1dSRodney W. Grimes 		/*
2047df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
2048df8bae1dSRodney W. Grimes 		 */
2049df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
2050df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
2051df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
20529b626c29SGarrett Wollman 		splx(s);
2053df8bae1dSRodney W. Grimes 		break;
2054df8bae1dSRodney W. Grimes 
2055df8bae1dSRodney W. Grimes 	default:
2056df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
2057df8bae1dSRodney W. Grimes 		break;
2058df8bae1dSRodney W. Grimes 	}
2059df8bae1dSRodney W. Grimes 
2060df8bae1dSRodney W. Grimes 	/*
2061df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
2062df8bae1dSRodney W. Grimes 	 */
2063df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
20641c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
2065df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2066df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2067df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
2068df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
2069df8bae1dSRodney W. Grimes 		*imop = NULL;
2070df8bae1dSRodney W. Grimes 	}
2071df8bae1dSRodney W. Grimes 
2072df8bae1dSRodney W. Grimes 	return (error);
2073df8bae1dSRodney W. Grimes }
2074df8bae1dSRodney W. Grimes 
2075df8bae1dSRodney W. Grimes /*
2076df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
2077df8bae1dSRodney W. Grimes  */
20780312fbe9SPoul-Henning Kamp static int
2079b375c9ecSLuigi Rizzo ip_getmoptions(sopt, imo)
2080b375c9ecSLuigi Rizzo 	struct sockopt *sopt;
2081b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
2082df8bae1dSRodney W. Grimes {
2083cfe8b629SGarrett Wollman 	struct in_addr addr;
2084df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
2085cfe8b629SGarrett Wollman 	int error, optval;
2086cfe8b629SGarrett Wollman 	u_char coptval;
2087df8bae1dSRodney W. Grimes 
2088cfe8b629SGarrett Wollman 	error = 0;
2089cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
2090f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
2091f0068c4aSGarrett Wollman 		if (imo != NULL)
2092cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
2093f0068c4aSGarrett Wollman 		else
2094cfe8b629SGarrett Wollman 			optval = -1;
2095cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
2096cfe8b629SGarrett Wollman 		break;
2097f0068c4aSGarrett Wollman 
2098df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
2099df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
2100cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
210133841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
210233841545SHajimu UMEMOTO 			/* return the value user has set */
210333841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
210433841545SHajimu UMEMOTO 		} else {
2105df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
2106cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
2107df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
2108df8bae1dSRodney W. Grimes 		}
2109cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
2110cfe8b629SGarrett Wollman 		break;
2111df8bae1dSRodney W. Grimes 
2112df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
2113cfe8b629SGarrett Wollman 		if (imo == 0)
2114cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
2115cfe8b629SGarrett Wollman 		else
2116cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
2117cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
2118cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
2119cfe8b629SGarrett Wollman 		else
2120cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
2121cfe8b629SGarrett Wollman 		break;
2122df8bae1dSRodney W. Grimes 
2123df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
2124cfe8b629SGarrett Wollman 		if (imo == 0)
2125cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
2126cfe8b629SGarrett Wollman 		else
2127cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
2128cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
2129cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
2130cfe8b629SGarrett Wollman 		else
2131cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
2132cfe8b629SGarrett Wollman 		break;
2133df8bae1dSRodney W. Grimes 
2134df8bae1dSRodney W. Grimes 	default:
2135cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
2136cfe8b629SGarrett Wollman 		break;
2137df8bae1dSRodney W. Grimes 	}
2138cfe8b629SGarrett Wollman 	return (error);
2139df8bae1dSRodney W. Grimes }
2140df8bae1dSRodney W. Grimes 
2141df8bae1dSRodney W. Grimes /*
2142df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
2143df8bae1dSRodney W. Grimes  */
2144df8bae1dSRodney W. Grimes void
2145b375c9ecSLuigi Rizzo ip_freemoptions(imo)
2146b375c9ecSLuigi Rizzo 	register struct ip_moptions *imo;
2147df8bae1dSRodney W. Grimes {
2148b375c9ecSLuigi Rizzo 	register int i;
2149df8bae1dSRodney W. Grimes 
2150df8bae1dSRodney W. Grimes 	if (imo != NULL) {
2151df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
2152df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
2153df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
2154df8bae1dSRodney W. Grimes 	}
2155df8bae1dSRodney W. Grimes }
2156df8bae1dSRodney W. Grimes 
2157df8bae1dSRodney W. Grimes /*
2158df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
2159df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
2160df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
2161f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
2162f5fea3ddSPaul Traina  * replicating that code here.
2163df8bae1dSRodney W. Grimes  */
2164df8bae1dSRodney W. Grimes static void
2165b375c9ecSLuigi Rizzo ip_mloopback(ifp, m, dst, hlen)
2166b375c9ecSLuigi Rizzo 	struct ifnet *ifp;
2167b375c9ecSLuigi Rizzo 	register struct mbuf *m;
2168b375c9ecSLuigi Rizzo 	register struct sockaddr_in *dst;
2169b375c9ecSLuigi Rizzo 	int hlen;
2170df8bae1dSRodney W. Grimes {
2171b375c9ecSLuigi Rizzo 	register struct ip *ip;
2172df8bae1dSRodney W. Grimes 	struct mbuf *copym;
2173df8bae1dSRodney W. Grimes 
2174b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
217586b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
217686b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
2177df8bae1dSRodney W. Grimes 	if (copym != NULL) {
2178df8bae1dSRodney W. Grimes 		/*
2179df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
2180df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
2181df8bae1dSRodney W. Grimes 		 */
2182df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
2183fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
2184fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
2185df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
218686b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
21879c9137eaSGarrett Wollman 		/*
21889c9137eaSGarrett Wollman 		 * NB:
2189e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
2190e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
2191e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
2192e1596dffSBill Fenner 		 * particular, everything which modifies the packet
2193e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
2194e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
2195e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
2196ed7509acSJulian Elischer 		 * of safety by using if_simloop().
21979c9137eaSGarrett Wollman 		 */
2198201c2527SJulian Elischer #if 1 /* XXX */
2199201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
22002b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
2201201c2527SJulian Elischer 						dst->sin_family);
2202201c2527SJulian Elischer 			dst->sin_family = AF_INET;
2203201c2527SJulian Elischer 		}
2204201c2527SJulian Elischer #endif
2205201c2527SJulian Elischer 
22069c9137eaSGarrett Wollman #ifdef notdef
2207e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
2208ed7509acSJulian Elischer 		ip_input(copym);
22099c9137eaSGarrett Wollman #else
221050c6dc99SJonathan Lemon 		/* if the checksum hasn't been computed, mark it as valid */
221150c6dc99SJonathan Lemon 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
221250c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_flags |=
221350c6dc99SJonathan Lemon 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
221450c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_data = 0xffff;
221550c6dc99SJonathan Lemon 		}
221606a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
22179c9137eaSGarrett Wollman #endif
2218df8bae1dSRodney W. Grimes 	}
2219df8bae1dSRodney W. Grimes }
2220