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