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