xref: /freebsd/sys/netinet/ip_output.c (revision cb7641e85b269f8edff421aba73f04b09b4ec031)
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"
444ed84624SRobert Watson #include "opt_mac.h"
45c4ac87eaSDarren Reed #include "opt_pfil_hooks.h"
4664dddc18SKris Kennaway #include "opt_random_ip_id.h"
47fbd1372aSJoerg Wunsch 
48df8bae1dSRodney W. Grimes #include <sys/param.h>
4926f9a767SRodney W. Grimes #include <sys/systm.h>
50f0f6d643SLuigi Rizzo #include <sys/kernel.h>
514ed84624SRobert Watson #include <sys/mac.h>
52df8bae1dSRodney W. Grimes #include <sys/malloc.h>
53df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
54df8bae1dSRodney W. Grimes #include <sys/protosw.h>
55df8bae1dSRodney W. Grimes #include <sys/socket.h>
56df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <net/if.h>
59df8bae1dSRodney W. Grimes #include <net/route.h>
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes #include <netinet/in.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
63df8bae1dSRodney W. Grimes #include <netinet/ip.h>
64df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
66df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
67df8bae1dSRodney W. Grimes 
689c9137eaSGarrett Wollman #include <machine/in_cksum.h>
69df8bae1dSRodney W. Grimes 
70a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
7155166637SPoul-Henning Kamp 
726a800098SYoshinobu Inoue #ifdef IPSEC
736a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
746a800098SYoshinobu Inoue #include <netkey/key.h>
756a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG
766a800098SYoshinobu Inoue #include <netkey/key_debug.h>
776a800098SYoshinobu Inoue #else
786a800098SYoshinobu Inoue #define	KEYDEBUG(lev,arg)
796a800098SYoshinobu Inoue #endif
806a800098SYoshinobu Inoue #endif /*IPSEC*/
816a800098SYoshinobu Inoue 
82cfe8b629SGarrett Wollman #include <netinet/ip_fw.h>
83b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
84b715f178SLuigi Rizzo 
852b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
862b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
87f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
88f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
892b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
90f9e354dfSJulian Elischer 
91f23b4c91SGarrett Wollman u_short ip_id;
92f23b4c91SGarrett Wollman 
934d77a549SAlfred Perlstein static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
944d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *);
95df8bae1dSRodney W. Grimes static void	ip_mloopback
964d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
970312fbe9SPoul-Henning Kamp static int	ip_getmoptions
984d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions *);
994d77a549SAlfred Perlstein static int	ip_pcbopts(int, struct mbuf **, struct mbuf *);
1000312fbe9SPoul-Henning Kamp static int	ip_setmoptions
1014d77a549SAlfred Perlstein 	(struct sockopt *, struct ip_moptions **);
102df8bae1dSRodney W. Grimes 
1034d77a549SAlfred Perlstein int	ip_optcopy(struct ip *, struct ip *);
104afed1b49SDarren Reed 
105afed1b49SDarren Reed 
10693e0e116SJulian Elischer extern	struct protosw inetsw[];
10793e0e116SJulian Elischer 
108df8bae1dSRodney W. Grimes /*
109df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
110df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
111df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
112df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
113df8bae1dSRodney W. Grimes  */
114df8bae1dSRodney W. Grimes int
115df8bae1dSRodney W. Grimes ip_output(m0, opt, ro, flags, imo)
116df8bae1dSRodney W. Grimes 	struct mbuf *m0;
117df8bae1dSRodney W. Grimes 	struct mbuf *opt;
118df8bae1dSRodney W. Grimes 	struct route *ro;
119df8bae1dSRodney W. Grimes 	int flags;
120df8bae1dSRodney W. Grimes 	struct ip_moptions *imo;
121df8bae1dSRodney W. Grimes {
12223bf9953SPoul-Henning Kamp 	struct ip *ip, *mhip;
1232b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
1242b25acc1SLuigi Rizzo 	struct mbuf *m;
12523bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
126df8bae1dSRodney W. Grimes 	int len, off, error = 0;
1272b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1283956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
129db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
1303efc3014SJulian Elischer 	struct in_addr pkt_dst;
1316a800098SYoshinobu Inoue #ifdef IPSEC
132e3f406b3SRuslan Ermilov 	struct route iproute;
133d0a98d79SYoshinobu Inoue 	struct socket *so = NULL;
1346a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1356a800098SYoshinobu Inoue #endif
1362b25acc1SLuigi Rizzo 	struct ip_fw_args args;
1372b25acc1SLuigi Rizzo 	int src_was_INADDR_ANY = 0;	/* as the name says... */
138c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
139c4ac87eaSDarren Reed 	struct packet_filter_hook *pfh;
140c4ac87eaSDarren Reed 	struct mbuf *m1;
141c4ac87eaSDarren Reed 	int rv;
142c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
143b715f178SLuigi Rizzo 
1442b25acc1SLuigi Rizzo 	args.eh = NULL;
1452b25acc1SLuigi Rizzo 	args.rule = NULL;
1462b25acc1SLuigi Rizzo 	args.next_hop = NULL;
1472b25acc1SLuigi Rizzo 	args.divert_rule = 0;			/* divert cookie */
1488948e4baSArchie Cobbs 
1492b25acc1SLuigi Rizzo 	/* Grab info from MT_TAG mbufs prepended to the chain. */
1502b25acc1SLuigi Rizzo 	for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) {
1512b25acc1SLuigi Rizzo 		switch(m0->m_tag_id) {
1522b25acc1SLuigi Rizzo 		default:
1532b25acc1SLuigi Rizzo 			printf("ip_output: unrecognised MT_TAG tag %d\n",
1542b25acc1SLuigi Rizzo 			    m0->m_tag_id);
1552b25acc1SLuigi Rizzo 			break;
1562b25acc1SLuigi Rizzo 
1572b25acc1SLuigi Rizzo 		case PACKET_TAG_DUMMYNET:
158b715f178SLuigi Rizzo 			/*
159b715f178SLuigi Rizzo 			 * the packet was already tagged, so part of the
160b715f178SLuigi Rizzo 			 * processing was already done, and we need to go down.
1616bc748b0SLuigi Rizzo 			 * Get parameters from the header.
162b715f178SLuigi Rizzo 			 */
1632b25acc1SLuigi Rizzo 			args.rule = ((struct dn_pkt *)m0)->rule;
164d1f04b29SLuigi Rizzo 			opt = NULL ;
1652b25acc1SLuigi Rizzo 			ro = & ( ((struct dn_pkt *)m0)->ro ) ;
166d1f04b29SLuigi Rizzo 			imo = NULL ;
1672b25acc1SLuigi Rizzo 			dst = ((struct dn_pkt *)m0)->dn_dst ;
1682b25acc1SLuigi Rizzo 			ifp = ((struct dn_pkt *)m0)->ifp ;
1692b25acc1SLuigi Rizzo 			flags = ((struct dn_pkt *)m0)->flags ;
1702b25acc1SLuigi Rizzo 			break;
171d1f04b29SLuigi Rizzo 
1722b25acc1SLuigi Rizzo 		case PACKET_TAG_DIVERT:
1737627c6cbSMaxime Henrion 			args.divert_rule = (intptr_t)m0->m_data & 0xffff;
1742b25acc1SLuigi Rizzo 			break;
1752b25acc1SLuigi Rizzo 
1762b25acc1SLuigi Rizzo 		case PACKET_TAG_IPFORWARD:
1772b25acc1SLuigi Rizzo 			args.next_hop = (struct sockaddr_in *)m0->m_data;
1782b25acc1SLuigi Rizzo 			break;
1792b25acc1SLuigi Rizzo 		}
1802b25acc1SLuigi Rizzo 	}
1812b25acc1SLuigi Rizzo 	m = m0;
1822b25acc1SLuigi Rizzo 
1832b25acc1SLuigi Rizzo 	KASSERT(!m || (m->m_flags & M_PKTHDR) != 0, ("ip_output: no HDR"));
1842b25acc1SLuigi Rizzo 
1852b25acc1SLuigi Rizzo 	KASSERT(ro != NULL, ("ip_output: no route, proto %d",
1862b25acc1SLuigi Rizzo 	    mtod(m, struct ip *)->ip_p));
1872b25acc1SLuigi Rizzo 
188d0a98d79SYoshinobu Inoue #ifdef IPSEC
189686cdd19SJun-ichiro itojun Hagino 	so = ipsec_getsocket(m);
19033841545SHajimu UMEMOTO 	(void)ipsec_setsocket(m, NULL);
191d0a98d79SYoshinobu Inoue #endif
1922b25acc1SLuigi Rizzo 	if (args.rule != NULL) {	/* dummynet already saw us */
193b715f178SLuigi Rizzo 		ip = mtod(m, struct ip *);
194b715f178SLuigi Rizzo 		hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
1953956b023SLuigi Rizzo 		if (ro->ro_rt)
1969a10980eSJonathan Lemon 			ia = ifatoia(ro->ro_rt->rt_ifa);
197b715f178SLuigi Rizzo 		goto sendit;
1982b25acc1SLuigi Rizzo 	}
199db40007dSAndrew R. Reiter 
200df8bae1dSRodney W. Grimes 	if (opt) {
201cb7641e8SMaxim Konovalov 		len = 0;
202df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
203cb7641e8SMaxim Konovalov 		if (len != 0)
204df8bae1dSRodney W. Grimes 			hlen = len;
205df8bae1dSRodney W. Grimes 	}
206df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
2072b25acc1SLuigi Rizzo 	pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
2083efc3014SJulian Elischer 
209df8bae1dSRodney W. Grimes 	/*
210df8bae1dSRodney W. Grimes 	 * Fill in IP header.
211df8bae1dSRodney W. Grimes 	 */
212df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
2139c9137eaSGarrett Wollman 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
214df8bae1dSRodney W. Grimes 		ip->ip_off &= IP_DF;
21564dddc18SKris Kennaway #ifdef RANDOM_IP_ID
21664dddc18SKris Kennaway 		ip->ip_id = ip_randomid();
21764dddc18SKris Kennaway #else
218e30177e0SRuslan Ermilov 		ip->ip_id = htons(ip_id++);
21964dddc18SKris Kennaway #endif
220df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
221df8bae1dSRodney W. Grimes 	} else {
2229c9137eaSGarrett Wollman 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
223df8bae1dSRodney W. Grimes 	}
2249c9137eaSGarrett Wollman 
225df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
226df8bae1dSRodney W. Grimes 	/*
227df8bae1dSRodney W. Grimes 	 * If there is a cached route,
228df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
229df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
230a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
231a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
232df8bae1dSRodney W. Grimes 	 */
233df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
234a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
2353efc3014SJulian Elischer 			  dst->sin_addr.s_addr != pkt_dst.s_addr)) {
236df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
237df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
238df8bae1dSRodney W. Grimes 	}
239df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
240a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
241df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
242df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2433efc3014SJulian Elischer 		dst->sin_addr = pkt_dst;
244df8bae1dSRodney W. Grimes 	}
245df8bae1dSRodney W. Grimes 	/*
246df8bae1dSRodney W. Grimes 	 * If routing to interface only,
247df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
248df8bae1dSRodney W. Grimes 	 */
249df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
250df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
251df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
252df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
253df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
254df8bae1dSRodney W. Grimes 			goto bad;
255df8bae1dSRodney W. Grimes 		}
256df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
257df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2589f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2593afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
26038c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2613afefa39SDaniel C. Sobral 		/*
26238c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
26338c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2643afefa39SDaniel C. Sobral 		 */
26538c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
26638c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
26738c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
268df8bae1dSRodney W. Grimes 	} else {
2692c17fe93SGarrett Wollman 		/*
270f2c2962eSRuslan Ermilov 		 * If this is the case, we probably don't want to allocate
271f2c2962eSRuslan Ermilov 		 * a protocol-cloned route since we didn't get one from the
272f2c2962eSRuslan Ermilov 		 * ULP.  This lets TCP do its thing, while not burdening
273f2c2962eSRuslan Ermilov 		 * forwarding or ICMP with the overhead of cloning a route.
274f2c2962eSRuslan Ermilov 		 * Of course, we still want to do any cloning requested by
275f2c2962eSRuslan Ermilov 		 * the link layer, as this is probably required in all cases
276f2c2962eSRuslan Ermilov 		 * for correct operation (as it is for ARP).
2772c17fe93SGarrett Wollman 		 */
278df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
2792c17fe93SGarrett Wollman 			rtalloc_ign(ro, RTF_PRCLONING);
280df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
281df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
282df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
283df8bae1dSRodney W. Grimes 			goto bad;
284df8bae1dSRodney W. Grimes 		}
285df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
286df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
287df8bae1dSRodney W. Grimes 		ro->ro_rt->rt_use++;
288df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
289f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2909f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
291f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2929f9b3dc4SGarrett Wollman 		else
2939f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
294df8bae1dSRodney W. Grimes 	}
2953efc3014SJulian Elischer 	if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
296df8bae1dSRodney W. Grimes 		struct in_multi *inm;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
299df8bae1dSRodney W. Grimes 		/*
300df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
301df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
302df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
303df8bae1dSRodney W. Grimes 		 */
304df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
305df8bae1dSRodney W. Grimes 		/*
306df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
307df8bae1dSRodney W. Grimes 		 */
308df8bae1dSRodney W. Grimes 		if (imo != NULL) {
309df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
3101c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3111c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
3121c5de19aSGarrett Wollman 				    ip_mcast_src(imo->imo_multicast_vif);
313df8bae1dSRodney W. Grimes 		} else
314df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
315df8bae1dSRodney W. Grimes 		/*
316df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
317df8bae1dSRodney W. Grimes 		 */
3181c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
319df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
320df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
321df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
322df8bae1dSRodney W. Grimes 				goto bad;
323df8bae1dSRodney W. Grimes 			}
3241c5de19aSGarrett Wollman 		}
325df8bae1dSRodney W. Grimes 		/*
326df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
327df8bae1dSRodney W. Grimes 		 * of outgoing interface.
328df8bae1dSRodney W. Grimes 		 */
329df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
33038c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
33138c1bc35SRuslan Ermilov 			if (ia != NULL)
33238c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
333df8bae1dSRodney W. Grimes 		}
334df8bae1dSRodney W. Grimes 
33592bdb2faSBill Fenner 		if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
33692bdb2faSBill Fenner 			/*
33792bdb2faSBill Fenner 			 * XXX
33892bdb2faSBill Fenner 			 * delayed checksums are not currently
33992bdb2faSBill Fenner 			 * compatible with IP multicast routing
34092bdb2faSBill Fenner 			 */
34192bdb2faSBill Fenner 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
34292bdb2faSBill Fenner 				in_delayed_cksum(m);
34392bdb2faSBill Fenner 				m->m_pkthdr.csum_flags &=
34492bdb2faSBill Fenner 					~CSUM_DELAY_DATA;
34592bdb2faSBill Fenner 			}
34692bdb2faSBill Fenner 		}
3473efc3014SJulian Elischer 		IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
348df8bae1dSRodney W. Grimes 		if (inm != NULL &&
349df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
350df8bae1dSRodney W. Grimes 			/*
351df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
352df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
353df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
354df8bae1dSRodney W. Grimes 			 */
35586b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
356df8bae1dSRodney W. Grimes 		}
357df8bae1dSRodney W. Grimes 		else {
358df8bae1dSRodney W. Grimes 			/*
359df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
360df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
361df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
362df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
363df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
364df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
365df8bae1dSRodney W. Grimes 			 *
366df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
367df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
368df8bae1dSRodney W. Grimes 			 * if necessary.
369df8bae1dSRodney W. Grimes 			 */
370df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
371f0068c4aSGarrett Wollman 				/*
372f0068c4aSGarrett Wollman 				 * Check if rsvp daemon is running. If not, don't
373f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
374f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
375f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
376f0068c4aSGarrett Wollman 				 */
377b124e4f2SGarrett Wollman 				if (!rsvp_on)
378f0068c4aSGarrett Wollman 				  imo = NULL;
379f0068c4aSGarrett Wollman 				if (ip_mforward(ip, ifp, m, imo) != 0) {
380df8bae1dSRodney W. Grimes 					m_freem(m);
381df8bae1dSRodney W. Grimes 					goto done;
382df8bae1dSRodney W. Grimes 				}
383df8bae1dSRodney W. Grimes 			}
384df8bae1dSRodney W. Grimes 		}
3855e9ae478SGarrett Wollman 
386df8bae1dSRodney W. Grimes 		/*
387df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
388df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
389df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
390df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
391df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
392df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
393df8bae1dSRodney W. Grimes 		 */
394f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
395df8bae1dSRodney W. Grimes 			m_freem(m);
396df8bae1dSRodney W. Grimes 			goto done;
397df8bae1dSRodney W. Grimes 		}
398df8bae1dSRodney W. Grimes 
399df8bae1dSRodney W. Grimes 		goto sendit;
400df8bae1dSRodney W. Grimes 	}
401df8bae1dSRodney W. Grimes #ifndef notdef
402df8bae1dSRodney W. Grimes 	/*
4032b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
4042b25acc1SLuigi Rizzo 	 * of the outoing interface. In case, keep note we did that, so
4052b25acc1SLuigi Rizzo 	 * if the the firewall changes the next-hop causing the output
4062b25acc1SLuigi Rizzo 	 * interface to change, we can fix that.
407df8bae1dSRodney W. Grimes 	 */
408f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
40938c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
41038c1bc35SRuslan Ermilov 		if (ia != NULL) {
411df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
4122b25acc1SLuigi Rizzo 			src_was_INADDR_ANY = 1;
413f9e354dfSJulian Elischer 		}
41438c1bc35SRuslan Ermilov 	}
415f9e354dfSJulian Elischer #endif /* notdef */
416df8bae1dSRodney W. Grimes 	/*
417b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
418b5390296SDavid Greenman 	 *      the packet or packet fragments
419b5390296SDavid Greenman 	 */
420b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
421b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
422b5390296SDavid Greenman 			error = ENOBUFS;
42335609d45SJonathan Lemon 			ipstat.ips_odropped++;
424b5390296SDavid Greenman 			goto bad;
425b5390296SDavid Greenman 	}
426b5390296SDavid Greenman 
427b5390296SDavid Greenman 	/*
428df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4298c3f5566SRuslan Ermilov 	 * verify user is allowed to send
430df8bae1dSRodney W. Grimes 	 * such a packet.
431df8bae1dSRodney W. Grimes 	 */
4329f9b3dc4SGarrett Wollman 	if (isbroadcast) {
433df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
434df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
435df8bae1dSRodney W. Grimes 			goto bad;
436df8bae1dSRodney W. Grimes 		}
437df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
438df8bae1dSRodney W. Grimes 			error = EACCES;
439df8bae1dSRodney W. Grimes 			goto bad;
440df8bae1dSRodney W. Grimes 		}
441df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
442df8bae1dSRodney W. Grimes 		if ((u_short)ip->ip_len > ifp->if_mtu) {
443df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
444df8bae1dSRodney W. Grimes 			goto bad;
445df8bae1dSRodney W. Grimes 		}
446df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4479f9b3dc4SGarrett Wollman 	} else {
448df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4499f9b3dc4SGarrett Wollman 	}
450df8bae1dSRodney W. Grimes 
451f1743588SDarren Reed sendit:
45233841545SHajimu UMEMOTO #ifdef IPSEC
45333841545SHajimu UMEMOTO 	/* get SP for this packet */
45433841545SHajimu UMEMOTO 	if (so == NULL)
45533841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
45633841545SHajimu UMEMOTO 	else
45733841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
45833841545SHajimu UMEMOTO 
45933841545SHajimu UMEMOTO 	if (sp == NULL) {
46033841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
46133841545SHajimu UMEMOTO 		goto bad;
46233841545SHajimu UMEMOTO 	}
46333841545SHajimu UMEMOTO 
46433841545SHajimu UMEMOTO 	error = 0;
46533841545SHajimu UMEMOTO 
46633841545SHajimu UMEMOTO 	/* check policy */
46733841545SHajimu UMEMOTO 	switch (sp->policy) {
46833841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
46933841545SHajimu UMEMOTO 		/*
47033841545SHajimu UMEMOTO 		 * This packet is just discarded.
47133841545SHajimu UMEMOTO 		 */
47233841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
47333841545SHajimu UMEMOTO 		goto bad;
47433841545SHajimu UMEMOTO 
47533841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
47633841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
47733841545SHajimu UMEMOTO 		/* no need to do IPsec. */
47833841545SHajimu UMEMOTO 		goto skip_ipsec;
47933841545SHajimu UMEMOTO 
48033841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
48133841545SHajimu UMEMOTO 		if (sp->req == NULL) {
48233841545SHajimu UMEMOTO 			/* acquire a policy */
48333841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
48433841545SHajimu UMEMOTO 			goto bad;
48533841545SHajimu UMEMOTO 		}
48633841545SHajimu UMEMOTO 		break;
48733841545SHajimu UMEMOTO 
48833841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
48933841545SHajimu UMEMOTO 	default:
49033841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
49133841545SHajimu UMEMOTO 	}
49233841545SHajimu UMEMOTO     {
49333841545SHajimu UMEMOTO 	struct ipsec_output_state state;
49433841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
49533841545SHajimu UMEMOTO 	state.m = m;
49633841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
49733841545SHajimu UMEMOTO 		state.ro = &iproute;
49833841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
49933841545SHajimu UMEMOTO 	} else
50033841545SHajimu UMEMOTO 		state.ro = ro;
50133841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
50233841545SHajimu UMEMOTO 
50333841545SHajimu UMEMOTO 	ip->ip_sum = 0;
50433841545SHajimu UMEMOTO 
50533841545SHajimu UMEMOTO 	/*
50633841545SHajimu UMEMOTO 	 * XXX
50733841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
50833841545SHajimu UMEMOTO 	 */
50933841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
51033841545SHajimu UMEMOTO 		in_delayed_cksum(m);
51133841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
51233841545SHajimu UMEMOTO 	}
51333841545SHajimu UMEMOTO 
514fd8e4ebcSMike Barcroft 	ip->ip_len = htons(ip->ip_len);
515fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
51633841545SHajimu UMEMOTO 
51733841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
51833841545SHajimu UMEMOTO 
51933841545SHajimu UMEMOTO 	m = state.m;
52033841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
52133841545SHajimu UMEMOTO 		/*
52233841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
52333841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
52433841545SHajimu UMEMOTO 		 */
52533841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
52633841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
52733841545SHajimu UMEMOTO 			ro = state.ro;
52833841545SHajimu UMEMOTO 		}
52933841545SHajimu UMEMOTO 	} else
53033841545SHajimu UMEMOTO 		ro = state.ro;
53133841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
53233841545SHajimu UMEMOTO 	if (error) {
53333841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
53433841545SHajimu UMEMOTO 		m0 = NULL;
53533841545SHajimu UMEMOTO 		switch (error) {
53633841545SHajimu UMEMOTO 		case EHOSTUNREACH:
53733841545SHajimu UMEMOTO 		case ENETUNREACH:
53833841545SHajimu UMEMOTO 		case EMSGSIZE:
53933841545SHajimu UMEMOTO 		case ENOBUFS:
54033841545SHajimu UMEMOTO 		case ENOMEM:
54133841545SHajimu UMEMOTO 			break;
54233841545SHajimu UMEMOTO 		default:
54333841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
54433841545SHajimu UMEMOTO 			/*fall through*/
54533841545SHajimu UMEMOTO 		case ENOENT:
54633841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
54733841545SHajimu UMEMOTO 			error = 0;
54833841545SHajimu UMEMOTO 			break;
54933841545SHajimu UMEMOTO 		}
55033841545SHajimu UMEMOTO 		goto bad;
55133841545SHajimu UMEMOTO 	}
55233841545SHajimu UMEMOTO     }
55333841545SHajimu UMEMOTO 
55433841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
55533841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
55633841545SHajimu UMEMOTO #ifdef _IP_VHL
55733841545SHajimu UMEMOTO 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
55833841545SHajimu UMEMOTO #else
55933841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
56033841545SHajimu UMEMOTO #endif
56133841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
56233841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
56333841545SHajimu UMEMOTO 			printf("ip_output: "
56433841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
56533841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
56633841545SHajimu UMEMOTO 			goto bad;
56733841545SHajimu UMEMOTO 		}
56833841545SHajimu UMEMOTO 	} else {
56933841545SHajimu UMEMOTO 		ia = ifatoia(ro->ro_rt->rt_ifa);
57033841545SHajimu UMEMOTO 		ifp = ro->ro_rt->rt_ifp;
57133841545SHajimu UMEMOTO 	}
57233841545SHajimu UMEMOTO 
57333841545SHajimu UMEMOTO 	/* make it flipped, again. */
574fd8e4ebcSMike Barcroft 	ip->ip_len = ntohs(ip->ip_len);
575fd8e4ebcSMike Barcroft 	ip->ip_off = ntohs(ip->ip_off);
57633841545SHajimu UMEMOTO skip_ipsec:
57733841545SHajimu UMEMOTO #endif /*IPSEC*/
57833841545SHajimu UMEMOTO 
579fed1c7e9SSøren Schmidt 	/*
580fed1c7e9SSøren Schmidt 	 * IpHack's section.
581fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
582e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
583fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
584fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
585fed1c7e9SSøren Schmidt 	 */
586c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
587c4ac87eaSDarren Reed 	/*
588c4ac87eaSDarren Reed 	 * Run through list of hooks for output packets.
589c4ac87eaSDarren Reed 	 */
590c4ac87eaSDarren Reed 	m1 = m;
591c4ac87eaSDarren Reed 	pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
592fc2ffbe6SPoul-Henning Kamp 	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
593c4ac87eaSDarren Reed 		if (pfh->pfil_func) {
594c4ac87eaSDarren Reed 			rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
595c4ac87eaSDarren Reed 			if (rv) {
596c4ac87eaSDarren Reed 				error = EHOSTUNREACH;
5971ee25934SPeter Wemm 				goto done;
5981ee25934SPeter Wemm 			}
599c4ac87eaSDarren Reed 			m = m1;
600c4ac87eaSDarren Reed 			if (m == NULL)
601c4ac87eaSDarren Reed 				goto done;
602c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
603c4ac87eaSDarren Reed 		}
604c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
605fed1c7e9SSøren Schmidt 
606df8bae1dSRodney W. Grimes 	/*
607e7319babSPoul-Henning Kamp 	 * Check with the firewall...
6083efc3014SJulian Elischer 	 * but not if we are already being fwd'd from a firewall.
609e7319babSPoul-Henning Kamp 	 */
6102b25acc1SLuigi Rizzo 	if (fw_enable && IPFW_LOADED && !args.next_hop) {
6119de9737fSPeter Wemm 		struct sockaddr_in *old = dst;
612b715f178SLuigi Rizzo 
6132b25acc1SLuigi Rizzo 		args.m = m;
6142b25acc1SLuigi Rizzo 		args.next_hop = dst;
6152b25acc1SLuigi Rizzo 		args.oif = ifp;
6162b25acc1SLuigi Rizzo 		off = ip_fw_chk_ptr(&args);
6172b25acc1SLuigi Rizzo 		m = args.m;
6182b25acc1SLuigi Rizzo 		dst = args.next_hop;
6192b25acc1SLuigi Rizzo 
620b715f178SLuigi Rizzo                 /*
621b715f178SLuigi Rizzo 		 * On return we must do the following:
622507b4b54SLuigi Rizzo 		 * m == NULL	-> drop the pkt (old interface, deprecated)
623aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
624b715f178SLuigi Rizzo 		 * 1<=off<= 0xffff		-> DIVERT
625aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
626aa1f5daaSJonathan Lemon 		 * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
627b715f178SLuigi Rizzo 		 * dst != old			-> IPFIREWALL_FORWARD
628b715f178SLuigi Rizzo 		 * off==0, dst==old		-> accept
629aa1f5daaSJonathan Lemon 		 * If some of the above modules are not compiled in, then
630b715f178SLuigi Rizzo 		 * we should't have to check the corresponding condition
631b715f178SLuigi Rizzo 		 * (because the ipfw control socket should not accept
632b715f178SLuigi Rizzo 		 * unsupported rules), but better play safe and drop
633b715f178SLuigi Rizzo 		 * packets in case of doubt.
634b715f178SLuigi Rizzo 		 */
635d60315beSLuigi Rizzo 		if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
636507b4b54SLuigi Rizzo 			if (m)
637507b4b54SLuigi Rizzo 				m_freem(m);
638507b4b54SLuigi Rizzo 			error = EACCES;
639507b4b54SLuigi Rizzo 			goto done;
640507b4b54SLuigi Rizzo 		}
641d60315beSLuigi Rizzo 		ip = mtod(m, struct ip *);
642b715f178SLuigi Rizzo 		if (off == 0 && dst == old)		/* common case */
643b715f178SLuigi Rizzo 			goto pass;
6447b109fa4SLuigi Rizzo                 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
645b715f178SLuigi Rizzo 			/*
646b715f178SLuigi Rizzo 			 * pass the pkt to dummynet. Need to include
647f0a53591SLuigi Rizzo 			 * pipe number, m, ifp, ro, dst because these are
648b715f178SLuigi Rizzo 			 * not recomputed in the next pass.
649b715f178SLuigi Rizzo 			 * All other parameters have been already used and
650b715f178SLuigi Rizzo 			 * so they are not needed anymore.
651b715f178SLuigi Rizzo 			 * XXX note: if the ifp or ro entry are deleted
652b715f178SLuigi Rizzo 			 * while a pkt is in dummynet, we are in trouble!
653b715f178SLuigi Rizzo 			 */
6542b25acc1SLuigi Rizzo 			args.ro = ro;
6552b25acc1SLuigi Rizzo 			args.dst = dst;
6562b25acc1SLuigi Rizzo 			args.flags = flags;
6572b25acc1SLuigi Rizzo 
6582b25acc1SLuigi Rizzo 			error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT,
6592b25acc1SLuigi Rizzo 				&args);
660b715f178SLuigi Rizzo 			goto done;
661b715f178SLuigi Rizzo 		}
662b715f178SLuigi Rizzo #ifdef IPDIVERT
6638948e4baSArchie Cobbs 		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
6648948e4baSArchie Cobbs 			struct mbuf *clone = NULL;
6658948e4baSArchie Cobbs 
6668948e4baSArchie Cobbs 			/* Clone packet if we're doing a 'tee' */
6678948e4baSArchie Cobbs 			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
6688948e4baSArchie Cobbs 				clone = m_dup(m, M_DONTWAIT);
6698948e4baSArchie Cobbs 
670ea53ecd9SJonathan Lemon 			/*
671ea53ecd9SJonathan Lemon 			 * XXX
672ea53ecd9SJonathan Lemon 			 * delayed checksums are not currently compatible
673ea53ecd9SJonathan Lemon 			 * with divert sockets.
674ea53ecd9SJonathan Lemon 			 */
675ea53ecd9SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
676ea53ecd9SJonathan Lemon 				in_delayed_cksum(m);
677ea53ecd9SJonathan Lemon 				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
678ea53ecd9SJonathan Lemon 			}
679ea53ecd9SJonathan Lemon 
6808948e4baSArchie Cobbs 			/* Restore packet header fields to original values */
681fd8e4ebcSMike Barcroft 			ip->ip_len = htons(ip->ip_len);
682fd8e4ebcSMike Barcroft 			ip->ip_off = htons(ip->ip_off);
6838948e4baSArchie Cobbs 
6848948e4baSArchie Cobbs 			/* Deliver packet to divert input routine */
6852b25acc1SLuigi Rizzo 			divert_packet(m, 0, off & 0xffff, args.divert_rule);
6868948e4baSArchie Cobbs 
6878948e4baSArchie Cobbs 			/* If 'tee', continue with original packet */
6888948e4baSArchie Cobbs 			if (clone != NULL) {
6898948e4baSArchie Cobbs 				m = clone;
6908948e4baSArchie Cobbs 				ip = mtod(m, struct ip *);
6918948e4baSArchie Cobbs 				goto pass;
6928948e4baSArchie Cobbs 			}
693b715f178SLuigi Rizzo 			goto done;
694b715f178SLuigi Rizzo 		}
695b715f178SLuigi Rizzo #endif
696b715f178SLuigi Rizzo 
6972b25acc1SLuigi Rizzo 		/* IPFIREWALL_FORWARD */
6982b25acc1SLuigi Rizzo 		/*
6992b25acc1SLuigi Rizzo 		 * Check dst to make sure it is directly reachable on the
700f9e354dfSJulian Elischer 		 * interface we previously thought it was.
701f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
702f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
703f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
704f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
705f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
706f9e354dfSJulian Elischer 		 * And I'm babbling.
707f9e354dfSJulian Elischer 		 */
7082b25acc1SLuigi Rizzo 		if (off == 0 && old != dst) { /* FORWARD, dst has changed */
7092b25acc1SLuigi Rizzo #if 0
7102b25acc1SLuigi Rizzo 			/*
7112b25acc1SLuigi Rizzo 			 * XXX To improve readability, this block should be
7122b25acc1SLuigi Rizzo 			 * changed into a function call as below:
7132b25acc1SLuigi Rizzo 			 */
7142b25acc1SLuigi Rizzo 			error = ip_ipforward(&m, &dst, &ifp);
7152b25acc1SLuigi Rizzo 			if (error)
7162b25acc1SLuigi Rizzo 				goto bad;
7172b25acc1SLuigi Rizzo 			if (m == NULL) /* ip_input consumed the mbuf */
7182b25acc1SLuigi Rizzo 				goto done;
7192b25acc1SLuigi Rizzo #else
720f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
721f9e354dfSJulian Elischer 
7222b25acc1SLuigi Rizzo 			/*
7232b25acc1SLuigi Rizzo 			 * XXX sro_fwd below is static, and a pointer
7242b25acc1SLuigi Rizzo 			 * to it gets passed to routines downstream.
7252b25acc1SLuigi Rizzo 			 * This could have surprisingly bad results in
7262b25acc1SLuigi Rizzo 			 * practice, because its content is overwritten
7272b25acc1SLuigi Rizzo 			 * by subsequent packets.
7282b25acc1SLuigi Rizzo 			 */
729f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
7302b25acc1SLuigi Rizzo 			static struct route sro_fwd;
7312b25acc1SLuigi Rizzo 			struct route *ro_fwd = &sro_fwd;
7322b25acc1SLuigi Rizzo 
7332b25acc1SLuigi Rizzo #if 0
7342b25acc1SLuigi Rizzo 			print_ip("IPFIREWALL_FORWARD: New dst ip: ",
7352b25acc1SLuigi Rizzo 			    dst->sin_addr, "\n");
736f9e354dfSJulian Elischer #endif
7372b25acc1SLuigi Rizzo 
738f9e354dfSJulian Elischer 			/*
739f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
7402b25acc1SLuigi Rizzo 			 * to a local socket. If so, then we should somehow
741f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
742f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
743f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
744f9e354dfSJulian Elischer 			 * you want to forward until they are being output
745f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
746f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
747f9e354dfSJulian Elischer 			 * we would not be able to control what happens
748f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
749f9e354dfSJulian Elischer 			 * it is done through a ISR.
750f9e354dfSJulian Elischer 			 */
751ca925d9cSJonathan Lemon 			LIST_FOREACH(ia,
752ca925d9cSJonathan Lemon 			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
753f9e354dfSJulian Elischer 				/*
754f9e354dfSJulian Elischer 				 * If the addr to forward to is one
755f9e354dfSJulian Elischer 				 * of ours, we pretend to
756f9e354dfSJulian Elischer 				 * be the destination for this packet.
757f9e354dfSJulian Elischer 				 */
758f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
759f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
760f9e354dfSJulian Elischer 					break;
761f9e354dfSJulian Elischer 			}
7622b25acc1SLuigi Rizzo 			if (ia) {	/* tell ip_input "dont filter" */
7632b25acc1SLuigi Rizzo 				struct m_hdr tag;
7642b25acc1SLuigi Rizzo 
7652b25acc1SLuigi Rizzo 				tag.mh_type = MT_TAG;
7662b25acc1SLuigi Rizzo 				tag.mh_flags = PACKET_TAG_IPFORWARD;
7672b25acc1SLuigi Rizzo 				tag.mh_data = (caddr_t)args.next_hop;
7682b25acc1SLuigi Rizzo 				tag.mh_next = m;
7692b25acc1SLuigi Rizzo 
770f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
771f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
77220c822f3SJonathan Lemon 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
77320c822f3SJonathan Lemon 					m->m_pkthdr.csum_flags |=
77420c822f3SJonathan Lemon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
77520c822f3SJonathan Lemon 					m0->m_pkthdr.csum_data = 0xffff;
77620c822f3SJonathan Lemon 				}
77720c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
77820c822f3SJonathan Lemon 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
779fd8e4ebcSMike Barcroft 				ip->ip_len = htons(ip->ip_len);
780fd8e4ebcSMike Barcroft 				ip->ip_off = htons(ip->ip_off);
7812b25acc1SLuigi Rizzo 				ip_input((struct mbuf *)&tag);
782f9e354dfSJulian Elischer 				goto done;
783f9e354dfSJulian Elischer 			}
784f9e354dfSJulian Elischer 			/* Some of the logic for this was
785f9e354dfSJulian Elischer 			 * nicked from above.
786f9e354dfSJulian Elischer 			 *
787f9e354dfSJulian Elischer 			 * This rewrites the cached route in a local PCB.
788f9e354dfSJulian Elischer 			 * Is this what we want to do?
789f9e354dfSJulian Elischer 			 */
790f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
791f9e354dfSJulian Elischer 
792f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
793f9e354dfSJulian Elischer 			rtalloc_ign(ro_fwd, RTF_PRCLONING);
794f9e354dfSJulian Elischer 
795f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt == 0) {
796f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
797f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
798f9e354dfSJulian Elischer 				goto bad;
799f9e354dfSJulian Elischer 			}
800f9e354dfSJulian Elischer 
801f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
802f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
803f9e354dfSJulian Elischer 			ro_fwd->ro_rt->rt_use++;
804f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
8052b25acc1SLuigi Rizzo 				dst = (struct sockaddr_in *)
8062b25acc1SLuigi Rizzo 					ro_fwd->ro_rt->rt_gateway;
807f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
808f9e354dfSJulian Elischer 				isbroadcast =
809f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
810f9e354dfSJulian Elischer 			else
811f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
8123f9e3122SYaroslav Tykhiy 			if (ro->ro_rt)
813f9e354dfSJulian Elischer 				RTFREE(ro->ro_rt);
814f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
815f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
816f9e354dfSJulian Elischer 
8172b25acc1SLuigi Rizzo #endif	/* ... block to be put into a function */
818f9e354dfSJulian Elischer 			/*
819f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
820f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
821f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
822f9e354dfSJulian Elischer 			 */
8232b25acc1SLuigi Rizzo 			if (src_was_INADDR_ANY)
824f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
825b715f178SLuigi Rizzo 			goto pass ;
826f9e354dfSJulian Elischer 		}
8272b25acc1SLuigi Rizzo 
828b715f178SLuigi Rizzo                 /*
829b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
830b715f178SLuigi Rizzo                  * we have to drop the pkt
831b715f178SLuigi Rizzo                  */
832b715f178SLuigi Rizzo 		m_freem(m);
833b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
834b715f178SLuigi Rizzo                 goto done;
83593e0e116SJulian Elischer 	}
836e7319babSPoul-Henning Kamp 
837b715f178SLuigi Rizzo pass:
83851c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
83951c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
84051c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
84151c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
84251c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
84351c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
84451c8ec4aSRuslan Ermilov 			goto bad;
84551c8ec4aSRuslan Ermilov 		}
84651c8ec4aSRuslan Ermilov 	}
84751c8ec4aSRuslan Ermilov 
848206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
849206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
850db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
851db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
852db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
853db4f9cc7SJonathan Lemon 	}
854206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
855db4f9cc7SJonathan Lemon 
856e7319babSPoul-Henning Kamp 	/*
857db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
858db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
859df8bae1dSRodney W. Grimes 	 */
860db4f9cc7SJonathan Lemon 	if ((u_short)ip->ip_len <= ifp->if_mtu ||
861db4f9cc7SJonathan Lemon 	    ifp->if_hwassist & CSUM_FRAGMENT) {
862fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
863fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
864df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
865db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
8669c9137eaSGarrett Wollman 			if (ip->ip_vhl == IP_VHL_BORING) {
8679c9137eaSGarrett Wollman 				ip->ip_sum = in_cksum_hdr(ip);
8689c9137eaSGarrett Wollman 			} else {
869df8bae1dSRodney W. Grimes 				ip->ip_sum = in_cksum(m, hlen);
8709c9137eaSGarrett Wollman 			}
871db4f9cc7SJonathan Lemon 		}
8725da9f8faSJosef Karthauser 
8735da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
87438c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
8755da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
8765da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
8775da9f8faSJosef Karthauser 		}
8785da9f8faSJosef Karthauser 
87933841545SHajimu UMEMOTO #ifdef IPSEC
88033841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
88133841545SHajimu UMEMOTO 		ipsec_delaux(m);
88233841545SHajimu UMEMOTO #endif
88333841545SHajimu UMEMOTO 
884df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
885df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
886df8bae1dSRodney W. Grimes 		goto done;
887df8bae1dSRodney W. Grimes 	}
888df8bae1dSRodney W. Grimes 	/*
889df8bae1dSRodney W. Grimes 	 * Too large for interface; fragment if possible.
890df8bae1dSRodney W. Grimes 	 * Must be able to put at least 8 bytes per fragment.
891df8bae1dSRodney W. Grimes 	 */
892df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
893df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
8943d1f141bSGarrett Wollman 		/*
8953d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
8963d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
8973d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
8983d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
8993d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
9003d1f141bSGarrett Wollman 		 */
9013d1f141bSGarrett Wollman 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
9023d1f141bSGarrett Wollman 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
9033d1f141bSGarrett Wollman 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
9043d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
9053d1f141bSGarrett Wollman 		}
906df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
907df8bae1dSRodney W. Grimes 		goto bad;
908df8bae1dSRodney W. Grimes 	}
909df8bae1dSRodney W. Grimes 	len = (ifp->if_mtu - hlen) &~ 7;
910df8bae1dSRodney W. Grimes 	if (len < 8) {
911df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
912df8bae1dSRodney W. Grimes 		goto bad;
913df8bae1dSRodney W. Grimes 	}
914df8bae1dSRodney W. Grimes 
915db4f9cc7SJonathan Lemon 	/*
916db4f9cc7SJonathan Lemon 	 * if the interface will not calculate checksums on
917db4f9cc7SJonathan Lemon 	 * fragmented packets, then do it here.
918db4f9cc7SJonathan Lemon 	 */
919db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
920db4f9cc7SJonathan Lemon 	    (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
921db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
922db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
923db4f9cc7SJonathan Lemon 	}
924db4f9cc7SJonathan Lemon 
92598cb733cSKenneth D. Merry 	if (len > PAGE_SIZE) {
92698cb733cSKenneth D. Merry 		/*
92798cb733cSKenneth D. Merry 		 * Fragement large datagrams such that each segment
92898cb733cSKenneth D. Merry 		 * contains a multiple of PAGE_SIZE amount of data,
92998cb733cSKenneth D. Merry 		 * plus headers. This enables a receiver to perform
93098cb733cSKenneth D. Merry 		 * page-flipping zero-copy optimizations.
93198cb733cSKenneth D. Merry 		 */
93298cb733cSKenneth D. Merry 
93398cb733cSKenneth D. Merry 		int newlen;
93498cb733cSKenneth D. Merry 		struct mbuf *mtmp;
93598cb733cSKenneth D. Merry 
93698cb733cSKenneth D. Merry 		for (mtmp = m, off = 0;
93798cb733cSKenneth D. Merry 		     mtmp && ((off + mtmp->m_len) <= ifp->if_mtu);
93898cb733cSKenneth D. Merry 		     mtmp = mtmp->m_next) {
93998cb733cSKenneth D. Merry 			off += mtmp->m_len;
94098cb733cSKenneth D. Merry 		}
94198cb733cSKenneth D. Merry 		/*
94298cb733cSKenneth D. Merry 		 * firstlen (off - hlen) must be aligned on an
94398cb733cSKenneth D. Merry 		 * 8-byte boundary
94498cb733cSKenneth D. Merry 		 */
94598cb733cSKenneth D. Merry 		if (off < hlen)
94698cb733cSKenneth D. Merry 			goto smart_frag_failure;
94798cb733cSKenneth D. Merry 		off = ((off - hlen) & ~7) + hlen;
94898cb733cSKenneth D. Merry 		newlen = (~PAGE_MASK) & ifp->if_mtu;
94998cb733cSKenneth D. Merry 		if ((newlen + sizeof (struct ip)) > ifp->if_mtu) {
95098cb733cSKenneth D. Merry 			/* we failed, go back the default */
95198cb733cSKenneth D. Merry smart_frag_failure:
95298cb733cSKenneth D. Merry 			newlen = len;
95398cb733cSKenneth D. Merry 			off = hlen + len;
95498cb733cSKenneth D. Merry 		}
95598cb733cSKenneth D. Merry 
95698cb733cSKenneth D. Merry /*		printf("ipfrag: len = %d, hlen = %d, mhlen = %d, newlen = %d, off = %d\n",
95798cb733cSKenneth D. Merry 		len, hlen, sizeof (struct ip), newlen, off);*/
95898cb733cSKenneth D. Merry 
95998cb733cSKenneth D. Merry 		len = newlen;
96098cb733cSKenneth D. Merry 
96198cb733cSKenneth D. Merry 	} else {
96298cb733cSKenneth D. Merry 		off = hlen + len;
96398cb733cSKenneth D. Merry 	}
96498cb733cSKenneth D. Merry 
96598cb733cSKenneth D. Merry 
96698cb733cSKenneth D. Merry 
967df8bae1dSRodney W. Grimes     {
96898cb733cSKenneth D. Merry 	int mhlen, firstlen = off - hlen;
969df8bae1dSRodney W. Grimes 	struct mbuf **mnext = &m->m_nextpkt;
970db4f9cc7SJonathan Lemon 	int nfrags = 1;
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes 	/*
973df8bae1dSRodney W. Grimes 	 * Loop through length of segment after first fragment,
974df8bae1dSRodney W. Grimes 	 * make new header and copy data of each part and link onto chain.
975df8bae1dSRodney W. Grimes 	 */
976df8bae1dSRodney W. Grimes 	m0 = m;
977df8bae1dSRodney W. Grimes 	mhlen = sizeof (struct ip);
97898cb733cSKenneth D. Merry 	for (; off < (u_short)ip->ip_len; off += len) {
979df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
980df8bae1dSRodney W. Grimes 		if (m == 0) {
981df8bae1dSRodney W. Grimes 			error = ENOBUFS;
982df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
983df8bae1dSRodney W. Grimes 			goto sendorfree;
984df8bae1dSRodney W. Grimes 		}
985db4f9cc7SJonathan Lemon 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
986df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
987df8bae1dSRodney W. Grimes 		mhip = mtod(m, struct ip *);
988df8bae1dSRodney W. Grimes 		*mhip = *ip;
989df8bae1dSRodney W. Grimes 		if (hlen > sizeof (struct ip)) {
990df8bae1dSRodney W. Grimes 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9919c9137eaSGarrett Wollman 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
992df8bae1dSRodney W. Grimes 		}
993df8bae1dSRodney W. Grimes 		m->m_len = mhlen;
994cc22c7a7SRuslan Ermilov 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
995df8bae1dSRodney W. Grimes 		if (off + len >= (u_short)ip->ip_len)
996df8bae1dSRodney W. Grimes 			len = (u_short)ip->ip_len - off;
997df8bae1dSRodney W. Grimes 		else
998df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
999df8bae1dSRodney W. Grimes 		mhip->ip_len = htons((u_short)(len + mhlen));
1000df8bae1dSRodney W. Grimes 		m->m_next = m_copy(m0, off, len);
1001df8bae1dSRodney W. Grimes 		if (m->m_next == 0) {
1002df8bae1dSRodney W. Grimes 			(void) m_free(m);
1003df8bae1dSRodney W. Grimes 			error = ENOBUFS;	/* ??? */
1004df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
1005df8bae1dSRodney W. Grimes 			goto sendorfree;
1006df8bae1dSRodney W. Grimes 		}
1007df8bae1dSRodney W. Grimes 		m->m_pkthdr.len = mhlen + len;
1008df8bae1dSRodney W. Grimes 		m->m_pkthdr.rcvif = (struct ifnet *)0;
10094ed84624SRobert Watson #ifdef MAC
10104ed84624SRobert Watson 		mac_create_fragment(m0, m);
10114ed84624SRobert Watson #endif
1012db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
1013fd8e4ebcSMike Barcroft 		mhip->ip_off = htons(mhip->ip_off);
1014df8bae1dSRodney W. Grimes 		mhip->ip_sum = 0;
1015db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
10169c9137eaSGarrett Wollman 			if (mhip->ip_vhl == IP_VHL_BORING) {
1017e2184122SBruce Evans 				mhip->ip_sum = in_cksum_hdr(mhip);
10189c9137eaSGarrett Wollman 			} else {
1019df8bae1dSRodney W. Grimes 				mhip->ip_sum = in_cksum(m, mhlen);
10209c9137eaSGarrett Wollman 			}
1021db4f9cc7SJonathan Lemon 		}
1022df8bae1dSRodney W. Grimes 		*mnext = m;
1023df8bae1dSRodney W. Grimes 		mnext = &m->m_nextpkt;
1024db4f9cc7SJonathan Lemon 		nfrags++;
1025df8bae1dSRodney W. Grimes 	}
1026db4f9cc7SJonathan Lemon 	ipstat.ips_ofragments += nfrags;
1027db4f9cc7SJonathan Lemon 
1028db4f9cc7SJonathan Lemon 	/* set first/last markers for fragment chain */
1029db4f9cc7SJonathan Lemon 	m->m_flags |= M_LASTFRAG;
1030db4f9cc7SJonathan Lemon 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1031db4f9cc7SJonathan Lemon 	m0->m_pkthdr.csum_data = nfrags;
1032db4f9cc7SJonathan Lemon 
1033df8bae1dSRodney W. Grimes 	/*
1034df8bae1dSRodney W. Grimes 	 * Update first fragment by trimming what's been copied out
1035df8bae1dSRodney W. Grimes 	 * and updating header, then send each fragment (in order).
1036df8bae1dSRodney W. Grimes 	 */
1037df8bae1dSRodney W. Grimes 	m = m0;
1038df8bae1dSRodney W. Grimes 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
1039df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hlen + firstlen;
1040df8bae1dSRodney W. Grimes 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
104104287599SRuslan Ermilov 	ip->ip_off |= IP_MF;
1042fd8e4ebcSMike Barcroft 	ip->ip_off = htons(ip->ip_off);
1043df8bae1dSRodney W. Grimes 	ip->ip_sum = 0;
1044db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_IP) {
10459c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
10469c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
10479c9137eaSGarrett Wollman 		} else {
1048df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
10499c9137eaSGarrett Wollman 		}
1050db4f9cc7SJonathan Lemon 	}
1051df8bae1dSRodney W. Grimes sendorfree:
1052df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m0) {
1053df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
1054df8bae1dSRodney W. Grimes 		m->m_nextpkt = 0;
105533841545SHajimu UMEMOTO #ifdef IPSEC
105633841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
105733841545SHajimu UMEMOTO 		ipsec_delaux(m);
105833841545SHajimu UMEMOTO #endif
105907203494SDaniel C. Sobral 		if (error == 0) {
1060fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
106107203494SDaniel C. Sobral 			if (ia != NULL) {
1062fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
1063fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
106407203494SDaniel C. Sobral 			}
1065fe937674SJosef Karthauser 
1066df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
1067df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
1068fe937674SJosef Karthauser 		} else
1069df8bae1dSRodney W. Grimes 			m_freem(m);
1070df8bae1dSRodney W. Grimes 	}
1071df8bae1dSRodney W. Grimes 
1072df8bae1dSRodney W. Grimes 	if (error == 0)
1073df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
1074df8bae1dSRodney W. Grimes     }
1075df8bae1dSRodney W. Grimes done:
10766a800098SYoshinobu Inoue #ifdef IPSEC
10776a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
10786a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
10796a800098SYoshinobu Inoue 		ro->ro_rt = NULL;
10806a800098SYoshinobu Inoue 	}
10816a800098SYoshinobu Inoue 	if (sp != NULL) {
10826a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
10836a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
10846a800098SYoshinobu Inoue 		key_freesp(sp);
10856a800098SYoshinobu Inoue 	}
10866a800098SYoshinobu Inoue #endif /* IPSEC */
1087df8bae1dSRodney W. Grimes 	return (error);
1088df8bae1dSRodney W. Grimes bad:
10893528d68fSBill Paul 	m_freem(m);
1090df8bae1dSRodney W. Grimes 	goto done;
1091df8bae1dSRodney W. Grimes }
1092df8bae1dSRodney W. Grimes 
10931c238475SJonathan Lemon void
1094db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1095db4f9cc7SJonathan Lemon {
1096db4f9cc7SJonathan Lemon 	struct ip *ip;
1097db4f9cc7SJonathan Lemon 	u_short csum, offset;
1098db4f9cc7SJonathan Lemon 
1099db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
1100db4f9cc7SJonathan Lemon 	offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1101db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1102206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1103206a3274SRuslan Ermilov 		csum = 0xffff;
1104db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1105db4f9cc7SJonathan Lemon 
1106db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1107db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1108db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1109db4f9cc7SJonathan Lemon 		/*
1110db4f9cc7SJonathan Lemon 		 * XXX
1111db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1112db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1113db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1114db4f9cc7SJonathan Lemon 		 */
1115db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1116db4f9cc7SJonathan Lemon 	}
1117db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1118db4f9cc7SJonathan Lemon }
1119db4f9cc7SJonathan Lemon 
1120df8bae1dSRodney W. Grimes /*
1121df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1122df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1123df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1124072b9b24SPaul Traina  *
1125072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1126df8bae1dSRodney W. Grimes  */
1127df8bae1dSRodney W. Grimes static struct mbuf *
1128df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen)
1129df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1130df8bae1dSRodney W. Grimes 	struct mbuf *opt;
1131df8bae1dSRodney W. Grimes 	int *phlen;
1132df8bae1dSRodney W. Grimes {
1133df8bae1dSRodney W. Grimes 	register struct ipoption *p = mtod(opt, struct ipoption *);
1134df8bae1dSRodney W. Grimes 	struct mbuf *n;
1135df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
1136df8bae1dSRodney W. Grimes 	unsigned optlen;
1137df8bae1dSRodney W. Grimes 
1138df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
1139cb7641e8SMaxim Konovalov 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) {
1140cb7641e8SMaxim Konovalov 		*phlen = 0;
1141df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1142cb7641e8SMaxim Konovalov 	}
1143df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1144df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1145df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1146df8bae1dSRodney W. Grimes 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1147cb7641e8SMaxim Konovalov 		if (n == 0) {
1148cb7641e8SMaxim Konovalov 			*phlen = 0;
1149df8bae1dSRodney W. Grimes 			return (m);
1150cb7641e8SMaxim Konovalov 		}
11515db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
11524ed84624SRobert Watson #ifdef MAC
11534ed84624SRobert Watson 		mac_create_mbuf_from_mbuf(m, n);
11544ed84624SRobert Watson #endif
1155df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1156df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1157df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1158df8bae1dSRodney W. Grimes 		n->m_next = m;
1159df8bae1dSRodney W. Grimes 		m = n;
1160df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1161df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
116294a5d9b6SDavid Greenman 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1163df8bae1dSRodney W. Grimes 	} else {
1164df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1165df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1166df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1167df8bae1dSRodney W. Grimes 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1168df8bae1dSRodney W. Grimes 	}
1169df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
11700453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1171df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
11729c9137eaSGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1173df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1174df8bae1dSRodney W. Grimes 	return (m);
1175df8bae1dSRodney W. Grimes }
1176df8bae1dSRodney W. Grimes 
1177df8bae1dSRodney W. Grimes /*
1178df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1179df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1180df8bae1dSRodney W. Grimes  */
1181beec8214SDarren Reed int
1182df8bae1dSRodney W. Grimes ip_optcopy(ip, jp)
1183df8bae1dSRodney W. Grimes 	struct ip *ip, *jp;
1184df8bae1dSRodney W. Grimes {
1185df8bae1dSRodney W. Grimes 	register u_char *cp, *dp;
1186df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1187df8bae1dSRodney W. Grimes 
1188df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1189df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
11909c9137eaSGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1191df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1192df8bae1dSRodney W. Grimes 		opt = cp[0];
1193df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1194df8bae1dSRodney W. Grimes 			break;
1195df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1196df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1197df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1198df8bae1dSRodney W. Grimes 			optlen = 1;
1199df8bae1dSRodney W. Grimes 			continue;
1200686cdd19SJun-ichiro itojun Hagino 		}
1201db40007dSAndrew R. Reiter 
1202db40007dSAndrew R. Reiter 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1203db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1204df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1205db40007dSAndrew R. Reiter 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1206db40007dSAndrew R. Reiter 		    ("ip_optcopy: malformed ipv4 option"));
1207db40007dSAndrew R. Reiter 
1208df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1209df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1210df8bae1dSRodney W. Grimes 			optlen = cnt;
1211df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
12120453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1213df8bae1dSRodney W. Grimes 			dp += optlen;
1214df8bae1dSRodney W. Grimes 		}
1215df8bae1dSRodney W. Grimes 	}
1216df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1217df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1218df8bae1dSRodney W. Grimes 	return (optlen);
1219df8bae1dSRodney W. Grimes }
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes /*
1222df8bae1dSRodney W. Grimes  * IP socket option processing.
1223df8bae1dSRodney W. Grimes  */
1224df8bae1dSRodney W. Grimes int
1225cfe8b629SGarrett Wollman ip_ctloutput(so, sopt)
1226df8bae1dSRodney W. Grimes 	struct socket *so;
1227cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1228df8bae1dSRodney W. Grimes {
1229cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1230cfe8b629SGarrett Wollman 	int	error, optval;
1231df8bae1dSRodney W. Grimes 
1232cfe8b629SGarrett Wollman 	error = optval = 0;
1233cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1234cfe8b629SGarrett Wollman 		return (EINVAL);
1235cfe8b629SGarrett Wollman 	}
1236df8bae1dSRodney W. Grimes 
1237cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1238cfe8b629SGarrett Wollman 	case SOPT_SET:
1239cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1240df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1241df8bae1dSRodney W. Grimes #ifdef notyet
1242df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1243df8bae1dSRodney W. Grimes #endif
1244cfe8b629SGarrett Wollman 		{
1245cfe8b629SGarrett Wollman 			struct mbuf *m;
1246cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1247cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1248cfe8b629SGarrett Wollman 				break;
1249cfe8b629SGarrett Wollman 			}
1250b40ce416SJulian Elischer 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1251cfe8b629SGarrett Wollman 			if (m == 0) {
1252cfe8b629SGarrett Wollman 				error = ENOBUFS;
1253cfe8b629SGarrett Wollman 				break;
1254cfe8b629SGarrett Wollman 			}
1255cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1256cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1257cfe8b629SGarrett Wollman 					    m->m_len);
1258cfe8b629SGarrett Wollman 
1259cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1260cfe8b629SGarrett Wollman 					   m));
1261cfe8b629SGarrett Wollman 		}
1262df8bae1dSRodney W. Grimes 
1263df8bae1dSRodney W. Grimes 		case IP_TOS:
1264df8bae1dSRodney W. Grimes 		case IP_TTL:
1265df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1266df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1267df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
126882c23ebaSBill Fenner 		case IP_RECVIF:
12696a800098SYoshinobu Inoue 		case IP_FAITH:
1270cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1271cfe8b629SGarrett Wollman 					    sizeof optval);
1272cfe8b629SGarrett Wollman 			if (error)
1273cfe8b629SGarrett Wollman 				break;
1274df8bae1dSRodney W. Grimes 
1275cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1276df8bae1dSRodney W. Grimes 			case IP_TOS:
1277ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1278df8bae1dSRodney W. Grimes 				break;
1279df8bae1dSRodney W. Grimes 
1280df8bae1dSRodney W. Grimes 			case IP_TTL:
1281ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1282df8bae1dSRodney W. Grimes 				break;
1283df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
1284df8bae1dSRodney W. Grimes 	if (optval) \
1285df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
1286df8bae1dSRodney W. Grimes 	else \
1287df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
1288df8bae1dSRodney W. Grimes 
1289df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1290df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1291df8bae1dSRodney W. Grimes 				break;
1292df8bae1dSRodney W. Grimes 
1293df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1294df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1295df8bae1dSRodney W. Grimes 				break;
1296df8bae1dSRodney W. Grimes 
1297df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1298df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1299df8bae1dSRodney W. Grimes 				break;
130082c23ebaSBill Fenner 
130182c23ebaSBill Fenner 			case IP_RECVIF:
130282c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
130382c23ebaSBill Fenner 				break;
13046a800098SYoshinobu Inoue 
13056a800098SYoshinobu Inoue 			case IP_FAITH:
13066a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
13076a800098SYoshinobu Inoue 				break;
1308df8bae1dSRodney W. Grimes 			}
1309df8bae1dSRodney W. Grimes 			break;
1310df8bae1dSRodney W. Grimes #undef OPTSET
1311df8bae1dSRodney W. Grimes 
1312df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1313f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1314df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1315df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1316df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1317df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1318cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1319df8bae1dSRodney W. Grimes 			break;
1320df8bae1dSRodney W. Grimes 
132133b3ac06SPeter Wemm 		case IP_PORTRANGE:
1322cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1323cfe8b629SGarrett Wollman 					    sizeof optval);
1324cfe8b629SGarrett Wollman 			if (error)
1325cfe8b629SGarrett Wollman 				break;
132633b3ac06SPeter Wemm 
132733b3ac06SPeter Wemm 			switch (optval) {
132833b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
132933b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
133033b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
133133b3ac06SPeter Wemm 				break;
133233b3ac06SPeter Wemm 
133333b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
133433b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
133533b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
133633b3ac06SPeter Wemm 				break;
133733b3ac06SPeter Wemm 
133833b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
133933b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
134033b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
134133b3ac06SPeter Wemm 				break;
134233b3ac06SPeter Wemm 
134333b3ac06SPeter Wemm 			default:
134433b3ac06SPeter Wemm 				error = EINVAL;
134533b3ac06SPeter Wemm 				break;
134633b3ac06SPeter Wemm 			}
1347ce8c72b1SPeter Wemm 			break;
134833b3ac06SPeter Wemm 
13496a800098SYoshinobu Inoue #ifdef IPSEC
13506a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13516a800098SYoshinobu Inoue 		{
13526a800098SYoshinobu Inoue 			caddr_t req;
1353686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
13546a800098SYoshinobu Inoue 			int priv;
13556a800098SYoshinobu Inoue 			struct mbuf *m;
13566a800098SYoshinobu Inoue 			int optname;
13576a800098SYoshinobu Inoue 
13586a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
13596a800098SYoshinobu Inoue 				break;
13606a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
13616a800098SYoshinobu Inoue 				break;
1362b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
136344731cabSJohn Baldwin 				suser(sopt->sopt_td) != 0) ? 0 : 1;
13646a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1365686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
13666a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1367686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
13686a800098SYoshinobu Inoue 			m_freem(m);
13696a800098SYoshinobu Inoue 			break;
13706a800098SYoshinobu Inoue 		}
13716a800098SYoshinobu Inoue #endif /*IPSEC*/
13726a800098SYoshinobu Inoue 
1373df8bae1dSRodney W. Grimes 		default:
1374df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1375df8bae1dSRodney W. Grimes 			break;
1376df8bae1dSRodney W. Grimes 		}
1377df8bae1dSRodney W. Grimes 		break;
1378df8bae1dSRodney W. Grimes 
1379cfe8b629SGarrett Wollman 	case SOPT_GET:
1380cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1381df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1382df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1383cfe8b629SGarrett Wollman 			if (inp->inp_options)
1384cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1385cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1386cfe8b629SGarrett Wollman 							  char *),
1387cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1388cfe8b629SGarrett Wollman 			else
1389cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1390df8bae1dSRodney W. Grimes 			break;
1391df8bae1dSRodney W. Grimes 
1392df8bae1dSRodney W. Grimes 		case IP_TOS:
1393df8bae1dSRodney W. Grimes 		case IP_TTL:
1394df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1395df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1396df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
139782c23ebaSBill Fenner 		case IP_RECVIF:
1398cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
13996a800098SYoshinobu Inoue 		case IP_FAITH:
1400cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1401df8bae1dSRodney W. Grimes 
1402df8bae1dSRodney W. Grimes 			case IP_TOS:
1403ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1404df8bae1dSRodney W. Grimes 				break;
1405df8bae1dSRodney W. Grimes 
1406df8bae1dSRodney W. Grimes 			case IP_TTL:
1407ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1408df8bae1dSRodney W. Grimes 				break;
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1411df8bae1dSRodney W. Grimes 
1412df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1413df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1414df8bae1dSRodney W. Grimes 				break;
1415df8bae1dSRodney W. Grimes 
1416df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1417df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1418df8bae1dSRodney W. Grimes 				break;
1419df8bae1dSRodney W. Grimes 
1420df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1421df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1422df8bae1dSRodney W. Grimes 				break;
142382c23ebaSBill Fenner 
142482c23ebaSBill Fenner 			case IP_RECVIF:
142582c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
142682c23ebaSBill Fenner 				break;
1427cfe8b629SGarrett Wollman 
1428cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1429cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1430cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1431cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1432cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1433cfe8b629SGarrett Wollman 				else
1434cfe8b629SGarrett Wollman 					optval = 0;
1435cfe8b629SGarrett Wollman 				break;
14366a800098SYoshinobu Inoue 
14376a800098SYoshinobu Inoue 			case IP_FAITH:
14386a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
14396a800098SYoshinobu Inoue 				break;
1440df8bae1dSRodney W. Grimes 			}
1441cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1442df8bae1dSRodney W. Grimes 			break;
1443df8bae1dSRodney W. Grimes 
1444df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1445f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1446df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1447df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1448df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1449df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1450cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
145133b3ac06SPeter Wemm 			break;
145233b3ac06SPeter Wemm 
14536a800098SYoshinobu Inoue #ifdef IPSEC
14546a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
14556a800098SYoshinobu Inoue 		{
1456f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
14576a800098SYoshinobu Inoue 			caddr_t req = NULL;
1458686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
14596a800098SYoshinobu Inoue 
1460686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
14616a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1462686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1463686cdd19SJun-ichiro itojun Hagino 			}
1464686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
14656a800098SYoshinobu Inoue 			if (error == 0)
14666a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1467f63e7634SYoshinobu Inoue 			if (error == 0)
14686a800098SYoshinobu Inoue 				m_freem(m);
14696a800098SYoshinobu Inoue 			break;
14706a800098SYoshinobu Inoue 		}
14716a800098SYoshinobu Inoue #endif /*IPSEC*/
14726a800098SYoshinobu Inoue 
1473df8bae1dSRodney W. Grimes 		default:
1474df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1475df8bae1dSRodney W. Grimes 			break;
1476df8bae1dSRodney W. Grimes 		}
1477df8bae1dSRodney W. Grimes 		break;
1478df8bae1dSRodney W. Grimes 	}
1479df8bae1dSRodney W. Grimes 	return (error);
1480df8bae1dSRodney W. Grimes }
1481df8bae1dSRodney W. Grimes 
1482df8bae1dSRodney W. Grimes /*
1483df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1484df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1485df8bae1dSRodney W. Grimes  * with destination address if source routed.
1486df8bae1dSRodney W. Grimes  */
14870312fbe9SPoul-Henning Kamp static int
1488df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m)
1489df8bae1dSRodney W. Grimes 	int optname;
1490df8bae1dSRodney W. Grimes 	struct mbuf **pcbopt;
1491df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1492df8bae1dSRodney W. Grimes {
1493d68fa50cSBruce Evans 	register int cnt, optlen;
1494df8bae1dSRodney W. Grimes 	register u_char *cp;
1495df8bae1dSRodney W. Grimes 	u_char opt;
1496df8bae1dSRodney W. Grimes 
1497df8bae1dSRodney W. Grimes 	/* turn off any old options */
1498df8bae1dSRodney W. Grimes 	if (*pcbopt)
1499df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1500df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1501df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1502df8bae1dSRodney W. Grimes 		/*
1503df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1504df8bae1dSRodney W. Grimes 		 */
1505df8bae1dSRodney W. Grimes 		if (m)
1506df8bae1dSRodney W. Grimes 			(void)m_free(m);
1507df8bae1dSRodney W. Grimes 		return (0);
1508df8bae1dSRodney W. Grimes 	}
1509df8bae1dSRodney W. Grimes 
15100c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1511df8bae1dSRodney W. Grimes 		goto bad;
1512df8bae1dSRodney W. Grimes 	/*
1513df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1514df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1515df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1516df8bae1dSRodney W. Grimes 	 */
1517df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1518df8bae1dSRodney W. Grimes 		goto bad;
1519df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1520df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1521df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1522df8bae1dSRodney W. Grimes 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1523df8bae1dSRodney W. Grimes 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1524df8bae1dSRodney W. Grimes 
1525df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1526df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1527df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1528df8bae1dSRodney W. Grimes 			break;
1529df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1530df8bae1dSRodney W. Grimes 			optlen = 1;
1531df8bae1dSRodney W. Grimes 		else {
1532707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1533707d00a3SJonathan Lemon 				goto bad;
1534df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1535707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1536df8bae1dSRodney W. Grimes 				goto bad;
1537df8bae1dSRodney W. Grimes 		}
1538df8bae1dSRodney W. Grimes 		switch (opt) {
1539df8bae1dSRodney W. Grimes 
1540df8bae1dSRodney W. Grimes 		default:
1541df8bae1dSRodney W. Grimes 			break;
1542df8bae1dSRodney W. Grimes 
1543df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1544df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1545df8bae1dSRodney W. Grimes 			/*
1546df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1547df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1548df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1549df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1550df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1551df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1552df8bae1dSRodney W. Grimes 			 */
1553df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1554df8bae1dSRodney W. Grimes 				goto bad;
1555df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1556df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1557df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1558df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1559df8bae1dSRodney W. Grimes 			/*
1560df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1561df8bae1dSRodney W. Grimes 			 */
1562df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1563df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1564df8bae1dSRodney W. Grimes 			/*
1565df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1566df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1567df8bae1dSRodney W. Grimes 			 */
1568df8bae1dSRodney W. Grimes 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1569df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr)),
1570df8bae1dSRodney W. Grimes 			    (caddr_t)&cp[IPOPT_OFFSET+1],
1571df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
1572df8bae1dSRodney W. Grimes 			break;
1573df8bae1dSRodney W. Grimes 		}
1574df8bae1dSRodney W. Grimes 	}
1575df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1576df8bae1dSRodney W. Grimes 		goto bad;
1577df8bae1dSRodney W. Grimes 	*pcbopt = m;
1578df8bae1dSRodney W. Grimes 	return (0);
1579df8bae1dSRodney W. Grimes 
1580df8bae1dSRodney W. Grimes bad:
1581df8bae1dSRodney W. Grimes 	(void)m_free(m);
1582df8bae1dSRodney W. Grimes 	return (EINVAL);
1583df8bae1dSRodney W. Grimes }
1584df8bae1dSRodney W. Grimes 
1585df8bae1dSRodney W. Grimes /*
1586cfe8b629SGarrett Wollman  * XXX
1587cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1588cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1589cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1590cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1591cfe8b629SGarrett Wollman  */
159233841545SHajimu UMEMOTO 
159333841545SHajimu UMEMOTO /*
159433841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
159533841545SHajimu UMEMOTO  */
159633841545SHajimu UMEMOTO static struct ifnet *
159733841545SHajimu UMEMOTO ip_multicast_if(a, ifindexp)
159833841545SHajimu UMEMOTO 	struct in_addr *a;
159933841545SHajimu UMEMOTO 	int *ifindexp;
160033841545SHajimu UMEMOTO {
160133841545SHajimu UMEMOTO 	int ifindex;
160233841545SHajimu UMEMOTO 	struct ifnet *ifp;
160333841545SHajimu UMEMOTO 
160433841545SHajimu UMEMOTO 	if (ifindexp)
160533841545SHajimu UMEMOTO 		*ifindexp = 0;
160633841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
160733841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
160833841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
160933841545SHajimu UMEMOTO 			return NULL;
1610f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
161133841545SHajimu UMEMOTO 		if (ifindexp)
161233841545SHajimu UMEMOTO 			*ifindexp = ifindex;
161333841545SHajimu UMEMOTO 	} else {
161433841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
161533841545SHajimu UMEMOTO 	}
161633841545SHajimu UMEMOTO 	return ifp;
161733841545SHajimu UMEMOTO }
161833841545SHajimu UMEMOTO 
1619cfe8b629SGarrett Wollman /*
1620df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1621df8bae1dSRodney W. Grimes  */
16220312fbe9SPoul-Henning Kamp static int
1623cfe8b629SGarrett Wollman ip_setmoptions(sopt, imop)
1624cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1625df8bae1dSRodney W. Grimes 	struct ip_moptions **imop;
1626df8bae1dSRodney W. Grimes {
1627cfe8b629SGarrett Wollman 	int error = 0;
1628cfe8b629SGarrett Wollman 	int i;
1629df8bae1dSRodney W. Grimes 	struct in_addr addr;
1630cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1631cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1632cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1633df8bae1dSRodney W. Grimes 	struct route ro;
1634cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
163533841545SHajimu UMEMOTO 	int ifindex;
16369b626c29SGarrett Wollman 	int s;
1637df8bae1dSRodney W. Grimes 
1638df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1639df8bae1dSRodney W. Grimes 		/*
1640df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1641df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1642df8bae1dSRodney W. Grimes 		 */
1643df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1644df8bae1dSRodney W. Grimes 		    M_WAITOK);
1645df8bae1dSRodney W. Grimes 
1646df8bae1dSRodney W. Grimes 		if (imo == NULL)
1647df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1648df8bae1dSRodney W. Grimes 		*imop = imo;
1649df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
165033841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
16511c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1652df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1653df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1654df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1655df8bae1dSRodney W. Grimes 	}
1656df8bae1dSRodney W. Grimes 
1657cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1658f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1659f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1660cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
16615e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
16625e9ae478SGarrett Wollman 			break;
16635e9ae478SGarrett Wollman 		}
1664cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1665cfe8b629SGarrett Wollman 		if (error)
1666f0068c4aSGarrett Wollman 			break;
16671c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1668f0068c4aSGarrett Wollman 			error = EINVAL;
1669f0068c4aSGarrett Wollman 			break;
1670f0068c4aSGarrett Wollman 		}
1671f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1672f0068c4aSGarrett Wollman 		break;
1673f0068c4aSGarrett Wollman 
1674df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1675df8bae1dSRodney W. Grimes 		/*
1676df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1677df8bae1dSRodney W. Grimes 		 */
1678cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1679cfe8b629SGarrett Wollman 		if (error)
1680df8bae1dSRodney W. Grimes 			break;
1681df8bae1dSRodney W. Grimes 		/*
1682df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1683df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1684df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1685df8bae1dSRodney W. Grimes 		 */
1686df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1687df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1688df8bae1dSRodney W. Grimes 			break;
1689df8bae1dSRodney W. Grimes 		}
1690df8bae1dSRodney W. Grimes 		/*
1691df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1692df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1693df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1694df8bae1dSRodney W. Grimes 		 */
169520e8807cSGarrett Wollman 		s = splimp();
169633841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1697df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1698fbc6ab00SBill Fenner 			splx(s);
1699df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1700df8bae1dSRodney W. Grimes 			break;
1701df8bae1dSRodney W. Grimes 		}
1702df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
170333841545SHajimu UMEMOTO 		if (ifindex)
170433841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
170533841545SHajimu UMEMOTO 		else
170633841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
17079b626c29SGarrett Wollman 		splx(s);
1708df8bae1dSRodney W. Grimes 		break;
1709df8bae1dSRodney W. Grimes 
1710df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1711df8bae1dSRodney W. Grimes 		/*
1712df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1713cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1714cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1715cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1716df8bae1dSRodney W. Grimes 		 */
1717cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1718cfe8b629SGarrett Wollman 			u_char ttl;
1719cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1720cfe8b629SGarrett Wollman 			if (error)
1721df8bae1dSRodney W. Grimes 				break;
1722cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1723cfe8b629SGarrett Wollman 		} else {
1724cfe8b629SGarrett Wollman 			u_int ttl;
1725cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1726cfe8b629SGarrett Wollman 					    sizeof ttl);
1727cfe8b629SGarrett Wollman 			if (error)
1728cfe8b629SGarrett Wollman 				break;
1729cfe8b629SGarrett Wollman 			if (ttl > 255)
1730cfe8b629SGarrett Wollman 				error = EINVAL;
1731cfe8b629SGarrett Wollman 			else
1732cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1733df8bae1dSRodney W. Grimes 		}
1734df8bae1dSRodney W. Grimes 		break;
1735df8bae1dSRodney W. Grimes 
1736df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1737df8bae1dSRodney W. Grimes 		/*
1738df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1739cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1740cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1741cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1742df8bae1dSRodney W. Grimes 		 */
1743cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1744cfe8b629SGarrett Wollman 			u_char loop;
1745cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1746cfe8b629SGarrett Wollman 			if (error)
1747df8bae1dSRodney W. Grimes 				break;
1748cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1749cfe8b629SGarrett Wollman 		} else {
1750cfe8b629SGarrett Wollman 			u_int loop;
1751cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1752cfe8b629SGarrett Wollman 					    sizeof loop);
1753cfe8b629SGarrett Wollman 			if (error)
1754cfe8b629SGarrett Wollman 				break;
1755cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1756df8bae1dSRodney W. Grimes 		}
1757df8bae1dSRodney W. Grimes 		break;
1758df8bae1dSRodney W. Grimes 
1759df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1760df8bae1dSRodney W. Grimes 		/*
1761df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1762df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1763df8bae1dSRodney W. Grimes 		 */
1764cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1765cfe8b629SGarrett Wollman 		if (error)
1766df8bae1dSRodney W. Grimes 			break;
1767cfe8b629SGarrett Wollman 
1768cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1769df8bae1dSRodney W. Grimes 			error = EINVAL;
1770df8bae1dSRodney W. Grimes 			break;
1771df8bae1dSRodney W. Grimes 		}
177220e8807cSGarrett Wollman 		s = splimp();
1773df8bae1dSRodney W. Grimes 		/*
1774df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1775df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1776df8bae1dSRodney W. Grimes 		 */
1777cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
17781c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1779df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1780df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1781df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1782cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
1783df8bae1dSRodney W. Grimes 			rtalloc(&ro);
1784df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1785df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
17869b626c29SGarrett Wollman 				splx(s);
1787df8bae1dSRodney W. Grimes 				break;
1788df8bae1dSRodney W. Grimes 			}
1789df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1790df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
1791df8bae1dSRodney W. Grimes 		}
1792df8bae1dSRodney W. Grimes 		else {
179333841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1794df8bae1dSRodney W. Grimes 		}
17959b626c29SGarrett Wollman 
1796df8bae1dSRodney W. Grimes 		/*
1797df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1798df8bae1dSRodney W. Grimes 		 * supports multicast.
1799df8bae1dSRodney W. Grimes 		 */
1800df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1801df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
18029b626c29SGarrett Wollman 			splx(s);
1803df8bae1dSRodney W. Grimes 			break;
1804df8bae1dSRodney W. Grimes 		}
1805df8bae1dSRodney W. Grimes 		/*
1806df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1807df8bae1dSRodney W. Grimes 		 * membership slots are full.
1808df8bae1dSRodney W. Grimes 		 */
1809df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1810df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1811df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1812cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1813df8bae1dSRodney W. Grimes 				break;
1814df8bae1dSRodney W. Grimes 		}
1815df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1816df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
18179b626c29SGarrett Wollman 			splx(s);
1818df8bae1dSRodney W. Grimes 			break;
1819df8bae1dSRodney W. Grimes 		}
1820df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1821df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
18229b626c29SGarrett Wollman 			splx(s);
1823df8bae1dSRodney W. Grimes 			break;
1824df8bae1dSRodney W. Grimes 		}
1825df8bae1dSRodney W. Grimes 		/*
1826df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1827df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1828df8bae1dSRodney W. Grimes 		 */
1829df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1830cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1831df8bae1dSRodney W. Grimes 			error = ENOBUFS;
18329b626c29SGarrett Wollman 			splx(s);
1833df8bae1dSRodney W. Grimes 			break;
1834df8bae1dSRodney W. Grimes 		}
1835df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
18369b626c29SGarrett Wollman 		splx(s);
1837df8bae1dSRodney W. Grimes 		break;
1838df8bae1dSRodney W. Grimes 
1839df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1840df8bae1dSRodney W. Grimes 		/*
1841df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1842df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1843df8bae1dSRodney W. Grimes 		 */
1844cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1845cfe8b629SGarrett Wollman 		if (error)
1846df8bae1dSRodney W. Grimes 			break;
1847cfe8b629SGarrett Wollman 
1848cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1849df8bae1dSRodney W. Grimes 			error = EINVAL;
1850df8bae1dSRodney W. Grimes 			break;
1851df8bae1dSRodney W. Grimes 		}
18529b626c29SGarrett Wollman 
185320e8807cSGarrett Wollman 		s = splimp();
1854df8bae1dSRodney W. Grimes 		/*
1855df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1856df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1857df8bae1dSRodney W. Grimes 		 */
1858cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1859df8bae1dSRodney W. Grimes 			ifp = NULL;
1860df8bae1dSRodney W. Grimes 		else {
186133841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1862df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1863df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
18649b626c29SGarrett Wollman 				splx(s);
1865df8bae1dSRodney W. Grimes 				break;
1866df8bae1dSRodney W. Grimes 			}
1867df8bae1dSRodney W. Grimes 		}
1868df8bae1dSRodney W. Grimes 		/*
1869df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1870df8bae1dSRodney W. Grimes 		 */
1871df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1872df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1873df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1874df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1875cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1876df8bae1dSRodney W. Grimes 				break;
1877df8bae1dSRodney W. Grimes 		}
1878df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1879df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
18809b626c29SGarrett Wollman 			splx(s);
1881df8bae1dSRodney W. Grimes 			break;
1882df8bae1dSRodney W. Grimes 		}
1883df8bae1dSRodney W. Grimes 		/*
1884df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1885df8bae1dSRodney W. Grimes 		 * membership points.
1886df8bae1dSRodney W. Grimes 		 */
1887df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1888df8bae1dSRodney W. Grimes 		/*
1889df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1890df8bae1dSRodney W. Grimes 		 */
1891df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1892df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1893df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
18949b626c29SGarrett Wollman 		splx(s);
1895df8bae1dSRodney W. Grimes 		break;
1896df8bae1dSRodney W. Grimes 
1897df8bae1dSRodney W. Grimes 	default:
1898df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1899df8bae1dSRodney W. Grimes 		break;
1900df8bae1dSRodney W. Grimes 	}
1901df8bae1dSRodney W. Grimes 
1902df8bae1dSRodney W. Grimes 	/*
1903df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1904df8bae1dSRodney W. Grimes 	 */
1905df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
19061c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1907df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1908df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1909df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1910df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1911df8bae1dSRodney W. Grimes 		*imop = NULL;
1912df8bae1dSRodney W. Grimes 	}
1913df8bae1dSRodney W. Grimes 
1914df8bae1dSRodney W. Grimes 	return (error);
1915df8bae1dSRodney W. Grimes }
1916df8bae1dSRodney W. Grimes 
1917df8bae1dSRodney W. Grimes /*
1918df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1919df8bae1dSRodney W. Grimes  */
19200312fbe9SPoul-Henning Kamp static int
1921cfe8b629SGarrett Wollman ip_getmoptions(sopt, imo)
1922cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1923df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1924df8bae1dSRodney W. Grimes {
1925cfe8b629SGarrett Wollman 	struct in_addr addr;
1926df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1927cfe8b629SGarrett Wollman 	int error, optval;
1928cfe8b629SGarrett Wollman 	u_char coptval;
1929df8bae1dSRodney W. Grimes 
1930cfe8b629SGarrett Wollman 	error = 0;
1931cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1932f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1933f0068c4aSGarrett Wollman 		if (imo != NULL)
1934cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1935f0068c4aSGarrett Wollman 		else
1936cfe8b629SGarrett Wollman 			optval = -1;
1937cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1938cfe8b629SGarrett Wollman 		break;
1939f0068c4aSGarrett Wollman 
1940df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1941df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1942cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
194333841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
194433841545SHajimu UMEMOTO 			/* return the value user has set */
194533841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
194633841545SHajimu UMEMOTO 		} else {
1947df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1948cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1949df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1950df8bae1dSRodney W. Grimes 		}
1951cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1952cfe8b629SGarrett Wollman 		break;
1953df8bae1dSRodney W. Grimes 
1954df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1955cfe8b629SGarrett Wollman 		if (imo == 0)
1956cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1957cfe8b629SGarrett Wollman 		else
1958cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
1959cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1960cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1961cfe8b629SGarrett Wollman 		else
1962cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1963cfe8b629SGarrett Wollman 		break;
1964df8bae1dSRodney W. Grimes 
1965df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1966cfe8b629SGarrett Wollman 		if (imo == 0)
1967cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1968cfe8b629SGarrett Wollman 		else
1969cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
1970cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1971cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1972cfe8b629SGarrett Wollman 		else
1973cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1974cfe8b629SGarrett Wollman 		break;
1975df8bae1dSRodney W. Grimes 
1976df8bae1dSRodney W. Grimes 	default:
1977cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1978cfe8b629SGarrett Wollman 		break;
1979df8bae1dSRodney W. Grimes 	}
1980cfe8b629SGarrett Wollman 	return (error);
1981df8bae1dSRodney W. Grimes }
1982df8bae1dSRodney W. Grimes 
1983df8bae1dSRodney W. Grimes /*
1984df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1985df8bae1dSRodney W. Grimes  */
1986df8bae1dSRodney W. Grimes void
1987df8bae1dSRodney W. Grimes ip_freemoptions(imo)
1988df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1989df8bae1dSRodney W. Grimes {
1990df8bae1dSRodney W. Grimes 	register int i;
1991df8bae1dSRodney W. Grimes 
1992df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1993df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1994df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1995df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1996df8bae1dSRodney W. Grimes 	}
1997df8bae1dSRodney W. Grimes }
1998df8bae1dSRodney W. Grimes 
1999df8bae1dSRodney W. Grimes /*
2000df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
2001df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
2002df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
2003f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
2004f5fea3ddSPaul Traina  * replicating that code here.
2005df8bae1dSRodney W. Grimes  */
2006df8bae1dSRodney W. Grimes static void
200786b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen)
2008df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
2009df8bae1dSRodney W. Grimes 	register struct mbuf *m;
2010df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
201186b1d6d2SBill Fenner 	int hlen;
2012df8bae1dSRodney W. Grimes {
2013df8bae1dSRodney W. Grimes 	register struct ip *ip;
2014df8bae1dSRodney W. Grimes 	struct mbuf *copym;
2015df8bae1dSRodney W. Grimes 
2016df8bae1dSRodney W. Grimes 	copym = m_copy(m, 0, M_COPYALL);
201786b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
201886b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
2019df8bae1dSRodney W. Grimes 	if (copym != NULL) {
2020df8bae1dSRodney W. Grimes 		/*
2021df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
2022df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
2023df8bae1dSRodney W. Grimes 		 */
2024df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
2025fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
2026fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
2027df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
20289c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
20299c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
20309c9137eaSGarrett Wollman 		} else {
203186b1d6d2SBill Fenner 			ip->ip_sum = in_cksum(copym, hlen);
20329c9137eaSGarrett Wollman 		}
20339c9137eaSGarrett Wollman 		/*
20349c9137eaSGarrett Wollman 		 * NB:
2035e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
2036e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
2037e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
2038e1596dffSBill Fenner 		 * particular, everything which modifies the packet
2039e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
2040e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
2041e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
2042ed7509acSJulian Elischer 		 * of safety by using if_simloop().
20439c9137eaSGarrett Wollman 		 */
2044201c2527SJulian Elischer #if 1 /* XXX */
2045201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
20462b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
2047201c2527SJulian Elischer 						dst->sin_family);
2048201c2527SJulian Elischer 			dst->sin_family = AF_INET;
2049201c2527SJulian Elischer 		}
2050201c2527SJulian Elischer #endif
2051201c2527SJulian Elischer 
20529c9137eaSGarrett Wollman #ifdef notdef
2053e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
2054ed7509acSJulian Elischer 		ip_input(copym);
20559c9137eaSGarrett Wollman #else
205650c6dc99SJonathan Lemon 		/* if the checksum hasn't been computed, mark it as valid */
205750c6dc99SJonathan Lemon 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
205850c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_flags |=
205950c6dc99SJonathan Lemon 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
206050c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_data = 0xffff;
206150c6dc99SJonathan Lemon 		}
206206a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
20639c9137eaSGarrett Wollman #endif
2064df8bae1dSRodney W. Grimes 	}
2065df8bae1dSRodney W. Grimes }
2066