xref: /freebsd/sys/netinet/ip_output.c (revision 2b25acc1581157d11ded358b54123a2f6c742d7b)
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 
379c9137eaSGarrett Wollman #define _IP_VHL
389c9137eaSGarrett Wollman 
391f7e052cSJulian Elischer #include "opt_ipfw.h"
40b715f178SLuigi Rizzo #include "opt_ipdn.h"
41fbd1372aSJoerg Wunsch #include "opt_ipdivert.h"
421ee25934SPeter Wemm #include "opt_ipfilter.h"
436a800098SYoshinobu Inoue #include "opt_ipsec.h"
44c4ac87eaSDarren Reed #include "opt_pfil_hooks.h"
4564dddc18SKris Kennaway #include "opt_random_ip_id.h"
46fbd1372aSJoerg Wunsch 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
4826f9a767SRodney W. Grimes #include <sys/systm.h>
49f0f6d643SLuigi Rizzo #include <sys/kernel.h>
50df8bae1dSRodney W. Grimes #include <sys/malloc.h>
51df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <net/if.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <netinet/in.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
65df8bae1dSRodney W. Grimes 
669c9137eaSGarrett Wollman #include <machine/in_cksum.h>
67df8bae1dSRodney W. Grimes 
68a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
6955166637SPoul-Henning Kamp 
706a800098SYoshinobu Inoue #ifdef IPSEC
716a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
726a800098SYoshinobu Inoue #include <netkey/key.h>
736a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG
746a800098SYoshinobu Inoue #include <netkey/key_debug.h>
756a800098SYoshinobu Inoue #else
766a800098SYoshinobu Inoue #define	KEYDEBUG(lev,arg)
776a800098SYoshinobu Inoue #endif
786a800098SYoshinobu Inoue #endif /*IPSEC*/
796a800098SYoshinobu Inoue 
80cfe8b629SGarrett Wollman #include <netinet/ip_fw.h>
81b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
82b715f178SLuigi Rizzo 
832b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
842b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
85f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
86f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
872b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
88f9e354dfSJulian Elischer 
89f23b4c91SGarrett Wollman u_short ip_id;
90f23b4c91SGarrett Wollman 
914d77a549SAlfred Perlstein static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
924d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *);
93df8bae1dSRodney W. Grimes static void	ip_mloopback
944d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
950312fbe9SPoul-Henning Kamp static int	ip_getmoptions
964d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions *);
974d77a549SAlfred Perlstein static int	ip_pcbopts(int, struct mbuf **, struct mbuf *);
980312fbe9SPoul-Henning Kamp static int	ip_setmoptions
994d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions **);
100df8bae1dSRodney W. Grimes 
1014d77a549SAlfred Perlstein int	ip_optcopy(struct ip *, struct ip *);
102afed1b49SDarren Reed 
103afed1b49SDarren Reed 
10493e0e116SJulian Elischer extern	struct protosw inetsw[];
10593e0e116SJulian Elischer 
106df8bae1dSRodney W. Grimes /*
107df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
108df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
109df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
110df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
111df8bae1dSRodney W. Grimes  */
112df8bae1dSRodney W. Grimes int
113df8bae1dSRodney W. Grimes ip_output(m0, opt, ro, flags, imo)
114df8bae1dSRodney W. Grimes 	struct mbuf *m0;
115df8bae1dSRodney W. Grimes 	struct mbuf *opt;
116df8bae1dSRodney W. Grimes 	struct route *ro;
117df8bae1dSRodney W. Grimes 	int flags;
118df8bae1dSRodney W. Grimes 	struct ip_moptions *imo;
119df8bae1dSRodney W. Grimes {
12023bf9953SPoul-Henning Kamp 	struct ip *ip, *mhip;
1212b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
1222b25acc1SLuigi Rizzo 	struct mbuf *m;
12323bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
124df8bae1dSRodney W. Grimes 	int len, off, error = 0;
1252b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
126df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
127db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
1283efc3014SJulian Elischer 	struct in_addr pkt_dst;
1296a800098SYoshinobu Inoue #ifdef IPSEC
130e3f406b3SRuslan Ermilov 	struct route iproute;
131d0a98d79SYoshinobu Inoue 	struct socket *so = NULL;
1326a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1336a800098SYoshinobu Inoue #endif
1342b25acc1SLuigi Rizzo 	struct ip_fw_args args;
1352b25acc1SLuigi Rizzo 	int src_was_INADDR_ANY = 0;	/* as the name says... */
136c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
137c4ac87eaSDarren Reed 	struct packet_filter_hook *pfh;
138c4ac87eaSDarren Reed 	struct mbuf *m1;
139c4ac87eaSDarren Reed 	int rv;
140c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
141b715f178SLuigi Rizzo 
1422b25acc1SLuigi Rizzo 	args.eh = NULL;
1432b25acc1SLuigi Rizzo 	args.rule = NULL;
1442b25acc1SLuigi Rizzo 	args.next_hop = NULL;
1452b25acc1SLuigi Rizzo 	args.divert_rule = 0;                   /* divert cookie */
1468948e4baSArchie Cobbs 
1472b25acc1SLuigi Rizzo 	/* Grab info from MT_TAG mbufs prepended to the chain. */
1482b25acc1SLuigi Rizzo         for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) {
1492b25acc1SLuigi Rizzo 		switch(m0->m_tag_id) {
1502b25acc1SLuigi Rizzo 		default:
1512b25acc1SLuigi Rizzo 			printf("ip_output: unrecognised MT_TAG tag %d\n",
1522b25acc1SLuigi Rizzo 			    m0->m_tag_id);
1532b25acc1SLuigi Rizzo 			break;
1542b25acc1SLuigi Rizzo 
1552b25acc1SLuigi Rizzo 		case PACKET_TAG_DUMMYNET:
156b715f178SLuigi Rizzo 			/*
157b715f178SLuigi Rizzo 			 * the packet was already tagged, so part of the
158b715f178SLuigi Rizzo 			 * processing was already done, and we need to go down.
1596bc748b0SLuigi Rizzo 			 * Get parameters from the header.
160b715f178SLuigi Rizzo 			 */
1612b25acc1SLuigi Rizzo 			args.rule = ((struct dn_pkt *)m0)->rule;
162d1f04b29SLuigi Rizzo 			opt = NULL ;
1632b25acc1SLuigi Rizzo 			ro = & ( ((struct dn_pkt *)m0)->ro ) ;
164d1f04b29SLuigi Rizzo 			imo = NULL ;
1652b25acc1SLuigi Rizzo 			dst = ((struct dn_pkt *)m0)->dn_dst ;
1662b25acc1SLuigi Rizzo 			ifp = ((struct dn_pkt *)m0)->ifp ;
1672b25acc1SLuigi Rizzo 			flags = ((struct dn_pkt *)m0)->flags ;
1682b25acc1SLuigi Rizzo 			break;
169d1f04b29SLuigi Rizzo 
1702b25acc1SLuigi Rizzo 		case PACKET_TAG_DIVERT:
1712b25acc1SLuigi Rizzo 			args.divert_rule = (int)m0->m_data & 0xffff;
1722b25acc1SLuigi Rizzo 			break;
1732b25acc1SLuigi Rizzo 
1742b25acc1SLuigi Rizzo 		case PACKET_TAG_IPFORWARD:
1752b25acc1SLuigi Rizzo 			args.next_hop = (struct sockaddr_in *)m0->m_data;
1762b25acc1SLuigi Rizzo 			break;
1772b25acc1SLuigi Rizzo 		}
1782b25acc1SLuigi Rizzo         }
1792b25acc1SLuigi Rizzo 	m = m0;
1802b25acc1SLuigi Rizzo 
1812b25acc1SLuigi Rizzo 	KASSERT(!m || (m->m_flags & M_PKTHDR) != 0, ("ip_output: no HDR"));
1822b25acc1SLuigi Rizzo 
1832b25acc1SLuigi Rizzo 	KASSERT(ro != NULL, ("ip_output: no route, proto %d",
1842b25acc1SLuigi Rizzo 	    mtod(m, struct ip *)->ip_p));
1852b25acc1SLuigi Rizzo 
186d0a98d79SYoshinobu Inoue #ifdef IPSEC
187686cdd19SJun-ichiro itojun Hagino 	so = ipsec_getsocket(m);
18833841545SHajimu UMEMOTO 	(void)ipsec_setsocket(m, NULL);
189d0a98d79SYoshinobu Inoue #endif
1902b25acc1SLuigi Rizzo 	if (args.rule != NULL)	{ /* dummynet already saw us */
191b715f178SLuigi Rizzo 		ip = mtod(m, struct ip *);
192b715f178SLuigi Rizzo 		hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
1939a10980eSJonathan Lemon 		ia = ifatoia(ro->ro_rt->rt_ifa);
194b715f178SLuigi Rizzo 		goto sendit;
1952b25acc1SLuigi Rizzo 	}
196db40007dSAndrew R. Reiter 
197df8bae1dSRodney W. Grimes 	if (opt) {
198df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
199df8bae1dSRodney W. Grimes 		hlen = len;
200df8bae1dSRodney W. Grimes 	}
201df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
2022b25acc1SLuigi Rizzo 	pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
2033efc3014SJulian Elischer 
204df8bae1dSRodney W. Grimes 	/*
205df8bae1dSRodney W. Grimes 	 * Fill in IP header.
206df8bae1dSRodney W. Grimes 	 */
207df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
2089c9137eaSGarrett Wollman 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
209df8bae1dSRodney W. Grimes 		ip->ip_off &= IP_DF;
21064dddc18SKris Kennaway #ifdef RANDOM_IP_ID
21164dddc18SKris Kennaway 		ip->ip_id = ip_randomid();
21264dddc18SKris Kennaway #else
213e30177e0SRuslan Ermilov 		ip->ip_id = htons(ip_id++);
21464dddc18SKris Kennaway #endif
215df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
216df8bae1dSRodney W. Grimes 	} else {
2179c9137eaSGarrett Wollman 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
218df8bae1dSRodney W. Grimes 	}
2199c9137eaSGarrett Wollman 
220df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
221df8bae1dSRodney W. Grimes 	/*
222df8bae1dSRodney W. Grimes 	 * If there is a cached route,
223df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
224df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
225a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
226a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
227df8bae1dSRodney W. Grimes 	 */
228df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
229a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
2303efc3014SJulian Elischer 			  dst->sin_addr.s_addr != pkt_dst.s_addr)) {
231df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
232df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
233df8bae1dSRodney W. Grimes 	}
234df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
235a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
236df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
237df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2383efc3014SJulian Elischer 		dst->sin_addr = pkt_dst;
239df8bae1dSRodney W. Grimes 	}
240df8bae1dSRodney W. Grimes 	/*
241df8bae1dSRodney W. Grimes 	 * If routing to interface only,
242df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
243df8bae1dSRodney W. Grimes 	 */
244df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
245df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
246df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
247df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
248df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
249df8bae1dSRodney W. Grimes 			goto bad;
250df8bae1dSRodney W. Grimes 		}
251df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
252df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2539f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2543afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
25538c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2563afefa39SDaniel C. Sobral 		/*
25738c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
25838c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2593afefa39SDaniel C. Sobral 		 */
26038c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
26138c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
26238c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
263df8bae1dSRodney W. Grimes 	} else {
2642c17fe93SGarrett Wollman 		/*
265f2c2962eSRuslan Ermilov 		 * If this is the case, we probably don't want to allocate
266f2c2962eSRuslan Ermilov 		 * a protocol-cloned route since we didn't get one from the
267f2c2962eSRuslan Ermilov 		 * ULP.  This lets TCP do its thing, while not burdening
268f2c2962eSRuslan Ermilov 		 * forwarding or ICMP with the overhead of cloning a route.
269f2c2962eSRuslan Ermilov 		 * Of course, we still want to do any cloning requested by
270f2c2962eSRuslan Ermilov 		 * the link layer, as this is probably required in all cases
271f2c2962eSRuslan Ermilov 		 * for correct operation (as it is for ARP).
2722c17fe93SGarrett Wollman 		 */
273df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
2742c17fe93SGarrett Wollman 			rtalloc_ign(ro, RTF_PRCLONING);
275df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
276df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
277df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
278df8bae1dSRodney W. Grimes 			goto bad;
279df8bae1dSRodney W. Grimes 		}
280df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
281df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
282df8bae1dSRodney W. Grimes 		ro->ro_rt->rt_use++;
283df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
284f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2859f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
286f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2879f9b3dc4SGarrett Wollman 		else
2889f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
289df8bae1dSRodney W. Grimes 	}
2903efc3014SJulian Elischer 	if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
291df8bae1dSRodney W. Grimes 		struct in_multi *inm;
292df8bae1dSRodney W. Grimes 
293df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
294df8bae1dSRodney W. Grimes 		/*
295df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
296df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
297df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
298df8bae1dSRodney W. Grimes 		 */
299df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
300df8bae1dSRodney W. Grimes 		/*
301df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
302df8bae1dSRodney W. Grimes 		 */
303df8bae1dSRodney W. Grimes 		if (imo != NULL) {
304df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
3051c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3061c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
3071c5de19aSGarrett Wollman 				    ip_mcast_src(imo->imo_multicast_vif);
308df8bae1dSRodney W. Grimes 		} else
309df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
310df8bae1dSRodney W. Grimes 		/*
311df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
312df8bae1dSRodney W. Grimes 		 */
3131c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
314df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
315df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
316df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
317df8bae1dSRodney W. Grimes 				goto bad;
318df8bae1dSRodney W. Grimes 			}
3191c5de19aSGarrett Wollman 		}
320df8bae1dSRodney W. Grimes 		/*
321df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
322df8bae1dSRodney W. Grimes 		 * of outgoing interface.
323df8bae1dSRodney W. Grimes 		 */
324df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
32538c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
32638c1bc35SRuslan Ermilov 			if (ia != NULL)
32738c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
328df8bae1dSRodney W. Grimes 		}
329df8bae1dSRodney W. Grimes 
33092bdb2faSBill Fenner 		if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
33192bdb2faSBill Fenner 			/*
33292bdb2faSBill Fenner 			 * XXX
33392bdb2faSBill Fenner 			 * delayed checksums are not currently
33492bdb2faSBill Fenner 			 * compatible with IP multicast routing
33592bdb2faSBill Fenner 			 */
33692bdb2faSBill Fenner 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
33792bdb2faSBill Fenner 				in_delayed_cksum(m);
33892bdb2faSBill Fenner 				m->m_pkthdr.csum_flags &=
33992bdb2faSBill Fenner 					~CSUM_DELAY_DATA;
34092bdb2faSBill Fenner 			}
34192bdb2faSBill Fenner 		}
3423efc3014SJulian Elischer 		IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
343df8bae1dSRodney W. Grimes 		if (inm != NULL &&
344df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
345df8bae1dSRodney W. Grimes 			/*
346df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
347df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
348df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
349df8bae1dSRodney W. Grimes 			 */
35086b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
351df8bae1dSRodney W. Grimes 		}
352df8bae1dSRodney W. Grimes 		else {
353df8bae1dSRodney W. Grimes 			/*
354df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
355df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
356df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
357df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
358df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
359df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
360df8bae1dSRodney W. Grimes 			 *
361df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
362df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
363df8bae1dSRodney W. Grimes 			 * if necessary.
364df8bae1dSRodney W. Grimes 			 */
365df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
366f0068c4aSGarrett Wollman 				/*
367f0068c4aSGarrett Wollman 				 * Check if rsvp daemon is running. If not, don't
368f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
369f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
370f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
371f0068c4aSGarrett Wollman 				 */
372b124e4f2SGarrett Wollman 				if (!rsvp_on)
373f0068c4aSGarrett Wollman 				  imo = NULL;
374f0068c4aSGarrett Wollman 				if (ip_mforward(ip, ifp, m, imo) != 0) {
375df8bae1dSRodney W. Grimes 					m_freem(m);
376df8bae1dSRodney W. Grimes 					goto done;
377df8bae1dSRodney W. Grimes 				}
378df8bae1dSRodney W. Grimes 			}
379df8bae1dSRodney W. Grimes 		}
3805e9ae478SGarrett Wollman 
381df8bae1dSRodney W. Grimes 		/*
382df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
383df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
384df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
385df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
386df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
387df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
388df8bae1dSRodney W. Grimes 		 */
389f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
390df8bae1dSRodney W. Grimes 			m_freem(m);
391df8bae1dSRodney W. Grimes 			goto done;
392df8bae1dSRodney W. Grimes 		}
393df8bae1dSRodney W. Grimes 
394df8bae1dSRodney W. Grimes 		goto sendit;
395df8bae1dSRodney W. Grimes 	}
396df8bae1dSRodney W. Grimes #ifndef notdef
397df8bae1dSRodney W. Grimes 	/*
3982b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
3992b25acc1SLuigi Rizzo 	 * of the outoing interface. In case, keep note we did that, so
4002b25acc1SLuigi Rizzo 	 * if the the firewall changes the next-hop causing the output
4012b25acc1SLuigi Rizzo 	 * interface to change, we can fix that.
402df8bae1dSRodney W. Grimes 	 */
403f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
40438c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
40538c1bc35SRuslan Ermilov 		if (ia != NULL) {
406df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
4072b25acc1SLuigi Rizzo 			src_was_INADDR_ANY = 1;
408f9e354dfSJulian Elischer 		}
40938c1bc35SRuslan Ermilov 	}
410f9e354dfSJulian Elischer #endif /* notdef */
411df8bae1dSRodney W. Grimes 	/*
412b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
413b5390296SDavid Greenman 	 *      the packet or packet fragments
414b5390296SDavid Greenman 	 */
415b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
416b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
417b5390296SDavid Greenman 			error = ENOBUFS;
41835609d45SJonathan Lemon 			ipstat.ips_odropped++;
419b5390296SDavid Greenman 			goto bad;
420b5390296SDavid Greenman 	}
421b5390296SDavid Greenman 
422b5390296SDavid Greenman 	/*
423df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4248c3f5566SRuslan Ermilov 	 * verify user is allowed to send
425df8bae1dSRodney W. Grimes 	 * such a packet.
426df8bae1dSRodney W. Grimes 	 */
4279f9b3dc4SGarrett Wollman 	if (isbroadcast) {
428df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
429df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
430df8bae1dSRodney W. Grimes 			goto bad;
431df8bae1dSRodney W. Grimes 		}
432df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
433df8bae1dSRodney W. Grimes 			error = EACCES;
434df8bae1dSRodney W. Grimes 			goto bad;
435df8bae1dSRodney W. Grimes 		}
436df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
437df8bae1dSRodney W. Grimes 		if ((u_short)ip->ip_len > ifp->if_mtu) {
438df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
439df8bae1dSRodney W. Grimes 			goto bad;
440df8bae1dSRodney W. Grimes 		}
441df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4429f9b3dc4SGarrett Wollman 	} else {
443df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4449f9b3dc4SGarrett Wollman 	}
445df8bae1dSRodney W. Grimes 
446f1743588SDarren Reed sendit:
44733841545SHajimu UMEMOTO #ifdef IPSEC
44833841545SHajimu UMEMOTO 	/* get SP for this packet */
44933841545SHajimu UMEMOTO 	if (so == NULL)
45033841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
45133841545SHajimu UMEMOTO 	else
45233841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
45333841545SHajimu UMEMOTO 
45433841545SHajimu UMEMOTO 	if (sp == NULL) {
45533841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
45633841545SHajimu UMEMOTO 		goto bad;
45733841545SHajimu UMEMOTO 	}
45833841545SHajimu UMEMOTO 
45933841545SHajimu UMEMOTO 	error = 0;
46033841545SHajimu UMEMOTO 
46133841545SHajimu UMEMOTO 	/* check policy */
46233841545SHajimu UMEMOTO 	switch (sp->policy) {
46333841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
46433841545SHajimu UMEMOTO 		/*
46533841545SHajimu UMEMOTO 		 * This packet is just discarded.
46633841545SHajimu UMEMOTO 		 */
46733841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
46833841545SHajimu UMEMOTO 		goto bad;
46933841545SHajimu UMEMOTO 
47033841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
47133841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
47233841545SHajimu UMEMOTO 		/* no need to do IPsec. */
47333841545SHajimu UMEMOTO 		goto skip_ipsec;
47433841545SHajimu UMEMOTO 
47533841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
47633841545SHajimu UMEMOTO 		if (sp->req == NULL) {
47733841545SHajimu UMEMOTO 			/* acquire a policy */
47833841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
47933841545SHajimu UMEMOTO 			goto bad;
48033841545SHajimu UMEMOTO 		}
48133841545SHajimu UMEMOTO 		break;
48233841545SHajimu UMEMOTO 
48333841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
48433841545SHajimu UMEMOTO 	default:
48533841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
48633841545SHajimu UMEMOTO 	}
48733841545SHajimu UMEMOTO     {
48833841545SHajimu UMEMOTO 	struct ipsec_output_state state;
48933841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
49033841545SHajimu UMEMOTO 	state.m = m;
49133841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
49233841545SHajimu UMEMOTO 		state.ro = &iproute;
49333841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
49433841545SHajimu UMEMOTO 	} else
49533841545SHajimu UMEMOTO 		state.ro = ro;
49633841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
49733841545SHajimu UMEMOTO 
49833841545SHajimu UMEMOTO 	ip->ip_sum = 0;
49933841545SHajimu UMEMOTO 
50033841545SHajimu UMEMOTO 	/*
50133841545SHajimu UMEMOTO 	 * XXX
50233841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
50333841545SHajimu UMEMOTO 	 */
50433841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
50533841545SHajimu UMEMOTO 		in_delayed_cksum(m);
50633841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
50733841545SHajimu UMEMOTO 	}
50833841545SHajimu UMEMOTO 
509fd8e4ebcSMike Barcroft 	ip->ip_len = htons(ip->ip_len);
510fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
51133841545SHajimu UMEMOTO 
51233841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
51333841545SHajimu UMEMOTO 
51433841545SHajimu UMEMOTO 	m = state.m;
51533841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
51633841545SHajimu UMEMOTO 		/*
51733841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
51833841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
51933841545SHajimu UMEMOTO 		 */
52033841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
52133841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
52233841545SHajimu UMEMOTO 			ro = state.ro;
52333841545SHajimu UMEMOTO 		}
52433841545SHajimu UMEMOTO 	} else
52533841545SHajimu UMEMOTO 		ro = state.ro;
52633841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
52733841545SHajimu UMEMOTO 	if (error) {
52833841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
52933841545SHajimu UMEMOTO 		m0 = NULL;
53033841545SHajimu UMEMOTO 		switch (error) {
53133841545SHajimu UMEMOTO 		case EHOSTUNREACH:
53233841545SHajimu UMEMOTO 		case ENETUNREACH:
53333841545SHajimu UMEMOTO 		case EMSGSIZE:
53433841545SHajimu UMEMOTO 		case ENOBUFS:
53533841545SHajimu UMEMOTO 		case ENOMEM:
53633841545SHajimu UMEMOTO 			break;
53733841545SHajimu UMEMOTO 		default:
53833841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
53933841545SHajimu UMEMOTO 			/*fall through*/
54033841545SHajimu UMEMOTO 		case ENOENT:
54133841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
54233841545SHajimu UMEMOTO 			error = 0;
54333841545SHajimu UMEMOTO 			break;
54433841545SHajimu UMEMOTO 		}
54533841545SHajimu UMEMOTO 		goto bad;
54633841545SHajimu UMEMOTO 	}
54733841545SHajimu UMEMOTO     }
54833841545SHajimu UMEMOTO 
54933841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
55033841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
55133841545SHajimu UMEMOTO #ifdef _IP_VHL
55233841545SHajimu UMEMOTO 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
55333841545SHajimu UMEMOTO #else
55433841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
55533841545SHajimu UMEMOTO #endif
55633841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
55733841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
55833841545SHajimu UMEMOTO 			printf("ip_output: "
55933841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
56033841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
56133841545SHajimu UMEMOTO 			goto bad;
56233841545SHajimu UMEMOTO 		}
56333841545SHajimu UMEMOTO 	} else {
56433841545SHajimu UMEMOTO 		ia = ifatoia(ro->ro_rt->rt_ifa);
56533841545SHajimu UMEMOTO 		ifp = ro->ro_rt->rt_ifp;
56633841545SHajimu UMEMOTO 	}
56733841545SHajimu UMEMOTO 
56833841545SHajimu UMEMOTO 	/* make it flipped, again. */
569fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
570fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
57133841545SHajimu UMEMOTO skip_ipsec:
57233841545SHajimu UMEMOTO #endif /*IPSEC*/
57333841545SHajimu UMEMOTO 
574fed1c7e9SSøren Schmidt 	/*
575fed1c7e9SSøren Schmidt 	 * IpHack's section.
576fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
577e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
578fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
579fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
580fed1c7e9SSøren Schmidt 	 */
581c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
582c4ac87eaSDarren Reed 	/*
583c4ac87eaSDarren Reed 	 * Run through list of hooks for output packets.
584c4ac87eaSDarren Reed 	 */
585c4ac87eaSDarren Reed 	m1 = m;
586c4ac87eaSDarren Reed 	pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
587fc2ffbe6SPoul-Henning Kamp 	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
588c4ac87eaSDarren Reed 		if (pfh->pfil_func) {
589c4ac87eaSDarren Reed 			rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
590c4ac87eaSDarren Reed 			if (rv) {
591c4ac87eaSDarren Reed 				error = EHOSTUNREACH;
5921ee25934SPeter Wemm 				goto done;
5931ee25934SPeter Wemm 			}
594c4ac87eaSDarren Reed 			m = m1;
595c4ac87eaSDarren Reed 			if (m == NULL)
596c4ac87eaSDarren Reed 				goto done;
597c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
598c4ac87eaSDarren Reed 		}
599c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
600fed1c7e9SSøren Schmidt 
601df8bae1dSRodney W. Grimes 	/*
602e7319babSPoul-Henning Kamp 	 * Check with the firewall...
6033efc3014SJulian Elischer 	 * but not if we are already being fwd'd from a firewall.
604e7319babSPoul-Henning Kamp 	 */
6052b25acc1SLuigi Rizzo 	if (fw_enable && IPFW_LOADED && !args.next_hop) {
6069de9737fSPeter Wemm 		struct sockaddr_in *old = dst;
607b715f178SLuigi Rizzo 
6082b25acc1SLuigi Rizzo 		args.m = m;
6092b25acc1SLuigi Rizzo 		args.next_hop = dst;
6102b25acc1SLuigi Rizzo 		args.oif = ifp;
6112b25acc1SLuigi Rizzo 		off = ip_fw_chk_ptr(&args);
6122b25acc1SLuigi Rizzo 		m = args.m;
6132b25acc1SLuigi Rizzo 		dst = args.next_hop;
6142b25acc1SLuigi Rizzo 
615b715f178SLuigi Rizzo                 /*
616b715f178SLuigi Rizzo                  * On return we must do the following:
617507b4b54SLuigi Rizzo                  * m == NULL         -> drop the pkt (old interface, deprecated)
618aa1f5daaSJonathan Lemon                  * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
619b715f178SLuigi Rizzo                  * 1<=off<= 0xffff		-> DIVERT
620aa1f5daaSJonathan Lemon                  * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
621aa1f5daaSJonathan Lemon                  * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
622b715f178SLuigi Rizzo                  * dst != old			-> IPFIREWALL_FORWARD
623b715f178SLuigi Rizzo                  * off==0, dst==old		-> accept
624aa1f5daaSJonathan Lemon                  * If some of the above modules are not compiled in, then
625b715f178SLuigi Rizzo                  * we should't have to check the corresponding condition
626b715f178SLuigi Rizzo                  * (because the ipfw control socket should not accept
627b715f178SLuigi Rizzo                  * unsupported rules), but better play safe and drop
628b715f178SLuigi Rizzo                  * packets in case of doubt.
629b715f178SLuigi Rizzo                  */
630d60315beSLuigi Rizzo 		if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
631507b4b54SLuigi Rizzo 			if (m)
632507b4b54SLuigi Rizzo 				m_freem(m);
633507b4b54SLuigi Rizzo 			error = EACCES;
634507b4b54SLuigi Rizzo 			goto done;
635507b4b54SLuigi Rizzo 		}
636d60315beSLuigi Rizzo 		ip = mtod(m, struct ip *);
637b715f178SLuigi Rizzo 		if (off == 0 && dst == old)		/* common case */
638b715f178SLuigi Rizzo 			goto pass;
6397b109fa4SLuigi Rizzo                 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
640b715f178SLuigi Rizzo 			/*
641b715f178SLuigi Rizzo 			 * pass the pkt to dummynet. Need to include
642f0a53591SLuigi Rizzo 			 * pipe number, m, ifp, ro, dst because these are
643b715f178SLuigi Rizzo 			 * not recomputed in the next pass.
644b715f178SLuigi Rizzo 			 * All other parameters have been already used and
645b715f178SLuigi Rizzo 			 * so they are not needed anymore.
646b715f178SLuigi Rizzo 			 * XXX note: if the ifp or ro entry are deleted
647b715f178SLuigi Rizzo 			 * while a pkt is in dummynet, we are in trouble!
648b715f178SLuigi Rizzo 			 */
6492b25acc1SLuigi Rizzo 			args.ro = ro;
6502b25acc1SLuigi Rizzo 			args.dst = dst;
6512b25acc1SLuigi Rizzo 			args.flags = flags;
6522b25acc1SLuigi Rizzo 
6532b25acc1SLuigi Rizzo 			error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT,
6542b25acc1SLuigi Rizzo 				&args);
655b715f178SLuigi Rizzo 			goto done;
656b715f178SLuigi Rizzo 		}
657b715f178SLuigi Rizzo #ifdef IPDIVERT
6588948e4baSArchie Cobbs 		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
6598948e4baSArchie Cobbs 			struct mbuf *clone = NULL;
6608948e4baSArchie Cobbs 
6618948e4baSArchie Cobbs 			/* Clone packet if we're doing a 'tee' */
6628948e4baSArchie Cobbs 			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
6638948e4baSArchie Cobbs 				clone = m_dup(m, M_DONTWAIT);
6648948e4baSArchie Cobbs 
665ea53ecd9SJonathan Lemon 			/*
666ea53ecd9SJonathan Lemon 			 * XXX
667ea53ecd9SJonathan Lemon 			 * delayed checksums are not currently compatible
668ea53ecd9SJonathan Lemon 			 * with divert sockets.
669ea53ecd9SJonathan Lemon 			 */
670ea53ecd9SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
671ea53ecd9SJonathan Lemon 				in_delayed_cksum(m);
672ea53ecd9SJonathan Lemon 				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
673ea53ecd9SJonathan Lemon 			}
674ea53ecd9SJonathan Lemon 
6758948e4baSArchie Cobbs 			/* Restore packet header fields to original values */
676fd8e4ebcSMike Barcroft 			ip->ip_len = htons(ip->ip_len);
677fd8e4ebcSMike Barcroft 			ip->ip_off = htons(ip->ip_off);
6788948e4baSArchie Cobbs 
6798948e4baSArchie Cobbs 			/* Deliver packet to divert input routine */
6802b25acc1SLuigi Rizzo 			divert_packet(m, 0, off & 0xffff, args.divert_rule);
6818948e4baSArchie Cobbs 
6828948e4baSArchie Cobbs 			/* If 'tee', continue with original packet */
6838948e4baSArchie Cobbs 			if (clone != NULL) {
6848948e4baSArchie Cobbs 				m = clone;
6858948e4baSArchie Cobbs 				ip = mtod(m, struct ip *);
6868948e4baSArchie Cobbs 				goto pass;
6878948e4baSArchie Cobbs 			}
688b715f178SLuigi Rizzo 			goto done;
689b715f178SLuigi Rizzo 		}
690b715f178SLuigi Rizzo #endif
691b715f178SLuigi Rizzo 
6922b25acc1SLuigi Rizzo 		/* IPFIREWALL_FORWARD */
6932b25acc1SLuigi Rizzo 		/*
6942b25acc1SLuigi Rizzo 		 * Check dst to make sure it is directly reachable on the
695f9e354dfSJulian Elischer 		 * interface we previously thought it was.
696f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
697f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
698f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
699f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
700f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
701f9e354dfSJulian Elischer 		 * And I'm babbling.
702f9e354dfSJulian Elischer 		 */
7032b25acc1SLuigi Rizzo 		if (off == 0 && old != dst) { /* FORWARD, dst has changed */
7042b25acc1SLuigi Rizzo #if 0
7052b25acc1SLuigi Rizzo 			/*
7062b25acc1SLuigi Rizzo 			 * XXX To improve readability, this block should be
7072b25acc1SLuigi Rizzo 			 * changed into a function call as below:
7082b25acc1SLuigi Rizzo 			 */
7092b25acc1SLuigi Rizzo 			error = ip_ipforward(&m, &dst, &ifp);
7102b25acc1SLuigi Rizzo 			if (error)
7112b25acc1SLuigi Rizzo 				goto bad;
7122b25acc1SLuigi Rizzo 			if (m == NULL) /* ip_input consumed the mbuf */
7132b25acc1SLuigi Rizzo 				goto done;
7142b25acc1SLuigi Rizzo #else
715f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
716f9e354dfSJulian Elischer 
7172b25acc1SLuigi Rizzo 			/*
7182b25acc1SLuigi Rizzo 			 * XXX sro_fwd below is static, and a pointer
7192b25acc1SLuigi Rizzo 			 * to it gets passed to routines downstream.
7202b25acc1SLuigi Rizzo 			 * This could have surprisingly bad results in
7212b25acc1SLuigi Rizzo 			 * practice, because its content is overwritten
7222b25acc1SLuigi Rizzo 			 * by subsequent packets.
7232b25acc1SLuigi Rizzo 			 */
724f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
7252b25acc1SLuigi Rizzo 			static struct route sro_fwd;
7262b25acc1SLuigi Rizzo 			struct route *ro_fwd = &sro_fwd;
7272b25acc1SLuigi Rizzo 
7282b25acc1SLuigi Rizzo #if 0
7292b25acc1SLuigi Rizzo 			print_ip("IPFIREWALL_FORWARD: New dst ip: ",
7302b25acc1SLuigi Rizzo 			    dst->sin_addr, "\n");
731f9e354dfSJulian Elischer #endif
7322b25acc1SLuigi Rizzo 
733f9e354dfSJulian Elischer 			/*
734f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
7352b25acc1SLuigi Rizzo 			 * to a local socket. If so, then we should somehow
736f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
737f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
738f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
739f9e354dfSJulian Elischer 			 * you want to forward until they are being output
740f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
741f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
742f9e354dfSJulian Elischer 			 * we would not be able to control what happens
743f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
744f9e354dfSJulian Elischer 			 * it is done through a ISR.
745f9e354dfSJulian Elischer 			 */
746ca925d9cSJonathan Lemon 			LIST_FOREACH(ia,
747ca925d9cSJonathan Lemon 			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
748f9e354dfSJulian Elischer 				/*
749f9e354dfSJulian Elischer 				 * If the addr to forward to is one
750f9e354dfSJulian Elischer 				 * of ours, we pretend to
751f9e354dfSJulian Elischer 				 * be the destination for this packet.
752f9e354dfSJulian Elischer 				 */
753f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
754f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
755f9e354dfSJulian Elischer 					break;
756f9e354dfSJulian Elischer 			}
7572b25acc1SLuigi Rizzo 			if (ia) {	/* tell ip_input "dont filter" */
7582b25acc1SLuigi Rizzo 				struct m_hdr tag;
7592b25acc1SLuigi Rizzo 
7602b25acc1SLuigi Rizzo 				tag.mh_type = MT_TAG;
7612b25acc1SLuigi Rizzo 				tag.mh_flags = PACKET_TAG_IPFORWARD;
7622b25acc1SLuigi Rizzo 				tag.mh_data = (caddr_t)args.next_hop;
7632b25acc1SLuigi Rizzo 				tag.mh_next = m;
7642b25acc1SLuigi Rizzo 
765f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
766f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
76720c822f3SJonathan Lemon 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
76820c822f3SJonathan Lemon 					m->m_pkthdr.csum_flags |=
76920c822f3SJonathan Lemon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
77020c822f3SJonathan Lemon 					m0->m_pkthdr.csum_data = 0xffff;
77120c822f3SJonathan Lemon 				}
77220c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
77320c822f3SJonathan Lemon 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
774fd8e4ebcSMike Barcroft 				ip->ip_len = htons(ip->ip_len);
775fd8e4ebcSMike Barcroft 				ip->ip_off = htons(ip->ip_off);
7762b25acc1SLuigi Rizzo 				ip_input((struct mbuf *)&tag);
777f9e354dfSJulian Elischer 				goto done;
778f9e354dfSJulian Elischer 			}
779f9e354dfSJulian Elischer 			/* Some of the logic for this was
780f9e354dfSJulian Elischer 			 * nicked from above.
781f9e354dfSJulian Elischer 			 *
782f9e354dfSJulian Elischer 			 * This rewrites the cached route in a local PCB.
783f9e354dfSJulian Elischer 			 * Is this what we want to do?
784f9e354dfSJulian Elischer 			 */
785f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
786f9e354dfSJulian Elischer 
787f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
788f9e354dfSJulian Elischer 			rtalloc_ign(ro_fwd, RTF_PRCLONING);
789f9e354dfSJulian Elischer 
790f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt == 0) {
791f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
792f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
793f9e354dfSJulian Elischer 				goto bad;
794f9e354dfSJulian Elischer 			}
795f9e354dfSJulian Elischer 
796f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
797f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
798f9e354dfSJulian Elischer 			ro_fwd->ro_rt->rt_use++;
799f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
8002b25acc1SLuigi Rizzo 				dst = (struct sockaddr_in *)
8012b25acc1SLuigi Rizzo 					ro_fwd->ro_rt->rt_gateway;
802f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
803f9e354dfSJulian Elischer 				isbroadcast =
804f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
805f9e354dfSJulian Elischer 			else
806f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
8073f9e3122SYaroslav Tykhiy 			if (ro->ro_rt)
808f9e354dfSJulian Elischer 				RTFREE(ro->ro_rt);
809f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
810f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
811f9e354dfSJulian Elischer 
8122b25acc1SLuigi Rizzo #endif	/* ... block to be put into a function */
813f9e354dfSJulian Elischer 			/*
814f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
815f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
816f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
817f9e354dfSJulian Elischer 			 */
8182b25acc1SLuigi Rizzo 			if (src_was_INADDR_ANY)
819f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
820b715f178SLuigi Rizzo 			goto pass ;
821f9e354dfSJulian Elischer 		}
8222b25acc1SLuigi Rizzo 
823b715f178SLuigi Rizzo                 /*
824b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
825b715f178SLuigi Rizzo                  * we have to drop the pkt
826b715f178SLuigi Rizzo                  */
827b715f178SLuigi Rizzo 		m_freem(m);
828b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
829b715f178SLuigi Rizzo                 goto done;
83093e0e116SJulian Elischer 	}
831e7319babSPoul-Henning Kamp 
832b715f178SLuigi Rizzo pass:
83351c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
83451c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
83551c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
83651c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
83751c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
83851c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
83951c8ec4aSRuslan Ermilov 			goto bad;
84051c8ec4aSRuslan Ermilov 		}
84151c8ec4aSRuslan Ermilov 	}
84251c8ec4aSRuslan Ermilov 
843206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
844206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
845db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
846db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
847db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
848db4f9cc7SJonathan Lemon 	}
849206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
850db4f9cc7SJonathan Lemon 
851e7319babSPoul-Henning Kamp 	/*
852db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
853db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
854df8bae1dSRodney W. Grimes 	 */
855db4f9cc7SJonathan Lemon 	if ((u_short)ip->ip_len <= ifp->if_mtu ||
856db4f9cc7SJonathan Lemon 	    ifp->if_hwassist & CSUM_FRAGMENT) {
857fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
858fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
859df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
860db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
8619c9137eaSGarrett Wollman 			if (ip->ip_vhl == IP_VHL_BORING) {
8629c9137eaSGarrett Wollman 				ip->ip_sum = in_cksum_hdr(ip);
8639c9137eaSGarrett Wollman 			} else {
864df8bae1dSRodney W. Grimes 				ip->ip_sum = in_cksum(m, hlen);
8659c9137eaSGarrett Wollman 			}
866db4f9cc7SJonathan Lemon 		}
8675da9f8faSJosef Karthauser 
8685da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
86938c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
8705da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
8715da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
8725da9f8faSJosef Karthauser 		}
8735da9f8faSJosef Karthauser 
87433841545SHajimu UMEMOTO #ifdef IPSEC
87533841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
87633841545SHajimu UMEMOTO 		ipsec_delaux(m);
87733841545SHajimu UMEMOTO #endif
87833841545SHajimu UMEMOTO 
879df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
880df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
881df8bae1dSRodney W. Grimes 		goto done;
882df8bae1dSRodney W. Grimes 	}
883df8bae1dSRodney W. Grimes 	/*
884df8bae1dSRodney W. Grimes 	 * Too large for interface; fragment if possible.
885df8bae1dSRodney W. Grimes 	 * Must be able to put at least 8 bytes per fragment.
886df8bae1dSRodney W. Grimes 	 */
887df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
888df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
8893d1f141bSGarrett Wollman 		/*
8903d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
8913d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
8923d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
8933d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
8943d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
8953d1f141bSGarrett Wollman 		 */
8963d1f141bSGarrett Wollman 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
8973d1f141bSGarrett Wollman 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
8983d1f141bSGarrett Wollman 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
8993d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
9003d1f141bSGarrett Wollman 		}
901df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
902df8bae1dSRodney W. Grimes 		goto bad;
903df8bae1dSRodney W. Grimes 	}
904df8bae1dSRodney W. Grimes 	len = (ifp->if_mtu - hlen) &~ 7;
905df8bae1dSRodney W. Grimes 	if (len < 8) {
906df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
907df8bae1dSRodney W. Grimes 		goto bad;
908df8bae1dSRodney W. Grimes 	}
909df8bae1dSRodney W. Grimes 
910db4f9cc7SJonathan Lemon 	/*
911db4f9cc7SJonathan Lemon 	 * if the interface will not calculate checksums on
912db4f9cc7SJonathan Lemon 	 * fragmented packets, then do it here.
913db4f9cc7SJonathan Lemon 	 */
914db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
915db4f9cc7SJonathan Lemon 	    (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
916db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
917db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
918db4f9cc7SJonathan Lemon 	}
919db4f9cc7SJonathan Lemon 
920df8bae1dSRodney W. Grimes     {
921df8bae1dSRodney W. Grimes 	int mhlen, firstlen = len;
922df8bae1dSRodney W. Grimes 	struct mbuf **mnext = &m->m_nextpkt;
923db4f9cc7SJonathan Lemon 	int nfrags = 1;
924df8bae1dSRodney W. Grimes 
925df8bae1dSRodney W. Grimes 	/*
926df8bae1dSRodney W. Grimes 	 * Loop through length of segment after first fragment,
927df8bae1dSRodney W. Grimes 	 * make new header and copy data of each part and link onto chain.
928df8bae1dSRodney W. Grimes 	 */
929df8bae1dSRodney W. Grimes 	m0 = m;
930df8bae1dSRodney W. Grimes 	mhlen = sizeof (struct ip);
931df8bae1dSRodney W. Grimes 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
932df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
933df8bae1dSRodney W. Grimes 		if (m == 0) {
934df8bae1dSRodney W. Grimes 			error = ENOBUFS;
935df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
936df8bae1dSRodney W. Grimes 			goto sendorfree;
937df8bae1dSRodney W. Grimes 		}
938db4f9cc7SJonathan Lemon 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
939df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
940df8bae1dSRodney W. Grimes 		mhip = mtod(m, struct ip *);
941df8bae1dSRodney W. Grimes 		*mhip = *ip;
942df8bae1dSRodney W. Grimes 		if (hlen > sizeof (struct ip)) {
943df8bae1dSRodney W. Grimes 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9449c9137eaSGarrett Wollman 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
945df8bae1dSRodney W. Grimes 		}
946df8bae1dSRodney W. Grimes 		m->m_len = mhlen;
947cc22c7a7SRuslan Ermilov 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
948df8bae1dSRodney W. Grimes 		if (off + len >= (u_short)ip->ip_len)
949df8bae1dSRodney W. Grimes 			len = (u_short)ip->ip_len - off;
950df8bae1dSRodney W. Grimes 		else
951df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
952df8bae1dSRodney W. Grimes 		mhip->ip_len = htons((u_short)(len + mhlen));
953df8bae1dSRodney W. Grimes 		m->m_next = m_copy(m0, off, len);
954df8bae1dSRodney W. Grimes 		if (m->m_next == 0) {
955df8bae1dSRodney W. Grimes 			(void) m_free(m);
956df8bae1dSRodney W. Grimes 			error = ENOBUFS;	/* ??? */
957df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
958df8bae1dSRodney W. Grimes 			goto sendorfree;
959df8bae1dSRodney W. Grimes 		}
960df8bae1dSRodney W. Grimes 		m->m_pkthdr.len = mhlen + len;
961df8bae1dSRodney W. Grimes 		m->m_pkthdr.rcvif = (struct ifnet *)0;
962db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
963fd8e4ebcSMike Barcroft 		mhip->ip_off = htons(mhip->ip_off);
964df8bae1dSRodney W. Grimes 		mhip->ip_sum = 0;
965db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
9669c9137eaSGarrett Wollman 			if (mhip->ip_vhl == IP_VHL_BORING) {
967e2184122SBruce Evans 				mhip->ip_sum = in_cksum_hdr(mhip);
9689c9137eaSGarrett Wollman 			} else {
969df8bae1dSRodney W. Grimes 				mhip->ip_sum = in_cksum(m, mhlen);
9709c9137eaSGarrett Wollman 			}
971db4f9cc7SJonathan Lemon 		}
972df8bae1dSRodney W. Grimes 		*mnext = m;
973df8bae1dSRodney W. Grimes 		mnext = &m->m_nextpkt;
974db4f9cc7SJonathan Lemon 		nfrags++;
975df8bae1dSRodney W. Grimes 	}
976db4f9cc7SJonathan Lemon 	ipstat.ips_ofragments += nfrags;
977db4f9cc7SJonathan Lemon 
978db4f9cc7SJonathan Lemon 	/* set first/last markers for fragment chain */
979db4f9cc7SJonathan Lemon 	m->m_flags |= M_LASTFRAG;
980db4f9cc7SJonathan Lemon 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
981db4f9cc7SJonathan Lemon 	m0->m_pkthdr.csum_data = nfrags;
982db4f9cc7SJonathan Lemon 
983df8bae1dSRodney W. Grimes 	/*
984df8bae1dSRodney W. Grimes 	 * Update first fragment by trimming what's been copied out
985df8bae1dSRodney W. Grimes 	 * and updating header, then send each fragment (in order).
986df8bae1dSRodney W. Grimes 	 */
987df8bae1dSRodney W. Grimes 	m = m0;
988df8bae1dSRodney W. Grimes 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
989df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hlen + firstlen;
990df8bae1dSRodney W. Grimes 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
99104287599SRuslan Ermilov 	ip->ip_off |= IP_MF;
992fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
993df8bae1dSRodney W. Grimes 	ip->ip_sum = 0;
994db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_IP) {
9959c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
9969c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
9979c9137eaSGarrett Wollman 		} else {
998df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
9999c9137eaSGarrett Wollman 		}
1000db4f9cc7SJonathan Lemon 	}
1001df8bae1dSRodney W. Grimes sendorfree:
1002df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m0) {
1003df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
1004df8bae1dSRodney W. Grimes 		m->m_nextpkt = 0;
100533841545SHajimu UMEMOTO #ifdef IPSEC
100633841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
100733841545SHajimu UMEMOTO 		ipsec_delaux(m);
100833841545SHajimu UMEMOTO #endif
100907203494SDaniel C. Sobral 		if (error == 0) {
1010fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
101107203494SDaniel C. Sobral 			if (ia != NULL) {
1012fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
1013fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
101407203494SDaniel C. Sobral 			}
1015fe937674SJosef Karthauser 
1016df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
1017df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
1018fe937674SJosef Karthauser 		} else
1019df8bae1dSRodney W. Grimes 			m_freem(m);
1020df8bae1dSRodney W. Grimes 	}
1021df8bae1dSRodney W. Grimes 
1022df8bae1dSRodney W. Grimes 	if (error == 0)
1023df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
1024df8bae1dSRodney W. Grimes     }
1025df8bae1dSRodney W. Grimes done:
10266a800098SYoshinobu Inoue #ifdef IPSEC
10276a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
10286a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
10296a800098SYoshinobu Inoue 		ro->ro_rt = NULL;
10306a800098SYoshinobu Inoue 	}
10316a800098SYoshinobu Inoue 	if (sp != NULL) {
10326a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
10336a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
10346a800098SYoshinobu Inoue 		key_freesp(sp);
10356a800098SYoshinobu Inoue 	}
10366a800098SYoshinobu Inoue #endif /* IPSEC */
1037df8bae1dSRodney W. Grimes 	return (error);
1038df8bae1dSRodney W. Grimes bad:
10393528d68fSBill Paul 	m_freem(m);
1040df8bae1dSRodney W. Grimes 	goto done;
1041df8bae1dSRodney W. Grimes }
1042df8bae1dSRodney W. Grimes 
10431c238475SJonathan Lemon void
1044db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1045db4f9cc7SJonathan Lemon {
1046db4f9cc7SJonathan Lemon 	struct ip *ip;
1047db4f9cc7SJonathan Lemon 	u_short csum, offset;
1048db4f9cc7SJonathan Lemon 
1049db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
1050db4f9cc7SJonathan Lemon 	offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1051db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1052206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1053206a3274SRuslan Ermilov 		csum = 0xffff;
1054db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1055db4f9cc7SJonathan Lemon 
1056db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1057db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1058db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1059db4f9cc7SJonathan Lemon 		/*
1060db4f9cc7SJonathan Lemon 		 * XXX
1061db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1062db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1063db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1064db4f9cc7SJonathan Lemon 		 */
1065db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1066db4f9cc7SJonathan Lemon 	}
1067db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1068db4f9cc7SJonathan Lemon }
1069db4f9cc7SJonathan Lemon 
1070df8bae1dSRodney W. Grimes /*
1071df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1072df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1073df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1074072b9b24SPaul Traina  *
1075072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1076df8bae1dSRodney W. Grimes  */
1077df8bae1dSRodney W. Grimes static struct mbuf *
1078df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen)
1079df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1080df8bae1dSRodney W. Grimes 	struct mbuf *opt;
1081df8bae1dSRodney W. Grimes 	int *phlen;
1082df8bae1dSRodney W. Grimes {
1083df8bae1dSRodney W. Grimes 	register struct ipoption *p = mtod(opt, struct ipoption *);
1084df8bae1dSRodney W. Grimes 	struct mbuf *n;
1085df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
1086df8bae1dSRodney W. Grimes 	unsigned optlen;
1087df8bae1dSRodney W. Grimes 
1088df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
1089df8bae1dSRodney W. Grimes 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
1090df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1091df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1092df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1093df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1094df8bae1dSRodney W. Grimes 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1095df8bae1dSRodney W. Grimes 		if (n == 0)
1096df8bae1dSRodney W. Grimes 			return (m);
10975db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
1098df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1099df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1100df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1101df8bae1dSRodney W. Grimes 		n->m_next = m;
1102df8bae1dSRodney W. Grimes 		m = n;
1103df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1104df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
110594a5d9b6SDavid Greenman 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1106df8bae1dSRodney W. Grimes 	} else {
1107df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1108df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1109df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1110df8bae1dSRodney W. Grimes 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1111df8bae1dSRodney W. Grimes 	}
1112df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
11130453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1114df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
11159c9137eaSGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1116df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1117df8bae1dSRodney W. Grimes 	return (m);
1118df8bae1dSRodney W. Grimes }
1119df8bae1dSRodney W. Grimes 
1120df8bae1dSRodney W. Grimes /*
1121df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1122df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1123df8bae1dSRodney W. Grimes  */
1124beec8214SDarren Reed int
1125df8bae1dSRodney W. Grimes ip_optcopy(ip, jp)
1126df8bae1dSRodney W. Grimes 	struct ip *ip, *jp;
1127df8bae1dSRodney W. Grimes {
1128df8bae1dSRodney W. Grimes 	register u_char *cp, *dp;
1129df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1130df8bae1dSRodney W. Grimes 
1131df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1132df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
11339c9137eaSGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1134df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1135df8bae1dSRodney W. Grimes 		opt = cp[0];
1136df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1137df8bae1dSRodney W. Grimes 			break;
1138df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1139df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1140df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1141df8bae1dSRodney W. Grimes 			optlen = 1;
1142df8bae1dSRodney W. Grimes 			continue;
1143686cdd19SJun-ichiro itojun Hagino 		}
1144db40007dSAndrew R. Reiter 
1145db40007dSAndrew R. Reiter 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1146db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1147df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1148db40007dSAndrew R. Reiter 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1149db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1150db40007dSAndrew R. Reiter 
1151df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1152df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1153df8bae1dSRodney W. Grimes 			optlen = cnt;
1154df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
11550453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1156df8bae1dSRodney W. Grimes 			dp += optlen;
1157df8bae1dSRodney W. Grimes 		}
1158df8bae1dSRodney W. Grimes 	}
1159df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1160df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1161df8bae1dSRodney W. Grimes 	return (optlen);
1162df8bae1dSRodney W. Grimes }
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes /*
1165df8bae1dSRodney W. Grimes  * IP socket option processing.
1166df8bae1dSRodney W. Grimes  */
1167df8bae1dSRodney W. Grimes int
1168cfe8b629SGarrett Wollman ip_ctloutput(so, sopt)
1169df8bae1dSRodney W. Grimes 	struct socket *so;
1170cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1171df8bae1dSRodney W. Grimes {
1172cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1173cfe8b629SGarrett Wollman 	int	error, optval;
1174df8bae1dSRodney W. Grimes 
1175cfe8b629SGarrett Wollman 	error = optval = 0;
1176cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1177cfe8b629SGarrett Wollman 		return (EINVAL);
1178cfe8b629SGarrett Wollman 	}
1179df8bae1dSRodney W. Grimes 
1180cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1181cfe8b629SGarrett Wollman 	case SOPT_SET:
1182cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1183df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1184df8bae1dSRodney W. Grimes #ifdef notyet
1185df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1186df8bae1dSRodney W. Grimes #endif
1187cfe8b629SGarrett Wollman 		{
1188cfe8b629SGarrett Wollman 			struct mbuf *m;
1189cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1190cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1191cfe8b629SGarrett Wollman 				break;
1192cfe8b629SGarrett Wollman 			}
1193b40ce416SJulian Elischer 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1194cfe8b629SGarrett Wollman 			if (m == 0) {
1195cfe8b629SGarrett Wollman 				error = ENOBUFS;
1196cfe8b629SGarrett Wollman 				break;
1197cfe8b629SGarrett Wollman 			}
1198cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1199cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1200cfe8b629SGarrett Wollman 					    m->m_len);
1201cfe8b629SGarrett Wollman 
1202cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1203cfe8b629SGarrett Wollman 					   m));
1204cfe8b629SGarrett Wollman 		}
1205df8bae1dSRodney W. Grimes 
1206df8bae1dSRodney W. Grimes 		case IP_TOS:
1207df8bae1dSRodney W. Grimes 		case IP_TTL:
1208df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1209df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1210df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
121182c23ebaSBill Fenner 		case IP_RECVIF:
12126a800098SYoshinobu Inoue 		case IP_FAITH:
1213cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1214cfe8b629SGarrett Wollman 					    sizeof optval);
1215cfe8b629SGarrett Wollman 			if (error)
1216cfe8b629SGarrett Wollman 				break;
1217df8bae1dSRodney W. Grimes 
1218cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1219df8bae1dSRodney W. Grimes 			case IP_TOS:
1220ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1221df8bae1dSRodney W. Grimes 				break;
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 			case IP_TTL:
1224ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1225df8bae1dSRodney W. Grimes 				break;
1226df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
1227df8bae1dSRodney W. Grimes 	if (optval) \
1228df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
1229df8bae1dSRodney W. Grimes 	else \
1230df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
1231df8bae1dSRodney W. Grimes 
1232df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1233df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1234df8bae1dSRodney W. Grimes 				break;
1235df8bae1dSRodney W. Grimes 
1236df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1237df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1238df8bae1dSRodney W. Grimes 				break;
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1241df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1242df8bae1dSRodney W. Grimes 				break;
124382c23ebaSBill Fenner 
124482c23ebaSBill Fenner 			case IP_RECVIF:
124582c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
124682c23ebaSBill Fenner 				break;
12476a800098SYoshinobu Inoue 
12486a800098SYoshinobu Inoue 			case IP_FAITH:
12496a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
12506a800098SYoshinobu Inoue 				break;
1251df8bae1dSRodney W. Grimes 			}
1252df8bae1dSRodney W. Grimes 			break;
1253df8bae1dSRodney W. Grimes #undef OPTSET
1254df8bae1dSRodney W. Grimes 
1255df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1256f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1257df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1258df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1259df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1260df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1261cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1262df8bae1dSRodney W. Grimes 			break;
1263df8bae1dSRodney W. Grimes 
126433b3ac06SPeter Wemm 		case IP_PORTRANGE:
1265cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1266cfe8b629SGarrett Wollman 					    sizeof optval);
1267cfe8b629SGarrett Wollman 			if (error)
1268cfe8b629SGarrett Wollman 				break;
126933b3ac06SPeter Wemm 
127033b3ac06SPeter Wemm 			switch (optval) {
127133b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
127233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
127333b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
127433b3ac06SPeter Wemm 				break;
127533b3ac06SPeter Wemm 
127633b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
127733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
127833b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
127933b3ac06SPeter Wemm 				break;
128033b3ac06SPeter Wemm 
128133b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
128233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
128333b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
128433b3ac06SPeter Wemm 				break;
128533b3ac06SPeter Wemm 
128633b3ac06SPeter Wemm 			default:
128733b3ac06SPeter Wemm 				error = EINVAL;
128833b3ac06SPeter Wemm 				break;
128933b3ac06SPeter Wemm 			}
1290ce8c72b1SPeter Wemm 			break;
129133b3ac06SPeter Wemm 
12926a800098SYoshinobu Inoue #ifdef IPSEC
12936a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12946a800098SYoshinobu Inoue 		{
12956a800098SYoshinobu Inoue 			caddr_t req;
1296686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
12976a800098SYoshinobu Inoue 			int priv;
12986a800098SYoshinobu Inoue 			struct mbuf *m;
12996a800098SYoshinobu Inoue 			int optname;
13006a800098SYoshinobu Inoue 
13016a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
13026a800098SYoshinobu Inoue 				break;
13036a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
13046a800098SYoshinobu Inoue 				break;
1305b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
130644731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
13076a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1308686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
13096a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1310686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
13116a800098SYoshinobu Inoue 			m_freem(m);
13126a800098SYoshinobu Inoue 			break;
13136a800098SYoshinobu Inoue 		}
13146a800098SYoshinobu Inoue #endif /*IPSEC*/
13156a800098SYoshinobu Inoue 
1316df8bae1dSRodney W. Grimes 		default:
1317df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1318df8bae1dSRodney W. Grimes 			break;
1319df8bae1dSRodney W. Grimes 		}
1320df8bae1dSRodney W. Grimes 		break;
1321df8bae1dSRodney W. Grimes 
1322cfe8b629SGarrett Wollman 	case SOPT_GET:
1323cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1324df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1325df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1326cfe8b629SGarrett Wollman 			if (inp->inp_options)
1327cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1328cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1329cfe8b629SGarrett Wollman 							  char *),
1330cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1331cfe8b629SGarrett Wollman 			else
1332cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1333df8bae1dSRodney W. Grimes 			break;
1334df8bae1dSRodney W. Grimes 
1335df8bae1dSRodney W. Grimes 		case IP_TOS:
1336df8bae1dSRodney W. Grimes 		case IP_TTL:
1337df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1338df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1339df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
134082c23ebaSBill Fenner 		case IP_RECVIF:
1341cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
13426a800098SYoshinobu Inoue 		case IP_FAITH:
1343cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1344df8bae1dSRodney W. Grimes 
1345df8bae1dSRodney W. Grimes 			case IP_TOS:
1346ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1347df8bae1dSRodney W. Grimes 				break;
1348df8bae1dSRodney W. Grimes 
1349df8bae1dSRodney W. Grimes 			case IP_TTL:
1350ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1351df8bae1dSRodney W. Grimes 				break;
1352df8bae1dSRodney W. Grimes 
1353df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1354df8bae1dSRodney W. Grimes 
1355df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1356df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1357df8bae1dSRodney W. Grimes 				break;
1358df8bae1dSRodney W. Grimes 
1359df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1360df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1361df8bae1dSRodney W. Grimes 				break;
1362df8bae1dSRodney W. Grimes 
1363df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1364df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1365df8bae1dSRodney W. Grimes 				break;
136682c23ebaSBill Fenner 
136782c23ebaSBill Fenner 			case IP_RECVIF:
136882c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
136982c23ebaSBill Fenner 				break;
1370cfe8b629SGarrett Wollman 
1371cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1372cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1373cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1374cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1375cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1376cfe8b629SGarrett Wollman 				else
1377cfe8b629SGarrett Wollman 					optval = 0;
1378cfe8b629SGarrett Wollman 				break;
13796a800098SYoshinobu Inoue 
13806a800098SYoshinobu Inoue 			case IP_FAITH:
13816a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
13826a800098SYoshinobu Inoue 				break;
1383df8bae1dSRodney W. Grimes 			}
1384cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1385df8bae1dSRodney W. Grimes 			break;
1386df8bae1dSRodney W. Grimes 
1387df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1388f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1389df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1390df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1391df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1392df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1393cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
139433b3ac06SPeter Wemm 			break;
139533b3ac06SPeter Wemm 
13966a800098SYoshinobu Inoue #ifdef IPSEC
13976a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13986a800098SYoshinobu Inoue 		{
1399f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
14006a800098SYoshinobu Inoue 			caddr_t req = NULL;
1401686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
14026a800098SYoshinobu Inoue 
1403686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
14046a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1405686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1406686cdd19SJun-ichiro itojun Hagino 			}
1407686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
14086a800098SYoshinobu Inoue 			if (error == 0)
14096a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1410f63e7634SYoshinobu Inoue 			if (error == 0)
14116a800098SYoshinobu Inoue 				m_freem(m);
14126a800098SYoshinobu Inoue 			break;
14136a800098SYoshinobu Inoue 		}
14146a800098SYoshinobu Inoue #endif /*IPSEC*/
14156a800098SYoshinobu Inoue 
1416df8bae1dSRodney W. Grimes 		default:
1417df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1418df8bae1dSRodney W. Grimes 			break;
1419df8bae1dSRodney W. Grimes 		}
1420df8bae1dSRodney W. Grimes 		break;
1421df8bae1dSRodney W. Grimes 	}
1422df8bae1dSRodney W. Grimes 	return (error);
1423df8bae1dSRodney W. Grimes }
1424df8bae1dSRodney W. Grimes 
1425df8bae1dSRodney W. Grimes /*
1426df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1427df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1428df8bae1dSRodney W. Grimes  * with destination address if source routed.
1429df8bae1dSRodney W. Grimes  */
14300312fbe9SPoul-Henning Kamp static int
1431df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m)
1432df8bae1dSRodney W. Grimes 	int optname;
1433df8bae1dSRodney W. Grimes 	struct mbuf **pcbopt;
1434df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1435df8bae1dSRodney W. Grimes {
1436d68fa50cSBruce Evans 	register int cnt, optlen;
1437df8bae1dSRodney W. Grimes 	register u_char *cp;
1438df8bae1dSRodney W. Grimes 	u_char opt;
1439df8bae1dSRodney W. Grimes 
1440df8bae1dSRodney W. Grimes 	/* turn off any old options */
1441df8bae1dSRodney W. Grimes 	if (*pcbopt)
1442df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1443df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1444df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1445df8bae1dSRodney W. Grimes 		/*
1446df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1447df8bae1dSRodney W. Grimes 		 */
1448df8bae1dSRodney W. Grimes 		if (m)
1449df8bae1dSRodney W. Grimes 			(void)m_free(m);
1450df8bae1dSRodney W. Grimes 		return (0);
1451df8bae1dSRodney W. Grimes 	}
1452df8bae1dSRodney W. Grimes 
14530c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1454df8bae1dSRodney W. Grimes 		goto bad;
1455df8bae1dSRodney W. Grimes 	/*
1456df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1457df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1458df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1459df8bae1dSRodney W. Grimes 	 */
1460df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1461df8bae1dSRodney W. Grimes 		goto bad;
1462df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1463df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1464df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1465df8bae1dSRodney W. Grimes 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1466df8bae1dSRodney W. Grimes 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1467df8bae1dSRodney W. Grimes 
1468df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1469df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1470df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1471df8bae1dSRodney W. Grimes 			break;
1472df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1473df8bae1dSRodney W. Grimes 			optlen = 1;
1474df8bae1dSRodney W. Grimes 		else {
1475707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1476707d00a3SJonathan Lemon 				goto bad;
1477df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1478707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1479df8bae1dSRodney W. Grimes 				goto bad;
1480df8bae1dSRodney W. Grimes 		}
1481df8bae1dSRodney W. Grimes 		switch (opt) {
1482df8bae1dSRodney W. Grimes 
1483df8bae1dSRodney W. Grimes 		default:
1484df8bae1dSRodney W. Grimes 			break;
1485df8bae1dSRodney W. Grimes 
1486df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1487df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1488df8bae1dSRodney W. Grimes 			/*
1489df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1490df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1491df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1492df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1493df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1494df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1495df8bae1dSRodney W. Grimes 			 */
1496df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1497df8bae1dSRodney W. Grimes 				goto bad;
1498df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1499df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1500df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1501df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1502df8bae1dSRodney W. Grimes 			/*
1503df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1504df8bae1dSRodney W. Grimes 			 */
1505df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1506df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1507df8bae1dSRodney W. Grimes 			/*
1508df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1509df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1510df8bae1dSRodney W. Grimes 			 */
1511df8bae1dSRodney W. Grimes 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1512df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr)),
1513df8bae1dSRodney W. Grimes 			    (caddr_t)&cp[IPOPT_OFFSET+1],
1514df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
1515df8bae1dSRodney W. Grimes 			break;
1516df8bae1dSRodney W. Grimes 		}
1517df8bae1dSRodney W. Grimes 	}
1518df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1519df8bae1dSRodney W. Grimes 		goto bad;
1520df8bae1dSRodney W. Grimes 	*pcbopt = m;
1521df8bae1dSRodney W. Grimes 	return (0);
1522df8bae1dSRodney W. Grimes 
1523df8bae1dSRodney W. Grimes bad:
1524df8bae1dSRodney W. Grimes 	(void)m_free(m);
1525df8bae1dSRodney W. Grimes 	return (EINVAL);
1526df8bae1dSRodney W. Grimes }
1527df8bae1dSRodney W. Grimes 
1528df8bae1dSRodney W. Grimes /*
1529cfe8b629SGarrett Wollman  * XXX
1530cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1531cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1532cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1533cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1534cfe8b629SGarrett Wollman  */
153533841545SHajimu UMEMOTO 
153633841545SHajimu UMEMOTO /*
153733841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
153833841545SHajimu UMEMOTO  */
153933841545SHajimu UMEMOTO static struct ifnet *
154033841545SHajimu UMEMOTO ip_multicast_if(a, ifindexp)
154133841545SHajimu UMEMOTO 	struct in_addr *a;
154233841545SHajimu UMEMOTO 	int *ifindexp;
154333841545SHajimu UMEMOTO {
154433841545SHajimu UMEMOTO 	int ifindex;
154533841545SHajimu UMEMOTO 	struct ifnet *ifp;
154633841545SHajimu UMEMOTO 
154733841545SHajimu UMEMOTO 	if (ifindexp)
154833841545SHajimu UMEMOTO 		*ifindexp = 0;
154933841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
155033841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
155133841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
155233841545SHajimu UMEMOTO 			return NULL;
1553f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
155433841545SHajimu UMEMOTO 		if (ifindexp)
155533841545SHajimu UMEMOTO 			*ifindexp = ifindex;
155633841545SHajimu UMEMOTO 	} else {
155733841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
155833841545SHajimu UMEMOTO 	}
155933841545SHajimu UMEMOTO 	return ifp;
156033841545SHajimu UMEMOTO }
156133841545SHajimu UMEMOTO 
1562cfe8b629SGarrett Wollman /*
1563df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1564df8bae1dSRodney W. Grimes  */
15650312fbe9SPoul-Henning Kamp static int
1566cfe8b629SGarrett Wollman ip_setmoptions(sopt, imop)
1567cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1568df8bae1dSRodney W. Grimes 	struct ip_moptions **imop;
1569df8bae1dSRodney W. Grimes {
1570cfe8b629SGarrett Wollman 	int error = 0;
1571cfe8b629SGarrett Wollman 	int i;
1572df8bae1dSRodney W. Grimes 	struct in_addr addr;
1573cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1574cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1575cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1576df8bae1dSRodney W. Grimes 	struct route ro;
1577cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
157833841545SHajimu UMEMOTO 	int ifindex;
15799b626c29SGarrett Wollman 	int s;
1580df8bae1dSRodney W. Grimes 
1581df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1582df8bae1dSRodney W. Grimes 		/*
1583df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1584df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1585df8bae1dSRodney W. Grimes 		 */
1586df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1587df8bae1dSRodney W. Grimes 		    M_WAITOK);
1588df8bae1dSRodney W. Grimes 
1589df8bae1dSRodney W. Grimes 		if (imo == NULL)
1590df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1591df8bae1dSRodney W. Grimes 		*imop = imo;
1592df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
159333841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
15941c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1595df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1596df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1597df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1598df8bae1dSRodney W. Grimes 	}
1599df8bae1dSRodney W. Grimes 
1600cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1601f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1602f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1603cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
16045e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
16055e9ae478SGarrett Wollman 			break;
16065e9ae478SGarrett Wollman 		}
1607cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1608cfe8b629SGarrett Wollman 		if (error)
1609f0068c4aSGarrett Wollman 			break;
16101c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1611f0068c4aSGarrett Wollman 			error = EINVAL;
1612f0068c4aSGarrett Wollman 			break;
1613f0068c4aSGarrett Wollman 		}
1614f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1615f0068c4aSGarrett Wollman 		break;
1616f0068c4aSGarrett Wollman 
1617df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1618df8bae1dSRodney W. Grimes 		/*
1619df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1620df8bae1dSRodney W. Grimes 		 */
1621cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1622cfe8b629SGarrett Wollman 		if (error)
1623df8bae1dSRodney W. Grimes 			break;
1624df8bae1dSRodney W. Grimes 		/*
1625df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1626df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1627df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1628df8bae1dSRodney W. Grimes 		 */
1629df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1630df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1631df8bae1dSRodney W. Grimes 			break;
1632df8bae1dSRodney W. Grimes 		}
1633df8bae1dSRodney W. Grimes 		/*
1634df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1635df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1636df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1637df8bae1dSRodney W. Grimes 		 */
163820e8807cSGarrett Wollman 		s = splimp();
163933841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1640df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1641fbc6ab00SBill Fenner 			splx(s);
1642df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1643df8bae1dSRodney W. Grimes 			break;
1644df8bae1dSRodney W. Grimes 		}
1645df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
164633841545SHajimu UMEMOTO 		if (ifindex)
164733841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
164833841545SHajimu UMEMOTO 		else
164933841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
16509b626c29SGarrett Wollman 		splx(s);
1651df8bae1dSRodney W. Grimes 		break;
1652df8bae1dSRodney W. Grimes 
1653df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1654df8bae1dSRodney W. Grimes 		/*
1655df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1656cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1657cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1658cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1659df8bae1dSRodney W. Grimes 		 */
1660cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1661cfe8b629SGarrett Wollman 			u_char ttl;
1662cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1663cfe8b629SGarrett Wollman 			if (error)
1664df8bae1dSRodney W. Grimes 				break;
1665cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1666cfe8b629SGarrett Wollman 		} else {
1667cfe8b629SGarrett Wollman 			u_int ttl;
1668cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1669cfe8b629SGarrett Wollman 					    sizeof ttl);
1670cfe8b629SGarrett Wollman 			if (error)
1671cfe8b629SGarrett Wollman 				break;
1672cfe8b629SGarrett Wollman 			if (ttl > 255)
1673cfe8b629SGarrett Wollman 				error = EINVAL;
1674cfe8b629SGarrett Wollman 			else
1675cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1676df8bae1dSRodney W. Grimes 		}
1677df8bae1dSRodney W. Grimes 		break;
1678df8bae1dSRodney W. Grimes 
1679df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1680df8bae1dSRodney W. Grimes 		/*
1681df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1682cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1683cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1684cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1685df8bae1dSRodney W. Grimes 		 */
1686cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1687cfe8b629SGarrett Wollman 			u_char loop;
1688cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1689cfe8b629SGarrett Wollman 			if (error)
1690df8bae1dSRodney W. Grimes 				break;
1691cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1692cfe8b629SGarrett Wollman 		} else {
1693cfe8b629SGarrett Wollman 			u_int loop;
1694cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1695cfe8b629SGarrett Wollman 					    sizeof loop);
1696cfe8b629SGarrett Wollman 			if (error)
1697cfe8b629SGarrett Wollman 				break;
1698cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1699df8bae1dSRodney W. Grimes 		}
1700df8bae1dSRodney W. Grimes 		break;
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1703df8bae1dSRodney W. Grimes 		/*
1704df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1705df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1706df8bae1dSRodney W. Grimes 		 */
1707cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1708cfe8b629SGarrett Wollman 		if (error)
1709df8bae1dSRodney W. Grimes 			break;
1710cfe8b629SGarrett Wollman 
1711cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1712df8bae1dSRodney W. Grimes 			error = EINVAL;
1713df8bae1dSRodney W. Grimes 			break;
1714df8bae1dSRodney W. Grimes 		}
171520e8807cSGarrett Wollman 		s = splimp();
1716df8bae1dSRodney W. Grimes 		/*
1717df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1718df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1719df8bae1dSRodney W. Grimes 		 */
1720cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
17211c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1722df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1723df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1724df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1725cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
1726df8bae1dSRodney W. Grimes 			rtalloc(&ro);
1727df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1728df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
17299b626c29SGarrett Wollman 				splx(s);
1730df8bae1dSRodney W. Grimes 				break;
1731df8bae1dSRodney W. Grimes 			}
1732df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1733df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
1734df8bae1dSRodney W. Grimes 		}
1735df8bae1dSRodney W. Grimes 		else {
173633841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1737df8bae1dSRodney W. Grimes 		}
17389b626c29SGarrett Wollman 
1739df8bae1dSRodney W. Grimes 		/*
1740df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1741df8bae1dSRodney W. Grimes 		 * supports multicast.
1742df8bae1dSRodney W. Grimes 		 */
1743df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1744df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
17459b626c29SGarrett Wollman 			splx(s);
1746df8bae1dSRodney W. Grimes 			break;
1747df8bae1dSRodney W. Grimes 		}
1748df8bae1dSRodney W. Grimes 		/*
1749df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1750df8bae1dSRodney W. Grimes 		 * membership slots are full.
1751df8bae1dSRodney W. Grimes 		 */
1752df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1753df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1754df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1755cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1756df8bae1dSRodney W. Grimes 				break;
1757df8bae1dSRodney W. Grimes 		}
1758df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1759df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
17609b626c29SGarrett Wollman 			splx(s);
1761df8bae1dSRodney W. Grimes 			break;
1762df8bae1dSRodney W. Grimes 		}
1763df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1764df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
17659b626c29SGarrett Wollman 			splx(s);
1766df8bae1dSRodney W. Grimes 			break;
1767df8bae1dSRodney W. Grimes 		}
1768df8bae1dSRodney W. Grimes 		/*
1769df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1770df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1771df8bae1dSRodney W. Grimes 		 */
1772df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1773cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1774df8bae1dSRodney W. Grimes 			error = ENOBUFS;
17759b626c29SGarrett Wollman 			splx(s);
1776df8bae1dSRodney W. Grimes 			break;
1777df8bae1dSRodney W. Grimes 		}
1778df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
17799b626c29SGarrett Wollman 		splx(s);
1780df8bae1dSRodney W. Grimes 		break;
1781df8bae1dSRodney W. Grimes 
1782df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1783df8bae1dSRodney W. Grimes 		/*
1784df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1785df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1786df8bae1dSRodney W. Grimes 		 */
1787cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1788cfe8b629SGarrett Wollman 		if (error)
1789df8bae1dSRodney W. Grimes 			break;
1790cfe8b629SGarrett Wollman 
1791cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1792df8bae1dSRodney W. Grimes 			error = EINVAL;
1793df8bae1dSRodney W. Grimes 			break;
1794df8bae1dSRodney W. Grimes 		}
17959b626c29SGarrett Wollman 
179620e8807cSGarrett Wollman 		s = splimp();
1797df8bae1dSRodney W. Grimes 		/*
1798df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1799df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1800df8bae1dSRodney W. Grimes 		 */
1801cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1802df8bae1dSRodney W. Grimes 			ifp = NULL;
1803df8bae1dSRodney W. Grimes 		else {
180433841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1805df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1806df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
18079b626c29SGarrett Wollman 				splx(s);
1808df8bae1dSRodney W. Grimes 				break;
1809df8bae1dSRodney W. Grimes 			}
1810df8bae1dSRodney W. Grimes 		}
1811df8bae1dSRodney W. Grimes 		/*
1812df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1813df8bae1dSRodney W. Grimes 		 */
1814df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1815df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1816df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1817df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1818cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1819df8bae1dSRodney W. Grimes 				break;
1820df8bae1dSRodney W. Grimes 		}
1821df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1822df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
18239b626c29SGarrett Wollman 			splx(s);
1824df8bae1dSRodney W. Grimes 			break;
1825df8bae1dSRodney W. Grimes 		}
1826df8bae1dSRodney W. Grimes 		/*
1827df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1828df8bae1dSRodney W. Grimes 		 * membership points.
1829df8bae1dSRodney W. Grimes 		 */
1830df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1831df8bae1dSRodney W. Grimes 		/*
1832df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1833df8bae1dSRodney W. Grimes 		 */
1834df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1835df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1836df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
18379b626c29SGarrett Wollman 		splx(s);
1838df8bae1dSRodney W. Grimes 		break;
1839df8bae1dSRodney W. Grimes 
1840df8bae1dSRodney W. Grimes 	default:
1841df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1842df8bae1dSRodney W. Grimes 		break;
1843df8bae1dSRodney W. Grimes 	}
1844df8bae1dSRodney W. Grimes 
1845df8bae1dSRodney W. Grimes 	/*
1846df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1847df8bae1dSRodney W. Grimes 	 */
1848df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
18491c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1850df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1851df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1852df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1853df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1854df8bae1dSRodney W. Grimes 		*imop = NULL;
1855df8bae1dSRodney W. Grimes 	}
1856df8bae1dSRodney W. Grimes 
1857df8bae1dSRodney W. Grimes 	return (error);
1858df8bae1dSRodney W. Grimes }
1859df8bae1dSRodney W. Grimes 
1860df8bae1dSRodney W. Grimes /*
1861df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1862df8bae1dSRodney W. Grimes  */
18630312fbe9SPoul-Henning Kamp static int
1864cfe8b629SGarrett Wollman ip_getmoptions(sopt, imo)
1865cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1866df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1867df8bae1dSRodney W. Grimes {
1868cfe8b629SGarrett Wollman 	struct in_addr addr;
1869df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1870cfe8b629SGarrett Wollman 	int error, optval;
1871cfe8b629SGarrett Wollman 	u_char coptval;
1872df8bae1dSRodney W. Grimes 
1873cfe8b629SGarrett Wollman 	error = 0;
1874cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1875f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1876f0068c4aSGarrett Wollman 		if (imo != NULL)
1877cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1878f0068c4aSGarrett Wollman 		else
1879cfe8b629SGarrett Wollman 			optval = -1;
1880cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1881cfe8b629SGarrett Wollman 		break;
1882f0068c4aSGarrett Wollman 
1883df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1884df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1885cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
188633841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
188733841545SHajimu UMEMOTO 			/* return the value user has set */
188833841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
188933841545SHajimu UMEMOTO 		} else {
1890df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1891cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1892df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1893df8bae1dSRodney W. Grimes 		}
1894cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1895cfe8b629SGarrett Wollman 		break;
1896df8bae1dSRodney W. Grimes 
1897df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1898cfe8b629SGarrett Wollman 		if (imo == 0)
1899cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1900cfe8b629SGarrett Wollman 		else
1901cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
1902cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1903cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1904cfe8b629SGarrett Wollman 		else
1905cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1906cfe8b629SGarrett Wollman 		break;
1907df8bae1dSRodney W. Grimes 
1908df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1909cfe8b629SGarrett Wollman 		if (imo == 0)
1910cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1911cfe8b629SGarrett Wollman 		else
1912cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
1913cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1914cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1915cfe8b629SGarrett Wollman 		else
1916cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1917cfe8b629SGarrett Wollman 		break;
1918df8bae1dSRodney W. Grimes 
1919df8bae1dSRodney W. Grimes 	default:
1920cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1921cfe8b629SGarrett Wollman 		break;
1922df8bae1dSRodney W. Grimes 	}
1923cfe8b629SGarrett Wollman 	return (error);
1924df8bae1dSRodney W. Grimes }
1925df8bae1dSRodney W. Grimes 
1926df8bae1dSRodney W. Grimes /*
1927df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1928df8bae1dSRodney W. Grimes  */
1929df8bae1dSRodney W. Grimes void
1930df8bae1dSRodney W. Grimes ip_freemoptions(imo)
1931df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1932df8bae1dSRodney W. Grimes {
1933df8bae1dSRodney W. Grimes 	register int i;
1934df8bae1dSRodney W. Grimes 
1935df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1936df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1937df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1938df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1939df8bae1dSRodney W. Grimes 	}
1940df8bae1dSRodney W. Grimes }
1941df8bae1dSRodney W. Grimes 
1942df8bae1dSRodney W. Grimes /*
1943df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1944df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1945df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1946f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1947f5fea3ddSPaul Traina  * replicating that code here.
1948df8bae1dSRodney W. Grimes  */
1949df8bae1dSRodney W. Grimes static void
195086b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen)
1951df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1952df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1953df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
195486b1d6d2SBill Fenner 	int hlen;
1955df8bae1dSRodney W. Grimes {
1956df8bae1dSRodney W. Grimes 	register struct ip *ip;
1957df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1958df8bae1dSRodney W. Grimes 
1959df8bae1dSRodney W. Grimes 	copym = m_copy(m, 0, M_COPYALL);
196086b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
196186b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1962df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1963df8bae1dSRodney W. Grimes 		/*
1964df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1965df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1966df8bae1dSRodney W. Grimes 		 */
1967df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1968fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1969fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1970df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
19719c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
19729c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
19739c9137eaSGarrett Wollman 		} else {
197486b1d6d2SBill Fenner 			ip->ip_sum = in_cksum(copym, hlen);
19759c9137eaSGarrett Wollman 		}
19769c9137eaSGarrett Wollman 		/*
19779c9137eaSGarrett Wollman 		 * NB:
1978e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1979e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1980e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1981e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1982e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1983e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1984e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1985ed7509acSJulian Elischer 		 * of safety by using if_simloop().
19869c9137eaSGarrett Wollman 		 */
1987201c2527SJulian Elischer #if 1 /* XXX */
1988201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
19892b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1990201c2527SJulian Elischer 						dst->sin_family);
1991201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1992201c2527SJulian Elischer 		}
1993201c2527SJulian Elischer #endif
1994201c2527SJulian Elischer 
19959c9137eaSGarrett Wollman #ifdef notdef
1996e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
1997ed7509acSJulian Elischer 		ip_input(copym);
19989c9137eaSGarrett Wollman #else
199950c6dc99SJonathan Lemon 		/* if the checksum hasn't been computed, mark it as valid */
200050c6dc99SJonathan Lemon 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
200150c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_flags |=
200250c6dc99SJonathan Lemon 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
200350c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_data = 0xffff;
200450c6dc99SJonathan Lemon 		}
200506a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
20069c9137eaSGarrett Wollman #endif
2007df8bae1dSRodney W. Grimes 	}
2008df8bae1dSRodney W. Grimes }
2009