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