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