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