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