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