ip_output.c revision 147256
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30 * $FreeBSD: head/sys/netinet/ip_output.c 147256 2005-06-10 16:49:24Z brooks $
31 */
32
33#include "opt_ipfw.h"
34#include "opt_ipsec.h"
35#include "opt_mac.h"
36#include "opt_mbuf_stress_test.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/mac.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/protosw.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47#include <sys/sysctl.h>
48
49#include <net/if.h>
50#include <net/netisr.h>
51#include <net/pfil.h>
52#include <net/route.h>
53
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#include <netinet/ip.h>
57#include <netinet/in_pcb.h>
58#include <netinet/in_var.h>
59#include <netinet/ip_var.h>
60
61#include <machine/in_cksum.h>
62
63static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
64
65#ifdef IPSEC
66#include <netinet6/ipsec.h>
67#include <netkey/key.h>
68#ifdef IPSEC_DEBUG
69#include <netkey/key_debug.h>
70#else
71#define	KEYDEBUG(lev,arg)
72#endif
73#endif /*IPSEC*/
74
75#ifdef FAST_IPSEC
76#include <netipsec/ipsec.h>
77#include <netipsec/xform.h>
78#include <netipsec/key.h>
79#endif /*FAST_IPSEC*/
80
81#define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
82				x, (ntohl(a.s_addr)>>24)&0xFF,\
83				  (ntohl(a.s_addr)>>16)&0xFF,\
84				  (ntohl(a.s_addr)>>8)&0xFF,\
85				  (ntohl(a.s_addr))&0xFF, y);
86
87u_short ip_id;
88
89#ifdef MBUF_STRESS_TEST
90int mbuf_frag_size = 0;
91SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
92	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
93#endif
94
95static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
96static struct ifnet *ip_multicast_if(struct in_addr *, int *);
97static void	ip_mloopback
98	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
99static int	ip_getmoptions(struct inpcb *, struct sockopt *);
100static int	ip_pcbopts(struct inpcb *, int, struct mbuf *);
101static int	ip_setmoptions(struct inpcb *, struct sockopt *);
102
103int	ip_optcopy(struct ip *, struct ip *);
104
105
106extern	struct protosw inetsw[];
107
108/*
109 * IP output.  The packet in mbuf chain m contains a skeletal IP
110 * header (with len, off, ttl, proto, tos, src, dst).
111 * The mbuf chain containing the packet will be freed.
112 * The mbuf opt, if present, will not be freed.
113 * In the IP forwarding case, the packet will arrive with options already
114 * inserted, so must have a NULL opt pointer.
115 */
116int
117ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
118	int flags, struct ip_moptions *imo, struct inpcb *inp)
119{
120	struct ip *ip;
121	struct ifnet *ifp = NULL;	/* keep compiler happy */
122	struct mbuf *m0;
123	int hlen = sizeof (struct ip);
124	int len, error = 0;
125	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
126	struct in_ifaddr *ia = NULL;
127	int isbroadcast, sw_csum;
128	struct route iproute;
129	struct in_addr odst;
130#ifdef IPFIREWALL_FORWARD
131	struct m_tag *fwd_tag = NULL;
132#endif
133#ifdef IPSEC
134	struct secpolicy *sp = NULL;
135#endif
136#ifdef FAST_IPSEC
137	struct secpolicy *sp = NULL;
138	struct tdb_ident *tdbi;
139	struct m_tag *mtag;
140	int s;
141#endif /* FAST_IPSEC */
142
143	M_ASSERTPKTHDR(m);
144
145	if (ro == NULL) {
146		ro = &iproute;
147		bzero(ro, sizeof (*ro));
148	}
149
150	if (inp != NULL)
151		INP_LOCK_ASSERT(inp);
152
153	if (opt) {
154		len = 0;
155		m = ip_insertoptions(m, opt, &len);
156		if (len != 0)
157			hlen = len;
158	}
159	ip = mtod(m, struct ip *);
160
161	/*
162	 * Fill in IP header.  If we are not allowing fragmentation,
163	 * then the ip_id field is meaningless, but we don't set it
164	 * to zero.  Doing so causes various problems when devices along
165	 * the path (routers, load balancers, firewalls, etc.) illegally
166	 * disable DF on our packet.  Note that a 16-bit counter
167	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
168	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
169	 * for Counting NATted Hosts", Proc. IMW'02, available at
170	 * <http://www.research.att.com/~smb/papers/fnat.pdf>.
171	 */
172	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
173		ip->ip_v = IPVERSION;
174		ip->ip_hl = hlen >> 2;
175		ip->ip_id = ip_newid();
176		ipstat.ips_localout++;
177	} else {
178		hlen = ip->ip_hl << 2;
179	}
180
181	dst = (struct sockaddr_in *)&ro->ro_dst;
182again:
183	/*
184	 * If there is a cached route,
185	 * check that it is to the same destination
186	 * and is still up.  If not, free it and try again.
187	 * The address family should also be checked in case of sharing the
188	 * cache with IPv6.
189	 */
190	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
191			  dst->sin_family != AF_INET ||
192			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
193		RTFREE(ro->ro_rt);
194		ro->ro_rt = (struct rtentry *)0;
195	}
196#ifdef IPFIREWALL_FORWARD
197	if (ro->ro_rt == NULL && fwd_tag == NULL) {
198#else
199	if (ro->ro_rt == NULL) {
200#endif
201		bzero(dst, sizeof(*dst));
202		dst->sin_family = AF_INET;
203		dst->sin_len = sizeof(*dst);
204		dst->sin_addr = ip->ip_dst;
205	}
206	/*
207	 * If routing to interface only,
208	 * short circuit routing lookup.
209	 */
210	if (flags & IP_ROUTETOIF) {
211		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
212		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
213			ipstat.ips_noroute++;
214			error = ENETUNREACH;
215			goto bad;
216		}
217		ifp = ia->ia_ifp;
218		ip->ip_ttl = 1;
219		isbroadcast = in_broadcast(dst->sin_addr, ifp);
220	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
221	    imo != NULL && imo->imo_multicast_ifp != NULL) {
222		/*
223		 * Bypass the normal routing lookup for multicast
224		 * packets if the interface is specified.
225		 */
226		ifp = imo->imo_multicast_ifp;
227		IFP_TO_IA(ifp, ia);
228		isbroadcast = 0;	/* fool gcc */
229	} else {
230		/*
231		 * We want to do any cloning requested by the link layer,
232		 * as this is probably required in all cases for correct
233		 * operation (as it is for ARP).
234		 */
235		if (ro->ro_rt == NULL)
236			rtalloc_ign(ro, 0);
237		if (ro->ro_rt == NULL) {
238			ipstat.ips_noroute++;
239			error = EHOSTUNREACH;
240			goto bad;
241		}
242		ia = ifatoia(ro->ro_rt->rt_ifa);
243		ifp = ro->ro_rt->rt_ifp;
244		ro->ro_rt->rt_rmx.rmx_pksent++;
245		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
246			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
247		if (ro->ro_rt->rt_flags & RTF_HOST)
248			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
249		else
250			isbroadcast = in_broadcast(dst->sin_addr, ifp);
251	}
252	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
253		struct in_multi *inm;
254
255		m->m_flags |= M_MCAST;
256		/*
257		 * IP destination address is multicast.  Make sure "dst"
258		 * still points to the address in "ro".  (It may have been
259		 * changed to point to a gateway address, above.)
260		 */
261		dst = (struct sockaddr_in *)&ro->ro_dst;
262		/*
263		 * See if the caller provided any multicast options
264		 */
265		if (imo != NULL) {
266			ip->ip_ttl = imo->imo_multicast_ttl;
267			if (imo->imo_multicast_vif != -1)
268				ip->ip_src.s_addr =
269				    ip_mcast_src ?
270				    ip_mcast_src(imo->imo_multicast_vif) :
271				    INADDR_ANY;
272		} else
273			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
274		/*
275		 * Confirm that the outgoing interface supports multicast.
276		 */
277		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
278			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
279				ipstat.ips_noroute++;
280				error = ENETUNREACH;
281				goto bad;
282			}
283		}
284		/*
285		 * If source address not specified yet, use address
286		 * of outgoing interface.
287		 */
288		if (ip->ip_src.s_addr == INADDR_ANY) {
289			/* Interface may have no addresses. */
290			if (ia != NULL)
291				ip->ip_src = IA_SIN(ia)->sin_addr;
292		}
293
294		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
295		if (inm != NULL &&
296		   (imo == NULL || imo->imo_multicast_loop)) {
297			/*
298			 * If we belong to the destination multicast group
299			 * on the outgoing interface, and the caller did not
300			 * forbid loopback, loop back a copy.
301			 */
302			ip_mloopback(ifp, m, dst, hlen);
303		}
304		else {
305			/*
306			 * If we are acting as a multicast router, perform
307			 * multicast forwarding as if the packet had just
308			 * arrived on the interface to which we are about
309			 * to send.  The multicast forwarding function
310			 * recursively calls this function, using the
311			 * IP_FORWARDING flag to prevent infinite recursion.
312			 *
313			 * Multicasts that are looped back by ip_mloopback(),
314			 * above, will be forwarded by the ip_input() routine,
315			 * if necessary.
316			 */
317			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
318				/*
319				 * If rsvp daemon is not running, do not
320				 * set ip_moptions. This ensures that the packet
321				 * is multicast and not just sent down one link
322				 * as prescribed by rsvpd.
323				 */
324				if (!rsvp_on)
325					imo = NULL;
326				if (ip_mforward &&
327				    ip_mforward(ip, ifp, m, imo) != 0) {
328					m_freem(m);
329					goto done;
330				}
331			}
332		}
333
334		/*
335		 * Multicasts with a time-to-live of zero may be looped-
336		 * back, above, but must not be transmitted on a network.
337		 * Also, multicasts addressed to the loopback interface
338		 * are not sent -- the above call to ip_mloopback() will
339		 * loop back a copy if this host actually belongs to the
340		 * destination group on the loopback interface.
341		 */
342		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
343			m_freem(m);
344			goto done;
345		}
346
347		goto sendit;
348	}
349#ifndef notdef
350	/*
351	 * If the source address is not specified yet, use the address
352	 * of the outoing interface.
353	 */
354	if (ip->ip_src.s_addr == INADDR_ANY) {
355		/* Interface may have no addresses. */
356		if (ia != NULL) {
357			ip->ip_src = IA_SIN(ia)->sin_addr;
358		}
359	}
360#endif /* notdef */
361	/*
362	 * Verify that we have any chance at all of being able to queue the
363	 * packet or packet fragments, unless ALTQ is enabled on the given
364	 * interface in which case packetdrop should be done by queueing.
365	 */
366#ifdef ALTQ
367	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
368	    ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
369	    ifp->if_snd.ifq_maxlen))
370#else
371	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
372	    ifp->if_snd.ifq_maxlen)
373#endif /* ALTQ */
374	{
375		error = ENOBUFS;
376		ipstat.ips_odropped++;
377		goto bad;
378	}
379
380	/*
381	 * Look for broadcast address and
382	 * verify user is allowed to send
383	 * such a packet.
384	 */
385	if (isbroadcast) {
386		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
387			error = EADDRNOTAVAIL;
388			goto bad;
389		}
390		if ((flags & IP_ALLOWBROADCAST) == 0) {
391			error = EACCES;
392			goto bad;
393		}
394		/* don't allow broadcast messages to be fragmented */
395		if (ip->ip_len > ifp->if_mtu) {
396			error = EMSGSIZE;
397			goto bad;
398		}
399		if (flags & IP_SENDONES)
400			ip->ip_dst.s_addr = INADDR_BROADCAST;
401		m->m_flags |= M_BCAST;
402	} else {
403		m->m_flags &= ~M_BCAST;
404	}
405
406sendit:
407#ifdef IPSEC
408	/* get SP for this packet */
409	if (inp == NULL)
410		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
411		    flags, &error);
412	else
413		sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
414
415	if (sp == NULL) {
416		ipsecstat.out_inval++;
417		goto bad;
418	}
419
420	error = 0;
421
422	/* check policy */
423	switch (sp->policy) {
424	case IPSEC_POLICY_DISCARD:
425		/*
426		 * This packet is just discarded.
427		 */
428		ipsecstat.out_polvio++;
429		goto bad;
430
431	case IPSEC_POLICY_BYPASS:
432	case IPSEC_POLICY_NONE:
433	case IPSEC_POLICY_TCP:
434		/* no need to do IPsec. */
435		goto skip_ipsec;
436
437	case IPSEC_POLICY_IPSEC:
438		if (sp->req == NULL) {
439			/* acquire a policy */
440			error = key_spdacquire(sp);
441			goto bad;
442		}
443		break;
444
445	case IPSEC_POLICY_ENTRUST:
446	default:
447		printf("ip_output: Invalid policy found. %d\n", sp->policy);
448	}
449    {
450	struct ipsec_output_state state;
451	bzero(&state, sizeof(state));
452	state.m = m;
453	if (flags & IP_ROUTETOIF) {
454		state.ro = &iproute;
455		bzero(&iproute, sizeof(iproute));
456	} else
457		state.ro = ro;
458	state.dst = (struct sockaddr *)dst;
459
460	ip->ip_sum = 0;
461
462	/*
463	 * XXX
464	 * delayed checksums are not currently compatible with IPsec
465	 */
466	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
467		in_delayed_cksum(m);
468		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
469	}
470
471	ip->ip_len = htons(ip->ip_len);
472	ip->ip_off = htons(ip->ip_off);
473
474	error = ipsec4_output(&state, sp, flags);
475
476	m = state.m;
477	if (flags & IP_ROUTETOIF) {
478		/*
479		 * if we have tunnel mode SA, we may need to ignore
480		 * IP_ROUTETOIF.
481		 */
482		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
483			flags &= ~IP_ROUTETOIF;
484			ro = state.ro;
485		}
486	} else
487		ro = state.ro;
488	dst = (struct sockaddr_in *)state.dst;
489	if (error) {
490		/* mbuf is already reclaimed in ipsec4_output. */
491		m = NULL;
492		switch (error) {
493		case EHOSTUNREACH:
494		case ENETUNREACH:
495		case EMSGSIZE:
496		case ENOBUFS:
497		case ENOMEM:
498			break;
499		default:
500			printf("ip4_output (ipsec): error code %d\n", error);
501			/*fall through*/
502		case ENOENT:
503			/* don't show these error codes to the user */
504			error = 0;
505			break;
506		}
507		goto bad;
508	}
509
510	/* be sure to update variables that are affected by ipsec4_output() */
511	ip = mtod(m, struct ip *);
512	hlen = ip->ip_hl << 2;
513	if (ro->ro_rt == NULL) {
514		if ((flags & IP_ROUTETOIF) == 0) {
515			printf("ip_output: "
516				"can't update route after IPsec processing\n");
517			error = EHOSTUNREACH;	/*XXX*/
518			goto bad;
519		}
520	} else {
521		if (state.encap) {
522			ia = ifatoia(ro->ro_rt->rt_ifa);
523			ifp = ro->ro_rt->rt_ifp;
524		}
525	}
526    }
527
528	/* make it flipped, again. */
529	ip->ip_len = ntohs(ip->ip_len);
530	ip->ip_off = ntohs(ip->ip_off);
531skip_ipsec:
532#endif /*IPSEC*/
533#ifdef FAST_IPSEC
534	/*
535	 * Check the security policy (SP) for the packet and, if
536	 * required, do IPsec-related processing.  There are two
537	 * cases here; the first time a packet is sent through
538	 * it will be untagged and handled by ipsec4_checkpolicy.
539	 * If the packet is resubmitted to ip_output (e.g. after
540	 * AH, ESP, etc. processing), there will be a tag to bypass
541	 * the lookup and related policy checking.
542	 */
543	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
544	s = splnet();
545	if (mtag != NULL) {
546		tdbi = (struct tdb_ident *)(mtag + 1);
547		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
548		if (sp == NULL)
549			error = -EINVAL;	/* force silent drop */
550		m_tag_delete(m, mtag);
551	} else {
552		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
553					&error, inp);
554	}
555	/*
556	 * There are four return cases:
557	 *    sp != NULL	 	    apply IPsec policy
558	 *    sp == NULL, error == 0	    no IPsec handling needed
559	 *    sp == NULL, error == -EINVAL  discard packet w/o error
560	 *    sp == NULL, error != 0	    discard packet, report error
561	 */
562	if (sp != NULL) {
563		/* Loop detection, check if ipsec processing already done */
564		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
565		for (mtag = m_tag_first(m); mtag != NULL;
566		     mtag = m_tag_next(m, mtag)) {
567			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
568				continue;
569			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
570			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
571				continue;
572			/*
573			 * Check if policy has an SA associated with it.
574			 * This can happen when an SP has yet to acquire
575			 * an SA; e.g. on first reference.  If it occurs,
576			 * then we let ipsec4_process_packet do its thing.
577			 */
578			if (sp->req->sav == NULL)
579				break;
580			tdbi = (struct tdb_ident *)(mtag + 1);
581			if (tdbi->spi == sp->req->sav->spi &&
582			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
583			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
584				 sizeof (union sockaddr_union)) == 0) {
585				/*
586				 * No IPsec processing is needed, free
587				 * reference to SP.
588				 *
589				 * NB: null pointer to avoid free at
590				 *     done: below.
591				 */
592				KEY_FREESP(&sp), sp = NULL;
593				splx(s);
594				goto spd_done;
595			}
596		}
597
598		/*
599		 * Do delayed checksums now because we send before
600		 * this is done in the normal processing path.
601		 */
602		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
603			in_delayed_cksum(m);
604			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
605		}
606
607		ip->ip_len = htons(ip->ip_len);
608		ip->ip_off = htons(ip->ip_off);
609
610		/* NB: callee frees mbuf */
611		error = ipsec4_process_packet(m, sp->req, flags, 0);
612		/*
613		 * Preserve KAME behaviour: ENOENT can be returned
614		 * when an SA acquire is in progress.  Don't propagate
615		 * this to user-level; it confuses applications.
616		 *
617		 * XXX this will go away when the SADB is redone.
618		 */
619		if (error == ENOENT)
620			error = 0;
621		splx(s);
622		goto done;
623	} else {
624		splx(s);
625
626		if (error != 0) {
627			/*
628			 * Hack: -EINVAL is used to signal that a packet
629			 * should be silently discarded.  This is typically
630			 * because we asked key management for an SA and
631			 * it was delayed (e.g. kicked up to IKE).
632			 */
633			if (error == -EINVAL)
634				error = 0;
635			goto bad;
636		} else {
637			/* No IPsec processing for this packet. */
638		}
639#ifdef notyet
640		/*
641		 * If deferred crypto processing is needed, check that
642		 * the interface supports it.
643		 */
644		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
645		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
646			/* notify IPsec to do its own crypto */
647			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
648			error = EHOSTUNREACH;
649			goto bad;
650		}
651#endif
652	}
653spd_done:
654#endif /* FAST_IPSEC */
655
656	/* Jump over all PFIL processing if hooks are not active. */
657	if (inet_pfil_hook.ph_busy_count == -1)
658		goto passout;
659
660	/* Run through list of hooks for output packets. */
661	odst.s_addr = ip->ip_dst.s_addr;
662	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
663	if (error != 0 || m == NULL)
664		goto done;
665
666	ip = mtod(m, struct ip *);
667
668	/* See if destination IP address was changed by packet filter. */
669	if (odst.s_addr != ip->ip_dst.s_addr) {
670		m->m_flags |= M_SKIP_FIREWALL;
671		/* If destination is now ourself drop to ip_input(). */
672		if (in_localip(ip->ip_dst)) {
673			m->m_flags |= M_FASTFWD_OURS;
674			if (m->m_pkthdr.rcvif == NULL)
675				m->m_pkthdr.rcvif = loif;
676			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
677				m->m_pkthdr.csum_flags |=
678				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
679				m->m_pkthdr.csum_data = 0xffff;
680			}
681			m->m_pkthdr.csum_flags |=
682			    CSUM_IP_CHECKED | CSUM_IP_VALID;
683
684			error = netisr_queue(NETISR_IP, m);
685			goto done;
686		} else
687			goto again;	/* Redo the routing table lookup. */
688	}
689
690#ifdef IPFIREWALL_FORWARD
691	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
692	if (m->m_flags & M_FASTFWD_OURS) {
693		if (m->m_pkthdr.rcvif == NULL)
694			m->m_pkthdr.rcvif = loif;
695		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
696			m->m_pkthdr.csum_flags |=
697			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
698			m->m_pkthdr.csum_data = 0xffff;
699		}
700		m->m_pkthdr.csum_flags |=
701			    CSUM_IP_CHECKED | CSUM_IP_VALID;
702
703		error = netisr_queue(NETISR_IP, m);
704		goto done;
705	}
706	/* Or forward to some other address? */
707	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
708	if (fwd_tag) {
709#ifndef IPFIREWALL_FORWARD_EXTENDED
710		if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) {
711#endif
712			dst = (struct sockaddr_in *)&ro->ro_dst;
713			bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
714			m->m_flags |= M_SKIP_FIREWALL;
715			m_tag_delete(m, fwd_tag);
716			goto again;
717#ifndef IPFIREWALL_FORWARD_EXTENDED
718		} else {
719			m_tag_delete(m, fwd_tag);
720			/* Continue. */
721		}
722#endif
723	}
724#endif /* IPFIREWALL_FORWARD */
725
726passout:
727	/* 127/8 must not appear on wire - RFC1122. */
728	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
729	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
730		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
731			ipstat.ips_badaddr++;
732			error = EADDRNOTAVAIL;
733			goto bad;
734		}
735	}
736
737	m->m_pkthdr.csum_flags |= CSUM_IP;
738	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
739	if (sw_csum & CSUM_DELAY_DATA) {
740		in_delayed_cksum(m);
741		sw_csum &= ~CSUM_DELAY_DATA;
742	}
743	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
744
745	/*
746	 * If small enough for interface, or the interface will take
747	 * care of the fragmentation for us, can just send directly.
748	 */
749	if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT &&
750	    ((ip->ip_off & IP_DF) == 0))) {
751		ip->ip_len = htons(ip->ip_len);
752		ip->ip_off = htons(ip->ip_off);
753		ip->ip_sum = 0;
754		if (sw_csum & CSUM_DELAY_IP)
755			ip->ip_sum = in_cksum(m, hlen);
756
757		/* Record statistics for this interface address. */
758		if (!(flags & IP_FORWARDING) && ia) {
759			ia->ia_ifa.if_opackets++;
760			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
761		}
762
763#ifdef IPSEC
764		/* clean ipsec history once it goes out of the node */
765		ipsec_delaux(m);
766#endif
767
768#ifdef MBUF_STRESS_TEST
769		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
770			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
771#endif
772		error = (*ifp->if_output)(ifp, m,
773				(struct sockaddr *)dst, ro->ro_rt);
774		goto done;
775	}
776
777	if (ip->ip_off & IP_DF) {
778		error = EMSGSIZE;
779		/*
780		 * This case can happen if the user changed the MTU
781		 * of an interface after enabling IP on it.  Because
782		 * most netifs don't keep track of routes pointing to
783		 * them, there is no way for one to update all its
784		 * routes when the MTU is changed.
785		 */
786		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
787		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
788			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
789		}
790		ipstat.ips_cantfrag++;
791		goto bad;
792	}
793
794	/*
795	 * Too large for interface; fragment if possible. If successful,
796	 * on return, m will point to a list of packets to be sent.
797	 */
798	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
799	if (error)
800		goto bad;
801	for (; m; m = m0) {
802		m0 = m->m_nextpkt;
803		m->m_nextpkt = 0;
804#ifdef IPSEC
805		/* clean ipsec history once it goes out of the node */
806		ipsec_delaux(m);
807#endif
808		if (error == 0) {
809			/* Record statistics for this interface address. */
810			if (ia != NULL) {
811				ia->ia_ifa.if_opackets++;
812				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
813			}
814
815			error = (*ifp->if_output)(ifp, m,
816			    (struct sockaddr *)dst, ro->ro_rt);
817		} else
818			m_freem(m);
819	}
820
821	if (error == 0)
822		ipstat.ips_fragmented++;
823
824done:
825	if (ro == &iproute && ro->ro_rt) {
826		RTFREE(ro->ro_rt);
827	}
828#ifdef IPSEC
829	if (sp != NULL) {
830		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
831			printf("DP ip_output call free SP:%p\n", sp));
832		key_freesp(sp);
833	}
834#endif
835#ifdef FAST_IPSEC
836	if (sp != NULL)
837		KEY_FREESP(&sp);
838#endif
839	return (error);
840bad:
841	m_freem(m);
842	goto done;
843}
844
845/*
846 * Create a chain of fragments which fit the given mtu. m_frag points to the
847 * mbuf to be fragmented; on return it points to the chain with the fragments.
848 * Return 0 if no error. If error, m_frag may contain a partially built
849 * chain of fragments that should be freed by the caller.
850 *
851 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
852 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
853 */
854int
855ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
856	    u_long if_hwassist_flags, int sw_csum)
857{
858	int error = 0;
859	int hlen = ip->ip_hl << 2;
860	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
861	int off;
862	struct mbuf *m0 = *m_frag;	/* the original packet		*/
863	int firstlen;
864	struct mbuf **mnext;
865	int nfrags;
866
867	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
868		ipstat.ips_cantfrag++;
869		return EMSGSIZE;
870	}
871
872	/*
873	 * Must be able to put at least 8 bytes per fragment.
874	 */
875	if (len < 8)
876		return EMSGSIZE;
877
878	/*
879	 * If the interface will not calculate checksums on
880	 * fragmented packets, then do it here.
881	 */
882	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
883	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
884		in_delayed_cksum(m0);
885		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
886	}
887
888	if (len > PAGE_SIZE) {
889		/*
890		 * Fragment large datagrams such that each segment
891		 * contains a multiple of PAGE_SIZE amount of data,
892		 * plus headers. This enables a receiver to perform
893		 * page-flipping zero-copy optimizations.
894		 *
895		 * XXX When does this help given that sender and receiver
896		 * could have different page sizes, and also mtu could
897		 * be less than the receiver's page size ?
898		 */
899		int newlen;
900		struct mbuf *m;
901
902		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
903			off += m->m_len;
904
905		/*
906		 * firstlen (off - hlen) must be aligned on an
907		 * 8-byte boundary
908		 */
909		if (off < hlen)
910			goto smart_frag_failure;
911		off = ((off - hlen) & ~7) + hlen;
912		newlen = (~PAGE_MASK) & mtu;
913		if ((newlen + sizeof (struct ip)) > mtu) {
914			/* we failed, go back the default */
915smart_frag_failure:
916			newlen = len;
917			off = hlen + len;
918		}
919		len = newlen;
920
921	} else {
922		off = hlen + len;
923	}
924
925	firstlen = off - hlen;
926	mnext = &m0->m_nextpkt;		/* pointer to next packet */
927
928	/*
929	 * Loop through length of segment after first fragment,
930	 * make new header and copy data of each part and link onto chain.
931	 * Here, m0 is the original packet, m is the fragment being created.
932	 * The fragments are linked off the m_nextpkt of the original
933	 * packet, which after processing serves as the first fragment.
934	 */
935	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
936		struct ip *mhip;	/* ip header on the fragment */
937		struct mbuf *m;
938		int mhlen = sizeof (struct ip);
939
940		MGETHDR(m, M_DONTWAIT, MT_HEADER);
941		if (m == NULL) {
942			error = ENOBUFS;
943			ipstat.ips_odropped++;
944			goto done;
945		}
946		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
947		/*
948		 * In the first mbuf, leave room for the link header, then
949		 * copy the original IP header including options. The payload
950		 * goes into an additional mbuf chain returned by m_copy().
951		 */
952		m->m_data += max_linkhdr;
953		mhip = mtod(m, struct ip *);
954		*mhip = *ip;
955		if (hlen > sizeof (struct ip)) {
956			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
957			mhip->ip_v = IPVERSION;
958			mhip->ip_hl = mhlen >> 2;
959		}
960		m->m_len = mhlen;
961		/* XXX do we need to add ip->ip_off below ? */
962		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
963		if (off + len >= ip->ip_len) {	/* last fragment */
964			len = ip->ip_len - off;
965			m->m_flags |= M_LASTFRAG;
966		} else
967			mhip->ip_off |= IP_MF;
968		mhip->ip_len = htons((u_short)(len + mhlen));
969		m->m_next = m_copy(m0, off, len);
970		if (m->m_next == NULL) {	/* copy failed */
971			m_free(m);
972			error = ENOBUFS;	/* ??? */
973			ipstat.ips_odropped++;
974			goto done;
975		}
976		m->m_pkthdr.len = mhlen + len;
977		m->m_pkthdr.rcvif = NULL;
978#ifdef MAC
979		mac_create_fragment(m0, m);
980#endif
981		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
982		mhip->ip_off = htons(mhip->ip_off);
983		mhip->ip_sum = 0;
984		if (sw_csum & CSUM_DELAY_IP)
985			mhip->ip_sum = in_cksum(m, mhlen);
986		*mnext = m;
987		mnext = &m->m_nextpkt;
988	}
989	ipstat.ips_ofragments += nfrags;
990
991	/* set first marker for fragment chain */
992	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
993	m0->m_pkthdr.csum_data = nfrags;
994
995	/*
996	 * Update first fragment by trimming what's been copied out
997	 * and updating header.
998	 */
999	m_adj(m0, hlen + firstlen - ip->ip_len);
1000	m0->m_pkthdr.len = hlen + firstlen;
1001	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
1002	ip->ip_off |= IP_MF;
1003	ip->ip_off = htons(ip->ip_off);
1004	ip->ip_sum = 0;
1005	if (sw_csum & CSUM_DELAY_IP)
1006		ip->ip_sum = in_cksum(m0, hlen);
1007
1008done:
1009	*m_frag = m0;
1010	return error;
1011}
1012
1013void
1014in_delayed_cksum(struct mbuf *m)
1015{
1016	struct ip *ip;
1017	u_short csum, offset;
1018
1019	ip = mtod(m, struct ip *);
1020	offset = ip->ip_hl << 2 ;
1021	csum = in_cksum_skip(m, ip->ip_len, offset);
1022	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1023		csum = 0xffff;
1024	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1025
1026	if (offset + sizeof(u_short) > m->m_len) {
1027		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1028		    m->m_len, offset, ip->ip_p);
1029		/*
1030		 * XXX
1031		 * this shouldn't happen, but if it does, the
1032		 * correct behavior may be to insert the checksum
1033		 * in the existing chain instead of rearranging it.
1034		 */
1035		m = m_pullup(m, offset + sizeof(u_short));
1036	}
1037	*(u_short *)(m->m_data + offset) = csum;
1038}
1039
1040/*
1041 * Insert IP options into preformed packet.
1042 * Adjust IP destination as required for IP source routing,
1043 * as indicated by a non-zero in_addr at the start of the options.
1044 *
1045 * XXX This routine assumes that the packet has no options in place.
1046 */
1047static struct mbuf *
1048ip_insertoptions(m, opt, phlen)
1049	register struct mbuf *m;
1050	struct mbuf *opt;
1051	int *phlen;
1052{
1053	register struct ipoption *p = mtod(opt, struct ipoption *);
1054	struct mbuf *n;
1055	register struct ip *ip = mtod(m, struct ip *);
1056	unsigned optlen;
1057
1058	optlen = opt->m_len - sizeof(p->ipopt_dst);
1059	if (optlen + ip->ip_len > IP_MAXPACKET) {
1060		*phlen = 0;
1061		return (m);		/* XXX should fail */
1062	}
1063	if (p->ipopt_dst.s_addr)
1064		ip->ip_dst = p->ipopt_dst;
1065	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1066		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1067		if (n == NULL) {
1068			*phlen = 0;
1069			return (m);
1070		}
1071		M_MOVE_PKTHDR(n, m);
1072		n->m_pkthdr.rcvif = NULL;
1073#ifdef MAC
1074		mac_create_mbuf_from_mbuf(m, n);
1075#endif
1076		n->m_pkthdr.len += optlen;
1077		m->m_len -= sizeof(struct ip);
1078		m->m_data += sizeof(struct ip);
1079		n->m_next = m;
1080		m = n;
1081		m->m_len = optlen + sizeof(struct ip);
1082		m->m_data += max_linkhdr;
1083		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1084	} else {
1085		m->m_data -= optlen;
1086		m->m_len += optlen;
1087		m->m_pkthdr.len += optlen;
1088		bcopy(ip, mtod(m, void *), sizeof(struct ip));
1089	}
1090	ip = mtod(m, struct ip *);
1091	bcopy(p->ipopt_list, ip + 1, optlen);
1092	*phlen = sizeof(struct ip) + optlen;
1093	ip->ip_v = IPVERSION;
1094	ip->ip_hl = *phlen >> 2;
1095	ip->ip_len += optlen;
1096	return (m);
1097}
1098
1099/*
1100 * Copy options from ip to jp,
1101 * omitting those not copied during fragmentation.
1102 */
1103int
1104ip_optcopy(ip, jp)
1105	struct ip *ip, *jp;
1106{
1107	register u_char *cp, *dp;
1108	int opt, optlen, cnt;
1109
1110	cp = (u_char *)(ip + 1);
1111	dp = (u_char *)(jp + 1);
1112	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1113	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1114		opt = cp[0];
1115		if (opt == IPOPT_EOL)
1116			break;
1117		if (opt == IPOPT_NOP) {
1118			/* Preserve for IP mcast tunnel's LSRR alignment. */
1119			*dp++ = IPOPT_NOP;
1120			optlen = 1;
1121			continue;
1122		}
1123
1124		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1125		    ("ip_optcopy: malformed ipv4 option"));
1126		optlen = cp[IPOPT_OLEN];
1127		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1128		    ("ip_optcopy: malformed ipv4 option"));
1129
1130		/* bogus lengths should have been caught by ip_dooptions */
1131		if (optlen > cnt)
1132			optlen = cnt;
1133		if (IPOPT_COPIED(opt)) {
1134			bcopy(cp, dp, optlen);
1135			dp += optlen;
1136		}
1137	}
1138	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1139		*dp++ = IPOPT_EOL;
1140	return (optlen);
1141}
1142
1143/*
1144 * IP socket option processing.
1145 */
1146int
1147ip_ctloutput(so, sopt)
1148	struct socket *so;
1149	struct sockopt *sopt;
1150{
1151	struct	inpcb *inp = sotoinpcb(so);
1152	int	error, optval;
1153
1154	error = optval = 0;
1155	if (sopt->sopt_level != IPPROTO_IP) {
1156		return (EINVAL);
1157	}
1158
1159	switch (sopt->sopt_dir) {
1160	case SOPT_SET:
1161		switch (sopt->sopt_name) {
1162		case IP_OPTIONS:
1163#ifdef notyet
1164		case IP_RETOPTS:
1165#endif
1166		{
1167			struct mbuf *m;
1168			if (sopt->sopt_valsize > MLEN) {
1169				error = EMSGSIZE;
1170				break;
1171			}
1172			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1173			if (m == NULL) {
1174				error = ENOBUFS;
1175				break;
1176			}
1177			m->m_len = sopt->sopt_valsize;
1178			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1179					    m->m_len);
1180			INP_LOCK(inp);
1181			error = ip_pcbopts(inp, sopt->sopt_name, m);
1182			INP_UNLOCK(inp);
1183			return (error);
1184		}
1185
1186		case IP_TOS:
1187		case IP_TTL:
1188		case IP_RECVOPTS:
1189		case IP_RECVRETOPTS:
1190		case IP_RECVDSTADDR:
1191		case IP_RECVTTL:
1192		case IP_RECVIF:
1193		case IP_FAITH:
1194		case IP_ONESBCAST:
1195			error = sooptcopyin(sopt, &optval, sizeof optval,
1196					    sizeof optval);
1197			if (error)
1198				break;
1199
1200			switch (sopt->sopt_name) {
1201			case IP_TOS:
1202				inp->inp_ip_tos = optval;
1203				break;
1204
1205			case IP_TTL:
1206				inp->inp_ip_ttl = optval;
1207				break;
1208#define	OPTSET(bit) do {						\
1209	INP_LOCK(inp);							\
1210	if (optval)							\
1211		inp->inp_flags |= bit;					\
1212	else								\
1213		inp->inp_flags &= ~bit;					\
1214	INP_UNLOCK(inp);						\
1215} while (0)
1216
1217			case IP_RECVOPTS:
1218				OPTSET(INP_RECVOPTS);
1219				break;
1220
1221			case IP_RECVRETOPTS:
1222				OPTSET(INP_RECVRETOPTS);
1223				break;
1224
1225			case IP_RECVDSTADDR:
1226				OPTSET(INP_RECVDSTADDR);
1227				break;
1228
1229			case IP_RECVTTL:
1230				OPTSET(INP_RECVTTL);
1231				break;
1232
1233			case IP_RECVIF:
1234				OPTSET(INP_RECVIF);
1235				break;
1236
1237			case IP_FAITH:
1238				OPTSET(INP_FAITH);
1239				break;
1240
1241			case IP_ONESBCAST:
1242				OPTSET(INP_ONESBCAST);
1243				break;
1244			}
1245			break;
1246#undef OPTSET
1247
1248		case IP_MULTICAST_IF:
1249		case IP_MULTICAST_VIF:
1250		case IP_MULTICAST_TTL:
1251		case IP_MULTICAST_LOOP:
1252		case IP_ADD_MEMBERSHIP:
1253		case IP_DROP_MEMBERSHIP:
1254			error = ip_setmoptions(inp, sopt);
1255			break;
1256
1257		case IP_PORTRANGE:
1258			error = sooptcopyin(sopt, &optval, sizeof optval,
1259					    sizeof optval);
1260			if (error)
1261				break;
1262
1263			INP_LOCK(inp);
1264			switch (optval) {
1265			case IP_PORTRANGE_DEFAULT:
1266				inp->inp_flags &= ~(INP_LOWPORT);
1267				inp->inp_flags &= ~(INP_HIGHPORT);
1268				break;
1269
1270			case IP_PORTRANGE_HIGH:
1271				inp->inp_flags &= ~(INP_LOWPORT);
1272				inp->inp_flags |= INP_HIGHPORT;
1273				break;
1274
1275			case IP_PORTRANGE_LOW:
1276				inp->inp_flags &= ~(INP_HIGHPORT);
1277				inp->inp_flags |= INP_LOWPORT;
1278				break;
1279
1280			default:
1281				error = EINVAL;
1282				break;
1283			}
1284			INP_UNLOCK(inp);
1285			break;
1286
1287#if defined(IPSEC) || defined(FAST_IPSEC)
1288		case IP_IPSEC_POLICY:
1289		{
1290			caddr_t req;
1291			size_t len = 0;
1292			int priv;
1293			struct mbuf *m;
1294			int optname;
1295
1296			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1297				break;
1298			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1299				break;
1300			priv = (sopt->sopt_td != NULL &&
1301				suser(sopt->sopt_td) != 0) ? 0 : 1;
1302			req = mtod(m, caddr_t);
1303			len = m->m_len;
1304			optname = sopt->sopt_name;
1305			error = ipsec4_set_policy(inp, optname, req, len, priv);
1306			m_freem(m);
1307			break;
1308		}
1309#endif /*IPSEC*/
1310
1311		default:
1312			error = ENOPROTOOPT;
1313			break;
1314		}
1315		break;
1316
1317	case SOPT_GET:
1318		switch (sopt->sopt_name) {
1319		case IP_OPTIONS:
1320		case IP_RETOPTS:
1321			if (inp->inp_options)
1322				error = sooptcopyout(sopt,
1323						     mtod(inp->inp_options,
1324							  char *),
1325						     inp->inp_options->m_len);
1326			else
1327				sopt->sopt_valsize = 0;
1328			break;
1329
1330		case IP_TOS:
1331		case IP_TTL:
1332		case IP_RECVOPTS:
1333		case IP_RECVRETOPTS:
1334		case IP_RECVDSTADDR:
1335		case IP_RECVTTL:
1336		case IP_RECVIF:
1337		case IP_PORTRANGE:
1338		case IP_FAITH:
1339		case IP_ONESBCAST:
1340			switch (sopt->sopt_name) {
1341
1342			case IP_TOS:
1343				optval = inp->inp_ip_tos;
1344				break;
1345
1346			case IP_TTL:
1347				optval = inp->inp_ip_ttl;
1348				break;
1349
1350#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1351
1352			case IP_RECVOPTS:
1353				optval = OPTBIT(INP_RECVOPTS);
1354				break;
1355
1356			case IP_RECVRETOPTS:
1357				optval = OPTBIT(INP_RECVRETOPTS);
1358				break;
1359
1360			case IP_RECVDSTADDR:
1361				optval = OPTBIT(INP_RECVDSTADDR);
1362				break;
1363
1364			case IP_RECVTTL:
1365				optval = OPTBIT(INP_RECVTTL);
1366				break;
1367
1368			case IP_RECVIF:
1369				optval = OPTBIT(INP_RECVIF);
1370				break;
1371
1372			case IP_PORTRANGE:
1373				if (inp->inp_flags & INP_HIGHPORT)
1374					optval = IP_PORTRANGE_HIGH;
1375				else if (inp->inp_flags & INP_LOWPORT)
1376					optval = IP_PORTRANGE_LOW;
1377				else
1378					optval = 0;
1379				break;
1380
1381			case IP_FAITH:
1382				optval = OPTBIT(INP_FAITH);
1383				break;
1384
1385			case IP_ONESBCAST:
1386				optval = OPTBIT(INP_ONESBCAST);
1387				break;
1388			}
1389			error = sooptcopyout(sopt, &optval, sizeof optval);
1390			break;
1391
1392		case IP_MULTICAST_IF:
1393		case IP_MULTICAST_VIF:
1394		case IP_MULTICAST_TTL:
1395		case IP_MULTICAST_LOOP:
1396		case IP_ADD_MEMBERSHIP:
1397		case IP_DROP_MEMBERSHIP:
1398			error = ip_getmoptions(inp, sopt);
1399			break;
1400
1401#if defined(IPSEC) || defined(FAST_IPSEC)
1402		case IP_IPSEC_POLICY:
1403		{
1404			struct mbuf *m = NULL;
1405			caddr_t req = NULL;
1406			size_t len = 0;
1407
1408			if (m != 0) {
1409				req = mtod(m, caddr_t);
1410				len = m->m_len;
1411			}
1412			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1413			if (error == 0)
1414				error = soopt_mcopyout(sopt, m); /* XXX */
1415			if (error == 0)
1416				m_freem(m);
1417			break;
1418		}
1419#endif /*IPSEC*/
1420
1421		default:
1422			error = ENOPROTOOPT;
1423			break;
1424		}
1425		break;
1426	}
1427	return (error);
1428}
1429
1430/*
1431 * Set up IP options in pcb for insertion in output packets.
1432 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1433 * with destination address if source routed.
1434 */
1435static int
1436ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
1437{
1438	register int cnt, optlen;
1439	register u_char *cp;
1440	struct mbuf **pcbopt;
1441	u_char opt;
1442
1443	INP_LOCK_ASSERT(inp);
1444
1445	pcbopt = &inp->inp_options;
1446
1447	/* turn off any old options */
1448	if (*pcbopt)
1449		(void)m_free(*pcbopt);
1450	*pcbopt = 0;
1451	if (m == NULL || m->m_len == 0) {
1452		/*
1453		 * Only turning off any previous options.
1454		 */
1455		if (m != NULL)
1456			(void)m_free(m);
1457		return (0);
1458	}
1459
1460	if (m->m_len % sizeof(int32_t))
1461		goto bad;
1462	/*
1463	 * IP first-hop destination address will be stored before
1464	 * actual options; move other options back
1465	 * and clear it when none present.
1466	 */
1467	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1468		goto bad;
1469	cnt = m->m_len;
1470	m->m_len += sizeof(struct in_addr);
1471	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1472	bcopy(mtod(m, void *), cp, (unsigned)cnt);
1473	bzero(mtod(m, void *), sizeof(struct in_addr));
1474
1475	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1476		opt = cp[IPOPT_OPTVAL];
1477		if (opt == IPOPT_EOL)
1478			break;
1479		if (opt == IPOPT_NOP)
1480			optlen = 1;
1481		else {
1482			if (cnt < IPOPT_OLEN + sizeof(*cp))
1483				goto bad;
1484			optlen = cp[IPOPT_OLEN];
1485			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1486				goto bad;
1487		}
1488		switch (opt) {
1489
1490		default:
1491			break;
1492
1493		case IPOPT_LSRR:
1494		case IPOPT_SSRR:
1495			/*
1496			 * user process specifies route as:
1497			 *	->A->B->C->D
1498			 * D must be our final destination (but we can't
1499			 * check that since we may not have connected yet).
1500			 * A is first hop destination, which doesn't appear in
1501			 * actual IP option, but is stored before the options.
1502			 */
1503			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1504				goto bad;
1505			m->m_len -= sizeof(struct in_addr);
1506			cnt -= sizeof(struct in_addr);
1507			optlen -= sizeof(struct in_addr);
1508			cp[IPOPT_OLEN] = optlen;
1509			/*
1510			 * Move first hop before start of options.
1511			 */
1512			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1513			    sizeof(struct in_addr));
1514			/*
1515			 * Then copy rest of options back
1516			 * to close up the deleted entry.
1517			 */
1518			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
1519			    &cp[IPOPT_OFFSET+1],
1520			    (unsigned)cnt - (IPOPT_MINOFF - 1));
1521			break;
1522		}
1523	}
1524	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1525		goto bad;
1526	*pcbopt = m;
1527	return (0);
1528
1529bad:
1530	(void)m_free(m);
1531	return (EINVAL);
1532}
1533
1534/*
1535 * XXX
1536 * The whole multicast option thing needs to be re-thought.
1537 * Several of these options are equally applicable to non-multicast
1538 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1539 * standard option (IP_TTL).
1540 */
1541
1542/*
1543 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1544 */
1545static struct ifnet *
1546ip_multicast_if(a, ifindexp)
1547	struct in_addr *a;
1548	int *ifindexp;
1549{
1550	int ifindex;
1551	struct ifnet *ifp;
1552
1553	if (ifindexp)
1554		*ifindexp = 0;
1555	if (ntohl(a->s_addr) >> 24 == 0) {
1556		ifindex = ntohl(a->s_addr) & 0xffffff;
1557		if (ifindex < 0 || if_index < ifindex)
1558			return NULL;
1559		ifp = ifnet_byindex(ifindex);
1560		if (ifindexp)
1561			*ifindexp = ifindex;
1562	} else {
1563		INADDR_TO_IFP(*a, ifp);
1564	}
1565	return ifp;
1566}
1567
1568/*
1569 * Set the IP multicast options in response to user setsockopt().
1570 */
1571static int
1572ip_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1573{
1574	int error = 0;
1575	int i;
1576	struct in_addr addr;
1577	struct ip_mreq mreq;
1578	struct ifnet *ifp;
1579	struct ip_moptions *imo;
1580	struct route ro;
1581	struct sockaddr_in *dst;
1582	int ifindex;
1583	int s;
1584
1585	imo = inp->inp_moptions;
1586	if (imo == NULL) {
1587		/*
1588		 * No multicast option buffer attached to the pcb;
1589		 * allocate one and initialize to default values.
1590		 */
1591		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1592		    M_WAITOK);
1593
1594		if (imo == NULL)
1595			return (ENOBUFS);
1596		inp->inp_moptions = imo;
1597		imo->imo_multicast_ifp = NULL;
1598		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1599		imo->imo_multicast_vif = -1;
1600		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1601		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1602		imo->imo_num_memberships = 0;
1603	}
1604
1605	switch (sopt->sopt_name) {
1606	/* store an index number for the vif you wanna use in the send */
1607	case IP_MULTICAST_VIF:
1608		if (legal_vif_num == 0) {
1609			error = EOPNOTSUPP;
1610			break;
1611		}
1612		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1613		if (error)
1614			break;
1615		if (!legal_vif_num(i) && (i != -1)) {
1616			error = EINVAL;
1617			break;
1618		}
1619		imo->imo_multicast_vif = i;
1620		break;
1621
1622	case IP_MULTICAST_IF:
1623		/*
1624		 * Select the interface for outgoing multicast packets.
1625		 */
1626		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1627		if (error)
1628			break;
1629		/*
1630		 * INADDR_ANY is used to remove a previous selection.
1631		 * When no interface is selected, a default one is
1632		 * chosen every time a multicast packet is sent.
1633		 */
1634		if (addr.s_addr == INADDR_ANY) {
1635			imo->imo_multicast_ifp = NULL;
1636			break;
1637		}
1638		/*
1639		 * The selected interface is identified by its local
1640		 * IP address.  Find the interface and confirm that
1641		 * it supports multicasting.
1642		 */
1643		s = splimp();
1644		ifp = ip_multicast_if(&addr, &ifindex);
1645		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1646			splx(s);
1647			error = EADDRNOTAVAIL;
1648			break;
1649		}
1650		imo->imo_multicast_ifp = ifp;
1651		if (ifindex)
1652			imo->imo_multicast_addr = addr;
1653		else
1654			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1655		splx(s);
1656		break;
1657
1658	case IP_MULTICAST_TTL:
1659		/*
1660		 * Set the IP time-to-live for outgoing multicast packets.
1661		 * The original multicast API required a char argument,
1662		 * which is inconsistent with the rest of the socket API.
1663		 * We allow either a char or an int.
1664		 */
1665		if (sopt->sopt_valsize == 1) {
1666			u_char ttl;
1667			error = sooptcopyin(sopt, &ttl, 1, 1);
1668			if (error)
1669				break;
1670			imo->imo_multicast_ttl = ttl;
1671		} else {
1672			u_int ttl;
1673			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1674					    sizeof ttl);
1675			if (error)
1676				break;
1677			if (ttl > 255)
1678				error = EINVAL;
1679			else
1680				imo->imo_multicast_ttl = ttl;
1681		}
1682		break;
1683
1684	case IP_MULTICAST_LOOP:
1685		/*
1686		 * Set the loopback flag for outgoing multicast packets.
1687		 * Must be zero or one.  The original multicast API required a
1688		 * char argument, which is inconsistent with the rest
1689		 * of the socket API.  We allow either a char or an int.
1690		 */
1691		if (sopt->sopt_valsize == 1) {
1692			u_char loop;
1693			error = sooptcopyin(sopt, &loop, 1, 1);
1694			if (error)
1695				break;
1696			imo->imo_multicast_loop = !!loop;
1697		} else {
1698			u_int loop;
1699			error = sooptcopyin(sopt, &loop, sizeof loop,
1700					    sizeof loop);
1701			if (error)
1702				break;
1703			imo->imo_multicast_loop = !!loop;
1704		}
1705		break;
1706
1707	case IP_ADD_MEMBERSHIP:
1708		/*
1709		 * Add a multicast group membership.
1710		 * Group must be a valid IP multicast address.
1711		 */
1712		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1713		if (error)
1714			break;
1715
1716		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1717			error = EINVAL;
1718			break;
1719		}
1720		s = splimp();
1721		/*
1722		 * If no interface address was provided, use the interface of
1723		 * the route to the given multicast address.
1724		 */
1725		if (mreq.imr_interface.s_addr == INADDR_ANY) {
1726			bzero((caddr_t)&ro, sizeof(ro));
1727			dst = (struct sockaddr_in *)&ro.ro_dst;
1728			dst->sin_len = sizeof(*dst);
1729			dst->sin_family = AF_INET;
1730			dst->sin_addr = mreq.imr_multiaddr;
1731			rtalloc_ign(&ro, RTF_CLONING);
1732			if (ro.ro_rt == NULL) {
1733				error = EADDRNOTAVAIL;
1734				splx(s);
1735				break;
1736			}
1737			ifp = ro.ro_rt->rt_ifp;
1738			RTFREE(ro.ro_rt);
1739		}
1740		else {
1741			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1742		}
1743
1744		/*
1745		 * See if we found an interface, and confirm that it
1746		 * supports multicast.
1747		 */
1748		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1749			error = EADDRNOTAVAIL;
1750			splx(s);
1751			break;
1752		}
1753		/*
1754		 * See if the membership already exists or if all the
1755		 * membership slots are full.
1756		 */
1757		for (i = 0; i < imo->imo_num_memberships; ++i) {
1758			if (imo->imo_membership[i]->inm_ifp == ifp &&
1759			    imo->imo_membership[i]->inm_addr.s_addr
1760						== mreq.imr_multiaddr.s_addr)
1761				break;
1762		}
1763		if (i < imo->imo_num_memberships) {
1764			error = EADDRINUSE;
1765			splx(s);
1766			break;
1767		}
1768		if (i == IP_MAX_MEMBERSHIPS) {
1769			error = ETOOMANYREFS;
1770			splx(s);
1771			break;
1772		}
1773		/*
1774		 * Everything looks good; add a new record to the multicast
1775		 * address list for the given interface.
1776		 */
1777		if ((imo->imo_membership[i] =
1778		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1779			error = ENOBUFS;
1780			splx(s);
1781			break;
1782		}
1783		++imo->imo_num_memberships;
1784		splx(s);
1785		break;
1786
1787	case IP_DROP_MEMBERSHIP:
1788		/*
1789		 * Drop a multicast group membership.
1790		 * Group must be a valid IP multicast address.
1791		 */
1792		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1793		if (error)
1794			break;
1795
1796		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1797			error = EINVAL;
1798			break;
1799		}
1800
1801		s = splimp();
1802		/*
1803		 * If an interface address was specified, get a pointer
1804		 * to its ifnet structure.
1805		 */
1806		if (mreq.imr_interface.s_addr == INADDR_ANY)
1807			ifp = NULL;
1808		else {
1809			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1810			if (ifp == NULL) {
1811				error = EADDRNOTAVAIL;
1812				splx(s);
1813				break;
1814			}
1815		}
1816		/*
1817		 * Find the membership in the membership array.
1818		 */
1819		for (i = 0; i < imo->imo_num_memberships; ++i) {
1820			if ((ifp == NULL ||
1821			     imo->imo_membership[i]->inm_ifp == ifp) &&
1822			     imo->imo_membership[i]->inm_addr.s_addr ==
1823			     mreq.imr_multiaddr.s_addr)
1824				break;
1825		}
1826		if (i == imo->imo_num_memberships) {
1827			error = EADDRNOTAVAIL;
1828			splx(s);
1829			break;
1830		}
1831		/*
1832		 * Give up the multicast address record to which the
1833		 * membership points.
1834		 */
1835		in_delmulti(imo->imo_membership[i]);
1836		/*
1837		 * Remove the gap in the membership array.
1838		 */
1839		for (++i; i < imo->imo_num_memberships; ++i)
1840			imo->imo_membership[i-1] = imo->imo_membership[i];
1841		--imo->imo_num_memberships;
1842		splx(s);
1843		break;
1844
1845	default:
1846		error = EOPNOTSUPP;
1847		break;
1848	}
1849
1850	/*
1851	 * If all options have default values, no need to keep the mbuf.
1852	 */
1853	if (imo->imo_multicast_ifp == NULL &&
1854	    imo->imo_multicast_vif == -1 &&
1855	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1856	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1857	    imo->imo_num_memberships == 0) {
1858		free(inp->inp_moptions, M_IPMOPTS);
1859		inp->inp_moptions = NULL;
1860	}
1861
1862	return (error);
1863}
1864
1865/*
1866 * Return the IP multicast options in response to user getsockopt().
1867 */
1868static int
1869ip_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1870{
1871	struct ip_moptions *imo;
1872	struct in_addr addr;
1873	struct in_ifaddr *ia;
1874	int error, optval;
1875	u_char coptval;
1876
1877	INP_LOCK(inp);
1878	imo = inp->inp_moptions;
1879
1880	error = 0;
1881	switch (sopt->sopt_name) {
1882	case IP_MULTICAST_VIF:
1883		if (imo != NULL)
1884			optval = imo->imo_multicast_vif;
1885		else
1886			optval = -1;
1887		INP_UNLOCK(inp);
1888		error = sooptcopyout(sopt, &optval, sizeof optval);
1889		break;
1890
1891	case IP_MULTICAST_IF:
1892		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1893			addr.s_addr = INADDR_ANY;
1894		else if (imo->imo_multicast_addr.s_addr) {
1895			/* return the value user has set */
1896			addr = imo->imo_multicast_addr;
1897		} else {
1898			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1899			addr.s_addr = (ia == NULL) ? INADDR_ANY
1900				: IA_SIN(ia)->sin_addr.s_addr;
1901		}
1902		INP_UNLOCK(inp);
1903		error = sooptcopyout(sopt, &addr, sizeof addr);
1904		break;
1905
1906	case IP_MULTICAST_TTL:
1907		if (imo == 0)
1908			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1909		else
1910			optval = coptval = imo->imo_multicast_ttl;
1911		INP_UNLOCK(inp);
1912		if (sopt->sopt_valsize == 1)
1913			error = sooptcopyout(sopt, &coptval, 1);
1914		else
1915			error = sooptcopyout(sopt, &optval, sizeof optval);
1916		break;
1917
1918	case IP_MULTICAST_LOOP:
1919		if (imo == 0)
1920			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1921		else
1922			optval = coptval = imo->imo_multicast_loop;
1923		INP_UNLOCK(inp);
1924		if (sopt->sopt_valsize == 1)
1925			error = sooptcopyout(sopt, &coptval, 1);
1926		else
1927			error = sooptcopyout(sopt, &optval, sizeof optval);
1928		break;
1929
1930	default:
1931		INP_UNLOCK(inp);
1932		error = ENOPROTOOPT;
1933		break;
1934	}
1935	INP_UNLOCK_ASSERT(inp);
1936
1937	return (error);
1938}
1939
1940/*
1941 * Discard the IP multicast options.
1942 */
1943void
1944ip_freemoptions(imo)
1945	register struct ip_moptions *imo;
1946{
1947	register int i;
1948
1949	if (imo != NULL) {
1950		for (i = 0; i < imo->imo_num_memberships; ++i)
1951			in_delmulti(imo->imo_membership[i]);
1952		free(imo, M_IPMOPTS);
1953	}
1954}
1955
1956/*
1957 * Routine called from ip_output() to loop back a copy of an IP multicast
1958 * packet to the input queue of a specified interface.  Note that this
1959 * calls the output routine of the loopback "driver", but with an interface
1960 * pointer that might NOT be a loopback interface -- evil, but easier than
1961 * replicating that code here.
1962 */
1963static void
1964ip_mloopback(ifp, m, dst, hlen)
1965	struct ifnet *ifp;
1966	register struct mbuf *m;
1967	register struct sockaddr_in *dst;
1968	int hlen;
1969{
1970	register struct ip *ip;
1971	struct mbuf *copym;
1972
1973	copym = m_copy(m, 0, M_COPYALL);
1974	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1975		copym = m_pullup(copym, hlen);
1976	if (copym != NULL) {
1977		/* If needed, compute the checksum and mark it as valid. */
1978		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1979			in_delayed_cksum(copym);
1980			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1981			copym->m_pkthdr.csum_flags |=
1982			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1983			copym->m_pkthdr.csum_data = 0xffff;
1984		}
1985		/*
1986		 * We don't bother to fragment if the IP length is greater
1987		 * than the interface's MTU.  Can this possibly matter?
1988		 */
1989		ip = mtod(copym, struct ip *);
1990		ip->ip_len = htons(ip->ip_len);
1991		ip->ip_off = htons(ip->ip_off);
1992		ip->ip_sum = 0;
1993		ip->ip_sum = in_cksum(copym, hlen);
1994		/*
1995		 * NB:
1996		 * It's not clear whether there are any lingering
1997		 * reentrancy problems in other areas which might
1998		 * be exposed by using ip_input directly (in
1999		 * particular, everything which modifies the packet
2000		 * in-place).  Yet another option is using the
2001		 * protosw directly to deliver the looped back
2002		 * packet.  For the moment, we'll err on the side
2003		 * of safety by using if_simloop().
2004		 */
2005#if 1 /* XXX */
2006		if (dst->sin_family != AF_INET) {
2007			printf("ip_mloopback: bad address family %d\n",
2008						dst->sin_family);
2009			dst->sin_family = AF_INET;
2010		}
2011#endif
2012
2013#ifdef notdef
2014		copym->m_pkthdr.rcvif = ifp;
2015		ip_input(copym);
2016#else
2017		if_simloop(ifp, copym, dst->sin_family, 0);
2018#endif
2019	}
2020}
2021