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