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