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