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