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