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