Home | History | Annotate | Line # | Download | only in netinet
ip_output.c revision 1.37
      1 /*	$NetBSD: ip_output.c,v 1.37 1997/01/11 05:21:11 thorpej 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 		case IP_RECVIF:
    550 			if (m == NULL || m->m_len != sizeof(int))
    551 				error = EINVAL;
    552 			else {
    553 				optval = *mtod(m, int *);
    554 				switch (optname) {
    555 
    556 				case IP_TOS:
    557 					inp->inp_ip.ip_tos = optval;
    558 					break;
    559 
    560 				case IP_TTL:
    561 					inp->inp_ip.ip_ttl = optval;
    562 					break;
    563 #define	OPTSET(bit) \
    564 	if (optval) \
    565 		inp->inp_flags |= bit; \
    566 	else \
    567 		inp->inp_flags &= ~bit;
    568 
    569 				case IP_RECVOPTS:
    570 					OPTSET(INP_RECVOPTS);
    571 					break;
    572 
    573 				case IP_RECVRETOPTS:
    574 					OPTSET(INP_RECVRETOPTS);
    575 					break;
    576 
    577 				case IP_RECVDSTADDR:
    578 					OPTSET(INP_RECVDSTADDR);
    579 					break;
    580 
    581 				case IP_RECVIF:
    582 					OPTSET(INP_RECVIF);
    583 					break;
    584 				}
    585 			}
    586 			break;
    587 #undef OPTSET
    588 
    589 		case IP_MULTICAST_IF:
    590 		case IP_MULTICAST_TTL:
    591 		case IP_MULTICAST_LOOP:
    592 		case IP_ADD_MEMBERSHIP:
    593 		case IP_DROP_MEMBERSHIP:
    594 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
    595 			break;
    596 
    597 		default:
    598 			error = ENOPROTOOPT;
    599 			break;
    600 		}
    601 		if (m)
    602 			(void)m_free(m);
    603 		break;
    604 
    605 	case PRCO_GETOPT:
    606 		switch (optname) {
    607 		case IP_OPTIONS:
    608 		case IP_RETOPTS:
    609 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    610 			if (inp->inp_options) {
    611 				m->m_len = inp->inp_options->m_len;
    612 				bcopy(mtod(inp->inp_options, caddr_t),
    613 				    mtod(m, caddr_t), (unsigned)m->m_len);
    614 			} else
    615 				m->m_len = 0;
    616 			break;
    617 
    618 		case IP_TOS:
    619 		case IP_TTL:
    620 		case IP_RECVOPTS:
    621 		case IP_RECVRETOPTS:
    622 		case IP_RECVDSTADDR:
    623 		case IP_RECVIF:
    624 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    625 			m->m_len = sizeof(int);
    626 			switch (optname) {
    627 
    628 			case IP_TOS:
    629 				optval = inp->inp_ip.ip_tos;
    630 				break;
    631 
    632 			case IP_TTL:
    633 				optval = inp->inp_ip.ip_ttl;
    634 				break;
    635 
    636 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
    637 
    638 			case IP_RECVOPTS:
    639 				optval = OPTBIT(INP_RECVOPTS);
    640 				break;
    641 
    642 			case IP_RECVRETOPTS:
    643 				optval = OPTBIT(INP_RECVRETOPTS);
    644 				break;
    645 
    646 			case IP_RECVDSTADDR:
    647 				optval = OPTBIT(INP_RECVDSTADDR);
    648 				break;
    649 
    650 			case IP_RECVIF:
    651 				optval = OPTBIT(INP_RECVIF);
    652 				break;
    653 			}
    654 			*mtod(m, int *) = optval;
    655 			break;
    656 
    657 		case IP_MULTICAST_IF:
    658 		case IP_MULTICAST_TTL:
    659 		case IP_MULTICAST_LOOP:
    660 		case IP_ADD_MEMBERSHIP:
    661 		case IP_DROP_MEMBERSHIP:
    662 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
    663 			break;
    664 
    665 		default:
    666 			error = ENOPROTOOPT;
    667 			break;
    668 		}
    669 		break;
    670 	}
    671 	return (error);
    672 }
    673 
    674 /*
    675  * Set up IP options in pcb for insertion in output packets.
    676  * Store in mbuf with pointer in pcbopt, adding pseudo-option
    677  * with destination address if source routed.
    678  */
    679 int
    680 #ifdef notyet
    681 ip_pcbopts(optname, pcbopt, m)
    682 	int optname;
    683 #else
    684 ip_pcbopts(pcbopt, m)
    685 #endif
    686 	struct mbuf **pcbopt;
    687 	register struct mbuf *m;
    688 {
    689 	register cnt, optlen;
    690 	register u_char *cp;
    691 	u_char opt;
    692 
    693 	/* turn off any old options */
    694 	if (*pcbopt)
    695 		(void)m_free(*pcbopt);
    696 	*pcbopt = 0;
    697 	if (m == (struct mbuf *)0 || m->m_len == 0) {
    698 		/*
    699 		 * Only turning off any previous options.
    700 		 */
    701 		if (m)
    702 			(void)m_free(m);
    703 		return (0);
    704 	}
    705 
    706 #ifndef	vax
    707 	if (m->m_len % sizeof(int32_t))
    708 		goto bad;
    709 #endif
    710 	/*
    711 	 * IP first-hop destination address will be stored before
    712 	 * actual options; move other options back
    713 	 * and clear it when none present.
    714 	 */
    715 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
    716 		goto bad;
    717 	cnt = m->m_len;
    718 	m->m_len += sizeof(struct in_addr);
    719 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
    720 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
    721 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
    722 
    723 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    724 		opt = cp[IPOPT_OPTVAL];
    725 		if (opt == IPOPT_EOL)
    726 			break;
    727 		if (opt == IPOPT_NOP)
    728 			optlen = 1;
    729 		else {
    730 			optlen = cp[IPOPT_OLEN];
    731 			if (optlen <= IPOPT_OLEN || optlen > cnt)
    732 				goto bad;
    733 		}
    734 		switch (opt) {
    735 
    736 		default:
    737 			break;
    738 
    739 		case IPOPT_LSRR:
    740 		case IPOPT_SSRR:
    741 			/*
    742 			 * user process specifies route as:
    743 			 *	->A->B->C->D
    744 			 * D must be our final destination (but we can't
    745 			 * check that since we may not have connected yet).
    746 			 * A is first hop destination, which doesn't appear in
    747 			 * actual IP option, but is stored before the options.
    748 			 */
    749 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
    750 				goto bad;
    751 			m->m_len -= sizeof(struct in_addr);
    752 			cnt -= sizeof(struct in_addr);
    753 			optlen -= sizeof(struct in_addr);
    754 			cp[IPOPT_OLEN] = optlen;
    755 			/*
    756 			 * Move first hop before start of options.
    757 			 */
    758 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
    759 			    sizeof(struct in_addr));
    760 			/*
    761 			 * Then copy rest of options back
    762 			 * to close up the deleted entry.
    763 			 */
    764 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
    765 			    sizeof(struct in_addr)),
    766 			    (caddr_t)&cp[IPOPT_OFFSET+1],
    767 			    (unsigned)cnt + sizeof(struct in_addr));
    768 			break;
    769 		}
    770 	}
    771 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
    772 		goto bad;
    773 	*pcbopt = m;
    774 	return (0);
    775 
    776 bad:
    777 	(void)m_free(m);
    778 	return (EINVAL);
    779 }
    780 
    781 /*
    782  * Set the IP multicast options in response to user setsockopt().
    783  */
    784 int
    785 ip_setmoptions(optname, imop, m)
    786 	int optname;
    787 	struct ip_moptions **imop;
    788 	struct mbuf *m;
    789 {
    790 	register int error = 0;
    791 	u_char loop;
    792 	register int i;
    793 	struct in_addr addr;
    794 	register struct ip_mreq *mreq;
    795 	register struct ifnet *ifp;
    796 	register struct ip_moptions *imo = *imop;
    797 	struct route ro;
    798 	register struct sockaddr_in *dst;
    799 
    800 	if (imo == NULL) {
    801 		/*
    802 		 * No multicast option buffer attached to the pcb;
    803 		 * allocate one and initialize to default values.
    804 		 */
    805 		imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
    806 		    M_WAITOK);
    807 
    808 		if (imo == NULL)
    809 			return (ENOBUFS);
    810 		*imop = imo;
    811 		imo->imo_multicast_ifp = NULL;
    812 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
    813 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
    814 		imo->imo_num_memberships = 0;
    815 	}
    816 
    817 	switch (optname) {
    818 
    819 	case IP_MULTICAST_IF:
    820 		/*
    821 		 * Select the interface for outgoing multicast packets.
    822 		 */
    823 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
    824 			error = EINVAL;
    825 			break;
    826 		}
    827 		addr = *(mtod(m, struct in_addr *));
    828 		/*
    829 		 * INADDR_ANY is used to remove a previous selection.
    830 		 * When no interface is selected, a default one is
    831 		 * chosen every time a multicast packet is sent.
    832 		 */
    833 		if (in_nullhost(addr)) {
    834 			imo->imo_multicast_ifp = NULL;
    835 			break;
    836 		}
    837 		/*
    838 		 * The selected interface is identified by its local
    839 		 * IP address.  Find the interface and confirm that
    840 		 * it supports multicasting.
    841 		 */
    842 		INADDR_TO_IFP(addr, ifp);
    843 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
    844 			error = EADDRNOTAVAIL;
    845 			break;
    846 		}
    847 		imo->imo_multicast_ifp = ifp;
    848 		break;
    849 
    850 	case IP_MULTICAST_TTL:
    851 		/*
    852 		 * Set the IP time-to-live for outgoing multicast packets.
    853 		 */
    854 		if (m == NULL || m->m_len != 1) {
    855 			error = EINVAL;
    856 			break;
    857 		}
    858 		imo->imo_multicast_ttl = *(mtod(m, u_char *));
    859 		break;
    860 
    861 	case IP_MULTICAST_LOOP:
    862 		/*
    863 		 * Set the loopback flag for outgoing multicast packets.
    864 		 * Must be zero or one.
    865 		 */
    866 		if (m == NULL || m->m_len != 1 ||
    867 		   (loop = *(mtod(m, u_char *))) > 1) {
    868 			error = EINVAL;
    869 			break;
    870 		}
    871 		imo->imo_multicast_loop = loop;
    872 		break;
    873 
    874 	case IP_ADD_MEMBERSHIP:
    875 		/*
    876 		 * Add a multicast group membership.
    877 		 * Group must be a valid IP multicast address.
    878 		 */
    879 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
    880 			error = EINVAL;
    881 			break;
    882 		}
    883 		mreq = mtod(m, struct ip_mreq *);
    884 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
    885 			error = EINVAL;
    886 			break;
    887 		}
    888 		/*
    889 		 * If no interface address was provided, use the interface of
    890 		 * the route to the given multicast address.
    891 		 */
    892 		if (in_nullhost(mreq->imr_interface)) {
    893 			ro.ro_rt = NULL;
    894 			dst = satosin(&ro.ro_dst);
    895 			dst->sin_len = sizeof(*dst);
    896 			dst->sin_family = AF_INET;
    897 			dst->sin_addr = mreq->imr_multiaddr;
    898 			rtalloc(&ro);
    899 			if (ro.ro_rt == NULL) {
    900 				error = EADDRNOTAVAIL;
    901 				break;
    902 			}
    903 			ifp = ro.ro_rt->rt_ifp;
    904 			rtfree(ro.ro_rt);
    905 		} else {
    906 			INADDR_TO_IFP(mreq->imr_interface, ifp);
    907 		}
    908 		/*
    909 		 * See if we found an interface, and confirm that it
    910 		 * supports multicast.
    911 		 */
    912 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
    913 			error = EADDRNOTAVAIL;
    914 			break;
    915 		}
    916 		/*
    917 		 * See if the membership already exists or if all the
    918 		 * membership slots are full.
    919 		 */
    920 		for (i = 0; i < imo->imo_num_memberships; ++i) {
    921 			if (imo->imo_membership[i]->inm_ifp == ifp &&
    922 			    in_hosteq(imo->imo_membership[i]->inm_addr,
    923 				      mreq->imr_multiaddr))
    924 				break;
    925 		}
    926 		if (i < imo->imo_num_memberships) {
    927 			error = EADDRINUSE;
    928 			break;
    929 		}
    930 		if (i == IP_MAX_MEMBERSHIPS) {
    931 			error = ETOOMANYREFS;
    932 			break;
    933 		}
    934 		/*
    935 		 * Everything looks good; add a new record to the multicast
    936 		 * address list for the given interface.
    937 		 */
    938 		if ((imo->imo_membership[i] =
    939 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
    940 			error = ENOBUFS;
    941 			break;
    942 		}
    943 		++imo->imo_num_memberships;
    944 		break;
    945 
    946 	case IP_DROP_MEMBERSHIP:
    947 		/*
    948 		 * Drop a multicast group membership.
    949 		 * Group must be a valid IP multicast address.
    950 		 */
    951 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
    952 			error = EINVAL;
    953 			break;
    954 		}
    955 		mreq = mtod(m, struct ip_mreq *);
    956 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
    957 			error = EINVAL;
    958 			break;
    959 		}
    960 		/*
    961 		 * If an interface address was specified, get a pointer
    962 		 * to its ifnet structure.
    963 		 */
    964 		if (in_nullhost(mreq->imr_interface))
    965 			ifp = NULL;
    966 		else {
    967 			INADDR_TO_IFP(mreq->imr_interface, ifp);
    968 			if (ifp == NULL) {
    969 				error = EADDRNOTAVAIL;
    970 				break;
    971 			}
    972 		}
    973 		/*
    974 		 * Find the membership in the membership array.
    975 		 */
    976 		for (i = 0; i < imo->imo_num_memberships; ++i) {
    977 			if ((ifp == NULL ||
    978 			     imo->imo_membership[i]->inm_ifp == ifp) &&
    979 			     in_hosteq(imo->imo_membership[i]->inm_addr,
    980 				       mreq->imr_multiaddr))
    981 				break;
    982 		}
    983 		if (i == imo->imo_num_memberships) {
    984 			error = EADDRNOTAVAIL;
    985 			break;
    986 		}
    987 		/*
    988 		 * Give up the multicast address record to which the
    989 		 * membership points.
    990 		 */
    991 		in_delmulti(imo->imo_membership[i]);
    992 		/*
    993 		 * Remove the gap in the membership array.
    994 		 */
    995 		for (++i; i < imo->imo_num_memberships; ++i)
    996 			imo->imo_membership[i-1] = imo->imo_membership[i];
    997 		--imo->imo_num_memberships;
    998 		break;
    999 
   1000 	default:
   1001 		error = EOPNOTSUPP;
   1002 		break;
   1003 	}
   1004 
   1005 	/*
   1006 	 * If all options have default values, no need to keep the mbuf.
   1007 	 */
   1008 	if (imo->imo_multicast_ifp == NULL &&
   1009 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
   1010 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
   1011 	    imo->imo_num_memberships == 0) {
   1012 		free(*imop, M_IPMOPTS);
   1013 		*imop = NULL;
   1014 	}
   1015 
   1016 	return (error);
   1017 }
   1018 
   1019 /*
   1020  * Return the IP multicast options in response to user getsockopt().
   1021  */
   1022 int
   1023 ip_getmoptions(optname, imo, mp)
   1024 	int optname;
   1025 	register struct ip_moptions *imo;
   1026 	register struct mbuf **mp;
   1027 {
   1028 	u_char *ttl;
   1029 	u_char *loop;
   1030 	struct in_addr *addr;
   1031 	struct in_ifaddr *ia;
   1032 
   1033 	*mp = m_get(M_WAIT, MT_SOOPTS);
   1034 
   1035 	switch (optname) {
   1036 
   1037 	case IP_MULTICAST_IF:
   1038 		addr = mtod(*mp, struct in_addr *);
   1039 		(*mp)->m_len = sizeof(struct in_addr);
   1040 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
   1041 			*addr = zeroin_addr;
   1042 		else {
   1043 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
   1044 			*addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
   1045 		}
   1046 		return (0);
   1047 
   1048 	case IP_MULTICAST_TTL:
   1049 		ttl = mtod(*mp, u_char *);
   1050 		(*mp)->m_len = 1;
   1051 		*ttl = imo ? imo->imo_multicast_ttl
   1052 			   : IP_DEFAULT_MULTICAST_TTL;
   1053 		return (0);
   1054 
   1055 	case IP_MULTICAST_LOOP:
   1056 		loop = mtod(*mp, u_char *);
   1057 		(*mp)->m_len = 1;
   1058 		*loop = imo ? imo->imo_multicast_loop
   1059 			    : IP_DEFAULT_MULTICAST_LOOP;
   1060 		return (0);
   1061 
   1062 	default:
   1063 		return (EOPNOTSUPP);
   1064 	}
   1065 }
   1066 
   1067 /*
   1068  * Discard the IP multicast options.
   1069  */
   1070 void
   1071 ip_freemoptions(imo)
   1072 	register struct ip_moptions *imo;
   1073 {
   1074 	register int i;
   1075 
   1076 	if (imo != NULL) {
   1077 		for (i = 0; i < imo->imo_num_memberships; ++i)
   1078 			in_delmulti(imo->imo_membership[i]);
   1079 		free(imo, M_IPMOPTS);
   1080 	}
   1081 }
   1082 
   1083 /*
   1084  * Routine called from ip_output() to loop back a copy of an IP multicast
   1085  * packet to the input queue of a specified interface.  Note that this
   1086  * calls the output routine of the loopback "driver", but with an interface
   1087  * pointer that might NOT be &loif -- easier than replicating that code here.
   1088  */
   1089 static void
   1090 ip_mloopback(ifp, m, dst)
   1091 	struct ifnet *ifp;
   1092 	register struct mbuf *m;
   1093 	register struct sockaddr_in *dst;
   1094 {
   1095 	register struct ip *ip;
   1096 	struct mbuf *copym;
   1097 
   1098 	copym = m_copy(m, 0, M_COPYALL);
   1099 	if (copym != NULL) {
   1100 		/*
   1101 		 * We don't bother to fragment if the IP length is greater
   1102 		 * than the interface's MTU.  Can this possibly matter?
   1103 		 */
   1104 		ip = mtod(copym, struct ip *);
   1105 		ip->ip_len = htons((u_int16_t)ip->ip_len);
   1106 		ip->ip_off = htons((u_int16_t)ip->ip_off);
   1107 		ip->ip_sum = 0;
   1108 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
   1109 		(void) looutput(ifp, copym, sintosa(dst), NULL);
   1110 	}
   1111 }
   1112