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