Home | History | Annotate | Line # | Download | only in netinet
ip_output.c revision 1.109
      1 /*	$NetBSD: ip_output.c,v 1.109 2003/08/15 03:42:03 jonathan Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Public Access Networks Corporation ("Panix").  It was developed under
     38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1990, 1993
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. Neither the name of the University nor the names of its contributors
     82  *    may be used to endorse or promote products derived from this software
     83  *    without specific prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
     98  */
     99 
    100 #include <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.109 2003/08/15 03:42:03 jonathan Exp $");
    102 
    103 #include "opt_pfil_hooks.h"
    104 #include "opt_ipsec.h"
    105 #include "opt_mrouting.h"
    106 
    107 #include <sys/param.h>
    108 #include <sys/malloc.h>
    109 #include <sys/mbuf.h>
    110 #include <sys/errno.h>
    111 #include <sys/protosw.h>
    112 #include <sys/socket.h>
    113 #include <sys/socketvar.h>
    114 #include <sys/systm.h>
    115 #include <sys/proc.h>
    116 
    117 #include <net/if.h>
    118 #include <net/route.h>
    119 #include <net/pfil.h>
    120 
    121 #include <netinet/in.h>
    122 #include <netinet/in_systm.h>
    123 #include <netinet/ip.h>
    124 #include <netinet/in_pcb.h>
    125 #include <netinet/in_var.h>
    126 #include <netinet/ip_var.h>
    127 
    128 #ifdef MROUTING
    129 #include <netinet/ip_mroute.h>
    130 #endif
    131 
    132 #include <machine/stdarg.h>
    133 
    134 #ifdef IPSEC
    135 #include <netinet6/ipsec.h>
    136 #include <netkey/key.h>
    137 #include <netkey/key_debug.h>
    138 #endif /*IPSEC*/
    139 
    140 #ifdef FAST_IPSEC
    141 #include <netipsec/ipsec.h>
    142 #include <netipsec/key.h>
    143 #include <netipsec/xform.h>
    144 #endif	/* FAST_IPSEC*/
    145 
    146 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
    147 static struct ifnet *ip_multicast_if __P((struct in_addr *, int *));
    148 static void ip_mloopback
    149 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
    150 
    151 #ifdef PFIL_HOOKS
    152 extern struct pfil_head inet_pfil_hook;			/* XXX */
    153 #endif
    154 
    155 /*
    156  * IP output.  The packet in mbuf chain m contains a skeletal IP
    157  * header (with len, off, ttl, proto, tos, src, dst).
    158  * The mbuf chain containing the packet will be freed.
    159  * The mbuf opt, if present, will not be freed.
    160  */
    161 int
    162 #if __STDC__
    163 ip_output(struct mbuf *m0, ...)
    164 #else
    165 ip_output(m0, va_alist)
    166 	struct mbuf *m0;
    167 	va_dcl
    168 #endif
    169 {
    170 	struct ip *ip, *mhip;
    171 	struct ifnet *ifp;
    172 	struct mbuf *m = m0;
    173 	int hlen = sizeof (struct ip);
    174 	int len, off, error = 0;
    175 	struct route iproute;
    176 	struct sockaddr_in *dst;
    177 	struct in_ifaddr *ia;
    178 	struct mbuf *opt;
    179 	struct route *ro;
    180 	int flags, sw_csum;
    181 	int *mtu_p;
    182 	u_long mtu;
    183 	struct ip_moptions *imo;
    184 	struct inpcb *inp;
    185 	va_list ap;
    186 #ifdef IPSEC
    187 	struct socket *so;
    188 	struct secpolicy *sp = NULL;
    189 #endif /*IPSEC*/
    190 #ifdef FAST_IPSEC
    191 	struct m_tag *mtag;
    192 	struct secpolicy *sp = NULL;
    193 	struct tdb_ident *tdbi;
    194 	int s;
    195 #endif
    196 	u_int16_t ip_len;
    197 
    198 	len = 0;
    199 	va_start(ap, m0);
    200 	opt = va_arg(ap, struct mbuf *);
    201 	ro = va_arg(ap, struct route *);
    202 	flags = va_arg(ap, int);
    203 	imo = va_arg(ap, struct ip_moptions *);
    204 	inp = va_arg(ap, struct inpcb *);
    205 	if (flags & IP_RETURNMTU)
    206 		mtu_p = va_arg(ap, int *);
    207 	else
    208 		mtu_p = NULL;
    209 	va_end(ap);
    210 
    211 	MCLAIM(m, &ip_tx_mowner);
    212 #ifdef IPSEC	/* XXX so = ((inp == NULL) ? NULL : inp->inp_socket; */
    213 	so = ipsec_getsocket(m);
    214 	(void)ipsec_setsocket(m, NULL);
    215 #endif /*IPSEC*/
    216 
    217 #ifdef	DIAGNOSTIC
    218 	if ((m->m_flags & M_PKTHDR) == 0)
    219 		panic("ip_output no HDR");
    220 #endif
    221 	if (opt) {
    222 		m = ip_insertoptions(m, opt, &len);
    223 		if (len >= sizeof(struct ip))
    224 			hlen = len;
    225 	}
    226 	ip = mtod(m, struct ip *);
    227 	/*
    228 	 * Fill in IP header.
    229 	 */
    230 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
    231 		ip->ip_v = IPVERSION;
    232 		ip->ip_off = htons(0);
    233 		ip->ip_id = htons(ip_id++);
    234 		ip->ip_hl = hlen >> 2;
    235 		ipstat.ips_localout++;
    236 	} else {
    237 		hlen = ip->ip_hl << 2;
    238 	}
    239 	/*
    240 	 * Route packet.
    241 	 */
    242 	if (ro == 0) {
    243 		ro = &iproute;
    244 		bzero((caddr_t)ro, sizeof (*ro));
    245 	}
    246 	dst = satosin(&ro->ro_dst);
    247 	/*
    248 	 * If there is a cached route,
    249 	 * check that it is to the same destination
    250 	 * and is still up.  If not, free it and try again.
    251 	 * The address family should also be checked in case of sharing the
    252 	 * cache with IPv6.
    253 	 */
    254 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    255 	    dst->sin_family != AF_INET ||
    256 	    !in_hosteq(dst->sin_addr, ip->ip_dst))) {
    257 		RTFREE(ro->ro_rt);
    258 		ro->ro_rt = (struct rtentry *)0;
    259 	}
    260 	if (ro->ro_rt == 0) {
    261 		bzero(dst, sizeof(*dst));
    262 		dst->sin_family = AF_INET;
    263 		dst->sin_len = sizeof(*dst);
    264 		dst->sin_addr = ip->ip_dst;
    265 	}
    266 	/*
    267 	 * If routing to interface only,
    268 	 * short circuit routing lookup.
    269 	 */
    270 	if (flags & IP_ROUTETOIF) {
    271 		if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) {
    272 			ipstat.ips_noroute++;
    273 			error = ENETUNREACH;
    274 			goto bad;
    275 		}
    276 		ifp = ia->ia_ifp;
    277 		mtu = ifp->if_mtu;
    278 		ip->ip_ttl = 1;
    279 	} else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
    280 	    ip->ip_dst.s_addr == INADDR_BROADCAST) &&
    281 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
    282 		ifp = imo->imo_multicast_ifp;
    283 		mtu = ifp->if_mtu;
    284 		IFP_TO_IA(ifp, ia);
    285 	} else {
    286 		if (ro->ro_rt == 0)
    287 			rtalloc(ro);
    288 		if (ro->ro_rt == 0) {
    289 			ipstat.ips_noroute++;
    290 			error = EHOSTUNREACH;
    291 			goto bad;
    292 		}
    293 		ia = ifatoia(ro->ro_rt->rt_ifa);
    294 		ifp = ro->ro_rt->rt_ifp;
    295 		if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0)
    296 			mtu = ifp->if_mtu;
    297 		ro->ro_rt->rt_use++;
    298 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
    299 			dst = satosin(ro->ro_rt->rt_gateway);
    300 	}
    301 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
    302 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
    303 		struct in_multi *inm;
    304 
    305 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
    306 			M_BCAST : M_MCAST;
    307 		/*
    308 		 * IP destination address is multicast.  Make sure "dst"
    309 		 * still points to the address in "ro".  (It may have been
    310 		 * changed to point to a gateway address, above.)
    311 		 */
    312 		dst = satosin(&ro->ro_dst);
    313 		/*
    314 		 * See if the caller provided any multicast options
    315 		 */
    316 		if (imo != NULL)
    317 			ip->ip_ttl = imo->imo_multicast_ttl;
    318 		else
    319 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
    320 
    321 		/*
    322 		 * if we don't know the outgoing ifp yet, we can't generate
    323 		 * output
    324 		 */
    325 		if (!ifp) {
    326 			ipstat.ips_noroute++;
    327 			error = ENETUNREACH;
    328 			goto bad;
    329 		}
    330 
    331 		/*
    332 		 * If the packet is multicast or broadcast, confirm that
    333 		 * the outgoing interface can transmit it.
    334 		 */
    335 		if (((m->m_flags & M_MCAST) &&
    336 		     (ifp->if_flags & IFF_MULTICAST) == 0) ||
    337 		    ((m->m_flags & M_BCAST) &&
    338 		     (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0))  {
    339 			ipstat.ips_noroute++;
    340 			error = ENETUNREACH;
    341 			goto bad;
    342 		}
    343 		/*
    344 		 * If source address not specified yet, use an address
    345 		 * of outgoing interface.
    346 		 */
    347 		if (in_nullhost(ip->ip_src)) {
    348 			struct in_ifaddr *ia;
    349 
    350 			IFP_TO_IA(ifp, ia);
    351 			if (!ia) {
    352 				error = EADDRNOTAVAIL;
    353 				goto bad;
    354 			}
    355 			ip->ip_src = ia->ia_addr.sin_addr;
    356 		}
    357 
    358 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
    359 		if (inm != NULL &&
    360 		   (imo == NULL || imo->imo_multicast_loop)) {
    361 			/*
    362 			 * If we belong to the destination multicast group
    363 			 * on the outgoing interface, and the caller did not
    364 			 * forbid loopback, loop back a copy.
    365 			 */
    366 			ip_mloopback(ifp, m, dst);
    367 		}
    368 #ifdef MROUTING
    369 		else {
    370 			/*
    371 			 * If we are acting as a multicast router, perform
    372 			 * multicast forwarding as if the packet had just
    373 			 * arrived on the interface to which we are about
    374 			 * to send.  The multicast forwarding function
    375 			 * recursively calls this function, using the
    376 			 * IP_FORWARDING flag to prevent infinite recursion.
    377 			 *
    378 			 * Multicasts that are looped back by ip_mloopback(),
    379 			 * above, will be forwarded by the ip_input() routine,
    380 			 * if necessary.
    381 			 */
    382 			extern struct socket *ip_mrouter;
    383 
    384 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
    385 				if (ip_mforward(m, ifp) != 0) {
    386 					m_freem(m);
    387 					goto done;
    388 				}
    389 			}
    390 		}
    391 #endif
    392 		/*
    393 		 * Multicasts with a time-to-live of zero may be looped-
    394 		 * back, above, but must not be transmitted on a network.
    395 		 * Also, multicasts addressed to the loopback interface
    396 		 * are not sent -- the above call to ip_mloopback() will
    397 		 * loop back a copy if this host actually belongs to the
    398 		 * destination group on the loopback interface.
    399 		 */
    400 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
    401 			m_freem(m);
    402 			goto done;
    403 		}
    404 
    405 		goto sendit;
    406 	}
    407 #ifndef notdef
    408 	/*
    409 	 * If source address not specified yet, use address
    410 	 * of outgoing interface.
    411 	 */
    412 	if (in_nullhost(ip->ip_src))
    413 		ip->ip_src = ia->ia_addr.sin_addr;
    414 #endif
    415 
    416 	/*
    417 	 * packets with Class-D address as source are not valid per
    418 	 * RFC 1112
    419 	 */
    420 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
    421 		ipstat.ips_odropped++;
    422 		error = EADDRNOTAVAIL;
    423 		goto bad;
    424 	}
    425 
    426 	/*
    427 	 * Look for broadcast address and
    428 	 * and verify user is allowed to send
    429 	 * such a packet.
    430 	 */
    431 	if (in_broadcast(dst->sin_addr, ifp)) {
    432 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    433 			error = EADDRNOTAVAIL;
    434 			goto bad;
    435 		}
    436 		if ((flags & IP_ALLOWBROADCAST) == 0) {
    437 			error = EACCES;
    438 			goto bad;
    439 		}
    440 		/* don't allow broadcast messages to be fragmented */
    441 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
    442 			error = EMSGSIZE;
    443 			goto bad;
    444 		}
    445 		m->m_flags |= M_BCAST;
    446 	} else
    447 		m->m_flags &= ~M_BCAST;
    448 
    449 sendit:
    450 	/*
    451 	 * If we're doing Path MTU Discovery, we need to set DF unless
    452 	 * the route's MTU is locked.
    453 	 */
    454 	if ((flags & IP_MTUDISC) != 0 && ro->ro_rt != NULL &&
    455 	    (ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
    456 		ip->ip_off |= htons(IP_DF);
    457 
    458 	/* Remember the current ip_len */
    459 	ip_len = ntohs(ip->ip_len);
    460 
    461 #ifdef IPSEC
    462 	/* get SP for this packet */
    463 	if (so == NULL)
    464 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
    465 	else
    466 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
    467 
    468 	if (sp == NULL) {
    469 		ipsecstat.out_inval++;
    470 		goto bad;
    471 	}
    472 
    473 	error = 0;
    474 
    475 	/* check policy */
    476 	switch (sp->policy) {
    477 	case IPSEC_POLICY_DISCARD:
    478 		/*
    479 		 * This packet is just discarded.
    480 		 */
    481 		ipsecstat.out_polvio++;
    482 		goto bad;
    483 
    484 	case IPSEC_POLICY_BYPASS:
    485 	case IPSEC_POLICY_NONE:
    486 		/* no need to do IPsec. */
    487 		goto skip_ipsec;
    488 
    489 	case IPSEC_POLICY_IPSEC:
    490 		if (sp->req == NULL) {
    491 			/* XXX should be panic ? */
    492 			printf("ip_output: No IPsec request specified.\n");
    493 			error = EINVAL;
    494 			goto bad;
    495 		}
    496 		break;
    497 
    498 	case IPSEC_POLICY_ENTRUST:
    499 	default:
    500 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
    501 	}
    502 
    503 	/*
    504 	 * ipsec4_output() expects ip_len and ip_off in network
    505 	 * order.  They have been set to network order above.
    506 	 */
    507 
    508     {
    509 	struct ipsec_output_state state;
    510 	bzero(&state, sizeof(state));
    511 	state.m = m;
    512 	if (flags & IP_ROUTETOIF) {
    513 		state.ro = &iproute;
    514 		bzero(&iproute, sizeof(iproute));
    515 	} else
    516 		state.ro = ro;
    517 	state.dst = (struct sockaddr *)dst;
    518 
    519 	/*
    520 	 * We can't defer the checksum of payload data if
    521 	 * we're about to encrypt/authenticate it.
    522 	 *
    523 	 * XXX When we support crypto offloading functions of
    524 	 * XXX network interfaces, we need to reconsider this,
    525 	 * XXX since it's likely that they'll support checksumming,
    526 	 * XXX as well.
    527 	 */
    528 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    529 		in_delayed_cksum(m);
    530 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    531 	}
    532 
    533 	error = ipsec4_output(&state, sp, flags);
    534 
    535 	m = state.m;
    536 	if (flags & IP_ROUTETOIF) {
    537 		/*
    538 		 * if we have tunnel mode SA, we may need to ignore
    539 		 * IP_ROUTETOIF.
    540 		 */
    541 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
    542 			flags &= ~IP_ROUTETOIF;
    543 			ro = state.ro;
    544 		}
    545 	} else
    546 		ro = state.ro;
    547 	dst = (struct sockaddr_in *)state.dst;
    548 	if (error) {
    549 		/* mbuf is already reclaimed in ipsec4_output. */
    550 		m0 = NULL;
    551 		switch (error) {
    552 		case EHOSTUNREACH:
    553 		case ENETUNREACH:
    554 		case EMSGSIZE:
    555 		case ENOBUFS:
    556 		case ENOMEM:
    557 			break;
    558 		default:
    559 			printf("ip4_output (ipsec): error code %d\n", error);
    560 			/*fall through*/
    561 		case ENOENT:
    562 			/* don't show these error codes to the user */
    563 			error = 0;
    564 			break;
    565 		}
    566 		goto bad;
    567 	}
    568 
    569 	/* be sure to update variables that are affected by ipsec4_output() */
    570 	ip = mtod(m, struct ip *);
    571 	hlen = ip->ip_hl << 2;
    572 	ip_len = ntohs(ip->ip_len);
    573 
    574 	if (ro->ro_rt == NULL) {
    575 		if ((flags & IP_ROUTETOIF) == 0) {
    576 			printf("ip_output: "
    577 				"can't update route after IPsec processing\n");
    578 			error = EHOSTUNREACH;	/*XXX*/
    579 			goto bad;
    580 		}
    581 	} else {
    582 		/* nobody uses ia beyond here */
    583 		if (state.encap)
    584 			ifp = ro->ro_rt->rt_ifp;
    585 	}
    586     }
    587 
    588 skip_ipsec:
    589 #endif /*IPSEC*/
    590 #ifdef FAST_IPSEC
    591 	/*
    592 	 * Check the security policy (SP) for the packet and, if
    593 	 * required, do IPsec-related processing.  There are two
    594 	 * cases here; the first time a packet is sent through
    595 	 * it will be untagged and handled by ipsec4_checkpolicy.
    596 	 * If the packet is resubmitted to ip_output (e.g. after
    597 	 * AH, ESP, etc. processing), there will be a tag to bypass
    598 	 * the lookup and related policy checking.
    599 	 */
    600 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
    601 	s = splsoftnet();
    602 	if (mtag != NULL) {
    603 		tdbi = (struct tdb_ident *)(mtag + 1);
    604 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
    605 		if (sp == NULL)
    606 			error = -EINVAL;	/* force silent drop */
    607 		m_tag_delete(m, mtag);
    608 	} else {
    609 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
    610 					&error, inp);
    611 	}
    612 	/*
    613 	 * There are four return cases:
    614 	 *    sp != NULL	 	    apply IPsec policy
    615 	 *    sp == NULL, error == 0	    no IPsec handling needed
    616 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
    617 	 *    sp == NULL, error != 0	    discard packet, report error
    618 	 */
    619 	if (sp != NULL) {
    620 		/* Loop detection, check if ipsec processing already done */
    621 		IPSEC_ASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
    622 		for (mtag = m_tag_first(m); mtag != NULL;
    623 		     mtag = m_tag_next(m, mtag)) {
    624 #ifdef MTAG_ABI_COMPAT
    625 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
    626 				continue;
    627 #endif
    628 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
    629 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
    630 				continue;
    631 			/*
    632 			 * Check if policy has an SA associated with it.
    633 			 * This can happen when an SP has yet to acquire
    634 			 * an SA; e.g. on first reference.  If it occurs,
    635 			 * then we let ipsec4_process_packet do its thing.
    636 			 */
    637 			if (sp->req->sav == NULL)
    638 				break;
    639 			tdbi = (struct tdb_ident *)(mtag + 1);
    640 			if (tdbi->spi == sp->req->sav->spi &&
    641 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
    642 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
    643 				 sizeof (union sockaddr_union)) == 0) {
    644 				/*
    645 				 * No IPsec processing is needed, free
    646 				 * reference to SP.
    647 				 *
    648 				 * NB: null pointer to avoid free at
    649 				 *     done: below.
    650 				 */
    651 				KEY_FREESP(&sp), sp = NULL;
    652 				splx(s);
    653 				goto spd_done;
    654 			}
    655 		}
    656 
    657 		/*
    658 		 * Do delayed checksums now because we send before
    659 		 * this is done in the normal processing path.
    660 		 */
    661 		if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    662 			in_delayed_cksum(m);
    663 			m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    664 		}
    665 
    666 #ifdef __FreeBSD__
    667 		ip->ip_len = htons(ip->ip_len);
    668 		ip->ip_off = htons(ip->ip_off);
    669 #endif
    670 
    671 		/* NB: callee frees mbuf */
    672 		error = ipsec4_process_packet(m, sp->req, flags, 0);
    673 		/*
    674 		 * Preserve KAME behaviour: ENOENT can be returned
    675 		 * when an SA acquire is in progress.  Don't propagate
    676 		 * this to user-level; it confuses applications.
    677 		 *
    678 		 * XXX this will go away when the SADB is redone.
    679 		 */
    680 		if (error == ENOENT)
    681 			error = 0;
    682 		splx(s);
    683 		goto done;
    684 	} else {
    685 		splx(s);
    686 
    687 		if (error != 0) {
    688 			/*
    689 			 * Hack: -EINVAL is used to signal that a packet
    690 			 * should be silently discarded.  This is typically
    691 			 * because we asked key management for an SA and
    692 			 * it was delayed (e.g. kicked up to IKE).
    693 			 */
    694 			if (error == -EINVAL)
    695 				error = 0;
    696 			goto bad;
    697 		} else {
    698 			/* No IPsec processing for this packet. */
    699 		}
    700 #ifdef notyet
    701 		/*
    702 		 * If deferred crypto processing is needed, check that
    703 		 * the interface supports it.
    704 		 */
    705 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
    706 		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
    707 			/* notify IPsec to do its own crypto */
    708 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
    709 			error = EHOSTUNREACH;
    710 			goto bad;
    711 		}
    712 #endif
    713 	}
    714 spd_done:
    715 #endif /* FAST_IPSEC */
    716 
    717 #ifdef PFIL_HOOKS
    718 	/*
    719 	 * Run through list of hooks for output packets.
    720 	 */
    721 	if ((error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
    722 		goto done;
    723 	if (m == NULL)
    724 		goto done;
    725 
    726 	ip = mtod(m, struct ip *);
    727 	hlen = ip->ip_hl << 2;
    728 #endif /* PFIL_HOOKS */
    729 
    730 	m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
    731 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
    732 	/*
    733 	 * If small enough for mtu of path, can just send directly.
    734 	 */
    735 	if (ip_len <= mtu) {
    736 #if IFA_STATS
    737 		/*
    738 		 * search for the source address structure to
    739 		 * maintain output statistics.
    740 		 */
    741 		INADDR_TO_IA(ip->ip_src, ia);
    742 		if (ia)
    743 			ia->ia_ifa.ifa_data.ifad_outbytes += ip_len;
    744 #endif
    745 		/*
    746 		 * Always initialize the sum to 0!  Some HW assisted
    747 		 * checksumming requires this.
    748 		 */
    749 		ip->ip_sum = 0;
    750 
    751 		/*
    752 		 * Perform any checksums that the hardware can't do
    753 		 * for us.
    754 		 *
    755 		 * XXX Does any hardware require the {th,uh}_sum
    756 		 * XXX fields to be 0?
    757 		 */
    758 		if (sw_csum & M_CSUM_IPv4) {
    759 			ip->ip_sum = in_cksum(m, hlen);
    760 			m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
    761 		}
    762 		if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    763 			in_delayed_cksum(m);
    764 			m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    765 		}
    766 
    767 #ifdef IPSEC
    768 		/* clean ipsec history once it goes out of the node */
    769 		ipsec_delaux(m);
    770 #endif
    771 		error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
    772 		goto done;
    773 	}
    774 
    775 	/*
    776 	 * We can't use HW checksumming if we're about to
    777 	 * to fragment the packet.
    778 	 *
    779 	 * XXX Some hardware can do this.
    780 	 */
    781 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    782 		in_delayed_cksum(m);
    783 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    784 	}
    785 
    786 	/*
    787 	 * Too large for interface; fragment if possible.
    788 	 * Must be able to put at least 8 bytes per fragment.
    789 	 */
    790 	if (ntohs(ip->ip_off) & IP_DF) {
    791 		if (flags & IP_RETURNMTU)
    792 			*mtu_p = mtu;
    793 		error = EMSGSIZE;
    794 		ipstat.ips_cantfrag++;
    795 		goto bad;
    796 	}
    797 	len = (mtu - hlen) &~ 7;
    798 	if (len < 8) {
    799 		error = EMSGSIZE;
    800 		goto bad;
    801 	}
    802 
    803     {
    804 	int mhlen, firstlen = len;
    805 	struct mbuf **mnext = &m->m_nextpkt;
    806 	int fragments = 0;
    807 	int s;
    808 
    809 	/*
    810 	 * Loop through length of segment after first fragment,
    811 	 * make new header and copy data of each part and link onto chain.
    812 	 */
    813 	m0 = m;
    814 	mhlen = sizeof (struct ip);
    815 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
    816 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    817 		if (m == 0) {
    818 			error = ENOBUFS;
    819 			ipstat.ips_odropped++;
    820 			goto sendorfree;
    821 		}
    822 		MCLAIM(m, m0->m_owner);
    823 		*mnext = m;
    824 		mnext = &m->m_nextpkt;
    825 		m->m_data += max_linkhdr;
    826 		mhip = mtod(m, struct ip *);
    827 		*mhip = *ip;
    828 		/* we must inherit MCAST and BCAST flags */
    829 		m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
    830 		if (hlen > sizeof (struct ip)) {
    831 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
    832 			mhip->ip_hl = mhlen >> 2;
    833 		}
    834 		m->m_len = mhlen;
    835 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
    836 		if (ip->ip_off & IP_MF)
    837 			mhip->ip_off |= IP_MF;
    838 		if (off + len >= ntohs(ip->ip_len))
    839 			len = ntohs(ip->ip_len) - off;
    840 		else
    841 			mhip->ip_off |= IP_MF;
    842 		HTONS(mhip->ip_off);
    843 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
    844 		m->m_next = m_copy(m0, off, len);
    845 		if (m->m_next == 0) {
    846 			error = ENOBUFS;	/* ??? */
    847 			ipstat.ips_odropped++;
    848 			goto sendorfree;
    849 		}
    850 		m->m_pkthdr.len = mhlen + len;
    851 		m->m_pkthdr.rcvif = (struct ifnet *)0;
    852 		mhip->ip_sum = 0;
    853 		if (sw_csum & M_CSUM_IPv4) {
    854 			mhip->ip_sum = in_cksum(m, mhlen);
    855 			KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
    856 		} else {
    857 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
    858 		}
    859 		ipstat.ips_ofragments++;
    860 		fragments++;
    861 	}
    862 	/*
    863 	 * Update first fragment by trimming what's been copied out
    864 	 * and updating header, then send each fragment (in order).
    865 	 */
    866 	m = m0;
    867 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
    868 	m->m_pkthdr.len = hlen + firstlen;
    869 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
    870 	ip->ip_off |= htons(IP_MF);
    871 	ip->ip_sum = 0;
    872 	if (sw_csum & M_CSUM_IPv4) {
    873 		ip->ip_sum = in_cksum(m, hlen);
    874 		m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
    875 	} else {
    876 		KASSERT(m->m_pkthdr.csum_flags & M_CSUM_IPv4);
    877 	}
    878 sendorfree:
    879 	/*
    880 	 * If there is no room for all the fragments, don't queue
    881 	 * any of them.
    882 	 */
    883 	s = splnet();
    884 	if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments)
    885 		error = ENOBUFS;
    886 	splx(s);
    887 	for (m = m0; m; m = m0) {
    888 		m0 = m->m_nextpkt;
    889 		m->m_nextpkt = 0;
    890 		if (error == 0) {
    891 #if IFA_STATS
    892 			/*
    893 			 * search for the source address structure to
    894 			 * maintain output statistics.
    895 			 */
    896 			INADDR_TO_IA(ip->ip_src, ia);
    897 			if (ia) {
    898 				ia->ia_ifa.ifa_data.ifad_outbytes +=
    899 				    ntohs(ip->ip_len);
    900 			}
    901 #endif
    902 #ifdef IPSEC
    903 			/* clean ipsec history once it goes out of the node */
    904 			ipsec_delaux(m);
    905 #endif
    906 			KASSERT((m->m_pkthdr.csum_flags &
    907 			    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
    908 			error = (*ifp->if_output)(ifp, m, sintosa(dst),
    909 			    ro->ro_rt);
    910 		} else
    911 			m_freem(m);
    912 	}
    913 
    914 	if (error == 0)
    915 		ipstat.ips_fragmented++;
    916     }
    917 done:
    918 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
    919 		RTFREE(ro->ro_rt);
    920 		ro->ro_rt = 0;
    921 	}
    922 
    923 #ifdef IPSEC
    924 	if (sp != NULL) {
    925 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
    926 			printf("DP ip_output call free SP:%p\n", sp));
    927 		key_freesp(sp);
    928 	}
    929 #endif /* IPSEC */
    930 #ifdef FAST_IPSEC
    931 	if (sp != NULL)
    932 		KEY_FREESP(&sp);
    933 #endif /* FAST_IPSEC */
    934 
    935 	return (error);
    936 bad:
    937 	m_freem(m);
    938 	goto done;
    939 }
    940 
    941 /*
    942  * Process a delayed payload checksum calculation.
    943  */
    944 void
    945 in_delayed_cksum(struct mbuf *m)
    946 {
    947 	struct ip *ip;
    948 	u_int16_t csum, offset;
    949 
    950 	ip = mtod(m, struct ip *);
    951 	offset = ip->ip_hl << 2;
    952 	csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
    953 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
    954 		csum = 0xffff;
    955 
    956 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
    957 
    958 	if ((offset + sizeof(u_int16_t)) > m->m_len) {
    959 		/* This happen when ip options were inserted
    960 		printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
    961 		    m->m_len, offset, ip->ip_p);
    962 		 */
    963 		m_copyback(m, offset, sizeof(csum), (caddr_t) &csum);
    964 	} else
    965 		*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
    966 }
    967 
    968 /*
    969  * Determine the maximum length of the options to be inserted;
    970  * we would far rather allocate too much space rather than too little.
    971  */
    972 
    973 u_int
    974 ip_optlen(inp)
    975 	struct inpcb *inp;
    976 {
    977 	struct mbuf *m = inp->inp_options;
    978 
    979 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
    980 		return (m->m_len - offsetof(struct ipoption, ipopt_dst));
    981 	else
    982 		return 0;
    983 }
    984 
    985 
    986 /*
    987  * Insert IP options into preformed packet.
    988  * Adjust IP destination as required for IP source routing,
    989  * as indicated by a non-zero in_addr at the start of the options.
    990  */
    991 static struct mbuf *
    992 ip_insertoptions(m, opt, phlen)
    993 	struct mbuf *m;
    994 	struct mbuf *opt;
    995 	int *phlen;
    996 {
    997 	struct ipoption *p = mtod(opt, struct ipoption *);
    998 	struct mbuf *n;
    999 	struct ip *ip = mtod(m, struct ip *);
   1000 	unsigned optlen;
   1001 
   1002 	optlen = opt->m_len - sizeof(p->ipopt_dst);
   1003 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
   1004 		return (m);		/* XXX should fail */
   1005 	if (!in_nullhost(p->ipopt_dst))
   1006 		ip->ip_dst = p->ipopt_dst;
   1007 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
   1008 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
   1009 		if (n == 0)
   1010 			return (m);
   1011 		MCLAIM(n, m->m_owner);
   1012 		M_COPY_PKTHDR(n, m);
   1013 		m->m_flags &= ~M_PKTHDR;
   1014 		m->m_len -= sizeof(struct ip);
   1015 		m->m_data += sizeof(struct ip);
   1016 		n->m_next = m;
   1017 		m = n;
   1018 		m->m_len = optlen + sizeof(struct ip);
   1019 		m->m_data += max_linkhdr;
   1020 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
   1021 	} else {
   1022 		m->m_data -= optlen;
   1023 		m->m_len += optlen;
   1024 		memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
   1025 	}
   1026 	m->m_pkthdr.len += optlen;
   1027 	ip = mtod(m, struct ip *);
   1028 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
   1029 	*phlen = sizeof(struct ip) + optlen;
   1030 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
   1031 	return (m);
   1032 }
   1033 
   1034 /*
   1035  * Copy options from ip to jp,
   1036  * omitting those not copied during fragmentation.
   1037  */
   1038 int
   1039 ip_optcopy(ip, jp)
   1040 	struct ip *ip, *jp;
   1041 {
   1042 	u_char *cp, *dp;
   1043 	int opt, optlen, cnt;
   1044 
   1045 	cp = (u_char *)(ip + 1);
   1046 	dp = (u_char *)(jp + 1);
   1047 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
   1048 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   1049 		opt = cp[0];
   1050 		if (opt == IPOPT_EOL)
   1051 			break;
   1052 		if (opt == IPOPT_NOP) {
   1053 			/* Preserve for IP mcast tunnel's LSRR alignment. */
   1054 			*dp++ = IPOPT_NOP;
   1055 			optlen = 1;
   1056 			continue;
   1057 		}
   1058 #ifdef DIAGNOSTIC
   1059 		if (cnt < IPOPT_OLEN + sizeof(*cp))
   1060 			panic("malformed IPv4 option passed to ip_optcopy");
   1061 #endif
   1062 		optlen = cp[IPOPT_OLEN];
   1063 #ifdef DIAGNOSTIC
   1064 		if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
   1065 			panic("malformed IPv4 option passed to ip_optcopy");
   1066 #endif
   1067 		/* bogus lengths should have been caught by ip_dooptions */
   1068 		if (optlen > cnt)
   1069 			optlen = cnt;
   1070 		if (IPOPT_COPIED(opt)) {
   1071 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
   1072 			dp += optlen;
   1073 		}
   1074 	}
   1075 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
   1076 		*dp++ = IPOPT_EOL;
   1077 	return (optlen);
   1078 }
   1079 
   1080 /*
   1081  * IP socket option processing.
   1082  */
   1083 int
   1084 ip_ctloutput(op, so, level, optname, mp)
   1085 	int op;
   1086 	struct socket *so;
   1087 	int level, optname;
   1088 	struct mbuf **mp;
   1089 {
   1090 	struct inpcb *inp = sotoinpcb(so);
   1091 	struct mbuf *m = *mp;
   1092 	int optval = 0;
   1093 	int error = 0;
   1094 #if defined(IPSEC) || defined(FAST_IPSEC)
   1095 #ifdef __NetBSD__
   1096 	struct proc *p = curproc;	/*XXX*/
   1097 #endif
   1098 #endif
   1099 
   1100 	if (level != IPPROTO_IP) {
   1101 		error = EINVAL;
   1102 		if (op == PRCO_SETOPT && *mp)
   1103 			(void) m_free(*mp);
   1104 	} else switch (op) {
   1105 
   1106 	case PRCO_SETOPT:
   1107 		switch (optname) {
   1108 		case IP_OPTIONS:
   1109 #ifdef notyet
   1110 		case IP_RETOPTS:
   1111 			return (ip_pcbopts(optname, &inp->inp_options, m));
   1112 #else
   1113 			return (ip_pcbopts(&inp->inp_options, m));
   1114 #endif
   1115 
   1116 		case IP_TOS:
   1117 		case IP_TTL:
   1118 		case IP_RECVOPTS:
   1119 		case IP_RECVRETOPTS:
   1120 		case IP_RECVDSTADDR:
   1121 		case IP_RECVIF:
   1122 			if (m == NULL || m->m_len != sizeof(int))
   1123 				error = EINVAL;
   1124 			else {
   1125 				optval = *mtod(m, int *);
   1126 				switch (optname) {
   1127 
   1128 				case IP_TOS:
   1129 					inp->inp_ip.ip_tos = optval;
   1130 					break;
   1131 
   1132 				case IP_TTL:
   1133 					inp->inp_ip.ip_ttl = optval;
   1134 					break;
   1135 #define	OPTSET(bit) \
   1136 	if (optval) \
   1137 		inp->inp_flags |= bit; \
   1138 	else \
   1139 		inp->inp_flags &= ~bit;
   1140 
   1141 				case IP_RECVOPTS:
   1142 					OPTSET(INP_RECVOPTS);
   1143 					break;
   1144 
   1145 				case IP_RECVRETOPTS:
   1146 					OPTSET(INP_RECVRETOPTS);
   1147 					break;
   1148 
   1149 				case IP_RECVDSTADDR:
   1150 					OPTSET(INP_RECVDSTADDR);
   1151 					break;
   1152 
   1153 				case IP_RECVIF:
   1154 					OPTSET(INP_RECVIF);
   1155 					break;
   1156 				}
   1157 			}
   1158 			break;
   1159 #undef OPTSET
   1160 
   1161 		case IP_MULTICAST_IF:
   1162 		case IP_MULTICAST_TTL:
   1163 		case IP_MULTICAST_LOOP:
   1164 		case IP_ADD_MEMBERSHIP:
   1165 		case IP_DROP_MEMBERSHIP:
   1166 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
   1167 			break;
   1168 
   1169 		case IP_PORTRANGE:
   1170 			if (m == 0 || m->m_len != sizeof(int))
   1171 				error = EINVAL;
   1172 			else {
   1173 				optval = *mtod(m, int *);
   1174 
   1175 				switch (optval) {
   1176 
   1177 				case IP_PORTRANGE_DEFAULT:
   1178 				case IP_PORTRANGE_HIGH:
   1179 					inp->inp_flags &= ~(INP_LOWPORT);
   1180 					break;
   1181 
   1182 				case IP_PORTRANGE_LOW:
   1183 					inp->inp_flags |= INP_LOWPORT;
   1184 					break;
   1185 
   1186 				default:
   1187 					error = EINVAL;
   1188 					break;
   1189 				}
   1190 			}
   1191 			break;
   1192 
   1193 #if defined(IPSEC) || defined(FAST_IPSEC)
   1194 		case IP_IPSEC_POLICY:
   1195 		{
   1196 			caddr_t req = NULL;
   1197 			size_t len = 0;
   1198 			int priv = 0;
   1199 
   1200 #ifdef __NetBSD__
   1201 			if (p == 0 || suser(p->p_ucred, &p->p_acflag))
   1202 				priv = 0;
   1203 			else
   1204 				priv = 1;
   1205 #else
   1206 			priv = (in6p->in6p_socket->so_state & SS_PRIV);
   1207 #endif
   1208 			if (m) {
   1209 				req = mtod(m, caddr_t);
   1210 				len = m->m_len;
   1211 			}
   1212 			error = ipsec4_set_policy(inp, optname, req, len, priv);
   1213 			break;
   1214 		    }
   1215 #endif /*IPSEC*/
   1216 
   1217 		default:
   1218 			error = ENOPROTOOPT;
   1219 			break;
   1220 		}
   1221 		if (m)
   1222 			(void)m_free(m);
   1223 		break;
   1224 
   1225 	case PRCO_GETOPT:
   1226 		switch (optname) {
   1227 		case IP_OPTIONS:
   1228 		case IP_RETOPTS:
   1229 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1230 			MCLAIM(m, so->so_mowner);
   1231 			if (inp->inp_options) {
   1232 				m->m_len = inp->inp_options->m_len;
   1233 				bcopy(mtod(inp->inp_options, caddr_t),
   1234 				    mtod(m, caddr_t), (unsigned)m->m_len);
   1235 			} else
   1236 				m->m_len = 0;
   1237 			break;
   1238 
   1239 		case IP_TOS:
   1240 		case IP_TTL:
   1241 		case IP_RECVOPTS:
   1242 		case IP_RECVRETOPTS:
   1243 		case IP_RECVDSTADDR:
   1244 		case IP_RECVIF:
   1245 		case IP_ERRORMTU:
   1246 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1247 			MCLAIM(m, so->so_mowner);
   1248 			m->m_len = sizeof(int);
   1249 			switch (optname) {
   1250 
   1251 			case IP_TOS:
   1252 				optval = inp->inp_ip.ip_tos;
   1253 				break;
   1254 
   1255 			case IP_TTL:
   1256 				optval = inp->inp_ip.ip_ttl;
   1257 				break;
   1258 
   1259 			case IP_ERRORMTU:
   1260 				optval = inp->inp_errormtu;
   1261 				break;
   1262 
   1263 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
   1264 
   1265 			case IP_RECVOPTS:
   1266 				optval = OPTBIT(INP_RECVOPTS);
   1267 				break;
   1268 
   1269 			case IP_RECVRETOPTS:
   1270 				optval = OPTBIT(INP_RECVRETOPTS);
   1271 				break;
   1272 
   1273 			case IP_RECVDSTADDR:
   1274 				optval = OPTBIT(INP_RECVDSTADDR);
   1275 				break;
   1276 
   1277 			case IP_RECVIF:
   1278 				optval = OPTBIT(INP_RECVIF);
   1279 				break;
   1280 			}
   1281 			*mtod(m, int *) = optval;
   1282 			break;
   1283 
   1284 #if defined(IPSEC) || defined(FAST_IPSEC)
   1285 		case IP_IPSEC_POLICY:
   1286 		{
   1287 			caddr_t req = NULL;
   1288 			size_t len = 0;
   1289 
   1290 			if (m) {
   1291 				req = mtod(m, caddr_t);
   1292 				len = m->m_len;
   1293 			}
   1294 			error = ipsec4_get_policy(inp, req, len, mp);
   1295 			break;
   1296 		}
   1297 #endif /*IPSEC*/
   1298 
   1299 		case IP_MULTICAST_IF:
   1300 		case IP_MULTICAST_TTL:
   1301 		case IP_MULTICAST_LOOP:
   1302 		case IP_ADD_MEMBERSHIP:
   1303 		case IP_DROP_MEMBERSHIP:
   1304 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
   1305 			if (*mp)
   1306 				MCLAIM(*mp, so->so_mowner);
   1307 			break;
   1308 
   1309 		case IP_PORTRANGE:
   1310 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1311 			MCLAIM(m, so->so_mowner);
   1312 			m->m_len = sizeof(int);
   1313 
   1314 			if (inp->inp_flags & INP_LOWPORT)
   1315 				optval = IP_PORTRANGE_LOW;
   1316 			else
   1317 				optval = IP_PORTRANGE_DEFAULT;
   1318 
   1319 			*mtod(m, int *) = optval;
   1320 			break;
   1321 
   1322 		default:
   1323 			error = ENOPROTOOPT;
   1324 			break;
   1325 		}
   1326 		break;
   1327 	}
   1328 	return (error);
   1329 }
   1330 
   1331 /*
   1332  * Set up IP options in pcb for insertion in output packets.
   1333  * Store in mbuf with pointer in pcbopt, adding pseudo-option
   1334  * with destination address if source routed.
   1335  */
   1336 int
   1337 #ifdef notyet
   1338 ip_pcbopts(optname, pcbopt, m)
   1339 	int optname;
   1340 #else
   1341 ip_pcbopts(pcbopt, m)
   1342 #endif
   1343 	struct mbuf **pcbopt;
   1344 	struct mbuf *m;
   1345 {
   1346 	int cnt, optlen;
   1347 	u_char *cp;
   1348 	u_char opt;
   1349 
   1350 	/* turn off any old options */
   1351 	if (*pcbopt)
   1352 		(void)m_free(*pcbopt);
   1353 	*pcbopt = 0;
   1354 	if (m == (struct mbuf *)0 || m->m_len == 0) {
   1355 		/*
   1356 		 * Only turning off any previous options.
   1357 		 */
   1358 		if (m)
   1359 			(void)m_free(m);
   1360 		return (0);
   1361 	}
   1362 
   1363 #ifndef	__vax__
   1364 	if (m->m_len % sizeof(int32_t))
   1365 		goto bad;
   1366 #endif
   1367 	/*
   1368 	 * IP first-hop destination address will be stored before
   1369 	 * actual options; move other options back
   1370 	 * and clear it when none present.
   1371 	 */
   1372 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
   1373 		goto bad;
   1374 	cnt = m->m_len;
   1375 	m->m_len += sizeof(struct in_addr);
   1376 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
   1377 	memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
   1378 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
   1379 
   1380 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   1381 		opt = cp[IPOPT_OPTVAL];
   1382 		if (opt == IPOPT_EOL)
   1383 			break;
   1384 		if (opt == IPOPT_NOP)
   1385 			optlen = 1;
   1386 		else {
   1387 			if (cnt < IPOPT_OLEN + sizeof(*cp))
   1388 				goto bad;
   1389 			optlen = cp[IPOPT_OLEN];
   1390 			if (optlen < IPOPT_OLEN  + sizeof(*cp) || optlen > cnt)
   1391 				goto bad;
   1392 		}
   1393 		switch (opt) {
   1394 
   1395 		default:
   1396 			break;
   1397 
   1398 		case IPOPT_LSRR:
   1399 		case IPOPT_SSRR:
   1400 			/*
   1401 			 * user process specifies route as:
   1402 			 *	->A->B->C->D
   1403 			 * D must be our final destination (but we can't
   1404 			 * check that since we may not have connected yet).
   1405 			 * A is first hop destination, which doesn't appear in
   1406 			 * actual IP option, but is stored before the options.
   1407 			 */
   1408 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
   1409 				goto bad;
   1410 			m->m_len -= sizeof(struct in_addr);
   1411 			cnt -= sizeof(struct in_addr);
   1412 			optlen -= sizeof(struct in_addr);
   1413 			cp[IPOPT_OLEN] = optlen;
   1414 			/*
   1415 			 * Move first hop before start of options.
   1416 			 */
   1417 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
   1418 			    sizeof(struct in_addr));
   1419 			/*
   1420 			 * Then copy rest of options back
   1421 			 * to close up the deleted entry.
   1422 			 */
   1423 			memmove(&cp[IPOPT_OFFSET+1],
   1424 			    (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
   1425 			    (unsigned)cnt + sizeof(struct in_addr));
   1426 			break;
   1427 		}
   1428 	}
   1429 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
   1430 		goto bad;
   1431 	*pcbopt = m;
   1432 	return (0);
   1433 
   1434 bad:
   1435 	(void)m_free(m);
   1436 	return (EINVAL);
   1437 }
   1438 
   1439 /*
   1440  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
   1441  */
   1442 static struct ifnet *
   1443 ip_multicast_if(a, ifindexp)
   1444 	struct in_addr *a;
   1445 	int *ifindexp;
   1446 {
   1447 	int ifindex;
   1448 	struct ifnet *ifp;
   1449 
   1450 	if (ifindexp)
   1451 		*ifindexp = 0;
   1452 	if (ntohl(a->s_addr) >> 24 == 0) {
   1453 		ifindex = ntohl(a->s_addr) & 0xffffff;
   1454 		if (ifindex < 0 || if_index < ifindex)
   1455 			return NULL;
   1456 		ifp = ifindex2ifnet[ifindex];
   1457 		if (ifindexp)
   1458 			*ifindexp = ifindex;
   1459 	} else {
   1460 		INADDR_TO_IFP(*a, ifp);
   1461 	}
   1462 	return ifp;
   1463 }
   1464 
   1465 /*
   1466  * Set the IP multicast options in response to user setsockopt().
   1467  */
   1468 int
   1469 ip_setmoptions(optname, imop, m)
   1470 	int optname;
   1471 	struct ip_moptions **imop;
   1472 	struct mbuf *m;
   1473 {
   1474 	int error = 0;
   1475 	u_char loop;
   1476 	int i;
   1477 	struct in_addr addr;
   1478 	struct ip_mreq *mreq;
   1479 	struct ifnet *ifp;
   1480 	struct ip_moptions *imo = *imop;
   1481 	struct route ro;
   1482 	struct sockaddr_in *dst;
   1483 	int ifindex;
   1484 
   1485 	if (imo == NULL) {
   1486 		/*
   1487 		 * No multicast option buffer attached to the pcb;
   1488 		 * allocate one and initialize to default values.
   1489 		 */
   1490 		imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
   1491 		    M_WAITOK);
   1492 
   1493 		if (imo == NULL)
   1494 			return (ENOBUFS);
   1495 		*imop = imo;
   1496 		imo->imo_multicast_ifp = NULL;
   1497 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
   1498 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
   1499 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
   1500 		imo->imo_num_memberships = 0;
   1501 	}
   1502 
   1503 	switch (optname) {
   1504 
   1505 	case IP_MULTICAST_IF:
   1506 		/*
   1507 		 * Select the interface for outgoing multicast packets.
   1508 		 */
   1509 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
   1510 			error = EINVAL;
   1511 			break;
   1512 		}
   1513 		addr = *(mtod(m, struct in_addr *));
   1514 		/*
   1515 		 * INADDR_ANY is used to remove a previous selection.
   1516 		 * When no interface is selected, a default one is
   1517 		 * chosen every time a multicast packet is sent.
   1518 		 */
   1519 		if (in_nullhost(addr)) {
   1520 			imo->imo_multicast_ifp = NULL;
   1521 			break;
   1522 		}
   1523 		/*
   1524 		 * The selected interface is identified by its local
   1525 		 * IP address.  Find the interface and confirm that
   1526 		 * it supports multicasting.
   1527 		 */
   1528 		ifp = ip_multicast_if(&addr, &ifindex);
   1529 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1530 			error = EADDRNOTAVAIL;
   1531 			break;
   1532 		}
   1533 		imo->imo_multicast_ifp = ifp;
   1534 		if (ifindex)
   1535 			imo->imo_multicast_addr = addr;
   1536 		else
   1537 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
   1538 		break;
   1539 
   1540 	case IP_MULTICAST_TTL:
   1541 		/*
   1542 		 * Set the IP time-to-live for outgoing multicast packets.
   1543 		 */
   1544 		if (m == NULL || m->m_len != 1) {
   1545 			error = EINVAL;
   1546 			break;
   1547 		}
   1548 		imo->imo_multicast_ttl = *(mtod(m, u_char *));
   1549 		break;
   1550 
   1551 	case IP_MULTICAST_LOOP:
   1552 		/*
   1553 		 * Set the loopback flag for outgoing multicast packets.
   1554 		 * Must be zero or one.
   1555 		 */
   1556 		if (m == NULL || m->m_len != 1 ||
   1557 		   (loop = *(mtod(m, u_char *))) > 1) {
   1558 			error = EINVAL;
   1559 			break;
   1560 		}
   1561 		imo->imo_multicast_loop = loop;
   1562 		break;
   1563 
   1564 	case IP_ADD_MEMBERSHIP:
   1565 		/*
   1566 		 * Add a multicast group membership.
   1567 		 * Group must be a valid IP multicast address.
   1568 		 */
   1569 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
   1570 			error = EINVAL;
   1571 			break;
   1572 		}
   1573 		mreq = mtod(m, struct ip_mreq *);
   1574 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
   1575 			error = EINVAL;
   1576 			break;
   1577 		}
   1578 		/*
   1579 		 * If no interface address was provided, use the interface of
   1580 		 * the route to the given multicast address.
   1581 		 */
   1582 		if (in_nullhost(mreq->imr_interface)) {
   1583 			bzero((caddr_t)&ro, sizeof(ro));
   1584 			ro.ro_rt = NULL;
   1585 			dst = satosin(&ro.ro_dst);
   1586 			dst->sin_len = sizeof(*dst);
   1587 			dst->sin_family = AF_INET;
   1588 			dst->sin_addr = mreq->imr_multiaddr;
   1589 			rtalloc(&ro);
   1590 			if (ro.ro_rt == NULL) {
   1591 				error = EADDRNOTAVAIL;
   1592 				break;
   1593 			}
   1594 			ifp = ro.ro_rt->rt_ifp;
   1595 			rtfree(ro.ro_rt);
   1596 		} else {
   1597 			ifp = ip_multicast_if(&mreq->imr_interface, NULL);
   1598 		}
   1599 		/*
   1600 		 * See if we found an interface, and confirm that it
   1601 		 * supports multicast.
   1602 		 */
   1603 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1604 			error = EADDRNOTAVAIL;
   1605 			break;
   1606 		}
   1607 		/*
   1608 		 * See if the membership already exists or if all the
   1609 		 * membership slots are full.
   1610 		 */
   1611 		for (i = 0; i < imo->imo_num_memberships; ++i) {
   1612 			if (imo->imo_membership[i]->inm_ifp == ifp &&
   1613 			    in_hosteq(imo->imo_membership[i]->inm_addr,
   1614 				      mreq->imr_multiaddr))
   1615 				break;
   1616 		}
   1617 		if (i < imo->imo_num_memberships) {
   1618 			error = EADDRINUSE;
   1619 			break;
   1620 		}
   1621 		if (i == IP_MAX_MEMBERSHIPS) {
   1622 			error = ETOOMANYREFS;
   1623 			break;
   1624 		}
   1625 		/*
   1626 		 * Everything looks good; add a new record to the multicast
   1627 		 * address list for the given interface.
   1628 		 */
   1629 		if ((imo->imo_membership[i] =
   1630 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
   1631 			error = ENOBUFS;
   1632 			break;
   1633 		}
   1634 		++imo->imo_num_memberships;
   1635 		break;
   1636 
   1637 	case IP_DROP_MEMBERSHIP:
   1638 		/*
   1639 		 * Drop a multicast group membership.
   1640 		 * Group must be a valid IP multicast address.
   1641 		 */
   1642 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
   1643 			error = EINVAL;
   1644 			break;
   1645 		}
   1646 		mreq = mtod(m, struct ip_mreq *);
   1647 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
   1648 			error = EINVAL;
   1649 			break;
   1650 		}
   1651 		/*
   1652 		 * If an interface address was specified, get a pointer
   1653 		 * to its ifnet structure.
   1654 		 */
   1655 		if (in_nullhost(mreq->imr_interface))
   1656 			ifp = NULL;
   1657 		else {
   1658 			ifp = ip_multicast_if(&mreq->imr_interface, NULL);
   1659 			if (ifp == NULL) {
   1660 				error = EADDRNOTAVAIL;
   1661 				break;
   1662 			}
   1663 		}
   1664 		/*
   1665 		 * Find the membership in the membership array.
   1666 		 */
   1667 		for (i = 0; i < imo->imo_num_memberships; ++i) {
   1668 			if ((ifp == NULL ||
   1669 			     imo->imo_membership[i]->inm_ifp == ifp) &&
   1670 			     in_hosteq(imo->imo_membership[i]->inm_addr,
   1671 				       mreq->imr_multiaddr))
   1672 				break;
   1673 		}
   1674 		if (i == imo->imo_num_memberships) {
   1675 			error = EADDRNOTAVAIL;
   1676 			break;
   1677 		}
   1678 		/*
   1679 		 * Give up the multicast address record to which the
   1680 		 * membership points.
   1681 		 */
   1682 		in_delmulti(imo->imo_membership[i]);
   1683 		/*
   1684 		 * Remove the gap in the membership array.
   1685 		 */
   1686 		for (++i; i < imo->imo_num_memberships; ++i)
   1687 			imo->imo_membership[i-1] = imo->imo_membership[i];
   1688 		--imo->imo_num_memberships;
   1689 		break;
   1690 
   1691 	default:
   1692 		error = EOPNOTSUPP;
   1693 		break;
   1694 	}
   1695 
   1696 	/*
   1697 	 * If all options have default values, no need to keep the mbuf.
   1698 	 */
   1699 	if (imo->imo_multicast_ifp == NULL &&
   1700 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
   1701 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
   1702 	    imo->imo_num_memberships == 0) {
   1703 		free(*imop, M_IPMOPTS);
   1704 		*imop = NULL;
   1705 	}
   1706 
   1707 	return (error);
   1708 }
   1709 
   1710 /*
   1711  * Return the IP multicast options in response to user getsockopt().
   1712  */
   1713 int
   1714 ip_getmoptions(optname, imo, mp)
   1715 	int optname;
   1716 	struct ip_moptions *imo;
   1717 	struct mbuf **mp;
   1718 {
   1719 	u_char *ttl;
   1720 	u_char *loop;
   1721 	struct in_addr *addr;
   1722 	struct in_ifaddr *ia;
   1723 
   1724 	*mp = m_get(M_WAIT, MT_SOOPTS);
   1725 
   1726 	switch (optname) {
   1727 
   1728 	case IP_MULTICAST_IF:
   1729 		addr = mtod(*mp, struct in_addr *);
   1730 		(*mp)->m_len = sizeof(struct in_addr);
   1731 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
   1732 			*addr = zeroin_addr;
   1733 		else if (imo->imo_multicast_addr.s_addr) {
   1734 			/* return the value user has set */
   1735 			*addr = imo->imo_multicast_addr;
   1736 		} else {
   1737 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
   1738 			*addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
   1739 		}
   1740 		return (0);
   1741 
   1742 	case IP_MULTICAST_TTL:
   1743 		ttl = mtod(*mp, u_char *);
   1744 		(*mp)->m_len = 1;
   1745 		*ttl = imo ? imo->imo_multicast_ttl
   1746 			   : IP_DEFAULT_MULTICAST_TTL;
   1747 		return (0);
   1748 
   1749 	case IP_MULTICAST_LOOP:
   1750 		loop = mtod(*mp, u_char *);
   1751 		(*mp)->m_len = 1;
   1752 		*loop = imo ? imo->imo_multicast_loop
   1753 			    : IP_DEFAULT_MULTICAST_LOOP;
   1754 		return (0);
   1755 
   1756 	default:
   1757 		return (EOPNOTSUPP);
   1758 	}
   1759 }
   1760 
   1761 /*
   1762  * Discard the IP multicast options.
   1763  */
   1764 void
   1765 ip_freemoptions(imo)
   1766 	struct ip_moptions *imo;
   1767 {
   1768 	int i;
   1769 
   1770 	if (imo != NULL) {
   1771 		for (i = 0; i < imo->imo_num_memberships; ++i)
   1772 			in_delmulti(imo->imo_membership[i]);
   1773 		free(imo, M_IPMOPTS);
   1774 	}
   1775 }
   1776 
   1777 /*
   1778  * Routine called from ip_output() to loop back a copy of an IP multicast
   1779  * packet to the input queue of a specified interface.  Note that this
   1780  * calls the output routine of the loopback "driver", but with an interface
   1781  * pointer that might NOT be &loif -- easier than replicating that code here.
   1782  */
   1783 static void
   1784 ip_mloopback(ifp, m, dst)
   1785 	struct ifnet *ifp;
   1786 	struct mbuf *m;
   1787 	struct sockaddr_in *dst;
   1788 {
   1789 	struct ip *ip;
   1790 	struct mbuf *copym;
   1791 
   1792 	copym = m_copy(m, 0, M_COPYALL);
   1793 	if (copym != NULL
   1794 	 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
   1795 		copym = m_pullup(copym, sizeof(struct ip));
   1796 	if (copym != NULL) {
   1797 		/*
   1798 		 * We don't bother to fragment if the IP length is greater
   1799 		 * than the interface's MTU.  Can this possibly matter?
   1800 		 */
   1801 		ip = mtod(copym, struct ip *);
   1802 
   1803 		if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
   1804 			in_delayed_cksum(copym);
   1805 			copym->m_pkthdr.csum_flags &=
   1806 			    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
   1807 		}
   1808 
   1809 		ip->ip_sum = 0;
   1810 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
   1811 		(void) looutput(ifp, copym, sintosa(dst), NULL);
   1812 	}
   1813 }
   1814