xref: /freebsd/sys/netinet/ip_output.c (revision fbd1372a0bacc9ed390cd76c61a59f9a8b7b02ef)
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
34fbd1372aSJoerg Wunsch  *	$Id: ip_output.c,v 1.60 1997/10/12 20:25:26 phk Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
379c9137eaSGarrett Wollman #define _IP_VHL
389c9137eaSGarrett Wollman 
39fbd1372aSJoerg Wunsch #include "opt_ipdivert.h"
40fbd1372aSJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
4226f9a767SRodney W. Grimes #include <sys/systm.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
44df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
45df8bae1dSRodney W. Grimes #include <sys/protosw.h>
46df8bae1dSRodney W. Grimes #include <sys/socket.h>
47df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <net/if.h>
50df8bae1dSRodney W. Grimes #include <net/route.h>
51df8bae1dSRodney W. Grimes 
52df8bae1dSRodney W. Grimes #include <netinet/in.h>
53df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
54df8bae1dSRodney W. Grimes #include <netinet/ip.h>
55df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
56df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
57df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #ifdef vax
60df8bae1dSRodney W. Grimes #include <machine/mtpr.h>
61df8bae1dSRodney W. Grimes #endif
629c9137eaSGarrett Wollman #include <machine/in_cksum.h>
63df8bae1dSRodney W. Grimes 
64a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
6555166637SPoul-Henning Kamp 
66f9493383SGarrett Wollman #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
67f9493383SGarrett Wollman #undef COMPAT_IPFW
68f9493383SGarrett Wollman #define COMPAT_IPFW 1
69f9493383SGarrett Wollman #else
70f9493383SGarrett Wollman #undef COMPAT_IPFW
71f9493383SGarrett Wollman #endif
72f9493383SGarrett Wollman 
73f23b4c91SGarrett Wollman u_short ip_id;
74f23b4c91SGarrett Wollman 
75df8bae1dSRodney W. Grimes static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
76df8bae1dSRodney W. Grimes static void	ip_mloopback
7786b1d6d2SBill Fenner 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
780312fbe9SPoul-Henning Kamp static int	ip_getmoptions
790312fbe9SPoul-Henning Kamp 	__P((int, struct ip_moptions *, struct mbuf **));
800312fbe9SPoul-Henning Kamp static int	ip_pcbopts __P((struct mbuf **, struct mbuf *));
810312fbe9SPoul-Henning Kamp static int	ip_setmoptions
820312fbe9SPoul-Henning Kamp 	__P((int, struct ip_moptions **, struct mbuf *));
83df8bae1dSRodney W. Grimes 
84afed1b49SDarren Reed #if defined(IPFILTER_LKM) || defined(IPFILTER)
85beec8214SDarren Reed int	ip_optcopy __P((struct ip *, struct ip *));
86afed1b49SDarren Reed extern int fr_check __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
87afed1b49SDarren Reed extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
88beec8214SDarren Reed #else
89beec8214SDarren Reed static int	ip_optcopy __P((struct ip *, struct ip *));
90afed1b49SDarren Reed #endif
91afed1b49SDarren Reed 
92afed1b49SDarren Reed 
9393e0e116SJulian Elischer extern	struct protosw inetsw[];
9493e0e116SJulian Elischer 
95df8bae1dSRodney W. Grimes /*
96df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
97df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
98df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
99df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
100df8bae1dSRodney W. Grimes  */
101df8bae1dSRodney W. Grimes int
102df8bae1dSRodney W. Grimes ip_output(m0, opt, ro, flags, imo)
103df8bae1dSRodney W. Grimes 	struct mbuf *m0;
104df8bae1dSRodney W. Grimes 	struct mbuf *opt;
105df8bae1dSRodney W. Grimes 	struct route *ro;
106df8bae1dSRodney W. Grimes 	int flags;
107df8bae1dSRodney W. Grimes 	struct ip_moptions *imo;
108df8bae1dSRodney W. Grimes {
10923bf9953SPoul-Henning Kamp 	struct ip *ip, *mhip;
11023bf9953SPoul-Henning Kamp 	struct ifnet *ifp;
11123bf9953SPoul-Henning Kamp 	struct mbuf *m = m0;
11223bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
113df8bae1dSRodney W. Grimes 	int len, off, error = 0;
114df8bae1dSRodney W. Grimes 	struct sockaddr_in *dst;
115df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1169f9b3dc4SGarrett Wollman 	int isbroadcast;
117df8bae1dSRodney W. Grimes 
118df8bae1dSRodney W. Grimes #ifdef	DIAGNOSTIC
119df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
120df8bae1dSRodney W. Grimes 		panic("ip_output no HDR");
1219c9137eaSGarrett Wollman 	if (!ro)
1229c9137eaSGarrett Wollman 		panic("ip_output no route, proto = %d",
1239c9137eaSGarrett Wollman 		      mtod(m, struct ip *)->ip_p);
124df8bae1dSRodney W. Grimes #endif
125df8bae1dSRodney W. Grimes 	if (opt) {
126df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
127df8bae1dSRodney W. Grimes 		hlen = len;
128df8bae1dSRodney W. Grimes 	}
129df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
130df8bae1dSRodney W. Grimes 	/*
131df8bae1dSRodney W. Grimes 	 * Fill in IP header.
132df8bae1dSRodney W. Grimes 	 */
133df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
1349c9137eaSGarrett Wollman 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
135df8bae1dSRodney W. Grimes 		ip->ip_off &= IP_DF;
136df8bae1dSRodney W. Grimes 		ip->ip_id = htons(ip_id++);
137df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
138df8bae1dSRodney W. Grimes 	} else {
1399c9137eaSGarrett Wollman 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
140df8bae1dSRodney W. Grimes 	}
1419c9137eaSGarrett Wollman 
142df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
143df8bae1dSRodney W. Grimes 	/*
144df8bae1dSRodney W. Grimes 	 * If there is a cached route,
145df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
146df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
147df8bae1dSRodney W. Grimes 	 */
148df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
149df8bae1dSRodney W. Grimes 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
150df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
151df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
152df8bae1dSRodney W. Grimes 	}
153df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
154df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
155df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
156df8bae1dSRodney W. Grimes 		dst->sin_addr = ip->ip_dst;
157df8bae1dSRodney W. Grimes 	}
158df8bae1dSRodney W. Grimes 	/*
159df8bae1dSRodney W. Grimes 	 * If routing to interface only,
160df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
161df8bae1dSRodney W. Grimes 	 */
162df8bae1dSRodney W. Grimes #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
163df8bae1dSRodney W. Grimes #define sintosa(sin)	((struct sockaddr *)(sin))
164df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
165df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
166df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
167df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
168df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
169df8bae1dSRodney W. Grimes 			goto bad;
170df8bae1dSRodney W. Grimes 		}
171df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
172df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
1739f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
174df8bae1dSRodney W. Grimes 	} else {
1752c17fe93SGarrett Wollman 		/*
1762c17fe93SGarrett Wollman 		 * If this is the case, we probably don't want to allocate
1772c17fe93SGarrett Wollman 		 * a protocol-cloned route since we didn't get one from the
1782c17fe93SGarrett Wollman 		 * ULP.  This lets TCP do its thing, while not burdening
1792c17fe93SGarrett Wollman 		 * forwarding or ICMP with the overhead of cloning a route.
1802c17fe93SGarrett Wollman 		 * Of course, we still want to do any cloning requested by
1812c17fe93SGarrett Wollman 		 * the link layer, as this is probably required in all cases
1822c17fe93SGarrett Wollman 		 * for correct operation (as it is for ARP).
1832c17fe93SGarrett Wollman 		 */
184df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
1852c17fe93SGarrett Wollman 			rtalloc_ign(ro, RTF_PRCLONING);
186df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
187df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
188df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
189df8bae1dSRodney W. Grimes 			goto bad;
190df8bae1dSRodney W. Grimes 		}
191df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
192df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
193df8bae1dSRodney W. Grimes 		ro->ro_rt->rt_use++;
194df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
195df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
1969f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
1979f9b3dc4SGarrett Wollman 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
1989f9b3dc4SGarrett Wollman 		else
1999f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
200df8bae1dSRodney W. Grimes 	}
201df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
202df8bae1dSRodney W. Grimes 		struct in_multi *inm;
203df8bae1dSRodney W. Grimes 
204df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
205df8bae1dSRodney W. Grimes 		/*
206df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
207df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
208df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
209df8bae1dSRodney W. Grimes 		 */
210df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
211df8bae1dSRodney W. Grimes 		/*
212df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
213df8bae1dSRodney W. Grimes 		 */
214df8bae1dSRodney W. Grimes 		if (imo != NULL) {
215df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
216df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL)
217df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
2181c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
2191c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
2201c5de19aSGarrett Wollman 				    ip_mcast_src(imo->imo_multicast_vif);
221df8bae1dSRodney W. Grimes 		} else
222df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
223df8bae1dSRodney W. Grimes 		/*
224df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
225df8bae1dSRodney W. Grimes 		 */
2261c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
227df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
228df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
229df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
230df8bae1dSRodney W. Grimes 				goto bad;
231df8bae1dSRodney W. Grimes 			}
2321c5de19aSGarrett Wollman 		}
233df8bae1dSRodney W. Grimes 		/*
234df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
235df8bae1dSRodney W. Grimes 		 * of outgoing interface.
236df8bae1dSRodney W. Grimes 		 */
237df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
238df8bae1dSRodney W. Grimes 			register struct in_ifaddr *ia;
239df8bae1dSRodney W. Grimes 
24059562606SGarrett Wollman 			for (ia = in_ifaddrhead.tqh_first; ia;
24159562606SGarrett Wollman 			     ia = ia->ia_link.tqe_next)
242df8bae1dSRodney W. Grimes 				if (ia->ia_ifp == ifp) {
243df8bae1dSRodney W. Grimes 					ip->ip_src = IA_SIN(ia)->sin_addr;
244df8bae1dSRodney W. Grimes 					break;
245df8bae1dSRodney W. Grimes 				}
246df8bae1dSRodney W. Grimes 		}
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
249df8bae1dSRodney W. Grimes 		if (inm != NULL &&
250df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
251df8bae1dSRodney W. Grimes 			/*
252df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
253df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
254df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
255df8bae1dSRodney W. Grimes 			 */
25686b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
257df8bae1dSRodney W. Grimes 		}
258df8bae1dSRodney W. Grimes 		else {
259df8bae1dSRodney W. Grimes 			/*
260df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
261df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
262df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
263df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
264df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
265df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
266df8bae1dSRodney W. Grimes 			 *
267df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
268df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
269df8bae1dSRodney W. Grimes 			 * if necessary.
270df8bae1dSRodney W. Grimes 			 */
271df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
272f0068c4aSGarrett Wollman 				/*
273f0068c4aSGarrett Wollman 				 * Check if rsvp daemon is running. If not, don't
274f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
275f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
276f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
277f0068c4aSGarrett Wollman 				 */
278b124e4f2SGarrett Wollman 				if (!rsvp_on)
279f0068c4aSGarrett Wollman 				  imo = NULL;
280f0068c4aSGarrett Wollman 				if (ip_mforward(ip, ifp, m, imo) != 0) {
281df8bae1dSRodney W. Grimes 					m_freem(m);
282df8bae1dSRodney W. Grimes 					goto done;
283df8bae1dSRodney W. Grimes 				}
284df8bae1dSRodney W. Grimes 			}
285df8bae1dSRodney W. Grimes 		}
2865e9ae478SGarrett Wollman 
287df8bae1dSRodney W. Grimes 		/*
288df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
289df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
290df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
291df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
292df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
293df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
294df8bae1dSRodney W. Grimes 		 */
295f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
296df8bae1dSRodney W. Grimes 			m_freem(m);
297df8bae1dSRodney W. Grimes 			goto done;
298df8bae1dSRodney W. Grimes 		}
299df8bae1dSRodney W. Grimes 
300df8bae1dSRodney W. Grimes 		goto sendit;
301df8bae1dSRodney W. Grimes 	}
302df8bae1dSRodney W. Grimes #ifndef notdef
303df8bae1dSRodney W. Grimes 	/*
304df8bae1dSRodney W. Grimes 	 * If source address not specified yet, use address
305df8bae1dSRodney W. Grimes 	 * of outgoing interface.
306df8bae1dSRodney W. Grimes 	 */
307df8bae1dSRodney W. Grimes 	if (ip->ip_src.s_addr == INADDR_ANY)
308df8bae1dSRodney W. Grimes 		ip->ip_src = IA_SIN(ia)->sin_addr;
309df8bae1dSRodney W. Grimes #endif
310df8bae1dSRodney W. Grimes 	/*
311b5390296SDavid Greenman 	 * Verify that we have any chance at all of being able to queue
312b5390296SDavid Greenman 	 *      the packet or packet fragments
313b5390296SDavid Greenman 	 */
314b5390296SDavid Greenman 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
315b5390296SDavid Greenman 		ifp->if_snd.ifq_maxlen) {
316b5390296SDavid Greenman 			error = ENOBUFS;
317b5390296SDavid Greenman 			goto bad;
318b5390296SDavid Greenman 	}
319b5390296SDavid Greenman 
320b5390296SDavid Greenman 	/*
321df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
322df8bae1dSRodney W. Grimes 	 * and verify user is allowed to send
323df8bae1dSRodney W. Grimes 	 * such a packet.
324df8bae1dSRodney W. Grimes 	 */
3259f9b3dc4SGarrett Wollman 	if (isbroadcast) {
326df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
327df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
328df8bae1dSRodney W. Grimes 			goto bad;
329df8bae1dSRodney W. Grimes 		}
330df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
331df8bae1dSRodney W. Grimes 			error = EACCES;
332df8bae1dSRodney W. Grimes 			goto bad;
333df8bae1dSRodney W. Grimes 		}
334df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
335df8bae1dSRodney W. Grimes 		if ((u_short)ip->ip_len > ifp->if_mtu) {
336df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
337df8bae1dSRodney W. Grimes 			goto bad;
338df8bae1dSRodney W. Grimes 		}
339df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
3409f9b3dc4SGarrett Wollman 	} else {
341df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
3429f9b3dc4SGarrett Wollman 	}
343df8bae1dSRodney W. Grimes 
344f1743588SDarren Reed sendit:
345afed1b49SDarren Reed #if defined(IPFILTER) || defined(IPFILTER_LKM)
346afed1b49SDarren Reed 	/*
347afed1b49SDarren Reed 	 * looks like most checking has been done now...do a filter check
348afed1b49SDarren Reed 	 */
349f1743588SDarren Reed 	if (fr_checkp) {
350f1743588SDarren Reed 		struct  mbuf    *m1 = m;
351f1743588SDarren Reed 
352f1743588SDarren Reed 		if ((*fr_checkp)(ip, hlen, ifp, 1, &m1))
353afed1b49SDarren Reed 			error = EHOSTUNREACH;
354f1743588SDarren Reed 		if (error || !m1)
355afed1b49SDarren Reed 			goto done;
356f1743588SDarren Reed 		ip = mtod(m = m1, struct ip *);
357afed1b49SDarren Reed 	}
358afed1b49SDarren Reed #endif
359fed1c7e9SSøren Schmidt         /*
360fed1c7e9SSøren Schmidt 	 * IpHack's section.
361fed1c7e9SSøren Schmidt 	 * - Xlate: translate packet's addr/port (NAT).
362e4676ba6SJulian Elischer 	 * - Firewall: deny/allow/etc.
363fed1c7e9SSøren Schmidt 	 * - Wrap: fake packet's addr/port <unimpl.>
364fed1c7e9SSøren Schmidt 	 * - Encapsulate: put it in another IP and send out. <unimp.>
365fed1c7e9SSøren Schmidt 	 */
366fed1c7e9SSøren Schmidt 
36758938916SGarrett Wollman #ifdef COMPAT_IPFW
3686713d4a7SSøren Schmidt         if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, ifp, IP_NAT_OUT)) {
369fed1c7e9SSøren Schmidt 		error = EACCES;
370fed1c7e9SSøren Schmidt 		goto done;
371fed1c7e9SSøren Schmidt 	}
372fed1c7e9SSøren Schmidt 
373df8bae1dSRodney W. Grimes 	/*
374e7319babSPoul-Henning Kamp 	 * Check with the firewall...
375e7319babSPoul-Henning Kamp 	 */
37693e0e116SJulian Elischer 	if (ip_fw_chk_ptr) {
37793e0e116SJulian Elischer #ifdef IPDIVERT
378e4676ba6SJulian Elischer 		ip_divert_port = (*ip_fw_chk_ptr)(&ip,
379e4676ba6SJulian Elischer 		    hlen, ifp, ip_divert_ignore, &m);
380d81e4043SBrian Somers 		ip_divert_ignore = 0;
381e4676ba6SJulian Elischer 		if (ip_divert_port) {		/* Divert packet */
38293e0e116SJulian Elischer 			(*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0);
38393e0e116SJulian Elischer 			goto done;
384e4676ba6SJulian Elischer 		}
38593e0e116SJulian Elischer #else
386e4676ba6SJulian Elischer 		/* If ipfw says divert, we have to just drop packet */
387e4676ba6SJulian Elischer 		if ((*ip_fw_chk_ptr)(&ip, hlen, ifp, 0, &m)) {
388e4676ba6SJulian Elischer 			m_freem(m);
38993e0e116SJulian Elischer 			goto done;
390e4676ba6SJulian Elischer 		}
39193e0e116SJulian Elischer #endif
392e4676ba6SJulian Elischer 		if (!m) {
393e4676ba6SJulian Elischer 			error = EACCES;
394e4676ba6SJulian Elischer 			goto done;
39593e0e116SJulian Elischer 		}
39693e0e116SJulian Elischer 	}
39793e0e116SJulian Elischer #endif /* COMPAT_IPFW */
398e7319babSPoul-Henning Kamp 
399e7319babSPoul-Henning Kamp 	/*
400df8bae1dSRodney W. Grimes 	 * If small enough for interface, can just send directly.
401df8bae1dSRodney W. Grimes 	 */
402df8bae1dSRodney W. Grimes 	if ((u_short)ip->ip_len <= ifp->if_mtu) {
403df8bae1dSRodney W. Grimes 		ip->ip_len = htons((u_short)ip->ip_len);
404df8bae1dSRodney W. Grimes 		ip->ip_off = htons((u_short)ip->ip_off);
405df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
4069c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
4079c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
4089c9137eaSGarrett Wollman 		} else {
409df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
4109c9137eaSGarrett Wollman 		}
411df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
412df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
413df8bae1dSRodney W. Grimes 		goto done;
414df8bae1dSRodney W. Grimes 	}
415df8bae1dSRodney W. Grimes 	/*
416df8bae1dSRodney W. Grimes 	 * Too large for interface; fragment if possible.
417df8bae1dSRodney W. Grimes 	 * Must be able to put at least 8 bytes per fragment.
418df8bae1dSRodney W. Grimes 	 */
419df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
420df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
4213d1f141bSGarrett Wollman 		/*
4223d1f141bSGarrett Wollman 		 * This case can happen if the user changed the MTU
4233d1f141bSGarrett Wollman 		 * of an interface after enabling IP on it.  Because
4243d1f141bSGarrett Wollman 		 * most netifs don't keep track of routes pointing to
4253d1f141bSGarrett Wollman 		 * them, there is no way for one to update all its
4263d1f141bSGarrett Wollman 		 * routes when the MTU is changed.
4273d1f141bSGarrett Wollman 		 */
4283d1f141bSGarrett Wollman 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
4293d1f141bSGarrett Wollman 		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
4303d1f141bSGarrett Wollman 		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
4313d1f141bSGarrett Wollman 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
4323d1f141bSGarrett Wollman 		}
433df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
434df8bae1dSRodney W. Grimes 		goto bad;
435df8bae1dSRodney W. Grimes 	}
436df8bae1dSRodney W. Grimes 	len = (ifp->if_mtu - hlen) &~ 7;
437df8bae1dSRodney W. Grimes 	if (len < 8) {
438df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
439df8bae1dSRodney W. Grimes 		goto bad;
440df8bae1dSRodney W. Grimes 	}
441df8bae1dSRodney W. Grimes 
442df8bae1dSRodney W. Grimes     {
443df8bae1dSRodney W. Grimes 	int mhlen, firstlen = len;
444df8bae1dSRodney W. Grimes 	struct mbuf **mnext = &m->m_nextpkt;
445df8bae1dSRodney W. Grimes 
446df8bae1dSRodney W. Grimes 	/*
447df8bae1dSRodney W. Grimes 	 * Loop through length of segment after first fragment,
448df8bae1dSRodney W. Grimes 	 * make new header and copy data of each part and link onto chain.
449df8bae1dSRodney W. Grimes 	 */
450df8bae1dSRodney W. Grimes 	m0 = m;
451df8bae1dSRodney W. Grimes 	mhlen = sizeof (struct ip);
452df8bae1dSRodney W. Grimes 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
453df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
454df8bae1dSRodney W. Grimes 		if (m == 0) {
455df8bae1dSRodney W. Grimes 			error = ENOBUFS;
456df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
457df8bae1dSRodney W. Grimes 			goto sendorfree;
458df8bae1dSRodney W. Grimes 		}
459df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
460df8bae1dSRodney W. Grimes 		mhip = mtod(m, struct ip *);
461df8bae1dSRodney W. Grimes 		*mhip = *ip;
462df8bae1dSRodney W. Grimes 		if (hlen > sizeof (struct ip)) {
463df8bae1dSRodney W. Grimes 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
4649c9137eaSGarrett Wollman 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
465df8bae1dSRodney W. Grimes 		}
466df8bae1dSRodney W. Grimes 		m->m_len = mhlen;
467df8bae1dSRodney W. Grimes 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
468df8bae1dSRodney W. Grimes 		if (ip->ip_off & IP_MF)
469df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
470df8bae1dSRodney W. Grimes 		if (off + len >= (u_short)ip->ip_len)
471df8bae1dSRodney W. Grimes 			len = (u_short)ip->ip_len - off;
472df8bae1dSRodney W. Grimes 		else
473df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
474df8bae1dSRodney W. Grimes 		mhip->ip_len = htons((u_short)(len + mhlen));
475df8bae1dSRodney W. Grimes 		m->m_next = m_copy(m0, off, len);
476df8bae1dSRodney W. Grimes 		if (m->m_next == 0) {
477df8bae1dSRodney W. Grimes 			(void) m_free(m);
478df8bae1dSRodney W. Grimes 			error = ENOBUFS;	/* ??? */
479df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
480df8bae1dSRodney W. Grimes 			goto sendorfree;
481df8bae1dSRodney W. Grimes 		}
482df8bae1dSRodney W. Grimes 		m->m_pkthdr.len = mhlen + len;
483df8bae1dSRodney W. Grimes 		m->m_pkthdr.rcvif = (struct ifnet *)0;
484df8bae1dSRodney W. Grimes 		mhip->ip_off = htons((u_short)mhip->ip_off);
485df8bae1dSRodney W. Grimes 		mhip->ip_sum = 0;
4869c9137eaSGarrett Wollman 		if (mhip->ip_vhl == IP_VHL_BORING) {
487e2184122SBruce Evans 			mhip->ip_sum = in_cksum_hdr(mhip);
4889c9137eaSGarrett Wollman 		} else {
489df8bae1dSRodney W. Grimes 			mhip->ip_sum = in_cksum(m, mhlen);
4909c9137eaSGarrett Wollman 		}
491df8bae1dSRodney W. Grimes 		*mnext = m;
492df8bae1dSRodney W. Grimes 		mnext = &m->m_nextpkt;
493df8bae1dSRodney W. Grimes 		ipstat.ips_ofragments++;
494df8bae1dSRodney W. Grimes 	}
495df8bae1dSRodney W. Grimes 	/*
496df8bae1dSRodney W. Grimes 	 * Update first fragment by trimming what's been copied out
497df8bae1dSRodney W. Grimes 	 * and updating header, then send each fragment (in order).
498df8bae1dSRodney W. Grimes 	 */
499df8bae1dSRodney W. Grimes 	m = m0;
500df8bae1dSRodney W. Grimes 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
501df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hlen + firstlen;
502df8bae1dSRodney W. Grimes 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
503df8bae1dSRodney W. Grimes 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
504df8bae1dSRodney W. Grimes 	ip->ip_sum = 0;
5059c9137eaSGarrett Wollman 	if (ip->ip_vhl == IP_VHL_BORING) {
5069c9137eaSGarrett Wollman 		ip->ip_sum = in_cksum_hdr(ip);
5079c9137eaSGarrett Wollman 	} else {
508df8bae1dSRodney W. Grimes 		ip->ip_sum = in_cksum(m, hlen);
5099c9137eaSGarrett Wollman 	}
510df8bae1dSRodney W. Grimes sendorfree:
511df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m0) {
512df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
513df8bae1dSRodney W. Grimes 		m->m_nextpkt = 0;
514df8bae1dSRodney W. Grimes 		if (error == 0)
515df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
516df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
517df8bae1dSRodney W. Grimes 		else
518df8bae1dSRodney W. Grimes 			m_freem(m);
519df8bae1dSRodney W. Grimes 	}
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 	if (error == 0)
522df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
523df8bae1dSRodney W. Grimes     }
524df8bae1dSRodney W. Grimes done:
525df8bae1dSRodney W. Grimes 	return (error);
526df8bae1dSRodney W. Grimes bad:
527df8bae1dSRodney W. Grimes 	m_freem(m0);
528df8bae1dSRodney W. Grimes 	goto done;
529df8bae1dSRodney W. Grimes }
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes /*
532df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
533df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
534df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
535072b9b24SPaul Traina  *
536072b9b24SPaul Traina  * XXX This routine assumes that the packet has no options in place.
537df8bae1dSRodney W. Grimes  */
538df8bae1dSRodney W. Grimes static struct mbuf *
539df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen)
540df8bae1dSRodney W. Grimes 	register struct mbuf *m;
541df8bae1dSRodney W. Grimes 	struct mbuf *opt;
542df8bae1dSRodney W. Grimes 	int *phlen;
543df8bae1dSRodney W. Grimes {
544df8bae1dSRodney W. Grimes 	register struct ipoption *p = mtod(opt, struct ipoption *);
545df8bae1dSRodney W. Grimes 	struct mbuf *n;
546df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
547df8bae1dSRodney W. Grimes 	unsigned optlen;
548df8bae1dSRodney W. Grimes 
549df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
550df8bae1dSRodney W. Grimes 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
551df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
552df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
553df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
554df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
555df8bae1dSRodney W. Grimes 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
556df8bae1dSRodney W. Grimes 		if (n == 0)
557df8bae1dSRodney W. Grimes 			return (m);
558df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
559df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
560df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
561df8bae1dSRodney W. Grimes 		n->m_next = m;
562df8bae1dSRodney W. Grimes 		m = n;
563df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
564df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
56594a5d9b6SDavid Greenman 		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
566df8bae1dSRodney W. Grimes 	} else {
567df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
568df8bae1dSRodney W. Grimes 		m->m_len += optlen;
569df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
570df8bae1dSRodney W. Grimes 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
571df8bae1dSRodney W. Grimes 	}
572df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
5730453d3cbSBruce Evans 	bcopy(p->ipopt_list, ip + 1, optlen);
574df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
5759c9137eaSGarrett Wollman 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
576df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
577df8bae1dSRodney W. Grimes 	return (m);
578df8bae1dSRodney W. Grimes }
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes /*
581df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
582df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
583df8bae1dSRodney W. Grimes  */
584beec8214SDarren Reed #if !defined(IPFILTER) && !defined(IPFILTER_LKM)
585beec8214SDarren Reed static
586beec8214SDarren Reed #endif
587beec8214SDarren Reed int
588df8bae1dSRodney W. Grimes ip_optcopy(ip, jp)
589df8bae1dSRodney W. Grimes 	struct ip *ip, *jp;
590df8bae1dSRodney W. Grimes {
591df8bae1dSRodney W. Grimes 	register u_char *cp, *dp;
592df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
595df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
5969c9137eaSGarrett Wollman 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
597df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
598df8bae1dSRodney W. Grimes 		opt = cp[0];
599df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
600df8bae1dSRodney W. Grimes 			break;
601df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
602df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
603df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
604df8bae1dSRodney W. Grimes 			optlen = 1;
605df8bae1dSRodney W. Grimes 			continue;
606df8bae1dSRodney W. Grimes 		} else
607df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
608df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
609df8bae1dSRodney W. Grimes 		if (optlen > cnt)
610df8bae1dSRodney W. Grimes 			optlen = cnt;
611df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
6120453d3cbSBruce Evans 			bcopy(cp, dp, optlen);
613df8bae1dSRodney W. Grimes 			dp += optlen;
614df8bae1dSRodney W. Grimes 		}
615df8bae1dSRodney W. Grimes 	}
616df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
617df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
618df8bae1dSRodney W. Grimes 	return (optlen);
619df8bae1dSRodney W. Grimes }
620df8bae1dSRodney W. Grimes 
621df8bae1dSRodney W. Grimes /*
622df8bae1dSRodney W. Grimes  * IP socket option processing.
623df8bae1dSRodney W. Grimes  */
624df8bae1dSRodney W. Grimes int
625a29f300eSGarrett Wollman ip_ctloutput(op, so, level, optname, mp, p)
626df8bae1dSRodney W. Grimes 	int op;
627df8bae1dSRodney W. Grimes 	struct socket *so;
628df8bae1dSRodney W. Grimes 	int level, optname;
629df8bae1dSRodney W. Grimes 	struct mbuf **mp;
630a29f300eSGarrett Wollman 	struct proc *p;
631df8bae1dSRodney W. Grimes {
632df8bae1dSRodney W. Grimes 	register struct inpcb *inp = sotoinpcb(so);
633df8bae1dSRodney W. Grimes 	register struct mbuf *m = *mp;
63426f9a767SRodney W. Grimes 	register int optval = 0;
635df8bae1dSRodney W. Grimes 	int error = 0;
636df8bae1dSRodney W. Grimes 
637df8bae1dSRodney W. Grimes 	if (level != IPPROTO_IP) {
638df8bae1dSRodney W. Grimes 		error = EINVAL;
639df8bae1dSRodney W. Grimes 		if (op == PRCO_SETOPT && *mp)
640df8bae1dSRodney W. Grimes 			(void) m_free(*mp);
641df8bae1dSRodney W. Grimes 	} else switch (op) {
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes 	case PRCO_SETOPT:
644df8bae1dSRodney W. Grimes 		switch (optname) {
645df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
646df8bae1dSRodney W. Grimes #ifdef notyet
647df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
648df8bae1dSRodney W. Grimes 			return (ip_pcbopts(optname, &inp->inp_options, m));
649df8bae1dSRodney W. Grimes #else
650df8bae1dSRodney W. Grimes 			return (ip_pcbopts(&inp->inp_options, m));
651df8bae1dSRodney W. Grimes #endif
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes 		case IP_TOS:
654df8bae1dSRodney W. Grimes 		case IP_TTL:
655df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
656df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
657df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
65882c23ebaSBill Fenner 		case IP_RECVIF:
65940a63d93SJoerg Wunsch 			if (m == 0 || m->m_len != sizeof(int))
660df8bae1dSRodney W. Grimes 				error = EINVAL;
661df8bae1dSRodney W. Grimes 			else {
662df8bae1dSRodney W. Grimes 				optval = *mtod(m, int *);
663df8bae1dSRodney W. Grimes 				switch (optname) {
664df8bae1dSRodney W. Grimes 
665df8bae1dSRodney W. Grimes 				case IP_TOS:
666ca98b82cSDavid Greenman 					inp->inp_ip_tos = optval;
667df8bae1dSRodney W. Grimes 					break;
668df8bae1dSRodney W. Grimes 
669df8bae1dSRodney W. Grimes 				case IP_TTL:
670ca98b82cSDavid Greenman 					inp->inp_ip_ttl = optval;
671df8bae1dSRodney W. Grimes 					break;
672df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
673df8bae1dSRodney W. Grimes 	if (optval) \
674df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
675df8bae1dSRodney W. Grimes 	else \
676df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
677df8bae1dSRodney W. Grimes 
678df8bae1dSRodney W. Grimes 				case IP_RECVOPTS:
679df8bae1dSRodney W. Grimes 					OPTSET(INP_RECVOPTS);
680df8bae1dSRodney W. Grimes 					break;
681df8bae1dSRodney W. Grimes 
682df8bae1dSRodney W. Grimes 				case IP_RECVRETOPTS:
683df8bae1dSRodney W. Grimes 					OPTSET(INP_RECVRETOPTS);
684df8bae1dSRodney W. Grimes 					break;
685df8bae1dSRodney W. Grimes 
686df8bae1dSRodney W. Grimes 				case IP_RECVDSTADDR:
687df8bae1dSRodney W. Grimes 					OPTSET(INP_RECVDSTADDR);
688df8bae1dSRodney W. Grimes 					break;
68982c23ebaSBill Fenner 
69082c23ebaSBill Fenner 				case IP_RECVIF:
69182c23ebaSBill Fenner 					OPTSET(INP_RECVIF);
69282c23ebaSBill Fenner 					break;
693df8bae1dSRodney W. Grimes 				}
694df8bae1dSRodney W. Grimes 			}
695df8bae1dSRodney W. Grimes 			break;
696df8bae1dSRodney W. Grimes #undef OPTSET
697df8bae1dSRodney W. Grimes 
698df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
699f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
700df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
701df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
702df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
703df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
704df8bae1dSRodney W. Grimes 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
705df8bae1dSRodney W. Grimes 			break;
706df8bae1dSRodney W. Grimes 
70733b3ac06SPeter Wemm 		case IP_PORTRANGE:
70833b3ac06SPeter Wemm 			if (m == 0 || m->m_len != sizeof(int))
70933b3ac06SPeter Wemm 				error = EINVAL;
71033b3ac06SPeter Wemm 			else {
71133b3ac06SPeter Wemm 				optval = *mtod(m, int *);
71233b3ac06SPeter Wemm 
71333b3ac06SPeter Wemm 				switch (optval) {
71433b3ac06SPeter Wemm 
71533b3ac06SPeter Wemm 				case IP_PORTRANGE_DEFAULT:
71633b3ac06SPeter Wemm 					inp->inp_flags &= ~(INP_LOWPORT);
71733b3ac06SPeter Wemm 					inp->inp_flags &= ~(INP_HIGHPORT);
71833b3ac06SPeter Wemm 					break;
71933b3ac06SPeter Wemm 
72033b3ac06SPeter Wemm 				case IP_PORTRANGE_HIGH:
72133b3ac06SPeter Wemm 					inp->inp_flags &= ~(INP_LOWPORT);
72233b3ac06SPeter Wemm 					inp->inp_flags |= INP_HIGHPORT;
72333b3ac06SPeter Wemm 					break;
72433b3ac06SPeter Wemm 
72533b3ac06SPeter Wemm 				case IP_PORTRANGE_LOW:
72633b3ac06SPeter Wemm 					inp->inp_flags &= ~(INP_HIGHPORT);
72733b3ac06SPeter Wemm 					inp->inp_flags |= INP_LOWPORT;
72833b3ac06SPeter Wemm 					break;
72933b3ac06SPeter Wemm 
73033b3ac06SPeter Wemm 				default:
73133b3ac06SPeter Wemm 					error = EINVAL;
73233b3ac06SPeter Wemm 					break;
73333b3ac06SPeter Wemm 				}
73433b3ac06SPeter Wemm 			}
735ce8c72b1SPeter Wemm 			break;
73633b3ac06SPeter Wemm 
737df8bae1dSRodney W. Grimes 		default:
738df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
739df8bae1dSRodney W. Grimes 			break;
740df8bae1dSRodney W. Grimes 		}
741df8bae1dSRodney W. Grimes 		if (m)
742df8bae1dSRodney W. Grimes 			(void)m_free(m);
743df8bae1dSRodney W. Grimes 		break;
744df8bae1dSRodney W. Grimes 
745df8bae1dSRodney W. Grimes 	case PRCO_GETOPT:
746df8bae1dSRodney W. Grimes 		switch (optname) {
747df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
748df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
749df8bae1dSRodney W. Grimes 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
750df8bae1dSRodney W. Grimes 			if (inp->inp_options) {
751df8bae1dSRodney W. Grimes 				m->m_len = inp->inp_options->m_len;
7520453d3cbSBruce Evans 				bcopy(mtod(inp->inp_options, void *),
7530453d3cbSBruce Evans 				    mtod(m, void *), m->m_len);
754df8bae1dSRodney W. Grimes 			} else
755df8bae1dSRodney W. Grimes 				m->m_len = 0;
756df8bae1dSRodney W. Grimes 			break;
757df8bae1dSRodney W. Grimes 
758df8bae1dSRodney W. Grimes 		case IP_TOS:
759df8bae1dSRodney W. Grimes 		case IP_TTL:
760df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
761df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
762df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
76382c23ebaSBill Fenner 		case IP_RECVIF:
764df8bae1dSRodney W. Grimes 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
765df8bae1dSRodney W. Grimes 			m->m_len = sizeof(int);
766df8bae1dSRodney W. Grimes 			switch (optname) {
767df8bae1dSRodney W. Grimes 
768df8bae1dSRodney W. Grimes 			case IP_TOS:
769ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
770df8bae1dSRodney W. Grimes 				break;
771df8bae1dSRodney W. Grimes 
772df8bae1dSRodney W. Grimes 			case IP_TTL:
773ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
774df8bae1dSRodney W. Grimes 				break;
775df8bae1dSRodney W. Grimes 
776df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
777df8bae1dSRodney W. Grimes 
778df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
779df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
780df8bae1dSRodney W. Grimes 				break;
781df8bae1dSRodney W. Grimes 
782df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
783df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
784df8bae1dSRodney W. Grimes 				break;
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
787df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
788df8bae1dSRodney W. Grimes 				break;
78982c23ebaSBill Fenner 
79082c23ebaSBill Fenner 			case IP_RECVIF:
79182c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
79282c23ebaSBill Fenner 				break;
793df8bae1dSRodney W. Grimes 			}
794df8bae1dSRodney W. Grimes 			*mtod(m, int *) = optval;
795df8bae1dSRodney W. Grimes 			break;
796df8bae1dSRodney W. Grimes 
797df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
798f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
799df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
800df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
801df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
802df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
803df8bae1dSRodney W. Grimes 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
804df8bae1dSRodney W. Grimes 			break;
805df8bae1dSRodney W. Grimes 
80633b3ac06SPeter Wemm 		case IP_PORTRANGE:
80733b3ac06SPeter Wemm 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
80833b3ac06SPeter Wemm 			m->m_len = sizeof(int);
80933b3ac06SPeter Wemm 
81033b3ac06SPeter Wemm 			if (inp->inp_flags & INP_HIGHPORT)
81133b3ac06SPeter Wemm 				optval = IP_PORTRANGE_HIGH;
81233b3ac06SPeter Wemm 			else if (inp->inp_flags & INP_LOWPORT)
81333b3ac06SPeter Wemm 				optval = IP_PORTRANGE_LOW;
81433b3ac06SPeter Wemm 			else
81533b3ac06SPeter Wemm 				optval = 0;
81633b3ac06SPeter Wemm 
81733b3ac06SPeter Wemm 			*mtod(m, int *) = optval;
81833b3ac06SPeter Wemm 			break;
81933b3ac06SPeter Wemm 
820df8bae1dSRodney W. Grimes 		default:
821df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
822df8bae1dSRodney W. Grimes 			break;
823df8bae1dSRodney W. Grimes 		}
824df8bae1dSRodney W. Grimes 		break;
825df8bae1dSRodney W. Grimes 	}
826df8bae1dSRodney W. Grimes 	return (error);
827df8bae1dSRodney W. Grimes }
828df8bae1dSRodney W. Grimes 
829df8bae1dSRodney W. Grimes /*
830df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
831df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
832df8bae1dSRodney W. Grimes  * with destination address if source routed.
833df8bae1dSRodney W. Grimes  */
8340312fbe9SPoul-Henning Kamp static int
835df8bae1dSRodney W. Grimes #ifdef notyet
836df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m)
837df8bae1dSRodney W. Grimes 	int optname;
838df8bae1dSRodney W. Grimes #else
839df8bae1dSRodney W. Grimes ip_pcbopts(pcbopt, m)
840df8bae1dSRodney W. Grimes #endif
841df8bae1dSRodney W. Grimes 	struct mbuf **pcbopt;
842df8bae1dSRodney W. Grimes 	register struct mbuf *m;
843df8bae1dSRodney W. Grimes {
844df8bae1dSRodney W. Grimes 	register cnt, optlen;
845df8bae1dSRodney W. Grimes 	register u_char *cp;
846df8bae1dSRodney W. Grimes 	u_char opt;
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes 	/* turn off any old options */
849df8bae1dSRodney W. Grimes 	if (*pcbopt)
850df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
851df8bae1dSRodney W. Grimes 	*pcbopt = 0;
852df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
853df8bae1dSRodney W. Grimes 		/*
854df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
855df8bae1dSRodney W. Grimes 		 */
856df8bae1dSRodney W. Grimes 		if (m)
857df8bae1dSRodney W. Grimes 			(void)m_free(m);
858df8bae1dSRodney W. Grimes 		return (0);
859df8bae1dSRodney W. Grimes 	}
860df8bae1dSRodney W. Grimes 
861df8bae1dSRodney W. Grimes #ifndef	vax
862df8bae1dSRodney W. Grimes 	if (m->m_len % sizeof(long))
863df8bae1dSRodney W. Grimes 		goto bad;
864df8bae1dSRodney W. Grimes #endif
865df8bae1dSRodney W. Grimes 	/*
866df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
867df8bae1dSRodney W. Grimes 	 * actual options; move other options back
868df8bae1dSRodney W. Grimes 	 * and clear it when none present.
869df8bae1dSRodney W. Grimes 	 */
870df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
871df8bae1dSRodney W. Grimes 		goto bad;
872df8bae1dSRodney W. Grimes 	cnt = m->m_len;
873df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
874df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
875df8bae1dSRodney W. Grimes 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
876df8bae1dSRodney W. Grimes 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
877df8bae1dSRodney W. Grimes 
878df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
879df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
880df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
881df8bae1dSRodney W. Grimes 			break;
882df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
883df8bae1dSRodney W. Grimes 			optlen = 1;
884df8bae1dSRodney W. Grimes 		else {
885df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
886df8bae1dSRodney W. Grimes 			if (optlen <= IPOPT_OLEN || optlen > cnt)
887df8bae1dSRodney W. Grimes 				goto bad;
888df8bae1dSRodney W. Grimes 		}
889df8bae1dSRodney W. Grimes 		switch (opt) {
890df8bae1dSRodney W. Grimes 
891df8bae1dSRodney W. Grimes 		default:
892df8bae1dSRodney W. Grimes 			break;
893df8bae1dSRodney W. Grimes 
894df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
895df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
896df8bae1dSRodney W. Grimes 			/*
897df8bae1dSRodney W. Grimes 			 * user process specifies route as:
898df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
899df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
900df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
901df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
902df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
903df8bae1dSRodney W. Grimes 			 */
904df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
905df8bae1dSRodney W. Grimes 				goto bad;
906df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
907df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
908df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
909df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
910df8bae1dSRodney W. Grimes 			/*
911df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
912df8bae1dSRodney W. Grimes 			 */
913df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
914df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
915df8bae1dSRodney W. Grimes 			/*
916df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
917df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
918df8bae1dSRodney W. Grimes 			 */
919df8bae1dSRodney W. Grimes 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
920df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr)),
921df8bae1dSRodney W. Grimes 			    (caddr_t)&cp[IPOPT_OFFSET+1],
922df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
923df8bae1dSRodney W. Grimes 			break;
924df8bae1dSRodney W. Grimes 		}
925df8bae1dSRodney W. Grimes 	}
926df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
927df8bae1dSRodney W. Grimes 		goto bad;
928df8bae1dSRodney W. Grimes 	*pcbopt = m;
929df8bae1dSRodney W. Grimes 	return (0);
930df8bae1dSRodney W. Grimes 
931df8bae1dSRodney W. Grimes bad:
932df8bae1dSRodney W. Grimes 	(void)m_free(m);
933df8bae1dSRodney W. Grimes 	return (EINVAL);
934df8bae1dSRodney W. Grimes }
935df8bae1dSRodney W. Grimes 
936df8bae1dSRodney W. Grimes /*
937df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
938df8bae1dSRodney W. Grimes  */
9390312fbe9SPoul-Henning Kamp static int
940df8bae1dSRodney W. Grimes ip_setmoptions(optname, imop, m)
941df8bae1dSRodney W. Grimes 	int optname;
942df8bae1dSRodney W. Grimes 	struct ip_moptions **imop;
943df8bae1dSRodney W. Grimes 	struct mbuf *m;
944df8bae1dSRodney W. Grimes {
945df8bae1dSRodney W. Grimes 	register int error = 0;
946df8bae1dSRodney W. Grimes 	u_char loop;
947df8bae1dSRodney W. Grimes 	register int i;
948df8bae1dSRodney W. Grimes 	struct in_addr addr;
949df8bae1dSRodney W. Grimes 	register struct ip_mreq *mreq;
950df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
951df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo = *imop;
952df8bae1dSRodney W. Grimes 	struct route ro;
953df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
9549b626c29SGarrett Wollman 	int s;
955df8bae1dSRodney W. Grimes 
956df8bae1dSRodney W. Grimes 	if (imo == NULL) {
957df8bae1dSRodney W. Grimes 		/*
958df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
959df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
960df8bae1dSRodney W. Grimes 		 */
961df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
962df8bae1dSRodney W. Grimes 		    M_WAITOK);
963df8bae1dSRodney W. Grimes 
964df8bae1dSRodney W. Grimes 		if (imo == NULL)
965df8bae1dSRodney W. Grimes 			return (ENOBUFS);
966df8bae1dSRodney W. Grimes 		*imop = imo;
967df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
9681c5de19aSGarrett Wollman 		imo->imo_multicast_vif = -1;
969df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
970df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
971df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
972df8bae1dSRodney W. Grimes 	}
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes 	switch (optname) {
975f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
976f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
9775e9ae478SGarrett Wollman 		if (!legal_vif_num) {
9785e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
9795e9ae478SGarrett Wollman 			break;
9805e9ae478SGarrett Wollman 		}
981f0068c4aSGarrett Wollman 		if (m == NULL || m->m_len != sizeof(int)) {
982f0068c4aSGarrett Wollman 			error = EINVAL;
983f0068c4aSGarrett Wollman 			break;
984f0068c4aSGarrett Wollman 		}
985f0068c4aSGarrett Wollman 		i = *(mtod(m, int *));
9861c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
987f0068c4aSGarrett Wollman 			error = EINVAL;
988f0068c4aSGarrett Wollman 			break;
989f0068c4aSGarrett Wollman 		}
990f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
991f0068c4aSGarrett Wollman 		break;
992f0068c4aSGarrett Wollman 
993df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
994df8bae1dSRodney W. Grimes 		/*
995df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
996df8bae1dSRodney W. Grimes 		 */
997df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
998df8bae1dSRodney W. Grimes 			error = EINVAL;
999df8bae1dSRodney W. Grimes 			break;
1000df8bae1dSRodney W. Grimes 		}
1001df8bae1dSRodney W. Grimes 		addr = *(mtod(m, struct in_addr *));
1002df8bae1dSRodney W. Grimes 		/*
1003df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1004df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1005df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1006df8bae1dSRodney W. Grimes 		 */
1007df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1008df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1009df8bae1dSRodney W. Grimes 			break;
1010df8bae1dSRodney W. Grimes 		}
1011df8bae1dSRodney W. Grimes 		/*
1012df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1013df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1014df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1015df8bae1dSRodney W. Grimes 		 */
101620e8807cSGarrett Wollman 		s = splimp();
1017df8bae1dSRodney W. Grimes 		INADDR_TO_IFP(addr, ifp);
1018df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1019fbc6ab00SBill Fenner 			splx(s);
1020df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1021df8bae1dSRodney W. Grimes 			break;
1022df8bae1dSRodney W. Grimes 		}
1023df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
10249b626c29SGarrett Wollman 		splx(s);
1025df8bae1dSRodney W. Grimes 		break;
1026df8bae1dSRodney W. Grimes 
1027df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1028df8bae1dSRodney W. Grimes 		/*
1029df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1030df8bae1dSRodney W. Grimes 		 */
1031df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != 1) {
1032df8bae1dSRodney W. Grimes 			error = EINVAL;
1033df8bae1dSRodney W. Grimes 			break;
1034df8bae1dSRodney W. Grimes 		}
1035df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = *(mtod(m, u_char *));
1036df8bae1dSRodney W. Grimes 		break;
1037df8bae1dSRodney W. Grimes 
1038df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1039df8bae1dSRodney W. Grimes 		/*
1040df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1041df8bae1dSRodney W. Grimes 		 * Must be zero or one.
1042df8bae1dSRodney W. Grimes 		 */
1043df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != 1 ||
1044df8bae1dSRodney W. Grimes 		   (loop = *(mtod(m, u_char *))) > 1) {
1045df8bae1dSRodney W. Grimes 			error = EINVAL;
1046df8bae1dSRodney W. Grimes 			break;
1047df8bae1dSRodney W. Grimes 		}
1048df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = loop;
1049df8bae1dSRodney W. Grimes 		break;
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1052df8bae1dSRodney W. Grimes 		/*
1053df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1054df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1055df8bae1dSRodney W. Grimes 		 */
1056df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1057df8bae1dSRodney W. Grimes 			error = EINVAL;
1058df8bae1dSRodney W. Grimes 			break;
1059df8bae1dSRodney W. Grimes 		}
1060df8bae1dSRodney W. Grimes 		mreq = mtod(m, struct ip_mreq *);
1061df8bae1dSRodney W. Grimes 		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1062df8bae1dSRodney W. Grimes 			error = EINVAL;
1063df8bae1dSRodney W. Grimes 			break;
1064df8bae1dSRodney W. Grimes 		}
106520e8807cSGarrett Wollman 		s = splimp();
1066df8bae1dSRodney W. Grimes 		/*
1067df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1068df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1069df8bae1dSRodney W. Grimes 		 */
1070df8bae1dSRodney W. Grimes 		if (mreq->imr_interface.s_addr == INADDR_ANY) {
10711c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1072df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1073df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1074df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1075df8bae1dSRodney W. Grimes 			dst->sin_addr = mreq->imr_multiaddr;
1076df8bae1dSRodney W. Grimes 			rtalloc(&ro);
1077df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1078df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
10799b626c29SGarrett Wollman 				splx(s);
1080df8bae1dSRodney W. Grimes 				break;
1081df8bae1dSRodney W. Grimes 			}
1082df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1083df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
1084df8bae1dSRodney W. Grimes 		}
1085df8bae1dSRodney W. Grimes 		else {
1086df8bae1dSRodney W. Grimes 			INADDR_TO_IFP(mreq->imr_interface, ifp);
1087df8bae1dSRodney W. Grimes 		}
10889b626c29SGarrett Wollman 
1089df8bae1dSRodney W. Grimes 		/*
1090df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1091df8bae1dSRodney W. Grimes 		 * supports multicast.
1092df8bae1dSRodney W. Grimes 		 */
1093df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1094df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
10959b626c29SGarrett Wollman 			splx(s);
1096df8bae1dSRodney W. Grimes 			break;
1097df8bae1dSRodney W. Grimes 		}
1098df8bae1dSRodney W. Grimes 		/*
1099df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1100df8bae1dSRodney W. Grimes 		 * membership slots are full.
1101df8bae1dSRodney W. Grimes 		 */
1102df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1103df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1104df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1105df8bae1dSRodney W. Grimes 						== mreq->imr_multiaddr.s_addr)
1106df8bae1dSRodney W. Grimes 				break;
1107df8bae1dSRodney W. Grimes 		}
1108df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1109df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
11109b626c29SGarrett Wollman 			splx(s);
1111df8bae1dSRodney W. Grimes 			break;
1112df8bae1dSRodney W. Grimes 		}
1113df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
1114df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
11159b626c29SGarrett Wollman 			splx(s);
1116df8bae1dSRodney W. Grimes 			break;
1117df8bae1dSRodney W. Grimes 		}
1118df8bae1dSRodney W. Grimes 		/*
1119df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1120df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1121df8bae1dSRodney W. Grimes 		 */
1122df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1123df8bae1dSRodney W. Grimes 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1124df8bae1dSRodney W. Grimes 			error = ENOBUFS;
11259b626c29SGarrett Wollman 			splx(s);
1126df8bae1dSRodney W. Grimes 			break;
1127df8bae1dSRodney W. Grimes 		}
1128df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
11299b626c29SGarrett Wollman 		splx(s);
1130df8bae1dSRodney W. Grimes 		break;
1131df8bae1dSRodney W. Grimes 
1132df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1133df8bae1dSRodney W. Grimes 		/*
1134df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1135df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1136df8bae1dSRodney W. Grimes 		 */
1137df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1138df8bae1dSRodney W. Grimes 			error = EINVAL;
1139df8bae1dSRodney W. Grimes 			break;
1140df8bae1dSRodney W. Grimes 		}
1141df8bae1dSRodney W. Grimes 		mreq = mtod(m, struct ip_mreq *);
1142df8bae1dSRodney W. Grimes 		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1143df8bae1dSRodney W. Grimes 			error = EINVAL;
1144df8bae1dSRodney W. Grimes 			break;
1145df8bae1dSRodney W. Grimes 		}
11469b626c29SGarrett Wollman 
114720e8807cSGarrett Wollman 		s = splimp();
1148df8bae1dSRodney W. Grimes 		/*
1149df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1150df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1151df8bae1dSRodney W. Grimes 		 */
1152df8bae1dSRodney W. Grimes 		if (mreq->imr_interface.s_addr == INADDR_ANY)
1153df8bae1dSRodney W. Grimes 			ifp = NULL;
1154df8bae1dSRodney W. Grimes 		else {
1155df8bae1dSRodney W. Grimes 			INADDR_TO_IFP(mreq->imr_interface, ifp);
1156df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1157df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
11589b626c29SGarrett Wollman 				splx(s);
1159df8bae1dSRodney W. Grimes 				break;
1160df8bae1dSRodney W. Grimes 			}
1161df8bae1dSRodney W. Grimes 		}
1162df8bae1dSRodney W. Grimes 		/*
1163df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1164df8bae1dSRodney W. Grimes 		 */
1165df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1166df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1167df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1168df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1169df8bae1dSRodney W. Grimes 			     mreq->imr_multiaddr.s_addr)
1170df8bae1dSRodney W. Grimes 				break;
1171df8bae1dSRodney W. Grimes 		}
1172df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1173df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
11749b626c29SGarrett Wollman 			splx(s);
1175df8bae1dSRodney W. Grimes 			break;
1176df8bae1dSRodney W. Grimes 		}
1177df8bae1dSRodney W. Grimes 		/*
1178df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1179df8bae1dSRodney W. Grimes 		 * membership points.
1180df8bae1dSRodney W. Grimes 		 */
1181df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1182df8bae1dSRodney W. Grimes 		/*
1183df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1184df8bae1dSRodney W. Grimes 		 */
1185df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1186df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1187df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
11889b626c29SGarrett Wollman 		splx(s);
1189df8bae1dSRodney W. Grimes 		break;
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes 	default:
1192df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1193df8bae1dSRodney W. Grimes 		break;
1194df8bae1dSRodney W. Grimes 	}
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes 	/*
1197df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
1198df8bae1dSRodney W. Grimes 	 */
1199df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
12001c5de19aSGarrett Wollman 	    imo->imo_multicast_vif == -1 &&
1201df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1202df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1203df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
1204df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
1205df8bae1dSRodney W. Grimes 		*imop = NULL;
1206df8bae1dSRodney W. Grimes 	}
1207df8bae1dSRodney W. Grimes 
1208df8bae1dSRodney W. Grimes 	return (error);
1209df8bae1dSRodney W. Grimes }
1210df8bae1dSRodney W. Grimes 
1211df8bae1dSRodney W. Grimes /*
1212df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1213df8bae1dSRodney W. Grimes  */
12140312fbe9SPoul-Henning Kamp static int
1215df8bae1dSRodney W. Grimes ip_getmoptions(optname, imo, mp)
1216df8bae1dSRodney W. Grimes 	int optname;
1217df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1218df8bae1dSRodney W. Grimes 	register struct mbuf **mp;
1219df8bae1dSRodney W. Grimes {
1220df8bae1dSRodney W. Grimes 	u_char *ttl;
1221df8bae1dSRodney W. Grimes 	u_char *loop;
1222df8bae1dSRodney W. Grimes 	struct in_addr *addr;
1223df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1224df8bae1dSRodney W. Grimes 
1225df8bae1dSRodney W. Grimes 	*mp = m_get(M_WAIT, MT_SOOPTS);
1226df8bae1dSRodney W. Grimes 
1227df8bae1dSRodney W. Grimes 	switch (optname) {
1228df8bae1dSRodney W. Grimes 
1229f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1230f0068c4aSGarrett Wollman 		if (imo != NULL)
1231f0068c4aSGarrett Wollman 			*(mtod(*mp, int *)) = imo->imo_multicast_vif;
1232f0068c4aSGarrett Wollman 		else
12331c5de19aSGarrett Wollman 			*(mtod(*mp, int *)) = -1;
1234f0068c4aSGarrett Wollman 		(*mp)->m_len = sizeof(int);
1235f0068c4aSGarrett Wollman 		return(0);
1236f0068c4aSGarrett Wollman 
1237df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1238df8bae1dSRodney W. Grimes 		addr = mtod(*mp, struct in_addr *);
1239df8bae1dSRodney W. Grimes 		(*mp)->m_len = sizeof(struct in_addr);
1240df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1241df8bae1dSRodney W. Grimes 			addr->s_addr = INADDR_ANY;
1242df8bae1dSRodney W. Grimes 		else {
1243df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1244df8bae1dSRodney W. Grimes 			addr->s_addr = (ia == NULL) ? INADDR_ANY
1245df8bae1dSRodney W. Grimes 					: IA_SIN(ia)->sin_addr.s_addr;
1246df8bae1dSRodney W. Grimes 		}
1247df8bae1dSRodney W. Grimes 		return (0);
1248df8bae1dSRodney W. Grimes 
1249df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1250df8bae1dSRodney W. Grimes 		ttl = mtod(*mp, u_char *);
1251df8bae1dSRodney W. Grimes 		(*mp)->m_len = 1;
1252df8bae1dSRodney W. Grimes 		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1253df8bae1dSRodney W. Grimes 				     : imo->imo_multicast_ttl;
1254df8bae1dSRodney W. Grimes 		return (0);
1255df8bae1dSRodney W. Grimes 
1256df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1257df8bae1dSRodney W. Grimes 		loop = mtod(*mp, u_char *);
1258df8bae1dSRodney W. Grimes 		(*mp)->m_len = 1;
1259df8bae1dSRodney W. Grimes 		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1260df8bae1dSRodney W. Grimes 				      : imo->imo_multicast_loop;
1261df8bae1dSRodney W. Grimes 		return (0);
1262df8bae1dSRodney W. Grimes 
1263df8bae1dSRodney W. Grimes 	default:
1264df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
1265df8bae1dSRodney W. Grimes 	}
1266df8bae1dSRodney W. Grimes }
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes /*
1269df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1270df8bae1dSRodney W. Grimes  */
1271df8bae1dSRodney W. Grimes void
1272df8bae1dSRodney W. Grimes ip_freemoptions(imo)
1273df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1274df8bae1dSRodney W. Grimes {
1275df8bae1dSRodney W. Grimes 	register int i;
1276df8bae1dSRodney W. Grimes 
1277df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1278df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1279df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1280df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1281df8bae1dSRodney W. Grimes 	}
1282df8bae1dSRodney W. Grimes }
1283df8bae1dSRodney W. Grimes 
1284df8bae1dSRodney W. Grimes /*
1285df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1286df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1287df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1288f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1289f5fea3ddSPaul Traina  * replicating that code here.
1290df8bae1dSRodney W. Grimes  */
1291df8bae1dSRodney W. Grimes static void
129286b1d6d2SBill Fenner ip_mloopback(ifp, m, dst, hlen)
1293df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1294df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1295df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
129686b1d6d2SBill Fenner 	int hlen;
1297df8bae1dSRodney W. Grimes {
1298df8bae1dSRodney W. Grimes 	register struct ip *ip;
1299df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1300df8bae1dSRodney W. Grimes 
1301df8bae1dSRodney W. Grimes 	copym = m_copy(m, 0, M_COPYALL);
130286b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
130386b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1304df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1305df8bae1dSRodney W. Grimes 		/*
1306df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1307df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1308df8bae1dSRodney W. Grimes 		 */
1309df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1310df8bae1dSRodney W. Grimes 		ip->ip_len = htons((u_short)ip->ip_len);
1311df8bae1dSRodney W. Grimes 		ip->ip_off = htons((u_short)ip->ip_off);
1312df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
13139c9137eaSGarrett Wollman 		if (ip->ip_vhl == IP_VHL_BORING) {
13149c9137eaSGarrett Wollman 			ip->ip_sum = in_cksum_hdr(ip);
13159c9137eaSGarrett Wollman 		} else {
131686b1d6d2SBill Fenner 			ip->ip_sum = in_cksum(copym, hlen);
13179c9137eaSGarrett Wollman 		}
13189c9137eaSGarrett Wollman 		/*
13199c9137eaSGarrett Wollman 		 * NB:
1320e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1321e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1322e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1323e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1324e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1325e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1326e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1327e1596dffSBill Fenner 		 * of safety by continuing to abuse looutput().
13289c9137eaSGarrett Wollman 		 */
13299c9137eaSGarrett Wollman #ifdef notdef
1330e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
13319c9137eaSGarrett Wollman 		ip_input(copym)
13329c9137eaSGarrett Wollman #else
1333df8bae1dSRodney W. Grimes 		(void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
13349c9137eaSGarrett Wollman #endif
1335df8bae1dSRodney W. Grimes 	}
1336df8bae1dSRodney W. Grimes }
1337