Home | History | Annotate | Line # | Download | only in netinet
ip_output.c revision 1.165
      1 /*	$NetBSD: ip_output.c,v 1.165 2006/07/23 22:06:13 ad 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  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1988, 1990, 1993
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. Neither the name of the University nor the names of its contributors
     82  *    may be used to endorse or promote products derived from this software
     83  *    without specific prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
     98  */
     99 
    100 #include <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.165 2006/07/23 22:06:13 ad Exp $");
    102 
    103 #include "opt_pfil_hooks.h"
    104 #include "opt_inet.h"
    105 #include "opt_ipsec.h"
    106 #include "opt_mrouting.h"
    107 
    108 #include <sys/param.h>
    109 #include <sys/malloc.h>
    110 #include <sys/mbuf.h>
    111 #include <sys/errno.h>
    112 #include <sys/protosw.h>
    113 #include <sys/socket.h>
    114 #include <sys/socketvar.h>
    115 #include <sys/kauth.h>
    116 #ifdef FAST_IPSEC
    117 #include <sys/domain.h>
    118 #endif
    119 #include <sys/systm.h>
    120 #include <sys/proc.h>
    121 
    122 #include <net/if.h>
    123 #include <net/route.h>
    124 #include <net/pfil.h>
    125 
    126 #include <netinet/in.h>
    127 #include <netinet/in_systm.h>
    128 #include <netinet/ip.h>
    129 #include <netinet/in_pcb.h>
    130 #include <netinet/in_var.h>
    131 #include <netinet/ip_var.h>
    132 #include <netinet/in_offload.h>
    133 
    134 #ifdef MROUTING
    135 #include <netinet/ip_mroute.h>
    136 #endif
    137 
    138 #include <machine/stdarg.h>
    139 
    140 #ifdef IPSEC
    141 #include <netinet6/ipsec.h>
    142 #include <netkey/key.h>
    143 #include <netkey/key_debug.h>
    144 #endif /*IPSEC*/
    145 
    146 #ifdef FAST_IPSEC
    147 #include <netipsec/ipsec.h>
    148 #include <netipsec/key.h>
    149 #include <netipsec/xform.h>
    150 #endif	/* FAST_IPSEC*/
    151 
    152 #ifdef IPSEC_NAT_T
    153 #include <netinet/udp.h>
    154 #endif
    155 
    156 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
    157 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
    158 static void ip_mloopback(struct ifnet *, struct mbuf *, struct sockaddr_in *);
    159 static int ip_getoptval(struct mbuf *, u_int8_t *, u_int);
    160 
    161 #ifdef PFIL_HOOKS
    162 extern struct pfil_head inet_pfil_hook;			/* XXX */
    163 #endif
    164 
    165 int	ip_do_loopback_cksum = 0;
    166 
    167 #define	IN_NEED_CHECKSUM(ifp, csum_flags) \
    168 	(__predict_true(((ifp)->if_flags & IFF_LOOPBACK) == 0 || \
    169 	(((csum_flags) & M_CSUM_UDPv4) != 0 && udp_do_loopback_cksum) || \
    170 	(((csum_flags) & M_CSUM_TCPv4) != 0 && tcp_do_loopback_cksum) || \
    171 	(((csum_flags) & M_CSUM_IPv4) != 0 && ip_do_loopback_cksum)))
    172 
    173 struct ip_tso_output_args {
    174 	struct ifnet *ifp;
    175 	struct sockaddr *sa;
    176 	struct rtentry *rt;
    177 };
    178 
    179 static int ip_tso_output_callback(void *, struct mbuf *);
    180 static int ip_tso_output(struct ifnet *, struct mbuf *, struct sockaddr *,
    181     struct rtentry *);
    182 
    183 static int
    184 ip_tso_output_callback(void *vp, struct mbuf *m)
    185 {
    186 	struct ip_tso_output_args *args = vp;
    187 	struct ifnet *ifp = args->ifp;
    188 
    189 	return (*ifp->if_output)(ifp, m, args->sa, args->rt);
    190 }
    191 
    192 static int
    193 ip_tso_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
    194     struct rtentry *rt)
    195 {
    196 	struct ip_tso_output_args args;
    197 
    198 	args.ifp = ifp;
    199 	args.sa = sa;
    200 	args.rt = rt;
    201 
    202 	return tcp4_segment(m, ip_tso_output_callback, &args);
    203 }
    204 
    205 /*
    206  * IP output.  The packet in mbuf chain m contains a skeletal IP
    207  * header (with len, off, ttl, proto, tos, src, dst).
    208  * The mbuf chain containing the packet will be freed.
    209  * The mbuf opt, if present, will not be freed.
    210  */
    211 int
    212 ip_output(struct mbuf *m0, ...)
    213 {
    214 	struct ip *ip;
    215 	struct ifnet *ifp;
    216 	struct mbuf *m = m0;
    217 	int hlen = sizeof (struct ip);
    218 	int len, error = 0;
    219 	struct route iproute;
    220 	struct sockaddr_in *dst;
    221 	struct in_ifaddr *ia;
    222 	struct mbuf *opt;
    223 	struct route *ro;
    224 	int flags, sw_csum;
    225 	int *mtu_p;
    226 	u_long mtu;
    227 	struct ip_moptions *imo;
    228 	struct socket *so;
    229 	va_list ap;
    230 #ifdef IPSEC_NAT_T
    231 	int natt_frag = 0;
    232 #endif
    233 #ifdef IPSEC
    234 	struct secpolicy *sp = NULL;
    235 #endif /*IPSEC*/
    236 #ifdef FAST_IPSEC
    237 	struct inpcb *inp;
    238 	struct m_tag *mtag;
    239 	struct secpolicy *sp = NULL;
    240 	struct tdb_ident *tdbi;
    241 	int s;
    242 #endif
    243 	u_int16_t ip_len;
    244 
    245 	len = 0;
    246 	va_start(ap, m0);
    247 	opt = va_arg(ap, struct mbuf *);
    248 	ro = va_arg(ap, struct route *);
    249 	flags = va_arg(ap, int);
    250 	imo = va_arg(ap, struct ip_moptions *);
    251 	so = va_arg(ap, struct socket *);
    252 	if (flags & IP_RETURNMTU)
    253 		mtu_p = va_arg(ap, int *);
    254 	else
    255 		mtu_p = NULL;
    256 	va_end(ap);
    257 
    258 	MCLAIM(m, &ip_tx_mowner);
    259 #ifdef FAST_IPSEC
    260 	if (so != NULL && so->so_proto->pr_domain->dom_family == AF_INET)
    261 		inp = (struct inpcb *)so->so_pcb;
    262 	else
    263 		inp = NULL;
    264 #endif /* FAST_IPSEC */
    265 
    266 #ifdef	DIAGNOSTIC
    267 	if ((m->m_flags & M_PKTHDR) == 0)
    268 		panic("ip_output: no HDR");
    269 
    270 	if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) != 0) {
    271 		panic("ip_output: IPv6 checksum offload flags: %d",
    272 		    m->m_pkthdr.csum_flags);
    273 	}
    274 
    275 	if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) ==
    276 	    (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    277 		panic("ip_output: conflicting checksum offload flags: %d",
    278 		    m->m_pkthdr.csum_flags);
    279 	}
    280 #endif
    281 	if (opt) {
    282 		m = ip_insertoptions(m, opt, &len);
    283 		if (len >= sizeof(struct ip))
    284 			hlen = len;
    285 	}
    286 	ip = mtod(m, struct ip *);
    287 	/*
    288 	 * Fill in IP header.
    289 	 */
    290 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
    291 		ip->ip_v = IPVERSION;
    292 		ip->ip_off = htons(0);
    293 		if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
    294 			ip->ip_id = ip_newid();
    295 		} else {
    296 
    297 			/*
    298 			 * TSO capable interfaces (typically?) increment
    299 			 * ip_id for each segment.
    300 			 * "allocate" enough ids here to increase the chance
    301 			 * for them to be unique.
    302 			 *
    303 			 * note that the following calculation is not
    304 			 * needed to be precise.  wasting some ip_id is fine.
    305 			 */
    306 
    307 			unsigned int segsz = m->m_pkthdr.segsz;
    308 			unsigned int datasz = ntohs(ip->ip_len) - hlen;
    309 			unsigned int num = howmany(datasz, segsz);
    310 
    311 			ip->ip_id = ip_newid_range(num);
    312 		}
    313 		ip->ip_hl = hlen >> 2;
    314 		ipstat.ips_localout++;
    315 	} else {
    316 		hlen = ip->ip_hl << 2;
    317 	}
    318 	/*
    319 	 * Route packet.
    320 	 */
    321 	if (ro == 0) {
    322 		ro = &iproute;
    323 		bzero((caddr_t)ro, sizeof (*ro));
    324 	}
    325 	dst = satosin(&ro->ro_dst);
    326 	/*
    327 	 * If there is a cached route,
    328 	 * check that it is to the same destination
    329 	 * and is still up.  If not, free it and try again.
    330 	 * The address family should also be checked in case of sharing the
    331 	 * cache with IPv6.
    332 	 */
    333 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    334 	    dst->sin_family != AF_INET ||
    335 	    !in_hosteq(dst->sin_addr, ip->ip_dst))) {
    336 		RTFREE(ro->ro_rt);
    337 		ro->ro_rt = (struct rtentry *)0;
    338 	}
    339 	if (ro->ro_rt == 0) {
    340 		bzero(dst, sizeof(*dst));
    341 		dst->sin_family = AF_INET;
    342 		dst->sin_len = sizeof(*dst);
    343 		dst->sin_addr = ip->ip_dst;
    344 	}
    345 	/*
    346 	 * If routing to interface only,
    347 	 * short circuit routing lookup.
    348 	 */
    349 	if (flags & IP_ROUTETOIF) {
    350 		if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) {
    351 			ipstat.ips_noroute++;
    352 			error = ENETUNREACH;
    353 			goto bad;
    354 		}
    355 		ifp = ia->ia_ifp;
    356 		mtu = ifp->if_mtu;
    357 		ip->ip_ttl = 1;
    358 	} else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
    359 	    ip->ip_dst.s_addr == INADDR_BROADCAST) &&
    360 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
    361 		ifp = imo->imo_multicast_ifp;
    362 		mtu = ifp->if_mtu;
    363 		IFP_TO_IA(ifp, ia);
    364 	} else {
    365 		if (ro->ro_rt == 0)
    366 			rtalloc(ro);
    367 		if (ro->ro_rt == 0) {
    368 			ipstat.ips_noroute++;
    369 			error = EHOSTUNREACH;
    370 			goto bad;
    371 		}
    372 		ia = ifatoia(ro->ro_rt->rt_ifa);
    373 		ifp = ro->ro_rt->rt_ifp;
    374 		if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0)
    375 			mtu = ifp->if_mtu;
    376 		ro->ro_rt->rt_use++;
    377 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
    378 			dst = satosin(ro->ro_rt->rt_gateway);
    379 	}
    380 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
    381 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
    382 		struct in_multi *inm;
    383 
    384 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
    385 			M_BCAST : M_MCAST;
    386 		/*
    387 		 * IP destination address is multicast.  Make sure "dst"
    388 		 * still points to the address in "ro".  (It may have been
    389 		 * changed to point to a gateway address, above.)
    390 		 */
    391 		dst = satosin(&ro->ro_dst);
    392 		/*
    393 		 * See if the caller provided any multicast options
    394 		 */
    395 		if (imo != NULL)
    396 			ip->ip_ttl = imo->imo_multicast_ttl;
    397 		else
    398 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
    399 
    400 		/*
    401 		 * if we don't know the outgoing ifp yet, we can't generate
    402 		 * output
    403 		 */
    404 		if (!ifp) {
    405 			ipstat.ips_noroute++;
    406 			error = ENETUNREACH;
    407 			goto bad;
    408 		}
    409 
    410 		/*
    411 		 * If the packet is multicast or broadcast, confirm that
    412 		 * the outgoing interface can transmit it.
    413 		 */
    414 		if (((m->m_flags & M_MCAST) &&
    415 		     (ifp->if_flags & IFF_MULTICAST) == 0) ||
    416 		    ((m->m_flags & M_BCAST) &&
    417 		     (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0))  {
    418 			ipstat.ips_noroute++;
    419 			error = ENETUNREACH;
    420 			goto bad;
    421 		}
    422 		/*
    423 		 * If source address not specified yet, use an address
    424 		 * of outgoing interface.
    425 		 */
    426 		if (in_nullhost(ip->ip_src)) {
    427 			struct in_ifaddr *xia;
    428 
    429 			IFP_TO_IA(ifp, xia);
    430 			if (!xia) {
    431 				error = EADDRNOTAVAIL;
    432 				goto bad;
    433 			}
    434 			ip->ip_src = xia->ia_addr.sin_addr;
    435 		}
    436 
    437 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
    438 		if (inm != NULL &&
    439 		   (imo == NULL || imo->imo_multicast_loop)) {
    440 			/*
    441 			 * If we belong to the destination multicast group
    442 			 * on the outgoing interface, and the caller did not
    443 			 * forbid loopback, loop back a copy.
    444 			 */
    445 			ip_mloopback(ifp, m, dst);
    446 		}
    447 #ifdef MROUTING
    448 		else {
    449 			/*
    450 			 * If we are acting as a multicast router, perform
    451 			 * multicast forwarding as if the packet had just
    452 			 * arrived on the interface to which we are about
    453 			 * to send.  The multicast forwarding function
    454 			 * recursively calls this function, using the
    455 			 * IP_FORWARDING flag to prevent infinite recursion.
    456 			 *
    457 			 * Multicasts that are looped back by ip_mloopback(),
    458 			 * above, will be forwarded by the ip_input() routine,
    459 			 * if necessary.
    460 			 */
    461 			extern struct socket *ip_mrouter;
    462 
    463 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
    464 				if (ip_mforward(m, ifp) != 0) {
    465 					m_freem(m);
    466 					goto done;
    467 				}
    468 			}
    469 		}
    470 #endif
    471 		/*
    472 		 * Multicasts with a time-to-live of zero may be looped-
    473 		 * back, above, but must not be transmitted on a network.
    474 		 * Also, multicasts addressed to the loopback interface
    475 		 * are not sent -- the above call to ip_mloopback() will
    476 		 * loop back a copy if this host actually belongs to the
    477 		 * destination group on the loopback interface.
    478 		 */
    479 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
    480 			m_freem(m);
    481 			goto done;
    482 		}
    483 
    484 		goto sendit;
    485 	}
    486 	/*
    487 	 * If source address not specified yet, use address
    488 	 * of outgoing interface.
    489 	 */
    490 	if (in_nullhost(ip->ip_src))
    491 		ip->ip_src = ia->ia_addr.sin_addr;
    492 
    493 	/*
    494 	 * packets with Class-D address as source are not valid per
    495 	 * RFC 1112
    496 	 */
    497 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
    498 		ipstat.ips_odropped++;
    499 		error = EADDRNOTAVAIL;
    500 		goto bad;
    501 	}
    502 
    503 	/*
    504 	 * Look for broadcast address and
    505 	 * and verify user is allowed to send
    506 	 * such a packet.
    507 	 */
    508 	if (in_broadcast(dst->sin_addr, ifp)) {
    509 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    510 			error = EADDRNOTAVAIL;
    511 			goto bad;
    512 		}
    513 		if ((flags & IP_ALLOWBROADCAST) == 0) {
    514 			error = EACCES;
    515 			goto bad;
    516 		}
    517 		/* don't allow broadcast messages to be fragmented */
    518 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
    519 			error = EMSGSIZE;
    520 			goto bad;
    521 		}
    522 		m->m_flags |= M_BCAST;
    523 	} else
    524 		m->m_flags &= ~M_BCAST;
    525 
    526 sendit:
    527 	/*
    528 	 * If we're doing Path MTU Discovery, we need to set DF unless
    529 	 * the route's MTU is locked.
    530 	 */
    531 	if ((flags & IP_MTUDISC) != 0 && ro->ro_rt != NULL &&
    532 	    (ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
    533 		ip->ip_off |= htons(IP_DF);
    534 
    535 	/* Remember the current ip_len */
    536 	ip_len = ntohs(ip->ip_len);
    537 
    538 #ifdef IPSEC
    539 	/* get SP for this packet */
    540 	if (so == NULL)
    541 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
    542 		    flags, &error);
    543 	else {
    544 		if (IPSEC_PCB_SKIP_IPSEC(sotoinpcb_hdr(so)->inph_sp,
    545 					 IPSEC_DIR_OUTBOUND))
    546 			goto skip_ipsec;
    547 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
    548 	}
    549 
    550 	if (sp == NULL) {
    551 		ipsecstat.out_inval++;
    552 		goto bad;
    553 	}
    554 
    555 	error = 0;
    556 
    557 	/* check policy */
    558 	switch (sp->policy) {
    559 	case IPSEC_POLICY_DISCARD:
    560 		/*
    561 		 * This packet is just discarded.
    562 		 */
    563 		ipsecstat.out_polvio++;
    564 		goto bad;
    565 
    566 	case IPSEC_POLICY_BYPASS:
    567 	case IPSEC_POLICY_NONE:
    568 		/* no need to do IPsec. */
    569 		goto skip_ipsec;
    570 
    571 	case IPSEC_POLICY_IPSEC:
    572 		if (sp->req == NULL) {
    573 			/* XXX should be panic ? */
    574 			printf("ip_output: No IPsec request specified.\n");
    575 			error = EINVAL;
    576 			goto bad;
    577 		}
    578 		break;
    579 
    580 	case IPSEC_POLICY_ENTRUST:
    581 	default:
    582 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
    583 	}
    584 
    585 #ifdef IPSEC_NAT_T
    586 	/*
    587 	 * NAT-T ESP fragmentation: don't do IPSec processing now,
    588 	 * we'll do it on each fragmented packet.
    589 	 */
    590 	if (sp->req->sav &&
    591 	    ((sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP) ||
    592 	     (sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP_NON_IKE))) {
    593 		if (ntohs(ip->ip_len) > sp->req->sav->esp_frag) {
    594 			natt_frag = 1;
    595 			mtu = sp->req->sav->esp_frag;
    596 			goto skip_ipsec;
    597 		}
    598 	}
    599 #endif /* IPSEC_NAT_T */
    600 
    601 	/*
    602 	 * ipsec4_output() expects ip_len and ip_off in network
    603 	 * order.  They have been set to network order above.
    604 	 */
    605 
    606     {
    607 	struct ipsec_output_state state;
    608 	bzero(&state, sizeof(state));
    609 	state.m = m;
    610 	if (flags & IP_ROUTETOIF) {
    611 		state.ro = &iproute;
    612 		bzero(&iproute, sizeof(iproute));
    613 	} else
    614 		state.ro = ro;
    615 	state.dst = (struct sockaddr *)dst;
    616 
    617 	/*
    618 	 * We can't defer the checksum of payload data if
    619 	 * we're about to encrypt/authenticate it.
    620 	 *
    621 	 * XXX When we support crypto offloading functions of
    622 	 * XXX network interfaces, we need to reconsider this,
    623 	 * XXX since it's likely that they'll support checksumming,
    624 	 * XXX as well.
    625 	 */
    626 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    627 		in_delayed_cksum(m);
    628 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    629 	}
    630 
    631 	error = ipsec4_output(&state, sp, flags);
    632 
    633 	m = state.m;
    634 	if (flags & IP_ROUTETOIF) {
    635 		/*
    636 		 * if we have tunnel mode SA, we may need to ignore
    637 		 * IP_ROUTETOIF.
    638 		 */
    639 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
    640 			flags &= ~IP_ROUTETOIF;
    641 			ro = state.ro;
    642 		}
    643 	} else
    644 		ro = state.ro;
    645 	dst = (struct sockaddr_in *)state.dst;
    646 	if (error) {
    647 		/* mbuf is already reclaimed in ipsec4_output. */
    648 		m0 = NULL;
    649 		switch (error) {
    650 		case EHOSTUNREACH:
    651 		case ENETUNREACH:
    652 		case EMSGSIZE:
    653 		case ENOBUFS:
    654 		case ENOMEM:
    655 			break;
    656 		default:
    657 			printf("ip4_output (ipsec): error code %d\n", error);
    658 			/*fall through*/
    659 		case ENOENT:
    660 			/* don't show these error codes to the user */
    661 			error = 0;
    662 			break;
    663 		}
    664 		goto bad;
    665 	}
    666 
    667 	/* be sure to update variables that are affected by ipsec4_output() */
    668 	ip = mtod(m, struct ip *);
    669 	hlen = ip->ip_hl << 2;
    670 	ip_len = ntohs(ip->ip_len);
    671 
    672 	if (ro->ro_rt == NULL) {
    673 		if ((flags & IP_ROUTETOIF) == 0) {
    674 			printf("ip_output: "
    675 				"can't update route after IPsec processing\n");
    676 			error = EHOSTUNREACH;	/*XXX*/
    677 			goto bad;
    678 		}
    679 	} else {
    680 		/* nobody uses ia beyond here */
    681 		if (state.encap) {
    682 			ifp = ro->ro_rt->rt_ifp;
    683 			if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0)
    684 				mtu = ifp->if_mtu;
    685 		}
    686 	}
    687     }
    688 skip_ipsec:
    689 #endif /*IPSEC*/
    690 #ifdef FAST_IPSEC
    691 	/*
    692 	 * Check the security policy (SP) for the packet and, if
    693 	 * required, do IPsec-related processing.  There are two
    694 	 * cases here; the first time a packet is sent through
    695 	 * it will be untagged and handled by ipsec4_checkpolicy.
    696 	 * If the packet is resubmitted to ip_output (e.g. after
    697 	 * AH, ESP, etc. processing), there will be a tag to bypass
    698 	 * the lookup and related policy checking.
    699 	 */
    700 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
    701 	s = splsoftnet();
    702 	if (mtag != NULL) {
    703 		tdbi = (struct tdb_ident *)(mtag + 1);
    704 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
    705 		if (sp == NULL)
    706 			error = -EINVAL;	/* force silent drop */
    707 		m_tag_delete(m, mtag);
    708 	} else {
    709 		if (inp != NULL &&
    710 		    IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
    711 			goto spd_done;
    712 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
    713 					&error, inp);
    714 	}
    715 	/*
    716 	 * There are four return cases:
    717 	 *    sp != NULL	 	    apply IPsec policy
    718 	 *    sp == NULL, error == 0	    no IPsec handling needed
    719 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
    720 	 *    sp == NULL, error != 0	    discard packet, report error
    721 	 */
    722 	if (sp != NULL) {
    723 #ifdef IPSEC_NAT_T
    724 		/*
    725 		 * NAT-T ESP fragmentation: don't do IPSec processing now,
    726 		 * we'll do it on each fragmented packet.
    727 		 */
    728 		if (sp->req->sav &&
    729 		    ((sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP) ||
    730 		     (sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP_NON_IKE))) {
    731 			if (ntohs(ip->ip_len) > sp->req->sav->esp_frag) {
    732 				natt_frag = 1;
    733 				mtu = sp->req->sav->esp_frag;
    734 				goto spd_done;
    735 			}
    736 		}
    737 #endif /* IPSEC_NAT_T */
    738 		/* Loop detection, check if ipsec processing already done */
    739 		IPSEC_ASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
    740 		for (mtag = m_tag_first(m); mtag != NULL;
    741 		     mtag = m_tag_next(m, mtag)) {
    742 #ifdef MTAG_ABI_COMPAT
    743 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
    744 				continue;
    745 #endif
    746 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
    747 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
    748 				continue;
    749 			/*
    750 			 * Check if policy has an SA associated with it.
    751 			 * This can happen when an SP has yet to acquire
    752 			 * an SA; e.g. on first reference.  If it occurs,
    753 			 * then we let ipsec4_process_packet do its thing.
    754 			 */
    755 			if (sp->req->sav == NULL)
    756 				break;
    757 			tdbi = (struct tdb_ident *)(mtag + 1);
    758 			if (tdbi->spi == sp->req->sav->spi &&
    759 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
    760 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
    761 				 sizeof (union sockaddr_union)) == 0) {
    762 				/*
    763 				 * No IPsec processing is needed, free
    764 				 * reference to SP.
    765 				 *
    766 				 * NB: null pointer to avoid free at
    767 				 *     done: below.
    768 				 */
    769 				KEY_FREESP(&sp), sp = NULL;
    770 				splx(s);
    771 				goto spd_done;
    772 			}
    773 		}
    774 
    775 		/*
    776 		 * Do delayed checksums now because we send before
    777 		 * this is done in the normal processing path.
    778 		 */
    779 		if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    780 			in_delayed_cksum(m);
    781 			m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    782 		}
    783 
    784 #ifdef __FreeBSD__
    785 		ip->ip_len = htons(ip->ip_len);
    786 		ip->ip_off = htons(ip->ip_off);
    787 #endif
    788 
    789 		/* NB: callee frees mbuf */
    790 		error = ipsec4_process_packet(m, sp->req, flags, 0);
    791 		/*
    792 		 * Preserve KAME behaviour: ENOENT can be returned
    793 		 * when an SA acquire is in progress.  Don't propagate
    794 		 * this to user-level; it confuses applications.
    795 		 *
    796 		 * XXX this will go away when the SADB is redone.
    797 		 */
    798 		if (error == ENOENT)
    799 			error = 0;
    800 		splx(s);
    801 		goto done;
    802 	} else {
    803 		splx(s);
    804 
    805 		if (error != 0) {
    806 			/*
    807 			 * Hack: -EINVAL is used to signal that a packet
    808 			 * should be silently discarded.  This is typically
    809 			 * because we asked key management for an SA and
    810 			 * it was delayed (e.g. kicked up to IKE).
    811 			 */
    812 			if (error == -EINVAL)
    813 				error = 0;
    814 			goto bad;
    815 		} else {
    816 			/* No IPsec processing for this packet. */
    817 		}
    818 #ifdef notyet
    819 		/*
    820 		 * If deferred crypto processing is needed, check that
    821 		 * the interface supports it.
    822 		 */
    823 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
    824 		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
    825 			/* notify IPsec to do its own crypto */
    826 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
    827 			error = EHOSTUNREACH;
    828 			goto bad;
    829 		}
    830 #endif
    831 	}
    832 spd_done:
    833 #endif /* FAST_IPSEC */
    834 
    835 #ifdef PFIL_HOOKS
    836 	/*
    837 	 * Run through list of hooks for output packets.
    838 	 */
    839 	if ((error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
    840 		goto done;
    841 	if (m == NULL)
    842 		goto done;
    843 
    844 	ip = mtod(m, struct ip *);
    845 	hlen = ip->ip_hl << 2;
    846 #endif /* PFIL_HOOKS */
    847 
    848 	m->m_pkthdr.csum_data |= hlen << 16;
    849 
    850 #if IFA_STATS
    851 	/*
    852 	 * search for the source address structure to
    853 	 * maintain output statistics.
    854 	 */
    855 	INADDR_TO_IA(ip->ip_src, ia);
    856 #endif
    857 
    858 	/* Maybe skip checksums on loopback interfaces. */
    859 	if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) {
    860 		m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
    861 	}
    862 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
    863 	/*
    864 	 * If small enough for mtu of path, or if using TCP segmentation
    865 	 * offload, can just send directly.
    866 	 */
    867 	if (ip_len <= mtu ||
    868 	    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0) {
    869 #if IFA_STATS
    870 		if (ia)
    871 			ia->ia_ifa.ifa_data.ifad_outbytes += ip_len;
    872 #endif
    873 		/*
    874 		 * Always initialize the sum to 0!  Some HW assisted
    875 		 * checksumming requires this.
    876 		 */
    877 		ip->ip_sum = 0;
    878 
    879 		if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
    880 			/*
    881 			 * Perform any checksums that the hardware can't do
    882 			 * for us.
    883 			 *
    884 			 * XXX Does any hardware require the {th,uh}_sum
    885 			 * XXX fields to be 0?
    886 			 */
    887 			if (sw_csum & M_CSUM_IPv4) {
    888 				KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
    889 				ip->ip_sum = in_cksum(m, hlen);
    890 				m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
    891 			}
    892 			if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    893 				if (IN_NEED_CHECKSUM(ifp,
    894 				    sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
    895 					in_delayed_cksum(m);
    896 				}
    897 				m->m_pkthdr.csum_flags &=
    898 				    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    899 			}
    900 		}
    901 
    902 #ifdef IPSEC
    903 		/* clean ipsec history once it goes out of the node */
    904 		ipsec_delaux(m);
    905 #endif
    906 
    907 		if (__predict_true(
    908 		    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0 ||
    909 		    (ifp->if_capenable & IFCAP_TSOv4) != 0)) {
    910 			error =
    911 			    (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
    912 		} else {
    913 			error =
    914 			    ip_tso_output(ifp, m, sintosa(dst), ro->ro_rt);
    915 		}
    916 		goto done;
    917 	}
    918 
    919 	/*
    920 	 * We can't use HW checksumming if we're about to
    921 	 * to fragment the packet.
    922 	 *
    923 	 * XXX Some hardware can do this.
    924 	 */
    925 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    926 		if (IN_NEED_CHECKSUM(ifp,
    927 		    m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
    928 			in_delayed_cksum(m);
    929 		}
    930 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    931 	}
    932 
    933 	/*
    934 	 * Too large for interface; fragment if possible.
    935 	 * Must be able to put at least 8 bytes per fragment.
    936 	 */
    937 	if (ntohs(ip->ip_off) & IP_DF) {
    938 		if (flags & IP_RETURNMTU)
    939 			*mtu_p = mtu;
    940 		error = EMSGSIZE;
    941 		ipstat.ips_cantfrag++;
    942 		goto bad;
    943 	}
    944 
    945 	error = ip_fragment(m, ifp, mtu);
    946 	if (error) {
    947 		m = NULL;
    948 		goto bad;
    949 	}
    950 
    951 	for (; m; m = m0) {
    952 		m0 = m->m_nextpkt;
    953 		m->m_nextpkt = 0;
    954 		if (error == 0) {
    955 #if IFA_STATS
    956 			if (ia)
    957 				ia->ia_ifa.ifa_data.ifad_outbytes +=
    958 				    ntohs(ip->ip_len);
    959 #endif
    960 #ifdef IPSEC
    961 			/* clean ipsec history once it goes out of the node */
    962 			ipsec_delaux(m);
    963 #endif /* IPSEC */
    964 
    965 #ifdef IPSEC_NAT_T
    966 			/*
    967 			 * If we get there, the packet has not been handeld by
    968 			 * IPSec whereas it should have. Now that it has been
    969 			 * fragmented, re-inject it in ip_output so that IPsec
    970 			 * processing can occur.
    971 			 */
    972 			if (natt_frag) {
    973 				error = ip_output(m, opt,
    974 				    ro, flags, imo, so, mtu_p);
    975 			} else
    976 #endif /* IPSEC_NAT_T */
    977 			{
    978 				KASSERT((m->m_pkthdr.csum_flags &
    979 				    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
    980 				error = (*ifp->if_output)(ifp, m, sintosa(dst),
    981 				    ro->ro_rt);
    982 			}
    983 		} else
    984 			m_freem(m);
    985 	}
    986 
    987 	if (error == 0)
    988 		ipstat.ips_fragmented++;
    989 done:
    990 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
    991 		RTFREE(ro->ro_rt);
    992 		ro->ro_rt = 0;
    993 	}
    994 
    995 #ifdef IPSEC
    996 	if (sp != NULL) {
    997 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
    998 			printf("DP ip_output call free SP:%p\n", sp));
    999 		key_freesp(sp);
   1000 	}
   1001 #endif /* IPSEC */
   1002 #ifdef FAST_IPSEC
   1003 	if (sp != NULL)
   1004 		KEY_FREESP(&sp);
   1005 #endif /* FAST_IPSEC */
   1006 
   1007 	return (error);
   1008 bad:
   1009 	m_freem(m);
   1010 	goto done;
   1011 }
   1012 
   1013 int
   1014 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
   1015 {
   1016 	struct ip *ip, *mhip;
   1017 	struct mbuf *m0;
   1018 	int len, hlen, off;
   1019 	int mhlen, firstlen;
   1020 	struct mbuf **mnext;
   1021 	int sw_csum = m->m_pkthdr.csum_flags;
   1022 	int fragments = 0;
   1023 	int s;
   1024 	int error = 0;
   1025 
   1026 	ip = mtod(m, struct ip *);
   1027 	hlen = ip->ip_hl << 2;
   1028 	if (ifp != NULL)
   1029 		sw_csum &= ~ifp->if_csum_flags_tx;
   1030 
   1031 	len = (mtu - hlen) &~ 7;
   1032 	if (len < 8) {
   1033 		m_freem(m);
   1034 		return (EMSGSIZE);
   1035 	}
   1036 
   1037 	firstlen = len;
   1038 	mnext = &m->m_nextpkt;
   1039 
   1040 	/*
   1041 	 * Loop through length of segment after first fragment,
   1042 	 * make new header and copy data of each part and link onto chain.
   1043 	 */
   1044 	m0 = m;
   1045 	mhlen = sizeof (struct ip);
   1046 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
   1047 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
   1048 		if (m == 0) {
   1049 			error = ENOBUFS;
   1050 			ipstat.ips_odropped++;
   1051 			goto sendorfree;
   1052 		}
   1053 		MCLAIM(m, m0->m_owner);
   1054 		*mnext = m;
   1055 		mnext = &m->m_nextpkt;
   1056 		m->m_data += max_linkhdr;
   1057 		mhip = mtod(m, struct ip *);
   1058 		*mhip = *ip;
   1059 		/* we must inherit MCAST and BCAST flags */
   1060 		m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
   1061 		if (hlen > sizeof (struct ip)) {
   1062 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
   1063 			mhip->ip_hl = mhlen >> 2;
   1064 		}
   1065 		m->m_len = mhlen;
   1066 		mhip->ip_off = ((off - hlen) >> 3) +
   1067 		    (ntohs(ip->ip_off) & ~IP_MF);
   1068 		if (ip->ip_off & htons(IP_MF))
   1069 			mhip->ip_off |= IP_MF;
   1070 		if (off + len >= ntohs(ip->ip_len))
   1071 			len = ntohs(ip->ip_len) - off;
   1072 		else
   1073 			mhip->ip_off |= IP_MF;
   1074 		HTONS(mhip->ip_off);
   1075 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
   1076 		m->m_next = m_copy(m0, off, len);
   1077 		if (m->m_next == 0) {
   1078 			error = ENOBUFS;	/* ??? */
   1079 			ipstat.ips_odropped++;
   1080 			goto sendorfree;
   1081 		}
   1082 		m->m_pkthdr.len = mhlen + len;
   1083 		m->m_pkthdr.rcvif = (struct ifnet *)0;
   1084 		mhip->ip_sum = 0;
   1085 		if (sw_csum & M_CSUM_IPv4) {
   1086 			mhip->ip_sum = in_cksum(m, mhlen);
   1087 			KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
   1088 		} else {
   1089 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   1090 			m->m_pkthdr.csum_data |= mhlen << 16;
   1091 		}
   1092 		ipstat.ips_ofragments++;
   1093 		fragments++;
   1094 	}
   1095 	/*
   1096 	 * Update first fragment by trimming what's been copied out
   1097 	 * and updating header, then send each fragment (in order).
   1098 	 */
   1099 	m = m0;
   1100 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
   1101 	m->m_pkthdr.len = hlen + firstlen;
   1102 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
   1103 	ip->ip_off |= htons(IP_MF);
   1104 	ip->ip_sum = 0;
   1105 	if (sw_csum & M_CSUM_IPv4) {
   1106 		ip->ip_sum = in_cksum(m, hlen);
   1107 		m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
   1108 	} else {
   1109 		KASSERT(m->m_pkthdr.csum_flags & M_CSUM_IPv4);
   1110 		KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
   1111 			sizeof(struct ip));
   1112 	}
   1113 sendorfree:
   1114 	/*
   1115 	 * If there is no room for all the fragments, don't queue
   1116 	 * any of them.
   1117 	 */
   1118 	if (ifp != NULL) {
   1119 		s = splnet();
   1120 		if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
   1121 		    error == 0) {
   1122 			error = ENOBUFS;
   1123 			ipstat.ips_odropped++;
   1124 			IFQ_INC_DROPS(&ifp->if_snd);
   1125 		}
   1126 		splx(s);
   1127 	}
   1128 	if (error) {
   1129 		for (m = m0; m; m = m0) {
   1130 			m0 = m->m_nextpkt;
   1131 			m->m_nextpkt = NULL;
   1132 			m_freem(m);
   1133 		}
   1134 	}
   1135 	return (error);
   1136 }
   1137 
   1138 /*
   1139  * Process a delayed payload checksum calculation.
   1140  */
   1141 void
   1142 in_delayed_cksum(struct mbuf *m)
   1143 {
   1144 	struct ip *ip;
   1145 	u_int16_t csum, offset;
   1146 
   1147 	ip = mtod(m, struct ip *);
   1148 	offset = ip->ip_hl << 2;
   1149 	csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
   1150 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
   1151 		csum = 0xffff;
   1152 
   1153 	offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data);
   1154 
   1155 	if ((offset + sizeof(u_int16_t)) > m->m_len) {
   1156 		/* This happen when ip options were inserted
   1157 		printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
   1158 		    m->m_len, offset, ip->ip_p);
   1159 		 */
   1160 		m_copyback(m, offset, sizeof(csum), (caddr_t) &csum);
   1161 	} else
   1162 		*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
   1163 }
   1164 
   1165 /*
   1166  * Determine the maximum length of the options to be inserted;
   1167  * we would far rather allocate too much space rather than too little.
   1168  */
   1169 
   1170 u_int
   1171 ip_optlen(struct inpcb *inp)
   1172 {
   1173 	struct mbuf *m = inp->inp_options;
   1174 
   1175 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
   1176 		return (m->m_len - offsetof(struct ipoption, ipopt_dst));
   1177 	else
   1178 		return 0;
   1179 }
   1180 
   1181 
   1182 /*
   1183  * Insert IP options into preformed packet.
   1184  * Adjust IP destination as required for IP source routing,
   1185  * as indicated by a non-zero in_addr at the start of the options.
   1186  */
   1187 static struct mbuf *
   1188 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
   1189 {
   1190 	struct ipoption *p = mtod(opt, struct ipoption *);
   1191 	struct mbuf *n;
   1192 	struct ip *ip = mtod(m, struct ip *);
   1193 	unsigned optlen;
   1194 
   1195 	optlen = opt->m_len - sizeof(p->ipopt_dst);
   1196 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
   1197 		return (m);		/* XXX should fail */
   1198 	if (!in_nullhost(p->ipopt_dst))
   1199 		ip->ip_dst = p->ipopt_dst;
   1200 	if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
   1201 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
   1202 		if (n == 0)
   1203 			return (m);
   1204 		MCLAIM(n, m->m_owner);
   1205 		M_MOVE_PKTHDR(n, m);
   1206 		m->m_len -= sizeof(struct ip);
   1207 		m->m_data += sizeof(struct ip);
   1208 		n->m_next = m;
   1209 		m = n;
   1210 		m->m_len = optlen + sizeof(struct ip);
   1211 		m->m_data += max_linkhdr;
   1212 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
   1213 	} else {
   1214 		m->m_data -= optlen;
   1215 		m->m_len += optlen;
   1216 		memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
   1217 	}
   1218 	m->m_pkthdr.len += optlen;
   1219 	ip = mtod(m, struct ip *);
   1220 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
   1221 	*phlen = sizeof(struct ip) + optlen;
   1222 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
   1223 	return (m);
   1224 }
   1225 
   1226 /*
   1227  * Copy options from ip to jp,
   1228  * omitting those not copied during fragmentation.
   1229  */
   1230 int
   1231 ip_optcopy(struct ip *ip, struct ip *jp)
   1232 {
   1233 	u_char *cp, *dp;
   1234 	int opt, optlen, cnt;
   1235 
   1236 	cp = (u_char *)(ip + 1);
   1237 	dp = (u_char *)(jp + 1);
   1238 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
   1239 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   1240 		opt = cp[0];
   1241 		if (opt == IPOPT_EOL)
   1242 			break;
   1243 		if (opt == IPOPT_NOP) {
   1244 			/* Preserve for IP mcast tunnel's LSRR alignment. */
   1245 			*dp++ = IPOPT_NOP;
   1246 			optlen = 1;
   1247 			continue;
   1248 		}
   1249 #ifdef DIAGNOSTIC
   1250 		if (cnt < IPOPT_OLEN + sizeof(*cp))
   1251 			panic("malformed IPv4 option passed to ip_optcopy");
   1252 #endif
   1253 		optlen = cp[IPOPT_OLEN];
   1254 #ifdef DIAGNOSTIC
   1255 		if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
   1256 			panic("malformed IPv4 option passed to ip_optcopy");
   1257 #endif
   1258 		/* bogus lengths should have been caught by ip_dooptions */
   1259 		if (optlen > cnt)
   1260 			optlen = cnt;
   1261 		if (IPOPT_COPIED(opt)) {
   1262 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
   1263 			dp += optlen;
   1264 		}
   1265 	}
   1266 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
   1267 		*dp++ = IPOPT_EOL;
   1268 	return (optlen);
   1269 }
   1270 
   1271 /*
   1272  * IP socket option processing.
   1273  */
   1274 int
   1275 ip_ctloutput(int op, struct socket *so, int level, int optname,
   1276     struct mbuf **mp)
   1277 {
   1278 	struct inpcb *inp = sotoinpcb(so);
   1279 	struct mbuf *m = *mp;
   1280 	int optval = 0;
   1281 	int error = 0;
   1282 #if defined(IPSEC) || defined(FAST_IPSEC)
   1283 	struct lwp *l = curlwp;	/*XXX*/
   1284 #endif
   1285 
   1286 	if (level != IPPROTO_IP) {
   1287 		error = EINVAL;
   1288 		if (op == PRCO_SETOPT && *mp)
   1289 			(void) m_free(*mp);
   1290 	} else switch (op) {
   1291 
   1292 	case PRCO_SETOPT:
   1293 		switch (optname) {
   1294 		case IP_OPTIONS:
   1295 #ifdef notyet
   1296 		case IP_RETOPTS:
   1297 			return (ip_pcbopts(optname, &inp->inp_options, m));
   1298 #else
   1299 			return (ip_pcbopts(&inp->inp_options, m));
   1300 #endif
   1301 
   1302 		case IP_TOS:
   1303 		case IP_TTL:
   1304 		case IP_RECVOPTS:
   1305 		case IP_RECVRETOPTS:
   1306 		case IP_RECVDSTADDR:
   1307 		case IP_RECVIF:
   1308 			if (m == NULL || m->m_len != sizeof(int))
   1309 				error = EINVAL;
   1310 			else {
   1311 				optval = *mtod(m, int *);
   1312 				switch (optname) {
   1313 
   1314 				case IP_TOS:
   1315 					inp->inp_ip.ip_tos = optval;
   1316 					break;
   1317 
   1318 				case IP_TTL:
   1319 					inp->inp_ip.ip_ttl = optval;
   1320 					break;
   1321 #define	OPTSET(bit) \
   1322 	if (optval) \
   1323 		inp->inp_flags |= bit; \
   1324 	else \
   1325 		inp->inp_flags &= ~bit;
   1326 
   1327 				case IP_RECVOPTS:
   1328 					OPTSET(INP_RECVOPTS);
   1329 					break;
   1330 
   1331 				case IP_RECVRETOPTS:
   1332 					OPTSET(INP_RECVRETOPTS);
   1333 					break;
   1334 
   1335 				case IP_RECVDSTADDR:
   1336 					OPTSET(INP_RECVDSTADDR);
   1337 					break;
   1338 
   1339 				case IP_RECVIF:
   1340 					OPTSET(INP_RECVIF);
   1341 					break;
   1342 				}
   1343 			}
   1344 			break;
   1345 #undef OPTSET
   1346 
   1347 		case IP_MULTICAST_IF:
   1348 		case IP_MULTICAST_TTL:
   1349 		case IP_MULTICAST_LOOP:
   1350 		case IP_ADD_MEMBERSHIP:
   1351 		case IP_DROP_MEMBERSHIP:
   1352 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
   1353 			break;
   1354 
   1355 		case IP_PORTRANGE:
   1356 			if (m == 0 || m->m_len != sizeof(int))
   1357 				error = EINVAL;
   1358 			else {
   1359 				optval = *mtod(m, int *);
   1360 
   1361 				switch (optval) {
   1362 
   1363 				case IP_PORTRANGE_DEFAULT:
   1364 				case IP_PORTRANGE_HIGH:
   1365 					inp->inp_flags &= ~(INP_LOWPORT);
   1366 					break;
   1367 
   1368 				case IP_PORTRANGE_LOW:
   1369 					inp->inp_flags |= INP_LOWPORT;
   1370 					break;
   1371 
   1372 				default:
   1373 					error = EINVAL;
   1374 					break;
   1375 				}
   1376 			}
   1377 			break;
   1378 
   1379 #if defined(IPSEC) || defined(FAST_IPSEC)
   1380 		case IP_IPSEC_POLICY:
   1381 		{
   1382 			caddr_t req = NULL;
   1383 			size_t len = 0;
   1384 			int priv = 0;
   1385 
   1386 #ifdef __NetBSD__
   1387 			if (l == 0 || kauth_authorize_generic(l->l_cred,
   1388 			    KAUTH_GENERIC_ISSUSER, &l->l_acflag))
   1389 				priv = 0;
   1390 			else
   1391 				priv = 1;
   1392 #else
   1393 			priv = (in6p->in6p_socket->so_state & SS_PRIV);
   1394 #endif
   1395 			if (m) {
   1396 				req = mtod(m, caddr_t);
   1397 				len = m->m_len;
   1398 			}
   1399 			error = ipsec4_set_policy(inp, optname, req, len, priv);
   1400 			break;
   1401 		    }
   1402 #endif /*IPSEC*/
   1403 
   1404 		default:
   1405 			error = ENOPROTOOPT;
   1406 			break;
   1407 		}
   1408 		if (m)
   1409 			(void)m_free(m);
   1410 		break;
   1411 
   1412 	case PRCO_GETOPT:
   1413 		switch (optname) {
   1414 		case IP_OPTIONS:
   1415 		case IP_RETOPTS:
   1416 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1417 			MCLAIM(m, so->so_mowner);
   1418 			if (inp->inp_options) {
   1419 				m->m_len = inp->inp_options->m_len;
   1420 				bcopy(mtod(inp->inp_options, caddr_t),
   1421 				    mtod(m, caddr_t), (unsigned)m->m_len);
   1422 			} else
   1423 				m->m_len = 0;
   1424 			break;
   1425 
   1426 		case IP_TOS:
   1427 		case IP_TTL:
   1428 		case IP_RECVOPTS:
   1429 		case IP_RECVRETOPTS:
   1430 		case IP_RECVDSTADDR:
   1431 		case IP_RECVIF:
   1432 		case IP_ERRORMTU:
   1433 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1434 			MCLAIM(m, so->so_mowner);
   1435 			m->m_len = sizeof(int);
   1436 			switch (optname) {
   1437 
   1438 			case IP_TOS:
   1439 				optval = inp->inp_ip.ip_tos;
   1440 				break;
   1441 
   1442 			case IP_TTL:
   1443 				optval = inp->inp_ip.ip_ttl;
   1444 				break;
   1445 
   1446 			case IP_ERRORMTU:
   1447 				optval = inp->inp_errormtu;
   1448 				break;
   1449 
   1450 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
   1451 
   1452 			case IP_RECVOPTS:
   1453 				optval = OPTBIT(INP_RECVOPTS);
   1454 				break;
   1455 
   1456 			case IP_RECVRETOPTS:
   1457 				optval = OPTBIT(INP_RECVRETOPTS);
   1458 				break;
   1459 
   1460 			case IP_RECVDSTADDR:
   1461 				optval = OPTBIT(INP_RECVDSTADDR);
   1462 				break;
   1463 
   1464 			case IP_RECVIF:
   1465 				optval = OPTBIT(INP_RECVIF);
   1466 				break;
   1467 			}
   1468 			*mtod(m, int *) = optval;
   1469 			break;
   1470 
   1471 #if 0	/* defined(IPSEC) || defined(FAST_IPSEC) */
   1472 		/* XXX: code broken */
   1473 		case IP_IPSEC_POLICY:
   1474 		{
   1475 			caddr_t req = NULL;
   1476 			size_t len = 0;
   1477 
   1478 			if (m) {
   1479 				req = mtod(m, caddr_t);
   1480 				len = m->m_len;
   1481 			}
   1482 			error = ipsec4_get_policy(inp, req, len, mp);
   1483 			break;
   1484 		}
   1485 #endif /*IPSEC*/
   1486 
   1487 		case IP_MULTICAST_IF:
   1488 		case IP_MULTICAST_TTL:
   1489 		case IP_MULTICAST_LOOP:
   1490 		case IP_ADD_MEMBERSHIP:
   1491 		case IP_DROP_MEMBERSHIP:
   1492 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
   1493 			if (*mp)
   1494 				MCLAIM(*mp, so->so_mowner);
   1495 			break;
   1496 
   1497 		case IP_PORTRANGE:
   1498 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1499 			MCLAIM(m, so->so_mowner);
   1500 			m->m_len = sizeof(int);
   1501 
   1502 			if (inp->inp_flags & INP_LOWPORT)
   1503 				optval = IP_PORTRANGE_LOW;
   1504 			else
   1505 				optval = IP_PORTRANGE_DEFAULT;
   1506 
   1507 			*mtod(m, int *) = optval;
   1508 			break;
   1509 
   1510 		default:
   1511 			error = ENOPROTOOPT;
   1512 			break;
   1513 		}
   1514 		break;
   1515 	}
   1516 	return (error);
   1517 }
   1518 
   1519 /*
   1520  * Set up IP options in pcb for insertion in output packets.
   1521  * Store in mbuf with pointer in pcbopt, adding pseudo-option
   1522  * with destination address if source routed.
   1523  */
   1524 int
   1525 #ifdef notyet
   1526 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m)
   1527 #else
   1528 ip_pcbopts(struct mbuf **pcbopt, struct mbuf *m)
   1529 #endif
   1530 {
   1531 	int cnt, optlen;
   1532 	u_char *cp;
   1533 	u_char opt;
   1534 
   1535 	/* turn off any old options */
   1536 	if (*pcbopt)
   1537 		(void)m_free(*pcbopt);
   1538 	*pcbopt = 0;
   1539 	if (m == (struct mbuf *)0 || m->m_len == 0) {
   1540 		/*
   1541 		 * Only turning off any previous options.
   1542 		 */
   1543 		if (m)
   1544 			(void)m_free(m);
   1545 		return (0);
   1546 	}
   1547 
   1548 #ifndef	__vax__
   1549 	if (m->m_len % sizeof(int32_t))
   1550 		goto bad;
   1551 #endif
   1552 	/*
   1553 	 * IP first-hop destination address will be stored before
   1554 	 * actual options; move other options back
   1555 	 * and clear it when none present.
   1556 	 */
   1557 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
   1558 		goto bad;
   1559 	cnt = m->m_len;
   1560 	m->m_len += sizeof(struct in_addr);
   1561 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
   1562 	memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
   1563 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
   1564 
   1565 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
   1566 		opt = cp[IPOPT_OPTVAL];
   1567 		if (opt == IPOPT_EOL)
   1568 			break;
   1569 		if (opt == IPOPT_NOP)
   1570 			optlen = 1;
   1571 		else {
   1572 			if (cnt < IPOPT_OLEN + sizeof(*cp))
   1573 				goto bad;
   1574 			optlen = cp[IPOPT_OLEN];
   1575 			if (optlen < IPOPT_OLEN  + sizeof(*cp) || optlen > cnt)
   1576 				goto bad;
   1577 		}
   1578 		switch (opt) {
   1579 
   1580 		default:
   1581 			break;
   1582 
   1583 		case IPOPT_LSRR:
   1584 		case IPOPT_SSRR:
   1585 			/*
   1586 			 * user process specifies route as:
   1587 			 *	->A->B->C->D
   1588 			 * D must be our final destination (but we can't
   1589 			 * check that since we may not have connected yet).
   1590 			 * A is first hop destination, which doesn't appear in
   1591 			 * actual IP option, but is stored before the options.
   1592 			 */
   1593 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
   1594 				goto bad;
   1595 			m->m_len -= sizeof(struct in_addr);
   1596 			cnt -= sizeof(struct in_addr);
   1597 			optlen -= sizeof(struct in_addr);
   1598 			cp[IPOPT_OLEN] = optlen;
   1599 			/*
   1600 			 * Move first hop before start of options.
   1601 			 */
   1602 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
   1603 			    sizeof(struct in_addr));
   1604 			/*
   1605 			 * Then copy rest of options back
   1606 			 * to close up the deleted entry.
   1607 			 */
   1608 			(void)memmove(&cp[IPOPT_OFFSET+1],
   1609 			    &cp[IPOPT_OFFSET+1] + sizeof(struct in_addr),
   1610 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
   1611 			break;
   1612 		}
   1613 	}
   1614 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
   1615 		goto bad;
   1616 	*pcbopt = m;
   1617 	return (0);
   1618 
   1619 bad:
   1620 	(void)m_free(m);
   1621 	return (EINVAL);
   1622 }
   1623 
   1624 /*
   1625  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
   1626  */
   1627 static struct ifnet *
   1628 ip_multicast_if(struct in_addr *a, int *ifindexp)
   1629 {
   1630 	int ifindex;
   1631 	struct ifnet *ifp = NULL;
   1632 	struct in_ifaddr *ia;
   1633 
   1634 	if (ifindexp)
   1635 		*ifindexp = 0;
   1636 	if (ntohl(a->s_addr) >> 24 == 0) {
   1637 		ifindex = ntohl(a->s_addr) & 0xffffff;
   1638 		if (ifindex < 0 || if_indexlim <= ifindex)
   1639 			return NULL;
   1640 		ifp = ifindex2ifnet[ifindex];
   1641 		if (!ifp)
   1642 			return NULL;
   1643 		if (ifindexp)
   1644 			*ifindexp = ifindex;
   1645 	} else {
   1646 		LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
   1647 			if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
   1648 			    (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
   1649 				ifp = ia->ia_ifp;
   1650 				break;
   1651 			}
   1652 		}
   1653 	}
   1654 	return ifp;
   1655 }
   1656 
   1657 static int
   1658 ip_getoptval(struct mbuf *m, u_int8_t *val, u_int maxval)
   1659 {
   1660 	u_int tval;
   1661 
   1662 	if (m == NULL)
   1663 		return EINVAL;
   1664 
   1665 	switch (m->m_len) {
   1666 	case sizeof(u_char):
   1667 		tval = *(mtod(m, u_char *));
   1668 		break;
   1669 	case sizeof(u_int):
   1670 		tval = *(mtod(m, u_int *));
   1671 		break;
   1672 	default:
   1673 		return EINVAL;
   1674 	}
   1675 
   1676 	if (tval > maxval)
   1677 		return EINVAL;
   1678 
   1679 	*val = tval;
   1680 	return 0;
   1681 }
   1682 
   1683 /*
   1684  * Set the IP multicast options in response to user setsockopt().
   1685  */
   1686 int
   1687 ip_setmoptions(int optname, struct ip_moptions **imop, struct mbuf *m)
   1688 {
   1689 	int error = 0;
   1690 	int i;
   1691 	struct in_addr addr;
   1692 	struct ip_mreq *mreq;
   1693 	struct ifnet *ifp;
   1694 	struct ip_moptions *imo = *imop;
   1695 	struct route ro;
   1696 	struct sockaddr_in *dst;
   1697 	int ifindex;
   1698 
   1699 	if (imo == NULL) {
   1700 		/*
   1701 		 * No multicast option buffer attached to the pcb;
   1702 		 * allocate one and initialize to default values.
   1703 		 */
   1704 		imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
   1705 		    M_WAITOK);
   1706 
   1707 		if (imo == NULL)
   1708 			return (ENOBUFS);
   1709 		*imop = imo;
   1710 		imo->imo_multicast_ifp = NULL;
   1711 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
   1712 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
   1713 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
   1714 		imo->imo_num_memberships = 0;
   1715 	}
   1716 
   1717 	switch (optname) {
   1718 
   1719 	case IP_MULTICAST_IF:
   1720 		/*
   1721 		 * Select the interface for outgoing multicast packets.
   1722 		 */
   1723 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
   1724 			error = EINVAL;
   1725 			break;
   1726 		}
   1727 		addr = *(mtod(m, struct in_addr *));
   1728 		/*
   1729 		 * INADDR_ANY is used to remove a previous selection.
   1730 		 * When no interface is selected, a default one is
   1731 		 * chosen every time a multicast packet is sent.
   1732 		 */
   1733 		if (in_nullhost(addr)) {
   1734 			imo->imo_multicast_ifp = NULL;
   1735 			break;
   1736 		}
   1737 		/*
   1738 		 * The selected interface is identified by its local
   1739 		 * IP address.  Find the interface and confirm that
   1740 		 * it supports multicasting.
   1741 		 */
   1742 		ifp = ip_multicast_if(&addr, &ifindex);
   1743 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1744 			error = EADDRNOTAVAIL;
   1745 			break;
   1746 		}
   1747 		imo->imo_multicast_ifp = ifp;
   1748 		if (ifindex)
   1749 			imo->imo_multicast_addr = addr;
   1750 		else
   1751 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
   1752 		break;
   1753 
   1754 	case IP_MULTICAST_TTL:
   1755 		/*
   1756 		 * Set the IP time-to-live for outgoing multicast packets.
   1757 		 */
   1758 		error = ip_getoptval(m, &imo->imo_multicast_ttl, MAXTTL);
   1759 		break;
   1760 
   1761 	case IP_MULTICAST_LOOP:
   1762 		/*
   1763 		 * Set the loopback flag for outgoing multicast packets.
   1764 		 * Must be zero or one.
   1765 		 */
   1766 		error = ip_getoptval(m, &imo->imo_multicast_loop, 1);
   1767 		break;
   1768 
   1769 	case IP_ADD_MEMBERSHIP:
   1770 		/*
   1771 		 * Add a multicast group membership.
   1772 		 * Group must be a valid IP multicast address.
   1773 		 */
   1774 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
   1775 			error = EINVAL;
   1776 			break;
   1777 		}
   1778 		mreq = mtod(m, struct ip_mreq *);
   1779 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
   1780 			error = EINVAL;
   1781 			break;
   1782 		}
   1783 		/*
   1784 		 * If no interface address was provided, use the interface of
   1785 		 * the route to the given multicast address.
   1786 		 */
   1787 		if (in_nullhost(mreq->imr_interface)) {
   1788 			bzero((caddr_t)&ro, sizeof(ro));
   1789 			ro.ro_rt = NULL;
   1790 			dst = satosin(&ro.ro_dst);
   1791 			dst->sin_len = sizeof(*dst);
   1792 			dst->sin_family = AF_INET;
   1793 			dst->sin_addr = mreq->imr_multiaddr;
   1794 			rtalloc(&ro);
   1795 			if (ro.ro_rt == NULL) {
   1796 				error = EADDRNOTAVAIL;
   1797 				break;
   1798 			}
   1799 			ifp = ro.ro_rt->rt_ifp;
   1800 			rtfree(ro.ro_rt);
   1801 		} else {
   1802 			ifp = ip_multicast_if(&mreq->imr_interface, NULL);
   1803 		}
   1804 		/*
   1805 		 * See if we found an interface, and confirm that it
   1806 		 * supports multicast.
   1807 		 */
   1808 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1809 			error = EADDRNOTAVAIL;
   1810 			break;
   1811 		}
   1812 		/*
   1813 		 * See if the membership already exists or if all the
   1814 		 * membership slots are full.
   1815 		 */
   1816 		for (i = 0; i < imo->imo_num_memberships; ++i) {
   1817 			if (imo->imo_membership[i]->inm_ifp == ifp &&
   1818 			    in_hosteq(imo->imo_membership[i]->inm_addr,
   1819 				      mreq->imr_multiaddr))
   1820 				break;
   1821 		}
   1822 		if (i < imo->imo_num_memberships) {
   1823 			error = EADDRINUSE;
   1824 			break;
   1825 		}
   1826 		if (i == IP_MAX_MEMBERSHIPS) {
   1827 			error = ETOOMANYREFS;
   1828 			break;
   1829 		}
   1830 		/*
   1831 		 * Everything looks good; add a new record to the multicast
   1832 		 * address list for the given interface.
   1833 		 */
   1834 		if ((imo->imo_membership[i] =
   1835 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
   1836 			error = ENOBUFS;
   1837 			break;
   1838 		}
   1839 		++imo->imo_num_memberships;
   1840 		break;
   1841 
   1842 	case IP_DROP_MEMBERSHIP:
   1843 		/*
   1844 		 * Drop a multicast group membership.
   1845 		 * Group must be a valid IP multicast address.
   1846 		 */
   1847 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
   1848 			error = EINVAL;
   1849 			break;
   1850 		}
   1851 		mreq = mtod(m, struct ip_mreq *);
   1852 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
   1853 			error = EINVAL;
   1854 			break;
   1855 		}
   1856 		/*
   1857 		 * If an interface address was specified, get a pointer
   1858 		 * to its ifnet structure.
   1859 		 */
   1860 		if (in_nullhost(mreq->imr_interface))
   1861 			ifp = NULL;
   1862 		else {
   1863 			ifp = ip_multicast_if(&mreq->imr_interface, NULL);
   1864 			if (ifp == NULL) {
   1865 				error = EADDRNOTAVAIL;
   1866 				break;
   1867 			}
   1868 		}
   1869 		/*
   1870 		 * Find the membership in the membership array.
   1871 		 */
   1872 		for (i = 0; i < imo->imo_num_memberships; ++i) {
   1873 			if ((ifp == NULL ||
   1874 			     imo->imo_membership[i]->inm_ifp == ifp) &&
   1875 			     in_hosteq(imo->imo_membership[i]->inm_addr,
   1876 				       mreq->imr_multiaddr))
   1877 				break;
   1878 		}
   1879 		if (i == imo->imo_num_memberships) {
   1880 			error = EADDRNOTAVAIL;
   1881 			break;
   1882 		}
   1883 		/*
   1884 		 * Give up the multicast address record to which the
   1885 		 * membership points.
   1886 		 */
   1887 		in_delmulti(imo->imo_membership[i]);
   1888 		/*
   1889 		 * Remove the gap in the membership array.
   1890 		 */
   1891 		for (++i; i < imo->imo_num_memberships; ++i)
   1892 			imo->imo_membership[i-1] = imo->imo_membership[i];
   1893 		--imo->imo_num_memberships;
   1894 		break;
   1895 
   1896 	default:
   1897 		error = EOPNOTSUPP;
   1898 		break;
   1899 	}
   1900 
   1901 	/*
   1902 	 * If all options have default values, no need to keep the mbuf.
   1903 	 */
   1904 	if (imo->imo_multicast_ifp == NULL &&
   1905 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
   1906 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
   1907 	    imo->imo_num_memberships == 0) {
   1908 		free(*imop, M_IPMOPTS);
   1909 		*imop = NULL;
   1910 	}
   1911 
   1912 	return (error);
   1913 }
   1914 
   1915 /*
   1916  * Return the IP multicast options in response to user getsockopt().
   1917  */
   1918 int
   1919 ip_getmoptions(int optname, struct ip_moptions *imo, struct mbuf **mp)
   1920 {
   1921 	u_char *ttl;
   1922 	u_char *loop;
   1923 	struct in_addr *addr;
   1924 	struct in_ifaddr *ia;
   1925 
   1926 	*mp = m_get(M_WAIT, MT_SOOPTS);
   1927 
   1928 	switch (optname) {
   1929 
   1930 	case IP_MULTICAST_IF:
   1931 		addr = mtod(*mp, struct in_addr *);
   1932 		(*mp)->m_len = sizeof(struct in_addr);
   1933 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
   1934 			*addr = zeroin_addr;
   1935 		else if (imo->imo_multicast_addr.s_addr) {
   1936 			/* return the value user has set */
   1937 			*addr = imo->imo_multicast_addr;
   1938 		} else {
   1939 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
   1940 			*addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
   1941 		}
   1942 		return (0);
   1943 
   1944 	case IP_MULTICAST_TTL:
   1945 		ttl = mtod(*mp, u_char *);
   1946 		(*mp)->m_len = 1;
   1947 		*ttl = imo ? imo->imo_multicast_ttl
   1948 			   : IP_DEFAULT_MULTICAST_TTL;
   1949 		return (0);
   1950 
   1951 	case IP_MULTICAST_LOOP:
   1952 		loop = mtod(*mp, u_char *);
   1953 		(*mp)->m_len = 1;
   1954 		*loop = imo ? imo->imo_multicast_loop
   1955 			    : IP_DEFAULT_MULTICAST_LOOP;
   1956 		return (0);
   1957 
   1958 	default:
   1959 		return (EOPNOTSUPP);
   1960 	}
   1961 }
   1962 
   1963 /*
   1964  * Discard the IP multicast options.
   1965  */
   1966 void
   1967 ip_freemoptions(struct ip_moptions *imo)
   1968 {
   1969 	int i;
   1970 
   1971 	if (imo != NULL) {
   1972 		for (i = 0; i < imo->imo_num_memberships; ++i)
   1973 			in_delmulti(imo->imo_membership[i]);
   1974 		free(imo, M_IPMOPTS);
   1975 	}
   1976 }
   1977 
   1978 /*
   1979  * Routine called from ip_output() to loop back a copy of an IP multicast
   1980  * packet to the input queue of a specified interface.  Note that this
   1981  * calls the output routine of the loopback "driver", but with an interface
   1982  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
   1983  */
   1984 static void
   1985 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst)
   1986 {
   1987 	struct ip *ip;
   1988 	struct mbuf *copym;
   1989 
   1990 	copym = m_copy(m, 0, M_COPYALL);
   1991 	if (copym != NULL
   1992 	 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
   1993 		copym = m_pullup(copym, sizeof(struct ip));
   1994 	if (copym != NULL) {
   1995 		/*
   1996 		 * We don't bother to fragment if the IP length is greater
   1997 		 * than the interface's MTU.  Can this possibly matter?
   1998 		 */
   1999 		ip = mtod(copym, struct ip *);
   2000 
   2001 		if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
   2002 			in_delayed_cksum(copym);
   2003 			copym->m_pkthdr.csum_flags &=
   2004 			    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
   2005 		}
   2006 
   2007 		ip->ip_sum = 0;
   2008 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
   2009 		(void) looutput(ifp, copym, sintosa(dst), NULL);
   2010 	}
   2011 }
   2012