Home | History | Annotate | Line # | Download | only in netinet
ip_output.c revision 1.40.2.1
      1 /*	$NetBSD: ip_output.c,v 1.40.2.1 1998/05/09 03:33:00 mycroft 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.le_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 			ip = mtod(m = m1, struct ip *);
    321 		}
    322 #endif /* PFIL_HOOKS */
    323 sendit:
    324 	/*
    325 	 * If small enough for interface, can just send directly.
    326 	 */
    327 	if ((u_int16_t)ip->ip_len <= ifp->if_mtu) {
    328 		ip->ip_len = htons((u_int16_t)ip->ip_len);
    329 		ip->ip_off = htons((u_int16_t)ip->ip_off);
    330 		ip->ip_sum = 0;
    331 		ip->ip_sum = in_cksum(m, hlen);
    332 		error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
    333 		goto done;
    334 	}
    335 	/*
    336 	 * Too large for interface; fragment if possible.
    337 	 * Must be able to put at least 8 bytes per fragment.
    338 	 */
    339 	if (ip->ip_off & IP_DF) {
    340 		if (flags & IP_RETURNMTU)
    341 			*mtu_p = ifp->if_mtu;
    342 		error = EMSGSIZE;
    343 		ipstat.ips_cantfrag++;
    344 		goto bad;
    345 	}
    346 	len = (ifp->if_mtu - hlen) &~ 7;
    347 	if (len < 8) {
    348 		error = EMSGSIZE;
    349 		goto bad;
    350 	}
    351 
    352     {
    353 	int mhlen, firstlen = len;
    354 	struct mbuf **mnext = &m->m_nextpkt;
    355 
    356 	/*
    357 	 * Loop through length of segment after first fragment,
    358 	 * make new header and copy data of each part and link onto chain.
    359 	 */
    360 	m0 = m;
    361 	mhlen = sizeof (struct ip);
    362 	for (off = hlen + len; off < (u_int16_t)ip->ip_len; off += len) {
    363 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    364 		if (m == 0) {
    365 			error = ENOBUFS;
    366 			ipstat.ips_odropped++;
    367 			goto sendorfree;
    368 		}
    369 		*mnext = m;
    370 		mnext = &m->m_nextpkt;
    371 		m->m_data += max_linkhdr;
    372 		mhip = mtod(m, struct ip *);
    373 		*mhip = *ip;
    374 		if (hlen > sizeof (struct ip)) {
    375 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
    376 			mhip->ip_hl = mhlen >> 2;
    377 		}
    378 		m->m_len = mhlen;
    379 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
    380 		if (ip->ip_off & IP_MF)
    381 			mhip->ip_off |= IP_MF;
    382 		if (off + len >= (u_int16_t)ip->ip_len)
    383 			len = (u_int16_t)ip->ip_len - off;
    384 		else
    385 			mhip->ip_off |= IP_MF;
    386 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
    387 		m->m_next = m_copy(m0, off, len);
    388 		if (m->m_next == 0) {
    389 			error = ENOBUFS;	/* ??? */
    390 			ipstat.ips_odropped++;
    391 			goto sendorfree;
    392 		}
    393 		m->m_pkthdr.len = mhlen + len;
    394 		m->m_pkthdr.rcvif = (struct ifnet *)0;
    395 		mhip->ip_off = htons((u_int16_t)mhip->ip_off);
    396 		mhip->ip_sum = 0;
    397 		mhip->ip_sum = in_cksum(m, mhlen);
    398 		ipstat.ips_ofragments++;
    399 	}
    400 	/*
    401 	 * Update first fragment by trimming what's been copied out
    402 	 * and updating header, then send each fragment (in order).
    403 	 */
    404 	m = m0;
    405 	m_adj(m, hlen + firstlen - (u_int16_t)ip->ip_len);
    406 	m->m_pkthdr.len = hlen + firstlen;
    407 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
    408 	ip->ip_off = htons((u_int16_t)(ip->ip_off | IP_MF));
    409 	ip->ip_sum = 0;
    410 	ip->ip_sum = in_cksum(m, hlen);
    411 sendorfree:
    412 	for (m = m0; m; m = m0) {
    413 		m0 = m->m_nextpkt;
    414 		m->m_nextpkt = 0;
    415 		if (error == 0)
    416 			error = (*ifp->if_output)(ifp, m, sintosa(dst),
    417 			    ro->ro_rt);
    418 		else
    419 			m_freem(m);
    420 	}
    421 
    422 	if (error == 0)
    423 		ipstat.ips_fragmented++;
    424     }
    425 done:
    426 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
    427 		RTFREE(ro->ro_rt);
    428 		ro->ro_rt = 0;
    429 	}
    430 	return (error);
    431 bad:
    432 	m_freem(m);
    433 	goto done;
    434 }
    435 
    436 /*
    437  * Determine the maximum length of the options to be inserted;
    438  * we would far rather allocate too much space rather than too little.
    439  */
    440 u_int
    441 ip_optlen(inp)
    442 	struct inpcb *inp;
    443 {
    444 	struct mbuf *m = inp->inp_options;
    445 
    446 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
    447 		return(m->m_len - offsetof(struct ipoption, ipopt_dst));
    448 	else
    449 		return 0;
    450 }
    451 
    452 
    453 /*
    454  * Insert IP options into preformed packet.
    455  * Adjust IP destination as required for IP source routing,
    456  * as indicated by a non-zero in_addr at the start of the options.
    457  */
    458 static struct mbuf *
    459 ip_insertoptions(m, opt, phlen)
    460 	register struct mbuf *m;
    461 	struct mbuf *opt;
    462 	int *phlen;
    463 {
    464 	register struct ipoption *p = mtod(opt, struct ipoption *);
    465 	struct mbuf *n;
    466 	register struct ip *ip = mtod(m, struct ip *);
    467 	unsigned optlen;
    468 
    469 	optlen = opt->m_len - sizeof(p->ipopt_dst);
    470 	if (optlen + (u_int16_t)ip->ip_len > IP_MAXPACKET)
    471 		return (m);		/* XXX should fail */
    472 	if (!in_nullhost(p->ipopt_dst))
    473 		ip->ip_dst = p->ipopt_dst;
    474 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
    475 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
    476 		if (n == 0)
    477 			return (m);
    478 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
    479 		m->m_len -= sizeof(struct ip);
    480 		m->m_data += sizeof(struct ip);
    481 		n->m_next = m;
    482 		m = n;
    483 		m->m_len = optlen + sizeof(struct ip);
    484 		m->m_data += max_linkhdr;
    485 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
    486 	} else {
    487 		m->m_data -= optlen;
    488 		m->m_len += optlen;
    489 		m->m_pkthdr.len += optlen;
    490 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
    491 	}
    492 	ip = mtod(m, struct ip *);
    493 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
    494 	*phlen = sizeof(struct ip) + optlen;
    495 	ip->ip_len += optlen;
    496 	return (m);
    497 }
    498 
    499 /*
    500  * Copy options from ip to jp,
    501  * omitting those not copied during fragmentation.
    502  */
    503 int
    504 ip_optcopy(ip, jp)
    505 	struct ip *ip, *jp;
    506 {
    507 	register u_char *cp, *dp;
    508 	int opt, optlen, cnt;
    509 
    510 	cp = (u_char *)(ip + 1);
    511 	dp = (u_char *)(jp + 1);
    512 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
    513 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    514 		opt = cp[0];
    515 		if (opt == IPOPT_EOL)
    516 			break;
    517 		if (opt == IPOPT_NOP) {
    518 			/* Preserve for IP mcast tunnel's LSRR alignment. */
    519 			*dp++ = IPOPT_NOP;
    520 			optlen = 1;
    521 			continue;
    522 		} else
    523 			optlen = cp[IPOPT_OLEN];
    524 		/* bogus lengths should have been caught by ip_dooptions */
    525 		if (optlen > cnt)
    526 			optlen = cnt;
    527 		if (IPOPT_COPIED(opt)) {
    528 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
    529 			dp += optlen;
    530 		}
    531 	}
    532 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
    533 		*dp++ = IPOPT_EOL;
    534 	return (optlen);
    535 }
    536 
    537 /*
    538  * IP socket option processing.
    539  */
    540 int
    541 ip_ctloutput(op, so, level, optname, mp)
    542 	int op;
    543 	struct socket *so;
    544 	int level, optname;
    545 	struct mbuf **mp;
    546 {
    547 	register struct inpcb *inp = sotoinpcb(so);
    548 	register struct mbuf *m = *mp;
    549 	register int optval = 0;
    550 	int error = 0;
    551 
    552 	if (level != IPPROTO_IP) {
    553 		error = EINVAL;
    554 		if (op == PRCO_SETOPT && *mp)
    555 			(void) m_free(*mp);
    556 	} else switch (op) {
    557 
    558 	case PRCO_SETOPT:
    559 		switch (optname) {
    560 		case IP_OPTIONS:
    561 #ifdef notyet
    562 		case IP_RETOPTS:
    563 			return (ip_pcbopts(optname, &inp->inp_options, m));
    564 #else
    565 			return (ip_pcbopts(&inp->inp_options, m));
    566 #endif
    567 
    568 		case IP_TOS:
    569 		case IP_TTL:
    570 		case IP_RECVOPTS:
    571 		case IP_RECVRETOPTS:
    572 		case IP_RECVDSTADDR:
    573 		case IP_RECVIF:
    574 			if (m == NULL || m->m_len != sizeof(int))
    575 				error = EINVAL;
    576 			else {
    577 				optval = *mtod(m, int *);
    578 				switch (optname) {
    579 
    580 				case IP_TOS:
    581 					inp->inp_ip.ip_tos = optval;
    582 					break;
    583 
    584 				case IP_TTL:
    585 					inp->inp_ip.ip_ttl = optval;
    586 					break;
    587 #define	OPTSET(bit) \
    588 	if (optval) \
    589 		inp->inp_flags |= bit; \
    590 	else \
    591 		inp->inp_flags &= ~bit;
    592 
    593 				case IP_RECVOPTS:
    594 					OPTSET(INP_RECVOPTS);
    595 					break;
    596 
    597 				case IP_RECVRETOPTS:
    598 					OPTSET(INP_RECVRETOPTS);
    599 					break;
    600 
    601 				case IP_RECVDSTADDR:
    602 					OPTSET(INP_RECVDSTADDR);
    603 					break;
    604 
    605 				case IP_RECVIF:
    606 					OPTSET(INP_RECVIF);
    607 					break;
    608 				}
    609 			}
    610 			break;
    611 #undef OPTSET
    612 
    613 		case IP_MULTICAST_IF:
    614 		case IP_MULTICAST_TTL:
    615 		case IP_MULTICAST_LOOP:
    616 		case IP_ADD_MEMBERSHIP:
    617 		case IP_DROP_MEMBERSHIP:
    618 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
    619 			break;
    620 
    621 		default:
    622 			error = ENOPROTOOPT;
    623 			break;
    624 		}
    625 		if (m)
    626 			(void)m_free(m);
    627 		break;
    628 
    629 	case PRCO_GETOPT:
    630 		switch (optname) {
    631 		case IP_OPTIONS:
    632 		case IP_RETOPTS:
    633 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    634 			if (inp->inp_options) {
    635 				m->m_len = inp->inp_options->m_len;
    636 				bcopy(mtod(inp->inp_options, caddr_t),
    637 				    mtod(m, caddr_t), (unsigned)m->m_len);
    638 			} else
    639 				m->m_len = 0;
    640 			break;
    641 
    642 		case IP_TOS:
    643 		case IP_TTL:
    644 		case IP_RECVOPTS:
    645 		case IP_RECVRETOPTS:
    646 		case IP_RECVDSTADDR:
    647 		case IP_RECVIF:
    648 		case IP_ERRORMTU:
    649 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
    650 			m->m_len = sizeof(int);
    651 			switch (optname) {
    652 
    653 			case IP_TOS:
    654 				optval = inp->inp_ip.ip_tos;
    655 				break;
    656 
    657 			case IP_TTL:
    658 				optval = inp->inp_ip.ip_ttl;
    659 				break;
    660 
    661 			case IP_ERRORMTU:
    662 				optval = inp->inp_errormtu;
    663 				break;
    664 
    665 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
    666 
    667 			case IP_RECVOPTS:
    668 				optval = OPTBIT(INP_RECVOPTS);
    669 				break;
    670 
    671 			case IP_RECVRETOPTS:
    672 				optval = OPTBIT(INP_RECVRETOPTS);
    673 				break;
    674 
    675 			case IP_RECVDSTADDR:
    676 				optval = OPTBIT(INP_RECVDSTADDR);
    677 				break;
    678 
    679 			case IP_RECVIF:
    680 				optval = OPTBIT(INP_RECVIF);
    681 				break;
    682 			}
    683 			*mtod(m, int *) = optval;
    684 			break;
    685 
    686 		case IP_MULTICAST_IF:
    687 		case IP_MULTICAST_TTL:
    688 		case IP_MULTICAST_LOOP:
    689 		case IP_ADD_MEMBERSHIP:
    690 		case IP_DROP_MEMBERSHIP:
    691 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
    692 			break;
    693 
    694 		default:
    695 			error = ENOPROTOOPT;
    696 			break;
    697 		}
    698 		break;
    699 	}
    700 	return (error);
    701 }
    702 
    703 /*
    704  * Set up IP options in pcb for insertion in output packets.
    705  * Store in mbuf with pointer in pcbopt, adding pseudo-option
    706  * with destination address if source routed.
    707  */
    708 int
    709 #ifdef notyet
    710 ip_pcbopts(optname, pcbopt, m)
    711 	int optname;
    712 #else
    713 ip_pcbopts(pcbopt, m)
    714 #endif
    715 	struct mbuf **pcbopt;
    716 	register struct mbuf *m;
    717 {
    718 	register cnt, optlen;
    719 	register u_char *cp;
    720 	u_char opt;
    721 
    722 	/* turn off any old options */
    723 	if (*pcbopt)
    724 		(void)m_free(*pcbopt);
    725 	*pcbopt = 0;
    726 	if (m == (struct mbuf *)0 || m->m_len == 0) {
    727 		/*
    728 		 * Only turning off any previous options.
    729 		 */
    730 		if (m)
    731 			(void)m_free(m);
    732 		return (0);
    733 	}
    734 
    735 #ifndef	vax
    736 	if (m->m_len % sizeof(int32_t))
    737 		goto bad;
    738 #endif
    739 	/*
    740 	 * IP first-hop destination address will be stored before
    741 	 * actual options; move other options back
    742 	 * and clear it when none present.
    743 	 */
    744 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
    745 		goto bad;
    746 	cnt = m->m_len;
    747 	m->m_len += sizeof(struct in_addr);
    748 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
    749 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
    750 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
    751 
    752 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    753 		opt = cp[IPOPT_OPTVAL];
    754 		if (opt == IPOPT_EOL)
    755 			break;
    756 		if (opt == IPOPT_NOP)
    757 			optlen = 1;
    758 		else {
    759 			optlen = cp[IPOPT_OLEN];
    760 			if (optlen <= IPOPT_OLEN || optlen > cnt)
    761 				goto bad;
    762 		}
    763 		switch (opt) {
    764 
    765 		default:
    766 			break;
    767 
    768 		case IPOPT_LSRR:
    769 		case IPOPT_SSRR:
    770 			/*
    771 			 * user process specifies route as:
    772 			 *	->A->B->C->D
    773 			 * D must be our final destination (but we can't
    774 			 * check that since we may not have connected yet).
    775 			 * A is first hop destination, which doesn't appear in
    776 			 * actual IP option, but is stored before the options.
    777 			 */
    778 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
    779 				goto bad;
    780 			m->m_len -= sizeof(struct in_addr);
    781 			cnt -= sizeof(struct in_addr);
    782 			optlen -= sizeof(struct in_addr);
    783 			cp[IPOPT_OLEN] = optlen;
    784 			/*
    785 			 * Move first hop before start of options.
    786 			 */
    787 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
    788 			    sizeof(struct in_addr));
    789 			/*
    790 			 * Then copy rest of options back
    791 			 * to close up the deleted entry.
    792 			 */
    793 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
    794 			    sizeof(struct in_addr)),
    795 			    (caddr_t)&cp[IPOPT_OFFSET+1],
    796 			    (unsigned)cnt + sizeof(struct in_addr));
    797 			break;
    798 		}
    799 	}
    800 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
    801 		goto bad;
    802 	*pcbopt = m;
    803 	return (0);
    804 
    805 bad:
    806 	(void)m_free(m);
    807 	return (EINVAL);
    808 }
    809 
    810 /*
    811  * Set the IP multicast options in response to user setsockopt().
    812  */
    813 int
    814 ip_setmoptions(optname, imop, m)
    815 	int optname;
    816 	struct ip_moptions **imop;
    817 	struct mbuf *m;
    818 {
    819 	register int error = 0;
    820 	u_char loop;
    821 	register int i;
    822 	struct in_addr addr;
    823 	register struct ip_mreq *mreq;
    824 	register struct ifnet *ifp;
    825 	register struct ip_moptions *imo = *imop;
    826 	struct route ro;
    827 	register struct sockaddr_in *dst;
    828 
    829 	if (imo == NULL) {
    830 		/*
    831 		 * No multicast option buffer attached to the pcb;
    832 		 * allocate one and initialize to default values.
    833 		 */
    834 		imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
    835 		    M_WAITOK);
    836 
    837 		if (imo == NULL)
    838 			return (ENOBUFS);
    839 		*imop = imo;
    840 		imo->imo_multicast_ifp = NULL;
    841 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
    842 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
    843 		imo->imo_num_memberships = 0;
    844 	}
    845 
    846 	switch (optname) {
    847 
    848 	case IP_MULTICAST_IF:
    849 		/*
    850 		 * Select the interface for outgoing multicast packets.
    851 		 */
    852 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
    853 			error = EINVAL;
    854 			break;
    855 		}
    856 		addr = *(mtod(m, struct in_addr *));
    857 		/*
    858 		 * INADDR_ANY is used to remove a previous selection.
    859 		 * When no interface is selected, a default one is
    860 		 * chosen every time a multicast packet is sent.
    861 		 */
    862 		if (in_nullhost(addr)) {
    863 			imo->imo_multicast_ifp = NULL;
    864 			break;
    865 		}
    866 		/*
    867 		 * The selected interface is identified by its local
    868 		 * IP address.  Find the interface and confirm that
    869 		 * it supports multicasting.
    870 		 */
    871 		INADDR_TO_IFP(addr, ifp);
    872 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
    873 			error = EADDRNOTAVAIL;
    874 			break;
    875 		}
    876 		imo->imo_multicast_ifp = ifp;
    877 		break;
    878 
    879 	case IP_MULTICAST_TTL:
    880 		/*
    881 		 * Set the IP time-to-live for outgoing multicast packets.
    882 		 */
    883 		if (m == NULL || m->m_len != 1) {
    884 			error = EINVAL;
    885 			break;
    886 		}
    887 		imo->imo_multicast_ttl = *(mtod(m, u_char *));
    888 		break;
    889 
    890 	case IP_MULTICAST_LOOP:
    891 		/*
    892 		 * Set the loopback flag for outgoing multicast packets.
    893 		 * Must be zero or one.
    894 		 */
    895 		if (m == NULL || m->m_len != 1 ||
    896 		   (loop = *(mtod(m, u_char *))) > 1) {
    897 			error = EINVAL;
    898 			break;
    899 		}
    900 		imo->imo_multicast_loop = loop;
    901 		break;
    902 
    903 	case IP_ADD_MEMBERSHIP:
    904 		/*
    905 		 * Add a multicast group membership.
    906 		 * Group must be a valid IP multicast address.
    907 		 */
    908 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
    909 			error = EINVAL;
    910 			break;
    911 		}
    912 		mreq = mtod(m, struct ip_mreq *);
    913 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
    914 			error = EINVAL;
    915 			break;
    916 		}
    917 		/*
    918 		 * If no interface address was provided, use the interface of
    919 		 * the route to the given multicast address.
    920 		 */
    921 		if (in_nullhost(mreq->imr_interface)) {
    922 			ro.ro_rt = NULL;
    923 			dst = satosin(&ro.ro_dst);
    924 			dst->sin_len = sizeof(*dst);
    925 			dst->sin_family = AF_INET;
    926 			dst->sin_addr = mreq->imr_multiaddr;
    927 			rtalloc(&ro);
    928 			if (ro.ro_rt == NULL) {
    929 				error = EADDRNOTAVAIL;
    930 				break;
    931 			}
    932 			ifp = ro.ro_rt->rt_ifp;
    933 			rtfree(ro.ro_rt);
    934 		} else {
    935 			INADDR_TO_IFP(mreq->imr_interface, ifp);
    936 		}
    937 		/*
    938 		 * See if we found an interface, and confirm that it
    939 		 * supports multicast.
    940 		 */
    941 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
    942 			error = EADDRNOTAVAIL;
    943 			break;
    944 		}
    945 		/*
    946 		 * See if the membership already exists or if all the
    947 		 * membership slots are full.
    948 		 */
    949 		for (i = 0; i < imo->imo_num_memberships; ++i) {
    950 			if (imo->imo_membership[i]->inm_ifp == ifp &&
    951 			    in_hosteq(imo->imo_membership[i]->inm_addr,
    952 				      mreq->imr_multiaddr))
    953 				break;
    954 		}
    955 		if (i < imo->imo_num_memberships) {
    956 			error = EADDRINUSE;
    957 			break;
    958 		}
    959 		if (i == IP_MAX_MEMBERSHIPS) {
    960 			error = ETOOMANYREFS;
    961 			break;
    962 		}
    963 		/*
    964 		 * Everything looks good; add a new record to the multicast
    965 		 * address list for the given interface.
    966 		 */
    967 		if ((imo->imo_membership[i] =
    968 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
    969 			error = ENOBUFS;
    970 			break;
    971 		}
    972 		++imo->imo_num_memberships;
    973 		break;
    974 
    975 	case IP_DROP_MEMBERSHIP:
    976 		/*
    977 		 * Drop a multicast group membership.
    978 		 * Group must be a valid IP multicast address.
    979 		 */
    980 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
    981 			error = EINVAL;
    982 			break;
    983 		}
    984 		mreq = mtod(m, struct ip_mreq *);
    985 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
    986 			error = EINVAL;
    987 			break;
    988 		}
    989 		/*
    990 		 * If an interface address was specified, get a pointer
    991 		 * to its ifnet structure.
    992 		 */
    993 		if (in_nullhost(mreq->imr_interface))
    994 			ifp = NULL;
    995 		else {
    996 			INADDR_TO_IFP(mreq->imr_interface, ifp);
    997 			if (ifp == NULL) {
    998 				error = EADDRNOTAVAIL;
    999 				break;
   1000 			}
   1001 		}
   1002 		/*
   1003 		 * Find the membership in the membership array.
   1004 		 */
   1005 		for (i = 0; i < imo->imo_num_memberships; ++i) {
   1006 			if ((ifp == NULL ||
   1007 			     imo->imo_membership[i]->inm_ifp == ifp) &&
   1008 			     in_hosteq(imo->imo_membership[i]->inm_addr,
   1009 				       mreq->imr_multiaddr))
   1010 				break;
   1011 		}
   1012 		if (i == imo->imo_num_memberships) {
   1013 			error = EADDRNOTAVAIL;
   1014 			break;
   1015 		}
   1016 		/*
   1017 		 * Give up the multicast address record to which the
   1018 		 * membership points.
   1019 		 */
   1020 		in_delmulti(imo->imo_membership[i]);
   1021 		/*
   1022 		 * Remove the gap in the membership array.
   1023 		 */
   1024 		for (++i; i < imo->imo_num_memberships; ++i)
   1025 			imo->imo_membership[i-1] = imo->imo_membership[i];
   1026 		--imo->imo_num_memberships;
   1027 		break;
   1028 
   1029 	default:
   1030 		error = EOPNOTSUPP;
   1031 		break;
   1032 	}
   1033 
   1034 	/*
   1035 	 * If all options have default values, no need to keep the mbuf.
   1036 	 */
   1037 	if (imo->imo_multicast_ifp == NULL &&
   1038 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
   1039 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
   1040 	    imo->imo_num_memberships == 0) {
   1041 		free(*imop, M_IPMOPTS);
   1042 		*imop = NULL;
   1043 	}
   1044 
   1045 	return (error);
   1046 }
   1047 
   1048 /*
   1049  * Return the IP multicast options in response to user getsockopt().
   1050  */
   1051 int
   1052 ip_getmoptions(optname, imo, mp)
   1053 	int optname;
   1054 	register struct ip_moptions *imo;
   1055 	register struct mbuf **mp;
   1056 {
   1057 	u_char *ttl;
   1058 	u_char *loop;
   1059 	struct in_addr *addr;
   1060 	struct in_ifaddr *ia;
   1061 
   1062 	*mp = m_get(M_WAIT, MT_SOOPTS);
   1063 
   1064 	switch (optname) {
   1065 
   1066 	case IP_MULTICAST_IF:
   1067 		addr = mtod(*mp, struct in_addr *);
   1068 		(*mp)->m_len = sizeof(struct in_addr);
   1069 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
   1070 			*addr = zeroin_addr;
   1071 		else {
   1072 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
   1073 			*addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
   1074 		}
   1075 		return (0);
   1076 
   1077 	case IP_MULTICAST_TTL:
   1078 		ttl = mtod(*mp, u_char *);
   1079 		(*mp)->m_len = 1;
   1080 		*ttl = imo ? imo->imo_multicast_ttl
   1081 			   : IP_DEFAULT_MULTICAST_TTL;
   1082 		return (0);
   1083 
   1084 	case IP_MULTICAST_LOOP:
   1085 		loop = mtod(*mp, u_char *);
   1086 		(*mp)->m_len = 1;
   1087 		*loop = imo ? imo->imo_multicast_loop
   1088 			    : IP_DEFAULT_MULTICAST_LOOP;
   1089 		return (0);
   1090 
   1091 	default:
   1092 		return (EOPNOTSUPP);
   1093 	}
   1094 }
   1095 
   1096 /*
   1097  * Discard the IP multicast options.
   1098  */
   1099 void
   1100 ip_freemoptions(imo)
   1101 	register struct ip_moptions *imo;
   1102 {
   1103 	register int i;
   1104 
   1105 	if (imo != NULL) {
   1106 		for (i = 0; i < imo->imo_num_memberships; ++i)
   1107 			in_delmulti(imo->imo_membership[i]);
   1108 		free(imo, M_IPMOPTS);
   1109 	}
   1110 }
   1111 
   1112 /*
   1113  * Routine called from ip_output() to loop back a copy of an IP multicast
   1114  * packet to the input queue of a specified interface.  Note that this
   1115  * calls the output routine of the loopback "driver", but with an interface
   1116  * pointer that might NOT be &loif -- easier than replicating that code here.
   1117  */
   1118 static void
   1119 ip_mloopback(ifp, m, dst)
   1120 	struct ifnet *ifp;
   1121 	register struct mbuf *m;
   1122 	register struct sockaddr_in *dst;
   1123 {
   1124 	register struct ip *ip;
   1125 	struct mbuf *copym;
   1126 
   1127 	copym = m_copy(m, 0, M_COPYALL);
   1128 	if (copym != NULL) {
   1129 		/*
   1130 		 * We don't bother to fragment if the IP length is greater
   1131 		 * than the interface's MTU.  Can this possibly matter?
   1132 		 */
   1133 		ip = mtod(copym, struct ip *);
   1134 		ip->ip_len = htons((u_int16_t)ip->ip_len);
   1135 		ip->ip_off = htons((u_int16_t)ip->ip_off);
   1136 		ip->ip_sum = 0;
   1137 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
   1138 		(void) looutput(ifp, copym, sintosa(dst), NULL);
   1139 	}
   1140 }
   1141