xref: /dragonfly/sys/netinet6/ip6_output.c (revision cecb9aae)
1 /*	$FreeBSD: src/sys/netinet6/ip6_output.c,v 1.13.2.18 2003/01/24 05:11:35 sam Exp $	*/
2 /*	$KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1982, 1986, 1988, 1990, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
66  */
67 
68 #include "opt_ip6fw.h"
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
72 
73 #include <sys/param.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/errno.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/proc.h>
83 #include <sys/priv.h>
84 
85 #include <sys/thread2.h>
86 #include <sys/msgport2.h>
87 
88 #include <net/if.h>
89 #include <net/route.h>
90 #include <net/pfil.h>
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 #include <netinet6/ip6protosw.h>
101 
102 #ifdef IPSEC
103 #include <netinet6/ipsec.h>
104 #ifdef INET6
105 #include <netinet6/ipsec6.h>
106 #endif
107 #include <netproto/key/key.h>
108 #endif /* IPSEC */
109 
110 #ifdef FAST_IPSEC
111 #include <netproto/ipsec/ipsec.h>
112 #include <netproto/ipsec/ipsec6.h>
113 #include <netproto/ipsec/key.h>
114 #endif
115 
116 #include <net/ip6fw/ip6_fw.h>
117 
118 #include <net/net_osdep.h>
119 
120 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
121 
122 struct ip6_exthdrs {
123 	struct mbuf *ip6e_ip6;
124 	struct mbuf *ip6e_hbh;
125 	struct mbuf *ip6e_dest1;
126 	struct mbuf *ip6e_rthdr;
127 	struct mbuf *ip6e_dest2;
128 };
129 
130 static int ip6_pcbopt (int, u_char *, int, struct ip6_pktopts **, int);
131 static int ip6_setpktoption (int, u_char *, int, struct ip6_pktopts *,
132 	 int, int, int, int);
133 static int ip6_pcbopts (struct ip6_pktopts **, struct mbuf *,
134 			    struct socket *, struct sockopt *);
135 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
136 static int ip6_setmoptions (int, struct ip6_moptions **, struct mbuf *);
137 static int ip6_getmoptions (int, struct ip6_moptions *, struct mbuf **);
138 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
139 	struct ifnet *, struct in6_addr *, u_long *, int *);
140 static int copyexthdr (void *, struct mbuf **);
141 static int ip6_insertfraghdr (struct mbuf *, struct mbuf *, int,
142 				  struct ip6_frag **);
143 static int ip6_insert_jumboopt (struct ip6_exthdrs *, u_int32_t);
144 static struct mbuf *ip6_splithdr (struct mbuf *);
145 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
146 
147 /*
148  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
149  * header (with pri, len, nxt, hlim, src, dst).
150  * This function may modify ver and hlim only.
151  * The mbuf chain containing the packet will be freed.
152  * The mbuf opt, if present, will not be freed.
153  *
154  * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
155  * nd_ifinfo.linkmtu is u_int32_t.  so we use u_long to hold largest one,
156  * which is rt_rmx.rmx_mtu.
157  */
158 int
159 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro,
160 	   int flags, struct ip6_moptions *im6o,
161 	   struct ifnet **ifpp,		/* XXX: just for statistics */
162 	   struct inpcb *inp)
163 {
164 	struct ip6_hdr *ip6, *mhip6;
165 	struct ifnet *ifp, *origifp;
166 	struct mbuf *m = m0;
167 	struct mbuf *mprev;
168 	u_char *nexthdrp;
169 	int hlen, tlen, len, off;
170 	struct route_in6 ip6route;
171 	struct sockaddr_in6 *dst;
172 	int error = 0;
173 	struct in6_ifaddr *ia = NULL;
174 	u_long mtu;
175 	int alwaysfrag, dontfrag;
176 	u_int32_t optlen, plen = 0, unfragpartlen;
177 	struct ip6_exthdrs exthdrs;
178 	struct in6_addr finaldst;
179 	struct route_in6 *ro_pmtu = NULL;
180 	boolean_t hdrsplit = FALSE;
181 	boolean_t needipsec = FALSE;
182 #ifdef IPSEC
183 	boolean_t needipsectun = FALSE;
184 	struct secpolicy *sp = NULL;
185 	struct socket *so = inp ? inp->inp_socket : NULL;
186 
187 	ip6 = mtod(m, struct ip6_hdr *);
188 #endif
189 #ifdef FAST_IPSEC
190 	boolean_t needipsectun = FALSE;
191 	struct secpolicy *sp = NULL;
192 
193 	ip6 = mtod(m, struct ip6_hdr *);
194 #endif
195 
196 	bzero(&exthdrs, sizeof exthdrs);
197 
198 	if (opt) {
199 		if ((error = copyexthdr(opt->ip6po_hbh, &exthdrs.ip6e_hbh)))
200 			goto freehdrs;
201 		if ((error = copyexthdr(opt->ip6po_dest1, &exthdrs.ip6e_dest1)))
202 			goto freehdrs;
203 		if ((error = copyexthdr(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr)))
204 			goto freehdrs;
205 		if ((error = copyexthdr(opt->ip6po_dest2, &exthdrs.ip6e_dest2)))
206 			goto freehdrs;
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 = FALSE;
236 		break;
237 
238 	case IPSEC_POLICY_IPSEC:
239 		if (sp->req == NULL) {
240 			error = key_spdacquire(sp);	/* acquire a policy */
241 			goto freehdrs;
242 		}
243 		needipsec = TRUE;
244 		break;
245 
246 	case IPSEC_POLICY_ENTRUST:
247 	default:
248 		kprintf("ip6_output: Invalid policy found. %d\n", sp->policy);
249 	}
250 #endif /* IPSEC */
251 #ifdef FAST_IPSEC
252 	/* get a security policy for this packet */
253 	if (inp == NULL)
254 		sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
255 	else
256 		sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
257 
258 	if (sp == NULL) {
259 		newipsecstat.ips_out_inval++;
260 		goto freehdrs;
261 	}
262 
263 	error = 0;
264 
265 	/* check policy */
266 	switch (sp->policy) {
267 	case IPSEC_POLICY_DISCARD:
268 		/*
269 		 * This packet is just discarded.
270 		 */
271 		newipsecstat.ips_out_polvio++;
272 		goto freehdrs;
273 
274 	case IPSEC_POLICY_BYPASS:
275 	case IPSEC_POLICY_NONE:
276 		/* no need to do IPsec. */
277 		needipsec = FALSE;
278 		break;
279 
280 	case IPSEC_POLICY_IPSEC:
281 		if (sp->req == NULL) {
282 			error = key_spdacquire(sp);	/* acquire a policy */
283 			goto freehdrs;
284 		}
285 		needipsec = TRUE;
286 		break;
287 
288 	case IPSEC_POLICY_ENTRUST:
289 	default:
290 		kprintf("ip6_output: Invalid policy found. %d\n", sp->policy);
291 	}
292 #endif /* FAST_IPSEC */
293 
294 	/*
295 	 * Calculate the total length of the extension header chain.
296 	 * Keep the length of the unfragmentable part for fragmentation.
297 	 */
298 	optlen = m_lengthm(exthdrs.ip6e_hbh, NULL) +
299 	    m_lengthm(exthdrs.ip6e_dest1, NULL) +
300 	    m_lengthm(exthdrs.ip6e_rthdr, NULL);
301 
302 	unfragpartlen = optlen + sizeof(struct ip6_hdr);
303 
304 	/* NOTE: we don't add AH/ESP length here. do that later. */
305 	optlen += m_lengthm(exthdrs.ip6e_dest2, NULL);
306 
307 	/*
308 	 * If we need IPsec, or there is at least one extension header,
309 	 * separate IP6 header from the payload.
310 	 */
311 	if ((needipsec || optlen) && !hdrsplit) {
312 		exthdrs.ip6e_ip6 = ip6_splithdr(m);
313 		if (exthdrs.ip6e_ip6 == NULL) {
314 			error = ENOBUFS;
315 			goto freehdrs;
316 		}
317 		m = exthdrs.ip6e_ip6;
318 		hdrsplit = TRUE;
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 			exthdrs.ip6e_ip6 = ip6_splithdr(m);
332 			if (exthdrs.ip6e_ip6 == NULL) {
333 				error = ENOBUFS;
334 				goto freehdrs;
335 			}
336 			m = exthdrs.ip6e_ip6;
337 			hdrsplit = TRUE;
338 		}
339 		/* adjust pointer */
340 		ip6 = mtod(m, struct ip6_hdr *);
341 		if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
342 			goto freehdrs;
343 		ip6->ip6_plen = 0;
344 	} else
345 		ip6->ip6_plen = htons(plen);
346 
347 	/*
348 	 * Concatenate headers and fill in next header fields.
349 	 * Here we have, on "m"
350 	 *	IPv6 payload
351 	 * and we insert headers accordingly.  Finally, we should be getting:
352 	 *	IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
353 	 *
354 	 * during the header composing process, "m" points to IPv6 header.
355 	 * "mprev" points to an extension header prior to esp.
356 	 */
357 
358 	nexthdrp = &ip6->ip6_nxt;
359 	mprev = m;
360 
361 	/*
362 	 * we treat dest2 specially.  this makes IPsec processing
363 	 * much easier.  the goal here is to make mprev point the
364 	 * mbuf prior to dest2.
365 	 *
366 	 * result: IPv6 dest2 payload
367 	 * m and mprev will point to IPv6 header.
368 	 */
369 	if (exthdrs.ip6e_dest2) {
370 		if (!hdrsplit)
371 			panic("assumption failed: hdr not split");
372 		exthdrs.ip6e_dest2->m_next = m->m_next;
373 		m->m_next = exthdrs.ip6e_dest2;
374 		*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
375 		ip6->ip6_nxt = IPPROTO_DSTOPTS;
376 	}
377 
378 /*
379  * Place m1 after mprev.
380  */
381 #define MAKE_CHAIN(m1, mprev, nexthdrp, i)\
382     do {\
383 	if (m1) {\
384 		if (!hdrsplit)\
385 			panic("assumption failed: hdr not split");\
386 		*mtod(m1, u_char *) = *nexthdrp;\
387 		*nexthdrp = (i);\
388 		nexthdrp = mtod(m1, u_char *);\
389 		m1->m_next = mprev->m_next;\
390 		mprev->m_next = m1;\
391 		mprev = m1;\
392 	}\
393     } while (0)
394 
395 	/*
396 	 * result: IPv6 hbh dest1 rthdr dest2 payload
397 	 * m will point to IPv6 header.  mprev will point to the
398 	 * extension header prior to dest2 (rthdr in the above case).
399 	 */
400 	MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
401 	MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, IPPROTO_DSTOPTS);
402 	MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, IPPROTO_ROUTING);
403 
404 #if defined(IPSEC) || defined(FAST_IPSEC)
405 	if (needipsec) {
406 		struct ipsec_output_state state;
407 		int segleft_org = 0;
408 		struct ip6_rthdr *rh = NULL;
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 		if (exthdrs.ip6e_rthdr) {
418 			rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
419 			segleft_org = rh->ip6r_segleft;
420 			rh->ip6r_segleft = 0;
421 		}
422 
423 		bzero(&state, sizeof state);
424 		state.m = m;
425 		error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
426 					    &needipsectun);
427 		m = state.m;
428 		if (error) {
429 			/* mbuf is already reclaimed in ipsec6_output_trans. */
430 			m = NULL;
431 			switch (error) {
432 			case EHOSTUNREACH:
433 			case ENETUNREACH:
434 			case EMSGSIZE:
435 			case ENOBUFS:
436 			case ENOMEM:
437 				break;
438 			default:
439 				kprintf("ip6_output (ipsec): error code %d\n",
440 				       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 #endif
455 
456 	/*
457 	 * If there is a routing header, replace destination address field
458 	 * with the first hop of the routing header.
459 	 */
460 	if (exthdrs.ip6e_rthdr) {
461 		struct ip6_rthdr *rh;
462 
463 		finaldst = ip6->ip6_dst;
464 		rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
465 		switch (rh->ip6r_type) {
466 		default:	/* is it possible? */
467 			 error = EINVAL;
468 			 goto bad;
469 		}
470 	}
471 
472 	/* Source address validation */
473 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
474 	    !(flags & IPV6_DADOUTPUT)) {
475 		error = EOPNOTSUPP;
476 		ip6stat.ip6s_badscope++;
477 		goto bad;
478 	}
479 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
480 		error = EOPNOTSUPP;
481 		ip6stat.ip6s_badscope++;
482 		goto bad;
483 	}
484 
485 	ip6stat.ip6s_localout++;
486 
487 	/*
488 	 * Route packet.
489 	 */
490 	if (ro == NULL) {
491 		ro = &ip6route;
492 		bzero(ro, sizeof(*ro));
493 	}
494 	ro_pmtu = ro;
495 	if (opt && opt->ip6po_rthdr)
496 		ro = &opt->ip6po_route;
497 	dst = (struct sockaddr_in6 *)&ro->ro_dst;
498 	/*
499 	 * If there is a cached route,
500 	 * check that it is to the same destination
501 	 * and is still up. If not, free it and try again.
502 	 */
503 	if (ro->ro_rt != NULL &&
504 	    (!(ro->ro_rt->rt_flags & RTF_UP) || dst->sin6_family != AF_INET6 ||
505 	     !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
506 		RTFREE(ro->ro_rt);
507 		ro->ro_rt = NULL;
508 	}
509 	if (ro->ro_rt == NULL) {
510 		bzero(dst, sizeof(*dst));
511 		dst->sin6_family = AF_INET6;
512 		dst->sin6_len = sizeof(struct sockaddr_in6);
513 		dst->sin6_addr = ip6->ip6_dst;
514 	}
515 #if defined(IPSEC) || defined(FAST_IPSEC)
516 	if (needipsec && needipsectun) {
517 		struct ipsec_output_state state;
518 
519 		/*
520 		 * All the extension headers will become inaccessible
521 		 * (since they can be encrypted).
522 		 * Don't panic, we need no more updates to extension headers
523 		 * on inner IPv6 packet (since they are now encapsulated).
524 		 *
525 		 * IPv6 [ESP|AH] IPv6 [extension headers] payload
526 		 */
527 		bzero(&exthdrs, sizeof(exthdrs));
528 		exthdrs.ip6e_ip6 = m;
529 
530 		bzero(&state, sizeof(state));
531 		state.m = m;
532 		state.ro = (struct route *)ro;
533 		state.dst = (struct sockaddr *)dst;
534 
535 		error = ipsec6_output_tunnel(&state, sp, flags);
536 
537 		m = state.m;
538 		ro = (struct route_in6 *)state.ro;
539 		dst = (struct sockaddr_in6 *)state.dst;
540 		if (error) {
541 			/* mbuf is already reclaimed in ipsec6_output_tunnel. */
542 			m0 = m = NULL;
543 			m = NULL;
544 			switch (error) {
545 			case EHOSTUNREACH:
546 			case ENETUNREACH:
547 			case EMSGSIZE:
548 			case ENOBUFS:
549 			case ENOMEM:
550 				break;
551 			default:
552 				kprintf("ip6_output (ipsec): error code %d\n", error);
553 				/* fall through */
554 			case ENOENT:
555 				/* don't show these error codes to the user */
556 				error = 0;
557 				break;
558 			}
559 			goto bad;
560 		}
561 
562 		exthdrs.ip6e_ip6 = m;
563 	}
564 #endif
565 
566 	if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
567 		/* Unicast */
568 
569 #define ifatoia6(ifa)	((struct in6_ifaddr *)(ifa))
570 #define sin6tosa(sin6)	((struct sockaddr *)(sin6))
571 		/* xxx
572 		 * interface selection comes here
573 		 * if an interface is specified from an upper layer,
574 		 * ifp must point it.
575 		 */
576 		if (ro->ro_rt == NULL) {
577 			/*
578 			 * non-bsdi always clone routes, if parent is
579 			 * PRF_CLONING.
580 			 */
581 			rtalloc((struct route *)ro);
582 		}
583 		if (ro->ro_rt == NULL) {
584 			ip6stat.ip6s_noroute++;
585 			error = EHOSTUNREACH;
586 			/* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
587 			goto bad;
588 		}
589 		ia = ifatoia6(ro->ro_rt->rt_ifa);
590 		ifp = ro->ro_rt->rt_ifp;
591 		ro->ro_rt->rt_use++;
592 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
593 			dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
594 		m->m_flags &= ~(M_BCAST | M_MCAST);	/* just in case */
595 
596 		in6_ifstat_inc(ifp, ifs6_out_request);
597 
598 		/*
599 		 * Check if the outgoing interface conflicts with
600 		 * the interface specified by ifi6_ifindex (if specified).
601 		 * Note that loopback interface is always okay.
602 		 * (this may happen when we are sending a packet to one of
603 		 *  our own addresses.)
604 		 */
605 		if (opt && opt->ip6po_pktinfo
606 		 && opt->ip6po_pktinfo->ipi6_ifindex) {
607 			if (!(ifp->if_flags & IFF_LOOPBACK)
608 			 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
609 				ip6stat.ip6s_noroute++;
610 				in6_ifstat_inc(ifp, ifs6_out_discard);
611 				error = EHOSTUNREACH;
612 				goto bad;
613 			}
614 		}
615 
616 		if (opt && opt->ip6po_hlim != -1)
617 			ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
618 	} else {
619 		/* Multicast */
620 		struct	in6_multi *in6m;
621 
622 		m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
623 
624 		/*
625 		 * See if the caller provided any multicast options
626 		 */
627 		ifp = NULL;
628 		if (im6o != NULL) {
629 			ip6->ip6_hlim = im6o->im6o_multicast_hlim;
630 			if (im6o->im6o_multicast_ifp != NULL)
631 				ifp = im6o->im6o_multicast_ifp;
632 		} else
633 			ip6->ip6_hlim = ip6_defmcasthlim;
634 
635 		/*
636 		 * See if the caller provided the outgoing interface
637 		 * as an ancillary data.
638 		 * Boundary check for ifindex is assumed to be already done.
639 		 */
640 		if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
641 			ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
642 
643 		/*
644 		 * If the destination is a node-local scope multicast,
645 		 * the packet should be loop-backed only.
646 		 */
647 		if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
648 			/*
649 			 * If the outgoing interface is already specified,
650 			 * it should be a loopback interface.
651 			 */
652 			if (ifp && !(ifp->if_flags & IFF_LOOPBACK)) {
653 				ip6stat.ip6s_badscope++;
654 				error = ENETUNREACH; /* XXX: better error? */
655 				/* XXX correct ifp? */
656 				in6_ifstat_inc(ifp, ifs6_out_discard);
657 				goto bad;
658 			} else {
659 				ifp = &loif[0];
660 			}
661 		}
662 
663 		if (opt && opt->ip6po_hlim != -1)
664 			ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
665 
666 		/*
667 		 * If caller did not provide an interface lookup a
668 		 * default in the routing table.  This is either a
669 		 * default for the speicfied group (i.e. a host
670 		 * route), or a multicast default (a route for the
671 		 * ``net'' ff00::/8).
672 		 */
673 		if (ifp == NULL) {
674 			if (ro->ro_rt == NULL) {
675 				ro->ro_rt =
676 				  rtpurelookup((struct sockaddr *)&ro->ro_dst);
677 			}
678 			if (ro->ro_rt == NULL) {
679 				ip6stat.ip6s_noroute++;
680 				error = EHOSTUNREACH;
681 				/* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
682 				goto bad;
683 			}
684 			ia = ifatoia6(ro->ro_rt->rt_ifa);
685 			ifp = ro->ro_rt->rt_ifp;
686 			ro->ro_rt->rt_use++;
687 		}
688 
689 		if (!(flags & IPV6_FORWARDING))
690 			in6_ifstat_inc(ifp, ifs6_out_request);
691 		in6_ifstat_inc(ifp, ifs6_out_mcast);
692 
693 		/*
694 		 * Confirm that the outgoing interface supports multicast.
695 		 */
696 		if (!(ifp->if_flags & IFF_MULTICAST)) {
697 			ip6stat.ip6s_noroute++;
698 			in6_ifstat_inc(ifp, ifs6_out_discard);
699 			error = ENETUNREACH;
700 			goto bad;
701 		}
702 		IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
703 		if (in6m != NULL &&
704 		   (im6o == NULL || im6o->im6o_multicast_loop)) {
705 			/*
706 			 * If we belong to the destination multicast group
707 			 * on the outgoing interface, and the caller did not
708 			 * forbid loopback, loop back a copy.
709 			 */
710 			ip6_mloopback(ifp, m, dst);
711 		} else {
712 			/*
713 			 * If we are acting as a multicast router, perform
714 			 * multicast forwarding as if the packet had just
715 			 * arrived on the interface to which we are about
716 			 * to send.  The multicast forwarding function
717 			 * recursively calls this function, using the
718 			 * IPV6_FORWARDING flag to prevent infinite recursion.
719 			 *
720 			 * Multicasts that are looped back by ip6_mloopback(),
721 			 * above, will be forwarded by the ip6_input() routine,
722 			 * if necessary.
723 			 */
724 			if (ip6_mrouter && !(flags & IPV6_FORWARDING)) {
725 				if (ip6_mforward(ip6, ifp, m) != 0) {
726 					m_freem(m);
727 					goto done;
728 				}
729 			}
730 		}
731 		/*
732 		 * Multicasts with a hoplimit of zero may be looped back,
733 		 * above, but must not be transmitted on a network.
734 		 * Also, multicasts addressed to the loopback interface
735 		 * are not sent -- the above call to ip6_mloopback() will
736 		 * loop back a copy if this host actually belongs to the
737 		 * destination group on the loopback interface.
738 		 */
739 		if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
740 			m_freem(m);
741 			goto done;
742 		}
743 	}
744 
745 	/*
746 	 * Fill the outgoing inteface to tell the upper layer
747 	 * to increment per-interface statistics.
748 	 */
749 	if (ifpp)
750 		*ifpp = ifp;
751 
752 	/* Determine path MTU. */
753 	if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
754 	    &alwaysfrag)) != 0)
755 		goto bad;
756 
757 	/*
758 	 * The caller of this function may specify to use the minimum MTU
759 	 * in some cases.
760 	 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
761 	 * setting.  The logic is a bit complicated; by default, unicast
762 	 * packets will follow path MTU while multicast packets will be sent at
763 	 * the minimum MTU.  If IP6PO_MINMTU_ALL is specified, all packets
764 	 * including unicast ones will be sent at the minimum MTU.  Multicast
765 	 * packets will always be sent at the minimum MTU unless
766 	 * IP6PO_MINMTU_DISABLE is explicitly specified.
767 	 * See RFC 3542 for more details.
768 	 */
769 	if (mtu > IPV6_MMTU) {
770 		if ((flags & IPV6_MINMTU))
771 			mtu = IPV6_MMTU;
772 		else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
773 			mtu = IPV6_MMTU;
774 		else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
775 			 (opt == NULL ||
776 			  opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
777 			mtu = IPV6_MMTU;
778 		}
779 	}
780 
781 	/* Fake scoped addresses */
782 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
783 		/*
784 		 * If source or destination address is a scoped address, and
785 		 * the packet is going to be sent to a loopback interface,
786 		 * we should keep the original interface.
787 		 */
788 
789 		/*
790 		 * XXX: this is a very experimental and temporary solution.
791 		 * We eventually have sockaddr_in6 and use the sin6_scope_id
792 		 * field of the structure here.
793 		 * We rely on the consistency between two scope zone ids
794 		 * of source and destination, which should already be assured.
795 		 * Larger scopes than link will be supported in the future.
796 		 */
797 		origifp = NULL;
798 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
799 			origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
800 		else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
801 			origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
802 		/*
803 		 * XXX: origifp can be NULL even in those two cases above.
804 		 * For example, if we remove the (only) link-local address
805 		 * from the loopback interface, and try to send a link-local
806 		 * address without link-id information.  Then the source
807 		 * address is ::1, and the destination address is the
808 		 * link-local address with its s6_addr16[1] being zero.
809 		 * What is worse, if the packet goes to the loopback interface
810 		 * by a default rejected route, the null pointer would be
811 		 * passed to looutput, and the kernel would hang.
812 		 * The following last resort would prevent such disaster.
813 		 */
814 		if (origifp == NULL)
815 			origifp = ifp;
816 	}
817 	else
818 		origifp = ifp;
819 	/*
820 	 * clear embedded scope identifiers if necessary.
821 	 * in6_clearscope will touch the addresses only when necessary.
822 	 */
823 	in6_clearscope(&ip6->ip6_src);
824 	in6_clearscope(&ip6->ip6_dst);
825 
826 	/*
827 	 * Check with the firewall...
828 	 */
829 	if (ip6_fw_enable && ip6_fw_chk_ptr) {
830 		u_short port = 0;
831 
832 		m->m_pkthdr.rcvif = NULL;	/* XXX */
833 		/* If ipfw says divert, we have to just drop packet */
834 		if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
835 			m_freem(m);
836 			goto done;
837 		}
838 		if (!m) {
839 			error = EACCES;
840 			goto done;
841 		}
842 	}
843 
844 	/*
845 	 * If the outgoing packet contains a hop-by-hop options header,
846 	 * it must be examined and processed even by the source node.
847 	 * (RFC 2460, section 4.)
848 	 */
849 	if (exthdrs.ip6e_hbh) {
850 		struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
851 		u_int32_t dummy1; /* XXX unused */
852 		u_int32_t dummy2; /* XXX unused */
853 
854 #ifdef DIAGNOSTIC
855 		if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
856 			panic("ip6e_hbh is not continuous");
857 #endif
858 		/*
859 		 *  XXX: if we have to send an ICMPv6 error to the sender,
860 		 *       we need the M_LOOP flag since icmp6_error() expects
861 		 *       the IPv6 and the hop-by-hop options header are
862 		 *       continuous unless the flag is set.
863 		 */
864 		m->m_flags |= M_LOOP;
865 		m->m_pkthdr.rcvif = ifp;
866 		if (ip6_process_hopopts(m,
867 					(u_int8_t *)(hbh + 1),
868 					((hbh->ip6h_len + 1) << 3) -
869 					sizeof(struct ip6_hbh),
870 					&dummy1, &dummy2) < 0) {
871 			/* m was already freed at this point */
872 			error = EINVAL;/* better error? */
873 			goto done;
874 		}
875 		m->m_flags &= ~M_LOOP; /* XXX */
876 		m->m_pkthdr.rcvif = NULL;
877 	}
878 
879 	/*
880 	 * Run through list of hooks for output packets.
881 	 */
882 	if (pfil_has_hooks(&inet6_pfil_hook)) {
883 		error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT);
884 		if (error != 0 || m == NULL)
885 			goto done;
886 		ip6 = mtod(m, struct ip6_hdr *);
887 	}
888 
889 	/*
890 	 * Send the packet to the outgoing interface.
891 	 * If necessary, do IPv6 fragmentation before sending.
892 	 *
893 	 * the logic here is rather complex:
894 	 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
895 	 * 1-a:	send as is if tlen <= path mtu
896 	 * 1-b:	fragment if tlen > path mtu
897 	 *
898 	 * 2: if user asks us not to fragment (dontfrag == 1)
899 	 * 2-a:	send as is if tlen <= interface mtu
900 	 * 2-b:	error if tlen > interface mtu
901 	 *
902 	 * 3: if we always need to attach fragment header (alwaysfrag == 1)
903 	 *	always fragment
904 	 *
905 	 * 4: if dontfrag == 1 && alwaysfrag == 1
906 	 *	error, as we cannot handle this conflicting request
907 	 */
908 	tlen = m->m_pkthdr.len;
909 
910 	if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
911 		dontfrag = 1;
912 	else
913 		dontfrag = 0;
914 	if (dontfrag && alwaysfrag) {	/* case 4 */
915 		/* conflicting request - can't transmit */
916 		error = EMSGSIZE;
917 		goto bad;
918 	}
919 	if (dontfrag && tlen > IN6_LINKMTU(ifp)) {	/* case 2-b */
920 		/*
921 		 * Even if the DONTFRAG option is specified, we cannot send the
922 		 * packet when the data length is larger than the MTU of the
923 		 * outgoing interface.
924 		 * Notify the error by sending IPV6_PATHMTU ancillary data as
925 		 * well as returning an error code (the latter is not described
926 		 * in the API spec.)
927 		 */
928 		u_int32_t mtu32;
929 		struct ip6ctlparam ip6cp;
930 
931 		mtu32 = (u_int32_t)mtu;
932 		bzero(&ip6cp, sizeof(ip6cp));
933 		ip6cp.ip6c_cmdarg = (void *)&mtu32;
934 		kpfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
935 		    (void *)&ip6cp);
936 
937 		error = EMSGSIZE;
938 		goto bad;
939 	}
940 
941 	/*
942 	 * transmit packet without fragmentation
943 	 */
944 	if (dontfrag || (!alwaysfrag && tlen <= mtu)) {	/* case 1-a and 2-a */
945 		struct in6_ifaddr *ia6;
946 
947 		ip6 = mtod(m, struct ip6_hdr *);
948 		ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
949 		if (ia6) {
950 			/* Record statistics for this interface address. */
951 			IFA_STAT_INC(&ia6->ia_ifa, opackets, 1);
952 			IFA_STAT_INC(&ia6->ia_ifa, obytes, m->m_pkthdr.len);
953 		}
954 #ifdef IPSEC
955 		/* clean ipsec history once it goes out of the node */
956 		ipsec_delaux(m);
957 #endif
958 		error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
959 		goto done;
960 	}
961 
962 	/*
963 	 * try to fragment the packet.  case 1-b and 3
964 	 */
965 	if (mtu < IPV6_MMTU) {
966 		/*
967 		 * note that path MTU is never less than IPV6_MMTU
968 		 * (see icmp6_input).
969 		 */
970 		error = EMSGSIZE;
971 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
972 		goto bad;
973 	} else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
974 		error = EMSGSIZE;
975 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
976 		goto bad;
977 	} else {
978 		struct mbuf **mnext, *m_frgpart;
979 		struct ip6_frag *ip6f;
980 		u_int32_t id = htonl(ip6_id++);
981 		u_char nextproto;
982 
983 		/*
984 		 * Too large for the destination or interface;
985 		 * fragment if possible.
986 		 * Must be able to put at least 8 bytes per fragment.
987 		 */
988 		hlen = unfragpartlen;
989 		if (mtu > IPV6_MAXPACKET)
990 			mtu = IPV6_MAXPACKET;
991 
992 		len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
993 		if (len < 8) {
994 			error = EMSGSIZE;
995 			in6_ifstat_inc(ifp, ifs6_out_fragfail);
996 			goto bad;
997 		}
998 
999 		mnext = &m->m_nextpkt;
1000 
1001 		/*
1002 		 * Change the next header field of the last header in the
1003 		 * unfragmentable part.
1004 		 */
1005 		if (exthdrs.ip6e_rthdr) {
1006 			nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1007 			*mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1008 		} else if (exthdrs.ip6e_dest1) {
1009 			nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1010 			*mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1011 		} else if (exthdrs.ip6e_hbh) {
1012 			nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1013 			*mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1014 		} else {
1015 			nextproto = ip6->ip6_nxt;
1016 			ip6->ip6_nxt = IPPROTO_FRAGMENT;
1017 		}
1018 
1019 		/*
1020 		 * Loop through length of segment after first fragment,
1021 		 * make new header and copy data of each part and link onto
1022 		 * chain.
1023 		 */
1024 		m0 = m;
1025 		for (off = hlen; off < tlen; off += len) {
1026 			MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1027 			if (!m) {
1028 				error = ENOBUFS;
1029 				ip6stat.ip6s_odropped++;
1030 				goto sendorfree;
1031 			}
1032 			m->m_pkthdr.rcvif = NULL;
1033 			m->m_flags = m0->m_flags & M_COPYFLAGS;
1034 			*mnext = m;
1035 			mnext = &m->m_nextpkt;
1036 			m->m_data += max_linkhdr;
1037 			mhip6 = mtod(m, struct ip6_hdr *);
1038 			*mhip6 = *ip6;
1039 			m->m_len = sizeof(*mhip6);
1040  			error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1041  			if (error) {
1042 				ip6stat.ip6s_odropped++;
1043 				goto sendorfree;
1044 			}
1045 			ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1046 			if (off + len >= tlen)
1047 				len = tlen - off;
1048 			else
1049 				ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1050 			mhip6->ip6_plen = htons((u_short)(len + hlen +
1051 							  sizeof(*ip6f) -
1052 							  sizeof(struct ip6_hdr)));
1053 			if ((m_frgpart = m_copy(m0, off, len)) == NULL) {
1054 				error = ENOBUFS;
1055 				ip6stat.ip6s_odropped++;
1056 				goto sendorfree;
1057 			}
1058 			m_cat(m, m_frgpart);
1059 			m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1060 			m->m_pkthdr.rcvif = NULL;
1061 			ip6f->ip6f_reserved = 0;
1062 			ip6f->ip6f_ident = id;
1063 			ip6f->ip6f_nxt = nextproto;
1064 			ip6stat.ip6s_ofragments++;
1065 			in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1066 		}
1067 
1068 		in6_ifstat_inc(ifp, ifs6_out_fragok);
1069 	}
1070 
1071 	/*
1072 	 * Remove leading garbages.
1073 	 */
1074 sendorfree:
1075 	m = m0->m_nextpkt;
1076 	m0->m_nextpkt = NULL;
1077 	m_freem(m0);
1078 	for (m0 = m; m; m = m0) {
1079 		m0 = m->m_nextpkt;
1080 		m->m_nextpkt = NULL;
1081 		if (error == 0) {
1082  			/* Record statistics for this interface address. */
1083  			if (ia) {
1084  				IFA_STAT_INC(&ia->ia_ifa, opackets, 1);
1085  				IFA_STAT_INC(&ia->ia_ifa, obytes,
1086 				    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
1111 #ifdef FAST_IPSEC
1112 	if (sp != NULL)
1113 		KEY_FREESP(&sp);
1114 #endif
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 copyexthdr(void *h, struct mbuf **mp)
1131 {
1132 	struct ip6_ext *hdr = h;
1133 	int hlen;
1134 	struct mbuf *m;
1135 
1136 	if (hdr == NULL)
1137 		return 0;
1138 
1139 	hlen = (hdr->ip6e_len + 1) * 8;
1140 	if (hlen > MCLBYTES)
1141 		return ENOBUFS;	/* XXX */
1142 
1143 	m = m_getb(hlen, MB_DONTWAIT, MT_DATA, 0);
1144 	if (!m)
1145 		return ENOBUFS;
1146 	m->m_len = hlen;
1147 
1148 	bcopy(hdr, mtod(m, caddr_t), hlen);
1149 
1150 	*mp = m;
1151 	return 0;
1152 }
1153 
1154 /*
1155  * Insert jumbo payload option.
1156  */
1157 static int
1158 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1159 {
1160 	struct mbuf *mopt;
1161 	u_char *optbuf;
1162 	u_int32_t v;
1163 
1164 #define JUMBOOPTLEN	8	/* length of jumbo payload option and padding */
1165 
1166 	/*
1167 	 * If there is no hop-by-hop options header, allocate new one.
1168 	 * If there is one but it doesn't have enough space to store the
1169 	 * jumbo payload option, allocate a cluster to store the whole options.
1170 	 * Otherwise, use it to store the options.
1171 	 */
1172 	if (exthdrs->ip6e_hbh == NULL) {
1173 		MGET(mopt, MB_DONTWAIT, MT_DATA);
1174 		if (mopt == NULL)
1175 			return (ENOBUFS);
1176 		mopt->m_len = JUMBOOPTLEN;
1177 		optbuf = mtod(mopt, u_char *);
1178 		optbuf[1] = 0;	/* = ((JUMBOOPTLEN) >> 3) - 1 */
1179 		exthdrs->ip6e_hbh = mopt;
1180 	} else {
1181 		struct ip6_hbh *hbh;
1182 
1183 		mopt = exthdrs->ip6e_hbh;
1184 		if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1185 			/*
1186 			 * XXX assumption:
1187 			 * - exthdrs->ip6e_hbh is not referenced from places
1188 			 *   other than exthdrs.
1189 			 * - exthdrs->ip6e_hbh is not an mbuf chain.
1190 			 */
1191 			int oldoptlen = mopt->m_len;
1192 			struct mbuf *n;
1193 
1194 			/*
1195 			 * XXX: give up if the whole (new) hbh header does
1196 			 * not fit even in an mbuf cluster.
1197 			 */
1198 			if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1199 				return (ENOBUFS);
1200 
1201 			/*
1202 			 * As a consequence, we must always prepare a cluster
1203 			 * at this point.
1204 			 */
1205 			n = m_getcl(MB_DONTWAIT, MT_DATA, 0);
1206 			if (!n)
1207 				return (ENOBUFS);
1208 			n->m_len = oldoptlen + JUMBOOPTLEN;
1209 			bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), oldoptlen);
1210 			optbuf = mtod(n, caddr_t) + oldoptlen;
1211 			m_freem(mopt);
1212 			mopt = exthdrs->ip6e_hbh = n;
1213 		} else {
1214 			optbuf = mtod(mopt, u_char *) + mopt->m_len;
1215 			mopt->m_len += JUMBOOPTLEN;
1216 		}
1217 		optbuf[0] = IP6OPT_PADN;
1218 		optbuf[1] = 1;
1219 
1220 		/*
1221 		 * Adjust the header length according to the pad and
1222 		 * the jumbo payload option.
1223 		 */
1224 		hbh = mtod(mopt, struct ip6_hbh *);
1225 		hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1226 	}
1227 
1228 	/* fill in the option. */
1229 	optbuf[2] = IP6OPT_JUMBO;
1230 	optbuf[3] = 4;
1231 	v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1232 	bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1233 
1234 	/* finally, adjust the packet header length */
1235 	exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1236 
1237 	return (0);
1238 #undef JUMBOOPTLEN
1239 }
1240 
1241 /*
1242  * Insert fragment header and copy unfragmentable header portions.
1243  */
1244 static int
1245 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1246 		  struct ip6_frag **frghdrp)
1247 {
1248 	struct mbuf *n, *mlast;
1249 
1250 	if (hlen > sizeof(struct ip6_hdr)) {
1251 		n = m_copym(m0, sizeof(struct ip6_hdr),
1252 			    hlen - sizeof(struct ip6_hdr), MB_DONTWAIT);
1253 		if (n == NULL)
1254 			return (ENOBUFS);
1255 		m->m_next = n;
1256 	} else
1257 		n = m;
1258 
1259 	/* Search for the last mbuf of unfragmentable part. */
1260 	for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1261 		;
1262 
1263 	if (!(mlast->m_flags & M_EXT) &&
1264 	    M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1265 		/* use the trailing space of the last mbuf for the fragment hdr */
1266 		*frghdrp =
1267 			(struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1268 		mlast->m_len += sizeof(struct ip6_frag);
1269 		m->m_pkthdr.len += sizeof(struct ip6_frag);
1270 	} else {
1271 		/* allocate a new mbuf for the fragment header */
1272 		struct mbuf *mfrg;
1273 
1274 		MGET(mfrg, MB_DONTWAIT, MT_DATA);
1275 		if (mfrg == NULL)
1276 			return (ENOBUFS);
1277 		mfrg->m_len = sizeof(struct ip6_frag);
1278 		*frghdrp = mtod(mfrg, struct ip6_frag *);
1279 		mlast->m_next = mfrg;
1280 	}
1281 
1282 	return (0);
1283 }
1284 
1285 static int
1286 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1287     struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1288     int *alwaysfragp)
1289 {
1290 	u_int32_t mtu = 0;
1291 	int alwaysfrag = 0;
1292 	int error = 0;
1293 
1294 	if (ro_pmtu != ro) {
1295 		/* The first hop and the final destination may differ. */
1296 		struct sockaddr_in6 *sa6_dst =
1297 		    (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1298 		if (ro_pmtu->ro_rt &&
1299 		    ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1300 		     !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1301 			RTFREE(ro_pmtu->ro_rt);
1302 			ro_pmtu->ro_rt = NULL;
1303 		}
1304 		if (ro_pmtu->ro_rt == NULL) {
1305 			bzero(sa6_dst, sizeof(*sa6_dst));
1306 			sa6_dst->sin6_family = AF_INET6;
1307 			sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1308 			sa6_dst->sin6_addr = *dst;
1309 
1310 			rtalloc((struct route *)ro_pmtu);
1311 		}
1312 	}
1313 	if (ro_pmtu->ro_rt) {
1314 		u_int32_t ifmtu;
1315 		struct in_conninfo inc;
1316 
1317 		bzero(&inc, sizeof(inc));
1318 		inc.inc_flags = 1; /* IPv6 */
1319 		inc.inc6_faddr = *dst;
1320 
1321 		if (ifp == NULL)
1322 			ifp = ro_pmtu->ro_rt->rt_ifp;
1323 		ifmtu = IN6_LINKMTU(ifp);
1324 		mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1325 		if (mtu == 0)
1326 			mtu = ifmtu;
1327 		else if (mtu < IPV6_MMTU) {
1328 			/*
1329 			 * RFC2460 section 5, last paragraph:
1330 			 * if we record ICMPv6 too big message with
1331 			 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1332 			 * or smaller, with framgent header attached.
1333 			 * (fragment header is needed regardless from the
1334 			 * packet size, for translators to identify packets)
1335 			 */
1336 			alwaysfrag = 1;
1337 			mtu = IPV6_MMTU;
1338 		} else if (mtu > ifmtu) {
1339 			/*
1340 			 * The MTU on the route is larger than the MTU on
1341 			 * the interface!  This shouldn't happen, unless the
1342 			 * MTU of the interface has been changed after the
1343 			 * interface was brought up.  Change the MTU in the
1344 			 * route to match the interface MTU (as long as the
1345 			 * field isn't locked).
1346 			 */
1347 			mtu = ifmtu;
1348 			ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1349 		}
1350 	} else if (ifp) {
1351 		mtu = IN6_LINKMTU(ifp);
1352 	} else
1353 		error = EHOSTUNREACH; /* XXX */
1354 
1355 	*mtup = mtu;
1356 	if (alwaysfragp)
1357 		*alwaysfragp = alwaysfrag;
1358 	return (error);
1359 }
1360 
1361 /*
1362  * IP6 socket option processing.
1363  */
1364 void
1365 ip6_ctloutput_dispatch(netmsg_t msg)
1366 {
1367 	int error;
1368 
1369 	error = ip6_ctloutput(msg->ctloutput.base.nm_so,
1370 			      msg->ctloutput.nm_sopt);
1371 	lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
1372 }
1373 
1374 int
1375 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1376 {
1377 	int optdatalen,uproto;
1378 	int privileged;
1379 	struct inpcb *in6p = so->so_pcb;
1380 	void *optdata;
1381 	int error, optval;
1382 	int level, op, optname;
1383 	int optlen;
1384 	struct thread *td;
1385 
1386 	if (sopt) {
1387 		level = sopt->sopt_level;
1388 		op = sopt->sopt_dir;
1389 		optname = sopt->sopt_name;
1390 		optlen = sopt->sopt_valsize;
1391 		td = sopt->sopt_td;
1392 	} else {
1393 		panic("ip6_ctloutput: arg soopt is NULL");
1394 		/* NOT REACHED */
1395 		td = NULL;
1396 	}
1397 	error = optval = 0;
1398 
1399 	uproto = (int)so->so_proto->pr_protocol;
1400 	privileged = (td == NULL || priv_check(td, PRIV_ROOT)) ? 0 : 1;
1401 
1402 	if (level == IPPROTO_IPV6) {
1403 		switch (op) {
1404 
1405 		case SOPT_SET:
1406 			switch (optname) {
1407 			case IPV6_2292PKTOPTIONS:
1408 #ifdef IPV6_PKTOPTIONS
1409 			case IPV6_PKTOPTIONS:
1410 #endif
1411 			{
1412 				struct mbuf *m;
1413 
1414 				error = soopt_getm(sopt, &m); /* XXX */
1415 				if (error != 0)
1416 					break;
1417 				soopt_to_mbuf(sopt, m); /* XXX */
1418 				error = ip6_pcbopts(&in6p->in6p_outputopts,
1419 						    m, so, sopt);
1420 				m_freem(m); /* XXX */
1421 				break;
1422 			}
1423 
1424 			/*
1425 			 * Use of some Hop-by-Hop options or some
1426 			 * Destination options, might require special
1427 			 * privilege.  That is, normal applications
1428 			 * (without special privilege) might be forbidden
1429 			 * from setting certain options in outgoing packets,
1430 			 * and might never see certain options in received
1431 			 * packets. [RFC 2292 Section 6]
1432 			 * KAME specific note:
1433 			 *  KAME prevents non-privileged users from sending or
1434 			 *  receiving ANY hbh/dst options in order to avoid
1435 			 *  overhead of parsing options in the kernel.
1436 			 */
1437 			case IPV6_RECVHOPOPTS:
1438 			case IPV6_RECVDSTOPTS:
1439 			case IPV6_RECVRTHDRDSTOPTS:
1440 				if (!privileged)
1441 					return (EPERM);
1442 			case IPV6_RECVPKTINFO:
1443 			case IPV6_RECVHOPLIMIT:
1444 			case IPV6_RECVRTHDR:
1445 			case IPV6_RECVPATHMTU:
1446 			case IPV6_RECVTCLASS:
1447 			case IPV6_AUTOFLOWLABEL:
1448 			case IPV6_HOPLIMIT:
1449 			/* FALLTHROUGH */
1450 			case IPV6_UNICAST_HOPS:
1451 			case IPV6_FAITH:
1452 
1453 			case IPV6_V6ONLY:
1454 				if (optlen != sizeof(int)) {
1455 					error = EINVAL;
1456 					break;
1457 				}
1458 				error = soopt_to_kbuf(sopt, &optval,
1459 					sizeof optval, sizeof optval);
1460 				if (error)
1461 					break;
1462 				switch (optname) {
1463 
1464 				case IPV6_UNICAST_HOPS:
1465 					if (optval < -1 || optval >= 256)
1466 						error = EINVAL;
1467 					else {
1468 						/* -1 = kernel default */
1469 						in6p->in6p_hops = optval;
1470 
1471 						if ((in6p->in6p_vflag &
1472 						     INP_IPV4) != 0)
1473 							in6p->inp_ip_ttl = optval;
1474 					}
1475 					break;
1476 #define OPTSET(bit) \
1477 do { \
1478 	if (optval) \
1479 		in6p->in6p_flags |= (bit); \
1480 	else \
1481 		in6p->in6p_flags &= ~(bit); \
1482 } while (0)
1483 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1484 /*
1485  * Although changed to RFC3542, It's better to also support RFC2292 API
1486  */
1487 #define OPTSET2292(bit) \
1488 do { \
1489 	in6p->in6p_flags |= IN6P_RFC2292; \
1490 	if (optval) \
1491 		in6p->in6p_flags |= (bit); \
1492 	else \
1493 		in6p->in6p_flags &= ~(bit); \
1494 } while (/*CONSTCOND*/ 0)
1495 
1496 				case IPV6_RECVPKTINFO:
1497 					/* cannot mix with RFC2292 */
1498 					if (OPTBIT(IN6P_RFC2292)) {
1499 						error = EINVAL;
1500 						break;
1501 					}
1502 					OPTSET(IN6P_PKTINFO);
1503 					break;
1504 
1505 				case IPV6_HOPLIMIT:
1506 				{
1507 					struct ip6_pktopts **optp;
1508 
1509 					/* cannot mix with RFC2292 */
1510 					if (OPTBIT(IN6P_RFC2292)) {
1511 						error = EINVAL;
1512 						break;
1513 					}
1514 					optp = &in6p->in6p_outputopts;
1515 					error = ip6_pcbopt(IPV6_HOPLIMIT,
1516 					    (u_char *)&optval, sizeof(optval),
1517 					    optp, uproto);
1518 					break;
1519 				}
1520 
1521 				case IPV6_RECVHOPLIMIT:
1522 					/* cannot mix with RFC2292 */
1523 					if (OPTBIT(IN6P_RFC2292)) {
1524 						error = EINVAL;
1525 						break;
1526 					}
1527 					OPTSET(IN6P_HOPLIMIT);
1528 					break;
1529 
1530 				case IPV6_RECVHOPOPTS:
1531 					/* cannot mix with RFC2292 */
1532 					if (OPTBIT(IN6P_RFC2292)) {
1533 						error = EINVAL;
1534 						break;
1535 					}
1536 					OPTSET(IN6P_HOPOPTS);
1537 					break;
1538 
1539 				case IPV6_RECVDSTOPTS:
1540 					/* cannot mix with RFC2292 */
1541 					if (OPTBIT(IN6P_RFC2292)) {
1542 						error = EINVAL;
1543 						break;
1544 					}
1545 					OPTSET(IN6P_DSTOPTS);
1546 					break;
1547 
1548 				case IPV6_RECVRTHDRDSTOPTS:
1549 					/* cannot mix with RFC2292 */
1550 					if (OPTBIT(IN6P_RFC2292)) {
1551 						error = EINVAL;
1552 						break;
1553 					}
1554 					OPTSET(IN6P_RTHDRDSTOPTS);
1555 					break;
1556 
1557 				case IPV6_RECVRTHDR:
1558 					/* cannot mix with RFC2292 */
1559 					if (OPTBIT(IN6P_RFC2292)) {
1560 						error = EINVAL;
1561 						break;
1562 					}
1563 					OPTSET(IN6P_RTHDR);
1564 					break;
1565 
1566 				case IPV6_RECVPATHMTU:
1567 					/*
1568 					 * We ignore this option for TCP
1569 					 * sockets.
1570 					 * (RFC3542 leaves this case
1571 					 * unspecified.)
1572 					 */
1573 					if (uproto != IPPROTO_TCP)
1574 						OPTSET(IN6P_MTU);
1575 					break;
1576 
1577 				case IPV6_RECVTCLASS:
1578 					/* cannot mix with RFC2292 XXX */
1579 					if (OPTBIT(IN6P_RFC2292)) {
1580 						error = EINVAL;
1581 						break;
1582 					}
1583 					OPTSET(IN6P_TCLASS);
1584 					break;
1585 
1586 				case IPV6_AUTOFLOWLABEL:
1587 					OPTSET(IN6P_AUTOFLOWLABEL);
1588 					break;
1589 
1590 				case IPV6_FAITH:
1591 					OPTSET(IN6P_FAITH);
1592 					break;
1593 
1594 				case IPV6_V6ONLY:
1595 					/*
1596 					 * make setsockopt(IPV6_V6ONLY)
1597 					 * available only prior to bind(2).
1598 					 */
1599 					if (in6p->in6p_lport ||
1600 					    !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1601 					{
1602 						error = EINVAL;
1603 						break;
1604 					}
1605 					OPTSET(IN6P_IPV6_V6ONLY);
1606 					if (optval)
1607 						in6p->in6p_vflag &= ~INP_IPV4;
1608 					else
1609 						in6p->in6p_vflag |= INP_IPV4;
1610 					break;
1611 				}
1612 				break;
1613 
1614 			case IPV6_TCLASS:
1615 			case IPV6_DONTFRAG:
1616 			case IPV6_USE_MIN_MTU:
1617 			case IPV6_PREFER_TEMPADDR:
1618 				if (optlen != sizeof(optval)) {
1619 					error = EINVAL;
1620 					break;
1621 				}
1622 				error = soopt_to_kbuf(sopt, &optval,
1623 					sizeof optval, sizeof optval);
1624 				if (error)
1625 					break;
1626 				{
1627 					struct ip6_pktopts **optp;
1628 					optp = &in6p->in6p_outputopts;
1629 					error = ip6_pcbopt(optname,
1630 					    (u_char *)&optval, sizeof(optval),
1631 					    optp, uproto);
1632 					break;
1633 				}
1634 
1635 			case IPV6_2292PKTINFO:
1636 			case IPV6_2292HOPLIMIT:
1637 			case IPV6_2292HOPOPTS:
1638 			case IPV6_2292DSTOPTS:
1639 			case IPV6_2292RTHDR:
1640 				/* RFC 2292 */
1641 				if (optlen != sizeof(int)) {
1642 					error = EINVAL;
1643 					break;
1644 				}
1645 				error = soopt_to_kbuf(sopt, &optval,
1646 					sizeof optval, sizeof optval);
1647 				if (error)
1648 					break;
1649 				switch (optname) {
1650 				case IPV6_2292PKTINFO:
1651 					OPTSET2292(IN6P_PKTINFO);
1652 					break;
1653 				case IPV6_2292HOPLIMIT:
1654 					OPTSET2292(IN6P_HOPLIMIT);
1655 					break;
1656 				case IPV6_2292HOPOPTS:
1657 					/*
1658 					 * Check super-user privilege.
1659 					 * See comments for IPV6_RECVHOPOPTS.
1660 					 */
1661 					if (!privileged)
1662 						return (EPERM);
1663 					OPTSET2292(IN6P_HOPOPTS);
1664 					break;
1665 				case IPV6_2292DSTOPTS:
1666 					if (!privileged)
1667 						return (EPERM);
1668 					OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1669 					break;
1670 				case IPV6_2292RTHDR:
1671 					OPTSET2292(IN6P_RTHDR);
1672 					break;
1673 				}
1674 				break;
1675 
1676 			case IPV6_PKTINFO:
1677 			case IPV6_HOPOPTS:
1678 			case IPV6_RTHDR:
1679 			case IPV6_DSTOPTS:
1680 			case IPV6_RTHDRDSTOPTS:
1681 			case IPV6_NEXTHOP:
1682 			{
1683 				/*
1684 				 * New advanced API (RFC3542)
1685 				 */
1686 				u_char *optbuf;
1687 				u_char optbuf_storage[MCLBYTES];
1688 				int optlen;
1689 				struct ip6_pktopts **optp;
1690 
1691 				/* cannot mix with RFC2292 */
1692 				if (OPTBIT(IN6P_RFC2292)) {
1693 					error = EINVAL;
1694 					break;
1695 				}
1696 
1697 				/*
1698 				 * We only ensure valsize is not too large
1699 				 * here.  Further validation will be done
1700 				 * later.
1701 				 */
1702 				error = soopt_to_kbuf(sopt, optbuf_storage,
1703 				    sizeof(optbuf_storage), 0);
1704 				if (error)
1705 					break;
1706 				optlen = sopt->sopt_valsize;
1707 				optbuf = optbuf_storage;
1708 				optp = &in6p->in6p_outputopts;
1709 				error = ip6_pcbopt(optname, optbuf, optlen,
1710 				    optp, uproto);
1711 				break;
1712 			}
1713 #undef OPTSET
1714 
1715 			case IPV6_MULTICAST_IF:
1716 			case IPV6_MULTICAST_HOPS:
1717 			case IPV6_MULTICAST_LOOP:
1718 			case IPV6_JOIN_GROUP:
1719 			case IPV6_LEAVE_GROUP:
1720 			    {
1721 				struct mbuf *m;
1722 				if (sopt->sopt_valsize > MLEN) {
1723 					error = EMSGSIZE;
1724 					break;
1725 				}
1726 				/* XXX */
1727 				MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_HEADER);
1728 				if (m == NULL) {
1729 					error = ENOBUFS;
1730 					break;
1731 				}
1732 				m->m_len = sopt->sopt_valsize;
1733 				error = soopt_to_kbuf(sopt, mtod(m, char *),
1734 						    m->m_len, m->m_len);
1735 				error =	ip6_setmoptions(sopt->sopt_name,
1736 							&in6p->in6p_moptions,
1737 							m);
1738 				m_free(m);
1739 			    }
1740 				break;
1741 
1742 			case IPV6_PORTRANGE:
1743 				error = soopt_to_kbuf(sopt, &optval,
1744 				    sizeof optval, sizeof optval);
1745 				if (error)
1746 					break;
1747 
1748 				switch (optval) {
1749 				case IPV6_PORTRANGE_DEFAULT:
1750 					in6p->in6p_flags &= ~(IN6P_LOWPORT);
1751 					in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1752 					break;
1753 
1754 				case IPV6_PORTRANGE_HIGH:
1755 					in6p->in6p_flags &= ~(IN6P_LOWPORT);
1756 					in6p->in6p_flags |= IN6P_HIGHPORT;
1757 					break;
1758 
1759 				case IPV6_PORTRANGE_LOW:
1760 					in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1761 					in6p->in6p_flags |= IN6P_LOWPORT;
1762 					break;
1763 
1764 				default:
1765 					error = EINVAL;
1766 					break;
1767 				}
1768 				break;
1769 
1770 #if defined(IPSEC) || defined(FAST_IPSEC)
1771 			case IPV6_IPSEC_POLICY:
1772 			    {
1773 				caddr_t req = NULL;
1774 				size_t len = 0;
1775 				struct mbuf *m;
1776 
1777 				if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1778 					break;
1779 				soopt_to_mbuf(sopt, m);		/* XXX */
1780 				if (m) {
1781 					req = mtod(m, caddr_t);
1782 					len = m->m_len;
1783 				}
1784 				error = ipsec6_set_policy(in6p, optname, req,
1785 							  len, privileged);
1786 				m_freem(m);
1787 			    }
1788 				break;
1789 #endif /* KAME IPSEC */
1790 
1791 			case IPV6_FW_ADD:
1792 			case IPV6_FW_DEL:
1793 			case IPV6_FW_FLUSH:
1794 			case IPV6_FW_ZERO:
1795 			    {
1796 				struct mbuf *m;
1797 				struct mbuf **mp = &m;
1798 
1799 				if (ip6_fw_ctl_ptr == NULL)
1800 					return EINVAL;
1801 				/* XXX */
1802 				if ((error = soopt_getm(sopt, &m)) != 0)
1803 					break;
1804 				/* XXX */
1805 				soopt_to_mbuf(sopt, m);
1806 				error = (*ip6_fw_ctl_ptr)(optname, mp);
1807 				m = *mp;
1808 			    }
1809 				break;
1810 
1811 			default:
1812 				error = ENOPROTOOPT;
1813 				break;
1814 			}
1815 			break;
1816 
1817 		case SOPT_GET:
1818 			switch (optname) {
1819 			case IPV6_2292PKTOPTIONS:
1820 #ifdef IPV6_PKTOPTIONS
1821 			case IPV6_PKTOPTIONS:
1822 #endif
1823 				/*
1824 				 * RFC3542 (effectively) deprecated the
1825 				 * semantics of the 2292-style pktoptions.
1826 				 * Since it was not reliable in nature (i.e.,
1827 				 * applications had to expect the lack of some
1828 				 * information after all), it would make sense
1829 				 * to simplify this part by always returning
1830 				 * empty data.
1831 				 */
1832 				if (in6p->in6p_options) {
1833 					struct mbuf *m;
1834 					m = m_copym(in6p->in6p_options,
1835 					    0, M_COPYALL, MB_WAIT);
1836 					error = soopt_from_mbuf(sopt, m);
1837 					if (error == 0)
1838 						m_freem(m);
1839 				} else
1840 					sopt->sopt_valsize = 0;
1841 				break;
1842 
1843 			case IPV6_RECVHOPOPTS:
1844 			case IPV6_RECVDSTOPTS:
1845 			case IPV6_RECVRTHDRDSTOPTS:
1846 			case IPV6_UNICAST_HOPS:
1847 			case IPV6_RECVPKTINFO:
1848 			case IPV6_RECVHOPLIMIT:
1849 			case IPV6_RECVRTHDR:
1850 			case IPV6_RECVPATHMTU:
1851 			case IPV6_RECVTCLASS:
1852 			case IPV6_AUTOFLOWLABEL:
1853 			case IPV6_FAITH:
1854 			case IPV6_V6ONLY:
1855 			case IPV6_PORTRANGE:
1856 				switch (optname) {
1857 
1858 				case IPV6_RECVHOPOPTS:
1859 					optval = OPTBIT(IN6P_HOPOPTS);
1860 					break;
1861 
1862 				case IPV6_RECVDSTOPTS:
1863 					optval = OPTBIT(IN6P_DSTOPTS);
1864 					break;
1865 
1866 				case IPV6_RECVRTHDRDSTOPTS:
1867 					optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1868 					break;
1869 
1870 				case IPV6_RECVPKTINFO:
1871 					optval = OPTBIT(IN6P_PKTINFO);
1872 					break;
1873 
1874 				case IPV6_RECVHOPLIMIT:
1875 					optval = OPTBIT(IN6P_HOPLIMIT);
1876 					break;
1877 
1878 				case IPV6_RECVRTHDR:
1879 					optval = OPTBIT(IN6P_RTHDR);
1880 					break;
1881 
1882 				case IPV6_RECVPATHMTU:
1883 					optval = OPTBIT(IN6P_MTU);
1884 					break;
1885 
1886 				case IPV6_RECVTCLASS:
1887 					optval = OPTBIT(IN6P_TCLASS);
1888 					break;
1889 
1890 				case IPV6_AUTOFLOWLABEL:
1891 					optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1892 					break;
1893 
1894 
1895 				case IPV6_UNICAST_HOPS:
1896 					optval = in6p->in6p_hops;
1897 					break;
1898 
1899 				case IPV6_FAITH:
1900 					optval = OPTBIT(IN6P_FAITH);
1901 					break;
1902 
1903 				case IPV6_V6ONLY:
1904 					optval = OPTBIT(IN6P_IPV6_V6ONLY);
1905 					break;
1906 
1907 				case IPV6_PORTRANGE:
1908 				    {
1909 					int flags;
1910 					flags = in6p->in6p_flags;
1911 					if (flags & IN6P_HIGHPORT)
1912 						optval = IPV6_PORTRANGE_HIGH;
1913 					else if (flags & IN6P_LOWPORT)
1914 						optval = IPV6_PORTRANGE_LOW;
1915 					else
1916 						optval = 0;
1917 					break;
1918 				    }
1919 				}
1920 				soopt_from_kbuf(sopt, &optval,
1921  					sizeof optval);
1922 				break;
1923 
1924 			case IPV6_PATHMTU:
1925 			{
1926 				u_long pmtu = 0;
1927 				struct ip6_mtuinfo mtuinfo;
1928 				struct route_in6 sro;
1929 
1930 				bzero(&sro, sizeof(sro));
1931 
1932 				if (!(so->so_state & SS_ISCONNECTED))
1933 					return (ENOTCONN);
1934 				/*
1935 				 * XXX: we dot not consider the case of source
1936 				 * routing, or optional information to specify
1937 				 * the outgoing interface.
1938 				 */
1939 				error = ip6_getpmtu(&sro, NULL, NULL,
1940 				    &in6p->in6p_faddr, &pmtu, NULL);
1941 				if (sro.ro_rt)
1942 					RTFREE(sro.ro_rt);
1943 				if (error)
1944 					break;
1945 				if (pmtu > IPV6_MAXPACKET)
1946 					pmtu = IPV6_MAXPACKET;
1947 
1948 				bzero(&mtuinfo, sizeof(mtuinfo));
1949 				mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1950 				optdata = (void *)&mtuinfo;
1951 				optdatalen = sizeof(mtuinfo);
1952 				soopt_from_kbuf(sopt, optdata,
1953 				    optdatalen);
1954 				break;
1955 			}
1956 
1957 			case IPV6_2292PKTINFO:
1958 			case IPV6_2292HOPLIMIT:
1959 			case IPV6_2292HOPOPTS:
1960 			case IPV6_2292RTHDR:
1961 			case IPV6_2292DSTOPTS:
1962 				if (optname == IPV6_2292HOPOPTS ||
1963 				    optname == IPV6_2292DSTOPTS ||
1964 				    !privileged)
1965 					return (EPERM);
1966 				switch (optname) {
1967 				case IPV6_2292PKTINFO:
1968 					optval = OPTBIT(IN6P_PKTINFO);
1969 					break;
1970 				case IPV6_2292HOPLIMIT:
1971 					optval = OPTBIT(IN6P_HOPLIMIT);
1972 					break;
1973 				case IPV6_2292HOPOPTS:
1974 					if (!privileged)
1975 						return (EPERM);
1976 					optval = OPTBIT(IN6P_HOPOPTS);
1977 					break;
1978 				case IPV6_2292RTHDR:
1979 					optval = OPTBIT(IN6P_RTHDR);
1980 					break;
1981 				case IPV6_2292DSTOPTS:
1982 					if (!privileged)
1983 						return (EPERM);
1984 					optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1985 					break;
1986 				}
1987 				soopt_from_kbuf(sopt, &optval,
1988  					sizeof optval);
1989 				break;
1990 
1991 			case IPV6_PKTINFO:
1992 			case IPV6_HOPOPTS:
1993 			case IPV6_RTHDR:
1994 			case IPV6_DSTOPTS:
1995 			case IPV6_RTHDRDSTOPTS:
1996 			case IPV6_NEXTHOP:
1997 			case IPV6_TCLASS:
1998 			case IPV6_DONTFRAG:
1999 			case IPV6_USE_MIN_MTU:
2000 			case IPV6_PREFER_TEMPADDR:
2001 				error = ip6_getpcbopt(in6p->in6p_outputopts,
2002 				    optname, sopt);
2003 				break;
2004 
2005 			case IPV6_MULTICAST_IF:
2006 			case IPV6_MULTICAST_HOPS:
2007 			case IPV6_MULTICAST_LOOP:
2008 			case IPV6_JOIN_GROUP:
2009 			case IPV6_LEAVE_GROUP:
2010 			    {
2011 				struct mbuf *m;
2012 				error = ip6_getmoptions(sopt->sopt_name,
2013 						in6p->in6p_moptions, &m);
2014 				if (error == 0)
2015 					soopt_from_kbuf(sopt,
2016  						mtod(m, char *), m->m_len);
2017 				m_freem(m);
2018 			    }
2019 				break;
2020 
2021 #if defined(IPSEC) || defined(FAST_IPSEC)
2022 			case IPV6_IPSEC_POLICY:
2023 			  {
2024 				caddr_t req = NULL;
2025 				size_t len = 0;
2026 				struct mbuf *m = NULL;
2027 				struct mbuf **mp = &m;
2028 
2029 				error = soopt_getm(sopt, &m); /* XXX */
2030 				if (error != 0)
2031 					break;
2032 				soopt_to_mbuf(sopt, m); /* XXX */
2033 				if (m) {
2034 					req = mtod(m, caddr_t);
2035 					len = m->m_len;
2036 				}
2037 				error = ipsec6_get_policy(in6p, req, len, mp);
2038 				if (error == 0)
2039 					error = soopt_from_mbuf(sopt, m); /*XXX*/
2040 				if (error == 0 && m != NULL)
2041 					m_freem(m);
2042 				break;
2043 			  }
2044 #endif /* KAME IPSEC */
2045 
2046 			case IPV6_FW_GET:
2047 			  {
2048 				struct mbuf *m;
2049 				struct mbuf **mp = &m;
2050 
2051 				if (ip6_fw_ctl_ptr == NULL)
2052 				{
2053 					return EINVAL;
2054 				}
2055 				error = (*ip6_fw_ctl_ptr)(optname, mp);
2056 				if (error == 0)
2057 					error = soopt_from_mbuf(sopt, m); /* XXX */
2058 				if (error == 0 && m != NULL)
2059 					m_freem(m);
2060 			  }
2061 				break;
2062 
2063 			default:
2064 				error = ENOPROTOOPT;
2065 				break;
2066 			}
2067 			break;
2068 		}
2069 	} else {
2070 		error = EINVAL;
2071 	}
2072 	return (error);
2073 }
2074 
2075 int
2076 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2077 {
2078 	int error = 0, optval, optlen;
2079 	const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2080 	struct in6pcb *in6p = sotoin6pcb(so);
2081 	int level, op, optname;
2082 
2083 	if (sopt) {
2084 		level = sopt->sopt_level;
2085 		op = sopt->sopt_dir;
2086 		optname = sopt->sopt_name;
2087 		optlen = sopt->sopt_valsize;
2088 	} else
2089 		panic("ip6_raw_ctloutput: arg soopt is NULL");
2090 
2091 	if (level != IPPROTO_IPV6) {
2092 		return (EINVAL);
2093 	}
2094 
2095 	switch (optname) {
2096 	case IPV6_CHECKSUM:
2097 		/*
2098 		 * For ICMPv6 sockets, no modification allowed for checksum
2099 		 * offset, permit "no change" values to help existing apps.
2100 		 *
2101 		 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2102 		 * for an ICMPv6 socket will fail."
2103 		 * The current behavior does not meet RFC3542.
2104 		 */
2105 		switch (op) {
2106 		case SOPT_SET:
2107 			if (optlen != sizeof(int)) {
2108 				error = EINVAL;
2109 				break;
2110 			}
2111 			error = soopt_to_kbuf(sopt, &optval,
2112 				    sizeof optval, sizeof optval);
2113 			if (error)
2114 				break;
2115 			if ((optval % 2) != 0) {
2116 				/* the API assumes even offset values */
2117 				error = EINVAL;
2118 			} else if (so->so_proto->pr_protocol ==
2119 			    IPPROTO_ICMPV6) {
2120 				if (optval != icmp6off)
2121 					error = EINVAL;
2122 			} else
2123 				in6p->in6p_cksum = optval;
2124 			break;
2125 
2126 		case SOPT_GET:
2127 			if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2128 				optval = icmp6off;
2129 			else
2130 				optval = in6p->in6p_cksum;
2131 
2132 			soopt_from_kbuf(sopt, &optval, sizeof(optval));
2133 			break;
2134 
2135 		default:
2136 			error = EINVAL;
2137 			break;
2138 		}
2139 		break;
2140 
2141 	default:
2142 		error = ENOPROTOOPT;
2143 		break;
2144 	}
2145 
2146 	return (error);
2147 }
2148 
2149 /*
2150  * Set up IP6 options in pcb for insertion in output packets or
2151  * specifying behavior of outgoing packets.
2152  */
2153 static int
2154 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2155     struct socket *so, struct sockopt *sopt)
2156 {
2157 	int priv = 0;
2158 	struct ip6_pktopts *opt = *pktopt;
2159 	int error = 0;
2160 
2161 	/* turn off any old options. */
2162 	if (opt) {
2163 #ifdef DIAGNOSTIC
2164 		if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2165 		    opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2166 		    opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2167 			kprintf("ip6_pcbopts: all specified options are cleared.\n");
2168 #endif
2169 		ip6_clearpktopts(opt, -1);
2170 	} else
2171 		opt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2172 	*pktopt = NULL;
2173 
2174 	if (!m || m->m_len == 0) {
2175 		/*
2176 		 * Only turning off any previous options, regardless of
2177 		 * whether the opt is just created or given.
2178 		 */
2179 		kfree(opt, M_IP6OPT);
2180 		return (0);
2181 	}
2182 
2183 	/*  set options specified by user. */
2184 	if ((error = ip6_setpktoptions(m, opt, NULL, so->so_proto->pr_protocol, priv)) != 0) {
2185 		ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2186 		kfree(opt, M_IP6OPT);
2187 		return (error);
2188 	}
2189 	*pktopt = opt;
2190 	return (0);
2191 }
2192 
2193 
2194 /*
2195  * Below three functions are introduced by merge to RFC3542
2196  */
2197 
2198 static int
2199 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2200 {
2201 	void *optdata = NULL;
2202 	int optdatalen = 0;
2203 	struct ip6_ext *ip6e;
2204 	int error = 0;
2205 	struct in6_pktinfo null_pktinfo;
2206 	int deftclass = 0, on;
2207 	int defminmtu = IP6PO_MINMTU_MCASTONLY;
2208 	int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2209 
2210 	switch (optname) {
2211 	case IPV6_PKTINFO:
2212 		if (pktopt && pktopt->ip6po_pktinfo)
2213 			optdata = (void *)pktopt->ip6po_pktinfo;
2214 		else {
2215 			/* XXX: we don't have to do this every time... */
2216 			bzero(&null_pktinfo, sizeof(null_pktinfo));
2217 			optdata = (void *)&null_pktinfo;
2218 		}
2219 		optdatalen = sizeof(struct in6_pktinfo);
2220 		break;
2221 	case IPV6_TCLASS:
2222 		if (pktopt && pktopt->ip6po_tclass >= 0)
2223 			optdata = (void *)&pktopt->ip6po_tclass;
2224 		else
2225 			optdata = (void *)&deftclass;
2226 		optdatalen = sizeof(int);
2227 		break;
2228 	case IPV6_HOPOPTS:
2229 		if (pktopt && pktopt->ip6po_hbh) {
2230 			optdata = (void *)pktopt->ip6po_hbh;
2231 			ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2232 			optdatalen = (ip6e->ip6e_len + 1) << 3;
2233 		}
2234 		break;
2235 	case IPV6_RTHDR:
2236 		if (pktopt && pktopt->ip6po_rthdr) {
2237 			optdata = (void *)pktopt->ip6po_rthdr;
2238 			ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2239 			optdatalen = (ip6e->ip6e_len + 1) << 3;
2240 		}
2241 		break;
2242 	case IPV6_RTHDRDSTOPTS:
2243 		if (pktopt && pktopt->ip6po_dest1) {
2244 			optdata = (void *)pktopt->ip6po_dest1;
2245 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2246 			optdatalen = (ip6e->ip6e_len + 1) << 3;
2247 		}
2248 		break;
2249 	case IPV6_DSTOPTS:
2250 		if (pktopt && pktopt->ip6po_dest2) {
2251 			optdata = (void *)pktopt->ip6po_dest2;
2252 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2253 			optdatalen = (ip6e->ip6e_len + 1) << 3;
2254 		}
2255 		break;
2256 	case IPV6_NEXTHOP:
2257 		if (pktopt && pktopt->ip6po_nexthop) {
2258 			optdata = (void *)pktopt->ip6po_nexthop;
2259 			optdatalen = pktopt->ip6po_nexthop->sa_len;
2260 		}
2261 		break;
2262 	case IPV6_USE_MIN_MTU:
2263 		if (pktopt)
2264 			optdata = (void *)&pktopt->ip6po_minmtu;
2265 		else
2266 			optdata = (void *)&defminmtu;
2267 		optdatalen = sizeof(int);
2268 		break;
2269 	case IPV6_DONTFRAG:
2270 		if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2271 			on = 1;
2272 		else
2273 			on = 0;
2274 		optdata = (void *)&on;
2275 		optdatalen = sizeof(on);
2276 		break;
2277 	case IPV6_PREFER_TEMPADDR:
2278 		if (pktopt)
2279 			optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2280 		else
2281 			optdata = (void *)&defpreftemp;
2282 		optdatalen = sizeof(int);
2283 		break;
2284 	default:		/* should not happen */
2285 #ifdef DIAGNOSTIC
2286 		panic("ip6_getpcbopt: unexpected option");
2287 #endif
2288 		return (ENOPROTOOPT);
2289 	}
2290 
2291 	soopt_from_kbuf(sopt, optdata, optdatalen);
2292 
2293 	return (error);
2294 }
2295 
2296 /*
2297  * initialize ip6_pktopts.  beware that there are non-zero default values in
2298  * the struct.
2299  */
2300 
2301 static int
2302 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, int uproto)
2303 {
2304 	struct ip6_pktopts *opt;
2305 	int priv =0;
2306 	if (*pktopt == NULL) {
2307 		*pktopt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2308 		init_ip6pktopts(*pktopt);
2309 	}
2310 	opt = *pktopt;
2311 
2312 	return (ip6_setpktoption(optname, buf, len, opt, 1, 0, uproto, priv));
2313 }
2314 
2315 /*
2316  * initialize ip6_pktopts.  beware that there are non-zero default values in
2317  * the struct.
2318  */
2319 void
2320 init_ip6pktopts(struct ip6_pktopts *opt)
2321 {
2322 
2323 	bzero(opt, sizeof(*opt));
2324 	opt->ip6po_hlim = -1;	/* -1 means default hop limit */
2325 	opt->ip6po_tclass = -1;	/* -1 means default traffic class */
2326 	opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2327 	opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2328 }
2329 
2330 void
2331 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2332 {
2333 	if (pktopt == NULL)
2334 		return;
2335 
2336 	if (optname == -1 || optname == IPV6_PKTINFO) {
2337 		if (pktopt->ip6po_pktinfo)
2338 			kfree(pktopt->ip6po_pktinfo, M_IP6OPT);
2339 		pktopt->ip6po_pktinfo = NULL;
2340 	}
2341 	if (optname == -1 || optname == IPV6_HOPLIMIT)
2342 		pktopt->ip6po_hlim = -1;
2343 	if (optname == -1 || optname == IPV6_TCLASS)
2344 		pktopt->ip6po_tclass = -1;
2345 	if (optname == -1 || optname == IPV6_NEXTHOP) {
2346 		if (pktopt->ip6po_nextroute.ro_rt) {
2347 			RTFREE(pktopt->ip6po_nextroute.ro_rt);
2348 			pktopt->ip6po_nextroute.ro_rt = NULL;
2349 		}
2350 		if (pktopt->ip6po_nexthop)
2351 			kfree(pktopt->ip6po_nexthop, M_IP6OPT);
2352 		pktopt->ip6po_nexthop = NULL;
2353 	}
2354 	if (optname == -1 || optname == IPV6_HOPOPTS) {
2355 		if (pktopt->ip6po_hbh)
2356 			kfree(pktopt->ip6po_hbh, M_IP6OPT);
2357 		pktopt->ip6po_hbh = NULL;
2358 	}
2359 	if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2360 		if (pktopt->ip6po_dest1)
2361 			kfree(pktopt->ip6po_dest1, M_IP6OPT);
2362 		pktopt->ip6po_dest1 = NULL;
2363 	}
2364 	if (optname == -1 || optname == IPV6_RTHDR) {
2365 		if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2366 			kfree(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2367 		pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2368 		if (pktopt->ip6po_route.ro_rt) {
2369 			RTFREE(pktopt->ip6po_route.ro_rt);
2370 			pktopt->ip6po_route.ro_rt = NULL;
2371 		}
2372 	}
2373 	if (optname == -1 || optname == IPV6_DSTOPTS) {
2374 		if (pktopt->ip6po_dest2)
2375 			kfree(pktopt->ip6po_dest2, M_IP6OPT);
2376 		pktopt->ip6po_dest2 = NULL;
2377 	}
2378 }
2379 
2380 #define PKTOPT_EXTHDRCPY(type) \
2381 do {\
2382 	if (src->type) {\
2383 		int hlen =\
2384 			(((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2385 		dst->type = kmalloc(hlen, M_IP6OPT, canwait);\
2386 		if (dst->type == NULL)\
2387 			goto bad;\
2388 		bcopy(src->type, dst->type, hlen);\
2389 	}\
2390 } while (0)
2391 
2392 struct ip6_pktopts *
2393 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2394 {
2395 	struct ip6_pktopts *dst;
2396 
2397 	if (src == NULL) {
2398 		kprintf("ip6_clearpktopts: invalid argument\n");
2399 		return (NULL);
2400 	}
2401 
2402 	dst = kmalloc(sizeof(*dst), M_IP6OPT, canwait | M_ZERO);
2403 	if (dst == NULL)
2404 		return (NULL);
2405 
2406 	dst->ip6po_hlim = src->ip6po_hlim;
2407 	if (src->ip6po_pktinfo) {
2408 		dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
2409 					    M_IP6OPT, canwait);
2410 		if (dst->ip6po_pktinfo == NULL)
2411 			goto bad;
2412 		*dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2413 	}
2414 	if (src->ip6po_nexthop) {
2415 		dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
2416 					    M_IP6OPT, canwait);
2417 		if (dst->ip6po_nexthop == NULL)
2418 			goto bad;
2419 		bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2420 		      src->ip6po_nexthop->sa_len);
2421 	}
2422 	PKTOPT_EXTHDRCPY(ip6po_hbh);
2423 	PKTOPT_EXTHDRCPY(ip6po_dest1);
2424 	PKTOPT_EXTHDRCPY(ip6po_dest2);
2425 	PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2426 	return (dst);
2427 
2428 bad:
2429 	if (dst->ip6po_pktinfo) kfree(dst->ip6po_pktinfo, M_IP6OPT);
2430 	if (dst->ip6po_nexthop) kfree(dst->ip6po_nexthop, M_IP6OPT);
2431 	if (dst->ip6po_hbh) kfree(dst->ip6po_hbh, M_IP6OPT);
2432 	if (dst->ip6po_dest1) kfree(dst->ip6po_dest1, M_IP6OPT);
2433 	if (dst->ip6po_dest2) kfree(dst->ip6po_dest2, M_IP6OPT);
2434 	if (dst->ip6po_rthdr) kfree(dst->ip6po_rthdr, M_IP6OPT);
2435 	kfree(dst, M_IP6OPT);
2436 	return (NULL);
2437 }
2438 
2439 static int
2440 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2441 {
2442 	if (dst == NULL || src == NULL)  {
2443 #ifdef DIAGNOSTIC
2444 		kprintf("ip6_clearpktopts: invalid argument\n");
2445 #endif
2446 		return (EINVAL);
2447 	}
2448 
2449 	dst->ip6po_hlim = src->ip6po_hlim;
2450 	dst->ip6po_tclass = src->ip6po_tclass;
2451 	dst->ip6po_flags = src->ip6po_flags;
2452 	if (src->ip6po_pktinfo) {
2453 		dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
2454 		    M_IP6OPT, canwait);
2455 		if (dst->ip6po_pktinfo == NULL)
2456 			goto bad;
2457 		*dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2458 	}
2459 	if (src->ip6po_nexthop) {
2460 		dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
2461 		    M_IP6OPT, canwait);
2462 		if (dst->ip6po_nexthop == NULL)
2463 			goto bad;
2464 		bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2465 		    src->ip6po_nexthop->sa_len);
2466 	}
2467 	PKTOPT_EXTHDRCPY(ip6po_hbh);
2468 	PKTOPT_EXTHDRCPY(ip6po_dest1);
2469 	PKTOPT_EXTHDRCPY(ip6po_dest2);
2470 	PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2471 	return (0);
2472 
2473   bad:
2474 	ip6_clearpktopts(dst, -1);
2475 	return (ENOBUFS);
2476 }
2477 #undef PKTOPT_EXTHDRCPY
2478 
2479 void
2480 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2481 {
2482 	if (pktopt == NULL)
2483 		return;
2484 
2485 	ip6_clearpktopts(pktopt, -1);
2486 
2487 	kfree(pktopt, M_IP6OPT);
2488 }
2489 
2490 /*
2491  * Set the IP6 multicast options in response to user setsockopt().
2492  */
2493 static int
2494 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2495 {
2496 	int error = 0;
2497 	u_int loop, ifindex;
2498 	struct ipv6_mreq *mreq;
2499 	struct ifnet *ifp;
2500 	struct ip6_moptions *im6o = *im6op;
2501 	struct route_in6 ro;
2502 	struct sockaddr_in6 *dst;
2503 	struct in6_multi_mship *imm;
2504 	struct thread *td = curthread;	/* XXX */
2505 
2506 	if (im6o == NULL) {
2507 		/*
2508 		 * No multicast option buffer attached to the pcb;
2509 		 * allocate one and initialize to default values.
2510 		 */
2511 		im6o = (struct ip6_moptions *)
2512 			kmalloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2513 
2514 		*im6op = im6o;
2515 		im6o->im6o_multicast_ifp = NULL;
2516 		im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2517 		im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2518 		LIST_INIT(&im6o->im6o_memberships);
2519 	}
2520 
2521 	switch (optname) {
2522 
2523 	case IPV6_MULTICAST_IF:
2524 		/*
2525 		 * Select the interface for outgoing multicast packets.
2526 		 */
2527 		if (m == NULL || m->m_len != sizeof(u_int)) {
2528 			error = EINVAL;
2529 			break;
2530 		}
2531 		bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2532 		if (ifindex < 0 || if_index < ifindex) {
2533 			error = ENXIO;	/* XXX EINVAL? */
2534 			break;
2535 		}
2536 		ifp = ifindex2ifnet[ifindex];
2537 		if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2538 			error = EADDRNOTAVAIL;
2539 			break;
2540 		}
2541 		im6o->im6o_multicast_ifp = ifp;
2542 		break;
2543 
2544 	case IPV6_MULTICAST_HOPS:
2545 	    {
2546 		/*
2547 		 * Set the IP6 hoplimit for outgoing multicast packets.
2548 		 */
2549 		int optval;
2550 		if (m == NULL || m->m_len != sizeof(int)) {
2551 			error = EINVAL;
2552 			break;
2553 		}
2554 		bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2555 		if (optval < -1 || optval >= 256)
2556 			error = EINVAL;
2557 		else if (optval == -1)
2558 			im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2559 		else
2560 			im6o->im6o_multicast_hlim = optval;
2561 		break;
2562 	    }
2563 
2564 	case IPV6_MULTICAST_LOOP:
2565 		/*
2566 		 * Set the loopback flag for outgoing multicast packets.
2567 		 * Must be zero or one.
2568 		 */
2569 		if (m == NULL || m->m_len != sizeof(u_int)) {
2570 			error = EINVAL;
2571 			break;
2572 		}
2573 		bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2574 		if (loop > 1) {
2575 			error = EINVAL;
2576 			break;
2577 		}
2578 		im6o->im6o_multicast_loop = loop;
2579 		break;
2580 
2581 	case IPV6_JOIN_GROUP:
2582 		/*
2583 		 * Add a multicast group membership.
2584 		 * Group must be a valid IP6 multicast address.
2585 		 */
2586 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2587 			error = EINVAL;
2588 			break;
2589 		}
2590 		mreq = mtod(m, struct ipv6_mreq *);
2591 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2592 			/*
2593 			 * We use the unspecified address to specify to accept
2594 			 * all multicast addresses. Only super user is allowed
2595 			 * to do this.
2596 			 */
2597 			if (priv_check(td, PRIV_ROOT))
2598 			{
2599 				error = EACCES;
2600 				break;
2601 			}
2602 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2603 			error = EINVAL;
2604 			break;
2605 		}
2606 
2607 		/*
2608 		 * If the interface is specified, validate it.
2609 		 */
2610 		if (mreq->ipv6mr_interface < 0
2611 		 || if_index < mreq->ipv6mr_interface) {
2612 			error = ENXIO;	/* XXX EINVAL? */
2613 			break;
2614 		}
2615 		/*
2616 		 * If no interface was explicitly specified, choose an
2617 		 * appropriate one according to the given multicast address.
2618 		 */
2619 		if (mreq->ipv6mr_interface == 0) {
2620 			/*
2621 			 * If the multicast address is in node-local scope,
2622 			 * the interface should be a loopback interface.
2623 			 * Otherwise, look up the routing table for the
2624 			 * address, and choose the outgoing interface.
2625 			 *   XXX: is it a good approach?
2626 			 */
2627 			if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
2628 				ifp = &loif[0];
2629 			} else {
2630 				ro.ro_rt = NULL;
2631 				dst = (struct sockaddr_in6 *)&ro.ro_dst;
2632 				bzero(dst, sizeof(*dst));
2633 				dst->sin6_len = sizeof(struct sockaddr_in6);
2634 				dst->sin6_family = AF_INET6;
2635 				dst->sin6_addr = mreq->ipv6mr_multiaddr;
2636 				rtalloc((struct route *)&ro);
2637 				if (ro.ro_rt == NULL) {
2638 					error = EADDRNOTAVAIL;
2639 					break;
2640 				}
2641 				ifp = ro.ro_rt->rt_ifp;
2642 				rtfree(ro.ro_rt);
2643 			}
2644 		} else
2645 			ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2646 
2647 		/*
2648 		 * See if we found an interface, and confirm that it
2649 		 * supports multicast
2650 		 */
2651 		if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2652 			error = EADDRNOTAVAIL;
2653 			break;
2654 		}
2655 		/*
2656 		 * Put interface index into the multicast address,
2657 		 * if the address has link-local scope.
2658 		 */
2659 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2660 			mreq->ipv6mr_multiaddr.s6_addr16[1]
2661 				= htons(mreq->ipv6mr_interface);
2662 		}
2663 		/*
2664 		 * See if the membership already exists.
2665 		 */
2666 		for (imm = im6o->im6o_memberships.lh_first;
2667 		     imm != NULL; imm = imm->i6mm_chain.le_next)
2668 			if (imm->i6mm_maddr->in6m_ifp == ifp &&
2669 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2670 					       &mreq->ipv6mr_multiaddr))
2671 				break;
2672 		if (imm != NULL) {
2673 			error = EADDRINUSE;
2674 			break;
2675 		}
2676 		/*
2677 		 * Everything looks good; add a new record to the multicast
2678 		 * address list for the given interface.
2679 		 */
2680 		imm = kmalloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2681 		if ((imm->i6mm_maddr =
2682 		     in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2683 			kfree(imm, M_IPMADDR);
2684 			break;
2685 		}
2686 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2687 		break;
2688 
2689 	case IPV6_LEAVE_GROUP:
2690 		/*
2691 		 * Drop a multicast group membership.
2692 		 * Group must be a valid IP6 multicast address.
2693 		 */
2694 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2695 			error = EINVAL;
2696 			break;
2697 		}
2698 		mreq = mtod(m, struct ipv6_mreq *);
2699 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2700 			if (priv_check(td, PRIV_ROOT)) {
2701 				error = EACCES;
2702 				break;
2703 			}
2704 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2705 			error = EINVAL;
2706 			break;
2707 		}
2708 		/*
2709 		 * If an interface address was specified, get a pointer
2710 		 * to its ifnet structure.
2711 		 */
2712 		if (mreq->ipv6mr_interface < 0
2713 		 || if_index < mreq->ipv6mr_interface) {
2714 			error = ENXIO;	/* XXX EINVAL? */
2715 			break;
2716 		}
2717 		ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2718 		/*
2719 		 * Put interface index into the multicast address,
2720 		 * if the address has link-local scope.
2721 		 */
2722 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2723 			mreq->ipv6mr_multiaddr.s6_addr16[1]
2724 				= htons(mreq->ipv6mr_interface);
2725 		}
2726 		/*
2727 		 * Find the membership in the membership list.
2728 		 */
2729 		for (imm = im6o->im6o_memberships.lh_first;
2730 		     imm != NULL; imm = imm->i6mm_chain.le_next) {
2731 			if ((ifp == NULL ||
2732 			     imm->i6mm_maddr->in6m_ifp == ifp) &&
2733 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2734 					       &mreq->ipv6mr_multiaddr))
2735 				break;
2736 		}
2737 		if (imm == NULL) {
2738 			/* Unable to resolve interface */
2739 			error = EADDRNOTAVAIL;
2740 			break;
2741 		}
2742 		/*
2743 		 * Give up the multicast address record to which the
2744 		 * membership points.
2745 		 */
2746 		LIST_REMOVE(imm, i6mm_chain);
2747 		in6_delmulti(imm->i6mm_maddr);
2748 		kfree(imm, M_IPMADDR);
2749 		break;
2750 
2751 	default:
2752 		error = EOPNOTSUPP;
2753 		break;
2754 	}
2755 
2756 	/*
2757 	 * If all options have default values, no need to keep the mbuf.
2758 	 */
2759 	if (im6o->im6o_multicast_ifp == NULL &&
2760 	    im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2761 	    im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2762 	    im6o->im6o_memberships.lh_first == NULL) {
2763 		kfree(*im6op, M_IPMOPTS);
2764 		*im6op = NULL;
2765 	}
2766 
2767 	return (error);
2768 }
2769 
2770 /*
2771  * Return the IP6 multicast options in response to user getsockopt().
2772  */
2773 static int
2774 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2775 {
2776 	u_int *hlim, *loop, *ifindex;
2777 
2778 	*mp = m_get(MB_WAIT, MT_HEADER);		/* XXX */
2779 
2780 	switch (optname) {
2781 
2782 	case IPV6_MULTICAST_IF:
2783 		ifindex = mtod(*mp, u_int *);
2784 		(*mp)->m_len = sizeof(u_int);
2785 		if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2786 			*ifindex = 0;
2787 		else
2788 			*ifindex = im6o->im6o_multicast_ifp->if_index;
2789 		return (0);
2790 
2791 	case IPV6_MULTICAST_HOPS:
2792 		hlim = mtod(*mp, u_int *);
2793 		(*mp)->m_len = sizeof(u_int);
2794 		if (im6o == NULL)
2795 			*hlim = ip6_defmcasthlim;
2796 		else
2797 			*hlim = im6o->im6o_multicast_hlim;
2798 		return (0);
2799 
2800 	case IPV6_MULTICAST_LOOP:
2801 		loop = mtod(*mp, u_int *);
2802 		(*mp)->m_len = sizeof(u_int);
2803 		if (im6o == NULL)
2804 			*loop = ip6_defmcasthlim;
2805 		else
2806 			*loop = im6o->im6o_multicast_loop;
2807 		return (0);
2808 
2809 	default:
2810 		return (EOPNOTSUPP);
2811 	}
2812 }
2813 
2814 /*
2815  * Discard the IP6 multicast options.
2816  */
2817 void
2818 ip6_freemoptions(struct ip6_moptions *im6o)
2819 {
2820 	struct in6_multi_mship *imm;
2821 
2822 	if (im6o == NULL)
2823 		return;
2824 
2825 	while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2826 		LIST_REMOVE(imm, i6mm_chain);
2827 		if (imm->i6mm_maddr)
2828 			in6_delmulti(imm->i6mm_maddr);
2829 		kfree(imm, M_IPMADDR);
2830 	}
2831 	kfree(im6o, M_IPMOPTS);
2832 }
2833 
2834 /*
2835  * Set a particular packet option, as a sticky option or an ancillary data
2836  * item.  "len" can be 0 only when it's a sticky option.
2837  * We have 4 cases of combination of "sticky" and "cmsg":
2838  * "sticky=0, cmsg=0": impossible
2839  * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2840  * "sticky=1, cmsg=0": RFC3542 socket option
2841  * "sticky=1, cmsg=1": RFC2292 socket option
2842  */
2843 static int
2844 ip6_setpktoption(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2845      int sticky, int cmsg, int uproto, int priv)
2846 {
2847 	int minmtupolicy, preftemp;
2848 	//int error;
2849 
2850 	if (!sticky && !cmsg) {
2851 		kprintf("ip6_setpktoption: impossible case\n");
2852 		return (EINVAL);
2853 	}
2854 
2855 	/*
2856 	 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2857 	 * not be specified in the context of RFC3542.  Conversely,
2858 	 * RFC3542 types should not be specified in the context of RFC2292.
2859 	 */
2860 	if (!cmsg) {
2861 		switch (optname) {
2862 		case IPV6_2292PKTINFO:
2863 		case IPV6_2292HOPLIMIT:
2864 		case IPV6_2292NEXTHOP:
2865 		case IPV6_2292HOPOPTS:
2866 		case IPV6_2292DSTOPTS:
2867 		case IPV6_2292RTHDR:
2868 		case IPV6_2292PKTOPTIONS:
2869 			return (ENOPROTOOPT);
2870 		}
2871 	}
2872 	if (sticky && cmsg) {
2873 		switch (optname) {
2874 		case IPV6_PKTINFO:
2875 		case IPV6_HOPLIMIT:
2876 		case IPV6_NEXTHOP:
2877 		case IPV6_HOPOPTS:
2878 		case IPV6_DSTOPTS:
2879 		case IPV6_RTHDRDSTOPTS:
2880 		case IPV6_RTHDR:
2881 		case IPV6_USE_MIN_MTU:
2882 		case IPV6_DONTFRAG:
2883 		case IPV6_TCLASS:
2884 		case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2885 			return (ENOPROTOOPT);
2886 		}
2887 	}
2888 
2889 	switch (optname) {
2890 	case IPV6_2292PKTINFO:
2891 	case IPV6_PKTINFO:
2892 	{
2893 		struct in6_pktinfo *pktinfo;
2894 		if (len != sizeof(struct in6_pktinfo))
2895 			return (EINVAL);
2896 		pktinfo = (struct in6_pktinfo *)buf;
2897 
2898 		/*
2899 		 * An application can clear any sticky IPV6_PKTINFO option by
2900 		 * doing a "regular" setsockopt with ipi6_addr being
2901 		 * in6addr_any and ipi6_ifindex being zero.
2902 		 * [RFC 3542, Section 6]
2903 		 */
2904 		if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2905 		    pktinfo->ipi6_ifindex == 0 &&
2906 		    IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2907 			ip6_clearpktopts(opt, optname);
2908 			break;
2909 		}
2910 
2911 		if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2912 		    sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2913 			return (EINVAL);
2914 		}
2915 
2916 		/* validate the interface index if specified. */
2917 		if (pktinfo->ipi6_ifindex > if_index ||
2918 		    pktinfo->ipi6_ifindex < 0) {
2919 			 return (ENXIO);
2920 		}
2921 		/*
2922 		 * Check if the requested source address is indeed a
2923 		 * unicast address assigned to the node, and can be
2924 		 * used as the packet's source address.
2925 		 */
2926 		if (opt->ip6po_pktinfo != NULL &&
2927 		    !IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2928 			struct in6_ifaddr *ia6;
2929 			struct sockaddr_in6 sin6;
2930 
2931 			bzero(&sin6, sizeof(sin6));
2932 			sin6.sin6_len = sizeof(sin6);
2933 			sin6.sin6_family = AF_INET6;
2934 			sin6.sin6_addr =
2935 			opt->ip6po_pktinfo->ipi6_addr;
2936 			ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2937 			if (ia6 == NULL ||
2938 				(ia6->ia6_flags & (IN6_IFF_ANYCAST |
2939 					IN6_IFF_NOTREADY)) != 0)
2940 			return (EADDRNOTAVAIL);
2941 		}
2942 
2943 		/*
2944 		 * We store the address anyway, and let in6_selectsrc()
2945 		 * validate the specified address.  This is because ipi6_addr
2946 		 * may not have enough information about its scope zone, and
2947 		 * we may need additional information (such as outgoing
2948 		 * interface or the scope zone of a destination address) to
2949 		 * disambiguate the scope.
2950 		 * XXX: the delay of the validation may confuse the
2951 		 * application when it is used as a sticky option.
2952 		 */
2953 		if (opt->ip6po_pktinfo == NULL) {
2954 			opt->ip6po_pktinfo = kmalloc(sizeof(*pktinfo),
2955 			    M_IP6OPT, M_NOWAIT);
2956 			if (opt->ip6po_pktinfo == NULL)
2957 				return (ENOBUFS);
2958 		}
2959 		bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2960 		break;
2961 	}
2962 
2963 	case IPV6_2292HOPLIMIT:
2964 	case IPV6_HOPLIMIT:
2965 	{
2966 		int *hlimp;
2967 
2968 		/*
2969 		 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2970 		 * to simplify the ordering among hoplimit options.
2971 		 */
2972 		if (optname == IPV6_HOPLIMIT && sticky)
2973 			return (ENOPROTOOPT);
2974 
2975 		if (len != sizeof(int))
2976 			return (EINVAL);
2977 		hlimp = (int *)buf;
2978 		if (*hlimp < -1 || *hlimp > 255)
2979 			return (EINVAL);
2980 
2981 		opt->ip6po_hlim = *hlimp;
2982 		break;
2983 	}
2984 
2985 	case IPV6_TCLASS:
2986 	{
2987 		int tclass;
2988 
2989 		if (len != sizeof(int))
2990 			return (EINVAL);
2991 		tclass = *(int *)buf;
2992 		if (tclass < -1 || tclass > 255)
2993 			return (EINVAL);
2994 
2995 		opt->ip6po_tclass = tclass;
2996 		break;
2997 	}
2998 
2999 	case IPV6_2292NEXTHOP:
3000 	case IPV6_NEXTHOP:
3001 		if (!priv)
3002 			return (EPERM);
3003 
3004 		if (len == 0) {	/* just remove the option */
3005 			ip6_clearpktopts(opt, IPV6_NEXTHOP);
3006 			break;
3007 		}
3008 
3009 		/* check if cmsg_len is large enough for sa_len */
3010 		if (len < sizeof(struct sockaddr) || len < *buf)
3011 			return (EINVAL);
3012 
3013 		switch (((struct sockaddr *)buf)->sa_family) {
3014 		case AF_INET6:
3015 		{
3016 			struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3017 			//int error;
3018 
3019 			if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3020 				return (EINVAL);
3021 
3022 			if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3023 			    IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3024 				return (EINVAL);
3025 			}
3026 			break;
3027 		}
3028 		case AF_LINK:	/* should eventually be supported */
3029 		default:
3030 			return (EAFNOSUPPORT);
3031 		}
3032 
3033 		/* turn off the previous option, then set the new option. */
3034 		ip6_clearpktopts(opt, IPV6_NEXTHOP);
3035 		opt->ip6po_nexthop = kmalloc(*buf, M_IP6OPT, M_NOWAIT);
3036 		if (opt->ip6po_nexthop == NULL)
3037 			return (ENOBUFS);
3038 		bcopy(buf, opt->ip6po_nexthop, *buf);
3039 		break;
3040 
3041 	case IPV6_2292HOPOPTS:
3042 	case IPV6_HOPOPTS:
3043 	{
3044 		struct ip6_hbh *hbh;
3045 		int hbhlen;
3046 
3047 		/*
3048 		 * XXX: We don't allow a non-privileged user to set ANY HbH
3049 		 * options, since per-option restriction has too much
3050 		 * overhead.
3051 		 */
3052 		if (!priv)
3053 			return (EPERM);
3054 		if (len == 0) {
3055 			ip6_clearpktopts(opt, IPV6_HOPOPTS);
3056 			break;	/* just remove the option */
3057 		}
3058 
3059 		/* message length validation */
3060 		if (len < sizeof(struct ip6_hbh))
3061 			return (EINVAL);
3062 		hbh = (struct ip6_hbh *)buf;
3063 		hbhlen = (hbh->ip6h_len + 1) << 3;
3064 		if (len != hbhlen)
3065 			return (EINVAL);
3066 
3067 		/* turn off the previous option, then set the new option. */
3068 		ip6_clearpktopts(opt, IPV6_HOPOPTS);
3069 		opt->ip6po_hbh = kmalloc(hbhlen, M_IP6OPT, M_NOWAIT);
3070 		if (opt->ip6po_hbh == NULL)
3071 			return (ENOBUFS);
3072 		bcopy(hbh, opt->ip6po_hbh, hbhlen);
3073 
3074 		break;
3075 	}
3076 
3077 	case IPV6_2292DSTOPTS:
3078 	case IPV6_DSTOPTS:
3079 	case IPV6_RTHDRDSTOPTS:
3080 	{
3081 		struct ip6_dest *dest, **newdest = NULL;
3082 		int destlen;
3083 		if (!priv)
3084 			return (EPERM);
3085 
3086 		if (len == 0) {
3087 			ip6_clearpktopts(opt, optname);
3088 			break;	/* just remove the option */
3089 		}
3090 
3091 		/* message length validation */
3092 		if (len < sizeof(struct ip6_dest))
3093 			return (EINVAL);
3094 		dest = (struct ip6_dest *)buf;
3095 		destlen = (dest->ip6d_len + 1) << 3;
3096 		if (len != destlen)
3097 			return (EINVAL);
3098 
3099 		/*
3100 		 * Determine the position that the destination options header
3101 		 * should be inserted; before or after the routing header.
3102 		 */
3103 		switch (optname) {
3104 		case IPV6_2292DSTOPTS:
3105 			/*
3106 			 * The old advacned API is ambiguous on this point.
3107 			 * Our approach is to determine the position based
3108 			 * according to the existence of a routing header.
3109 			 * Note, however, that this depends on the order of the
3110 			 * extension headers in the ancillary data; the 1st
3111 			 * part of the destination options header must appear
3112 			 * before the routing header in the ancillary data,
3113 			 * too.
3114 			 * RFC3542 solved the ambiguity by introducing
3115 			 * separate ancillary data or option types.
3116 			 */
3117 			if (opt->ip6po_rthdr == NULL)
3118 				newdest = &opt->ip6po_dest1;
3119 			else
3120 				newdest = &opt->ip6po_dest2;
3121 			break;
3122 		case IPV6_RTHDRDSTOPTS:
3123 			newdest = &opt->ip6po_dest1;
3124 			break;
3125 		case IPV6_DSTOPTS:
3126 			newdest = &opt->ip6po_dest2;
3127 			break;
3128 		}
3129 
3130 		/* turn off the previous option, then set the new option. */
3131 		ip6_clearpktopts(opt, optname);
3132 		*newdest = kmalloc(destlen, M_IP6OPT, M_NOWAIT);
3133 		if (*newdest == NULL)
3134 			return (ENOBUFS);
3135 		bcopy(dest, *newdest, destlen);
3136 
3137 		break;
3138 	}
3139 
3140 	case IPV6_2292RTHDR:
3141 	case IPV6_RTHDR:
3142 	{
3143 		struct ip6_rthdr *rth;
3144 		int rthlen;
3145 
3146 		if (len == 0) {
3147 			ip6_clearpktopts(opt, IPV6_RTHDR);
3148 			break;	/* just remove the option */
3149 		}
3150 
3151 		/* message length validation */
3152 		if (len < sizeof(struct ip6_rthdr))
3153 			return (EINVAL);
3154 		rth = (struct ip6_rthdr *)buf;
3155 		rthlen = (rth->ip6r_len + 1) << 3;
3156 		if (len != rthlen)
3157 			return (EINVAL);
3158 
3159 		switch (rth->ip6r_type) {
3160 		default:
3161 			return (EINVAL);	/* not supported */
3162 		}
3163 
3164 		/* turn off the previous option */
3165 		ip6_clearpktopts(opt, IPV6_RTHDR);
3166 		opt->ip6po_rthdr = kmalloc(rthlen, M_IP6OPT, M_NOWAIT);
3167 		if (opt->ip6po_rthdr == NULL)
3168 			return (ENOBUFS);
3169 		bcopy(rth, opt->ip6po_rthdr, rthlen);
3170 
3171 		break;
3172 	}
3173 
3174 	case IPV6_USE_MIN_MTU:
3175 		if (len != sizeof(int))
3176 			return (EINVAL);
3177 		minmtupolicy = *(int *)buf;
3178 		if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3179 		    minmtupolicy != IP6PO_MINMTU_DISABLE &&
3180 		    minmtupolicy != IP6PO_MINMTU_ALL) {
3181 			return (EINVAL);
3182 		}
3183 		opt->ip6po_minmtu = minmtupolicy;
3184 		break;
3185 
3186 	case IPV6_DONTFRAG:
3187 		if (len != sizeof(int))
3188 			return (EINVAL);
3189 
3190 		if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3191 			/*
3192 			 * we ignore this option for TCP sockets.
3193 			 * (RFC3542 leaves this case unspecified.)
3194 			 */
3195 			opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3196 		} else
3197 			opt->ip6po_flags |= IP6PO_DONTFRAG;
3198 		break;
3199 
3200 	case IPV6_PREFER_TEMPADDR:
3201 		if (len != sizeof(int))
3202 			return (EINVAL);
3203 		preftemp = *(int *)buf;
3204 		if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3205 		    preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3206 		    preftemp != IP6PO_TEMPADDR_PREFER) {
3207 			return (EINVAL);
3208 		}
3209 		opt->ip6po_prefer_tempaddr = preftemp;
3210 		break;
3211 
3212 	default:
3213 		return (ENOPROTOOPT);
3214 	} /* end of switch */
3215 
3216 	return (0);
3217 }
3218 
3219 
3220 /*
3221  * Set IPv6 outgoing packet options based on advanced API.
3222  */
3223 int
3224 ip6_setpktoptions(struct mbuf *control, struct ip6_pktopts *opt,
3225     struct ip6_pktopts *stickyopt, int uproto, int priv)
3226 {
3227 	struct cmsghdr *cm = NULL;
3228 
3229 	if (control == NULL || opt == NULL)
3230 		return (EINVAL);
3231 
3232 	init_ip6pktopts(opt);
3233 
3234 	/*
3235 	 * XXX: Currently, we assume all the optional information is stored
3236 	 * in a single mbuf.
3237 	 */
3238 	if (stickyopt) {
3239 		int error;
3240 
3241 		/*
3242 		 * If stickyopt is provided, make a local copy of the options
3243 		 * for this particular packet, then override them by ancillary
3244 		 * objects.
3245 		 * XXX: copypktopts() does not copy the cached route to a next
3246 		 * hop (if any).  This is not very good in terms of efficiency,
3247 		 * but we can allow this since this option should be rarely
3248 		 * used.
3249 		 */
3250 		if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
3251 			return (error);
3252 	}
3253 
3254 	/*
3255 	 * XXX: Currently, we assume all the optional information is stored
3256 	 * in a single mbuf.
3257 	 */
3258 	if (control->m_next)
3259 		return (EINVAL);
3260 
3261 	for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
3262 	    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
3263 		int error;
3264 
3265 		if (control->m_len < CMSG_LEN(0))
3266 			return (EINVAL);
3267 
3268 		cm = mtod(control, struct cmsghdr *);
3269 		if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
3270 			return (EINVAL);
3271 		if (cm->cmsg_level != IPPROTO_IPV6)
3272 			continue;
3273 
3274 		error = ip6_setpktoption(cm->cmsg_type, CMSG_DATA(cm),
3275 		    cm->cmsg_len - CMSG_LEN(0), opt, 0, 1, uproto, priv);
3276 		if (error)
3277 			return (error);
3278 	}
3279 
3280 	return (0);
3281 }
3282 
3283 /*
3284  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3285  * packet to the input queue of a specified interface.  Note that this
3286  * calls the output routine of the loopback "driver", but with an interface
3287  * pointer that might NOT be &loif -- easier than replicating that code here.
3288  */
3289 void
3290 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
3291 {
3292 	struct mbuf *copym;
3293 	struct ip6_hdr *ip6;
3294 
3295 	copym = m_copy(m, 0, M_COPYALL);
3296 	if (copym == NULL)
3297 		return;
3298 
3299 	/*
3300 	 * Make sure to deep-copy IPv6 header portion in case the data
3301 	 * is in an mbuf cluster, so that we can safely override the IPv6
3302 	 * header portion later.
3303 	 */
3304 	if ((copym->m_flags & M_EXT) != 0 ||
3305 	    copym->m_len < sizeof(struct ip6_hdr)) {
3306 		copym = m_pullup(copym, sizeof(struct ip6_hdr));
3307 		if (copym == NULL)
3308 			return;
3309 	}
3310 
3311 #ifdef DIAGNOSTIC
3312 	if (copym->m_len < sizeof(*ip6)) {
3313 		m_freem(copym);
3314 		return;
3315 	}
3316 #endif
3317 
3318 	ip6 = mtod(copym, struct ip6_hdr *);
3319 	/*
3320 	 * clear embedded scope identifiers if necessary.
3321 	 * in6_clearscope will touch the addresses only when necessary.
3322 	 */
3323 	in6_clearscope(&ip6->ip6_src);
3324 	in6_clearscope(&ip6->ip6_dst);
3325 
3326 	if_simloop(ifp, copym, dst->sin6_family, 0);
3327 }
3328 
3329 /*
3330  * Separate the IPv6 header from the payload into its own mbuf.
3331  *
3332  * Returns the new mbuf chain or the original mbuf if no payload.
3333  * Returns NULL if can't allocate new mbuf for header.
3334  */
3335 static struct mbuf *
3336 ip6_splithdr(struct mbuf *m)
3337 {
3338 	struct mbuf *mh;
3339 
3340 	if (m->m_len <= sizeof(struct ip6_hdr))		/* no payload */
3341 		return (m);
3342 
3343 	MGETHDR(mh, MB_DONTWAIT, MT_HEADER);
3344 	if (mh == NULL)
3345 		return (NULL);
3346 	mh->m_len = sizeof(struct ip6_hdr);
3347 	M_MOVE_PKTHDR(mh, m);
3348 	MH_ALIGN(mh, sizeof(struct ip6_hdr));
3349 	bcopy(mtod(m, caddr_t), mtod(mh, caddr_t), sizeof(struct ip6_hdr));
3350 	m->m_data += sizeof(struct ip6_hdr);
3351 	m->m_len -= sizeof(struct ip6_hdr);
3352 	mh->m_next = m;
3353 	return (mh);
3354 }
3355 
3356 /*
3357  * Compute IPv6 extension header length.
3358  */
3359 int
3360 ip6_optlen(struct in6pcb *in6p)
3361 {
3362 	int len;
3363 
3364 	if (!in6p->in6p_outputopts)
3365 		return 0;
3366 
3367 	len = 0;
3368 #define elen(x) \
3369     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3370 
3371 	len += elen(in6p->in6p_outputopts->ip6po_hbh);
3372 	if (in6p->in6p_outputopts->ip6po_rthdr)
3373 		/* dest1 is valid with rthdr only */
3374 		len += elen(in6p->in6p_outputopts->ip6po_dest1);
3375 	len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3376 	len += elen(in6p->in6p_outputopts->ip6po_dest2);
3377 	return len;
3378 #undef elen
3379 }
3380