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