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