xref: /freebsd-11-stable/sys/netinet6/ip6_output.c (revision 531e543b44128086f232924a3ec2c8771868205e)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
30  */
31 
32 /*-
33  * Copyright (c) 1982, 1986, 1988, 1990, 1993
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 4. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
61  */
62 
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
65 
66 #include "opt_inet.h"
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
69 #include "opt_sctp.h"
70 #include "opt_route.h"
71 #include "opt_rss.h"
72 
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/errno.h>
78 #include <sys/priv.h>
79 #include <sys/proc.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
84 #include <sys/ucred.h>
85 
86 #include <machine/in_cksum.h>
87 
88 #include <net/if.h>
89 #include <net/if_var.h>
90 #include <net/if_llatbl.h>
91 #include <net/netisr.h>
92 #include <net/route.h>
93 #include <net/pfil.h>
94 #include <net/rss_config.h>
95 #include <net/vnet.h>
96 
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip_var.h>
100 #include <netinet6/in6_fib.h>
101 #include <netinet6/in6_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet/icmp6.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/tcp_var.h>
107 #include <netinet6/nd6.h>
108 #include <netinet6/in6_rss.h>
109 
110 #include <netipsec/ipsec_support.h>
111 #ifdef SCTP
112 #include <netinet/sctp.h>
113 #include <netinet/sctp_crc32.h>
114 #endif
115 
116 #include <netinet6/ip6protosw.h>
117 #include <netinet6/scope6_var.h>
118 
119 #ifdef FLOWTABLE
120 #include <net/flowtable.h>
121 #endif
122 
123 extern int in6_mcast_loop;
124 
125 struct ip6_exthdrs {
126 	struct mbuf *ip6e_ip6;
127 	struct mbuf *ip6e_hbh;
128 	struct mbuf *ip6e_dest1;
129 	struct mbuf *ip6e_rthdr;
130 	struct mbuf *ip6e_dest2;
131 };
132 
133 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
134 
135 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
136 			   struct ucred *, int);
137 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
138 	struct socket *, struct sockopt *);
139 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
140 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
141 	struct ucred *, int, int, int);
142 
143 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
144 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
145 	struct ip6_frag **);
146 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
147 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
148 static int ip6_getpmtu(struct route_in6 *, int,
149 	struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
150 	u_int);
151 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
152 	u_long *, int *, u_int);
153 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
154 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
155 
156 
157 /*
158  * Make an extension header from option data.  hp is the source,
159  * mp is the destination, and _ol is the optlen.
160  */
161 #define	MAKE_EXTHDR(hp, mp, _ol)					\
162     do {								\
163 	if (hp) {							\
164 		struct ip6_ext *eh = (struct ip6_ext *)(hp);		\
165 		error = ip6_copyexthdr((mp), (caddr_t)(hp),		\
166 		    ((eh)->ip6e_len + 1) << 3);				\
167 		if (error)						\
168 			goto freehdrs;					\
169 		(_ol) += (*(mp))->m_len;				\
170 	}								\
171     } while (/*CONSTCOND*/ 0)
172 
173 /*
174  * Form a chain of extension headers.
175  * m is the extension header mbuf
176  * mp is the previous mbuf in the chain
177  * p is the next header
178  * i is the type of option.
179  */
180 #define MAKE_CHAIN(m, mp, p, i)\
181     do {\
182 	if (m) {\
183 		if (!hdrsplit) \
184 			panic("%s:%d: assumption failed: "\
185 			    "hdr not split: hdrsplit %d exthdrs %p",\
186 			    __func__, __LINE__, hdrsplit, &exthdrs);\
187 		*mtod((m), u_char *) = *(p);\
188 		*(p) = (i);\
189 		p = mtod((m), u_char *);\
190 		(m)->m_next = (mp)->m_next;\
191 		(mp)->m_next = (m);\
192 		(mp) = (m);\
193 	}\
194     } while (/*CONSTCOND*/ 0)
195 
196 void
in6_delayed_cksum(struct mbuf * m,uint32_t plen,u_short offset)197 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
198 {
199 	u_short csum;
200 
201 	csum = in_cksum_skip(m, offset + plen, offset);
202 	if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
203 		csum = 0xffff;
204 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
205 
206 	if (offset + sizeof(csum) > m->m_len)
207 		m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
208 	else
209 		*(u_short *)mtodo(m, offset) = csum;
210 }
211 
212 static int
ip6_output_delayed_csum(struct mbuf * m,struct ifnet * ifp,int csum_flags,int plen,int optlen,bool frag __unused)213 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
214     int plen, int optlen, bool frag __unused)
215 {
216 
217 	KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
218 	    "csum_flags %#x frag %d\n",
219 	    __func__, __LINE__, plen, optlen, m, ifp, csum_flags, frag));
220 
221 	if ((csum_flags & CSUM_DELAY_DATA_IPV6) ||
222 #ifdef SCTP
223 	    (csum_flags & CSUM_SCTP_IPV6) ||
224 #endif
225 	    false) {
226 		if (csum_flags & CSUM_DELAY_DATA_IPV6) {
227 			in6_delayed_cksum(m, plen - optlen,
228 			    sizeof(struct ip6_hdr) + optlen);
229 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
230 		}
231 #ifdef SCTP
232 		if (csum_flags & CSUM_SCTP_IPV6) {
233 			sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
234 			m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
235 		}
236 #endif
237 	}
238 
239 	return (0);
240 }
241 
242 int
ip6_fragment(struct ifnet * ifp,struct mbuf * m0,int hlen,u_char nextproto,int mtu,uint32_t id)243 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
244     int mtu, uint32_t id)
245 {
246 	struct mbuf *m, **mnext, *m_frgpart;
247 	struct ip6_hdr *ip6, *mhip6;
248 	struct ip6_frag *ip6f;
249 	int off;
250 	int error;
251 	int tlen = m0->m_pkthdr.len;
252 
253 	KASSERT(( mtu % 8 == 0), ("Fragment length must be a multiple of 8"));
254 
255 	m = m0;
256 	ip6 = mtod(m, struct ip6_hdr *);
257 	mnext = &m->m_nextpkt;
258 
259 	for (off = hlen; off < tlen; off += mtu) {
260 		m = m_gethdr(M_NOWAIT, MT_DATA);
261 		if (!m) {
262 			IP6STAT_INC(ip6s_odropped);
263 			return (ENOBUFS);
264 		}
265 
266 		/*
267 		 * Make sure the complete packet header gets copied
268 		 * from the originating mbuf to the newly created
269 		 * mbuf. This also ensures that existing firewall
270 		 * classification(s), VLAN tags and so on get copied
271 		 * to the resulting fragmented packet(s):
272 		 */
273 		if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
274 			m_free(m);
275 			IP6STAT_INC(ip6s_odropped);
276 			return (ENOBUFS);
277 		}
278 
279 		*mnext = m;
280 		mnext = &m->m_nextpkt;
281 		m->m_data += max_linkhdr;
282 		mhip6 = mtod(m, struct ip6_hdr *);
283 		*mhip6 = *ip6;
284 		m->m_len = sizeof(*mhip6);
285 		error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
286 		if (error) {
287 			IP6STAT_INC(ip6s_odropped);
288 			return (error);
289 		}
290 		ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
291 		if (off + mtu >= tlen)
292 			mtu = tlen - off;
293 		else
294 			ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
295 		mhip6->ip6_plen = htons((u_short)(mtu + hlen +
296 		    sizeof(*ip6f) - sizeof(struct ip6_hdr)));
297 		if ((m_frgpart = m_copy(m0, off, mtu)) == NULL) {
298 			IP6STAT_INC(ip6s_odropped);
299 			return (ENOBUFS);
300 		}
301 		m_cat(m, m_frgpart);
302 		m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
303 		ip6f->ip6f_reserved = 0;
304 		ip6f->ip6f_ident = id;
305 		ip6f->ip6f_nxt = nextproto;
306 		IP6STAT_INC(ip6s_ofragments);
307 		in6_ifstat_inc(ifp, ifs6_out_fragcreat);
308 	}
309 
310 	return (0);
311 }
312 
313 /*
314  * IP6 output.
315  * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
316  * nxt, hlim, src, dst).
317  * This function may modify ver and hlim only.
318  * The mbuf chain containing the packet will be freed.
319  * The mbuf opt, if present, will not be freed.
320  * If route_in6 ro is present and has ro_rt initialized, route lookup would be
321  * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
322  * then result of route lookup is stored in ro->ro_rt.
323  *
324  * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
325  * is uint32_t.  So we use u_long to hold largest one, which is rt_mtu.
326  *
327  * ifpp - XXX: just for statistics
328  */
329 /*
330  * XXX TODO: no flowid is assigned for outbound flows?
331  */
332 int
ip6_output(struct mbuf * m0,struct ip6_pktopts * opt,struct route_in6 * ro,int flags,struct ip6_moptions * im6o,struct ifnet ** ifpp,struct inpcb * inp)333 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
334     struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
335     struct ifnet **ifpp, struct inpcb *inp)
336 {
337 	struct ip6_hdr *ip6;
338 	struct ifnet *ifp, *origifp;
339 	struct mbuf *m = m0;
340 	struct mbuf *mprev;
341 	int tlen, len;
342 	struct route_in6 ip6route;
343 	struct rtentry *rt = NULL;
344 	struct sockaddr_in6 *dst, src_sa, dst_sa;
345 	struct in6_addr odst;
346 	u_char *nexthdrp;
347 	int error = 0;
348 	struct in6_ifaddr *ia = NULL;
349 	u_long mtu;
350 	int alwaysfrag, dontfrag;
351 	u_int32_t optlen, plen = 0, unfragpartlen;
352 	struct ip6_exthdrs exthdrs;
353 	struct in6_addr src0, dst0;
354 	u_int32_t zone;
355 	struct route_in6 *ro_pmtu = NULL;
356 	bool hdrsplit;
357 	int sw_csum, tso;
358 	int needfiblookup;
359 	uint32_t fibnum;
360 	struct m_tag *fwd_tag = NULL;
361 	uint32_t id;
362 
363 	if (inp != NULL) {
364 		INP_LOCK_ASSERT(inp);
365 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
366 		if ((flags & IP_NODEFAULTFLOWID) == 0) {
367 			/* Unconditionally set flowid. */
368 			m->m_pkthdr.flowid = inp->inp_flowid;
369 			M_HASHTYPE_SET(m, inp->inp_flowtype);
370 		}
371 	}
372 
373 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
374 	/*
375 	 * IPSec checking which handles several cases.
376 	 * FAST IPSEC: We re-injected the packet.
377 	 * XXX: need scope argument.
378 	 */
379 	if (IPSEC_ENABLED(ipv6)) {
380 		if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
381 			if (error == EINPROGRESS)
382 				error = 0;
383 			goto done;
384 		}
385 	}
386 #endif /* IPSEC */
387 
388 	/* Source address validation. */
389 	ip6 = mtod(m, struct ip6_hdr *);
390 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
391 	    (flags & IPV6_UNSPECSRC) == 0) {
392 		error = EOPNOTSUPP;
393 		IP6STAT_INC(ip6s_badscope);
394 		goto bad;
395 	}
396 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
397 		error = EOPNOTSUPP;
398 		IP6STAT_INC(ip6s_badscope);
399 		goto bad;
400 	}
401 
402 	/*
403 	 * If we are given packet options to add extension headers prepare them.
404 	 * Calculate the total length of the extension header chain.
405 	 * Keep the length of the unfragmentable part for fragmentation.
406 	 */
407 	bzero(&exthdrs, sizeof(exthdrs));
408 	optlen = 0;
409 	unfragpartlen = sizeof(struct ip6_hdr);
410 	if (opt) {
411 		/* Hop-by-Hop options header. */
412 		MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
413 
414 		/* Destination options header (1st part). */
415 		if (opt->ip6po_rthdr) {
416 #ifndef RTHDR_SUPPORT_IMPLEMENTED
417 			/*
418 			 * If there is a routing header, discard the packet
419 			 * right away here. RH0/1 are obsolete and we do not
420 			 * currently support RH2/3/4.
421 			 * People trying to use RH253/254 may want to disable
422 			 * this check.
423 			 * The moment we do support any routing header (again)
424 			 * this block should check the routing type more
425 			 * selectively.
426 			 */
427 			error = EINVAL;
428 			goto bad;
429 #endif
430 
431 			/*
432 			 * Destination options header (1st part).
433 			 * This only makes sense with a routing header.
434 			 * See Section 9.2 of RFC 3542.
435 			 * Disabling this part just for MIP6 convenience is
436 			 * a bad idea.  We need to think carefully about a
437 			 * way to make the advanced API coexist with MIP6
438 			 * options, which might automatically be inserted in
439 			 * the kernel.
440 			 */
441 			MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
442 			    optlen);
443 		}
444 		/* Routing header. */
445 		MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
446 
447 		unfragpartlen += optlen;
448 
449 		/*
450 		 * NOTE: we don't add AH/ESP length here (done in
451 		 * ip6_ipsec_output()).
452 		 */
453 
454 		/* Destination options header (2nd part). */
455 		MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
456 	}
457 
458 	/*
459 	 * If there is at least one extension header,
460 	 * separate IP6 header from the payload.
461 	 */
462 	hdrsplit = false;
463 	if (optlen) {
464 		if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
465 			m = NULL;
466 			goto freehdrs;
467 		}
468 		m = exthdrs.ip6e_ip6;
469 		ip6 = mtod(m, struct ip6_hdr *);
470 		hdrsplit = true;
471 	}
472 
473 	/* Adjust mbuf packet header length. */
474 	m->m_pkthdr.len += optlen;
475 	plen = m->m_pkthdr.len - sizeof(*ip6);
476 
477 	/* If this is a jumbo payload, insert a jumbo payload option. */
478 	if (plen > IPV6_MAXPACKET) {
479 		if (!hdrsplit) {
480 			if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
481 				m = NULL;
482 				goto freehdrs;
483 			}
484 			m = exthdrs.ip6e_ip6;
485 			ip6 = mtod(m, struct ip6_hdr *);
486 			hdrsplit = true;
487 		}
488 		if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
489 			goto freehdrs;
490 		ip6->ip6_plen = 0;
491 	} else
492 		ip6->ip6_plen = htons(plen);
493 	nexthdrp = &ip6->ip6_nxt;
494 
495 	if (optlen) {
496 		/*
497 		 * Concatenate headers and fill in next header fields.
498 		 * Here we have, on "m"
499 		 *	IPv6 payload
500 		 * and we insert headers accordingly.
501 		 * Finally, we should be getting:
502 		 *	IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
503 		 *
504 		 * During the header composing process "m" points to IPv6
505 		 * header.  "mprev" points to an extension header prior to esp.
506 		 */
507 		mprev = m;
508 
509 		/*
510 		 * We treat dest2 specially.  This makes IPsec processing
511 		 * much easier.  The goal here is to make mprev point the
512 		 * mbuf prior to dest2.
513 		 *
514 		 * Result: IPv6 dest2 payload.
515 		 * m and mprev will point to IPv6 header.
516 		 */
517 		if (exthdrs.ip6e_dest2) {
518 			if (!hdrsplit)
519 				panic("%s:%d: assumption failed: "
520 				    "hdr not split: hdrsplit %d exthdrs %p",
521 				    __func__, __LINE__, hdrsplit, &exthdrs);
522 			exthdrs.ip6e_dest2->m_next = m->m_next;
523 			m->m_next = exthdrs.ip6e_dest2;
524 			*mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
525 			ip6->ip6_nxt = IPPROTO_DSTOPTS;
526 		}
527 
528 		/*
529 		 * Result: IPv6 hbh dest1 rthdr dest2 payload.
530 		 * m will point to IPv6 header.  mprev will point to the
531 		 * extension header prior to dest2 (rthdr in the above case).
532 		 */
533 		MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
534 		MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
535 			   IPPROTO_DSTOPTS);
536 		MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
537 			   IPPROTO_ROUTING);
538 	}
539 
540 	IP6STAT_INC(ip6s_localout);
541 
542 	/* Route packet. */
543 	if (ro == NULL) {
544 		ro = &ip6route;
545 		bzero((caddr_t)ro, sizeof(*ro));
546 	}
547 	ro_pmtu = ro;
548 	if (opt && opt->ip6po_rthdr)
549 		ro = &opt->ip6po_route;
550 	dst = (struct sockaddr_in6 *)&ro->ro_dst;
551 #ifdef FLOWTABLE
552 	if (ro->ro_rt == NULL)
553 		(void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
554 #endif
555 	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
556 again:
557 	/*
558 	 * If specified, try to fill in the traffic class field.
559 	 * Do not override if a non-zero value is already set.
560 	 * We check the diffserv field and the ECN field separately.
561 	 */
562 	if (opt && opt->ip6po_tclass >= 0) {
563 		int mask = 0;
564 
565 		if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
566 			mask |= 0xfc;
567 		if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
568 			mask |= 0x03;
569 		if (mask != 0)
570 			ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
571 	}
572 
573 	/* Fill in or override the hop limit field, if necessary. */
574 	if (opt && opt->ip6po_hlim != -1)
575 		ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
576 	else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
577 		if (im6o != NULL)
578 			ip6->ip6_hlim = im6o->im6o_multicast_hlim;
579 		else
580 			ip6->ip6_hlim = V_ip6_defmcasthlim;
581 	}
582 	/*
583 	 * Validate route against routing table additions;
584 	 * a better/more specific route might have been added.
585 	 * Make sure that the address family is set in route.
586 	 */
587 	if (inp) {
588 		ro->ro_dst.sin6_family = AF_INET6;
589 		RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
590 	}
591 	if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
592 	    ro->ro_dst.sin6_family == AF_INET6 &&
593 	    IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
594 		rt = ro->ro_rt;
595 		ifp = ro->ro_rt->rt_ifp;
596 	} else {
597 		if (ro->ro_lle)
598 			LLE_FREE(ro->ro_lle);	/* zeros ro_lle */
599 		ro->ro_lle = NULL;
600 		if (fwd_tag == NULL) {
601 			bzero(&dst_sa, sizeof(dst_sa));
602 			dst_sa.sin6_family = AF_INET6;
603 			dst_sa.sin6_len = sizeof(dst_sa);
604 			dst_sa.sin6_addr = ip6->ip6_dst;
605 		}
606 		error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
607 		    &rt, fibnum);
608 		if (error != 0) {
609 			if (ifp != NULL)
610 				in6_ifstat_inc(ifp, ifs6_out_discard);
611 			goto bad;
612 		}
613 	}
614 	if (rt == NULL) {
615 		/*
616 		 * If in6_selectroute() does not return a route entry
617 		 * dst may not have been updated.
618 		 */
619 		*dst = dst_sa;	/* XXX */
620 	}
621 
622 	/* Then rt (for unicast) and ifp must be non-NULL valid values. */
623 	if ((flags & IPV6_FORWARDING) == 0) {
624 		/* XXX: the FORWARDING flag can be set for mrouting. */
625 		in6_ifstat_inc(ifp, ifs6_out_request);
626 	}
627 	if (rt != NULL) {
628 		ia = (struct in6_ifaddr *)(rt->rt_ifa);
629 		counter_u64_add(rt->rt_pksent, 1);
630 	}
631 
632 	/* Setup data structures for scope ID checks. */
633 	src0 = ip6->ip6_src;
634 	bzero(&src_sa, sizeof(src_sa));
635 	src_sa.sin6_family = AF_INET6;
636 	src_sa.sin6_len = sizeof(src_sa);
637 	src_sa.sin6_addr = ip6->ip6_src;
638 
639 	dst0 = ip6->ip6_dst;
640 	/* Re-initialize to be sure. */
641 	bzero(&dst_sa, sizeof(dst_sa));
642 	dst_sa.sin6_family = AF_INET6;
643 	dst_sa.sin6_len = sizeof(dst_sa);
644 	dst_sa.sin6_addr = ip6->ip6_dst;
645 
646 	/* Check for valid scope ID. */
647 	if (in6_setscope(&src0, ifp, &zone) == 0 &&
648 	    sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
649 	    in6_setscope(&dst0, ifp, &zone) == 0 &&
650 	    sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
651 		/*
652 		 * The outgoing interface is in the zone of the source
653 		 * and destination addresses.
654 		 *
655 		 * Because the loopback interface cannot receive
656 		 * packets with a different scope ID than its own,
657 		 * there is a trick to pretend the outgoing packet
658 		 * was received by the real network interface, by
659 		 * setting "origifp" different from "ifp". This is
660 		 * only allowed when "ifp" is a loopback network
661 		 * interface. Refer to code in nd6_output_ifp() for
662 		 * more details.
663 		 */
664 		origifp = ifp;
665 
666 		/*
667 		 * We should use ia_ifp to support the case of sending
668 		 * packets to an address of our own.
669 		 */
670 		if (ia != NULL && ia->ia_ifp)
671 			ifp = ia->ia_ifp;
672 
673 	} else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
674 	    sa6_recoverscope(&src_sa) != 0 ||
675 	    sa6_recoverscope(&dst_sa) != 0 ||
676 	    dst_sa.sin6_scope_id == 0 ||
677 	    (src_sa.sin6_scope_id != 0 &&
678 	    src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
679 	    (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
680 		/*
681 		 * If the destination network interface is not a
682 		 * loopback interface, or the destination network
683 		 * address has no scope ID, or the source address has
684 		 * a scope ID set which is different from the
685 		 * destination address one, or there is no network
686 		 * interface representing this scope ID, the address
687 		 * pair is considered invalid.
688 		 */
689 		IP6STAT_INC(ip6s_badscope);
690 		in6_ifstat_inc(ifp, ifs6_out_discard);
691 		if (error == 0)
692 			error = EHOSTUNREACH; /* XXX */
693 		goto bad;
694 	}
695 	/* All scope ID checks are successful. */
696 
697 	if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
698 		if (opt && opt->ip6po_nextroute.ro_rt) {
699 			/*
700 			 * The nexthop is explicitly specified by the
701 			 * application.  We assume the next hop is an IPv6
702 			 * address.
703 			 */
704 			dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
705 		}
706 		else if ((rt->rt_flags & RTF_GATEWAY))
707 			dst = (struct sockaddr_in6 *)rt->rt_gateway;
708 	}
709 
710 	if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
711 		m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
712 	} else {
713 		m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
714 		in6_ifstat_inc(ifp, ifs6_out_mcast);
715 
716 		/* Confirm that the outgoing interface supports multicast. */
717 		if (!(ifp->if_flags & IFF_MULTICAST)) {
718 			IP6STAT_INC(ip6s_noroute);
719 			in6_ifstat_inc(ifp, ifs6_out_discard);
720 			error = ENETUNREACH;
721 			goto bad;
722 		}
723 		if ((im6o == NULL && in6_mcast_loop) ||
724 		    (im6o && im6o->im6o_multicast_loop)) {
725 			/*
726 			 * Loop back multicast datagram if not expressly
727 			 * forbidden to do so, even if we have not joined
728 			 * the address; protocols will filter it later,
729 			 * thus deferring a hash lookup and lock acquisition
730 			 * at the expense of an m_copym().
731 			 */
732 			ip6_mloopback(ifp, m);
733 		} else {
734 			/*
735 			 * If we are acting as a multicast router, perform
736 			 * multicast forwarding as if the packet had just
737 			 * arrived on the interface to which we are about
738 			 * to send.  The multicast forwarding function
739 			 * recursively calls this function, using the
740 			 * IPV6_FORWARDING flag to prevent infinite recursion.
741 			 *
742 			 * Multicasts that are looped back by ip6_mloopback(),
743 			 * above, will be forwarded by the ip6_input() routine,
744 			 * if necessary.
745 			 */
746 			if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
747 				/*
748 				 * XXX: ip6_mforward expects that rcvif is NULL
749 				 * when it is called from the originating path.
750 				 * However, it may not always be the case.
751 				 */
752 				m->m_pkthdr.rcvif = NULL;
753 				if (ip6_mforward(ip6, ifp, m) != 0) {
754 					m_freem(m);
755 					goto done;
756 				}
757 			}
758 		}
759 		/*
760 		 * Multicasts with a hoplimit of zero may be looped back,
761 		 * above, but must not be transmitted on a network.
762 		 * Also, multicasts addressed to the loopback interface
763 		 * are not sent -- the above call to ip6_mloopback() will
764 		 * loop back a copy if this host actually belongs to the
765 		 * destination group on the loopback interface.
766 		 */
767 		if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
768 		    IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
769 			m_freem(m);
770 			goto done;
771 		}
772 	}
773 
774 	/*
775 	 * Fill the outgoing inteface to tell the upper layer
776 	 * to increment per-interface statistics.
777 	 */
778 	if (ifpp)
779 		*ifpp = ifp;
780 
781 	/* Determine path MTU. */
782 	if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
783 		    &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
784 		goto bad;
785 
786 	/*
787 	 * The caller of this function may specify to use the minimum MTU
788 	 * in some cases.
789 	 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
790 	 * setting.  The logic is a bit complicated; by default, unicast
791 	 * packets will follow path MTU while multicast packets will be sent at
792 	 * the minimum MTU.  If IP6PO_MINMTU_ALL is specified, all packets
793 	 * including unicast ones will be sent at the minimum MTU.  Multicast
794 	 * packets will always be sent at the minimum MTU unless
795 	 * IP6PO_MINMTU_DISABLE is explicitly specified.
796 	 * See RFC 3542 for more details.
797 	 */
798 	if (mtu > IPV6_MMTU) {
799 		if ((flags & IPV6_MINMTU))
800 			mtu = IPV6_MMTU;
801 		else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
802 			mtu = IPV6_MMTU;
803 		else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
804 			 (opt == NULL ||
805 			  opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
806 			mtu = IPV6_MMTU;
807 		}
808 	}
809 
810 	/*
811 	 * Clear embedded scope identifiers if necessary.
812 	 * in6_clearscope() will touch the addresses only when necessary.
813 	 */
814 	in6_clearscope(&ip6->ip6_src);
815 	in6_clearscope(&ip6->ip6_dst);
816 
817 	/*
818 	 * If the outgoing packet contains a hop-by-hop options header,
819 	 * it must be examined and processed even by the source node.
820 	 * (RFC 2460, section 4.)
821 	 */
822 	if (exthdrs.ip6e_hbh) {
823 		struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
824 		u_int32_t dummy; /* XXX unused */
825 		u_int32_t plen = 0; /* XXX: ip6_process will check the value */
826 
827 #ifdef DIAGNOSTIC
828 		if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
829 			panic("ip6e_hbh is not contiguous");
830 #endif
831 		/*
832 		 *  XXX: if we have to send an ICMPv6 error to the sender,
833 		 *       we need the M_LOOP flag since icmp6_error() expects
834 		 *       the IPv6 and the hop-by-hop options header are
835 		 *       contiguous unless the flag is set.
836 		 */
837 		m->m_flags |= M_LOOP;
838 		m->m_pkthdr.rcvif = ifp;
839 		if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
840 		    ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
841 		    &dummy, &plen) < 0) {
842 			/* m was already freed at this point. */
843 			error = EINVAL;/* better error? */
844 			goto done;
845 		}
846 		m->m_flags &= ~M_LOOP; /* XXX */
847 		m->m_pkthdr.rcvif = NULL;
848 	}
849 
850 	/* Jump over all PFIL processing if hooks are not active. */
851 	if (!PFIL_HOOKED(&V_inet6_pfil_hook))
852 		goto passout;
853 
854 	odst = ip6->ip6_dst;
855 	/* Run through list of hooks for output packets. */
856 	error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, 0, inp);
857 	if (error != 0 || m == NULL)
858 		goto done;
859 	/* adjust pointer */
860 	ip6 = mtod(m, struct ip6_hdr *);
861 
862 	needfiblookup = 0;
863 	/* See if destination IP address was changed by packet filter. */
864 	if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
865 		m->m_flags |= M_SKIP_FIREWALL;
866 		/* If destination is now ourself drop to ip6_input(). */
867 		if (in6_localip(&ip6->ip6_dst)) {
868 			m->m_flags |= M_FASTFWD_OURS;
869 			if (m->m_pkthdr.rcvif == NULL)
870 				m->m_pkthdr.rcvif = V_loif;
871 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
872 				m->m_pkthdr.csum_flags |=
873 				    CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
874 				m->m_pkthdr.csum_data = 0xffff;
875 			}
876 #ifdef SCTP
877 			if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
878 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
879 #endif
880 			error = netisr_queue(NETISR_IPV6, m);
881 			goto done;
882 		} else {
883 			RO_RTFREE(ro);
884 			needfiblookup = 1; /* Redo the routing table lookup. */
885 			if (ro->ro_lle)
886 				LLE_FREE(ro->ro_lle);	/* zeros ro_lle */
887 			ro->ro_lle = NULL;
888 		}
889 	}
890 	/* See if fib was changed by packet filter. */
891 	if (fibnum != M_GETFIB(m)) {
892 		m->m_flags |= M_SKIP_FIREWALL;
893 		fibnum = M_GETFIB(m);
894 		RO_RTFREE(ro);
895 		needfiblookup = 1;
896 		if (ro->ro_lle)
897 			LLE_FREE(ro->ro_lle);	/* zeros ro_lle */
898 		ro->ro_lle = NULL;
899 	}
900 	if (needfiblookup)
901 		goto again;
902 
903 	/* See if local, if yes, send it to netisr. */
904 	if (m->m_flags & M_FASTFWD_OURS) {
905 		if (m->m_pkthdr.rcvif == NULL)
906 			m->m_pkthdr.rcvif = V_loif;
907 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
908 			m->m_pkthdr.csum_flags |=
909 			    CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
910 			m->m_pkthdr.csum_data = 0xffff;
911 		}
912 #ifdef SCTP
913 		if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
914 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
915 #endif
916 		error = netisr_queue(NETISR_IPV6, m);
917 		goto done;
918 	}
919 	/* Or forward to some other address? */
920 	if ((m->m_flags & M_IP6_NEXTHOP) &&
921 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
922 		dst = (struct sockaddr_in6 *)&ro->ro_dst;
923 		bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
924 		m->m_flags |= M_SKIP_FIREWALL;
925 		m->m_flags &= ~M_IP6_NEXTHOP;
926 		m_tag_delete(m, fwd_tag);
927 		goto again;
928 	}
929 
930 passout:
931 	/*
932 	 * Send the packet to the outgoing interface.
933 	 * If necessary, do IPv6 fragmentation before sending.
934 	 *
935 	 * The logic here is rather complex:
936 	 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
937 	 * 1-a:	send as is if tlen <= path mtu
938 	 * 1-b:	fragment if tlen > path mtu
939 	 *
940 	 * 2: if user asks us not to fragment (dontfrag == 1)
941 	 * 2-a:	send as is if tlen <= interface mtu
942 	 * 2-b:	error if tlen > interface mtu
943 	 *
944 	 * 3: if we always need to attach fragment header (alwaysfrag == 1)
945 	 *	always fragment
946 	 *
947 	 * 4: if dontfrag == 1 && alwaysfrag == 1
948 	 *	error, as we cannot handle this conflicting request.
949 	 */
950 	sw_csum = m->m_pkthdr.csum_flags;
951 	if (!hdrsplit) {
952 		tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
953 		sw_csum &= ~ifp->if_hwassist;
954 	} else
955 		tso = 0;
956 	/*
957 	 * If we added extension headers, we will not do TSO and calculate the
958 	 * checksums ourselves for now.
959 	 * XXX-BZ  Need a framework to know when the NIC can handle it, even
960 	 * with ext. hdrs.
961 	 */
962 	error = ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen, false);
963 	if (error != 0)
964 		goto bad;
965 	/* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
966 	tlen = m->m_pkthdr.len;
967 
968 	if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
969 		dontfrag = 1;
970 	else
971 		dontfrag = 0;
972 	if (dontfrag && alwaysfrag) {	/* Case 4. */
973 		/* Conflicting request - can't transmit. */
974 		error = EMSGSIZE;
975 		goto bad;
976 	}
977 	if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) {	/* Case 2-b. */
978 		/*
979 		 * Even if the DONTFRAG option is specified, we cannot send the
980 		 * packet when the data length is larger than the MTU of the
981 		 * outgoing interface.
982 		 * Notify the error by sending IPV6_PATHMTU ancillary data if
983 		 * application wanted to know the MTU value. Also return an
984 		 * error code (this is not described in the API spec).
985 		 */
986 		if (inp != NULL)
987 			ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
988 		error = EMSGSIZE;
989 		goto bad;
990 	}
991 
992 	/* Transmit packet without fragmentation. */
993 	if (dontfrag || (!alwaysfrag && tlen <= mtu)) {	/* Cases 1-a and 2-a. */
994 		struct in6_ifaddr *ia6;
995 
996 		ip6 = mtod(m, struct ip6_hdr *);
997 		ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
998 		if (ia6) {
999 			/* Record statistics for this interface address. */
1000 			counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1001 			counter_u64_add(ia6->ia_ifa.ifa_obytes,
1002 			    m->m_pkthdr.len);
1003 			ifa_free(&ia6->ia_ifa);
1004 		}
1005 		error = nd6_output_ifp(ifp, origifp, m, dst,
1006 		    (struct route *)ro);
1007 		goto done;
1008 	}
1009 
1010 	/* Try to fragment the packet.  Cases 1-b and 3. */
1011 	if (mtu < IPV6_MMTU) {
1012 		/* Path MTU cannot be less than IPV6_MMTU. */
1013 		error = EMSGSIZE;
1014 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
1015 		goto bad;
1016 	} else if (ip6->ip6_plen == 0) {
1017 		/* Jumbo payload cannot be fragmented. */
1018 		error = EMSGSIZE;
1019 		in6_ifstat_inc(ifp, ifs6_out_fragfail);
1020 		goto bad;
1021 	} else {
1022 		u_char nextproto;
1023 
1024 		/*
1025 		 * Too large for the destination or interface;
1026 		 * fragment if possible.
1027 		 * Must be able to put at least 8 bytes per fragment.
1028 		 */
1029 		if (mtu > IPV6_MAXPACKET)
1030 			mtu = IPV6_MAXPACKET;
1031 
1032 		len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1033 		if (len < 8) {
1034 			error = EMSGSIZE;
1035 			in6_ifstat_inc(ifp, ifs6_out_fragfail);
1036 			goto bad;
1037 		}
1038 
1039 		/*
1040 		 * If the interface will not calculate checksums on
1041 		 * fragmented packets, then do it here.
1042 		 * XXX-BZ handle the hw offloading case.  Need flags.
1043 		 */
1044 		error = ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags,
1045 		    plen, optlen, true);
1046 		if (error != 0)
1047 			goto bad;
1048 
1049 		/*
1050 		 * Change the next header field of the last header in the
1051 		 * unfragmentable part.
1052 		 */
1053 		if (exthdrs.ip6e_rthdr) {
1054 			nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1055 			*mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1056 		} else if (exthdrs.ip6e_dest1) {
1057 			nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1058 			*mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1059 		} else if (exthdrs.ip6e_hbh) {
1060 			nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1061 			*mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1062 		} else {
1063 			ip6 = mtod(m, struct ip6_hdr *);
1064 			nextproto = ip6->ip6_nxt;
1065 			ip6->ip6_nxt = IPPROTO_FRAGMENT;
1066 		}
1067 
1068 		/*
1069 		 * Loop through length of segment after first fragment,
1070 		 * make new header and copy data of each part and link onto
1071 		 * chain.
1072 		 */
1073 		m0 = m;
1074 		id = htonl(ip6_randomid());
1075 		error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1076 		if (error != 0)
1077 			goto sendorfree;
1078 
1079 		in6_ifstat_inc(ifp, ifs6_out_fragok);
1080 	}
1081 
1082 	/* Remove leading garbage. */
1083 sendorfree:
1084 	m = m0->m_nextpkt;
1085 	m0->m_nextpkt = 0;
1086 	m_freem(m0);
1087 	for (; m; m = m0) {
1088 		m0 = m->m_nextpkt;
1089 		m->m_nextpkt = 0;
1090 		if (error == 0) {
1091 			/* Record statistics for this interface address. */
1092 			if (ia) {
1093 				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1094 				counter_u64_add(ia->ia_ifa.ifa_obytes,
1095 				    m->m_pkthdr.len);
1096 			}
1097 			error = nd6_output_ifp(ifp, origifp, m, dst,
1098 			    (struct route *)ro);
1099 		} else
1100 			m_freem(m);
1101 	}
1102 
1103 	if (error == 0)
1104 		IP6STAT_INC(ip6s_fragmented);
1105 
1106 done:
1107 	/*
1108 	 * Release the route if using our private route, or if
1109 	 * (with flowtable) we don't have our own reference.
1110 	 */
1111 	if (ro == &ip6route ||
1112 	    (ro != NULL && ro->ro_flags & RT_NORTREF))
1113 		RO_RTFREE(ro);
1114 	return (error);
1115 
1116 freehdrs:
1117 	m_freem(exthdrs.ip6e_hbh);	/* m_freem() checks if mbuf is NULL. */
1118 	m_freem(exthdrs.ip6e_dest1);
1119 	m_freem(exthdrs.ip6e_rthdr);
1120 	m_freem(exthdrs.ip6e_dest2);
1121 	/* FALLTHROUGH */
1122 bad:
1123 	if (m)
1124 		m_freem(m);
1125 	goto done;
1126 }
1127 
1128 static int
ip6_copyexthdr(struct mbuf ** mp,caddr_t hdr,int hlen)1129 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1130 {
1131 	struct mbuf *m;
1132 
1133 	if (hlen > MCLBYTES)
1134 		return (ENOBUFS); /* XXX */
1135 
1136 	if (hlen > MLEN)
1137 		m = m_getcl(M_NOWAIT, MT_DATA, 0);
1138 	else
1139 		m = m_get(M_NOWAIT, MT_DATA);
1140 	if (m == NULL)
1141 		return (ENOBUFS);
1142 	m->m_len = hlen;
1143 	if (hdr)
1144 		bcopy(hdr, mtod(m, caddr_t), hlen);
1145 
1146 	*mp = m;
1147 	return (0);
1148 }
1149 
1150 /*
1151  * Insert jumbo payload option.
1152  */
1153 static int
ip6_insert_jumboopt(struct ip6_exthdrs * exthdrs,u_int32_t plen)1154 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1155 {
1156 	struct mbuf *mopt;
1157 	u_char *optbuf;
1158 	u_int32_t v;
1159 
1160 #define JUMBOOPTLEN	8	/* length of jumbo payload option and padding */
1161 
1162 	/*
1163 	 * If there is no hop-by-hop options header, allocate new one.
1164 	 * If there is one but it doesn't have enough space to store the
1165 	 * jumbo payload option, allocate a cluster to store the whole options.
1166 	 * Otherwise, use it to store the options.
1167 	 */
1168 	if (exthdrs->ip6e_hbh == NULL) {
1169 		mopt = m_get(M_NOWAIT, MT_DATA);
1170 		if (mopt == NULL)
1171 			return (ENOBUFS);
1172 		mopt->m_len = JUMBOOPTLEN;
1173 		optbuf = mtod(mopt, u_char *);
1174 		optbuf[1] = 0;	/* = ((JUMBOOPTLEN) >> 3) - 1 */
1175 		exthdrs->ip6e_hbh = mopt;
1176 	} else {
1177 		struct ip6_hbh *hbh;
1178 
1179 		mopt = exthdrs->ip6e_hbh;
1180 		if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1181 			/*
1182 			 * XXX assumption:
1183 			 * - exthdrs->ip6e_hbh is not referenced from places
1184 			 *   other than exthdrs.
1185 			 * - exthdrs->ip6e_hbh is not an mbuf chain.
1186 			 */
1187 			int oldoptlen = mopt->m_len;
1188 			struct mbuf *n;
1189 
1190 			/*
1191 			 * XXX: give up if the whole (new) hbh header does
1192 			 * not fit even in an mbuf cluster.
1193 			 */
1194 			if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1195 				return (ENOBUFS);
1196 
1197 			/*
1198 			 * As a consequence, we must always prepare a cluster
1199 			 * at this point.
1200 			 */
1201 			n = m_getcl(M_NOWAIT, MT_DATA, 0);
1202 			if (n == NULL)
1203 				return (ENOBUFS);
1204 			n->m_len = oldoptlen + JUMBOOPTLEN;
1205 			bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1206 			    oldoptlen);
1207 			optbuf = mtod(n, caddr_t) + oldoptlen;
1208 			m_freem(mopt);
1209 			mopt = exthdrs->ip6e_hbh = n;
1210 		} else {
1211 			optbuf = mtod(mopt, u_char *) + mopt->m_len;
1212 			mopt->m_len += JUMBOOPTLEN;
1213 		}
1214 		optbuf[0] = IP6OPT_PADN;
1215 		optbuf[1] = 1;
1216 
1217 		/*
1218 		 * Adjust the header length according to the pad and
1219 		 * the jumbo payload option.
1220 		 */
1221 		hbh = mtod(mopt, struct ip6_hbh *);
1222 		hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1223 	}
1224 
1225 	/* fill in the option. */
1226 	optbuf[2] = IP6OPT_JUMBO;
1227 	optbuf[3] = 4;
1228 	v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1229 	bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1230 
1231 	/* finally, adjust the packet header length */
1232 	exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1233 
1234 	return (0);
1235 #undef JUMBOOPTLEN
1236 }
1237 
1238 /*
1239  * Insert fragment header and copy unfragmentable header portions.
1240  */
1241 static int
ip6_insertfraghdr(struct mbuf * m0,struct mbuf * m,int hlen,struct ip6_frag ** frghdrp)1242 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1243     struct ip6_frag **frghdrp)
1244 {
1245 	struct mbuf *n, *mlast;
1246 
1247 	if (hlen > sizeof(struct ip6_hdr)) {
1248 		n = m_copym(m0, sizeof(struct ip6_hdr),
1249 		    hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1250 		if (n == NULL)
1251 			return (ENOBUFS);
1252 		m->m_next = n;
1253 	} else
1254 		n = m;
1255 
1256 	/* Search for the last mbuf of unfragmentable part. */
1257 	for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1258 		;
1259 
1260 	if (M_WRITABLE(mlast) &&
1261 	    M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1262 		/* use the trailing space of the last mbuf for the fragment hdr */
1263 		*frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1264 		    mlast->m_len);
1265 		mlast->m_len += sizeof(struct ip6_frag);
1266 		m->m_pkthdr.len += sizeof(struct ip6_frag);
1267 	} else {
1268 		/* allocate a new mbuf for the fragment header */
1269 		struct mbuf *mfrg;
1270 
1271 		mfrg = m_get(M_NOWAIT, MT_DATA);
1272 		if (mfrg == NULL)
1273 			return (ENOBUFS);
1274 		mfrg->m_len = sizeof(struct ip6_frag);
1275 		*frghdrp = mtod(mfrg, struct ip6_frag *);
1276 		mlast->m_next = mfrg;
1277 	}
1278 
1279 	return (0);
1280 }
1281 
1282 /*
1283  * Calculates IPv6 path mtu for destination @dst.
1284  * Resulting MTU is stored in @mtup.
1285  *
1286  * Returns 0 on success.
1287  */
1288 static int
ip6_getpmtu_ctl(u_int fibnum,const struct in6_addr * dst,u_long * mtup)1289 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1290 {
1291 	struct nhop6_extended nh6;
1292 	struct in6_addr kdst;
1293 	uint32_t scopeid;
1294 	struct ifnet *ifp;
1295 	u_long mtu;
1296 	int error;
1297 
1298 	in6_splitscope(dst, &kdst, &scopeid);
1299 	if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1300 		return (EHOSTUNREACH);
1301 
1302 	ifp = nh6.nh_ifp;
1303 	mtu = nh6.nh_mtu;
1304 
1305 	error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1306 	fib6_free_nh_ext(fibnum, &nh6);
1307 
1308 	return (error);
1309 }
1310 
1311 /*
1312  * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1313  * and cached data in @ro_pmtu.
1314  * MTU from (successful) route lookup is saved (along with dst)
1315  * inside @ro_pmtu to avoid subsequent route lookups after packet
1316  * filter processing.
1317  *
1318  * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1319  * Returns 0 on success.
1320  */
1321 static int
ip6_getpmtu(struct route_in6 * ro_pmtu,int do_lookup,struct ifnet * ifp,const struct in6_addr * dst,u_long * mtup,int * alwaysfragp,u_int fibnum,u_int proto)1322 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1323     struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1324     int *alwaysfragp, u_int fibnum, u_int proto)
1325 {
1326 	struct nhop6_basic nh6;
1327 	struct in6_addr kdst;
1328 	uint32_t scopeid;
1329 	struct sockaddr_in6 *sa6_dst;
1330 	u_long mtu;
1331 
1332 	mtu = 0;
1333 	if (do_lookup) {
1334 
1335 		/*
1336 		 * Here ro_pmtu has final destination address, while
1337 		 * ro might represent immediate destination.
1338 		 * Use ro_pmtu destination since mtu might differ.
1339 		 */
1340 		sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1341 		if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1342 			ro_pmtu->ro_mtu = 0;
1343 
1344 		if (ro_pmtu->ro_mtu == 0) {
1345 			bzero(sa6_dst, sizeof(*sa6_dst));
1346 			sa6_dst->sin6_family = AF_INET6;
1347 			sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1348 			sa6_dst->sin6_addr = *dst;
1349 
1350 			in6_splitscope(dst, &kdst, &scopeid);
1351 			if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1352 			    &nh6) == 0)
1353 				ro_pmtu->ro_mtu = nh6.nh_mtu;
1354 		}
1355 
1356 		mtu = ro_pmtu->ro_mtu;
1357 	}
1358 
1359 	if (ro_pmtu->ro_rt)
1360 		mtu = ro_pmtu->ro_rt->rt_mtu;
1361 
1362 	return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1363 }
1364 
1365 /*
1366  * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1367  * hostcache data for @dst.
1368  * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1369  *
1370  * Returns 0 on success.
1371  */
1372 static int
ip6_calcmtu(struct ifnet * ifp,const struct in6_addr * dst,u_long rt_mtu,u_long * mtup,int * alwaysfragp,u_int proto)1373 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1374     u_long *mtup, int *alwaysfragp, u_int proto)
1375 {
1376 	u_long mtu = 0;
1377 	int alwaysfrag = 0;
1378 	int error = 0;
1379 
1380 	if (rt_mtu > 0) {
1381 		u_int32_t ifmtu;
1382 		struct in_conninfo inc;
1383 
1384 		bzero(&inc, sizeof(inc));
1385 		inc.inc_flags |= INC_ISIPV6;
1386 		inc.inc6_faddr = *dst;
1387 
1388 		ifmtu = IN6_LINKMTU(ifp);
1389 
1390 		/* TCP is known to react to pmtu changes so skip hc */
1391 		if (proto != IPPROTO_TCP)
1392 			mtu = tcp_hc_getmtu(&inc);
1393 
1394 		if (mtu)
1395 			mtu = min(mtu, rt_mtu);
1396 		else
1397 			mtu = rt_mtu;
1398 		if (mtu == 0)
1399 			mtu = ifmtu;
1400 		else if (mtu < IPV6_MMTU) {
1401 			/*
1402 			 * RFC2460 section 5, last paragraph:
1403 			 * if we record ICMPv6 too big message with
1404 			 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1405 			 * or smaller, with framgent header attached.
1406 			 * (fragment header is needed regardless from the
1407 			 * packet size, for translators to identify packets)
1408 			 */
1409 			alwaysfrag = 1;
1410 			mtu = IPV6_MMTU;
1411 		}
1412 	} else if (ifp) {
1413 		mtu = IN6_LINKMTU(ifp);
1414 	} else
1415 		error = EHOSTUNREACH; /* XXX */
1416 
1417 	*mtup = mtu;
1418 	if (alwaysfragp)
1419 		*alwaysfragp = alwaysfrag;
1420 	return (error);
1421 }
1422 
1423 /*
1424  * IP6 socket option processing.
1425  */
1426 int
ip6_ctloutput(struct socket * so,struct sockopt * sopt)1427 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1428 {
1429 	int optdatalen, uproto;
1430 	void *optdata;
1431 	struct inpcb *in6p = sotoinpcb(so);
1432 	int error, optval;
1433 	int level, op, optname;
1434 	int optlen;
1435 	struct thread *td;
1436 #ifdef	RSS
1437 	uint32_t rss_bucket;
1438 	int retval;
1439 #endif
1440 
1441 /*
1442  * Don't use more than a quarter of mbuf clusters.  N.B.:
1443  * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1444  * on LP64 architectures, so cast to u_long to avoid undefined
1445  * behavior.  ILP32 architectures cannot have nmbclusters
1446  * large enough to overflow for other reasons.
1447  */
1448 #define IPV6_PKTOPTIONS_MBUF_LIMIT	((u_long)nmbclusters * MCLBYTES / 4)
1449 
1450 	level = sopt->sopt_level;
1451 	op = sopt->sopt_dir;
1452 	optname = sopt->sopt_name;
1453 	optlen = sopt->sopt_valsize;
1454 	td = sopt->sopt_td;
1455 	error = 0;
1456 	optval = 0;
1457 	uproto = (int)so->so_proto->pr_protocol;
1458 
1459 	if (level != IPPROTO_IPV6) {
1460 		error = EINVAL;
1461 
1462 		if (sopt->sopt_level == SOL_SOCKET &&
1463 		    sopt->sopt_dir == SOPT_SET) {
1464 			switch (sopt->sopt_name) {
1465 			case SO_REUSEADDR:
1466 				INP_WLOCK(in6p);
1467 				if ((so->so_options & SO_REUSEADDR) != 0)
1468 					in6p->inp_flags2 |= INP_REUSEADDR;
1469 				else
1470 					in6p->inp_flags2 &= ~INP_REUSEADDR;
1471 				INP_WUNLOCK(in6p);
1472 				error = 0;
1473 				break;
1474 			case SO_REUSEPORT:
1475 				INP_WLOCK(in6p);
1476 				if ((so->so_options & SO_REUSEPORT) != 0)
1477 					in6p->inp_flags2 |= INP_REUSEPORT;
1478 				else
1479 					in6p->inp_flags2 &= ~INP_REUSEPORT;
1480 				INP_WUNLOCK(in6p);
1481 				error = 0;
1482 				break;
1483 			case SO_SETFIB:
1484 				INP_WLOCK(in6p);
1485 				in6p->inp_inc.inc_fibnum = so->so_fibnum;
1486 				INP_WUNLOCK(in6p);
1487 				error = 0;
1488 				break;
1489 			default:
1490 				break;
1491 			}
1492 		}
1493 	} else {		/* level == IPPROTO_IPV6 */
1494 		switch (op) {
1495 
1496 		case SOPT_SET:
1497 			switch (optname) {
1498 			case IPV6_2292PKTOPTIONS:
1499 #ifdef IPV6_PKTOPTIONS
1500 			case IPV6_PKTOPTIONS:
1501 #endif
1502 			{
1503 				struct mbuf *m;
1504 
1505 				if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1506 					printf("ip6_ctloutput: mbuf limit hit\n");
1507 					error = ENOBUFS;
1508 					break;
1509 				}
1510 
1511 				error = soopt_getm(sopt, &m); /* XXX */
1512 				if (error != 0)
1513 					break;
1514 				error = soopt_mcopyin(sopt, m); /* XXX */
1515 				if (error != 0)
1516 					break;
1517 				INP_WLOCK(in6p);
1518 				error = ip6_pcbopts(&in6p->in6p_outputopts,
1519 						    m, so, sopt);
1520 				INP_WUNLOCK(in6p);
1521 				m_freem(m); /* XXX */
1522 				break;
1523 			}
1524 
1525 			/*
1526 			 * Use of some Hop-by-Hop options or some
1527 			 * Destination options, might require special
1528 			 * privilege.  That is, normal applications
1529 			 * (without special privilege) might be forbidden
1530 			 * from setting certain options in outgoing packets,
1531 			 * and might never see certain options in received
1532 			 * packets. [RFC 2292 Section 6]
1533 			 * KAME specific note:
1534 			 *  KAME prevents non-privileged users from sending or
1535 			 *  receiving ANY hbh/dst options in order to avoid
1536 			 *  overhead of parsing options in the kernel.
1537 			 */
1538 			case IPV6_RECVHOPOPTS:
1539 			case IPV6_RECVDSTOPTS:
1540 			case IPV6_RECVRTHDRDSTOPTS:
1541 				if (td != NULL) {
1542 					error = priv_check(td,
1543 					    PRIV_NETINET_SETHDROPTS);
1544 					if (error)
1545 						break;
1546 				}
1547 				/* FALLTHROUGH */
1548 			case IPV6_UNICAST_HOPS:
1549 			case IPV6_HOPLIMIT:
1550 
1551 			case IPV6_RECVPKTINFO:
1552 			case IPV6_RECVHOPLIMIT:
1553 			case IPV6_RECVRTHDR:
1554 			case IPV6_RECVPATHMTU:
1555 			case IPV6_RECVTCLASS:
1556 			case IPV6_RECVFLOWID:
1557 #ifdef	RSS
1558 			case IPV6_RECVRSSBUCKETID:
1559 #endif
1560 			case IPV6_V6ONLY:
1561 			case IPV6_AUTOFLOWLABEL:
1562 			case IPV6_BINDANY:
1563 			case IPV6_BINDMULTI:
1564 #ifdef	RSS
1565 			case IPV6_RSS_LISTEN_BUCKET:
1566 #endif
1567 				if (optname == IPV6_BINDANY && td != NULL) {
1568 					error = priv_check(td,
1569 					    PRIV_NETINET_BINDANY);
1570 					if (error)
1571 						break;
1572 				}
1573 
1574 				if (optlen != sizeof(int)) {
1575 					error = EINVAL;
1576 					break;
1577 				}
1578 				error = sooptcopyin(sopt, &optval,
1579 					sizeof optval, sizeof optval);
1580 				if (error)
1581 					break;
1582 				switch (optname) {
1583 
1584 				case IPV6_UNICAST_HOPS:
1585 					if (optval < -1 || optval >= 256)
1586 						error = EINVAL;
1587 					else {
1588 						/* -1 = kernel default */
1589 						in6p->in6p_hops = optval;
1590 						if ((in6p->inp_vflag &
1591 						     INP_IPV4) != 0)
1592 							in6p->inp_ip_ttl = optval;
1593 					}
1594 					break;
1595 #define OPTSET(bit) \
1596 do { \
1597 	INP_WLOCK(in6p); \
1598 	if (optval) \
1599 		in6p->inp_flags |= (bit); \
1600 	else \
1601 		in6p->inp_flags &= ~(bit); \
1602 	INP_WUNLOCK(in6p); \
1603 } while (/*CONSTCOND*/ 0)
1604 #define OPTSET2292(bit) \
1605 do { \
1606 	INP_WLOCK(in6p); \
1607 	in6p->inp_flags |= IN6P_RFC2292; \
1608 	if (optval) \
1609 		in6p->inp_flags |= (bit); \
1610 	else \
1611 		in6p->inp_flags &= ~(bit); \
1612 	INP_WUNLOCK(in6p); \
1613 } while (/*CONSTCOND*/ 0)
1614 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1615 
1616 #define OPTSET2(bit, val) do {						\
1617 	INP_WLOCK(in6p);						\
1618 	if (val)							\
1619 		in6p->inp_flags2 |= bit;				\
1620 	else								\
1621 		in6p->inp_flags2 &= ~bit;				\
1622 	INP_WUNLOCK(in6p);						\
1623 } while (0)
1624 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1625 
1626 				case IPV6_RECVPKTINFO:
1627 					/* cannot mix with RFC2292 */
1628 					if (OPTBIT(IN6P_RFC2292)) {
1629 						error = EINVAL;
1630 						break;
1631 					}
1632 					OPTSET(IN6P_PKTINFO);
1633 					break;
1634 
1635 				case IPV6_HOPLIMIT:
1636 				{
1637 					struct ip6_pktopts **optp;
1638 
1639 					/* cannot mix with RFC2292 */
1640 					if (OPTBIT(IN6P_RFC2292)) {
1641 						error = EINVAL;
1642 						break;
1643 					}
1644 					optp = &in6p->in6p_outputopts;
1645 					error = ip6_pcbopt(IPV6_HOPLIMIT,
1646 					    (u_char *)&optval, sizeof(optval),
1647 					    optp, (td != NULL) ? td->td_ucred :
1648 					    NULL, uproto);
1649 					break;
1650 				}
1651 
1652 				case IPV6_RECVHOPLIMIT:
1653 					/* cannot mix with RFC2292 */
1654 					if (OPTBIT(IN6P_RFC2292)) {
1655 						error = EINVAL;
1656 						break;
1657 					}
1658 					OPTSET(IN6P_HOPLIMIT);
1659 					break;
1660 
1661 				case IPV6_RECVHOPOPTS:
1662 					/* cannot mix with RFC2292 */
1663 					if (OPTBIT(IN6P_RFC2292)) {
1664 						error = EINVAL;
1665 						break;
1666 					}
1667 					OPTSET(IN6P_HOPOPTS);
1668 					break;
1669 
1670 				case IPV6_RECVDSTOPTS:
1671 					/* cannot mix with RFC2292 */
1672 					if (OPTBIT(IN6P_RFC2292)) {
1673 						error = EINVAL;
1674 						break;
1675 					}
1676 					OPTSET(IN6P_DSTOPTS);
1677 					break;
1678 
1679 				case IPV6_RECVRTHDRDSTOPTS:
1680 					/* cannot mix with RFC2292 */
1681 					if (OPTBIT(IN6P_RFC2292)) {
1682 						error = EINVAL;
1683 						break;
1684 					}
1685 					OPTSET(IN6P_RTHDRDSTOPTS);
1686 					break;
1687 
1688 				case IPV6_RECVRTHDR:
1689 					/* cannot mix with RFC2292 */
1690 					if (OPTBIT(IN6P_RFC2292)) {
1691 						error = EINVAL;
1692 						break;
1693 					}
1694 					OPTSET(IN6P_RTHDR);
1695 					break;
1696 
1697 				case IPV6_RECVPATHMTU:
1698 					/*
1699 					 * We ignore this option for TCP
1700 					 * sockets.
1701 					 * (RFC3542 leaves this case
1702 					 * unspecified.)
1703 					 */
1704 					if (uproto != IPPROTO_TCP)
1705 						OPTSET(IN6P_MTU);
1706 					break;
1707 
1708 				case IPV6_RECVFLOWID:
1709 					OPTSET2(INP_RECVFLOWID, optval);
1710 					break;
1711 
1712 #ifdef	RSS
1713 				case IPV6_RECVRSSBUCKETID:
1714 					OPTSET2(INP_RECVRSSBUCKETID, optval);
1715 					break;
1716 #endif
1717 
1718 				case IPV6_V6ONLY:
1719 					/*
1720 					 * make setsockopt(IPV6_V6ONLY)
1721 					 * available only prior to bind(2).
1722 					 * see ipng mailing list, Jun 22 2001.
1723 					 */
1724 					if (in6p->inp_lport ||
1725 					    !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1726 						error = EINVAL;
1727 						break;
1728 					}
1729 					OPTSET(IN6P_IPV6_V6ONLY);
1730 					if (optval)
1731 						in6p->inp_vflag &= ~INP_IPV4;
1732 					else
1733 						in6p->inp_vflag |= INP_IPV4;
1734 					break;
1735 				case IPV6_RECVTCLASS:
1736 					/* cannot mix with RFC2292 XXX */
1737 					if (OPTBIT(IN6P_RFC2292)) {
1738 						error = EINVAL;
1739 						break;
1740 					}
1741 					OPTSET(IN6P_TCLASS);
1742 					break;
1743 				case IPV6_AUTOFLOWLABEL:
1744 					OPTSET(IN6P_AUTOFLOWLABEL);
1745 					break;
1746 
1747 				case IPV6_BINDANY:
1748 					OPTSET(INP_BINDANY);
1749 					break;
1750 
1751 				case IPV6_BINDMULTI:
1752 					OPTSET2(INP_BINDMULTI, optval);
1753 					break;
1754 #ifdef	RSS
1755 				case IPV6_RSS_LISTEN_BUCKET:
1756 					if ((optval >= 0) &&
1757 					    (optval < rss_getnumbuckets())) {
1758 						in6p->inp_rss_listen_bucket = optval;
1759 						OPTSET2(INP_RSS_BUCKET_SET, 1);
1760 					} else {
1761 						error = EINVAL;
1762 					}
1763 					break;
1764 #endif
1765 				}
1766 				break;
1767 
1768 			case IPV6_TCLASS:
1769 			case IPV6_DONTFRAG:
1770 			case IPV6_USE_MIN_MTU:
1771 			case IPV6_PREFER_TEMPADDR:
1772 				if (optlen != sizeof(optval)) {
1773 					error = EINVAL;
1774 					break;
1775 				}
1776 				error = sooptcopyin(sopt, &optval,
1777 					sizeof optval, sizeof optval);
1778 				if (error)
1779 					break;
1780 				{
1781 					struct ip6_pktopts **optp;
1782 					optp = &in6p->in6p_outputopts;
1783 					error = ip6_pcbopt(optname,
1784 					    (u_char *)&optval, sizeof(optval),
1785 					    optp, (td != NULL) ? td->td_ucred :
1786 					    NULL, uproto);
1787 					break;
1788 				}
1789 
1790 			case IPV6_2292PKTINFO:
1791 			case IPV6_2292HOPLIMIT:
1792 			case IPV6_2292HOPOPTS:
1793 			case IPV6_2292DSTOPTS:
1794 			case IPV6_2292RTHDR:
1795 				/* RFC 2292 */
1796 				if (optlen != sizeof(int)) {
1797 					error = EINVAL;
1798 					break;
1799 				}
1800 				error = sooptcopyin(sopt, &optval,
1801 					sizeof optval, sizeof optval);
1802 				if (error)
1803 					break;
1804 				switch (optname) {
1805 				case IPV6_2292PKTINFO:
1806 					OPTSET2292(IN6P_PKTINFO);
1807 					break;
1808 				case IPV6_2292HOPLIMIT:
1809 					OPTSET2292(IN6P_HOPLIMIT);
1810 					break;
1811 				case IPV6_2292HOPOPTS:
1812 					/*
1813 					 * Check super-user privilege.
1814 					 * See comments for IPV6_RECVHOPOPTS.
1815 					 */
1816 					if (td != NULL) {
1817 						error = priv_check(td,
1818 						    PRIV_NETINET_SETHDROPTS);
1819 						if (error)
1820 							return (error);
1821 					}
1822 					OPTSET2292(IN6P_HOPOPTS);
1823 					break;
1824 				case IPV6_2292DSTOPTS:
1825 					if (td != NULL) {
1826 						error = priv_check(td,
1827 						    PRIV_NETINET_SETHDROPTS);
1828 						if (error)
1829 							return (error);
1830 					}
1831 					OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1832 					break;
1833 				case IPV6_2292RTHDR:
1834 					OPTSET2292(IN6P_RTHDR);
1835 					break;
1836 				}
1837 				break;
1838 			case IPV6_PKTINFO:
1839 			case IPV6_HOPOPTS:
1840 			case IPV6_RTHDR:
1841 			case IPV6_DSTOPTS:
1842 			case IPV6_RTHDRDSTOPTS:
1843 			case IPV6_NEXTHOP:
1844 			{
1845 				/* new advanced API (RFC3542) */
1846 				u_char *optbuf;
1847 				u_char optbuf_storage[MCLBYTES];
1848 				int optlen;
1849 				struct ip6_pktopts **optp;
1850 
1851 				/* cannot mix with RFC2292 */
1852 				if (OPTBIT(IN6P_RFC2292)) {
1853 					error = EINVAL;
1854 					break;
1855 				}
1856 
1857 				/*
1858 				 * We only ensure valsize is not too large
1859 				 * here.  Further validation will be done
1860 				 * later.
1861 				 */
1862 				error = sooptcopyin(sopt, optbuf_storage,
1863 				    sizeof(optbuf_storage), 0);
1864 				if (error)
1865 					break;
1866 				optlen = sopt->sopt_valsize;
1867 				optbuf = optbuf_storage;
1868 				optp = &in6p->in6p_outputopts;
1869 				error = ip6_pcbopt(optname, optbuf, optlen,
1870 				    optp, (td != NULL) ? td->td_ucred : NULL,
1871 				    uproto);
1872 				break;
1873 			}
1874 #undef OPTSET
1875 
1876 			case IPV6_MULTICAST_IF:
1877 			case IPV6_MULTICAST_HOPS:
1878 			case IPV6_MULTICAST_LOOP:
1879 			case IPV6_JOIN_GROUP:
1880 			case IPV6_LEAVE_GROUP:
1881 			case IPV6_MSFILTER:
1882 			case MCAST_BLOCK_SOURCE:
1883 			case MCAST_UNBLOCK_SOURCE:
1884 			case MCAST_JOIN_GROUP:
1885 			case MCAST_LEAVE_GROUP:
1886 			case MCAST_JOIN_SOURCE_GROUP:
1887 			case MCAST_LEAVE_SOURCE_GROUP:
1888 				error = ip6_setmoptions(in6p, sopt);
1889 				break;
1890 
1891 			case IPV6_PORTRANGE:
1892 				error = sooptcopyin(sopt, &optval,
1893 				    sizeof optval, sizeof optval);
1894 				if (error)
1895 					break;
1896 
1897 				INP_WLOCK(in6p);
1898 				switch (optval) {
1899 				case IPV6_PORTRANGE_DEFAULT:
1900 					in6p->inp_flags &= ~(INP_LOWPORT);
1901 					in6p->inp_flags &= ~(INP_HIGHPORT);
1902 					break;
1903 
1904 				case IPV6_PORTRANGE_HIGH:
1905 					in6p->inp_flags &= ~(INP_LOWPORT);
1906 					in6p->inp_flags |= INP_HIGHPORT;
1907 					break;
1908 
1909 				case IPV6_PORTRANGE_LOW:
1910 					in6p->inp_flags &= ~(INP_HIGHPORT);
1911 					in6p->inp_flags |= INP_LOWPORT;
1912 					break;
1913 
1914 				default:
1915 					error = EINVAL;
1916 					break;
1917 				}
1918 				INP_WUNLOCK(in6p);
1919 				break;
1920 
1921 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1922 			case IPV6_IPSEC_POLICY:
1923 				if (IPSEC_ENABLED(ipv6)) {
1924 					error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1925 					break;
1926 				}
1927 				/* FALLTHROUGH */
1928 #endif /* IPSEC */
1929 
1930 			default:
1931 				error = ENOPROTOOPT;
1932 				break;
1933 			}
1934 			break;
1935 
1936 		case SOPT_GET:
1937 			switch (optname) {
1938 
1939 			case IPV6_2292PKTOPTIONS:
1940 #ifdef IPV6_PKTOPTIONS
1941 			case IPV6_PKTOPTIONS:
1942 #endif
1943 				/*
1944 				 * RFC3542 (effectively) deprecated the
1945 				 * semantics of the 2292-style pktoptions.
1946 				 * Since it was not reliable in nature (i.e.,
1947 				 * applications had to expect the lack of some
1948 				 * information after all), it would make sense
1949 				 * to simplify this part by always returning
1950 				 * empty data.
1951 				 */
1952 				sopt->sopt_valsize = 0;
1953 				break;
1954 
1955 			case IPV6_RECVHOPOPTS:
1956 			case IPV6_RECVDSTOPTS:
1957 			case IPV6_RECVRTHDRDSTOPTS:
1958 			case IPV6_UNICAST_HOPS:
1959 			case IPV6_RECVPKTINFO:
1960 			case IPV6_RECVHOPLIMIT:
1961 			case IPV6_RECVRTHDR:
1962 			case IPV6_RECVPATHMTU:
1963 
1964 			case IPV6_V6ONLY:
1965 			case IPV6_PORTRANGE:
1966 			case IPV6_RECVTCLASS:
1967 			case IPV6_AUTOFLOWLABEL:
1968 			case IPV6_BINDANY:
1969 			case IPV6_FLOWID:
1970 			case IPV6_FLOWTYPE:
1971 			case IPV6_RECVFLOWID:
1972 #ifdef	RSS
1973 			case IPV6_RSSBUCKETID:
1974 			case IPV6_RECVRSSBUCKETID:
1975 #endif
1976 			case IPV6_BINDMULTI:
1977 				switch (optname) {
1978 
1979 				case IPV6_RECVHOPOPTS:
1980 					optval = OPTBIT(IN6P_HOPOPTS);
1981 					break;
1982 
1983 				case IPV6_RECVDSTOPTS:
1984 					optval = OPTBIT(IN6P_DSTOPTS);
1985 					break;
1986 
1987 				case IPV6_RECVRTHDRDSTOPTS:
1988 					optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1989 					break;
1990 
1991 				case IPV6_UNICAST_HOPS:
1992 					optval = in6p->in6p_hops;
1993 					break;
1994 
1995 				case IPV6_RECVPKTINFO:
1996 					optval = OPTBIT(IN6P_PKTINFO);
1997 					break;
1998 
1999 				case IPV6_RECVHOPLIMIT:
2000 					optval = OPTBIT(IN6P_HOPLIMIT);
2001 					break;
2002 
2003 				case IPV6_RECVRTHDR:
2004 					optval = OPTBIT(IN6P_RTHDR);
2005 					break;
2006 
2007 				case IPV6_RECVPATHMTU:
2008 					optval = OPTBIT(IN6P_MTU);
2009 					break;
2010 
2011 				case IPV6_V6ONLY:
2012 					optval = OPTBIT(IN6P_IPV6_V6ONLY);
2013 					break;
2014 
2015 				case IPV6_PORTRANGE:
2016 				    {
2017 					int flags;
2018 					flags = in6p->inp_flags;
2019 					if (flags & INP_HIGHPORT)
2020 						optval = IPV6_PORTRANGE_HIGH;
2021 					else if (flags & INP_LOWPORT)
2022 						optval = IPV6_PORTRANGE_LOW;
2023 					else
2024 						optval = 0;
2025 					break;
2026 				    }
2027 				case IPV6_RECVTCLASS:
2028 					optval = OPTBIT(IN6P_TCLASS);
2029 					break;
2030 
2031 				case IPV6_AUTOFLOWLABEL:
2032 					optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2033 					break;
2034 
2035 				case IPV6_BINDANY:
2036 					optval = OPTBIT(INP_BINDANY);
2037 					break;
2038 
2039 				case IPV6_FLOWID:
2040 					optval = in6p->inp_flowid;
2041 					break;
2042 
2043 				case IPV6_FLOWTYPE:
2044 					optval = in6p->inp_flowtype;
2045 					break;
2046 
2047 				case IPV6_RECVFLOWID:
2048 					optval = OPTBIT2(INP_RECVFLOWID);
2049 					break;
2050 #ifdef	RSS
2051 				case IPV6_RSSBUCKETID:
2052 					retval =
2053 					    rss_hash2bucket(in6p->inp_flowid,
2054 					    in6p->inp_flowtype,
2055 					    &rss_bucket);
2056 					if (retval == 0)
2057 						optval = rss_bucket;
2058 					else
2059 						error = EINVAL;
2060 					break;
2061 
2062 				case IPV6_RECVRSSBUCKETID:
2063 					optval = OPTBIT2(INP_RECVRSSBUCKETID);
2064 					break;
2065 #endif
2066 
2067 				case IPV6_BINDMULTI:
2068 					optval = OPTBIT2(INP_BINDMULTI);
2069 					break;
2070 
2071 				}
2072 				if (error)
2073 					break;
2074 				error = sooptcopyout(sopt, &optval,
2075 					sizeof optval);
2076 				break;
2077 
2078 			case IPV6_PATHMTU:
2079 			{
2080 				u_long pmtu = 0;
2081 				struct ip6_mtuinfo mtuinfo;
2082 
2083 				if (!(so->so_state & SS_ISCONNECTED))
2084 					return (ENOTCONN);
2085 				/*
2086 				 * XXX: we dot not consider the case of source
2087 				 * routing, or optional information to specify
2088 				 * the outgoing interface.
2089 				 */
2090 				error = ip6_getpmtu_ctl(so->so_fibnum,
2091 				    &in6p->in6p_faddr, &pmtu);
2092 				if (error)
2093 					break;
2094 				if (pmtu > IPV6_MAXPACKET)
2095 					pmtu = IPV6_MAXPACKET;
2096 
2097 				bzero(&mtuinfo, sizeof(mtuinfo));
2098 				mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2099 				optdata = (void *)&mtuinfo;
2100 				optdatalen = sizeof(mtuinfo);
2101 				error = sooptcopyout(sopt, optdata,
2102 				    optdatalen);
2103 				break;
2104 			}
2105 
2106 			case IPV6_2292PKTINFO:
2107 			case IPV6_2292HOPLIMIT:
2108 			case IPV6_2292HOPOPTS:
2109 			case IPV6_2292RTHDR:
2110 			case IPV6_2292DSTOPTS:
2111 				switch (optname) {
2112 				case IPV6_2292PKTINFO:
2113 					optval = OPTBIT(IN6P_PKTINFO);
2114 					break;
2115 				case IPV6_2292HOPLIMIT:
2116 					optval = OPTBIT(IN6P_HOPLIMIT);
2117 					break;
2118 				case IPV6_2292HOPOPTS:
2119 					optval = OPTBIT(IN6P_HOPOPTS);
2120 					break;
2121 				case IPV6_2292RTHDR:
2122 					optval = OPTBIT(IN6P_RTHDR);
2123 					break;
2124 				case IPV6_2292DSTOPTS:
2125 					optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2126 					break;
2127 				}
2128 				error = sooptcopyout(sopt, &optval,
2129 				    sizeof optval);
2130 				break;
2131 			case IPV6_PKTINFO:
2132 			case IPV6_HOPOPTS:
2133 			case IPV6_RTHDR:
2134 			case IPV6_DSTOPTS:
2135 			case IPV6_RTHDRDSTOPTS:
2136 			case IPV6_NEXTHOP:
2137 			case IPV6_TCLASS:
2138 			case IPV6_DONTFRAG:
2139 			case IPV6_USE_MIN_MTU:
2140 			case IPV6_PREFER_TEMPADDR:
2141 				error = ip6_getpcbopt(in6p->in6p_outputopts,
2142 				    optname, sopt);
2143 				break;
2144 
2145 			case IPV6_MULTICAST_IF:
2146 			case IPV6_MULTICAST_HOPS:
2147 			case IPV6_MULTICAST_LOOP:
2148 			case IPV6_MSFILTER:
2149 				error = ip6_getmoptions(in6p, sopt);
2150 				break;
2151 
2152 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2153 			case IPV6_IPSEC_POLICY:
2154 				if (IPSEC_ENABLED(ipv6)) {
2155 					error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2156 					break;
2157 				}
2158 				/* FALLTHROUGH */
2159 #endif /* IPSEC */
2160 			default:
2161 				error = ENOPROTOOPT;
2162 				break;
2163 			}
2164 			break;
2165 		}
2166 	}
2167 	return (error);
2168 }
2169 
2170 int
ip6_raw_ctloutput(struct socket * so,struct sockopt * sopt)2171 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2172 {
2173 	int error = 0, optval, optlen;
2174 	const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2175 	struct inpcb *in6p = sotoinpcb(so);
2176 	int level, op, optname;
2177 
2178 	level = sopt->sopt_level;
2179 	op = sopt->sopt_dir;
2180 	optname = sopt->sopt_name;
2181 	optlen = sopt->sopt_valsize;
2182 
2183 	if (level != IPPROTO_IPV6) {
2184 		return (EINVAL);
2185 	}
2186 
2187 	switch (optname) {
2188 	case IPV6_CHECKSUM:
2189 		/*
2190 		 * For ICMPv6 sockets, no modification allowed for checksum
2191 		 * offset, permit "no change" values to help existing apps.
2192 		 *
2193 		 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2194 		 * for an ICMPv6 socket will fail."
2195 		 * The current behavior does not meet RFC3542.
2196 		 */
2197 		switch (op) {
2198 		case SOPT_SET:
2199 			if (optlen != sizeof(int)) {
2200 				error = EINVAL;
2201 				break;
2202 			}
2203 			error = sooptcopyin(sopt, &optval, sizeof(optval),
2204 					    sizeof(optval));
2205 			if (error)
2206 				break;
2207 			if (optval < -1 || (optval % 2) != 0) {
2208 				/*
2209 				 * The API assumes non-negative even offset
2210 				 * values or -1 as a special value.
2211 				 */
2212 				error = EINVAL;
2213 			} else if (so->so_proto->pr_protocol ==
2214 			    IPPROTO_ICMPV6) {
2215 				if (optval != icmp6off)
2216 					error = EINVAL;
2217 			} else
2218 				in6p->in6p_cksum = optval;
2219 			break;
2220 
2221 		case SOPT_GET:
2222 			if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2223 				optval = icmp6off;
2224 			else
2225 				optval = in6p->in6p_cksum;
2226 
2227 			error = sooptcopyout(sopt, &optval, sizeof(optval));
2228 			break;
2229 
2230 		default:
2231 			error = EINVAL;
2232 			break;
2233 		}
2234 		break;
2235 
2236 	default:
2237 		error = ENOPROTOOPT;
2238 		break;
2239 	}
2240 
2241 	return (error);
2242 }
2243 
2244 /*
2245  * Set up IP6 options in pcb for insertion in output packets or
2246  * specifying behavior of outgoing packets.
2247  */
2248 static int
ip6_pcbopts(struct ip6_pktopts ** pktopt,struct mbuf * m,struct socket * so,struct sockopt * sopt)2249 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2250     struct socket *so, struct sockopt *sopt)
2251 {
2252 	struct ip6_pktopts *opt = *pktopt;
2253 	int error = 0;
2254 	struct thread *td = sopt->sopt_td;
2255 
2256 	/* turn off any old options. */
2257 	if (opt) {
2258 #ifdef DIAGNOSTIC
2259 		if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2260 		    opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2261 		    opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2262 			printf("ip6_pcbopts: all specified options are cleared.\n");
2263 #endif
2264 		ip6_clearpktopts(opt, -1);
2265 	} else {
2266 		opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2267 		if (opt == NULL)
2268 			return (ENOMEM);
2269 	}
2270 	*pktopt = NULL;
2271 
2272 	if (!m || m->m_len == 0) {
2273 		/*
2274 		 * Only turning off any previous options, regardless of
2275 		 * whether the opt is just created or given.
2276 		 */
2277 		free(opt, M_IP6OPT);
2278 		return (0);
2279 	}
2280 
2281 	/*  set options specified by user. */
2282 	if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2283 	    td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2284 		ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2285 		free(opt, M_IP6OPT);
2286 		return (error);
2287 	}
2288 	*pktopt = opt;
2289 	return (0);
2290 }
2291 
2292 /*
2293  * initialize ip6_pktopts.  beware that there are non-zero default values in
2294  * the struct.
2295  */
2296 void
ip6_initpktopts(struct ip6_pktopts * opt)2297 ip6_initpktopts(struct ip6_pktopts *opt)
2298 {
2299 
2300 	bzero(opt, sizeof(*opt));
2301 	opt->ip6po_hlim = -1;	/* -1 means default hop limit */
2302 	opt->ip6po_tclass = -1;	/* -1 means default traffic class */
2303 	opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2304 	opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2305 }
2306 
2307 static int
ip6_pcbopt(int optname,u_char * buf,int len,struct ip6_pktopts ** pktopt,struct ucred * cred,int uproto)2308 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2309     struct ucred *cred, int uproto)
2310 {
2311 	struct ip6_pktopts *opt;
2312 
2313 	if (*pktopt == NULL) {
2314 		*pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2315 		    M_WAITOK);
2316 		ip6_initpktopts(*pktopt);
2317 	}
2318 	opt = *pktopt;
2319 
2320 	return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2321 }
2322 
2323 static int
ip6_getpcbopt(struct ip6_pktopts * pktopt,int optname,struct sockopt * sopt)2324 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2325 {
2326 	void *optdata = NULL;
2327 	int optdatalen = 0;
2328 	struct ip6_ext *ip6e;
2329 	int error = 0;
2330 	struct in6_pktinfo null_pktinfo;
2331 	int deftclass = 0, on;
2332 	int defminmtu = IP6PO_MINMTU_MCASTONLY;
2333 	int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2334 
2335 	switch (optname) {
2336 	case IPV6_PKTINFO:
2337 		optdata = (void *)&null_pktinfo;
2338 		if (pktopt && pktopt->ip6po_pktinfo) {
2339 			bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2340 			    sizeof(null_pktinfo));
2341 			in6_clearscope(&null_pktinfo.ipi6_addr);
2342 		} else {
2343 			/* XXX: we don't have to do this every time... */
2344 			bzero(&null_pktinfo, sizeof(null_pktinfo));
2345 		}
2346 		optdatalen = sizeof(struct in6_pktinfo);
2347 		break;
2348 	case IPV6_TCLASS:
2349 		if (pktopt && pktopt->ip6po_tclass >= 0)
2350 			optdata = (void *)&pktopt->ip6po_tclass;
2351 		else
2352 			optdata = (void *)&deftclass;
2353 		optdatalen = sizeof(int);
2354 		break;
2355 	case IPV6_HOPOPTS:
2356 		if (pktopt && pktopt->ip6po_hbh) {
2357 			optdata = (void *)pktopt->ip6po_hbh;
2358 			ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2359 			optdatalen = (ip6e->ip6e_len + 1) << 3;
2360 		}
2361 		break;
2362 	case IPV6_RTHDR:
2363 		if (pktopt && pktopt->ip6po_rthdr) {
2364 			optdata = (void *)pktopt->ip6po_rthdr;
2365 			ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2366 			optdatalen = (ip6e->ip6e_len + 1) << 3;
2367 		}
2368 		break;
2369 	case IPV6_RTHDRDSTOPTS:
2370 		if (pktopt && pktopt->ip6po_dest1) {
2371 			optdata = (void *)pktopt->ip6po_dest1;
2372 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2373 			optdatalen = (ip6e->ip6e_len + 1) << 3;
2374 		}
2375 		break;
2376 	case IPV6_DSTOPTS:
2377 		if (pktopt && pktopt->ip6po_dest2) {
2378 			optdata = (void *)pktopt->ip6po_dest2;
2379 			ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2380 			optdatalen = (ip6e->ip6e_len + 1) << 3;
2381 		}
2382 		break;
2383 	case IPV6_NEXTHOP:
2384 		if (pktopt && pktopt->ip6po_nexthop) {
2385 			optdata = (void *)pktopt->ip6po_nexthop;
2386 			optdatalen = pktopt->ip6po_nexthop->sa_len;
2387 		}
2388 		break;
2389 	case IPV6_USE_MIN_MTU:
2390 		if (pktopt)
2391 			optdata = (void *)&pktopt->ip6po_minmtu;
2392 		else
2393 			optdata = (void *)&defminmtu;
2394 		optdatalen = sizeof(int);
2395 		break;
2396 	case IPV6_DONTFRAG:
2397 		if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2398 			on = 1;
2399 		else
2400 			on = 0;
2401 		optdata = (void *)&on;
2402 		optdatalen = sizeof(on);
2403 		break;
2404 	case IPV6_PREFER_TEMPADDR:
2405 		if (pktopt)
2406 			optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2407 		else
2408 			optdata = (void *)&defpreftemp;
2409 		optdatalen = sizeof(int);
2410 		break;
2411 	default:		/* should not happen */
2412 #ifdef DIAGNOSTIC
2413 		panic("ip6_getpcbopt: unexpected option\n");
2414 #endif
2415 		return (ENOPROTOOPT);
2416 	}
2417 
2418 	error = sooptcopyout(sopt, optdata, optdatalen);
2419 
2420 	return (error);
2421 }
2422 
2423 void
ip6_clearpktopts(struct ip6_pktopts * pktopt,int optname)2424 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2425 {
2426 	if (pktopt == NULL)
2427 		return;
2428 
2429 	if (optname == -1 || optname == IPV6_PKTINFO) {
2430 		if (pktopt->ip6po_pktinfo)
2431 			free(pktopt->ip6po_pktinfo, M_IP6OPT);
2432 		pktopt->ip6po_pktinfo = NULL;
2433 	}
2434 	if (optname == -1 || optname == IPV6_HOPLIMIT)
2435 		pktopt->ip6po_hlim = -1;
2436 	if (optname == -1 || optname == IPV6_TCLASS)
2437 		pktopt->ip6po_tclass = -1;
2438 	if (optname == -1 || optname == IPV6_NEXTHOP) {
2439 		if (pktopt->ip6po_nextroute.ro_rt) {
2440 			RTFREE(pktopt->ip6po_nextroute.ro_rt);
2441 			pktopt->ip6po_nextroute.ro_rt = NULL;
2442 		}
2443 		if (pktopt->ip6po_nexthop)
2444 			free(pktopt->ip6po_nexthop, M_IP6OPT);
2445 		pktopt->ip6po_nexthop = NULL;
2446 	}
2447 	if (optname == -1 || optname == IPV6_HOPOPTS) {
2448 		if (pktopt->ip6po_hbh)
2449 			free(pktopt->ip6po_hbh, M_IP6OPT);
2450 		pktopt->ip6po_hbh = NULL;
2451 	}
2452 	if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2453 		if (pktopt->ip6po_dest1)
2454 			free(pktopt->ip6po_dest1, M_IP6OPT);
2455 		pktopt->ip6po_dest1 = NULL;
2456 	}
2457 	if (optname == -1 || optname == IPV6_RTHDR) {
2458 		if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2459 			free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2460 		pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2461 		if (pktopt->ip6po_route.ro_rt) {
2462 			RTFREE(pktopt->ip6po_route.ro_rt);
2463 			pktopt->ip6po_route.ro_rt = NULL;
2464 		}
2465 	}
2466 	if (optname == -1 || optname == IPV6_DSTOPTS) {
2467 		if (pktopt->ip6po_dest2)
2468 			free(pktopt->ip6po_dest2, M_IP6OPT);
2469 		pktopt->ip6po_dest2 = NULL;
2470 	}
2471 }
2472 
2473 #define PKTOPT_EXTHDRCPY(type) \
2474 do {\
2475 	if (src->type) {\
2476 		int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2477 		dst->type = malloc(hlen, M_IP6OPT, canwait);\
2478 		if (dst->type == NULL)\
2479 			goto bad;\
2480 		bcopy(src->type, dst->type, hlen);\
2481 	}\
2482 } while (/*CONSTCOND*/ 0)
2483 
2484 static int
copypktopts(struct ip6_pktopts * dst,struct ip6_pktopts * src,int canwait)2485 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2486 {
2487 	if (dst == NULL || src == NULL)  {
2488 		printf("ip6_clearpktopts: invalid argument\n");
2489 		return (EINVAL);
2490 	}
2491 
2492 	dst->ip6po_hlim = src->ip6po_hlim;
2493 	dst->ip6po_tclass = src->ip6po_tclass;
2494 	dst->ip6po_flags = src->ip6po_flags;
2495 	dst->ip6po_minmtu = src->ip6po_minmtu;
2496 	dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2497 	if (src->ip6po_pktinfo) {
2498 		dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2499 		    M_IP6OPT, canwait);
2500 		if (dst->ip6po_pktinfo == NULL)
2501 			goto bad;
2502 		*dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2503 	}
2504 	if (src->ip6po_nexthop) {
2505 		dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2506 		    M_IP6OPT, canwait);
2507 		if (dst->ip6po_nexthop == NULL)
2508 			goto bad;
2509 		bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2510 		    src->ip6po_nexthop->sa_len);
2511 	}
2512 	PKTOPT_EXTHDRCPY(ip6po_hbh);
2513 	PKTOPT_EXTHDRCPY(ip6po_dest1);
2514 	PKTOPT_EXTHDRCPY(ip6po_dest2);
2515 	PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2516 	return (0);
2517 
2518   bad:
2519 	ip6_clearpktopts(dst, -1);
2520 	return (ENOBUFS);
2521 }
2522 #undef PKTOPT_EXTHDRCPY
2523 
2524 struct ip6_pktopts *
ip6_copypktopts(struct ip6_pktopts * src,int canwait)2525 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2526 {
2527 	int error;
2528 	struct ip6_pktopts *dst;
2529 
2530 	dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2531 	if (dst == NULL)
2532 		return (NULL);
2533 	ip6_initpktopts(dst);
2534 
2535 	if ((error = copypktopts(dst, src, canwait)) != 0) {
2536 		free(dst, M_IP6OPT);
2537 		return (NULL);
2538 	}
2539 
2540 	return (dst);
2541 }
2542 
2543 void
ip6_freepcbopts(struct ip6_pktopts * pktopt)2544 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2545 {
2546 	if (pktopt == NULL)
2547 		return;
2548 
2549 	ip6_clearpktopts(pktopt, -1);
2550 
2551 	free(pktopt, M_IP6OPT);
2552 }
2553 
2554 /*
2555  * Set IPv6 outgoing packet options based on advanced API.
2556  */
2557 int
ip6_setpktopts(struct mbuf * control,struct ip6_pktopts * opt,struct ip6_pktopts * stickyopt,struct ucred * cred,int uproto)2558 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2559     struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2560 {
2561 	struct cmsghdr *cm = NULL;
2562 
2563 	if (control == NULL || opt == NULL)
2564 		return (EINVAL);
2565 
2566 	ip6_initpktopts(opt);
2567 	if (stickyopt) {
2568 		int error;
2569 
2570 		/*
2571 		 * If stickyopt is provided, make a local copy of the options
2572 		 * for this particular packet, then override them by ancillary
2573 		 * objects.
2574 		 * XXX: copypktopts() does not copy the cached route to a next
2575 		 * hop (if any).  This is not very good in terms of efficiency,
2576 		 * but we can allow this since this option should be rarely
2577 		 * used.
2578 		 */
2579 		if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2580 			return (error);
2581 	}
2582 
2583 	/*
2584 	 * XXX: Currently, we assume all the optional information is stored
2585 	 * in a single mbuf.
2586 	 */
2587 	if (control->m_next)
2588 		return (EINVAL);
2589 
2590 	for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2591 	    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2592 		int error;
2593 
2594 		if (control->m_len < CMSG_LEN(0))
2595 			return (EINVAL);
2596 
2597 		cm = mtod(control, struct cmsghdr *);
2598 		if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2599 			return (EINVAL);
2600 		if (cm->cmsg_level != IPPROTO_IPV6)
2601 			continue;
2602 
2603 		error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2604 		    cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2605 		if (error)
2606 			return (error);
2607 	}
2608 
2609 	return (0);
2610 }
2611 
2612 /*
2613  * Set a particular packet option, as a sticky option or an ancillary data
2614  * item.  "len" can be 0 only when it's a sticky option.
2615  * We have 4 cases of combination of "sticky" and "cmsg":
2616  * "sticky=0, cmsg=0": impossible
2617  * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2618  * "sticky=1, cmsg=0": RFC3542 socket option
2619  * "sticky=1, cmsg=1": RFC2292 socket option
2620  */
2621 static int
ip6_setpktopt(int optname,u_char * buf,int len,struct ip6_pktopts * opt,struct ucred * cred,int sticky,int cmsg,int uproto)2622 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2623     struct ucred *cred, int sticky, int cmsg, int uproto)
2624 {
2625 	int minmtupolicy, preftemp;
2626 	int error;
2627 
2628 	if (!sticky && !cmsg) {
2629 #ifdef DIAGNOSTIC
2630 		printf("ip6_setpktopt: impossible case\n");
2631 #endif
2632 		return (EINVAL);
2633 	}
2634 
2635 	/*
2636 	 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2637 	 * not be specified in the context of RFC3542.  Conversely,
2638 	 * RFC3542 types should not be specified in the context of RFC2292.
2639 	 */
2640 	if (!cmsg) {
2641 		switch (optname) {
2642 		case IPV6_2292PKTINFO:
2643 		case IPV6_2292HOPLIMIT:
2644 		case IPV6_2292NEXTHOP:
2645 		case IPV6_2292HOPOPTS:
2646 		case IPV6_2292DSTOPTS:
2647 		case IPV6_2292RTHDR:
2648 		case IPV6_2292PKTOPTIONS:
2649 			return (ENOPROTOOPT);
2650 		}
2651 	}
2652 	if (sticky && cmsg) {
2653 		switch (optname) {
2654 		case IPV6_PKTINFO:
2655 		case IPV6_HOPLIMIT:
2656 		case IPV6_NEXTHOP:
2657 		case IPV6_HOPOPTS:
2658 		case IPV6_DSTOPTS:
2659 		case IPV6_RTHDRDSTOPTS:
2660 		case IPV6_RTHDR:
2661 		case IPV6_USE_MIN_MTU:
2662 		case IPV6_DONTFRAG:
2663 		case IPV6_TCLASS:
2664 		case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2665 			return (ENOPROTOOPT);
2666 		}
2667 	}
2668 
2669 	switch (optname) {
2670 	case IPV6_2292PKTINFO:
2671 	case IPV6_PKTINFO:
2672 	{
2673 		struct ifnet *ifp = NULL;
2674 		struct in6_pktinfo *pktinfo;
2675 
2676 		if (len != sizeof(struct in6_pktinfo))
2677 			return (EINVAL);
2678 
2679 		pktinfo = (struct in6_pktinfo *)buf;
2680 
2681 		/*
2682 		 * An application can clear any sticky IPV6_PKTINFO option by
2683 		 * doing a "regular" setsockopt with ipi6_addr being
2684 		 * in6addr_any and ipi6_ifindex being zero.
2685 		 * [RFC 3542, Section 6]
2686 		 */
2687 		if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2688 		    pktinfo->ipi6_ifindex == 0 &&
2689 		    IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2690 			ip6_clearpktopts(opt, optname);
2691 			break;
2692 		}
2693 
2694 		if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2695 		    sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2696 			return (EINVAL);
2697 		}
2698 		if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2699 			return (EINVAL);
2700 		/* validate the interface index if specified. */
2701 		if (pktinfo->ipi6_ifindex > V_if_index)
2702 			 return (ENXIO);
2703 		if (pktinfo->ipi6_ifindex) {
2704 			ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2705 			if (ifp == NULL)
2706 				return (ENXIO);
2707 		}
2708 		if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2709 		    (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2710 			return (ENETDOWN);
2711 
2712 		if (ifp != NULL &&
2713 		    !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2714 			struct in6_ifaddr *ia;
2715 
2716 			in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2717 			ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2718 			if (ia == NULL)
2719 				return (EADDRNOTAVAIL);
2720 			ifa_free(&ia->ia_ifa);
2721 		}
2722 		/*
2723 		 * We store the address anyway, and let in6_selectsrc()
2724 		 * validate the specified address.  This is because ipi6_addr
2725 		 * may not have enough information about its scope zone, and
2726 		 * we may need additional information (such as outgoing
2727 		 * interface or the scope zone of a destination address) to
2728 		 * disambiguate the scope.
2729 		 * XXX: the delay of the validation may confuse the
2730 		 * application when it is used as a sticky option.
2731 		 */
2732 		if (opt->ip6po_pktinfo == NULL) {
2733 			opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2734 			    M_IP6OPT, M_NOWAIT);
2735 			if (opt->ip6po_pktinfo == NULL)
2736 				return (ENOBUFS);
2737 		}
2738 		bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2739 		break;
2740 	}
2741 
2742 	case IPV6_2292HOPLIMIT:
2743 	case IPV6_HOPLIMIT:
2744 	{
2745 		int *hlimp;
2746 
2747 		/*
2748 		 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2749 		 * to simplify the ordering among hoplimit options.
2750 		 */
2751 		if (optname == IPV6_HOPLIMIT && sticky)
2752 			return (ENOPROTOOPT);
2753 
2754 		if (len != sizeof(int))
2755 			return (EINVAL);
2756 		hlimp = (int *)buf;
2757 		if (*hlimp < -1 || *hlimp > 255)
2758 			return (EINVAL);
2759 
2760 		opt->ip6po_hlim = *hlimp;
2761 		break;
2762 	}
2763 
2764 	case IPV6_TCLASS:
2765 	{
2766 		int tclass;
2767 
2768 		if (len != sizeof(int))
2769 			return (EINVAL);
2770 		tclass = *(int *)buf;
2771 		if (tclass < -1 || tclass > 255)
2772 			return (EINVAL);
2773 
2774 		opt->ip6po_tclass = tclass;
2775 		break;
2776 	}
2777 
2778 	case IPV6_2292NEXTHOP:
2779 	case IPV6_NEXTHOP:
2780 		if (cred != NULL) {
2781 			error = priv_check_cred(cred,
2782 			    PRIV_NETINET_SETHDROPTS, 0);
2783 			if (error)
2784 				return (error);
2785 		}
2786 
2787 		if (len == 0) {	/* just remove the option */
2788 			ip6_clearpktopts(opt, IPV6_NEXTHOP);
2789 			break;
2790 		}
2791 
2792 		/* check if cmsg_len is large enough for sa_len */
2793 		if (len < sizeof(struct sockaddr) || len < *buf)
2794 			return (EINVAL);
2795 
2796 		switch (((struct sockaddr *)buf)->sa_family) {
2797 		case AF_INET6:
2798 		{
2799 			struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2800 			int error;
2801 
2802 			if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2803 				return (EINVAL);
2804 
2805 			if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2806 			    IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2807 				return (EINVAL);
2808 			}
2809 			if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2810 			    != 0) {
2811 				return (error);
2812 			}
2813 			break;
2814 		}
2815 		case AF_LINK:	/* should eventually be supported */
2816 		default:
2817 			return (EAFNOSUPPORT);
2818 		}
2819 
2820 		/* turn off the previous option, then set the new option. */
2821 		ip6_clearpktopts(opt, IPV6_NEXTHOP);
2822 		opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2823 		if (opt->ip6po_nexthop == NULL)
2824 			return (ENOBUFS);
2825 		bcopy(buf, opt->ip6po_nexthop, *buf);
2826 		break;
2827 
2828 	case IPV6_2292HOPOPTS:
2829 	case IPV6_HOPOPTS:
2830 	{
2831 		struct ip6_hbh *hbh;
2832 		int hbhlen;
2833 
2834 		/*
2835 		 * XXX: We don't allow a non-privileged user to set ANY HbH
2836 		 * options, since per-option restriction has too much
2837 		 * overhead.
2838 		 */
2839 		if (cred != NULL) {
2840 			error = priv_check_cred(cred,
2841 			    PRIV_NETINET_SETHDROPTS, 0);
2842 			if (error)
2843 				return (error);
2844 		}
2845 
2846 		if (len == 0) {
2847 			ip6_clearpktopts(opt, IPV6_HOPOPTS);
2848 			break;	/* just remove the option */
2849 		}
2850 
2851 		/* message length validation */
2852 		if (len < sizeof(struct ip6_hbh))
2853 			return (EINVAL);
2854 		hbh = (struct ip6_hbh *)buf;
2855 		hbhlen = (hbh->ip6h_len + 1) << 3;
2856 		if (len != hbhlen)
2857 			return (EINVAL);
2858 
2859 		/* turn off the previous option, then set the new option. */
2860 		ip6_clearpktopts(opt, IPV6_HOPOPTS);
2861 		opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2862 		if (opt->ip6po_hbh == NULL)
2863 			return (ENOBUFS);
2864 		bcopy(hbh, opt->ip6po_hbh, hbhlen);
2865 
2866 		break;
2867 	}
2868 
2869 	case IPV6_2292DSTOPTS:
2870 	case IPV6_DSTOPTS:
2871 	case IPV6_RTHDRDSTOPTS:
2872 	{
2873 		struct ip6_dest *dest, **newdest = NULL;
2874 		int destlen;
2875 
2876 		if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2877 			error = priv_check_cred(cred,
2878 			    PRIV_NETINET_SETHDROPTS, 0);
2879 			if (error)
2880 				return (error);
2881 		}
2882 
2883 		if (len == 0) {
2884 			ip6_clearpktopts(opt, optname);
2885 			break;	/* just remove the option */
2886 		}
2887 
2888 		/* message length validation */
2889 		if (len < sizeof(struct ip6_dest))
2890 			return (EINVAL);
2891 		dest = (struct ip6_dest *)buf;
2892 		destlen = (dest->ip6d_len + 1) << 3;
2893 		if (len != destlen)
2894 			return (EINVAL);
2895 
2896 		/*
2897 		 * Determine the position that the destination options header
2898 		 * should be inserted; before or after the routing header.
2899 		 */
2900 		switch (optname) {
2901 		case IPV6_2292DSTOPTS:
2902 			/*
2903 			 * The old advacned API is ambiguous on this point.
2904 			 * Our approach is to determine the position based
2905 			 * according to the existence of a routing header.
2906 			 * Note, however, that this depends on the order of the
2907 			 * extension headers in the ancillary data; the 1st
2908 			 * part of the destination options header must appear
2909 			 * before the routing header in the ancillary data,
2910 			 * too.
2911 			 * RFC3542 solved the ambiguity by introducing
2912 			 * separate ancillary data or option types.
2913 			 */
2914 			if (opt->ip6po_rthdr == NULL)
2915 				newdest = &opt->ip6po_dest1;
2916 			else
2917 				newdest = &opt->ip6po_dest2;
2918 			break;
2919 		case IPV6_RTHDRDSTOPTS:
2920 			newdest = &opt->ip6po_dest1;
2921 			break;
2922 		case IPV6_DSTOPTS:
2923 			newdest = &opt->ip6po_dest2;
2924 			break;
2925 		}
2926 
2927 		/* turn off the previous option, then set the new option. */
2928 		ip6_clearpktopts(opt, optname);
2929 		*newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2930 		if (*newdest == NULL)
2931 			return (ENOBUFS);
2932 		bcopy(dest, *newdest, destlen);
2933 
2934 		break;
2935 	}
2936 
2937 	case IPV6_2292RTHDR:
2938 	case IPV6_RTHDR:
2939 	{
2940 		struct ip6_rthdr *rth;
2941 		int rthlen;
2942 
2943 		if (len == 0) {
2944 			ip6_clearpktopts(opt, IPV6_RTHDR);
2945 			break;	/* just remove the option */
2946 		}
2947 
2948 		/* message length validation */
2949 		if (len < sizeof(struct ip6_rthdr))
2950 			return (EINVAL);
2951 		rth = (struct ip6_rthdr *)buf;
2952 		rthlen = (rth->ip6r_len + 1) << 3;
2953 		if (len != rthlen)
2954 			return (EINVAL);
2955 
2956 		switch (rth->ip6r_type) {
2957 		case IPV6_RTHDR_TYPE_0:
2958 			if (rth->ip6r_len == 0)	/* must contain one addr */
2959 				return (EINVAL);
2960 			if (rth->ip6r_len % 2) /* length must be even */
2961 				return (EINVAL);
2962 			if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2963 				return (EINVAL);
2964 			break;
2965 		default:
2966 			return (EINVAL);	/* not supported */
2967 		}
2968 
2969 		/* turn off the previous option */
2970 		ip6_clearpktopts(opt, IPV6_RTHDR);
2971 		opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2972 		if (opt->ip6po_rthdr == NULL)
2973 			return (ENOBUFS);
2974 		bcopy(rth, opt->ip6po_rthdr, rthlen);
2975 
2976 		break;
2977 	}
2978 
2979 	case IPV6_USE_MIN_MTU:
2980 		if (len != sizeof(int))
2981 			return (EINVAL);
2982 		minmtupolicy = *(int *)buf;
2983 		if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2984 		    minmtupolicy != IP6PO_MINMTU_DISABLE &&
2985 		    minmtupolicy != IP6PO_MINMTU_ALL) {
2986 			return (EINVAL);
2987 		}
2988 		opt->ip6po_minmtu = minmtupolicy;
2989 		break;
2990 
2991 	case IPV6_DONTFRAG:
2992 		if (len != sizeof(int))
2993 			return (EINVAL);
2994 
2995 		if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2996 			/*
2997 			 * we ignore this option for TCP sockets.
2998 			 * (RFC3542 leaves this case unspecified.)
2999 			 */
3000 			opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3001 		} else
3002 			opt->ip6po_flags |= IP6PO_DONTFRAG;
3003 		break;
3004 
3005 	case IPV6_PREFER_TEMPADDR:
3006 		if (len != sizeof(int))
3007 			return (EINVAL);
3008 		preftemp = *(int *)buf;
3009 		if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3010 		    preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3011 		    preftemp != IP6PO_TEMPADDR_PREFER) {
3012 			return (EINVAL);
3013 		}
3014 		opt->ip6po_prefer_tempaddr = preftemp;
3015 		break;
3016 
3017 	default:
3018 		return (ENOPROTOOPT);
3019 	} /* end of switch */
3020 
3021 	return (0);
3022 }
3023 
3024 /*
3025  * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3026  * packet to the input queue of a specified interface.  Note that this
3027  * calls the output routine of the loopback "driver", but with an interface
3028  * pointer that might NOT be &loif -- easier than replicating that code here.
3029  */
3030 void
ip6_mloopback(struct ifnet * ifp,struct mbuf * m)3031 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3032 {
3033 	struct mbuf *copym;
3034 	struct ip6_hdr *ip6;
3035 
3036 	copym = m_copy(m, 0, M_COPYALL);
3037 	if (copym == NULL)
3038 		return;
3039 
3040 	/*
3041 	 * Make sure to deep-copy IPv6 header portion in case the data
3042 	 * is in an mbuf cluster, so that we can safely override the IPv6
3043 	 * header portion later.
3044 	 */
3045 	if (!M_WRITABLE(copym) ||
3046 	    copym->m_len < sizeof(struct ip6_hdr)) {
3047 		copym = m_pullup(copym, sizeof(struct ip6_hdr));
3048 		if (copym == NULL)
3049 			return;
3050 	}
3051 	ip6 = mtod(copym, struct ip6_hdr *);
3052 	/*
3053 	 * clear embedded scope identifiers if necessary.
3054 	 * in6_clearscope will touch the addresses only when necessary.
3055 	 */
3056 	in6_clearscope(&ip6->ip6_src);
3057 	in6_clearscope(&ip6->ip6_dst);
3058 	if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3059 		copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3060 		    CSUM_PSEUDO_HDR;
3061 		copym->m_pkthdr.csum_data = 0xffff;
3062 	}
3063 	if_simloop(ifp, copym, AF_INET6, 0);
3064 }
3065 
3066 /*
3067  * Chop IPv6 header off from the payload.
3068  */
3069 static int
ip6_splithdr(struct mbuf * m,struct ip6_exthdrs * exthdrs)3070 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3071 {
3072 	struct mbuf *mh;
3073 	struct ip6_hdr *ip6;
3074 
3075 	ip6 = mtod(m, struct ip6_hdr *);
3076 	if (m->m_len > sizeof(*ip6)) {
3077 		mh = m_gethdr(M_NOWAIT, MT_DATA);
3078 		if (mh == NULL) {
3079 			m_freem(m);
3080 			return ENOBUFS;
3081 		}
3082 		m_move_pkthdr(mh, m);
3083 		M_ALIGN(mh, sizeof(*ip6));
3084 		m->m_len -= sizeof(*ip6);
3085 		m->m_data += sizeof(*ip6);
3086 		mh->m_next = m;
3087 		m = mh;
3088 		m->m_len = sizeof(*ip6);
3089 		bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3090 	}
3091 	exthdrs->ip6e_ip6 = m;
3092 	return 0;
3093 }
3094 
3095 /*
3096  * Compute IPv6 extension header length.
3097  */
3098 int
ip6_optlen(struct inpcb * in6p)3099 ip6_optlen(struct inpcb *in6p)
3100 {
3101 	int len;
3102 
3103 	if (!in6p->in6p_outputopts)
3104 		return 0;
3105 
3106 	len = 0;
3107 #define elen(x) \
3108     (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3109 
3110 	len += elen(in6p->in6p_outputopts->ip6po_hbh);
3111 	if (in6p->in6p_outputopts->ip6po_rthdr)
3112 		/* dest1 is valid with rthdr only */
3113 		len += elen(in6p->in6p_outputopts->ip6po_dest1);
3114 	len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3115 	len += elen(in6p->in6p_outputopts->ip6po_dest2);
3116 	return len;
3117 #undef elen
3118 }
3119