Home | History | Annotate | Line # | Download | only in netinet6
ip6_output.c revision 1.25
      1 /*	$NetBSD: ip6_output.c,v 1.25 2000/08/19 08:15:54 itojun Exp $	*/
      2 /*	$KAME: ip6_output.c,v 1.122 2000/08/19 02:12:02 jinmei 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 "opt_inet.h"
     69 #include "opt_ipsec.h"
     70 #include "opt_pfil_hooks.h"
     71 
     72 #include <sys/param.h>
     73 #include <sys/malloc.h>
     74 #include <sys/mbuf.h>
     75 #include <sys/errno.h>
     76 #include <sys/protosw.h>
     77 #include <sys/socket.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/systm.h>
     80 #include <sys/proc.h>
     81 
     82 #include <net/if.h>
     83 #include <net/route.h>
     84 #ifdef PFIL_HOOKS
     85 #include <net/pfil.h>
     86 #endif
     87 
     88 #include <netinet/in.h>
     89 #include <netinet/in_var.h>
     90 #include <netinet/ip6.h>
     91 #include <netinet/icmp6.h>
     92 #include <netinet6/ip6_var.h>
     93 #include <netinet6/in6_pcb.h>
     94 #include <netinet6/nd6.h>
     95 
     96 #ifdef IPSEC
     97 #include <netinet6/ipsec.h>
     98 #include <netkey/key.h>
     99 #endif /* IPSEC */
    100 
    101 #include "loop.h"
    102 
    103 #include <net/net_osdep.h>
    104 
    105 #ifdef IPV6FIREWALL
    106 #include <netinet6/ip6_fw.h>
    107 #endif
    108 
    109 struct ip6_exthdrs {
    110 	struct mbuf *ip6e_ip6;
    111 	struct mbuf *ip6e_hbh;
    112 	struct mbuf *ip6e_dest1;
    113 	struct mbuf *ip6e_rthdr;
    114 	struct mbuf *ip6e_dest2;
    115 };
    116 
    117 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
    118 			    struct socket *));
    119 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
    120 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
    121 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
    122 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
    123 				  struct ip6_frag **));
    124 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
    125 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
    126 
    127 extern struct ifnet loif[NLOOP];
    128 
    129 /*
    130  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
    131  * header (with pri, len, nxt, hlim, src, dst).
    132  * This function may modify ver and hlim only.
    133  * The mbuf chain containing the packet will be freed.
    134  * The mbuf opt, if present, will not be freed.
    135  */
    136 int
    137 ip6_output(m0, opt, ro, flags, im6o, ifpp)
    138 	struct mbuf *m0;
    139 	struct ip6_pktopts *opt;
    140 	struct route_in6 *ro;
    141 	int flags;
    142 	struct ip6_moptions *im6o;
    143 	struct ifnet **ifpp;		/* XXX: just for statistics */
    144 {
    145 	struct ip6_hdr *ip6, *mhip6;
    146 	struct ifnet *ifp, *origifp;
    147 	struct mbuf *m = m0;
    148 	int hlen, tlen, len, off;
    149 	struct route_in6 ip6route;
    150 	struct sockaddr_in6 *dst;
    151 	int error = 0;
    152 	struct in6_ifaddr *ia;
    153 	u_long mtu;
    154 	u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
    155 	struct ip6_exthdrs exthdrs;
    156 	struct in6_addr finaldst;
    157 	struct route_in6 *ro_pmtu = NULL;
    158 	int hdrsplit = 0;
    159 	int needipsec = 0;
    160 #ifdef PFIL_HOOKS
    161 	struct packet_filter_hook *pfh;
    162 	struct mbuf *m1;
    163 	int rv;
    164 #endif /* PFIL_HOOKS */
    165 #ifdef IPSEC
    166 	int needipsectun = 0;
    167 	struct socket *so;
    168 	struct secpolicy *sp = NULL;
    169 
    170 	/* for AH processing. stupid to have "socket" variable in IP layer... */
    171 	so = ipsec_getsocket(m);
    172 	ipsec_setsocket(m, NULL);
    173 	ip6 = mtod(m, struct ip6_hdr *);
    174 #endif /* IPSEC */
    175 
    176 #define MAKE_EXTHDR(hp, mp)						\
    177     do {								\
    178 	if (hp) {							\
    179 		struct ip6_ext *eh = (struct ip6_ext *)(hp);		\
    180 		error = ip6_copyexthdr((mp), (caddr_t)(hp), 		\
    181 				       ((eh)->ip6e_len + 1) << 3);	\
    182 		if (error)						\
    183 			goto freehdrs;					\
    184 	}								\
    185     } while (0)
    186 
    187 	bzero(&exthdrs, sizeof(exthdrs));
    188 	if (opt) {
    189 		/* Hop-by-Hop options header */
    190 		MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
    191 		/* Destination options header(1st part) */
    192 		MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
    193 		/* Routing header */
    194 		MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
    195 		/* Destination options header(2nd part) */
    196 		MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
    197 	}
    198 
    199 #ifdef IPSEC
    200 	/* get a security policy for this packet */
    201 	if (so == NULL)
    202 		sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
    203 	else
    204 		sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
    205 
    206 	if (sp == NULL) {
    207 		ipsec6stat.out_inval++;
    208 		goto freehdrs;
    209 	}
    210 
    211 	error = 0;
    212 
    213 	/* check policy */
    214 	switch (sp->policy) {
    215 	case IPSEC_POLICY_DISCARD:
    216 		/*
    217 		 * This packet is just discarded.
    218 		 */
    219 		ipsec6stat.out_polvio++;
    220 		goto freehdrs;
    221 
    222 	case IPSEC_POLICY_BYPASS:
    223 	case IPSEC_POLICY_NONE:
    224 		/* no need to do IPsec. */
    225 		needipsec = 0;
    226 		break;
    227 
    228 	case IPSEC_POLICY_IPSEC:
    229 		if (sp->req == NULL) {
    230 			/* XXX should be panic ? */
    231 			printf("ip6_output: No IPsec request specified.\n");
    232 			error = EINVAL;
    233 			goto freehdrs;
    234 		}
    235 		needipsec = 1;
    236 		break;
    237 
    238 	case IPSEC_POLICY_ENTRUST:
    239 	default:
    240 		printf("ip6_output: Invalid policy found. %d\n", sp->policy);
    241 	}
    242 #endif /* IPSEC */
    243 
    244 	/*
    245 	 * Calculate the total length of the extension header chain.
    246 	 * Keep the length of the unfragmentable part for fragmentation.
    247 	 */
    248 	optlen = 0;
    249 	if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
    250 	if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
    251 	if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
    252 	unfragpartlen = optlen + sizeof(struct ip6_hdr);
    253 	/* NOTE: we don't add AH/ESP length here. do that later. */
    254 	if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
    255 
    256 	/*
    257 	 * If we need IPsec, or there is at least one extension header,
    258 	 * separate IP6 header from the payload.
    259 	 */
    260 	if ((needipsec || optlen) && !hdrsplit) {
    261 		if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
    262 			m = NULL;
    263 			goto freehdrs;
    264 		}
    265 		m = exthdrs.ip6e_ip6;
    266 		hdrsplit++;
    267 	}
    268 
    269 	/* adjust pointer */
    270 	ip6 = mtod(m, struct ip6_hdr *);
    271 
    272 	/* adjust mbuf packet header length */
    273 	m->m_pkthdr.len += optlen;
    274 	plen = m->m_pkthdr.len - sizeof(*ip6);
    275 
    276 	/* If this is a jumbo payload, insert a jumbo payload option. */
    277 	if (plen > IPV6_MAXPACKET) {
    278 		if (!hdrsplit) {
    279 			if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
    280 				m = NULL;
    281 				goto freehdrs;
    282 			}
    283 			m = exthdrs.ip6e_ip6;
    284 			hdrsplit++;
    285 		}
    286 		/* adjust pointer */
    287 		ip6 = mtod(m, struct ip6_hdr *);
    288 		if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
    289 			goto freehdrs;
    290 		ip6->ip6_plen = 0;
    291 	} else
    292 		ip6->ip6_plen = htons(plen);
    293 
    294 	/*
    295 	 * Concatenate headers and fill in next header fields.
    296 	 * Here we have, on "m"
    297 	 *	IPv6 payload
    298 	 * and we insert headers accordingly.  Finally, we should be getting:
    299 	 *	IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
    300 	 *
    301 	 * during the header composing process, "m" points to IPv6 header.
    302 	 * "mprev" points to an extension header prior to esp.
    303 	 */
    304 	{
    305 		u_char *nexthdrp = &ip6->ip6_nxt;
    306 		struct mbuf *mprev = m;
    307 
    308 		/*
    309 		 * we treat dest2 specially.  this makes IPsec processing
    310 		 * much easier.
    311 		 *
    312 		 * result: IPv6 dest2 payload
    313 		 * m and mprev will point to IPv6 header.
    314 		 */
    315 		if (exthdrs.ip6e_dest2) {
    316 			if (!hdrsplit)
    317 				panic("assumption failed: hdr not split");
    318 			exthdrs.ip6e_dest2->m_next = m->m_next;
    319 			m->m_next = exthdrs.ip6e_dest2;
    320 			*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
    321 			ip6->ip6_nxt = IPPROTO_DSTOPTS;
    322 		}
    323 
    324 #define MAKE_CHAIN(m, mp, p, i)\
    325     do {\
    326 	if (m) {\
    327 		if (!hdrsplit) \
    328 			panic("assumption failed: hdr not split"); \
    329 		*mtod((m), u_char *) = *(p);\
    330 		*(p) = (i);\
    331 		p = mtod((m), u_char *);\
    332 		(m)->m_next = (mp)->m_next;\
    333 		(mp)->m_next = (m);\
    334 		(mp) = (m);\
    335 	}\
    336     } while (0)
    337 		/*
    338 		 * result: IPv6 hbh dest1 rthdr dest2 payload
    339 		 * m will point to IPv6 header.  mprev will point to the
    340 		 * extension header prior to dest2 (rthdr in the above case).
    341 		 */
    342 		MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
    343 			   nexthdrp, IPPROTO_HOPOPTS);
    344 		MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
    345 			   nexthdrp, IPPROTO_DSTOPTS);
    346 		MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
    347 			   nexthdrp, IPPROTO_ROUTING);
    348 
    349 #ifdef IPSEC
    350 		if (!needipsec)
    351 			goto skip_ipsec2;
    352 
    353 		/*
    354 		 * pointers after IPsec headers are not valid any more.
    355 		 * other pointers need a great care too.
    356 		 * (IPsec routines should not mangle mbufs prior to AH/ESP)
    357 		 */
    358 		exthdrs.ip6e_dest2 = NULL;
    359 
    360 	    {
    361 		struct ip6_rthdr *rh = NULL;
    362 		int segleft_org = 0;
    363 		struct ipsec_output_state state;
    364 
    365 		if (exthdrs.ip6e_rthdr) {
    366 			rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
    367 			segleft_org = rh->ip6r_segleft;
    368 			rh->ip6r_segleft = 0;
    369 		}
    370 
    371 		bzero(&state, sizeof(state));
    372 		state.m = m;
    373 		error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
    374 			&needipsectun);
    375 		m = state.m;
    376 		if (error) {
    377 			/* mbuf is already reclaimed in ipsec6_output_trans. */
    378 			m = NULL;
    379 			switch (error) {
    380 			case EHOSTUNREACH:
    381 			case ENETUNREACH:
    382 			case EMSGSIZE:
    383 			case ENOBUFS:
    384 			case ENOMEM:
    385 				break;
    386 			default:
    387 				printf("ip6_output (ipsec): error code %d\n", error);
    388 				/*fall through*/
    389 			case ENOENT:
    390 				/* don't show these error codes to the user */
    391 				error = 0;
    392 				break;
    393 			}
    394 			goto bad;
    395 		}
    396 		if (exthdrs.ip6e_rthdr) {
    397 			/* ah6_output doesn't modify mbuf chain */
    398 			rh->ip6r_segleft = segleft_org;
    399 		}
    400 	    }
    401 skip_ipsec2:;
    402 #endif
    403 	}
    404 
    405 	/*
    406 	 * If there is a routing header, replace destination address field
    407 	 * with the first hop of the routing header.
    408 	 */
    409 	if (exthdrs.ip6e_rthdr) {
    410 		struct ip6_rthdr *rh =
    411 			(struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
    412 						  struct ip6_rthdr *));
    413 		struct ip6_rthdr0 *rh0;
    414 
    415 		finaldst = ip6->ip6_dst;
    416 		switch(rh->ip6r_type) {
    417 		case IPV6_RTHDR_TYPE_0:
    418 			 rh0 = (struct ip6_rthdr0 *)rh;
    419 			 ip6->ip6_dst = rh0->ip6r0_addr[0];
    420 			 bcopy((caddr_t)&rh0->ip6r0_addr[1],
    421 				 (caddr_t)&rh0->ip6r0_addr[0],
    422 				 sizeof(struct in6_addr) * (rh0->ip6r0_segleft - 1)
    423 				 );
    424 			 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
    425 			 break;
    426 		default:	/* is it possible? */
    427 			 error = EINVAL;
    428 			 goto bad;
    429 		}
    430 	}
    431 
    432 	/* Source address validation */
    433 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
    434 	    (flags & IPV6_DADOUTPUT) == 0) {
    435 		error = EOPNOTSUPP;
    436 		ip6stat.ip6s_badscope++;
    437 		goto bad;
    438 	}
    439 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
    440 		error = EOPNOTSUPP;
    441 		ip6stat.ip6s_badscope++;
    442 		goto bad;
    443 	}
    444 
    445 	ip6stat.ip6s_localout++;
    446 
    447 	/*
    448 	 * Route packet.
    449 	 */
    450 	if (ro == 0) {
    451 		ro = &ip6route;
    452 		bzero((caddr_t)ro, sizeof(*ro));
    453 	}
    454 	ro_pmtu = ro;
    455 	if (opt && opt->ip6po_rthdr)
    456 		ro = &opt->ip6po_route;
    457 	dst = (struct sockaddr_in6 *)&ro->ro_dst;
    458 	/*
    459 	 * If there is a cached route,
    460 	 * check that it is to the same destination
    461 	 * and is still up. If not, free it and try again.
    462 	 */
    463 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    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) {
    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 			 mtu = ifmtu;
    748 			 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
    749 				 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
    750 		}
    751 	} else {
    752 		mtu = nd_ifinfo[ifp->if_index].linkmtu;
    753 	}
    754 
    755 	/* Fake scoped addresses */
    756 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
    757 		/*
    758 		 * If source or destination address is a scoped address, and
    759 		 * the packet is going to be sent to a loopback interface,
    760 		 * we should keep the original interface.
    761 		 */
    762 
    763 		/*
    764 		 * XXX: this is a very experimental and temporary solution.
    765 		 * We eventually have sockaddr_in6 and use the sin6_scope_id
    766 		 * field of the structure here.
    767 		 * We rely on the consistency between two scope zone ids
    768 		 * of source add destination, which should already be assured
    769 		 * Larger scopes than link will be supported in the near
    770 		 * future.
    771 		 */
    772 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    773 			origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
    774 		else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    775 			origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
    776 		else
    777 			origifp = ifp;
    778 	}
    779 	else
    780 		origifp = ifp;
    781 #ifndef FAKE_LOOPBACK_IF
    782 	if ((ifp->if_flags & IFF_LOOPBACK) == 0)
    783 #else
    784 	if (1)
    785 #endif
    786 	{
    787 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
    788 			ip6->ip6_src.s6_addr16[1] = 0;
    789 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
    790 			ip6->ip6_dst.s6_addr16[1] = 0;
    791 	}
    792 
    793 	/*
    794 	 * If the outgoing packet contains a hop-by-hop options header,
    795 	 * it must be examined and processed even by the source node.
    796 	 * (RFC 2460, section 4.)
    797 	 */
    798 	if (exthdrs.ip6e_hbh) {
    799 		struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh,
    800 					   struct ip6_hbh *);
    801 		u_int32_t dummy1; /* XXX unused */
    802 		u_int32_t dummy2; /* XXX unused */
    803 
    804 		/*
    805 		 *  XXX: if we have to send an ICMPv6 error to the sender,
    806 		 *       we need the M_LOOP flag since icmp6_error() expects
    807 		 *       the IPv6 and the hop-by-hop options header are
    808 		 *       continuous unless the flag is set.
    809 		 */
    810 		m->m_flags |= M_LOOP;
    811 		m->m_pkthdr.rcvif = ifp;
    812 		if (ip6_process_hopopts(m,
    813 					(u_int8_t *)(hbh + 1),
    814 					((hbh->ip6h_len + 1) << 3) -
    815 					sizeof(struct ip6_hbh),
    816 					&dummy1, &dummy2) < 0) {
    817 			/* m was already freed at this point */
    818 			error = EINVAL;/* better error? */
    819 			goto done;
    820 		}
    821 		m->m_flags &= ~M_LOOP; /* XXX */
    822 		m->m_pkthdr.rcvif = NULL;
    823 	}
    824 
    825 #ifdef PFIL_HOOKS
    826 	/*
    827 	 * Run through list of hooks for output packets.
    828 	 */
    829 	m1 = m;
    830 	pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IPV6]].pr_pfh);
    831 	for (; pfh; pfh = pfh->pfil_link.tqe_next)
    832 		if (pfh->pfil_func) {
    833 		    	rv = pfh->pfil_func(ip6, sizeof(*ip6), ifp, 1, &m1);
    834 			if (rv) {
    835 				error = EHOSTUNREACH;
    836 				goto done;
    837 			}
    838 			m = m1;
    839 			if (m == NULL)
    840 				goto done;
    841 			ip6 = mtod(m, struct ip6_hdr *);
    842 		}
    843 #endif /* PFIL_HOOKS */
    844 	/*
    845 	 * Send the packet to the outgoing interface.
    846 	 * If necessary, do IPv6 fragmentation before sending.
    847 	 */
    848 	tlen = m->m_pkthdr.len;
    849 	if (tlen <= mtu
    850 #ifdef notyet
    851 	    /*
    852 	     * On any link that cannot convey a 1280-octet packet in one piece,
    853 	     * link-specific fragmentation and reassembly must be provided at
    854 	     * a layer below IPv6. [RFC 2460, sec.5]
    855 	     * Thus if the interface has ability of link-level fragmentation,
    856 	     * we can just send the packet even if the packet size is
    857 	     * larger than the link's MTU.
    858 	     * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
    859 	     */
    860 
    861 	    || ifp->if_flags & IFF_FRAGMENTABLE
    862 #endif
    863 	    )
    864 	{
    865 #ifdef IFA_STATS
    866 		if (IFA_STATS) {
    867 			struct in6_ifaddr *ia6;
    868 			ip6 = mtod(m, struct ip6_hdr *);
    869 			ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
    870 			if (ia6) {
    871 				ia->ia_ifa.ifa_data.ifad_outbytes +=
    872 					m->m_pkthdr.len;
    873 			}
    874 		}
    875 #endif
    876 #ifdef OLDIP6OUTPUT
    877 		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
    878 					  ro->ro_rt);
    879 #else
    880 		error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
    881 #endif
    882 		goto done;
    883 	} else if (mtu < IPV6_MMTU) {
    884 		/*
    885 		 * note that path MTU is never less than IPV6_MMTU
    886 		 * (see icmp6_input).
    887 		 */
    888 		error = EMSGSIZE;
    889 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
    890 		goto bad;
    891 	} else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
    892 		error = EMSGSIZE;
    893 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
    894 		goto bad;
    895 	} else {
    896 		struct mbuf **mnext, *m_frgpart;
    897 		struct ip6_frag *ip6f;
    898 		u_int32_t id = htonl(ip6_id++);
    899 		u_char nextproto;
    900 
    901 		/*
    902 		 * Too large for the destination or interface;
    903 		 * fragment if possible.
    904 		 * Must be able to put at least 8 bytes per fragment.
    905 		 */
    906 		hlen = unfragpartlen;
    907 		if (mtu > IPV6_MAXPACKET)
    908 			mtu = IPV6_MAXPACKET;
    909 		len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
    910 		if (len < 8) {
    911 			error = EMSGSIZE;
    912 			in6_ifstat_inc(ifp, ifs6_out_fragfail);
    913 			goto bad;
    914 		}
    915 
    916 		mnext = &m->m_nextpkt;
    917 
    918 		/*
    919 		 * Change the next header field of the last header in the
    920 		 * unfragmentable part.
    921 		 */
    922 		if (exthdrs.ip6e_rthdr) {
    923 			nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
    924 			*mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
    925 		} else if (exthdrs.ip6e_dest1) {
    926 			nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
    927 			*mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
    928 		} else if (exthdrs.ip6e_hbh) {
    929 			nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
    930 			*mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
    931 		} else {
    932 			nextproto = ip6->ip6_nxt;
    933 			ip6->ip6_nxt = IPPROTO_FRAGMENT;
    934 		}
    935 
    936 		/*
    937 		 * Loop through length of segment after first fragment,
    938 		 * make new header and copy data of each part and link onto chain.
    939 		 */
    940 		m0 = m;
    941 		for (off = hlen; off < tlen; off += len) {
    942 			MGETHDR(m, M_DONTWAIT, MT_HEADER);
    943 			if (!m) {
    944 				error = ENOBUFS;
    945 				ip6stat.ip6s_odropped++;
    946 				goto sendorfree;
    947 			}
    948 			m->m_flags = m0->m_flags & M_COPYFLAGS;
    949 			*mnext = m;
    950 			mnext = &m->m_nextpkt;
    951 			m->m_data += max_linkhdr;
    952 			mhip6 = mtod(m, struct ip6_hdr *);
    953 			*mhip6 = *ip6;
    954 			m->m_len = sizeof(*mhip6);
    955  			error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
    956  			if (error) {
    957 				ip6stat.ip6s_odropped++;
    958 				goto sendorfree;
    959 			}
    960 			ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
    961 			if (off + len >= tlen)
    962 				len = tlen - off;
    963 			else
    964 				ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
    965 			mhip6->ip6_plen = htons((u_short)(len + hlen +
    966 							  sizeof(*ip6f) -
    967 							  sizeof(struct ip6_hdr)));
    968 			if ((m_frgpart = m_copy(m0, off, len)) == 0) {
    969 				error = ENOBUFS;
    970 				ip6stat.ip6s_odropped++;
    971 				goto sendorfree;
    972 			}
    973 			m_cat(m, m_frgpart);
    974 			m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
    975 			m->m_pkthdr.rcvif = (struct ifnet *)0;
    976 			ip6f->ip6f_reserved = 0;
    977 			ip6f->ip6f_ident = id;
    978 			ip6f->ip6f_nxt = nextproto;
    979 			ip6stat.ip6s_ofragments++;
    980 			in6_ifstat_inc(ifp, ifs6_out_fragcreat);
    981 		}
    982 
    983 		in6_ifstat_inc(ifp, ifs6_out_fragok);
    984 	}
    985 
    986 	/*
    987 	 * Remove leading garbages.
    988 	 */
    989 sendorfree:
    990 	m = m0->m_nextpkt;
    991 	m0->m_nextpkt = 0;
    992 	m_freem(m0);
    993 	for (m0 = m; m; m = m0) {
    994 		m0 = m->m_nextpkt;
    995 		m->m_nextpkt = 0;
    996 		if (error == 0) {
    997 #ifdef IFA_STATS
    998 			if (IFA_STATS) {
    999 				struct in6_ifaddr *ia6;
   1000 				ip6 = mtod(m, struct ip6_hdr *);
   1001 				ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
   1002 				if (ia6) {
   1003 					ia->ia_ifa.ifa_data.ifad_outbytes +=
   1004 						m->m_pkthdr.len;
   1005 				}
   1006 			}
   1007 #endif
   1008 #ifdef OLDIP6OUTPUT
   1009 			error = (*ifp->if_output)(ifp, m,
   1010 						  (struct sockaddr *)dst,
   1011 						  ro->ro_rt);
   1012 #else
   1013 			error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
   1014 #endif
   1015 		} else
   1016 			m_freem(m);
   1017 	}
   1018 
   1019 	if (error == 0)
   1020 		ip6stat.ip6s_fragmented++;
   1021 
   1022 done:
   1023 	if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
   1024 		RTFREE(ro->ro_rt);
   1025 	} else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
   1026 		RTFREE(ro_pmtu->ro_rt);
   1027 	}
   1028 
   1029 #ifdef IPSEC
   1030 	if (sp != NULL)
   1031 		key_freesp(sp);
   1032 #endif /* IPSEC */
   1033 
   1034 	return(error);
   1035 
   1036 freehdrs:
   1037 	m_freem(exthdrs.ip6e_hbh);	/* m_freem will check if mbuf is 0 */
   1038 	m_freem(exthdrs.ip6e_dest1);
   1039 	m_freem(exthdrs.ip6e_rthdr);
   1040 	m_freem(exthdrs.ip6e_dest2);
   1041 	/* fall through */
   1042 bad:
   1043 	m_freem(m);
   1044 	goto done;
   1045 }
   1046 
   1047 static int
   1048 ip6_copyexthdr(mp, hdr, hlen)
   1049 	struct mbuf **mp;
   1050 	caddr_t hdr;
   1051 	int hlen;
   1052 {
   1053 	struct mbuf *m;
   1054 
   1055 	if (hlen > MCLBYTES)
   1056 		return(ENOBUFS); /* XXX */
   1057 
   1058 	MGET(m, M_DONTWAIT, MT_DATA);
   1059 	if (!m)
   1060 		return(ENOBUFS);
   1061 
   1062 	if (hlen > MLEN) {
   1063 		MCLGET(m, M_DONTWAIT);
   1064 		if ((m->m_flags & M_EXT) == 0) {
   1065 			m_free(m);
   1066 			return(ENOBUFS);
   1067 		}
   1068 	}
   1069 	m->m_len = hlen;
   1070 	if (hdr)
   1071 		bcopy(hdr, mtod(m, caddr_t), hlen);
   1072 
   1073 	*mp = m;
   1074 	return(0);
   1075 }
   1076 
   1077 /*
   1078  * Insert jumbo payload option.
   1079  */
   1080 static int
   1081 ip6_insert_jumboopt(exthdrs, plen)
   1082 	struct ip6_exthdrs *exthdrs;
   1083 	u_int32_t plen;
   1084 {
   1085 	struct mbuf *mopt;
   1086 	u_char *optbuf;
   1087 	u_int32_t v;
   1088 
   1089 #define JUMBOOPTLEN	8	/* length of jumbo payload option and padding */
   1090 
   1091 	/*
   1092 	 * If there is no hop-by-hop options header, allocate new one.
   1093 	 * If there is one but it doesn't have enough space to store the
   1094 	 * jumbo payload option, allocate a cluster to store the whole options.
   1095 	 * Otherwise, use it to store the options.
   1096 	 */
   1097 	if (exthdrs->ip6e_hbh == 0) {
   1098 		MGET(mopt, M_DONTWAIT, MT_DATA);
   1099 		if (mopt == 0)
   1100 			return(ENOBUFS);
   1101 		mopt->m_len = JUMBOOPTLEN;
   1102 		optbuf = mtod(mopt, u_char *);
   1103 		optbuf[1] = 0;	/* = ((JUMBOOPTLEN) >> 3) - 1 */
   1104 		exthdrs->ip6e_hbh = mopt;
   1105 	} else {
   1106 		struct ip6_hbh *hbh;
   1107 
   1108 		mopt = exthdrs->ip6e_hbh;
   1109 		if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
   1110 			/*
   1111 			 * XXX assumption:
   1112 			 * - exthdrs->ip6e_hbh is not referenced from places
   1113 			 *   other than exthdrs.
   1114 			 * - exthdrs->ip6e_hbh is not an mbuf chain.
   1115 			 */
   1116 			int oldoptlen = mopt->m_len;
   1117 			struct mbuf *n;
   1118 
   1119 			/*
   1120 			 * XXX: give up if the whole (new) hbh header does
   1121 			 * not fit even in an mbuf cluster.
   1122 			 */
   1123 			if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
   1124 				return(ENOBUFS);
   1125 
   1126 			/*
   1127 			 * As a consequence, we must always prepare a cluster
   1128 			 * at this point.
   1129 			 */
   1130 			MGET(n, M_DONTWAIT, MT_DATA);
   1131 			if (n) {
   1132 				MCLGET(n, M_DONTWAIT);
   1133 				if ((n->m_flags & M_EXT) == 0) {
   1134 					m_freem(n);
   1135 					n = NULL;
   1136 				}
   1137 			}
   1138 			if (!n)
   1139 				return(ENOBUFS);
   1140 			n->m_len = oldoptlen + JUMBOOPTLEN;
   1141 			bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
   1142 			      oldoptlen);
   1143 			optbuf = mtod(n, caddr_t) + oldoptlen;
   1144 			m_freem(mopt);
   1145 			exthdrs->ip6e_hbh = n;
   1146 		} else {
   1147 			optbuf = mtod(mopt, u_char *) + mopt->m_len;
   1148 			mopt->m_len += JUMBOOPTLEN;
   1149 		}
   1150 		optbuf[0] = IP6OPT_PADN;
   1151 		optbuf[1] = 1;
   1152 
   1153 		/*
   1154 		 * Adjust the header length according to the pad and
   1155 		 * the jumbo payload option.
   1156 		 */
   1157 		hbh = mtod(mopt, struct ip6_hbh *);
   1158 		hbh->ip6h_len += (JUMBOOPTLEN >> 3);
   1159 	}
   1160 
   1161 	/* fill in the option. */
   1162 	optbuf[2] = IP6OPT_JUMBO;
   1163 	optbuf[3] = 4;
   1164 	v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
   1165 	bcopy(&v, &optbuf[4], sizeof(u_int32_t));
   1166 
   1167 	/* finally, adjust the packet header length */
   1168 	exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
   1169 
   1170 	return(0);
   1171 #undef JUMBOOPTLEN
   1172 }
   1173 
   1174 /*
   1175  * Insert fragment header and copy unfragmentable header portions.
   1176  */
   1177 static int
   1178 ip6_insertfraghdr(m0, m, hlen, frghdrp)
   1179 	struct mbuf *m0, *m;
   1180 	int hlen;
   1181 	struct ip6_frag **frghdrp;
   1182 {
   1183 	struct mbuf *n, *mlast;
   1184 
   1185 	if (hlen > sizeof(struct ip6_hdr)) {
   1186 		n = m_copym(m0, sizeof(struct ip6_hdr),
   1187 			    hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
   1188 		if (n == 0)
   1189 			return(ENOBUFS);
   1190 		m->m_next = n;
   1191 	} else
   1192 		n = m;
   1193 
   1194 	/* Search for the last mbuf of unfragmentable part. */
   1195 	for (mlast = n; mlast->m_next; mlast = mlast->m_next)
   1196 		;
   1197 
   1198 	if ((mlast->m_flags & M_EXT) == 0 &&
   1199 	    M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
   1200 		/* use the trailing space of the last mbuf for the fragment hdr */
   1201 		*frghdrp =
   1202 			(struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
   1203 		mlast->m_len += sizeof(struct ip6_frag);
   1204 		m->m_pkthdr.len += sizeof(struct ip6_frag);
   1205 	} else {
   1206 		/* allocate a new mbuf for the fragment header */
   1207 		struct mbuf *mfrg;
   1208 
   1209 		MGET(mfrg, M_DONTWAIT, MT_DATA);
   1210 		if (mfrg == 0)
   1211 			return(ENOBUFS);
   1212 		mfrg->m_len = sizeof(struct ip6_frag);
   1213 		*frghdrp = mtod(mfrg, struct ip6_frag *);
   1214 		mlast->m_next = mfrg;
   1215 	}
   1216 
   1217 	return(0);
   1218 }
   1219 
   1220 /*
   1221  * IP6 socket option processing.
   1222  */
   1223 int
   1224 ip6_ctloutput(op, so, level, optname, mp)
   1225 	int op;
   1226 	struct socket *so;
   1227 	int level, optname;
   1228 	struct mbuf **mp;
   1229 {
   1230 	register struct in6pcb *in6p = sotoin6pcb(so);
   1231 	register struct mbuf *m = *mp;
   1232 	register int optval = 0;
   1233 	int error = 0;
   1234 	struct proc *p = curproc;	/* XXX */
   1235 
   1236 	if (level == IPPROTO_IPV6) {
   1237 		switch (op) {
   1238 
   1239 		case PRCO_SETOPT:
   1240 			switch (optname) {
   1241 			case IPV6_PKTOPTIONS:
   1242 				/* m is freed in ip6_pcbopts */
   1243 				return(ip6_pcbopts(&in6p->in6p_outputopts,
   1244 						   m, so));
   1245 			case IPV6_HOPOPTS:
   1246 			case IPV6_DSTOPTS:
   1247 				if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
   1248 					error = EPERM;
   1249 					break;
   1250 				}
   1251 				/* fall through */
   1252 			case IPV6_UNICAST_HOPS:
   1253 			case IPV6_RECVOPTS:
   1254 			case IPV6_RECVRETOPTS:
   1255 			case IPV6_RECVDSTADDR:
   1256 			case IPV6_PKTINFO:
   1257 			case IPV6_HOPLIMIT:
   1258 			case IPV6_RTHDR:
   1259 			case IPV6_CHECKSUM:
   1260 			case IPV6_FAITH:
   1261 #ifndef INET6_BINDV6ONLY
   1262 			case IPV6_BINDV6ONLY:
   1263 #endif
   1264 				if (!m || m->m_len != sizeof(int))
   1265 					error = EINVAL;
   1266 				else {
   1267 					optval = *mtod(m, int *);
   1268 					switch (optname) {
   1269 
   1270 					case IPV6_UNICAST_HOPS:
   1271 						if (optval < -1 || optval >= 256)
   1272 							error = EINVAL;
   1273 						else {
   1274 							/* -1 = kernel default */
   1275 							in6p->in6p_hops = optval;
   1276 						}
   1277 						break;
   1278 #define OPTSET(bit) \
   1279 	if (optval) \
   1280 		in6p->in6p_flags |= bit; \
   1281 	else \
   1282 		in6p->in6p_flags &= ~bit;
   1283 
   1284 					case IPV6_RECVOPTS:
   1285 						OPTSET(IN6P_RECVOPTS);
   1286 						break;
   1287 
   1288 					case IPV6_RECVRETOPTS:
   1289 						OPTSET(IN6P_RECVRETOPTS);
   1290 						break;
   1291 
   1292 					case IPV6_RECVDSTADDR:
   1293 						OPTSET(IN6P_RECVDSTADDR);
   1294 						break;
   1295 
   1296 					case IPV6_PKTINFO:
   1297 						OPTSET(IN6P_PKTINFO);
   1298 						break;
   1299 
   1300 					case IPV6_HOPLIMIT:
   1301 						OPTSET(IN6P_HOPLIMIT);
   1302 						break;
   1303 
   1304 					case IPV6_HOPOPTS:
   1305 						OPTSET(IN6P_HOPOPTS);
   1306 						break;
   1307 
   1308 					case IPV6_DSTOPTS:
   1309 						OPTSET(IN6P_DSTOPTS);
   1310 						break;
   1311 
   1312 					case IPV6_RTHDR:
   1313 						OPTSET(IN6P_RTHDR);
   1314 						break;
   1315 
   1316 					case IPV6_CHECKSUM:
   1317 						in6p->in6p_cksum = optval;
   1318 						break;
   1319 
   1320 					case IPV6_FAITH:
   1321 						OPTSET(IN6P_FAITH);
   1322 						break;
   1323 
   1324 #ifndef INET6_BINDV6ONLY
   1325 					case IPV6_BINDV6ONLY:
   1326 						OPTSET(IN6P_BINDV6ONLY);
   1327 						break;
   1328 #endif
   1329 					}
   1330 				}
   1331 				break;
   1332 #undef OPTSET
   1333 
   1334 			case IPV6_MULTICAST_IF:
   1335 			case IPV6_MULTICAST_HOPS:
   1336 			case IPV6_MULTICAST_LOOP:
   1337 			case IPV6_JOIN_GROUP:
   1338 			case IPV6_LEAVE_GROUP:
   1339 				error =	ip6_setmoptions(optname, &in6p->in6p_moptions, m);
   1340 				break;
   1341 
   1342 			case IPV6_PORTRANGE:
   1343 				optval = *mtod(m, int *);
   1344 
   1345 				switch (optval) {
   1346 				case IPV6_PORTRANGE_DEFAULT:
   1347 					in6p->in6p_flags &= ~(IN6P_LOWPORT);
   1348 					in6p->in6p_flags &= ~(IN6P_HIGHPORT);
   1349 					break;
   1350 
   1351 				case IPV6_PORTRANGE_HIGH:
   1352 					in6p->in6p_flags &= ~(IN6P_LOWPORT);
   1353 					in6p->in6p_flags |= IN6P_HIGHPORT;
   1354 					break;
   1355 
   1356 				case IPV6_PORTRANGE_LOW:
   1357 					in6p->in6p_flags &= ~(IN6P_HIGHPORT);
   1358 					in6p->in6p_flags |= IN6P_LOWPORT;
   1359 					break;
   1360 
   1361 				default:
   1362 					error = EINVAL;
   1363 					break;
   1364 				}
   1365 				break;
   1366 
   1367 #ifdef IPSEC
   1368 			case IPV6_IPSEC_POLICY:
   1369 			    {
   1370 				caddr_t req = NULL;
   1371 				size_t len = 0;
   1372 
   1373 				int priv = 0;
   1374 				if (p == 0 || suser(p->p_ucred, &p->p_acflag))
   1375 					priv = 0;
   1376 				else
   1377 					priv = 1;
   1378 				if (m) {
   1379 					req = mtod(m, caddr_t);
   1380 					len = m->m_len;
   1381 				}
   1382 				error = ipsec6_set_policy(in6p,
   1383 				                   optname, req, len, priv);
   1384 			    }
   1385 				break;
   1386 #endif /* IPSEC */
   1387 
   1388 			default:
   1389 				error = ENOPROTOOPT;
   1390 				break;
   1391 			}
   1392 			if (m)
   1393 				(void)m_free(m);
   1394 			break;
   1395 
   1396 		case PRCO_GETOPT:
   1397 			switch (optname) {
   1398 
   1399 			case IPV6_OPTIONS:
   1400 			case IPV6_RETOPTS:
   1401 #if 0
   1402 				*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1403 				if (in6p->in6p_options) {
   1404 					m->m_len = in6p->in6p_options->m_len;
   1405 					bcopy(mtod(in6p->in6p_options, caddr_t),
   1406 					      mtod(m, caddr_t),
   1407 					      (unsigned)m->m_len);
   1408 				} else
   1409 					m->m_len = 0;
   1410 				break;
   1411 #else
   1412 				error = ENOPROTOOPT;
   1413 				break;
   1414 #endif
   1415 
   1416 			case IPV6_PKTOPTIONS:
   1417 				if (in6p->in6p_options) {
   1418 					*mp = m_copym(in6p->in6p_options, 0,
   1419 						      M_COPYALL, M_WAIT);
   1420 				} else {
   1421 					*mp = m_get(M_WAIT, MT_SOOPTS);
   1422 					(*mp)->m_len = 0;
   1423 				}
   1424 				break;
   1425 
   1426 			case IPV6_HOPOPTS:
   1427 			case IPV6_DSTOPTS:
   1428 				if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
   1429 					error = EPERM;
   1430 					break;
   1431 				}
   1432 				/* fall through */
   1433 			case IPV6_UNICAST_HOPS:
   1434 			case IPV6_RECVOPTS:
   1435 			case IPV6_RECVRETOPTS:
   1436 			case IPV6_RECVDSTADDR:
   1437 			case IPV6_PORTRANGE:
   1438 			case IPV6_PKTINFO:
   1439 			case IPV6_HOPLIMIT:
   1440 			case IPV6_RTHDR:
   1441 			case IPV6_CHECKSUM:
   1442 			case IPV6_FAITH:
   1443 #ifndef INET6_BINDV6ONLY
   1444 			case IPV6_BINDV6ONLY:
   1445 #endif
   1446 				*mp = m = m_get(M_WAIT, MT_SOOPTS);
   1447 				m->m_len = sizeof(int);
   1448 				switch (optname) {
   1449 
   1450 				case IPV6_UNICAST_HOPS:
   1451 					optval = in6p->in6p_hops;
   1452 					break;
   1453 
   1454 #define OPTBIT(bit) (in6p->in6p_flags & bit ? 1 : 0)
   1455 
   1456 				case IPV6_RECVOPTS:
   1457 					optval = OPTBIT(IN6P_RECVOPTS);
   1458 					break;
   1459 
   1460 				case IPV6_RECVRETOPTS:
   1461 					optval = OPTBIT(IN6P_RECVRETOPTS);
   1462 					break;
   1463 
   1464 				case IPV6_RECVDSTADDR:
   1465 					optval = OPTBIT(IN6P_RECVDSTADDR);
   1466 					break;
   1467 
   1468 				case IPV6_PORTRANGE:
   1469 				    {
   1470 					int flags;
   1471 					flags = in6p->in6p_flags;
   1472 					if (flags & IN6P_HIGHPORT)
   1473 						optval = IPV6_PORTRANGE_HIGH;
   1474 					else if (flags & IN6P_LOWPORT)
   1475 						optval = IPV6_PORTRANGE_LOW;
   1476 					else
   1477 						optval = 0;
   1478 					break;
   1479 				    }
   1480 
   1481 				case IPV6_PKTINFO:
   1482 					optval = OPTBIT(IN6P_PKTINFO);
   1483 					break;
   1484 
   1485 				case IPV6_HOPLIMIT:
   1486 					optval = OPTBIT(IN6P_HOPLIMIT);
   1487 					break;
   1488 
   1489 				case IPV6_HOPOPTS:
   1490 					optval = OPTBIT(IN6P_HOPOPTS);
   1491 					break;
   1492 
   1493 				case IPV6_DSTOPTS:
   1494 					optval = OPTBIT(IN6P_DSTOPTS);
   1495 					break;
   1496 
   1497 				case IPV6_RTHDR:
   1498 					optval = OPTBIT(IN6P_RTHDR);
   1499 					break;
   1500 
   1501 				case IPV6_CHECKSUM:
   1502 					optval = in6p->in6p_cksum;
   1503 					break;
   1504 
   1505 				case IPV6_FAITH:
   1506 					optval = OPTBIT(IN6P_FAITH);
   1507 					break;
   1508 
   1509 #ifndef INET6_BINDV6ONLY
   1510 				case IPV6_BINDV6ONLY:
   1511 					optval = OPTBIT(IN6P_BINDV6ONLY);
   1512 					break;
   1513 #endif
   1514 				}
   1515 				*mtod(m, int *) = optval;
   1516 				break;
   1517 
   1518 			case IPV6_MULTICAST_IF:
   1519 			case IPV6_MULTICAST_HOPS:
   1520 			case IPV6_MULTICAST_LOOP:
   1521 			case IPV6_JOIN_GROUP:
   1522 			case IPV6_LEAVE_GROUP:
   1523 				error = ip6_getmoptions(optname, in6p->in6p_moptions, mp);
   1524 				break;
   1525 
   1526 #ifdef IPSEC
   1527 			case IPV6_IPSEC_POLICY:
   1528 			{
   1529 				caddr_t req = NULL;
   1530 				size_t len = 0;
   1531 
   1532 				if (m) {
   1533 					req = mtod(m, caddr_t);
   1534 					len = m->m_len;
   1535 				}
   1536 				error = ipsec6_get_policy(in6p, req, len, mp);
   1537 				break;
   1538 			}
   1539 #endif /* IPSEC */
   1540 
   1541 			default:
   1542 				error = ENOPROTOOPT;
   1543 				break;
   1544 			}
   1545 			break;
   1546 		}
   1547 	} else {
   1548 		error = EINVAL;
   1549 		if (op == PRCO_SETOPT && *mp)
   1550 			(void)m_free(*mp);
   1551 	}
   1552 	return(error);
   1553 }
   1554 
   1555 /*
   1556  * Set up IP6 options in pcb for insertion in output packets.
   1557  * Store in mbuf with pointer in pcbopt, adding pseudo-option
   1558  * with destination address if source routed.
   1559  */
   1560 static int
   1561 ip6_pcbopts(pktopt, m, so)
   1562 	struct ip6_pktopts **pktopt;
   1563 	register struct mbuf *m;
   1564 	struct socket *so;
   1565 {
   1566 	register struct ip6_pktopts *opt = *pktopt;
   1567 	int error = 0;
   1568 	struct proc *p = curproc;	/* XXX */
   1569 	int priv = 0;
   1570 
   1571 	/* turn off any old options. */
   1572 	if (opt) {
   1573 		if (opt->ip6po_m)
   1574 			(void)m_free(opt->ip6po_m);
   1575 	} else
   1576 		opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
   1577 	*pktopt = 0;
   1578 
   1579 	if (!m || m->m_len == 0) {
   1580 		/*
   1581 		 * Only turning off any previous options.
   1582 		 */
   1583 		if (opt)
   1584 			free(opt, M_IP6OPT);
   1585 		if (m)
   1586 			(void)m_free(m);
   1587 		return(0);
   1588 	}
   1589 
   1590 	/*  set options specified by user. */
   1591 	if (p && !suser(p->p_ucred, &p->p_acflag))
   1592 		priv = 1;
   1593 	if ((error = ip6_setpktoptions(m, opt, priv)) != 0) {
   1594 		(void)m_free(m);
   1595 		return(error);
   1596 	}
   1597 	*pktopt = opt;
   1598 	return(0);
   1599 }
   1600 
   1601 /*
   1602  * Set the IP6 multicast options in response to user setsockopt().
   1603  */
   1604 static int
   1605 ip6_setmoptions(optname, im6op, m)
   1606 	int optname;
   1607 	struct ip6_moptions **im6op;
   1608 	struct mbuf *m;
   1609 {
   1610 	int error = 0;
   1611 	u_int loop, ifindex;
   1612 	struct ipv6_mreq *mreq;
   1613 	struct ifnet *ifp;
   1614 	struct ip6_moptions *im6o = *im6op;
   1615 	struct route_in6 ro;
   1616 	struct sockaddr_in6 *dst;
   1617 	struct in6_multi_mship *imm;
   1618 	struct proc *p = curproc;	/* XXX */
   1619 
   1620 	if (im6o == NULL) {
   1621 		/*
   1622 		 * No multicast option buffer attached to the pcb;
   1623 		 * allocate one and initialize to default values.
   1624 		 */
   1625 		im6o = (struct ip6_moptions *)
   1626 			malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
   1627 
   1628 		if (im6o == NULL)
   1629 			return(ENOBUFS);
   1630 		*im6op = im6o;
   1631 		im6o->im6o_multicast_ifp = NULL;
   1632 		im6o->im6o_multicast_hlim = ip6_defmcasthlim;
   1633 		im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
   1634 		LIST_INIT(&im6o->im6o_memberships);
   1635 	}
   1636 
   1637 	switch (optname) {
   1638 
   1639 	case IPV6_MULTICAST_IF:
   1640 		/*
   1641 		 * Select the interface for outgoing multicast packets.
   1642 		 */
   1643 		if (m == NULL || m->m_len != sizeof(u_int)) {
   1644 			error = EINVAL;
   1645 			break;
   1646 		}
   1647 		bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
   1648 		if (ifindex < 0 || if_index < ifindex) {
   1649 			error = ENXIO;	/* XXX EINVAL? */
   1650 			break;
   1651 		}
   1652 		ifp = ifindex2ifnet[ifindex];
   1653 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1654 			error = EADDRNOTAVAIL;
   1655 			break;
   1656 		}
   1657 		im6o->im6o_multicast_ifp = ifp;
   1658 		break;
   1659 
   1660 	case IPV6_MULTICAST_HOPS:
   1661 	    {
   1662 		/*
   1663 		 * Set the IP6 hoplimit for outgoing multicast packets.
   1664 		 */
   1665 		int optval;
   1666 		if (m == NULL || m->m_len != sizeof(int)) {
   1667 			error = EINVAL;
   1668 			break;
   1669 		}
   1670 		bcopy(mtod(m, u_int *), &optval, sizeof(optval));
   1671 		if (optval < -1 || optval >= 256)
   1672 			error = EINVAL;
   1673 		else if (optval == -1)
   1674 			im6o->im6o_multicast_hlim = ip6_defmcasthlim;
   1675 		else
   1676 			im6o->im6o_multicast_hlim = optval;
   1677 		break;
   1678 	    }
   1679 
   1680 	case IPV6_MULTICAST_LOOP:
   1681 		/*
   1682 		 * Set the loopback flag for outgoing multicast packets.
   1683 		 * Must be zero or one.
   1684 		 */
   1685 		if (m == NULL || m->m_len != sizeof(u_int)) {
   1686 			error = EINVAL;
   1687 			break;
   1688 		}
   1689 		bcopy(mtod(m, u_int *), &loop, sizeof(loop));
   1690 		if (loop > 1) {
   1691 			error = EINVAL;
   1692 			break;
   1693 		}
   1694 		im6o->im6o_multicast_loop = loop;
   1695 		break;
   1696 
   1697 	case IPV6_JOIN_GROUP:
   1698 		/*
   1699 		 * Add a multicast group membership.
   1700 		 * Group must be a valid IP6 multicast address.
   1701 		 */
   1702 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
   1703 			error = EINVAL;
   1704 			break;
   1705 		}
   1706 		mreq = mtod(m, struct ipv6_mreq *);
   1707 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
   1708 			/*
   1709 			 * We use the unspecified address to specify to accept
   1710 			 * all multicast addresses. Only super user is allowed
   1711 			 * to do this.
   1712 			 */
   1713 			if (suser(p->p_ucred, &p->p_acflag)) {
   1714 				error = EACCES;
   1715 				break;
   1716 			}
   1717 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
   1718 			error = EINVAL;
   1719 			break;
   1720 		}
   1721 
   1722 		/*
   1723 		 * If the interface is specified, validate it.
   1724 		 */
   1725 		if (mreq->ipv6mr_interface < 0
   1726 		 || if_index < mreq->ipv6mr_interface) {
   1727 			error = ENXIO;	/* XXX EINVAL? */
   1728 			break;
   1729 		}
   1730 		/*
   1731 		 * If no interface was explicitly specified, choose an
   1732 		 * appropriate one according to the given multicast address.
   1733 		 */
   1734 		if (mreq->ipv6mr_interface == 0) {
   1735 			/*
   1736 			 * If the multicast address is in node-local scope,
   1737 			 * the interface should be a loopback interface.
   1738 			 * Otherwise, look up the routing table for the
   1739 			 * address, and choose the outgoing interface.
   1740 			 *   XXX: is it a good approach?
   1741 			 */
   1742 			if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
   1743 				ifp = &loif[0];
   1744 			} else {
   1745 				ro.ro_rt = NULL;
   1746 				dst = (struct sockaddr_in6 *)&ro.ro_dst;
   1747 				bzero(dst, sizeof(*dst));
   1748 				dst->sin6_len = sizeof(struct sockaddr_in6);
   1749 				dst->sin6_family = AF_INET6;
   1750 				dst->sin6_addr = mreq->ipv6mr_multiaddr;
   1751 				rtalloc((struct route *)&ro);
   1752 				if (ro.ro_rt == NULL) {
   1753 					error = EADDRNOTAVAIL;
   1754 					break;
   1755 				}
   1756 				ifp = ro.ro_rt->rt_ifp;
   1757 				rtfree(ro.ro_rt);
   1758 			}
   1759 		} else
   1760 			ifp = ifindex2ifnet[mreq->ipv6mr_interface];
   1761 
   1762 		/*
   1763 		 * See if we found an interface, and confirm that it
   1764 		 * supports multicast
   1765 		 */
   1766 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1767 			error = EADDRNOTAVAIL;
   1768 			break;
   1769 		}
   1770 		/*
   1771 		 * Put interface index into the multicast address,
   1772 		 * if the address has link-local scope.
   1773 		 */
   1774 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
   1775 			mreq->ipv6mr_multiaddr.s6_addr16[1]
   1776 				= htons(mreq->ipv6mr_interface);
   1777 		}
   1778 		/*
   1779 		 * See if the membership already exists.
   1780 		 */
   1781 		for (imm = im6o->im6o_memberships.lh_first;
   1782 		     imm != NULL; imm = imm->i6mm_chain.le_next)
   1783 			if (imm->i6mm_maddr->in6m_ifp == ifp &&
   1784 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
   1785 					       &mreq->ipv6mr_multiaddr))
   1786 				break;
   1787 		if (imm != NULL) {
   1788 			error = EADDRINUSE;
   1789 			break;
   1790 		}
   1791 		/*
   1792 		 * Everything looks good; add a new record to the multicast
   1793 		 * address list for the given interface.
   1794 		 */
   1795 		imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
   1796 		if (imm == NULL) {
   1797 			error = ENOBUFS;
   1798 			break;
   1799 		}
   1800 		if ((imm->i6mm_maddr =
   1801 		     in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
   1802 			free(imm, M_IPMADDR);
   1803 			break;
   1804 		}
   1805 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
   1806 		break;
   1807 
   1808 	case IPV6_LEAVE_GROUP:
   1809 		/*
   1810 		 * Drop a multicast group membership.
   1811 		 * Group must be a valid IP6 multicast address.
   1812 		 */
   1813 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
   1814 			error = EINVAL;
   1815 			break;
   1816 		}
   1817 		mreq = mtod(m, struct ipv6_mreq *);
   1818 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
   1819 			if (suser(p->p_ucred, &p->p_acflag)) {
   1820 				error = EACCES;
   1821 				break;
   1822 			}
   1823 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
   1824 			error = EINVAL;
   1825 			break;
   1826 		}
   1827 		/*
   1828 		 * If an interface address was specified, get a pointer
   1829 		 * to its ifnet structure.
   1830 		 */
   1831 		if (mreq->ipv6mr_interface < 0
   1832 		 || if_index < mreq->ipv6mr_interface) {
   1833 			error = ENXIO;	/* XXX EINVAL? */
   1834 			break;
   1835 		}
   1836 		ifp = ifindex2ifnet[mreq->ipv6mr_interface];
   1837 		/*
   1838 		 * Put interface index into the multicast address,
   1839 		 * if the address has link-local scope.
   1840 		 */
   1841 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
   1842 			mreq->ipv6mr_multiaddr.s6_addr16[1]
   1843 				= htons(mreq->ipv6mr_interface);
   1844 		}
   1845 		/*
   1846 		 * Find the membership in the membership list.
   1847 		 */
   1848 		for (imm = im6o->im6o_memberships.lh_first;
   1849 		     imm != NULL; imm = imm->i6mm_chain.le_next) {
   1850 			if ((ifp == NULL ||
   1851 			     imm->i6mm_maddr->in6m_ifp == ifp) &&
   1852 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
   1853 					       &mreq->ipv6mr_multiaddr))
   1854 				break;
   1855 		}
   1856 		if (imm == NULL) {
   1857 			/* Unable to resolve interface */
   1858 			error = EADDRNOTAVAIL;
   1859 			break;
   1860 		}
   1861 		/*
   1862 		 * Give up the multicast address record to which the
   1863 		 * membership points.
   1864 		 */
   1865 		LIST_REMOVE(imm, i6mm_chain);
   1866 		in6_delmulti(imm->i6mm_maddr);
   1867 		free(imm, M_IPMADDR);
   1868 		break;
   1869 
   1870 	default:
   1871 		error = EOPNOTSUPP;
   1872 		break;
   1873 	}
   1874 
   1875 	/*
   1876 	 * If all options have default values, no need to keep the mbuf.
   1877 	 */
   1878 	if (im6o->im6o_multicast_ifp == NULL &&
   1879 	    im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
   1880 	    im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
   1881 	    im6o->im6o_memberships.lh_first == NULL) {
   1882 		free(*im6op, M_IPMOPTS);
   1883 		*im6op = NULL;
   1884 	}
   1885 
   1886 	return(error);
   1887 }
   1888 
   1889 /*
   1890  * Return the IP6 multicast options in response to user getsockopt().
   1891  */
   1892 static int
   1893 ip6_getmoptions(optname, im6o, mp)
   1894 	int optname;
   1895 	register struct ip6_moptions *im6o;
   1896 	register struct mbuf **mp;
   1897 {
   1898 	u_int *hlim, *loop, *ifindex;
   1899 
   1900 	*mp = m_get(M_WAIT, MT_SOOPTS);
   1901 
   1902 	switch (optname) {
   1903 
   1904 	case IPV6_MULTICAST_IF:
   1905 		ifindex = mtod(*mp, u_int *);
   1906 		(*mp)->m_len = sizeof(u_int);
   1907 		if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
   1908 			*ifindex = 0;
   1909 		else
   1910 			*ifindex = im6o->im6o_multicast_ifp->if_index;
   1911 		return(0);
   1912 
   1913 	case IPV6_MULTICAST_HOPS:
   1914 		hlim = mtod(*mp, u_int *);
   1915 		(*mp)->m_len = sizeof(u_int);
   1916 		if (im6o == NULL)
   1917 			*hlim = ip6_defmcasthlim;
   1918 		else
   1919 			*hlim = im6o->im6o_multicast_hlim;
   1920 		return(0);
   1921 
   1922 	case IPV6_MULTICAST_LOOP:
   1923 		loop = mtod(*mp, u_int *);
   1924 		(*mp)->m_len = sizeof(u_int);
   1925 		if (im6o == NULL)
   1926 			*loop = ip6_defmcasthlim;
   1927 		else
   1928 			*loop = im6o->im6o_multicast_loop;
   1929 		return(0);
   1930 
   1931 	default:
   1932 		return(EOPNOTSUPP);
   1933 	}
   1934 }
   1935 
   1936 /*
   1937  * Discard the IP6 multicast options.
   1938  */
   1939 void
   1940 ip6_freemoptions(im6o)
   1941 	register struct ip6_moptions *im6o;
   1942 {
   1943 	struct in6_multi_mship *imm;
   1944 
   1945 	if (im6o == NULL)
   1946 		return;
   1947 
   1948 	while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
   1949 		LIST_REMOVE(imm, i6mm_chain);
   1950 		if (imm->i6mm_maddr)
   1951 			in6_delmulti(imm->i6mm_maddr);
   1952 		free(imm, M_IPMADDR);
   1953 	}
   1954 	free(im6o, M_IPMOPTS);
   1955 }
   1956 
   1957 /*
   1958  * Set IPv6 outgoing packet options based on advanced API.
   1959  */
   1960 int
   1961 ip6_setpktoptions(control, opt, priv)
   1962 	struct mbuf *control;
   1963 	struct ip6_pktopts *opt;
   1964 	int priv;
   1965 {
   1966 	register struct cmsghdr *cm = 0;
   1967 
   1968 	if (control == 0 || opt == 0)
   1969 		return(EINVAL);
   1970 
   1971 	bzero(opt, sizeof(*opt));
   1972 	opt->ip6po_hlim = -1; /* -1 means to use default hop limit */
   1973 
   1974 	/*
   1975 	 * XXX: Currently, we assume all the optional information is stored
   1976 	 * in a single mbuf.
   1977 	 */
   1978 	if (control->m_next)
   1979 		return(EINVAL);
   1980 
   1981 	opt->ip6po_m = control;
   1982 
   1983 	for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
   1984 		     control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
   1985 		cm = mtod(control, struct cmsghdr *);
   1986 		if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
   1987 			return(EINVAL);
   1988 		if (cm->cmsg_level != IPPROTO_IPV6)
   1989 			continue;
   1990 
   1991 		switch(cm->cmsg_type) {
   1992 		case IPV6_PKTINFO:
   1993 			if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
   1994 				return(EINVAL);
   1995 			opt->ip6po_pktinfo = (struct in6_pktinfo *)CMSG_DATA(cm);
   1996 			if (opt->ip6po_pktinfo->ipi6_ifindex &&
   1997 			    IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
   1998 				opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
   1999 					htons(opt->ip6po_pktinfo->ipi6_ifindex);
   2000 
   2001 			if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
   2002 			 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
   2003 				return(ENXIO);
   2004 			}
   2005 
   2006 			/*
   2007 			 * Check if the requested source address is indeed a
   2008 			 * unicast address assigned to the node.
   2009 			 */
   2010 			if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
   2011 				struct ifaddr *ia;
   2012 				struct sockaddr_in6 sin6;
   2013 
   2014 				bzero(&sin6, sizeof(sin6));
   2015 				sin6.sin6_len = sizeof(sin6);
   2016 				sin6.sin6_family = AF_INET6;
   2017 				sin6.sin6_addr =
   2018 					opt->ip6po_pktinfo->ipi6_addr;
   2019 				ia = ifa_ifwithaddr(sin6tosa(&sin6));
   2020 				if (ia == NULL ||
   2021 				    (opt->ip6po_pktinfo->ipi6_ifindex &&
   2022 				     (ia->ifa_ifp->if_index !=
   2023 				      opt->ip6po_pktinfo->ipi6_ifindex))) {
   2024 					return(EADDRNOTAVAIL);
   2025 				}
   2026 				/*
   2027 				 * Check if the requested source address is
   2028 				 * indeed a unicast address assigned to the
   2029 				 * node.
   2030 				 */
   2031 				if (IN6_IS_ADDR_MULTICAST(&opt->ip6po_pktinfo->ipi6_addr))
   2032 					return(EADDRNOTAVAIL);
   2033 			}
   2034 			break;
   2035 
   2036 		case IPV6_HOPLIMIT:
   2037 			if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
   2038 				return(EINVAL);
   2039 
   2040 			bcopy(CMSG_DATA(cm), &opt->ip6po_hlim,
   2041 			    sizeof(opt->ip6po_hlim));
   2042 			if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
   2043 				return(EINVAL);
   2044 			break;
   2045 
   2046 		case IPV6_NEXTHOP:
   2047 			if (!priv)
   2048 				return(EPERM);
   2049 
   2050 			if (cm->cmsg_len < sizeof(u_char) ||
   2051 			    /* check if cmsg_len is large enough for sa_len */
   2052 			    cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
   2053 				return(EINVAL);
   2054 
   2055 			opt->ip6po_nexthop = (struct sockaddr *)CMSG_DATA(cm);
   2056 
   2057 			break;
   2058 
   2059 		case IPV6_HOPOPTS:
   2060 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
   2061 				return(EINVAL);
   2062 			opt->ip6po_hbh = (struct ip6_hbh *)CMSG_DATA(cm);
   2063 			if (cm->cmsg_len !=
   2064 			    CMSG_LEN((opt->ip6po_hbh->ip6h_len + 1) << 3))
   2065 				return(EINVAL);
   2066 			break;
   2067 
   2068 		case IPV6_DSTOPTS:
   2069 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
   2070 				return(EINVAL);
   2071 
   2072 			/*
   2073 			 * If there is no routing header yet, the destination
   2074 			 * options header should be put on the 1st part.
   2075 			 * Otherwise, the header should be on the 2nd part.
   2076 			 * (See RFC 2460, section 4.1)
   2077 			 */
   2078 			if (opt->ip6po_rthdr == NULL) {
   2079 				opt->ip6po_dest1 =
   2080 					(struct ip6_dest *)CMSG_DATA(cm);
   2081 				if (cm->cmsg_len !=
   2082 				    CMSG_LEN((opt->ip6po_dest1->ip6d_len + 1)
   2083 					     << 3))
   2084 					return(EINVAL);
   2085 			}
   2086 			else {
   2087 				opt->ip6po_dest2 =
   2088 					(struct ip6_dest *)CMSG_DATA(cm);
   2089 				if (cm->cmsg_len !=
   2090 				    CMSG_LEN((opt->ip6po_dest2->ip6d_len + 1)
   2091 					     << 3))
   2092 					return(EINVAL);
   2093 			}
   2094 			break;
   2095 
   2096 		case IPV6_RTHDR:
   2097 			if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
   2098 				return(EINVAL);
   2099 			opt->ip6po_rthdr = (struct ip6_rthdr *)CMSG_DATA(cm);
   2100 			if (cm->cmsg_len !=
   2101 			    CMSG_LEN((opt->ip6po_rthdr->ip6r_len + 1) << 3))
   2102 				return(EINVAL);
   2103 			switch(opt->ip6po_rthdr->ip6r_type) {
   2104 			case IPV6_RTHDR_TYPE_0:
   2105 				if (opt->ip6po_rthdr->ip6r_segleft == 0)
   2106 					return(EINVAL);
   2107 				break;
   2108 			default:
   2109 				return(EINVAL);
   2110 			}
   2111 			break;
   2112 
   2113 		default:
   2114 			return(ENOPROTOOPT);
   2115 		}
   2116 	}
   2117 
   2118 	return(0);
   2119 }
   2120 
   2121 /*
   2122  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
   2123  * packet to the input queue of a specified interface.  Note that this
   2124  * calls the output routine of the loopback "driver", but with an interface
   2125  * pointer that might NOT be &loif -- easier than replicating that code here.
   2126  */
   2127 void
   2128 ip6_mloopback(ifp, m, dst)
   2129 	struct ifnet *ifp;
   2130 	register struct mbuf *m;
   2131 	register struct sockaddr_in6 *dst;
   2132 {
   2133 	struct mbuf *copym;
   2134 	struct ip6_hdr *ip6;
   2135 
   2136 	copym = m_copy(m, 0, M_COPYALL);
   2137 	if (copym == NULL)
   2138 		return;
   2139 
   2140 	/*
   2141 	 * Make sure to deep-copy IPv6 header portion in case the data
   2142 	 * is in an mbuf cluster, so that we can safely override the IPv6
   2143 	 * header portion later.
   2144 	 */
   2145 	if ((copym->m_flags & M_EXT) != 0 ||
   2146 	    copym->m_len < sizeof(struct ip6_hdr)) {
   2147 		copym = m_pullup(copym, sizeof(struct ip6_hdr));
   2148 		if (copym == NULL)
   2149 			return;
   2150 	}
   2151 
   2152 #ifdef DIAGNOSTIC
   2153 	if (copym->m_len < sizeof(*ip6)) {
   2154 		m_freem(copym);
   2155 		return;
   2156 	}
   2157 #endif
   2158 
   2159 #ifndef FAKE_LOOPBACK_IF
   2160 	if ((ifp->if_flags & IFF_LOOPBACK) == 0)
   2161 #else
   2162 	if (1)
   2163 #endif
   2164 	{
   2165 		ip6 = mtod(copym, struct ip6_hdr *);
   2166 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
   2167 			ip6->ip6_src.s6_addr16[1] = 0;
   2168 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
   2169 			ip6->ip6_dst.s6_addr16[1] = 0;
   2170 	}
   2171 
   2172 	(void)looutput(ifp, copym, (struct sockaddr *)dst, NULL);
   2173 }
   2174 
   2175 /*
   2176  * Chop IPv6 header off from the payload.
   2177  */
   2178 static int
   2179 ip6_splithdr(m, exthdrs)
   2180 	struct mbuf *m;
   2181 	struct ip6_exthdrs *exthdrs;
   2182 {
   2183 	struct mbuf *mh;
   2184 	struct ip6_hdr *ip6;
   2185 
   2186 	ip6 = mtod(m, struct ip6_hdr *);
   2187 	if (m->m_len > sizeof(*ip6)) {
   2188 		MGETHDR(mh, M_DONTWAIT, MT_HEADER);
   2189 		if (mh == 0) {
   2190 			m_freem(m);
   2191 			return ENOBUFS;
   2192 		}
   2193 		M_COPY_PKTHDR(mh, m);
   2194 		MH_ALIGN(mh, sizeof(*ip6));
   2195 		m->m_flags &= ~M_PKTHDR;
   2196 		m->m_len -= sizeof(*ip6);
   2197 		m->m_data += sizeof(*ip6);
   2198 		mh->m_next = m;
   2199 		m = mh;
   2200 		m->m_len = sizeof(*ip6);
   2201 		bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
   2202 	}
   2203 	exthdrs->ip6e_ip6 = m;
   2204 	return 0;
   2205 }
   2206 
   2207 /*
   2208  * Compute IPv6 extension header length.
   2209  */
   2210 int
   2211 ip6_optlen(in6p)
   2212 	struct in6pcb *in6p;
   2213 {
   2214 	int len;
   2215 
   2216 	if (!in6p->in6p_outputopts)
   2217 		return 0;
   2218 
   2219 	len = 0;
   2220 #define elen(x) \
   2221     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
   2222 
   2223 	len += elen(in6p->in6p_outputopts->ip6po_hbh);
   2224 	len += elen(in6p->in6p_outputopts->ip6po_dest1);
   2225 	len += elen(in6p->in6p_outputopts->ip6po_rthdr);
   2226 	len += elen(in6p->in6p_outputopts->ip6po_dest2);
   2227 	return len;
   2228 #undef elen
   2229 }
   2230