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