ip_output.c revision 128003
1292920Sdim/*
2292920Sdim * Copyright (c) 1982, 1986, 1988, 1990, 1993
3292920Sdim *	The Regents of the University of California.  All rights reserved.
4292920Sdim *
5292920Sdim * Redistribution and use in source and binary forms, with or without
6292920Sdim * modification, are permitted provided that the following conditions
7292920Sdim * are met:
8292920Sdim * 1. Redistributions of source code must retain the above copyright
9292920Sdim *    notice, this list of conditions and the following disclaimer.
10292920Sdim * 2. Redistributions in binary form must reproduce the above copyright
11292920Sdim *    notice, this list of conditions and the following disclaimer in the
12292920Sdim *    documentation and/or other materials provided with the distribution.
13292920Sdim * 3. All advertising materials mentioning features or use of this software
14292920Sdim *    must display the following acknowledgement:
15292920Sdim *	This product includes software developed by the University of
16292920Sdim *	California, Berkeley and its contributors.
17292920Sdim * 4. Neither the name of the University nor the names of its contributors
18292920Sdim *    may be used to endorse or promote products derived from this software
19292920Sdim *    without specific prior written permission.
20292920Sdim *
21292920Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22292920Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23292920Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24292920Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25292920Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26292920Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27292920Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28292920Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29292920Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30292920Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31292920Sdim * SUCH DAMAGE.
32292920Sdim *
33292920Sdim *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
34292920Sdim * $FreeBSD: head/sys/netinet/ip_output.c 128003 2004-04-07 10:01:39Z ru $
35292920Sdim */
36292920Sdim
37292920Sdim#include "opt_ipfw.h"
38292920Sdim#include "opt_ipdn.h"
39292920Sdim#include "opt_ipdivert.h"
40292920Sdim#include "opt_ipfilter.h"
41292920Sdim#include "opt_ipsec.h"
42292920Sdim#include "opt_mac.h"
43292920Sdim#include "opt_pfil_hooks.h"
44292920Sdim#include "opt_random_ip_id.h"
45292920Sdim#include "opt_mbuf_stress_test.h"
46292920Sdim
47292920Sdim#include <sys/param.h>
48292920Sdim#include <sys/systm.h>
49292920Sdim#include <sys/kernel.h>
50292920Sdim#include <sys/mac.h>
51292920Sdim#include <sys/malloc.h>
52292920Sdim#include <sys/mbuf.h>
53292920Sdim#include <sys/protosw.h>
54292920Sdim#include <sys/socket.h>
55292920Sdim#include <sys/socketvar.h>
56292920Sdim#include <sys/sysctl.h>
57292920Sdim
58292920Sdim#include <net/if.h>
59292920Sdim#include <net/route.h>
60292920Sdim
61292920Sdim#include <netinet/in.h>
62292920Sdim#include <netinet/in_systm.h>
63292920Sdim#include <netinet/ip.h>
64292920Sdim#include <netinet/in_pcb.h>
65292920Sdim#include <netinet/in_var.h>
66292920Sdim#include <netinet/ip_var.h>
67292920Sdim
68292920Sdim#ifdef PFIL_HOOKS
69292920Sdim#include <net/pfil.h>
70292920Sdim#endif
71292920Sdim
72292920Sdim#include <machine/in_cksum.h>
73292920Sdim
74292920Sdimstatic MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
75292920Sdim
76292920Sdim#ifdef IPSEC
77292920Sdim#include <netinet6/ipsec.h>
78292920Sdim#include <netkey/key.h>
79292920Sdim#ifdef IPSEC_DEBUG
80292920Sdim#include <netkey/key_debug.h>
81292920Sdim#else
82292920Sdim#define	KEYDEBUG(lev,arg)
83292920Sdim#endif
84292920Sdim#endif /*IPSEC*/
85292920Sdim
86292920Sdim#ifdef FAST_IPSEC
87292920Sdim#include <netipsec/ipsec.h>
88292920Sdim#include <netipsec/xform.h>
89292920Sdim#include <netipsec/key.h>
90292920Sdim#endif /*FAST_IPSEC*/
91292920Sdim
92292920Sdim#include <netinet/ip_fw.h>
93292920Sdim#include <netinet/ip_divert.h>
94292920Sdim#include <netinet/ip_dummynet.h>
95292920Sdim
96292920Sdim#define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
97292920Sdim				x, (ntohl(a.s_addr)>>24)&0xFF,\
98292920Sdim				  (ntohl(a.s_addr)>>16)&0xFF,\
99292920Sdim				  (ntohl(a.s_addr)>>8)&0xFF,\
100292920Sdim				  (ntohl(a.s_addr))&0xFF, y);
101292920Sdim
102292920Sdimu_short ip_id;
103292920Sdim
104292920Sdim#ifdef MBUF_STRESS_TEST
105292920Sdimint mbuf_frag_size = 0;
106292920SdimSYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
107292920Sdim	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
108292920Sdim#endif
109292920Sdim
110292920Sdimstatic struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
111292920Sdimstatic struct ifnet *ip_multicast_if(struct in_addr *, int *);
112292920Sdimstatic void	ip_mloopback
113292920Sdim	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
114292920Sdimstatic int	ip_getmoptions
115292920Sdim	(struct sockopt *, struct ip_moptions *);
116292920Sdimstatic int	ip_pcbopts(int, struct mbuf **, struct mbuf *);
117292920Sdimstatic int	ip_setmoptions
118292920Sdim	(struct sockopt *, struct ip_moptions **);
119292920Sdim
120292920Sdimint	ip_optcopy(struct ip *, struct ip *);
121292920Sdim
122292920Sdim
123292920Sdimextern	struct protosw inetsw[];
124292920Sdim
125292920Sdim/*
126292920Sdim * IP output.  The packet in mbuf chain m contains a skeletal IP
127292920Sdim * header (with len, off, ttl, proto, tos, src, dst).
128292920Sdim * The mbuf chain containing the packet will be freed.
129292920Sdim * The mbuf opt, if present, will not be freed.
130292920Sdim * In the IP forwarding case, the packet will arrive with options already
131292920Sdim * inserted, so must have a NULL opt pointer.
132292920Sdim */
133292920Sdimint
134292920Sdimip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
135292920Sdim	int flags, struct ip_moptions *imo, struct inpcb *inp)
136292920Sdim{
137292920Sdim	struct ip *ip;
138292920Sdim	struct ifnet *ifp = NULL;	/* keep compiler happy */
139292920Sdim	struct mbuf *m0;
140292920Sdim	int hlen = sizeof (struct ip);
141292920Sdim	int len, off, error = 0;
142292920Sdim	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
143292920Sdim	struct in_ifaddr *ia = NULL;
144292920Sdim	int isbroadcast, sw_csum;
145292920Sdim	struct in_addr pkt_dst;
146292920Sdim	struct route iproute;
147292920Sdim	struct m_tag *mtag, *dummytag;
148292920Sdim#ifdef IPSEC
149292920Sdim	struct secpolicy *sp = NULL;
150292920Sdim#endif
151292920Sdim#ifdef FAST_IPSEC
152292920Sdim	struct secpolicy *sp = NULL;
153292920Sdim	struct tdb_ident *tdbi;
154292920Sdim	int s;
155292920Sdim#endif /* FAST_IPSEC */
156292920Sdim	struct ip_fw_args args;
157292920Sdim	int src_was_INADDR_ANY = 0;	/* as the name says... */
158292920Sdim
159292920Sdim	args.eh = NULL;
160292920Sdim	args.rule = NULL;
161292920Sdim
162292920Sdim	M_ASSERTPKTHDR(m);
163292920Sdim
164292920Sdim	args.next_hop = ip_claim_next_hop(m);
165292920Sdim	dummytag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
166292920Sdim	if (dummytag != NULL) {
167292920Sdim		struct dn_pkt_tag *dt = (struct dn_pkt_tag *)(dummytag+1);
168292920Sdim		/*
169292920Sdim		 * Prevent lower layers from finding the tag
170292920Sdim		 * Cleanup and free is done below
171292920Sdim		 */
172292920Sdim		m_tag_unlink(m, dummytag);
173292920Sdim		/*
174292920Sdim		 * the packet was already tagged, so part of the
175292920Sdim		 * processing was already done, and we need to go down.
176292920Sdim		 * Get parameters from the header.
177292920Sdim		 */
178292920Sdim		args.rule = dt->rule;
179292920Sdim		ro = &(dt->ro);
180292920Sdim		dst = dt->dn_dst;
181292920Sdim		ifp = dt->ifp;
182292920Sdim	}
183292920Sdim
184292920Sdim	if (ro == NULL) {
185292920Sdim		ro = &iproute;
186292920Sdim		bzero(ro, sizeof (*ro));
187292920Sdim	}
188292920Sdim
189292920Sdim	if (inp != NULL)
190292920Sdim		INP_LOCK_ASSERT(inp);
191292920Sdim
192292920Sdim	if (args.rule != NULL) {	/* dummynet already saw us */
193292920Sdim		ip = mtod(m, struct ip *);
194292920Sdim		hlen = ip->ip_hl << 2 ;
195292920Sdim		if (ro->ro_rt)
196292920Sdim			ia = ifatoia(ro->ro_rt->rt_ifa);
197292920Sdim		goto sendit;
198292920Sdim	}
199292920Sdim
200292920Sdim	if (opt) {
201292920Sdim		len = 0;
202292920Sdim		m = ip_insertoptions(m, opt, &len);
203292920Sdim		if (len != 0)
204292920Sdim			hlen = len;
205292920Sdim	}
206292920Sdim	ip = mtod(m, struct ip *);
207292920Sdim	pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
208292920Sdim
209292920Sdim	/*
210292920Sdim	 * Fill in IP header.  If we are not allowing fragmentation,
211292920Sdim	 * then the ip_id field is meaningless, but we don't set it
212292920Sdim	 * to zero.  Doing so causes various problems when devices along
213292920Sdim	 * the path (routers, load balancers, firewalls, etc.) illegally
214292920Sdim	 * disable DF on our packet.  Note that a 16-bit counter
215292920Sdim	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
216292920Sdim	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
217292920Sdim	 * for Counting NATted Hosts", Proc. IMW'02, available at
218292920Sdim	 * <http://www.research.att.com/~smb/papers/fnat.pdf>.
219292920Sdim	 */
220292920Sdim	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
221292920Sdim		ip->ip_v = IPVERSION;
222292920Sdim		ip->ip_hl = hlen >> 2;
223292920Sdim#ifdef RANDOM_IP_ID
224292920Sdim		ip->ip_id = ip_randomid();
225292920Sdim#else
226292920Sdim		ip->ip_id = htons(ip_id++);
227292920Sdim#endif
228292920Sdim		ipstat.ips_localout++;
229292920Sdim	} else {
230292920Sdim		hlen = ip->ip_hl << 2;
231292920Sdim	}
232292920Sdim
233292920Sdim	dst = (struct sockaddr_in *)&ro->ro_dst;
234292920Sdim	/*
235292920Sdim	 * If there is a cached route,
236292920Sdim	 * check that it is to the same destination
237292920Sdim	 * and is still up.  If not, free it and try again.
238292920Sdim	 * The address family should also be checked in case of sharing the
239292920Sdim	 * cache with IPv6.
240292920Sdim	 */
241292920Sdim	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
242292920Sdim			  dst->sin_family != AF_INET ||
243292920Sdim			  dst->sin_addr.s_addr != pkt_dst.s_addr)) {
244292920Sdim		RTFREE(ro->ro_rt);
245292920Sdim		ro->ro_rt = (struct rtentry *)0;
246292920Sdim	}
247292920Sdim	if (ro->ro_rt == 0) {
248292920Sdim		bzero(dst, sizeof(*dst));
249292920Sdim		dst->sin_family = AF_INET;
250292920Sdim		dst->sin_len = sizeof(*dst);
251292920Sdim		dst->sin_addr = pkt_dst;
252292920Sdim	}
253292920Sdim	/*
254292920Sdim	 * If routing to interface only,
255292920Sdim	 * short circuit routing lookup.
256292920Sdim	 */
257292920Sdim	if (flags & IP_ROUTETOIF) {
258292920Sdim		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
259292920Sdim		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
260292920Sdim			ipstat.ips_noroute++;
261292920Sdim			error = ENETUNREACH;
262292920Sdim			goto bad;
263292920Sdim		}
264292920Sdim		ifp = ia->ia_ifp;
265292920Sdim		ip->ip_ttl = 1;
266292920Sdim		isbroadcast = in_broadcast(dst->sin_addr, ifp);
267292920Sdim	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
268292920Sdim	    imo != NULL && imo->imo_multicast_ifp != NULL) {
269292920Sdim		/*
270292920Sdim		 * Bypass the normal routing lookup for multicast
271292920Sdim		 * packets if the interface is specified.
272292920Sdim		 */
273292920Sdim		ifp = imo->imo_multicast_ifp;
274292920Sdim		IFP_TO_IA(ifp, ia);
275292920Sdim		isbroadcast = 0;	/* fool gcc */
276292920Sdim	} else {
277292920Sdim		/*
278292920Sdim		 * We want to do any cloning requested by the link layer,
279292920Sdim		 * as this is probably required in all cases for correct
280292920Sdim		 * operation (as it is for ARP).
281292920Sdim		 */
282292920Sdim		if (ro->ro_rt == 0)
283292920Sdim			rtalloc(ro);
284292920Sdim		if (ro->ro_rt == 0) {
285292920Sdim			ipstat.ips_noroute++;
286292920Sdim			error = EHOSTUNREACH;
287292920Sdim			goto bad;
288292920Sdim		}
289292920Sdim		ia = ifatoia(ro->ro_rt->rt_ifa);
290292920Sdim		ifp = ro->ro_rt->rt_ifp;
291292920Sdim		ro->ro_rt->rt_rmx.rmx_pksent++;
292292920Sdim		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
293292920Sdim			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
294292920Sdim		if (ro->ro_rt->rt_flags & RTF_HOST)
295292920Sdim			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
296292920Sdim		else
297292920Sdim			isbroadcast = in_broadcast(dst->sin_addr, ifp);
298292920Sdim	}
299292920Sdim	if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
300292920Sdim		struct in_multi *inm;
301292920Sdim
302292920Sdim		m->m_flags |= M_MCAST;
303292920Sdim		/*
304292920Sdim		 * IP destination address is multicast.  Make sure "dst"
305292920Sdim		 * still points to the address in "ro".  (It may have been
306292920Sdim		 * changed to point to a gateway address, above.)
307292920Sdim		 */
308292920Sdim		dst = (struct sockaddr_in *)&ro->ro_dst;
309292920Sdim		/*
310292920Sdim		 * See if the caller provided any multicast options
311292920Sdim		 */
312292920Sdim		if (imo != NULL) {
313292920Sdim			ip->ip_ttl = imo->imo_multicast_ttl;
314292920Sdim			if (imo->imo_multicast_vif != -1)
315292920Sdim				ip->ip_src.s_addr =
316292920Sdim				    ip_mcast_src ?
317292920Sdim				    ip_mcast_src(imo->imo_multicast_vif) :
318292920Sdim				    INADDR_ANY;
319292920Sdim		} else
320292920Sdim			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
321292920Sdim		/*
322292920Sdim		 * Confirm that the outgoing interface supports multicast.
323292920Sdim		 */
324292920Sdim		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
325292920Sdim			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
326292920Sdim				ipstat.ips_noroute++;
327292920Sdim				error = ENETUNREACH;
328292920Sdim				goto bad;
329292920Sdim			}
330292920Sdim		}
331292920Sdim		/*
332292920Sdim		 * If source address not specified yet, use address
333292920Sdim		 * of outgoing interface.
334292920Sdim		 */
335292920Sdim		if (ip->ip_src.s_addr == INADDR_ANY) {
336292920Sdim			/* Interface may have no addresses. */
337292920Sdim			if (ia != NULL)
338292920Sdim				ip->ip_src = IA_SIN(ia)->sin_addr;
339292920Sdim		}
340292920Sdim
341292920Sdim		IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
342292920Sdim		if (inm != NULL &&
343292920Sdim		   (imo == NULL || imo->imo_multicast_loop)) {
344292920Sdim			/*
345292920Sdim			 * If we belong to the destination multicast group
346292920Sdim			 * on the outgoing interface, and the caller did not
347292920Sdim			 * forbid loopback, loop back a copy.
348292920Sdim			 */
349292920Sdim			ip_mloopback(ifp, m, dst, hlen);
350292920Sdim		}
351292920Sdim		else {
352292920Sdim			/*
353292920Sdim			 * If we are acting as a multicast router, perform
354292920Sdim			 * multicast forwarding as if the packet had just
355292920Sdim			 * arrived on the interface to which we are about
356292920Sdim			 * to send.  The multicast forwarding function
357292920Sdim			 * recursively calls this function, using the
358292920Sdim			 * IP_FORWARDING flag to prevent infinite recursion.
359292920Sdim			 *
360292920Sdim			 * Multicasts that are looped back by ip_mloopback(),
361292920Sdim			 * above, will be forwarded by the ip_input() routine,
362292920Sdim			 * if necessary.
363292920Sdim			 */
364292920Sdim			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
365292920Sdim				/*
366292920Sdim				 * If rsvp daemon is not running, do not
367292920Sdim				 * set ip_moptions. This ensures that the packet
368292920Sdim				 * is multicast and not just sent down one link
369292920Sdim				 * as prescribed by rsvpd.
370292920Sdim				 */
371292920Sdim				if (!rsvp_on)
372292920Sdim					imo = NULL;
373292920Sdim				if (ip_mforward &&
374292920Sdim				    ip_mforward(ip, ifp, m, imo) != 0) {
375292920Sdim					m_freem(m);
376292920Sdim					goto done;
377292920Sdim				}
378292920Sdim			}
379292920Sdim		}
380292920Sdim
381292920Sdim		/*
382292920Sdim		 * Multicasts with a time-to-live of zero may be looped-
383292920Sdim		 * back, above, but must not be transmitted on a network.
384292920Sdim		 * Also, multicasts addressed to the loopback interface
385292920Sdim		 * are not sent -- the above call to ip_mloopback() will
386292920Sdim		 * loop back a copy if this host actually belongs to the
387292920Sdim		 * destination group on the loopback interface.
388292920Sdim		 */
389292920Sdim		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
390292920Sdim			m_freem(m);
391292920Sdim			goto done;
392292920Sdim		}
393292920Sdim
394292920Sdim		goto sendit;
395292920Sdim	}
396292920Sdim#ifndef notdef
397292920Sdim	/*
398292920Sdim	 * If the source address is not specified yet, use the address
399292920Sdim	 * of the outoing interface. In case, keep note we did that, so
400292920Sdim	 * if the the firewall changes the next-hop causing the output
401292920Sdim	 * interface to change, we can fix that.
402292920Sdim	 */
403292920Sdim	if (ip->ip_src.s_addr == INADDR_ANY) {
404292920Sdim		/* Interface may have no addresses. */
405292920Sdim		if (ia != NULL) {
406292920Sdim			ip->ip_src = IA_SIN(ia)->sin_addr;
407292920Sdim			src_was_INADDR_ANY = 1;
408292920Sdim		}
409292920Sdim	}
410292920Sdim#endif /* notdef */
411292920Sdim	/*
412292920Sdim	 * Verify that we have any chance at all of being able to queue
413292920Sdim	 *      the packet or packet fragments
414292920Sdim	 */
415292920Sdim	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
416292920Sdim		ifp->if_snd.ifq_maxlen) {
417292920Sdim			error = ENOBUFS;
418292920Sdim			ipstat.ips_odropped++;
419292920Sdim			goto bad;
420292920Sdim	}
421292920Sdim
422292920Sdim	/*
423292920Sdim	 * Look for broadcast address and
424292920Sdim	 * verify user is allowed to send
425292920Sdim	 * such a packet.
426292920Sdim	 */
427292920Sdim	if (isbroadcast) {
428292920Sdim		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
429292920Sdim			error = EADDRNOTAVAIL;
430292920Sdim			goto bad;
431292920Sdim		}
432292920Sdim		if ((flags & IP_ALLOWBROADCAST) == 0) {
433292920Sdim			error = EACCES;
434292920Sdim			goto bad;
435292920Sdim		}
436292920Sdim		/* don't allow broadcast messages to be fragmented */
437292920Sdim		if (ip->ip_len > ifp->if_mtu) {
438292920Sdim			error = EMSGSIZE;
439292920Sdim			goto bad;
440292920Sdim		}
441292920Sdim		if (flags & IP_SENDONES)
442292920Sdim			ip->ip_dst.s_addr = INADDR_BROADCAST;
443292920Sdim		m->m_flags |= M_BCAST;
444292920Sdim	} else {
445292920Sdim		m->m_flags &= ~M_BCAST;
446292920Sdim	}
447292920Sdim
448292920Sdimsendit:
449292920Sdim#ifdef IPSEC
450292920Sdim	/* get SP for this packet */
451292920Sdim	if (inp == NULL)
452292920Sdim		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
453292920Sdim		    flags, &error);
454292920Sdim	else
455292920Sdim		sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
456292920Sdim
457292920Sdim	if (sp == NULL) {
458292920Sdim		ipsecstat.out_inval++;
459292920Sdim		goto bad;
460292920Sdim	}
461292920Sdim
462292920Sdim	error = 0;
463292920Sdim
464292920Sdim	/* check policy */
465292920Sdim	switch (sp->policy) {
466292920Sdim	case IPSEC_POLICY_DISCARD:
467292920Sdim		/*
468292920Sdim		 * This packet is just discarded.
469292920Sdim		 */
470292920Sdim		ipsecstat.out_polvio++;
471292920Sdim		goto bad;
472292920Sdim
473292920Sdim	case IPSEC_POLICY_BYPASS:
474292920Sdim	case IPSEC_POLICY_NONE:
475292920Sdim	case IPSEC_POLICY_TCP:
476292920Sdim		/* no need to do IPsec. */
477292920Sdim		goto skip_ipsec;
478292920Sdim
479292920Sdim	case IPSEC_POLICY_IPSEC:
480292920Sdim		if (sp->req == NULL) {
481292920Sdim			/* acquire a policy */
482292920Sdim			error = key_spdacquire(sp);
483292920Sdim			goto bad;
484292920Sdim		}
485292920Sdim		break;
486292920Sdim
487292920Sdim	case IPSEC_POLICY_ENTRUST:
488292920Sdim	default:
489292920Sdim		printf("ip_output: Invalid policy found. %d\n", sp->policy);
490292920Sdim	}
491292920Sdim    {
492292920Sdim	struct ipsec_output_state state;
493292920Sdim	bzero(&state, sizeof(state));
494292920Sdim	state.m = m;
495292920Sdim	if (flags & IP_ROUTETOIF) {
496292920Sdim		state.ro = &iproute;
497292920Sdim		bzero(&iproute, sizeof(iproute));
498292920Sdim	} else
499292920Sdim		state.ro = ro;
500292920Sdim	state.dst = (struct sockaddr *)dst;
501292920Sdim
502292920Sdim	ip->ip_sum = 0;
503292920Sdim
504292920Sdim	/*
505292920Sdim	 * XXX
506292920Sdim	 * delayed checksums are not currently compatible with IPsec
507292920Sdim	 */
508292920Sdim	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
509292920Sdim		in_delayed_cksum(m);
510292920Sdim		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
511292920Sdim	}
512292920Sdim
513292920Sdim	ip->ip_len = htons(ip->ip_len);
514292920Sdim	ip->ip_off = htons(ip->ip_off);
515292920Sdim
516292920Sdim	error = ipsec4_output(&state, sp, flags);
517292920Sdim
518292920Sdim	m = state.m;
519292920Sdim	if (flags & IP_ROUTETOIF) {
520292920Sdim		/*
521292920Sdim		 * if we have tunnel mode SA, we may need to ignore
522292920Sdim		 * IP_ROUTETOIF.
523292920Sdim		 */
524292920Sdim		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
525292920Sdim			flags &= ~IP_ROUTETOIF;
526292920Sdim			ro = state.ro;
527292920Sdim		}
528292920Sdim	} else
529292920Sdim		ro = state.ro;
530292920Sdim	dst = (struct sockaddr_in *)state.dst;
531292920Sdim	if (error) {
532292920Sdim		/* mbuf is already reclaimed in ipsec4_output. */
533292920Sdim		m = NULL;
534292920Sdim		switch (error) {
535292920Sdim		case EHOSTUNREACH:
536292920Sdim		case ENETUNREACH:
537292920Sdim		case EMSGSIZE:
538292920Sdim		case ENOBUFS:
539292920Sdim		case ENOMEM:
540292920Sdim			break;
541292920Sdim		default:
542292920Sdim			printf("ip4_output (ipsec): error code %d\n", error);
543292920Sdim			/*fall through*/
544292920Sdim		case ENOENT:
545292920Sdim			/* don't show these error codes to the user */
546292920Sdim			error = 0;
547292920Sdim			break;
548292920Sdim		}
549292920Sdim		goto bad;
550292920Sdim	}
551292920Sdim
552292920Sdim	/* be sure to update variables that are affected by ipsec4_output() */
553292920Sdim	ip = mtod(m, struct ip *);
554292920Sdim	hlen = ip->ip_hl << 2;
555292920Sdim	if (ro->ro_rt == NULL) {
556292920Sdim		if ((flags & IP_ROUTETOIF) == 0) {
557292920Sdim			printf("ip_output: "
558292920Sdim				"can't update route after IPsec processing\n");
559292920Sdim			error = EHOSTUNREACH;	/*XXX*/
560292920Sdim			goto bad;
561292920Sdim		}
562292920Sdim	} else {
563292920Sdim		if (state.encap) {
564292920Sdim			ia = ifatoia(ro->ro_rt->rt_ifa);
565292920Sdim			ifp = ro->ro_rt->rt_ifp;
566292920Sdim		}
567292920Sdim	}
568292920Sdim    }
569292920Sdim
570292920Sdim	/* make it flipped, again. */
571292920Sdim	ip->ip_len = ntohs(ip->ip_len);
572292920Sdim	ip->ip_off = ntohs(ip->ip_off);
573292920Sdimskip_ipsec:
574292920Sdim#endif /*IPSEC*/
575292920Sdim#ifdef FAST_IPSEC
576292920Sdim	/*
577292920Sdim	 * Check the security policy (SP) for the packet and, if
578292920Sdim	 * required, do IPsec-related processing.  There are two
579292920Sdim	 * cases here; the first time a packet is sent through
580292920Sdim	 * it will be untagged and handled by ipsec4_checkpolicy.
581292920Sdim	 * If the packet is resubmitted to ip_output (e.g. after
582292920Sdim	 * AH, ESP, etc. processing), there will be a tag to bypass
583292920Sdim	 * the lookup and related policy checking.
584292920Sdim	 */
585292920Sdim	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
586292920Sdim	s = splnet();
587292920Sdim	if (mtag != NULL) {
588292920Sdim		tdbi = (struct tdb_ident *)(mtag + 1);
589292920Sdim		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
590292920Sdim		if (sp == NULL)
591292920Sdim			error = -EINVAL;	/* force silent drop */
592292920Sdim		m_tag_delete(m, mtag);
593292920Sdim	} else {
594292920Sdim		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
595292920Sdim					&error, inp);
596292920Sdim	}
597292920Sdim	/*
598292920Sdim	 * There are four return cases:
599292920Sdim	 *    sp != NULL	 	    apply IPsec policy
600292920Sdim	 *    sp == NULL, error == 0	    no IPsec handling needed
601292920Sdim	 *    sp == NULL, error == -EINVAL  discard packet w/o error
602292920Sdim	 *    sp == NULL, error != 0	    discard packet, report error
603292920Sdim	 */
604292920Sdim	if (sp != NULL) {
605292920Sdim		/* Loop detection, check if ipsec processing already done */
606292920Sdim		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
607292920Sdim		for (mtag = m_tag_first(m); mtag != NULL;
608292920Sdim		     mtag = m_tag_next(m, mtag)) {
609292920Sdim			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
610292920Sdim				continue;
611292920Sdim			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
612292920Sdim			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
613292920Sdim				continue;
614292920Sdim			/*
615292920Sdim			 * Check if policy has an SA associated with it.
616292920Sdim			 * This can happen when an SP has yet to acquire
617292920Sdim			 * an SA; e.g. on first reference.  If it occurs,
618292920Sdim			 * then we let ipsec4_process_packet do its thing.
619292920Sdim			 */
620292920Sdim			if (sp->req->sav == NULL)
621292920Sdim				break;
622292920Sdim			tdbi = (struct tdb_ident *)(mtag + 1);
623292920Sdim			if (tdbi->spi == sp->req->sav->spi &&
624292920Sdim			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
625292920Sdim			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
626292920Sdim				 sizeof (union sockaddr_union)) == 0) {
627292920Sdim				/*
628292920Sdim				 * No IPsec processing is needed, free
629292920Sdim				 * reference to SP.
630292920Sdim				 *
631292920Sdim				 * NB: null pointer to avoid free at
632292920Sdim				 *     done: below.
633292920Sdim				 */
634292920Sdim				KEY_FREESP(&sp), sp = NULL;
635292920Sdim				splx(s);
636292920Sdim				goto spd_done;
637292920Sdim			}
638292920Sdim		}
639292920Sdim
640292920Sdim		/*
641292920Sdim		 * Do delayed checksums now because we send before
642292920Sdim		 * this is done in the normal processing path.
643292920Sdim		 */
644292920Sdim		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
645292920Sdim			in_delayed_cksum(m);
646292920Sdim			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
647292920Sdim		}
648292920Sdim
649292920Sdim		ip->ip_len = htons(ip->ip_len);
650292920Sdim		ip->ip_off = htons(ip->ip_off);
651292920Sdim
652292920Sdim		/* NB: callee frees mbuf */
653292920Sdim		error = ipsec4_process_packet(m, sp->req, flags, 0);
654292920Sdim		/*
655292920Sdim		 * Preserve KAME behaviour: ENOENT can be returned
656292920Sdim		 * when an SA acquire is in progress.  Don't propagate
657292920Sdim		 * this to user-level; it confuses applications.
658292920Sdim		 *
659292920Sdim		 * XXX this will go away when the SADB is redone.
660292920Sdim		 */
661292920Sdim		if (error == ENOENT)
662292920Sdim			error = 0;
663292920Sdim		splx(s);
664292920Sdim		goto done;
665292920Sdim	} else {
666292920Sdim		splx(s);
667292920Sdim
668292920Sdim		if (error != 0) {
669292920Sdim			/*
670292920Sdim			 * Hack: -EINVAL is used to signal that a packet
671292920Sdim			 * should be silently discarded.  This is typically
672292920Sdim			 * because we asked key management for an SA and
673292920Sdim			 * it was delayed (e.g. kicked up to IKE).
674292920Sdim			 */
675292920Sdim			if (error == -EINVAL)
676292920Sdim				error = 0;
677292920Sdim			goto bad;
678292920Sdim		} else {
679292920Sdim			/* No IPsec processing for this packet. */
680292920Sdim		}
681292920Sdim#ifdef notyet
682292920Sdim		/*
683292920Sdim		 * If deferred crypto processing is needed, check that
684292920Sdim		 * the interface supports it.
685292920Sdim		 */
686292920Sdim		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
687292920Sdim		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
688292920Sdim			/* notify IPsec to do its own crypto */
689292920Sdim			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
690292920Sdim			error = EHOSTUNREACH;
691292920Sdim			goto bad;
692292920Sdim		}
693292920Sdim#endif
694292920Sdim	}
695292920Sdimspd_done:
696292920Sdim#endif /* FAST_IPSEC */
697292920Sdim
698292920Sdim	/*
699292920Sdim	 * IpHack's section.
700292920Sdim	 * - Xlate: translate packet's addr/port (NAT).
701292920Sdim	 * - Firewall: deny/allow/etc.
702292920Sdim	 * - Wrap: fake packet's addr/port <unimpl.>
703292920Sdim	 * - Encapsulate: put it in another IP and send out. <unimp.>
704292920Sdim	 */
705292920Sdim#ifdef PFIL_HOOKS
706292920Sdim	/*
707292920Sdim	 * Run through list of hooks for output packets.
708292920Sdim	 */
709292920Sdim	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT);
710292920Sdim	if (error != 0 || m == NULL)
711292920Sdim		goto done;
712292920Sdim	ip = mtod(m, struct ip *);
713292920Sdim#endif /* PFIL_HOOKS */
714292920Sdim
715292920Sdim	/*
716292920Sdim	 * Check with the firewall...
717292920Sdim	 * but not if we are already being fwd'd from a firewall.
718292920Sdim	 */
719292920Sdim	if (fw_enable && IPFW_LOADED && !args.next_hop) {
720292920Sdim		struct sockaddr_in *old = dst;
721292920Sdim
722292920Sdim		args.m = m;
723292920Sdim		args.next_hop = dst;
724292920Sdim		args.oif = ifp;
725292920Sdim		off = ip_fw_chk_ptr(&args);
726292920Sdim		m = args.m;
727292920Sdim		dst = args.next_hop;
728292920Sdim
729292920Sdim                /*
730292920Sdim		 * On return we must do the following:
731292920Sdim		 * m == NULL	-> drop the pkt (old interface, deprecated)
732292920Sdim		 * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
733292920Sdim		 * 1<=off<= 0xffff		-> DIVERT
734292920Sdim		 * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
735292920Sdim		 * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
736292920Sdim		 * dst != old			-> IPFIREWALL_FORWARD
737292920Sdim		 * off==0, dst==old		-> accept
738292920Sdim		 * If some of the above modules are not compiled in, then
739292920Sdim		 * we should't have to check the corresponding condition
740292920Sdim		 * (because the ipfw control socket should not accept
741292920Sdim		 * unsupported rules), but better play safe and drop
742292920Sdim		 * packets in case of doubt.
743292920Sdim		 */
744292920Sdim		if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
745292920Sdim			if (m)
746292920Sdim				m_freem(m);
747292920Sdim			error = EACCES;
748292920Sdim			goto done;
749292920Sdim		}
750292920Sdim		ip = mtod(m, struct ip *);
751292920Sdim		if (off == 0 && dst == old)		/* common case */
752292920Sdim			goto pass;
753292920Sdim                if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
754292920Sdim			/*
755292920Sdim			 * pass the pkt to dummynet. Need to include
756292920Sdim			 * pipe number, m, ifp, ro, dst because these are
757292920Sdim			 * not recomputed in the next pass.
758292920Sdim			 * All other parameters have been already used and
759292920Sdim			 * so they are not needed anymore.
760292920Sdim			 * XXX note: if the ifp or ro entry are deleted
761292920Sdim			 * while a pkt is in dummynet, we are in trouble!
762292920Sdim			 */
763292920Sdim			args.ro = ro;
764292920Sdim			args.dst = dst;
765292920Sdim			args.flags = flags;
766292920Sdim
767292920Sdim			error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT,
768292920Sdim				&args);
769292920Sdim			goto done;
770292920Sdim		}
771292920Sdim#ifdef IPDIVERT
772292920Sdim		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
773292920Sdim			struct mbuf *clone;
774292920Sdim
775292920Sdim			/* Clone packet if we're doing a 'tee' */
776292920Sdim			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
777292920Sdim				clone = divert_clone(m);
778292920Sdim			else
779292920Sdim				clone = NULL;
780292920Sdim
781292920Sdim			/*
782292920Sdim			 * XXX
783292920Sdim			 * delayed checksums are not currently compatible
784292920Sdim			 * with divert sockets.
785292920Sdim			 */
786292920Sdim			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
787292920Sdim				in_delayed_cksum(m);
788292920Sdim				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
789292920Sdim			}
790292920Sdim
791292920Sdim			/* Restore packet header fields to original values */
792292920Sdim			ip->ip_len = htons(ip->ip_len);
793292920Sdim			ip->ip_off = htons(ip->ip_off);
794292920Sdim
795292920Sdim			/* Deliver packet to divert input routine */
796292920Sdim			divert_packet(m, 0);
797292920Sdim
798292920Sdim			/* If 'tee', continue with original packet */
799292920Sdim			if (clone != NULL) {
800292920Sdim				m = clone;
801292920Sdim				ip = mtod(m, struct ip *);
802292920Sdim				goto pass;
803292920Sdim			}
804292920Sdim			goto done;
805292920Sdim		}
806292920Sdim#endif
807292920Sdim
808292920Sdim		/* IPFIREWALL_FORWARD */
809292920Sdim		/*
810292920Sdim		 * Check dst to make sure it is directly reachable on the
811292920Sdim		 * interface we previously thought it was.
812292920Sdim		 * If it isn't (which may be likely in some situations) we have
813292920Sdim		 * to re-route it (ie, find a route for the next-hop and the
814292920Sdim		 * associated interface) and set them here. This is nested
815292920Sdim		 * forwarding which in most cases is undesirable, except where
816292920Sdim		 * such control is nigh impossible. So we do it here.
817292920Sdim		 * And I'm babbling.
818292920Sdim		 */
819292920Sdim		if (off == 0 && old != dst) { /* FORWARD, dst has changed */
820292920Sdim#if 0
821292920Sdim			/*
822292920Sdim			 * XXX To improve readability, this block should be
823292920Sdim			 * changed into a function call as below:
824292920Sdim			 */
825292920Sdim			error = ip_ipforward(&m, &dst, &ifp);
826292920Sdim			if (error)
827292920Sdim				goto bad;
828292920Sdim			if (m == NULL) /* ip_input consumed the mbuf */
829292920Sdim				goto done;
830292920Sdim#else
831292920Sdim			struct in_ifaddr *ia;
832292920Sdim
833292920Sdim			/*
834292920Sdim			 * XXX sro_fwd below is static, and a pointer
835292920Sdim			 * to it gets passed to routines downstream.
836292920Sdim			 * This could have surprisingly bad results in
837292920Sdim			 * practice, because its content is overwritten
838292920Sdim			 * by subsequent packets.
839292920Sdim			 */
840292920Sdim			/* There must be a better way to do this next line... */
841292920Sdim			static struct route sro_fwd;
842292920Sdim			struct route *ro_fwd = &sro_fwd;
843292920Sdim
844292920Sdim#if 0
845292920Sdim			print_ip("IPFIREWALL_FORWARD: New dst ip: ",
846292920Sdim			    dst->sin_addr, "\n");
847292920Sdim#endif
848292920Sdim
849292920Sdim			/*
850292920Sdim			 * We need to figure out if we have been forwarded
851292920Sdim			 * to a local socket. If so, then we should somehow
852292920Sdim			 * "loop back" to ip_input, and get directed to the
853292920Sdim			 * PCB as if we had received this packet. This is
854292920Sdim			 * because it may be dificult to identify the packets
855292920Sdim			 * you want to forward until they are being output
856292920Sdim			 * and have selected an interface. (e.g. locally
857292920Sdim			 * initiated packets) If we used the loopback inteface,
858292920Sdim			 * we would not be able to control what happens
859292920Sdim			 * as the packet runs through ip_input() as
860292920Sdim			 * it is done through an ISR.
861292920Sdim			 */
862292920Sdim			LIST_FOREACH(ia,
863292920Sdim			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
864292920Sdim				/*
865292920Sdim				 * If the addr to forward to is one
866292920Sdim				 * of ours, we pretend to
867292920Sdim				 * be the destination for this packet.
868292920Sdim				 */
869292920Sdim				if (IA_SIN(ia)->sin_addr.s_addr ==
870292920Sdim						 dst->sin_addr.s_addr)
871292920Sdim					break;
872292920Sdim			}
873292920Sdim			if (ia) {	/* tell ip_input "dont filter" */
874292920Sdim				mtag = m_tag_get(
875292920Sdim				    PACKET_TAG_IPFORWARD,
876292920Sdim				    sizeof(struct sockaddr_in *), M_NOWAIT);
877292920Sdim				if (mtag == NULL) {
878292920Sdim					error = ENOBUFS;
879292920Sdim					goto bad;
880292920Sdim				}
881292920Sdim				*(struct sockaddr_in **)(mtag+1) =
882292920Sdim				    args.next_hop;
883292920Sdim				m_tag_prepend(m, mtag);
884292920Sdim
885292920Sdim				if (m->m_pkthdr.rcvif == NULL)
886292920Sdim					m->m_pkthdr.rcvif = ifunit("lo0");
887292920Sdim				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
888292920Sdim					m->m_pkthdr.csum_flags |=
889292920Sdim					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
890292920Sdim					m->m_pkthdr.csum_data = 0xffff;
891292920Sdim				}
892292920Sdim				m->m_pkthdr.csum_flags |=
893292920Sdim				    CSUM_IP_CHECKED | CSUM_IP_VALID;
894292920Sdim				ip->ip_len = htons(ip->ip_len);
895292920Sdim				ip->ip_off = htons(ip->ip_off);
896292920Sdim				ip_input(m);
897292920Sdim				goto done;
898292920Sdim			}
899292920Sdim			/*
900292920Sdim			 * Some of the logic for this was
901292920Sdim			 * nicked from above.
902292920Sdim			 */
903292920Sdim			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
904292920Sdim
905292920Sdim			ro_fwd->ro_rt = 0;
906292920Sdim			rtalloc_ign(ro_fwd, RTF_CLONING);
907292920Sdim
908292920Sdim			if (ro_fwd->ro_rt == 0) {
909292920Sdim				ipstat.ips_noroute++;
910292920Sdim				error = EHOSTUNREACH;
911292920Sdim				goto bad;
912292920Sdim			}
913292920Sdim
914292920Sdim			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
915292920Sdim			ifp = ro_fwd->ro_rt->rt_ifp;
916292920Sdim			ro_fwd->ro_rt->rt_rmx.rmx_pksent++;
917292920Sdim			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
918292920Sdim				dst = (struct sockaddr_in *)
919292920Sdim					ro_fwd->ro_rt->rt_gateway;
920292920Sdim			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
921292920Sdim				isbroadcast =
922292920Sdim				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
923292920Sdim			else
924292920Sdim				isbroadcast = in_broadcast(dst->sin_addr, ifp);
925292920Sdim			if (ro->ro_rt)
926292920Sdim				RTFREE(ro->ro_rt);
927292920Sdim			ro->ro_rt = ro_fwd->ro_rt;
928292920Sdim			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
929292920Sdim
930292920Sdim#endif	/* ... block to be put into a function */
931292920Sdim			/*
932292920Sdim			 * If we added a default src ip earlier,
933292920Sdim			 * which would have been gotten from the-then
934292920Sdim			 * interface, do it again, from the new one.
935292920Sdim			 */
936292920Sdim			if (src_was_INADDR_ANY)
937292920Sdim				ip->ip_src = IA_SIN(ia)->sin_addr;
938292920Sdim			goto pass ;
939292920Sdim		}
940292920Sdim
941292920Sdim                /*
942292920Sdim                 * if we get here, none of the above matches, and
943292920Sdim                 * we have to drop the pkt
944292920Sdim                 */
945292920Sdim		m_freem(m);
946292920Sdim                error = EACCES; /* not sure this is the right error msg */
947292920Sdim                goto done;
948292920Sdim	}
949292920Sdim
950292920Sdimpass:
951292920Sdim	/* 127/8 must not appear on wire - RFC1122. */
952292920Sdim	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
953292920Sdim	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
954292920Sdim		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
955292920Sdim			ipstat.ips_badaddr++;
956292920Sdim			error = EADDRNOTAVAIL;
957292920Sdim			goto bad;
958292920Sdim		}
959292920Sdim	}
960292920Sdim
961292920Sdim	m->m_pkthdr.csum_flags |= CSUM_IP;
962292920Sdim	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
963292920Sdim	if (sw_csum & CSUM_DELAY_DATA) {
964292920Sdim		in_delayed_cksum(m);
965292920Sdim		sw_csum &= ~CSUM_DELAY_DATA;
966292920Sdim	}
967292920Sdim	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
968292920Sdim
969292920Sdim	/*
970292920Sdim	 * If small enough for interface, or the interface will take
971292920Sdim	 * care of the fragmentation for us, can just send directly.
972292920Sdim	 */
973292920Sdim	if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
974292920Sdim	    ((ip->ip_off & IP_DF) == 0))) {
975292920Sdim		ip->ip_len = htons(ip->ip_len);
976292920Sdim		ip->ip_off = htons(ip->ip_off);
977292920Sdim		ip->ip_sum = 0;
978292920Sdim		if (sw_csum & CSUM_DELAY_IP)
979292920Sdim			ip->ip_sum = in_cksum(m, hlen);
980292920Sdim
981292920Sdim		/* Record statistics for this interface address. */
982292920Sdim		if (!(flags & IP_FORWARDING) && ia) {
983292920Sdim			ia->ia_ifa.if_opackets++;
984292920Sdim			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
985292920Sdim		}
986292920Sdim
987292920Sdim#ifdef IPSEC
988292920Sdim		/* clean ipsec history once it goes out of the node */
989292920Sdim		ipsec_delaux(m);
990292920Sdim#endif
991292920Sdim
992292920Sdim#ifdef MBUF_STRESS_TEST
993292920Sdim		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
994292920Sdim			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
995292920Sdim#endif
996292920Sdim		error = (*ifp->if_output)(ifp, m,
997292920Sdim				(struct sockaddr *)dst, ro->ro_rt);
998292920Sdim		goto done;
999292920Sdim	}
1000292920Sdim
1001292920Sdim	if (ip->ip_off & IP_DF) {
1002292920Sdim		error = EMSGSIZE;
1003292920Sdim		/*
1004292920Sdim		 * This case can happen if the user changed the MTU
1005292920Sdim		 * of an interface after enabling IP on it.  Because
1006292920Sdim		 * most netifs don't keep track of routes pointing to
1007292920Sdim		 * them, there is no way for one to update all its
1008292920Sdim		 * routes when the MTU is changed.
1009292920Sdim		 */
1010292920Sdim		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
1011292920Sdim		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
1012292920Sdim			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
1013292920Sdim		}
1014292920Sdim		ipstat.ips_cantfrag++;
1015292920Sdim		goto bad;
1016292920Sdim	}
1017292920Sdim
1018292920Sdim	/*
1019292920Sdim	 * Too large for interface; fragment if possible. If successful,
1020292920Sdim	 * on return, m will point to a list of packets to be sent.
1021292920Sdim	 */
1022292920Sdim	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
1023292920Sdim	if (error)
1024292920Sdim		goto bad;
1025292920Sdim	for (; m; m = m0) {
1026292920Sdim		m0 = m->m_nextpkt;
1027292920Sdim		m->m_nextpkt = 0;
1028292920Sdim#ifdef IPSEC
1029292920Sdim		/* clean ipsec history once it goes out of the node */
1030292920Sdim		ipsec_delaux(m);
1031292920Sdim#endif
1032292920Sdim		if (error == 0) {
1033292920Sdim			/* Record statistics for this interface address. */
1034292920Sdim			if (ia != NULL) {
1035292920Sdim				ia->ia_ifa.if_opackets++;
1036292920Sdim				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1037292920Sdim			}
1038292920Sdim
1039292920Sdim			error = (*ifp->if_output)(ifp, m,
1040292920Sdim			    (struct sockaddr *)dst, ro->ro_rt);
1041292920Sdim		} else
1042292920Sdim			m_freem(m);
1043292920Sdim	}
1044292920Sdim
1045292920Sdim	if (error == 0)
1046292920Sdim		ipstat.ips_fragmented++;
1047292920Sdim
1048292920Sdimdone:
1049292920Sdim	if (ro == &iproute && ro->ro_rt) {
1050292920Sdim		RTFREE(ro->ro_rt);
1051292920Sdim		ro->ro_rt = NULL;
1052292920Sdim	}
1053292920Sdim	if (dummytag) {
1054292920Sdim		struct dn_pkt_tag *dt = (struct dn_pkt_tag *)(dummytag+1);
1055292920Sdim		if (dt->ro.ro_rt)
1056292920Sdim			RTFREE(dt->ro.ro_rt);
1057292920Sdim		m_tag_free(dummytag);
1058292920Sdim	}
1059292920Sdim#ifdef IPSEC
1060292920Sdim	if (sp != NULL) {
1061292920Sdim		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1062292920Sdim			printf("DP ip_output call free SP:%p\n", sp));
1063292920Sdim		key_freesp(sp);
1064292920Sdim	}
1065292920Sdim#endif
1066292920Sdim#ifdef FAST_IPSEC
1067292920Sdim	if (sp != NULL)
1068292920Sdim		KEY_FREESP(&sp);
1069292920Sdim#endif
1070292920Sdim	return (error);
1071292920Sdimbad:
1072292920Sdim	m_freem(m);
1073292920Sdim	goto done;
1074292920Sdim}
1075292920Sdim
1076292920Sdim/*
1077292920Sdim * Create a chain of fragments which fit the given mtu. m_frag points to the
1078292920Sdim * mbuf to be fragmented; on return it points to the chain with the fragments.
1079292920Sdim * Return 0 if no error. If error, m_frag may contain a partially built
1080292920Sdim * chain of fragments that should be freed by the caller.
1081292920Sdim *
1082292920Sdim * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
1083292920Sdim * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
1084292920Sdim */
1085292920Sdimint
1086292920Sdimip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
1087292920Sdim	    u_long if_hwassist_flags, int sw_csum)
1088292920Sdim{
1089292920Sdim	int error = 0;
1090292920Sdim	int hlen = ip->ip_hl << 2;
1091292920Sdim	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
1092292920Sdim	int off;
1093292920Sdim	struct mbuf *m0 = *m_frag;	/* the original packet		*/
1094292920Sdim	int firstlen;
1095292920Sdim	struct mbuf **mnext;
1096292920Sdim	int nfrags;
1097292920Sdim
1098292920Sdim	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
1099292920Sdim		ipstat.ips_cantfrag++;
1100292920Sdim		return EMSGSIZE;
1101292920Sdim	}
1102292920Sdim
1103292920Sdim	/*
1104292920Sdim	 * Must be able to put at least 8 bytes per fragment.
1105292920Sdim	 */
1106292920Sdim	if (len < 8)
1107292920Sdim		return EMSGSIZE;
1108292920Sdim
1109292920Sdim	/*
1110292920Sdim	 * If the interface will not calculate checksums on
1111292920Sdim	 * fragmented packets, then do it here.
1112292920Sdim	 */
1113292920Sdim	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
1114292920Sdim	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
1115292920Sdim		in_delayed_cksum(m0);
1116292920Sdim		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1117292920Sdim	}
1118292920Sdim
1119292920Sdim	if (len > PAGE_SIZE) {
1120292920Sdim		/*
1121292920Sdim		 * Fragment large datagrams such that each segment
1122292920Sdim		 * contains a multiple of PAGE_SIZE amount of data,
1123292920Sdim		 * plus headers. This enables a receiver to perform
1124292920Sdim		 * page-flipping zero-copy optimizations.
1125292920Sdim		 *
1126292920Sdim		 * XXX When does this help given that sender and receiver
1127292920Sdim		 * could have different page sizes, and also mtu could
1128292920Sdim		 * be less than the receiver's page size ?
1129292920Sdim		 */
1130292920Sdim		int newlen;
1131292920Sdim		struct mbuf *m;
1132292920Sdim
1133292920Sdim		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
1134292920Sdim			off += m->m_len;
1135292920Sdim
1136292920Sdim		/*
1137292920Sdim		 * firstlen (off - hlen) must be aligned on an
1138292920Sdim		 * 8-byte boundary
1139292920Sdim		 */
1140292920Sdim		if (off < hlen)
1141292920Sdim			goto smart_frag_failure;
1142292920Sdim		off = ((off - hlen) & ~7) + hlen;
1143292920Sdim		newlen = (~PAGE_MASK) & mtu;
1144292920Sdim		if ((newlen + sizeof (struct ip)) > mtu) {
1145292920Sdim			/* we failed, go back the default */
1146292920Sdimsmart_frag_failure:
1147292920Sdim			newlen = len;
1148292920Sdim			off = hlen + len;
1149292920Sdim		}
1150292920Sdim		len = newlen;
1151292920Sdim
1152292920Sdim	} else {
1153292920Sdim		off = hlen + len;
1154292920Sdim	}
1155292920Sdim
1156292920Sdim	firstlen = off - hlen;
1157292920Sdim	mnext = &m0->m_nextpkt;		/* pointer to next packet */
1158292920Sdim
1159292920Sdim	/*
1160292920Sdim	 * Loop through length of segment after first fragment,
1161292920Sdim	 * make new header and copy data of each part and link onto chain.
1162292920Sdim	 * Here, m0 is the original packet, m is the fragment being created.
1163292920Sdim	 * The fragments are linked off the m_nextpkt of the original
1164292920Sdim	 * packet, which after processing serves as the first fragment.
1165292920Sdim	 */
1166292920Sdim	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
1167292920Sdim		struct ip *mhip;	/* ip header on the fragment */
1168292920Sdim		struct mbuf *m;
1169292920Sdim		int mhlen = sizeof (struct ip);
1170292920Sdim
1171292920Sdim		MGETHDR(m, M_DONTWAIT, MT_HEADER);
1172292920Sdim		if (m == 0) {
1173292920Sdim			error = ENOBUFS;
1174292920Sdim			ipstat.ips_odropped++;
1175292920Sdim			goto done;
1176292920Sdim		}
1177292920Sdim		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
1178292920Sdim		/*
1179292920Sdim		 * In the first mbuf, leave room for the link header, then
1180292920Sdim		 * copy the original IP header including options. The payload
1181292920Sdim		 * goes into an additional mbuf chain returned by m_copy().
1182292920Sdim		 */
1183292920Sdim		m->m_data += max_linkhdr;
1184292920Sdim		mhip = mtod(m, struct ip *);
1185292920Sdim		*mhip = *ip;
1186292920Sdim		if (hlen > sizeof (struct ip)) {
1187292920Sdim			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1188292920Sdim			mhip->ip_v = IPVERSION;
1189292920Sdim			mhip->ip_hl = mhlen >> 2;
1190292920Sdim		}
1191292920Sdim		m->m_len = mhlen;
1192292920Sdim		/* XXX do we need to add ip->ip_off below ? */
1193292920Sdim		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
1194292920Sdim		if (off + len >= ip->ip_len) {	/* last fragment */
1195292920Sdim			len = ip->ip_len - off;
1196292920Sdim			m->m_flags |= M_LASTFRAG;
1197292920Sdim		} else
1198292920Sdim			mhip->ip_off |= IP_MF;
1199292920Sdim		mhip->ip_len = htons((u_short)(len + mhlen));
1200292920Sdim		m->m_next = m_copy(m0, off, len);
1201292920Sdim		if (m->m_next == 0) {		/* copy failed */
1202			m_free(m);
1203			error = ENOBUFS;	/* ??? */
1204			ipstat.ips_odropped++;
1205			goto done;
1206		}
1207		m->m_pkthdr.len = mhlen + len;
1208		m->m_pkthdr.rcvif = (struct ifnet *)0;
1209#ifdef MAC
1210		mac_create_fragment(m0, m);
1211#endif
1212		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
1213		mhip->ip_off = htons(mhip->ip_off);
1214		mhip->ip_sum = 0;
1215		if (sw_csum & CSUM_DELAY_IP)
1216			mhip->ip_sum = in_cksum(m, mhlen);
1217		*mnext = m;
1218		mnext = &m->m_nextpkt;
1219	}
1220	ipstat.ips_ofragments += nfrags;
1221
1222	/* set first marker for fragment chain */
1223	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1224	m0->m_pkthdr.csum_data = nfrags;
1225
1226	/*
1227	 * Update first fragment by trimming what's been copied out
1228	 * and updating header.
1229	 */
1230	m_adj(m0, hlen + firstlen - ip->ip_len);
1231	m0->m_pkthdr.len = hlen + firstlen;
1232	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
1233	ip->ip_off |= IP_MF;
1234	ip->ip_off = htons(ip->ip_off);
1235	ip->ip_sum = 0;
1236	if (sw_csum & CSUM_DELAY_IP)
1237		ip->ip_sum = in_cksum(m0, hlen);
1238
1239done:
1240	*m_frag = m0;
1241	return error;
1242}
1243
1244void
1245in_delayed_cksum(struct mbuf *m)
1246{
1247	struct ip *ip;
1248	u_short csum, offset;
1249
1250	ip = mtod(m, struct ip *);
1251	offset = ip->ip_hl << 2 ;
1252	csum = in_cksum_skip(m, ip->ip_len, offset);
1253	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1254		csum = 0xffff;
1255	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1256
1257	if (offset + sizeof(u_short) > m->m_len) {
1258		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1259		    m->m_len, offset, ip->ip_p);
1260		/*
1261		 * XXX
1262		 * this shouldn't happen, but if it does, the
1263		 * correct behavior may be to insert the checksum
1264		 * in the existing chain instead of rearranging it.
1265		 */
1266		m = m_pullup(m, offset + sizeof(u_short));
1267	}
1268	*(u_short *)(m->m_data + offset) = csum;
1269}
1270
1271/*
1272 * Insert IP options into preformed packet.
1273 * Adjust IP destination as required for IP source routing,
1274 * as indicated by a non-zero in_addr at the start of the options.
1275 *
1276 * XXX This routine assumes that the packet has no options in place.
1277 */
1278static struct mbuf *
1279ip_insertoptions(m, opt, phlen)
1280	register struct mbuf *m;
1281	struct mbuf *opt;
1282	int *phlen;
1283{
1284	register struct ipoption *p = mtod(opt, struct ipoption *);
1285	struct mbuf *n;
1286	register struct ip *ip = mtod(m, struct ip *);
1287	unsigned optlen;
1288
1289	optlen = opt->m_len - sizeof(p->ipopt_dst);
1290	if (optlen + ip->ip_len > IP_MAXPACKET) {
1291		*phlen = 0;
1292		return (m);		/* XXX should fail */
1293	}
1294	if (p->ipopt_dst.s_addr)
1295		ip->ip_dst = p->ipopt_dst;
1296	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1297		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1298		if (n == 0) {
1299			*phlen = 0;
1300			return (m);
1301		}
1302		n->m_pkthdr.rcvif = (struct ifnet *)0;
1303#ifdef MAC
1304		mac_create_mbuf_from_mbuf(m, n);
1305#endif
1306		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1307		m->m_len -= sizeof(struct ip);
1308		m->m_data += sizeof(struct ip);
1309		n->m_next = m;
1310		m = n;
1311		m->m_len = optlen + sizeof(struct ip);
1312		m->m_data += max_linkhdr;
1313		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1314	} else {
1315		m->m_data -= optlen;
1316		m->m_len += optlen;
1317		m->m_pkthdr.len += optlen;
1318		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1319	}
1320	ip = mtod(m, struct ip *);
1321	bcopy(p->ipopt_list, ip + 1, optlen);
1322	*phlen = sizeof(struct ip) + optlen;
1323	ip->ip_v = IPVERSION;
1324	ip->ip_hl = *phlen >> 2;
1325	ip->ip_len += optlen;
1326	return (m);
1327}
1328
1329/*
1330 * Copy options from ip to jp,
1331 * omitting those not copied during fragmentation.
1332 */
1333int
1334ip_optcopy(ip, jp)
1335	struct ip *ip, *jp;
1336{
1337	register u_char *cp, *dp;
1338	int opt, optlen, cnt;
1339
1340	cp = (u_char *)(ip + 1);
1341	dp = (u_char *)(jp + 1);
1342	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1343	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1344		opt = cp[0];
1345		if (opt == IPOPT_EOL)
1346			break;
1347		if (opt == IPOPT_NOP) {
1348			/* Preserve for IP mcast tunnel's LSRR alignment. */
1349			*dp++ = IPOPT_NOP;
1350			optlen = 1;
1351			continue;
1352		}
1353
1354		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1355		    ("ip_optcopy: malformed ipv4 option"));
1356		optlen = cp[IPOPT_OLEN];
1357		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1358		    ("ip_optcopy: malformed ipv4 option"));
1359
1360		/* bogus lengths should have been caught by ip_dooptions */
1361		if (optlen > cnt)
1362			optlen = cnt;
1363		if (IPOPT_COPIED(opt)) {
1364			bcopy(cp, dp, optlen);
1365			dp += optlen;
1366		}
1367	}
1368	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1369		*dp++ = IPOPT_EOL;
1370	return (optlen);
1371}
1372
1373/*
1374 * IP socket option processing.
1375 */
1376int
1377ip_ctloutput(so, sopt)
1378	struct socket *so;
1379	struct sockopt *sopt;
1380{
1381	struct	inpcb *inp = sotoinpcb(so);
1382	int	error, optval;
1383
1384	error = optval = 0;
1385	if (sopt->sopt_level != IPPROTO_IP) {
1386		return (EINVAL);
1387	}
1388
1389	switch (sopt->sopt_dir) {
1390	case SOPT_SET:
1391		switch (sopt->sopt_name) {
1392		case IP_OPTIONS:
1393#ifdef notyet
1394		case IP_RETOPTS:
1395#endif
1396		{
1397			struct mbuf *m;
1398			if (sopt->sopt_valsize > MLEN) {
1399				error = EMSGSIZE;
1400				break;
1401			}
1402			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1403			if (m == 0) {
1404				error = ENOBUFS;
1405				break;
1406			}
1407			m->m_len = sopt->sopt_valsize;
1408			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1409					    m->m_len);
1410
1411			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1412					   m));
1413		}
1414
1415		case IP_TOS:
1416		case IP_TTL:
1417		case IP_RECVOPTS:
1418		case IP_RECVRETOPTS:
1419		case IP_RECVDSTADDR:
1420		case IP_RECVTTL:
1421		case IP_RECVIF:
1422		case IP_FAITH:
1423		case IP_ONESBCAST:
1424			error = sooptcopyin(sopt, &optval, sizeof optval,
1425					    sizeof optval);
1426			if (error)
1427				break;
1428
1429			switch (sopt->sopt_name) {
1430			case IP_TOS:
1431				inp->inp_ip_tos = optval;
1432				break;
1433
1434			case IP_TTL:
1435				inp->inp_ip_ttl = optval;
1436				break;
1437#define	OPTSET(bit) \
1438	if (optval) \
1439		inp->inp_flags |= bit; \
1440	else \
1441		inp->inp_flags &= ~bit;
1442
1443			case IP_RECVOPTS:
1444				OPTSET(INP_RECVOPTS);
1445				break;
1446
1447			case IP_RECVRETOPTS:
1448				OPTSET(INP_RECVRETOPTS);
1449				break;
1450
1451			case IP_RECVDSTADDR:
1452				OPTSET(INP_RECVDSTADDR);
1453				break;
1454
1455			case IP_RECVTTL:
1456				OPTSET(INP_RECVTTL);
1457				break;
1458
1459			case IP_RECVIF:
1460				OPTSET(INP_RECVIF);
1461				break;
1462
1463			case IP_FAITH:
1464				OPTSET(INP_FAITH);
1465				break;
1466
1467			case IP_ONESBCAST:
1468				OPTSET(INP_ONESBCAST);
1469				break;
1470			}
1471			break;
1472#undef OPTSET
1473
1474		case IP_MULTICAST_IF:
1475		case IP_MULTICAST_VIF:
1476		case IP_MULTICAST_TTL:
1477		case IP_MULTICAST_LOOP:
1478		case IP_ADD_MEMBERSHIP:
1479		case IP_DROP_MEMBERSHIP:
1480			error = ip_setmoptions(sopt, &inp->inp_moptions);
1481			break;
1482
1483		case IP_PORTRANGE:
1484			error = sooptcopyin(sopt, &optval, sizeof optval,
1485					    sizeof optval);
1486			if (error)
1487				break;
1488
1489			switch (optval) {
1490			case IP_PORTRANGE_DEFAULT:
1491				inp->inp_flags &= ~(INP_LOWPORT);
1492				inp->inp_flags &= ~(INP_HIGHPORT);
1493				break;
1494
1495			case IP_PORTRANGE_HIGH:
1496				inp->inp_flags &= ~(INP_LOWPORT);
1497				inp->inp_flags |= INP_HIGHPORT;
1498				break;
1499
1500			case IP_PORTRANGE_LOW:
1501				inp->inp_flags &= ~(INP_HIGHPORT);
1502				inp->inp_flags |= INP_LOWPORT;
1503				break;
1504
1505			default:
1506				error = EINVAL;
1507				break;
1508			}
1509			break;
1510
1511#if defined(IPSEC) || defined(FAST_IPSEC)
1512		case IP_IPSEC_POLICY:
1513		{
1514			caddr_t req;
1515			size_t len = 0;
1516			int priv;
1517			struct mbuf *m;
1518			int optname;
1519
1520			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1521				break;
1522			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1523				break;
1524			priv = (sopt->sopt_td != NULL &&
1525				suser(sopt->sopt_td) != 0) ? 0 : 1;
1526			req = mtod(m, caddr_t);
1527			len = m->m_len;
1528			optname = sopt->sopt_name;
1529			error = ipsec4_set_policy(inp, optname, req, len, priv);
1530			m_freem(m);
1531			break;
1532		}
1533#endif /*IPSEC*/
1534
1535		default:
1536			error = ENOPROTOOPT;
1537			break;
1538		}
1539		break;
1540
1541	case SOPT_GET:
1542		switch (sopt->sopt_name) {
1543		case IP_OPTIONS:
1544		case IP_RETOPTS:
1545			if (inp->inp_options)
1546				error = sooptcopyout(sopt,
1547						     mtod(inp->inp_options,
1548							  char *),
1549						     inp->inp_options->m_len);
1550			else
1551				sopt->sopt_valsize = 0;
1552			break;
1553
1554		case IP_TOS:
1555		case IP_TTL:
1556		case IP_RECVOPTS:
1557		case IP_RECVRETOPTS:
1558		case IP_RECVDSTADDR:
1559		case IP_RECVTTL:
1560		case IP_RECVIF:
1561		case IP_PORTRANGE:
1562		case IP_FAITH:
1563		case IP_ONESBCAST:
1564			switch (sopt->sopt_name) {
1565
1566			case IP_TOS:
1567				optval = inp->inp_ip_tos;
1568				break;
1569
1570			case IP_TTL:
1571				optval = inp->inp_ip_ttl;
1572				break;
1573
1574#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1575
1576			case IP_RECVOPTS:
1577				optval = OPTBIT(INP_RECVOPTS);
1578				break;
1579
1580			case IP_RECVRETOPTS:
1581				optval = OPTBIT(INP_RECVRETOPTS);
1582				break;
1583
1584			case IP_RECVDSTADDR:
1585				optval = OPTBIT(INP_RECVDSTADDR);
1586				break;
1587
1588			case IP_RECVTTL:
1589				optval = OPTBIT(INP_RECVTTL);
1590				break;
1591
1592			case IP_RECVIF:
1593				optval = OPTBIT(INP_RECVIF);
1594				break;
1595
1596			case IP_PORTRANGE:
1597				if (inp->inp_flags & INP_HIGHPORT)
1598					optval = IP_PORTRANGE_HIGH;
1599				else if (inp->inp_flags & INP_LOWPORT)
1600					optval = IP_PORTRANGE_LOW;
1601				else
1602					optval = 0;
1603				break;
1604
1605			case IP_FAITH:
1606				optval = OPTBIT(INP_FAITH);
1607				break;
1608
1609			case IP_ONESBCAST:
1610				optval = OPTBIT(INP_ONESBCAST);
1611				break;
1612			}
1613			error = sooptcopyout(sopt, &optval, sizeof optval);
1614			break;
1615
1616		case IP_MULTICAST_IF:
1617		case IP_MULTICAST_VIF:
1618		case IP_MULTICAST_TTL:
1619		case IP_MULTICAST_LOOP:
1620		case IP_ADD_MEMBERSHIP:
1621		case IP_DROP_MEMBERSHIP:
1622			error = ip_getmoptions(sopt, inp->inp_moptions);
1623			break;
1624
1625#if defined(IPSEC) || defined(FAST_IPSEC)
1626		case IP_IPSEC_POLICY:
1627		{
1628			struct mbuf *m = NULL;
1629			caddr_t req = NULL;
1630			size_t len = 0;
1631
1632			if (m != 0) {
1633				req = mtod(m, caddr_t);
1634				len = m->m_len;
1635			}
1636			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1637			if (error == 0)
1638				error = soopt_mcopyout(sopt, m); /* XXX */
1639			if (error == 0)
1640				m_freem(m);
1641			break;
1642		}
1643#endif /*IPSEC*/
1644
1645		default:
1646			error = ENOPROTOOPT;
1647			break;
1648		}
1649		break;
1650	}
1651	return (error);
1652}
1653
1654/*
1655 * Set up IP options in pcb for insertion in output packets.
1656 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1657 * with destination address if source routed.
1658 */
1659static int
1660ip_pcbopts(optname, pcbopt, m)
1661	int optname;
1662	struct mbuf **pcbopt;
1663	register struct mbuf *m;
1664{
1665	register int cnt, optlen;
1666	register u_char *cp;
1667	u_char opt;
1668
1669	/* turn off any old options */
1670	if (*pcbopt)
1671		(void)m_free(*pcbopt);
1672	*pcbopt = 0;
1673	if (m == (struct mbuf *)0 || m->m_len == 0) {
1674		/*
1675		 * Only turning off any previous options.
1676		 */
1677		if (m)
1678			(void)m_free(m);
1679		return (0);
1680	}
1681
1682	if (m->m_len % sizeof(int32_t))
1683		goto bad;
1684	/*
1685	 * IP first-hop destination address will be stored before
1686	 * actual options; move other options back
1687	 * and clear it when none present.
1688	 */
1689	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1690		goto bad;
1691	cnt = m->m_len;
1692	m->m_len += sizeof(struct in_addr);
1693	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1694	bcopy(mtod(m, void *), cp, (unsigned)cnt);
1695	bzero(mtod(m, void *), sizeof(struct in_addr));
1696
1697	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1698		opt = cp[IPOPT_OPTVAL];
1699		if (opt == IPOPT_EOL)
1700			break;
1701		if (opt == IPOPT_NOP)
1702			optlen = 1;
1703		else {
1704			if (cnt < IPOPT_OLEN + sizeof(*cp))
1705				goto bad;
1706			optlen = cp[IPOPT_OLEN];
1707			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1708				goto bad;
1709		}
1710		switch (opt) {
1711
1712		default:
1713			break;
1714
1715		case IPOPT_LSRR:
1716		case IPOPT_SSRR:
1717			/*
1718			 * user process specifies route as:
1719			 *	->A->B->C->D
1720			 * D must be our final destination (but we can't
1721			 * check that since we may not have connected yet).
1722			 * A is first hop destination, which doesn't appear in
1723			 * actual IP option, but is stored before the options.
1724			 */
1725			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1726				goto bad;
1727			m->m_len -= sizeof(struct in_addr);
1728			cnt -= sizeof(struct in_addr);
1729			optlen -= sizeof(struct in_addr);
1730			cp[IPOPT_OLEN] = optlen;
1731			/*
1732			 * Move first hop before start of options.
1733			 */
1734			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1735			    sizeof(struct in_addr));
1736			/*
1737			 * Then copy rest of options back
1738			 * to close up the deleted entry.
1739			 */
1740			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1741			    &cp[IPOPT_OFFSET+1],
1742			    (unsigned)cnt + sizeof(struct in_addr));
1743			break;
1744		}
1745	}
1746	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1747		goto bad;
1748	*pcbopt = m;
1749	return (0);
1750
1751bad:
1752	(void)m_free(m);
1753	return (EINVAL);
1754}
1755
1756/*
1757 * XXX
1758 * The whole multicast option thing needs to be re-thought.
1759 * Several of these options are equally applicable to non-multicast
1760 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1761 * standard option (IP_TTL).
1762 */
1763
1764/*
1765 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1766 */
1767static struct ifnet *
1768ip_multicast_if(a, ifindexp)
1769	struct in_addr *a;
1770	int *ifindexp;
1771{
1772	int ifindex;
1773	struct ifnet *ifp;
1774
1775	if (ifindexp)
1776		*ifindexp = 0;
1777	if (ntohl(a->s_addr) >> 24 == 0) {
1778		ifindex = ntohl(a->s_addr) & 0xffffff;
1779		if (ifindex < 0 || if_index < ifindex)
1780			return NULL;
1781		ifp = ifnet_byindex(ifindex);
1782		if (ifindexp)
1783			*ifindexp = ifindex;
1784	} else {
1785		INADDR_TO_IFP(*a, ifp);
1786	}
1787	return ifp;
1788}
1789
1790/*
1791 * Set the IP multicast options in response to user setsockopt().
1792 */
1793static int
1794ip_setmoptions(sopt, imop)
1795	struct sockopt *sopt;
1796	struct ip_moptions **imop;
1797{
1798	int error = 0;
1799	int i;
1800	struct in_addr addr;
1801	struct ip_mreq mreq;
1802	struct ifnet *ifp;
1803	struct ip_moptions *imo = *imop;
1804	struct route ro;
1805	struct sockaddr_in *dst;
1806	int ifindex;
1807	int s;
1808
1809	if (imo == NULL) {
1810		/*
1811		 * No multicast option buffer attached to the pcb;
1812		 * allocate one and initialize to default values.
1813		 */
1814		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1815		    M_WAITOK);
1816
1817		if (imo == NULL)
1818			return (ENOBUFS);
1819		*imop = imo;
1820		imo->imo_multicast_ifp = NULL;
1821		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1822		imo->imo_multicast_vif = -1;
1823		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1824		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1825		imo->imo_num_memberships = 0;
1826	}
1827
1828	switch (sopt->sopt_name) {
1829	/* store an index number for the vif you wanna use in the send */
1830	case IP_MULTICAST_VIF:
1831		if (legal_vif_num == 0) {
1832			error = EOPNOTSUPP;
1833			break;
1834		}
1835		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1836		if (error)
1837			break;
1838		if (!legal_vif_num(i) && (i != -1)) {
1839			error = EINVAL;
1840			break;
1841		}
1842		imo->imo_multicast_vif = i;
1843		break;
1844
1845	case IP_MULTICAST_IF:
1846		/*
1847		 * Select the interface for outgoing multicast packets.
1848		 */
1849		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1850		if (error)
1851			break;
1852		/*
1853		 * INADDR_ANY is used to remove a previous selection.
1854		 * When no interface is selected, a default one is
1855		 * chosen every time a multicast packet is sent.
1856		 */
1857		if (addr.s_addr == INADDR_ANY) {
1858			imo->imo_multicast_ifp = NULL;
1859			break;
1860		}
1861		/*
1862		 * The selected interface is identified by its local
1863		 * IP address.  Find the interface and confirm that
1864		 * it supports multicasting.
1865		 */
1866		s = splimp();
1867		ifp = ip_multicast_if(&addr, &ifindex);
1868		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1869			splx(s);
1870			error = EADDRNOTAVAIL;
1871			break;
1872		}
1873		imo->imo_multicast_ifp = ifp;
1874		if (ifindex)
1875			imo->imo_multicast_addr = addr;
1876		else
1877			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1878		splx(s);
1879		break;
1880
1881	case IP_MULTICAST_TTL:
1882		/*
1883		 * Set the IP time-to-live for outgoing multicast packets.
1884		 * The original multicast API required a char argument,
1885		 * which is inconsistent with the rest of the socket API.
1886		 * We allow either a char or an int.
1887		 */
1888		if (sopt->sopt_valsize == 1) {
1889			u_char ttl;
1890			error = sooptcopyin(sopt, &ttl, 1, 1);
1891			if (error)
1892				break;
1893			imo->imo_multicast_ttl = ttl;
1894		} else {
1895			u_int ttl;
1896			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1897					    sizeof ttl);
1898			if (error)
1899				break;
1900			if (ttl > 255)
1901				error = EINVAL;
1902			else
1903				imo->imo_multicast_ttl = ttl;
1904		}
1905		break;
1906
1907	case IP_MULTICAST_LOOP:
1908		/*
1909		 * Set the loopback flag for outgoing multicast packets.
1910		 * Must be zero or one.  The original multicast API required a
1911		 * char argument, which is inconsistent with the rest
1912		 * of the socket API.  We allow either a char or an int.
1913		 */
1914		if (sopt->sopt_valsize == 1) {
1915			u_char loop;
1916			error = sooptcopyin(sopt, &loop, 1, 1);
1917			if (error)
1918				break;
1919			imo->imo_multicast_loop = !!loop;
1920		} else {
1921			u_int loop;
1922			error = sooptcopyin(sopt, &loop, sizeof loop,
1923					    sizeof loop);
1924			if (error)
1925				break;
1926			imo->imo_multicast_loop = !!loop;
1927		}
1928		break;
1929
1930	case IP_ADD_MEMBERSHIP:
1931		/*
1932		 * Add a multicast group membership.
1933		 * Group must be a valid IP multicast address.
1934		 */
1935		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1936		if (error)
1937			break;
1938
1939		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1940			error = EINVAL;
1941			break;
1942		}
1943		s = splimp();
1944		/*
1945		 * If no interface address was provided, use the interface of
1946		 * the route to the given multicast address.
1947		 */
1948		if (mreq.imr_interface.s_addr == INADDR_ANY) {
1949			bzero((caddr_t)&ro, sizeof(ro));
1950			dst = (struct sockaddr_in *)&ro.ro_dst;
1951			dst->sin_len = sizeof(*dst);
1952			dst->sin_family = AF_INET;
1953			dst->sin_addr = mreq.imr_multiaddr;
1954			rtalloc_ign(&ro, RTF_CLONING);
1955			if (ro.ro_rt == NULL) {
1956				error = EADDRNOTAVAIL;
1957				splx(s);
1958				break;
1959			}
1960			ifp = ro.ro_rt->rt_ifp;
1961			RTFREE(ro.ro_rt);
1962		}
1963		else {
1964			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1965		}
1966
1967		/*
1968		 * See if we found an interface, and confirm that it
1969		 * supports multicast.
1970		 */
1971		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1972			error = EADDRNOTAVAIL;
1973			splx(s);
1974			break;
1975		}
1976		/*
1977		 * See if the membership already exists or if all the
1978		 * membership slots are full.
1979		 */
1980		for (i = 0; i < imo->imo_num_memberships; ++i) {
1981			if (imo->imo_membership[i]->inm_ifp == ifp &&
1982			    imo->imo_membership[i]->inm_addr.s_addr
1983						== mreq.imr_multiaddr.s_addr)
1984				break;
1985		}
1986		if (i < imo->imo_num_memberships) {
1987			error = EADDRINUSE;
1988			splx(s);
1989			break;
1990		}
1991		if (i == IP_MAX_MEMBERSHIPS) {
1992			error = ETOOMANYREFS;
1993			splx(s);
1994			break;
1995		}
1996		/*
1997		 * Everything looks good; add a new record to the multicast
1998		 * address list for the given interface.
1999		 */
2000		if ((imo->imo_membership[i] =
2001		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
2002			error = ENOBUFS;
2003			splx(s);
2004			break;
2005		}
2006		++imo->imo_num_memberships;
2007		splx(s);
2008		break;
2009
2010	case IP_DROP_MEMBERSHIP:
2011		/*
2012		 * Drop a multicast group membership.
2013		 * Group must be a valid IP multicast address.
2014		 */
2015		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
2016		if (error)
2017			break;
2018
2019		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
2020			error = EINVAL;
2021			break;
2022		}
2023
2024		s = splimp();
2025		/*
2026		 * If an interface address was specified, get a pointer
2027		 * to its ifnet structure.
2028		 */
2029		if (mreq.imr_interface.s_addr == INADDR_ANY)
2030			ifp = NULL;
2031		else {
2032			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
2033			if (ifp == NULL) {
2034				error = EADDRNOTAVAIL;
2035				splx(s);
2036				break;
2037			}
2038		}
2039		/*
2040		 * Find the membership in the membership array.
2041		 */
2042		for (i = 0; i < imo->imo_num_memberships; ++i) {
2043			if ((ifp == NULL ||
2044			     imo->imo_membership[i]->inm_ifp == ifp) &&
2045			     imo->imo_membership[i]->inm_addr.s_addr ==
2046			     mreq.imr_multiaddr.s_addr)
2047				break;
2048		}
2049		if (i == imo->imo_num_memberships) {
2050			error = EADDRNOTAVAIL;
2051			splx(s);
2052			break;
2053		}
2054		/*
2055		 * Give up the multicast address record to which the
2056		 * membership points.
2057		 */
2058		in_delmulti(imo->imo_membership[i]);
2059		/*
2060		 * Remove the gap in the membership array.
2061		 */
2062		for (++i; i < imo->imo_num_memberships; ++i)
2063			imo->imo_membership[i-1] = imo->imo_membership[i];
2064		--imo->imo_num_memberships;
2065		splx(s);
2066		break;
2067
2068	default:
2069		error = EOPNOTSUPP;
2070		break;
2071	}
2072
2073	/*
2074	 * If all options have default values, no need to keep the mbuf.
2075	 */
2076	if (imo->imo_multicast_ifp == NULL &&
2077	    imo->imo_multicast_vif == -1 &&
2078	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2079	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2080	    imo->imo_num_memberships == 0) {
2081		free(*imop, M_IPMOPTS);
2082		*imop = NULL;
2083	}
2084
2085	return (error);
2086}
2087
2088/*
2089 * Return the IP multicast options in response to user getsockopt().
2090 */
2091static int
2092ip_getmoptions(sopt, imo)
2093	struct sockopt *sopt;
2094	register struct ip_moptions *imo;
2095{
2096	struct in_addr addr;
2097	struct in_ifaddr *ia;
2098	int error, optval;
2099	u_char coptval;
2100
2101	error = 0;
2102	switch (sopt->sopt_name) {
2103	case IP_MULTICAST_VIF:
2104		if (imo != NULL)
2105			optval = imo->imo_multicast_vif;
2106		else
2107			optval = -1;
2108		error = sooptcopyout(sopt, &optval, sizeof optval);
2109		break;
2110
2111	case IP_MULTICAST_IF:
2112		if (imo == NULL || imo->imo_multicast_ifp == NULL)
2113			addr.s_addr = INADDR_ANY;
2114		else if (imo->imo_multicast_addr.s_addr) {
2115			/* return the value user has set */
2116			addr = imo->imo_multicast_addr;
2117		} else {
2118			IFP_TO_IA(imo->imo_multicast_ifp, ia);
2119			addr.s_addr = (ia == NULL) ? INADDR_ANY
2120				: IA_SIN(ia)->sin_addr.s_addr;
2121		}
2122		error = sooptcopyout(sopt, &addr, sizeof addr);
2123		break;
2124
2125	case IP_MULTICAST_TTL:
2126		if (imo == 0)
2127			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
2128		else
2129			optval = coptval = imo->imo_multicast_ttl;
2130		if (sopt->sopt_valsize == 1)
2131			error = sooptcopyout(sopt, &coptval, 1);
2132		else
2133			error = sooptcopyout(sopt, &optval, sizeof optval);
2134		break;
2135
2136	case IP_MULTICAST_LOOP:
2137		if (imo == 0)
2138			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
2139		else
2140			optval = coptval = imo->imo_multicast_loop;
2141		if (sopt->sopt_valsize == 1)
2142			error = sooptcopyout(sopt, &coptval, 1);
2143		else
2144			error = sooptcopyout(sopt, &optval, sizeof optval);
2145		break;
2146
2147	default:
2148		error = ENOPROTOOPT;
2149		break;
2150	}
2151	return (error);
2152}
2153
2154/*
2155 * Discard the IP multicast options.
2156 */
2157void
2158ip_freemoptions(imo)
2159	register struct ip_moptions *imo;
2160{
2161	register int i;
2162
2163	if (imo != NULL) {
2164		for (i = 0; i < imo->imo_num_memberships; ++i)
2165			in_delmulti(imo->imo_membership[i]);
2166		free(imo, M_IPMOPTS);
2167	}
2168}
2169
2170/*
2171 * Routine called from ip_output() to loop back a copy of an IP multicast
2172 * packet to the input queue of a specified interface.  Note that this
2173 * calls the output routine of the loopback "driver", but with an interface
2174 * pointer that might NOT be a loopback interface -- evil, but easier than
2175 * replicating that code here.
2176 */
2177static void
2178ip_mloopback(ifp, m, dst, hlen)
2179	struct ifnet *ifp;
2180	register struct mbuf *m;
2181	register struct sockaddr_in *dst;
2182	int hlen;
2183{
2184	register struct ip *ip;
2185	struct mbuf *copym;
2186
2187	copym = m_copy(m, 0, M_COPYALL);
2188	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
2189		copym = m_pullup(copym, hlen);
2190	if (copym != NULL) {
2191		/* If needed, compute the checksum and mark it as valid. */
2192		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2193			in_delayed_cksum(copym);
2194			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2195			copym->m_pkthdr.csum_flags |=
2196			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2197			copym->m_pkthdr.csum_data = 0xffff;
2198		}
2199		/*
2200		 * We don't bother to fragment if the IP length is greater
2201		 * than the interface's MTU.  Can this possibly matter?
2202		 */
2203		ip = mtod(copym, struct ip *);
2204		ip->ip_len = htons(ip->ip_len);
2205		ip->ip_off = htons(ip->ip_off);
2206		ip->ip_sum = 0;
2207		ip->ip_sum = in_cksum(copym, hlen);
2208		/*
2209		 * NB:
2210		 * It's not clear whether there are any lingering
2211		 * reentrancy problems in other areas which might
2212		 * be exposed by using ip_input directly (in
2213		 * particular, everything which modifies the packet
2214		 * in-place).  Yet another option is using the
2215		 * protosw directly to deliver the looped back
2216		 * packet.  For the moment, we'll err on the side
2217		 * of safety by using if_simloop().
2218		 */
2219#if 1 /* XXX */
2220		if (dst->sin_family != AF_INET) {
2221			printf("ip_mloopback: bad address family %d\n",
2222						dst->sin_family);
2223			dst->sin_family = AF_INET;
2224		}
2225#endif
2226
2227#ifdef notdef
2228		copym->m_pkthdr.rcvif = ifp;
2229		ip_input(copym);
2230#else
2231		if_simloop(ifp, copym, dst->sin_family, 0);
2232#endif
2233	}
2234}
2235