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