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