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