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