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