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