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