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