xref: /freebsd/sys/netinet/ip_output.c (revision f0f6d6434dae6f048d18395b2067ce09fc8b804b)
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
34f0f6d643SLuigi Rizzo  *	$Id: ip_output.c,v 1.84 1998/12/14 18:09:13 luigi Exp $
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"
43fbd1372aSJoerg Wunsch 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
4526f9a767SRodney W. Grimes #include <sys/systm.h>
46f0f6d643SLuigi Rizzo #include <sys/kernel.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49df8bae1dSRodney W. Grimes #include <sys/protosw.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52df8bae1dSRodney W. Grimes 
53df8bae1dSRodney W. Grimes #include <net/if.h>
54df8bae1dSRodney W. Grimes #include <net/route.h>
55df8bae1dSRodney W. Grimes 
56df8bae1dSRodney W. Grimes #include <netinet/in.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
58df8bae1dSRodney W. Grimes #include <netinet/ip.h>
59df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
60df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #ifdef vax
64df8bae1dSRodney W. Grimes #include <machine/mtpr.h>
65df8bae1dSRodney W. Grimes #endif
669c9137eaSGarrett Wollman #include <machine/in_cksum.h>
67df8bae1dSRodney W. Grimes 
68a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
6955166637SPoul-Henning Kamp 
70f9493383SGarrett Wollman #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
71f9493383SGarrett Wollman #undef COMPAT_IPFW
72f9493383SGarrett Wollman #define COMPAT_IPFW 1
73f9493383SGarrett Wollman #else
74f9493383SGarrett Wollman #undef COMPAT_IPFW
75f9493383SGarrett Wollman #endif
76f9493383SGarrett Wollman 
77cfe8b629SGarrett Wollman #ifdef COMPAT_IPFW
78cfe8b629SGarrett Wollman #include <netinet/ip_fw.h>
79cfe8b629SGarrett Wollman #endif
80cfe8b629SGarrett Wollman 
81b715f178SLuigi Rizzo #ifdef DUMMYNET
82b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
83b715f178SLuigi Rizzo #endif
84b715f178SLuigi Rizzo 
85f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD_DEBUG
86f9e354dfSJulian Elischer #define print_ip(a)	 printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
87f9e354dfSJulian Elischer 				 		  (ntohl(a.s_addr)>>16)&0xFF,\
88f9e354dfSJulian Elischer 						  (ntohl(a.s_addr)>>8)&0xFF,\
89f9e354dfSJulian Elischer 						  (ntohl(a.s_addr))&0xFF);
90f9e354dfSJulian Elischer #endif
91f9e354dfSJulian Elischer 
92f23b4c91SGarrett Wollman u_short ip_id;
93f23b4c91SGarrett Wollman 
94df8bae1dSRodney W. Grimes static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
95df8bae1dSRodney W. Grimes static void	ip_mloopback
9686b1d6d2SBill Fenner 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
970312fbe9SPoul-Henning Kamp static int	ip_getmoptions
98cfe8b629SGarrett Wollman 	__P((struct sockopt *, struct ip_moptions *));
99cfe8b629SGarrett Wollman static int	ip_pcbopts __P((int, struct mbuf **, struct mbuf *));
1000312fbe9SPoul-Henning Kamp static int	ip_setmoptions
101cfe8b629SGarrett Wollman 	__P((struct sockopt *, struct ip_moptions **));
102df8bae1dSRodney W. Grimes 
103afed1b49SDarren Reed #if defined(IPFILTER_LKM) || defined(IPFILTER)
104beec8214SDarren Reed int	ip_optcopy __P((struct ip *, struct ip *));
105afed1b49SDarren Reed extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
106beec8214SDarren Reed #else
107beec8214SDarren Reed static int	ip_optcopy __P((struct ip *, struct ip *));
108afed1b49SDarren Reed #endif
109afed1b49SDarren Reed 
110afed1b49SDarren Reed 
11193e0e116SJulian Elischer extern	struct protosw inetsw[];
11293e0e116SJulian Elischer 
113df8bae1dSRodney W. Grimes /*
114df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
115df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
116df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
117df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
118df8bae1dSRodney W. Grimes  */
119df8bae1dSRodney W. Grimes int
120df8bae1dSRodney W. Grimes ip_output(m0, opt, ro, flags, imo)
121df8bae1dSRodney W. Grimes 	struct mbuf *m0;
122df8bae1dSRodney W. Grimes 	struct mbuf *opt;
123df8bae1dSRodney W. Grimes 	struct route *ro;
124df8bae1dSRodney W. Grimes 	int flags;
125df8bae1dSRodney W. Grimes 	struct ip_moptions *imo;
126df8bae1dSRodney W. Grimes {
12723bf9953SPoul-Henning Kamp 	struct ip *ip, *mhip;
12823bf9953SPoul-Henning Kamp 	struct ifnet *ifp;
12923bf9953SPoul-Henning Kamp 	struct mbuf *m = m0;
13023bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
131df8bae1dSRodney W. Grimes 	int len, off, error = 0;
132df8bae1dSRodney W. Grimes 	struct sockaddr_in *dst;
133df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1349f9b3dc4SGarrett Wollman 	int isbroadcast;
135f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
136f9e354dfSJulian Elischer 	int fwd_rewrite_src = 0;
137f9e354dfSJulian Elischer #endif
138df8bae1dSRodney W. Grimes 
139b715f178SLuigi Rizzo #ifndef IPDIVERT /* dummy variable for the firewall code to play with */
140b715f178SLuigi Rizzo         u_short ip_divert_cookie = 0 ;
141b715f178SLuigi Rizzo #endif
142b715f178SLuigi Rizzo #ifdef COMPAT_IPFW
143b715f178SLuigi Rizzo 	struct ip_fw_chain *rule = NULL ;
144b715f178SLuigi Rizzo #endif
145b715f178SLuigi Rizzo 
146b715f178SLuigi Rizzo #if defined(IPFIREWALL) && defined(DUMMYNET)
147b715f178SLuigi Rizzo         /*
148b715f178SLuigi Rizzo          * dummynet packet are prepended a vestigial mbuf with
149b715f178SLuigi Rizzo          * m_type = MT_DUMMYNET and m_data pointing to the matching
150b715f178SLuigi Rizzo          * rule.
151b715f178SLuigi Rizzo          */
152b715f178SLuigi Rizzo         if (m->m_type == MT_DUMMYNET) {
153b715f178SLuigi Rizzo             struct mbuf *tmp_m = m ;
154b715f178SLuigi Rizzo             /*
155b715f178SLuigi Rizzo              * the packet was already tagged, so part of the
156b715f178SLuigi Rizzo              * processing was already done, and we need to go down.
157b715f178SLuigi Rizzo              * opt, flags and imo have already been used, and now
158b715f178SLuigi Rizzo              * they are used to hold ifp and hlen and NULL, respectively.
159b715f178SLuigi Rizzo              */
160b715f178SLuigi Rizzo             rule = (struct ip_fw_chain *)(m->m_data) ;
161b715f178SLuigi Rizzo             m = m->m_next ;
162b715f178SLuigi Rizzo             free(tmp_m, M_IPFW);
163b715f178SLuigi Rizzo             ip = mtod(m, struct ip *);
164b715f178SLuigi Rizzo             dst = (struct sockaddr_in *)&ro->ro_dst;
165b715f178SLuigi Rizzo             ifp = (struct ifnet *)opt;
166b715f178SLuigi Rizzo             hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
167b715f178SLuigi Rizzo             opt = NULL ;
168b715f178SLuigi Rizzo             flags = 0 ; /* XXX is this correct ? */
169b715f178SLuigi Rizzo             goto sendit;
170b715f178SLuigi Rizzo         } else
171b715f178SLuigi Rizzo             rule = NULL ;
172b715f178SLuigi Rizzo #endif
173b715f178SLuigi Rizzo 
174df8bae1dSRodney W. Grimes #ifdef	DIAGNOSTIC
175df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
176df8bae1dSRodney W. Grimes 		panic("ip_output no HDR");
1779c9137eaSGarrett Wollman 	if (!ro)
1789c9137eaSGarrett Wollman 		panic("ip_output no route, proto = %d",
1799c9137eaSGarrett Wollman 		      mtod(m, struct ip *)->ip_p);
180df8bae1dSRodney W. Grimes #endif
181df8bae1dSRodney W. Grimes 	if (opt) {
182df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
183df8bae1dSRodney W. Grimes 		hlen = len;
184df8bae1dSRodney W. Grimes 	}
185df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
186df8bae1dSRodney W. Grimes 	/*
187df8bae1dSRodney W. Grimes 	 * Fill in IP header.
188df8bae1dSRodney W. Grimes 	 */
189df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
1909c9137eaSGarrett Wollman 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
191df8bae1dSRodney W. Grimes 		ip->ip_off &= IP_DF;
192df8bae1dSRodney W. Grimes 		ip->ip_id = htons(ip_id++);
193df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
194df8bae1dSRodney W. Grimes 	} else {
1959c9137eaSGarrett Wollman 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
196df8bae1dSRodney W. Grimes 	}
1979c9137eaSGarrett Wollman 
198df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
199df8bae1dSRodney W. Grimes 	/*
200df8bae1dSRodney W. Grimes 	 * If there is a cached route,
201df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
202df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
203df8bae1dSRodney W. Grimes 	 */
204df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
205df8bae1dSRodney W. Grimes 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
206df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
207df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
208df8bae1dSRodney W. Grimes 	}
209df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
210df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
211df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
212df8bae1dSRodney W. Grimes 		dst->sin_addr = ip->ip_dst;
213df8bae1dSRodney W. Grimes 	}
214df8bae1dSRodney W. Grimes 	/*
215df8bae1dSRodney W. Grimes 	 * If routing to interface only,
216df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
217df8bae1dSRodney W. Grimes 	 */
218df8bae1dSRodney W. Grimes #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
219df8bae1dSRodney W. Grimes #define sintosa(sin)	((struct sockaddr *)(sin))
220df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
221df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
222df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
223df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
224df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
225df8bae1dSRodney W. Grimes 			goto bad;
226df8bae1dSRodney W. Grimes 		}
227df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
228df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2299f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
230df8bae1dSRodney W. Grimes 	} else {
2312c17fe93SGarrett Wollman 		/*
2322c17fe93SGarrett Wollman 		 * If this is the case, we probably don't want to allocate
2332c17fe93SGarrett Wollman 		 * a protocol-cloned route since we didn't get one from the
2342c17fe93SGarrett Wollman 		 * ULP.  This lets TCP do its thing, while not burdening
2352c17fe93SGarrett Wollman 		 * forwarding or ICMP with the overhead of cloning a route.
2362c17fe93SGarrett Wollman 		 * Of course, we still want to do any cloning requested by
2372c17fe93SGarrett Wollman 		 * the link layer, as this is probably required in all cases
2382c17fe93SGarrett Wollman 		 * for correct operation (as it is for ARP).
2392c17fe93SGarrett Wollman 		 */
240df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
2412c17fe93SGarrett Wollman 			rtalloc_ign(ro, RTF_PRCLONING);
242df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
243df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
244df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
245df8bae1dSRodney W. Grimes 			goto bad;
246df8bae1dSRodney W. Grimes 		}
247df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
248df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
249df8bae1dSRodney W. Grimes 		ro->ro_rt->rt_use++;
250df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
251df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2529f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
2539f9b3dc4SGarrett Wollman 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2549f9b3dc4SGarrett Wollman 		else
2559f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
256df8bae1dSRodney W. Grimes 	}
257df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
258df8bae1dSRodney W. Grimes 		struct in_multi *inm;
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
261df8bae1dSRodney W. Grimes 		/*
262df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
263df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
264df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
265df8bae1dSRodney W. Grimes 		 */
266df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
267df8bae1dSRodney W. Grimes 		/*
268df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
269df8bae1dSRodney W. Grimes 		 */
270df8bae1dSRodney W. Grimes 		if (imo != NULL) {
271df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
272df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL)
273df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
2741c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
2751c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
2761c5de19aSGarrett Wollman 				    ip_mcast_src(imo->imo_multicast_vif);
277df8bae1dSRodney W. Grimes 		} else
278df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
279df8bae1dSRodney W. Grimes 		/*
280df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
281df8bae1dSRodney W. Grimes 		 */
2821c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
283df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
284df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
285df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
286df8bae1dSRodney W. Grimes 				goto bad;
287df8bae1dSRodney W. Grimes 			}
2881c5de19aSGarrett Wollman 		}
289df8bae1dSRodney W. Grimes 		/*
290df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
291df8bae1dSRodney W. Grimes 		 * of outgoing interface.
292df8bae1dSRodney W. Grimes 		 */
293df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
2940abc78a6SPoul-Henning Kamp 			register struct in_ifaddr *ia1;
295df8bae1dSRodney W. Grimes 
2960abc78a6SPoul-Henning Kamp 			for (ia1 = in_ifaddrhead.tqh_first; ia1;
2970abc78a6SPoul-Henning Kamp 			     ia1 = ia1->ia_link.tqe_next)
2980abc78a6SPoul-Henning Kamp 				if (ia1->ia_ifp == ifp) {
2990abc78a6SPoul-Henning Kamp 					ip->ip_src = IA_SIN(ia1)->sin_addr;
300df8bae1dSRodney W. Grimes 					break;
301df8bae1dSRodney W. Grimes 				}
302df8bae1dSRodney W. Grimes 		}
303df8bae1dSRodney W. Grimes 
304df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
305df8bae1dSRodney W. Grimes 		if (inm != NULL &&
306df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
307df8bae1dSRodney W. Grimes 			/*
308df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
309df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
310df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
311df8bae1dSRodney W. Grimes 			 */
31286b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
313df8bae1dSRodney W. Grimes 		}
314df8bae1dSRodney W. Grimes 		else {
315df8bae1dSRodney W. Grimes 			/*
316df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
317df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
318df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
319df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
320df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
321df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
322df8bae1dSRodney W. Grimes 			 *
323df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
324df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
325df8bae1dSRodney W. Grimes 			 * if necessary.
326df8bae1dSRodney W. Grimes 			 */
327df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
328f0068c4aSGarrett Wollman 				/*
329f0068c4aSGarrett Wollman 				 * Check if rsvp daemon is running. If not, don't
330f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
331f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
332f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
333f0068c4aSGarrett Wollman 				 */
334b124e4f2SGarrett Wollman 				if (!rsvp_on)
335f0068c4aSGarrett Wollman 				  imo = NULL;
336f0068c4aSGarrett Wollman 				if (ip_mforward(ip, ifp, m, imo) != 0) {
337df8bae1dSRodney W. Grimes 					m_freem(m);
338df8bae1dSRodney W. Grimes 					goto done;
339df8bae1dSRodney W. Grimes 				}
340df8bae1dSRodney W. Grimes 			}
341df8bae1dSRodney W. Grimes 		}
3425e9ae478SGarrett Wollman 
343df8bae1dSRodney W. Grimes 		/*
344df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
345df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
346df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
347df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
348df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
349df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
350df8bae1dSRodney W. Grimes 		 */
351f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
352df8bae1dSRodney W. Grimes 			m_freem(m);
353df8bae1dSRodney W. Grimes 			goto done;
354df8bae1dSRodney W. Grimes 		}
355df8bae1dSRodney W. Grimes 
356df8bae1dSRodney W. Grimes 		goto sendit;
357df8bae1dSRodney W. Grimes 	}
358df8bae1dSRodney W. Grimes #ifndef notdef
359df8bae1dSRodney W. Grimes 	/*
360df8bae1dSRodney W. Grimes 	 * If source address not specified yet, use address
361df8bae1dSRodney W. Grimes 	 * of outgoing interface.
362df8bae1dSRodney W. Grimes 	 */
363f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
364df8bae1dSRodney W. Grimes 		ip->ip_src = IA_SIN(ia)->sin_addr;
365f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
366f9e354dfSJulian Elischer 		/* Keep note that we did this - if the firewall changes
367f9e354dfSJulian Elischer 		 * the next-hop, our interface may change, changing the
368f9e354dfSJulian Elischer 		 * default source IP. It's a shame so much effort happens
369f9e354dfSJulian Elischer 		 * twice. Oh well.
370f9e354dfSJulian Elischer 		 */
371f9e354dfSJulian Elischer 		fwd_rewrite_src++;
372f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */
373f9e354dfSJulian Elischer 	}
374f9e354dfSJulian Elischer #endif /* notdef */
375df8bae1dSRodney W. Grimes 	/*
376b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
377b5390296SDavid Greenman 	 *      the packet or packet fragments
378b5390296SDavid Greenman 	 */
379b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
380b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
381b5390296SDavid Greenman 			error = ENOBUFS;
382b5390296SDavid Greenman 			goto bad;
383b5390296SDavid Greenman 	}
384b5390296SDavid Greenman 
385b5390296SDavid Greenman 	/*
386df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
387df8bae1dSRodney W. Grimes 	 * and verify user is allowed to send
388df8bae1dSRodney W. Grimes 	 * such a packet.
389df8bae1dSRodney W. Grimes 	 */
3909f9b3dc4SGarrett Wollman 	if (isbroadcast) {
391df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
392df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
393df8bae1dSRodney W. Grimes 			goto bad;
394df8bae1dSRodney W. Grimes 		}
395df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
396df8bae1dSRodney W. Grimes 			error = EACCES;
397df8bae1dSRodney W. Grimes 			goto bad;
398df8bae1dSRodney W. Grimes 		}
399df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
400df8bae1dSRodney W. Grimes 		if ((u_short)ip->ip_len > ifp->if_mtu) {
401df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
402df8bae1dSRodney W. Grimes 			goto bad;
403df8bae1dSRodney W. Grimes 		}
404df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4059f9b3dc4SGarrett Wollman 	} else {
406df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4079f9b3dc4SGarrett Wollman 	}
408df8bae1dSRodney W. Grimes 
409f1743588SDarren Reed sendit:
410fed1c7e9SSøren Schmidt 	/*
411fed1c7e9SSøren Schmidt 	 * IpHack's section.
412fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
413e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
414fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
415fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
416fed1c7e9SSøren Schmidt 	 */
4171ee25934SPeter Wemm #if defined(IPFILTER) || defined(IPFILTER_LKM)
4181ee25934SPeter Wemm 	if (fr_checkp) {
4191ee25934SPeter Wemm 		struct  mbuf    *m1 = m;
4201ee25934SPeter Wemm 
4211ee25934SPeter Wemm 		if ((error = (*fr_checkp)(ip, hlen, ifp, 1, &m1)) || !m1)
4221ee25934SPeter Wemm 			goto done;
4231ee25934SPeter Wemm 		ip = mtod(m = m1, struct ip *);
4241ee25934SPeter Wemm 	}
4251ee25934SPeter Wemm #endif
426fed1c7e9SSøren Schmidt 
42758938916SGarrett Wollman #ifdef COMPAT_IPFW
4286713d4a7SSøren Schmidt         if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, ifp, IP_NAT_OUT)) {
429fed1c7e9SSøren Schmidt 		error = EACCES;
430fed1c7e9SSøren Schmidt 		goto done;
431fed1c7e9SSøren Schmidt 	}
432fed1c7e9SSøren Schmidt 
433df8bae1dSRodney W. Grimes 	/*
434e7319babSPoul-Henning Kamp 	 * Check with the firewall...
435e7319babSPoul-Henning Kamp 	 */
43693e0e116SJulian Elischer 	if (ip_fw_chk_ptr) {
4379de9737fSPeter Wemm 		struct sockaddr_in *old = dst;
438b715f178SLuigi Rizzo 
439b715f178SLuigi Rizzo 		off = (*ip_fw_chk_ptr)(&ip,
440b715f178SLuigi Rizzo 		    hlen, ifp, &ip_divert_cookie, &m, &rule, &dst);
441b715f178SLuigi Rizzo                 /*
442b715f178SLuigi Rizzo                  * On return we must do the following:
443b715f178SLuigi Rizzo                  * m == NULL         -> drop the pkt
444b715f178SLuigi Rizzo                  * 1<=off<= 0xffff   -> DIVERT
445b715f178SLuigi Rizzo                  * (off & 0x10000)   -> send to a DUMMYNET pipe
446b715f178SLuigi Rizzo                  * dst != old        -> IPFIREWALL_FORWARD
447b715f178SLuigi Rizzo                  * off==0, dst==old  -> accept
448b715f178SLuigi Rizzo                  * If some of the above modules is not compiled in, then
449b715f178SLuigi Rizzo                  * we should't have to check the corresponding condition
450b715f178SLuigi Rizzo                  * (because the ipfw control socket should not accept
451b715f178SLuigi Rizzo                  * unsupported rules), but better play safe and drop
452b715f178SLuigi Rizzo                  * packets in case of doubt.
453b715f178SLuigi Rizzo                  */
454b715f178SLuigi Rizzo 		if (!m) { /* firewall said to reject */
455e4676ba6SJulian Elischer 			error = EACCES;
456e4676ba6SJulian Elischer 			goto done;
45793e0e116SJulian Elischer 		}
458b715f178SLuigi Rizzo 		if (off == 0 && dst == old) /* common case */
459b715f178SLuigi Rizzo 			goto pass ;
460b715f178SLuigi Rizzo #ifdef DUMMYNET
461b715f178SLuigi Rizzo                 if (off & 0x10000) {
462b715f178SLuigi Rizzo                     /*
463b715f178SLuigi Rizzo                      * pass the pkt to dummynet. Need to include
464b715f178SLuigi Rizzo                      * pipe number, m, ifp, ro, hlen because these are
465b715f178SLuigi Rizzo                      * not recomputed in the next pass.
466b715f178SLuigi Rizzo                      * All other parameters have been already used and
467b715f178SLuigi Rizzo                      * so they are not needed anymore.
468b715f178SLuigi Rizzo                      * XXX note: if the ifp or ro entry are deleted
469b715f178SLuigi Rizzo                      * while a pkt is in dummynet, we are in trouble!
470b715f178SLuigi Rizzo                      */
471b715f178SLuigi Rizzo                     dummynet_io(off & 0xffff, DN_TO_IP_OUT, m,ifp,ro,hlen,rule);
472b715f178SLuigi Rizzo 			goto done;
473b715f178SLuigi Rizzo 		}
474b715f178SLuigi Rizzo #endif
475b715f178SLuigi Rizzo #ifdef IPDIVERT
476b715f178SLuigi Rizzo                 if (off > 0 && off < 0x10000) {         /* Divert packet */
477b715f178SLuigi Rizzo                        ip_divert_port = off & 0xffff ;
478b715f178SLuigi Rizzo                        (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0);
479b715f178SLuigi Rizzo 			goto done;
480b715f178SLuigi Rizzo 		}
481b715f178SLuigi Rizzo #endif
482b715f178SLuigi Rizzo 
483f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD
484f9e354dfSJulian Elischer 		/* Here we check dst to make sure it's directly reachable on the
485f9e354dfSJulian Elischer 		 * interface we previously thought it was.
486f9e354dfSJulian Elischer 		 * If it isn't (which may be likely in some situations) we have
487f9e354dfSJulian Elischer 		 * to re-route it (ie, find a route for the next-hop and the
488f9e354dfSJulian Elischer 		 * associated interface) and set them here. This is nested
489f9e354dfSJulian Elischer 		 * forwarding which in most cases is undesirable, except where
490f9e354dfSJulian Elischer 		 * such control is nigh impossible. So we do it here.
491f9e354dfSJulian Elischer 		 * And I'm babbling.
492f9e354dfSJulian Elischer 		 */
493b715f178SLuigi Rizzo 		if (off == 0 && old != dst) {
494f9e354dfSJulian Elischer 			struct in_ifaddr *ia;
495f9e354dfSJulian Elischer 
496f9e354dfSJulian Elischer 			/* It's changed... */
497f9e354dfSJulian Elischer 			/* There must be a better way to do this next line... */
498f9e354dfSJulian Elischer 			static struct route sro_fwd, *ro_fwd = &sro_fwd;
499f9e354dfSJulian Elischer #ifdef IPFIREWALL_FORWARD_DEBUG
500f9e354dfSJulian Elischer 			printf("IPFIREWALL_FORWARD: New dst ip: ");
501f9e354dfSJulian Elischer 			print_ip(dst->sin_addr);
502f9e354dfSJulian Elischer 			printf("\n");
503f9e354dfSJulian Elischer #endif
504f9e354dfSJulian Elischer 			/*
505f9e354dfSJulian Elischer 			 * We need to figure out if we have been forwarded
506f9e354dfSJulian Elischer 			 * to a local socket. If so then we should somehow
507f9e354dfSJulian Elischer 			 * "loop back" to ip_input, and get directed to the
508f9e354dfSJulian Elischer 			 * PCB as if we had received this packet. This is
509f9e354dfSJulian Elischer 			 * because it may be dificult to identify the packets
510f9e354dfSJulian Elischer 			 * you want to forward until they are being output
511f9e354dfSJulian Elischer 			 * and have selected an interface. (e.g. locally
512f9e354dfSJulian Elischer 			 * initiated packets) If we used the loopback inteface,
513f9e354dfSJulian Elischer 			 * we would not be able to control what happens
514f9e354dfSJulian Elischer 			 * as the packet runs through ip_input() as
515f9e354dfSJulian Elischer 			 * it is done through a ISR.
516f9e354dfSJulian Elischer 			 */
517d4295c32SJulian Elischer 			for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
518f9e354dfSJulian Elischer 					ia = TAILQ_NEXT(ia, ia_link)) {
519f9e354dfSJulian Elischer 				/*
520f9e354dfSJulian Elischer 				 * If the addr to forward to is one
521f9e354dfSJulian Elischer 				 * of ours, we pretend to
522f9e354dfSJulian Elischer 				 * be the destination for this packet.
523f9e354dfSJulian Elischer 				 */
524f9e354dfSJulian Elischer 				if (IA_SIN(ia)->sin_addr.s_addr ==
525f9e354dfSJulian Elischer 						 dst->sin_addr.s_addr)
526f9e354dfSJulian Elischer 					break;
527f9e354dfSJulian Elischer 			}
528f9e354dfSJulian Elischer 			if (ia) {
529f9e354dfSJulian Elischer 				/* tell ip_input "dont filter" */
530f9e354dfSJulian Elischer 				ip_fw_fwd_addr = dst;
531f9e354dfSJulian Elischer 				if (m->m_pkthdr.rcvif == NULL)
532f9e354dfSJulian Elischer 					m->m_pkthdr.rcvif = ifunit("lo0");
533f9e354dfSJulian Elischer 				ip->ip_len = htons((u_short)ip->ip_len);
534f9e354dfSJulian Elischer 				ip->ip_off = htons((u_short)ip->ip_off);
535f9e354dfSJulian Elischer 				ip->ip_sum = 0;
536f9e354dfSJulian Elischer 				if (ip->ip_vhl == IP_VHL_BORING) {
537f9e354dfSJulian Elischer 					ip->ip_sum = in_cksum_hdr(ip);
538f9e354dfSJulian Elischer 				} else {
539f9e354dfSJulian Elischer 					ip->ip_sum = in_cksum(m, hlen);
540f9e354dfSJulian Elischer 				}
541f9e354dfSJulian Elischer 				ip_input(m);
542f9e354dfSJulian Elischer 				goto done;
543f9e354dfSJulian Elischer 			}
544f9e354dfSJulian Elischer 			/* Some of the logic for this was
545f9e354dfSJulian Elischer 			 * nicked from above.
546f9e354dfSJulian Elischer 			 *
547f9e354dfSJulian Elischer 			 * This rewrites the cached route in a local PCB.
548f9e354dfSJulian Elischer 			 * Is this what we want to do?
549f9e354dfSJulian Elischer 			 */
550f9e354dfSJulian Elischer 			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
551f9e354dfSJulian Elischer 
552f9e354dfSJulian Elischer 			ro_fwd->ro_rt = 0;
553f9e354dfSJulian Elischer 			rtalloc_ign(ro_fwd, RTF_PRCLONING);
554f9e354dfSJulian Elischer 
555f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt == 0) {
556f9e354dfSJulian Elischer 				ipstat.ips_noroute++;
557f9e354dfSJulian Elischer 				error = EHOSTUNREACH;
558f9e354dfSJulian Elischer 				goto bad;
559f9e354dfSJulian Elischer 			}
560f9e354dfSJulian Elischer 
561f9e354dfSJulian Elischer 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
562f9e354dfSJulian Elischer 			ifp = ro_fwd->ro_rt->rt_ifp;
563f9e354dfSJulian Elischer 			ro_fwd->ro_rt->rt_use++;
564f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
565f9e354dfSJulian Elischer 				dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
566f9e354dfSJulian Elischer 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
567f9e354dfSJulian Elischer 				isbroadcast =
568f9e354dfSJulian Elischer 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
569f9e354dfSJulian Elischer 			else
570f9e354dfSJulian Elischer 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
571f9e354dfSJulian Elischer 			RTFREE(ro->ro_rt);
572f9e354dfSJulian Elischer 			ro->ro_rt = ro_fwd->ro_rt;
573f9e354dfSJulian Elischer 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
574f9e354dfSJulian Elischer 
575f9e354dfSJulian Elischer 			/*
576f9e354dfSJulian Elischer 			 * If we added a default src ip earlier,
577f9e354dfSJulian Elischer 			 * which would have been gotten from the-then
578f9e354dfSJulian Elischer 			 * interface, do it again, from the new one.
579f9e354dfSJulian Elischer 			 */
580f9e354dfSJulian Elischer 			if (fwd_rewrite_src)
581f9e354dfSJulian Elischer 				ip->ip_src = IA_SIN(ia)->sin_addr;
582b715f178SLuigi Rizzo 			goto pass ;
583f9e354dfSJulian Elischer 		}
584f9e354dfSJulian Elischer #endif /* IPFIREWALL_FORWARD */
585b715f178SLuigi Rizzo                 /*
586b715f178SLuigi Rizzo                  * if we get here, none of the above matches, and
587b715f178SLuigi Rizzo                  * we have to drop the pkt
588b715f178SLuigi Rizzo                  */
589b715f178SLuigi Rizzo 		m_freem(m);
590b715f178SLuigi Rizzo                 error = EACCES; /* not sure this is the right error msg */
591b715f178SLuigi Rizzo                 goto done;
59293e0e116SJulian Elischer 	}
59393e0e116SJulian Elischer #endif /* COMPAT_IPFW */
594e7319babSPoul-Henning Kamp 
595b715f178SLuigi Rizzo pass:
596e7319babSPoul-Henning Kamp 	/*
597df8bae1dSRodney W. Grimes 	 * If small enough for interface, can just send directly.
598df8bae1dSRodney W. Grimes 	 */
599df8bae1dSRodney W. Grimes 	if ((u_short)ip->ip_len <= ifp->if_mtu) {
600df8bae1dSRodney W. Grimes 		ip->ip_len = htons((u_short)ip->ip_len);
601df8bae1dSRodney W. Grimes 		ip->ip_off = htons((u_short)ip->ip_off);
602df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
6039c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
6049c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
6059c9137eaSGarrett Wollman 		} else {
606df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
6079c9137eaSGarrett Wollman 		}
608df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
609df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
610df8bae1dSRodney W. Grimes 		goto done;
611df8bae1dSRodney W. Grimes 	}
612df8bae1dSRodney W. Grimes 	/*
613df8bae1dSRodney W. Grimes 	 * Too large for interface; fragment if possible.
614df8bae1dSRodney W. Grimes 	 * Must be able to put at least 8 bytes per fragment.
615df8bae1dSRodney W. Grimes 	 */
616df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
617df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
6183d1f141bSGarrett Wollman 		/*
6193d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
6203d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
6213d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
6223d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
6233d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
6243d1f141bSGarrett Wollman 		 */
6253d1f141bSGarrett Wollman 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
6263d1f141bSGarrett Wollman 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
6273d1f141bSGarrett Wollman 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
6283d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
6293d1f141bSGarrett Wollman 		}
630df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
631df8bae1dSRodney W. Grimes 		goto bad;
632df8bae1dSRodney W. Grimes 	}
633df8bae1dSRodney W. Grimes 	len = (ifp->if_mtu - hlen) &~ 7;
634df8bae1dSRodney W. Grimes 	if (len < 8) {
635df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
636df8bae1dSRodney W. Grimes 		goto bad;
637df8bae1dSRodney W. Grimes 	}
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes     {
640df8bae1dSRodney W. Grimes 	int mhlen, firstlen = len;
641df8bae1dSRodney W. Grimes 	struct mbuf **mnext = &m->m_nextpkt;
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes 	/*
644df8bae1dSRodney W. Grimes 	 * Loop through length of segment after first fragment,
645df8bae1dSRodney W. Grimes 	 * make new header and copy data of each part and link onto chain.
646df8bae1dSRodney W. Grimes 	 */
647df8bae1dSRodney W. Grimes 	m0 = m;
648df8bae1dSRodney W. Grimes 	mhlen = sizeof (struct ip);
649df8bae1dSRodney W. Grimes 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
650df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
651df8bae1dSRodney W. Grimes 		if (m == 0) {
652df8bae1dSRodney W. Grimes 			error = ENOBUFS;
653df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
654df8bae1dSRodney W. Grimes 			goto sendorfree;
655df8bae1dSRodney W. Grimes 		}
656db028362SGarrett Wollman 		m->m_flags |= (m0->m_flags & M_MCAST);
657df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
658df8bae1dSRodney W. Grimes 		mhip = mtod(m, struct ip *);
659df8bae1dSRodney W. Grimes 		*mhip = *ip;
660df8bae1dSRodney W. Grimes 		if (hlen > sizeof (struct ip)) {
661df8bae1dSRodney W. Grimes 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
6629c9137eaSGarrett Wollman 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
663df8bae1dSRodney W. Grimes 		}
664df8bae1dSRodney W. Grimes 		m->m_len = mhlen;
665df8bae1dSRodney W. Grimes 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
666df8bae1dSRodney W. Grimes 		if (ip->ip_off & IP_MF)
667df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
668df8bae1dSRodney W. Grimes 		if (off + len >= (u_short)ip->ip_len)
669df8bae1dSRodney W. Grimes 			len = (u_short)ip->ip_len - off;
670df8bae1dSRodney W. Grimes 		else
671df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
672df8bae1dSRodney W. Grimes 		mhip->ip_len = htons((u_short)(len + mhlen));
673df8bae1dSRodney W. Grimes 		m->m_next = m_copy(m0, off, len);
674df8bae1dSRodney W. Grimes 		if (m->m_next == 0) {
675df8bae1dSRodney W. Grimes 			(void) m_free(m);
676df8bae1dSRodney W. Grimes 			error = ENOBUFS;	/* ??? */
677df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
678df8bae1dSRodney W. Grimes 			goto sendorfree;
679df8bae1dSRodney W. Grimes 		}
680df8bae1dSRodney W. Grimes 		m->m_pkthdr.len = mhlen + len;
681df8bae1dSRodney W. Grimes 		m->m_pkthdr.rcvif = (struct ifnet *)0;
682df8bae1dSRodney W. Grimes 		mhip->ip_off = htons((u_short)mhip->ip_off);
683df8bae1dSRodney W. Grimes 		mhip->ip_sum = 0;
6849c9137eaSGarrett Wollman 		if (mhip->ip_vhl == IP_VHL_BORING) {
685e2184122SBruce Evans 			mhip->ip_sum = in_cksum_hdr(mhip);
6869c9137eaSGarrett Wollman 		} else {
687df8bae1dSRodney W. Grimes 			mhip->ip_sum = in_cksum(m, mhlen);
6889c9137eaSGarrett Wollman 		}
689df8bae1dSRodney W. Grimes 		*mnext = m;
690df8bae1dSRodney W. Grimes 		mnext = &m->m_nextpkt;
691df8bae1dSRodney W. Grimes 		ipstat.ips_ofragments++;
692df8bae1dSRodney W. Grimes 	}
693df8bae1dSRodney W. Grimes 	/*
694df8bae1dSRodney W. Grimes 	 * Update first fragment by trimming what's been copied out
695df8bae1dSRodney W. Grimes 	 * and updating header, then send each fragment (in order).
696df8bae1dSRodney W. Grimes 	 */
697df8bae1dSRodney W. Grimes 	m = m0;
698df8bae1dSRodney W. Grimes 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
699df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hlen + firstlen;
700df8bae1dSRodney W. Grimes 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
701df8bae1dSRodney W. Grimes 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
702df8bae1dSRodney W. Grimes 	ip->ip_sum = 0;
7039c9137eaSGarrett Wollman 	if (ip->ip_vhl == IP_VHL_BORING) {
7049c9137eaSGarrett Wollman 		ip->ip_sum = in_cksum_hdr(ip);
7059c9137eaSGarrett Wollman 	} else {
706df8bae1dSRodney W. Grimes 		ip->ip_sum = in_cksum(m, hlen);
7079c9137eaSGarrett Wollman 	}
708df8bae1dSRodney W. Grimes sendorfree:
709df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m0) {
710df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
711df8bae1dSRodney W. Grimes 		m->m_nextpkt = 0;
712df8bae1dSRodney W. Grimes 		if (error == 0)
713df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
714df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
715df8bae1dSRodney W. Grimes 		else
716df8bae1dSRodney W. Grimes 			m_freem(m);
717df8bae1dSRodney W. Grimes 	}
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes 	if (error == 0)
720df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
721df8bae1dSRodney W. Grimes     }
722df8bae1dSRodney W. Grimes done:
723df8bae1dSRodney W. Grimes 	return (error);
724df8bae1dSRodney W. Grimes bad:
725df8bae1dSRodney W. Grimes 	m_freem(m0);
726df8bae1dSRodney W. Grimes 	goto done;
727df8bae1dSRodney W. Grimes }
728df8bae1dSRodney W. Grimes 
729df8bae1dSRodney W. Grimes /*
730df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
731df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
732df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
733072b9b24SPaul Traina  *
734072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
735df8bae1dSRodney W. Grimes  */
736df8bae1dSRodney W. Grimes static struct mbuf *
737df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen)
738df8bae1dSRodney W. Grimes 	register struct mbuf *m;
739df8bae1dSRodney W. Grimes 	struct mbuf *opt;
740df8bae1dSRodney W. Grimes 	int *phlen;
741df8bae1dSRodney W. Grimes {
742df8bae1dSRodney W. Grimes 	register struct ipoption *p = mtod(opt, struct ipoption *);
743df8bae1dSRodney W. Grimes 	struct mbuf *n;
744df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
745df8bae1dSRodney W. Grimes 	unsigned optlen;
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
748df8bae1dSRodney W. Grimes 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
749df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
750df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
751df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
752df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
753df8bae1dSRodney W. Grimes 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
754df8bae1dSRodney W. Grimes 		if (n == 0)
755df8bae1dSRodney W. Grimes 			return (m);
756df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
757df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
758df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
759df8bae1dSRodney W. Grimes 		n->m_next = m;
760df8bae1dSRodney W. Grimes 		m = n;
761df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
762df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
76394a5d9b6SDavid Greenman 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
764df8bae1dSRodney W. Grimes 	} else {
765df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
766df8bae1dSRodney W. Grimes 		m->m_len += optlen;
767df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
768df8bae1dSRodney W. Grimes 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
769df8bae1dSRodney W. Grimes 	}
770df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
7710453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
772df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
7739c9137eaSGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
774df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
775df8bae1dSRodney W. Grimes 	return (m);
776df8bae1dSRodney W. Grimes }
777df8bae1dSRodney W. Grimes 
778df8bae1dSRodney W. Grimes /*
779df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
780df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
781df8bae1dSRodney W. Grimes  */
782beec8214SDarren Reed #if !defined(IPFILTER) && !defined(IPFILTER_LKM)
783beec8214SDarren Reed static
784beec8214SDarren Reed #endif
785beec8214SDarren Reed int
786df8bae1dSRodney W. Grimes ip_optcopy(ip, jp)
787df8bae1dSRodney W. Grimes 	struct ip *ip, *jp;
788df8bae1dSRodney W. Grimes {
789df8bae1dSRodney W. Grimes 	register u_char *cp, *dp;
790df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
793df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
7949c9137eaSGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
795df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
796df8bae1dSRodney W. Grimes 		opt = cp[0];
797df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
798df8bae1dSRodney W. Grimes 			break;
799df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
800df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
801df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
802df8bae1dSRodney W. Grimes 			optlen = 1;
803df8bae1dSRodney W. Grimes 			continue;
804df8bae1dSRodney W. Grimes 		} else
805df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
806df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
807df8bae1dSRodney W. Grimes 		if (optlen > cnt)
808df8bae1dSRodney W. Grimes 			optlen = cnt;
809df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
8100453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
811df8bae1dSRodney W. Grimes 			dp += optlen;
812df8bae1dSRodney W. Grimes 		}
813df8bae1dSRodney W. Grimes 	}
814df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
815df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
816df8bae1dSRodney W. Grimes 	return (optlen);
817df8bae1dSRodney W. Grimes }
818df8bae1dSRodney W. Grimes 
819df8bae1dSRodney W. Grimes /*
820df8bae1dSRodney W. Grimes  * IP socket option processing.
821df8bae1dSRodney W. Grimes  */
822df8bae1dSRodney W. Grimes int
823cfe8b629SGarrett Wollman ip_ctloutput(so, sopt)
824df8bae1dSRodney W. Grimes 	struct socket *so;
825cfe8b629SGarrett Wollman 	struct sockopt *sopt;
826df8bae1dSRodney W. Grimes {
827cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
828cfe8b629SGarrett Wollman 	int	error, optval;
829df8bae1dSRodney W. Grimes 
830cfe8b629SGarrett Wollman 	error = optval = 0;
831cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
832cfe8b629SGarrett Wollman 		return (EINVAL);
833cfe8b629SGarrett Wollman 	}
834df8bae1dSRodney W. Grimes 
835cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
836cfe8b629SGarrett Wollman 	case SOPT_SET:
837cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
838df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
839df8bae1dSRodney W. Grimes #ifdef notyet
840df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
841df8bae1dSRodney W. Grimes #endif
842cfe8b629SGarrett Wollman 		{
843cfe8b629SGarrett Wollman 			struct mbuf *m;
844cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
845cfe8b629SGarrett Wollman 				error = EMSGSIZE;
846cfe8b629SGarrett Wollman 				break;
847cfe8b629SGarrett Wollman 			}
848cfe8b629SGarrett Wollman 			MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_HEADER);
849cfe8b629SGarrett Wollman 			if (m == 0) {
850cfe8b629SGarrett Wollman 				error = ENOBUFS;
851cfe8b629SGarrett Wollman 				break;
852cfe8b629SGarrett Wollman 			}
853cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
854cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
855cfe8b629SGarrett Wollman 					    m->m_len);
856cfe8b629SGarrett Wollman 
857cfe8b629SGarrett Wollman 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
858cfe8b629SGarrett Wollman 					   m));
859cfe8b629SGarrett Wollman 		}
860df8bae1dSRodney W. Grimes 
861df8bae1dSRodney W. Grimes 		case IP_TOS:
862df8bae1dSRodney W. Grimes 		case IP_TTL:
863df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
864df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
865df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
86682c23ebaSBill Fenner 		case IP_RECVIF:
867cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
868cfe8b629SGarrett Wollman 					    sizeof optval);
869cfe8b629SGarrett Wollman 			if (error)
870cfe8b629SGarrett Wollman 				break;
871df8bae1dSRodney W. Grimes 
872cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
873df8bae1dSRodney W. Grimes 			case IP_TOS:
874ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
875df8bae1dSRodney W. Grimes 				break;
876df8bae1dSRodney W. Grimes 
877df8bae1dSRodney W. Grimes 			case IP_TTL:
878ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
879df8bae1dSRodney W. Grimes 				break;
880df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
881df8bae1dSRodney W. Grimes 	if (optval) \
882df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
883df8bae1dSRodney W. Grimes 	else \
884df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
885df8bae1dSRodney W. Grimes 
886df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
887df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
888df8bae1dSRodney W. Grimes 				break;
889df8bae1dSRodney W. Grimes 
890df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
891df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
892df8bae1dSRodney W. Grimes 				break;
893df8bae1dSRodney W. Grimes 
894df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
895df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
896df8bae1dSRodney W. Grimes 				break;
89782c23ebaSBill Fenner 
89882c23ebaSBill Fenner 			case IP_RECVIF:
89982c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
90082c23ebaSBill Fenner 				break;
901df8bae1dSRodney W. Grimes 			}
902df8bae1dSRodney W. Grimes 			break;
903df8bae1dSRodney W. Grimes #undef OPTSET
904df8bae1dSRodney W. Grimes 
905df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
906f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
907df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
908df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
909df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
910df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
911cfe8b629SGarrett Wollman 			error = ip_setmoptions(sopt, &inp->inp_moptions);
912df8bae1dSRodney W. Grimes 			break;
913df8bae1dSRodney W. Grimes 
91433b3ac06SPeter Wemm 		case IP_PORTRANGE:
915cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
916cfe8b629SGarrett Wollman 					    sizeof optval);
917cfe8b629SGarrett Wollman 			if (error)
918cfe8b629SGarrett Wollman 				break;
91933b3ac06SPeter Wemm 
92033b3ac06SPeter Wemm 			switch (optval) {
92133b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
92233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
92333b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
92433b3ac06SPeter Wemm 				break;
92533b3ac06SPeter Wemm 
92633b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
92733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
92833b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
92933b3ac06SPeter Wemm 				break;
93033b3ac06SPeter Wemm 
93133b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
93233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
93333b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
93433b3ac06SPeter Wemm 				break;
93533b3ac06SPeter Wemm 
93633b3ac06SPeter Wemm 			default:
93733b3ac06SPeter Wemm 				error = EINVAL;
93833b3ac06SPeter Wemm 				break;
93933b3ac06SPeter Wemm 			}
940ce8c72b1SPeter Wemm 			break;
94133b3ac06SPeter Wemm 
942df8bae1dSRodney W. Grimes 		default:
943df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
944df8bae1dSRodney W. Grimes 			break;
945df8bae1dSRodney W. Grimes 		}
946df8bae1dSRodney W. Grimes 		break;
947df8bae1dSRodney W. Grimes 
948cfe8b629SGarrett Wollman 	case SOPT_GET:
949cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
950df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
951df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
952cfe8b629SGarrett Wollman 			if (inp->inp_options)
953cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
954cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
955cfe8b629SGarrett Wollman 							  char *),
956cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
957cfe8b629SGarrett Wollman 			else
958cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
959df8bae1dSRodney W. Grimes 			break;
960df8bae1dSRodney W. Grimes 
961df8bae1dSRodney W. Grimes 		case IP_TOS:
962df8bae1dSRodney W. Grimes 		case IP_TTL:
963df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
964df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
965df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
96682c23ebaSBill Fenner 		case IP_RECVIF:
967cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
968cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
969df8bae1dSRodney W. Grimes 
970df8bae1dSRodney W. Grimes 			case IP_TOS:
971ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
972df8bae1dSRodney W. Grimes 				break;
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes 			case IP_TTL:
975ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
976df8bae1dSRodney W. Grimes 				break;
977df8bae1dSRodney W. Grimes 
978df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
981df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
982df8bae1dSRodney W. Grimes 				break;
983df8bae1dSRodney W. Grimes 
984df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
985df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
986df8bae1dSRodney W. Grimes 				break;
987df8bae1dSRodney W. Grimes 
988df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
989df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
990df8bae1dSRodney W. Grimes 				break;
99182c23ebaSBill Fenner 
99282c23ebaSBill Fenner 			case IP_RECVIF:
99382c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
99482c23ebaSBill Fenner 				break;
995cfe8b629SGarrett Wollman 
996cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
997cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
998cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
999cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1000cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1001cfe8b629SGarrett Wollman 				else
1002cfe8b629SGarrett Wollman 					optval = 0;
1003cfe8b629SGarrett Wollman 				break;
1004df8bae1dSRodney W. Grimes 			}
1005cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1006df8bae1dSRodney W. Grimes 			break;
1007df8bae1dSRodney W. Grimes 
1008df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1009f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1010df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1011df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1012df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1013df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
1014cfe8b629SGarrett Wollman 			error = ip_getmoptions(sopt, inp->inp_moptions);
101533b3ac06SPeter Wemm 			break;
101633b3ac06SPeter Wemm 
1017df8bae1dSRodney W. Grimes 		default:
1018df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1019df8bae1dSRodney W. Grimes 			break;
1020df8bae1dSRodney W. Grimes 		}
1021df8bae1dSRodney W. Grimes 		break;
1022df8bae1dSRodney W. Grimes 	}
1023df8bae1dSRodney W. Grimes 	return (error);
1024df8bae1dSRodney W. Grimes }
1025df8bae1dSRodney W. Grimes 
1026df8bae1dSRodney W. Grimes /*
1027df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
1028df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1029df8bae1dSRodney W. Grimes  * with destination address if source routed.
1030df8bae1dSRodney W. Grimes  */
10310312fbe9SPoul-Henning Kamp static int
1032df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m)
1033df8bae1dSRodney W. Grimes 	int optname;
1034df8bae1dSRodney W. Grimes 	struct mbuf **pcbopt;
1035df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1036df8bae1dSRodney W. Grimes {
1037d68fa50cSBruce Evans 	register int cnt, optlen;
1038df8bae1dSRodney W. Grimes 	register u_char *cp;
1039df8bae1dSRodney W. Grimes 	u_char opt;
1040df8bae1dSRodney W. Grimes 
1041df8bae1dSRodney W. Grimes 	/* turn off any old options */
1042df8bae1dSRodney W. Grimes 	if (*pcbopt)
1043df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
1044df8bae1dSRodney W. Grimes 	*pcbopt = 0;
1045df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1046df8bae1dSRodney W. Grimes 		/*
1047df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
1048df8bae1dSRodney W. Grimes 		 */
1049df8bae1dSRodney W. Grimes 		if (m)
1050df8bae1dSRodney W. Grimes 			(void)m_free(m);
1051df8bae1dSRodney W. Grimes 		return (0);
1052df8bae1dSRodney W. Grimes 	}
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes #ifndef	vax
10550c8d2590SBruce Evans 	if (m->m_len % sizeof(int32_t))
1056df8bae1dSRodney W. Grimes 		goto bad;
1057df8bae1dSRodney W. Grimes #endif
1058df8bae1dSRodney W. Grimes 	/*
1059df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
1060df8bae1dSRodney W. Grimes 	 * actual options; move other options back
1061df8bae1dSRodney W. Grimes 	 * and clear it when none present.
1062df8bae1dSRodney W. Grimes 	 */
1063df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1064df8bae1dSRodney W. Grimes 		goto bad;
1065df8bae1dSRodney W. Grimes 	cnt = m->m_len;
1066df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
1067df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1068df8bae1dSRodney W. Grimes 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1069df8bae1dSRodney W. Grimes 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1072df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
1073df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
1074df8bae1dSRodney W. Grimes 			break;
1075df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
1076df8bae1dSRodney W. Grimes 			optlen = 1;
1077df8bae1dSRodney W. Grimes 		else {
1078df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
1079df8bae1dSRodney W. Grimes 			if (optlen <= IPOPT_OLEN || optlen > cnt)
1080df8bae1dSRodney W. Grimes 				goto bad;
1081df8bae1dSRodney W. Grimes 		}
1082df8bae1dSRodney W. Grimes 		switch (opt) {
1083df8bae1dSRodney W. Grimes 
1084df8bae1dSRodney W. Grimes 		default:
1085df8bae1dSRodney W. Grimes 			break;
1086df8bae1dSRodney W. Grimes 
1087df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
1088df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
1089df8bae1dSRodney W. Grimes 			/*
1090df8bae1dSRodney W. Grimes 			 * user process specifies route as:
1091df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
1092df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
1093df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
1094df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
1095df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
1096df8bae1dSRodney W. Grimes 			 */
1097df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1098df8bae1dSRodney W. Grimes 				goto bad;
1099df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
1100df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
1101df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
1102df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
1103df8bae1dSRodney W. Grimes 			/*
1104df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
1105df8bae1dSRodney W. Grimes 			 */
1106df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1107df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
1108df8bae1dSRodney W. Grimes 			/*
1109df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
1110df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
1111df8bae1dSRodney W. Grimes 			 */
1112df8bae1dSRodney W. Grimes 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1113df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr)),
1114df8bae1dSRodney W. Grimes 			    (caddr_t)&cp[IPOPT_OFFSET+1],
1115df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
1116df8bae1dSRodney W. Grimes 			break;
1117df8bae1dSRodney W. Grimes 		}
1118df8bae1dSRodney W. Grimes 	}
1119df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1120df8bae1dSRodney W. Grimes 		goto bad;
1121df8bae1dSRodney W. Grimes 	*pcbopt = m;
1122df8bae1dSRodney W. Grimes 	return (0);
1123df8bae1dSRodney W. Grimes 
1124df8bae1dSRodney W. Grimes bad:
1125df8bae1dSRodney W. Grimes 	(void)m_free(m);
1126df8bae1dSRodney W. Grimes 	return (EINVAL);
1127df8bae1dSRodney W. Grimes }
1128df8bae1dSRodney W. Grimes 
1129df8bae1dSRodney W. Grimes /*
1130cfe8b629SGarrett Wollman  * XXX
1131cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1132cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1133cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1134cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1135cfe8b629SGarrett Wollman  */
1136cfe8b629SGarrett Wollman /*
1137df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1138df8bae1dSRodney W. Grimes  */
11390312fbe9SPoul-Henning Kamp static int
1140cfe8b629SGarrett Wollman ip_setmoptions(sopt, imop)
1141cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1142df8bae1dSRodney W. Grimes 	struct ip_moptions **imop;
1143df8bae1dSRodney W. Grimes {
1144cfe8b629SGarrett Wollman 	int error = 0;
1145cfe8b629SGarrett Wollman 	int i;
1146df8bae1dSRodney W. Grimes 	struct in_addr addr;
1147cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1148cfe8b629SGarrett Wollman 	struct ifnet *ifp;
1149cfe8b629SGarrett Wollman 	struct ip_moptions *imo = *imop;
1150df8bae1dSRodney W. Grimes 	struct route ro;
1151cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
11529b626c29SGarrett Wollman 	int s;
1153df8bae1dSRodney W. Grimes 
1154df8bae1dSRodney W. Grimes 	if (imo == NULL) {
1155df8bae1dSRodney W. Grimes 		/*
1156df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
1157df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
1158df8bae1dSRodney W. Grimes 		 */
1159df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1160df8bae1dSRodney W. Grimes 		    M_WAITOK);
1161df8bae1dSRodney W. Grimes 
1162df8bae1dSRodney W. Grimes 		if (imo == NULL)
1163df8bae1dSRodney W. Grimes 			return (ENOBUFS);
1164df8bae1dSRodney W. Grimes 		*imop = imo;
1165df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
11661c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
1167df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1168df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1169df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
1170df8bae1dSRodney W. Grimes 	}
1171df8bae1dSRodney W. Grimes 
1172cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1173f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1174f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1175cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
11765e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
11775e9ae478SGarrett Wollman 			break;
11785e9ae478SGarrett Wollman 		}
1179cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1180cfe8b629SGarrett Wollman 		if (error)
1181f0068c4aSGarrett Wollman 			break;
11821c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1183f0068c4aSGarrett Wollman 			error = EINVAL;
1184f0068c4aSGarrett Wollman 			break;
1185f0068c4aSGarrett Wollman 		}
1186f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1187f0068c4aSGarrett Wollman 		break;
1188f0068c4aSGarrett Wollman 
1189df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1190df8bae1dSRodney W. Grimes 		/*
1191df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1192df8bae1dSRodney W. Grimes 		 */
1193cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1194cfe8b629SGarrett Wollman 		if (error)
1195df8bae1dSRodney W. Grimes 			break;
1196df8bae1dSRodney W. Grimes 		/*
1197df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1198df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1199df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1200df8bae1dSRodney W. Grimes 		 */
1201df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1202df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1203df8bae1dSRodney W. Grimes 			break;
1204df8bae1dSRodney W. Grimes 		}
1205df8bae1dSRodney W. Grimes 		/*
1206df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1207df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1208df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1209df8bae1dSRodney W. Grimes 		 */
121020e8807cSGarrett Wollman 		s = splimp();
1211df8bae1dSRodney W. Grimes 		INADDR_TO_IFP(addr, ifp);
1212df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1213fbc6ab00SBill Fenner 			splx(s);
1214df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1215df8bae1dSRodney W. Grimes 			break;
1216df8bae1dSRodney W. Grimes 		}
1217df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
12189b626c29SGarrett Wollman 		splx(s);
1219df8bae1dSRodney W. Grimes 		break;
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1222df8bae1dSRodney W. Grimes 		/*
1223df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1224cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1225cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1226cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1227df8bae1dSRodney W. Grimes 		 */
1228cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1229cfe8b629SGarrett Wollman 			u_char ttl;
1230cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1231cfe8b629SGarrett Wollman 			if (error)
1232df8bae1dSRodney W. Grimes 				break;
1233cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1234cfe8b629SGarrett Wollman 		} else {
1235cfe8b629SGarrett Wollman 			u_int ttl;
1236cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1237cfe8b629SGarrett Wollman 					    sizeof ttl);
1238cfe8b629SGarrett Wollman 			if (error)
1239cfe8b629SGarrett Wollman 				break;
1240cfe8b629SGarrett Wollman 			if (ttl > 255)
1241cfe8b629SGarrett Wollman 				error = EINVAL;
1242cfe8b629SGarrett Wollman 			else
1243cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1244df8bae1dSRodney W. Grimes 		}
1245df8bae1dSRodney W. Grimes 		break;
1246df8bae1dSRodney W. Grimes 
1247df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1248df8bae1dSRodney W. Grimes 		/*
1249df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1250cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1251cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1252cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1253df8bae1dSRodney W. Grimes 		 */
1254cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1255cfe8b629SGarrett Wollman 			u_char loop;
1256cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1257cfe8b629SGarrett Wollman 			if (error)
1258df8bae1dSRodney W. Grimes 				break;
1259cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1260cfe8b629SGarrett Wollman 		} else {
1261cfe8b629SGarrett Wollman 			u_int loop;
1262cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1263cfe8b629SGarrett Wollman 					    sizeof loop);
1264cfe8b629SGarrett Wollman 			if (error)
1265cfe8b629SGarrett Wollman 				break;
1266cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1267df8bae1dSRodney W. Grimes 		}
1268df8bae1dSRodney W. Grimes 		break;
1269df8bae1dSRodney W. Grimes 
1270df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1271df8bae1dSRodney W. Grimes 		/*
1272df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1273df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1274df8bae1dSRodney W. Grimes 		 */
1275cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1276cfe8b629SGarrett Wollman 		if (error)
1277df8bae1dSRodney W. Grimes 			break;
1278cfe8b629SGarrett Wollman 
1279cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1280df8bae1dSRodney W. Grimes 			error = EINVAL;
1281df8bae1dSRodney W. Grimes 			break;
1282df8bae1dSRodney W. Grimes 		}
128320e8807cSGarrett Wollman 		s = splimp();
1284df8bae1dSRodney W. Grimes 		/*
1285df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1286df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1287df8bae1dSRodney W. Grimes 		 */
1288cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
12891c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1290df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1291df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1292df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1293cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
1294df8bae1dSRodney W. Grimes 			rtalloc(&ro);
1295df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1296df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
12979b626c29SGarrett Wollman 				splx(s);
1298df8bae1dSRodney W. Grimes 				break;
1299df8bae1dSRodney W. Grimes 			}
1300df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1301df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
1302df8bae1dSRodney W. Grimes 		}
1303df8bae1dSRodney W. Grimes 		else {
1304cfe8b629SGarrett Wollman 			INADDR_TO_IFP(mreq.imr_interface, ifp);
1305df8bae1dSRodney W. Grimes 		}
13069b626c29SGarrett Wollman 
1307df8bae1dSRodney W. Grimes 		/*
1308df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1309df8bae1dSRodney W. Grimes 		 * supports multicast.
1310df8bae1dSRodney W. Grimes 		 */
1311df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1312df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
13139b626c29SGarrett Wollman 			splx(s);
1314df8bae1dSRodney W. Grimes 			break;
1315df8bae1dSRodney W. Grimes 		}
1316df8bae1dSRodney W. Grimes 		/*
1317df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1318df8bae1dSRodney W. Grimes 		 * membership slots are full.
1319df8bae1dSRodney W. Grimes 		 */
1320df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1321df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1322df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1323cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1324df8bae1dSRodney W. Grimes 				break;
1325df8bae1dSRodney W. Grimes 		}
1326df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1327df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
13289b626c29SGarrett Wollman 			splx(s);
1329df8bae1dSRodney W. Grimes 			break;
1330df8bae1dSRodney W. Grimes 		}
1331df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1332df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
13339b626c29SGarrett Wollman 			splx(s);
1334df8bae1dSRodney W. Grimes 			break;
1335df8bae1dSRodney W. Grimes 		}
1336df8bae1dSRodney W. Grimes 		/*
1337df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1338df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1339df8bae1dSRodney W. Grimes 		 */
1340df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1341cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1342df8bae1dSRodney W. Grimes 			error = ENOBUFS;
13439b626c29SGarrett Wollman 			splx(s);
1344df8bae1dSRodney W. Grimes 			break;
1345df8bae1dSRodney W. Grimes 		}
1346df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
13479b626c29SGarrett Wollman 		splx(s);
1348df8bae1dSRodney W. Grimes 		break;
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1351df8bae1dSRodney W. Grimes 		/*
1352df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1353df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1354df8bae1dSRodney W. Grimes 		 */
1355cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1356cfe8b629SGarrett Wollman 		if (error)
1357df8bae1dSRodney W. Grimes 			break;
1358cfe8b629SGarrett Wollman 
1359cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1360df8bae1dSRodney W. Grimes 			error = EINVAL;
1361df8bae1dSRodney W. Grimes 			break;
1362df8bae1dSRodney W. Grimes 		}
13639b626c29SGarrett Wollman 
136420e8807cSGarrett Wollman 		s = splimp();
1365df8bae1dSRodney W. Grimes 		/*
1366df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1367df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1368df8bae1dSRodney W. Grimes 		 */
1369cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1370df8bae1dSRodney W. Grimes 			ifp = NULL;
1371df8bae1dSRodney W. Grimes 		else {
1372cfe8b629SGarrett Wollman 			INADDR_TO_IFP(mreq.imr_interface, ifp);
1373df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1374df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
13759b626c29SGarrett Wollman 				splx(s);
1376df8bae1dSRodney W. Grimes 				break;
1377df8bae1dSRodney W. Grimes 			}
1378df8bae1dSRodney W. Grimes 		}
1379df8bae1dSRodney W. Grimes 		/*
1380df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1381df8bae1dSRodney W. Grimes 		 */
1382df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1383df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1384df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1385df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1386cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1387df8bae1dSRodney W. Grimes 				break;
1388df8bae1dSRodney W. Grimes 		}
1389df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1390df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
13919b626c29SGarrett Wollman 			splx(s);
1392df8bae1dSRodney W. Grimes 			break;
1393df8bae1dSRodney W. Grimes 		}
1394df8bae1dSRodney W. Grimes 		/*
1395df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1396df8bae1dSRodney W. Grimes 		 * membership points.
1397df8bae1dSRodney W. Grimes 		 */
1398df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1399df8bae1dSRodney W. Grimes 		/*
1400df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1401df8bae1dSRodney W. Grimes 		 */
1402df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1403df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1404df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
14059b626c29SGarrett Wollman 		splx(s);
1406df8bae1dSRodney W. Grimes 		break;
1407df8bae1dSRodney W. Grimes 
1408df8bae1dSRodney W. Grimes 	default:
1409df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1410df8bae1dSRodney W. Grimes 		break;
1411df8bae1dSRodney W. Grimes 	}
1412df8bae1dSRodney W. Grimes 
1413df8bae1dSRodney W. Grimes 	/*
1414df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1415df8bae1dSRodney W. Grimes 	 */
1416df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
14171c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1418df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1419df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1420df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1421df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1422df8bae1dSRodney W. Grimes 		*imop = NULL;
1423df8bae1dSRodney W. Grimes 	}
1424df8bae1dSRodney W. Grimes 
1425df8bae1dSRodney W. Grimes 	return (error);
1426df8bae1dSRodney W. Grimes }
1427df8bae1dSRodney W. Grimes 
1428df8bae1dSRodney W. Grimes /*
1429df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1430df8bae1dSRodney W. Grimes  */
14310312fbe9SPoul-Henning Kamp static int
1432cfe8b629SGarrett Wollman ip_getmoptions(sopt, imo)
1433cfe8b629SGarrett Wollman 	struct sockopt *sopt;
1434df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1435df8bae1dSRodney W. Grimes {
1436cfe8b629SGarrett Wollman 	struct in_addr addr;
1437df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1438cfe8b629SGarrett Wollman 	int error, optval;
1439cfe8b629SGarrett Wollman 	u_char coptval;
1440df8bae1dSRodney W. Grimes 
1441cfe8b629SGarrett Wollman 	error = 0;
1442cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1443f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1444f0068c4aSGarrett Wollman 		if (imo != NULL)
1445cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1446f0068c4aSGarrett Wollman 		else
1447cfe8b629SGarrett Wollman 			optval = -1;
1448cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1449cfe8b629SGarrett Wollman 		break;
1450f0068c4aSGarrett Wollman 
1451df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1452df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1453cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
1454df8bae1dSRodney W. Grimes 		else {
1455df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1456cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1457df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1458df8bae1dSRodney W. Grimes 		}
1459cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1460cfe8b629SGarrett Wollman 		break;
1461df8bae1dSRodney W. Grimes 
1462df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1463cfe8b629SGarrett Wollman 		if (imo == 0)
1464cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1465cfe8b629SGarrett Wollman 		else
1466cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
1467cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1468cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1469cfe8b629SGarrett Wollman 		else
1470cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1471cfe8b629SGarrett Wollman 		break;
1472df8bae1dSRodney W. Grimes 
1473df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1474cfe8b629SGarrett Wollman 		if (imo == 0)
1475cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1476cfe8b629SGarrett Wollman 		else
1477cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
1478cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1479cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1480cfe8b629SGarrett Wollman 		else
1481cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1482cfe8b629SGarrett Wollman 		break;
1483df8bae1dSRodney W. Grimes 
1484df8bae1dSRodney W. Grimes 	default:
1485cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1486cfe8b629SGarrett Wollman 		break;
1487df8bae1dSRodney W. Grimes 	}
1488cfe8b629SGarrett Wollman 	return (error);
1489df8bae1dSRodney W. Grimes }
1490df8bae1dSRodney W. Grimes 
1491df8bae1dSRodney W. Grimes /*
1492df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1493df8bae1dSRodney W. Grimes  */
1494df8bae1dSRodney W. Grimes void
1495df8bae1dSRodney W. Grimes ip_freemoptions(imo)
1496df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1497df8bae1dSRodney W. Grimes {
1498df8bae1dSRodney W. Grimes 	register int i;
1499df8bae1dSRodney W. Grimes 
1500df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1501df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1502df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1503df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1504df8bae1dSRodney W. Grimes 	}
1505df8bae1dSRodney W. Grimes }
1506df8bae1dSRodney W. Grimes 
1507df8bae1dSRodney W. Grimes /*
1508df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1509df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1510df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1511f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1512f5fea3ddSPaul Traina  * replicating that code here.
1513df8bae1dSRodney W. Grimes  */
1514df8bae1dSRodney W. Grimes static void
151586b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen)
1516df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1517df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1518df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
151986b1d6d2SBill Fenner 	int hlen;
1520df8bae1dSRodney W. Grimes {
1521df8bae1dSRodney W. Grimes 	register struct ip *ip;
1522df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1523df8bae1dSRodney W. Grimes 
1524df8bae1dSRodney W. Grimes 	copym = m_copy(m, 0, M_COPYALL);
152586b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
152686b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1527df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1528df8bae1dSRodney W. Grimes 		/*
1529df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1530df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1531df8bae1dSRodney W. Grimes 		 */
1532df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1533df8bae1dSRodney W. Grimes 		ip->ip_len = htons((u_short)ip->ip_len);
1534df8bae1dSRodney W. Grimes 		ip->ip_off = htons((u_short)ip->ip_off);
1535df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
15369c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
15379c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
15389c9137eaSGarrett Wollman 		} else {
153986b1d6d2SBill Fenner 			ip->ip_sum = in_cksum(copym, hlen);
15409c9137eaSGarrett Wollman 		}
15419c9137eaSGarrett Wollman 		/*
15429c9137eaSGarrett Wollman 		 * NB:
1543e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1544e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1545e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1546e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1547e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1548e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1549e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1550ed7509acSJulian Elischer 		 * of safety by using if_simloop().
15519c9137eaSGarrett Wollman 		 */
1552201c2527SJulian Elischer #if 1 /* XXX */
1553201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
15542b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1555201c2527SJulian Elischer 						dst->sin_family);
1556201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1557201c2527SJulian Elischer 		}
1558201c2527SJulian Elischer #endif
1559201c2527SJulian Elischer 
15609c9137eaSGarrett Wollman #ifdef notdef
1561e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
1562ed7509acSJulian Elischer 		ip_input(copym);
15639c9137eaSGarrett Wollman #else
1564ed7509acSJulian Elischer 		if_simloop(ifp, copym, (struct sockaddr *)dst, 0);
15659c9137eaSGarrett Wollman #endif
1566df8bae1dSRodney W. Grimes 	}
1567df8bae1dSRodney W. Grimes }
1568