Home | History | Annotate | Line # | Download | only in netinet6
ip6_output.c revision 1.5
      1 /*	$NetBSD: ip6_output.c,v 1.5 1999/07/22 03:59:42 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * 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. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1988, 1990, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
     65  */
     66 
     67 #ifdef __FreeBSD__
     68 #include "opt_ip6fw.h"
     69 #endif
     70 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
     71 #include "opt_inet.h"
     72 #include "opt_ipsec.h"
     73 #endif
     74 
     75 #include <sys/param.h>
     76 #include <sys/malloc.h>
     77 #include <sys/mbuf.h>
     78 #include <sys/errno.h>
     79 #include <sys/protosw.h>
     80 #include <sys/socket.h>
     81 #include <sys/socketvar.h>
     82 #include <sys/systm.h>
     83 #include <sys/proc.h>
     84 
     85 #include <net/if.h>
     86 #include <net/route.h>
     87 
     88 #include <netinet/in.h>
     89 #include <netinet/in_var.h>
     90 #include <netinet6/in6_systm.h>
     91 #include <netinet6/ip6.h>
     92 #include <netinet6/icmp6.h>
     93 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     94 #include <netinet6/in6_pcb.h>
     95 #else
     96 #include <netinet/in_pcb.h>
     97 #endif
     98 #include <netinet6/ip6_var.h>
     99 #include <netinet6/nd6.h>
    100 
    101 #ifdef IPSEC
    102 #include <netinet6/ipsec.h>
    103 #include <netkey/key.h>
    104 #include <netkey/key_debug.h>
    105 #endif /* IPSEC */
    106 
    107 #include "loop.h"
    108 
    109 struct ip6_exthdrs {
    110 	struct mbuf *ip6e_ip6;
    111 	struct mbuf *ip6e_hbh;
    112 	struct mbuf *ip6e_dest1;
    113 	struct mbuf *ip6e_rthdr;
    114 	struct mbuf *ip6e_dest2;
    115 };
    116 
    117 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
    118 			    struct socket *));
    119 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
    120 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
    121 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
    122 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
    123 				  struct ip6_frag **));
    124 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
    125 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
    126 #ifdef __bsdi__
    127 extern struct ifnet loif;
    128 #endif
    129 
    130 #ifdef __NetBSD__
    131 extern struct ifnet **ifindex2ifnet;
    132 extern struct ifnet loif[NLOOP];
    133 #endif
    134 
    135 /*
    136  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
    137  * header (with pri, len, nxt, hlim, src, dst).
    138  * This function may modify ver and hlim only.
    139  * The mbuf chain containing the packet will be freed.
    140  * The mbuf opt, if present, will not be freed.
    141  */
    142 int
    143 ip6_output(m0, opt, ro, flags, im6o)
    144 	struct mbuf *m0;
    145 	struct ip6_pktopts *opt;
    146 	struct route_in6 *ro;
    147 	int flags;
    148 	struct ip6_moptions *im6o;
    149 {
    150 	struct ip6_hdr *ip6, *mhip6;
    151 	struct ifnet *ifp;
    152 	struct mbuf *m = m0;
    153 	int hlen, tlen, len, off;
    154 	struct route_in6 ip6route;
    155 	struct sockaddr_in6 *dst;
    156 	int error = 0;
    157 	struct in6_ifaddr *ia;
    158 	u_long mtu;
    159 	u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
    160 	struct ip6_exthdrs exthdrs;
    161 	struct in6_addr finaldst;
    162 	struct route_in6 *ro_pmtu = NULL;
    163 	int hdrsplit = 0;
    164 	int needipsec = 0;
    165 #ifdef IPSEC
    166 	int needipsectun = 0;
    167 	struct socket *so;
    168 	struct secpolicy *sp = NULL;
    169 
    170 	/* for AH processing. stupid to have "socket" variable in IP layer... */
    171 	so = (struct socket *)m->m_pkthdr.rcvif;
    172 	m->m_pkthdr.rcvif = NULL;
    173 	ip6 = mtod(m, struct ip6_hdr *);
    174 #endif /* IPSEC */
    175 
    176 #define MAKE_EXTHDR(hp,mp)						\
    177     {									\
    178 	if (hp) {							\
    179 		struct ip6_ext *eh = (struct ip6_ext *)(hp);		\
    180 		error = ip6_copyexthdr((mp), (caddr_t)(hp), 		\
    181 				       ((eh)->ip6e_len + 1) << 3);	\
    182 		if (error)						\
    183 			goto freehdrs;					\
    184 	}								\
    185     }
    186 
    187 	bzero(&exthdrs, sizeof(exthdrs));
    188 	if (opt) {
    189 		/* Hop-by-Hop options header */
    190 		MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
    191 		/* Destination options header(1st part) */
    192 		MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
    193 		/* Routing header */
    194 		MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
    195 		/* Destination options header(2nd part) */
    196 		MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
    197 	}
    198 
    199 #ifdef IPSEC
    200 	/* get a security policy for this packet */
    201 	if (so == NULL)
    202 		sp = ipsec6_getpolicybyaddr(m, 0, &error);
    203 	else
    204 		sp = ipsec6_getpolicybysock(m, so, &error);
    205 
    206 	if (sp == NULL) {
    207 		ipsec6stat.out_inval++;
    208 		goto bad;
    209 	}
    210 
    211 	error = 0;
    212 
    213 	/* check policy */
    214 	switch (sp->policy) {
    215 	case IPSEC_POLICY_DISCARD:
    216 		/*
    217 		 * This packet is just discarded.
    218 		 */
    219 		ipsec6stat.out_polvio++;
    220 		goto bad;
    221 
    222 	case IPSEC_POLICY_BYPASS:
    223 	case IPSEC_POLICY_NONE:
    224 		/* no need to do IPsec. */
    225 		needipsec = 0;
    226 		break;
    227 
    228 	case IPSEC_POLICY_IPSEC:
    229 		if (sp->req == NULL) {
    230 			/* XXX should be panic ? */
    231 			printf("ip6_output: No IPsec request specified.\n");
    232 			error = EINVAL;
    233 			goto bad;
    234 		}
    235 		needipsec = 1;
    236 		break;
    237 
    238 	case IPSEC_POLICY_ENTRUST:
    239 	default:
    240 		printf("ip6_output: Invalid policy found. %d\n", sp->policy);
    241 	}
    242 #endif /* IPSEC */
    243 
    244 	/*
    245 	 * Calculate the total length of the extension header chain.
    246 	 * Keep the length of the unfragmentable part for fragmentation.
    247 	 */
    248 	if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
    249 	if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
    250 	if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
    251 	unfragpartlen = plen + sizeof(struct ip6_hdr);
    252 	/* NOTE: we don't add AH/ESP length here. do that later. */
    253 	if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
    254 
    255 	/*
    256 	 * If we need IPsec, or there is at least one extension header,
    257 	 * separate IP6 header from the payload.
    258 	 */
    259 	if ((needipsec || optlen) && !hdrsplit) {
    260 		if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
    261 			m = NULL;
    262 			goto freehdrs;
    263 		}
    264 		m = exthdrs.ip6e_ip6;
    265 		hdrsplit++;
    266 	}
    267 
    268 	/* adjust pointer */
    269 	ip6 = mtod(m, struct ip6_hdr *);
    270 
    271 	/* adjust mbuf packet header length */
    272 	m->m_pkthdr.len += optlen;
    273 	plen = m->m_pkthdr.len - sizeof(*ip6);
    274 
    275 	/* If this is a jumbo payload, insert a jumbo payload option. */
    276 	if (plen > IPV6_MAXPACKET) {
    277 		if (!hdrsplit) {
    278 			if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
    279 				m = NULL;
    280 				goto freehdrs;
    281 			}
    282 			m = exthdrs.ip6e_ip6;
    283 			hdrsplit++;
    284 		}
    285 		/* adjust pointer */
    286 		ip6 = mtod(m, struct ip6_hdr *);
    287 		if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
    288 			goto freehdrs;
    289 		ip6->ip6_plen = 0;
    290 	} else
    291 		ip6->ip6_plen = htons(plen);
    292 
    293 	/*
    294 	 * Concatenate headers and fill in next header fields.
    295 	 * Here we have, on "m"
    296 	 *	IPv6 payload  or
    297 	 *	IPv6 [esp* dest2 payload]
    298 	 * and we insert headers accordingly.  Finally, we should be getting:
    299 	 *	IPv6 hbh dest1 rthdr ah* dest2 payload  or
    300 	 *	IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
    301 	 */
    302 	{
    303 		u_char *nexthdrp = &ip6->ip6_nxt;
    304 		struct mbuf *mprev = m;
    305 
    306 		/*
    307 		 * we treat dest2 specially.  this makes IPsec processing
    308 		 * much easier.
    309 		 */
    310 		if (exthdrs.ip6e_dest2) {
    311 			if (!hdrsplit)
    312 				panic("assumption failed: hdr not split");
    313 			*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
    314 			ip6->ip6_nxt = IPPROTO_DSTOPTS;
    315 		}
    316 
    317 #define MAKE_CHAIN(m,mp,p,i)\
    318     {\
    319 	if (m) {\
    320 		if (!hdrsplit) \
    321 			panic("assumption failed: hdr not split"); \
    322 		*mtod((m), u_char *) = *(p);\
    323 		*(p) = (i);\
    324 		p = mtod((m), u_char *);\
    325 		(m)->m_next = (mp)->m_next;\
    326 		(mp)->m_next = (m);\
    327 		(mp) = (m);\
    328 	}\
    329     }
    330 		MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
    331 			   nexthdrp, IPPROTO_HOPOPTS);
    332 		MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
    333 			   nexthdrp, IPPROTO_DSTOPTS);
    334 		MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
    335 			   nexthdrp, IPPROTO_ROUTING);
    336 
    337 #ifdef IPSEC
    338 		if (!needipsec)
    339 			goto skip_ipsec2;
    340 
    341 		/*
    342 		 * pointers after IPsec headers are not valid any more.
    343 		 * other pointers need a great care too.
    344 		 * (IPsec routines should not mangle mbufs prior to AH/ESP)
    345 		 */
    346 		exthdrs.ip6e_dest2 = NULL;
    347 
    348 	    {
    349 		struct ip6_rthdr *rh = NULL;
    350 		int segleft_org = 0;
    351 		struct ipsec_output_state state;
    352 
    353 		if (exthdrs.ip6e_rthdr) {
    354 			rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
    355 			segleft_org = rh->ip6r_segleft;
    356 			rh->ip6r_segleft = 0;
    357 		}
    358 
    359 		bzero(&state, sizeof(state));
    360 		state.m = m;
    361 		error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
    362 			&needipsectun);
    363 		m = state.m;
    364 		if (error) {
    365 			/* mbuf is already reclaimed in ipsec6_output_trans. */
    366 			m = NULL;
    367 			switch (error) {
    368 			case EHOSTUNREACH:
    369 			case ENETUNREACH:
    370 			case EMSGSIZE:
    371 			case ENOBUFS:
    372 			case ENOMEM:
    373 				break;
    374 			default:
    375 				printf("ip6_output (ipsec): error code %d\n", error);
    376 				/*fall through*/
    377 			case ENOENT:
    378 				/* don't show these error codes to the user */
    379 				error = 0;
    380 				break;
    381 			}
    382 			goto bad;
    383 		}
    384 		if (exthdrs.ip6e_rthdr) {
    385 			/* ah6_output doesn't modify mbuf chain */
    386 			rh->ip6r_segleft = segleft_org;
    387 		}
    388 	    }
    389 skip_ipsec2:;
    390 #endif
    391 	}
    392 
    393 	/*
    394 	 * If there is a routing header, replace destination address field
    395 	 * with the first hop of the routing header.
    396 	 */
    397 	if (exthdrs.ip6e_rthdr) {
    398 		struct ip6_rthdr *rh =
    399 			(struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
    400 						  struct ip6_rthdr *));
    401 		struct ip6_rthdr0 *rh0;
    402 
    403 		finaldst = ip6->ip6_dst;
    404 		switch(rh->ip6r_type) {
    405 		case IPV6_RTHDR_TYPE_0:
    406 			 rh0 = (struct ip6_rthdr0 *)rh;
    407 			 ip6->ip6_dst = rh0->ip6r0_addr[0];
    408 			 bcopy((caddr_t)&rh0->ip6r0_addr[1],
    409 				 (caddr_t)&rh0->ip6r0_addr[0],
    410 				 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
    411 				 );
    412 			 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
    413 			 break;
    414 		default:	/* is it possible? */
    415 			 error = EINVAL;
    416 			 goto bad;
    417 		}
    418 	}
    419 
    420 	/* Source address validation */
    421 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
    422 	    (flags & IPV6_DADOUTPUT) == 0) {
    423 		error = EOPNOTSUPP;
    424 		ip6stat.ip6s_badscope++;
    425 		goto bad;
    426 	}
    427 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
    428 		error = EOPNOTSUPP;
    429 		ip6stat.ip6s_badscope++;
    430 		goto bad;
    431 	}
    432 
    433 	ip6stat.ip6s_localout++;
    434 
    435 	/*
    436 	 * Route packet.
    437 	 */
    438 	if (ro == 0) {
    439 		ro = &ip6route;
    440 		bzero((caddr_t)ro, sizeof(*ro));
    441 	}
    442 	ro_pmtu = ro;
    443 	if (opt && opt->ip6po_rthdr)
    444 		ro = &opt->ip6po_route;
    445 	dst = (struct sockaddr_in6 *)&ro->ro_dst;
    446 	/*
    447 	 * If there is a cached route,
    448 	 * check that it is to the same destination
    449 	 * and is still up. If not, free it and try again.
    450 	 */
    451 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    452 			 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
    453 		RTFREE(ro->ro_rt);
    454 		ro->ro_rt = (struct rtentry *)0;
    455 	}
    456 	if (ro->ro_rt == 0) {
    457 		bzero(dst, sizeof(*dst));
    458 		dst->sin6_family = AF_INET6;
    459 		dst->sin6_len = sizeof(struct sockaddr_in6);
    460 		dst->sin6_addr = ip6->ip6_dst;
    461 	}
    462 #ifdef IPSEC
    463 	if (needipsec && needipsectun) {
    464 		struct ipsec_output_state state;
    465 
    466 		/*
    467 		 * All the extension headers will become inaccessible
    468 		 * (since they can be encrypted).
    469 		 * Don't panic, we need no more updates to extension headers
    470 		 * on inner IPv6 packet (since they are now encapsulated).
    471 		 *
    472 		 * IPv6 [ESP|AH] IPv6 [extension headers] payload
    473 		 */
    474 		bzero(&exthdrs, sizeof(exthdrs));
    475 		exthdrs.ip6e_ip6 = m;
    476 
    477 		bzero(&state, sizeof(state));
    478 		state.m = m;
    479 		state.ro = (struct route *)ro;
    480 		state.dst = (struct sockaddr *)dst;
    481 
    482 		error = ipsec6_output_tunnel(&state, sp, flags);
    483 
    484 		m = state.m;
    485 		ro = (struct route_in6 *)state.ro;
    486 		dst = (struct sockaddr_in6 *)state.dst;
    487 		if (error) {
    488 			/* mbuf is already reclaimed in ipsec6_output_tunnel. */
    489 			m0 = m = NULL;
    490 			m = NULL;
    491 			switch (error) {
    492 			case EHOSTUNREACH:
    493 			case ENETUNREACH:
    494 			case EMSGSIZE:
    495 			case ENOBUFS:
    496 			case ENOMEM:
    497 				break;
    498 			default:
    499 				printf("ip6_output (ipsec): error code %d\n", error);
    500 				/*fall through*/
    501 			case ENOENT:
    502 				/* don't show these error codes to the user */
    503 				error = 0;
    504 				break;
    505 			}
    506 			goto bad;
    507 		}
    508 
    509 		exthdrs.ip6e_ip6 = m;
    510 	}
    511 #endif /*IPESC*/
    512 
    513 	if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    514 		/* Unicast */
    515 
    516 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
    517 #define sin6tosa(sin6)	((struct sockaddr *)(sin6))
    518 		/* xxx
    519 		 * interface selection comes here
    520 		 * if an interface is specified from an upper layer,
    521 		 * ifp must point it.
    522 		 */
    523 		if (ro->ro_rt == 0) {
    524 			if (ro == &ip6route)	/* xxx kazu */
    525 				rtalloc((struct route *)ro);
    526 			else
    527 				rtcalloc((struct route *)ro);
    528 		}
    529 		if (ro->ro_rt == 0) {
    530 			ip6stat.ip6s_noroute++;
    531 			error = EHOSTUNREACH;
    532 			goto bad;
    533 		}
    534 		ia = ifatoia6(ro->ro_rt->rt_ifa);
    535 		ifp = ro->ro_rt->rt_ifp;
    536 		ro->ro_rt->rt_use++;
    537 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
    538 			dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
    539 		m->m_flags &= ~(M_BCAST | M_MCAST);	/* just in case */
    540 
    541 		/*
    542 		 * Check if there is the outgoing interface conflicts with
    543 		 * the interface specified by ifi6_ifindex(if specified).
    544 		 * Note that loopback interface is always okay.
    545 		 * (this happens when we are sending packet toward my
    546 		 * interface)
    547 		 */
    548 		if (opt && opt->ip6po_pktinfo
    549 		 && opt->ip6po_pktinfo->ipi6_ifindex) {
    550 			if (!(ifp->if_flags & IFF_LOOPBACK)
    551 			 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
    552 				ip6stat.ip6s_noroute++;
    553 				error = EHOSTUNREACH;
    554 				goto bad;
    555 			}
    556 		}
    557 
    558 		if (opt && opt->ip6po_hlim != -1)
    559 			ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
    560 	} else {
    561 		/* Multicast */
    562 		struct	in6_multi *in6m;
    563 
    564 		m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
    565 
    566 		/*
    567 		 * See if the caller provided any multicast options
    568 		 */
    569 		ifp = NULL;
    570 		if (im6o != NULL) {
    571 			ip6->ip6_hlim = im6o->im6o_multicast_hlim;
    572 			if (im6o->im6o_multicast_ifp != NULL)
    573 				ifp = im6o->im6o_multicast_ifp;
    574 		} else
    575 			ip6->ip6_hlim = ip6_defmcasthlim;
    576 
    577 		/*
    578 		 * See if the caller provided the outgoing interface
    579 		 * as an ancillary data.
    580 		 * Boundary check for ifindex is assumed to be already done.
    581 		 */
    582 		if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
    583 			ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
    584 
    585 		/*
    586 		 * If the destination is a node-local scope multicast,
    587 		 * the packet should be loop-backed only.
    588 		 */
    589 		if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
    590 			/*
    591 			 * If the outgoing interface is already specified,
    592 			 * it should be a loopback interface.
    593 			 */
    594 			if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
    595 				ip6stat.ip6s_badscope++;
    596 				error = ENETUNREACH; /* XXX: better error? */
    597 				goto bad;
    598 			}
    599 			else {
    600 #ifdef __bsdi__
    601 				ifp = &loif;
    602 #else
    603 				ifp = &loif[0];
    604 #endif
    605 			}
    606 		}
    607 
    608 		if (opt && opt->ip6po_hlim != -1)
    609 			ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
    610 
    611 		/*
    612 		 * If caller did not provide an interface lookup a
    613 		 * default in the routing table.  This is either a
    614 		 * default for the speicfied group (i.e. a host
    615 		 * route), or a multicast default (a route for the
    616 		 * ``net'' ff00::/8).
    617 		 */
    618 		if (ifp == NULL) {
    619 			if (ro->ro_rt == 0) {
    620 #ifdef __FreeBSD__
    621 				ro->ro_rt = rtalloc1((struct sockaddr *)
    622 						&ro->ro_dst, 0, 0UL);
    623 #endif /*__FreeBSD__*/
    624 #if defined(__bsdi__) || defined(__NetBSD__)
    625 				ro->ro_rt = rtalloc1((struct sockaddr *)
    626 						&ro->ro_dst, 0);
    627 #endif /*__bsdi__*/
    628 			}
    629 			if (ro->ro_rt == 0) {
    630 				ip6stat.ip6s_noroute++;
    631 				error = EHOSTUNREACH;
    632 				goto bad;
    633 			}
    634 			ia = ifatoia6(ro->ro_rt->rt_ifa);
    635 			ifp = ro->ro_rt->rt_ifp;
    636 			ro->ro_rt->rt_use++;
    637 		}
    638 		/*
    639 		 * Confirm that the outgoing interface supports multicast.
    640 		 */
    641 		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
    642 			ip6stat.ip6s_noroute++;
    643 			error = ENETUNREACH;
    644 			goto bad;
    645 		}
    646 		IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
    647 		if (in6m != NULL &&
    648 		   (im6o == NULL || im6o->im6o_multicast_loop)) {
    649 			/*
    650 			 * If we belong to the destination multicast group
    651 			 * on the outgoing interface, and the caller did not
    652 			 * forbid loopback, loop back a copy.
    653 			 */
    654 			ip6_mloopback(ifp, m, dst);
    655 		} else {
    656 			/*
    657 			 * If we are acting as a multicast router, perform
    658 			 * multicast forwarding as if the packet had just
    659 			 * arrived on the interface to which we are about
    660 			 * to send.  The multicast forwarding function
    661 			 * recursively calls this function, using the
    662 			 * IPV6_FORWARDING flag to prevent infinite recursion.
    663 			 *
    664 			 * Multicasts that are looped back by ip6_mloopback(),
    665 			 * above, will be forwarded by the ip6_input() routine,
    666 			 * if necessary.
    667 			 */
    668 			if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
    669 				if (ip6_mforward(ip6, ifp, m) != NULL) {
    670 					m_freem(m);
    671 					goto done;
    672 				}
    673 			}
    674 		}
    675 		/*
    676 		 * Multicasts with a hoplimit of zero may be looped back,
    677 		 * above, but must not be transmitted on a network.
    678 		 * Also, multicasts addressed to the loopback interface
    679 		 * are not sent -- the above call to ip6_mloopback() will
    680 		 * loop back a copy if this host actually belongs to the
    681 		 * destination group on the loopback interface.
    682 		 */
    683 		if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
    684 			m_freem(m);
    685 			goto done;
    686 		}
    687 	}
    688 
    689 	/*
    690 	 * Determine path MTU.
    691 	 */
    692 	if (ro_pmtu != ro) {
    693 		/* The first hop and the final destination may differ. */
    694 		struct sockaddr_in6 *sin6_fin =
    695 			(struct sockaddr_in6 *)&ro_pmtu->ro_dst;
    696 		if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    697 				       !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
    698 							   &finaldst))) {
    699 			RTFREE(ro_pmtu->ro_rt);
    700 			ro_pmtu->ro_rt = (struct rtentry *)0;
    701 		}
    702 		if (ro_pmtu->ro_rt == 0) {
    703 			bzero(sin6_fin, sizeof(*sin6_fin));
    704 			sin6_fin->sin6_family = AF_INET6;
    705 			sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
    706 			sin6_fin->sin6_addr = finaldst;
    707 
    708 #if 0
    709 			rtcalloc((struct route *)ro_pmtu);
    710 #else
    711 			rtalloc((struct route *)ro_pmtu);
    712 #endif
    713 		}
    714 	}
    715 	if (ro_pmtu->ro_rt != NULL) {
    716 		u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu;
    717 
    718 		mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
    719 		if (mtu > ifmtu) {
    720 			/*
    721 			 * The MTU on the route is larger than the MTU on
    722 			 * the interface!  This shouldn't happen, unless the
    723 			 * MTU of the interface has been changed after the
    724 			 * interface was brought up.  Change the MTU in the
    725 			 * route to match the interface MTU (as long as the
    726 			 * field isn't locked).
    727 			 */
    728 			 mtu = ifmtu;
    729 			 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
    730 				 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
    731 		}
    732 	} else {
    733 		mtu = nd_ifinfo[ifp->if_index].linkmtu;
    734 	}
    735 
    736 	/*
    737 	 * Fake link-local scope-class addresses
    738 	 */
    739 	if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
    740 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    741 			ip6->ip6_src.s6_addr16[1] = 0;
    742 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    743 			ip6->ip6_dst.s6_addr16[1] = 0;
    744 	}
    745 
    746 	/*
    747 	 * If the outgoing packet contains a hop-by-hop options header,
    748 	 * it must be examined and processed even by the source node.
    749 	 * (RFC 2460, section 4.)
    750 	 */
    751 	if (exthdrs.ip6e_hbh) {
    752 		struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh,
    753 					   struct ip6_hbh *);
    754 		u_int32_t dummy1; /* XXX unused */
    755 		u_int32_t dummy2; /* XXX unused */
    756 
    757 		/*
    758 		 *  XXX: if we have to send an ICMPv6 error to the sender,
    759 		 *       we need the M_LOOP flag since icmp6_error() expects
    760 		 *       the IPv6 and the hop-by-hop options header are
    761 		 *       continuous unless the flag is set.
    762 		 */
    763 		m->m_flags |= M_LOOP;
    764 		m->m_pkthdr.rcvif = ifp;
    765 		if (ip6_process_hopopts(m,
    766 					(u_int8_t *)(hbh + 1),
    767 					((hbh->ip6h_len + 1) << 3) -
    768 					sizeof(struct ip6_hbh),
    769 					&dummy1, &dummy2) < 0) {
    770 			/* m was already freed at this point */
    771 			error = EINVAL;/* better error? */
    772 			goto done;
    773 		}
    774 		m->m_flags &= ~M_LOOP; /* XXX */
    775 		m->m_pkthdr.rcvif = NULL;
    776 	}
    777 
    778 	/*
    779 	 * Send the packet to the outgoing interface.
    780 	 * If necessary, do IPv6 fragmentation before sending.
    781 	 */
    782 	tlen = m->m_pkthdr.len;
    783 	if (tlen <= mtu
    784 #ifdef notyet
    785 	    /*
    786 	     * On any link that cannot convey a 1280-octet packet in one piece,
    787 	     * link-specific fragmentation and reassembly must be provided at
    788 	     * a layer below IPv6. [RFC 2460, sec.5]
    789 	     * Thus if the interface has ability of link-level fragmentation,
    790 	     * we can just send the packet even if the packet size is
    791 	     * larger than the link's MTU.
    792 	     * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
    793 	     */
    794 
    795 	    || ifp->if_flags & IFF_FRAGMENTABLE
    796 #endif
    797 	    )
    798 	{
    799 #ifdef NEWIP6OUTPUT
    800 		error = nd6_output(ifp, m, dst, ro->ro_rt);
    801 #else
    802 		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
    803 					  ro->ro_rt);
    804 #endif
    805 		goto done;
    806 	} else if (mtu < IPV6_MMTU) {
    807 		/*
    808 		 * note that path MTU is never less than IPV6_MMTU
    809 		 * (see icmp6_input).
    810 		 */
    811 		error = EMSGSIZE;
    812 		goto bad;
    813 	} else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
    814 		error = EMSGSIZE;
    815 		goto bad;
    816 	} else {
    817 		struct mbuf **mnext, *m_frgpart;
    818 		struct ip6_frag *ip6f;
    819 		u_int32_t id = htonl(ip6_id++);
    820 		u_char nextproto;
    821 
    822 		/*
    823 		 * Too large for the destination or interface;
    824 		 * fragment if possible.
    825 		 * Must be able to put at least 8 bytes per fragment.
    826 		 */
    827 		hlen = unfragpartlen;
    828 		if (mtu > IPV6_MAXPACKET)
    829 			mtu = IPV6_MAXPACKET;
    830 		len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
    831 		if (len < 8) {
    832 			error = EMSGSIZE;
    833 			goto bad;
    834 		}
    835 
    836 		mnext = &m->m_nextpkt;
    837 
    838 		/*
    839 		 * Change the next header field of the last header in the
    840 		 * unfragmentable part.
    841 		 */
    842 		if (exthdrs.ip6e_rthdr) {
    843 			nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
    844 			*mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
    845 		}
    846 		else if (exthdrs.ip6e_dest1) {
    847 			nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
    848 			*mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
    849 		}
    850 		else if (exthdrs.ip6e_hbh) {
    851 			nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
    852 			*mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
    853 		}
    854 		else {
    855 			nextproto = ip6->ip6_nxt;
    856 			ip6->ip6_nxt = IPPROTO_FRAGMENT;
    857 		}
    858 
    859 		/*
    860 		 * Loop through length of segment after first fragment,
    861 		 * make new header and copy data of each part and link onto chain.
    862 		 */
    863 		m0 = m;
    864 		for (off = hlen; off < tlen; off += len) {
    865 			MGETHDR(m, M_DONTWAIT, MT_HEADER);
    866 			if (!m) {
    867 				error = ENOBUFS;
    868 				ip6stat.ip6s_odropped++;
    869 				goto sendorfree;
    870 			}
    871 			m->m_flags = m0->m_flags & M_COPYFLAGS;
    872 			*mnext = m;
    873 			mnext = &m->m_nextpkt;
    874 			m->m_data += max_linkhdr;
    875 			mhip6 = mtod(m, struct ip6_hdr *);
    876 			*mhip6 = *ip6;
    877 			m->m_len = sizeof(*mhip6);
    878  			error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
    879  			if (error) {
    880 				ip6stat.ip6s_odropped++;
    881 				goto sendorfree;
    882 			}
    883 			ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
    884 			if (off + len >= tlen)
    885 				len = tlen - off;
    886 			else
    887 				ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
    888 			mhip6->ip6_plen = htons((u_short)(len + hlen +
    889 							  sizeof(*ip6f) -
    890 							  sizeof(struct ip6_hdr)));
    891 			if ((m_frgpart = m_copy(m0, off, len)) == 0) {
    892 				error = ENOBUFS;
    893 				ip6stat.ip6s_odropped++;
    894 				goto sendorfree;
    895 			}
    896 			m_cat(m, m_frgpart);
    897 			m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
    898 			m->m_pkthdr.rcvif = (struct ifnet *)0;
    899 			ip6f->ip6f_reserved = 0;
    900 			ip6f->ip6f_ident = id;
    901 			ip6f->ip6f_nxt = nextproto;
    902 			ip6stat.ip6s_ofragments++;
    903 		}
    904 	}
    905 
    906 	/*
    907 	 * Remove leading garbages.
    908 	 */
    909 sendorfree:
    910 	m = m0->m_nextpkt;
    911 	m0->m_nextpkt = 0;
    912 	m_freem(m0);
    913 	for (m0 = m; m; m = m0) {
    914 		m0 = m->m_nextpkt;
    915 		m->m_nextpkt = 0;
    916 		if (error == 0) {
    917 #ifdef NEWIP6OUTPUT
    918 			error = nd6_output(ifp, m, dst, ro->ro_rt);
    919 #else
    920 			error = (*ifp->if_output)(ifp, m,
    921 						  (struct sockaddr *)dst,
    922 						  ro->ro_rt);
    923 #endif
    924 		}
    925 		else
    926 			m_freem(m);
    927 	}
    928 
    929 	if (error == 0)
    930 		ip6stat.ip6s_fragmented++;
    931 
    932 done:
    933 	if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
    934 		RTFREE(ro->ro_rt);
    935 	} else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
    936 		RTFREE(ro_pmtu->ro_rt);
    937 	}
    938 
    939 #ifdef IPSEC
    940 	if (sp != NULL)
    941 		key_freesp(sp);
    942 #endif /* IPSEC */
    943 
    944 	return(error);
    945 
    946 freehdrs:
    947 	m_freem(exthdrs.ip6e_hbh);	/* m_freem will check if mbuf is 0 */
    948 	m_freem(exthdrs.ip6e_dest1);
    949 	m_freem(exthdrs.ip6e_rthdr);
    950 	m_freem(exthdrs.ip6e_dest2);
    951 	/* fall through */
    952 bad:
    953 	m_freem(m);
    954 	goto done;
    955 }
    956 
    957 static int
    958 ip6_copyexthdr(mp, hdr, hlen)
    959 	struct mbuf **mp;
    960 	caddr_t hdr;
    961 	int hlen;
    962 {
    963 	struct mbuf *m;
    964 
    965 	if (hlen > MCLBYTES)
    966 		return(ENOBUFS); /* XXX */
    967 
    968 	MGET(m, M_DONTWAIT, MT_DATA);
    969 	if (!m)
    970 		return(ENOBUFS);
    971 
    972 	if (hlen > MLEN) {
    973 		MCLGET(m, M_DONTWAIT);
    974 		if ((m->m_flags & M_EXT) == 0) {
    975 			m_free(m);
    976 			return(ENOBUFS);
    977 		}
    978 	}
    979 	m->m_len = hlen;
    980 	if (hdr)
    981 		bcopy(hdr, mtod(m, caddr_t), hlen);
    982 
    983 	*mp = m;
    984 	return(0);
    985 }
    986 
    987 /*
    988  * Insert jumbo payload option.
    989  */
    990 static int
    991 ip6_insert_jumboopt(exthdrs, plen)
    992 	struct ip6_exthdrs *exthdrs;
    993 	u_int32_t plen;
    994 {
    995 	struct mbuf *mopt;
    996 	u_char *optbuf;
    997 
    998 #define JUMBOOPTLEN	8	/* length of jumbo payload option and padding */
    999 
   1000 	/*
   1001 	 * If there is no hop-by-hop options header, allocate new one.
   1002 	 * If there is one but it doesn't have enough space to store the
   1003 	 * jumbo payload option, allocate a cluster to store the whole options.
   1004 	 * Otherwise, use it to store the options.
   1005 	 */
   1006 	if (exthdrs->ip6e_hbh == 0) {
   1007 		MGET(mopt, M_DONTWAIT, MT_DATA);
   1008 		if (mopt == 0)
   1009 			return(ENOBUFS);
   1010 		mopt->m_len = JUMBOOPTLEN;
   1011 		optbuf = mtod(mopt, u_char *);
   1012 		optbuf[1] = 0;	/* = ((JUMBOOPTLEN) >> 3) - 1 */
   1013 		exthdrs->ip6e_hbh = mopt;
   1014 	}
   1015 	else {
   1016 		struct ip6_hbh *hbh;
   1017 
   1018 		mopt = exthdrs->ip6e_hbh;
   1019 		if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
   1020 			caddr_t oldoptp = mtod(mopt, caddr_t);
   1021 			int oldoptlen = mopt->m_len;
   1022 
   1023 			if (mopt->m_flags & M_EXT)
   1024 				return(ENOBUFS); /* XXX */
   1025 			MCLGET(mopt, M_DONTWAIT);
   1026 			if ((mopt->m_flags & M_EXT) == 0)
   1027 				return(ENOBUFS);
   1028 
   1029 			bcopy(oldoptp, mtod(mopt, caddr_t), oldoptlen);
   1030 			optbuf = mtod(mopt, caddr_t) + oldoptlen;
   1031 			mopt->m_len = oldoptlen + JUMBOOPTLEN;
   1032 		}
   1033 		else {
   1034 			optbuf = mtod(mopt, u_char *) + mopt->m_len;
   1035 			mopt->m_len += JUMBOOPTLEN;
   1036 		}
   1037 		optbuf[0] = IP6OPT_PADN;
   1038 		optbuf[1] = 1;
   1039 
   1040 		/*
   1041 		 * Adjust the header length according to the pad and
   1042 		 * the jumbo payload option.
   1043 		 */
   1044 		hbh = mtod(mopt, struct ip6_hbh *);
   1045 		hbh->ip6h_len += (JUMBOOPTLEN >> 3);
   1046 	}
   1047 
   1048 	/* fill in the option. */
   1049 	optbuf[2] = IP6OPT_JUMBO;
   1050 	optbuf[3] = 4;
   1051 	*(u_int32_t *)&optbuf[4] = htonl(plen + JUMBOOPTLEN);
   1052 
   1053 	/* finally, adjust the packet header length */
   1054 	exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
   1055 
   1056 	return(0);
   1057 #undef JUMBOOPTLEN
   1058 }
   1059 
   1060 /*
   1061  * Insert fragment header and copy unfragmentable header portions.
   1062  */
   1063 static int
   1064 ip6_insertfraghdr(m0, m, hlen, frghdrp)
   1065 	struct mbuf *m0, *m;
   1066 	int hlen;
   1067 	struct ip6_frag **frghdrp;
   1068 {
   1069 	struct mbuf *n, *mlast;
   1070 
   1071 	if (hlen > sizeof(struct ip6_hdr)) {
   1072 		n = m_copym(m0, sizeof(struct ip6_hdr),
   1073 			    hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
   1074 		if (n == 0)
   1075 			return(ENOBUFS);
   1076 		m->m_next = n;
   1077 	}
   1078 	else
   1079 		n = m;
   1080 
   1081 	/* Search for the last mbuf of unfragmentable part. */
   1082 	for (mlast = n; mlast->m_next; mlast = mlast->m_next)
   1083 		;
   1084 
   1085 	if ((mlast->m_flags & M_EXT) == 0 &&
   1086 	    M_TRAILINGSPACE(mlast) < sizeof(struct ip6_frag)) {
   1087 		/* use the trailing space of the last mbuf for the fragment hdr */
   1088 		*frghdrp =
   1089 			(struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
   1090 		mlast->m_len += sizeof(struct ip6_frag);
   1091 		m->m_pkthdr.len += sizeof(struct ip6_frag);
   1092 	}
   1093 	else {
   1094 		/* allocate a new mbuf for the fragment header */
   1095 		struct mbuf *mfrg;
   1096 
   1097 		MGET(mfrg, M_DONTWAIT, MT_DATA);
   1098 		if (mfrg == 0)
   1099 			return(ENOBUFS);
   1100 		mfrg->m_len = sizeof(struct ip6_frag);
   1101 		*frghdrp = mtod(mfrg, struct ip6_frag *);
   1102 		mlast->m_next = mfrg;
   1103 	}
   1104 
   1105 	return(0);
   1106 }
   1107 
   1108 /*
   1109  * IP6 socket option processing.
   1110  */
   1111 int
   1112 ip6_ctloutput(op, so, level, optname, mp)
   1113 	int op;
   1114 	struct socket *so;
   1115 	int level, optname;
   1116 	struct mbuf **mp;
   1117 {
   1118 	register struct in6pcb *in6p = sotoin6pcb(so);
   1119 	register struct mbuf *m = *mp;
   1120 	register int optval = 0;
   1121 	int error = 0;
   1122 	struct proc *p = curproc;	/* XXX */
   1123 
   1124 	if (level == IPPROTO_IPV6)
   1125 		switch (op) {
   1126 
   1127 		case PRCO_SETOPT:
   1128 			switch (optname) {
   1129 			case IPV6_PKTOPTIONS:
   1130 				return(ip6_pcbopts(&in6p->in6p_outputopts,
   1131 						   m, so));
   1132 			case IPV6_HOPOPTS:
   1133 			case IPV6_DSTOPTS:
   1134 				if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
   1135 					error = EPERM;
   1136 					break;
   1137 				}
   1138 				/* fall through */
   1139 			case IPV6_UNICAST_HOPS:
   1140 			case IPV6_RECVOPTS:
   1141 			case IPV6_RECVRETOPTS:
   1142 			case IPV6_RECVDSTADDR:
   1143 			case IPV6_PKTINFO:
   1144 			case IPV6_HOPLIMIT:
   1145 			case IPV6_RTHDR:
   1146 			case IPV6_CHECKSUM:
   1147 			case IPV6_FAITH:
   1148 				if (!m || m->m_len != sizeof(int))
   1149 					error = EINVAL;
   1150 				else {
   1151 					optval = *mtod(m, int *);
   1152 					switch (optname) {
   1153 
   1154 					case IPV6_UNICAST_HOPS:
   1155 						if (optval < -1 || optval >= 256)
   1156 							error = EINVAL;
   1157 						else {
   1158 							/* -1 = kernel default */
   1159 							in6p->in6p_hops = optval;
   1160 						}
   1161 						break;
   1162 #define OPTSET(bit) \
   1163 	if (optval) \
   1164 		in6p->in6p_flags |= bit; \
   1165 	else \
   1166 		in6p->in6p_flags &= ~bit;
   1167 
   1168 					case IPV6_RECVOPTS:
   1169 						OPTSET(IN6P_RECVOPTS);
   1170 						break;
   1171 
   1172 					case IPV6_RECVRETOPTS:
   1173 						OPTSET(IN6P_RECVRETOPTS);
   1174 						break;
   1175 
   1176 					case IPV6_RECVDSTADDR:
   1177 						OPTSET(IN6P_RECVDSTADDR);
   1178 						break;
   1179 
   1180 					case IPV6_PKTINFO:
   1181 						OPTSET(IN6P_PKTINFO);
   1182 						break;
   1183 
   1184 					case IPV6_HOPLIMIT:
   1185 						OPTSET(IN6P_HOPLIMIT);
   1186 						break;
   1187 
   1188 					case IPV6_HOPOPTS:
   1189 						OPTSET(IN6P_HOPOPTS);
   1190 						break;
   1191 
   1192 					case IPV6_DSTOPTS:
   1193 						OPTSET(IN6P_DSTOPTS);
   1194 						break;
   1195 
   1196 					case IPV6_RTHDR:
   1197 						OPTSET(IN6P_RTHDR);
   1198 						break;
   1199 
   1200 					case IPV6_CHECKSUM:
   1201 						in6p->in6p_cksum = optval;
   1202 						break;
   1203 
   1204 					case IPV6_FAITH:
   1205 						OPTSET(IN6P_FAITH);
   1206 						break;
   1207 					}
   1208 				}
   1209 				break;
   1210 #undef OPTSET
   1211 
   1212 			case IPV6_MULTICAST_IF:
   1213 			case IPV6_MULTICAST_HOPS:
   1214 			case IPV6_MULTICAST_LOOP:
   1215 			case IPV6_JOIN_GROUP:
   1216 			case IPV6_LEAVE_GROUP:
   1217 				error =	ip6_setmoptions(optname, &in6p->in6p_moptions, m);
   1218 				break;
   1219 
   1220 #ifdef IPSEC
   1221 			case IPV6_IPSEC_POLICY:
   1222 			    {
   1223 				caddr_t req = NULL;
   1224 				int len = 0;
   1225 				int priv = 0;
   1226 #ifdef __NetBSD__
   1227 				if (p == 0 || suser(p->p_ucred, &p->p_acflag))
   1228 					priv = 0;
   1229 				else
   1230 					priv = 1;
   1231 #else
   1232 				priv = (in6p->in6p_socket->so_state & SS_PRIV);
   1233 #endif
   1234 				if (m != 0) {
   1235 					req = mtod(m, caddr_t);
   1236 					len = m->m_len;
   1237 				}
   1238 				error = ipsec_set_policy(&in6p->in6p_sp,
   1239 				                   optname, req, len,
   1240 				                   priv);
   1241 			    }
   1242 				break;
   1243 #endif /* IPSEC */
   1244 
   1245 			default:
   1246 				error = ENOPROTOOPT;
   1247 				break;
   1248 			}
   1249 			if (m)
   1250 				(void)m_free(m);
   1251 			break;
   1252 
   1253 		case PRCO_GETOPT:
   1254 			switch (optname) {
   1255 
   1256 			case IPV6_OPTIONS:
   1257 			case IPV6_RETOPTS:
   1258 #if 0
   1259 				*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1260 				if (in6p->in6p_options) {
   1261 					m->m_len = in6p->in6p_options->m_len;
   1262 					bcopy(mtod(in6p->in6p_options, caddr_t),
   1263 					      mtod(m, caddr_t),
   1264 					      (unsigned)m->m_len);
   1265 				} else
   1266 					m->m_len = 0;
   1267 				break;
   1268 #else
   1269 				error = ENOPROTOOPT;
   1270 				break;
   1271 #endif
   1272 
   1273 			case IPV6_PKTOPTIONS:
   1274 				if (in6p->in6p_options) {
   1275 					*mp = m_copym(in6p->in6p_options, 0,
   1276 						      M_COPYALL, M_WAIT);
   1277 				} else {
   1278 					*mp = m_get(M_WAIT, MT_SOOPTS);
   1279 					(*mp)->m_len = 0;
   1280 				}
   1281 				break;
   1282 
   1283 			case IPV6_HOPOPTS:
   1284 			case IPV6_DSTOPTS:
   1285 				if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
   1286 					error = EPERM;
   1287 					break;
   1288 				}
   1289 				/* fall through */
   1290 			case IPV6_UNICAST_HOPS:
   1291 			case IPV6_RECVOPTS:
   1292 			case IPV6_RECVRETOPTS:
   1293 			case IPV6_RECVDSTADDR:
   1294 			case IPV6_PKTINFO:
   1295 			case IPV6_HOPLIMIT:
   1296 			case IPV6_RTHDR:
   1297 			case IPV6_CHECKSUM:
   1298 			case IPV6_FAITH:
   1299 				*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1300 				m->m_len = sizeof(int);
   1301 				switch (optname) {
   1302 
   1303 				case IPV6_UNICAST_HOPS:
   1304 					optval = in6p->in6p_hops;
   1305 					break;
   1306 
   1307 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
   1308 
   1309 				case IPV6_RECVOPTS:
   1310 					optval = OPTBIT(IN6P_RECVOPTS);
   1311 					break;
   1312 
   1313 				case IPV6_RECVRETOPTS:
   1314 					optval = OPTBIT(IN6P_RECVRETOPTS);
   1315 					break;
   1316 
   1317 				case IPV6_RECVDSTADDR:
   1318 					optval = OPTBIT(IN6P_RECVDSTADDR);
   1319 					break;
   1320 
   1321 				case IPV6_PKTINFO:
   1322 					optval = OPTBIT(IN6P_PKTINFO);
   1323 					break;
   1324 
   1325 				case IPV6_HOPLIMIT:
   1326 					optval = OPTBIT(IN6P_HOPLIMIT);
   1327 					break;
   1328 
   1329 				case IPV6_HOPOPTS:
   1330 					optval = OPTBIT(IN6P_HOPOPTS);
   1331 					break;
   1332 
   1333 				case IPV6_DSTOPTS:
   1334 					optval = OPTBIT(IN6P_DSTOPTS);
   1335 					break;
   1336 
   1337 				case IPV6_RTHDR:
   1338 					optval = OPTBIT(IN6P_RTHDR);
   1339 					break;
   1340 
   1341 				case IPV6_CHECKSUM:
   1342 					optval = in6p->in6p_cksum;
   1343 					break;
   1344 
   1345 				case IPV6_FAITH:
   1346 					optval = OPTBIT(IN6P_FAITH);
   1347 					break;
   1348 				}
   1349 				*mtod(m, int *) = optval;
   1350 				break;
   1351 
   1352 			case IPV6_MULTICAST_IF:
   1353 			case IPV6_MULTICAST_HOPS:
   1354 			case IPV6_MULTICAST_LOOP:
   1355 			case IPV6_JOIN_GROUP:
   1356 			case IPV6_LEAVE_GROUP:
   1357 				error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
   1358 				break;
   1359 
   1360 #ifdef IPSEC
   1361 			case IPV6_IPSEC_POLICY:
   1362 				error = ipsec_get_policy(in6p->in6p_sp, mp);
   1363 				break;
   1364 #endif /* IPSEC */
   1365 
   1366 			default:
   1367 				error = ENOPROTOOPT;
   1368 				break;
   1369 			}
   1370 			break;
   1371 		}
   1372 	else {
   1373 		error = EINVAL;
   1374 		if (op == PRCO_SETOPT && *mp)
   1375 			(void)m_free(*mp);
   1376 	}
   1377 	return(error);
   1378 }
   1379 
   1380 /*
   1381  * Set up IP6 options in pcb for insertion in output packets.
   1382  * Store in mbuf with pointer in pcbopt, adding pseudo-option
   1383  * with destination address if source routed.
   1384  */
   1385 static int
   1386 ip6_pcbopts(pktopt, m, so)
   1387 	struct ip6_pktopts **pktopt;
   1388 	register struct mbuf *m;
   1389 	struct socket *so;
   1390 {
   1391 	register struct ip6_pktopts *opt = *pktopt;
   1392 	int error = 0;
   1393 	struct proc *p = curproc;	/* XXX */
   1394 	int priv = 0;
   1395 
   1396 	/* turn off any old options. */
   1397 	if (opt) {
   1398 		if (opt->ip6po_m)
   1399 			(void)m_free(opt->ip6po_m);
   1400 	}
   1401 	else
   1402 		opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
   1403 	*pktopt = 0;
   1404 
   1405 	if (!m || m->m_len == 0) {
   1406 		/*
   1407 		 * Only turning off any previous options.
   1408 		 */
   1409 		if (opt)
   1410 			free(opt, M_IP6OPT);
   1411 		if (m)
   1412 			(void)m_free(m);
   1413 		return(0);
   1414 	}
   1415 
   1416 	/*  set options specified by user. */
   1417 	if (p && !suser(p->p_ucred, &p->p_acflag))
   1418 		priv = 1;
   1419 	if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
   1420 		(void)m_free(m);
   1421 		return(error);
   1422 	}
   1423 	*pktopt = opt;
   1424 	return(0);
   1425 }
   1426 
   1427 /*
   1428  * Set the IP6 multicast options in response to user setsockopt().
   1429  */
   1430 static int
   1431 ip6_setmoptions(optname, im6op, m)
   1432 	int optname;
   1433 	struct ip6_moptions **im6op;
   1434 	struct mbuf *m;
   1435 {
   1436 	int error = 0;
   1437 	u_int loop, ifindex;
   1438 	struct ipv6_mreq *mreq;
   1439 	struct ifnet *ifp;
   1440 	struct ip6_moptions *im6o = *im6op;
   1441 	struct route_in6 ro;
   1442 	struct sockaddr_in6 *dst;
   1443 	struct in6_multi_mship *imm;
   1444 	struct proc *p = curproc;	/* XXX */
   1445 
   1446 	if (im6o == NULL) {
   1447 		/*
   1448 		 * No multicast option buffer attached to the pcb;
   1449 		 * allocate one and initialize to default values.
   1450 		 */
   1451 		im6o = (struct ip6_moptions *)
   1452 			malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
   1453 
   1454 		if (im6o == NULL)
   1455 			return(ENOBUFS);
   1456 		*im6op = im6o;
   1457 		im6o->im6o_multicast_ifp = NULL;
   1458 		im6o->im6o_multicast_hlim = ip6_defmcasthlim;
   1459 		im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
   1460 		LIST_INIT(&im6o->im6o_memberships);
   1461 	}
   1462 
   1463 	switch (optname) {
   1464 
   1465 	case IPV6_MULTICAST_IF:
   1466 		/*
   1467 		 * Select the interface for outgoing multicast packets.
   1468 		 */
   1469 		if (m == NULL || m->m_len != sizeof(u_int)) {
   1470 			error = EINVAL;
   1471 			break;
   1472 		}
   1473 		ifindex = *(mtod(m, u_int *));
   1474 		if (ifindex < 0 || if_index < ifindex) {
   1475 			error = ENXIO;	/* XXX EINVAL? */
   1476 			break;
   1477 		}
   1478 		ifp = ifindex2ifnet[ifindex];
   1479 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1480 			error = EADDRNOTAVAIL;
   1481 			break;
   1482 		}
   1483 		im6o->im6o_multicast_ifp = ifp;
   1484 		break;
   1485 
   1486 	case IPV6_MULTICAST_HOPS:
   1487 	    {
   1488 		/*
   1489 		 * Set the IP6 hoplimit for outgoing multicast packets.
   1490 		 */
   1491 		int optval;
   1492 		if (m == NULL || m->m_len != sizeof(int)) {
   1493 			error = EINVAL;
   1494 			break;
   1495 		}
   1496 		optval = *(mtod(m, u_int *));
   1497 		if (optval < -1 || optval >= 256)
   1498 			error = EINVAL;
   1499 		else if (optval == -1)
   1500 			im6o->im6o_multicast_hlim = ip6_defmcasthlim;
   1501 		else
   1502 			im6o->im6o_multicast_hlim = optval;
   1503 		break;
   1504 	    }
   1505 
   1506 	case IPV6_MULTICAST_LOOP:
   1507 		/*
   1508 		 * Set the loopback flag for outgoing multicast packets.
   1509 		 * Must be zero or one.
   1510 		 */
   1511 		if (m == NULL || m->m_len != sizeof(u_int) ||
   1512 		   (loop = *(mtod(m, u_int *))) > 1) {
   1513 			error = EINVAL;
   1514 			break;
   1515 		}
   1516 		im6o->im6o_multicast_loop = loop;
   1517 		break;
   1518 
   1519 	case IPV6_JOIN_GROUP:
   1520 		/*
   1521 		 * Add a multicast group membership.
   1522 		 * Group must be a valid IP6 multicast address.
   1523 		 */
   1524 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
   1525 			error = EINVAL;
   1526 			break;
   1527 		}
   1528 		mreq = mtod(m, struct ipv6_mreq *);
   1529 		if (IN6_IS_ADDR_ANY(&mreq->ipv6mr_multiaddr)) {
   1530 			/*
   1531 			 * We use the unspecified address to specify to accept
   1532 			 * all multicast addresses. Only super user is allowed
   1533 			 * to do this.
   1534 			 */
   1535 			if (suser(p->p_ucred, &p->p_acflag)) {
   1536 				error = EACCES;
   1537 				break;
   1538 			}
   1539 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
   1540 			error = EINVAL;
   1541 			break;
   1542 		}
   1543 
   1544 		/*
   1545 		 * If the interface is specified, validate it.
   1546 		 */
   1547 		if (mreq->ipv6mr_interface < 0
   1548 		 || if_index < mreq->ipv6mr_interface) {
   1549 			error = ENXIO;	/* XXX EINVAL? */
   1550 			break;
   1551 		}
   1552 		/*
   1553 		 * If no interface was explicitly specified, choose an
   1554 		 * appropriate one according to the given multicast address.
   1555 		 */
   1556 		if (mreq->ipv6mr_interface == 0) {
   1557 			/*
   1558 			 * If the multicast address is in node-local scope,
   1559 			 * the interface should be a loopback interface.
   1560 			 * Otherwise, look up the routing table for the
   1561 			 * address, and choose the outgoing interface.
   1562 			 *   XXX: is it a good approach?
   1563 			 */
   1564 			if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
   1565 #ifdef __bsdi__
   1566 				ifp = &loif;
   1567 #else
   1568 				ifp = &loif[0];
   1569 #endif
   1570 			}
   1571 			else {
   1572 				ro.ro_rt = NULL;
   1573 				dst = (struct sockaddr_in6 *)&ro.ro_dst;
   1574 				bzero(dst, sizeof(*dst));
   1575 				dst->sin6_len = sizeof(struct sockaddr_in6);
   1576 				dst->sin6_family = AF_INET6;
   1577 				dst->sin6_addr = mreq->ipv6mr_multiaddr;
   1578 				rtalloc((struct route *)&ro);
   1579 				if (ro.ro_rt == NULL) {
   1580 					error = EADDRNOTAVAIL;
   1581 					break;
   1582 				}
   1583 				ifp = ro.ro_rt->rt_ifp;
   1584 				rtfree(ro.ro_rt);
   1585 			}
   1586 		} else
   1587 			ifp = ifindex2ifnet[mreq->ipv6mr_interface];
   1588 
   1589 		/*
   1590 		 * See if we found an interface, and confirm that it
   1591 		 * supports multicast
   1592 		 */
   1593 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1594 			error = EADDRNOTAVAIL;
   1595 			break;
   1596 		}
   1597 		/*
   1598 		 * Put interface index into the multicast address,
   1599 		 * if the address has link-local scope.
   1600 		 */
   1601 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
   1602 			mreq->ipv6mr_multiaddr.s6_addr16[1]
   1603 				= htons(mreq->ipv6mr_interface);
   1604 		}
   1605 		/*
   1606 		 * See if the membership already exists.
   1607 		 */
   1608 		for (imm = im6o->im6o_memberships.lh_first;
   1609 		     imm != NULL; imm = imm->i6mm_chain.le_next)
   1610 			if (imm->i6mm_maddr->in6m_ifp == ifp &&
   1611 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
   1612 					       &mreq->ipv6mr_multiaddr))
   1613 				break;
   1614 		if (imm != NULL) {
   1615 			error = EADDRINUSE;
   1616 			break;
   1617 		}
   1618 		/*
   1619 		 * Everything looks good; add a new record to the multicast
   1620 		 * address list for the given interface.
   1621 		 */
   1622 		imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
   1623 		if (imm == NULL) {
   1624 			error = ENOBUFS;
   1625 			break;
   1626 		}
   1627 		if ((imm->i6mm_maddr =
   1628 		     in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
   1629 			free(imm, M_IPMADDR);
   1630 			break;
   1631 		}
   1632 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
   1633 		break;
   1634 
   1635 	case IPV6_LEAVE_GROUP:
   1636 		/*
   1637 		 * Drop a multicast group membership.
   1638 		 * Group must be a valid IP6 multicast address.
   1639 		 */
   1640 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
   1641 			error = EINVAL;
   1642 			break;
   1643 		}
   1644 		mreq = mtod(m, struct ipv6_mreq *);
   1645 		if (IN6_IS_ADDR_ANY(&mreq->ipv6mr_multiaddr)) {
   1646 			if (suser(p->p_ucred, &p->p_acflag)) {
   1647 				error = EACCES;
   1648 				break;
   1649 			}
   1650 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
   1651 			error = EINVAL;
   1652 			break;
   1653 		}
   1654 		/*
   1655 		 * If an interface address was specified, get a pointer
   1656 		 * to its ifnet structure.
   1657 		 */
   1658 		if (mreq->ipv6mr_interface < 0
   1659 		 || if_index < mreq->ipv6mr_interface) {
   1660 			error = ENXIO;	/* XXX EINVAL? */
   1661 			break;
   1662 		}
   1663 		ifp = ifindex2ifnet[mreq->ipv6mr_interface];
   1664 		/*
   1665 		 * Put interface index into the multicast address,
   1666 		 * if the address has link-local scope.
   1667 		 */
   1668 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
   1669 			mreq->ipv6mr_multiaddr.s6_addr16[1]
   1670 				= htons(mreq->ipv6mr_interface);
   1671 		}
   1672 		/*
   1673 		 * Find the membership in the membership list.
   1674 		 */
   1675 		for (imm = im6o->im6o_memberships.lh_first;
   1676 		     imm != NULL; imm = imm->i6mm_chain.le_next) {
   1677 			if ((ifp == NULL ||
   1678 			     imm->i6mm_maddr->in6m_ifp == ifp) &&
   1679 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
   1680 					       &mreq->ipv6mr_multiaddr))
   1681 				break;
   1682 		}
   1683 		if (imm == NULL) {
   1684 			/* Unable to resolve interface */
   1685 			error = EADDRNOTAVAIL;
   1686 			break;
   1687 		}
   1688 		/*
   1689 		 * Give up the multicast address record to which the
   1690 		 * membership points.
   1691 		 */
   1692 		LIST_REMOVE(imm, i6mm_chain);
   1693 		in6_delmulti(imm->i6mm_maddr);
   1694 		free(imm, M_IPMADDR);
   1695 		break;
   1696 
   1697 	default:
   1698 		error = EOPNOTSUPP;
   1699 		break;
   1700 	}
   1701 
   1702 	/*
   1703 	 * If all options have default values, no need to keep the mbuf.
   1704 	 */
   1705 	if (im6o->im6o_multicast_ifp == NULL &&
   1706 	    im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
   1707 	    im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
   1708 	    im6o->im6o_memberships.lh_first == NULL) {
   1709 		free(*im6op, M_IPMOPTS);
   1710 		*im6op = NULL;
   1711 	}
   1712 
   1713 	return(error);
   1714 }
   1715 
   1716 /*
   1717  * Return the IP6 multicast options in response to user getsockopt().
   1718  */
   1719 static int
   1720 ip6_getmoptions(optname, im6o, mp)
   1721 	int optname;
   1722 	register struct ip6_moptions *im6o;
   1723 	register struct mbuf **mp;
   1724 {
   1725 	u_int *hlim, *loop, *ifindex;
   1726 
   1727 	*mp = m_get(M_WAIT, MT_SOOPTS);
   1728 
   1729 	switch (optname) {
   1730 
   1731 	case IPV6_MULTICAST_IF:
   1732 		ifindex = mtod(*mp, u_int *);
   1733 		(*mp)->m_len = sizeof(u_int);
   1734 		if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
   1735 			*ifindex = 0;
   1736 		else
   1737 			*ifindex = im6o->im6o_multicast_ifp->if_index;
   1738 		return(0);
   1739 
   1740 	case IPV6_MULTICAST_HOPS:
   1741 		hlim = mtod(*mp, u_int *);
   1742 		(*mp)->m_len = sizeof(u_int);
   1743 		if (im6o == NULL)
   1744 			*hlim = ip6_defmcasthlim;
   1745 		else
   1746 			*hlim = im6o->im6o_multicast_hlim;
   1747 		return(0);
   1748 
   1749 	case IPV6_MULTICAST_LOOP:
   1750 		loop = mtod(*mp, u_int *);
   1751 		(*mp)->m_len = sizeof(u_int);
   1752 		if (im6o == NULL)
   1753 			*loop = ip6_defmcasthlim;
   1754 		else
   1755 			*loop = im6o->im6o_multicast_loop;
   1756 		return(0);
   1757 
   1758 	default:
   1759 		return(EOPNOTSUPP);
   1760 	}
   1761 }
   1762 
   1763 /*
   1764  * Discard the IP6 multicast options.
   1765  */
   1766 void
   1767 ip6_freemoptions(im6o)
   1768 	register struct ip6_moptions *im6o;
   1769 {
   1770 	struct in6_multi_mship *imm;
   1771 
   1772 	if (im6o == NULL)
   1773 		return;
   1774 
   1775 	while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
   1776 		LIST_REMOVE(imm, i6mm_chain);
   1777 		if (imm->i6mm_maddr)
   1778 			in6_delmulti(imm->i6mm_maddr);
   1779 		free(imm, M_IPMADDR);
   1780 	}
   1781 	free(im6o, M_IPMOPTS);
   1782 }
   1783 
   1784 /*
   1785  * Set IPv6 outgoing packet options based on advanced API.
   1786  */
   1787 int
   1788 ip6_setpktoptions(control, opt, priv)
   1789 	struct mbuf *control;
   1790 	struct ip6_pktopts *opt;
   1791 	int priv;
   1792 {
   1793 	register struct cmsghdr *cm = 0;
   1794 
   1795 	if (control == 0 || opt == 0)
   1796 		return(EINVAL);
   1797 
   1798 	bzero(opt, sizeof(*opt));
   1799 	opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
   1800 
   1801 	/*
   1802 	 * XXX: Currently, we assume all the optional information is stored
   1803 	 * in a single mbuf.
   1804 	 */
   1805 	if (control->m_next)
   1806 		return(EINVAL);
   1807 
   1808 	opt->ip6po_m = control;
   1809 
   1810 	for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
   1811 		     control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
   1812 		cm = mtod(control, struct cmsghdr *);
   1813 		if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
   1814 			return(EINVAL);
   1815 		if (cm->cmsg_level != IPPROTO_IPV6)
   1816 			continue;
   1817 
   1818 		switch(cm->cmsg_type) {
   1819 		case IPV6_PKTINFO:
   1820 			if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
   1821 				return(EINVAL);
   1822 			opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
   1823 			if (opt->ip6po_pktinfo->ipi6_ifindex &&
   1824 			    IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
   1825 				opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
   1826 					htons(opt->ip6po_pktinfo->ipi6_ifindex);
   1827 
   1828 			if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
   1829 			 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
   1830 				return(ENXIO);
   1831 			}
   1832 
   1833 			if (!IN6_IS_ADDR_ANY(&opt->ip6po_pktinfo->ipi6_addr)) {
   1834 				struct ifaddr *ia;
   1835 				struct sockaddr_in6 sin6;
   1836 
   1837 				bzero(&sin6, sizeof(sin6));
   1838 				sin6.sin6_len = sizeof(sin6);
   1839 				sin6.sin6_family = AF_INET6;
   1840 				sin6.sin6_addr =
   1841 					opt->ip6po_pktinfo->ipi6_addr;
   1842 				ia = ifa_ifwithaddr(sin6tosa(&sin6));
   1843 				if (ia == NULL ||
   1844 				    (opt->ip6po_pktinfo->ipi6_ifindex &&
   1845 				     (ia->ifa_ifp->if_index !=
   1846 				      opt->ip6po_pktinfo->ipi6_ifindex))) {
   1847 					return(EADDRNOTAVAIL);
   1848 				}
   1849 				/*
   1850 				 * Check if the requested source address is
   1851 				 * indeed a unicast address assigned to the
   1852 				 * node.
   1853 				 */
   1854 				if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
   1855 					return(EADDRNOTAVAIL);
   1856 			}
   1857 			break;
   1858 
   1859 		case IPV6_HOPLIMIT:
   1860 			if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
   1861 				return(EINVAL);
   1862 
   1863 			opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
   1864 			if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
   1865 				return(EINVAL);
   1866 			break;
   1867 
   1868 		case IPV6_NEXTHOP:
   1869 			if (!priv)
   1870 				return(EPERM);
   1871 			if (cm->cmsg_len < sizeof(u_char) ||
   1872 			    cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
   1873 				return(EINVAL);
   1874 
   1875 			opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
   1876 
   1877 			break;
   1878 
   1879 		case IPV6_HOPOPTS:
   1880 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
   1881 				return(EINVAL);
   1882 			opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm);
   1883 			if (cm->cmsg_len !=
   1884 			    CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3))
   1885 				return(EINVAL);
   1886 			break;
   1887 
   1888 		case IPV6_DSTOPTS:
   1889 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
   1890 				return(EINVAL);
   1891 
   1892 			/*
   1893 			 * If there is no routing header yet, the destination
   1894 			 * options header should be put on the 1st part.
   1895 			 * Otherwise, the header should be on the 2nd part.
   1896 			 * (See RFC 2460, section 4.1)
   1897 			 */
   1898 			if (opt->ip6po_rthdr == NULL) {
   1899 				opt->ip6po_dest1 =
   1900 					(struct ip6_dest *)CMSG_DATA(cm);
   1901 				if (cm->cmsg_len !=
   1902 				    CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1)
   1903 					     << 3))
   1904 					return(EINVAL);
   1905 			}
   1906 			else {
   1907 				opt->ip6po_dest2 =
   1908 					(struct ip6_dest *)CMSG_DATA(cm);
   1909 				if (cm->cmsg_len !=
   1910 				    CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1)
   1911 					     << 3))
   1912 					return(EINVAL);
   1913 			}
   1914 			break;
   1915 
   1916 		case IPV6_RTHDR:
   1917 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
   1918 				return(EINVAL);
   1919 			opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm);
   1920 			if (cm->cmsg_len !=
   1921 			    CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3))
   1922 				return(EINVAL);
   1923 			switch(opt->ip6po_rthdr->ip6r_type) {
   1924 			case IPV6_RTHDR_TYPE_0:
   1925 				if (opt->ip6po_rthdr->ip6r_segleft == 0)
   1926 					return(EINVAL);
   1927 				break;
   1928 			default:
   1929 				return(EINVAL);
   1930 			}
   1931 			break;
   1932 
   1933 		default:
   1934 			return(ENOPROTOOPT);
   1935 		}
   1936 	}
   1937 
   1938 	return(0);
   1939 }
   1940 
   1941 /*
   1942  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
   1943  * packet to the input queue of a specified interface.  Note that this
   1944  * calls the output routine of the loopback "driver", but with an interface
   1945  * pointer that might NOT be &loif -- easier than replicating that code here.
   1946  */
   1947 void
   1948 ip6_mloopback(ifp, m, dst)
   1949 	struct ifnet *ifp;
   1950 	register struct mbuf *m;
   1951 	register struct sockaddr_in6 *dst;
   1952 {
   1953 	struct	mbuf *copym;
   1954 
   1955 	copym = m_copy(m, 0, M_COPYALL);
   1956 	if (copym != NULL)
   1957 		(void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
   1958 }
   1959 
   1960 /*
   1961  * Chop IPv6 header off from the payload.
   1962  */
   1963 static int
   1964 ip6_splithdr(m, exthdrs)
   1965 	struct mbuf *m;
   1966 	struct ip6_exthdrs *exthdrs;
   1967 {
   1968 	struct mbuf *mh;
   1969 	struct ip6_hdr *ip6;
   1970 
   1971 	ip6 = mtod(m, struct ip6_hdr *);
   1972 	if (m->m_len > sizeof(*ip6)) {
   1973 		MGETHDR(mh, M_DONTWAIT, MT_HEADER);
   1974 		if (mh == 0) {
   1975 			m_freem(m);
   1976 			return ENOBUFS;
   1977 		}
   1978 		M_COPY_PKTHDR(mh, m);
   1979 		MH_ALIGN(mh, sizeof(*ip6));
   1980 		m->m_flags &= ~M_PKTHDR;
   1981 		m->m_len -= sizeof(*ip6);
   1982 		m->m_data += sizeof(*ip6);
   1983 		mh->m_next = m;
   1984 		m = mh;
   1985 		m->m_len = sizeof(*ip6);
   1986 		bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
   1987 	}
   1988 	exthdrs->ip6e_ip6 = m;
   1989 	return 0;
   1990 }
   1991 
   1992 /*
   1993  * Compute IPv6 extension header length.
   1994  */
   1995 int
   1996 ip6_optlen(in6p)
   1997 	struct in6pcb *in6p;
   1998 {
   1999 	int len;
   2000 
   2001 	if (!in6p->in6p_outputopts)
   2002 		return 0;
   2003 
   2004 	len = 0;
   2005 #define elen(x) \
   2006     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
   2007 
   2008 	len += elen(in6p->in6p_outputopts->ip6po_hbh);
   2009 	len += elen(in6p->in6p_outputopts->ip6po_dest1);
   2010 	len += elen(in6p->in6p_outputopts->ip6po_rthdr);
   2011 	len += elen(in6p->in6p_outputopts->ip6po_dest2);
   2012 	return len;
   2013 #undef elen
   2014 }
   2015