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