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