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