xref: /openbsd-src/sys/netinet6/ip6_output.c (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1 /*	$OpenBSD: ip6_output.c,v 1.279 2023/07/07 08:05:02 bluhm 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. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
62  */
63 
64 #include "pf.h"
65 
66 #include <sys/param.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.h>
69 #include <sys/errno.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/proc.h>
74 #include <sys/systm.h>
75 
76 #include <net/if.h>
77 #include <net/if_var.h>
78 #include <net/if_enc.h>
79 #include <net/route.h>
80 
81 #include <netinet/in.h>
82 #include <netinet/ip.h>
83 #include <netinet/in_pcb.h>
84 #include <netinet/udp.h>
85 #include <netinet/tcp.h>
86 
87 #include <netinet/ip_var.h>
88 #include <netinet/tcp_timer.h>
89 #include <netinet/tcp_var.h>
90 #include <netinet/udp_var.h>
91 
92 #include <netinet6/in6_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet/icmp6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/nd6.h>
97 
98 #include <crypto/idgen.h>
99 
100 #if NPF > 0
101 #include <net/pfvar.h>
102 #endif
103 
104 #ifdef IPSEC
105 #include <netinet/ip_ipsp.h>
106 #include <netinet/ip_ah.h>
107 #include <netinet/ip_esp.h>
108 
109 #ifdef ENCDEBUG
110 #define DPRINTF(fmt, args...)						\
111 	do {								\
112 		if (encdebug)						\
113 			printf("%s: " fmt "\n", __func__, ## args);	\
114 	} while (0)
115 #else
116 #define DPRINTF(fmt, args...)						\
117 	do { } while (0)
118 #endif
119 #endif /* IPSEC */
120 
121 struct ip6_exthdrs {
122 	struct mbuf *ip6e_ip6;
123 	struct mbuf *ip6e_hbh;
124 	struct mbuf *ip6e_dest1;
125 	struct mbuf *ip6e_rthdr;
126 	struct mbuf *ip6e_dest2;
127 };
128 
129 int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **, int, int);
130 int ip6_getpcbopt(struct ip6_pktopts *, int, struct mbuf *);
131 int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, int, int, int);
132 int ip6_setmoptions(int, struct ip6_moptions **, struct mbuf *, unsigned int);
133 int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf *);
134 int ip6_copyexthdr(struct mbuf **, caddr_t, int);
135 int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
136 	struct ip6_frag **);
137 int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
138 int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
139 int ip6_getpmtu(struct rtentry *, struct ifnet *, u_long *);
140 int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *);
141 static __inline u_int16_t __attribute__((__unused__))
142     in6_cksum_phdr(const struct in6_addr *, const struct in6_addr *,
143     u_int32_t, u_int32_t);
144 void in6_delayed_cksum(struct mbuf *, u_int8_t);
145 
146 int ip6_output_ipsec_pmtu_update(struct tdb *, struct route_in6 *,
147     struct in6_addr *, int, int, int);
148 
149 /* Context for non-repeating IDs */
150 struct idgen32_ctx ip6_id_ctx;
151 
152 /*
153  * IP6 output. The packet in mbuf chain m contains a skeletal IP6
154  * header (with pri, len, nxt, hlim, src, dst).
155  * This function may modify ver and hlim only.
156  * The mbuf chain containing the packet will be freed.
157  * The mbuf opt, if present, will not be freed.
158  *
159  * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int.
160  * We use u_long to hold largest one, * which is rt_mtu.
161  */
162 int
163 ip6_output(struct mbuf *m, struct ip6_pktopts *opt, struct route_in6 *ro,
164     int flags, struct ip6_moptions *im6o, struct inpcb *inp)
165 {
166 	struct ip6_hdr *ip6;
167 	struct ifnet *ifp = NULL;
168 	struct mbuf_list ml;
169 	int hlen, tlen;
170 	struct route_in6 ip6route;
171 	struct rtentry *rt = NULL;
172 	struct sockaddr_in6 *dst, dstsock;
173 	int error = 0;
174 	u_long mtu;
175 	int dontfrag;
176 	u_int16_t src_scope, dst_scope;
177 	u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
178 	struct ip6_exthdrs exthdrs;
179 	struct in6_addr finaldst;
180 	struct route_in6 *ro_pmtu = NULL;
181 	int hdrsplit = 0;
182 	u_int8_t sproto = 0;
183 	u_char nextproto;
184 #ifdef IPSEC
185 	struct tdb *tdb = NULL;
186 #endif /* IPSEC */
187 
188 #ifdef IPSEC
189 	if (inp && (inp->inp_flags & INP_IPV6) == 0)
190 		panic("%s: IPv4 pcb is passed", __func__);
191 #endif /* IPSEC */
192 
193 	ip6 = mtod(m, struct ip6_hdr *);
194 	finaldst = ip6->ip6_dst;
195 
196 #define MAKE_EXTHDR(hp, mp)						\
197     do {								\
198 	if (hp) {							\
199 		struct ip6_ext *eh = (struct ip6_ext *)(hp);		\
200 		error = ip6_copyexthdr((mp), (caddr_t)(hp),		\
201 		    ((eh)->ip6e_len + 1) << 3);				\
202 		if (error)						\
203 			goto freehdrs;					\
204 	}								\
205     } while (0)
206 
207 	bzero(&exthdrs, sizeof(exthdrs));
208 
209 	if (opt) {
210 		/* Hop-by-Hop options header */
211 		MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
212 		/* Destination options header(1st part) */
213 		MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
214 		/* Routing header */
215 		MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
216 		/* Destination options header(2nd part) */
217 		MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
218 	}
219 
220 #ifdef IPSEC
221 	if (ipsec_in_use || inp != NULL) {
222 		error = ip6_output_ipsec_lookup(m, inp, &tdb);
223 		if (error) {
224 			/*
225 			 * -EINVAL is used to indicate that the packet should
226 			 * be silently dropped, typically because we've asked
227 			 * key management for an SA.
228 			 */
229 			if (error == -EINVAL) /* Should silently drop packet */
230 				error = 0;
231 
232 			goto freehdrs;
233 		}
234 	}
235 #endif /* IPSEC */
236 
237 	/*
238 	 * Calculate the total length of the extension header chain.
239 	 * Keep the length of the unfragmentable part for fragmentation.
240 	 */
241 	optlen = 0;
242 	if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
243 	if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
244 	if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
245 	unfragpartlen = optlen + sizeof(struct ip6_hdr);
246 	/* NOTE: we don't add AH/ESP length here. do that later. */
247 	if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
248 
249 	/*
250 	 * If we need IPsec, or there is at least one extension header,
251 	 * separate IP6 header from the payload.
252 	 */
253 	if ((sproto || optlen) && !hdrsplit) {
254 		if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
255 			m = NULL;
256 			goto freehdrs;
257 		}
258 		m = exthdrs.ip6e_ip6;
259 		hdrsplit++;
260 	}
261 
262 	/* adjust pointer */
263 	ip6 = mtod(m, struct ip6_hdr *);
264 
265 	/* adjust mbuf packet header length */
266 	m->m_pkthdr.len += optlen;
267 	plen = m->m_pkthdr.len - sizeof(*ip6);
268 
269 	/* If this is a jumbo payload, insert a jumbo payload option. */
270 	if (plen > IPV6_MAXPACKET) {
271 		if (!hdrsplit) {
272 			if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
273 				m = NULL;
274 				goto freehdrs;
275 			}
276 			m = exthdrs.ip6e_ip6;
277 			hdrsplit++;
278 		}
279 		/* adjust pointer */
280 		ip6 = mtod(m, struct ip6_hdr *);
281 		if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
282 			goto freehdrs;
283 		ip6->ip6_plen = 0;
284 	} else
285 		ip6->ip6_plen = htons(plen);
286 
287 	/*
288 	 * Concatenate headers and fill in next header fields.
289 	 * Here we have, on "m"
290 	 *	IPv6 payload
291 	 * and we insert headers accordingly.  Finally, we should be getting:
292 	 *	IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
293 	 *
294 	 * during the header composing process, "m" points to IPv6 header.
295 	 * "mprev" points to an extension header prior to esp.
296 	 */
297 	{
298 		u_char *nexthdrp = &ip6->ip6_nxt;
299 		struct mbuf *mprev = m;
300 
301 		/*
302 		 * we treat dest2 specially.  this makes IPsec processing
303 		 * much easier.  the goal here is to make mprev point the
304 		 * mbuf prior to dest2.
305 		 *
306 		 * result: IPv6 dest2 payload
307 		 * m and mprev will point to IPv6 header.
308 		 */
309 		if (exthdrs.ip6e_dest2) {
310 			if (!hdrsplit)
311 				panic("%s: assumption failed: hdr not split",
312 				    __func__);
313 			exthdrs.ip6e_dest2->m_next = m->m_next;
314 			m->m_next = exthdrs.ip6e_dest2;
315 			*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
316 			ip6->ip6_nxt = IPPROTO_DSTOPTS;
317 		}
318 
319 #define MAKE_CHAIN(m, mp, p, i)\
320     do {\
321 	if (m) {\
322 		if (!hdrsplit) \
323 			panic("assumption failed: hdr not split"); \
324 		*mtod((m), u_char *) = *(p);\
325 		*(p) = (i);\
326 		p = mtod((m), u_char *);\
327 		(m)->m_next = (mp)->m_next;\
328 		(mp)->m_next = (m);\
329 		(mp) = (m);\
330 	}\
331     } while (0)
332 		/*
333 		 * result: IPv6 hbh dest1 rthdr dest2 payload
334 		 * m will point to IPv6 header.  mprev will point to the
335 		 * extension header prior to dest2 (rthdr in the above case).
336 		 */
337 		MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
338 		MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
339 		    IPPROTO_DSTOPTS);
340 		MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
341 		    IPPROTO_ROUTING);
342 	}
343 
344 	/*
345 	 * If there is a routing header, replace the destination address field
346 	 * with the first hop of the routing header.
347 	 */
348 	if (exthdrs.ip6e_rthdr) {
349 		struct ip6_rthdr *rh;
350 		struct ip6_rthdr0 *rh0;
351 		struct in6_addr *addr;
352 
353 		rh = (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
354 		    struct ip6_rthdr *));
355 		switch (rh->ip6r_type) {
356 		case IPV6_RTHDR_TYPE_0:
357 			rh0 = (struct ip6_rthdr0 *)rh;
358 			addr = (struct in6_addr *)(rh0 + 1);
359 			ip6->ip6_dst = addr[0];
360 			bcopy(&addr[1], &addr[0],
361 			    sizeof(struct in6_addr) * (rh0->ip6r0_segleft - 1));
362 			addr[rh0->ip6r0_segleft - 1] = finaldst;
363 			break;
364 		default:	/* is it possible? */
365 			error = EINVAL;
366 			goto bad;
367 		}
368 	}
369 
370 	/* Source address validation */
371 	if (!(flags & IPV6_UNSPECSRC) &&
372 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
373 		/*
374 		 * XXX: we can probably assume validation in the caller, but
375 		 * we explicitly check the address here for safety.
376 		 */
377 		error = EOPNOTSUPP;
378 		ip6stat_inc(ip6s_badscope);
379 		goto bad;
380 	}
381 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
382 		error = EOPNOTSUPP;
383 		ip6stat_inc(ip6s_badscope);
384 		goto bad;
385 	}
386 
387 	ip6stat_inc(ip6s_localout);
388 
389 	/*
390 	 * Route packet.
391 	 */
392 #if NPF > 0
393 reroute:
394 #endif
395 
396 	/* initialize cached route */
397 	if (ro == NULL) {
398 		ro = &ip6route;
399 		bzero((caddr_t)ro, sizeof(*ro));
400 	}
401 	ro_pmtu = ro;
402 	if (opt && opt->ip6po_rthdr)
403 		ro = &opt->ip6po_route;
404 	dst = &ro->ro_dst;
405 
406 	/*
407 	 * if specified, try to fill in the traffic class field.
408 	 * do not override if a non-zero value is already set.
409 	 * we check the diffserv field and the ecn field separately.
410 	 */
411 	if (opt && opt->ip6po_tclass >= 0) {
412 		int mask = 0;
413 
414 		if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
415 			mask |= 0xfc;
416 		if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
417 			mask |= 0x03;
418 		if (mask != 0)
419 			ip6->ip6_flow |=
420 			    htonl((opt->ip6po_tclass & mask) << 20);
421 	}
422 
423 	/* fill in or override the hop limit field, if necessary. */
424 	if (opt && opt->ip6po_hlim != -1)
425 		ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
426 	else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
427 		if (im6o != NULL)
428 			ip6->ip6_hlim = im6o->im6o_hlim;
429 		else
430 			ip6->ip6_hlim = ip6_defmcasthlim;
431 	}
432 
433 #ifdef IPSEC
434 	if (tdb != NULL) {
435 		/*
436 		 * XXX what should we do if ip6_hlim == 0 and the
437 		 * packet gets tunneled?
438 		 */
439 		/*
440 		 * if we are source-routing, do not attempt to tunnel the
441 		 * packet just because ip6_dst is different from what tdb has.
442 		 * XXX
443 		 */
444 		error = ip6_output_ipsec_send(tdb, m, ro,
445 		    exthdrs.ip6e_rthdr ? 1 : 0, 0);
446 		goto done;
447 	}
448 #endif /* IPSEC */
449 
450 	bzero(&dstsock, sizeof(dstsock));
451 	dstsock.sin6_family = AF_INET6;
452 	dstsock.sin6_addr = ip6->ip6_dst;
453 	dstsock.sin6_len = sizeof(dstsock);
454 	ro->ro_tableid = m->m_pkthdr.ph_rtableid;
455 
456 	if (IN6_IS_ADDR_MULTICAST(&dstsock.sin6_addr)) {
457 		struct in6_pktinfo *pi = NULL;
458 
459 		/*
460 		 * If the caller specify the outgoing interface
461 		 * explicitly, use it.
462 		 */
463 		if (opt != NULL && (pi = opt->ip6po_pktinfo) != NULL)
464 			ifp = if_get(pi->ipi6_ifindex);
465 
466 		if (ifp == NULL && im6o != NULL)
467 			ifp = if_get(im6o->im6o_ifidx);
468 	}
469 
470 	if (ifp == NULL) {
471 		rt = in6_selectroute(&dstsock, opt, ro, ro->ro_tableid);
472 		if (rt == NULL) {
473 			ip6stat_inc(ip6s_noroute);
474 			error = EHOSTUNREACH;
475 			goto bad;
476 		}
477 		if (ISSET(rt->rt_flags, RTF_LOCAL))
478 			ifp = if_get(rtable_loindex(m->m_pkthdr.ph_rtableid));
479 		else
480 			ifp = if_get(rt->rt_ifidx);
481 		/*
482 		 * We aren't using rtisvalid() here because the UP/DOWN state
483 		 * machine is broken with some Ethernet drivers like em(4).
484 		 * As a result we might try to use an invalid cached route
485 		 * entry while an interface is being detached.
486 		 */
487 		if (ifp == NULL) {
488 			ip6stat_inc(ip6s_noroute);
489 			error = EHOSTUNREACH;
490 			goto bad;
491 		}
492 	} else {
493 		*dst = dstsock;
494 	}
495 
496 	if (rt && (rt->rt_flags & RTF_GATEWAY) &&
497 	    !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
498 		dst = satosin6(rt->rt_gateway);
499 
500 	if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
501 		/* Unicast */
502 
503 		m->m_flags &= ~(M_BCAST | M_MCAST);	/* just in case */
504 	} else {
505 		/* Multicast */
506 
507 		m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
508 
509 		/*
510 		 * Confirm that the outgoing interface supports multicast.
511 		 */
512 		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
513 			ip6stat_inc(ip6s_noroute);
514 			error = ENETUNREACH;
515 			goto bad;
516 		}
517 
518 		if ((im6o == NULL || im6o->im6o_loop) &&
519 		    in6_hasmulti(&ip6->ip6_dst, ifp)) {
520 			/*
521 			 * If we belong to the destination multicast group
522 			 * on the outgoing interface, and the caller did not
523 			 * forbid loopback, loop back a copy.
524 			 * Can't defer TCP/UDP checksumming, do the
525 			 * computation now.
526 			 */
527 			in6_proto_cksum_out(m, NULL);
528 			ip6_mloopback(ifp, m, dst);
529 		}
530 #ifdef MROUTING
531 		else {
532 			/*
533 			 * If we are acting as a multicast router, perform
534 			 * multicast forwarding as if the packet had just
535 			 * arrived on the interface to which we are about
536 			 * to send.  The multicast forwarding function
537 			 * recursively calls this function, using the
538 			 * IPV6_FORWARDING flag to prevent infinite recursion.
539 			 *
540 			 * Multicasts that are looped back by ip6_mloopback(),
541 			 * above, will be forwarded by the ip6_input() routine,
542 			 * if necessary.
543 			 */
544 			if (ip6_mforwarding && ip6_mrouter[ifp->if_rdomain] &&
545 			    (flags & IPV6_FORWARDING) == 0) {
546 				if (ip6_mforward(ip6, ifp, m) != 0) {
547 					m_freem(m);
548 					goto done;
549 				}
550 			}
551 		}
552 #endif
553 		/*
554 		 * Multicasts with a hoplimit of zero may be looped back,
555 		 * above, but must not be transmitted on a network.
556 		 * Also, multicasts addressed to the loopback interface
557 		 * are not sent -- the above call to ip6_mloopback() will
558 		 * loop back a copy if this host actually belongs to the
559 		 * destination group on the loopback interface.
560 		 */
561 		if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
562 		    IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
563 			m_freem(m);
564 			goto done;
565 		}
566 	}
567 
568 	/*
569 	 * If this packet is going through a loopback interface we won't
570 	 * be able to restore its scope ID using the interface index.
571 	 */
572 	if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) {
573 		if (ifp->if_flags & IFF_LOOPBACK)
574 			src_scope = ip6->ip6_src.s6_addr16[1];
575 		ip6->ip6_src.s6_addr16[1] = 0;
576 	}
577 	if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) {
578 		if (ifp->if_flags & IFF_LOOPBACK)
579 			dst_scope = ip6->ip6_dst.s6_addr16[1];
580 		ip6->ip6_dst.s6_addr16[1] = 0;
581 	}
582 
583 	/* Determine path MTU. */
584 	if ((error = ip6_getpmtu(ro_pmtu->ro_rt, ifp, &mtu)) != 0)
585 		goto bad;
586 
587 	/*
588 	 * The caller of this function may specify to use the minimum MTU
589 	 * in some cases.
590 	 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
591 	 * setting.  The logic is a bit complicated; by default, unicast
592 	 * packets will follow path MTU while multicast packets will be sent at
593 	 * the minimum MTU.  If IP6PO_MINMTU_ALL is specified, all packets
594 	 * including unicast ones will be sent at the minimum MTU.  Multicast
595 	 * packets will always be sent at the minimum MTU unless
596 	 * IP6PO_MINMTU_DISABLE is explicitly specified.
597 	 * See RFC 3542 for more details.
598 	 */
599 	if (mtu > IPV6_MMTU) {
600 		if ((flags & IPV6_MINMTU))
601 			mtu = IPV6_MMTU;
602 		else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
603 			mtu = IPV6_MMTU;
604 		else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && (opt == NULL ||
605 		    opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
606 			mtu = IPV6_MMTU;
607 		}
608 	}
609 
610 	/*
611 	 * If the outgoing packet contains a hop-by-hop options header,
612 	 * it must be examined and processed even by the source node.
613 	 * (RFC 2460, section 4.)
614 	 */
615 	if (exthdrs.ip6e_hbh) {
616 		struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
617 		u_int32_t rtalert; /* returned value is ignored */
618 		u_int32_t plen = 0; /* no more than 1 jumbo payload option! */
619 
620 		m->m_pkthdr.ph_ifidx = ifp->if_index;
621 		if (ip6_process_hopopts(&m, (u_int8_t *)(hbh + 1),
622 		    ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
623 		    &rtalert, &plen) < 0) {
624 			/* m was already freed at this point */
625 			error = EINVAL;/* better error? */
626 			goto done;
627 		}
628 		m->m_pkthdr.ph_ifidx = 0;
629 	}
630 
631 #if NPF > 0
632 	if (pf_test(AF_INET6, PF_OUT, ifp, &m) != PF_PASS) {
633 		error = EACCES;
634 		m_freem(m);
635 		goto done;
636 	}
637 	if (m == NULL)
638 		goto done;
639 	ip6 = mtod(m, struct ip6_hdr *);
640 	if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) ==
641 	    (PF_TAG_REROUTE | PF_TAG_GENERATED)) {
642 		/* already rerun the route lookup, go on */
643 		m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE);
644 	} else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) {
645 		/* tag as generated to skip over pf_test on rerun */
646 		m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
647 		finaldst = ip6->ip6_dst;
648 		ro = NULL;
649 		if_put(ifp); /* drop reference since destination changed */
650 		ifp = NULL;
651 		goto reroute;
652 	}
653 #endif
654 
655 	/*
656 	 * If the packet is not going on the wire it can be destined
657 	 * to any local address.  In this case do not clear its scopes
658 	 * to let ip6_input() find a matching local route.
659 	 */
660 	if (ifp->if_flags & IFF_LOOPBACK) {
661 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
662 			ip6->ip6_src.s6_addr16[1] = src_scope;
663 		if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
664 			ip6->ip6_dst.s6_addr16[1] = dst_scope;
665 	}
666 
667 	/*
668 	 * Send the packet to the outgoing interface.
669 	 * If necessary, do IPv6 fragmentation before sending.
670 	 *
671 	 * the logic here is rather complex:
672 	 * 1: normal case (dontfrag == 0)
673 	 * 1-a: send as is if tlen <= path mtu
674 	 * 1-b: fragment if tlen > path mtu
675 	 *
676 	 * 2: if user asks us not to fragment (dontfrag == 1)
677 	 * 2-a: send as is if tlen <= interface mtu
678 	 * 2-b: error if tlen > interface mtu
679 	 */
680 	tlen = ISSET(m->m_pkthdr.csum_flags, M_TCP_TSO) ?
681 	    m->m_pkthdr.ph_mss : m->m_pkthdr.len;
682 
683 	if (ISSET(m->m_pkthdr.csum_flags, M_IPV6_DF_OUT)) {
684 		CLR(m->m_pkthdr.csum_flags, M_IPV6_DF_OUT);
685 		dontfrag = 1;
686 	} else if (opt && ISSET(opt->ip6po_flags, IP6PO_DONTFRAG))
687 		dontfrag = 1;
688 	else
689 		dontfrag = 0;
690 
691 	if (dontfrag && tlen > ifp->if_mtu) {		/* case 2-b */
692 #ifdef IPSEC
693 		if (ip_mtudisc)
694 			ipsec_adjust_mtu(m, mtu);
695 #endif
696 		error = EMSGSIZE;
697 		goto bad;
698 	}
699 
700 	/*
701 	 * transmit packet without fragmentation
702 	 */
703 	if (dontfrag || tlen <= mtu) {			/* case 1-a and 2-a */
704 		error = if_output_tso(ifp, &m, sin6tosa(dst), ro->ro_rt,
705 		    ifp->if_mtu);
706 		if (error || m == NULL)
707 			goto done;
708 		goto bad;				/* should not happen */
709 	}
710 
711 	/*
712 	 * try to fragment the packet.  case 1-b
713 	 */
714 	if (mtu < IPV6_MMTU) {
715 		/* path MTU cannot be less than IPV6_MMTU */
716 		error = EMSGSIZE;
717 		goto bad;
718 	} else if (ip6->ip6_plen == 0) {
719 		/* jumbo payload cannot be fragmented */
720 		error = EMSGSIZE;
721 		goto bad;
722 	}
723 
724 	/*
725 	 * Too large for the destination or interface;
726 	 * fragment if possible.
727 	 * Must be able to put at least 8 bytes per fragment.
728 	 */
729 	hlen = unfragpartlen;
730 	if (mtu > IPV6_MAXPACKET)
731 		mtu = IPV6_MAXPACKET;
732 
733 	/*
734 	 * If we are doing fragmentation, we can't defer TCP/UDP
735 	 * checksumming; compute the checksum and clear the flag.
736 	 */
737         in6_proto_cksum_out(m, NULL);
738 
739 	/*
740 	 * Change the next header field of the last header in the
741 	 * unfragmentable part.
742 	 */
743 	if (exthdrs.ip6e_rthdr) {
744 		nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
745 		*mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
746 	} else if (exthdrs.ip6e_dest1) {
747 		nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
748 		*mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
749 	} else if (exthdrs.ip6e_hbh) {
750 		nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
751 		*mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
752 	} else {
753 		nextproto = ip6->ip6_nxt;
754 		ip6->ip6_nxt = IPPROTO_FRAGMENT;
755 	}
756 
757 	if ((error = ip6_fragment(m, &ml, hlen, nextproto, mtu)) ||
758 	    (error = if_output_ml(ifp, &ml, sin6tosa(dst), ro->ro_rt)))
759 		goto done;
760 	ip6stat_inc(ip6s_fragmented);
761 
762 done:
763 	if (ro == &ip6route && ro->ro_rt) {
764 		rtfree(ro->ro_rt);
765 	} else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
766 		rtfree(ro_pmtu->ro_rt);
767 	}
768 	if_put(ifp);
769 #ifdef IPSEC
770 	tdb_unref(tdb);
771 #endif /* IPSEC */
772 	return (error);
773 
774 freehdrs:
775 	m_freem(exthdrs.ip6e_hbh);	/* m_freem will check if mbuf is 0 */
776 	m_freem(exthdrs.ip6e_dest1);
777 	m_freem(exthdrs.ip6e_rthdr);
778 	m_freem(exthdrs.ip6e_dest2);
779 	/* FALLTHROUGH */
780 bad:
781 	m_freem(m);
782 	goto done;
783 }
784 
785 int
786 ip6_fragment(struct mbuf *m0, struct mbuf_list *ml, int hlen, u_char nextproto,
787     u_long mtu)
788 {
789 	struct ip6_hdr *ip6;
790 	u_int32_t id;
791 	int tlen, len, off;
792 	int error;
793 
794 	ml_init(ml);
795 
796 	ip6 = mtod(m0, struct ip6_hdr *);
797 	tlen = m0->m_pkthdr.len;
798 	len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
799 	if (len < 8) {
800 		error = EMSGSIZE;
801 		goto bad;
802 	}
803 	id = htonl(ip6_randomid());
804 
805 	/*
806 	 * Loop through length of payload,
807 	 * make new header and copy data of each part and link onto chain.
808 	 */
809 	for (off = hlen; off < tlen; off += len) {
810 		struct mbuf *m;
811 		struct mbuf *mlast;
812 		struct ip6_hdr *mhip6;
813 		struct ip6_frag *ip6f;
814 
815 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
816 		if (m == NULL) {
817 			error = ENOBUFS;
818 			goto bad;
819 		}
820 		ml_enqueue(ml, m);
821 		if ((error = m_dup_pkthdr(m, m0, M_DONTWAIT)) != 0)
822 			goto bad;
823 		m->m_data += max_linkhdr;
824 		mhip6 = mtod(m, struct ip6_hdr *);
825 		*mhip6 = *ip6;
826 		m->m_len = sizeof(struct ip6_hdr);
827 
828 		if ((error = ip6_insertfraghdr(m0, m, hlen, &ip6f)) != 0)
829 			goto bad;
830 		ip6f->ip6f_offlg = htons((off - hlen) & ~7);
831 		if (off + len >= tlen)
832 			len = tlen - off;
833 		else
834 			ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
835 
836 		m->m_pkthdr.len = hlen + sizeof(struct ip6_frag) + len;
837 		mhip6->ip6_plen = htons(m->m_pkthdr.len -
838 		    sizeof(struct ip6_hdr));
839 		for (mlast = m; mlast->m_next; mlast = mlast->m_next)
840 			;
841 		mlast->m_next = m_copym(m0, off, len, M_DONTWAIT);
842 		if (mlast->m_next == NULL) {
843 			error = ENOBUFS;
844 			goto bad;
845 		}
846 
847 		ip6f->ip6f_reserved = 0;
848 		ip6f->ip6f_ident = id;
849 		ip6f->ip6f_nxt = nextproto;
850 	}
851 
852 	ip6stat_add(ip6s_ofragments, ml_len(ml));
853 	m_freem(m0);
854 	return (0);
855 
856 bad:
857 	ip6stat_inc(ip6s_odropped);
858 	ml_purge(ml);
859 	m_freem(m0);
860 	return (error);
861 }
862 
863 int
864 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
865 {
866 	struct mbuf *m;
867 
868 	if (hlen > MCLBYTES)
869 		return (ENOBUFS); /* XXX */
870 
871 	MGET(m, M_DONTWAIT, MT_DATA);
872 	if (!m)
873 		return (ENOBUFS);
874 
875 	if (hlen > MLEN) {
876 		MCLGET(m, M_DONTWAIT);
877 		if ((m->m_flags & M_EXT) == 0) {
878 			m_free(m);
879 			return (ENOBUFS);
880 		}
881 	}
882 	m->m_len = hlen;
883 	if (hdr)
884 		memcpy(mtod(m, caddr_t), hdr, hlen);
885 
886 	*mp = m;
887 	return (0);
888 }
889 
890 /*
891  * Insert jumbo payload option.
892  */
893 int
894 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
895 {
896 	struct mbuf *mopt;
897 	u_int8_t *optbuf;
898 	u_int32_t v;
899 
900 #define JUMBOOPTLEN	8	/* length of jumbo payload option and padding */
901 
902 	/*
903 	 * If there is no hop-by-hop options header, allocate new one.
904 	 * If there is one but it doesn't have enough space to store the
905 	 * jumbo payload option, allocate a cluster to store the whole options.
906 	 * Otherwise, use it to store the options.
907 	 */
908 	if (exthdrs->ip6e_hbh == 0) {
909 		MGET(mopt, M_DONTWAIT, MT_DATA);
910 		if (mopt == NULL)
911 			return (ENOBUFS);
912 		mopt->m_len = JUMBOOPTLEN;
913 		optbuf = mtod(mopt, u_int8_t *);
914 		optbuf[1] = 0;	/* = ((JUMBOOPTLEN) >> 3) - 1 */
915 		exthdrs->ip6e_hbh = mopt;
916 	} else {
917 		struct ip6_hbh *hbh;
918 
919 		mopt = exthdrs->ip6e_hbh;
920 		if (m_trailingspace(mopt) < JUMBOOPTLEN) {
921 			/*
922 			 * XXX assumption:
923 			 * - exthdrs->ip6e_hbh is not referenced from places
924 			 *   other than exthdrs.
925 			 * - exthdrs->ip6e_hbh is not an mbuf chain.
926 			 */
927 			int oldoptlen = mopt->m_len;
928 			struct mbuf *n;
929 
930 			/*
931 			 * XXX: give up if the whole (new) hbh header does
932 			 * not fit even in an mbuf cluster.
933 			 */
934 			if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
935 				return (ENOBUFS);
936 
937 			/*
938 			 * As a consequence, we must always prepare a cluster
939 			 * at this point.
940 			 */
941 			MGET(n, M_DONTWAIT, MT_DATA);
942 			if (n) {
943 				MCLGET(n, M_DONTWAIT);
944 				if ((n->m_flags & M_EXT) == 0) {
945 					m_freem(n);
946 					n = NULL;
947 				}
948 			}
949 			if (!n)
950 				return (ENOBUFS);
951 			n->m_len = oldoptlen + JUMBOOPTLEN;
952 			memcpy(mtod(n, caddr_t), mtod(mopt, caddr_t),
953 			      oldoptlen);
954 			optbuf = mtod(n, u_int8_t *) + oldoptlen;
955 			m_freem(mopt);
956 			mopt = exthdrs->ip6e_hbh = n;
957 		} else {
958 			optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
959 			mopt->m_len += JUMBOOPTLEN;
960 		}
961 		optbuf[0] = IP6OPT_PADN;
962 		optbuf[1] = 0;
963 
964 		/*
965 		 * Adjust the header length according to the pad and
966 		 * the jumbo payload option.
967 		 */
968 		hbh = mtod(mopt, struct ip6_hbh *);
969 		hbh->ip6h_len += (JUMBOOPTLEN >> 3);
970 	}
971 
972 	/* fill in the option. */
973 	optbuf[2] = IP6OPT_JUMBO;
974 	optbuf[3] = 4;
975 	v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
976 	memcpy(&optbuf[4], &v, sizeof(u_int32_t));
977 
978 	/* finally, adjust the packet header length */
979 	exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
980 
981 	return (0);
982 #undef JUMBOOPTLEN
983 }
984 
985 /*
986  * Insert fragment header and copy unfragmentable header portions.
987  */
988 int
989 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
990     struct ip6_frag **frghdrp)
991 {
992 	struct mbuf *n, *mlast;
993 
994 	if (hlen > sizeof(struct ip6_hdr)) {
995 		n = m_copym(m0, sizeof(struct ip6_hdr),
996 		    hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
997 		if (n == NULL)
998 			return (ENOBUFS);
999 		m->m_next = n;
1000 	} else
1001 		n = m;
1002 
1003 	/* Search for the last mbuf of unfragmentable part. */
1004 	for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1005 		;
1006 
1007 	if ((mlast->m_flags & M_EXT) == 0 &&
1008 	    m_trailingspace(mlast) >= sizeof(struct ip6_frag)) {
1009 		/* use the trailing space of the last mbuf for fragment hdr */
1010 		*frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1011 		    mlast->m_len);
1012 		mlast->m_len += sizeof(struct ip6_frag);
1013 		m->m_pkthdr.len += sizeof(struct ip6_frag);
1014 	} else {
1015 		/* allocate a new mbuf for the fragment header */
1016 		struct mbuf *mfrg;
1017 
1018 		MGET(mfrg, M_DONTWAIT, MT_DATA);
1019 		if (mfrg == NULL)
1020 			return (ENOBUFS);
1021 		mfrg->m_len = sizeof(struct ip6_frag);
1022 		*frghdrp = mtod(mfrg, struct ip6_frag *);
1023 		mlast->m_next = mfrg;
1024 	}
1025 
1026 	return (0);
1027 }
1028 
1029 int
1030 ip6_getpmtu(struct rtentry *rt, struct ifnet *ifp, u_long *mtup)
1031 {
1032 	u_int32_t mtu = 0;
1033 	int error = 0;
1034 
1035 	if (rt != NULL) {
1036 		mtu = rt->rt_mtu;
1037 		if (mtu == 0)
1038 			mtu = ifp->if_mtu;
1039 		else if (mtu < IPV6_MMTU) {
1040 			/* RFC8021 IPv6 Atomic Fragments Considered Harmful */
1041 			mtu = IPV6_MMTU;
1042 		} else if (mtu > ifp->if_mtu) {
1043 			/*
1044 			 * The MTU on the route is larger than the MTU on
1045 			 * the interface!  This shouldn't happen, unless the
1046 			 * MTU of the interface has been changed after the
1047 			 * interface was brought up.  Change the MTU in the
1048 			 * route to match the interface MTU (as long as the
1049 			 * field isn't locked).
1050 			 */
1051 			mtu = ifp->if_mtu;
1052 			if (!(rt->rt_locks & RTV_MTU))
1053 				rt->rt_mtu = mtu;
1054 		}
1055 	} else {
1056 		mtu = ifp->if_mtu;
1057 	}
1058 
1059 	*mtup = mtu;
1060 	return (error);
1061 }
1062 
1063 /*
1064  * IP6 socket option processing.
1065  */
1066 int
1067 ip6_ctloutput(int op, struct socket *so, int level, int optname,
1068     struct mbuf *m)
1069 {
1070 	int privileged, optdatalen, uproto;
1071 	void *optdata;
1072 	struct inpcb *inp = sotoinpcb(so);
1073 	int error, optval;
1074 	struct proc *p = curproc; /* For IPsec and rdomain */
1075 	u_int rtableid, rtid = 0;
1076 
1077 	error = optval = 0;
1078 
1079 	privileged = (inp->inp_socket->so_state & SS_PRIV);
1080 	uproto = (int)so->so_proto->pr_protocol;
1081 
1082 	if (level != IPPROTO_IPV6)
1083 		return (EINVAL);
1084 
1085 	rtableid = p->p_p->ps_rtableid;
1086 
1087 	switch (op) {
1088 	case PRCO_SETOPT:
1089 		switch (optname) {
1090 		/*
1091 		 * Use of some Hop-by-Hop options or some
1092 		 * Destination options, might require special
1093 		 * privilege.  That is, normal applications
1094 		 * (without special privilege) might be forbidden
1095 		 * from setting certain options in outgoing packets,
1096 		 * and might never see certain options in received
1097 		 * packets. [RFC 2292 Section 6]
1098 		 * KAME specific note:
1099 		 *  KAME prevents non-privileged users from sending or
1100 		 *  receiving ANY hbh/dst options in order to avoid
1101 		 *  overhead of parsing options in the kernel.
1102 		 */
1103 		case IPV6_RECVHOPOPTS:
1104 		case IPV6_RECVDSTOPTS:
1105 			if (!privileged) {
1106 				error = EPERM;
1107 				break;
1108 			}
1109 			/* FALLTHROUGH */
1110 		case IPV6_UNICAST_HOPS:
1111 		case IPV6_MINHOPCOUNT:
1112 		case IPV6_HOPLIMIT:
1113 
1114 		case IPV6_RECVPKTINFO:
1115 		case IPV6_RECVHOPLIMIT:
1116 		case IPV6_RECVRTHDR:
1117 		case IPV6_RECVPATHMTU:
1118 		case IPV6_RECVTCLASS:
1119 		case IPV6_V6ONLY:
1120 		case IPV6_AUTOFLOWLABEL:
1121 		case IPV6_RECVDSTPORT:
1122 			if (m == NULL || m->m_len != sizeof(int)) {
1123 				error = EINVAL;
1124 				break;
1125 			}
1126 			optval = *mtod(m, int *);
1127 			switch (optname) {
1128 
1129 			case IPV6_UNICAST_HOPS:
1130 				if (optval < -1 || optval >= 256)
1131 					error = EINVAL;
1132 				else {
1133 					/* -1 = kernel default */
1134 					inp->inp_hops = optval;
1135 				}
1136 				break;
1137 
1138 			case IPV6_MINHOPCOUNT:
1139 				if (optval < 0 || optval > 255)
1140 					error = EINVAL;
1141 				else
1142 					inp->inp_ip6_minhlim = optval;
1143 				break;
1144 
1145 #define OPTSET(bit) \
1146 do { \
1147 	if (optval) \
1148 		inp->inp_flags |= (bit); \
1149 	else \
1150 		inp->inp_flags &= ~(bit); \
1151 } while (/*CONSTCOND*/ 0)
1152 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1153 
1154 			case IPV6_RECVPKTINFO:
1155 				OPTSET(IN6P_PKTINFO);
1156 				break;
1157 
1158 			case IPV6_HOPLIMIT:
1159 			{
1160 				struct ip6_pktopts **optp;
1161 
1162 				optp = &inp->inp_outputopts6;
1163 				error = ip6_pcbopt(IPV6_HOPLIMIT,
1164 				    (u_char *)&optval, sizeof(optval), optp,
1165 				    privileged, uproto);
1166 				break;
1167 			}
1168 
1169 			case IPV6_RECVHOPLIMIT:
1170 				OPTSET(IN6P_HOPLIMIT);
1171 				break;
1172 
1173 			case IPV6_RECVHOPOPTS:
1174 				OPTSET(IN6P_HOPOPTS);
1175 				break;
1176 
1177 			case IPV6_RECVDSTOPTS:
1178 				OPTSET(IN6P_DSTOPTS);
1179 				break;
1180 
1181 			case IPV6_RECVRTHDR:
1182 				OPTSET(IN6P_RTHDR);
1183 				break;
1184 
1185 			case IPV6_RECVPATHMTU:
1186 				/*
1187 				 * We ignore this option for TCP
1188 				 * sockets.
1189 				 * (RFC3542 leaves this case
1190 				 * unspecified.)
1191 				 */
1192 				if (uproto != IPPROTO_TCP)
1193 					OPTSET(IN6P_MTU);
1194 				break;
1195 
1196 			case IPV6_V6ONLY:
1197 				/*
1198 				 * make setsockopt(IPV6_V6ONLY)
1199 				 * available only prior to bind(2).
1200 				 * see ipng mailing list, Jun 22 2001.
1201 				 */
1202 				if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(
1203 				    &inp->inp_laddr6)) {
1204 					error = EINVAL;
1205 					break;
1206 				}
1207 				/* No support for IPv4-mapped addresses. */
1208 				if (!optval)
1209 					error = EINVAL;
1210 				else
1211 					error = 0;
1212 				break;
1213 			case IPV6_RECVTCLASS:
1214 				OPTSET(IN6P_TCLASS);
1215 				break;
1216 			case IPV6_AUTOFLOWLABEL:
1217 				OPTSET(IN6P_AUTOFLOWLABEL);
1218 				break;
1219 
1220 			case IPV6_RECVDSTPORT:
1221 				OPTSET(IN6P_RECVDSTPORT);
1222 				break;
1223 			}
1224 			break;
1225 
1226 		case IPV6_TCLASS:
1227 		case IPV6_DONTFRAG:
1228 		case IPV6_USE_MIN_MTU:
1229 			if (m == NULL || m->m_len != sizeof(optval)) {
1230 				error = EINVAL;
1231 				break;
1232 			}
1233 			optval = *mtod(m, int *);
1234 			{
1235 				struct ip6_pktopts **optp;
1236 				optp = &inp->inp_outputopts6;
1237 				error = ip6_pcbopt(optname, (u_char *)&optval,
1238 				    sizeof(optval), optp, privileged, uproto);
1239 				break;
1240 			}
1241 
1242 		case IPV6_PKTINFO:
1243 		case IPV6_HOPOPTS:
1244 		case IPV6_RTHDR:
1245 		case IPV6_DSTOPTS:
1246 		case IPV6_RTHDRDSTOPTS:
1247 		{
1248 			/* new advanced API (RFC3542) */
1249 			u_char *optbuf;
1250 			int optbuflen;
1251 			struct ip6_pktopts **optp;
1252 
1253 			if (m && m->m_next) {
1254 				error = EINVAL;	/* XXX */
1255 				break;
1256 			}
1257 			if (m) {
1258 				optbuf = mtod(m, u_char *);
1259 				optbuflen = m->m_len;
1260 			} else {
1261 				optbuf = NULL;
1262 				optbuflen = 0;
1263 			}
1264 			optp = &inp->inp_outputopts6;
1265 			error = ip6_pcbopt(optname, optbuf, optbuflen, optp,
1266 			    privileged, uproto);
1267 			break;
1268 		}
1269 #undef OPTSET
1270 
1271 		case IPV6_MULTICAST_IF:
1272 		case IPV6_MULTICAST_HOPS:
1273 		case IPV6_MULTICAST_LOOP:
1274 		case IPV6_JOIN_GROUP:
1275 		case IPV6_LEAVE_GROUP:
1276 			error =	ip6_setmoptions(optname,
1277 						&inp->inp_moptions6,
1278 						m, inp->inp_rtableid);
1279 			break;
1280 
1281 		case IPV6_PORTRANGE:
1282 			if (m == NULL || m->m_len != sizeof(int)) {
1283 				error = EINVAL;
1284 				break;
1285 			}
1286 			optval = *mtod(m, int *);
1287 
1288 			switch (optval) {
1289 			case IPV6_PORTRANGE_DEFAULT:
1290 				inp->inp_flags &= ~(IN6P_LOWPORT);
1291 				inp->inp_flags &= ~(IN6P_HIGHPORT);
1292 				break;
1293 
1294 			case IPV6_PORTRANGE_HIGH:
1295 				inp->inp_flags &= ~(IN6P_LOWPORT);
1296 				inp->inp_flags |= IN6P_HIGHPORT;
1297 				break;
1298 
1299 			case IPV6_PORTRANGE_LOW:
1300 				inp->inp_flags &= ~(IN6P_HIGHPORT);
1301 				inp->inp_flags |= IN6P_LOWPORT;
1302 				break;
1303 
1304 			default:
1305 				error = EINVAL;
1306 				break;
1307 			}
1308 			break;
1309 
1310 		case IPSEC6_OUTSA:
1311 			error = EINVAL;
1312 			break;
1313 
1314 		case IPV6_AUTH_LEVEL:
1315 		case IPV6_ESP_TRANS_LEVEL:
1316 		case IPV6_ESP_NETWORK_LEVEL:
1317 		case IPV6_IPCOMP_LEVEL:
1318 #ifndef IPSEC
1319 			error = EINVAL;
1320 #else
1321 			if (m == NULL || m->m_len != sizeof(int)) {
1322 				error = EINVAL;
1323 				break;
1324 			}
1325 			optval = *mtod(m, int *);
1326 
1327 			if (optval < IPSEC_LEVEL_BYPASS ||
1328 			    optval > IPSEC_LEVEL_UNIQUE) {
1329 				error = EINVAL;
1330 				break;
1331 			}
1332 
1333 			switch (optname) {
1334 			case IPV6_AUTH_LEVEL:
1335 				if (optval < IPSEC_AUTH_LEVEL_DEFAULT &&
1336 				    suser(p)) {
1337 					error = EACCES;
1338 					break;
1339 				}
1340 				inp->inp_seclevel[SL_AUTH] = optval;
1341 				break;
1342 
1343 			case IPV6_ESP_TRANS_LEVEL:
1344 				if (optval < IPSEC_ESP_TRANS_LEVEL_DEFAULT &&
1345 				    suser(p)) {
1346 					error = EACCES;
1347 					break;
1348 				}
1349 				inp->inp_seclevel[SL_ESP_TRANS] = optval;
1350 				break;
1351 
1352 			case IPV6_ESP_NETWORK_LEVEL:
1353 				if (optval < IPSEC_ESP_NETWORK_LEVEL_DEFAULT &&
1354 				    suser(p)) {
1355 					error = EACCES;
1356 					break;
1357 				}
1358 				inp->inp_seclevel[SL_ESP_NETWORK] = optval;
1359 				break;
1360 
1361 			case IPV6_IPCOMP_LEVEL:
1362 				if (optval < IPSEC_IPCOMP_LEVEL_DEFAULT &&
1363 				    suser(p)) {
1364 					error = EACCES;
1365 					break;
1366 				}
1367 				inp->inp_seclevel[SL_IPCOMP] = optval;
1368 				break;
1369 			}
1370 #endif
1371 			break;
1372 		case SO_RTABLE:
1373 			if (m == NULL || m->m_len < sizeof(u_int)) {
1374 				error = EINVAL;
1375 				break;
1376 			}
1377 			rtid = *mtod(m, u_int *);
1378 			if (inp->inp_rtableid == rtid)
1379 				break;
1380 			/* needs privileges to switch when already set */
1381 			if (rtableid != rtid && rtableid != 0 &&
1382 			    (error = suser(p)) != 0)
1383 				break;
1384 			/* table must exist */
1385 			if (!rtable_exists(rtid)) {
1386 				error = EINVAL;
1387 				break;
1388 			}
1389 			if (inp->inp_lport) {
1390 				error = EBUSY;
1391 				break;
1392 			}
1393 			inp->inp_rtableid = rtid;
1394 			in_pcbrehash(inp);
1395 			break;
1396 		case IPV6_PIPEX:
1397 			if (m != NULL && m->m_len == sizeof(int))
1398 				inp->inp_pipex = *mtod(m, int *);
1399 			else
1400 				error = EINVAL;
1401 			break;
1402 
1403 		default:
1404 			error = ENOPROTOOPT;
1405 			break;
1406 		}
1407 		break;
1408 
1409 	case PRCO_GETOPT:
1410 		switch (optname) {
1411 
1412 		case IPV6_RECVHOPOPTS:
1413 		case IPV6_RECVDSTOPTS:
1414 		case IPV6_UNICAST_HOPS:
1415 		case IPV6_MINHOPCOUNT:
1416 		case IPV6_RECVPKTINFO:
1417 		case IPV6_RECVHOPLIMIT:
1418 		case IPV6_RECVRTHDR:
1419 		case IPV6_RECVPATHMTU:
1420 
1421 		case IPV6_V6ONLY:
1422 		case IPV6_PORTRANGE:
1423 		case IPV6_RECVTCLASS:
1424 		case IPV6_AUTOFLOWLABEL:
1425 		case IPV6_RECVDSTPORT:
1426 			switch (optname) {
1427 
1428 			case IPV6_RECVHOPOPTS:
1429 				optval = OPTBIT(IN6P_HOPOPTS);
1430 				break;
1431 
1432 			case IPV6_RECVDSTOPTS:
1433 				optval = OPTBIT(IN6P_DSTOPTS);
1434 				break;
1435 
1436 			case IPV6_UNICAST_HOPS:
1437 				optval = inp->inp_hops;
1438 				break;
1439 
1440 			case IPV6_MINHOPCOUNT:
1441 				optval = inp->inp_ip6_minhlim;
1442 				break;
1443 
1444 			case IPV6_RECVPKTINFO:
1445 				optval = OPTBIT(IN6P_PKTINFO);
1446 				break;
1447 
1448 			case IPV6_RECVHOPLIMIT:
1449 				optval = OPTBIT(IN6P_HOPLIMIT);
1450 				break;
1451 
1452 			case IPV6_RECVRTHDR:
1453 				optval = OPTBIT(IN6P_RTHDR);
1454 				break;
1455 
1456 			case IPV6_RECVPATHMTU:
1457 				optval = OPTBIT(IN6P_MTU);
1458 				break;
1459 
1460 			case IPV6_V6ONLY:
1461 				optval = 1;
1462 				break;
1463 
1464 			case IPV6_PORTRANGE:
1465 			    {
1466 				int flags;
1467 				flags = inp->inp_flags;
1468 				if (flags & IN6P_HIGHPORT)
1469 					optval = IPV6_PORTRANGE_HIGH;
1470 				else if (flags & IN6P_LOWPORT)
1471 					optval = IPV6_PORTRANGE_LOW;
1472 				else
1473 					optval = 0;
1474 				break;
1475 			    }
1476 			case IPV6_RECVTCLASS:
1477 				optval = OPTBIT(IN6P_TCLASS);
1478 				break;
1479 
1480 			case IPV6_AUTOFLOWLABEL:
1481 				optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1482 				break;
1483 
1484 			case IPV6_RECVDSTPORT:
1485 				optval = OPTBIT(IN6P_RECVDSTPORT);
1486 				break;
1487 			}
1488 			if (error)
1489 				break;
1490 			m->m_len = sizeof(int);
1491 			*mtod(m, int *) = optval;
1492 			break;
1493 
1494 		case IPV6_PATHMTU:
1495 		{
1496 			u_long pmtu = 0;
1497 			struct ip6_mtuinfo mtuinfo;
1498 			struct ifnet *ifp;
1499 			struct rtentry *rt;
1500 
1501 			if (!(so->so_state & SS_ISCONNECTED))
1502 				return (ENOTCONN);
1503 
1504 			rt = in_pcbrtentry(inp);
1505 			if (!rtisvalid(rt))
1506 				return (EHOSTUNREACH);
1507 
1508 			ifp = if_get(rt->rt_ifidx);
1509 			if (ifp == NULL)
1510 				return (EHOSTUNREACH);
1511 			/*
1512 			 * XXX: we dot not consider the case of source
1513 			 * routing, or optional information to specify
1514 			 * the outgoing interface.
1515 			 */
1516 			error = ip6_getpmtu(rt, ifp, &pmtu);
1517 			if_put(ifp);
1518 			if (error)
1519 				break;
1520 			if (pmtu > IPV6_MAXPACKET)
1521 				pmtu = IPV6_MAXPACKET;
1522 
1523 			bzero(&mtuinfo, sizeof(mtuinfo));
1524 			mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1525 			optdata = (void *)&mtuinfo;
1526 			optdatalen = sizeof(mtuinfo);
1527 			if (optdatalen > MCLBYTES)
1528 				return (EMSGSIZE); /* XXX */
1529 			if (optdatalen > MLEN)
1530 				MCLGET(m, M_WAIT);
1531 			m->m_len = optdatalen;
1532 			bcopy(optdata, mtod(m, void *), optdatalen);
1533 			break;
1534 		}
1535 
1536 		case IPV6_PKTINFO:
1537 		case IPV6_HOPOPTS:
1538 		case IPV6_RTHDR:
1539 		case IPV6_DSTOPTS:
1540 		case IPV6_RTHDRDSTOPTS:
1541 		case IPV6_TCLASS:
1542 		case IPV6_DONTFRAG:
1543 		case IPV6_USE_MIN_MTU:
1544 			error = ip6_getpcbopt(inp->inp_outputopts6,
1545 			    optname, m);
1546 			break;
1547 
1548 		case IPV6_MULTICAST_IF:
1549 		case IPV6_MULTICAST_HOPS:
1550 		case IPV6_MULTICAST_LOOP:
1551 		case IPV6_JOIN_GROUP:
1552 		case IPV6_LEAVE_GROUP:
1553 			error = ip6_getmoptions(optname,
1554 			    inp->inp_moptions6, m);
1555 			break;
1556 
1557 		case IPSEC6_OUTSA:
1558 			error = EINVAL;
1559 			break;
1560 
1561 		case IPV6_AUTH_LEVEL:
1562 		case IPV6_ESP_TRANS_LEVEL:
1563 		case IPV6_ESP_NETWORK_LEVEL:
1564 		case IPV6_IPCOMP_LEVEL:
1565 #ifndef IPSEC
1566 			m->m_len = sizeof(int);
1567 			*mtod(m, int *) = IPSEC_LEVEL_NONE;
1568 #else
1569 			m->m_len = sizeof(int);
1570 			switch (optname) {
1571 			case IPV6_AUTH_LEVEL:
1572 				optval = inp->inp_seclevel[SL_AUTH];
1573 				break;
1574 
1575 			case IPV6_ESP_TRANS_LEVEL:
1576 				optval =
1577 				    inp->inp_seclevel[SL_ESP_TRANS];
1578 				break;
1579 
1580 			case IPV6_ESP_NETWORK_LEVEL:
1581 				optval =
1582 				    inp->inp_seclevel[SL_ESP_NETWORK];
1583 				break;
1584 
1585 			case IPV6_IPCOMP_LEVEL:
1586 				optval = inp->inp_seclevel[SL_IPCOMP];
1587 				break;
1588 			}
1589 			*mtod(m, int *) = optval;
1590 #endif
1591 			break;
1592 		case SO_RTABLE:
1593 			m->m_len = sizeof(u_int);
1594 			*mtod(m, u_int *) = inp->inp_rtableid;
1595 			break;
1596 		case IPV6_PIPEX:
1597 			m->m_len = sizeof(int);
1598 			*mtod(m, int *) = inp->inp_pipex;
1599 			break;
1600 
1601 		default:
1602 			error = ENOPROTOOPT;
1603 			break;
1604 		}
1605 		break;
1606 	}
1607 	return (error);
1608 }
1609 
1610 int
1611 ip6_raw_ctloutput(int op, struct socket *so, int level, int optname,
1612     struct mbuf *m)
1613 {
1614 	int error = 0, optval;
1615 	const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1616 	struct inpcb *inp = sotoinpcb(so);
1617 
1618 	if (level != IPPROTO_IPV6)
1619 		return (EINVAL);
1620 
1621 	switch (optname) {
1622 	case IPV6_CHECKSUM:
1623 		/*
1624 		 * For ICMPv6 sockets, no modification allowed for checksum
1625 		 * offset, permit "no change" values to help existing apps.
1626 		 *
1627 		 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
1628 		 * for an ICMPv6 socket will fail."
1629 		 * The current behavior does not meet RFC3542.
1630 		 */
1631 		switch (op) {
1632 		case PRCO_SETOPT:
1633 			if (m == NULL || m->m_len != sizeof(int)) {
1634 				error = EINVAL;
1635 				break;
1636 			}
1637 			optval = *mtod(m, int *);
1638 			if (optval < -1 ||
1639 			    (optval > 0 && (optval % 2) != 0)) {
1640 				/*
1641 				 * The API assumes non-negative even offset
1642 				 * values or -1 as a special value.
1643 				 */
1644 				error = EINVAL;
1645 			} else if (so->so_proto->pr_protocol ==
1646 			    IPPROTO_ICMPV6) {
1647 				if (optval != icmp6off)
1648 					error = EINVAL;
1649 			} else
1650 				inp->inp_cksum6 = optval;
1651 			break;
1652 
1653 		case PRCO_GETOPT:
1654 			if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
1655 				optval = icmp6off;
1656 			else
1657 				optval = inp->inp_cksum6;
1658 
1659 			m->m_len = sizeof(int);
1660 			*mtod(m, int *) = optval;
1661 			break;
1662 
1663 		default:
1664 			error = EINVAL;
1665 			break;
1666 		}
1667 		break;
1668 
1669 	default:
1670 		error = ENOPROTOOPT;
1671 		break;
1672 	}
1673 
1674 	return (error);
1675 }
1676 
1677 /*
1678  * initialize ip6_pktopts.  beware that there are non-zero default values in
1679  * the struct.
1680  */
1681 void
1682 ip6_initpktopts(struct ip6_pktopts *opt)
1683 {
1684 	bzero(opt, sizeof(*opt));
1685 	opt->ip6po_hlim = -1;	/* -1 means default hop limit */
1686 	opt->ip6po_tclass = -1;	/* -1 means default traffic class */
1687 	opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
1688 }
1689 
1690 int
1691 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
1692     int priv, int uproto)
1693 {
1694 	struct ip6_pktopts *opt;
1695 
1696 	if (*pktopt == NULL) {
1697 		*pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
1698 		    M_WAITOK);
1699 		ip6_initpktopts(*pktopt);
1700 	}
1701 	opt = *pktopt;
1702 
1703 	return (ip6_setpktopt(optname, buf, len, opt, priv, 1, uproto));
1704 }
1705 
1706 int
1707 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct mbuf *m)
1708 {
1709 	void *optdata = NULL;
1710 	int optdatalen = 0;
1711 	struct ip6_ext *ip6e;
1712 	int error = 0;
1713 	struct in6_pktinfo null_pktinfo;
1714 	int deftclass = 0, on;
1715 	int defminmtu = IP6PO_MINMTU_MCASTONLY;
1716 
1717 	switch (optname) {
1718 	case IPV6_PKTINFO:
1719 		if (pktopt && pktopt->ip6po_pktinfo)
1720 			optdata = (void *)pktopt->ip6po_pktinfo;
1721 		else {
1722 			/* XXX: we don't have to do this every time... */
1723 			bzero(&null_pktinfo, sizeof(null_pktinfo));
1724 			optdata = (void *)&null_pktinfo;
1725 		}
1726 		optdatalen = sizeof(struct in6_pktinfo);
1727 		break;
1728 	case IPV6_TCLASS:
1729 		if (pktopt && pktopt->ip6po_tclass >= 0)
1730 			optdata = (void *)&pktopt->ip6po_tclass;
1731 		else
1732 			optdata = (void *)&deftclass;
1733 		optdatalen = sizeof(int);
1734 		break;
1735 	case IPV6_HOPOPTS:
1736 		if (pktopt && pktopt->ip6po_hbh) {
1737 			optdata = (void *)pktopt->ip6po_hbh;
1738 			ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
1739 			optdatalen = (ip6e->ip6e_len + 1) << 3;
1740 		}
1741 		break;
1742 	case IPV6_RTHDR:
1743 		if (pktopt && pktopt->ip6po_rthdr) {
1744 			optdata = (void *)pktopt->ip6po_rthdr;
1745 			ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
1746 			optdatalen = (ip6e->ip6e_len + 1) << 3;
1747 		}
1748 		break;
1749 	case IPV6_RTHDRDSTOPTS:
1750 		if (pktopt && pktopt->ip6po_dest1) {
1751 			optdata = (void *)pktopt->ip6po_dest1;
1752 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
1753 			optdatalen = (ip6e->ip6e_len + 1) << 3;
1754 		}
1755 		break;
1756 	case IPV6_DSTOPTS:
1757 		if (pktopt && pktopt->ip6po_dest2) {
1758 			optdata = (void *)pktopt->ip6po_dest2;
1759 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
1760 			optdatalen = (ip6e->ip6e_len + 1) << 3;
1761 		}
1762 		break;
1763 	case IPV6_USE_MIN_MTU:
1764 		if (pktopt)
1765 			optdata = (void *)&pktopt->ip6po_minmtu;
1766 		else
1767 			optdata = (void *)&defminmtu;
1768 		optdatalen = sizeof(int);
1769 		break;
1770 	case IPV6_DONTFRAG:
1771 		if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
1772 			on = 1;
1773 		else
1774 			on = 0;
1775 		optdata = (void *)&on;
1776 		optdatalen = sizeof(on);
1777 		break;
1778 	default:		/* should not happen */
1779 #ifdef DIAGNOSTIC
1780 		panic("%s: unexpected option", __func__);
1781 #endif
1782 		return (ENOPROTOOPT);
1783 	}
1784 
1785 	if (optdatalen > MCLBYTES)
1786 		return (EMSGSIZE); /* XXX */
1787 	if (optdatalen > MLEN)
1788 		MCLGET(m, M_WAIT);
1789 	m->m_len = optdatalen;
1790 	if (optdatalen)
1791 		bcopy(optdata, mtod(m, void *), optdatalen);
1792 
1793 	return (error);
1794 }
1795 
1796 void
1797 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
1798 {
1799 	if (optname == -1 || optname == IPV6_PKTINFO) {
1800 		if (pktopt->ip6po_pktinfo)
1801 			free(pktopt->ip6po_pktinfo, M_IP6OPT, 0);
1802 		pktopt->ip6po_pktinfo = NULL;
1803 	}
1804 	if (optname == -1 || optname == IPV6_HOPLIMIT)
1805 		pktopt->ip6po_hlim = -1;
1806 	if (optname == -1 || optname == IPV6_TCLASS)
1807 		pktopt->ip6po_tclass = -1;
1808 	if (optname == -1 || optname == IPV6_HOPOPTS) {
1809 		if (pktopt->ip6po_hbh)
1810 			free(pktopt->ip6po_hbh, M_IP6OPT, 0);
1811 		pktopt->ip6po_hbh = NULL;
1812 	}
1813 	if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
1814 		if (pktopt->ip6po_dest1)
1815 			free(pktopt->ip6po_dest1, M_IP6OPT, 0);
1816 		pktopt->ip6po_dest1 = NULL;
1817 	}
1818 	if (optname == -1 || optname == IPV6_RTHDR) {
1819 		if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
1820 			free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT, 0);
1821 		pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
1822 		if (pktopt->ip6po_route.ro_rt) {
1823 			rtfree(pktopt->ip6po_route.ro_rt);
1824 			pktopt->ip6po_route.ro_rt = NULL;
1825 		}
1826 	}
1827 	if (optname == -1 || optname == IPV6_DSTOPTS) {
1828 		if (pktopt->ip6po_dest2)
1829 			free(pktopt->ip6po_dest2, M_IP6OPT, 0);
1830 		pktopt->ip6po_dest2 = NULL;
1831 	}
1832 }
1833 
1834 #define PKTOPT_EXTHDRCPY(type) \
1835 do {\
1836 	if (src->type) {\
1837 		size_t hlen;\
1838 		hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1839 		dst->type = malloc(hlen, M_IP6OPT, M_NOWAIT);\
1840 		if (dst->type == NULL)\
1841 			goto bad;\
1842 		memcpy(dst->type, src->type, hlen);\
1843 	}\
1844 } while (/*CONSTCOND*/ 0)
1845 
1846 int
1847 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src)
1848 {
1849 	dst->ip6po_hlim = src->ip6po_hlim;
1850 	dst->ip6po_tclass = src->ip6po_tclass;
1851 	dst->ip6po_flags = src->ip6po_flags;
1852 	if (src->ip6po_pktinfo) {
1853 		dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
1854 		    M_IP6OPT, M_NOWAIT);
1855 		if (dst->ip6po_pktinfo == NULL)
1856 			goto bad;
1857 		*dst->ip6po_pktinfo = *src->ip6po_pktinfo;
1858 	}
1859 	PKTOPT_EXTHDRCPY(ip6po_hbh);
1860 	PKTOPT_EXTHDRCPY(ip6po_dest1);
1861 	PKTOPT_EXTHDRCPY(ip6po_dest2);
1862 	PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
1863 	return (0);
1864 
1865   bad:
1866 	ip6_clearpktopts(dst, -1);
1867 	return (ENOBUFS);
1868 }
1869 #undef PKTOPT_EXTHDRCPY
1870 
1871 void
1872 ip6_freepcbopts(struct ip6_pktopts *pktopt)
1873 {
1874 	if (pktopt == NULL)
1875 		return;
1876 
1877 	ip6_clearpktopts(pktopt, -1);
1878 
1879 	free(pktopt, M_IP6OPT, 0);
1880 }
1881 
1882 /*
1883  * Set the IP6 multicast options in response to user setsockopt().
1884  */
1885 int
1886 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m,
1887     unsigned int rtableid)
1888 {
1889 	int error = 0;
1890 	u_int loop, ifindex;
1891 	struct ipv6_mreq *mreq;
1892 	struct ifnet *ifp;
1893 	struct ip6_moptions *im6o = *im6op;
1894 	struct in6_multi_mship *imm;
1895 	struct proc *p = curproc;	/* XXX */
1896 
1897 	if (im6o == NULL) {
1898 		/*
1899 		 * No multicast option buffer attached to the pcb;
1900 		 * allocate one and initialize to default values.
1901 		 */
1902 		im6o = malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1903 		if (im6o == NULL)
1904 			return (ENOBUFS);
1905 		*im6op = im6o;
1906 		im6o->im6o_ifidx = 0;
1907 		im6o->im6o_hlim = ip6_defmcasthlim;
1908 		im6o->im6o_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1909 		LIST_INIT(&im6o->im6o_memberships);
1910 	}
1911 
1912 	switch (optname) {
1913 
1914 	case IPV6_MULTICAST_IF:
1915 		/*
1916 		 * Select the interface for outgoing multicast packets.
1917 		 */
1918 		if (m == NULL || m->m_len != sizeof(u_int)) {
1919 			error = EINVAL;
1920 			break;
1921 		}
1922 		memcpy(&ifindex, mtod(m, u_int *), sizeof(ifindex));
1923 		if (ifindex != 0) {
1924 			ifp = if_get(ifindex);
1925 			if (ifp == NULL) {
1926 				error = ENXIO;	/* XXX EINVAL? */
1927 				break;
1928 			}
1929 			if (ifp->if_rdomain != rtable_l2(rtableid) ||
1930 			    (ifp->if_flags & IFF_MULTICAST) == 0) {
1931 				error = EADDRNOTAVAIL;
1932 				if_put(ifp);
1933 				break;
1934 			}
1935 			if_put(ifp);
1936 		}
1937 		im6o->im6o_ifidx = ifindex;
1938 		break;
1939 
1940 	case IPV6_MULTICAST_HOPS:
1941 	    {
1942 		/*
1943 		 * Set the IP6 hoplimit for outgoing multicast packets.
1944 		 */
1945 		int optval;
1946 		if (m == NULL || m->m_len != sizeof(int)) {
1947 			error = EINVAL;
1948 			break;
1949 		}
1950 		memcpy(&optval, mtod(m, u_int *), sizeof(optval));
1951 		if (optval < -1 || optval >= 256)
1952 			error = EINVAL;
1953 		else if (optval == -1)
1954 			im6o->im6o_hlim = ip6_defmcasthlim;
1955 		else
1956 			im6o->im6o_hlim = optval;
1957 		break;
1958 	    }
1959 
1960 	case IPV6_MULTICAST_LOOP:
1961 		/*
1962 		 * Set the loopback flag for outgoing multicast packets.
1963 		 * Must be zero or one.
1964 		 */
1965 		if (m == NULL || m->m_len != sizeof(u_int)) {
1966 			error = EINVAL;
1967 			break;
1968 		}
1969 		memcpy(&loop, mtod(m, u_int *), sizeof(loop));
1970 		if (loop > 1) {
1971 			error = EINVAL;
1972 			break;
1973 		}
1974 		im6o->im6o_loop = loop;
1975 		break;
1976 
1977 	case IPV6_JOIN_GROUP:
1978 		/*
1979 		 * Add a multicast group membership.
1980 		 * Group must be a valid IP6 multicast address.
1981 		 */
1982 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
1983 			error = EINVAL;
1984 			break;
1985 		}
1986 		mreq = mtod(m, struct ipv6_mreq *);
1987 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
1988 			/*
1989 			 * We use the unspecified address to specify to accept
1990 			 * all multicast addresses. Only super user is allowed
1991 			 * to do this.
1992 			 */
1993 			if (suser(p))
1994 			{
1995 				error = EACCES;
1996 				break;
1997 			}
1998 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
1999 			error = EINVAL;
2000 			break;
2001 		}
2002 
2003 		/*
2004 		 * If no interface was explicitly specified, choose an
2005 		 * appropriate one according to the given multicast address.
2006 		 */
2007 		if (mreq->ipv6mr_interface == 0) {
2008 			struct rtentry *rt;
2009 			struct sockaddr_in6 dst;
2010 
2011 			memset(&dst, 0, sizeof(dst));
2012 			dst.sin6_len = sizeof(dst);
2013 			dst.sin6_family = AF_INET6;
2014 			dst.sin6_addr = mreq->ipv6mr_multiaddr;
2015 			rt = rtalloc(sin6tosa(&dst), RT_RESOLVE, rtableid);
2016 			if (rt == NULL) {
2017 				error = EADDRNOTAVAIL;
2018 				break;
2019 			}
2020 			ifp = if_get(rt->rt_ifidx);
2021 			rtfree(rt);
2022 		} else {
2023 			/*
2024 			 * If the interface is specified, validate it.
2025 			 */
2026 			ifp = if_get(mreq->ipv6mr_interface);
2027 			if (ifp == NULL) {
2028 				error = ENXIO;	/* XXX EINVAL? */
2029 				break;
2030 			}
2031 		}
2032 
2033 		/*
2034 		 * See if we found an interface, and confirm that it
2035 		 * supports multicast
2036 		 */
2037 		if (ifp == NULL || ifp->if_rdomain != rtable_l2(rtableid) ||
2038 		    (ifp->if_flags & IFF_MULTICAST) == 0) {
2039 			if_put(ifp);
2040 			error = EADDRNOTAVAIL;
2041 			break;
2042 		}
2043 		/*
2044 		 * Put interface index into the multicast address,
2045 		 * if the address has link/interface-local scope.
2046 		 */
2047 		if (IN6_IS_SCOPE_EMBED(&mreq->ipv6mr_multiaddr)) {
2048 			mreq->ipv6mr_multiaddr.s6_addr16[1] =
2049 			    htons(ifp->if_index);
2050 		}
2051 		/*
2052 		 * See if the membership already exists.
2053 		 */
2054 		LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain)
2055 			if (imm->i6mm_maddr->in6m_ifidx == ifp->if_index &&
2056 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2057 			    &mreq->ipv6mr_multiaddr))
2058 				break;
2059 		if (imm != NULL) {
2060 			if_put(ifp);
2061 			error = EADDRINUSE;
2062 			break;
2063 		}
2064 		/*
2065 		 * Everything looks good; add a new record to the multicast
2066 		 * address list for the given interface.
2067 		 */
2068 		imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error);
2069 		if_put(ifp);
2070 		if (!imm)
2071 			break;
2072 		LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2073 		break;
2074 
2075 	case IPV6_LEAVE_GROUP:
2076 		/*
2077 		 * Drop a multicast group membership.
2078 		 * Group must be a valid IP6 multicast address.
2079 		 */
2080 		if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2081 			error = EINVAL;
2082 			break;
2083 		}
2084 		mreq = mtod(m, struct ipv6_mreq *);
2085 		if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2086 			if (suser(p)) {
2087 				error = EACCES;
2088 				break;
2089 			}
2090 		} else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2091 			error = EINVAL;
2092 			break;
2093 		}
2094 
2095 		/*
2096 		 * Put interface index into the multicast address,
2097 		 * if the address has link-local scope.
2098 		 */
2099 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2100 			mreq->ipv6mr_multiaddr.s6_addr16[1] =
2101 			    htons(mreq->ipv6mr_interface);
2102 		}
2103 
2104 		/*
2105 		 * If an interface address was specified, get a pointer
2106 		 * to its ifnet structure.
2107 		 */
2108 		if (mreq->ipv6mr_interface == 0)
2109 			ifp = NULL;
2110 		else {
2111 			ifp = if_get(mreq->ipv6mr_interface);
2112 			if (ifp == NULL) {
2113 				error = ENXIO;	/* XXX EINVAL? */
2114 				break;
2115 			}
2116 		}
2117 
2118 		/*
2119 		 * Find the membership in the membership list.
2120 		 */
2121 		LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) {
2122 			if ((ifp == NULL ||
2123 			    imm->i6mm_maddr->in6m_ifidx == ifp->if_index) &&
2124 			    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2125 			    &mreq->ipv6mr_multiaddr))
2126 				break;
2127 		}
2128 
2129 		if_put(ifp);
2130 
2131 		if (imm == NULL) {
2132 			/* Unable to resolve interface */
2133 			error = EADDRNOTAVAIL;
2134 			break;
2135 		}
2136 		/*
2137 		 * Give up the multicast address record to which the
2138 		 * membership points.
2139 		 */
2140 		LIST_REMOVE(imm, i6mm_chain);
2141 		in6_leavegroup(imm);
2142 		break;
2143 
2144 	default:
2145 		error = EOPNOTSUPP;
2146 		break;
2147 	}
2148 
2149 	/*
2150 	 * If all options have default values, no need to keep the option
2151 	 * structure.
2152 	 */
2153 	if (im6o->im6o_ifidx == 0 &&
2154 	    im6o->im6o_hlim == ip6_defmcasthlim &&
2155 	    im6o->im6o_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2156 	    LIST_EMPTY(&im6o->im6o_memberships)) {
2157 		free(*im6op, M_IPMOPTS, sizeof(**im6op));
2158 		*im6op = NULL;
2159 	}
2160 
2161 	return (error);
2162 }
2163 
2164 /*
2165  * Return the IP6 multicast options in response to user getsockopt().
2166  */
2167 int
2168 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf *m)
2169 {
2170 	u_int *hlim, *loop, *ifindex;
2171 
2172 	switch (optname) {
2173 	case IPV6_MULTICAST_IF:
2174 		ifindex = mtod(m, u_int *);
2175 		m->m_len = sizeof(u_int);
2176 		if (im6o == NULL || im6o->im6o_ifidx == 0)
2177 			*ifindex = 0;
2178 		else
2179 			*ifindex = im6o->im6o_ifidx;
2180 		return (0);
2181 
2182 	case IPV6_MULTICAST_HOPS:
2183 		hlim = mtod(m, u_int *);
2184 		m->m_len = sizeof(u_int);
2185 		if (im6o == NULL)
2186 			*hlim = ip6_defmcasthlim;
2187 		else
2188 			*hlim = im6o->im6o_hlim;
2189 		return (0);
2190 
2191 	case IPV6_MULTICAST_LOOP:
2192 		loop = mtod(m, u_int *);
2193 		m->m_len = sizeof(u_int);
2194 		if (im6o == NULL)
2195 			*loop = ip6_defmcasthlim;
2196 		else
2197 			*loop = im6o->im6o_loop;
2198 		return (0);
2199 
2200 	default:
2201 		return (EOPNOTSUPP);
2202 	}
2203 }
2204 
2205 /*
2206  * Discard the IP6 multicast options.
2207  */
2208 void
2209 ip6_freemoptions(struct ip6_moptions *im6o)
2210 {
2211 	struct in6_multi_mship *imm;
2212 
2213 	if (im6o == NULL)
2214 		return;
2215 
2216 	while (!LIST_EMPTY(&im6o->im6o_memberships)) {
2217 		imm = LIST_FIRST(&im6o->im6o_memberships);
2218 		LIST_REMOVE(imm, i6mm_chain);
2219 		in6_leavegroup(imm);
2220 	}
2221 	free(im6o, M_IPMOPTS, sizeof(*im6o));
2222 }
2223 
2224 /*
2225  * Set IPv6 outgoing packet options based on advanced API.
2226  */
2227 int
2228 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2229     struct ip6_pktopts *stickyopt, int priv, int uproto)
2230 {
2231 	u_int clen;
2232 	struct cmsghdr *cm = 0;
2233 	caddr_t cmsgs;
2234 	int error;
2235 
2236 	if (control == NULL || opt == NULL)
2237 		return (EINVAL);
2238 
2239 	ip6_initpktopts(opt);
2240 	if (stickyopt) {
2241 		int error;
2242 
2243 		/*
2244 		 * If stickyopt is provided, make a local copy of the options
2245 		 * for this particular packet, then override them by ancillary
2246 		 * objects.
2247 		 * XXX: copypktopts() does not copy the cached route to a next
2248 		 * hop (if any).  This is not very good in terms of efficiency,
2249 		 * but we can allow this since this option should be rarely
2250 		 * used.
2251 		 */
2252 		if ((error = copypktopts(opt, stickyopt)) != 0)
2253 			return (error);
2254 	}
2255 
2256 	/*
2257 	 * XXX: Currently, we assume all the optional information is stored
2258 	 * in a single mbuf.
2259 	 */
2260 	if (control->m_next)
2261 		return (EINVAL);
2262 
2263 	clen = control->m_len;
2264 	cmsgs = mtod(control, caddr_t);
2265 	do {
2266 		if (clen < CMSG_LEN(0))
2267 			return (EINVAL);
2268 		cm = (struct cmsghdr *)cmsgs;
2269 		if (cm->cmsg_len < CMSG_LEN(0) || cm->cmsg_len > clen ||
2270 		    CMSG_ALIGN(cm->cmsg_len) > clen)
2271 			return (EINVAL);
2272 		if (cm->cmsg_level == IPPROTO_IPV6) {
2273 			error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2274 			    cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, uproto);
2275 			if (error)
2276 				return (error);
2277 		}
2278 
2279 		clen -= CMSG_ALIGN(cm->cmsg_len);
2280 		cmsgs += CMSG_ALIGN(cm->cmsg_len);
2281 	} while (clen);
2282 
2283 	return (0);
2284 }
2285 
2286 /*
2287  * Set a particular packet option, as a sticky option or an ancillary data
2288  * item.  "len" can be 0 only when it's a sticky option.
2289  */
2290 int
2291 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2292     int priv, int sticky, int uproto)
2293 {
2294 	int minmtupolicy;
2295 
2296 	switch (optname) {
2297 	case IPV6_PKTINFO:
2298 	{
2299 		struct ifnet *ifp = NULL;
2300 		struct in6_pktinfo *pktinfo;
2301 
2302 		if (len != sizeof(struct in6_pktinfo))
2303 			return (EINVAL);
2304 
2305 		pktinfo = (struct in6_pktinfo *)buf;
2306 
2307 		/*
2308 		 * An application can clear any sticky IPV6_PKTINFO option by
2309 		 * doing a "regular" setsockopt with ipi6_addr being
2310 		 * in6addr_any and ipi6_ifindex being zero.
2311 		 * [RFC 3542, Section 6]
2312 		 */
2313 		if (opt->ip6po_pktinfo &&
2314 		    pktinfo->ipi6_ifindex == 0 &&
2315 		    IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2316 			ip6_clearpktopts(opt, optname);
2317 			break;
2318 		}
2319 
2320 		if (uproto == IPPROTO_TCP &&
2321 		    sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2322 			return (EINVAL);
2323 		}
2324 
2325 		if (pktinfo->ipi6_ifindex) {
2326 			ifp = if_get(pktinfo->ipi6_ifindex);
2327 			if (ifp == NULL)
2328 				return (ENXIO);
2329 			if_put(ifp);
2330 		}
2331 
2332 		/*
2333 		 * We store the address anyway, and let in6_selectsrc()
2334 		 * validate the specified address.  This is because ipi6_addr
2335 		 * may not have enough information about its scope zone, and
2336 		 * we may need additional information (such as outgoing
2337 		 * interface or the scope zone of a destination address) to
2338 		 * disambiguate the scope.
2339 		 * XXX: the delay of the validation may confuse the
2340 		 * application when it is used as a sticky option.
2341 		 */
2342 		if (opt->ip6po_pktinfo == NULL) {
2343 			opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2344 			    M_IP6OPT, M_NOWAIT);
2345 			if (opt->ip6po_pktinfo == NULL)
2346 				return (ENOBUFS);
2347 		}
2348 		bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2349 		break;
2350 	}
2351 
2352 	case IPV6_HOPLIMIT:
2353 	{
2354 		int *hlimp;
2355 
2356 		/*
2357 		 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2358 		 * to simplify the ordering among hoplimit options.
2359 		 */
2360 		if (sticky)
2361 			return (ENOPROTOOPT);
2362 
2363 		if (len != sizeof(int))
2364 			return (EINVAL);
2365 		hlimp = (int *)buf;
2366 		if (*hlimp < -1 || *hlimp > 255)
2367 			return (EINVAL);
2368 
2369 		opt->ip6po_hlim = *hlimp;
2370 		break;
2371 	}
2372 
2373 	case IPV6_TCLASS:
2374 	{
2375 		int tclass;
2376 
2377 		if (len != sizeof(int))
2378 			return (EINVAL);
2379 		tclass = *(int *)buf;
2380 		if (tclass < -1 || tclass > 255)
2381 			return (EINVAL);
2382 
2383 		opt->ip6po_tclass = tclass;
2384 		break;
2385 	}
2386 	case IPV6_HOPOPTS:
2387 	{
2388 		struct ip6_hbh *hbh;
2389 		int hbhlen;
2390 
2391 		/*
2392 		 * XXX: We don't allow a non-privileged user to set ANY HbH
2393 		 * options, since per-option restriction has too much
2394 		 * overhead.
2395 		 */
2396 		if (!priv)
2397 			return (EPERM);
2398 
2399 		if (len == 0) {
2400 			ip6_clearpktopts(opt, IPV6_HOPOPTS);
2401 			break;	/* just remove the option */
2402 		}
2403 
2404 		/* message length validation */
2405 		if (len < sizeof(struct ip6_hbh))
2406 			return (EINVAL);
2407 		hbh = (struct ip6_hbh *)buf;
2408 		hbhlen = (hbh->ip6h_len + 1) << 3;
2409 		if (len != hbhlen)
2410 			return (EINVAL);
2411 
2412 		/* turn off the previous option, then set the new option. */
2413 		ip6_clearpktopts(opt, IPV6_HOPOPTS);
2414 		opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2415 		if (opt->ip6po_hbh == NULL)
2416 			return (ENOBUFS);
2417 		memcpy(opt->ip6po_hbh, hbh, hbhlen);
2418 
2419 		break;
2420 	}
2421 
2422 	case IPV6_DSTOPTS:
2423 	case IPV6_RTHDRDSTOPTS:
2424 	{
2425 		struct ip6_dest *dest, **newdest = NULL;
2426 		int destlen;
2427 
2428 		if (!priv)	/* XXX: see the comment for IPV6_HOPOPTS */
2429 			return (EPERM);
2430 
2431 		if (len == 0) {
2432 			ip6_clearpktopts(opt, optname);
2433 			break;	/* just remove the option */
2434 		}
2435 
2436 		/* message length validation */
2437 		if (len < sizeof(struct ip6_dest))
2438 			return (EINVAL);
2439 		dest = (struct ip6_dest *)buf;
2440 		destlen = (dest->ip6d_len + 1) << 3;
2441 		if (len != destlen)
2442 			return (EINVAL);
2443 		/*
2444 		 * Determine the position that the destination options header
2445 		 * should be inserted; before or after the routing header.
2446 		 */
2447 		switch (optname) {
2448 		case IPV6_RTHDRDSTOPTS:
2449 			newdest = &opt->ip6po_dest1;
2450 			break;
2451 		case IPV6_DSTOPTS:
2452 			newdest = &opt->ip6po_dest2;
2453 			break;
2454 		}
2455 
2456 		/* turn off the previous option, then set the new option. */
2457 		ip6_clearpktopts(opt, optname);
2458 		*newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2459 		if (*newdest == NULL)
2460 			return (ENOBUFS);
2461 		memcpy(*newdest, dest, destlen);
2462 
2463 		break;
2464 	}
2465 
2466 	case IPV6_RTHDR:
2467 	{
2468 		struct ip6_rthdr *rth;
2469 		int rthlen;
2470 
2471 		if (len == 0) {
2472 			ip6_clearpktopts(opt, IPV6_RTHDR);
2473 			break;	/* just remove the option */
2474 		}
2475 
2476 		/* message length validation */
2477 		if (len < sizeof(struct ip6_rthdr))
2478 			return (EINVAL);
2479 		rth = (struct ip6_rthdr *)buf;
2480 		rthlen = (rth->ip6r_len + 1) << 3;
2481 		if (len != rthlen)
2482 			return (EINVAL);
2483 
2484 		switch (rth->ip6r_type) {
2485 		case IPV6_RTHDR_TYPE_0:
2486 			if (rth->ip6r_len == 0)	/* must contain one addr */
2487 				return (EINVAL);
2488 			if (rth->ip6r_len % 2) /* length must be even */
2489 				return (EINVAL);
2490 			if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2491 				return (EINVAL);
2492 			break;
2493 		default:
2494 			return (EINVAL);	/* not supported */
2495 		}
2496 		/* turn off the previous option */
2497 		ip6_clearpktopts(opt, IPV6_RTHDR);
2498 		opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2499 		if (opt->ip6po_rthdr == NULL)
2500 			return (ENOBUFS);
2501 		memcpy(opt->ip6po_rthdr, rth, rthlen);
2502 		break;
2503 	}
2504 
2505 	case IPV6_USE_MIN_MTU:
2506 		if (len != sizeof(int))
2507 			return (EINVAL);
2508 		minmtupolicy = *(int *)buf;
2509 		if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2510 		    minmtupolicy != IP6PO_MINMTU_DISABLE &&
2511 		    minmtupolicy != IP6PO_MINMTU_ALL) {
2512 			return (EINVAL);
2513 		}
2514 		opt->ip6po_minmtu = minmtupolicy;
2515 		break;
2516 
2517 	case IPV6_DONTFRAG:
2518 		if (len != sizeof(int))
2519 			return (EINVAL);
2520 
2521 		if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2522 			/*
2523 			 * we ignore this option for TCP sockets.
2524 			 * (RFC3542 leaves this case unspecified.)
2525 			 */
2526 			opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2527 		} else
2528 			opt->ip6po_flags |= IP6PO_DONTFRAG;
2529 		break;
2530 
2531 	default:
2532 		return (ENOPROTOOPT);
2533 	} /* end of switch */
2534 
2535 	return (0);
2536 }
2537 
2538 /*
2539  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2540  * packet to the input queue of a specified interface.
2541  */
2542 void
2543 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2544 {
2545 	struct mbuf *copym;
2546 	struct ip6_hdr *ip6;
2547 
2548 	/*
2549 	 * Duplicate the packet.
2550 	 */
2551 	copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
2552 	if (copym == NULL)
2553 		return;
2554 
2555 	/*
2556 	 * Make sure to deep-copy IPv6 header portion in case the data
2557 	 * is in an mbuf cluster, so that we can safely override the IPv6
2558 	 * header portion later.
2559 	 */
2560 	if ((copym->m_flags & M_EXT) != 0 ||
2561 	    copym->m_len < sizeof(struct ip6_hdr)) {
2562 		copym = m_pullup(copym, sizeof(struct ip6_hdr));
2563 		if (copym == NULL)
2564 			return;
2565 	}
2566 
2567 #ifdef DIAGNOSTIC
2568 	if (copym->m_len < sizeof(*ip6)) {
2569 		m_freem(copym);
2570 		return;
2571 	}
2572 #endif
2573 
2574 	ip6 = mtod(copym, struct ip6_hdr *);
2575 	if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
2576 		ip6->ip6_src.s6_addr16[1] = 0;
2577 	if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
2578 		ip6->ip6_dst.s6_addr16[1] = 0;
2579 
2580 	if_input_local(ifp, copym, dst->sin6_family);
2581 }
2582 
2583 /*
2584  * Chop IPv6 header off from the payload.
2585  */
2586 int
2587 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2588 {
2589 	struct mbuf *mh;
2590 	struct ip6_hdr *ip6;
2591 
2592 	ip6 = mtod(m, struct ip6_hdr *);
2593 	if (m->m_len > sizeof(*ip6)) {
2594 		MGET(mh, M_DONTWAIT, MT_HEADER);
2595 		if (mh == NULL) {
2596 			m_freem(m);
2597 			return ENOBUFS;
2598 		}
2599 		M_MOVE_PKTHDR(mh, m);
2600 		m_align(mh, sizeof(*ip6));
2601 		m->m_len -= sizeof(*ip6);
2602 		m->m_data += sizeof(*ip6);
2603 		mh->m_next = m;
2604 		m = mh;
2605 		m->m_len = sizeof(*ip6);
2606 		bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2607 	}
2608 	exthdrs->ip6e_ip6 = m;
2609 	return 0;
2610 }
2611 
2612 u_int32_t
2613 ip6_randomid(void)
2614 {
2615 	return idgen32(&ip6_id_ctx);
2616 }
2617 
2618 void
2619 ip6_randomid_init(void)
2620 {
2621 	idgen32_init(&ip6_id_ctx);
2622 }
2623 
2624 /*
2625  *	Compute significant parts of the IPv6 checksum pseudo-header
2626  *	for use in a delayed TCP/UDP checksum calculation.
2627  */
2628 static __inline u_int16_t __attribute__((__unused__))
2629 in6_cksum_phdr(const struct in6_addr *src, const struct in6_addr *dst,
2630     u_int32_t len, u_int32_t nxt)
2631 {
2632 	u_int32_t sum = 0;
2633 	const u_int16_t *w;
2634 
2635 	w = (const u_int16_t *) src;
2636 	sum += w[0];
2637 	if (!IN6_IS_SCOPE_EMBED(src))
2638 		sum += w[1];
2639 	sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
2640 	sum += w[6]; sum += w[7];
2641 
2642 	w = (const u_int16_t *) dst;
2643 	sum += w[0];
2644 	if (!IN6_IS_SCOPE_EMBED(dst))
2645 		sum += w[1];
2646 	sum += w[2]; sum += w[3]; sum += w[4]; sum += w[5];
2647 	sum += w[6]; sum += w[7];
2648 
2649 	sum += (u_int16_t)(len >> 16) + (u_int16_t)(len /*& 0xffff*/);
2650 
2651 	sum += (u_int16_t)(nxt >> 16) + (u_int16_t)(nxt /*& 0xffff*/);
2652 
2653 	sum = (u_int16_t)(sum >> 16) + (u_int16_t)(sum /*& 0xffff*/);
2654 
2655 	if (sum > 0xffff)
2656 		sum -= 0xffff;
2657 
2658 	return (sum);
2659 }
2660 
2661 /*
2662  * Process a delayed payload checksum calculation.
2663  */
2664 void
2665 in6_delayed_cksum(struct mbuf *m, u_int8_t nxt)
2666 {
2667 	int nxtp, offset;
2668 	u_int16_t csum;
2669 
2670 	offset = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxtp);
2671 	if (offset <= 0 || nxtp != nxt)
2672 		/* If the desired next protocol isn't found, punt. */
2673 		return;
2674 	csum = (u_int16_t)(in6_cksum(m, 0, offset, m->m_pkthdr.len - offset));
2675 
2676 	switch (nxt) {
2677 	case IPPROTO_TCP:
2678 		offset += offsetof(struct tcphdr, th_sum);
2679 		break;
2680 
2681 	case IPPROTO_UDP:
2682 		offset += offsetof(struct udphdr, uh_sum);
2683 		if (csum == 0)
2684 			csum = 0xffff;
2685 		break;
2686 
2687 	case IPPROTO_ICMPV6:
2688 		offset += offsetof(struct icmp6_hdr, icmp6_cksum);
2689 		break;
2690 	}
2691 
2692 	if ((offset + sizeof(u_int16_t)) > m->m_len)
2693 		m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT);
2694 	else
2695 		*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
2696 }
2697 
2698 void
2699 in6_proto_cksum_out(struct mbuf *m, struct ifnet *ifp)
2700 {
2701 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2702 
2703 	/* some hw and in6_delayed_cksum need the pseudo header cksum */
2704 	if (m->m_pkthdr.csum_flags &
2705 	    (M_TCP_CSUM_OUT|M_UDP_CSUM_OUT|M_ICMP_CSUM_OUT)) {
2706 		int nxt, offset;
2707 		u_int16_t csum;
2708 
2709 		offset = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
2710 		if (ISSET(m->m_pkthdr.csum_flags, M_TCP_TSO) &&
2711 		    in_ifcap_cksum(m, ifp, IFCAP_TSOv6)) {
2712 			csum = in6_cksum_phdr(&ip6->ip6_src, &ip6->ip6_dst,
2713 			    htonl(0), htonl(nxt));
2714 		} else {
2715 			csum = in6_cksum_phdr(&ip6->ip6_src, &ip6->ip6_dst,
2716 			    htonl(m->m_pkthdr.len - offset), htonl(nxt));
2717 		}
2718 		if (nxt == IPPROTO_TCP)
2719 			offset += offsetof(struct tcphdr, th_sum);
2720 		else if (nxt == IPPROTO_UDP)
2721 			offset += offsetof(struct udphdr, uh_sum);
2722 		else if (nxt == IPPROTO_ICMPV6)
2723 			offset += offsetof(struct icmp6_hdr, icmp6_cksum);
2724 		if ((offset + sizeof(u_int16_t)) > m->m_len)
2725 			m_copyback(m, offset, sizeof(csum), &csum, M_NOWAIT);
2726 		else
2727 			*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
2728 	}
2729 
2730 	if (m->m_pkthdr.csum_flags & M_TCP_CSUM_OUT) {
2731 		if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_TCPv6) ||
2732 		    ip6->ip6_nxt != IPPROTO_TCP ||
2733 		    ifp->if_bridgeidx != 0) {
2734 			tcpstat_inc(tcps_outswcsum);
2735 			in6_delayed_cksum(m, IPPROTO_TCP);
2736 			m->m_pkthdr.csum_flags &= ~M_TCP_CSUM_OUT; /* Clear */
2737 		}
2738 	} else if (m->m_pkthdr.csum_flags & M_UDP_CSUM_OUT) {
2739 		if (!ifp || !(ifp->if_capabilities & IFCAP_CSUM_UDPv6) ||
2740 		    ip6->ip6_nxt != IPPROTO_UDP ||
2741 		    ifp->if_bridgeidx != 0) {
2742 			udpstat_inc(udps_outswcsum);
2743 			in6_delayed_cksum(m, IPPROTO_UDP);
2744 			m->m_pkthdr.csum_flags &= ~M_UDP_CSUM_OUT; /* Clear */
2745 		}
2746 	} else if (m->m_pkthdr.csum_flags & M_ICMP_CSUM_OUT) {
2747 		in6_delayed_cksum(m, IPPROTO_ICMPV6);
2748 		m->m_pkthdr.csum_flags &= ~M_ICMP_CSUM_OUT; /* Clear */
2749 	}
2750 }
2751 
2752 #ifdef IPSEC
2753 int
2754 ip6_output_ipsec_lookup(struct mbuf *m, struct inpcb *inp, struct tdb **tdbout)
2755 {
2756 	struct tdb *tdb;
2757 	struct m_tag *mtag;
2758 	struct tdb_ident *tdbi;
2759 	int error;
2760 
2761 	/*
2762 	 * Check if there was an outgoing SA bound to the flow
2763 	 * from a transport protocol.
2764 	 */
2765 
2766 	/* Do we have any pending SAs to apply ? */
2767 	error = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr),
2768 	    IPSP_DIRECTION_OUT, NULL, inp, &tdb, NULL);
2769 	if (error || tdb == NULL) {
2770 		*tdbout = NULL;
2771 		return error;
2772 	}
2773 	/* Loop detection */
2774 	for (mtag = m_tag_first(m); mtag != NULL; mtag = m_tag_next(m, mtag)) {
2775 		if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE)
2776 			continue;
2777 		tdbi = (struct tdb_ident *)(mtag + 1);
2778 		if (tdbi->spi == tdb->tdb_spi &&
2779 		    tdbi->proto == tdb->tdb_sproto &&
2780 		    tdbi->rdomain == tdb->tdb_rdomain &&
2781 		    !memcmp(&tdbi->dst, &tdb->tdb_dst,
2782 		    sizeof(union sockaddr_union))) {
2783 			/* no IPsec needed */
2784 			tdb_unref(tdb);
2785 			*tdbout = NULL;
2786 			return 0;
2787 		}
2788 	}
2789 	*tdbout = tdb;
2790 	return 0;
2791 }
2792 
2793 int
2794 ip6_output_ipsec_pmtu_update(struct tdb *tdb, struct route_in6 *ro,
2795     struct in6_addr *dst, int ifidx, int rtableid, int transportmode)
2796 {
2797 	struct rtentry *rt = NULL;
2798 	int rt_mtucloned = 0;
2799 
2800 	/* Find a host route to store the mtu in */
2801 	if (ro != NULL)
2802 		rt = ro->ro_rt;
2803 	/* but don't add a PMTU route for transport mode SAs */
2804 	if (transportmode)
2805 		rt = NULL;
2806 	else if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0) {
2807 		struct sockaddr_in6 sin6;
2808 		int error;
2809 
2810 		memset(&sin6, 0, sizeof(sin6));
2811 		sin6.sin6_family = AF_INET6;
2812 		sin6.sin6_len = sizeof(sin6);
2813 		sin6.sin6_addr = *dst;
2814 		sin6.sin6_scope_id = in6_addr2scopeid(ifidx, dst);
2815 		error = in6_embedscope(dst, &sin6, NULL);
2816 		if (error) {
2817 			/* should be impossible */
2818 			return error;
2819 		}
2820 		rt = icmp6_mtudisc_clone(&sin6, rtableid, 1);
2821 		rt_mtucloned = 1;
2822 	}
2823 	DPRINTF("spi %08x mtu %d rt %p cloned %d",
2824 	    ntohl(tdb->tdb_spi), tdb->tdb_mtu, rt, rt_mtucloned);
2825 	if (rt != NULL) {
2826 		rt->rt_mtu = tdb->tdb_mtu;
2827 		if (ro != NULL && ro->ro_rt != NULL) {
2828 			rtfree(ro->ro_rt);
2829 			ro->ro_rt = rtalloc(sin6tosa(&ro->ro_dst), RT_RESOLVE,
2830 			    rtableid);
2831 		}
2832 		if (rt_mtucloned)
2833 			rtfree(rt);
2834 	}
2835 	return 0;
2836 }
2837 
2838 int
2839 ip6_output_ipsec_send(struct tdb *tdb, struct mbuf *m, struct route_in6 *ro,
2840     int tunalready, int fwd)
2841 {
2842 	struct mbuf_list ml;
2843 	struct ifnet *encif = NULL;
2844 	struct ip6_hdr *ip6;
2845 	struct in6_addr dst;
2846 	u_int len;
2847 	int error, ifidx, rtableid, tso = 0;
2848 
2849 #if NPF > 0
2850 	/*
2851 	 * Packet filter
2852 	 */
2853 	if ((encif = enc_getif(tdb->tdb_rdomain, tdb->tdb_tap)) == NULL ||
2854 	    pf_test(AF_INET6, fwd ? PF_FWD : PF_OUT, encif, &m) != PF_PASS) {
2855 		m_freem(m);
2856 		return EACCES;
2857 	}
2858 	if (m == NULL)
2859 		return 0;
2860 	/*
2861 	 * PF_TAG_REROUTE handling or not...
2862 	 * Packet is entering IPsec so the routing is
2863 	 * already overruled by the IPsec policy.
2864 	 * Until now the change was not reconsidered.
2865 	 * What's the behaviour?
2866 	 */
2867 #endif
2868 
2869 	/* Check if we can chop the TCP packet */
2870 	ip6 = mtod(m, struct ip6_hdr *);
2871 	if (ISSET(m->m_pkthdr.csum_flags, M_TCP_TSO) &&
2872 	    m->m_pkthdr.ph_mss <= tdb->tdb_mtu) {
2873 		tso = 1;
2874 		len = m->m_pkthdr.ph_mss;
2875 	} else
2876 		len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
2877 
2878 	/* Check if we are allowed to fragment */
2879 	dst = ip6->ip6_dst;
2880 	ifidx = m->m_pkthdr.ph_ifidx;
2881 	rtableid = m->m_pkthdr.ph_rtableid;
2882 	if (ip_mtudisc && tdb->tdb_mtu &&
2883 	    len > tdb->tdb_mtu && tdb->tdb_mtutimeout > gettime()) {
2884 		int transportmode;
2885 
2886 		transportmode = (tdb->tdb_dst.sa.sa_family == AF_INET6) &&
2887 		    (IN6_ARE_ADDR_EQUAL(&tdb->tdb_dst.sin6.sin6_addr, &dst));
2888 		error = ip6_output_ipsec_pmtu_update(tdb, ro, &dst, ifidx,
2889 		    rtableid, transportmode);
2890 		if (error) {
2891 			ipsecstat_inc(ipsec_odrops);
2892 			tdbstat_inc(tdb, tdb_odrops);
2893 			m_freem(m);
2894 			return error;
2895 		}
2896 		ipsec_adjust_mtu(m, tdb->tdb_mtu);
2897 		m_freem(m);
2898 		return EMSGSIZE;
2899 	}
2900 	/* propagate don't fragment for v6-over-v6 */
2901 	if (ip_mtudisc)
2902 		SET(m->m_pkthdr.csum_flags, M_IPV6_DF_OUT);
2903 
2904 	/*
2905 	 * Clear these -- they'll be set in the recursive invocation
2906 	 * as needed.
2907 	 */
2908 	m->m_flags &= ~(M_BCAST | M_MCAST);
2909 
2910 	if (tso) {
2911 		error = tcp_chopper(m, &ml, encif, len);
2912 		if (error)
2913 			goto done;
2914 	} else {
2915 		CLR(m->m_pkthdr.csum_flags, M_TCP_TSO);
2916 		in6_proto_cksum_out(m, encif);
2917 		ml_init(&ml);
2918 		ml_enqueue(&ml, m);
2919 	}
2920 
2921 	KERNEL_LOCK();
2922 	while ((m = ml_dequeue(&ml)) != NULL) {
2923 		/* Callee frees mbuf */
2924 		error = ipsp_process_packet(m, tdb, AF_INET6, tunalready);
2925 		if (error)
2926 			break;
2927 	}
2928 	KERNEL_UNLOCK();
2929  done:
2930 	if (error) {
2931 		ml_purge(&ml);
2932 		ipsecstat_inc(ipsec_odrops);
2933 		tdbstat_inc(tdb, tdb_odrops);
2934 	}
2935 	if (!error && tso)
2936 		tcpstat_inc(tcps_outswtso);
2937 	if (ip_mtudisc && error == EMSGSIZE)
2938 		ip6_output_ipsec_pmtu_update(tdb, ro, &dst, ifidx, rtableid, 0);
2939 	return error;
2940 }
2941 #endif /* IPSEC */
2942