xref: /freebsd/sys/netinet/ip_output.c (revision 3afefa39249562015a071a27686655ee3c6db13c)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
379c9137eaSGarrett Wollman #define _IP_VHL
389c9137eaSGarrett Wollman 
391f7e052cSJulian Elischer #include "opt_ipfw.h"
40b715f178SLuigi Rizzo #include "opt_ipdn.h"
41fbd1372aSJoerg Wunsch #include "opt_ipdivert.h"
421ee25934SPeter Wemm #include "opt_ipfilter.h"
436a800098SYoshinobu Inoue #include "opt_ipsec.h"
44c4ac87eaSDarren Reed #include "opt_pfil_hooks.h"
4564dddc18SKris Kennaway #include "opt_random_ip_id.h"
46fbd1372aSJoerg Wunsch 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
4826f9a767SRodney W. Grimes #include <sys/systm.h>
49f0f6d643SLuigi Rizzo #include <sys/kernel.h>
50df8bae1dSRodney W. Grimes #include <sys/malloc.h>
51df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <net/if.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <netinet/in.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
65df8bae1dSRodney W. Grimes 
666a800098SYoshinobu Inoue #include "faith.h"
676a800098SYoshinobu Inoue 
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>
83cfe8b629SGarrett Wollman 
84b715f178SLuigi Rizzo #ifdef DUMMYNET
85b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
86b715f178SLuigi Rizzo #endif
87b715f178SLuigi Rizzo 
88f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD_DEBUG
89f9e354dfSJulian Elischer #define print_ip(a)	 printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
90f9e354dfSJulian Elischer 				 		  (ntohl(a.s_addr)>>16)&0xFF,\
91f9e354dfSJulian Elischer 						  (ntohl(a.s_addr)>>8)&0xFF,\
92f9e354dfSJulian Elischer 						  (ntohl(a.s_addr))&0xFF);
93f9e354dfSJulian Elischer #endif
94f9e354dfSJulian Elischer 
95f23b4c91SGarrett Wollman u_short ip_id;
96f23b4c91SGarrett Wollman 
97df8bae1dSRodney W. Grimes static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
9833841545SHajimu UMEMOTO static struct ifnet *ip_multicast_if __P((struct in_addr *, int *));
99df8bae1dSRodney W. Grimes static void	ip_mloopback
10086b1d6d2SBill Fenner 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
1010312fbe9SPoul-Henning Kamp static int	ip_getmoptions
102cfe8b629SGarrett Wollman 	__P((struct sockopt *, struct ip_moptions *));
103cfe8b629SGarrett Wollman static int	ip_pcbopts __P((int, struct mbuf **, struct mbuf *));
1040312fbe9SPoul-Henning Kamp static int	ip_setmoptions
105cfe8b629SGarrett Wollman 	__P((struct sockopt *, struct ip_moptions **));
106df8bae1dSRodney W. Grimes 
107beec8214SDarren Reed int	ip_optcopy __P((struct ip *, struct ip *));
108afed1b49SDarren Reed 
109afed1b49SDarren Reed 
11093e0e116SJulian Elischer extern	struct protosw inetsw[];
11193e0e116SJulian Elischer 
112df8bae1dSRodney W. Grimes /*
113df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
114df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
115df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
116df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
117df8bae1dSRodney W. Grimes  */
118df8bae1dSRodney W. Grimes int
119df8bae1dSRodney W. Grimes ip_output(m0, opt, ro, flags, imo)
120df8bae1dSRodney W. Grimes 	struct mbuf *m0;
121df8bae1dSRodney W. Grimes 	struct mbuf *opt;
122df8bae1dSRodney W. Grimes 	struct route *ro;
123df8bae1dSRodney W. Grimes 	int flags;
124df8bae1dSRodney W. Grimes 	struct ip_moptions *imo;
125df8bae1dSRodney W. Grimes {
12623bf9953SPoul-Henning Kamp 	struct ip *ip, *mhip;
12723bf9953SPoul-Henning Kamp 	struct ifnet *ifp;
12823bf9953SPoul-Henning Kamp 	struct mbuf *m = m0;
12923bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
130df8bae1dSRodney W. Grimes 	int len, off, error = 0;
131df8bae1dSRodney W. Grimes 	struct sockaddr_in *dst;
132df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
133db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
1346a800098SYoshinobu Inoue #ifdef IPSEC
1356a800098SYoshinobu Inoue 	struct route iproute;
136d0a98d79SYoshinobu Inoue 	struct socket *so = NULL;
1376a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1386a800098SYoshinobu Inoue #endif
1398948e4baSArchie Cobbs 	u_int16_t divert_cookie;		/* firewall cookie */
140c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
141c4ac87eaSDarren Reed 	struct packet_filter_hook *pfh;
142c4ac87eaSDarren Reed 	struct mbuf *m1;
143c4ac87eaSDarren Reed 	int rv;
144c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
145f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
146f9e354dfSJulian Elischer 	int fwd_rewrite_src = 0;
147f9e354dfSJulian Elischer #endif
148b715f178SLuigi Rizzo 	struct ip_fw_chain *rule = NULL;
149b715f178SLuigi Rizzo 
1508948e4baSArchie Cobbs #ifdef IPDIVERT
1518948e4baSArchie Cobbs 	/* Get and reset firewall cookie */
1528948e4baSArchie Cobbs 	divert_cookie = ip_divert_cookie;
1538948e4baSArchie Cobbs 	ip_divert_cookie = 0;
1548948e4baSArchie Cobbs #else
1558948e4baSArchie Cobbs 	divert_cookie = 0;
1568948e4baSArchie Cobbs #endif
1578948e4baSArchie Cobbs 
158b715f178SLuigi Rizzo #if defined(IPFIREWALL) && defined(DUMMYNET)
159b715f178SLuigi Rizzo         /*
160b715f178SLuigi Rizzo          * dummynet packet are prepended a vestigial mbuf with
161b715f178SLuigi Rizzo          * m_type = MT_DUMMYNET and m_data pointing to the matching
162b715f178SLuigi Rizzo          * rule.
163b715f178SLuigi Rizzo          */
164b715f178SLuigi Rizzo         if (m->m_type == MT_DUMMYNET) {
165b715f178SLuigi Rizzo             /*
166b715f178SLuigi Rizzo              * the packet was already tagged, so part of the
167b715f178SLuigi Rizzo              * processing was already done, and we need to go down.
1686bc748b0SLuigi Rizzo              * Get parameters from the header.
169b715f178SLuigi Rizzo              */
170b715f178SLuigi Rizzo             rule = (struct ip_fw_chain *)(m->m_data) ;
171d1f04b29SLuigi Rizzo 	    opt = NULL ;
172d1f04b29SLuigi Rizzo 	    ro = & ( ((struct dn_pkt *)m)->ro ) ;
173d1f04b29SLuigi Rizzo 	    imo = NULL ;
174d1f04b29SLuigi Rizzo 	    dst = ((struct dn_pkt *)m)->dn_dst ;
175d1f04b29SLuigi Rizzo 	    ifp = ((struct dn_pkt *)m)->ifp ;
176d0a98d79SYoshinobu Inoue 	    flags = ((struct dn_pkt *)m)->flags ;
177d1f04b29SLuigi Rizzo 
17817458d35SLuigi Rizzo             m0 = m = m->m_next ;
179d0a98d79SYoshinobu Inoue #ifdef IPSEC
180686cdd19SJun-ichiro itojun Hagino 	    so = ipsec_getsocket(m);
18133841545SHajimu UMEMOTO 	    (void)ipsec_setsocket(m, NULL);
182d0a98d79SYoshinobu Inoue #endif
183b715f178SLuigi Rizzo             ip = mtod(m, struct ip *);
184b715f178SLuigi Rizzo             hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
1855d936aa1SIan Dowse             ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa;
186b715f178SLuigi Rizzo             goto sendit;
187b715f178SLuigi Rizzo         } else
188b715f178SLuigi Rizzo             rule = NULL ;
189b715f178SLuigi Rizzo #endif
190d0a98d79SYoshinobu Inoue #ifdef IPSEC
191686cdd19SJun-ichiro itojun Hagino 	so = ipsec_getsocket(m);
19233841545SHajimu UMEMOTO 	(void)ipsec_setsocket(m, NULL);
193d0a98d79SYoshinobu Inoue #endif
194b715f178SLuigi Rizzo 
195df8bae1dSRodney W. Grimes #ifdef	DIAGNOSTIC
196df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
197df8bae1dSRodney W. Grimes 		panic("ip_output no HDR");
1989c9137eaSGarrett Wollman 	if (!ro)
1999c9137eaSGarrett Wollman 		panic("ip_output no route, proto = %d",
2009c9137eaSGarrett Wollman 		      mtod(m, struct ip *)->ip_p);
201df8bae1dSRodney W. Grimes #endif
202df8bae1dSRodney W. Grimes 	if (opt) {
203df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
204df8bae1dSRodney W. Grimes 		hlen = len;
205df8bae1dSRodney W. Grimes 	}
206df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
207df8bae1dSRodney W. Grimes 	/*
208df8bae1dSRodney W. Grimes 	 * Fill in IP header.
209df8bae1dSRodney W. Grimes 	 */
210df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
2119c9137eaSGarrett Wollman 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
212df8bae1dSRodney W. Grimes 		ip->ip_off &= IP_DF;
21364dddc18SKris Kennaway #ifdef RANDOM_IP_ID
21464dddc18SKris Kennaway 		ip->ip_id = ip_randomid();
21564dddc18SKris Kennaway #else
216e30177e0SRuslan Ermilov 		ip->ip_id = htons(ip_id++);
21764dddc18SKris Kennaway #endif
218df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
219df8bae1dSRodney W. Grimes 	} else {
2209c9137eaSGarrett Wollman 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
221df8bae1dSRodney W. Grimes 	}
2229c9137eaSGarrett Wollman 
223df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
224df8bae1dSRodney W. Grimes 	/*
225df8bae1dSRodney W. Grimes 	 * If there is a cached route,
226df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
227df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
228df8bae1dSRodney W. Grimes 	 */
229df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
230df8bae1dSRodney W. Grimes 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
231df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
232df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
233df8bae1dSRodney W. Grimes 	}
234df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
235df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
236df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
237df8bae1dSRodney W. Grimes 		dst->sin_addr = ip->ip_dst;
238df8bae1dSRodney W. Grimes 	}
239df8bae1dSRodney W. Grimes 	/*
240df8bae1dSRodney W. Grimes 	 * If routing to interface only,
241df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
242df8bae1dSRodney W. Grimes 	 */
243df8bae1dSRodney W. Grimes #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
244df8bae1dSRodney W. Grimes #define sintosa(sin)	((struct sockaddr *)(sin))
245df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
246df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
247df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
248df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
249df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
250df8bae1dSRodney W. Grimes 			goto bad;
251df8bae1dSRodney W. Grimes 		}
252df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
253df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2549f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2553afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
2563afefa39SDaniel C. Sobral 	    (imo != NULL) &&
2573afefa39SDaniel C. Sobral 	    (imo->imo_multicast_ifp != NULL)) {
2583afefa39SDaniel C. Sobral 		/*
2593afefa39SDaniel C. Sobral 		 * bypass the normal routing lookup for
2603afefa39SDaniel C. Sobral 		 * multicast packets if the interface is
2613afefa39SDaniel C. Sobral 		 * specified
2623afefa39SDaniel C. Sobral 		 */
2633afefa39SDaniel C. Sobral 		/* No Operation */
264df8bae1dSRodney W. Grimes 	} else {
2652c17fe93SGarrett Wollman 		/*
2663afefa39SDaniel C. Sobral 		 * If this is the case, we probably don't want
2673afefa39SDaniel C. Sobral 		 * to allocate a protocol-cloned route since we
2683afefa39SDaniel C. Sobral 		 * didn't get one from the ULP.  This lets TCP
2693afefa39SDaniel C. Sobral 		 * do its thing, while not burdening forwarding
2703afefa39SDaniel C. Sobral 		 * or ICMP with the overhead of cloning a route.
2713afefa39SDaniel C. Sobral 		 * Of course, we still want to do any cloning
2723afefa39SDaniel C. Sobral 		 * requested by the link layer, as this is
2733afefa39SDaniel C. Sobral 		 * probably required in all cases for correct
2743afefa39SDaniel C. Sobral 		 * operation (as it is for ARP).
2752c17fe93SGarrett Wollman 		 */
276df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
2772c17fe93SGarrett Wollman 			rtalloc_ign(ro, RTF_PRCLONING);
278df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
279df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
280df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
281df8bae1dSRodney W. Grimes 			goto bad;
282df8bae1dSRodney W. Grimes 		}
283df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
284df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
285df8bae1dSRodney W. Grimes 		ro->ro_rt->rt_use++;
286df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
2873afefa39SDaniel C. Sobral 			dst = (struct sockaddr_in *)
2883afefa39SDaniel C. Sobral 			    ro->ro_rt->rt_gateway;
2899f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
2903afefa39SDaniel C. Sobral 			isbroadcast = (ro->ro_rt->rt_flags &
2913afefa39SDaniel C. Sobral 			    RTF_BROADCAST);
2929f9b3dc4SGarrett Wollman 		else
2939f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
294df8bae1dSRodney W. Grimes 	}
295df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_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;
310df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL)
311df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
3121c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3131c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
3141c5de19aSGarrett Wollman 				    ip_mcast_src(imo->imo_multicast_vif);
315df8bae1dSRodney W. Grimes 		} else
316df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
317df8bae1dSRodney W. Grimes 		/*
318df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
319df8bae1dSRodney W. Grimes 		 */
3201c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
321df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
322df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
323df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
324df8bae1dSRodney W. Grimes 				goto bad;
325df8bae1dSRodney W. Grimes 			}
3261c5de19aSGarrett Wollman 		}
327df8bae1dSRodney W. Grimes 		/*
328df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
329df8bae1dSRodney W. Grimes 		 * of outgoing interface.
330df8bae1dSRodney W. Grimes 		 */
331df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
3320abc78a6SPoul-Henning Kamp 			register struct in_ifaddr *ia1;
333df8bae1dSRodney W. Grimes 
33437d40066SPoul-Henning Kamp 			TAILQ_FOREACH(ia1, &in_ifaddrhead, ia_link)
3350abc78a6SPoul-Henning Kamp 				if (ia1->ia_ifp == ifp) {
3360abc78a6SPoul-Henning Kamp 					ip->ip_src = IA_SIN(ia1)->sin_addr;
337df8bae1dSRodney W. Grimes 					break;
338df8bae1dSRodney W. Grimes 				}
339df8bae1dSRodney W. Grimes 		}
340df8bae1dSRodney W. Grimes 
341df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
342df8bae1dSRodney W. Grimes 		if (inm != NULL &&
343df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
344df8bae1dSRodney W. Grimes 			/*
345df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
346df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
347df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
348df8bae1dSRodney W. Grimes 			 */
34986b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
350df8bae1dSRodney W. Grimes 		}
351df8bae1dSRodney W. Grimes 		else {
352df8bae1dSRodney W. Grimes 			/*
353df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
354df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
355df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
356df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
357df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
358df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
359df8bae1dSRodney W. Grimes 			 *
360df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
361df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
362df8bae1dSRodney W. Grimes 			 * if necessary.
363df8bae1dSRodney W. Grimes 			 */
364df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
365f0068c4aSGarrett Wollman 				/*
366f0068c4aSGarrett Wollman 				 * Check if rsvp daemon is running. If not, don't
367f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
368f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
369f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
370f0068c4aSGarrett Wollman 				 */
371b124e4f2SGarrett Wollman 				if (!rsvp_on)
372f0068c4aSGarrett Wollman 				  imo = NULL;
373f0068c4aSGarrett Wollman 				if (ip_mforward(ip, ifp, m, imo) != 0) {
374df8bae1dSRodney W. Grimes 					m_freem(m);
375df8bae1dSRodney W. Grimes 					goto done;
376df8bae1dSRodney W. Grimes 				}
377df8bae1dSRodney W. Grimes 			}
378df8bae1dSRodney W. Grimes 		}
3795e9ae478SGarrett Wollman 
380df8bae1dSRodney W. Grimes 		/*
381df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
382df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
383df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
384df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
385df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
386df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
387df8bae1dSRodney W. Grimes 		 */
388f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
389df8bae1dSRodney W. Grimes 			m_freem(m);
390df8bae1dSRodney W. Grimes 			goto done;
391df8bae1dSRodney W. Grimes 		}
392df8bae1dSRodney W. Grimes 
393df8bae1dSRodney W. Grimes 		goto sendit;
394df8bae1dSRodney W. Grimes 	}
395df8bae1dSRodney W. Grimes #ifndef notdef
396df8bae1dSRodney W. Grimes 	/*
397df8bae1dSRodney W. Grimes 	 * If source address not specified yet, use address
398df8bae1dSRodney W. Grimes 	 * of outgoing interface.
399df8bae1dSRodney W. Grimes 	 */
400f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
401df8bae1dSRodney W. Grimes 		ip->ip_src = IA_SIN(ia)->sin_addr;
402f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
403f9e354dfSJulian Elischer 		/* Keep note that we did this - if the firewall changes
404f9e354dfSJulian Elischer 		 * the next-hop, our interface may change, changing the
405f9e354dfSJulian Elischer 		 * default source IP. It's a shame so much effort happens
406f9e354dfSJulian Elischer 		 * twice. Oh well.
407f9e354dfSJulian Elischer 		 */
408f9e354dfSJulian Elischer 		fwd_rewrite_src++;
409f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */
410f9e354dfSJulian Elischer 	}
411f9e354dfSJulian Elischer #endif /* notdef */
412df8bae1dSRodney W. Grimes 	/*
413b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
414b5390296SDavid Greenman 	 *      the packet or packet fragments
415b5390296SDavid Greenman 	 */
416b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
417b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
418b5390296SDavid Greenman 			error = ENOBUFS;
419b5390296SDavid Greenman 			goto bad;
420b5390296SDavid Greenman 	}
421b5390296SDavid Greenman 
422b5390296SDavid Greenman 	/*
423df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
424df8bae1dSRodney W. Grimes 	 * and verify user is allowed to send
425df8bae1dSRodney W. Grimes 	 * such a packet.
426df8bae1dSRodney W. Grimes 	 */
4279f9b3dc4SGarrett Wollman 	if (isbroadcast) {
428df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
429df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
430df8bae1dSRodney W. Grimes 			goto bad;
431df8bae1dSRodney W. Grimes 		}
432df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
433df8bae1dSRodney W. Grimes 			error = EACCES;
434df8bae1dSRodney W. Grimes 			goto bad;
435df8bae1dSRodney W. Grimes 		}
436df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
437df8bae1dSRodney W. Grimes 		if ((u_short)ip->ip_len > ifp->if_mtu) {
438df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
439df8bae1dSRodney W. Grimes 			goto bad;
440df8bae1dSRodney W. Grimes 		}
441df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4429f9b3dc4SGarrett Wollman 	} else {
443df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4449f9b3dc4SGarrett Wollman 	}
445df8bae1dSRodney W. Grimes 
446f1743588SDarren Reed sendit:
44733841545SHajimu UMEMOTO #ifdef IPSEC
44833841545SHajimu UMEMOTO 	/* get SP for this packet */
44933841545SHajimu UMEMOTO 	if (so == NULL)
45033841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
45133841545SHajimu UMEMOTO 	else
45233841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
45333841545SHajimu UMEMOTO 
45433841545SHajimu UMEMOTO 	if (sp == NULL) {
45533841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
45633841545SHajimu UMEMOTO 		goto bad;
45733841545SHajimu UMEMOTO 	}
45833841545SHajimu UMEMOTO 
45933841545SHajimu UMEMOTO 	error = 0;
46033841545SHajimu UMEMOTO 
46133841545SHajimu UMEMOTO 	/* check policy */
46233841545SHajimu UMEMOTO 	switch (sp->policy) {
46333841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
46433841545SHajimu UMEMOTO 		/*
46533841545SHajimu UMEMOTO 		 * This packet is just discarded.
46633841545SHajimu UMEMOTO 		 */
46733841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
46833841545SHajimu UMEMOTO 		goto bad;
46933841545SHajimu UMEMOTO 
47033841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
47133841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
47233841545SHajimu UMEMOTO 		/* no need to do IPsec. */
47333841545SHajimu UMEMOTO 		goto skip_ipsec;
47433841545SHajimu UMEMOTO 
47533841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
47633841545SHajimu UMEMOTO 		if (sp->req == NULL) {
47733841545SHajimu UMEMOTO 			/* acquire a policy */
47833841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
47933841545SHajimu UMEMOTO 			goto bad;
48033841545SHajimu UMEMOTO 		}
48133841545SHajimu UMEMOTO 		break;
48233841545SHajimu UMEMOTO 
48333841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
48433841545SHajimu UMEMOTO 	default:
48533841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
48633841545SHajimu UMEMOTO 	}
48733841545SHajimu UMEMOTO     {
48833841545SHajimu UMEMOTO 	struct ipsec_output_state state;
48933841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
49033841545SHajimu UMEMOTO 	state.m = m;
49133841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
49233841545SHajimu UMEMOTO 		state.ro = &iproute;
49333841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
49433841545SHajimu UMEMOTO 	} else
49533841545SHajimu UMEMOTO 		state.ro = ro;
49633841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
49733841545SHajimu UMEMOTO 
49833841545SHajimu UMEMOTO 	ip->ip_sum = 0;
49933841545SHajimu UMEMOTO 
50033841545SHajimu UMEMOTO 	/*
50133841545SHajimu UMEMOTO 	 * XXX
50233841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
50333841545SHajimu UMEMOTO 	 */
50433841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
50533841545SHajimu UMEMOTO 		in_delayed_cksum(m);
50633841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
50733841545SHajimu UMEMOTO 	}
50833841545SHajimu UMEMOTO 
50933841545SHajimu UMEMOTO 	HTONS(ip->ip_len);
51033841545SHajimu UMEMOTO 	HTONS(ip->ip_off);
51133841545SHajimu UMEMOTO 
51233841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
51333841545SHajimu UMEMOTO 
51433841545SHajimu UMEMOTO 	m = state.m;
51533841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
51633841545SHajimu UMEMOTO 		/*
51733841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
51833841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
51933841545SHajimu UMEMOTO 		 */
52033841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
52133841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
52233841545SHajimu UMEMOTO 			ro = state.ro;
52333841545SHajimu UMEMOTO 		}
52433841545SHajimu UMEMOTO 	} else
52533841545SHajimu UMEMOTO 		ro = state.ro;
52633841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
52733841545SHajimu UMEMOTO 	if (error) {
52833841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
52933841545SHajimu UMEMOTO 		m0 = NULL;
53033841545SHajimu UMEMOTO 		switch (error) {
53133841545SHajimu UMEMOTO 		case EHOSTUNREACH:
53233841545SHajimu UMEMOTO 		case ENETUNREACH:
53333841545SHajimu UMEMOTO 		case EMSGSIZE:
53433841545SHajimu UMEMOTO 		case ENOBUFS:
53533841545SHajimu UMEMOTO 		case ENOMEM:
53633841545SHajimu UMEMOTO 			break;
53733841545SHajimu UMEMOTO 		default:
53833841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
53933841545SHajimu UMEMOTO 			/*fall through*/
54033841545SHajimu UMEMOTO 		case ENOENT:
54133841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
54233841545SHajimu UMEMOTO 			error = 0;
54333841545SHajimu UMEMOTO 			break;
54433841545SHajimu UMEMOTO 		}
54533841545SHajimu UMEMOTO 		goto bad;
54633841545SHajimu UMEMOTO 	}
54733841545SHajimu UMEMOTO     }
54833841545SHajimu UMEMOTO 
54933841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
55033841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
55133841545SHajimu UMEMOTO #ifdef _IP_VHL
55233841545SHajimu UMEMOTO 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
55333841545SHajimu UMEMOTO #else
55433841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
55533841545SHajimu UMEMOTO #endif
55633841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
55733841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
55833841545SHajimu UMEMOTO 			printf("ip_output: "
55933841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
56033841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
56133841545SHajimu UMEMOTO 			goto bad;
56233841545SHajimu UMEMOTO 		}
56333841545SHajimu UMEMOTO 	} else {
56433841545SHajimu UMEMOTO 		ia = ifatoia(ro->ro_rt->rt_ifa);
56533841545SHajimu UMEMOTO 		ifp = ro->ro_rt->rt_ifp;
56633841545SHajimu UMEMOTO 	}
56733841545SHajimu UMEMOTO 
56833841545SHajimu UMEMOTO 	/* make it flipped, again. */
56933841545SHajimu UMEMOTO 	NTOHS(ip->ip_len);
57033841545SHajimu UMEMOTO 	NTOHS(ip->ip_off);
57133841545SHajimu UMEMOTO skip_ipsec:
57233841545SHajimu UMEMOTO #endif /*IPSEC*/
57333841545SHajimu UMEMOTO 
574fed1c7e9SSøren Schmidt 	/*
575fed1c7e9SSøren Schmidt 	 * IpHack's section.
576fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
577e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
578fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
579fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
580fed1c7e9SSøren Schmidt 	 */
581c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
582c4ac87eaSDarren Reed 	/*
583c4ac87eaSDarren Reed 	 * Run through list of hooks for output packets.
584c4ac87eaSDarren Reed 	 */
585c4ac87eaSDarren Reed 	m1 = m;
586c4ac87eaSDarren Reed 	pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
587fc2ffbe6SPoul-Henning Kamp 	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
588c4ac87eaSDarren Reed 		if (pfh->pfil_func) {
589c4ac87eaSDarren Reed 			rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
590c4ac87eaSDarren Reed 			if (rv) {
591c4ac87eaSDarren Reed 				error = EHOSTUNREACH;
5921ee25934SPeter Wemm 				goto done;
5931ee25934SPeter Wemm 			}
594c4ac87eaSDarren Reed 			m = m1;
595c4ac87eaSDarren Reed 			if (m == NULL)
596c4ac87eaSDarren Reed 				goto done;
597c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
598c4ac87eaSDarren Reed 		}
599c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
600fed1c7e9SSøren Schmidt 
601df8bae1dSRodney W. Grimes 	/*
602e7319babSPoul-Henning Kamp 	 * Check with the firewall...
603e7319babSPoul-Henning Kamp 	 */
6046bc748b0SLuigi Rizzo 	if (fw_enable && ip_fw_chk_ptr) {
6059de9737fSPeter Wemm 		struct sockaddr_in *old = dst;
606b715f178SLuigi Rizzo 
607b715f178SLuigi Rizzo 		off = (*ip_fw_chk_ptr)(&ip,
6088948e4baSArchie Cobbs 		    hlen, ifp, &divert_cookie, &m, &rule, &dst);
609b715f178SLuigi Rizzo                 /*
610b715f178SLuigi Rizzo                  * On return we must do the following:
611507b4b54SLuigi Rizzo                  * m == NULL         -> drop the pkt (old interface, deprecated)
612507b4b54SLuigi Rizzo                  * (off & 0x40000)   -> drop the pkt (new interface)
613b715f178SLuigi Rizzo                  * 1<=off<= 0xffff   -> DIVERT
614b715f178SLuigi Rizzo                  * (off & 0x10000)   -> send to a DUMMYNET pipe
6158948e4baSArchie Cobbs                  * (off & 0x20000)   -> TEE the packet
616b715f178SLuigi Rizzo                  * dst != old        -> IPFIREWALL_FORWARD
617b715f178SLuigi Rizzo                  * off==0, dst==old  -> accept
618b715f178SLuigi Rizzo                  * If some of the above modules is not compiled in, then
619b715f178SLuigi Rizzo                  * we should't have to check the corresponding condition
620b715f178SLuigi Rizzo                  * (because the ipfw control socket should not accept
621b715f178SLuigi Rizzo                  * unsupported rules), but better play safe and drop
622b715f178SLuigi Rizzo                  * packets in case of doubt.
623b715f178SLuigi Rizzo                  */
624507b4b54SLuigi Rizzo 		if (off & IP_FW_PORT_DENY_FLAG) { /* XXX new interface-denied */
625507b4b54SLuigi Rizzo 		    if (m)
626507b4b54SLuigi Rizzo 			m_freem(m);
627507b4b54SLuigi Rizzo 		    error = EACCES ;
628507b4b54SLuigi Rizzo 		    goto done;
629507b4b54SLuigi Rizzo 		}
630b715f178SLuigi Rizzo 		if (!m) { /* firewall said to reject */
631507b4b54SLuigi Rizzo 		    static int __debug=10;
632507b4b54SLuigi Rizzo 		    if (__debug >0) {
633507b4b54SLuigi Rizzo 			printf("firewall returns NULL, please update!\n");
634507b4b54SLuigi Rizzo 			__debug-- ;
635507b4b54SLuigi Rizzo 		    }
636e4676ba6SJulian Elischer 		    error = EACCES;
637e4676ba6SJulian Elischer 		    goto done;
63893e0e116SJulian Elischer 		}
639b715f178SLuigi Rizzo 		if (off == 0 && dst == old) /* common case */
640b715f178SLuigi Rizzo 			goto pass ;
641b715f178SLuigi Rizzo #ifdef DUMMYNET
6428948e4baSArchie Cobbs                 if ((off & IP_FW_PORT_DYNT_FLAG) != 0) {
643b715f178SLuigi Rizzo                     /*
644b715f178SLuigi Rizzo                      * pass the pkt to dummynet. Need to include
645f0a53591SLuigi Rizzo                      * pipe number, m, ifp, ro, dst because these are
646b715f178SLuigi Rizzo                      * not recomputed in the next pass.
647b715f178SLuigi Rizzo                      * All other parameters have been already used and
648b715f178SLuigi Rizzo                      * so they are not needed anymore.
649b715f178SLuigi Rizzo                      * XXX note: if the ifp or ro entry are deleted
650b715f178SLuigi Rizzo                      * while a pkt is in dummynet, we are in trouble!
651b715f178SLuigi Rizzo                      */
6527a726a2dSLuigi Rizzo 		    error = dummynet_io(off & 0xffff, DN_TO_IP_OUT, m,
6537a726a2dSLuigi Rizzo 				ifp,ro,dst,rule, flags);
654b715f178SLuigi Rizzo 		    goto done;
655b715f178SLuigi Rizzo 		}
656b715f178SLuigi Rizzo #endif
657b715f178SLuigi Rizzo #ifdef IPDIVERT
6588948e4baSArchie Cobbs 		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
6598948e4baSArchie Cobbs 			struct mbuf *clone = NULL;
6608948e4baSArchie Cobbs 
6618948e4baSArchie Cobbs 			/* Clone packet if we're doing a 'tee' */
6628948e4baSArchie Cobbs 			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
6638948e4baSArchie Cobbs 				clone = m_dup(m, M_DONTWAIT);
6648948e4baSArchie Cobbs 
665ea53ecd9SJonathan Lemon 			/*
666ea53ecd9SJonathan Lemon 			 * XXX
667ea53ecd9SJonathan Lemon 			 * delayed checksums are not currently compatible
668ea53ecd9SJonathan Lemon 			 * with divert sockets.
669ea53ecd9SJonathan Lemon 			 */
670ea53ecd9SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
671ea53ecd9SJonathan Lemon 				in_delayed_cksum(m);
672ea53ecd9SJonathan Lemon 				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
673ea53ecd9SJonathan Lemon 			}
674ea53ecd9SJonathan Lemon 
6758948e4baSArchie Cobbs 			/* Restore packet header fields to original values */
6768948e4baSArchie Cobbs 			HTONS(ip->ip_len);
6778948e4baSArchie Cobbs 			HTONS(ip->ip_off);
6788948e4baSArchie Cobbs 
6798948e4baSArchie Cobbs 			/* Deliver packet to divert input routine */
6808948e4baSArchie Cobbs 			ip_divert_cookie = divert_cookie;
6818948e4baSArchie Cobbs 			divert_packet(m, 0, off & 0xffff);
6828948e4baSArchie Cobbs 
6838948e4baSArchie Cobbs 			/* If 'tee', continue with original packet */
6848948e4baSArchie Cobbs 			if (clone != NULL) {
6858948e4baSArchie Cobbs 				m = clone;
6868948e4baSArchie Cobbs 				ip = mtod(m, struct ip *);
6878948e4baSArchie Cobbs 				goto pass;
6888948e4baSArchie Cobbs 			}
689b715f178SLuigi Rizzo 			goto done;
690b715f178SLuigi Rizzo 		}
691b715f178SLuigi Rizzo #endif
692b715f178SLuigi Rizzo 
693f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
694f9e354dfSJulian Elischer 		/* Here we check dst to make sure it's directly reachable on the
695f9e354dfSJulian Elischer 		 * interface we previously thought it was.
696f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
697f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
698f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
699f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
700f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
701f9e354dfSJulian Elischer 		 * And I'm babbling.
702f9e354dfSJulian Elischer 		 */
703b715f178SLuigi Rizzo 		if (off == 0 && old != dst) {
704f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
705f9e354dfSJulian Elischer 
706f9e354dfSJulian Elischer 			/* It's changed... */
707f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
708f9e354dfSJulian Elischer 			static struct route sro_fwd, *ro_fwd = &sro_fwd;
709f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD_DEBUG
710f9e354dfSJulian Elischer 			printf("IPFIREWALL_FORWARD: New dst ip: ");
711f9e354dfSJulian Elischer 			print_ip(dst->sin_addr);
712f9e354dfSJulian Elischer 			printf("\n");
713f9e354dfSJulian Elischer #endif
714f9e354dfSJulian Elischer 			/*
715f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
716f9e354dfSJulian Elischer 			 * to a local socket. If so then we should somehow
717f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
718f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
719f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
720f9e354dfSJulian Elischer 			 * you want to forward until they are being output
721f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
722f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
723f9e354dfSJulian Elischer 			 * we would not be able to control what happens
724f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
725f9e354dfSJulian Elischer 			 * it is done through a ISR.
726f9e354dfSJulian Elischer 			 */
72737d40066SPoul-Henning Kamp 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
728f9e354dfSJulian Elischer 				/*
729f9e354dfSJulian Elischer 				 * If the addr to forward to is one
730f9e354dfSJulian Elischer 				 * of ours, we pretend to
731f9e354dfSJulian Elischer 				 * be the destination for this packet.
732f9e354dfSJulian Elischer 				 */
733f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
734f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
735f9e354dfSJulian Elischer 					break;
736f9e354dfSJulian Elischer 			}
737f9e354dfSJulian Elischer 			if (ia) {
738f9e354dfSJulian Elischer 				/* tell ip_input "dont filter" */
739f9e354dfSJulian Elischer 				ip_fw_fwd_addr = dst;
740f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
741f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
74220c822f3SJonathan Lemon 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
74320c822f3SJonathan Lemon 					m->m_pkthdr.csum_flags |=
74420c822f3SJonathan Lemon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
74520c822f3SJonathan Lemon 					m0->m_pkthdr.csum_data = 0xffff;
74620c822f3SJonathan Lemon 				}
74720c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
74820c822f3SJonathan Lemon 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
74904287599SRuslan Ermilov 				HTONS(ip->ip_len);
75004287599SRuslan Ermilov 				HTONS(ip->ip_off);
751f9e354dfSJulian Elischer 				ip_input(m);
752f9e354dfSJulian Elischer 				goto done;
753f9e354dfSJulian Elischer 			}
754f9e354dfSJulian Elischer 			/* Some of the logic for this was
755f9e354dfSJulian Elischer 			 * nicked from above.
756f9e354dfSJulian Elischer 			 *
757f9e354dfSJulian Elischer 			 * This rewrites the cached route in a local PCB.
758f9e354dfSJulian Elischer 			 * Is this what we want to do?
759f9e354dfSJulian Elischer 			 */
760f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
761f9e354dfSJulian Elischer 
762f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
763f9e354dfSJulian Elischer 			rtalloc_ign(ro_fwd, RTF_PRCLONING);
764f9e354dfSJulian Elischer 
765f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt == 0) {
766f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
767f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
768f9e354dfSJulian Elischer 				goto bad;
769f9e354dfSJulian Elischer 			}
770f9e354dfSJulian Elischer 
771f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
772f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
773f9e354dfSJulian Elischer 			ro_fwd->ro_rt->rt_use++;
774f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
775f9e354dfSJulian Elischer 				dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
776f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
777f9e354dfSJulian Elischer 				isbroadcast =
778f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
779f9e354dfSJulian Elischer 			else
780f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
781f9e354dfSJulian Elischer 			RTFREE(ro->ro_rt);
782f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
783f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
784f9e354dfSJulian Elischer 
785f9e354dfSJulian Elischer 			/*
786f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
787f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
788f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
789f9e354dfSJulian Elischer 			 */
790f9e354dfSJulian Elischer 			if (fwd_rewrite_src)
791f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
792b715f178SLuigi Rizzo 			goto pass ;
793f9e354dfSJulian Elischer 		}
794f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */
795b715f178SLuigi Rizzo                 /*
796b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
797b715f178SLuigi Rizzo                  * we have to drop the pkt
798b715f178SLuigi Rizzo                  */
799b715f178SLuigi Rizzo 		m_freem(m);
800b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
801b715f178SLuigi Rizzo                 goto done;
80293e0e116SJulian Elischer 	}
803e7319babSPoul-Henning Kamp 
804b715f178SLuigi Rizzo pass:
805206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
806206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
807db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
808db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
809db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
810db4f9cc7SJonathan Lemon 	}
811206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
812db4f9cc7SJonathan Lemon 
813e7319babSPoul-Henning Kamp 	/*
814db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
815db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
816df8bae1dSRodney W. Grimes 	 */
817db4f9cc7SJonathan Lemon 	if ((u_short)ip->ip_len <= ifp->if_mtu ||
818db4f9cc7SJonathan Lemon 	    ifp->if_hwassist & CSUM_FRAGMENT) {
81904287599SRuslan Ermilov 		HTONS(ip->ip_len);
82004287599SRuslan Ermilov 		HTONS(ip->ip_off);
821df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
822db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
8239c9137eaSGarrett Wollman 			if (ip->ip_vhl == IP_VHL_BORING) {
8249c9137eaSGarrett Wollman 				ip->ip_sum = in_cksum_hdr(ip);
8259c9137eaSGarrett Wollman 			} else {
826df8bae1dSRodney W. Grimes 				ip->ip_sum = in_cksum(m, hlen);
8279c9137eaSGarrett Wollman 			}
828db4f9cc7SJonathan Lemon 		}
8295da9f8faSJosef Karthauser 
8305da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
8315da9f8faSJosef Karthauser 		if (!(flags & IP_FORWARDING)) {
8325da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
8335da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
8345da9f8faSJosef Karthauser 		}
8355da9f8faSJosef Karthauser 
83633841545SHajimu UMEMOTO #ifdef IPSEC
83733841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
83833841545SHajimu UMEMOTO 		ipsec_delaux(m);
83933841545SHajimu UMEMOTO #endif
84033841545SHajimu UMEMOTO 
841df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
842df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
843df8bae1dSRodney W. Grimes 		goto done;
844df8bae1dSRodney W. Grimes 	}
845df8bae1dSRodney W. Grimes 	/*
846df8bae1dSRodney W. Grimes 	 * Too large for interface; fragment if possible.
847df8bae1dSRodney W. Grimes 	 * Must be able to put at least 8 bytes per fragment.
848df8bae1dSRodney W. Grimes 	 */
849df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
850df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
8513d1f141bSGarrett Wollman 		/*
8523d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
8533d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
8543d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
8553d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
8563d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
8573d1f141bSGarrett Wollman 		 */
8583d1f141bSGarrett Wollman 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
8593d1f141bSGarrett Wollman 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
8603d1f141bSGarrett Wollman 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
8613d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
8623d1f141bSGarrett Wollman 		}
863df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
864df8bae1dSRodney W. Grimes 		goto bad;
865df8bae1dSRodney W. Grimes 	}
866df8bae1dSRodney W. Grimes 	len = (ifp->if_mtu - hlen) &~ 7;
867df8bae1dSRodney W. Grimes 	if (len < 8) {
868df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
869df8bae1dSRodney W. Grimes 		goto bad;
870df8bae1dSRodney W. Grimes 	}
871df8bae1dSRodney W. Grimes 
872db4f9cc7SJonathan Lemon 	/*
873db4f9cc7SJonathan Lemon 	 * if the interface will not calculate checksums on
874db4f9cc7SJonathan Lemon 	 * fragmented packets, then do it here.
875db4f9cc7SJonathan Lemon 	 */
876db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
877db4f9cc7SJonathan Lemon 	    (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
878db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
879db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
880db4f9cc7SJonathan Lemon 	}
881db4f9cc7SJonathan Lemon 
882df8bae1dSRodney W. Grimes     {
883df8bae1dSRodney W. Grimes 	int mhlen, firstlen = len;
884df8bae1dSRodney W. Grimes 	struct mbuf **mnext = &m->m_nextpkt;
885db4f9cc7SJonathan Lemon 	int nfrags = 1;
886df8bae1dSRodney W. Grimes 
887df8bae1dSRodney W. Grimes 	/*
888df8bae1dSRodney W. Grimes 	 * Loop through length of segment after first fragment,
889df8bae1dSRodney W. Grimes 	 * make new header and copy data of each part and link onto chain.
890df8bae1dSRodney W. Grimes 	 */
891df8bae1dSRodney W. Grimes 	m0 = m;
892df8bae1dSRodney W. Grimes 	mhlen = sizeof (struct ip);
893df8bae1dSRodney W. Grimes 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
894df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
895df8bae1dSRodney W. Grimes 		if (m == 0) {
896df8bae1dSRodney W. Grimes 			error = ENOBUFS;
897df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
898df8bae1dSRodney W. Grimes 			goto sendorfree;
899df8bae1dSRodney W. Grimes 		}
900db4f9cc7SJonathan Lemon 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
901df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
902df8bae1dSRodney W. Grimes 		mhip = mtod(m, struct ip *);
903df8bae1dSRodney W. Grimes 		*mhip = *ip;
904df8bae1dSRodney W. Grimes 		if (hlen > sizeof (struct ip)) {
905df8bae1dSRodney W. Grimes 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9069c9137eaSGarrett Wollman 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
907df8bae1dSRodney W. Grimes 		}
908df8bae1dSRodney W. Grimes 		m->m_len = mhlen;
909cc22c7a7SRuslan Ermilov 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
910df8bae1dSRodney W. Grimes 		if (off + len >= (u_short)ip->ip_len)
911df8bae1dSRodney W. Grimes 			len = (u_short)ip->ip_len - off;
912df8bae1dSRodney W. Grimes 		else
913df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
914df8bae1dSRodney W. Grimes 		mhip->ip_len = htons((u_short)(len + mhlen));
915df8bae1dSRodney W. Grimes 		m->m_next = m_copy(m0, off, len);
916df8bae1dSRodney W. Grimes 		if (m->m_next == 0) {
917df8bae1dSRodney W. Grimes 			(void) m_free(m);
918df8bae1dSRodney W. Grimes 			error = ENOBUFS;	/* ??? */
919df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
920df8bae1dSRodney W. Grimes 			goto sendorfree;
921df8bae1dSRodney W. Grimes 		}
922df8bae1dSRodney W. Grimes 		m->m_pkthdr.len = mhlen + len;
923df8bae1dSRodney W. Grimes 		m->m_pkthdr.rcvif = (struct ifnet *)0;
924db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
92504287599SRuslan Ermilov 		HTONS(mhip->ip_off);
926df8bae1dSRodney W. Grimes 		mhip->ip_sum = 0;
927db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
9289c9137eaSGarrett Wollman 			if (mhip->ip_vhl == IP_VHL_BORING) {
929e2184122SBruce Evans 				mhip->ip_sum = in_cksum_hdr(mhip);
9309c9137eaSGarrett Wollman 			} else {
931df8bae1dSRodney W. Grimes 				mhip->ip_sum = in_cksum(m, mhlen);
9329c9137eaSGarrett Wollman 			}
933db4f9cc7SJonathan Lemon 		}
934df8bae1dSRodney W. Grimes 		*mnext = m;
935df8bae1dSRodney W. Grimes 		mnext = &m->m_nextpkt;
936db4f9cc7SJonathan Lemon 		nfrags++;
937df8bae1dSRodney W. Grimes 	}
938db4f9cc7SJonathan Lemon 	ipstat.ips_ofragments += nfrags;
939db4f9cc7SJonathan Lemon 
940db4f9cc7SJonathan Lemon 	/* set first/last markers for fragment chain */
941db4f9cc7SJonathan Lemon 	m->m_flags |= M_LASTFRAG;
942db4f9cc7SJonathan Lemon 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
943db4f9cc7SJonathan Lemon 	m0->m_pkthdr.csum_data = nfrags;
944db4f9cc7SJonathan Lemon 
945df8bae1dSRodney W. Grimes 	/*
946df8bae1dSRodney W. Grimes 	 * Update first fragment by trimming what's been copied out
947df8bae1dSRodney W. Grimes 	 * and updating header, then send each fragment (in order).
948df8bae1dSRodney W. Grimes 	 */
949df8bae1dSRodney W. Grimes 	m = m0;
950df8bae1dSRodney W. Grimes 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
951df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hlen + firstlen;
952df8bae1dSRodney W. Grimes 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
95304287599SRuslan Ermilov 	ip->ip_off |= IP_MF;
95404287599SRuslan Ermilov 	HTONS(ip->ip_off);
955df8bae1dSRodney W. Grimes 	ip->ip_sum = 0;
956db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_IP) {
9579c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
9589c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
9599c9137eaSGarrett Wollman 		} else {
960df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
9619c9137eaSGarrett Wollman 		}
962db4f9cc7SJonathan Lemon 	}
963df8bae1dSRodney W. Grimes sendorfree:
964df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m0) {
965df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
966df8bae1dSRodney W. Grimes 		m->m_nextpkt = 0;
96733841545SHajimu UMEMOTO #ifdef IPSEC
96833841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
96933841545SHajimu UMEMOTO 		ipsec_delaux(m);
97033841545SHajimu UMEMOTO #endif
971fe937674SJosef Karthauser 		if (error == 0) {
972fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
973fe937674SJosef Karthauser 			ia->ia_ifa.if_opackets++;
974fe937674SJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
975fe937674SJosef Karthauser 
976df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
977df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
978fe937674SJosef Karthauser 		} else
979df8bae1dSRodney W. Grimes 			m_freem(m);
980df8bae1dSRodney W. Grimes 	}
981df8bae1dSRodney W. Grimes 
982df8bae1dSRodney W. Grimes 	if (error == 0)
983df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
984df8bae1dSRodney W. Grimes     }
985df8bae1dSRodney W. Grimes done:
9866a800098SYoshinobu Inoue #ifdef IPSEC
9876a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
9886a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
9896a800098SYoshinobu Inoue 		ro->ro_rt = NULL;
9906a800098SYoshinobu Inoue 	}
9916a800098SYoshinobu Inoue 	if (sp != NULL) {
9926a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
9936a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
9946a800098SYoshinobu Inoue 		key_freesp(sp);
9956a800098SYoshinobu Inoue 	}
9966a800098SYoshinobu Inoue #endif /* IPSEC */
997df8bae1dSRodney W. Grimes 	return (error);
998df8bae1dSRodney W. Grimes bad:
999df8bae1dSRodney W. Grimes 	m_freem(m0);
1000df8bae1dSRodney W. Grimes 	goto done;
1001df8bae1dSRodney W. Grimes }
1002df8bae1dSRodney W. Grimes 
10031c238475SJonathan Lemon void
1004db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1005db4f9cc7SJonathan Lemon {
1006db4f9cc7SJonathan Lemon 	struct ip *ip;
1007db4f9cc7SJonathan Lemon 	u_short csum, offset;
1008db4f9cc7SJonathan Lemon 
1009db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
1010db4f9cc7SJonathan Lemon 	offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1011db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1012206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1013206a3274SRuslan Ermilov 		csum = 0xffff;
1014db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1015db4f9cc7SJonathan Lemon 
1016db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1017db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1018db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1019db4f9cc7SJonathan Lemon 		/*
1020db4f9cc7SJonathan Lemon 		 * XXX
1021db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1022db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1023db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1024db4f9cc7SJonathan Lemon 		 */
1025db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1026db4f9cc7SJonathan Lemon 	}
1027db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1028db4f9cc7SJonathan Lemon }
1029db4f9cc7SJonathan Lemon 
1030df8bae1dSRodney W. Grimes /*
1031df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1032df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1033df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1034072b9b24SPaul Traina  *
1035072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1036df8bae1dSRodney W. Grimes  */
1037df8bae1dSRodney W. Grimes static struct mbuf *
1038df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen)
1039df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1040df8bae1dSRodney W. Grimes 	struct mbuf *opt;
1041df8bae1dSRodney W. Grimes 	int *phlen;
1042df8bae1dSRodney W. Grimes {
1043df8bae1dSRodney W. Grimes 	register struct ipoption *p = mtod(opt, struct ipoption *);
1044df8bae1dSRodney W. Grimes 	struct mbuf *n;
1045df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
1046df8bae1dSRodney W. Grimes 	unsigned optlen;
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
1049df8bae1dSRodney W. Grimes 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
1050df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1051df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1052df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1053df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1054df8bae1dSRodney W. Grimes 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1055df8bae1dSRodney W. Grimes 		if (n == 0)
1056df8bae1dSRodney W. Grimes 			return (m);
10575db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
1058df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1059df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1060df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1061df8bae1dSRodney W. Grimes 		n->m_next = m;
1062df8bae1dSRodney W. Grimes 		m = n;
1063df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1064df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
106594a5d9b6SDavid Greenman 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1066df8bae1dSRodney W. Grimes 	} else {
1067df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1068df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1069df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1070df8bae1dSRodney W. Grimes 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1071df8bae1dSRodney W. Grimes 	}
1072df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
10730453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1074df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
10759c9137eaSGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1076df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1077df8bae1dSRodney W. Grimes 	return (m);
1078df8bae1dSRodney W. Grimes }
1079df8bae1dSRodney W. Grimes 
1080df8bae1dSRodney W. Grimes /*
1081df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1082df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1083df8bae1dSRodney W. Grimes  */
1084beec8214SDarren Reed int
1085df8bae1dSRodney W. Grimes ip_optcopy(ip, jp)
1086df8bae1dSRodney W. Grimes 	struct ip *ip, *jp;
1087df8bae1dSRodney W. Grimes {
1088df8bae1dSRodney W. Grimes 	register u_char *cp, *dp;
1089df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1090df8bae1dSRodney W. Grimes 
1091df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1092df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
10939c9137eaSGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1094df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1095df8bae1dSRodney W. Grimes 		opt = cp[0];
1096df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1097df8bae1dSRodney W. Grimes 			break;
1098df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1099df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1100df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1101df8bae1dSRodney W. Grimes 			optlen = 1;
1102df8bae1dSRodney W. Grimes 			continue;
1103686cdd19SJun-ichiro itojun Hagino 		}
1104686cdd19SJun-ichiro itojun Hagino #ifdef DIAGNOSTIC
1105686cdd19SJun-ichiro itojun Hagino 		if (cnt < IPOPT_OLEN + sizeof(*cp))
1106686cdd19SJun-ichiro itojun Hagino 			panic("malformed IPv4 option passed to ip_optcopy");
1107686cdd19SJun-ichiro itojun Hagino #endif
1108df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1109686cdd19SJun-ichiro itojun Hagino #ifdef DIAGNOSTIC
1110686cdd19SJun-ichiro itojun Hagino 		if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1111686cdd19SJun-ichiro itojun Hagino 			panic("malformed IPv4 option passed to ip_optcopy");
1112686cdd19SJun-ichiro itojun Hagino #endif
1113df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1114df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1115df8bae1dSRodney W. Grimes 			optlen = cnt;
1116df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
11170453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1118df8bae1dSRodney W. Grimes 			dp += optlen;
1119df8bae1dSRodney W. Grimes 		}
1120df8bae1dSRodney W. Grimes 	}
1121df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1122df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1123df8bae1dSRodney W. Grimes 	return (optlen);
1124df8bae1dSRodney W. Grimes }
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes /*
1127df8bae1dSRodney W. Grimes  * IP socket option processing.
1128df8bae1dSRodney W. Grimes  */
1129df8bae1dSRodney W. Grimes int
1130cfe8b629SGarrett Wollman ip_ctloutput(so, sopt)
1131df8bae1dSRodney W. Grimes 	struct socket *so;
1132cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1133df8bae1dSRodney W. Grimes {
1134cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1135cfe8b629SGarrett Wollman 	int	error, optval;
1136df8bae1dSRodney W. Grimes 
1137cfe8b629SGarrett Wollman 	error = optval = 0;
1138cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1139cfe8b629SGarrett Wollman 		return (EINVAL);
1140cfe8b629SGarrett Wollman 	}
1141df8bae1dSRodney W. Grimes 
1142cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1143cfe8b629SGarrett Wollman 	case SOPT_SET:
1144cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1145df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1146df8bae1dSRodney W. Grimes #ifdef notyet
1147df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1148df8bae1dSRodney W. Grimes #endif
1149cfe8b629SGarrett Wollman 		{
1150cfe8b629SGarrett Wollman 			struct mbuf *m;
1151cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1152cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1153cfe8b629SGarrett Wollman 				break;
1154cfe8b629SGarrett Wollman 			}
11552a0c503eSBosko Milekic 			MGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1156cfe8b629SGarrett Wollman 			if (m == 0) {
1157cfe8b629SGarrett Wollman 				error = ENOBUFS;
1158cfe8b629SGarrett Wollman 				break;
1159cfe8b629SGarrett Wollman 			}
1160cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1161cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1162cfe8b629SGarrett Wollman 					    m->m_len);
1163cfe8b629SGarrett Wollman 
1164cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1165cfe8b629SGarrett Wollman 					   m));
1166cfe8b629SGarrett Wollman 		}
1167df8bae1dSRodney W. Grimes 
1168df8bae1dSRodney W. Grimes 		case IP_TOS:
1169df8bae1dSRodney W. Grimes 		case IP_TTL:
1170df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1171df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1172df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
117382c23ebaSBill Fenner 		case IP_RECVIF:
11746a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
11756a800098SYoshinobu Inoue 		case IP_FAITH:
11766a800098SYoshinobu Inoue #endif
1177cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1178cfe8b629SGarrett Wollman 					    sizeof optval);
1179cfe8b629SGarrett Wollman 			if (error)
1180cfe8b629SGarrett Wollman 				break;
1181df8bae1dSRodney W. Grimes 
1182cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1183df8bae1dSRodney W. Grimes 			case IP_TOS:
1184ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1185df8bae1dSRodney W. Grimes 				break;
1186df8bae1dSRodney W. Grimes 
1187df8bae1dSRodney W. Grimes 			case IP_TTL:
1188ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1189df8bae1dSRodney W. Grimes 				break;
1190df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
1191df8bae1dSRodney W. Grimes 	if (optval) \
1192df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
1193df8bae1dSRodney W. Grimes 	else \
1194df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1197df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1198df8bae1dSRodney W. Grimes 				break;
1199df8bae1dSRodney W. Grimes 
1200df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1201df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1202df8bae1dSRodney W. Grimes 				break;
1203df8bae1dSRodney W. Grimes 
1204df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1205df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1206df8bae1dSRodney W. Grimes 				break;
120782c23ebaSBill Fenner 
120882c23ebaSBill Fenner 			case IP_RECVIF:
120982c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
121082c23ebaSBill Fenner 				break;
12116a800098SYoshinobu Inoue 
12126a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
12136a800098SYoshinobu Inoue 			case IP_FAITH:
12146a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
12156a800098SYoshinobu Inoue 				break;
12166a800098SYoshinobu Inoue #endif
1217df8bae1dSRodney W. Grimes 			}
1218df8bae1dSRodney W. Grimes 			break;
1219df8bae1dSRodney W. Grimes #undef OPTSET
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1222f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1223df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1224df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1225df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1226df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1227cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1228df8bae1dSRodney W. Grimes 			break;
1229df8bae1dSRodney W. Grimes 
123033b3ac06SPeter Wemm 		case IP_PORTRANGE:
1231cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1232cfe8b629SGarrett Wollman 					    sizeof optval);
1233cfe8b629SGarrett Wollman 			if (error)
1234cfe8b629SGarrett Wollman 				break;
123533b3ac06SPeter Wemm 
123633b3ac06SPeter Wemm 			switch (optval) {
123733b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
123833b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
123933b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
124033b3ac06SPeter Wemm 				break;
124133b3ac06SPeter Wemm 
124233b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
124333b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
124433b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
124533b3ac06SPeter Wemm 				break;
124633b3ac06SPeter Wemm 
124733b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
124833b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
124933b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
125033b3ac06SPeter Wemm 				break;
125133b3ac06SPeter Wemm 
125233b3ac06SPeter Wemm 			default:
125333b3ac06SPeter Wemm 				error = EINVAL;
125433b3ac06SPeter Wemm 				break;
125533b3ac06SPeter Wemm 			}
1256ce8c72b1SPeter Wemm 			break;
125733b3ac06SPeter Wemm 
12586a800098SYoshinobu Inoue #ifdef IPSEC
12596a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12606a800098SYoshinobu Inoue 		{
12616a800098SYoshinobu Inoue 			caddr_t req;
1262686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
12636a800098SYoshinobu Inoue 			int priv;
12646a800098SYoshinobu Inoue 			struct mbuf *m;
12656a800098SYoshinobu Inoue 			int optname;
12666a800098SYoshinobu Inoue 
12676a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
12686a800098SYoshinobu Inoue 				break;
12696a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12706a800098SYoshinobu Inoue 				break;
12716a800098SYoshinobu Inoue 			priv = (sopt->sopt_p != NULL &&
12726a800098SYoshinobu Inoue 				suser(sopt->sopt_p) != 0) ? 0 : 1;
12736a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1274686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
12756a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1276686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
12776a800098SYoshinobu Inoue 			m_freem(m);
12786a800098SYoshinobu Inoue 			break;
12796a800098SYoshinobu Inoue 		}
12806a800098SYoshinobu Inoue #endif /*IPSEC*/
12816a800098SYoshinobu Inoue 
1282df8bae1dSRodney W. Grimes 		default:
1283df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1284df8bae1dSRodney W. Grimes 			break;
1285df8bae1dSRodney W. Grimes 		}
1286df8bae1dSRodney W. Grimes 		break;
1287df8bae1dSRodney W. Grimes 
1288cfe8b629SGarrett Wollman 	case SOPT_GET:
1289cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1290df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1291df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1292cfe8b629SGarrett Wollman 			if (inp->inp_options)
1293cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1294cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1295cfe8b629SGarrett Wollman 							  char *),
1296cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1297cfe8b629SGarrett Wollman 			else
1298cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1299df8bae1dSRodney W. Grimes 			break;
1300df8bae1dSRodney W. Grimes 
1301df8bae1dSRodney W. Grimes 		case IP_TOS:
1302df8bae1dSRodney W. Grimes 		case IP_TTL:
1303df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1304df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1305df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
130682c23ebaSBill Fenner 		case IP_RECVIF:
1307cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
13086a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
13096a800098SYoshinobu Inoue 		case IP_FAITH:
13106a800098SYoshinobu Inoue #endif
1311cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1312df8bae1dSRodney W. Grimes 
1313df8bae1dSRodney W. Grimes 			case IP_TOS:
1314ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1315df8bae1dSRodney W. Grimes 				break;
1316df8bae1dSRodney W. Grimes 
1317df8bae1dSRodney W. Grimes 			case IP_TTL:
1318ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1319df8bae1dSRodney W. Grimes 				break;
1320df8bae1dSRodney W. Grimes 
1321df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1322df8bae1dSRodney W. Grimes 
1323df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1324df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1325df8bae1dSRodney W. Grimes 				break;
1326df8bae1dSRodney W. Grimes 
1327df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1328df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1329df8bae1dSRodney W. Grimes 				break;
1330df8bae1dSRodney W. Grimes 
1331df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1332df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1333df8bae1dSRodney W. Grimes 				break;
133482c23ebaSBill Fenner 
133582c23ebaSBill Fenner 			case IP_RECVIF:
133682c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
133782c23ebaSBill Fenner 				break;
1338cfe8b629SGarrett Wollman 
1339cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1340cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1341cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1342cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1343cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1344cfe8b629SGarrett Wollman 				else
1345cfe8b629SGarrett Wollman 					optval = 0;
1346cfe8b629SGarrett Wollman 				break;
13476a800098SYoshinobu Inoue 
13486a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
13496a800098SYoshinobu Inoue 			case IP_FAITH:
13506a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
13516a800098SYoshinobu Inoue 				break;
13526a800098SYoshinobu Inoue #endif
1353df8bae1dSRodney W. Grimes 			}
1354cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1355df8bae1dSRodney W. Grimes 			break;
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1358f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1359df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1360df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1361df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1362df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1363cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
136433b3ac06SPeter Wemm 			break;
136533b3ac06SPeter Wemm 
13666a800098SYoshinobu Inoue #ifdef IPSEC
13676a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13686a800098SYoshinobu Inoue 		{
1369f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
13706a800098SYoshinobu Inoue 			caddr_t req = NULL;
1371686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
13726a800098SYoshinobu Inoue 
1373686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
13746a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1375686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1376686cdd19SJun-ichiro itojun Hagino 			}
1377686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
13786a800098SYoshinobu Inoue 			if (error == 0)
13796a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1380f63e7634SYoshinobu Inoue 			if (error == 0)
13816a800098SYoshinobu Inoue 				m_freem(m);
13826a800098SYoshinobu Inoue 			break;
13836a800098SYoshinobu Inoue 		}
13846a800098SYoshinobu Inoue #endif /*IPSEC*/
13856a800098SYoshinobu Inoue 
1386df8bae1dSRodney W. Grimes 		default:
1387df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1388df8bae1dSRodney W. Grimes 			break;
1389df8bae1dSRodney W. Grimes 		}
1390df8bae1dSRodney W. Grimes 		break;
1391df8bae1dSRodney W. Grimes 	}
1392df8bae1dSRodney W. Grimes 	return (error);
1393df8bae1dSRodney W. Grimes }
1394df8bae1dSRodney W. Grimes 
1395df8bae1dSRodney W. Grimes /*
1396df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1397df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1398df8bae1dSRodney W. Grimes  * with destination address if source routed.
1399df8bae1dSRodney W. Grimes  */
14000312fbe9SPoul-Henning Kamp static int
1401df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m)
1402df8bae1dSRodney W. Grimes 	int optname;
1403df8bae1dSRodney W. Grimes 	struct mbuf **pcbopt;
1404df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1405df8bae1dSRodney W. Grimes {
1406d68fa50cSBruce Evans 	register int cnt, optlen;
1407df8bae1dSRodney W. Grimes 	register u_char *cp;
1408df8bae1dSRodney W. Grimes 	u_char opt;
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 	/* turn off any old options */
1411df8bae1dSRodney W. Grimes 	if (*pcbopt)
1412df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1413df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1414df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1415df8bae1dSRodney W. Grimes 		/*
1416df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1417df8bae1dSRodney W. Grimes 		 */
1418df8bae1dSRodney W. Grimes 		if (m)
1419df8bae1dSRodney W. Grimes 			(void)m_free(m);
1420df8bae1dSRodney W. Grimes 		return (0);
1421df8bae1dSRodney W. Grimes 	}
1422df8bae1dSRodney W. Grimes 
14230c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1424df8bae1dSRodney W. Grimes 		goto bad;
1425df8bae1dSRodney W. Grimes 	/*
1426df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1427df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1428df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1429df8bae1dSRodney W. Grimes 	 */
1430df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1431df8bae1dSRodney W. Grimes 		goto bad;
1432df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1433df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1434df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1435df8bae1dSRodney W. Grimes 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1436df8bae1dSRodney W. Grimes 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1437df8bae1dSRodney W. Grimes 
1438df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1439df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1440df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1441df8bae1dSRodney W. Grimes 			break;
1442df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1443df8bae1dSRodney W. Grimes 			optlen = 1;
1444df8bae1dSRodney W. Grimes 		else {
1445707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1446707d00a3SJonathan Lemon 				goto bad;
1447df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1448707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1449df8bae1dSRodney W. Grimes 				goto bad;
1450df8bae1dSRodney W. Grimes 		}
1451df8bae1dSRodney W. Grimes 		switch (opt) {
1452df8bae1dSRodney W. Grimes 
1453df8bae1dSRodney W. Grimes 		default:
1454df8bae1dSRodney W. Grimes 			break;
1455df8bae1dSRodney W. Grimes 
1456df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1457df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1458df8bae1dSRodney W. Grimes 			/*
1459df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1460df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1461df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1462df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1463df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1464df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1465df8bae1dSRodney W. Grimes 			 */
1466df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1467df8bae1dSRodney W. Grimes 				goto bad;
1468df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1469df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1470df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1471df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1472df8bae1dSRodney W. Grimes 			/*
1473df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1474df8bae1dSRodney W. Grimes 			 */
1475df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1476df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1477df8bae1dSRodney W. Grimes 			/*
1478df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1479df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1480df8bae1dSRodney W. Grimes 			 */
1481df8bae1dSRodney W. Grimes 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1482df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr)),
1483df8bae1dSRodney W. Grimes 			    (caddr_t)&cp[IPOPT_OFFSET+1],
1484df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
1485df8bae1dSRodney W. Grimes 			break;
1486df8bae1dSRodney W. Grimes 		}
1487df8bae1dSRodney W. Grimes 	}
1488df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1489df8bae1dSRodney W. Grimes 		goto bad;
1490df8bae1dSRodney W. Grimes 	*pcbopt = m;
1491df8bae1dSRodney W. Grimes 	return (0);
1492df8bae1dSRodney W. Grimes 
1493df8bae1dSRodney W. Grimes bad:
1494df8bae1dSRodney W. Grimes 	(void)m_free(m);
1495df8bae1dSRodney W. Grimes 	return (EINVAL);
1496df8bae1dSRodney W. Grimes }
1497df8bae1dSRodney W. Grimes 
1498df8bae1dSRodney W. Grimes /*
1499cfe8b629SGarrett Wollman  * XXX
1500cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1501cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1502cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1503cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1504cfe8b629SGarrett Wollman  */
150533841545SHajimu UMEMOTO 
150633841545SHajimu UMEMOTO /*
150733841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
150833841545SHajimu UMEMOTO  */
150933841545SHajimu UMEMOTO static struct ifnet *
151033841545SHajimu UMEMOTO ip_multicast_if(a, ifindexp)
151133841545SHajimu UMEMOTO 	struct in_addr *a;
151233841545SHajimu UMEMOTO 	int *ifindexp;
151333841545SHajimu UMEMOTO {
151433841545SHajimu UMEMOTO 	int ifindex;
151533841545SHajimu UMEMOTO 	struct ifnet *ifp;
151633841545SHajimu UMEMOTO 
151733841545SHajimu UMEMOTO 	if (ifindexp)
151833841545SHajimu UMEMOTO 		*ifindexp = 0;
151933841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
152033841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
152133841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
152233841545SHajimu UMEMOTO 			return NULL;
152333841545SHajimu UMEMOTO 		ifp = ifindex2ifnet[ifindex];
152433841545SHajimu UMEMOTO 		if (ifindexp)
152533841545SHajimu UMEMOTO 			*ifindexp = ifindex;
152633841545SHajimu UMEMOTO 	} else {
152733841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
152833841545SHajimu UMEMOTO 	}
152933841545SHajimu UMEMOTO 	return ifp;
153033841545SHajimu UMEMOTO }
153133841545SHajimu UMEMOTO 
1532cfe8b629SGarrett Wollman /*
1533df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1534df8bae1dSRodney W. Grimes  */
15350312fbe9SPoul-Henning Kamp static int
1536cfe8b629SGarrett Wollman ip_setmoptions(sopt, imop)
1537cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1538df8bae1dSRodney W. Grimes 	struct ip_moptions **imop;
1539df8bae1dSRodney W. Grimes {
1540cfe8b629SGarrett Wollman 	int error = 0;
1541cfe8b629SGarrett Wollman 	int i;
1542df8bae1dSRodney W. Grimes 	struct in_addr addr;
1543cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1544cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1545cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1546df8bae1dSRodney W. Grimes 	struct route ro;
1547cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
154833841545SHajimu UMEMOTO 	int ifindex;
15499b626c29SGarrett Wollman 	int s;
1550df8bae1dSRodney W. Grimes 
1551df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1552df8bae1dSRodney W. Grimes 		/*
1553df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1554df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1555df8bae1dSRodney W. Grimes 		 */
1556df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1557df8bae1dSRodney W. Grimes 		    M_WAITOK);
1558df8bae1dSRodney W. Grimes 
1559df8bae1dSRodney W. Grimes 		if (imo == NULL)
1560df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1561df8bae1dSRodney W. Grimes 		*imop = imo;
1562df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
156333841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
15641c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1565df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1566df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1567df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1568df8bae1dSRodney W. Grimes 	}
1569df8bae1dSRodney W. Grimes 
1570cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1571f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1572f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1573cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
15745e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
15755e9ae478SGarrett Wollman 			break;
15765e9ae478SGarrett Wollman 		}
1577cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1578cfe8b629SGarrett Wollman 		if (error)
1579f0068c4aSGarrett Wollman 			break;
15801c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1581f0068c4aSGarrett Wollman 			error = EINVAL;
1582f0068c4aSGarrett Wollman 			break;
1583f0068c4aSGarrett Wollman 		}
1584f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1585f0068c4aSGarrett Wollman 		break;
1586f0068c4aSGarrett Wollman 
1587df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1588df8bae1dSRodney W. Grimes 		/*
1589df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1590df8bae1dSRodney W. Grimes 		 */
1591cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1592cfe8b629SGarrett Wollman 		if (error)
1593df8bae1dSRodney W. Grimes 			break;
1594df8bae1dSRodney W. Grimes 		/*
1595df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1596df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1597df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1598df8bae1dSRodney W. Grimes 		 */
1599df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1600df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1601df8bae1dSRodney W. Grimes 			break;
1602df8bae1dSRodney W. Grimes 		}
1603df8bae1dSRodney W. Grimes 		/*
1604df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1605df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1606df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1607df8bae1dSRodney W. Grimes 		 */
160820e8807cSGarrett Wollman 		s = splimp();
160933841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1610df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1611fbc6ab00SBill Fenner 			splx(s);
1612df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1613df8bae1dSRodney W. Grimes 			break;
1614df8bae1dSRodney W. Grimes 		}
1615df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
161633841545SHajimu UMEMOTO 		if (ifindex)
161733841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
161833841545SHajimu UMEMOTO 		else
161933841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
16209b626c29SGarrett Wollman 		splx(s);
1621df8bae1dSRodney W. Grimes 		break;
1622df8bae1dSRodney W. Grimes 
1623df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1624df8bae1dSRodney W. Grimes 		/*
1625df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1626cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1627cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1628cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1629df8bae1dSRodney W. Grimes 		 */
1630cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1631cfe8b629SGarrett Wollman 			u_char ttl;
1632cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1633cfe8b629SGarrett Wollman 			if (error)
1634df8bae1dSRodney W. Grimes 				break;
1635cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1636cfe8b629SGarrett Wollman 		} else {
1637cfe8b629SGarrett Wollman 			u_int ttl;
1638cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1639cfe8b629SGarrett Wollman 					    sizeof ttl);
1640cfe8b629SGarrett Wollman 			if (error)
1641cfe8b629SGarrett Wollman 				break;
1642cfe8b629SGarrett Wollman 			if (ttl > 255)
1643cfe8b629SGarrett Wollman 				error = EINVAL;
1644cfe8b629SGarrett Wollman 			else
1645cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1646df8bae1dSRodney W. Grimes 		}
1647df8bae1dSRodney W. Grimes 		break;
1648df8bae1dSRodney W. Grimes 
1649df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1650df8bae1dSRodney W. Grimes 		/*
1651df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1652cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1653cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1654cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1655df8bae1dSRodney W. Grimes 		 */
1656cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1657cfe8b629SGarrett Wollman 			u_char loop;
1658cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1659cfe8b629SGarrett Wollman 			if (error)
1660df8bae1dSRodney W. Grimes 				break;
1661cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1662cfe8b629SGarrett Wollman 		} else {
1663cfe8b629SGarrett Wollman 			u_int loop;
1664cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1665cfe8b629SGarrett Wollman 					    sizeof loop);
1666cfe8b629SGarrett Wollman 			if (error)
1667cfe8b629SGarrett Wollman 				break;
1668cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1669df8bae1dSRodney W. Grimes 		}
1670df8bae1dSRodney W. Grimes 		break;
1671df8bae1dSRodney W. Grimes 
1672df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1673df8bae1dSRodney W. Grimes 		/*
1674df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1675df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1676df8bae1dSRodney W. Grimes 		 */
1677cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1678cfe8b629SGarrett Wollman 		if (error)
1679df8bae1dSRodney W. Grimes 			break;
1680cfe8b629SGarrett Wollman 
1681cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1682df8bae1dSRodney W. Grimes 			error = EINVAL;
1683df8bae1dSRodney W. Grimes 			break;
1684df8bae1dSRodney W. Grimes 		}
168520e8807cSGarrett Wollman 		s = splimp();
1686df8bae1dSRodney W. Grimes 		/*
1687df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1688df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1689df8bae1dSRodney W. Grimes 		 */
1690cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
16911c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1692df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1693df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1694df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1695cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
1696df8bae1dSRodney W. Grimes 			rtalloc(&ro);
1697df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1698df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
16999b626c29SGarrett Wollman 				splx(s);
1700df8bae1dSRodney W. Grimes 				break;
1701df8bae1dSRodney W. Grimes 			}
1702df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1703df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
1704df8bae1dSRodney W. Grimes 		}
1705df8bae1dSRodney W. Grimes 		else {
170633841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1707df8bae1dSRodney W. Grimes 		}
17089b626c29SGarrett Wollman 
1709df8bae1dSRodney W. Grimes 		/*
1710df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1711df8bae1dSRodney W. Grimes 		 * supports multicast.
1712df8bae1dSRodney W. Grimes 		 */
1713df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1714df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
17159b626c29SGarrett Wollman 			splx(s);
1716df8bae1dSRodney W. Grimes 			break;
1717df8bae1dSRodney W. Grimes 		}
1718df8bae1dSRodney W. Grimes 		/*
1719df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1720df8bae1dSRodney W. Grimes 		 * membership slots are full.
1721df8bae1dSRodney W. Grimes 		 */
1722df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1723df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1724df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1725cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1726df8bae1dSRodney W. Grimes 				break;
1727df8bae1dSRodney W. Grimes 		}
1728df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1729df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
17309b626c29SGarrett Wollman 			splx(s);
1731df8bae1dSRodney W. Grimes 			break;
1732df8bae1dSRodney W. Grimes 		}
1733df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1734df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
17359b626c29SGarrett Wollman 			splx(s);
1736df8bae1dSRodney W. Grimes 			break;
1737df8bae1dSRodney W. Grimes 		}
1738df8bae1dSRodney W. Grimes 		/*
1739df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1740df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1741df8bae1dSRodney W. Grimes 		 */
1742df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1743cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1744df8bae1dSRodney W. Grimes 			error = ENOBUFS;
17459b626c29SGarrett Wollman 			splx(s);
1746df8bae1dSRodney W. Grimes 			break;
1747df8bae1dSRodney W. Grimes 		}
1748df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
17499b626c29SGarrett Wollman 		splx(s);
1750df8bae1dSRodney W. Grimes 		break;
1751df8bae1dSRodney W. Grimes 
1752df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1753df8bae1dSRodney W. Grimes 		/*
1754df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1755df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1756df8bae1dSRodney W. Grimes 		 */
1757cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1758cfe8b629SGarrett Wollman 		if (error)
1759df8bae1dSRodney W. Grimes 			break;
1760cfe8b629SGarrett Wollman 
1761cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1762df8bae1dSRodney W. Grimes 			error = EINVAL;
1763df8bae1dSRodney W. Grimes 			break;
1764df8bae1dSRodney W. Grimes 		}
17659b626c29SGarrett Wollman 
176620e8807cSGarrett Wollman 		s = splimp();
1767df8bae1dSRodney W. Grimes 		/*
1768df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1769df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1770df8bae1dSRodney W. Grimes 		 */
1771cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1772df8bae1dSRodney W. Grimes 			ifp = NULL;
1773df8bae1dSRodney W. Grimes 		else {
177433841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1775df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1776df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
17779b626c29SGarrett Wollman 				splx(s);
1778df8bae1dSRodney W. Grimes 				break;
1779df8bae1dSRodney W. Grimes 			}
1780df8bae1dSRodney W. Grimes 		}
1781df8bae1dSRodney W. Grimes 		/*
1782df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1783df8bae1dSRodney W. Grimes 		 */
1784df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1785df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1786df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1787df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1788cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1789df8bae1dSRodney W. Grimes 				break;
1790df8bae1dSRodney W. Grimes 		}
1791df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1792df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
17939b626c29SGarrett Wollman 			splx(s);
1794df8bae1dSRodney W. Grimes 			break;
1795df8bae1dSRodney W. Grimes 		}
1796df8bae1dSRodney W. Grimes 		/*
1797df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1798df8bae1dSRodney W. Grimes 		 * membership points.
1799df8bae1dSRodney W. Grimes 		 */
1800df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1801df8bae1dSRodney W. Grimes 		/*
1802df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1803df8bae1dSRodney W. Grimes 		 */
1804df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1805df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1806df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
18079b626c29SGarrett Wollman 		splx(s);
1808df8bae1dSRodney W. Grimes 		break;
1809df8bae1dSRodney W. Grimes 
1810df8bae1dSRodney W. Grimes 	default:
1811df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1812df8bae1dSRodney W. Grimes 		break;
1813df8bae1dSRodney W. Grimes 	}
1814df8bae1dSRodney W. Grimes 
1815df8bae1dSRodney W. Grimes 	/*
1816df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1817df8bae1dSRodney W. Grimes 	 */
1818df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
18191c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1820df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1821df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1822df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1823df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1824df8bae1dSRodney W. Grimes 		*imop = NULL;
1825df8bae1dSRodney W. Grimes 	}
1826df8bae1dSRodney W. Grimes 
1827df8bae1dSRodney W. Grimes 	return (error);
1828df8bae1dSRodney W. Grimes }
1829df8bae1dSRodney W. Grimes 
1830df8bae1dSRodney W. Grimes /*
1831df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1832df8bae1dSRodney W. Grimes  */
18330312fbe9SPoul-Henning Kamp static int
1834cfe8b629SGarrett Wollman ip_getmoptions(sopt, imo)
1835cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1836df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1837df8bae1dSRodney W. Grimes {
1838cfe8b629SGarrett Wollman 	struct in_addr addr;
1839df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1840cfe8b629SGarrett Wollman 	int error, optval;
1841cfe8b629SGarrett Wollman 	u_char coptval;
1842df8bae1dSRodney W. Grimes 
1843cfe8b629SGarrett Wollman 	error = 0;
1844cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1845f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1846f0068c4aSGarrett Wollman 		if (imo != NULL)
1847cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1848f0068c4aSGarrett Wollman 		else
1849cfe8b629SGarrett Wollman 			optval = -1;
1850cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1851cfe8b629SGarrett Wollman 		break;
1852f0068c4aSGarrett Wollman 
1853df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1854df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1855cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
185633841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
185733841545SHajimu UMEMOTO 			/* return the value user has set */
185833841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
185933841545SHajimu UMEMOTO 		} else {
1860df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1861cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1862df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1863df8bae1dSRodney W. Grimes 		}
1864cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1865cfe8b629SGarrett Wollman 		break;
1866df8bae1dSRodney W. Grimes 
1867df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1868cfe8b629SGarrett Wollman 		if (imo == 0)
1869cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1870cfe8b629SGarrett Wollman 		else
1871cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
1872cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1873cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1874cfe8b629SGarrett Wollman 		else
1875cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1876cfe8b629SGarrett Wollman 		break;
1877df8bae1dSRodney W. Grimes 
1878df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1879cfe8b629SGarrett Wollman 		if (imo == 0)
1880cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1881cfe8b629SGarrett Wollman 		else
1882cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
1883cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1884cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1885cfe8b629SGarrett Wollman 		else
1886cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1887cfe8b629SGarrett Wollman 		break;
1888df8bae1dSRodney W. Grimes 
1889df8bae1dSRodney W. Grimes 	default:
1890cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1891cfe8b629SGarrett Wollman 		break;
1892df8bae1dSRodney W. Grimes 	}
1893cfe8b629SGarrett Wollman 	return (error);
1894df8bae1dSRodney W. Grimes }
1895df8bae1dSRodney W. Grimes 
1896df8bae1dSRodney W. Grimes /*
1897df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1898df8bae1dSRodney W. Grimes  */
1899df8bae1dSRodney W. Grimes void
1900df8bae1dSRodney W. Grimes ip_freemoptions(imo)
1901df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1902df8bae1dSRodney W. Grimes {
1903df8bae1dSRodney W. Grimes 	register int i;
1904df8bae1dSRodney W. Grimes 
1905df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1906df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1907df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1908df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1909df8bae1dSRodney W. Grimes 	}
1910df8bae1dSRodney W. Grimes }
1911df8bae1dSRodney W. Grimes 
1912df8bae1dSRodney W. Grimes /*
1913df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1914df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1915df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1916f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1917f5fea3ddSPaul Traina  * replicating that code here.
1918df8bae1dSRodney W. Grimes  */
1919df8bae1dSRodney W. Grimes static void
192086b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen)
1921df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1922df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1923df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
192486b1d6d2SBill Fenner 	int hlen;
1925df8bae1dSRodney W. Grimes {
1926df8bae1dSRodney W. Grimes 	register struct ip *ip;
1927df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1928df8bae1dSRodney W. Grimes 
1929df8bae1dSRodney W. Grimes 	copym = m_copy(m, 0, M_COPYALL);
193086b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
193186b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1932df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1933df8bae1dSRodney W. Grimes 		/*
1934df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1935df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1936df8bae1dSRodney W. Grimes 		 */
1937df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
193804287599SRuslan Ermilov 		HTONS(ip->ip_len);
193904287599SRuslan Ermilov 		HTONS(ip->ip_off);
1940df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
19419c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
19429c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
19439c9137eaSGarrett Wollman 		} else {
194486b1d6d2SBill Fenner 			ip->ip_sum = in_cksum(copym, hlen);
19459c9137eaSGarrett Wollman 		}
19469c9137eaSGarrett Wollman 		/*
19479c9137eaSGarrett Wollman 		 * NB:
1948e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1949e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1950e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1951e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1952e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1953e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1954e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1955ed7509acSJulian Elischer 		 * of safety by using if_simloop().
19569c9137eaSGarrett Wollman 		 */
1957201c2527SJulian Elischer #if 1 /* XXX */
1958201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
19592b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1960201c2527SJulian Elischer 						dst->sin_family);
1961201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1962201c2527SJulian Elischer 		}
1963201c2527SJulian Elischer #endif
1964201c2527SJulian Elischer 
19659c9137eaSGarrett Wollman #ifdef notdef
1966e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
1967ed7509acSJulian Elischer 		ip_input(copym);
19689c9137eaSGarrett Wollman #else
196950c6dc99SJonathan Lemon 		/* if the checksum hasn't been computed, mark it as valid */
197050c6dc99SJonathan Lemon 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
197150c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_flags |=
197250c6dc99SJonathan Lemon 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
197350c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_data = 0xffff;
197450c6dc99SJonathan Lemon 		}
197506a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
19769c9137eaSGarrett Wollman #endif
1977df8bae1dSRodney W. Grimes 	}
1978df8bae1dSRodney W. Grimes }
1979