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