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