ip_output.c revision 87916
179979Sobrien/*
279979Sobrien * Copyright (c) 1982, 1986, 1988, 1990, 1993
379979Sobrien *	The Regents of the University of California.  All rights reserved.
479979Sobrien *
579979Sobrien * Redistribution and use in source and binary forms, with or without
679979Sobrien * modification, are permitted provided that the following conditions
779979Sobrien * are met:
879979Sobrien * 1. Redistributions of source code must retain the above copyright
979979Sobrien *    notice, this list of conditions and the following disclaimer.
1079979Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1179979Sobrien *    notice, this list of conditions and the following disclaimer in the
1279979Sobrien *    documentation and/or other materials provided with the distribution.
1379979Sobrien * 3. All advertising materials mentioning features or use of this software
1479979Sobrien *    must display the following acknowledgement:
1579979Sobrien *	This product includes software developed by the University of
1679979Sobrien *	California, Berkeley and its contributors.
1779979Sobrien * 4. Neither the name of the University nor the names of its contributors
1879979Sobrien *    may be used to endorse or promote products derived from this software
1979979Sobrien *    without specific prior written permission.
2079979Sobrien *
2179979Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2279979Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2379979Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2479979Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2579979Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2679979Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2779979Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2879979Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2979979Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3079979Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3179979Sobrien * SUCH DAMAGE.
3279979Sobrien *
3379979Sobrien *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
3479979Sobrien * $FreeBSD: head/sys/netinet/ip_output.c 87916 2001-12-14 19:34:11Z jlemon $
3579979Sobrien */
3679979Sobrien
3779979Sobrien#define _IP_VHL
3879979Sobrien
3979979Sobrien#include "opt_ipfw.h"
4079979Sobrien#include "opt_ipdn.h"
4179979Sobrien#include "opt_ipdivert.h"
4279979Sobrien#include "opt_ipfilter.h"
4379979Sobrien#include "opt_ipsec.h"
4479979Sobrien#include "opt_pfil_hooks.h"
4579979Sobrien#include "opt_random_ip_id.h"
4679979Sobrien
4779979Sobrien#include <sys/param.h>
4879979Sobrien#include <sys/systm.h>
4979979Sobrien#include <sys/kernel.h>
5079979Sobrien#include <sys/malloc.h>
5179979Sobrien#include <sys/mbuf.h>
5279979Sobrien#include <sys/protosw.h>
5379979Sobrien#include <sys/socket.h>
5479979Sobrien#include <sys/socketvar.h>
5579979Sobrien
5679979Sobrien#include <net/if.h>
5779979Sobrien#include <net/route.h>
5879979Sobrien
5979979Sobrien#include <netinet/in.h>
6079979Sobrien#include <netinet/in_systm.h>
6179979Sobrien#include <netinet/ip.h>
6279979Sobrien#include <netinet/in_pcb.h>
6379979Sobrien#include <netinet/in_var.h>
6479979Sobrien#include <netinet/ip_var.h>
6579979Sobrien
6679979Sobrien#include <machine/in_cksum.h>
6779979Sobrien
6879979Sobrienstatic MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
6979979Sobrien
7079979Sobrien#ifdef IPSEC
7179979Sobrien#include <netinet6/ipsec.h>
7279979Sobrien#include <netkey/key.h>
7379979Sobrien#ifdef IPSEC_DEBUG
7479979Sobrien#include <netkey/key_debug.h>
7579979Sobrien#else
7679979Sobrien#define	KEYDEBUG(lev,arg)
7779979Sobrien#endif
7879979Sobrien#endif /*IPSEC*/
7979979Sobrien
8079979Sobrien#include <netinet/ip_fw.h>
8179979Sobrien#include <netinet/ip_dummynet.h>
8279979Sobrien
8379979Sobrien#ifdef IPFIREWALL_FORWARD_DEBUG
8479979Sobrien#define print_ip(a)	 printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
8579979Sobrien				 		  (ntohl(a.s_addr)>>16)&0xFF,\
8679979Sobrien						  (ntohl(a.s_addr)>>8)&0xFF,\
8779979Sobrien						  (ntohl(a.s_addr))&0xFF);
8879979Sobrien#endif
8979979Sobrien
9079979Sobrienu_short ip_id;
9179979Sobrien
9279979Sobrienstatic struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
9379979Sobrienstatic struct ifnet *ip_multicast_if __P((struct in_addr *, int *));
9479979Sobrienstatic void	ip_mloopback
9579979Sobrien	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
9679979Sobrienstatic int	ip_getmoptions
9779979Sobrien	__P((struct sockopt *, struct ip_moptions *));
9879979Sobrienstatic int	ip_pcbopts __P((int, struct mbuf **, struct mbuf *));
9979979Sobrienstatic int	ip_setmoptions
10079979Sobrien	__P((struct sockopt *, struct ip_moptions **));
10179979Sobrien
10279979Sobrienint	ip_optcopy __P((struct ip *, struct ip *));
10379979Sobrien
10479979Sobrien
10579979Sobrienextern	struct protosw inetsw[];
10679979Sobrien
10779979Sobrien/*
10879979Sobrien * IP output.  The packet in mbuf chain m contains a skeletal IP
10979979Sobrien * header (with len, off, ttl, proto, tos, src, dst).
11079979Sobrien * The mbuf chain containing the packet will be freed.
11179979Sobrien * The mbuf opt, if present, will not be freed.
11279979Sobrien */
11379979Sobrienint
11479979Sobrienip_output(m0, opt, ro, flags, imo)
11579979Sobrien	struct mbuf *m0;
11679979Sobrien	struct mbuf *opt;
11779979Sobrien	struct route *ro;
11879979Sobrien	int flags;
11979979Sobrien	struct ip_moptions *imo;
12079979Sobrien{
12179979Sobrien	struct ip *ip, *mhip;
12279979Sobrien	struct ifnet *ifp;
12379979Sobrien	struct mbuf *m = m0;
12479979Sobrien	int hlen = sizeof (struct ip);
12579979Sobrien	int len, off, error = 0;
12679979Sobrien	struct route iproute;
12779979Sobrien	struct sockaddr_in *dst;
12879979Sobrien	struct in_ifaddr *ia;
12979979Sobrien	int isbroadcast, sw_csum;
13079979Sobrien#ifdef IPSEC
13179979Sobrien	struct socket *so = NULL;
13279979Sobrien	struct secpolicy *sp = NULL;
13379979Sobrien#endif
13479979Sobrien	u_int16_t divert_cookie;		/* firewall cookie */
13579979Sobrien#ifdef PFIL_HOOKS
13679979Sobrien	struct packet_filter_hook *pfh;
13779979Sobrien	struct mbuf *m1;
13879979Sobrien	int rv;
13979979Sobrien#endif /* PFIL_HOOKS */
14079979Sobrien#ifdef IPFIREWALL_FORWARD
14179979Sobrien	int fwd_rewrite_src = 0;
14279979Sobrien#endif
14379979Sobrien	struct ip_fw *rule = NULL;
14479979Sobrien
14579979Sobrien#ifdef IPDIVERT
14679979Sobrien	/* Get and reset firewall cookie */
14779979Sobrien	divert_cookie = ip_divert_cookie;
14879979Sobrien	ip_divert_cookie = 0;
14979979Sobrien#else
15079979Sobrien	divert_cookie = 0;
15179979Sobrien#endif
15279979Sobrien
15379979Sobrien        /*
15479979Sobrien         * dummynet packet are prepended a vestigial mbuf with
15579979Sobrien         * m_type = MT_DUMMYNET and m_data pointing to the matching
15679979Sobrien         * rule.
15779979Sobrien         */
15879979Sobrien        if (m->m_type == MT_DUMMYNET) {
15979979Sobrien            /*
16079979Sobrien             * the packet was already tagged, so part of the
16179979Sobrien             * processing was already done, and we need to go down.
16279979Sobrien             * Get parameters from the header.
16379979Sobrien             */
16479979Sobrien            rule = (struct ip_fw *)(m->m_data) ;
16579979Sobrien	    opt = NULL ;
16679979Sobrien	    ro = & ( ((struct dn_pkt *)m)->ro ) ;
16779979Sobrien	    imo = NULL ;
16879979Sobrien	    dst = ((struct dn_pkt *)m)->dn_dst ;
16979979Sobrien	    ifp = ((struct dn_pkt *)m)->ifp ;
17079979Sobrien	    flags = ((struct dn_pkt *)m)->flags ;
17179979Sobrien
17279979Sobrien            m0 = m = m->m_next ;
17379979Sobrien#ifdef IPSEC
17479979Sobrien	    so = ipsec_getsocket(m);
17579979Sobrien	    (void)ipsec_setsocket(m, NULL);
17679979Sobrien#endif
17779979Sobrien            ip = mtod(m, struct ip *);
17879979Sobrien            hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
17979979Sobrien            ia = ifatoia(ro->ro_rt->rt_ifa);
18079979Sobrien            goto sendit;
18179979Sobrien        } else
18279979Sobrien            rule = NULL ;
18379979Sobrien#ifdef IPSEC
18479979Sobrien	so = ipsec_getsocket(m);
18579979Sobrien	(void)ipsec_setsocket(m, NULL);
18679979Sobrien#endif
18779979Sobrien
18879979Sobrien#ifdef	DIAGNOSTIC
18979979Sobrien	if ((m->m_flags & M_PKTHDR) == 0)
19079979Sobrien		panic("ip_output no HDR");
19179979Sobrien#endif
19279979Sobrien	if (opt) {
19379979Sobrien		m = ip_insertoptions(m, opt, &len);
19479979Sobrien		hlen = len;
19579979Sobrien	}
19679979Sobrien	ip = mtod(m, struct ip *);
19779979Sobrien	/*
19879979Sobrien	 * Fill in IP header.
19979979Sobrien	 */
20079979Sobrien	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
20179979Sobrien		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
20279979Sobrien		ip->ip_off &= IP_DF;
20379979Sobrien#ifdef RANDOM_IP_ID
20479979Sobrien		ip->ip_id = ip_randomid();
20579979Sobrien#else
20679979Sobrien		ip->ip_id = htons(ip_id++);
20779979Sobrien#endif
20879979Sobrien		ipstat.ips_localout++;
20979979Sobrien	} else {
21079979Sobrien		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
21179979Sobrien	}
21279979Sobrien
21379979Sobrien	/* Route packet. */
21479979Sobrien	if (ro == NULL) {
21579979Sobrien		ro = &iproute;
21679979Sobrien		bzero(ro, sizeof(*ro));
21779979Sobrien	}
21879979Sobrien	dst = (struct sockaddr_in *)&ro->ro_dst;
21979979Sobrien	/*
22079979Sobrien	 * If there is a cached route,
22179979Sobrien	 * check that it is to the same destination
22279979Sobrien	 * and is still up.  If not, free it and try again.
22379979Sobrien	 */
22479979Sobrien	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
22579979Sobrien	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
22679979Sobrien		RTFREE(ro->ro_rt);
22779979Sobrien		ro->ro_rt = (struct rtentry *)0;
22879979Sobrien	}
22979979Sobrien	if (ro->ro_rt == 0) {
23079979Sobrien		dst->sin_family = AF_INET;
23179979Sobrien		dst->sin_len = sizeof(*dst);
23279979Sobrien		dst->sin_addr = ip->ip_dst;
23379979Sobrien	}
23479979Sobrien	/*
23579979Sobrien	 * If routing to interface only,
23679979Sobrien	 * short circuit routing lookup.
23779979Sobrien	 */
23879979Sobrien	if (flags & IP_ROUTETOIF) {
23979979Sobrien		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
24079979Sobrien		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
24179979Sobrien			ipstat.ips_noroute++;
24279979Sobrien			error = ENETUNREACH;
24379979Sobrien			goto bad;
24479979Sobrien		}
24579979Sobrien		ifp = ia->ia_ifp;
24679979Sobrien		ip->ip_ttl = 1;
24779979Sobrien		isbroadcast = in_broadcast(dst->sin_addr, ifp);
24879979Sobrien	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
24979979Sobrien	    imo != NULL && imo->imo_multicast_ifp != NULL) {
25079979Sobrien		/*
25179979Sobrien		 * Bypass the normal routing lookup for multicast
25279979Sobrien		 * packets if the interface is specified.
25379979Sobrien		 */
25479979Sobrien		ifp = imo->imo_multicast_ifp;
25579979Sobrien		IFP_TO_IA(ifp, ia);
25679979Sobrien		isbroadcast = 0;	/* fool gcc */
25779979Sobrien	} else {
25879979Sobrien		/*
25979979Sobrien		 * If this is the case, we probably don't want to allocate
26079979Sobrien		 * a protocol-cloned route since we didn't get one from the
26179979Sobrien		 * ULP.  This lets TCP do its thing, while not burdening
26279979Sobrien		 * forwarding or ICMP with the overhead of cloning a route.
26379979Sobrien		 * Of course, we still want to do any cloning requested by
26479979Sobrien		 * the link layer, as this is probably required in all cases
26579979Sobrien		 * for correct operation (as it is for ARP).
26679979Sobrien		 */
26779979Sobrien		if (ro->ro_rt == 0)
26879979Sobrien			rtalloc_ign(ro, RTF_PRCLONING);
26979979Sobrien		if (ro->ro_rt == 0) {
27079979Sobrien			ipstat.ips_noroute++;
27179979Sobrien			error = EHOSTUNREACH;
27279979Sobrien			goto bad;
27379979Sobrien		}
27479979Sobrien		ia = ifatoia(ro->ro_rt->rt_ifa);
27579979Sobrien		ifp = ro->ro_rt->rt_ifp;
27679979Sobrien		ro->ro_rt->rt_use++;
27779979Sobrien		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
27879979Sobrien			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
27979979Sobrien		if (ro->ro_rt->rt_flags & RTF_HOST)
28079979Sobrien			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
28179979Sobrien		else
282			isbroadcast = in_broadcast(dst->sin_addr, ifp);
283	}
284	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
285		struct in_multi *inm;
286
287		m->m_flags |= M_MCAST;
288		/*
289		 * IP destination address is multicast.  Make sure "dst"
290		 * still points to the address in "ro".  (It may have been
291		 * changed to point to a gateway address, above.)
292		 */
293		dst = (struct sockaddr_in *)&ro->ro_dst;
294		/*
295		 * See if the caller provided any multicast options
296		 */
297		if (imo != NULL) {
298			ip->ip_ttl = imo->imo_multicast_ttl;
299			if (imo->imo_multicast_vif != -1)
300				ip->ip_src.s_addr =
301				    ip_mcast_src(imo->imo_multicast_vif);
302		} else
303			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
304		/*
305		 * Confirm that the outgoing interface supports multicast.
306		 */
307		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
308			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
309				ipstat.ips_noroute++;
310				error = ENETUNREACH;
311				goto bad;
312			}
313		}
314		/*
315		 * If source address not specified yet, use address
316		 * of outgoing interface.
317		 */
318		if (ip->ip_src.s_addr == INADDR_ANY) {
319			/* Interface may have no addresses. */
320			if (ia != NULL)
321				ip->ip_src = IA_SIN(ia)->sin_addr;
322		}
323
324		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
325		if (inm != NULL &&
326		   (imo == NULL || imo->imo_multicast_loop)) {
327			/*
328			 * If we belong to the destination multicast group
329			 * on the outgoing interface, and the caller did not
330			 * forbid loopback, loop back a copy.
331			 */
332			ip_mloopback(ifp, m, dst, hlen);
333		}
334		else {
335			/*
336			 * If we are acting as a multicast router, perform
337			 * multicast forwarding as if the packet had just
338			 * arrived on the interface to which we are about
339			 * to send.  The multicast forwarding function
340			 * recursively calls this function, using the
341			 * IP_FORWARDING flag to prevent infinite recursion.
342			 *
343			 * Multicasts that are looped back by ip_mloopback(),
344			 * above, will be forwarded by the ip_input() routine,
345			 * if necessary.
346			 */
347			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
348				/*
349				 * Check if rsvp daemon is running. If not, don't
350				 * set ip_moptions. This ensures that the packet
351				 * is multicast and not just sent down one link
352				 * as prescribed by rsvpd.
353				 */
354				if (!rsvp_on)
355				  imo = NULL;
356				if (ip_mforward(ip, ifp, m, imo) != 0) {
357					m_freem(m);
358					goto done;
359				}
360			}
361		}
362
363		/*
364		 * Multicasts with a time-to-live of zero may be looped-
365		 * back, above, but must not be transmitted on a network.
366		 * Also, multicasts addressed to the loopback interface
367		 * are not sent -- the above call to ip_mloopback() will
368		 * loop back a copy if this host actually belongs to the
369		 * destination group on the loopback interface.
370		 */
371		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
372			m_freem(m);
373			goto done;
374		}
375
376		goto sendit;
377	}
378#ifndef notdef
379	/*
380	 * If source address not specified yet, use address
381	 * of outgoing interface.
382	 */
383	if (ip->ip_src.s_addr == INADDR_ANY) {
384		/* Interface may have no addresses. */
385		if (ia != NULL) {
386			ip->ip_src = IA_SIN(ia)->sin_addr;
387#ifdef IPFIREWALL_FORWARD
388			/* Keep note that we did this - if the firewall changes
389		 	* the next-hop, our interface may change, changing the
390		 	* default source IP. It's a shame so much effort happens
391		 	* twice. Oh well.
392		 	*/
393			fwd_rewrite_src++;
394#endif /* IPFIREWALL_FORWARD */
395		}
396	}
397#endif /* notdef */
398	/*
399	 * Verify that we have any chance at all of being able to queue
400	 *      the packet or packet fragments
401	 */
402	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
403		ifp->if_snd.ifq_maxlen) {
404			error = ENOBUFS;
405			ipstat.ips_odropped++;
406			goto bad;
407	}
408
409	/*
410	 * Look for broadcast address and
411	 * and verify user is allowed to send
412	 * such a packet.
413	 */
414	if (isbroadcast) {
415		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
416			error = EADDRNOTAVAIL;
417			goto bad;
418		}
419		if ((flags & IP_ALLOWBROADCAST) == 0) {
420			error = EACCES;
421			goto bad;
422		}
423		/* don't allow broadcast messages to be fragmented */
424		if ((u_short)ip->ip_len > ifp->if_mtu) {
425			error = EMSGSIZE;
426			goto bad;
427		}
428		m->m_flags |= M_BCAST;
429	} else {
430		m->m_flags &= ~M_BCAST;
431	}
432
433sendit:
434#ifdef IPSEC
435	/* get SP for this packet */
436	if (so == NULL)
437		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
438	else
439		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
440
441	if (sp == NULL) {
442		ipsecstat.out_inval++;
443		goto bad;
444	}
445
446	error = 0;
447
448	/* check policy */
449	switch (sp->policy) {
450	case IPSEC_POLICY_DISCARD:
451		/*
452		 * This packet is just discarded.
453		 */
454		ipsecstat.out_polvio++;
455		goto bad;
456
457	case IPSEC_POLICY_BYPASS:
458	case IPSEC_POLICY_NONE:
459		/* no need to do IPsec. */
460		goto skip_ipsec;
461
462	case IPSEC_POLICY_IPSEC:
463		if (sp->req == NULL) {
464			/* acquire a policy */
465			error = key_spdacquire(sp);
466			goto bad;
467		}
468		break;
469
470	case IPSEC_POLICY_ENTRUST:
471	default:
472		printf("ip_output: Invalid policy found. %d\n", sp->policy);
473	}
474    {
475	struct ipsec_output_state state;
476	bzero(&state, sizeof(state));
477	state.m = m;
478	if (flags & IP_ROUTETOIF) {
479		state.ro = &iproute;
480		bzero(&iproute, sizeof(iproute));
481	} else
482		state.ro = ro;
483	state.dst = (struct sockaddr *)dst;
484
485	ip->ip_sum = 0;
486
487	/*
488	 * XXX
489	 * delayed checksums are not currently compatible with IPsec
490	 */
491	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
492		in_delayed_cksum(m);
493		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
494	}
495
496	HTONS(ip->ip_len);
497	HTONS(ip->ip_off);
498
499	error = ipsec4_output(&state, sp, flags);
500
501	m = state.m;
502	if (flags & IP_ROUTETOIF) {
503		/*
504		 * if we have tunnel mode SA, we may need to ignore
505		 * IP_ROUTETOIF.
506		 */
507		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
508			flags &= ~IP_ROUTETOIF;
509			ro = state.ro;
510		}
511	} else
512		ro = state.ro;
513	dst = (struct sockaddr_in *)state.dst;
514	if (error) {
515		/* mbuf is already reclaimed in ipsec4_output. */
516		m0 = NULL;
517		switch (error) {
518		case EHOSTUNREACH:
519		case ENETUNREACH:
520		case EMSGSIZE:
521		case ENOBUFS:
522		case ENOMEM:
523			break;
524		default:
525			printf("ip4_output (ipsec): error code %d\n", error);
526			/*fall through*/
527		case ENOENT:
528			/* don't show these error codes to the user */
529			error = 0;
530			break;
531		}
532		goto bad;
533	}
534    }
535
536	/* be sure to update variables that are affected by ipsec4_output() */
537	ip = mtod(m, struct ip *);
538#ifdef _IP_VHL
539	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
540#else
541	hlen = ip->ip_hl << 2;
542#endif
543	if (ro->ro_rt == NULL) {
544		if ((flags & IP_ROUTETOIF) == 0) {
545			printf("ip_output: "
546				"can't update route after IPsec processing\n");
547			error = EHOSTUNREACH;	/*XXX*/
548			goto bad;
549		}
550	} else {
551		ia = ifatoia(ro->ro_rt->rt_ifa);
552		ifp = ro->ro_rt->rt_ifp;
553	}
554
555	/* make it flipped, again. */
556	NTOHS(ip->ip_len);
557	NTOHS(ip->ip_off);
558skip_ipsec:
559#endif /*IPSEC*/
560
561	/*
562	 * IpHack's section.
563	 * - Xlate: translate packet's addr/port (NAT).
564	 * - Firewall: deny/allow/etc.
565	 * - Wrap: fake packet's addr/port <unimpl.>
566	 * - Encapsulate: put it in another IP and send out. <unimp.>
567	 */
568#ifdef PFIL_HOOKS
569	/*
570	 * Run through list of hooks for output packets.
571	 */
572	m1 = m;
573	pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
574	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
575		if (pfh->pfil_func) {
576			rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
577			if (rv) {
578				error = EHOSTUNREACH;
579				goto done;
580			}
581			m = m1;
582			if (m == NULL)
583				goto done;
584			ip = mtod(m, struct ip *);
585		}
586#endif /* PFIL_HOOKS */
587
588	/*
589	 * Check with the firewall...
590	 */
591	if (fw_enable && IPFW_LOADED) {
592		struct sockaddr_in *old = dst;
593
594		off = ip_fw_chk_ptr(&ip,
595		    hlen, ifp, &divert_cookie, &m, &rule, &dst);
596                /*
597                 * On return we must do the following:
598                 * m == NULL         -> drop the pkt (old interface, deprecated)
599                 * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
600                 * 1<=off<= 0xffff		-> DIVERT
601                 * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
602                 * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
603                 * dst != old			-> IPFIREWALL_FORWARD
604                 * off==0, dst==old		-> accept
605                 * If some of the above modules are not compiled in, then
606                 * we should't have to check the corresponding condition
607                 * (because the ipfw control socket should not accept
608                 * unsupported rules), but better play safe and drop
609                 * packets in case of doubt.
610                 */
611		if (off & IP_FW_PORT_DENY_FLAG) { /* XXX new interface-denied */
612			if (m)
613				m_freem(m);
614			error = EACCES;
615			goto done;
616		}
617		if (!m) {			/* firewall said to reject */
618			static int __debug=10;
619
620			if (__debug > 0) {
621				printf(
622				    "firewall returns NULL, please update!\n");
623				__debug--;
624			}
625			error = EACCES;
626			goto done;
627		}
628		if (off == 0 && dst == old)		/* common case */
629			goto pass;
630                if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
631			/*
632			 * pass the pkt to dummynet. Need to include
633			 * pipe number, m, ifp, ro, dst because these are
634			 * not recomputed in the next pass.
635			 * All other parameters have been already used and
636			 * so they are not needed anymore.
637			 * XXX note: if the ifp or ro entry are deleted
638			 * while a pkt is in dummynet, we are in trouble!
639			 */
640			error = ip_dn_io_ptr(off & 0xffff, DN_TO_IP_OUT, m,
641			    ifp, ro, dst, rule, flags);
642			goto done;
643		}
644#ifdef IPDIVERT
645		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
646			struct mbuf *clone = NULL;
647
648			/* Clone packet if we're doing a 'tee' */
649			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
650				clone = m_dup(m, M_DONTWAIT);
651
652			/*
653			 * XXX
654			 * delayed checksums are not currently compatible
655			 * with divert sockets.
656			 */
657			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
658				in_delayed_cksum(m);
659				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
660			}
661
662			/* Restore packet header fields to original values */
663			HTONS(ip->ip_len);
664			HTONS(ip->ip_off);
665
666			/* Deliver packet to divert input routine */
667			ip_divert_cookie = divert_cookie;
668			divert_packet(m, 0, off & 0xffff);
669
670			/* If 'tee', continue with original packet */
671			if (clone != NULL) {
672				m = clone;
673				ip = mtod(m, struct ip *);
674				goto pass;
675			}
676			goto done;
677		}
678#endif
679
680#ifdef IPFIREWALL_FORWARD
681		/* Here we check dst to make sure it's directly reachable on the
682		 * interface we previously thought it was.
683		 * If it isn't (which may be likely in some situations) we have
684		 * to re-route it (ie, find a route for the next-hop and the
685		 * associated interface) and set them here. This is nested
686		 * forwarding which in most cases is undesirable, except where
687		 * such control is nigh impossible. So we do it here.
688		 * And I'm babbling.
689		 */
690		if (off == 0 && old != dst) {
691			struct in_ifaddr *ia;
692
693			/* It's changed... */
694			/* There must be a better way to do this next line... */
695			static struct route sro_fwd, *ro_fwd = &sro_fwd;
696#ifdef IPFIREWALL_FORWARD_DEBUG
697			printf("IPFIREWALL_FORWARD: New dst ip: ");
698			print_ip(dst->sin_addr);
699			printf("\n");
700#endif
701			/*
702			 * We need to figure out if we have been forwarded
703			 * to a local socket. If so then we should somehow
704			 * "loop back" to ip_input, and get directed to the
705			 * PCB as if we had received this packet. This is
706			 * because it may be dificult to identify the packets
707			 * you want to forward until they are being output
708			 * and have selected an interface. (e.g. locally
709			 * initiated packets) If we used the loopback inteface,
710			 * we would not be able to control what happens
711			 * as the packet runs through ip_input() as
712			 * it is done through a ISR.
713			 */
714			LIST_FOREACH(ia,
715			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
716				/*
717				 * If the addr to forward to is one
718				 * of ours, we pretend to
719				 * be the destination for this packet.
720				 */
721				if (IA_SIN(ia)->sin_addr.s_addr ==
722						 dst->sin_addr.s_addr)
723					break;
724			}
725			if (ia) {
726				/* tell ip_input "dont filter" */
727				ip_fw_fwd_addr = dst;
728				if (m->m_pkthdr.rcvif == NULL)
729					m->m_pkthdr.rcvif = ifunit("lo0");
730				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
731					m->m_pkthdr.csum_flags |=
732					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
733					m0->m_pkthdr.csum_data = 0xffff;
734				}
735				m->m_pkthdr.csum_flags |=
736				    CSUM_IP_CHECKED | CSUM_IP_VALID;
737				HTONS(ip->ip_len);
738				HTONS(ip->ip_off);
739				ip_input(m);
740				goto done;
741			}
742			/* Some of the logic for this was
743			 * nicked from above.
744			 *
745			 * This rewrites the cached route in a local PCB.
746			 * Is this what we want to do?
747			 */
748			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
749
750			ro_fwd->ro_rt = 0;
751			rtalloc_ign(ro_fwd, RTF_PRCLONING);
752
753			if (ro_fwd->ro_rt == 0) {
754				ipstat.ips_noroute++;
755				error = EHOSTUNREACH;
756				goto bad;
757			}
758
759			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
760			ifp = ro_fwd->ro_rt->rt_ifp;
761			ro_fwd->ro_rt->rt_use++;
762			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
763				dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
764			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
765				isbroadcast =
766				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
767			else
768				isbroadcast = in_broadcast(dst->sin_addr, ifp);
769			RTFREE(ro->ro_rt);
770			ro->ro_rt = ro_fwd->ro_rt;
771			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
772
773			/*
774			 * If we added a default src ip earlier,
775			 * which would have been gotten from the-then
776			 * interface, do it again, from the new one.
777			 */
778			if (fwd_rewrite_src)
779				ip->ip_src = IA_SIN(ia)->sin_addr;
780			goto pass ;
781		}
782#endif /* IPFIREWALL_FORWARD */
783                /*
784                 * if we get here, none of the above matches, and
785                 * we have to drop the pkt
786                 */
787		m_freem(m);
788                error = EACCES; /* not sure this is the right error msg */
789                goto done;
790	}
791
792pass:
793	m->m_pkthdr.csum_flags |= CSUM_IP;
794	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
795	if (sw_csum & CSUM_DELAY_DATA) {
796		in_delayed_cksum(m);
797		sw_csum &= ~CSUM_DELAY_DATA;
798	}
799	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
800
801	/*
802	 * If small enough for interface, or the interface will take
803	 * care of the fragmentation for us, can just send directly.
804	 */
805	if ((u_short)ip->ip_len <= ifp->if_mtu ||
806	    ifp->if_hwassist & CSUM_FRAGMENT) {
807		HTONS(ip->ip_len);
808		HTONS(ip->ip_off);
809		ip->ip_sum = 0;
810		if (sw_csum & CSUM_DELAY_IP) {
811			if (ip->ip_vhl == IP_VHL_BORING) {
812				ip->ip_sum = in_cksum_hdr(ip);
813			} else {
814				ip->ip_sum = in_cksum(m, hlen);
815			}
816		}
817
818		/* Record statistics for this interface address. */
819		if (!(flags & IP_FORWARDING) && ia) {
820			ia->ia_ifa.if_opackets++;
821			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
822		}
823
824#ifdef IPSEC
825		/* clean ipsec history once it goes out of the node */
826		ipsec_delaux(m);
827#endif
828
829		error = (*ifp->if_output)(ifp, m,
830				(struct sockaddr *)dst, ro->ro_rt);
831		goto done;
832	}
833	/*
834	 * Too large for interface; fragment if possible.
835	 * Must be able to put at least 8 bytes per fragment.
836	 */
837	if (ip->ip_off & IP_DF) {
838		error = EMSGSIZE;
839		/*
840		 * This case can happen if the user changed the MTU
841		 * of an interface after enabling IP on it.  Because
842		 * most netifs don't keep track of routes pointing to
843		 * them, there is no way for one to update all its
844		 * routes when the MTU is changed.
845		 */
846		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
847		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
848		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
849			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
850		}
851		ipstat.ips_cantfrag++;
852		goto bad;
853	}
854	len = (ifp->if_mtu - hlen) &~ 7;
855	if (len < 8) {
856		error = EMSGSIZE;
857		goto bad;
858	}
859
860	/*
861	 * if the interface will not calculate checksums on
862	 * fragmented packets, then do it here.
863	 */
864	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
865	    (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
866		in_delayed_cksum(m);
867		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
868	}
869
870    {
871	int mhlen, firstlen = len;
872	struct mbuf **mnext = &m->m_nextpkt;
873	int nfrags = 1;
874
875	/*
876	 * Loop through length of segment after first fragment,
877	 * make new header and copy data of each part and link onto chain.
878	 */
879	m0 = m;
880	mhlen = sizeof (struct ip);
881	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
882		MGETHDR(m, M_DONTWAIT, MT_HEADER);
883		if (m == 0) {
884			error = ENOBUFS;
885			ipstat.ips_odropped++;
886			goto sendorfree;
887		}
888		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
889		m->m_data += max_linkhdr;
890		mhip = mtod(m, struct ip *);
891		*mhip = *ip;
892		if (hlen > sizeof (struct ip)) {
893			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
894			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
895		}
896		m->m_len = mhlen;
897		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
898		if (off + len >= (u_short)ip->ip_len)
899			len = (u_short)ip->ip_len - off;
900		else
901			mhip->ip_off |= IP_MF;
902		mhip->ip_len = htons((u_short)(len + mhlen));
903		m->m_next = m_copy(m0, off, len);
904		if (m->m_next == 0) {
905			(void) m_free(m);
906			error = ENOBUFS;	/* ??? */
907			ipstat.ips_odropped++;
908			goto sendorfree;
909		}
910		m->m_pkthdr.len = mhlen + len;
911		m->m_pkthdr.rcvif = (struct ifnet *)0;
912		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
913		HTONS(mhip->ip_off);
914		mhip->ip_sum = 0;
915		if (sw_csum & CSUM_DELAY_IP) {
916			if (mhip->ip_vhl == IP_VHL_BORING) {
917				mhip->ip_sum = in_cksum_hdr(mhip);
918			} else {
919				mhip->ip_sum = in_cksum(m, mhlen);
920			}
921		}
922		*mnext = m;
923		mnext = &m->m_nextpkt;
924		nfrags++;
925	}
926	ipstat.ips_ofragments += nfrags;
927
928	/* set first/last markers for fragment chain */
929	m->m_flags |= M_LASTFRAG;
930	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
931	m0->m_pkthdr.csum_data = nfrags;
932
933	/*
934	 * Update first fragment by trimming what's been copied out
935	 * and updating header, then send each fragment (in order).
936	 */
937	m = m0;
938	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
939	m->m_pkthdr.len = hlen + firstlen;
940	ip->ip_len = htons((u_short)m->m_pkthdr.len);
941	ip->ip_off |= IP_MF;
942	HTONS(ip->ip_off);
943	ip->ip_sum = 0;
944	if (sw_csum & CSUM_DELAY_IP) {
945		if (ip->ip_vhl == IP_VHL_BORING) {
946			ip->ip_sum = in_cksum_hdr(ip);
947		} else {
948			ip->ip_sum = in_cksum(m, hlen);
949		}
950	}
951sendorfree:
952	for (m = m0; m; m = m0) {
953		m0 = m->m_nextpkt;
954		m->m_nextpkt = 0;
955#ifdef IPSEC
956		/* clean ipsec history once it goes out of the node */
957		ipsec_delaux(m);
958#endif
959		if (error == 0) {
960			/* Record statistics for this interface address. */
961			if (ia != NULL) {
962				ia->ia_ifa.if_opackets++;
963				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
964			}
965
966			error = (*ifp->if_output)(ifp, m,
967			    (struct sockaddr *)dst, ro->ro_rt);
968		} else
969			m_freem(m);
970	}
971
972	if (error == 0)
973		ipstat.ips_fragmented++;
974    }
975done:
976#ifdef IPSEC
977	if (ro == &iproute && ro->ro_rt) {
978		RTFREE(ro->ro_rt);
979		ro->ro_rt = NULL;
980	}
981	if (sp != NULL) {
982		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
983			printf("DP ip_output call free SP:%p\n", sp));
984		key_freesp(sp);
985	}
986#endif /* IPSEC */
987	return (error);
988bad:
989	m_freem(m);
990	goto done;
991}
992
993void
994in_delayed_cksum(struct mbuf *m)
995{
996	struct ip *ip;
997	u_short csum, offset;
998
999	ip = mtod(m, struct ip *);
1000	offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1001	csum = in_cksum_skip(m, ip->ip_len, offset);
1002	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1003		csum = 0xffff;
1004	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1005
1006	if (offset + sizeof(u_short) > m->m_len) {
1007		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1008		    m->m_len, offset, ip->ip_p);
1009		/*
1010		 * XXX
1011		 * this shouldn't happen, but if it does, the
1012		 * correct behavior may be to insert the checksum
1013		 * in the existing chain instead of rearranging it.
1014		 */
1015		m = m_pullup(m, offset + sizeof(u_short));
1016	}
1017	*(u_short *)(m->m_data + offset) = csum;
1018}
1019
1020/*
1021 * Insert IP options into preformed packet.
1022 * Adjust IP destination as required for IP source routing,
1023 * as indicated by a non-zero in_addr at the start of the options.
1024 *
1025 * XXX This routine assumes that the packet has no options in place.
1026 */
1027static struct mbuf *
1028ip_insertoptions(m, opt, phlen)
1029	register struct mbuf *m;
1030	struct mbuf *opt;
1031	int *phlen;
1032{
1033	register struct ipoption *p = mtod(opt, struct ipoption *);
1034	struct mbuf *n;
1035	register struct ip *ip = mtod(m, struct ip *);
1036	unsigned optlen;
1037
1038	optlen = opt->m_len - sizeof(p->ipopt_dst);
1039	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
1040		return (m);		/* XXX should fail */
1041	if (p->ipopt_dst.s_addr)
1042		ip->ip_dst = p->ipopt_dst;
1043	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1044		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1045		if (n == 0)
1046			return (m);
1047		n->m_pkthdr.rcvif = (struct ifnet *)0;
1048		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1049		m->m_len -= sizeof(struct ip);
1050		m->m_data += sizeof(struct ip);
1051		n->m_next = m;
1052		m = n;
1053		m->m_len = optlen + sizeof(struct ip);
1054		m->m_data += max_linkhdr;
1055		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1056	} else {
1057		m->m_data -= optlen;
1058		m->m_len += optlen;
1059		m->m_pkthdr.len += optlen;
1060		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1061	}
1062	ip = mtod(m, struct ip *);
1063	bcopy(p->ipopt_list, ip + 1, optlen);
1064	*phlen = sizeof(struct ip) + optlen;
1065	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1066	ip->ip_len += optlen;
1067	return (m);
1068}
1069
1070/*
1071 * Copy options from ip to jp,
1072 * omitting those not copied during fragmentation.
1073 */
1074int
1075ip_optcopy(ip, jp)
1076	struct ip *ip, *jp;
1077{
1078	register u_char *cp, *dp;
1079	int opt, optlen, cnt;
1080
1081	cp = (u_char *)(ip + 1);
1082	dp = (u_char *)(jp + 1);
1083	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1084	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1085		opt = cp[0];
1086		if (opt == IPOPT_EOL)
1087			break;
1088		if (opt == IPOPT_NOP) {
1089			/* Preserve for IP mcast tunnel's LSRR alignment. */
1090			*dp++ = IPOPT_NOP;
1091			optlen = 1;
1092			continue;
1093		}
1094#ifdef DIAGNOSTIC
1095		if (cnt < IPOPT_OLEN + sizeof(*cp))
1096			panic("malformed IPv4 option passed to ip_optcopy");
1097#endif
1098		optlen = cp[IPOPT_OLEN];
1099#ifdef DIAGNOSTIC
1100		if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1101			panic("malformed IPv4 option passed to ip_optcopy");
1102#endif
1103		/* bogus lengths should have been caught by ip_dooptions */
1104		if (optlen > cnt)
1105			optlen = cnt;
1106		if (IPOPT_COPIED(opt)) {
1107			bcopy(cp, dp, optlen);
1108			dp += optlen;
1109		}
1110	}
1111	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1112		*dp++ = IPOPT_EOL;
1113	return (optlen);
1114}
1115
1116/*
1117 * IP socket option processing.
1118 */
1119int
1120ip_ctloutput(so, sopt)
1121	struct socket *so;
1122	struct sockopt *sopt;
1123{
1124	struct	inpcb *inp = sotoinpcb(so);
1125	int	error, optval;
1126
1127	error = optval = 0;
1128	if (sopt->sopt_level != IPPROTO_IP) {
1129		return (EINVAL);
1130	}
1131
1132	switch (sopt->sopt_dir) {
1133	case SOPT_SET:
1134		switch (sopt->sopt_name) {
1135		case IP_OPTIONS:
1136#ifdef notyet
1137		case IP_RETOPTS:
1138#endif
1139		{
1140			struct mbuf *m;
1141			if (sopt->sopt_valsize > MLEN) {
1142				error = EMSGSIZE;
1143				break;
1144			}
1145			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1146			if (m == 0) {
1147				error = ENOBUFS;
1148				break;
1149			}
1150			m->m_len = sopt->sopt_valsize;
1151			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1152					    m->m_len);
1153
1154			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1155					   m));
1156		}
1157
1158		case IP_TOS:
1159		case IP_TTL:
1160		case IP_RECVOPTS:
1161		case IP_RECVRETOPTS:
1162		case IP_RECVDSTADDR:
1163		case IP_RECVIF:
1164		case IP_FAITH:
1165			error = sooptcopyin(sopt, &optval, sizeof optval,
1166					    sizeof optval);
1167			if (error)
1168				break;
1169
1170			switch (sopt->sopt_name) {
1171			case IP_TOS:
1172				inp->inp_ip_tos = optval;
1173				break;
1174
1175			case IP_TTL:
1176				inp->inp_ip_ttl = optval;
1177				break;
1178#define	OPTSET(bit) \
1179	if (optval) \
1180		inp->inp_flags |= bit; \
1181	else \
1182		inp->inp_flags &= ~bit;
1183
1184			case IP_RECVOPTS:
1185				OPTSET(INP_RECVOPTS);
1186				break;
1187
1188			case IP_RECVRETOPTS:
1189				OPTSET(INP_RECVRETOPTS);
1190				break;
1191
1192			case IP_RECVDSTADDR:
1193				OPTSET(INP_RECVDSTADDR);
1194				break;
1195
1196			case IP_RECVIF:
1197				OPTSET(INP_RECVIF);
1198				break;
1199
1200			case IP_FAITH:
1201				OPTSET(INP_FAITH);
1202				break;
1203			}
1204			break;
1205#undef OPTSET
1206
1207		case IP_MULTICAST_IF:
1208		case IP_MULTICAST_VIF:
1209		case IP_MULTICAST_TTL:
1210		case IP_MULTICAST_LOOP:
1211		case IP_ADD_MEMBERSHIP:
1212		case IP_DROP_MEMBERSHIP:
1213			error = ip_setmoptions(sopt, &inp->inp_moptions);
1214			break;
1215
1216		case IP_PORTRANGE:
1217			error = sooptcopyin(sopt, &optval, sizeof optval,
1218					    sizeof optval);
1219			if (error)
1220				break;
1221
1222			switch (optval) {
1223			case IP_PORTRANGE_DEFAULT:
1224				inp->inp_flags &= ~(INP_LOWPORT);
1225				inp->inp_flags &= ~(INP_HIGHPORT);
1226				break;
1227
1228			case IP_PORTRANGE_HIGH:
1229				inp->inp_flags &= ~(INP_LOWPORT);
1230				inp->inp_flags |= INP_HIGHPORT;
1231				break;
1232
1233			case IP_PORTRANGE_LOW:
1234				inp->inp_flags &= ~(INP_HIGHPORT);
1235				inp->inp_flags |= INP_LOWPORT;
1236				break;
1237
1238			default:
1239				error = EINVAL;
1240				break;
1241			}
1242			break;
1243
1244#ifdef IPSEC
1245		case IP_IPSEC_POLICY:
1246		{
1247			caddr_t req;
1248			size_t len = 0;
1249			int priv;
1250			struct mbuf *m;
1251			int optname;
1252
1253			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1254				break;
1255			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1256				break;
1257			priv = (sopt->sopt_td != NULL &&
1258				suser_td(sopt->sopt_td) != 0) ? 0 : 1;
1259			req = mtod(m, caddr_t);
1260			len = m->m_len;
1261			optname = sopt->sopt_name;
1262			error = ipsec4_set_policy(inp, optname, req, len, priv);
1263			m_freem(m);
1264			break;
1265		}
1266#endif /*IPSEC*/
1267
1268		default:
1269			error = ENOPROTOOPT;
1270			break;
1271		}
1272		break;
1273
1274	case SOPT_GET:
1275		switch (sopt->sopt_name) {
1276		case IP_OPTIONS:
1277		case IP_RETOPTS:
1278			if (inp->inp_options)
1279				error = sooptcopyout(sopt,
1280						     mtod(inp->inp_options,
1281							  char *),
1282						     inp->inp_options->m_len);
1283			else
1284				sopt->sopt_valsize = 0;
1285			break;
1286
1287		case IP_TOS:
1288		case IP_TTL:
1289		case IP_RECVOPTS:
1290		case IP_RECVRETOPTS:
1291		case IP_RECVDSTADDR:
1292		case IP_RECVIF:
1293		case IP_PORTRANGE:
1294		case IP_FAITH:
1295			switch (sopt->sopt_name) {
1296
1297			case IP_TOS:
1298				optval = inp->inp_ip_tos;
1299				break;
1300
1301			case IP_TTL:
1302				optval = inp->inp_ip_ttl;
1303				break;
1304
1305#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1306
1307			case IP_RECVOPTS:
1308				optval = OPTBIT(INP_RECVOPTS);
1309				break;
1310
1311			case IP_RECVRETOPTS:
1312				optval = OPTBIT(INP_RECVRETOPTS);
1313				break;
1314
1315			case IP_RECVDSTADDR:
1316				optval = OPTBIT(INP_RECVDSTADDR);
1317				break;
1318
1319			case IP_RECVIF:
1320				optval = OPTBIT(INP_RECVIF);
1321				break;
1322
1323			case IP_PORTRANGE:
1324				if (inp->inp_flags & INP_HIGHPORT)
1325					optval = IP_PORTRANGE_HIGH;
1326				else if (inp->inp_flags & INP_LOWPORT)
1327					optval = IP_PORTRANGE_LOW;
1328				else
1329					optval = 0;
1330				break;
1331
1332			case IP_FAITH:
1333				optval = OPTBIT(INP_FAITH);
1334				break;
1335			}
1336			error = sooptcopyout(sopt, &optval, sizeof optval);
1337			break;
1338
1339		case IP_MULTICAST_IF:
1340		case IP_MULTICAST_VIF:
1341		case IP_MULTICAST_TTL:
1342		case IP_MULTICAST_LOOP:
1343		case IP_ADD_MEMBERSHIP:
1344		case IP_DROP_MEMBERSHIP:
1345			error = ip_getmoptions(sopt, inp->inp_moptions);
1346			break;
1347
1348#ifdef IPSEC
1349		case IP_IPSEC_POLICY:
1350		{
1351			struct mbuf *m = NULL;
1352			caddr_t req = NULL;
1353			size_t len = 0;
1354
1355			if (m != 0) {
1356				req = mtod(m, caddr_t);
1357				len = m->m_len;
1358			}
1359			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1360			if (error == 0)
1361				error = soopt_mcopyout(sopt, m); /* XXX */
1362			if (error == 0)
1363				m_freem(m);
1364			break;
1365		}
1366#endif /*IPSEC*/
1367
1368		default:
1369			error = ENOPROTOOPT;
1370			break;
1371		}
1372		break;
1373	}
1374	return (error);
1375}
1376
1377/*
1378 * Set up IP options in pcb for insertion in output packets.
1379 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1380 * with destination address if source routed.
1381 */
1382static int
1383ip_pcbopts(optname, pcbopt, m)
1384	int optname;
1385	struct mbuf **pcbopt;
1386	register struct mbuf *m;
1387{
1388	register int cnt, optlen;
1389	register u_char *cp;
1390	u_char opt;
1391
1392	/* turn off any old options */
1393	if (*pcbopt)
1394		(void)m_free(*pcbopt);
1395	*pcbopt = 0;
1396	if (m == (struct mbuf *)0 || m->m_len == 0) {
1397		/*
1398		 * Only turning off any previous options.
1399		 */
1400		if (m)
1401			(void)m_free(m);
1402		return (0);
1403	}
1404
1405	if (m->m_len % sizeof(int32_t))
1406		goto bad;
1407	/*
1408	 * IP first-hop destination address will be stored before
1409	 * actual options; move other options back
1410	 * and clear it when none present.
1411	 */
1412	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1413		goto bad;
1414	cnt = m->m_len;
1415	m->m_len += sizeof(struct in_addr);
1416	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1417	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1418	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1419
1420	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1421		opt = cp[IPOPT_OPTVAL];
1422		if (opt == IPOPT_EOL)
1423			break;
1424		if (opt == IPOPT_NOP)
1425			optlen = 1;
1426		else {
1427			if (cnt < IPOPT_OLEN + sizeof(*cp))
1428				goto bad;
1429			optlen = cp[IPOPT_OLEN];
1430			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1431				goto bad;
1432		}
1433		switch (opt) {
1434
1435		default:
1436			break;
1437
1438		case IPOPT_LSRR:
1439		case IPOPT_SSRR:
1440			/*
1441			 * user process specifies route as:
1442			 *	->A->B->C->D
1443			 * D must be our final destination (but we can't
1444			 * check that since we may not have connected yet).
1445			 * A is first hop destination, which doesn't appear in
1446			 * actual IP option, but is stored before the options.
1447			 */
1448			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1449				goto bad;
1450			m->m_len -= sizeof(struct in_addr);
1451			cnt -= sizeof(struct in_addr);
1452			optlen -= sizeof(struct in_addr);
1453			cp[IPOPT_OLEN] = optlen;
1454			/*
1455			 * Move first hop before start of options.
1456			 */
1457			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1458			    sizeof(struct in_addr));
1459			/*
1460			 * Then copy rest of options back
1461			 * to close up the deleted entry.
1462			 */
1463			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1464			    sizeof(struct in_addr)),
1465			    (caddr_t)&cp[IPOPT_OFFSET+1],
1466			    (unsigned)cnt + sizeof(struct in_addr));
1467			break;
1468		}
1469	}
1470	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1471		goto bad;
1472	*pcbopt = m;
1473	return (0);
1474
1475bad:
1476	(void)m_free(m);
1477	return (EINVAL);
1478}
1479
1480/*
1481 * XXX
1482 * The whole multicast option thing needs to be re-thought.
1483 * Several of these options are equally applicable to non-multicast
1484 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1485 * standard option (IP_TTL).
1486 */
1487
1488/*
1489 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1490 */
1491static struct ifnet *
1492ip_multicast_if(a, ifindexp)
1493	struct in_addr *a;
1494	int *ifindexp;
1495{
1496	int ifindex;
1497	struct ifnet *ifp;
1498
1499	if (ifindexp)
1500		*ifindexp = 0;
1501	if (ntohl(a->s_addr) >> 24 == 0) {
1502		ifindex = ntohl(a->s_addr) & 0xffffff;
1503		if (ifindex < 0 || if_index < ifindex)
1504			return NULL;
1505		ifp = ifnet_byindex(ifindex);
1506		if (ifindexp)
1507			*ifindexp = ifindex;
1508	} else {
1509		INADDR_TO_IFP(*a, ifp);
1510	}
1511	return ifp;
1512}
1513
1514/*
1515 * Set the IP multicast options in response to user setsockopt().
1516 */
1517static int
1518ip_setmoptions(sopt, imop)
1519	struct sockopt *sopt;
1520	struct ip_moptions **imop;
1521{
1522	int error = 0;
1523	int i;
1524	struct in_addr addr;
1525	struct ip_mreq mreq;
1526	struct ifnet *ifp;
1527	struct ip_moptions *imo = *imop;
1528	struct route ro;
1529	struct sockaddr_in *dst;
1530	int ifindex;
1531	int s;
1532
1533	if (imo == NULL) {
1534		/*
1535		 * No multicast option buffer attached to the pcb;
1536		 * allocate one and initialize to default values.
1537		 */
1538		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1539		    M_WAITOK);
1540
1541		if (imo == NULL)
1542			return (ENOBUFS);
1543		*imop = imo;
1544		imo->imo_multicast_ifp = NULL;
1545		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1546		imo->imo_multicast_vif = -1;
1547		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1548		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1549		imo->imo_num_memberships = 0;
1550	}
1551
1552	switch (sopt->sopt_name) {
1553	/* store an index number for the vif you wanna use in the send */
1554	case IP_MULTICAST_VIF:
1555		if (legal_vif_num == 0) {
1556			error = EOPNOTSUPP;
1557			break;
1558		}
1559		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1560		if (error)
1561			break;
1562		if (!legal_vif_num(i) && (i != -1)) {
1563			error = EINVAL;
1564			break;
1565		}
1566		imo->imo_multicast_vif = i;
1567		break;
1568
1569	case IP_MULTICAST_IF:
1570		/*
1571		 * Select the interface for outgoing multicast packets.
1572		 */
1573		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1574		if (error)
1575			break;
1576		/*
1577		 * INADDR_ANY is used to remove a previous selection.
1578		 * When no interface is selected, a default one is
1579		 * chosen every time a multicast packet is sent.
1580		 */
1581		if (addr.s_addr == INADDR_ANY) {
1582			imo->imo_multicast_ifp = NULL;
1583			break;
1584		}
1585		/*
1586		 * The selected interface is identified by its local
1587		 * IP address.  Find the interface and confirm that
1588		 * it supports multicasting.
1589		 */
1590		s = splimp();
1591		ifp = ip_multicast_if(&addr, &ifindex);
1592		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1593			splx(s);
1594			error = EADDRNOTAVAIL;
1595			break;
1596		}
1597		imo->imo_multicast_ifp = ifp;
1598		if (ifindex)
1599			imo->imo_multicast_addr = addr;
1600		else
1601			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1602		splx(s);
1603		break;
1604
1605	case IP_MULTICAST_TTL:
1606		/*
1607		 * Set the IP time-to-live for outgoing multicast packets.
1608		 * The original multicast API required a char argument,
1609		 * which is inconsistent with the rest of the socket API.
1610		 * We allow either a char or an int.
1611		 */
1612		if (sopt->sopt_valsize == 1) {
1613			u_char ttl;
1614			error = sooptcopyin(sopt, &ttl, 1, 1);
1615			if (error)
1616				break;
1617			imo->imo_multicast_ttl = ttl;
1618		} else {
1619			u_int ttl;
1620			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1621					    sizeof ttl);
1622			if (error)
1623				break;
1624			if (ttl > 255)
1625				error = EINVAL;
1626			else
1627				imo->imo_multicast_ttl = ttl;
1628		}
1629		break;
1630
1631	case IP_MULTICAST_LOOP:
1632		/*
1633		 * Set the loopback flag for outgoing multicast packets.
1634		 * Must be zero or one.  The original multicast API required a
1635		 * char argument, which is inconsistent with the rest
1636		 * of the socket API.  We allow either a char or an int.
1637		 */
1638		if (sopt->sopt_valsize == 1) {
1639			u_char loop;
1640			error = sooptcopyin(sopt, &loop, 1, 1);
1641			if (error)
1642				break;
1643			imo->imo_multicast_loop = !!loop;
1644		} else {
1645			u_int loop;
1646			error = sooptcopyin(sopt, &loop, sizeof loop,
1647					    sizeof loop);
1648			if (error)
1649				break;
1650			imo->imo_multicast_loop = !!loop;
1651		}
1652		break;
1653
1654	case IP_ADD_MEMBERSHIP:
1655		/*
1656		 * Add a multicast group membership.
1657		 * Group must be a valid IP multicast address.
1658		 */
1659		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1660		if (error)
1661			break;
1662
1663		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1664			error = EINVAL;
1665			break;
1666		}
1667		s = splimp();
1668		/*
1669		 * If no interface address was provided, use the interface of
1670		 * the route to the given multicast address.
1671		 */
1672		if (mreq.imr_interface.s_addr == INADDR_ANY) {
1673			bzero((caddr_t)&ro, sizeof(ro));
1674			dst = (struct sockaddr_in *)&ro.ro_dst;
1675			dst->sin_len = sizeof(*dst);
1676			dst->sin_family = AF_INET;
1677			dst->sin_addr = mreq.imr_multiaddr;
1678			rtalloc(&ro);
1679			if (ro.ro_rt == NULL) {
1680				error = EADDRNOTAVAIL;
1681				splx(s);
1682				break;
1683			}
1684			ifp = ro.ro_rt->rt_ifp;
1685			rtfree(ro.ro_rt);
1686		}
1687		else {
1688			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1689		}
1690
1691		/*
1692		 * See if we found an interface, and confirm that it
1693		 * supports multicast.
1694		 */
1695		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1696			error = EADDRNOTAVAIL;
1697			splx(s);
1698			break;
1699		}
1700		/*
1701		 * See if the membership already exists or if all the
1702		 * membership slots are full.
1703		 */
1704		for (i = 0; i < imo->imo_num_memberships; ++i) {
1705			if (imo->imo_membership[i]->inm_ifp == ifp &&
1706			    imo->imo_membership[i]->inm_addr.s_addr
1707						== mreq.imr_multiaddr.s_addr)
1708				break;
1709		}
1710		if (i < imo->imo_num_memberships) {
1711			error = EADDRINUSE;
1712			splx(s);
1713			break;
1714		}
1715		if (i == IP_MAX_MEMBERSHIPS) {
1716			error = ETOOMANYREFS;
1717			splx(s);
1718			break;
1719		}
1720		/*
1721		 * Everything looks good; add a new record to the multicast
1722		 * address list for the given interface.
1723		 */
1724		if ((imo->imo_membership[i] =
1725		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1726			error = ENOBUFS;
1727			splx(s);
1728			break;
1729		}
1730		++imo->imo_num_memberships;
1731		splx(s);
1732		break;
1733
1734	case IP_DROP_MEMBERSHIP:
1735		/*
1736		 * Drop a multicast group membership.
1737		 * Group must be a valid IP multicast address.
1738		 */
1739		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1740		if (error)
1741			break;
1742
1743		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1744			error = EINVAL;
1745			break;
1746		}
1747
1748		s = splimp();
1749		/*
1750		 * If an interface address was specified, get a pointer
1751		 * to its ifnet structure.
1752		 */
1753		if (mreq.imr_interface.s_addr == INADDR_ANY)
1754			ifp = NULL;
1755		else {
1756			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1757			if (ifp == NULL) {
1758				error = EADDRNOTAVAIL;
1759				splx(s);
1760				break;
1761			}
1762		}
1763		/*
1764		 * Find the membership in the membership array.
1765		 */
1766		for (i = 0; i < imo->imo_num_memberships; ++i) {
1767			if ((ifp == NULL ||
1768			     imo->imo_membership[i]->inm_ifp == ifp) &&
1769			     imo->imo_membership[i]->inm_addr.s_addr ==
1770			     mreq.imr_multiaddr.s_addr)
1771				break;
1772		}
1773		if (i == imo->imo_num_memberships) {
1774			error = EADDRNOTAVAIL;
1775			splx(s);
1776			break;
1777		}
1778		/*
1779		 * Give up the multicast address record to which the
1780		 * membership points.
1781		 */
1782		in_delmulti(imo->imo_membership[i]);
1783		/*
1784		 * Remove the gap in the membership array.
1785		 */
1786		for (++i; i < imo->imo_num_memberships; ++i)
1787			imo->imo_membership[i-1] = imo->imo_membership[i];
1788		--imo->imo_num_memberships;
1789		splx(s);
1790		break;
1791
1792	default:
1793		error = EOPNOTSUPP;
1794		break;
1795	}
1796
1797	/*
1798	 * If all options have default values, no need to keep the mbuf.
1799	 */
1800	if (imo->imo_multicast_ifp == NULL &&
1801	    imo->imo_multicast_vif == -1 &&
1802	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1803	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1804	    imo->imo_num_memberships == 0) {
1805		free(*imop, M_IPMOPTS);
1806		*imop = NULL;
1807	}
1808
1809	return (error);
1810}
1811
1812/*
1813 * Return the IP multicast options in response to user getsockopt().
1814 */
1815static int
1816ip_getmoptions(sopt, imo)
1817	struct sockopt *sopt;
1818	register struct ip_moptions *imo;
1819{
1820	struct in_addr addr;
1821	struct in_ifaddr *ia;
1822	int error, optval;
1823	u_char coptval;
1824
1825	error = 0;
1826	switch (sopt->sopt_name) {
1827	case IP_MULTICAST_VIF:
1828		if (imo != NULL)
1829			optval = imo->imo_multicast_vif;
1830		else
1831			optval = -1;
1832		error = sooptcopyout(sopt, &optval, sizeof optval);
1833		break;
1834
1835	case IP_MULTICAST_IF:
1836		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1837			addr.s_addr = INADDR_ANY;
1838		else if (imo->imo_multicast_addr.s_addr) {
1839			/* return the value user has set */
1840			addr = imo->imo_multicast_addr;
1841		} else {
1842			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1843			addr.s_addr = (ia == NULL) ? INADDR_ANY
1844				: IA_SIN(ia)->sin_addr.s_addr;
1845		}
1846		error = sooptcopyout(sopt, &addr, sizeof addr);
1847		break;
1848
1849	case IP_MULTICAST_TTL:
1850		if (imo == 0)
1851			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1852		else
1853			optval = coptval = imo->imo_multicast_ttl;
1854		if (sopt->sopt_valsize == 1)
1855			error = sooptcopyout(sopt, &coptval, 1);
1856		else
1857			error = sooptcopyout(sopt, &optval, sizeof optval);
1858		break;
1859
1860	case IP_MULTICAST_LOOP:
1861		if (imo == 0)
1862			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1863		else
1864			optval = coptval = imo->imo_multicast_loop;
1865		if (sopt->sopt_valsize == 1)
1866			error = sooptcopyout(sopt, &coptval, 1);
1867		else
1868			error = sooptcopyout(sopt, &optval, sizeof optval);
1869		break;
1870
1871	default:
1872		error = ENOPROTOOPT;
1873		break;
1874	}
1875	return (error);
1876}
1877
1878/*
1879 * Discard the IP multicast options.
1880 */
1881void
1882ip_freemoptions(imo)
1883	register struct ip_moptions *imo;
1884{
1885	register int i;
1886
1887	if (imo != NULL) {
1888		for (i = 0; i < imo->imo_num_memberships; ++i)
1889			in_delmulti(imo->imo_membership[i]);
1890		free(imo, M_IPMOPTS);
1891	}
1892}
1893
1894/*
1895 * Routine called from ip_output() to loop back a copy of an IP multicast
1896 * packet to the input queue of a specified interface.  Note that this
1897 * calls the output routine of the loopback "driver", but with an interface
1898 * pointer that might NOT be a loopback interface -- evil, but easier than
1899 * replicating that code here.
1900 */
1901static void
1902ip_mloopback(ifp, m, dst, hlen)
1903	struct ifnet *ifp;
1904	register struct mbuf *m;
1905	register struct sockaddr_in *dst;
1906	int hlen;
1907{
1908	register struct ip *ip;
1909	struct mbuf *copym;
1910
1911	copym = m_copy(m, 0, M_COPYALL);
1912	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1913		copym = m_pullup(copym, hlen);
1914	if (copym != NULL) {
1915		/*
1916		 * We don't bother to fragment if the IP length is greater
1917		 * than the interface's MTU.  Can this possibly matter?
1918		 */
1919		ip = mtod(copym, struct ip *);
1920		HTONS(ip->ip_len);
1921		HTONS(ip->ip_off);
1922		ip->ip_sum = 0;
1923		if (ip->ip_vhl == IP_VHL_BORING) {
1924			ip->ip_sum = in_cksum_hdr(ip);
1925		} else {
1926			ip->ip_sum = in_cksum(copym, hlen);
1927		}
1928		/*
1929		 * NB:
1930		 * It's not clear whether there are any lingering
1931		 * reentrancy problems in other areas which might
1932		 * be exposed by using ip_input directly (in
1933		 * particular, everything which modifies the packet
1934		 * in-place).  Yet another option is using the
1935		 * protosw directly to deliver the looped back
1936		 * packet.  For the moment, we'll err on the side
1937		 * of safety by using if_simloop().
1938		 */
1939#if 1 /* XXX */
1940		if (dst->sin_family != AF_INET) {
1941			printf("ip_mloopback: bad address family %d\n",
1942						dst->sin_family);
1943			dst->sin_family = AF_INET;
1944		}
1945#endif
1946
1947#ifdef notdef
1948		copym->m_pkthdr.rcvif = ifp;
1949		ip_input(copym);
1950#else
1951		/* if the checksum hasn't been computed, mark it as valid */
1952		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1953			copym->m_pkthdr.csum_flags |=
1954			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1955			copym->m_pkthdr.csum_data = 0xffff;
1956		}
1957		if_simloop(ifp, copym, dst->sin_family, 0);
1958#endif
1959	}
1960}
1961