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