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