xref: /freebsd/sys/netinet/ip_output.c (revision 1c23847582ad9c4bfd31d26df29e03cfc59a1ac7)
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"
44fbd1372aSJoerg Wunsch 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
4626f9a767SRodney W. Grimes #include <sys/systm.h>
47f0f6d643SLuigi Rizzo #include <sys/kernel.h>
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
536a800098SYoshinobu Inoue #include <sys/proc.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #include <net/if.h>
56df8bae1dSRodney W. Grimes #include <net/route.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes #include <netinet/in.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
60df8bae1dSRodney W. Grimes #include <netinet/ip.h>
61df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
62df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
63df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
64df8bae1dSRodney W. Grimes 
656a800098SYoshinobu Inoue #include "faith.h"
666a800098SYoshinobu Inoue 
67df8bae1dSRodney W. Grimes #ifdef vax
68df8bae1dSRodney W. Grimes #include <machine/mtpr.h>
69df8bae1dSRodney W. Grimes #endif
709c9137eaSGarrett Wollman #include <machine/in_cksum.h>
71df8bae1dSRodney W. Grimes 
72a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
7355166637SPoul-Henning Kamp 
746a800098SYoshinobu Inoue #ifdef IPSEC
756a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
766a800098SYoshinobu Inoue #include <netkey/key.h>
776a800098SYoshinobu Inoue #ifdef IPSEC_DEBUG
786a800098SYoshinobu Inoue #include <netkey/key_debug.h>
796a800098SYoshinobu Inoue #else
806a800098SYoshinobu Inoue #define	KEYDEBUG(lev,arg)
816a800098SYoshinobu Inoue #endif
826a800098SYoshinobu Inoue #endif /*IPSEC*/
836a800098SYoshinobu Inoue 
84cfe8b629SGarrett Wollman #include <netinet/ip_fw.h>
85cfe8b629SGarrett Wollman 
86b715f178SLuigi Rizzo #ifdef DUMMYNET
87b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
88b715f178SLuigi Rizzo #endif
89b715f178SLuigi Rizzo 
90f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD_DEBUG
91f9e354dfSJulian Elischer #define print_ip(a)	 printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
92f9e354dfSJulian Elischer 				 		  (ntohl(a.s_addr)>>16)&0xFF,\
93f9e354dfSJulian Elischer 						  (ntohl(a.s_addr)>>8)&0xFF,\
94f9e354dfSJulian Elischer 						  (ntohl(a.s_addr))&0xFF);
95f9e354dfSJulian Elischer #endif
96f9e354dfSJulian Elischer 
97f23b4c91SGarrett Wollman u_short ip_id;
98f23b4c91SGarrett Wollman 
99df8bae1dSRodney W. Grimes static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
100df8bae1dSRodney W. Grimes static void	ip_mloopback
10186b1d6d2SBill Fenner 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
1020312fbe9SPoul-Henning Kamp static int	ip_getmoptions
103cfe8b629SGarrett Wollman 	__P((struct sockopt *, struct ip_moptions *));
104cfe8b629SGarrett Wollman static int	ip_pcbopts __P((int, struct mbuf **, struct mbuf *));
1050312fbe9SPoul-Henning Kamp static int	ip_setmoptions
106cfe8b629SGarrett Wollman 	__P((struct sockopt *, struct ip_moptions **));
107df8bae1dSRodney W. Grimes 
108beec8214SDarren Reed int	ip_optcopy __P((struct ip *, struct ip *));
109afed1b49SDarren Reed extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
110afed1b49SDarren Reed 
111afed1b49SDarren Reed 
11293e0e116SJulian Elischer extern	struct protosw inetsw[];
11393e0e116SJulian Elischer 
114df8bae1dSRodney W. Grimes /*
115df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
116df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
117df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
118df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
119df8bae1dSRodney W. Grimes  */
120df8bae1dSRodney W. Grimes int
121df8bae1dSRodney W. Grimes ip_output(m0, opt, ro, flags, imo)
122df8bae1dSRodney W. Grimes 	struct mbuf *m0;
123df8bae1dSRodney W. Grimes 	struct mbuf *opt;
124df8bae1dSRodney W. Grimes 	struct route *ro;
125df8bae1dSRodney W. Grimes 	int flags;
126df8bae1dSRodney W. Grimes 	struct ip_moptions *imo;
127df8bae1dSRodney W. Grimes {
12823bf9953SPoul-Henning Kamp 	struct ip *ip, *mhip;
12923bf9953SPoul-Henning Kamp 	struct ifnet *ifp;
13023bf9953SPoul-Henning Kamp 	struct mbuf *m = m0;
13123bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
132df8bae1dSRodney W. Grimes 	int len, off, error = 0;
133df8bae1dSRodney W. Grimes 	struct sockaddr_in *dst;
134df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
135db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
1366a800098SYoshinobu Inoue #ifdef IPSEC
1376a800098SYoshinobu Inoue 	struct route iproute;
138d0a98d79SYoshinobu Inoue 	struct socket *so = NULL;
1396a800098SYoshinobu Inoue 	struct secpolicy *sp = NULL;
1406a800098SYoshinobu Inoue #endif
1418948e4baSArchie Cobbs 	u_int16_t divert_cookie;		/* firewall cookie */
142f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
143f9e354dfSJulian Elischer 	int fwd_rewrite_src = 0;
144f9e354dfSJulian Elischer #endif
145b715f178SLuigi Rizzo 	struct ip_fw_chain *rule = NULL;
146b715f178SLuigi Rizzo 
1478948e4baSArchie Cobbs #ifdef IPDIVERT
1488948e4baSArchie Cobbs 	/* Get and reset firewall cookie */
1498948e4baSArchie Cobbs 	divert_cookie = ip_divert_cookie;
1508948e4baSArchie Cobbs 	ip_divert_cookie = 0;
1518948e4baSArchie Cobbs #else
1528948e4baSArchie Cobbs 	divert_cookie = 0;
1538948e4baSArchie Cobbs #endif
1548948e4baSArchie Cobbs 
1556a800098SYoshinobu Inoue 	/*
1566a800098SYoshinobu Inoue 	 * NOTE: If IP_SOCKINMRCVIF flag is set, 'socket *' is kept in
1576a800098SYoshinobu Inoue 	 * m->m_pkthdr.rcvif for later IPSEC check. In this case,
1586a800098SYoshinobu Inoue 	 * m->m_pkthdr will be NULL cleared after the contents is saved in
1596a800098SYoshinobu Inoue 	 * 'so'.
1606a800098SYoshinobu Inoue 	 * NULL clearance of rcvif should be natural because the packet should
1616a800098SYoshinobu Inoue 	 * have been sent from my own socket and has no rcvif in this case.
1626a800098SYoshinobu Inoue 	 * It is also necessary because someone might consider it as
1636a800098SYoshinobu Inoue 	 * 'ifnet *', and cause SEGV.
1646a800098SYoshinobu Inoue 	 */
165b715f178SLuigi Rizzo #if defined(IPFIREWALL) && defined(DUMMYNET)
166b715f178SLuigi Rizzo         /*
167b715f178SLuigi Rizzo          * dummynet packet are prepended a vestigial mbuf with
168b715f178SLuigi Rizzo          * m_type = MT_DUMMYNET and m_data pointing to the matching
169b715f178SLuigi Rizzo          * rule.
170b715f178SLuigi Rizzo          */
171b715f178SLuigi Rizzo         if (m->m_type == MT_DUMMYNET) {
172b715f178SLuigi Rizzo             /*
173b715f178SLuigi Rizzo              * the packet was already tagged, so part of the
174b715f178SLuigi Rizzo              * processing was already done, and we need to go down.
1756bc748b0SLuigi Rizzo              * Get parameters from the header.
176b715f178SLuigi Rizzo              */
177b715f178SLuigi Rizzo             rule = (struct ip_fw_chain *)(m->m_data) ;
178d1f04b29SLuigi Rizzo 	    opt = NULL ;
179d1f04b29SLuigi Rizzo 	    ro = & ( ((struct dn_pkt *)m)->ro ) ;
180d1f04b29SLuigi Rizzo 	    imo = NULL ;
181d1f04b29SLuigi Rizzo 	    dst = ((struct dn_pkt *)m)->dn_dst ;
182d1f04b29SLuigi Rizzo 	    ifp = ((struct dn_pkt *)m)->ifp ;
183d0a98d79SYoshinobu Inoue 	    flags = ((struct dn_pkt *)m)->flags ;
184d1f04b29SLuigi Rizzo 
18517458d35SLuigi Rizzo             m0 = m = m->m_next ;
186d0a98d79SYoshinobu Inoue #ifdef IPSEC
187d0a98d79SYoshinobu Inoue 	    if ((flags & IP_SOCKINMRCVIF) != 0) {
188d0a98d79SYoshinobu Inoue 	        so = (struct socket *)m->m_pkthdr.rcvif;
189d0a98d79SYoshinobu Inoue 	        m->m_pkthdr.rcvif = NULL;
190d0a98d79SYoshinobu Inoue 	    }
191d0a98d79SYoshinobu Inoue #endif
192b715f178SLuigi Rizzo             ip = mtod(m, struct ip *);
193b715f178SLuigi Rizzo             hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
194b715f178SLuigi Rizzo             goto sendit;
195b715f178SLuigi Rizzo         } else
196b715f178SLuigi Rizzo             rule = NULL ;
197b715f178SLuigi Rizzo #endif
198d0a98d79SYoshinobu Inoue #ifdef IPSEC
199d0a98d79SYoshinobu Inoue 	if ((flags & IP_SOCKINMRCVIF) != 0) {
200d0a98d79SYoshinobu Inoue 		so = (struct socket *)m->m_pkthdr.rcvif;
201d0a98d79SYoshinobu Inoue 		m->m_pkthdr.rcvif = NULL;
202d0a98d79SYoshinobu Inoue 	}
203d0a98d79SYoshinobu Inoue #endif
204b715f178SLuigi Rizzo 
205df8bae1dSRodney W. Grimes #ifdef	DIAGNOSTIC
206df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
207df8bae1dSRodney W. Grimes 		panic("ip_output no HDR");
2089c9137eaSGarrett Wollman 	if (!ro)
2099c9137eaSGarrett Wollman 		panic("ip_output no route, proto = %d",
2109c9137eaSGarrett Wollman 		      mtod(m, struct ip *)->ip_p);
211df8bae1dSRodney W. Grimes #endif
212df8bae1dSRodney W. Grimes 	if (opt) {
213df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
214df8bae1dSRodney W. Grimes 		hlen = len;
215df8bae1dSRodney W. Grimes 	}
216df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
217df8bae1dSRodney W. Grimes 	/*
218df8bae1dSRodney W. Grimes 	 * Fill in IP header.
219df8bae1dSRodney W. Grimes 	 */
220df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
2219c9137eaSGarrett Wollman 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
222df8bae1dSRodney W. Grimes 		ip->ip_off &= IP_DF;
223df8bae1dSRodney W. Grimes 		ip->ip_id = htons(ip_id++);
224df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
225df8bae1dSRodney W. Grimes 	} else {
2269c9137eaSGarrett Wollman 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
227df8bae1dSRodney W. Grimes 	}
2289c9137eaSGarrett Wollman 
229df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
230df8bae1dSRodney W. Grimes 	/*
231df8bae1dSRodney W. Grimes 	 * If there is a cached route,
232df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
233df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
234df8bae1dSRodney W. Grimes 	 */
235df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
236df8bae1dSRodney W. Grimes 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
237df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
238df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
239df8bae1dSRodney W. Grimes 	}
240df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
241df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
242df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
243df8bae1dSRodney W. Grimes 		dst->sin_addr = ip->ip_dst;
244df8bae1dSRodney W. Grimes 	}
245df8bae1dSRodney W. Grimes 	/*
246df8bae1dSRodney W. Grimes 	 * If routing to interface only,
247df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
248df8bae1dSRodney W. Grimes 	 */
249df8bae1dSRodney W. Grimes #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
250df8bae1dSRodney W. Grimes #define sintosa(sin)	((struct sockaddr *)(sin))
251df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
252df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
253df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
254df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
255df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
256df8bae1dSRodney W. Grimes 			goto bad;
257df8bae1dSRodney W. Grimes 		}
258df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
259df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2609f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
261df8bae1dSRodney W. Grimes 	} else {
2622c17fe93SGarrett Wollman 		/*
2632c17fe93SGarrett Wollman 		 * If this is the case, we probably don't want to allocate
2642c17fe93SGarrett Wollman 		 * a protocol-cloned route since we didn't get one from the
2652c17fe93SGarrett Wollman 		 * ULP.  This lets TCP do its thing, while not burdening
2662c17fe93SGarrett Wollman 		 * forwarding or ICMP with the overhead of cloning a route.
2672c17fe93SGarrett Wollman 		 * Of course, we still want to do any cloning requested by
2682c17fe93SGarrett Wollman 		 * the link layer, as this is probably required in all cases
2692c17fe93SGarrett Wollman 		 * 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)
282df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2839f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
2849f9b3dc4SGarrett Wollman 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2859f9b3dc4SGarrett Wollman 		else
2869f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
287df8bae1dSRodney W. Grimes 	}
288df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_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;
303df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL)
304df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
3051c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3061c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
3071c5de19aSGarrett Wollman 				    ip_mcast_src(imo->imo_multicast_vif);
308df8bae1dSRodney W. Grimes 		} else
309df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
310df8bae1dSRodney W. Grimes 		/*
311df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
312df8bae1dSRodney W. Grimes 		 */
3131c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
314df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
315df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
316df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
317df8bae1dSRodney W. Grimes 				goto bad;
318df8bae1dSRodney W. Grimes 			}
3191c5de19aSGarrett Wollman 		}
320df8bae1dSRodney W. Grimes 		/*
321df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
322df8bae1dSRodney W. Grimes 		 * of outgoing interface.
323df8bae1dSRodney W. Grimes 		 */
324df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
3250abc78a6SPoul-Henning Kamp 			register struct in_ifaddr *ia1;
326df8bae1dSRodney W. Grimes 
3270abc78a6SPoul-Henning Kamp 			for (ia1 = in_ifaddrhead.tqh_first; ia1;
3280abc78a6SPoul-Henning Kamp 			     ia1 = ia1->ia_link.tqe_next)
3290abc78a6SPoul-Henning Kamp 				if (ia1->ia_ifp == ifp) {
3300abc78a6SPoul-Henning Kamp 					ip->ip_src = IA_SIN(ia1)->sin_addr;
331df8bae1dSRodney W. Grimes 					break;
332df8bae1dSRodney W. Grimes 				}
333df8bae1dSRodney W. Grimes 		}
334df8bae1dSRodney W. Grimes 
335df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
336df8bae1dSRodney W. Grimes 		if (inm != NULL &&
337df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
338df8bae1dSRodney W. Grimes 			/*
339df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
340df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
341df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
342df8bae1dSRodney W. Grimes 			 */
34386b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
344df8bae1dSRodney W. Grimes 		}
345df8bae1dSRodney W. Grimes 		else {
346df8bae1dSRodney W. Grimes 			/*
347df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
348df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
349df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
350df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
351df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
352df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
353df8bae1dSRodney W. Grimes 			 *
354df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
355df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
356df8bae1dSRodney W. Grimes 			 * if necessary.
357df8bae1dSRodney W. Grimes 			 */
358df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
359f0068c4aSGarrett Wollman 				/*
360f0068c4aSGarrett Wollman 				 * Check if rsvp daemon is running. If not, don't
361f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
362f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
363f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
364f0068c4aSGarrett Wollman 				 */
365b124e4f2SGarrett Wollman 				if (!rsvp_on)
366f0068c4aSGarrett Wollman 				  imo = NULL;
367f0068c4aSGarrett Wollman 				if (ip_mforward(ip, ifp, m, imo) != 0) {
368df8bae1dSRodney W. Grimes 					m_freem(m);
369df8bae1dSRodney W. Grimes 					goto done;
370df8bae1dSRodney W. Grimes 				}
371df8bae1dSRodney W. Grimes 			}
372df8bae1dSRodney W. Grimes 		}
3735e9ae478SGarrett Wollman 
374df8bae1dSRodney W. Grimes 		/*
375df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
376df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
377df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
378df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
379df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
380df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
381df8bae1dSRodney W. Grimes 		 */
382f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
383df8bae1dSRodney W. Grimes 			m_freem(m);
384df8bae1dSRodney W. Grimes 			goto done;
385df8bae1dSRodney W. Grimes 		}
386df8bae1dSRodney W. Grimes 
387df8bae1dSRodney W. Grimes 		goto sendit;
388df8bae1dSRodney W. Grimes 	}
389df8bae1dSRodney W. Grimes #ifndef notdef
390df8bae1dSRodney W. Grimes 	/*
391df8bae1dSRodney W. Grimes 	 * If source address not specified yet, use address
392df8bae1dSRodney W. Grimes 	 * of outgoing interface.
393df8bae1dSRodney W. Grimes 	 */
394f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
395df8bae1dSRodney W. Grimes 		ip->ip_src = IA_SIN(ia)->sin_addr;
396f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
397f9e354dfSJulian Elischer 		/* Keep note that we did this - if the firewall changes
398f9e354dfSJulian Elischer 		 * the next-hop, our interface may change, changing the
399f9e354dfSJulian Elischer 		 * default source IP. It's a shame so much effort happens
400f9e354dfSJulian Elischer 		 * twice. Oh well.
401f9e354dfSJulian Elischer 		 */
402f9e354dfSJulian Elischer 		fwd_rewrite_src++;
403f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */
404f9e354dfSJulian Elischer 	}
405f9e354dfSJulian Elischer #endif /* notdef */
406df8bae1dSRodney W. Grimes 	/*
407b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
408b5390296SDavid Greenman 	 *      the packet or packet fragments
409b5390296SDavid Greenman 	 */
410b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
411b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
412b5390296SDavid Greenman 			error = ENOBUFS;
413b5390296SDavid Greenman 			goto bad;
414b5390296SDavid Greenman 	}
415b5390296SDavid Greenman 
416b5390296SDavid Greenman 	/*
417df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
418df8bae1dSRodney W. Grimes 	 * and verify user is allowed to send
419df8bae1dSRodney W. Grimes 	 * such a packet.
420df8bae1dSRodney W. Grimes 	 */
4219f9b3dc4SGarrett Wollman 	if (isbroadcast) {
422df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
423df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
424df8bae1dSRodney W. Grimes 			goto bad;
425df8bae1dSRodney W. Grimes 		}
426df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
427df8bae1dSRodney W. Grimes 			error = EACCES;
428df8bae1dSRodney W. Grimes 			goto bad;
429df8bae1dSRodney W. Grimes 		}
430df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
431df8bae1dSRodney W. Grimes 		if ((u_short)ip->ip_len > ifp->if_mtu) {
432df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
433df8bae1dSRodney W. Grimes 			goto bad;
434df8bae1dSRodney W. Grimes 		}
435df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4369f9b3dc4SGarrett Wollman 	} else {
437df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4389f9b3dc4SGarrett Wollman 	}
439df8bae1dSRodney W. Grimes 
440f1743588SDarren Reed sendit:
441fed1c7e9SSøren Schmidt 	/*
442fed1c7e9SSøren Schmidt 	 * IpHack's section.
443fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
444e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
445fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
446fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
447fed1c7e9SSøren Schmidt 	 */
4481ee25934SPeter Wemm 	if (fr_checkp) {
4491ee25934SPeter Wemm 		struct  mbuf    *m1 = m;
4501ee25934SPeter Wemm 
4511ee25934SPeter Wemm 		if ((error = (*fr_checkp)(ip, hlen, ifp, 1, &m1)) || !m1)
4521ee25934SPeter Wemm 			goto done;
4531ee25934SPeter Wemm 		ip = mtod(m = m1, struct ip *);
4541ee25934SPeter Wemm 	}
455fed1c7e9SSøren Schmidt 
456df8bae1dSRodney W. Grimes 	/*
457e7319babSPoul-Henning Kamp 	 * Check with the firewall...
458e7319babSPoul-Henning Kamp 	 */
4596bc748b0SLuigi Rizzo 	if (fw_enable && ip_fw_chk_ptr) {
4609de9737fSPeter Wemm 		struct sockaddr_in *old = dst;
461b715f178SLuigi Rizzo 
462b715f178SLuigi Rizzo 		off = (*ip_fw_chk_ptr)(&ip,
4638948e4baSArchie Cobbs 		    hlen, ifp, &divert_cookie, &m, &rule, &dst);
464b715f178SLuigi Rizzo                 /*
465b715f178SLuigi Rizzo                  * On return we must do the following:
466b715f178SLuigi Rizzo                  * m == NULL         -> drop the pkt
467b715f178SLuigi Rizzo                  * 1<=off<= 0xffff   -> DIVERT
468b715f178SLuigi Rizzo                  * (off & 0x10000)   -> send to a DUMMYNET pipe
4698948e4baSArchie Cobbs                  * (off & 0x20000)   -> TEE the packet
470b715f178SLuigi Rizzo                  * dst != old        -> IPFIREWALL_FORWARD
471b715f178SLuigi Rizzo                  * off==0, dst==old  -> accept
472b715f178SLuigi Rizzo                  * If some of the above modules is not compiled in, then
473b715f178SLuigi Rizzo                  * we should't have to check the corresponding condition
474b715f178SLuigi Rizzo                  * (because the ipfw control socket should not accept
475b715f178SLuigi Rizzo                  * unsupported rules), but better play safe and drop
476b715f178SLuigi Rizzo                  * packets in case of doubt.
477b715f178SLuigi Rizzo                  */
478b715f178SLuigi Rizzo 		if (!m) { /* firewall said to reject */
479e4676ba6SJulian Elischer 			error = EACCES;
480e4676ba6SJulian Elischer 			goto done;
48193e0e116SJulian Elischer 		}
482b715f178SLuigi Rizzo 		if (off == 0 && dst == old) /* common case */
483b715f178SLuigi Rizzo 			goto pass ;
484b715f178SLuigi Rizzo #ifdef DUMMYNET
4858948e4baSArchie Cobbs                 if ((off & IP_FW_PORT_DYNT_FLAG) != 0) {
486b715f178SLuigi Rizzo                     /*
487b715f178SLuigi Rizzo                      * pass the pkt to dummynet. Need to include
488f0a53591SLuigi Rizzo                      * pipe number, m, ifp, ro, dst because these are
489b715f178SLuigi Rizzo                      * not recomputed in the next pass.
490b715f178SLuigi Rizzo                      * All other parameters have been already used and
491b715f178SLuigi Rizzo                      * so they are not needed anymore.
492b715f178SLuigi Rizzo                      * XXX note: if the ifp or ro entry are deleted
493b715f178SLuigi Rizzo                      * while a pkt is in dummynet, we are in trouble!
494b715f178SLuigi Rizzo                      */
4956a800098SYoshinobu Inoue                     dummynet_io(off & 0xffff, DN_TO_IP_OUT, m,ifp,ro,dst,rule,
4966a800098SYoshinobu Inoue 				flags);
497b715f178SLuigi Rizzo 			goto done;
498b715f178SLuigi Rizzo 		}
499b715f178SLuigi Rizzo #endif
500b715f178SLuigi Rizzo #ifdef IPDIVERT
5018948e4baSArchie Cobbs 		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
5028948e4baSArchie Cobbs 			struct mbuf *clone = NULL;
5038948e4baSArchie Cobbs 
5048948e4baSArchie Cobbs 			/* Clone packet if we're doing a 'tee' */
5058948e4baSArchie Cobbs 			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
5068948e4baSArchie Cobbs 				clone = m_dup(m, M_DONTWAIT);
5078948e4baSArchie Cobbs 
508ea53ecd9SJonathan Lemon 			/*
509ea53ecd9SJonathan Lemon 			 * XXX
510ea53ecd9SJonathan Lemon 			 * delayed checksums are not currently compatible
511ea53ecd9SJonathan Lemon 			 * with divert sockets.
512ea53ecd9SJonathan Lemon 			 */
513ea53ecd9SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
514ea53ecd9SJonathan Lemon 				in_delayed_cksum(m);
515ea53ecd9SJonathan Lemon 				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
516ea53ecd9SJonathan Lemon 			}
517ea53ecd9SJonathan Lemon 
5188948e4baSArchie Cobbs 			/* Restore packet header fields to original values */
5198948e4baSArchie Cobbs 			HTONS(ip->ip_len);
5208948e4baSArchie Cobbs 			HTONS(ip->ip_off);
5218948e4baSArchie Cobbs 
5228948e4baSArchie Cobbs 			/* Deliver packet to divert input routine */
5238948e4baSArchie Cobbs 			ip_divert_cookie = divert_cookie;
5248948e4baSArchie Cobbs 			divert_packet(m, 0, off & 0xffff);
5258948e4baSArchie Cobbs 
5268948e4baSArchie Cobbs 			/* If 'tee', continue with original packet */
5278948e4baSArchie Cobbs 			if (clone != NULL) {
5288948e4baSArchie Cobbs 				m = clone;
5298948e4baSArchie Cobbs 				ip = mtod(m, struct ip *);
5308948e4baSArchie Cobbs 				goto pass;
5318948e4baSArchie Cobbs 			}
532b715f178SLuigi Rizzo 			goto done;
533b715f178SLuigi Rizzo 		}
534b715f178SLuigi Rizzo #endif
535b715f178SLuigi Rizzo 
536f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
537f9e354dfSJulian Elischer 		/* Here we check dst to make sure it's directly reachable on the
538f9e354dfSJulian Elischer 		 * interface we previously thought it was.
539f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
540f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
541f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
542f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
543f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
544f9e354dfSJulian Elischer 		 * And I'm babbling.
545f9e354dfSJulian Elischer 		 */
546b715f178SLuigi Rizzo 		if (off == 0 && old != dst) {
547f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
548f9e354dfSJulian Elischer 
549f9e354dfSJulian Elischer 			/* It's changed... */
550f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
551f9e354dfSJulian Elischer 			static struct route sro_fwd, *ro_fwd = &sro_fwd;
552f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD_DEBUG
553f9e354dfSJulian Elischer 			printf("IPFIREWALL_FORWARD: New dst ip: ");
554f9e354dfSJulian Elischer 			print_ip(dst->sin_addr);
555f9e354dfSJulian Elischer 			printf("\n");
556f9e354dfSJulian Elischer #endif
557f9e354dfSJulian Elischer 			/*
558f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
559f9e354dfSJulian Elischer 			 * to a local socket. If so then we should somehow
560f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
561f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
562f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
563f9e354dfSJulian Elischer 			 * you want to forward until they are being output
564f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
565f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
566f9e354dfSJulian Elischer 			 * we would not be able to control what happens
567f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
568f9e354dfSJulian Elischer 			 * it is done through a ISR.
569f9e354dfSJulian Elischer 			 */
570d4295c32SJulian Elischer 			for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
571f9e354dfSJulian Elischer 					ia = TAILQ_NEXT(ia, ia_link)) {
572f9e354dfSJulian Elischer 				/*
573f9e354dfSJulian Elischer 				 * If the addr to forward to is one
574f9e354dfSJulian Elischer 				 * of ours, we pretend to
575f9e354dfSJulian Elischer 				 * be the destination for this packet.
576f9e354dfSJulian Elischer 				 */
577f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
578f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
579f9e354dfSJulian Elischer 					break;
580f9e354dfSJulian Elischer 			}
581f9e354dfSJulian Elischer 			if (ia) {
582f9e354dfSJulian Elischer 				/* tell ip_input "dont filter" */
583f9e354dfSJulian Elischer 				ip_fw_fwd_addr = dst;
584f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
585f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
58620c822f3SJonathan Lemon 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
58720c822f3SJonathan Lemon 					m->m_pkthdr.csum_flags |=
58820c822f3SJonathan Lemon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
58920c822f3SJonathan Lemon 					m0->m_pkthdr.csum_data = 0xffff;
59020c822f3SJonathan Lemon 				}
59120c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
59220c822f3SJonathan Lemon 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
593f9e354dfSJulian Elischer 				ip->ip_len = htons((u_short)ip->ip_len);
594f9e354dfSJulian Elischer 				ip->ip_off = htons((u_short)ip->ip_off);
595f9e354dfSJulian Elischer 				ip_input(m);
596f9e354dfSJulian Elischer 				goto done;
597f9e354dfSJulian Elischer 			}
598f9e354dfSJulian Elischer 			/* Some of the logic for this was
599f9e354dfSJulian Elischer 			 * nicked from above.
600f9e354dfSJulian Elischer 			 *
601f9e354dfSJulian Elischer 			 * This rewrites the cached route in a local PCB.
602f9e354dfSJulian Elischer 			 * Is this what we want to do?
603f9e354dfSJulian Elischer 			 */
604f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
605f9e354dfSJulian Elischer 
606f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
607f9e354dfSJulian Elischer 			rtalloc_ign(ro_fwd, RTF_PRCLONING);
608f9e354dfSJulian Elischer 
609f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt == 0) {
610f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
611f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
612f9e354dfSJulian Elischer 				goto bad;
613f9e354dfSJulian Elischer 			}
614f9e354dfSJulian Elischer 
615f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
616f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
617f9e354dfSJulian Elischer 			ro_fwd->ro_rt->rt_use++;
618f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
619f9e354dfSJulian Elischer 				dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
620f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
621f9e354dfSJulian Elischer 				isbroadcast =
622f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
623f9e354dfSJulian Elischer 			else
624f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
625f9e354dfSJulian Elischer 			RTFREE(ro->ro_rt);
626f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
627f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
628f9e354dfSJulian Elischer 
629f9e354dfSJulian Elischer 			/*
630f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
631f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
632f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
633f9e354dfSJulian Elischer 			 */
634f9e354dfSJulian Elischer 			if (fwd_rewrite_src)
635f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
636b715f178SLuigi Rizzo 			goto pass ;
637f9e354dfSJulian Elischer 		}
638f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */
639b715f178SLuigi Rizzo                 /*
640b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
641b715f178SLuigi Rizzo                  * we have to drop the pkt
642b715f178SLuigi Rizzo                  */
643b715f178SLuigi Rizzo 		m_freem(m);
644b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
645b715f178SLuigi Rizzo                 goto done;
64693e0e116SJulian Elischer 	}
647e7319babSPoul-Henning Kamp 
648b715f178SLuigi Rizzo pass:
6496a800098SYoshinobu Inoue #ifdef IPSEC
6506a800098SYoshinobu Inoue 	/* get SP for this packet */
6516a800098SYoshinobu Inoue 	if (so == NULL)
6526a800098SYoshinobu Inoue 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
6536a800098SYoshinobu Inoue 	else
6546a800098SYoshinobu Inoue 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
6556a800098SYoshinobu Inoue 
6566a800098SYoshinobu Inoue 	if (sp == NULL) {
6576a800098SYoshinobu Inoue 		ipsecstat.out_inval++;
6586a800098SYoshinobu Inoue 		goto bad;
6596a800098SYoshinobu Inoue 	}
6606a800098SYoshinobu Inoue 
6616a800098SYoshinobu Inoue 	error = 0;
6626a800098SYoshinobu Inoue 
6636a800098SYoshinobu Inoue 	/* check policy */
6646a800098SYoshinobu Inoue 	switch (sp->policy) {
6656a800098SYoshinobu Inoue 	case IPSEC_POLICY_DISCARD:
6666a800098SYoshinobu Inoue 		/*
6676a800098SYoshinobu Inoue 		 * This packet is just discarded.
6686a800098SYoshinobu Inoue 		 */
6696a800098SYoshinobu Inoue 		ipsecstat.out_polvio++;
6706a800098SYoshinobu Inoue 		goto bad;
6716a800098SYoshinobu Inoue 
6726a800098SYoshinobu Inoue 	case IPSEC_POLICY_BYPASS:
6736a800098SYoshinobu Inoue 	case IPSEC_POLICY_NONE:
6746a800098SYoshinobu Inoue 		/* no need to do IPsec. */
6756a800098SYoshinobu Inoue 		goto skip_ipsec;
6766a800098SYoshinobu Inoue 
6776a800098SYoshinobu Inoue 	case IPSEC_POLICY_IPSEC:
6786a800098SYoshinobu Inoue 		if (sp->req == NULL) {
6796a800098SYoshinobu Inoue 			/* XXX should be panic ? */
6806a800098SYoshinobu Inoue 			printf("ip_output: No IPsec request specified.\n");
6816a800098SYoshinobu Inoue 			error = EINVAL;
6826a800098SYoshinobu Inoue 			goto bad;
6836a800098SYoshinobu Inoue 		}
6846a800098SYoshinobu Inoue 		break;
6856a800098SYoshinobu Inoue 
6866a800098SYoshinobu Inoue 	case IPSEC_POLICY_ENTRUST:
6876a800098SYoshinobu Inoue 	default:
6886a800098SYoshinobu Inoue 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
6896a800098SYoshinobu Inoue 	}
6906a800098SYoshinobu Inoue     {
6916a800098SYoshinobu Inoue 	struct ipsec_output_state state;
6926a800098SYoshinobu Inoue 	bzero(&state, sizeof(state));
6936a800098SYoshinobu Inoue 	state.m = m;
6946a800098SYoshinobu Inoue 	if (flags & IP_ROUTETOIF) {
6956a800098SYoshinobu Inoue 		state.ro = &iproute;
6966a800098SYoshinobu Inoue 		bzero(&iproute, sizeof(iproute));
6976a800098SYoshinobu Inoue 	} else
6986a800098SYoshinobu Inoue 		state.ro = ro;
6996a800098SYoshinobu Inoue 	state.dst = (struct sockaddr *)dst;
7006a800098SYoshinobu Inoue 
7017cba257aSYoshinobu Inoue 	ip->ip_sum = 0;
7027cba257aSYoshinobu Inoue 
703db4f9cc7SJonathan Lemon 	/*
704db4f9cc7SJonathan Lemon 	 * XXX
705db4f9cc7SJonathan Lemon 	 * delayed checksums are not currently compatible with IPsec
706db4f9cc7SJonathan Lemon 	 */
707db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
708db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
709db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
710db4f9cc7SJonathan Lemon 	}
711db4f9cc7SJonathan Lemon 
7127cba257aSYoshinobu Inoue 	ip->ip_len = htons((u_short)ip->ip_len);
7137cba257aSYoshinobu Inoue 	ip->ip_off = htons((u_short)ip->ip_off);
7147cba257aSYoshinobu Inoue 
7156a800098SYoshinobu Inoue 	error = ipsec4_output(&state, sp, flags);
7166a800098SYoshinobu Inoue 
7176a800098SYoshinobu Inoue 	m = state.m;
7186a800098SYoshinobu Inoue 	if (flags & IP_ROUTETOIF) {
7196a800098SYoshinobu Inoue 		/*
7206a800098SYoshinobu Inoue 		 * if we have tunnel mode SA, we may need to ignore
7216a800098SYoshinobu Inoue 		 * IP_ROUTETOIF.
7226a800098SYoshinobu Inoue 		 */
7236a800098SYoshinobu Inoue 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
7246a800098SYoshinobu Inoue 			flags &= ~IP_ROUTETOIF;
7256a800098SYoshinobu Inoue 			ro = state.ro;
7266a800098SYoshinobu Inoue 		}
7276a800098SYoshinobu Inoue 	} else
7286a800098SYoshinobu Inoue 		ro = state.ro;
7296a800098SYoshinobu Inoue 	dst = (struct sockaddr_in *)state.dst;
7306a800098SYoshinobu Inoue 	if (error) {
7316a800098SYoshinobu Inoue 		/* mbuf is already reclaimed in ipsec4_output. */
7326a800098SYoshinobu Inoue 		m0 = NULL;
7336a800098SYoshinobu Inoue 		switch (error) {
7346a800098SYoshinobu Inoue 		case EHOSTUNREACH:
7356a800098SYoshinobu Inoue 		case ENETUNREACH:
7366a800098SYoshinobu Inoue 		case EMSGSIZE:
7376a800098SYoshinobu Inoue 		case ENOBUFS:
7386a800098SYoshinobu Inoue 		case ENOMEM:
7396a800098SYoshinobu Inoue 			break;
7406a800098SYoshinobu Inoue 		default:
7416a800098SYoshinobu Inoue 			printf("ip4_output (ipsec): error code %d\n", error);
7426a800098SYoshinobu Inoue 			/*fall through*/
7436a800098SYoshinobu Inoue 		case ENOENT:
7446a800098SYoshinobu Inoue 			/* don't show these error codes to the user */
7456a800098SYoshinobu Inoue 			error = 0;
7466a800098SYoshinobu Inoue 			break;
7476a800098SYoshinobu Inoue 		}
7486a800098SYoshinobu Inoue 		goto bad;
7496a800098SYoshinobu Inoue 	}
7506a800098SYoshinobu Inoue     }
7516a800098SYoshinobu Inoue 
7526a800098SYoshinobu Inoue 	/* be sure to update variables that are affected by ipsec4_output() */
7536a800098SYoshinobu Inoue 	ip = mtod(m, struct ip *);
7546a800098SYoshinobu Inoue #ifdef _IP_VHL
7556a800098SYoshinobu Inoue 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
7566a800098SYoshinobu Inoue #else
7576a800098SYoshinobu Inoue 	hlen = ip->ip_hl << 2;
7586a800098SYoshinobu Inoue #endif
7596a800098SYoshinobu Inoue 	if (ro->ro_rt == NULL) {
7606a800098SYoshinobu Inoue 		if ((flags & IP_ROUTETOIF) == 0) {
7616a800098SYoshinobu Inoue 			printf("ip_output: "
7626a800098SYoshinobu Inoue 				"can't update route after IPsec processing\n");
7636a800098SYoshinobu Inoue 			error = EHOSTUNREACH;	/*XXX*/
7646a800098SYoshinobu Inoue 			goto bad;
7656a800098SYoshinobu Inoue 		}
7666a800098SYoshinobu Inoue 	} else {
7676a800098SYoshinobu Inoue 		/* nobody uses ia beyond here */
7686a800098SYoshinobu Inoue 		ifp = ro->ro_rt->rt_ifp;
7696a800098SYoshinobu Inoue 	}
7706a800098SYoshinobu Inoue 
7716a800098SYoshinobu Inoue 	/* make it flipped, again. */
7726a800098SYoshinobu Inoue 	ip->ip_len = ntohs((u_short)ip->ip_len);
7736a800098SYoshinobu Inoue 	ip->ip_off = ntohs((u_short)ip->ip_off);
7746a800098SYoshinobu Inoue skip_ipsec:
7756a800098SYoshinobu Inoue #endif /*IPSEC*/
7766a800098SYoshinobu Inoue 
777db4f9cc7SJonathan Lemon 	sw_csum = m->m_pkthdr.csum_flags | CSUM_IP;
778db4f9cc7SJonathan Lemon 	m->m_pkthdr.csum_flags = sw_csum & ifp->if_hwassist;
779db4f9cc7SJonathan Lemon 	sw_csum &= ~ifp->if_hwassist;
780db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
781db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
782db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
783db4f9cc7SJonathan Lemon 	}
784db4f9cc7SJonathan Lemon 
785e7319babSPoul-Henning Kamp 	/*
786db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
787db4f9cc7SJonathan Lemon 	 * care of the fragmentation for us, can just send directly.
788df8bae1dSRodney W. Grimes 	 */
789db4f9cc7SJonathan Lemon 	if ((u_short)ip->ip_len <= ifp->if_mtu ||
790db4f9cc7SJonathan Lemon 	    ifp->if_hwassist & CSUM_FRAGMENT) {
791df8bae1dSRodney W. Grimes 		ip->ip_len = htons((u_short)ip->ip_len);
792df8bae1dSRodney W. Grimes 		ip->ip_off = htons((u_short)ip->ip_off);
793df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
794db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
7959c9137eaSGarrett Wollman 			if (ip->ip_vhl == IP_VHL_BORING) {
7969c9137eaSGarrett Wollman 				ip->ip_sum = in_cksum_hdr(ip);
7979c9137eaSGarrett Wollman 			} else {
798df8bae1dSRodney W. Grimes 				ip->ip_sum = in_cksum(m, hlen);
7999c9137eaSGarrett Wollman 			}
800db4f9cc7SJonathan Lemon 		}
801df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
802df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
803df8bae1dSRodney W. Grimes 		goto done;
804df8bae1dSRodney W. Grimes 	}
805df8bae1dSRodney W. Grimes 	/*
806df8bae1dSRodney W. Grimes 	 * Too large for interface; fragment if possible.
807df8bae1dSRodney W. Grimes 	 * Must be able to put at least 8 bytes per fragment.
808df8bae1dSRodney W. Grimes 	 */
809df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
810df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
8113d1f141bSGarrett Wollman 		/*
8123d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
8133d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
8143d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
8153d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
8163d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
8173d1f141bSGarrett Wollman 		 */
8183d1f141bSGarrett Wollman 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
8193d1f141bSGarrett Wollman 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
8203d1f141bSGarrett Wollman 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
8213d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
8223d1f141bSGarrett Wollman 		}
823df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
824df8bae1dSRodney W. Grimes 		goto bad;
825df8bae1dSRodney W. Grimes 	}
826df8bae1dSRodney W. Grimes 	len = (ifp->if_mtu - hlen) &~ 7;
827df8bae1dSRodney W. Grimes 	if (len < 8) {
828df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
829df8bae1dSRodney W. Grimes 		goto bad;
830df8bae1dSRodney W. Grimes 	}
831df8bae1dSRodney W. Grimes 
832db4f9cc7SJonathan Lemon 	/*
833db4f9cc7SJonathan Lemon 	 * if the interface will not calculate checksums on
834db4f9cc7SJonathan Lemon 	 * fragmented packets, then do it here.
835db4f9cc7SJonathan Lemon 	 */
836db4f9cc7SJonathan Lemon 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
837db4f9cc7SJonathan Lemon 	    (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
838db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
839db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
840db4f9cc7SJonathan Lemon 	}
841db4f9cc7SJonathan Lemon 
842df8bae1dSRodney W. Grimes     {
843df8bae1dSRodney W. Grimes 	int mhlen, firstlen = len;
844df8bae1dSRodney W. Grimes 	struct mbuf **mnext = &m->m_nextpkt;
845db4f9cc7SJonathan Lemon 	int nfrags = 1;
846df8bae1dSRodney W. Grimes 
847df8bae1dSRodney W. Grimes 	/*
848df8bae1dSRodney W. Grimes 	 * Loop through length of segment after first fragment,
849df8bae1dSRodney W. Grimes 	 * make new header and copy data of each part and link onto chain.
850df8bae1dSRodney W. Grimes 	 */
851df8bae1dSRodney W. Grimes 	m0 = m;
852df8bae1dSRodney W. Grimes 	mhlen = sizeof (struct ip);
853df8bae1dSRodney W. Grimes 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
854df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
855df8bae1dSRodney W. Grimes 		if (m == 0) {
856df8bae1dSRodney W. Grimes 			error = ENOBUFS;
857df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
858df8bae1dSRodney W. Grimes 			goto sendorfree;
859df8bae1dSRodney W. Grimes 		}
860db4f9cc7SJonathan Lemon 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
861df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
862df8bae1dSRodney W. Grimes 		mhip = mtod(m, struct ip *);
863df8bae1dSRodney W. Grimes 		*mhip = *ip;
864df8bae1dSRodney W. Grimes 		if (hlen > sizeof (struct ip)) {
865df8bae1dSRodney W. Grimes 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
8669c9137eaSGarrett Wollman 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
867df8bae1dSRodney W. Grimes 		}
868df8bae1dSRodney W. Grimes 		m->m_len = mhlen;
869df8bae1dSRodney W. Grimes 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
870df8bae1dSRodney W. Grimes 		if (ip->ip_off & IP_MF)
871df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
872df8bae1dSRodney W. Grimes 		if (off + len >= (u_short)ip->ip_len)
873df8bae1dSRodney W. Grimes 			len = (u_short)ip->ip_len - off;
874df8bae1dSRodney W. Grimes 		else
875df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
876df8bae1dSRodney W. Grimes 		mhip->ip_len = htons((u_short)(len + mhlen));
877df8bae1dSRodney W. Grimes 		m->m_next = m_copy(m0, off, len);
878df8bae1dSRodney W. Grimes 		if (m->m_next == 0) {
879df8bae1dSRodney W. Grimes 			(void) m_free(m);
880df8bae1dSRodney W. Grimes 			error = ENOBUFS;	/* ??? */
881df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
882df8bae1dSRodney W. Grimes 			goto sendorfree;
883df8bae1dSRodney W. Grimes 		}
884df8bae1dSRodney W. Grimes 		m->m_pkthdr.len = mhlen + len;
885df8bae1dSRodney W. Grimes 		m->m_pkthdr.rcvif = (struct ifnet *)0;
886db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
887df8bae1dSRodney W. Grimes 		mhip->ip_off = htons((u_short)mhip->ip_off);
888df8bae1dSRodney W. Grimes 		mhip->ip_sum = 0;
889db4f9cc7SJonathan Lemon 		if (sw_csum & CSUM_DELAY_IP) {
8909c9137eaSGarrett Wollman 			if (mhip->ip_vhl == IP_VHL_BORING) {
891e2184122SBruce Evans 				mhip->ip_sum = in_cksum_hdr(mhip);
8929c9137eaSGarrett Wollman 			} else {
893df8bae1dSRodney W. Grimes 				mhip->ip_sum = in_cksum(m, mhlen);
8949c9137eaSGarrett Wollman 			}
895db4f9cc7SJonathan Lemon 		}
896df8bae1dSRodney W. Grimes 		*mnext = m;
897df8bae1dSRodney W. Grimes 		mnext = &m->m_nextpkt;
898db4f9cc7SJonathan Lemon 		nfrags++;
899df8bae1dSRodney W. Grimes 	}
900db4f9cc7SJonathan Lemon 	ipstat.ips_ofragments += nfrags;
901db4f9cc7SJonathan Lemon 
902db4f9cc7SJonathan Lemon 	/* set first/last markers for fragment chain */
903db4f9cc7SJonathan Lemon 	m->m_flags |= M_LASTFRAG;
904db4f9cc7SJonathan Lemon 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
905db4f9cc7SJonathan Lemon 	m0->m_pkthdr.csum_data = nfrags;
906db4f9cc7SJonathan Lemon 
907df8bae1dSRodney W. Grimes 	/*
908df8bae1dSRodney W. Grimes 	 * Update first fragment by trimming what's been copied out
909df8bae1dSRodney W. Grimes 	 * and updating header, then send each fragment (in order).
910df8bae1dSRodney W. Grimes 	 */
911df8bae1dSRodney W. Grimes 	m = m0;
912df8bae1dSRodney W. Grimes 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
913df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hlen + firstlen;
914df8bae1dSRodney W. Grimes 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
915df8bae1dSRodney W. Grimes 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
916df8bae1dSRodney W. Grimes 	ip->ip_sum = 0;
917db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_IP) {
9189c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
9199c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
9209c9137eaSGarrett Wollman 		} else {
921df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
9229c9137eaSGarrett Wollman 		}
923db4f9cc7SJonathan Lemon 	}
924df8bae1dSRodney W. Grimes sendorfree:
925df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m0) {
926df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
927df8bae1dSRodney W. Grimes 		m->m_nextpkt = 0;
928df8bae1dSRodney W. Grimes 		if (error == 0)
929df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
930df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
931df8bae1dSRodney W. Grimes 		else
932df8bae1dSRodney W. Grimes 			m_freem(m);
933df8bae1dSRodney W. Grimes 	}
934df8bae1dSRodney W. Grimes 
935df8bae1dSRodney W. Grimes 	if (error == 0)
936df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
937df8bae1dSRodney W. Grimes     }
938df8bae1dSRodney W. Grimes done:
9396a800098SYoshinobu Inoue #ifdef IPSEC
9406a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
9416a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
9426a800098SYoshinobu Inoue 		ro->ro_rt = NULL;
9436a800098SYoshinobu Inoue 	}
9446a800098SYoshinobu Inoue 	if (sp != NULL) {
9456a800098SYoshinobu Inoue 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
9466a800098SYoshinobu Inoue 			printf("DP ip_output call free SP:%p\n", sp));
9476a800098SYoshinobu Inoue 		key_freesp(sp);
9486a800098SYoshinobu Inoue 	}
9496a800098SYoshinobu Inoue #endif /* IPSEC */
950df8bae1dSRodney W. Grimes 	return (error);
951df8bae1dSRodney W. Grimes bad:
952df8bae1dSRodney W. Grimes 	m_freem(m0);
953df8bae1dSRodney W. Grimes 	goto done;
954df8bae1dSRodney W. Grimes }
955df8bae1dSRodney W. Grimes 
9561c238475SJonathan Lemon void
957db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
958db4f9cc7SJonathan Lemon {
959db4f9cc7SJonathan Lemon 	struct ip *ip;
960db4f9cc7SJonathan Lemon 	u_short csum, offset;
961db4f9cc7SJonathan Lemon 
962db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
963db4f9cc7SJonathan Lemon 	offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
964db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
965db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
966db4f9cc7SJonathan Lemon 
967db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
968db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
969db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
970db4f9cc7SJonathan Lemon 		/*
971db4f9cc7SJonathan Lemon 		 * XXX
972db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
973db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
974db4f9cc7SJonathan Lemon 		 * in the existing chain instead of rearranging it.
975db4f9cc7SJonathan Lemon 		 */
976db4f9cc7SJonathan Lemon 		m = m_pullup(m, offset + sizeof(u_short));
977db4f9cc7SJonathan Lemon 	}
978db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
979db4f9cc7SJonathan Lemon }
980db4f9cc7SJonathan Lemon 
981df8bae1dSRodney W. Grimes /*
982df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
983df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
984df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
985072b9b24SPaul Traina  *
986072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
987df8bae1dSRodney W. Grimes  */
988df8bae1dSRodney W. Grimes static struct mbuf *
989df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen)
990df8bae1dSRodney W. Grimes 	register struct mbuf *m;
991df8bae1dSRodney W. Grimes 	struct mbuf *opt;
992df8bae1dSRodney W. Grimes 	int *phlen;
993df8bae1dSRodney W. Grimes {
994df8bae1dSRodney W. Grimes 	register struct ipoption *p = mtod(opt, struct ipoption *);
995df8bae1dSRodney W. Grimes 	struct mbuf *n;
996df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
997df8bae1dSRodney W. Grimes 	unsigned optlen;
998df8bae1dSRodney W. Grimes 
999df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
1000df8bae1dSRodney W. Grimes 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
1001df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
1002df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
1003df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
1004df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1005df8bae1dSRodney W. Grimes 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1006df8bae1dSRodney W. Grimes 		if (n == 0)
1007df8bae1dSRodney W. Grimes 			return (m);
10085db1e34eSRuslan Ermilov 		n->m_pkthdr.rcvif = (struct ifnet *)0;
1009df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1010df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
1011df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
1012df8bae1dSRodney W. Grimes 		n->m_next = m;
1013df8bae1dSRodney W. Grimes 		m = n;
1014df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
1015df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
101694a5d9b6SDavid Greenman 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1017df8bae1dSRodney W. Grimes 	} else {
1018df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
1019df8bae1dSRodney W. Grimes 		m->m_len += optlen;
1020df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
1021df8bae1dSRodney W. Grimes 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1022df8bae1dSRodney W. Grimes 	}
1023df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
10240453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
1025df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
10269c9137eaSGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1027df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
1028df8bae1dSRodney W. Grimes 	return (m);
1029df8bae1dSRodney W. Grimes }
1030df8bae1dSRodney W. Grimes 
1031df8bae1dSRodney W. Grimes /*
1032df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
1033df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
1034df8bae1dSRodney W. Grimes  */
1035beec8214SDarren Reed int
1036df8bae1dSRodney W. Grimes ip_optcopy(ip, jp)
1037df8bae1dSRodney W. Grimes 	struct ip *ip, *jp;
1038df8bae1dSRodney W. Grimes {
1039df8bae1dSRodney W. Grimes 	register u_char *cp, *dp;
1040df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
1041df8bae1dSRodney W. Grimes 
1042df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
1043df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
10449c9137eaSGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1045df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1046df8bae1dSRodney W. Grimes 		opt = cp[0];
1047df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1048df8bae1dSRodney W. Grimes 			break;
1049df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
1050df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1051df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
1052df8bae1dSRodney W. Grimes 			optlen = 1;
1053df8bae1dSRodney W. Grimes 			continue;
1054df8bae1dSRodney W. Grimes 		} else
1055df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1056df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
1057df8bae1dSRodney W. Grimes 		if (optlen > cnt)
1058df8bae1dSRodney W. Grimes 			optlen = cnt;
1059df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
10600453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
1061df8bae1dSRodney W. Grimes 			dp += optlen;
1062df8bae1dSRodney W. Grimes 		}
1063df8bae1dSRodney W. Grimes 	}
1064df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1065df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
1066df8bae1dSRodney W. Grimes 	return (optlen);
1067df8bae1dSRodney W. Grimes }
1068df8bae1dSRodney W. Grimes 
1069df8bae1dSRodney W. Grimes /*
1070df8bae1dSRodney W. Grimes  * IP socket option processing.
1071df8bae1dSRodney W. Grimes  */
1072df8bae1dSRodney W. Grimes int
1073cfe8b629SGarrett Wollman ip_ctloutput(so, sopt)
1074df8bae1dSRodney W. Grimes 	struct socket *so;
1075cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1076df8bae1dSRodney W. Grimes {
1077cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
1078cfe8b629SGarrett Wollman 	int	error, optval;
1079df8bae1dSRodney W. Grimes 
1080cfe8b629SGarrett Wollman 	error = optval = 0;
1081cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
1082cfe8b629SGarrett Wollman 		return (EINVAL);
1083cfe8b629SGarrett Wollman 	}
1084df8bae1dSRodney W. Grimes 
1085cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
1086cfe8b629SGarrett Wollman 	case SOPT_SET:
1087cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1088df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1089df8bae1dSRodney W. Grimes #ifdef notyet
1090df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1091df8bae1dSRodney W. Grimes #endif
1092cfe8b629SGarrett Wollman 		{
1093cfe8b629SGarrett Wollman 			struct mbuf *m;
1094cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
1095cfe8b629SGarrett Wollman 				error = EMSGSIZE;
1096cfe8b629SGarrett Wollman 				break;
1097cfe8b629SGarrett Wollman 			}
1098cfe8b629SGarrett Wollman 			MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_HEADER);
1099cfe8b629SGarrett Wollman 			if (m == 0) {
1100cfe8b629SGarrett Wollman 				error = ENOBUFS;
1101cfe8b629SGarrett Wollman 				break;
1102cfe8b629SGarrett Wollman 			}
1103cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
1104cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1105cfe8b629SGarrett Wollman 					    m->m_len);
1106cfe8b629SGarrett Wollman 
1107cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1108cfe8b629SGarrett Wollman 					   m));
1109cfe8b629SGarrett Wollman 		}
1110df8bae1dSRodney W. Grimes 
1111df8bae1dSRodney W. Grimes 		case IP_TOS:
1112df8bae1dSRodney W. Grimes 		case IP_TTL:
1113df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1114df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1115df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
111682c23ebaSBill Fenner 		case IP_RECVIF:
11176a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
11186a800098SYoshinobu Inoue 		case IP_FAITH:
11196a800098SYoshinobu Inoue #endif
1120cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1121cfe8b629SGarrett Wollman 					    sizeof optval);
1122cfe8b629SGarrett Wollman 			if (error)
1123cfe8b629SGarrett Wollman 				break;
1124df8bae1dSRodney W. Grimes 
1125cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1126df8bae1dSRodney W. Grimes 			case IP_TOS:
1127ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
1128df8bae1dSRodney W. Grimes 				break;
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes 			case IP_TTL:
1131ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
1132df8bae1dSRodney W. Grimes 				break;
1133df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
1134df8bae1dSRodney W. Grimes 	if (optval) \
1135df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
1136df8bae1dSRodney W. Grimes 	else \
1137df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1140df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
1141df8bae1dSRodney W. Grimes 				break;
1142df8bae1dSRodney W. Grimes 
1143df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1144df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
1145df8bae1dSRodney W. Grimes 				break;
1146df8bae1dSRodney W. Grimes 
1147df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1148df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
1149df8bae1dSRodney W. Grimes 				break;
115082c23ebaSBill Fenner 
115182c23ebaSBill Fenner 			case IP_RECVIF:
115282c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
115382c23ebaSBill Fenner 				break;
11546a800098SYoshinobu Inoue 
11556a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
11566a800098SYoshinobu Inoue 			case IP_FAITH:
11576a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
11586a800098SYoshinobu Inoue 				break;
11596a800098SYoshinobu Inoue #endif
1160df8bae1dSRodney W. Grimes 			}
1161df8bae1dSRodney W. Grimes 			break;
1162df8bae1dSRodney W. Grimes #undef OPTSET
1163df8bae1dSRodney W. Grimes 
1164df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1165f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1166df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1167df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1168df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1169df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1170cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1171df8bae1dSRodney W. Grimes 			break;
1172df8bae1dSRodney W. Grimes 
117333b3ac06SPeter Wemm 		case IP_PORTRANGE:
1174cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1175cfe8b629SGarrett Wollman 					    sizeof optval);
1176cfe8b629SGarrett Wollman 			if (error)
1177cfe8b629SGarrett Wollman 				break;
117833b3ac06SPeter Wemm 
117933b3ac06SPeter Wemm 			switch (optval) {
118033b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
118133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
118233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
118333b3ac06SPeter Wemm 				break;
118433b3ac06SPeter Wemm 
118533b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
118633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
118733b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
118833b3ac06SPeter Wemm 				break;
118933b3ac06SPeter Wemm 
119033b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
119133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
119233b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
119333b3ac06SPeter Wemm 				break;
119433b3ac06SPeter Wemm 
119533b3ac06SPeter Wemm 			default:
119633b3ac06SPeter Wemm 				error = EINVAL;
119733b3ac06SPeter Wemm 				break;
119833b3ac06SPeter Wemm 			}
1199ce8c72b1SPeter Wemm 			break;
120033b3ac06SPeter Wemm 
12016a800098SYoshinobu Inoue #ifdef IPSEC
12026a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
12036a800098SYoshinobu Inoue 		{
12046a800098SYoshinobu Inoue 			caddr_t req;
12056a800098SYoshinobu Inoue 			int priv;
12066a800098SYoshinobu Inoue 			struct mbuf *m;
12076a800098SYoshinobu Inoue 			int optname;
12086a800098SYoshinobu Inoue 
12096a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
12106a800098SYoshinobu Inoue 				break;
12116a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
12126a800098SYoshinobu Inoue 				break;
12136a800098SYoshinobu Inoue 			priv = (sopt->sopt_p != NULL &&
12146a800098SYoshinobu Inoue 				suser(sopt->sopt_p) != 0) ? 0 : 1;
12156a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
12166a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
12176a800098SYoshinobu Inoue 			error = ipsec4_set_policy(inp, optname, req, priv);
12186a800098SYoshinobu Inoue 			m_freem(m);
12196a800098SYoshinobu Inoue 			break;
12206a800098SYoshinobu Inoue 		}
12216a800098SYoshinobu Inoue #endif /*IPSEC*/
12226a800098SYoshinobu Inoue 
1223df8bae1dSRodney W. Grimes 		default:
1224df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1225df8bae1dSRodney W. Grimes 			break;
1226df8bae1dSRodney W. Grimes 		}
1227df8bae1dSRodney W. Grimes 		break;
1228df8bae1dSRodney W. Grimes 
1229cfe8b629SGarrett Wollman 	case SOPT_GET:
1230cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1231df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1232df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1233cfe8b629SGarrett Wollman 			if (inp->inp_options)
1234cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1235cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1236cfe8b629SGarrett Wollman 							  char *),
1237cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1238cfe8b629SGarrett Wollman 			else
1239cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1240df8bae1dSRodney W. Grimes 			break;
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes 		case IP_TOS:
1243df8bae1dSRodney W. Grimes 		case IP_TTL:
1244df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1245df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1246df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
124782c23ebaSBill Fenner 		case IP_RECVIF:
1248cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
12496a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
12506a800098SYoshinobu Inoue 		case IP_FAITH:
12516a800098SYoshinobu Inoue #endif
1252cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1253df8bae1dSRodney W. Grimes 
1254df8bae1dSRodney W. Grimes 			case IP_TOS:
1255ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1256df8bae1dSRodney W. Grimes 				break;
1257df8bae1dSRodney W. Grimes 
1258df8bae1dSRodney W. Grimes 			case IP_TTL:
1259ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1260df8bae1dSRodney W. Grimes 				break;
1261df8bae1dSRodney W. Grimes 
1262df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1263df8bae1dSRodney W. Grimes 
1264df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1265df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1266df8bae1dSRodney W. Grimes 				break;
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1269df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1270df8bae1dSRodney W. Grimes 				break;
1271df8bae1dSRodney W. Grimes 
1272df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1273df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1274df8bae1dSRodney W. Grimes 				break;
127582c23ebaSBill Fenner 
127682c23ebaSBill Fenner 			case IP_RECVIF:
127782c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
127882c23ebaSBill Fenner 				break;
1279cfe8b629SGarrett Wollman 
1280cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1281cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1282cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1283cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1284cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1285cfe8b629SGarrett Wollman 				else
1286cfe8b629SGarrett Wollman 					optval = 0;
1287cfe8b629SGarrett Wollman 				break;
12886a800098SYoshinobu Inoue 
12896a800098SYoshinobu Inoue #if defined(NFAITH) && NFAITH > 0
12906a800098SYoshinobu Inoue 			case IP_FAITH:
12916a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
12926a800098SYoshinobu Inoue 				break;
12936a800098SYoshinobu Inoue #endif
1294df8bae1dSRodney W. Grimes 			}
1295cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1296df8bae1dSRodney W. Grimes 			break;
1297df8bae1dSRodney W. Grimes 
1298df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1299f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1300df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1301df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1302df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1303df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1304cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
130533b3ac06SPeter Wemm 			break;
130633b3ac06SPeter Wemm 
13076a800098SYoshinobu Inoue #ifdef IPSEC
13086a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
13096a800098SYoshinobu Inoue 		{
1310f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
13116a800098SYoshinobu Inoue 			caddr_t req = NULL;
13126a800098SYoshinobu Inoue 
13136a800098SYoshinobu Inoue 			if (m != 0)
13146a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
13156a800098SYoshinobu Inoue 			error = ipsec4_get_policy(sotoinpcb(so), req, &m);
13166a800098SYoshinobu Inoue 			if (error == 0)
13176a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1318f63e7634SYoshinobu Inoue 			if (error == 0)
13196a800098SYoshinobu Inoue 				m_freem(m);
13206a800098SYoshinobu Inoue 			break;
13216a800098SYoshinobu Inoue 		}
13226a800098SYoshinobu Inoue #endif /*IPSEC*/
13236a800098SYoshinobu Inoue 
1324df8bae1dSRodney W. Grimes 		default:
1325df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1326df8bae1dSRodney W. Grimes 			break;
1327df8bae1dSRodney W. Grimes 		}
1328df8bae1dSRodney W. Grimes 		break;
1329df8bae1dSRodney W. Grimes 	}
1330df8bae1dSRodney W. Grimes 	return (error);
1331df8bae1dSRodney W. Grimes }
1332df8bae1dSRodney W. Grimes 
1333df8bae1dSRodney W. Grimes /*
1334df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1335df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1336df8bae1dSRodney W. Grimes  * with destination address if source routed.
1337df8bae1dSRodney W. Grimes  */
13380312fbe9SPoul-Henning Kamp static int
1339df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m)
1340df8bae1dSRodney W. Grimes 	int optname;
1341df8bae1dSRodney W. Grimes 	struct mbuf **pcbopt;
1342df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1343df8bae1dSRodney W. Grimes {
1344d68fa50cSBruce Evans 	register int cnt, optlen;
1345df8bae1dSRodney W. Grimes 	register u_char *cp;
1346df8bae1dSRodney W. Grimes 	u_char opt;
1347df8bae1dSRodney W. Grimes 
1348df8bae1dSRodney W. Grimes 	/* turn off any old options */
1349df8bae1dSRodney W. Grimes 	if (*pcbopt)
1350df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1351df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1352df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1353df8bae1dSRodney W. Grimes 		/*
1354df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1355df8bae1dSRodney W. Grimes 		 */
1356df8bae1dSRodney W. Grimes 		if (m)
1357df8bae1dSRodney W. Grimes 			(void)m_free(m);
1358df8bae1dSRodney W. Grimes 		return (0);
1359df8bae1dSRodney W. Grimes 	}
1360df8bae1dSRodney W. Grimes 
1361df8bae1dSRodney W. Grimes #ifndef	vax
13620c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1363df8bae1dSRodney W. Grimes 		goto bad;
1364df8bae1dSRodney W. Grimes #endif
1365df8bae1dSRodney W. Grimes 	/*
1366df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1367df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1368df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1369df8bae1dSRodney W. Grimes 	 */
1370df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1371df8bae1dSRodney W. Grimes 		goto bad;
1372df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1373df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1374df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1375df8bae1dSRodney W. Grimes 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1376df8bae1dSRodney W. Grimes 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1377df8bae1dSRodney W. Grimes 
1378df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1379df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1380df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1381df8bae1dSRodney W. Grimes 			break;
1382df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1383df8bae1dSRodney W. Grimes 			optlen = 1;
1384df8bae1dSRodney W. Grimes 		else {
1385df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1386df8bae1dSRodney W. Grimes 			if (optlen <= IPOPT_OLEN || optlen > cnt)
1387df8bae1dSRodney W. Grimes 				goto bad;
1388df8bae1dSRodney W. Grimes 		}
1389df8bae1dSRodney W. Grimes 		switch (opt) {
1390df8bae1dSRodney W. Grimes 
1391df8bae1dSRodney W. Grimes 		default:
1392df8bae1dSRodney W. Grimes 			break;
1393df8bae1dSRodney W. Grimes 
1394df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1395df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1396df8bae1dSRodney W. Grimes 			/*
1397df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1398df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1399df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1400df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1401df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1402df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1403df8bae1dSRodney W. Grimes 			 */
1404df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1405df8bae1dSRodney W. Grimes 				goto bad;
1406df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1407df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1408df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1409df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1410df8bae1dSRodney W. Grimes 			/*
1411df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1412df8bae1dSRodney W. Grimes 			 */
1413df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1414df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1415df8bae1dSRodney W. Grimes 			/*
1416df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1417df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1418df8bae1dSRodney W. Grimes 			 */
1419df8bae1dSRodney W. Grimes 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1420df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr)),
1421df8bae1dSRodney W. Grimes 			    (caddr_t)&cp[IPOPT_OFFSET+1],
1422df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
1423df8bae1dSRodney W. Grimes 			break;
1424df8bae1dSRodney W. Grimes 		}
1425df8bae1dSRodney W. Grimes 	}
1426df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1427df8bae1dSRodney W. Grimes 		goto bad;
1428df8bae1dSRodney W. Grimes 	*pcbopt = m;
1429df8bae1dSRodney W. Grimes 	return (0);
1430df8bae1dSRodney W. Grimes 
1431df8bae1dSRodney W. Grimes bad:
1432df8bae1dSRodney W. Grimes 	(void)m_free(m);
1433df8bae1dSRodney W. Grimes 	return (EINVAL);
1434df8bae1dSRodney W. Grimes }
1435df8bae1dSRodney W. Grimes 
1436df8bae1dSRodney W. Grimes /*
1437cfe8b629SGarrett Wollman  * XXX
1438cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1439cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1440cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1441cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1442cfe8b629SGarrett Wollman  */
1443cfe8b629SGarrett Wollman /*
1444df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1445df8bae1dSRodney W. Grimes  */
14460312fbe9SPoul-Henning Kamp static int
1447cfe8b629SGarrett Wollman ip_setmoptions(sopt, imop)
1448cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1449df8bae1dSRodney W. Grimes 	struct ip_moptions **imop;
1450df8bae1dSRodney W. Grimes {
1451cfe8b629SGarrett Wollman 	int error = 0;
1452cfe8b629SGarrett Wollman 	int i;
1453df8bae1dSRodney W. Grimes 	struct in_addr addr;
1454cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1455cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1456cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1457df8bae1dSRodney W. Grimes 	struct route ro;
1458cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
14599b626c29SGarrett Wollman 	int s;
1460df8bae1dSRodney W. Grimes 
1461df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1462df8bae1dSRodney W. Grimes 		/*
1463df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1464df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1465df8bae1dSRodney W. Grimes 		 */
1466df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1467df8bae1dSRodney W. Grimes 		    M_WAITOK);
1468df8bae1dSRodney W. Grimes 
1469df8bae1dSRodney W. Grimes 		if (imo == NULL)
1470df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1471df8bae1dSRodney W. Grimes 		*imop = imo;
1472df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
14731c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1474df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1475df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1476df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1477df8bae1dSRodney W. Grimes 	}
1478df8bae1dSRodney W. Grimes 
1479cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1480f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1481f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1482cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
14835e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
14845e9ae478SGarrett Wollman 			break;
14855e9ae478SGarrett Wollman 		}
1486cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1487cfe8b629SGarrett Wollman 		if (error)
1488f0068c4aSGarrett Wollman 			break;
14891c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1490f0068c4aSGarrett Wollman 			error = EINVAL;
1491f0068c4aSGarrett Wollman 			break;
1492f0068c4aSGarrett Wollman 		}
1493f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1494f0068c4aSGarrett Wollman 		break;
1495f0068c4aSGarrett Wollman 
1496df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1497df8bae1dSRodney W. Grimes 		/*
1498df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1499df8bae1dSRodney W. Grimes 		 */
1500cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1501cfe8b629SGarrett Wollman 		if (error)
1502df8bae1dSRodney W. Grimes 			break;
1503df8bae1dSRodney W. Grimes 		/*
1504df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1505df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1506df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1507df8bae1dSRodney W. Grimes 		 */
1508df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1509df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1510df8bae1dSRodney W. Grimes 			break;
1511df8bae1dSRodney W. Grimes 		}
1512df8bae1dSRodney W. Grimes 		/*
1513df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1514df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1515df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1516df8bae1dSRodney W. Grimes 		 */
151720e8807cSGarrett Wollman 		s = splimp();
1518df8bae1dSRodney W. Grimes 		INADDR_TO_IFP(addr, ifp);
1519df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1520fbc6ab00SBill Fenner 			splx(s);
1521df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1522df8bae1dSRodney W. Grimes 			break;
1523df8bae1dSRodney W. Grimes 		}
1524df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
15259b626c29SGarrett Wollman 		splx(s);
1526df8bae1dSRodney W. Grimes 		break;
1527df8bae1dSRodney W. Grimes 
1528df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1529df8bae1dSRodney W. Grimes 		/*
1530df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1531cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1532cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1533cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1534df8bae1dSRodney W. Grimes 		 */
1535cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1536cfe8b629SGarrett Wollman 			u_char ttl;
1537cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1538cfe8b629SGarrett Wollman 			if (error)
1539df8bae1dSRodney W. Grimes 				break;
1540cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1541cfe8b629SGarrett Wollman 		} else {
1542cfe8b629SGarrett Wollman 			u_int ttl;
1543cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1544cfe8b629SGarrett Wollman 					    sizeof ttl);
1545cfe8b629SGarrett Wollman 			if (error)
1546cfe8b629SGarrett Wollman 				break;
1547cfe8b629SGarrett Wollman 			if (ttl > 255)
1548cfe8b629SGarrett Wollman 				error = EINVAL;
1549cfe8b629SGarrett Wollman 			else
1550cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1551df8bae1dSRodney W. Grimes 		}
1552df8bae1dSRodney W. Grimes 		break;
1553df8bae1dSRodney W. Grimes 
1554df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1555df8bae1dSRodney W. Grimes 		/*
1556df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1557cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1558cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1559cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1560df8bae1dSRodney W. Grimes 		 */
1561cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1562cfe8b629SGarrett Wollman 			u_char loop;
1563cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1564cfe8b629SGarrett Wollman 			if (error)
1565df8bae1dSRodney W. Grimes 				break;
1566cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1567cfe8b629SGarrett Wollman 		} else {
1568cfe8b629SGarrett Wollman 			u_int loop;
1569cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1570cfe8b629SGarrett Wollman 					    sizeof loop);
1571cfe8b629SGarrett Wollman 			if (error)
1572cfe8b629SGarrett Wollman 				break;
1573cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1574df8bae1dSRodney W. Grimes 		}
1575df8bae1dSRodney W. Grimes 		break;
1576df8bae1dSRodney W. Grimes 
1577df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1578df8bae1dSRodney W. Grimes 		/*
1579df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1580df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1581df8bae1dSRodney W. Grimes 		 */
1582cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1583cfe8b629SGarrett Wollman 		if (error)
1584df8bae1dSRodney W. Grimes 			break;
1585cfe8b629SGarrett Wollman 
1586cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1587df8bae1dSRodney W. Grimes 			error = EINVAL;
1588df8bae1dSRodney W. Grimes 			break;
1589df8bae1dSRodney W. Grimes 		}
159020e8807cSGarrett Wollman 		s = splimp();
1591df8bae1dSRodney W. Grimes 		/*
1592df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1593df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1594df8bae1dSRodney W. Grimes 		 */
1595cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
15961c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1597df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1598df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1599df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1600cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
1601df8bae1dSRodney W. Grimes 			rtalloc(&ro);
1602df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1603df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
16049b626c29SGarrett Wollman 				splx(s);
1605df8bae1dSRodney W. Grimes 				break;
1606df8bae1dSRodney W. Grimes 			}
1607df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1608df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
1609df8bae1dSRodney W. Grimes 		}
1610df8bae1dSRodney W. Grimes 		else {
1611cfe8b629SGarrett Wollman 			INADDR_TO_IFP(mreq.imr_interface, ifp);
1612df8bae1dSRodney W. Grimes 		}
16139b626c29SGarrett Wollman 
1614df8bae1dSRodney W. Grimes 		/*
1615df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1616df8bae1dSRodney W. Grimes 		 * supports multicast.
1617df8bae1dSRodney W. Grimes 		 */
1618df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1619df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
16209b626c29SGarrett Wollman 			splx(s);
1621df8bae1dSRodney W. Grimes 			break;
1622df8bae1dSRodney W. Grimes 		}
1623df8bae1dSRodney W. Grimes 		/*
1624df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1625df8bae1dSRodney W. Grimes 		 * membership slots are full.
1626df8bae1dSRodney W. Grimes 		 */
1627df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1628df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1629df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1630cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1631df8bae1dSRodney W. Grimes 				break;
1632df8bae1dSRodney W. Grimes 		}
1633df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1634df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
16359b626c29SGarrett Wollman 			splx(s);
1636df8bae1dSRodney W. Grimes 			break;
1637df8bae1dSRodney W. Grimes 		}
1638df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1639df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
16409b626c29SGarrett Wollman 			splx(s);
1641df8bae1dSRodney W. Grimes 			break;
1642df8bae1dSRodney W. Grimes 		}
1643df8bae1dSRodney W. Grimes 		/*
1644df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1645df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1646df8bae1dSRodney W. Grimes 		 */
1647df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1648cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1649df8bae1dSRodney W. Grimes 			error = ENOBUFS;
16509b626c29SGarrett Wollman 			splx(s);
1651df8bae1dSRodney W. Grimes 			break;
1652df8bae1dSRodney W. Grimes 		}
1653df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
16549b626c29SGarrett Wollman 		splx(s);
1655df8bae1dSRodney W. Grimes 		break;
1656df8bae1dSRodney W. Grimes 
1657df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1658df8bae1dSRodney W. Grimes 		/*
1659df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1660df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1661df8bae1dSRodney W. Grimes 		 */
1662cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1663cfe8b629SGarrett Wollman 		if (error)
1664df8bae1dSRodney W. Grimes 			break;
1665cfe8b629SGarrett Wollman 
1666cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1667df8bae1dSRodney W. Grimes 			error = EINVAL;
1668df8bae1dSRodney W. Grimes 			break;
1669df8bae1dSRodney W. Grimes 		}
16709b626c29SGarrett Wollman 
167120e8807cSGarrett Wollman 		s = splimp();
1672df8bae1dSRodney W. Grimes 		/*
1673df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1674df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1675df8bae1dSRodney W. Grimes 		 */
1676cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1677df8bae1dSRodney W. Grimes 			ifp = NULL;
1678df8bae1dSRodney W. Grimes 		else {
1679cfe8b629SGarrett Wollman 			INADDR_TO_IFP(mreq.imr_interface, ifp);
1680df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1681df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
16829b626c29SGarrett Wollman 				splx(s);
1683df8bae1dSRodney W. Grimes 				break;
1684df8bae1dSRodney W. Grimes 			}
1685df8bae1dSRodney W. Grimes 		}
1686df8bae1dSRodney W. Grimes 		/*
1687df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1688df8bae1dSRodney W. Grimes 		 */
1689df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1690df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1691df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1692df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1693cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1694df8bae1dSRodney W. Grimes 				break;
1695df8bae1dSRodney W. Grimes 		}
1696df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1697df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
16989b626c29SGarrett Wollman 			splx(s);
1699df8bae1dSRodney W. Grimes 			break;
1700df8bae1dSRodney W. Grimes 		}
1701df8bae1dSRodney W. Grimes 		/*
1702df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1703df8bae1dSRodney W. Grimes 		 * membership points.
1704df8bae1dSRodney W. Grimes 		 */
1705df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1706df8bae1dSRodney W. Grimes 		/*
1707df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1708df8bae1dSRodney W. Grimes 		 */
1709df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1710df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1711df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
17129b626c29SGarrett Wollman 		splx(s);
1713df8bae1dSRodney W. Grimes 		break;
1714df8bae1dSRodney W. Grimes 
1715df8bae1dSRodney W. Grimes 	default:
1716df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1717df8bae1dSRodney W. Grimes 		break;
1718df8bae1dSRodney W. Grimes 	}
1719df8bae1dSRodney W. Grimes 
1720df8bae1dSRodney W. Grimes 	/*
1721df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1722df8bae1dSRodney W. Grimes 	 */
1723df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
17241c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1725df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1726df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1727df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1728df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1729df8bae1dSRodney W. Grimes 		*imop = NULL;
1730df8bae1dSRodney W. Grimes 	}
1731df8bae1dSRodney W. Grimes 
1732df8bae1dSRodney W. Grimes 	return (error);
1733df8bae1dSRodney W. Grimes }
1734df8bae1dSRodney W. Grimes 
1735df8bae1dSRodney W. Grimes /*
1736df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1737df8bae1dSRodney W. Grimes  */
17380312fbe9SPoul-Henning Kamp static int
1739cfe8b629SGarrett Wollman ip_getmoptions(sopt, imo)
1740cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1741df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1742df8bae1dSRodney W. Grimes {
1743cfe8b629SGarrett Wollman 	struct in_addr addr;
1744df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1745cfe8b629SGarrett Wollman 	int error, optval;
1746cfe8b629SGarrett Wollman 	u_char coptval;
1747df8bae1dSRodney W. Grimes 
1748cfe8b629SGarrett Wollman 	error = 0;
1749cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1750f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1751f0068c4aSGarrett Wollman 		if (imo != NULL)
1752cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1753f0068c4aSGarrett Wollman 		else
1754cfe8b629SGarrett Wollman 			optval = -1;
1755cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1756cfe8b629SGarrett Wollman 		break;
1757f0068c4aSGarrett Wollman 
1758df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1759df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1760cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
1761df8bae1dSRodney W. Grimes 		else {
1762df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1763cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1764df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1765df8bae1dSRodney W. Grimes 		}
1766cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1767cfe8b629SGarrett Wollman 		break;
1768df8bae1dSRodney W. Grimes 
1769df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1770cfe8b629SGarrett Wollman 		if (imo == 0)
1771cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1772cfe8b629SGarrett Wollman 		else
1773cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
1774cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1775cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1776cfe8b629SGarrett Wollman 		else
1777cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1778cfe8b629SGarrett Wollman 		break;
1779df8bae1dSRodney W. Grimes 
1780df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1781cfe8b629SGarrett Wollman 		if (imo == 0)
1782cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1783cfe8b629SGarrett Wollman 		else
1784cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
1785cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1786cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1787cfe8b629SGarrett Wollman 		else
1788cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1789cfe8b629SGarrett Wollman 		break;
1790df8bae1dSRodney W. Grimes 
1791df8bae1dSRodney W. Grimes 	default:
1792cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1793cfe8b629SGarrett Wollman 		break;
1794df8bae1dSRodney W. Grimes 	}
1795cfe8b629SGarrett Wollman 	return (error);
1796df8bae1dSRodney W. Grimes }
1797df8bae1dSRodney W. Grimes 
1798df8bae1dSRodney W. Grimes /*
1799df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1800df8bae1dSRodney W. Grimes  */
1801df8bae1dSRodney W. Grimes void
1802df8bae1dSRodney W. Grimes ip_freemoptions(imo)
1803df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1804df8bae1dSRodney W. Grimes {
1805df8bae1dSRodney W. Grimes 	register int i;
1806df8bae1dSRodney W. Grimes 
1807df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1808df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1809df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1810df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1811df8bae1dSRodney W. Grimes 	}
1812df8bae1dSRodney W. Grimes }
1813df8bae1dSRodney W. Grimes 
1814df8bae1dSRodney W. Grimes /*
1815df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1816df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1817df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1818f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1819f5fea3ddSPaul Traina  * replicating that code here.
1820df8bae1dSRodney W. Grimes  */
1821df8bae1dSRodney W. Grimes static void
182286b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen)
1823df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1824df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1825df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
182686b1d6d2SBill Fenner 	int hlen;
1827df8bae1dSRodney W. Grimes {
1828df8bae1dSRodney W. Grimes 	register struct ip *ip;
1829df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1830df8bae1dSRodney W. Grimes 
1831df8bae1dSRodney W. Grimes 	copym = m_copy(m, 0, M_COPYALL);
183286b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
183386b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1834df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1835df8bae1dSRodney W. Grimes 		/*
1836df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1837df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1838df8bae1dSRodney W. Grimes 		 */
1839df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1840df8bae1dSRodney W. Grimes 		ip->ip_len = htons((u_short)ip->ip_len);
1841df8bae1dSRodney W. Grimes 		ip->ip_off = htons((u_short)ip->ip_off);
1842df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
18439c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
18449c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
18459c9137eaSGarrett Wollman 		} else {
184686b1d6d2SBill Fenner 			ip->ip_sum = in_cksum(copym, hlen);
18479c9137eaSGarrett Wollman 		}
18489c9137eaSGarrett Wollman 		/*
18499c9137eaSGarrett Wollman 		 * NB:
1850e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1851e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1852e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1853e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1854e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1855e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1856e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1857ed7509acSJulian Elischer 		 * of safety by using if_simloop().
18589c9137eaSGarrett Wollman 		 */
1859201c2527SJulian Elischer #if 1 /* XXX */
1860201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
18612b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1862201c2527SJulian Elischer 						dst->sin_family);
1863201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1864201c2527SJulian Elischer 		}
1865201c2527SJulian Elischer #endif
1866201c2527SJulian Elischer 
18679c9137eaSGarrett Wollman #ifdef notdef
1868e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
1869ed7509acSJulian Elischer 		ip_input(copym);
18709c9137eaSGarrett Wollman #else
1871ed7509acSJulian Elischer 		if_simloop(ifp, copym, (struct sockaddr *)dst, 0);
18729c9137eaSGarrett Wollman #endif
1873df8bae1dSRodney W. Grimes 	}
1874df8bae1dSRodney W. Grimes }
1875