ip_output.c revision 103478
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 103478 2002-09-17 11:13:04Z 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	len = 0;
145	args.eh = NULL;
146	args.rule = NULL;
147	args.next_hop = NULL;
148	args.divert_rule = 0;			/* divert cookie */
149
150	/* Grab info from MT_TAG mbufs prepended to the chain. */
151	for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) {
152		switch(m0->m_tag_id) {
153		default:
154			printf("ip_output: unrecognised MT_TAG tag %d\n",
155			    m0->m_tag_id);
156			break;
157
158		case PACKET_TAG_DUMMYNET:
159			/*
160			 * the packet was already tagged, so part of the
161			 * processing was already done, and we need to go down.
162			 * Get parameters from the header.
163			 */
164			args.rule = ((struct dn_pkt *)m0)->rule;
165			opt = NULL ;
166			ro = & ( ((struct dn_pkt *)m0)->ro ) ;
167			imo = NULL ;
168			dst = ((struct dn_pkt *)m0)->dn_dst ;
169			ifp = ((struct dn_pkt *)m0)->ifp ;
170			flags = ((struct dn_pkt *)m0)->flags ;
171			break;
172
173		case PACKET_TAG_DIVERT:
174			args.divert_rule = (intptr_t)m0->m_data & 0xffff;
175			break;
176
177		case PACKET_TAG_IPFORWARD:
178			args.next_hop = (struct sockaddr_in *)m0->m_data;
179			break;
180		}
181	}
182	m = m0;
183
184	KASSERT(!m || (m->m_flags & M_PKTHDR) != 0, ("ip_output: no HDR"));
185
186	KASSERT(ro != NULL, ("ip_output: no route, proto %d",
187	    mtod(m, struct ip *)->ip_p));
188
189#ifdef IPSEC
190	so = ipsec_getsocket(m);
191	(void)ipsec_setsocket(m, NULL);
192#endif
193	if (args.rule != NULL) {	/* dummynet already saw us */
194		ip = mtod(m, struct ip *);
195		hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
196		if (ro->ro_rt)
197			ia = ifatoia(ro->ro_rt->rt_ifa);
198		goto sendit;
199	}
200
201	if (opt) {
202		m = ip_insertoptions(m, opt, &len);
203		if (len >= sizeof(struct ip))
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		return (m);		/* XXX should fail */
1141	if (p->ipopt_dst.s_addr)
1142		ip->ip_dst = p->ipopt_dst;
1143	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1144		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1145		if (n == 0)
1146			return (m);
1147		n->m_pkthdr.rcvif = (struct ifnet *)0;
1148#ifdef MAC
1149		mac_create_mbuf_from_mbuf(m, n);
1150#endif
1151		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1152		m->m_len -= sizeof(struct ip);
1153		m->m_data += sizeof(struct ip);
1154		n->m_next = m;
1155		m = n;
1156		m->m_len = optlen + sizeof(struct ip);
1157		m->m_data += max_linkhdr;
1158		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1159	} else {
1160		m->m_data -= optlen;
1161		m->m_len += optlen;
1162		m->m_pkthdr.len += optlen;
1163		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1164	}
1165	ip = mtod(m, struct ip *);
1166	bcopy(p->ipopt_list, ip + 1, optlen);
1167	*phlen = sizeof(struct ip) + optlen;
1168	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1169	ip->ip_len += optlen;
1170	return (m);
1171}
1172
1173/*
1174 * Copy options from ip to jp,
1175 * omitting those not copied during fragmentation.
1176 */
1177int
1178ip_optcopy(ip, jp)
1179	struct ip *ip, *jp;
1180{
1181	register u_char *cp, *dp;
1182	int opt, optlen, cnt;
1183
1184	cp = (u_char *)(ip + 1);
1185	dp = (u_char *)(jp + 1);
1186	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1187	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1188		opt = cp[0];
1189		if (opt == IPOPT_EOL)
1190			break;
1191		if (opt == IPOPT_NOP) {
1192			/* Preserve for IP mcast tunnel's LSRR alignment. */
1193			*dp++ = IPOPT_NOP;
1194			optlen = 1;
1195			continue;
1196		}
1197
1198		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1199		    ("ip_optcopy: malformed ipv4 option"));
1200		optlen = cp[IPOPT_OLEN];
1201		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1202		    ("ip_optcopy: malformed ipv4 option"));
1203
1204		/* bogus lengths should have been caught by ip_dooptions */
1205		if (optlen > cnt)
1206			optlen = cnt;
1207		if (IPOPT_COPIED(opt)) {
1208			bcopy(cp, dp, optlen);
1209			dp += optlen;
1210		}
1211	}
1212	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1213		*dp++ = IPOPT_EOL;
1214	return (optlen);
1215}
1216
1217/*
1218 * IP socket option processing.
1219 */
1220int
1221ip_ctloutput(so, sopt)
1222	struct socket *so;
1223	struct sockopt *sopt;
1224{
1225	struct	inpcb *inp = sotoinpcb(so);
1226	int	error, optval;
1227
1228	error = optval = 0;
1229	if (sopt->sopt_level != IPPROTO_IP) {
1230		return (EINVAL);
1231	}
1232
1233	switch (sopt->sopt_dir) {
1234	case SOPT_SET:
1235		switch (sopt->sopt_name) {
1236		case IP_OPTIONS:
1237#ifdef notyet
1238		case IP_RETOPTS:
1239#endif
1240		{
1241			struct mbuf *m;
1242			if (sopt->sopt_valsize > MLEN) {
1243				error = EMSGSIZE;
1244				break;
1245			}
1246			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1247			if (m == 0) {
1248				error = ENOBUFS;
1249				break;
1250			}
1251			m->m_len = sopt->sopt_valsize;
1252			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1253					    m->m_len);
1254
1255			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1256					   m));
1257		}
1258
1259		case IP_TOS:
1260		case IP_TTL:
1261		case IP_RECVOPTS:
1262		case IP_RECVRETOPTS:
1263		case IP_RECVDSTADDR:
1264		case IP_RECVIF:
1265		case IP_FAITH:
1266			error = sooptcopyin(sopt, &optval, sizeof optval,
1267					    sizeof optval);
1268			if (error)
1269				break;
1270
1271			switch (sopt->sopt_name) {
1272			case IP_TOS:
1273				inp->inp_ip_tos = optval;
1274				break;
1275
1276			case IP_TTL:
1277				inp->inp_ip_ttl = optval;
1278				break;
1279#define	OPTSET(bit) \
1280	if (optval) \
1281		inp->inp_flags |= bit; \
1282	else \
1283		inp->inp_flags &= ~bit;
1284
1285			case IP_RECVOPTS:
1286				OPTSET(INP_RECVOPTS);
1287				break;
1288
1289			case IP_RECVRETOPTS:
1290				OPTSET(INP_RECVRETOPTS);
1291				break;
1292
1293			case IP_RECVDSTADDR:
1294				OPTSET(INP_RECVDSTADDR);
1295				break;
1296
1297			case IP_RECVIF:
1298				OPTSET(INP_RECVIF);
1299				break;
1300
1301			case IP_FAITH:
1302				OPTSET(INP_FAITH);
1303				break;
1304			}
1305			break;
1306#undef OPTSET
1307
1308		case IP_MULTICAST_IF:
1309		case IP_MULTICAST_VIF:
1310		case IP_MULTICAST_TTL:
1311		case IP_MULTICAST_LOOP:
1312		case IP_ADD_MEMBERSHIP:
1313		case IP_DROP_MEMBERSHIP:
1314			error = ip_setmoptions(sopt, &inp->inp_moptions);
1315			break;
1316
1317		case IP_PORTRANGE:
1318			error = sooptcopyin(sopt, &optval, sizeof optval,
1319					    sizeof optval);
1320			if (error)
1321				break;
1322
1323			switch (optval) {
1324			case IP_PORTRANGE_DEFAULT:
1325				inp->inp_flags &= ~(INP_LOWPORT);
1326				inp->inp_flags &= ~(INP_HIGHPORT);
1327				break;
1328
1329			case IP_PORTRANGE_HIGH:
1330				inp->inp_flags &= ~(INP_LOWPORT);
1331				inp->inp_flags |= INP_HIGHPORT;
1332				break;
1333
1334			case IP_PORTRANGE_LOW:
1335				inp->inp_flags &= ~(INP_HIGHPORT);
1336				inp->inp_flags |= INP_LOWPORT;
1337				break;
1338
1339			default:
1340				error = EINVAL;
1341				break;
1342			}
1343			break;
1344
1345#ifdef IPSEC
1346		case IP_IPSEC_POLICY:
1347		{
1348			caddr_t req;
1349			size_t len = 0;
1350			int priv;
1351			struct mbuf *m;
1352			int optname;
1353
1354			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1355				break;
1356			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1357				break;
1358			priv = (sopt->sopt_td != NULL &&
1359				suser(sopt->sopt_td) != 0) ? 0 : 1;
1360			req = mtod(m, caddr_t);
1361			len = m->m_len;
1362			optname = sopt->sopt_name;
1363			error = ipsec4_set_policy(inp, optname, req, len, priv);
1364			m_freem(m);
1365			break;
1366		}
1367#endif /*IPSEC*/
1368
1369		default:
1370			error = ENOPROTOOPT;
1371			break;
1372		}
1373		break;
1374
1375	case SOPT_GET:
1376		switch (sopt->sopt_name) {
1377		case IP_OPTIONS:
1378		case IP_RETOPTS:
1379			if (inp->inp_options)
1380				error = sooptcopyout(sopt,
1381						     mtod(inp->inp_options,
1382							  char *),
1383						     inp->inp_options->m_len);
1384			else
1385				sopt->sopt_valsize = 0;
1386			break;
1387
1388		case IP_TOS:
1389		case IP_TTL:
1390		case IP_RECVOPTS:
1391		case IP_RECVRETOPTS:
1392		case IP_RECVDSTADDR:
1393		case IP_RECVIF:
1394		case IP_PORTRANGE:
1395		case IP_FAITH:
1396			switch (sopt->sopt_name) {
1397
1398			case IP_TOS:
1399				optval = inp->inp_ip_tos;
1400				break;
1401
1402			case IP_TTL:
1403				optval = inp->inp_ip_ttl;
1404				break;
1405
1406#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1407
1408			case IP_RECVOPTS:
1409				optval = OPTBIT(INP_RECVOPTS);
1410				break;
1411
1412			case IP_RECVRETOPTS:
1413				optval = OPTBIT(INP_RECVRETOPTS);
1414				break;
1415
1416			case IP_RECVDSTADDR:
1417				optval = OPTBIT(INP_RECVDSTADDR);
1418				break;
1419
1420			case IP_RECVIF:
1421				optval = OPTBIT(INP_RECVIF);
1422				break;
1423
1424			case IP_PORTRANGE:
1425				if (inp->inp_flags & INP_HIGHPORT)
1426					optval = IP_PORTRANGE_HIGH;
1427				else if (inp->inp_flags & INP_LOWPORT)
1428					optval = IP_PORTRANGE_LOW;
1429				else
1430					optval = 0;
1431				break;
1432
1433			case IP_FAITH:
1434				optval = OPTBIT(INP_FAITH);
1435				break;
1436			}
1437			error = sooptcopyout(sopt, &optval, sizeof optval);
1438			break;
1439
1440		case IP_MULTICAST_IF:
1441		case IP_MULTICAST_VIF:
1442		case IP_MULTICAST_TTL:
1443		case IP_MULTICAST_LOOP:
1444		case IP_ADD_MEMBERSHIP:
1445		case IP_DROP_MEMBERSHIP:
1446			error = ip_getmoptions(sopt, inp->inp_moptions);
1447			break;
1448
1449#ifdef IPSEC
1450		case IP_IPSEC_POLICY:
1451		{
1452			struct mbuf *m = NULL;
1453			caddr_t req = NULL;
1454			size_t len = 0;
1455
1456			if (m != 0) {
1457				req = mtod(m, caddr_t);
1458				len = m->m_len;
1459			}
1460			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1461			if (error == 0)
1462				error = soopt_mcopyout(sopt, m); /* XXX */
1463			if (error == 0)
1464				m_freem(m);
1465			break;
1466		}
1467#endif /*IPSEC*/
1468
1469		default:
1470			error = ENOPROTOOPT;
1471			break;
1472		}
1473		break;
1474	}
1475	return (error);
1476}
1477
1478/*
1479 * Set up IP options in pcb for insertion in output packets.
1480 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1481 * with destination address if source routed.
1482 */
1483static int
1484ip_pcbopts(optname, pcbopt, m)
1485	int optname;
1486	struct mbuf **pcbopt;
1487	register struct mbuf *m;
1488{
1489	register int cnt, optlen;
1490	register u_char *cp;
1491	u_char opt;
1492
1493	/* turn off any old options */
1494	if (*pcbopt)
1495		(void)m_free(*pcbopt);
1496	*pcbopt = 0;
1497	if (m == (struct mbuf *)0 || m->m_len == 0) {
1498		/*
1499		 * Only turning off any previous options.
1500		 */
1501		if (m)
1502			(void)m_free(m);
1503		return (0);
1504	}
1505
1506	if (m->m_len % sizeof(int32_t))
1507		goto bad;
1508	/*
1509	 * IP first-hop destination address will be stored before
1510	 * actual options; move other options back
1511	 * and clear it when none present.
1512	 */
1513	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1514		goto bad;
1515	cnt = m->m_len;
1516	m->m_len += sizeof(struct in_addr);
1517	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1518	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1519	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1520
1521	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1522		opt = cp[IPOPT_OPTVAL];
1523		if (opt == IPOPT_EOL)
1524			break;
1525		if (opt == IPOPT_NOP)
1526			optlen = 1;
1527		else {
1528			if (cnt < IPOPT_OLEN + sizeof(*cp))
1529				goto bad;
1530			optlen = cp[IPOPT_OLEN];
1531			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1532				goto bad;
1533		}
1534		switch (opt) {
1535
1536		default:
1537			break;
1538
1539		case IPOPT_LSRR:
1540		case IPOPT_SSRR:
1541			/*
1542			 * user process specifies route as:
1543			 *	->A->B->C->D
1544			 * D must be our final destination (but we can't
1545			 * check that since we may not have connected yet).
1546			 * A is first hop destination, which doesn't appear in
1547			 * actual IP option, but is stored before the options.
1548			 */
1549			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1550				goto bad;
1551			m->m_len -= sizeof(struct in_addr);
1552			cnt -= sizeof(struct in_addr);
1553			optlen -= sizeof(struct in_addr);
1554			cp[IPOPT_OLEN] = optlen;
1555			/*
1556			 * Move first hop before start of options.
1557			 */
1558			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1559			    sizeof(struct in_addr));
1560			/*
1561			 * Then copy rest of options back
1562			 * to close up the deleted entry.
1563			 */
1564			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1565			    sizeof(struct in_addr)),
1566			    (caddr_t)&cp[IPOPT_OFFSET+1],
1567			    (unsigned)cnt + sizeof(struct in_addr));
1568			break;
1569		}
1570	}
1571	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1572		goto bad;
1573	*pcbopt = m;
1574	return (0);
1575
1576bad:
1577	(void)m_free(m);
1578	return (EINVAL);
1579}
1580
1581/*
1582 * XXX
1583 * The whole multicast option thing needs to be re-thought.
1584 * Several of these options are equally applicable to non-multicast
1585 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1586 * standard option (IP_TTL).
1587 */
1588
1589/*
1590 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1591 */
1592static struct ifnet *
1593ip_multicast_if(a, ifindexp)
1594	struct in_addr *a;
1595	int *ifindexp;
1596{
1597	int ifindex;
1598	struct ifnet *ifp;
1599
1600	if (ifindexp)
1601		*ifindexp = 0;
1602	if (ntohl(a->s_addr) >> 24 == 0) {
1603		ifindex = ntohl(a->s_addr) & 0xffffff;
1604		if (ifindex < 0 || if_index < ifindex)
1605			return NULL;
1606		ifp = ifnet_byindex(ifindex);
1607		if (ifindexp)
1608			*ifindexp = ifindex;
1609	} else {
1610		INADDR_TO_IFP(*a, ifp);
1611	}
1612	return ifp;
1613}
1614
1615/*
1616 * Set the IP multicast options in response to user setsockopt().
1617 */
1618static int
1619ip_setmoptions(sopt, imop)
1620	struct sockopt *sopt;
1621	struct ip_moptions **imop;
1622{
1623	int error = 0;
1624	int i;
1625	struct in_addr addr;
1626	struct ip_mreq mreq;
1627	struct ifnet *ifp;
1628	struct ip_moptions *imo = *imop;
1629	struct route ro;
1630	struct sockaddr_in *dst;
1631	int ifindex;
1632	int s;
1633
1634	if (imo == NULL) {
1635		/*
1636		 * No multicast option buffer attached to the pcb;
1637		 * allocate one and initialize to default values.
1638		 */
1639		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1640		    M_WAITOK);
1641
1642		if (imo == NULL)
1643			return (ENOBUFS);
1644		*imop = imo;
1645		imo->imo_multicast_ifp = NULL;
1646		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1647		imo->imo_multicast_vif = -1;
1648		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1649		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1650		imo->imo_num_memberships = 0;
1651	}
1652
1653	switch (sopt->sopt_name) {
1654	/* store an index number for the vif you wanna use in the send */
1655	case IP_MULTICAST_VIF:
1656		if (legal_vif_num == 0) {
1657			error = EOPNOTSUPP;
1658			break;
1659		}
1660		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1661		if (error)
1662			break;
1663		if (!legal_vif_num(i) && (i != -1)) {
1664			error = EINVAL;
1665			break;
1666		}
1667		imo->imo_multicast_vif = i;
1668		break;
1669
1670	case IP_MULTICAST_IF:
1671		/*
1672		 * Select the interface for outgoing multicast packets.
1673		 */
1674		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1675		if (error)
1676			break;
1677		/*
1678		 * INADDR_ANY is used to remove a previous selection.
1679		 * When no interface is selected, a default one is
1680		 * chosen every time a multicast packet is sent.
1681		 */
1682		if (addr.s_addr == INADDR_ANY) {
1683			imo->imo_multicast_ifp = NULL;
1684			break;
1685		}
1686		/*
1687		 * The selected interface is identified by its local
1688		 * IP address.  Find the interface and confirm that
1689		 * it supports multicasting.
1690		 */
1691		s = splimp();
1692		ifp = ip_multicast_if(&addr, &ifindex);
1693		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1694			splx(s);
1695			error = EADDRNOTAVAIL;
1696			break;
1697		}
1698		imo->imo_multicast_ifp = ifp;
1699		if (ifindex)
1700			imo->imo_multicast_addr = addr;
1701		else
1702			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1703		splx(s);
1704		break;
1705
1706	case IP_MULTICAST_TTL:
1707		/*
1708		 * Set the IP time-to-live for outgoing multicast packets.
1709		 * The original multicast API required a char argument,
1710		 * which is inconsistent with the rest of the socket API.
1711		 * We allow either a char or an int.
1712		 */
1713		if (sopt->sopt_valsize == 1) {
1714			u_char ttl;
1715			error = sooptcopyin(sopt, &ttl, 1, 1);
1716			if (error)
1717				break;
1718			imo->imo_multicast_ttl = ttl;
1719		} else {
1720			u_int ttl;
1721			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1722					    sizeof ttl);
1723			if (error)
1724				break;
1725			if (ttl > 255)
1726				error = EINVAL;
1727			else
1728				imo->imo_multicast_ttl = ttl;
1729		}
1730		break;
1731
1732	case IP_MULTICAST_LOOP:
1733		/*
1734		 * Set the loopback flag for outgoing multicast packets.
1735		 * Must be zero or one.  The original multicast API required a
1736		 * char argument, which is inconsistent with the rest
1737		 * of the socket API.  We allow either a char or an int.
1738		 */
1739		if (sopt->sopt_valsize == 1) {
1740			u_char loop;
1741			error = sooptcopyin(sopt, &loop, 1, 1);
1742			if (error)
1743				break;
1744			imo->imo_multicast_loop = !!loop;
1745		} else {
1746			u_int loop;
1747			error = sooptcopyin(sopt, &loop, sizeof loop,
1748					    sizeof loop);
1749			if (error)
1750				break;
1751			imo->imo_multicast_loop = !!loop;
1752		}
1753		break;
1754
1755	case IP_ADD_MEMBERSHIP:
1756		/*
1757		 * Add a multicast group membership.
1758		 * Group must be a valid IP multicast address.
1759		 */
1760		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1761		if (error)
1762			break;
1763
1764		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1765			error = EINVAL;
1766			break;
1767		}
1768		s = splimp();
1769		/*
1770		 * If no interface address was provided, use the interface of
1771		 * the route to the given multicast address.
1772		 */
1773		if (mreq.imr_interface.s_addr == INADDR_ANY) {
1774			bzero((caddr_t)&ro, sizeof(ro));
1775			dst = (struct sockaddr_in *)&ro.ro_dst;
1776			dst->sin_len = sizeof(*dst);
1777			dst->sin_family = AF_INET;
1778			dst->sin_addr = mreq.imr_multiaddr;
1779			rtalloc(&ro);
1780			if (ro.ro_rt == NULL) {
1781				error = EADDRNOTAVAIL;
1782				splx(s);
1783				break;
1784			}
1785			ifp = ro.ro_rt->rt_ifp;
1786			rtfree(ro.ro_rt);
1787		}
1788		else {
1789			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1790		}
1791
1792		/*
1793		 * See if we found an interface, and confirm that it
1794		 * supports multicast.
1795		 */
1796		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1797			error = EADDRNOTAVAIL;
1798			splx(s);
1799			break;
1800		}
1801		/*
1802		 * See if the membership already exists or if all the
1803		 * membership slots are full.
1804		 */
1805		for (i = 0; i < imo->imo_num_memberships; ++i) {
1806			if (imo->imo_membership[i]->inm_ifp == ifp &&
1807			    imo->imo_membership[i]->inm_addr.s_addr
1808						== mreq.imr_multiaddr.s_addr)
1809				break;
1810		}
1811		if (i < imo->imo_num_memberships) {
1812			error = EADDRINUSE;
1813			splx(s);
1814			break;
1815		}
1816		if (i == IP_MAX_MEMBERSHIPS) {
1817			error = ETOOMANYREFS;
1818			splx(s);
1819			break;
1820		}
1821		/*
1822		 * Everything looks good; add a new record to the multicast
1823		 * address list for the given interface.
1824		 */
1825		if ((imo->imo_membership[i] =
1826		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1827			error = ENOBUFS;
1828			splx(s);
1829			break;
1830		}
1831		++imo->imo_num_memberships;
1832		splx(s);
1833		break;
1834
1835	case IP_DROP_MEMBERSHIP:
1836		/*
1837		 * Drop a multicast group membership.
1838		 * Group must be a valid IP multicast address.
1839		 */
1840		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1841		if (error)
1842			break;
1843
1844		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1845			error = EINVAL;
1846			break;
1847		}
1848
1849		s = splimp();
1850		/*
1851		 * If an interface address was specified, get a pointer
1852		 * to its ifnet structure.
1853		 */
1854		if (mreq.imr_interface.s_addr == INADDR_ANY)
1855			ifp = NULL;
1856		else {
1857			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1858			if (ifp == NULL) {
1859				error = EADDRNOTAVAIL;
1860				splx(s);
1861				break;
1862			}
1863		}
1864		/*
1865		 * Find the membership in the membership array.
1866		 */
1867		for (i = 0; i < imo->imo_num_memberships; ++i) {
1868			if ((ifp == NULL ||
1869			     imo->imo_membership[i]->inm_ifp == ifp) &&
1870			     imo->imo_membership[i]->inm_addr.s_addr ==
1871			     mreq.imr_multiaddr.s_addr)
1872				break;
1873		}
1874		if (i == imo->imo_num_memberships) {
1875			error = EADDRNOTAVAIL;
1876			splx(s);
1877			break;
1878		}
1879		/*
1880		 * Give up the multicast address record to which the
1881		 * membership points.
1882		 */
1883		in_delmulti(imo->imo_membership[i]);
1884		/*
1885		 * Remove the gap in the membership array.
1886		 */
1887		for (++i; i < imo->imo_num_memberships; ++i)
1888			imo->imo_membership[i-1] = imo->imo_membership[i];
1889		--imo->imo_num_memberships;
1890		splx(s);
1891		break;
1892
1893	default:
1894		error = EOPNOTSUPP;
1895		break;
1896	}
1897
1898	/*
1899	 * If all options have default values, no need to keep the mbuf.
1900	 */
1901	if (imo->imo_multicast_ifp == NULL &&
1902	    imo->imo_multicast_vif == -1 &&
1903	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1904	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1905	    imo->imo_num_memberships == 0) {
1906		free(*imop, M_IPMOPTS);
1907		*imop = NULL;
1908	}
1909
1910	return (error);
1911}
1912
1913/*
1914 * Return the IP multicast options in response to user getsockopt().
1915 */
1916static int
1917ip_getmoptions(sopt, imo)
1918	struct sockopt *sopt;
1919	register struct ip_moptions *imo;
1920{
1921	struct in_addr addr;
1922	struct in_ifaddr *ia;
1923	int error, optval;
1924	u_char coptval;
1925
1926	error = 0;
1927	switch (sopt->sopt_name) {
1928	case IP_MULTICAST_VIF:
1929		if (imo != NULL)
1930			optval = imo->imo_multicast_vif;
1931		else
1932			optval = -1;
1933		error = sooptcopyout(sopt, &optval, sizeof optval);
1934		break;
1935
1936	case IP_MULTICAST_IF:
1937		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1938			addr.s_addr = INADDR_ANY;
1939		else if (imo->imo_multicast_addr.s_addr) {
1940			/* return the value user has set */
1941			addr = imo->imo_multicast_addr;
1942		} else {
1943			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1944			addr.s_addr = (ia == NULL) ? INADDR_ANY
1945				: IA_SIN(ia)->sin_addr.s_addr;
1946		}
1947		error = sooptcopyout(sopt, &addr, sizeof addr);
1948		break;
1949
1950	case IP_MULTICAST_TTL:
1951		if (imo == 0)
1952			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1953		else
1954			optval = coptval = imo->imo_multicast_ttl;
1955		if (sopt->sopt_valsize == 1)
1956			error = sooptcopyout(sopt, &coptval, 1);
1957		else
1958			error = sooptcopyout(sopt, &optval, sizeof optval);
1959		break;
1960
1961	case IP_MULTICAST_LOOP:
1962		if (imo == 0)
1963			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1964		else
1965			optval = coptval = imo->imo_multicast_loop;
1966		if (sopt->sopt_valsize == 1)
1967			error = sooptcopyout(sopt, &coptval, 1);
1968		else
1969			error = sooptcopyout(sopt, &optval, sizeof optval);
1970		break;
1971
1972	default:
1973		error = ENOPROTOOPT;
1974		break;
1975	}
1976	return (error);
1977}
1978
1979/*
1980 * Discard the IP multicast options.
1981 */
1982void
1983ip_freemoptions(imo)
1984	register struct ip_moptions *imo;
1985{
1986	register int i;
1987
1988	if (imo != NULL) {
1989		for (i = 0; i < imo->imo_num_memberships; ++i)
1990			in_delmulti(imo->imo_membership[i]);
1991		free(imo, M_IPMOPTS);
1992	}
1993}
1994
1995/*
1996 * Routine called from ip_output() to loop back a copy of an IP multicast
1997 * packet to the input queue of a specified interface.  Note that this
1998 * calls the output routine of the loopback "driver", but with an interface
1999 * pointer that might NOT be a loopback interface -- evil, but easier than
2000 * replicating that code here.
2001 */
2002static void
2003ip_mloopback(ifp, m, dst, hlen)
2004	struct ifnet *ifp;
2005	register struct mbuf *m;
2006	register struct sockaddr_in *dst;
2007	int hlen;
2008{
2009	register struct ip *ip;
2010	struct mbuf *copym;
2011
2012	copym = m_copy(m, 0, M_COPYALL);
2013	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
2014		copym = m_pullup(copym, hlen);
2015	if (copym != NULL) {
2016		/*
2017		 * We don't bother to fragment if the IP length is greater
2018		 * than the interface's MTU.  Can this possibly matter?
2019		 */
2020		ip = mtod(copym, struct ip *);
2021		ip->ip_len = htons(ip->ip_len);
2022		ip->ip_off = htons(ip->ip_off);
2023		ip->ip_sum = 0;
2024		if (ip->ip_vhl == IP_VHL_BORING) {
2025			ip->ip_sum = in_cksum_hdr(ip);
2026		} else {
2027			ip->ip_sum = in_cksum(copym, hlen);
2028		}
2029		/*
2030		 * NB:
2031		 * It's not clear whether there are any lingering
2032		 * reentrancy problems in other areas which might
2033		 * be exposed by using ip_input directly (in
2034		 * particular, everything which modifies the packet
2035		 * in-place).  Yet another option is using the
2036		 * protosw directly to deliver the looped back
2037		 * packet.  For the moment, we'll err on the side
2038		 * of safety by using if_simloop().
2039		 */
2040#if 1 /* XXX */
2041		if (dst->sin_family != AF_INET) {
2042			printf("ip_mloopback: bad address family %d\n",
2043						dst->sin_family);
2044			dst->sin_family = AF_INET;
2045		}
2046#endif
2047
2048#ifdef notdef
2049		copym->m_pkthdr.rcvif = ifp;
2050		ip_input(copym);
2051#else
2052		/* if the checksum hasn't been computed, mark it as valid */
2053		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2054			copym->m_pkthdr.csum_flags |=
2055			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2056			copym->m_pkthdr.csum_data = 0xffff;
2057		}
2058		if_simloop(ifp, copym, dst->sin_family, 0);
2059#endif
2060	}
2061}
2062