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