Home | History | Annotate | Line # | Download | only in netinet
ip_output.c revision 1.54
      1 /*	$NetBSD: ip_output.c,v 1.54 1998/12/19 02:46:12 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Public Access Networks Corporation ("Panix").  It was developed under
      9  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1982, 1986, 1988, 1990, 1993
     42  *	The Regents of the University of California.  All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by the University of
     55  *	California, Berkeley and its contributors.
     56  * 4. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
     73  */
     74 
     75 #include "opt_pfil_hooks.h"
     76 #include "opt_mrouting.h"
     77 
     78 #include <sys/param.h>
     79 #include <sys/malloc.h>
     80 #include <sys/mbuf.h>
     81 #include <sys/errno.h>
     82 #include <sys/protosw.h>
     83 #include <sys/socket.h>
     84 #include <sys/socketvar.h>
     85 #include <sys/systm.h>
     86 
     87 #include <net/if.h>
     88 #include <net/route.h>
     89 #include <net/pfil.h>
     90 
     91 #include <netinet/in.h>
     92 #include <netinet/in_systm.h>
     93 #include <netinet/ip.h>
     94 #include <netinet/in_pcb.h>
     95 #include <netinet/in_var.h>
     96 #include <netinet/ip_var.h>
     97 
     98 #ifdef __vax__
     99 #include <machine/mtpr.h>
    100 #endif
    101 
    102 #include <machine/stdarg.h>
    103 
    104 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
    105 static void ip_mloopback
    106 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
    107 
    108 /*
    109  * IP output.  The packet in mbuf chain m contains a skeletal IP
    110  * header (with len, off, ttl, proto, tos, src, dst).
    111  * The mbuf chain containing the packet will be freed.
    112  * The mbuf opt, if present, will not be freed.
    113  */
    114 int
    115 #if __STDC__
    116 ip_output(struct mbuf *m0, ...)
    117 #else
    118 ip_output(m0, va_alist)
    119 	struct mbuf *m0;
    120 	va_dcl
    121 #endif
    122 {
    123 	register struct ip *ip, *mhip;
    124 	register struct ifnet *ifp;
    125 	register struct mbuf *m = m0;
    126 	register int hlen = sizeof (struct ip);
    127 	int len, off, error = 0;
    128 	struct route iproute;
    129 	struct sockaddr_in *dst;
    130 	struct in_ifaddr *ia;
    131 	struct mbuf *opt;
    132 	struct route *ro;
    133 	int flags;
    134 	int *mtu_p;
    135 	int mtu;
    136 	struct ip_moptions *imo;
    137 	va_list ap;
    138 #ifdef PFIL_HOOKS
    139 	struct packet_filter_hook *pfh;
    140 	struct mbuf *m1;
    141 	int rv;
    142 #endif /* PFIL_HOOKS */
    143 
    144 	va_start(ap, m0);
    145 	opt = va_arg(ap, struct mbuf *);
    146 	ro = va_arg(ap, struct route *);
    147 	flags = va_arg(ap, int);
    148 	imo = va_arg(ap, struct ip_moptions *);
    149 	if (flags & IP_RETURNMTU)
    150 		mtu_p = va_arg(ap, int *);
    151 	else
    152 		mtu_p = NULL;
    153 	va_end(ap);
    154 
    155 #ifdef	DIAGNOSTIC
    156 	if ((m->m_flags & M_PKTHDR) == 0)
    157 		panic("ip_output no HDR");
    158 #endif
    159 	if (opt) {
    160 		m = ip_insertoptions(m, opt, &len);
    161 		hlen = len;
    162 	}
    163 	ip = mtod(m, struct ip *);
    164 	/*
    165 	 * Fill in IP header.
    166 	 */
    167 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
    168 		ip->ip_v = IPVERSION;
    169 		ip->ip_off &= IP_DF;
    170 		ip->ip_id = htons(ip_id++);
    171 		ip->ip_hl = hlen >> 2;
    172 		ipstat.ips_localout++;
    173 	} else {
    174 		hlen = ip->ip_hl << 2;
    175 	}
    176 	/*
    177 	 * Route packet.
    178 	 */
    179 	if (ro == 0) {
    180 		ro = &iproute;
    181 		bzero((caddr_t)ro, sizeof (*ro));
    182 	}
    183 	dst = satosin(&ro->ro_dst);
    184 	/*
    185 	 * If there is a cached route,
    186 	 * check that it is to the same destination
    187 	 * and is still up.  If not, free it and try again.
    188 	 */
    189 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    190 	    !in_hosteq(dst->sin_addr, ip->ip_dst))) {
    191 		RTFREE(ro->ro_rt);
    192 		ro->ro_rt = (struct rtentry *)0;
    193 	}
    194 	if (ro->ro_rt == 0) {
    195 		dst->sin_family = AF_INET;
    196 		dst->sin_len = sizeof(*dst);
    197 		dst->sin_addr = ip->ip_dst;
    198 	}
    199 	/*
    200 	 * If routing to interface only,
    201 	 * short circuit routing lookup.
    202 	 */
    203 	if (flags & IP_ROUTETOIF) {
    204 		if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) {
    205 			ipstat.ips_noroute++;
    206 			error = ENETUNREACH;
    207 			goto bad;
    208 		}
    209 		ifp = ia->ia_ifp;
    210 		mtu = ifp->if_mtu;
    211 		ip->ip_ttl = 1;
    212 	} else {
    213 		if (ro->ro_rt == 0)
    214 			rtalloc(ro);
    215 		if (ro->ro_rt == 0) {
    216 			ipstat.ips_noroute++;
    217 			error = EHOSTUNREACH;
    218 			goto bad;
    219 		}
    220 		ia = ifatoia(ro->ro_rt->rt_ifa);
    221 		ifp = ro->ro_rt->rt_ifp;
    222 		if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0)
    223 			mtu = ifp->if_mtu;
    224 		ro->ro_rt->rt_use++;
    225 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
    226 			dst = satosin(ro->ro_rt->rt_gateway);
    227 	}
    228 	if (IN_MULTICAST(ip->ip_dst.s_addr)) {
    229 		struct in_multi *inm;
    230 
    231 		m->m_flags |= M_MCAST;
    232 		/*
    233 		 * IP destination address is multicast.  Make sure "dst"
    234 		 * still points to the address in "ro".  (It may have been
    235 		 * changed to point to a gateway address, above.)
    236 		 */
    237 		dst = satosin(&ro->ro_dst);
    238 		/*
    239 		 * See if the caller provided any multicast options
    240 		 */
    241 		if (imo != NULL) {
    242 			ip->ip_ttl = imo->imo_multicast_ttl;
    243 			if (imo->imo_multicast_ifp != NULL) {
    244 				ifp = imo->imo_multicast_ifp;
    245 				mtu = ifp->if_mtu;
    246 			}
    247 		} else
    248 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
    249 		/*
    250 		 * Confirm that the outgoing interface supports multicast.
    251 		 */
    252 		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    253 			ipstat.ips_noroute++;
    254 			error = ENETUNREACH;
    255 			goto bad;
    256 		}
    257 		/*
    258 		 * If source address not specified yet, use an address
    259 		 * of outgoing interface.
    260 		 */
    261 		if (in_nullhost(ip->ip_src)) {
    262 			register struct in_ifaddr *ia;
    263 
    264 			IFP_TO_IA(ifp, ia);
    265 			ip->ip_src = ia->ia_addr.sin_addr;
    266 		}
    267 
    268 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
    269 		if (inm != NULL &&
    270 		   (imo == NULL || imo->imo_multicast_loop)) {
    271 			/*
    272 			 * If we belong to the destination multicast group
    273 			 * on the outgoing interface, and the caller did not
    274 			 * forbid loopback, loop back a copy.
    275 			 */
    276 			ip_mloopback(ifp, m, dst);
    277 		}
    278 #ifdef MROUTING
    279 		else {
    280 			/*
    281 			 * If we are acting as a multicast router, perform
    282 			 * multicast forwarding as if the packet had just
    283 			 * arrived on the interface to which we are about
    284 			 * to send.  The multicast forwarding function
    285 			 * recursively calls this function, using the
    286 			 * IP_FORWARDING flag to prevent infinite recursion.
    287 			 *
    288 			 * Multicasts that are looped back by ip_mloopback(),
    289 			 * above, will be forwarded by the ip_input() routine,
    290 			 * if necessary.
    291 			 */
    292 			extern struct socket *ip_mrouter;
    293 
    294 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
    295 				if (ip_mforward(m, ifp) != 0) {
    296 					m_freem(m);
    297 					goto done;
    298 				}
    299 			}
    300 		}
    301 #endif
    302 		/*
    303 		 * Multicasts with a time-to-live of zero may be looped-
    304 		 * back, above, but must not be transmitted on a network.
    305 		 * Also, multicasts addressed to the loopback interface
    306 		 * are not sent -- the above call to ip_mloopback() will
    307 		 * loop back a copy if this host actually belongs to the
    308 		 * destination group on the loopback interface.
    309 		 */
    310 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
    311 			m_freem(m);
    312 			goto done;
    313 		}
    314 
    315 		goto sendit;
    316 	}
    317 #ifndef notdef
    318 	/*
    319 	 * If source address not specified yet, use address
    320 	 * of outgoing interface.
    321 	 */
    322 	if (in_nullhost(ip->ip_src))
    323 		ip->ip_src = ia->ia_addr.sin_addr;
    324 #endif
    325 	/*
    326 	 * Look for broadcast address and
    327 	 * and verify user is allowed to send
    328 	 * such a packet.
    329 	 */
    330 	if (in_broadcast(dst->sin_addr, ifp)) {
    331 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    332 			error = EADDRNOTAVAIL;
    333 			goto bad;
    334 		}
    335 		if ((flags & IP_ALLOWBROADCAST) == 0) {
    336 			error = EACCES;
    337 			goto bad;
    338 		}
    339 		/* don't allow broadcast messages to be fragmented */
    340 		if ((u_int16_t)ip->ip_len > ifp->if_mtu) {
    341 			error = EMSGSIZE;
    342 			goto bad;
    343 		}
    344 		m->m_flags |= M_BCAST;
    345 	} else
    346 		m->m_flags &= ~M_BCAST;
    347 
    348 #ifdef PFIL_HOOKS
    349 	/*
    350 	 * Run through list of hooks for output packets.
    351 	 */
    352 	m1 = m;
    353 	for (pfh = pfil_hook_get(PFIL_OUT); pfh; pfh = pfh->pfil_link.tqe_next)
    354 		if (pfh->pfil_func) {
    355 		    	rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
    356 			if (rv) {
    357 				error = EHOSTUNREACH;
    358 				goto done;
    359 			}
    360 			m = m1;
    361 			if (m == NULL)
    362 				goto done;
    363 			ip = mtod(m, struct ip *);
    364 		}
    365 #endif /* PFIL_HOOKS */
    366 sendit:
    367 	/*
    368 	 * If small enough for mtu of path, can just send directly.
    369 	 */
    370 	if ((u_int16_t)ip->ip_len <= mtu) {
    371 		ip->ip_len = htons((u_int16_t)ip->ip_len);
    372 		ip->ip_off = htons((u_int16_t)ip->ip_off);
    373 		ip->ip_sum = 0;
    374 		ip->ip_sum = in_cksum(m, hlen);
    375 		error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
    376 		goto done;
    377 	}
    378 	/*
    379 	 * Too large for interface; fragment if possible.
    380 	 * Must be able to put at least 8 bytes per fragment.
    381 	 */
    382 	if (ip->ip_off & IP_DF) {
    383 		if (flags & IP_RETURNMTU)
    384 			*mtu_p = mtu;
    385 		error = EMSGSIZE;
    386 		ipstat.ips_cantfrag++;
    387 		goto bad;
    388 	}
    389 	len = (mtu - hlen) &~ 7;
    390 	if (len < 8) {
    391 		error = EMSGSIZE;
    392 		goto bad;
    393 	}
    394 
    395     {
    396 	int mhlen, firstlen = len;
    397 	struct mbuf **mnext = &m->m_nextpkt;
    398 	int fragments = 0;
    399 	int s;
    400 
    401 	/*
    402 	 * Loop through length of segment after first fragment,
    403 	 * make new header and copy data of each part and link onto chain.
    404 	 */
    405 	m0 = m;
    406 	mhlen = sizeof (struct ip);
    407 	for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
    408 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    409 		if (m == 0) {
    410 			error = ENOBUFS;
    411 			ipstat.ips_odropped++;
    412 			goto sendorfree;
    413 		}
    414 		*mnext = m;
    415 		mnext = &m->m_nextpkt;
    416 		m->m_data += max_linkhdr;
    417 		mhip = mtod(m, struct ip *);
    418 		*mhip = *ip;
    419 		if (hlen > sizeof (struct ip)) {
    420 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
    421 			mhip->ip_hl = mhlen >> 2;
    422 		}
    423 		m->m_len = mhlen;
    424 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
    425 		if (ip->ip_off & IP_MF)
    426 			mhip->ip_off |= IP_MF;
    427 		if (off + len >= (u_int16_t)ip->ip_len)
    428 			len = (u_int16_t)ip->ip_len - off;
    429 		else
    430 			mhip->ip_off |= IP_MF;
    431 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
    432 		m->m_next = m_copy(m0, off, len);
    433 		if (m->m_next == 0) {
    434 			error = ENOBUFS;	/* ??? */
    435 			ipstat.ips_odropped++;
    436 			goto sendorfree;
    437 		}
    438 		m->m_pkthdr.len = mhlen + len;
    439 		m->m_pkthdr.rcvif = (struct ifnet *)0;
    440 		mhip->ip_off = htons((u_int16_t)mhip->ip_off);
    441 		mhip->ip_sum = 0;
    442 		mhip->ip_sum = in_cksum(m, mhlen);
    443 		ipstat.ips_ofragments++;
    444 		fragments++;
    445 	}
    446 	/*
    447 	 * Update first fragment by trimming what's been copied out
    448 	 * and updating header, then send each fragment (in order).
    449 	 */
    450 	m = m0;
    451 	m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
    452 	m->m_pkthdr.len = hlen + firstlen;
    453 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
    454 	ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF));
    455 	ip->ip_sum = 0;
    456 	ip->ip_sum = in_cksum(m, hlen);
    457 sendorfree:
    458 	/*
    459 	 * If there is no room for all the fragments, don't queue
    460 	 * any of them.
    461 	 */
    462 	s = splimp();
    463 	if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments)
    464 		error = ENOBUFS;
    465 	splx(s);
    466 	for (m = m0; m; m = m0) {
    467 		m0 = m->m_nextpkt;
    468 		m->m_nextpkt = 0;
    469 		if (error == 0)
    470 			error = (*ifp->if_output)(ifp, m, sintosa(dst),
    471 			    ro->ro_rt);
    472 		else
    473 			m_freem(m);
    474 	}
    475 
    476 	if (error == 0)
    477 		ipstat.ips_fragmented++;
    478     }
    479 done:
    480 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
    481 		RTFREE(ro->ro_rt);
    482 		ro->ro_rt = 0;
    483 	}
    484 	return (error);
    485 bad:
    486 	m_freem(m);
    487 	goto done;
    488 }
    489 
    490 /*
    491  * Determine the maximum length of the options to be inserted;
    492  * we would far rather allocate too much space rather than too little.
    493  */
    494 
    495 u_int
    496 ip_optlen(inp)
    497 	struct inpcb *inp;
    498 {
    499 	struct mbuf *m = inp->inp_options;
    500 
    501 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
    502 		return(m->m_len - offsetof(struct ipoption, ipopt_dst));
    503 	else
    504 		return 0;
    505 }
    506 
    507 
    508 /*
    509  * Insert IP options into preformed packet.
    510  * Adjust IP destination as required for IP source routing,
    511  * as indicated by a non-zero in_addr at the start of the options.
    512  */
    513 static struct mbuf *
    514 ip_insertoptions(m, opt, phlen)
    515 	register struct mbuf *m;
    516 	struct mbuf *opt;
    517 	int *phlen;
    518 {
    519 	register struct ipoption *p = mtod(opt, struct ipoption *);
    520 	struct mbuf *n;
    521 	register struct ip *ip = mtod(m, struct ip *);
    522 	unsigned optlen;
    523 
    524 	optlen = opt->m_len - sizeof(p->ipopt_dst);
    525 	if (optlen + (u_int16_t)ip->ip_len > IP_MAXPACKET)
    526 		return (m);		/* XXX should fail */
    527 	if (!in_nullhost(p->ipopt_dst))
    528 		ip->ip_dst = p->ipopt_dst;
    529 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
    530 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
    531 		if (n == 0)
    532 			return (m);
    533 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
    534 		m->m_len -= sizeof(struct ip);
    535 		m->m_data += sizeof(struct ip);
    536 		n->m_next = m;
    537 		m = n;
    538 		m->m_len = optlen + sizeof(struct ip);
    539 		m->m_data += max_linkhdr;
    540 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
    541 	} else {
    542 		m->m_data -= optlen;
    543 		m->m_len += optlen;
    544 		m->m_pkthdr.len += optlen;
    545 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
    546 	}
    547 	ip = mtod(m, struct ip *);
    548 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
    549 	*phlen = sizeof(struct ip) + optlen;
    550 	ip->ip_len += optlen;
    551 	return (m);
    552 }
    553 
    554 /*
    555  * Copy options from ip to jp,
    556  * omitting those not copied during fragmentation.
    557  */
    558 int
    559 ip_optcopy(ip, jp)
    560 	struct ip *ip, *jp;
    561 {
    562 	register u_char *cp, *dp;
    563 	int opt, optlen, cnt;
    564 
    565 	cp = (u_char *)(ip + 1);
    566 	dp = (u_char *)(jp + 1);
    567 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
    568 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    569 		opt = cp[0];
    570 		if (opt == IPOPT_EOL)
    571 			break;
    572 		if (opt == IPOPT_NOP) {
    573 			/* Preserve for IP mcast tunnel's LSRR alignment. */
    574 			*dp++ = IPOPT_NOP;
    575 			optlen = 1;
    576 			continue;
    577 		} else
    578 			optlen = cp[IPOPT_OLEN];
    579 		/* bogus lengths should have been caught by ip_dooptions */
    580 		if (optlen > cnt)
    581 			optlen = cnt;
    582 		if (IPOPT_COPIED(opt)) {
    583 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
    584 			dp += optlen;
    585 		}
    586 	}
    587 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
    588 		*dp++ = IPOPT_EOL;
    589 	return (optlen);
    590 }
    591 
    592 /*
    593  * IP socket option processing.
    594  */
    595 int
    596 ip_ctloutput(op, so, level, optname, mp)
    597 	int op;
    598 	struct socket *so;
    599 	int level, optname;
    600 	struct mbuf **mp;
    601 {
    602 	register struct inpcb *inp = sotoinpcb(so);
    603 	register struct mbuf *m = *mp;
    604 	register int optval = 0;
    605 	int error = 0;
    606 
    607 	if (level != IPPROTO_IP) {
    608 		error = EINVAL;
    609 		if (op == PRCO_SETOPT && *mp)
    610 			(void) m_free(*mp);
    611 	} else switch (op) {
    612 
    613 	case PRCO_SETOPT:
    614 		switch (optname) {
    615 		case IP_OPTIONS:
    616 #ifdef notyet
    617 		case IP_RETOPTS:
    618 			return (ip_pcbopts(optname, &inp->inp_options, m));
    619 #else
    620 			return (ip_pcbopts(&inp->inp_options, m));
    621 #endif
    622 
    623 		case IP_TOS:
    624 		case IP_TTL:
    625 		case IP_RECVOPTS:
    626 		case IP_RECVRETOPTS:
    627 		case IP_RECVDSTADDR:
    628 		case IP_RECVIF:
    629 			if (m == NULL || m->m_len != sizeof(int))
    630 				error = EINVAL;
    631 			else {
    632 				optval = *mtod(m, int *);
    633 				switch (optname) {
    634 
    635 				case IP_TOS:
    636 					inp->inp_ip.ip_tos = optval;
    637 					break;
    638 
    639 				case IP_TTL:
    640 					inp->inp_ip.ip_ttl = optval;
    641 					break;
    642 #define	OPTSET(bit) \
    643 	if (optval) \
    644 		inp->inp_flags |= bit; \
    645 	else \
    646 		inp->inp_flags &= ~bit;
    647 
    648 				case IP_RECVOPTS:
    649 					OPTSET(INP_RECVOPTS);
    650 					break;
    651 
    652 				case IP_RECVRETOPTS:
    653 					OPTSET(INP_RECVRETOPTS);
    654 					break;
    655 
    656 				case IP_RECVDSTADDR:
    657 					OPTSET(INP_RECVDSTADDR);
    658 					break;
    659 
    660 				case IP_RECVIF:
    661 					OPTSET(INP_RECVIF);
    662 					break;
    663 				}
    664 			}
    665 			break;
    666 #undef OPTSET
    667 
    668 		case IP_MULTICAST_IF:
    669 		case IP_MULTICAST_TTL:
    670 		case IP_MULTICAST_LOOP:
    671 		case IP_ADD_MEMBERSHIP:
    672 		case IP_DROP_MEMBERSHIP:
    673 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
    674 			break;
    675 
    676 		case IP_PORTRANGE:
    677 			if (m == 0 || m->m_len != sizeof(int))
    678 				error = EINVAL;
    679 			else {
    680 				optval = *mtod(m, int *);
    681 
    682 				switch (optval) {
    683 
    684 				case IP_PORTRANGE_DEFAULT:
    685 				case IP_PORTRANGE_HIGH:
    686 					inp->inp_flags &= ~(INP_LOWPORT);
    687 					break;
    688 
    689 				case IP_PORTRANGE_LOW:
    690 					inp->inp_flags |= INP_LOWPORT;
    691 					break;
    692 
    693 				default:
    694 					error = EINVAL;
    695 					break;
    696 				}
    697 			}
    698 			break;
    699 
    700 		default:
    701 			error = ENOPROTOOPT;
    702 			break;
    703 		}
    704 		if (m)
    705 			(void)m_free(m);
    706 		break;
    707 
    708 	case PRCO_GETOPT:
    709 		switch (optname) {
    710 		case IP_OPTIONS:
    711 		case IP_RETOPTS:
    712 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    713 			if (inp->inp_options) {
    714 				m->m_len = inp->inp_options->m_len;
    715 				bcopy(mtod(inp->inp_options, caddr_t),
    716 				    mtod(m, caddr_t), (unsigned)m->m_len);
    717 			} else
    718 				m->m_len = 0;
    719 			break;
    720 
    721 		case IP_TOS:
    722 		case IP_TTL:
    723 		case IP_RECVOPTS:
    724 		case IP_RECVRETOPTS:
    725 		case IP_RECVDSTADDR:
    726 		case IP_RECVIF:
    727 		case IP_ERRORMTU:
    728 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    729 			m->m_len = sizeof(int);
    730 			switch (optname) {
    731 
    732 			case IP_TOS:
    733 				optval = inp->inp_ip.ip_tos;
    734 				break;
    735 
    736 			case IP_TTL:
    737 				optval = inp->inp_ip.ip_ttl;
    738 				break;
    739 
    740 			case IP_ERRORMTU:
    741 				optval = inp->inp_errormtu;
    742 				break;
    743 
    744 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
    745 
    746 			case IP_RECVOPTS:
    747 				optval = OPTBIT(INP_RECVOPTS);
    748 				break;
    749 
    750 			case IP_RECVRETOPTS:
    751 				optval = OPTBIT(INP_RECVRETOPTS);
    752 				break;
    753 
    754 			case IP_RECVDSTADDR:
    755 				optval = OPTBIT(INP_RECVDSTADDR);
    756 				break;
    757 
    758 			case IP_RECVIF:
    759 				optval = OPTBIT(INP_RECVIF);
    760 				break;
    761 			}
    762 			*mtod(m, int *) = optval;
    763 			break;
    764 
    765 		case IP_MULTICAST_IF:
    766 		case IP_MULTICAST_TTL:
    767 		case IP_MULTICAST_LOOP:
    768 		case IP_ADD_MEMBERSHIP:
    769 		case IP_DROP_MEMBERSHIP:
    770 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
    771 			break;
    772 
    773 		case IP_PORTRANGE:
    774 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    775 			m->m_len = sizeof(int);
    776 
    777 			if (inp->inp_flags & INP_LOWPORT)
    778 				optval = IP_PORTRANGE_LOW;
    779 			else
    780 				optval = IP_PORTRANGE_DEFAULT;
    781 
    782 			*mtod(m, int *) = optval;
    783 			break;
    784 
    785 		default:
    786 			error = ENOPROTOOPT;
    787 			break;
    788 		}
    789 		break;
    790 	}
    791 	return (error);
    792 }
    793 
    794 /*
    795  * Set up IP options in pcb for insertion in output packets.
    796  * Store in mbuf with pointer in pcbopt, adding pseudo-option
    797  * with destination address if source routed.
    798  */
    799 int
    800 #ifdef notyet
    801 ip_pcbopts(optname, pcbopt, m)
    802 	int optname;
    803 #else
    804 ip_pcbopts(pcbopt, m)
    805 #endif
    806 	struct mbuf **pcbopt;
    807 	register struct mbuf *m;
    808 {
    809 	register int cnt, optlen;
    810 	register u_char *cp;
    811 	u_char opt;
    812 
    813 	/* turn off any old options */
    814 	if (*pcbopt)
    815 		(void)m_free(*pcbopt);
    816 	*pcbopt = 0;
    817 	if (m == (struct mbuf *)0 || m->m_len == 0) {
    818 		/*
    819 		 * Only turning off any previous options.
    820 		 */
    821 		if (m)
    822 			(void)m_free(m);
    823 		return (0);
    824 	}
    825 
    826 #ifndef	vax
    827 	if (m->m_len % sizeof(int32_t))
    828 		goto bad;
    829 #endif
    830 	/*
    831 	 * IP first-hop destination address will be stored before
    832 	 * actual options; move other options back
    833 	 * and clear it when none present.
    834 	 */
    835 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
    836 		goto bad;
    837 	cnt = m->m_len;
    838 	m->m_len += sizeof(struct in_addr);
    839 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
    840 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
    841 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
    842 
    843 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    844 		opt = cp[IPOPT_OPTVAL];
    845 		if (opt == IPOPT_EOL)
    846 			break;
    847 		if (opt == IPOPT_NOP)
    848 			optlen = 1;
    849 		else {
    850 			optlen = cp[IPOPT_OLEN];
    851 			if (optlen <= IPOPT_OLEN || optlen > cnt)
    852 				goto bad;
    853 		}
    854 		switch (opt) {
    855 
    856 		default:
    857 			break;
    858 
    859 		case IPOPT_LSRR:
    860 		case IPOPT_SSRR:
    861 			/*
    862 			 * user process specifies route as:
    863 			 *	->A->B->C->D
    864 			 * D must be our final destination (but we can't
    865 			 * check that since we may not have connected yet).
    866 			 * A is first hop destination, which doesn't appear in
    867 			 * actual IP option, but is stored before the options.
    868 			 */
    869 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
    870 				goto bad;
    871 			m->m_len -= sizeof(struct in_addr);
    872 			cnt -= sizeof(struct in_addr);
    873 			optlen -= sizeof(struct in_addr);
    874 			cp[IPOPT_OLEN] = optlen;
    875 			/*
    876 			 * Move first hop before start of options.
    877 			 */
    878 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
    879 			    sizeof(struct in_addr));
    880 			/*
    881 			 * Then copy rest of options back
    882 			 * to close up the deleted entry.
    883 			 */
    884 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
    885 			    sizeof(struct in_addr)),
    886 			    (caddr_t)&cp[IPOPT_OFFSET+1],
    887 			    (unsigned)cnt + sizeof(struct in_addr));
    888 			break;
    889 		}
    890 	}
    891 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
    892 		goto bad;
    893 	*pcbopt = m;
    894 	return (0);
    895 
    896 bad:
    897 	(void)m_free(m);
    898 	return (EINVAL);
    899 }
    900 
    901 /*
    902  * Set the IP multicast options in response to user setsockopt().
    903  */
    904 int
    905 ip_setmoptions(optname, imop, m)
    906 	int optname;
    907 	struct ip_moptions **imop;
    908 	struct mbuf *m;
    909 {
    910 	register int error = 0;
    911 	u_char loop;
    912 	register int i;
    913 	struct in_addr addr;
    914 	register struct ip_mreq *mreq;
    915 	register struct ifnet *ifp;
    916 	register struct ip_moptions *imo = *imop;
    917 	struct route ro;
    918 	register struct sockaddr_in *dst;
    919 
    920 	if (imo == NULL) {
    921 		/*
    922 		 * No multicast option buffer attached to the pcb;
    923 		 * allocate one and initialize to default values.
    924 		 */
    925 		imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
    926 		    M_WAITOK);
    927 
    928 		if (imo == NULL)
    929 			return (ENOBUFS);
    930 		*imop = imo;
    931 		imo->imo_multicast_ifp = NULL;
    932 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
    933 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
    934 		imo->imo_num_memberships = 0;
    935 	}
    936 
    937 	switch (optname) {
    938 
    939 	case IP_MULTICAST_IF:
    940 		/*
    941 		 * Select the interface for outgoing multicast packets.
    942 		 */
    943 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
    944 			error = EINVAL;
    945 			break;
    946 		}
    947 		addr = *(mtod(m, struct in_addr *));
    948 		/*
    949 		 * INADDR_ANY is used to remove a previous selection.
    950 		 * When no interface is selected, a default one is
    951 		 * chosen every time a multicast packet is sent.
    952 		 */
    953 		if (in_nullhost(addr)) {
    954 			imo->imo_multicast_ifp = NULL;
    955 			break;
    956 		}
    957 		/*
    958 		 * The selected interface is identified by its local
    959 		 * IP address.  Find the interface and confirm that
    960 		 * it supports multicasting.
    961 		 */
    962 		INADDR_TO_IFP(addr, ifp);
    963 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
    964 			error = EADDRNOTAVAIL;
    965 			break;
    966 		}
    967 		imo->imo_multicast_ifp = ifp;
    968 		break;
    969 
    970 	case IP_MULTICAST_TTL:
    971 		/*
    972 		 * Set the IP time-to-live for outgoing multicast packets.
    973 		 */
    974 		if (m == NULL || m->m_len != 1) {
    975 			error = EINVAL;
    976 			break;
    977 		}
    978 		imo->imo_multicast_ttl = *(mtod(m, u_char *));
    979 		break;
    980 
    981 	case IP_MULTICAST_LOOP:
    982 		/*
    983 		 * Set the loopback flag for outgoing multicast packets.
    984 		 * Must be zero or one.
    985 		 */
    986 		if (m == NULL || m->m_len != 1 ||
    987 		   (loop = *(mtod(m, u_char *))) > 1) {
    988 			error = EINVAL;
    989 			break;
    990 		}
    991 		imo->imo_multicast_loop = loop;
    992 		break;
    993 
    994 	case IP_ADD_MEMBERSHIP:
    995 		/*
    996 		 * Add a multicast group membership.
    997 		 * Group must be a valid IP multicast address.
    998 		 */
    999 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
   1000 			error = EINVAL;
   1001 			break;
   1002 		}
   1003 		mreq = mtod(m, struct ip_mreq *);
   1004 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
   1005 			error = EINVAL;
   1006 			break;
   1007 		}
   1008 		/*
   1009 		 * If no interface address was provided, use the interface of
   1010 		 * the route to the given multicast address.
   1011 		 */
   1012 		if (in_nullhost(mreq->imr_interface)) {
   1013 			bzero((caddr_t)&ro, sizeof(ro));
   1014 			ro.ro_rt = NULL;
   1015 			dst = satosin(&ro.ro_dst);
   1016 			dst->sin_len = sizeof(*dst);
   1017 			dst->sin_family = AF_INET;
   1018 			dst->sin_addr = mreq->imr_multiaddr;
   1019 			rtalloc(&ro);
   1020 			if (ro.ro_rt == NULL) {
   1021 				error = EADDRNOTAVAIL;
   1022 				break;
   1023 			}
   1024 			ifp = ro.ro_rt->rt_ifp;
   1025 			rtfree(ro.ro_rt);
   1026 		} else {
   1027 			INADDR_TO_IFP(mreq->imr_interface, ifp);
   1028 		}
   1029 		/*
   1030 		 * See if we found an interface, and confirm that it
   1031 		 * supports multicast.
   1032 		 */
   1033 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1034 			error = EADDRNOTAVAIL;
   1035 			break;
   1036 		}
   1037 		/*
   1038 		 * See if the membership already exists or if all the
   1039 		 * membership slots are full.
   1040 		 */
   1041 		for (i = 0; i < imo->imo_num_memberships; ++i) {
   1042 			if (imo->imo_membership[i]->inm_ifp == ifp &&
   1043 			    in_hosteq(imo->imo_membership[i]->inm_addr,
   1044 				      mreq->imr_multiaddr))
   1045 				break;
   1046 		}
   1047 		if (i < imo->imo_num_memberships) {
   1048 			error = EADDRINUSE;
   1049 			break;
   1050 		}
   1051 		if (i == IP_MAX_MEMBERSHIPS) {
   1052 			error = ETOOMANYREFS;
   1053 			break;
   1054 		}
   1055 		/*
   1056 		 * Everything looks good; add a new record to the multicast
   1057 		 * address list for the given interface.
   1058 		 */
   1059 		if ((imo->imo_membership[i] =
   1060 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
   1061 			error = ENOBUFS;
   1062 			break;
   1063 		}
   1064 		++imo->imo_num_memberships;
   1065 		break;
   1066 
   1067 	case IP_DROP_MEMBERSHIP:
   1068 		/*
   1069 		 * Drop a multicast group membership.
   1070 		 * Group must be a valid IP multicast address.
   1071 		 */
   1072 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
   1073 			error = EINVAL;
   1074 			break;
   1075 		}
   1076 		mreq = mtod(m, struct ip_mreq *);
   1077 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
   1078 			error = EINVAL;
   1079 			break;
   1080 		}
   1081 		/*
   1082 		 * If an interface address was specified, get a pointer
   1083 		 * to its ifnet structure.
   1084 		 */
   1085 		if (in_nullhost(mreq->imr_interface))
   1086 			ifp = NULL;
   1087 		else {
   1088 			INADDR_TO_IFP(mreq->imr_interface, ifp);
   1089 			if (ifp == NULL) {
   1090 				error = EADDRNOTAVAIL;
   1091 				break;
   1092 			}
   1093 		}
   1094 		/*
   1095 		 * Find the membership in the membership array.
   1096 		 */
   1097 		for (i = 0; i < imo->imo_num_memberships; ++i) {
   1098 			if ((ifp == NULL ||
   1099 			     imo->imo_membership[i]->inm_ifp == ifp) &&
   1100 			     in_hosteq(imo->imo_membership[i]->inm_addr,
   1101 				       mreq->imr_multiaddr))
   1102 				break;
   1103 		}
   1104 		if (i == imo->imo_num_memberships) {
   1105 			error = EADDRNOTAVAIL;
   1106 			break;
   1107 		}
   1108 		/*
   1109 		 * Give up the multicast address record to which the
   1110 		 * membership points.
   1111 		 */
   1112 		in_delmulti(imo->imo_membership[i]);
   1113 		/*
   1114 		 * Remove the gap in the membership array.
   1115 		 */
   1116 		for (++i; i < imo->imo_num_memberships; ++i)
   1117 			imo->imo_membership[i-1] = imo->imo_membership[i];
   1118 		--imo->imo_num_memberships;
   1119 		break;
   1120 
   1121 	default:
   1122 		error = EOPNOTSUPP;
   1123 		break;
   1124 	}
   1125 
   1126 	/*
   1127 	 * If all options have default values, no need to keep the mbuf.
   1128 	 */
   1129 	if (imo->imo_multicast_ifp == NULL &&
   1130 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
   1131 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
   1132 	    imo->imo_num_memberships == 0) {
   1133 		free(*imop, M_IPMOPTS);
   1134 		*imop = NULL;
   1135 	}
   1136 
   1137 	return (error);
   1138 }
   1139 
   1140 /*
   1141  * Return the IP multicast options in response to user getsockopt().
   1142  */
   1143 int
   1144 ip_getmoptions(optname, imo, mp)
   1145 	int optname;
   1146 	register struct ip_moptions *imo;
   1147 	register struct mbuf **mp;
   1148 {
   1149 	u_char *ttl;
   1150 	u_char *loop;
   1151 	struct in_addr *addr;
   1152 	struct in_ifaddr *ia;
   1153 
   1154 	*mp = m_get(M_WAIT, MT_SOOPTS);
   1155 
   1156 	switch (optname) {
   1157 
   1158 	case IP_MULTICAST_IF:
   1159 		addr = mtod(*mp, struct in_addr *);
   1160 		(*mp)->m_len = sizeof(struct in_addr);
   1161 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
   1162 			*addr = zeroin_addr;
   1163 		else {
   1164 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
   1165 			*addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
   1166 		}
   1167 		return (0);
   1168 
   1169 	case IP_MULTICAST_TTL:
   1170 		ttl = mtod(*mp, u_char *);
   1171 		(*mp)->m_len = 1;
   1172 		*ttl = imo ? imo->imo_multicast_ttl
   1173 			   : IP_DEFAULT_MULTICAST_TTL;
   1174 		return (0);
   1175 
   1176 	case IP_MULTICAST_LOOP:
   1177 		loop = mtod(*mp, u_char *);
   1178 		(*mp)->m_len = 1;
   1179 		*loop = imo ? imo->imo_multicast_loop
   1180 			    : IP_DEFAULT_MULTICAST_LOOP;
   1181 		return (0);
   1182 
   1183 	default:
   1184 		return (EOPNOTSUPP);
   1185 	}
   1186 }
   1187 
   1188 /*
   1189  * Discard the IP multicast options.
   1190  */
   1191 void
   1192 ip_freemoptions(imo)
   1193 	register struct ip_moptions *imo;
   1194 {
   1195 	register int i;
   1196 
   1197 	if (imo != NULL) {
   1198 		for (i = 0; i < imo->imo_num_memberships; ++i)
   1199 			in_delmulti(imo->imo_membership[i]);
   1200 		free(imo, M_IPMOPTS);
   1201 	}
   1202 }
   1203 
   1204 /*
   1205  * Routine called from ip_output() to loop back a copy of an IP multicast
   1206  * packet to the input queue of a specified interface.  Note that this
   1207  * calls the output routine of the loopback "driver", but with an interface
   1208  * pointer that might NOT be &loif -- easier than replicating that code here.
   1209  */
   1210 static void
   1211 ip_mloopback(ifp, m, dst)
   1212 	struct ifnet *ifp;
   1213 	register struct mbuf *m;
   1214 	register struct sockaddr_in *dst;
   1215 {
   1216 	register struct ip *ip;
   1217 	struct mbuf *copym;
   1218 
   1219 	copym = m_copy(m, 0, M_COPYALL);
   1220 	if (copym != NULL) {
   1221 		/*
   1222 		 * We don't bother to fragment if the IP length is greater
   1223 		 * than the interface's MTU.  Can this possibly matter?
   1224 		 */
   1225 		ip = mtod(copym, struct ip *);
   1226 		ip->ip_len = htons((u_int16_t)ip->ip_len);
   1227 		ip->ip_off = htons((u_int16_t)ip->ip_off);
   1228 		ip->ip_sum = 0;
   1229 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
   1230 		(void) looutput(ifp, copym, sintosa(dst), NULL);
   1231 	}
   1232 }
   1233