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