Home | History | Annotate | Line # | Download | only in netinet
ip_output.c revision 1.324.2.2
      1 /*	$NetBSD: ip_output.c,v 1.324.2.2 2024/09/21 12:28:46 martin 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.324.2.2 2024/09/21 12:28:46 martin Exp $");
     95 
     96 #ifdef _KERNEL_OPT
     97 #include "opt_inet.h"
     98 #include "opt_ipsec.h"
     99 #include "opt_mrouting.h"
    100 #include "opt_net_mpsafe.h"
    101 #include "opt_mpls.h"
    102 #endif
    103 
    104 #include "arp.h"
    105 
    106 #include <sys/param.h>
    107 #include <sys/kmem.h>
    108 #include <sys/mbuf.h>
    109 #include <sys/socket.h>
    110 #include <sys/socketvar.h>
    111 #include <sys/kauth.h>
    112 #include <sys/systm.h>
    113 #include <sys/syslog.h>
    114 
    115 #include <net/if.h>
    116 #include <net/if_types.h>
    117 #include <net/route.h>
    118 #include <net/pfil.h>
    119 
    120 #include <netinet/in.h>
    121 #include <netinet/in_systm.h>
    122 #include <netinet/ip.h>
    123 #include <netinet/in_pcb.h>
    124 #include <netinet/in_var.h>
    125 #include <netinet/ip_var.h>
    126 #include <netinet/ip_private.h>
    127 #include <netinet/in_offload.h>
    128 #include <netinet/portalgo.h>
    129 #include <netinet/udp.h>
    130 #include <netinet/udp_var.h>
    131 
    132 #ifdef INET6
    133 #include <netinet6/ip6_var.h>
    134 #endif
    135 
    136 #ifdef MROUTING
    137 #include <netinet/ip_mroute.h>
    138 #endif
    139 
    140 #ifdef IPSEC
    141 #include <netipsec/ipsec.h>
    142 #include <netipsec/key.h>
    143 #endif
    144 
    145 #ifdef MPLS
    146 #include <netmpls/mpls.h>
    147 #include <netmpls/mpls_var.h>
    148 #endif
    149 
    150 static int ip_pcbopts(struct inpcb *, const struct sockopt *);
    151 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
    152 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
    153 static void ip_mloopback(struct ifnet *, struct mbuf *,
    154     const struct sockaddr_in *);
    155 static int ip_ifaddrvalid(const struct in_ifaddr *);
    156 
    157 extern pfil_head_t *inet_pfil_hook;			/* XXX */
    158 
    159 int ip_do_loopback_cksum = 0;
    160 
    161 static int
    162 ip_mark_mpls(struct ifnet * const ifp, struct mbuf * const m,
    163     const struct rtentry *rt)
    164 {
    165 	int error = 0;
    166 #ifdef MPLS
    167 	union mpls_shim msh;
    168 
    169 	if (rt == NULL || rt_gettag(rt) == NULL ||
    170 	    rt_gettag(rt)->sa_family != AF_MPLS ||
    171 	    (m->m_flags & (M_MCAST | M_BCAST)) != 0 ||
    172 	    ifp->if_type != IFT_ETHER)
    173 		return 0;
    174 
    175 	msh.s_addr = MPLS_GETSADDR(rt);
    176 	if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
    177 		struct m_tag *mtag;
    178 		/*
    179 		 * XXX tentative solution to tell ether_output
    180 		 * it's MPLS. Need some more efficient solution.
    181 		 */
    182 		mtag = m_tag_get(PACKET_TAG_MPLS,
    183 		    sizeof(int) /* dummy */,
    184 		    M_NOWAIT);
    185 		if (mtag == NULL)
    186 			return ENOMEM;
    187 		m_tag_prepend(m, mtag);
    188 	}
    189 #endif
    190 	return error;
    191 }
    192 
    193 /*
    194  * Send an IP packet to a host.
    195  */
    196 int
    197 ip_if_output(struct ifnet * const ifp, struct mbuf * const m,
    198     const struct sockaddr * const dst, const struct rtentry *rt)
    199 {
    200 	int error = 0;
    201 
    202 	if (rt != NULL) {
    203 		error = rt_check_reject_route(rt, ifp);
    204 		if (error != 0) {
    205 			IP_STATINC(IP_STAT_RTREJECT);
    206 			m_freem(m);
    207 			return error;
    208 		}
    209 	}
    210 
    211 	error = ip_mark_mpls(ifp, m, rt);
    212 	if (error != 0) {
    213 		m_freem(m);
    214 		return error;
    215 	}
    216 
    217 	error = if_output_lock(ifp, ifp, m, dst, rt);
    218 
    219 	return error;
    220 }
    221 
    222 /*
    223  * IP output.  The packet in mbuf chain m contains a skeletal IP
    224  * header (with len, off, ttl, proto, tos, src, dst).
    225  * The mbuf chain containing the packet will be freed.
    226  * The mbuf opt, if present, will not be freed.
    227  */
    228 int
    229 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags,
    230     struct ip_moptions *imo, struct inpcb *inp)
    231 {
    232 	struct rtentry *rt;
    233 	struct ip *ip;
    234 	struct ifnet *ifp, *mifp = NULL;
    235 	struct mbuf *m = m0;
    236 	int len, hlen, error = 0;
    237 	struct route iproute;
    238 	const struct sockaddr_in *dst;
    239 	struct in_ifaddr *ia = NULL;
    240 	struct ifaddr *ifa;
    241 	int isbroadcast;
    242 	int sw_csum;
    243 	u_long mtu;
    244 	bool natt_frag = false;
    245 	bool rtmtu_nolock;
    246 	union {
    247 		struct sockaddr		sa;
    248 		struct sockaddr_in	sin;
    249 	} udst, usrc;
    250 	struct sockaddr *rdst = &udst.sa;	/* real IP destination, as
    251 						 * opposed to the nexthop
    252 						 */
    253 	struct psref psref, psref_ia;
    254 	int bound;
    255 	bool bind_need_restore = false;
    256 	const struct sockaddr *sa;
    257 
    258 	len = 0;
    259 
    260 	MCLAIM(m, &ip_tx_mowner);
    261 
    262 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    263 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 0);
    264 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) !=
    265 	    (M_CSUM_TCPv4|M_CSUM_UDPv4));
    266 	KASSERT(m->m_len >= sizeof(struct ip));
    267 
    268 	hlen = sizeof(struct ip);
    269 	if (opt) {
    270 		m = ip_insertoptions(m, opt, &len);
    271 		hlen = len;
    272 	}
    273 	ip = mtod(m, struct ip *);
    274 
    275 	/*
    276 	 * Fill in IP header.
    277 	 */
    278 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
    279 		ip->ip_v = IPVERSION;
    280 		ip->ip_off = htons(0);
    281 		/* ip->ip_id filled in after we find out source ia */
    282 		ip->ip_hl = hlen >> 2;
    283 		IP_STATINC(IP_STAT_LOCALOUT);
    284 	} else {
    285 		hlen = ip->ip_hl << 2;
    286 	}
    287 
    288 	/*
    289 	 * Route packet.
    290 	 */
    291 	if (ro == NULL) {
    292 		memset(&iproute, 0, sizeof(iproute));
    293 		ro = &iproute;
    294 	}
    295 	sockaddr_in_init(&udst.sin, &ip->ip_dst, 0);
    296 	dst = satocsin(rtcache_getdst(ro));
    297 
    298 	/*
    299 	 * If there is a cached route, check that it is to the same
    300 	 * destination and is still up.  If not, free it and try again.
    301 	 * The address family should also be checked in case of sharing
    302 	 * the cache with IPv6.
    303 	 */
    304 	if (dst && (dst->sin_family != AF_INET ||
    305 	    !in_hosteq(dst->sin_addr, ip->ip_dst)))
    306 		rtcache_free(ro);
    307 
    308 	/* XXX must be before rtcache operations */
    309 	bound = curlwp_bind();
    310 	bind_need_restore = true;
    311 
    312 	if ((rt = rtcache_validate(ro)) == NULL &&
    313 	    (rt = rtcache_update(ro, 1)) == NULL) {
    314 		dst = &udst.sin;
    315 		error = rtcache_setdst(ro, &udst.sa);
    316 		if (error != 0) {
    317 			IP_STATINC(IP_STAT_ODROPPED);
    318 			goto bad;
    319 		}
    320 	}
    321 
    322 	/*
    323 	 * If routing to interface only, short circuit routing lookup.
    324 	 */
    325 	if (flags & IP_ROUTETOIF) {
    326 		ifa = ifa_ifwithladdr_psref(sintocsa(dst), &psref_ia);
    327 		if (ifa == NULL) {
    328 			IP_STATINC(IP_STAT_NOROUTE);
    329 			error = ENETUNREACH;
    330 			goto bad;
    331 		}
    332 		/* ia is already referenced by psref_ia */
    333 		ia = ifatoia(ifa);
    334 
    335 		ifp = ia->ia_ifp;
    336 		mtu = ifp->if_mtu;
    337 		ip->ip_ttl = 1;
    338 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
    339 	} else if (((IN_MULTICAST(ip->ip_dst.s_addr) ||
    340 	    ip->ip_dst.s_addr == INADDR_BROADCAST) ||
    341 	    (flags & IP_ROUTETOIFINDEX)) &&
    342 	    imo != NULL && imo->imo_multicast_if_index != 0) {
    343 		ifp = mifp = if_get_byindex(imo->imo_multicast_if_index, &psref);
    344 		if (ifp == NULL) {
    345 			IP_STATINC(IP_STAT_NOROUTE);
    346 			error = ENETUNREACH;
    347 			goto bad;
    348 		}
    349 		mtu = ifp->if_mtu;
    350 		ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
    351 		if (IN_MULTICAST(ip->ip_dst.s_addr) ||
    352 		    ip->ip_dst.s_addr == INADDR_BROADCAST) {
    353 			isbroadcast = 0;
    354 		} else {
    355 			/* IP_ROUTETOIFINDEX */
    356 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
    357 			if ((isbroadcast == 0) && ((ifp->if_flags &
    358 			    (IFF_LOOPBACK | IFF_POINTOPOINT)) == 0) &&
    359 			    (in_direct(dst->sin_addr, ifp) == 0)) {
    360 				/* gateway address required */
    361 				if (rt == NULL)
    362 					rt = rtcache_init(ro);
    363 				if (rt == NULL || rt->rt_ifp != ifp) {
    364 					IP_STATINC(IP_STAT_NOROUTE);
    365 					error = EHOSTUNREACH;
    366 					goto bad;
    367 				}
    368 				rt->rt_use++;
    369 				if (rt->rt_flags & RTF_GATEWAY)
    370 					dst = satosin(rt->rt_gateway);
    371 				if (rt->rt_flags & RTF_HOST)
    372 					isbroadcast =
    373 					    rt->rt_flags & RTF_BROADCAST;
    374 			}
    375 		}
    376 	} else {
    377 		if (rt == NULL)
    378 			rt = rtcache_init(ro);
    379 		if (rt == NULL) {
    380 			IP_STATINC(IP_STAT_NOROUTE);
    381 			error = EHOSTUNREACH;
    382 			goto bad;
    383 		}
    384 		if (ifa_is_destroying(rt->rt_ifa)) {
    385 			rtcache_unref(rt, ro);
    386 			rt = NULL;
    387 			IP_STATINC(IP_STAT_NOROUTE);
    388 			error = EHOSTUNREACH;
    389 			goto bad;
    390 		}
    391 		ifa_acquire(rt->rt_ifa, &psref_ia);
    392 		ia = ifatoia(rt->rt_ifa);
    393 		ifp = rt->rt_ifp;
    394 		if ((mtu = rt->rt_rmx.rmx_mtu) == 0)
    395 			mtu = ifp->if_mtu;
    396 		rt->rt_use++;
    397 		if (rt->rt_flags & RTF_GATEWAY)
    398 			dst = satosin(rt->rt_gateway);
    399 		if (rt->rt_flags & RTF_HOST)
    400 			isbroadcast = rt->rt_flags & RTF_BROADCAST;
    401 		else
    402 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
    403 	}
    404 	rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0;
    405 
    406 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
    407 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
    408 		bool inmgroup;
    409 
    410 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
    411 		    M_BCAST : M_MCAST;
    412 		/*
    413 		 * See if the caller provided any multicast options
    414 		 */
    415 		if (imo != NULL)
    416 			ip->ip_ttl = imo->imo_multicast_ttl;
    417 		else
    418 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
    419 
    420 		/*
    421 		 * if we don't know the outgoing ifp yet, we can't generate
    422 		 * output
    423 		 */
    424 		if (!ifp) {
    425 			IP_STATINC(IP_STAT_NOROUTE);
    426 			error = ENETUNREACH;
    427 			goto bad;
    428 		}
    429 
    430 		/*
    431 		 * If the packet is multicast or broadcast, confirm that
    432 		 * the outgoing interface can transmit it.
    433 		 */
    434 		if (((m->m_flags & M_MCAST) &&
    435 		     (ifp->if_flags & IFF_MULTICAST) == 0) ||
    436 		    ((m->m_flags & M_BCAST) &&
    437 		     (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0))  {
    438 			IP_STATINC(IP_STAT_NOROUTE);
    439 			error = ENETUNREACH;
    440 			goto bad;
    441 		}
    442 		/*
    443 		 * If source address not specified yet, use an address
    444 		 * of outgoing interface.
    445 		 */
    446 		if (in_nullhost(ip->ip_src)) {
    447 			struct in_ifaddr *xia;
    448 			struct ifaddr *xifa;
    449 			struct psref _psref;
    450 
    451 			xia = in_get_ia_from_ifp_psref(ifp, &_psref);
    452 			if (!xia) {
    453 				IP_STATINC(IP_STAT_IFNOADDR);
    454 				error = EADDRNOTAVAIL;
    455 				goto bad;
    456 			}
    457 			xifa = &xia->ia_ifa;
    458 			if (xifa->ifa_getifa != NULL) {
    459 				ia4_release(xia, &_psref);
    460 				/* FIXME ifa_getifa is NOMPSAFE */
    461 				xia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
    462 				if (xia == NULL) {
    463 					IP_STATINC(IP_STAT_IFNOADDR);
    464 					error = EADDRNOTAVAIL;
    465 					goto bad;
    466 				}
    467 				ia4_acquire(xia, &_psref);
    468 			}
    469 			ip->ip_src = xia->ia_addr.sin_addr;
    470 			ia4_release(xia, &_psref);
    471 		}
    472 
    473 		inmgroup = in_multi_group(ip->ip_dst, ifp, flags);
    474 		if (inmgroup && (imo == NULL || imo->imo_multicast_loop)) {
    475 			/*
    476 			 * If we belong to the destination multicast group
    477 			 * on the outgoing interface, and the caller did not
    478 			 * forbid loopback, loop back a copy.
    479 			 */
    480 			ip_mloopback(ifp, m, &udst.sin);
    481 		}
    482 #ifdef MROUTING
    483 		else {
    484 			/*
    485 			 * If we are acting as a multicast router, perform
    486 			 * multicast forwarding as if the packet had just
    487 			 * arrived on the interface to which we are about
    488 			 * to send.  The multicast forwarding function
    489 			 * recursively calls this function, using the
    490 			 * IP_FORWARDING flag to prevent infinite recursion.
    491 			 *
    492 			 * Multicasts that are looped back by ip_mloopback(),
    493 			 * above, will be forwarded by the ip_input() routine,
    494 			 * if necessary.
    495 			 */
    496 			extern struct socket *ip_mrouter;
    497 
    498 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
    499 				if (ip_mforward(m, ifp) != 0) {
    500 					m_freem(m);
    501 					goto done;
    502 				}
    503 			}
    504 		}
    505 #endif
    506 		/*
    507 		 * Multicasts with a time-to-live of zero may be looped-
    508 		 * back, above, but must not be transmitted on a network.
    509 		 * Also, multicasts addressed to the loopback interface
    510 		 * are not sent -- the above call to ip_mloopback() will
    511 		 * loop back a copy if this host actually belongs to the
    512 		 * destination group on the loopback interface.
    513 		 */
    514 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
    515 			IP_STATINC(IP_STAT_ODROPPED);
    516 			m_freem(m);
    517 			goto done;
    518 		}
    519 		goto sendit;
    520 	}
    521 
    522 	/*
    523 	 * If source address not specified yet, use address
    524 	 * of outgoing interface.
    525 	 */
    526 	if (in_nullhost(ip->ip_src)) {
    527 		struct ifaddr *xifa;
    528 
    529 		xifa = &ia->ia_ifa;
    530 		if (xifa->ifa_getifa != NULL) {
    531 			ia4_release(ia, &psref_ia);
    532 			/* FIXME ifa_getifa is NOMPSAFE */
    533 			ia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
    534 			if (ia == NULL) {
    535 				error = EADDRNOTAVAIL;
    536 				goto bad;
    537 			}
    538 			ia4_acquire(ia, &psref_ia);
    539 		}
    540 		ip->ip_src = ia->ia_addr.sin_addr;
    541 	}
    542 
    543 	/*
    544 	 * Packets with Class-D address as source are not valid per
    545 	 * RFC1112.
    546 	 */
    547 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
    548 		IP_STATINC(IP_STAT_ODROPPED);
    549 		error = EADDRNOTAVAIL;
    550 		goto bad;
    551 	}
    552 
    553 	/*
    554 	 * Look for broadcast address and verify user is allowed to
    555 	 * send such a packet.
    556 	 */
    557 	if (isbroadcast) {
    558 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    559 			IP_STATINC(IP_STAT_BCASTDENIED);
    560 			error = EADDRNOTAVAIL;
    561 			goto bad;
    562 		}
    563 		if ((flags & IP_ALLOWBROADCAST) == 0) {
    564 			IP_STATINC(IP_STAT_BCASTDENIED);
    565 			error = EACCES;
    566 			goto bad;
    567 		}
    568 		/* don't allow broadcast messages to be fragmented */
    569 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
    570 			IP_STATINC(IP_STAT_BCASTDENIED);
    571 			error = EMSGSIZE;
    572 			goto bad;
    573 		}
    574 		m->m_flags |= M_BCAST;
    575 	} else
    576 		m->m_flags &= ~M_BCAST;
    577 
    578 sendit:
    579 	if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) {
    580 		if (m->m_pkthdr.len < IP_MINFRAGSIZE) {
    581 			ip->ip_id = 0;
    582 		} else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
    583 			ip->ip_id = ip_newid(ia);
    584 		} else {
    585 			/*
    586 			 * TSO capable interfaces (typically?) increment
    587 			 * ip_id for each segment.
    588 			 * "allocate" enough ids here to increase the chance
    589 			 * for them to be unique.
    590 			 *
    591 			 * note that the following calculation is not
    592 			 * needed to be precise.  wasting some ip_id is fine.
    593 			 */
    594 
    595 			unsigned int segsz = m->m_pkthdr.segsz;
    596 			unsigned int datasz = ntohs(ip->ip_len) - hlen;
    597 			unsigned int num = howmany(datasz, segsz);
    598 
    599 			ip->ip_id = ip_newid_range(ia, num);
    600 		}
    601 	}
    602 	if (ia != NULL) {
    603 		ia4_release(ia, &psref_ia);
    604 		ia = NULL;
    605 	}
    606 
    607 	/*
    608 	 * If we're doing Path MTU Discovery, we need to set DF unless
    609 	 * the route's MTU is locked.
    610 	 */
    611 	if ((flags & IP_MTUDISC) != 0 && rtmtu_nolock) {
    612 		ip->ip_off |= htons(IP_DF);
    613 	}
    614 
    615 #ifdef IPSEC
    616 	if (ipsec_used) {
    617 		bool ipsec_done = false;
    618 		bool count_drop = false;
    619 
    620 		/* Perform IPsec processing, if any. */
    621 		error = ipsec4_output(m, inp, flags, &mtu, &natt_frag,
    622 		    &ipsec_done, &count_drop);
    623 		if (count_drop)
    624 			IP_STATINC(IP_STAT_IPSECDROP_OUT);
    625 		if (error || ipsec_done)
    626 			goto done;
    627 	}
    628 
    629 	if (!ipsec_used || !natt_frag)
    630 #endif
    631 	{
    632 		/*
    633 		 * Run through list of hooks for output packets.
    634 		 */
    635 		error = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_OUT);
    636 		if (error || m == NULL) {
    637 			IP_STATINC(IP_STAT_PFILDROP_OUT);
    638 			goto done;
    639 		}
    640 	}
    641 
    642 	ip = mtod(m, struct ip *);
    643 	hlen = ip->ip_hl << 2;
    644 
    645 	m->m_pkthdr.csum_data |= hlen << 16;
    646 
    647 	/*
    648 	 * search for the source address structure to
    649 	 * maintain output statistics, and verify address
    650 	 * validity
    651 	 */
    652 	KASSERT(ia == NULL);
    653 	sockaddr_in_init(&usrc.sin, &ip->ip_src, 0);
    654 	ifa = ifaof_ifpforaddr_psref(&usrc.sa, ifp, &psref_ia);
    655 	if (ifa != NULL)
    656 		ia = ifatoia(ifa);
    657 
    658 	/*
    659 	 * Ensure we only send from a valid address.
    660 	 * A NULL address is valid because the packet could be
    661 	 * generated from a packet filter.
    662 	 */
    663 	if (ia != NULL && (flags & IP_FORWARDING) == 0 &&
    664 	    (error = ip_ifaddrvalid(ia)) != 0)
    665 	{
    666 		ARPLOG(LOG_ERR,
    667 		    "refusing to send from invalid address %s (pid %d)\n",
    668 		    ARPLOGADDR(&ip->ip_src), curproc->p_pid);
    669 		IP_STATINC(IP_STAT_ODROPPED);
    670 		if (error == 1)
    671 			/*
    672 			 * Address exists, but is tentative or detached.
    673 			 * We can't send from it because it's invalid,
    674 			 * so we drop the packet.
    675 			 */
    676 			error = 0;
    677 		else
    678 			error = EADDRNOTAVAIL;
    679 		goto bad;
    680 	}
    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 	/* Need to fragment the packet */
    689 	if (ntohs(ip->ip_len) > mtu &&
    690 	    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
    691 		goto fragment;
    692 	}
    693 
    694 #if IFA_STATS
    695 	if (ia)
    696 		ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
    697 #endif
    698 	/*
    699 	 * Always initialize the sum to 0!  Some HW assisted
    700 	 * checksumming requires this.
    701 	 */
    702 	ip->ip_sum = 0;
    703 
    704 	if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
    705 		/*
    706 		 * Perform any checksums that the hardware can't do
    707 		 * for us.
    708 		 *
    709 		 * XXX Does any hardware require the {th,uh}_sum
    710 		 * XXX fields to be 0?
    711 		 */
    712 		if (sw_csum & M_CSUM_IPv4) {
    713 			KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
    714 			ip->ip_sum = in_cksum(m, hlen);
    715 			m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
    716 		}
    717 		if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    718 			if (IN_NEED_CHECKSUM(ifp,
    719 			    sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
    720 				in_undefer_cksum_tcpudp(m);
    721 			}
    722 			m->m_pkthdr.csum_flags &=
    723 			    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    724 		}
    725 	}
    726 
    727 	sa = (m->m_flags & M_MCAST) ? sintocsa(rdst) : sintocsa(dst);
    728 
    729 	/* Send it */
    730 	if (__predict_false(sw_csum & M_CSUM_TSOv4)) {
    731 		/*
    732 		 * TSO4 is required by a packet, but disabled for
    733 		 * the interface.
    734 		 */
    735 		error = ip_tso_output(ifp, m, sa, rt);
    736 	} else
    737 		error = ip_if_output(ifp, m, sa, rt);
    738 	goto done;
    739 
    740 fragment:
    741 	/*
    742 	 * We can't use HW checksumming if we're about to fragment the packet.
    743 	 *
    744 	 * XXX Some hardware can do this.
    745 	 */
    746 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    747 		if (IN_NEED_CHECKSUM(ifp,
    748 		    m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
    749 			in_undefer_cksum_tcpudp(m);
    750 		}
    751 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    752 	}
    753 
    754 	/*
    755 	 * Too large for interface; fragment if possible.
    756 	 * Must be able to put at least 8 bytes per fragment.
    757 	 */
    758 	if (ntohs(ip->ip_off) & IP_DF) {
    759 		if (flags & IP_RETURNMTU) {
    760 			KASSERT(inp != NULL);
    761 			in4p_errormtu(inp) = mtu;
    762 		}
    763 		error = EMSGSIZE;
    764 		IP_STATINC(IP_STAT_CANTFRAG);
    765 		goto bad;
    766 	}
    767 
    768 	error = ip_fragment(m, ifp, mtu);
    769 	if (error) {
    770 		m = NULL;
    771 		goto bad;
    772 	}
    773 
    774 	for (; m; m = m0) {
    775 		m0 = m->m_nextpkt;
    776 		m->m_nextpkt = NULL;
    777 		if (error) {
    778 			m_freem(m);
    779 			continue;
    780 		}
    781 #if IFA_STATS
    782 		if (ia)
    783 			ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
    784 #endif
    785 		/*
    786 		 * If we get there, the packet has not been handled by
    787 		 * IPsec whereas it should have. Now that it has been
    788 		 * fragmented, re-inject it in ip_output so that IPsec
    789 		 * processing can occur.
    790 		 */
    791 		if (natt_frag) {
    792 			error = ip_output(m, opt, NULL,
    793 			    flags | IP_RAWOUTPUT | IP_NOIPNEWID,
    794 			    imo, inp);
    795 		} else {
    796 			KASSERT((m->m_pkthdr.csum_flags &
    797 			    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
    798 			error = ip_if_output(ifp, m, (m->m_flags & M_MCAST) ?
    799 			    sintocsa(rdst) : sintocsa(dst), rt);
    800 		}
    801 	}
    802 	if (error == 0) {
    803 		IP_STATINC(IP_STAT_FRAGMENTED);
    804 	}
    805 
    806 done:
    807 	ia4_release(ia, &psref_ia);
    808 	rtcache_unref(rt, ro);
    809 	if (ro == &iproute) {
    810 		rtcache_free(&iproute);
    811 	}
    812 	if (mifp != NULL) {
    813 		if_put(mifp, &psref);
    814 	}
    815 	if (bind_need_restore)
    816 		curlwp_bindx(bound);
    817 	return error;
    818 
    819 bad:
    820 	m_freem(m);
    821 	goto done;
    822 }
    823 
    824 int
    825 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
    826 {
    827 	struct ip *ip, *mhip;
    828 	struct mbuf *m0;
    829 	int len, hlen, off;
    830 	int mhlen, firstlen;
    831 	struct mbuf **mnext;
    832 	int sw_csum = m->m_pkthdr.csum_flags;
    833 	int fragments = 0;
    834 	int error = 0;
    835 	int ipoff, ipflg;
    836 
    837 	ip = mtod(m, struct ip *);
    838 	hlen = ip->ip_hl << 2;
    839 
    840 	/* Preserve the offset and flags. */
    841 	ipoff = ntohs(ip->ip_off) & IP_OFFMASK;
    842 	ipflg = ntohs(ip->ip_off) & (IP_RF|IP_DF|IP_MF);
    843 
    844 	if (ifp != NULL)
    845 		sw_csum &= ~ifp->if_csum_flags_tx;
    846 
    847 	len = (mtu - hlen) &~ 7;
    848 	if (len < 8) {
    849 		IP_STATINC(IP_STAT_CANTFRAG);
    850 		m_freem(m);
    851 		return EMSGSIZE;
    852 	}
    853 
    854 	firstlen = len;
    855 	mnext = &m->m_nextpkt;
    856 
    857 	/*
    858 	 * Loop through length of segment after first fragment,
    859 	 * make new header and copy data of each part and link onto chain.
    860 	 */
    861 	m0 = m;
    862 	mhlen = sizeof(struct ip);
    863 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
    864 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    865 		if (m == NULL) {
    866 			error = ENOBUFS;
    867 			IP_STATINC(IP_STAT_ODROPPED);
    868 			goto sendorfree;
    869 		}
    870 		MCLAIM(m, m0->m_owner);
    871 
    872 		*mnext = m;
    873 		mnext = &m->m_nextpkt;
    874 
    875 		m->m_data += max_linkhdr;
    876 		mhip = mtod(m, struct ip *);
    877 		*mhip = *ip;
    878 
    879 		/* we must inherit the flags */
    880 		m->m_flags |= m0->m_flags & M_COPYFLAGS;
    881 
    882 		if (hlen > sizeof(struct ip)) {
    883 			mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip);
    884 			mhip->ip_hl = mhlen >> 2;
    885 		}
    886 		m->m_len = mhlen;
    887 
    888 		mhip->ip_off = ((off - hlen) >> 3) + ipoff;
    889 		mhip->ip_off |= ipflg;
    890 		if (off + len >= ntohs(ip->ip_len))
    891 			len = ntohs(ip->ip_len) - off;
    892 		else
    893 			mhip->ip_off |= IP_MF;
    894 		HTONS(mhip->ip_off);
    895 
    896 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
    897 		m->m_next = m_copym(m0, off, len, M_DONTWAIT);
    898 		if (m->m_next == NULL) {
    899 			error = ENOBUFS;
    900 			IP_STATINC(IP_STAT_ODROPPED);
    901 			goto sendorfree;
    902 		}
    903 
    904 		m->m_pkthdr.len = mhlen + len;
    905 		m_reset_rcvif(m);
    906 
    907 		mhip->ip_sum = 0;
    908 		KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
    909 		if (sw_csum & M_CSUM_IPv4) {
    910 			mhip->ip_sum = in_cksum(m, mhlen);
    911 		} else {
    912 			/*
    913 			 * checksum is hw-offloaded or not necessary.
    914 			 */
    915 			m->m_pkthdr.csum_flags |=
    916 			    m0->m_pkthdr.csum_flags & M_CSUM_IPv4;
    917 			m->m_pkthdr.csum_data |= mhlen << 16;
    918 			KASSERT(!(ifp != NULL &&
    919 			    IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
    920 			    (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
    921 		}
    922 		IP_STATINC(IP_STAT_OFRAGMENTS);
    923 		fragments++;
    924 	}
    925 
    926 	/*
    927 	 * Update first fragment by trimming what's been copied out
    928 	 * and updating header, then send each fragment (in order).
    929 	 */
    930 	m = m0;
    931 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
    932 	m->m_pkthdr.len = hlen + firstlen;
    933 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
    934 	ip->ip_off |= htons(IP_MF);
    935 	ip->ip_sum = 0;
    936 	if (sw_csum & M_CSUM_IPv4) {
    937 		ip->ip_sum = in_cksum(m, hlen);
    938 		m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
    939 	} else {
    940 		/*
    941 		 * checksum is hw-offloaded or not necessary.
    942 		 */
    943 		KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
    944 		    (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
    945 		KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
    946 		    sizeof(struct ip));
    947 	}
    948 
    949 sendorfree:
    950 	/*
    951 	 * If there is no room for all the fragments, don't queue
    952 	 * any of them.
    953 	 */
    954 	if (ifp != NULL) {
    955 		IFQ_LOCK(&ifp->if_snd);
    956 		if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
    957 		    error == 0) {
    958 			error = ENOBUFS;
    959 			IP_STATINC(IP_STAT_ODROPPED);
    960 			IFQ_INC_DROPS(&ifp->if_snd);
    961 		}
    962 		IFQ_UNLOCK(&ifp->if_snd);
    963 	}
    964 	if (error) {
    965 		for (m = m0; m; m = m0) {
    966 			m0 = m->m_nextpkt;
    967 			m->m_nextpkt = NULL;
    968 			m_freem(m);
    969 		}
    970 	}
    971 
    972 	return error;
    973 }
    974 
    975 /*
    976  * Determine the maximum length of the options to be inserted;
    977  * we would far rather allocate too much space rather than too little.
    978  */
    979 u_int
    980 ip_optlen(struct inpcb *inp)
    981 {
    982 	struct mbuf *m = inp->inp_options;
    983 
    984 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) {
    985 		return (m->m_len - offsetof(struct ipoption, ipopt_dst));
    986 	}
    987 	return 0;
    988 }
    989 
    990 /*
    991  * Insert IP options into preformed packet.
    992  * Adjust IP destination as required for IP source routing,
    993  * as indicated by a non-zero in_addr at the start of the options.
    994  */
    995 static struct mbuf *
    996 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
    997 {
    998 	struct ipoption *p = mtod(opt, struct ipoption *);
    999 	struct mbuf *n;
   1000 	struct ip *ip = mtod(m, struct ip *);
   1001 	unsigned optlen;
   1002 
   1003 	optlen = opt->m_len - sizeof(p->ipopt_dst);
   1004 	KASSERT(optlen % 4 == 0);
   1005 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
   1006 		return m;		/* XXX should fail */
   1007 	if (!in_nullhost(p->ipopt_dst))
   1008 		ip->ip_dst = p->ipopt_dst;
   1009 	if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
   1010 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
   1011 		if (n == NULL)
   1012 			return m;
   1013 		MCLAIM(n, m->m_owner);
   1014 		m_move_pkthdr(n, m);
   1015 		m->m_len -= sizeof(struct ip);
   1016 		m->m_data += sizeof(struct ip);
   1017 		n->m_next = m;
   1018 		n->m_len = optlen + sizeof(struct ip);
   1019 		n->m_data += max_linkhdr;
   1020 		memcpy(mtod(n, void *), ip, sizeof(struct ip));
   1021 		m = n;
   1022 	} else {
   1023 		m->m_data -= optlen;
   1024 		m->m_len += optlen;
   1025 		memmove(mtod(m, void *), ip, sizeof(struct ip));
   1026 	}
   1027 	m->m_pkthdr.len += optlen;
   1028 	ip = mtod(m, struct ip *);
   1029 	memcpy(ip + 1, p->ipopt_list, optlen);
   1030 	*phlen = sizeof(struct ip) + optlen;
   1031 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
   1032 	return m;
   1033 }
   1034 
   1035 /*
   1036  * Copy options from ipsrc to ipdst, omitting those not copied during
   1037  * fragmentation.
   1038  */
   1039 int
   1040 ip_optcopy(struct ip *ipsrc, struct ip *ipdst)
   1041 {
   1042 	u_char *cp, *dp;
   1043 	int opt, optlen, cnt;
   1044 
   1045 	cp = (u_char *)(ipsrc + 1);
   1046 	dp = (u_char *)(ipdst + 1);
   1047 	cnt = (ipsrc->ip_hl << 2) - sizeof(struct ip);
   1048 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   1049 		opt = cp[0];
   1050 		if (opt == IPOPT_EOL)
   1051 			break;
   1052 		if (opt == IPOPT_NOP) {
   1053 			/* Preserve for IP mcast tunnel's LSRR alignment. */
   1054 			*dp++ = IPOPT_NOP;
   1055 			optlen = 1;
   1056 			continue;
   1057 		}
   1058 
   1059 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp));
   1060 		optlen = cp[IPOPT_OLEN];
   1061 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt);
   1062 
   1063 		/* Invalid lengths should have been caught by ip_dooptions. */
   1064 		if (optlen > cnt)
   1065 			optlen = cnt;
   1066 		if (IPOPT_COPIED(opt)) {
   1067 			bcopy((void *)cp, (void *)dp, (unsigned)optlen);
   1068 			dp += optlen;
   1069 		}
   1070 	}
   1071 
   1072 	for (optlen = dp - (u_char *)(ipdst+1); optlen & 0x3; optlen++) {
   1073 		*dp++ = IPOPT_EOL;
   1074 	}
   1075 
   1076 	return optlen;
   1077 }
   1078 
   1079 /*
   1080  * IP socket option processing.
   1081  */
   1082 int
   1083 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
   1084 {
   1085 	struct inpcb *inp = sotoinpcb(so);
   1086 	struct ip *ip = &in4p_ip(inp);
   1087 	int inpflags = inp->inp_flags;
   1088 	int optval = 0, error = 0;
   1089 	struct in_pktinfo pktinfo;
   1090 
   1091 	KASSERT(solocked(so));
   1092 
   1093 	if (sopt->sopt_level != IPPROTO_IP) {
   1094 		if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER)
   1095 			return 0;
   1096 		return ENOPROTOOPT;
   1097 	}
   1098 
   1099 	switch (op) {
   1100 	case PRCO_SETOPT:
   1101 		switch (sopt->sopt_name) {
   1102 		case IP_OPTIONS:
   1103 #ifdef notyet
   1104 		case IP_RETOPTS:
   1105 #endif
   1106 			error = ip_pcbopts(inp, sopt);
   1107 			break;
   1108 
   1109 		case IP_TOS:
   1110 		case IP_TTL:
   1111 		case IP_MINTTL:
   1112 		case IP_RECVOPTS:
   1113 		case IP_RECVRETOPTS:
   1114 		case IP_RECVDSTADDR:
   1115 		case IP_RECVIF:
   1116 		case IP_RECVPKTINFO:
   1117 		case IP_RECVTTL:
   1118 		case IP_BINDANY:
   1119 			error = sockopt_getint(sopt, &optval);
   1120 			if (error)
   1121 				break;
   1122 
   1123 			switch (sopt->sopt_name) {
   1124 			case IP_TOS:
   1125 				ip->ip_tos = optval;
   1126 				break;
   1127 
   1128 			case IP_TTL:
   1129 				ip->ip_ttl = optval;
   1130 				break;
   1131 
   1132 			case IP_MINTTL:
   1133 				if (optval > 0 && optval <= MAXTTL)
   1134 					in4p_ip_minttl(inp) = optval;
   1135 				else
   1136 					error = EINVAL;
   1137 				break;
   1138 #define	OPTSET(bit) \
   1139 	if (optval) \
   1140 		inpflags |= bit; \
   1141 	else \
   1142 		inpflags &= ~bit;
   1143 
   1144 			case IP_RECVOPTS:
   1145 				OPTSET(INP_RECVOPTS);
   1146 				break;
   1147 
   1148 			case IP_RECVPKTINFO:
   1149 				OPTSET(INP_RECVPKTINFO);
   1150 				break;
   1151 
   1152 			case IP_RECVRETOPTS:
   1153 				OPTSET(INP_RECVRETOPTS);
   1154 				break;
   1155 
   1156 			case IP_RECVDSTADDR:
   1157 				OPTSET(INP_RECVDSTADDR);
   1158 				break;
   1159 
   1160 			case IP_RECVIF:
   1161 				OPTSET(INP_RECVIF);
   1162 				break;
   1163 
   1164 			case IP_RECVTTL:
   1165 				OPTSET(INP_RECVTTL);
   1166 				break;
   1167 
   1168 			case IP_BINDANY:
   1169 				error = kauth_authorize_network(
   1170 				    kauth_cred_get(), KAUTH_NETWORK_BIND,
   1171 				    KAUTH_REQ_NETWORK_BIND_ANYADDR, so,
   1172 				    NULL, NULL);
   1173 				if (error == 0) {
   1174 					OPTSET(INP_BINDANY);
   1175 				}
   1176 				break;
   1177 			}
   1178 			break;
   1179 		case IP_PKTINFO:
   1180 			error = sockopt_getint(sopt, &optval);
   1181 			if (!error) {
   1182 				/* Linux compatibility */
   1183 				OPTSET(INP_RECVPKTINFO);
   1184 				break;
   1185 			}
   1186 			error = sockopt_get(sopt, &pktinfo, sizeof(pktinfo));
   1187 			if (error)
   1188 				break;
   1189 
   1190 			if (pktinfo.ipi_ifindex == 0) {
   1191 				in4p_prefsrcip(inp) = pktinfo.ipi_addr;
   1192 				break;
   1193 			}
   1194 
   1195 			/* Solaris compatibility */
   1196 			struct ifnet *ifp;
   1197 			struct in_ifaddr *ia;
   1198 			int s;
   1199 
   1200 			/* pick up primary address */
   1201 			s = pserialize_read_enter();
   1202 			ifp = if_byindex(pktinfo.ipi_ifindex);
   1203 			if (ifp == NULL) {
   1204 				pserialize_read_exit(s);
   1205 				error = EADDRNOTAVAIL;
   1206 				break;
   1207 			}
   1208 			ia = in_get_ia_from_ifp(ifp);
   1209 			if (ia == NULL) {
   1210 				pserialize_read_exit(s);
   1211 				error = EADDRNOTAVAIL;
   1212 				break;
   1213 			}
   1214 			in4p_prefsrcip(inp) = IA_SIN(ia)->sin_addr;
   1215 			pserialize_read_exit(s);
   1216 			break;
   1217 		break;
   1218 #undef OPTSET
   1219 
   1220 		case IP_MULTICAST_IF:
   1221 		case IP_MULTICAST_TTL:
   1222 		case IP_MULTICAST_LOOP:
   1223 		case IP_ADD_MEMBERSHIP:
   1224 		case IP_DROP_MEMBERSHIP:
   1225 			error = ip_setmoptions(&inp->inp_moptions, sopt);
   1226 			break;
   1227 
   1228 		case IP_PORTRANGE:
   1229 			error = sockopt_getint(sopt, &optval);
   1230 			if (error)
   1231 				break;
   1232 
   1233 			switch (optval) {
   1234 			case IP_PORTRANGE_DEFAULT:
   1235 			case IP_PORTRANGE_HIGH:
   1236 				inpflags &= ~(INP_LOWPORT);
   1237 				break;
   1238 
   1239 			case IP_PORTRANGE_LOW:
   1240 				inpflags |= INP_LOWPORT;
   1241 				break;
   1242 
   1243 			default:
   1244 				error = EINVAL;
   1245 				break;
   1246 			}
   1247 			break;
   1248 
   1249 		case IP_PORTALGO:
   1250 			error = sockopt_getint(sopt, &optval);
   1251 			if (error)
   1252 				break;
   1253 
   1254 			error = portalgo_algo_index_select(inp, optval);
   1255 			break;
   1256 
   1257 #if defined(IPSEC)
   1258 		case IP_IPSEC_POLICY:
   1259 			if (ipsec_enabled) {
   1260 				error = ipsec_set_policy(inp,
   1261 				    sopt->sopt_data, sopt->sopt_size,
   1262 				    curlwp->l_cred);
   1263 			} else
   1264 				error = ENOPROTOOPT;
   1265 			break;
   1266 #endif /* IPSEC */
   1267 
   1268 		default:
   1269 			error = ENOPROTOOPT;
   1270 			break;
   1271 		}
   1272 		break;
   1273 
   1274 	case PRCO_GETOPT:
   1275 		switch (sopt->sopt_name) {
   1276 		case IP_OPTIONS:
   1277 		case IP_RETOPTS: {
   1278 			struct mbuf *mopts = inp->inp_options;
   1279 
   1280 			if (mopts) {
   1281 				struct mbuf *m;
   1282 
   1283 				m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT);
   1284 				if (m == NULL) {
   1285 					error = ENOBUFS;
   1286 					break;
   1287 				}
   1288 				error = sockopt_setmbuf(sopt, m);
   1289 			}
   1290 			break;
   1291 		}
   1292 		case IP_TOS:
   1293 		case IP_TTL:
   1294 		case IP_MINTTL:
   1295 		case IP_RECVOPTS:
   1296 		case IP_RECVRETOPTS:
   1297 		case IP_RECVDSTADDR:
   1298 		case IP_RECVIF:
   1299 		case IP_RECVPKTINFO:
   1300 		case IP_RECVTTL:
   1301 		case IP_ERRORMTU:
   1302 		case IP_BINDANY:
   1303 			switch (sopt->sopt_name) {
   1304 			case IP_TOS:
   1305 				optval = ip->ip_tos;
   1306 				break;
   1307 
   1308 			case IP_TTL:
   1309 				optval = ip->ip_ttl;
   1310 				break;
   1311 
   1312 			case IP_MINTTL:
   1313 				optval = in4p_ip_minttl(inp);
   1314 				break;
   1315 
   1316 			case IP_ERRORMTU:
   1317 				optval = in4p_errormtu(inp);
   1318 				break;
   1319 
   1320 #define	OPTBIT(bit)	(inpflags & bit ? 1 : 0)
   1321 
   1322 			case IP_RECVOPTS:
   1323 				optval = OPTBIT(INP_RECVOPTS);
   1324 				break;
   1325 
   1326 			case IP_RECVPKTINFO:
   1327 				optval = OPTBIT(INP_RECVPKTINFO);
   1328 				break;
   1329 
   1330 			case IP_RECVRETOPTS:
   1331 				optval = OPTBIT(INP_RECVRETOPTS);
   1332 				break;
   1333 
   1334 			case IP_RECVDSTADDR:
   1335 				optval = OPTBIT(INP_RECVDSTADDR);
   1336 				break;
   1337 
   1338 			case IP_RECVIF:
   1339 				optval = OPTBIT(INP_RECVIF);
   1340 				break;
   1341 
   1342 			case IP_RECVTTL:
   1343 				optval = OPTBIT(INP_RECVTTL);
   1344 				break;
   1345 
   1346 			case IP_BINDANY:
   1347 				optval = OPTBIT(INP_BINDANY);
   1348 				break;
   1349 			}
   1350 			error = sockopt_setint(sopt, optval);
   1351 			break;
   1352 
   1353 		case IP_PKTINFO:
   1354 			switch (sopt->sopt_size) {
   1355 			case sizeof(int):
   1356 				/* Linux compatibility */
   1357 				optval = OPTBIT(INP_RECVPKTINFO);
   1358 				error = sockopt_setint(sopt, optval);
   1359 				break;
   1360 			case sizeof(struct in_pktinfo):
   1361 				/* Solaris compatibility */
   1362 				pktinfo.ipi_ifindex = 0;
   1363 				pktinfo.ipi_addr = in4p_prefsrcip(inp);
   1364 				error = sockopt_set(sopt, &pktinfo,
   1365 				    sizeof(pktinfo));
   1366 				break;
   1367 			default:
   1368 				/*
   1369 				 * While size is stuck at 0, and, later, if
   1370 				 * the caller doesn't use an exactly sized
   1371 				 * recipient for the data, default to Linux
   1372 				 * compatibility
   1373 				 */
   1374 				optval = OPTBIT(INP_RECVPKTINFO);
   1375 				error = sockopt_setint(sopt, optval);
   1376 				break;
   1377 			}
   1378 			break;
   1379 
   1380 #if 0	/* defined(IPSEC) */
   1381 		case IP_IPSEC_POLICY:
   1382 		{
   1383 			struct mbuf *m = NULL;
   1384 
   1385 			/* XXX this will return EINVAL as sopt is empty */
   1386 			error = ipsec_get_policy(inp, sopt->sopt_data,
   1387 			    sopt->sopt_size, &m);
   1388 			if (error == 0)
   1389 				error = sockopt_setmbuf(sopt, m);
   1390 			break;
   1391 		}
   1392 #endif /*IPSEC*/
   1393 
   1394 		case IP_MULTICAST_IF:
   1395 		case IP_MULTICAST_TTL:
   1396 		case IP_MULTICAST_LOOP:
   1397 		case IP_ADD_MEMBERSHIP:
   1398 		case IP_DROP_MEMBERSHIP:
   1399 			error = ip_getmoptions(inp->inp_moptions, sopt);
   1400 			break;
   1401 
   1402 		case IP_PORTRANGE:
   1403 			if (inpflags & INP_LOWPORT)
   1404 				optval = IP_PORTRANGE_LOW;
   1405 			else
   1406 				optval = IP_PORTRANGE_DEFAULT;
   1407 			error = sockopt_setint(sopt, optval);
   1408 			break;
   1409 
   1410 		case IP_PORTALGO:
   1411 			optval = inp->inp_portalgo;
   1412 			error = sockopt_setint(sopt, optval);
   1413 			break;
   1414 
   1415 		default:
   1416 			error = ENOPROTOOPT;
   1417 			break;
   1418 		}
   1419 		break;
   1420 	}
   1421 
   1422 	if (!error) {
   1423 		inp->inp_flags = inpflags;
   1424 	}
   1425 	return error;
   1426 }
   1427 
   1428 static int
   1429 ip_pktinfo_prepare(const struct inpcb *inp, const struct in_pktinfo *pktinfo,
   1430     struct ip_pktopts *pktopts, int *flags, kauth_cred_t cred)
   1431 {
   1432 	struct ip_moptions *imo;
   1433 	int error = 0;
   1434 	bool addrset = false;
   1435 
   1436 	if (!in_nullhost(pktinfo->ipi_addr)) {
   1437 		pktopts->ippo_laddr.sin_addr = pktinfo->ipi_addr;
   1438 		/* EADDRNOTAVAIL? */
   1439 		error = inpcb_bindableaddr(inp, &pktopts->ippo_laddr, cred);
   1440 		if (error != 0)
   1441 			return error;
   1442 		addrset = true;
   1443 	}
   1444 
   1445 	if (pktinfo->ipi_ifindex != 0) {
   1446 		if (!addrset) {
   1447 			struct ifnet *ifp;
   1448 			struct in_ifaddr *ia;
   1449 			int s;
   1450 
   1451 			/* pick up primary address */
   1452 			s = pserialize_read_enter();
   1453 			ifp = if_byindex(pktinfo->ipi_ifindex);
   1454 			if (ifp == NULL) {
   1455 				pserialize_read_exit(s);
   1456 				return EADDRNOTAVAIL;
   1457 			}
   1458 			ia = in_get_ia_from_ifp(ifp);
   1459 			if (ia == NULL) {
   1460 				pserialize_read_exit(s);
   1461 				return EADDRNOTAVAIL;
   1462 			}
   1463 			pktopts->ippo_laddr.sin_addr = IA_SIN(ia)->sin_addr;
   1464 			pserialize_read_exit(s);
   1465 		}
   1466 
   1467 		/*
   1468 		 * If specified ipi_ifindex,
   1469 		 * use copied or locally initialized ip_moptions.
   1470 		 * Original ip_moptions must not be modified.
   1471 		 */
   1472 		imo = &pktopts->ippo_imobuf;	/* local buf in pktopts */
   1473 		if (pktopts->ippo_imo != NULL) {
   1474 			memcpy(imo, pktopts->ippo_imo, sizeof(*imo));
   1475 		} else {
   1476 			memset(imo, 0, sizeof(*imo));
   1477 			imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
   1478 			imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
   1479 		}
   1480 		imo->imo_multicast_if_index = pktinfo->ipi_ifindex;
   1481 		pktopts->ippo_imo = imo;
   1482 		*flags |= IP_ROUTETOIFINDEX;
   1483 	}
   1484 	return error;
   1485 }
   1486 
   1487 /*
   1488  * Set up IP outgoing packet options. Even if control is NULL,
   1489  * pktopts->ippo_laddr and pktopts->ippo_imo are set and used.
   1490  */
   1491 int
   1492 ip_setpktopts(struct mbuf *control, struct ip_pktopts *pktopts, int *flags,
   1493     struct inpcb *inp, kauth_cred_t cred)
   1494 {
   1495 	struct cmsghdr *cm;
   1496 	struct in_pktinfo pktinfo;
   1497 	int error;
   1498 
   1499 	pktopts->ippo_imo = inp->inp_moptions;
   1500 
   1501 	struct in_addr *ia = in_nullhost(in4p_prefsrcip(inp)) ? &in4p_laddr(inp) :
   1502 	    &in4p_prefsrcip(inp);
   1503 	sockaddr_in_init(&pktopts->ippo_laddr, ia, 0);
   1504 
   1505 	if (control == NULL)
   1506 		return 0;
   1507 
   1508 	/*
   1509 	 * XXX: Currently, we assume all the optional information is
   1510 	 * stored in a single mbuf.
   1511 	 */
   1512 	if (control->m_next)
   1513 		return EINVAL;
   1514 
   1515 	for (; control->m_len > 0;
   1516 	    control->m_data += CMSG_ALIGN(cm->cmsg_len),
   1517 	    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
   1518 		cm = mtod(control, struct cmsghdr *);
   1519 		if ((control->m_len < sizeof(*cm)) ||
   1520 		    (cm->cmsg_len == 0) ||
   1521 		    (cm->cmsg_len > control->m_len)) {
   1522 			return EINVAL;
   1523 		}
   1524 		if (cm->cmsg_level != IPPROTO_IP)
   1525 			continue;
   1526 
   1527 		switch (cm->cmsg_type) {
   1528 		case IP_PKTINFO:
   1529 			if (cm->cmsg_len != CMSG_LEN(sizeof(pktinfo)))
   1530 				return EINVAL;
   1531 			memcpy(&pktinfo, CMSG_DATA(cm), sizeof(pktinfo));
   1532 			error = ip_pktinfo_prepare(inp, &pktinfo, pktopts,
   1533 			    flags, cred);
   1534 			if (error)
   1535 				return error;
   1536 			break;
   1537 		case IP_SENDSRCADDR: /* FreeBSD compatibility */
   1538 			if (cm->cmsg_len != CMSG_LEN(sizeof(struct in_addr)))
   1539 				return EINVAL;
   1540 			pktinfo.ipi_ifindex = 0;
   1541 			pktinfo.ipi_addr =
   1542 			    ((struct in_pktinfo *)CMSG_DATA(cm))->ipi_addr;
   1543 			error = ip_pktinfo_prepare(inp, &pktinfo, pktopts,
   1544 			    flags, cred);
   1545 			if (error)
   1546 				return error;
   1547 			break;
   1548 		default:
   1549 			return ENOPROTOOPT;
   1550 		}
   1551 	}
   1552 	return 0;
   1553 }
   1554 
   1555 /*
   1556  * Set up IP options in pcb for insertion in output packets.
   1557  * Store in mbuf with pointer in pcbopt, adding pseudo-option
   1558  * with destination address if source routed.
   1559  */
   1560 static int
   1561 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt)
   1562 {
   1563 	struct mbuf *m;
   1564 	const u_char *cp;
   1565 	u_char *dp;
   1566 	int cnt;
   1567 
   1568 	KASSERT(inp_locked(inp));
   1569 
   1570 	/* Turn off any old options. */
   1571 	if (inp->inp_options) {
   1572 		m_free(inp->inp_options);
   1573 	}
   1574 	inp->inp_options = NULL;
   1575 	if ((cnt = sopt->sopt_size) == 0) {
   1576 		/* Only turning off any previous options. */
   1577 		return 0;
   1578 	}
   1579 	cp = sopt->sopt_data;
   1580 
   1581 	if (cnt % 4) {
   1582 		/* Must be 4-byte aligned, because there's no padding. */
   1583 		return EINVAL;
   1584 	}
   1585 
   1586 	m = m_get(M_DONTWAIT, MT_SOOPTS);
   1587 	if (m == NULL)
   1588 		return ENOBUFS;
   1589 
   1590 	dp = mtod(m, u_char *);
   1591 	memset(dp, 0, sizeof(struct in_addr));
   1592 	dp += sizeof(struct in_addr);
   1593 	m->m_len = sizeof(struct in_addr);
   1594 
   1595 	/*
   1596 	 * IP option list according to RFC791. Each option is of the form
   1597 	 *
   1598 	 *	[optval] [olen] [(olen - 2) data bytes]
   1599 	 *
   1600 	 * We validate the list and copy options to an mbuf for prepending
   1601 	 * to data packets. The IP first-hop destination address will be
   1602 	 * stored before actual options and is zero if unset.
   1603 	 */
   1604 	while (cnt > 0) {
   1605 		uint8_t optval, olen, offset;
   1606 
   1607 		optval = cp[IPOPT_OPTVAL];
   1608 
   1609 		if (optval == IPOPT_EOL || optval == IPOPT_NOP) {
   1610 			olen = 1;
   1611 		} else {
   1612 			if (cnt < IPOPT_OLEN + 1)
   1613 				goto bad;
   1614 
   1615 			olen = cp[IPOPT_OLEN];
   1616 			if (olen < IPOPT_OLEN + 1 || olen > cnt)
   1617 				goto bad;
   1618 		}
   1619 
   1620 		if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) {
   1621 			/*
   1622 			 * user process specifies route as:
   1623 			 *	->A->B->C->D
   1624 			 * D must be our final destination (but we can't
   1625 			 * check that since we may not have connected yet).
   1626 			 * A is first hop destination, which doesn't appear in
   1627 			 * actual IP option, but is stored before the options.
   1628 			 */
   1629 			if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr))
   1630 				goto bad;
   1631 
   1632 			offset = cp[IPOPT_OFFSET];
   1633 			memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1,
   1634 			    sizeof(struct in_addr));
   1635 
   1636 			cp += sizeof(struct in_addr);
   1637 			cnt -= sizeof(struct in_addr);
   1638 			olen -= sizeof(struct in_addr);
   1639 
   1640 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
   1641 				goto bad;
   1642 
   1643 			memcpy(dp, cp, olen);
   1644 			dp[IPOPT_OPTVAL] = optval;
   1645 			dp[IPOPT_OLEN] = olen;
   1646 			dp[IPOPT_OFFSET] = offset;
   1647 			break;
   1648 		} else {
   1649 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
   1650 				goto bad;
   1651 
   1652 			memcpy(dp, cp, olen);
   1653 			break;
   1654 		}
   1655 
   1656 		dp += olen;
   1657 		m->m_len += olen;
   1658 
   1659 		if (optval == IPOPT_EOL)
   1660 			break;
   1661 
   1662 		cp += olen;
   1663 		cnt -= olen;
   1664 	}
   1665 
   1666 	inp->inp_options = m;
   1667 	return 0;
   1668 
   1669 bad:
   1670 	(void)m_free(m);
   1671 	return EINVAL;
   1672 }
   1673 
   1674 /*
   1675  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
   1676  * Must be called in a pserialize critical section.
   1677  */
   1678 static struct ifnet *
   1679 ip_multicast_if(struct in_addr *a, int *ifindexp)
   1680 {
   1681 	int ifindex;
   1682 	struct ifnet *ifp = NULL;
   1683 	struct in_ifaddr *ia;
   1684 
   1685 	if (ifindexp)
   1686 		*ifindexp = 0;
   1687 	if (ntohl(a->s_addr) >> 24 == 0) {
   1688 		ifindex = ntohl(a->s_addr) & 0xffffff;
   1689 		ifp = if_byindex(ifindex);
   1690 		if (!ifp)
   1691 			return NULL;
   1692 		if (ifindexp)
   1693 			*ifindexp = ifindex;
   1694 	} else {
   1695 		IN_ADDRHASH_READER_FOREACH(ia, a->s_addr) {
   1696 			if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
   1697 			    (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
   1698 				ifp = ia->ia_ifp;
   1699 				if (if_is_deactivated(ifp))
   1700 					ifp = NULL;
   1701 				break;
   1702 			}
   1703 		}
   1704 	}
   1705 	return ifp;
   1706 }
   1707 
   1708 static int
   1709 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval)
   1710 {
   1711 	u_int tval;
   1712 	u_char cval;
   1713 	int error;
   1714 
   1715 	if (sopt == NULL)
   1716 		return EINVAL;
   1717 
   1718 	switch (sopt->sopt_size) {
   1719 	case sizeof(u_char):
   1720 		error = sockopt_get(sopt, &cval, sizeof(u_char));
   1721 		tval = cval;
   1722 		break;
   1723 
   1724 	case sizeof(u_int):
   1725 		error = sockopt_get(sopt, &tval, sizeof(u_int));
   1726 		break;
   1727 
   1728 	default:
   1729 		error = EINVAL;
   1730 	}
   1731 
   1732 	if (error)
   1733 		return error;
   1734 
   1735 	if (tval > maxval)
   1736 		return EINVAL;
   1737 
   1738 	*val = tval;
   1739 	return 0;
   1740 }
   1741 
   1742 static int
   1743 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp,
   1744     struct psref *psref, struct in_addr *ia, bool add)
   1745 {
   1746 	int error;
   1747 	struct ip_mreq mreq;
   1748 
   1749 	error = sockopt_get(sopt, &mreq, sizeof(mreq));
   1750 	if (error)
   1751 		return error;
   1752 
   1753 	if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr))
   1754 		return EINVAL;
   1755 
   1756 	memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia));
   1757 
   1758 	if (in_nullhost(mreq.imr_interface)) {
   1759 		union {
   1760 			struct sockaddr		dst;
   1761 			struct sockaddr_in	dst4;
   1762 		} u;
   1763 		struct route ro;
   1764 
   1765 		if (!add) {
   1766 			*ifp = NULL;
   1767 			return 0;
   1768 		}
   1769 		/*
   1770 		 * If no interface address was provided, use the interface of
   1771 		 * the route to the given multicast address.
   1772 		 */
   1773 		struct rtentry *rt;
   1774 		memset(&ro, 0, sizeof(ro));
   1775 
   1776 		sockaddr_in_init(&u.dst4, ia, 0);
   1777 		error = rtcache_setdst(&ro, &u.dst);
   1778 		if (error != 0)
   1779 			return error;
   1780 		*ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL;
   1781 		if (*ifp != NULL) {
   1782 			if (if_is_deactivated(*ifp))
   1783 				*ifp = NULL;
   1784 			else
   1785 				if_acquire(*ifp, psref);
   1786 		}
   1787 		rtcache_unref(rt, &ro);
   1788 		rtcache_free(&ro);
   1789 	} else {
   1790 		int s = pserialize_read_enter();
   1791 		*ifp = ip_multicast_if(&mreq.imr_interface, NULL);
   1792 		if (!add && *ifp == NULL) {
   1793 			pserialize_read_exit(s);
   1794 			return EADDRNOTAVAIL;
   1795 		}
   1796 		if (*ifp != NULL) {
   1797 			if (if_is_deactivated(*ifp))
   1798 				*ifp = NULL;
   1799 			else
   1800 				if_acquire(*ifp, psref);
   1801 		}
   1802 		pserialize_read_exit(s);
   1803 	}
   1804 	return 0;
   1805 }
   1806 
   1807 /*
   1808  * Add a multicast group membership.
   1809  * Group must be a valid IP multicast address.
   1810  */
   1811 static int
   1812 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt)
   1813 {
   1814 	struct ifnet *ifp = NULL;	// XXX: gcc [ppc]
   1815 	struct in_addr ia;
   1816 	int i, error, bound;
   1817 	struct psref psref;
   1818 
   1819 	/* imo is protected by solock or referenced only by the caller */
   1820 
   1821 	bound = curlwp_bind();
   1822 	if (sopt->sopt_size == sizeof(struct ip_mreq))
   1823 		error = ip_get_membership(sopt, &ifp, &psref, &ia, true);
   1824 	else {
   1825 #ifdef INET6
   1826 		error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
   1827 #else
   1828 		error = EINVAL;
   1829 #endif
   1830 	}
   1831 
   1832 	if (error)
   1833 		goto out;
   1834 
   1835 	/*
   1836 	 * See if we found an interface, and confirm that it
   1837 	 * supports multicast.
   1838 	 */
   1839 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1840 		error = EADDRNOTAVAIL;
   1841 		goto out;
   1842 	}
   1843 
   1844 	/*
   1845 	 * See if the membership already exists or if all the
   1846 	 * membership slots are full.
   1847 	 */
   1848 	for (i = 0; i < imo->imo_num_memberships; ++i) {
   1849 		if (imo->imo_membership[i]->inm_ifp == ifp &&
   1850 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
   1851 			break;
   1852 	}
   1853 	if (i < imo->imo_num_memberships) {
   1854 		error = EADDRINUSE;
   1855 		goto out;
   1856 	}
   1857 
   1858 	if (i == IP_MAX_MEMBERSHIPS) {
   1859 		error = ETOOMANYREFS;
   1860 		goto out;
   1861 	}
   1862 
   1863 	/*
   1864 	 * Everything looks good; add a new record to the multicast
   1865 	 * address list for the given interface.
   1866 	 */
   1867 	imo->imo_membership[i] = in_addmulti(&ia, ifp);
   1868 	if (imo->imo_membership[i] == NULL) {
   1869 		error = ENOBUFS;
   1870 		goto out;
   1871 	}
   1872 
   1873 	++imo->imo_num_memberships;
   1874 	error = 0;
   1875 out:
   1876 	if_put(ifp, &psref);
   1877 	curlwp_bindx(bound);
   1878 	return error;
   1879 }
   1880 
   1881 /*
   1882  * Drop a multicast group membership.
   1883  * Group must be a valid IP multicast address.
   1884  */
   1885 static int
   1886 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt)
   1887 {
   1888 	struct in_addr ia = { .s_addr = 0 };	// XXX: gcc [ppc]
   1889 	struct ifnet *ifp = NULL;		// XXX: gcc [ppc]
   1890 	int i, error, bound;
   1891 	struct psref psref;
   1892 
   1893 	/* imo is protected by solock or referenced only by the caller */
   1894 
   1895 	bound = curlwp_bind();
   1896 	if (sopt->sopt_size == sizeof(struct ip_mreq))
   1897 		error = ip_get_membership(sopt, &ifp, &psref, &ia, false);
   1898 	else {
   1899 #ifdef INET6
   1900 		error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
   1901 #else
   1902 		error = EINVAL;
   1903 #endif
   1904 	}
   1905 
   1906 	if (error)
   1907 		goto out;
   1908 
   1909 	/*
   1910 	 * Find the membership in the membership array.
   1911 	 */
   1912 	for (i = 0; i < imo->imo_num_memberships; ++i) {
   1913 		if ((ifp == NULL ||
   1914 		     imo->imo_membership[i]->inm_ifp == ifp) &&
   1915 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
   1916 			break;
   1917 	}
   1918 	if (i == imo->imo_num_memberships) {
   1919 		error = EADDRNOTAVAIL;
   1920 		goto out;
   1921 	}
   1922 
   1923 	/*
   1924 	 * Give up the multicast address record to which the
   1925 	 * membership points.
   1926 	 */
   1927 	in_delmulti(imo->imo_membership[i]);
   1928 
   1929 	/*
   1930 	 * Remove the gap in the membership array.
   1931 	 */
   1932 	for (++i; i < imo->imo_num_memberships; ++i)
   1933 		imo->imo_membership[i-1] = imo->imo_membership[i];
   1934 	--imo->imo_num_memberships;
   1935 	error = 0;
   1936 out:
   1937 	if_put(ifp, &psref);
   1938 	curlwp_bindx(bound);
   1939 	return error;
   1940 }
   1941 
   1942 /*
   1943  * Set the IP multicast options in response to user setsockopt().
   1944  */
   1945 int
   1946 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt)
   1947 {
   1948 	struct ip_moptions *imo = *pimo;
   1949 	struct in_addr addr;
   1950 	struct ifnet *ifp;
   1951 	int ifindex, error = 0;
   1952 
   1953 	/* The passed imo isn't NULL, it should be protected by solock */
   1954 
   1955 	if (!imo) {
   1956 		/*
   1957 		 * No multicast option buffer attached to the pcb;
   1958 		 * allocate one and initialize to default values.
   1959 		 */
   1960 		imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP);
   1961 		if (imo == NULL)
   1962 			return ENOBUFS;
   1963 
   1964 		imo->imo_multicast_if_index = 0;
   1965 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
   1966 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
   1967 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
   1968 		imo->imo_num_memberships = 0;
   1969 		*pimo = imo;
   1970 	}
   1971 
   1972 	switch (sopt->sopt_name) {
   1973 	case IP_MULTICAST_IF: {
   1974 		int s;
   1975 		/*
   1976 		 * Select the interface for outgoing multicast packets.
   1977 		 */
   1978 		error = sockopt_get(sopt, &addr, sizeof(addr));
   1979 		if (error)
   1980 			break;
   1981 
   1982 		/*
   1983 		 * INADDR_ANY is used to remove a previous selection.
   1984 		 * When no interface is selected, a default one is
   1985 		 * chosen every time a multicast packet is sent.
   1986 		 */
   1987 		if (in_nullhost(addr)) {
   1988 			imo->imo_multicast_if_index = 0;
   1989 			break;
   1990 		}
   1991 		/*
   1992 		 * The selected interface is identified by its local
   1993 		 * IP address.  Find the interface and confirm that
   1994 		 * it supports multicasting.
   1995 		 */
   1996 		s = pserialize_read_enter();
   1997 		ifp = ip_multicast_if(&addr, &ifindex);
   1998 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1999 			pserialize_read_exit(s);
   2000 			error = EADDRNOTAVAIL;
   2001 			break;
   2002 		}
   2003 		imo->imo_multicast_if_index = ifp->if_index;
   2004 		pserialize_read_exit(s);
   2005 		if (ifindex)
   2006 			imo->imo_multicast_addr = addr;
   2007 		else
   2008 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
   2009 		break;
   2010 	    }
   2011 
   2012 	case IP_MULTICAST_TTL:
   2013 		/*
   2014 		 * Set the IP time-to-live for outgoing multicast packets.
   2015 		 */
   2016 		error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL);
   2017 		break;
   2018 
   2019 	case IP_MULTICAST_LOOP:
   2020 		/*
   2021 		 * Set the loopback flag for outgoing multicast packets.
   2022 		 * Must be zero or one.
   2023 		 */
   2024 		error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1);
   2025 		break;
   2026 
   2027 	case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */
   2028 		error = ip_add_membership(imo, sopt);
   2029 		break;
   2030 
   2031 	case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */
   2032 		error = ip_drop_membership(imo, sopt);
   2033 		break;
   2034 
   2035 	default:
   2036 		error = EOPNOTSUPP;
   2037 		break;
   2038 	}
   2039 
   2040 	/*
   2041 	 * If all options have default values, no need to keep the mbuf.
   2042 	 */
   2043 	if (imo->imo_multicast_if_index == 0 &&
   2044 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
   2045 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
   2046 	    imo->imo_num_memberships == 0) {
   2047 		kmem_intr_free(imo, sizeof(*imo));
   2048 		*pimo = NULL;
   2049 	}
   2050 
   2051 	return error;
   2052 }
   2053 
   2054 /*
   2055  * Return the IP multicast options in response to user getsockopt().
   2056  */
   2057 int
   2058 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt)
   2059 {
   2060 	struct in_addr addr;
   2061 	uint8_t optval;
   2062 	int error = 0;
   2063 
   2064 	/* imo is protected by solock or referenced only by the caller */
   2065 
   2066 	switch (sopt->sopt_name) {
   2067 	case IP_MULTICAST_IF:
   2068 		if (imo == NULL || imo->imo_multicast_if_index == 0)
   2069 			addr = zeroin_addr;
   2070 		else if (imo->imo_multicast_addr.s_addr) {
   2071 			/* return the value user has set */
   2072 			addr = imo->imo_multicast_addr;
   2073 		} else {
   2074 			struct ifnet *ifp;
   2075 			struct in_ifaddr *ia = NULL;
   2076 			int s = pserialize_read_enter();
   2077 
   2078 			ifp = if_byindex(imo->imo_multicast_if_index);
   2079 			if (ifp != NULL) {
   2080 				ia = in_get_ia_from_ifp(ifp);
   2081 			}
   2082 			addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
   2083 			pserialize_read_exit(s);
   2084 		}
   2085 		error = sockopt_set(sopt, &addr, sizeof(addr));
   2086 		break;
   2087 
   2088 	case IP_MULTICAST_TTL:
   2089 		optval = imo ? imo->imo_multicast_ttl
   2090 		    : IP_DEFAULT_MULTICAST_TTL;
   2091 
   2092 		error = sockopt_set(sopt, &optval, sizeof(optval));
   2093 		break;
   2094 
   2095 	case IP_MULTICAST_LOOP:
   2096 		optval = imo ? imo->imo_multicast_loop
   2097 		    : IP_DEFAULT_MULTICAST_LOOP;
   2098 
   2099 		error = sockopt_set(sopt, &optval, sizeof(optval));
   2100 		break;
   2101 
   2102 	default:
   2103 		error = EOPNOTSUPP;
   2104 	}
   2105 
   2106 	return error;
   2107 }
   2108 
   2109 /*
   2110  * Discard the IP multicast options.
   2111  */
   2112 void
   2113 ip_freemoptions(struct ip_moptions *imo)
   2114 {
   2115 	int i;
   2116 
   2117 	/* The owner of imo (inp) should be protected by solock */
   2118 
   2119 	if (imo != NULL) {
   2120 		for (i = 0; i < imo->imo_num_memberships; ++i) {
   2121 			struct in_multi *inm = imo->imo_membership[i];
   2122 			in_delmulti(inm);
   2123 			/* ifp should not leave thanks to solock */
   2124 		}
   2125 
   2126 		kmem_intr_free(imo, sizeof(*imo));
   2127 	}
   2128 }
   2129 
   2130 /*
   2131  * Routine called from ip_output() to loop back a copy of an IP multicast
   2132  * packet to the input queue of a specified interface.  Note that this
   2133  * calls the output routine of the loopback "driver", but with an interface
   2134  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
   2135  */
   2136 static void
   2137 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst)
   2138 {
   2139 	struct ip *ip;
   2140 	struct mbuf *copym;
   2141 
   2142 	copym = m_copypacket(m, M_DONTWAIT);
   2143 	if (copym != NULL &&
   2144 	    (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
   2145 		copym = m_pullup(copym, sizeof(struct ip));
   2146 	if (copym == NULL)
   2147 		return;
   2148 	/*
   2149 	 * We don't bother to fragment if the IP length is greater
   2150 	 * than the interface's MTU.  Can this possibly matter?
   2151 	 */
   2152 	ip = mtod(copym, struct ip *);
   2153 
   2154 	if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
   2155 		in_undefer_cksum_tcpudp(copym);
   2156 		copym->m_pkthdr.csum_flags &=
   2157 		    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
   2158 	}
   2159 
   2160 	ip->ip_sum = 0;
   2161 	ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
   2162 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
   2163 	(void)looutput(ifp, copym, sintocsa(dst), NULL);
   2164 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
   2165 }
   2166 
   2167 /*
   2168  * Ensure sending address is valid.
   2169  * Returns 0 on success, -1 if an error should be sent back or 1
   2170  * if the packet could be dropped without error (protocol dependent).
   2171  */
   2172 static int
   2173 ip_ifaddrvalid(const struct in_ifaddr *ia)
   2174 {
   2175 
   2176 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
   2177 		return 0;
   2178 
   2179 	if (ia->ia4_flags & IN_IFF_DUPLICATED)
   2180 		return -1;
   2181 	else if (ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED))
   2182 		return 1;
   2183 
   2184 	return 0;
   2185 }
   2186