xref: /freebsd/sys/netinet/ip_output.c (revision 3efc30142cb8327bd1bb4cf97638be498e9970c9)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
379c9137eaSGarrett Wollman #define _IP_VHL
389c9137eaSGarrett Wollman 
391f7e052cSJulian Elischer #include "opt_ipfw.h"
40b715f178SLuigi Rizzo #include "opt_ipdn.h"
41fbd1372aSJoerg Wunsch #include "opt_ipdivert.h"
421ee25934SPeter Wemm #include "opt_ipfilter.h"
436a800098SYoshinobu Inoue #include "opt_ipsec.h"
44c4ac87eaSDarren Reed #include "opt_pfil_hooks.h"
4564dddc18SKris Kennaway #include "opt_random_ip_id.h"
46fbd1372aSJoerg Wunsch 
47df8bae1dSRodney W. Grimes #include <sys/param.h>
4826f9a767SRodney W. Grimes #include <sys/systm.h>
49f0f6d643SLuigi Rizzo #include <sys/kernel.h>
50df8bae1dSRodney W. Grimes #include <sys/malloc.h>
51df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <net/if.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <netinet/in.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
65df8bae1dSRodney W. Grimes 
669c9137eaSGarrett Wollman #include <machine/in_cksum.h>
67df8bae1dSRodney W. Grimes 
68a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
6955166637SPoul-Henning Kamp 
706a800098SYoshinobu Inoue #ifdef IPSEC
716a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
726a800098SYoshinobu Inoue #include <netkey/key.h>
736a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG
746a800098SYoshinobu Inoue #include <netkey/key_debug.h>
756a800098SYoshinobu Inoue #else
766a800098SYoshinobu Inoue #define	KEYDEBUG(lev,arg)
776a800098SYoshinobu Inoue #endif
786a800098SYoshinobu Inoue #endif /*IPSEC*/
796a800098SYoshinobu Inoue 
80cfe8b629SGarrett Wollman #include <netinet/ip_fw.h>
81b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
82b715f178SLuigi Rizzo 
83f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD_DEBUG
84f9e354dfSJulian Elischer #define print_ip(a)	 printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
85f9e354dfSJulian Elischer 				 		  (ntohl(a.s_addr)>>16)&0xFF,\
86f9e354dfSJulian Elischer 						  (ntohl(a.s_addr)>>8)&0xFF,\
87f9e354dfSJulian Elischer 						  (ntohl(a.s_addr))&0xFF);
88f9e354dfSJulian Elischer #endif
89f9e354dfSJulian Elischer 
90f23b4c91SGarrett Wollman u_short ip_id;
91f23b4c91SGarrett Wollman 
92df8bae1dSRodney W. Grimes static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
9333841545SHajimu UMEMOTO static struct ifnet *ip_multicast_if __P((struct in_addr *, int *));
94df8bae1dSRodney W. Grimes static void	ip_mloopback
9586b1d6d2SBill Fenner 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
960312fbe9SPoul-Henning Kamp static int	ip_getmoptions
97cfe8b629SGarrett Wollman 	__P((struct sockopt *, struct ip_moptions *));
98cfe8b629SGarrett Wollman static int	ip_pcbopts __P((int, struct mbuf **, struct mbuf *));
990312fbe9SPoul-Henning Kamp static int	ip_setmoptions
100cfe8b629SGarrett Wollman 	__P((struct sockopt *, struct ip_moptions **));
101df8bae1dSRodney W. Grimes 
102beec8214SDarren Reed int	ip_optcopy __P((struct ip *, struct ip *));
103afed1b49SDarren Reed 
104afed1b49SDarren Reed 
10593e0e116SJulian Elischer extern	struct protosw inetsw[];
10693e0e116SJulian Elischer 
107df8bae1dSRodney W. Grimes /*
108df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
109df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
110df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
111df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
112df8bae1dSRodney W. Grimes  */
113df8bae1dSRodney W. Grimes int
114df8bae1dSRodney W. Grimes ip_output(m0, opt, ro, flags, imo)
115df8bae1dSRodney W. Grimes 	struct mbuf *m0;
116df8bae1dSRodney W. Grimes 	struct mbuf *opt;
117df8bae1dSRodney W. Grimes 	struct route *ro;
118df8bae1dSRodney W. Grimes 	int flags;
119df8bae1dSRodney W. Grimes 	struct ip_moptions *imo;
120df8bae1dSRodney W. Grimes {
12123bf9953SPoul-Henning Kamp 	struct ip *ip, *mhip;
12223bf9953SPoul-Henning Kamp 	struct ifnet *ifp;
12323bf9953SPoul-Henning Kamp 	struct mbuf *m = m0;
12423bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
125df8bae1dSRodney W. Grimes 	int len, off, error = 0;
12604d59553SRuslan Ermilov 	struct route iproute;
127df8bae1dSRodney W. Grimes 	struct sockaddr_in *dst;
128df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
129db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
1303efc3014SJulian Elischer 	struct in_addr pkt_dst;
1316a800098SYoshinobu Inoue #ifdef IPSEC
132d0a98d79SYoshinobu Inoue 	struct socket *so = NULL;
1336a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1346a800098SYoshinobu Inoue #endif
1358948e4baSArchie Cobbs 	u_int16_t divert_cookie;		/* firewall cookie */
136c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
137c4ac87eaSDarren Reed 	struct packet_filter_hook *pfh;
138c4ac87eaSDarren Reed 	struct mbuf *m1;
139c4ac87eaSDarren Reed 	int rv;
140c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
141f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
142f9e354dfSJulian Elischer 	int fwd_rewrite_src = 0;
143f9e354dfSJulian Elischer #endif
144830cc178SLuigi Rizzo 	struct ip_fw *rule = NULL;
145b715f178SLuigi Rizzo 
1468948e4baSArchie Cobbs #ifdef IPDIVERT
1478948e4baSArchie Cobbs 	/* Get and reset firewall cookie */
1488948e4baSArchie Cobbs 	divert_cookie = ip_divert_cookie;
1498948e4baSArchie Cobbs 	ip_divert_cookie = 0;
1508948e4baSArchie Cobbs #else
1518948e4baSArchie Cobbs 	divert_cookie = 0;
1528948e4baSArchie Cobbs #endif
1538948e4baSArchie Cobbs 
154b715f178SLuigi Rizzo         /*
155b715f178SLuigi Rizzo          * dummynet packet are prepended a vestigial mbuf with
156b715f178SLuigi Rizzo          * m_type = MT_DUMMYNET and m_data pointing to the matching
157b715f178SLuigi Rizzo          * rule.
158b715f178SLuigi Rizzo          */
159b715f178SLuigi Rizzo         if (m->m_type == MT_DUMMYNET) {
160b715f178SLuigi Rizzo             /*
161b715f178SLuigi Rizzo              * the packet was already tagged, so part of the
162b715f178SLuigi Rizzo              * processing was already done, and we need to go down.
1636bc748b0SLuigi Rizzo              * Get parameters from the header.
164b715f178SLuigi Rizzo              */
165830cc178SLuigi Rizzo             rule = (struct ip_fw *)(m->m_data) ;
166d1f04b29SLuigi Rizzo 	    opt = NULL ;
167d1f04b29SLuigi Rizzo 	    ro = & ( ((struct dn_pkt *)m)->ro ) ;
168d1f04b29SLuigi Rizzo 	    imo = NULL ;
169d1f04b29SLuigi Rizzo 	    dst = ((struct dn_pkt *)m)->dn_dst ;
170d1f04b29SLuigi Rizzo 	    ifp = ((struct dn_pkt *)m)->ifp ;
171d0a98d79SYoshinobu Inoue 	    flags = ((struct dn_pkt *)m)->flags ;
172d1f04b29SLuigi Rizzo 
17317458d35SLuigi Rizzo             m0 = m = m->m_next ;
174d0a98d79SYoshinobu Inoue #ifdef IPSEC
175686cdd19SJun-ichiro itojun Hagino 	    so = ipsec_getsocket(m);
17633841545SHajimu UMEMOTO 	    (void)ipsec_setsocket(m, NULL);
177d0a98d79SYoshinobu Inoue #endif
178b715f178SLuigi Rizzo             ip = mtod(m, struct ip *);
179b715f178SLuigi Rizzo             hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
1809a10980eSJonathan Lemon             ia = ifatoia(ro->ro_rt->rt_ifa);
181b715f178SLuigi Rizzo             goto sendit;
182b715f178SLuigi Rizzo         } else
183b715f178SLuigi Rizzo             rule = NULL ;
184d0a98d79SYoshinobu Inoue #ifdef IPSEC
185686cdd19SJun-ichiro itojun Hagino 	so = ipsec_getsocket(m);
18633841545SHajimu UMEMOTO 	(void)ipsec_setsocket(m, NULL);
187d0a98d79SYoshinobu Inoue #endif
188b715f178SLuigi Rizzo 
189df8bae1dSRodney W. Grimes #ifdef	DIAGNOSTIC
190df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
191df8bae1dSRodney W. Grimes 		panic("ip_output no HDR");
192df8bae1dSRodney W. Grimes #endif
193df8bae1dSRodney W. Grimes 	if (opt) {
194df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
195df8bae1dSRodney W. Grimes 		hlen = len;
196df8bae1dSRodney W. Grimes 	}
197df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1983efc3014SJulian Elischer 	pkt_dst = ip_fw_fwd_addr == NULL
1993efc3014SJulian Elischer 		? ip->ip_dst : ip_fw_fwd_addr->sin_addr;
2003efc3014SJulian Elischer 
201df8bae1dSRodney W. Grimes 	/*
202df8bae1dSRodney W. Grimes 	 * Fill in IP header.
203df8bae1dSRodney W. Grimes 	 */
204df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
2059c9137eaSGarrett Wollman 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
206df8bae1dSRodney W. Grimes 		ip->ip_off &= IP_DF;
20764dddc18SKris Kennaway #ifdef RANDOM_IP_ID
20864dddc18SKris Kennaway 		ip->ip_id = ip_randomid();
20964dddc18SKris Kennaway #else
210e30177e0SRuslan Ermilov 		ip->ip_id = htons(ip_id++);
21164dddc18SKris Kennaway #endif
212df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
213df8bae1dSRodney W. Grimes 	} else {
2149c9137eaSGarrett Wollman 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
215df8bae1dSRodney W. Grimes 	}
2169c9137eaSGarrett Wollman 
21704d59553SRuslan Ermilov 	/* Route packet. */
21804d59553SRuslan Ermilov 	if (ro == NULL) {
21904d59553SRuslan Ermilov 		ro = &iproute;
22004d59553SRuslan Ermilov 		bzero(ro, sizeof(*ro));
22104d59553SRuslan Ermilov 	}
222df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
223df8bae1dSRodney W. Grimes 	/*
224df8bae1dSRodney W. Grimes 	 * If there is a cached route,
225df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
226df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
227df8bae1dSRodney W. Grimes 	 */
228df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
2293efc3014SJulian Elischer 	   dst->sin_addr.s_addr != pkt_dst.s_addr)) {
230df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
231df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
232df8bae1dSRodney W. Grimes 	}
233df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
234df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
235df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2363efc3014SJulian Elischer 		dst->sin_addr = pkt_dst;
237df8bae1dSRodney W. Grimes 	}
238df8bae1dSRodney W. Grimes 	/*
239df8bae1dSRodney W. Grimes 	 * If routing to interface only,
240df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
241df8bae1dSRodney W. Grimes 	 */
242df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
243df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
244df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
245df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
246df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
247df8bae1dSRodney W. Grimes 			goto bad;
248df8bae1dSRodney W. Grimes 		}
249df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
250df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2519f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2523afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
25338c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2543afefa39SDaniel C. Sobral 		/*
25538c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
25638c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2573afefa39SDaniel C. Sobral 		 */
25838c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
25938c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
26038c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
261df8bae1dSRodney W. Grimes 	} else {
2622c17fe93SGarrett Wollman 		/*
263f2c2962eSRuslan Ermilov 		 * If this is the case, we probably don't want to allocate
264f2c2962eSRuslan Ermilov 		 * a protocol-cloned route since we didn't get one from the
265f2c2962eSRuslan Ermilov 		 * ULP.  This lets TCP do its thing, while not burdening
266f2c2962eSRuslan Ermilov 		 * forwarding or ICMP with the overhead of cloning a route.
267f2c2962eSRuslan Ermilov 		 * Of course, we still want to do any cloning requested by
268f2c2962eSRuslan Ermilov 		 * the link layer, as this is probably required in all cases
269f2c2962eSRuslan Ermilov 		 * for correct operation (as it is for ARP).
2702c17fe93SGarrett Wollman 		 */
271df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
2722c17fe93SGarrett Wollman 			rtalloc_ign(ro, RTF_PRCLONING);
273df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
274df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
275df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
276df8bae1dSRodney W. Grimes 			goto bad;
277df8bae1dSRodney W. Grimes 		}
278df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
279df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
280df8bae1dSRodney W. Grimes 		ro->ro_rt->rt_use++;
281df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
282f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2839f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
284f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2859f9b3dc4SGarrett Wollman 		else
2869f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
287df8bae1dSRodney W. Grimes 	}
2883efc3014SJulian Elischer 	if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
289df8bae1dSRodney W. Grimes 		struct in_multi *inm;
290df8bae1dSRodney W. Grimes 
291df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
292df8bae1dSRodney W. Grimes 		/*
293df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
294df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
295df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
296df8bae1dSRodney W. Grimes 		 */
297df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
298df8bae1dSRodney W. Grimes 		/*
299df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
300df8bae1dSRodney W. Grimes 		 */
301df8bae1dSRodney W. Grimes 		if (imo != NULL) {
302df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
3031c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3041c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
3051c5de19aSGarrett Wollman 				    ip_mcast_src(imo->imo_multicast_vif);
306df8bae1dSRodney W. Grimes 		} else
307df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
308df8bae1dSRodney W. Grimes 		/*
309df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
310df8bae1dSRodney W. Grimes 		 */
3111c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
312df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
313df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
314df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
315df8bae1dSRodney W. Grimes 				goto bad;
316df8bae1dSRodney W. Grimes 			}
3171c5de19aSGarrett Wollman 		}
318df8bae1dSRodney W. Grimes 		/*
319df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
320df8bae1dSRodney W. Grimes 		 * of outgoing interface.
321df8bae1dSRodney W. Grimes 		 */
322df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
32338c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
32438c1bc35SRuslan Ermilov 			if (ia != NULL)
32538c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
326df8bae1dSRodney W. Grimes 		}
327df8bae1dSRodney W. Grimes 
3283efc3014SJulian Elischer 		IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
329df8bae1dSRodney W. Grimes 		if (inm != NULL &&
330df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
331df8bae1dSRodney W. Grimes 			/*
332df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
333df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
334df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
335df8bae1dSRodney W. Grimes 			 */
33686b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
337df8bae1dSRodney W. Grimes 		}
338df8bae1dSRodney W. Grimes 		else {
339df8bae1dSRodney W. Grimes 			/*
340df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
341df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
342df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
343df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
344df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
345df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
346df8bae1dSRodney W. Grimes 			 *
347df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
348df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
349df8bae1dSRodney W. Grimes 			 * if necessary.
350df8bae1dSRodney W. Grimes 			 */
351df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
352f0068c4aSGarrett Wollman 				/*
353f0068c4aSGarrett Wollman 				 * Check if rsvp daemon is running. If not, don't
354f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
355f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
356f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
357f0068c4aSGarrett Wollman 				 */
358b124e4f2SGarrett Wollman 				if (!rsvp_on)
359f0068c4aSGarrett Wollman 				  imo = NULL;
360f0068c4aSGarrett Wollman 				if (ip_mforward(ip, ifp, m, imo) != 0) {
361df8bae1dSRodney W. Grimes 					m_freem(m);
362df8bae1dSRodney W. Grimes 					goto done;
363df8bae1dSRodney W. Grimes 				}
364df8bae1dSRodney W. Grimes 			}
365df8bae1dSRodney W. Grimes 		}
3665e9ae478SGarrett Wollman 
367df8bae1dSRodney W. Grimes 		/*
368df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
369df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
370df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
371df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
372df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
373df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
374df8bae1dSRodney W. Grimes 		 */
375f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
376df8bae1dSRodney W. Grimes 			m_freem(m);
377df8bae1dSRodney W. Grimes 			goto done;
378df8bae1dSRodney W. Grimes 		}
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes 		goto sendit;
381df8bae1dSRodney W. Grimes 	}
382df8bae1dSRodney W. Grimes #ifndef notdef
383df8bae1dSRodney W. Grimes 	/*
384df8bae1dSRodney W. Grimes 	 * If source address not specified yet, use address
385df8bae1dSRodney W. Grimes 	 * of outgoing interface.
386df8bae1dSRodney W. Grimes 	 */
387f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
38838c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
38938c1bc35SRuslan Ermilov 		if (ia != NULL) {
390df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
391f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
392f9e354dfSJulian Elischer 			/* Keep note that we did this - if the firewall changes
393f9e354dfSJulian Elischer 		 	* the next-hop, our interface may change, changing the
394f9e354dfSJulian Elischer 		 	* default source IP. It's a shame so much effort happens
395f9e354dfSJulian Elischer 		 	* twice. Oh well.
396f9e354dfSJulian Elischer 		 	*/
397f9e354dfSJulian Elischer 			fwd_rewrite_src++;
398f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */
399f9e354dfSJulian Elischer 		}
40038c1bc35SRuslan Ermilov 	}
401f9e354dfSJulian Elischer #endif /* notdef */
402df8bae1dSRodney W. Grimes 	/*
403b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
404b5390296SDavid Greenman 	 *      the packet or packet fragments
405b5390296SDavid Greenman 	 */
406b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
407b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
408b5390296SDavid Greenman 			error = ENOBUFS;
40935609d45SJonathan Lemon 			ipstat.ips_odropped++;
410b5390296SDavid Greenman 			goto bad;
411b5390296SDavid Greenman 	}
412b5390296SDavid Greenman 
413b5390296SDavid Greenman 	/*
414df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
415df8bae1dSRodney W. Grimes 	 * and verify user is allowed to send
416df8bae1dSRodney W. Grimes 	 * such a packet.
417df8bae1dSRodney W. Grimes 	 */
4189f9b3dc4SGarrett Wollman 	if (isbroadcast) {
419df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
420df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
421df8bae1dSRodney W. Grimes 			goto bad;
422df8bae1dSRodney W. Grimes 		}
423df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
424df8bae1dSRodney W. Grimes 			error = EACCES;
425df8bae1dSRodney W. Grimes 			goto bad;
426df8bae1dSRodney W. Grimes 		}
427df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
428df8bae1dSRodney W. Grimes 		if ((u_short)ip->ip_len > ifp->if_mtu) {
429df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
430df8bae1dSRodney W. Grimes 			goto bad;
431df8bae1dSRodney W. Grimes 		}
432df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4339f9b3dc4SGarrett Wollman 	} else {
434df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4359f9b3dc4SGarrett Wollman 	}
436df8bae1dSRodney W. Grimes 
437f1743588SDarren Reed sendit:
43833841545SHajimu UMEMOTO #ifdef IPSEC
43933841545SHajimu UMEMOTO 	/* get SP for this packet */
44033841545SHajimu UMEMOTO 	if (so == NULL)
44133841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
44233841545SHajimu UMEMOTO 	else
44333841545SHajimu UMEMOTO 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
44433841545SHajimu UMEMOTO 
44533841545SHajimu UMEMOTO 	if (sp == NULL) {
44633841545SHajimu UMEMOTO 		ipsecstat.out_inval++;
44733841545SHajimu UMEMOTO 		goto bad;
44833841545SHajimu UMEMOTO 	}
44933841545SHajimu UMEMOTO 
45033841545SHajimu UMEMOTO 	error = 0;
45133841545SHajimu UMEMOTO 
45233841545SHajimu UMEMOTO 	/* check policy */
45333841545SHajimu UMEMOTO 	switch (sp->policy) {
45433841545SHajimu UMEMOTO 	case IPSEC_POLICY_DISCARD:
45533841545SHajimu UMEMOTO 		/*
45633841545SHajimu UMEMOTO 		 * This packet is just discarded.
45733841545SHajimu UMEMOTO 		 */
45833841545SHajimu UMEMOTO 		ipsecstat.out_polvio++;
45933841545SHajimu UMEMOTO 		goto bad;
46033841545SHajimu UMEMOTO 
46133841545SHajimu UMEMOTO 	case IPSEC_POLICY_BYPASS:
46233841545SHajimu UMEMOTO 	case IPSEC_POLICY_NONE:
46333841545SHajimu UMEMOTO 		/* no need to do IPsec. */
46433841545SHajimu UMEMOTO 		goto skip_ipsec;
46533841545SHajimu UMEMOTO 
46633841545SHajimu UMEMOTO 	case IPSEC_POLICY_IPSEC:
46733841545SHajimu UMEMOTO 		if (sp->req == NULL) {
46833841545SHajimu UMEMOTO 			/* acquire a policy */
46933841545SHajimu UMEMOTO 			error = key_spdacquire(sp);
47033841545SHajimu UMEMOTO 			goto bad;
47133841545SHajimu UMEMOTO 		}
47233841545SHajimu UMEMOTO 		break;
47333841545SHajimu UMEMOTO 
47433841545SHajimu UMEMOTO 	case IPSEC_POLICY_ENTRUST:
47533841545SHajimu UMEMOTO 	default:
47633841545SHajimu UMEMOTO 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
47733841545SHajimu UMEMOTO 	}
47833841545SHajimu UMEMOTO     {
47933841545SHajimu UMEMOTO 	struct ipsec_output_state state;
48033841545SHajimu UMEMOTO 	bzero(&state, sizeof(state));
48133841545SHajimu UMEMOTO 	state.m = m;
48233841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
48333841545SHajimu UMEMOTO 		state.ro = &iproute;
48433841545SHajimu UMEMOTO 		bzero(&iproute, sizeof(iproute));
48533841545SHajimu UMEMOTO 	} else
48633841545SHajimu UMEMOTO 		state.ro = ro;
48733841545SHajimu UMEMOTO 	state.dst = (struct sockaddr *)dst;
48833841545SHajimu UMEMOTO 
48933841545SHajimu UMEMOTO 	ip->ip_sum = 0;
49033841545SHajimu UMEMOTO 
49133841545SHajimu UMEMOTO 	/*
49233841545SHajimu UMEMOTO 	 * XXX
49333841545SHajimu UMEMOTO 	 * delayed checksums are not currently compatible with IPsec
49433841545SHajimu UMEMOTO 	 */
49533841545SHajimu UMEMOTO 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
49633841545SHajimu UMEMOTO 		in_delayed_cksum(m);
49733841545SHajimu UMEMOTO 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
49833841545SHajimu UMEMOTO 	}
49933841545SHajimu UMEMOTO 
50033841545SHajimu UMEMOTO 	HTONS(ip->ip_len);
50133841545SHajimu UMEMOTO 	HTONS(ip->ip_off);
50233841545SHajimu UMEMOTO 
50333841545SHajimu UMEMOTO 	error = ipsec4_output(&state, sp, flags);
50433841545SHajimu UMEMOTO 
50533841545SHajimu UMEMOTO 	m = state.m;
50633841545SHajimu UMEMOTO 	if (flags & IP_ROUTETOIF) {
50733841545SHajimu UMEMOTO 		/*
50833841545SHajimu UMEMOTO 		 * if we have tunnel mode SA, we may need to ignore
50933841545SHajimu UMEMOTO 		 * IP_ROUTETOIF.
51033841545SHajimu UMEMOTO 		 */
51133841545SHajimu UMEMOTO 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
51233841545SHajimu UMEMOTO 			flags &= ~IP_ROUTETOIF;
51333841545SHajimu UMEMOTO 			ro = state.ro;
51433841545SHajimu UMEMOTO 		}
51533841545SHajimu UMEMOTO 	} else
51633841545SHajimu UMEMOTO 		ro = state.ro;
51733841545SHajimu UMEMOTO 	dst = (struct sockaddr_in *)state.dst;
51833841545SHajimu UMEMOTO 	if (error) {
51933841545SHajimu UMEMOTO 		/* mbuf is already reclaimed in ipsec4_output. */
52033841545SHajimu UMEMOTO 		m0 = NULL;
52133841545SHajimu UMEMOTO 		switch (error) {
52233841545SHajimu UMEMOTO 		case EHOSTUNREACH:
52333841545SHajimu UMEMOTO 		case ENETUNREACH:
52433841545SHajimu UMEMOTO 		case EMSGSIZE:
52533841545SHajimu UMEMOTO 		case ENOBUFS:
52633841545SHajimu UMEMOTO 		case ENOMEM:
52733841545SHajimu UMEMOTO 			break;
52833841545SHajimu UMEMOTO 		default:
52933841545SHajimu UMEMOTO 			printf("ip4_output (ipsec): error code %d\n", error);
53033841545SHajimu UMEMOTO 			/*fall through*/
53133841545SHajimu UMEMOTO 		case ENOENT:
53233841545SHajimu UMEMOTO 			/* don't show these error codes to the user */
53333841545SHajimu UMEMOTO 			error = 0;
53433841545SHajimu UMEMOTO 			break;
53533841545SHajimu UMEMOTO 		}
53633841545SHajimu UMEMOTO 		goto bad;
53733841545SHajimu UMEMOTO 	}
53833841545SHajimu UMEMOTO     }
53933841545SHajimu UMEMOTO 
54033841545SHajimu UMEMOTO 	/* be sure to update variables that are affected by ipsec4_output() */
54133841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
54233841545SHajimu UMEMOTO #ifdef _IP_VHL
54333841545SHajimu UMEMOTO 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
54433841545SHajimu UMEMOTO #else
54533841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
54633841545SHajimu UMEMOTO #endif
54733841545SHajimu UMEMOTO 	if (ro->ro_rt == NULL) {
54833841545SHajimu UMEMOTO 		if ((flags & IP_ROUTETOIF) == 0) {
54933841545SHajimu UMEMOTO 			printf("ip_output: "
55033841545SHajimu UMEMOTO 				"can't update route after IPsec processing\n");
55133841545SHajimu UMEMOTO 			error = EHOSTUNREACH;	/*XXX*/
55233841545SHajimu UMEMOTO 			goto bad;
55333841545SHajimu UMEMOTO 		}
55433841545SHajimu UMEMOTO 	} else {
55533841545SHajimu UMEMOTO 		ia = ifatoia(ro->ro_rt->rt_ifa);
55633841545SHajimu UMEMOTO 		ifp = ro->ro_rt->rt_ifp;
55733841545SHajimu UMEMOTO 	}
55833841545SHajimu UMEMOTO 
55933841545SHajimu UMEMOTO 	/* make it flipped, again. */
56033841545SHajimu UMEMOTO 	NTOHS(ip->ip_len);
56133841545SHajimu UMEMOTO 	NTOHS(ip->ip_off);
56233841545SHajimu UMEMOTO skip_ipsec:
56333841545SHajimu UMEMOTO #endif /*IPSEC*/
56433841545SHajimu UMEMOTO 
565fed1c7e9SSøren Schmidt 	/*
566fed1c7e9SSøren Schmidt 	 * IpHack's section.
567fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
568e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
569fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
570fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
571fed1c7e9SSøren Schmidt 	 */
572c4ac87eaSDarren Reed #ifdef PFIL_HOOKS
573c4ac87eaSDarren Reed 	/*
574c4ac87eaSDarren Reed 	 * Run through list of hooks for output packets.
575c4ac87eaSDarren Reed 	 */
576c4ac87eaSDarren Reed 	m1 = m;
577c4ac87eaSDarren Reed 	pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
578fc2ffbe6SPoul-Henning Kamp 	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
579c4ac87eaSDarren Reed 		if (pfh->pfil_func) {
580c4ac87eaSDarren Reed 			rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
581c4ac87eaSDarren Reed 			if (rv) {
582c4ac87eaSDarren Reed 				error = EHOSTUNREACH;
5831ee25934SPeter Wemm 				goto done;
5841ee25934SPeter Wemm 			}
585c4ac87eaSDarren Reed 			m = m1;
586c4ac87eaSDarren Reed 			if (m == NULL)
587c4ac87eaSDarren Reed 				goto done;
588c4ac87eaSDarren Reed 			ip = mtod(m, struct ip *);
589c4ac87eaSDarren Reed 		}
590c4ac87eaSDarren Reed #endif /* PFIL_HOOKS */
591fed1c7e9SSøren Schmidt 
592df8bae1dSRodney W. Grimes 	/*
593e7319babSPoul-Henning Kamp 	 * Check with the firewall...
5943efc3014SJulian Elischer 	 * but not if we are already being fwd'd from a firewall.
595e7319babSPoul-Henning Kamp 	 */
5963efc3014SJulian Elischer 	if (fw_enable && IPFW_LOADED && !ip_fw_fwd_addr) {
5979de9737fSPeter Wemm 		struct sockaddr_in *old = dst;
598b715f178SLuigi Rizzo 
5997b109fa4SLuigi Rizzo 		off = ip_fw_chk_ptr(&ip,
6008948e4baSArchie Cobbs 		    hlen, ifp, &divert_cookie, &m, &rule, &dst);
601b715f178SLuigi Rizzo                 /*
602b715f178SLuigi Rizzo                  * On return we must do the following:
603507b4b54SLuigi Rizzo                  * m == NULL         -> drop the pkt (old interface, deprecated)
604aa1f5daaSJonathan Lemon                  * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
605b715f178SLuigi Rizzo                  * 1<=off<= 0xffff		-> DIVERT
606aa1f5daaSJonathan Lemon                  * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
607aa1f5daaSJonathan Lemon                  * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
608b715f178SLuigi Rizzo                  * dst != old			-> IPFIREWALL_FORWARD
609b715f178SLuigi Rizzo                  * off==0, dst==old		-> accept
610aa1f5daaSJonathan Lemon                  * If some of the above modules are not compiled in, then
611b715f178SLuigi Rizzo                  * we should't have to check the corresponding condition
612b715f178SLuigi Rizzo                  * (because the ipfw control socket should not accept
613b715f178SLuigi Rizzo                  * unsupported rules), but better play safe and drop
614b715f178SLuigi Rizzo                  * packets in case of doubt.
615b715f178SLuigi Rizzo                  */
616507b4b54SLuigi Rizzo 		if (off & IP_FW_PORT_DENY_FLAG) { /* XXX new interface-denied */
617507b4b54SLuigi Rizzo 			if (m)
618507b4b54SLuigi Rizzo 				m_freem(m);
619507b4b54SLuigi Rizzo 			error = EACCES;
620507b4b54SLuigi Rizzo 			goto done;
621507b4b54SLuigi Rizzo 		}
622b715f178SLuigi Rizzo 		if (!m) {			/* firewall said to reject */
623507b4b54SLuigi Rizzo 			static int __debug=10;
624aa1f5daaSJonathan Lemon 
625507b4b54SLuigi Rizzo 			if (__debug > 0) {
626aa1f5daaSJonathan Lemon 				printf(
627aa1f5daaSJonathan Lemon 				    "firewall returns NULL, please update!\n");
628507b4b54SLuigi Rizzo 				__debug--;
629507b4b54SLuigi Rizzo 			}
630e4676ba6SJulian Elischer 			error = EACCES;
631e4676ba6SJulian Elischer 			goto done;
63293e0e116SJulian Elischer 		}
633b715f178SLuigi Rizzo 		if (off == 0 && dst == old)		/* common case */
634b715f178SLuigi Rizzo 			goto pass;
6357b109fa4SLuigi Rizzo                 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
636b715f178SLuigi Rizzo 			/*
637b715f178SLuigi Rizzo 			 * pass the pkt to dummynet. Need to include
638f0a53591SLuigi Rizzo 			 * pipe number, m, ifp, ro, dst because these are
639b715f178SLuigi Rizzo 			 * not recomputed in the next pass.
640b715f178SLuigi Rizzo 			 * All other parameters have been already used and
641b715f178SLuigi Rizzo 			 * so they are not needed anymore.
642b715f178SLuigi Rizzo 			 * XXX note: if the ifp or ro entry are deleted
643b715f178SLuigi Rizzo 			 * while a pkt is in dummynet, we are in trouble!
644b715f178SLuigi Rizzo 			 */
645db69a05dSPaul Saab 			error = ip_dn_io_ptr(off & 0xffff, DN_TO_IP_OUT, m,
6467a726a2dSLuigi Rizzo 			    ifp, ro, dst, rule, flags);
647b715f178SLuigi Rizzo 			goto done;
648b715f178SLuigi Rizzo 		}
649b715f178SLuigi Rizzo #ifdef IPDIVERT
6508948e4baSArchie Cobbs 		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
6518948e4baSArchie Cobbs 			struct mbuf *clone = NULL;
6528948e4baSArchie Cobbs 
6538948e4baSArchie Cobbs 			/* Clone packet if we're doing a 'tee' */
6548948e4baSArchie Cobbs 			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
6558948e4baSArchie Cobbs 				clone = m_dup(m, M_DONTWAIT);
6568948e4baSArchie Cobbs 
657ea53ecd9SJonathan Lemon 			/*
658ea53ecd9SJonathan Lemon 			 * XXX
659ea53ecd9SJonathan Lemon 			 * delayed checksums are not currently compatible
660ea53ecd9SJonathan Lemon 			 * with divert sockets.
661ea53ecd9SJonathan Lemon 			 */
662ea53ecd9SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
663ea53ecd9SJonathan Lemon 				in_delayed_cksum(m);
664ea53ecd9SJonathan Lemon 				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
665ea53ecd9SJonathan Lemon 			}
666ea53ecd9SJonathan Lemon 
6678948e4baSArchie Cobbs 			/* Restore packet header fields to original values */
6688948e4baSArchie Cobbs 			HTONS(ip->ip_len);
6698948e4baSArchie Cobbs 			HTONS(ip->ip_off);
6708948e4baSArchie Cobbs 
6718948e4baSArchie Cobbs 			/* Deliver packet to divert input routine */
6728948e4baSArchie Cobbs 			ip_divert_cookie = divert_cookie;
6738948e4baSArchie Cobbs 			divert_packet(m, 0, off & 0xffff);
6748948e4baSArchie Cobbs 
6758948e4baSArchie Cobbs 			/* If 'tee', continue with original packet */
6768948e4baSArchie Cobbs 			if (clone != NULL) {
6778948e4baSArchie Cobbs 				m = clone;
6788948e4baSArchie Cobbs 				ip = mtod(m, struct ip *);
6798948e4baSArchie Cobbs 				goto pass;
6808948e4baSArchie Cobbs 			}
681b715f178SLuigi Rizzo 			goto done;
682b715f178SLuigi Rizzo 		}
683b715f178SLuigi Rizzo #endif
684b715f178SLuigi Rizzo 
685f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
686f9e354dfSJulian Elischer 		/* Here we check dst to make sure it's directly reachable on the
687f9e354dfSJulian Elischer 		 * interface we previously thought it was.
688f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
689f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
690f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
691f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
692f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
693f9e354dfSJulian Elischer 		 * And I'm babbling.
694f9e354dfSJulian Elischer 		 */
695b715f178SLuigi Rizzo 		if (off == 0 && old != dst) {
696f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
697f9e354dfSJulian Elischer 
698f9e354dfSJulian Elischer 			/* It's changed... */
699f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
700f9e354dfSJulian Elischer 			static struct route sro_fwd, *ro_fwd = &sro_fwd;
701f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD_DEBUG
702f9e354dfSJulian Elischer 			printf("IPFIREWALL_FORWARD: New dst ip: ");
703f9e354dfSJulian Elischer 			print_ip(dst->sin_addr);
704f9e354dfSJulian Elischer 			printf("\n");
705f9e354dfSJulian Elischer #endif
706f9e354dfSJulian Elischer 			/*
707f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
708f9e354dfSJulian Elischer 			 * to a local socket. If so then we should somehow
709f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
710f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
711f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
712f9e354dfSJulian Elischer 			 * you want to forward until they are being output
713f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
714f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
715f9e354dfSJulian Elischer 			 * we would not be able to control what happens
716f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
717f9e354dfSJulian Elischer 			 * it is done through a ISR.
718f9e354dfSJulian Elischer 			 */
719ca925d9cSJonathan Lemon 			LIST_FOREACH(ia,
720ca925d9cSJonathan Lemon 			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
721f9e354dfSJulian Elischer 				/*
722f9e354dfSJulian Elischer 				 * If the addr to forward to is one
723f9e354dfSJulian Elischer 				 * of ours, we pretend to
724f9e354dfSJulian Elischer 				 * be the destination for this packet.
725f9e354dfSJulian Elischer 				 */
726f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
727f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
728f9e354dfSJulian Elischer 					break;
729f9e354dfSJulian Elischer 			}
730f9e354dfSJulian Elischer 			if (ia) {
731f9e354dfSJulian Elischer 				/* tell ip_input "dont filter" */
732f9e354dfSJulian Elischer 				ip_fw_fwd_addr = dst;
733f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
734f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
73520c822f3SJonathan Lemon 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
73620c822f3SJonathan Lemon 					m->m_pkthdr.csum_flags |=
73720c822f3SJonathan Lemon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
73820c822f3SJonathan Lemon 					m0->m_pkthdr.csum_data = 0xffff;
73920c822f3SJonathan Lemon 				}
74020c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
74120c822f3SJonathan Lemon 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
74204287599SRuslan Ermilov 				HTONS(ip->ip_len);
74304287599SRuslan Ermilov 				HTONS(ip->ip_off);
744f9e354dfSJulian Elischer 				ip_input(m);
745f9e354dfSJulian Elischer 				goto done;
746f9e354dfSJulian Elischer 			}
747f9e354dfSJulian Elischer 			/* Some of the logic for this was
748f9e354dfSJulian Elischer 			 * nicked from above.
749f9e354dfSJulian Elischer 			 *
750f9e354dfSJulian Elischer 			 * This rewrites the cached route in a local PCB.
751f9e354dfSJulian Elischer 			 * Is this what we want to do?
752f9e354dfSJulian Elischer 			 */
753f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
754f9e354dfSJulian Elischer 
755f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
756f9e354dfSJulian Elischer 			rtalloc_ign(ro_fwd, RTF_PRCLONING);
757f9e354dfSJulian Elischer 
758f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt == 0) {
759f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
760f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
761f9e354dfSJulian Elischer 				goto bad;
762f9e354dfSJulian Elischer 			}
763f9e354dfSJulian Elischer 
764f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
765f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
766f9e354dfSJulian Elischer 			ro_fwd->ro_rt->rt_use++;
767f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
768f9e354dfSJulian Elischer 				dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
769f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
770f9e354dfSJulian Elischer 				isbroadcast =
771f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
772f9e354dfSJulian Elischer 			else
773f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
7743f9e3122SYaroslav Tykhiy 			if (ro->ro_rt)
775f9e354dfSJulian Elischer 				RTFREE(ro->ro_rt);
776f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
777f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
778f9e354dfSJulian Elischer 
779f9e354dfSJulian Elischer 			/*
780f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
781f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
782f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
783f9e354dfSJulian Elischer 			 */
784f9e354dfSJulian Elischer 			if (fwd_rewrite_src)
785f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
786b715f178SLuigi Rizzo 			goto pass ;
787f9e354dfSJulian Elischer 		}
788f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */
789b715f178SLuigi Rizzo                 /*
790b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
791b715f178SLuigi Rizzo                  * we have to drop the pkt
792b715f178SLuigi Rizzo                  */
793b715f178SLuigi Rizzo 		m_freem(m);
794b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
795b715f178SLuigi Rizzo                 goto done;
79693e0e116SJulian Elischer 	}
797e7319babSPoul-Henning Kamp 
7983efc3014SJulian Elischer 	ip_fw_fwd_addr = NULL;
799b715f178SLuigi Rizzo pass:
800206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
801206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
802db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
803db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
804db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
805db4f9cc7SJonathan Lemon 	}
806206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
807db4f9cc7SJonathan Lemon 
808e7319babSPoul-Henning Kamp 	/*
809db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
810db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
811df8bae1dSRodney W. Grimes 	 */
812db4f9cc7SJonathan Lemon 	if ((u_short)ip->ip_len <= ifp->if_mtu ||
813db4f9cc7SJonathan Lemon 	    ifp->if_hwassist & CSUM_FRAGMENT) {
81404287599SRuslan Ermilov 		HTONS(ip->ip_len);
81504287599SRuslan Ermilov 		HTONS(ip->ip_off);
816df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
817db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
8189c9137eaSGarrett Wollman 			if (ip->ip_vhl == IP_VHL_BORING) {
8199c9137eaSGarrett Wollman 				ip->ip_sum = in_cksum_hdr(ip);
8209c9137eaSGarrett Wollman 			} else {
821df8bae1dSRodney W. Grimes 				ip->ip_sum = in_cksum(m, hlen);
8229c9137eaSGarrett Wollman 			}
823db4f9cc7SJonathan Lemon 		}
8245da9f8faSJosef Karthauser 
8255da9f8faSJosef Karthauser 		/* Record statistics for this interface address. */
82638c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
8275da9f8faSJosef Karthauser 			ia->ia_ifa.if_opackets++;
8285da9f8faSJosef Karthauser 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
8295da9f8faSJosef Karthauser 		}
8305da9f8faSJosef Karthauser 
83133841545SHajimu UMEMOTO #ifdef IPSEC
83233841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
83333841545SHajimu UMEMOTO 		ipsec_delaux(m);
83433841545SHajimu UMEMOTO #endif
83533841545SHajimu UMEMOTO 
836df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
837df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
838df8bae1dSRodney W. Grimes 		goto done;
839df8bae1dSRodney W. Grimes 	}
840df8bae1dSRodney W. Grimes 	/*
841df8bae1dSRodney W. Grimes 	 * Too large for interface; fragment if possible.
842df8bae1dSRodney W. Grimes 	 * Must be able to put at least 8 bytes per fragment.
843df8bae1dSRodney W. Grimes 	 */
844df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
845df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
8463d1f141bSGarrett Wollman 		/*
8473d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
8483d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
8493d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
8503d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
8513d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
8523d1f141bSGarrett Wollman 		 */
8533d1f141bSGarrett Wollman 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
8543d1f141bSGarrett Wollman 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
8553d1f141bSGarrett Wollman 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
8563d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
8573d1f141bSGarrett Wollman 		}
858df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
859df8bae1dSRodney W. Grimes 		goto bad;
860df8bae1dSRodney W. Grimes 	}
861df8bae1dSRodney W. Grimes 	len = (ifp->if_mtu - hlen) &~ 7;
862df8bae1dSRodney W. Grimes 	if (len < 8) {
863df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
864df8bae1dSRodney W. Grimes 		goto bad;
865df8bae1dSRodney W. Grimes 	}
866df8bae1dSRodney W. Grimes 
867db4f9cc7SJonathan Lemon 	/*
868db4f9cc7SJonathan Lemon 	 * if the interface will not calculate checksums on
869db4f9cc7SJonathan Lemon 	 * fragmented packets, then do it here.
870db4f9cc7SJonathan Lemon 	 */
871db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
872db4f9cc7SJonathan Lemon 	    (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
873db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
874db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
875db4f9cc7SJonathan Lemon 	}
876db4f9cc7SJonathan Lemon 
877df8bae1dSRodney W. Grimes     {
878df8bae1dSRodney W. Grimes 	int mhlen, firstlen = len;
879df8bae1dSRodney W. Grimes 	struct mbuf **mnext = &m->m_nextpkt;
880db4f9cc7SJonathan Lemon 	int nfrags = 1;
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes 	/*
883df8bae1dSRodney W. Grimes 	 * Loop through length of segment after first fragment,
884df8bae1dSRodney W. Grimes 	 * make new header and copy data of each part and link onto chain.
885df8bae1dSRodney W. Grimes 	 */
886df8bae1dSRodney W. Grimes 	m0 = m;
887df8bae1dSRodney W. Grimes 	mhlen = sizeof (struct ip);
888df8bae1dSRodney W. Grimes 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
889df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
890df8bae1dSRodney W. Grimes 		if (m == 0) {
891df8bae1dSRodney W. Grimes 			error = ENOBUFS;
892df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
893df8bae1dSRodney W. Grimes 			goto sendorfree;
894df8bae1dSRodney W. Grimes 		}
895db4f9cc7SJonathan Lemon 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
896df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
897df8bae1dSRodney W. Grimes 		mhip = mtod(m, struct ip *);
898df8bae1dSRodney W. Grimes 		*mhip = *ip;
899df8bae1dSRodney W. Grimes 		if (hlen > sizeof (struct ip)) {
900df8bae1dSRodney W. Grimes 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
9019c9137eaSGarrett Wollman 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
902df8bae1dSRodney W. Grimes 		}
903df8bae1dSRodney W. Grimes 		m->m_len = mhlen;
904cc22c7a7SRuslan Ermilov 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
905df8bae1dSRodney W. Grimes 		if (off + len >= (u_short)ip->ip_len)
906df8bae1dSRodney W. Grimes 			len = (u_short)ip->ip_len - off;
907df8bae1dSRodney W. Grimes 		else
908df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
909df8bae1dSRodney W. Grimes 		mhip->ip_len = htons((u_short)(len + mhlen));
910df8bae1dSRodney W. Grimes 		m->m_next = m_copy(m0, off, len);
911df8bae1dSRodney W. Grimes 		if (m->m_next == 0) {
912df8bae1dSRodney W. Grimes 			(void) m_free(m);
913df8bae1dSRodney W. Grimes 			error = ENOBUFS;	/* ??? */
914df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
915df8bae1dSRodney W. Grimes 			goto sendorfree;
916df8bae1dSRodney W. Grimes 		}
917df8bae1dSRodney W. Grimes 		m->m_pkthdr.len = mhlen + len;
918df8bae1dSRodney W. Grimes 		m->m_pkthdr.rcvif = (struct ifnet *)0;
919db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
92004287599SRuslan Ermilov 		HTONS(mhip->ip_off);
921df8bae1dSRodney W. Grimes 		mhip->ip_sum = 0;
922db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
9239c9137eaSGarrett Wollman 			if (mhip->ip_vhl == IP_VHL_BORING) {
924e2184122SBruce Evans 				mhip->ip_sum = in_cksum_hdr(mhip);
9259c9137eaSGarrett Wollman 			} else {
926df8bae1dSRodney W. Grimes 				mhip->ip_sum = in_cksum(m, mhlen);
9279c9137eaSGarrett Wollman 			}
928db4f9cc7SJonathan Lemon 		}
929df8bae1dSRodney W. Grimes 		*mnext = m;
930df8bae1dSRodney W. Grimes 		mnext = &m->m_nextpkt;
931db4f9cc7SJonathan Lemon 		nfrags++;
932df8bae1dSRodney W. Grimes 	}
933db4f9cc7SJonathan Lemon 	ipstat.ips_ofragments += nfrags;
934db4f9cc7SJonathan Lemon 
935db4f9cc7SJonathan Lemon 	/* set first/last markers for fragment chain */
936db4f9cc7SJonathan Lemon 	m->m_flags |= M_LASTFRAG;
937db4f9cc7SJonathan Lemon 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
938db4f9cc7SJonathan Lemon 	m0->m_pkthdr.csum_data = nfrags;
939db4f9cc7SJonathan Lemon 
940df8bae1dSRodney W. Grimes 	/*
941df8bae1dSRodney W. Grimes 	 * Update first fragment by trimming what's been copied out
942df8bae1dSRodney W. Grimes 	 * and updating header, then send each fragment (in order).
943df8bae1dSRodney W. Grimes 	 */
944df8bae1dSRodney W. Grimes 	m = m0;
945df8bae1dSRodney W. Grimes 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
946df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hlen + firstlen;
947df8bae1dSRodney W. Grimes 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
94804287599SRuslan Ermilov 	ip->ip_off |= IP_MF;
94904287599SRuslan Ermilov 	HTONS(ip->ip_off);
950df8bae1dSRodney W. Grimes 	ip->ip_sum = 0;
951db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_IP) {
9529c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
9539c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
9549c9137eaSGarrett Wollman 		} else {
955df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
9569c9137eaSGarrett Wollman 		}
957db4f9cc7SJonathan Lemon 	}
958df8bae1dSRodney W. Grimes sendorfree:
959df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m0) {
960df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
961df8bae1dSRodney W. Grimes 		m->m_nextpkt = 0;
96233841545SHajimu UMEMOTO #ifdef IPSEC
96333841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
96433841545SHajimu UMEMOTO 		ipsec_delaux(m);
96533841545SHajimu UMEMOTO #endif
96607203494SDaniel C. Sobral 		if (error == 0) {
967fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
96807203494SDaniel C. Sobral 			if (ia != NULL) {
969fe937674SJosef Karthauser 				ia->ia_ifa.if_opackets++;
970fe937674SJosef Karthauser 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
97107203494SDaniel C. Sobral 			}
972fe937674SJosef Karthauser 
973df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
974df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
975fe937674SJosef Karthauser 		} else
976df8bae1dSRodney W. Grimes 			m_freem(m);
977df8bae1dSRodney W. Grimes 	}
978df8bae1dSRodney W. Grimes 
979df8bae1dSRodney W. Grimes 	if (error == 0)
980df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
981df8bae1dSRodney W. Grimes     }
982df8bae1dSRodney W. Grimes done:
9836a800098SYoshinobu Inoue #ifdef IPSEC
9846a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
9856a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
9866a800098SYoshinobu Inoue 		ro->ro_rt = NULL;
9876a800098SYoshinobu Inoue 	}
9886a800098SYoshinobu Inoue 	if (sp != NULL) {
9896a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
9906a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
9916a800098SYoshinobu Inoue 		key_freesp(sp);
9926a800098SYoshinobu Inoue 	}
9936a800098SYoshinobu Inoue #endif /* IPSEC */
994df8bae1dSRodney W. Grimes 	return (error);
995df8bae1dSRodney W. Grimes bad:
9963528d68fSBill Paul 	m_freem(m);
997df8bae1dSRodney W. Grimes 	goto done;
998df8bae1dSRodney W. Grimes }
999df8bae1dSRodney W. Grimes 
10001c238475SJonathan Lemon void
1001db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
1002db4f9cc7SJonathan Lemon {
1003db4f9cc7SJonathan Lemon 	struct ip *ip;
1004db4f9cc7SJonathan Lemon 	u_short csum, offset;
1005db4f9cc7SJonathan Lemon 
1006db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
1007db4f9cc7SJonathan Lemon 	offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1008db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
1009206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1010206a3274SRuslan Ermilov 		csum = 0xffff;
1011db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1012db4f9cc7SJonathan Lemon 
1013db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
1014db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1015db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
1016db4f9cc7SJonathan Lemon 		/*
1017db4f9cc7SJonathan Lemon 		 * XXX
1018db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
1019db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
1020db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
1021db4f9cc7SJonathan Lemon 		 */
1022db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
1023db4f9cc7SJonathan Lemon 	}
1024db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
1025db4f9cc7SJonathan Lemon }
1026db4f9cc7SJonathan Lemon 
1027df8bae1dSRodney W. Grimes /*
1028df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
1029df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
1030df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
1031072b9b24SPaul Traina  *
1032072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
1033df8bae1dSRodney W. Grimes  */
1034df8bae1dSRodney W. Grimes static struct mbuf *
1035df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen)
1036df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1037df8bae1dSRodney W. Grimes 	struct mbuf *opt;
1038df8bae1dSRodney W. Grimes 	int *phlen;
1039df8bae1dSRodney W. Grimes {
1040df8bae1dSRodney W. Grimes 	register struct ipoption *p = mtod(opt, struct ipoption *);
1041df8bae1dSRodney W. Grimes 	struct mbuf *n;
1042df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
1043df8bae1dSRodney W. Grimes 	unsigned optlen;
1044df8bae1dSRodney W. Grimes 
1045df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
1046df8bae1dSRodney W. Grimes 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
1047df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1048df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1049df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1050df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1051df8bae1dSRodney W. Grimes 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1052df8bae1dSRodney W. Grimes 		if (n == 0)
1053df8bae1dSRodney W. Grimes 			return (m);
10545db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
1055df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1056df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1057df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1058df8bae1dSRodney W. Grimes 		n->m_next = m;
1059df8bae1dSRodney W. Grimes 		m = n;
1060df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1061df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
106294a5d9b6SDavid Greenman 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1063df8bae1dSRodney W. Grimes 	} else {
1064df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1065df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1066df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1067df8bae1dSRodney W. Grimes 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1068df8bae1dSRodney W. Grimes 	}
1069df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
10700453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1071df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
10729c9137eaSGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1073df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1074df8bae1dSRodney W. Grimes 	return (m);
1075df8bae1dSRodney W. Grimes }
1076df8bae1dSRodney W. Grimes 
1077df8bae1dSRodney W. Grimes /*
1078df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1079df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1080df8bae1dSRodney W. Grimes  */
1081beec8214SDarren Reed int
1082df8bae1dSRodney W. Grimes ip_optcopy(ip, jp)
1083df8bae1dSRodney W. Grimes 	struct ip *ip, *jp;
1084df8bae1dSRodney W. Grimes {
1085df8bae1dSRodney W. Grimes 	register u_char *cp, *dp;
1086df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1087df8bae1dSRodney W. Grimes 
1088df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1089df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
10909c9137eaSGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1091df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1092df8bae1dSRodney W. Grimes 		opt = cp[0];
1093df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1094df8bae1dSRodney W. Grimes 			break;
1095df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1096df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1097df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1098df8bae1dSRodney W. Grimes 			optlen = 1;
1099df8bae1dSRodney W. Grimes 			continue;
1100686cdd19SJun-ichiro itojun Hagino 		}
1101686cdd19SJun-ichiro itojun Hagino #ifdef DIAGNOSTIC
1102686cdd19SJun-ichiro itojun Hagino 		if (cnt < IPOPT_OLEN + sizeof(*cp))
1103686cdd19SJun-ichiro itojun Hagino 			panic("malformed IPv4 option passed to ip_optcopy");
1104686cdd19SJun-ichiro itojun Hagino #endif
1105df8bae1dSRodney W. Grimes 		optlen = cp[IPOPT_OLEN];
1106686cdd19SJun-ichiro itojun Hagino #ifdef DIAGNOSTIC
1107686cdd19SJun-ichiro itojun Hagino 		if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1108686cdd19SJun-ichiro itojun Hagino 			panic("malformed IPv4 option passed to ip_optcopy");
1109686cdd19SJun-ichiro itojun Hagino #endif
1110df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1111df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1112df8bae1dSRodney W. Grimes 			optlen = cnt;
1113df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
11140453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1115df8bae1dSRodney W. Grimes 			dp += optlen;
1116df8bae1dSRodney W. Grimes 		}
1117df8bae1dSRodney W. Grimes 	}
1118df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1119df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1120df8bae1dSRodney W. Grimes 	return (optlen);
1121df8bae1dSRodney W. Grimes }
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes /*
1124df8bae1dSRodney W. Grimes  * IP socket option processing.
1125df8bae1dSRodney W. Grimes  */
1126df8bae1dSRodney W. Grimes int
1127cfe8b629SGarrett Wollman ip_ctloutput(so, sopt)
1128df8bae1dSRodney W. Grimes 	struct socket *so;
1129cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1130df8bae1dSRodney W. Grimes {
1131cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1132cfe8b629SGarrett Wollman 	int	error, optval;
1133df8bae1dSRodney W. Grimes 
1134cfe8b629SGarrett Wollman 	error = optval = 0;
1135cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1136cfe8b629SGarrett Wollman 		return (EINVAL);
1137cfe8b629SGarrett Wollman 	}
1138df8bae1dSRodney W. Grimes 
1139cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1140cfe8b629SGarrett Wollman 	case SOPT_SET:
1141cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1142df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1143df8bae1dSRodney W. Grimes #ifdef notyet
1144df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1145df8bae1dSRodney W. Grimes #endif
1146cfe8b629SGarrett Wollman 		{
1147cfe8b629SGarrett Wollman 			struct mbuf *m;
1148cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1149cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1150cfe8b629SGarrett Wollman 				break;
1151cfe8b629SGarrett Wollman 			}
1152b40ce416SJulian Elischer 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1153cfe8b629SGarrett Wollman 			if (m == 0) {
1154cfe8b629SGarrett Wollman 				error = ENOBUFS;
1155cfe8b629SGarrett Wollman 				break;
1156cfe8b629SGarrett Wollman 			}
1157cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1158cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1159cfe8b629SGarrett Wollman 					    m->m_len);
1160cfe8b629SGarrett Wollman 
1161cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1162cfe8b629SGarrett Wollman 					   m));
1163cfe8b629SGarrett Wollman 		}
1164df8bae1dSRodney W. Grimes 
1165df8bae1dSRodney W. Grimes 		case IP_TOS:
1166df8bae1dSRodney W. Grimes 		case IP_TTL:
1167df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1168df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1169df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
117082c23ebaSBill Fenner 		case IP_RECVIF:
11716a800098SYoshinobu Inoue 		case IP_FAITH:
1172cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1173cfe8b629SGarrett Wollman 					    sizeof optval);
1174cfe8b629SGarrett Wollman 			if (error)
1175cfe8b629SGarrett Wollman 				break;
1176df8bae1dSRodney W. Grimes 
1177cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1178df8bae1dSRodney W. Grimes 			case IP_TOS:
1179ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1180df8bae1dSRodney W. Grimes 				break;
1181df8bae1dSRodney W. Grimes 
1182df8bae1dSRodney W. Grimes 			case IP_TTL:
1183ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1184df8bae1dSRodney W. Grimes 				break;
1185df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
1186df8bae1dSRodney W. Grimes 	if (optval) \
1187df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
1188df8bae1dSRodney W. Grimes 	else \
1189df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1192df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1193df8bae1dSRodney W. Grimes 				break;
1194df8bae1dSRodney W. Grimes 
1195df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1196df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1197df8bae1dSRodney W. Grimes 				break;
1198df8bae1dSRodney W. Grimes 
1199df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1200df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1201df8bae1dSRodney W. Grimes 				break;
120282c23ebaSBill Fenner 
120382c23ebaSBill Fenner 			case IP_RECVIF:
120482c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
120582c23ebaSBill Fenner 				break;
12066a800098SYoshinobu Inoue 
12076a800098SYoshinobu Inoue 			case IP_FAITH:
12086a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
12096a800098SYoshinobu Inoue 				break;
1210df8bae1dSRodney W. Grimes 			}
1211df8bae1dSRodney W. Grimes 			break;
1212df8bae1dSRodney W. Grimes #undef OPTSET
1213df8bae1dSRodney W. Grimes 
1214df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1215f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1216df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1217df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1218df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1219df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1220cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1221df8bae1dSRodney W. Grimes 			break;
1222df8bae1dSRodney W. Grimes 
122333b3ac06SPeter Wemm 		case IP_PORTRANGE:
1224cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1225cfe8b629SGarrett Wollman 					    sizeof optval);
1226cfe8b629SGarrett Wollman 			if (error)
1227cfe8b629SGarrett Wollman 				break;
122833b3ac06SPeter Wemm 
122933b3ac06SPeter Wemm 			switch (optval) {
123033b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
123133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
123233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
123333b3ac06SPeter Wemm 				break;
123433b3ac06SPeter Wemm 
123533b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
123633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
123733b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
123833b3ac06SPeter Wemm 				break;
123933b3ac06SPeter Wemm 
124033b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
124133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
124233b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
124333b3ac06SPeter Wemm 				break;
124433b3ac06SPeter Wemm 
124533b3ac06SPeter Wemm 			default:
124633b3ac06SPeter Wemm 				error = EINVAL;
124733b3ac06SPeter Wemm 				break;
124833b3ac06SPeter Wemm 			}
1249ce8c72b1SPeter Wemm 			break;
125033b3ac06SPeter Wemm 
12516a800098SYoshinobu Inoue #ifdef IPSEC
12526a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12536a800098SYoshinobu Inoue 		{
12546a800098SYoshinobu Inoue 			caddr_t req;
1255686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
12566a800098SYoshinobu Inoue 			int priv;
12576a800098SYoshinobu Inoue 			struct mbuf *m;
12586a800098SYoshinobu Inoue 			int optname;
12596a800098SYoshinobu Inoue 
12606a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
12616a800098SYoshinobu Inoue 				break;
12626a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12636a800098SYoshinobu Inoue 				break;
1264b40ce416SJulian Elischer 			priv = (sopt->sopt_td != NULL &&
1265b40ce416SJulian Elischer 				suser_td(sopt->sopt_td) != 0) ? 0 : 1;
12666a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1267686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
12686a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1269686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
12706a800098SYoshinobu Inoue 			m_freem(m);
12716a800098SYoshinobu Inoue 			break;
12726a800098SYoshinobu Inoue 		}
12736a800098SYoshinobu Inoue #endif /*IPSEC*/
12746a800098SYoshinobu Inoue 
1275df8bae1dSRodney W. Grimes 		default:
1276df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1277df8bae1dSRodney W. Grimes 			break;
1278df8bae1dSRodney W. Grimes 		}
1279df8bae1dSRodney W. Grimes 		break;
1280df8bae1dSRodney W. Grimes 
1281cfe8b629SGarrett Wollman 	case SOPT_GET:
1282cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1283df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1284df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1285cfe8b629SGarrett Wollman 			if (inp->inp_options)
1286cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1287cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1288cfe8b629SGarrett Wollman 							  char *),
1289cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1290cfe8b629SGarrett Wollman 			else
1291cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1292df8bae1dSRodney W. Grimes 			break;
1293df8bae1dSRodney W. Grimes 
1294df8bae1dSRodney W. Grimes 		case IP_TOS:
1295df8bae1dSRodney W. Grimes 		case IP_TTL:
1296df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1297df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1298df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
129982c23ebaSBill Fenner 		case IP_RECVIF:
1300cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
13016a800098SYoshinobu Inoue 		case IP_FAITH:
1302cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1303df8bae1dSRodney W. Grimes 
1304df8bae1dSRodney W. Grimes 			case IP_TOS:
1305ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1306df8bae1dSRodney W. Grimes 				break;
1307df8bae1dSRodney W. Grimes 
1308df8bae1dSRodney W. Grimes 			case IP_TTL:
1309ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1310df8bae1dSRodney W. Grimes 				break;
1311df8bae1dSRodney W. Grimes 
1312df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1313df8bae1dSRodney W. Grimes 
1314df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1315df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1316df8bae1dSRodney W. Grimes 				break;
1317df8bae1dSRodney W. Grimes 
1318df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1319df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1320df8bae1dSRodney W. Grimes 				break;
1321df8bae1dSRodney W. Grimes 
1322df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1323df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1324df8bae1dSRodney W. Grimes 				break;
132582c23ebaSBill Fenner 
132682c23ebaSBill Fenner 			case IP_RECVIF:
132782c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
132882c23ebaSBill Fenner 				break;
1329cfe8b629SGarrett Wollman 
1330cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1331cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1332cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1333cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1334cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1335cfe8b629SGarrett Wollman 				else
1336cfe8b629SGarrett Wollman 					optval = 0;
1337cfe8b629SGarrett Wollman 				break;
13386a800098SYoshinobu Inoue 
13396a800098SYoshinobu Inoue 			case IP_FAITH:
13406a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
13416a800098SYoshinobu Inoue 				break;
1342df8bae1dSRodney W. Grimes 			}
1343cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1344df8bae1dSRodney W. Grimes 			break;
1345df8bae1dSRodney W. Grimes 
1346df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1347f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1348df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1349df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1350df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1351df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1352cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
135333b3ac06SPeter Wemm 			break;
135433b3ac06SPeter Wemm 
13556a800098SYoshinobu Inoue #ifdef IPSEC
13566a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13576a800098SYoshinobu Inoue 		{
1358f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
13596a800098SYoshinobu Inoue 			caddr_t req = NULL;
1360686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
13616a800098SYoshinobu Inoue 
1362686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
13636a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1364686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1365686cdd19SJun-ichiro itojun Hagino 			}
1366686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
13676a800098SYoshinobu Inoue 			if (error == 0)
13686a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1369f63e7634SYoshinobu Inoue 			if (error == 0)
13706a800098SYoshinobu Inoue 				m_freem(m);
13716a800098SYoshinobu Inoue 			break;
13726a800098SYoshinobu Inoue 		}
13736a800098SYoshinobu Inoue #endif /*IPSEC*/
13746a800098SYoshinobu Inoue 
1375df8bae1dSRodney W. Grimes 		default:
1376df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1377df8bae1dSRodney W. Grimes 			break;
1378df8bae1dSRodney W. Grimes 		}
1379df8bae1dSRodney W. Grimes 		break;
1380df8bae1dSRodney W. Grimes 	}
1381df8bae1dSRodney W. Grimes 	return (error);
1382df8bae1dSRodney W. Grimes }
1383df8bae1dSRodney W. Grimes 
1384df8bae1dSRodney W. Grimes /*
1385df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1386df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1387df8bae1dSRodney W. Grimes  * with destination address if source routed.
1388df8bae1dSRodney W. Grimes  */
13890312fbe9SPoul-Henning Kamp static int
1390df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m)
1391df8bae1dSRodney W. Grimes 	int optname;
1392df8bae1dSRodney W. Grimes 	struct mbuf **pcbopt;
1393df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1394df8bae1dSRodney W. Grimes {
1395d68fa50cSBruce Evans 	register int cnt, optlen;
1396df8bae1dSRodney W. Grimes 	register u_char *cp;
1397df8bae1dSRodney W. Grimes 	u_char opt;
1398df8bae1dSRodney W. Grimes 
1399df8bae1dSRodney W. Grimes 	/* turn off any old options */
1400df8bae1dSRodney W. Grimes 	if (*pcbopt)
1401df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1402df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1403df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1404df8bae1dSRodney W. Grimes 		/*
1405df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1406df8bae1dSRodney W. Grimes 		 */
1407df8bae1dSRodney W. Grimes 		if (m)
1408df8bae1dSRodney W. Grimes 			(void)m_free(m);
1409df8bae1dSRodney W. Grimes 		return (0);
1410df8bae1dSRodney W. Grimes 	}
1411df8bae1dSRodney W. Grimes 
14120c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1413df8bae1dSRodney W. Grimes 		goto bad;
1414df8bae1dSRodney W. Grimes 	/*
1415df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1416df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1417df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1418df8bae1dSRodney W. Grimes 	 */
1419df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1420df8bae1dSRodney W. Grimes 		goto bad;
1421df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1422df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1423df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1424df8bae1dSRodney W. Grimes 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1425df8bae1dSRodney W. Grimes 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1426df8bae1dSRodney W. Grimes 
1427df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1428df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1429df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1430df8bae1dSRodney W. Grimes 			break;
1431df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1432df8bae1dSRodney W. Grimes 			optlen = 1;
1433df8bae1dSRodney W. Grimes 		else {
1434707d00a3SJonathan Lemon 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1435707d00a3SJonathan Lemon 				goto bad;
1436df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1437707d00a3SJonathan Lemon 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1438df8bae1dSRodney W. Grimes 				goto bad;
1439df8bae1dSRodney W. Grimes 		}
1440df8bae1dSRodney W. Grimes 		switch (opt) {
1441df8bae1dSRodney W. Grimes 
1442df8bae1dSRodney W. Grimes 		default:
1443df8bae1dSRodney W. Grimes 			break;
1444df8bae1dSRodney W. Grimes 
1445df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1446df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1447df8bae1dSRodney W. Grimes 			/*
1448df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1449df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1450df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1451df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1452df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1453df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1454df8bae1dSRodney W. Grimes 			 */
1455df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1456df8bae1dSRodney W. Grimes 				goto bad;
1457df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1458df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1459df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1460df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1461df8bae1dSRodney W. Grimes 			/*
1462df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1463df8bae1dSRodney W. Grimes 			 */
1464df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1465df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1466df8bae1dSRodney W. Grimes 			/*
1467df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1468df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1469df8bae1dSRodney W. Grimes 			 */
1470df8bae1dSRodney W. Grimes 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1471df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr)),
1472df8bae1dSRodney W. Grimes 			    (caddr_t)&cp[IPOPT_OFFSET+1],
1473df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
1474df8bae1dSRodney W. Grimes 			break;
1475df8bae1dSRodney W. Grimes 		}
1476df8bae1dSRodney W. Grimes 	}
1477df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1478df8bae1dSRodney W. Grimes 		goto bad;
1479df8bae1dSRodney W. Grimes 	*pcbopt = m;
1480df8bae1dSRodney W. Grimes 	return (0);
1481df8bae1dSRodney W. Grimes 
1482df8bae1dSRodney W. Grimes bad:
1483df8bae1dSRodney W. Grimes 	(void)m_free(m);
1484df8bae1dSRodney W. Grimes 	return (EINVAL);
1485df8bae1dSRodney W. Grimes }
1486df8bae1dSRodney W. Grimes 
1487df8bae1dSRodney W. Grimes /*
1488cfe8b629SGarrett Wollman  * XXX
1489cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1490cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1491cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1492cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1493cfe8b629SGarrett Wollman  */
149433841545SHajimu UMEMOTO 
149533841545SHajimu UMEMOTO /*
149633841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
149733841545SHajimu UMEMOTO  */
149833841545SHajimu UMEMOTO static struct ifnet *
149933841545SHajimu UMEMOTO ip_multicast_if(a, ifindexp)
150033841545SHajimu UMEMOTO 	struct in_addr *a;
150133841545SHajimu UMEMOTO 	int *ifindexp;
150233841545SHajimu UMEMOTO {
150333841545SHajimu UMEMOTO 	int ifindex;
150433841545SHajimu UMEMOTO 	struct ifnet *ifp;
150533841545SHajimu UMEMOTO 
150633841545SHajimu UMEMOTO 	if (ifindexp)
150733841545SHajimu UMEMOTO 		*ifindexp = 0;
150833841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
150933841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
151033841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
151133841545SHajimu UMEMOTO 			return NULL;
1512f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
151333841545SHajimu UMEMOTO 		if (ifindexp)
151433841545SHajimu UMEMOTO 			*ifindexp = ifindex;
151533841545SHajimu UMEMOTO 	} else {
151633841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
151733841545SHajimu UMEMOTO 	}
151833841545SHajimu UMEMOTO 	return ifp;
151933841545SHajimu UMEMOTO }
152033841545SHajimu UMEMOTO 
1521cfe8b629SGarrett Wollman /*
1522df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1523df8bae1dSRodney W. Grimes  */
15240312fbe9SPoul-Henning Kamp static int
1525cfe8b629SGarrett Wollman ip_setmoptions(sopt, imop)
1526cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1527df8bae1dSRodney W. Grimes 	struct ip_moptions **imop;
1528df8bae1dSRodney W. Grimes {
1529cfe8b629SGarrett Wollman 	int error = 0;
1530cfe8b629SGarrett Wollman 	int i;
1531df8bae1dSRodney W. Grimes 	struct in_addr addr;
1532cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1533cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1534cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1535df8bae1dSRodney W. Grimes 	struct route ro;
1536cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
153733841545SHajimu UMEMOTO 	int ifindex;
15389b626c29SGarrett Wollman 	int s;
1539df8bae1dSRodney W. Grimes 
1540df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1541df8bae1dSRodney W. Grimes 		/*
1542df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1543df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1544df8bae1dSRodney W. Grimes 		 */
1545df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1546df8bae1dSRodney W. Grimes 		    M_WAITOK);
1547df8bae1dSRodney W. Grimes 
1548df8bae1dSRodney W. Grimes 		if (imo == NULL)
1549df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1550df8bae1dSRodney W. Grimes 		*imop = imo;
1551df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
155233841545SHajimu UMEMOTO 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
15531c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1554df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1555df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1556df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1557df8bae1dSRodney W. Grimes 	}
1558df8bae1dSRodney W. Grimes 
1559cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1560f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1561f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1562cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
15635e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
15645e9ae478SGarrett Wollman 			break;
15655e9ae478SGarrett Wollman 		}
1566cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1567cfe8b629SGarrett Wollman 		if (error)
1568f0068c4aSGarrett Wollman 			break;
15691c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1570f0068c4aSGarrett Wollman 			error = EINVAL;
1571f0068c4aSGarrett Wollman 			break;
1572f0068c4aSGarrett Wollman 		}
1573f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1574f0068c4aSGarrett Wollman 		break;
1575f0068c4aSGarrett Wollman 
1576df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1577df8bae1dSRodney W. Grimes 		/*
1578df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1579df8bae1dSRodney W. Grimes 		 */
1580cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1581cfe8b629SGarrett Wollman 		if (error)
1582df8bae1dSRodney W. Grimes 			break;
1583df8bae1dSRodney W. Grimes 		/*
1584df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1585df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1586df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1587df8bae1dSRodney W. Grimes 		 */
1588df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1589df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1590df8bae1dSRodney W. Grimes 			break;
1591df8bae1dSRodney W. Grimes 		}
1592df8bae1dSRodney W. Grimes 		/*
1593df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1594df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1595df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1596df8bae1dSRodney W. Grimes 		 */
159720e8807cSGarrett Wollman 		s = splimp();
159833841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1599df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1600fbc6ab00SBill Fenner 			splx(s);
1601df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1602df8bae1dSRodney W. Grimes 			break;
1603df8bae1dSRodney W. Grimes 		}
1604df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
160533841545SHajimu UMEMOTO 		if (ifindex)
160633841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
160733841545SHajimu UMEMOTO 		else
160833841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
16099b626c29SGarrett Wollman 		splx(s);
1610df8bae1dSRodney W. Grimes 		break;
1611df8bae1dSRodney W. Grimes 
1612df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1613df8bae1dSRodney W. Grimes 		/*
1614df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1615cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1616cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1617cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1618df8bae1dSRodney W. Grimes 		 */
1619cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1620cfe8b629SGarrett Wollman 			u_char ttl;
1621cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1622cfe8b629SGarrett Wollman 			if (error)
1623df8bae1dSRodney W. Grimes 				break;
1624cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1625cfe8b629SGarrett Wollman 		} else {
1626cfe8b629SGarrett Wollman 			u_int ttl;
1627cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1628cfe8b629SGarrett Wollman 					    sizeof ttl);
1629cfe8b629SGarrett Wollman 			if (error)
1630cfe8b629SGarrett Wollman 				break;
1631cfe8b629SGarrett Wollman 			if (ttl > 255)
1632cfe8b629SGarrett Wollman 				error = EINVAL;
1633cfe8b629SGarrett Wollman 			else
1634cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1635df8bae1dSRodney W. Grimes 		}
1636df8bae1dSRodney W. Grimes 		break;
1637df8bae1dSRodney W. Grimes 
1638df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1639df8bae1dSRodney W. Grimes 		/*
1640df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1641cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1642cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1643cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1644df8bae1dSRodney W. Grimes 		 */
1645cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1646cfe8b629SGarrett Wollman 			u_char loop;
1647cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1648cfe8b629SGarrett Wollman 			if (error)
1649df8bae1dSRodney W. Grimes 				break;
1650cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1651cfe8b629SGarrett Wollman 		} else {
1652cfe8b629SGarrett Wollman 			u_int loop;
1653cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1654cfe8b629SGarrett Wollman 					    sizeof loop);
1655cfe8b629SGarrett Wollman 			if (error)
1656cfe8b629SGarrett Wollman 				break;
1657cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1658df8bae1dSRodney W. Grimes 		}
1659df8bae1dSRodney W. Grimes 		break;
1660df8bae1dSRodney W. Grimes 
1661df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1662df8bae1dSRodney W. Grimes 		/*
1663df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1664df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1665df8bae1dSRodney W. Grimes 		 */
1666cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1667cfe8b629SGarrett Wollman 		if (error)
1668df8bae1dSRodney W. Grimes 			break;
1669cfe8b629SGarrett Wollman 
1670cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1671df8bae1dSRodney W. Grimes 			error = EINVAL;
1672df8bae1dSRodney W. Grimes 			break;
1673df8bae1dSRodney W. Grimes 		}
167420e8807cSGarrett Wollman 		s = splimp();
1675df8bae1dSRodney W. Grimes 		/*
1676df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1677df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1678df8bae1dSRodney W. Grimes 		 */
1679cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
16801c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1681df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1682df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1683df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1684cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
1685df8bae1dSRodney W. Grimes 			rtalloc(&ro);
1686df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1687df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
16889b626c29SGarrett Wollman 				splx(s);
1689df8bae1dSRodney W. Grimes 				break;
1690df8bae1dSRodney W. Grimes 			}
1691df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1692df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
1693df8bae1dSRodney W. Grimes 		}
1694df8bae1dSRodney W. Grimes 		else {
169533841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1696df8bae1dSRodney W. Grimes 		}
16979b626c29SGarrett Wollman 
1698df8bae1dSRodney W. Grimes 		/*
1699df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1700df8bae1dSRodney W. Grimes 		 * supports multicast.
1701df8bae1dSRodney W. Grimes 		 */
1702df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1703df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
17049b626c29SGarrett Wollman 			splx(s);
1705df8bae1dSRodney W. Grimes 			break;
1706df8bae1dSRodney W. Grimes 		}
1707df8bae1dSRodney W. Grimes 		/*
1708df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1709df8bae1dSRodney W. Grimes 		 * membership slots are full.
1710df8bae1dSRodney W. Grimes 		 */
1711df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1712df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1713df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1714cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1715df8bae1dSRodney W. Grimes 				break;
1716df8bae1dSRodney W. Grimes 		}
1717df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1718df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
17199b626c29SGarrett Wollman 			splx(s);
1720df8bae1dSRodney W. Grimes 			break;
1721df8bae1dSRodney W. Grimes 		}
1722df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1723df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
17249b626c29SGarrett Wollman 			splx(s);
1725df8bae1dSRodney W. Grimes 			break;
1726df8bae1dSRodney W. Grimes 		}
1727df8bae1dSRodney W. Grimes 		/*
1728df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1729df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1730df8bae1dSRodney W. Grimes 		 */
1731df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1732cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1733df8bae1dSRodney W. Grimes 			error = ENOBUFS;
17349b626c29SGarrett Wollman 			splx(s);
1735df8bae1dSRodney W. Grimes 			break;
1736df8bae1dSRodney W. Grimes 		}
1737df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
17389b626c29SGarrett Wollman 		splx(s);
1739df8bae1dSRodney W. Grimes 		break;
1740df8bae1dSRodney W. Grimes 
1741df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1742df8bae1dSRodney W. Grimes 		/*
1743df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1744df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1745df8bae1dSRodney W. Grimes 		 */
1746cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1747cfe8b629SGarrett Wollman 		if (error)
1748df8bae1dSRodney W. Grimes 			break;
1749cfe8b629SGarrett Wollman 
1750cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1751df8bae1dSRodney W. Grimes 			error = EINVAL;
1752df8bae1dSRodney W. Grimes 			break;
1753df8bae1dSRodney W. Grimes 		}
17549b626c29SGarrett Wollman 
175520e8807cSGarrett Wollman 		s = splimp();
1756df8bae1dSRodney W. Grimes 		/*
1757df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1758df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1759df8bae1dSRodney W. Grimes 		 */
1760cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1761df8bae1dSRodney W. Grimes 			ifp = NULL;
1762df8bae1dSRodney W. Grimes 		else {
176333841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1764df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1765df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
17669b626c29SGarrett Wollman 				splx(s);
1767df8bae1dSRodney W. Grimes 				break;
1768df8bae1dSRodney W. Grimes 			}
1769df8bae1dSRodney W. Grimes 		}
1770df8bae1dSRodney W. Grimes 		/*
1771df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1772df8bae1dSRodney W. Grimes 		 */
1773df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1774df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1775df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1776df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1777cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1778df8bae1dSRodney W. Grimes 				break;
1779df8bae1dSRodney W. Grimes 		}
1780df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1781df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
17829b626c29SGarrett Wollman 			splx(s);
1783df8bae1dSRodney W. Grimes 			break;
1784df8bae1dSRodney W. Grimes 		}
1785df8bae1dSRodney W. Grimes 		/*
1786df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1787df8bae1dSRodney W. Grimes 		 * membership points.
1788df8bae1dSRodney W. Grimes 		 */
1789df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1790df8bae1dSRodney W. Grimes 		/*
1791df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1792df8bae1dSRodney W. Grimes 		 */
1793df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1794df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1795df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
17969b626c29SGarrett Wollman 		splx(s);
1797df8bae1dSRodney W. Grimes 		break;
1798df8bae1dSRodney W. Grimes 
1799df8bae1dSRodney W. Grimes 	default:
1800df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1801df8bae1dSRodney W. Grimes 		break;
1802df8bae1dSRodney W. Grimes 	}
1803df8bae1dSRodney W. Grimes 
1804df8bae1dSRodney W. Grimes 	/*
1805df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1806df8bae1dSRodney W. Grimes 	 */
1807df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
18081c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1809df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1810df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1811df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1812df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1813df8bae1dSRodney W. Grimes 		*imop = NULL;
1814df8bae1dSRodney W. Grimes 	}
1815df8bae1dSRodney W. Grimes 
1816df8bae1dSRodney W. Grimes 	return (error);
1817df8bae1dSRodney W. Grimes }
1818df8bae1dSRodney W. Grimes 
1819df8bae1dSRodney W. Grimes /*
1820df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1821df8bae1dSRodney W. Grimes  */
18220312fbe9SPoul-Henning Kamp static int
1823cfe8b629SGarrett Wollman ip_getmoptions(sopt, imo)
1824cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1825df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1826df8bae1dSRodney W. Grimes {
1827cfe8b629SGarrett Wollman 	struct in_addr addr;
1828df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1829cfe8b629SGarrett Wollman 	int error, optval;
1830cfe8b629SGarrett Wollman 	u_char coptval;
1831df8bae1dSRodney W. Grimes 
1832cfe8b629SGarrett Wollman 	error = 0;
1833cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1834f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1835f0068c4aSGarrett Wollman 		if (imo != NULL)
1836cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1837f0068c4aSGarrett Wollman 		else
1838cfe8b629SGarrett Wollman 			optval = -1;
1839cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1840cfe8b629SGarrett Wollman 		break;
1841f0068c4aSGarrett Wollman 
1842df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1843df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1844cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
184533841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
184633841545SHajimu UMEMOTO 			/* return the value user has set */
184733841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
184833841545SHajimu UMEMOTO 		} else {
1849df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1850cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1851df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1852df8bae1dSRodney W. Grimes 		}
1853cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1854cfe8b629SGarrett Wollman 		break;
1855df8bae1dSRodney W. Grimes 
1856df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1857cfe8b629SGarrett Wollman 		if (imo == 0)
1858cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1859cfe8b629SGarrett Wollman 		else
1860cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
1861cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1862cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1863cfe8b629SGarrett Wollman 		else
1864cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1865cfe8b629SGarrett Wollman 		break;
1866df8bae1dSRodney W. Grimes 
1867df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1868cfe8b629SGarrett Wollman 		if (imo == 0)
1869cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1870cfe8b629SGarrett Wollman 		else
1871cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
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 	default:
1879cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1880cfe8b629SGarrett Wollman 		break;
1881df8bae1dSRodney W. Grimes 	}
1882cfe8b629SGarrett Wollman 	return (error);
1883df8bae1dSRodney W. Grimes }
1884df8bae1dSRodney W. Grimes 
1885df8bae1dSRodney W. Grimes /*
1886df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1887df8bae1dSRodney W. Grimes  */
1888df8bae1dSRodney W. Grimes void
1889df8bae1dSRodney W. Grimes ip_freemoptions(imo)
1890df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1891df8bae1dSRodney W. Grimes {
1892df8bae1dSRodney W. Grimes 	register int i;
1893df8bae1dSRodney W. Grimes 
1894df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1895df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1896df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1897df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1898df8bae1dSRodney W. Grimes 	}
1899df8bae1dSRodney W. Grimes }
1900df8bae1dSRodney W. Grimes 
1901df8bae1dSRodney W. Grimes /*
1902df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1903df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1904df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1905f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1906f5fea3ddSPaul Traina  * replicating that code here.
1907df8bae1dSRodney W. Grimes  */
1908df8bae1dSRodney W. Grimes static void
190986b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen)
1910df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1911df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1912df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
191386b1d6d2SBill Fenner 	int hlen;
1914df8bae1dSRodney W. Grimes {
1915df8bae1dSRodney W. Grimes 	register struct ip *ip;
1916df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1917df8bae1dSRodney W. Grimes 
1918df8bae1dSRodney W. Grimes 	copym = m_copy(m, 0, M_COPYALL);
191986b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
192086b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1921df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1922df8bae1dSRodney W. Grimes 		/*
1923df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1924df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1925df8bae1dSRodney W. Grimes 		 */
1926df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
192704287599SRuslan Ermilov 		HTONS(ip->ip_len);
192804287599SRuslan Ermilov 		HTONS(ip->ip_off);
1929df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
19309c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
19319c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
19329c9137eaSGarrett Wollman 		} else {
193386b1d6d2SBill Fenner 			ip->ip_sum = in_cksum(copym, hlen);
19349c9137eaSGarrett Wollman 		}
19359c9137eaSGarrett Wollman 		/*
19369c9137eaSGarrett Wollman 		 * NB:
1937e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1938e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1939e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1940e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1941e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1942e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1943e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1944ed7509acSJulian Elischer 		 * of safety by using if_simloop().
19459c9137eaSGarrett Wollman 		 */
1946201c2527SJulian Elischer #if 1 /* XXX */
1947201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
19482b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1949201c2527SJulian Elischer 						dst->sin_family);
1950201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1951201c2527SJulian Elischer 		}
1952201c2527SJulian Elischer #endif
1953201c2527SJulian Elischer 
19549c9137eaSGarrett Wollman #ifdef notdef
1955e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
1956ed7509acSJulian Elischer 		ip_input(copym);
19579c9137eaSGarrett Wollman #else
195850c6dc99SJonathan Lemon 		/* if the checksum hasn't been computed, mark it as valid */
195950c6dc99SJonathan Lemon 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
196050c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_flags |=
196150c6dc99SJonathan Lemon 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
196250c6dc99SJonathan Lemon 			copym->m_pkthdr.csum_data = 0xffff;
196350c6dc99SJonathan Lemon 		}
196406a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
19659c9137eaSGarrett Wollman #endif
1966df8bae1dSRodney W. Grimes 	}
1967df8bae1dSRodney W. Grimes }
1968