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