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