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