2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
71 #include "opt_kern_tls.h"
72 #include "opt_ratelimit.h"
73 #include "opt_route.h"
77 #include <sys/param.h>
78 #include <sys/kernel.h>
80 #include <sys/malloc.h>
82 #include <sys/errno.h>
85 #include <sys/protosw.h>
86 #include <sys/socket.h>
87 #include <sys/socketvar.h>
88 #include <sys/syslog.h>
89 #include <sys/ucred.h>
91 #include <machine/in_cksum.h>
94 #include <net/if_var.h>
95 #include <net/if_llatbl.h>
96 #include <net/netisr.h>
97 #include <net/route.h>
99 #include <net/rss_config.h>
100 #include <net/vnet.h>
102 #include <netinet/in.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip_var.h>
105 #include <netinet6/in6_fib.h>
106 #include <netinet6/in6_var.h>
107 #include <netinet/ip6.h>
108 #include <netinet/icmp6.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet/in_pcb.h>
111 #include <netinet/tcp_var.h>
112 #include <netinet6/nd6.h>
113 #include <netinet6/in6_rss.h>
115 #include <netipsec/ipsec_support.h>
117 #include <netinet/sctp.h>
118 #include <netinet/sctp_crc32.h>
121 #include <netinet6/ip6protosw.h>
122 #include <netinet6/scope6_var.h>
124 extern int in6_mcast_loop;
127 struct mbuf *ip6e_ip6;
128 struct mbuf *ip6e_hbh;
129 struct mbuf *ip6e_dest1;
130 struct mbuf *ip6e_rthdr;
131 struct mbuf *ip6e_dest2;
134 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
136 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
137 struct ucred *, int);
138 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
139 struct socket *, struct sockopt *);
140 static int ip6_getpcbopt(struct inpcb *, int, struct sockopt *);
141 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
142 struct ucred *, int, int, int);
144 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
145 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
147 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
148 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
149 static int ip6_getpmtu(struct route_in6 *, int,
150 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
152 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
153 u_long *, int *, u_int);
154 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
155 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
159 * Make an extension header from option data. hp is the source,
160 * mp is the destination, and _ol is the optlen.
162 #define MAKE_EXTHDR(hp, mp, _ol) \
165 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
166 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
167 ((eh)->ip6e_len + 1) << 3); \
170 (_ol) += (*(mp))->m_len; \
172 } while (/*CONSTCOND*/ 0)
175 * Form a chain of extension headers.
176 * m is the extension header mbuf
177 * mp is the previous mbuf in the chain
178 * p is the next header
179 * i is the type of option.
181 #define MAKE_CHAIN(m, mp, p, i)\
185 panic("%s:%d: assumption failed: "\
186 "hdr not split: hdrsplit %d exthdrs %p",\
187 __func__, __LINE__, hdrsplit, &exthdrs);\
188 *mtod((m), u_char *) = *(p);\
190 p = mtod((m), u_char *);\
191 (m)->m_next = (mp)->m_next;\
195 } while (/*CONSTCOND*/ 0)
198 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
202 csum = in_cksum_skip(m, offset + plen, offset);
203 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
205 offset += m->m_pkthdr.csum_data; /* checksum offset */
207 if (offset + sizeof(csum) > m->m_len)
208 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
210 *(u_short *)mtodo(m, offset) = csum;
214 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
215 int plen, int optlen, bool frag)
218 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
219 "csum_flags %#x frag %d\n",
220 __func__, __LINE__, plen, optlen, m, ifp, csum_flags, frag));
222 if ((csum_flags & CSUM_DELAY_DATA_IPV6) ||
224 (csum_flags & CSUM_SCTP_IPV6) ||
226 (!frag && (ifp->if_capenable & IFCAP_NOMAP) == 0)) {
227 m = mb_unmapped_to_ext(m);
230 in6_ifstat_inc(ifp, ifs6_out_fragfail);
232 IP6STAT_INC(ip6s_odropped);
235 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
236 in6_delayed_cksum(m, plen - optlen,
237 sizeof(struct ip6_hdr) + optlen);
238 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
241 if (csum_flags & CSUM_SCTP_IPV6) {
242 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
243 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
252 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
253 int fraglen , uint32_t id)
255 struct mbuf *m, **mnext, *m_frgpart;
256 struct ip6_hdr *ip6, *mhip6;
257 struct ip6_frag *ip6f;
260 int tlen = m0->m_pkthdr.len;
262 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
265 ip6 = mtod(m, struct ip6_hdr *);
266 mnext = &m->m_nextpkt;
268 for (off = hlen; off < tlen; off += fraglen) {
269 m = m_gethdr(M_NOWAIT, MT_DATA);
271 IP6STAT_INC(ip6s_odropped);
276 * Make sure the complete packet header gets copied
277 * from the originating mbuf to the newly created
278 * mbuf. This also ensures that existing firewall
279 * classification(s), VLAN tags and so on get copied
280 * to the resulting fragmented packet(s):
282 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
284 IP6STAT_INC(ip6s_odropped);
289 mnext = &m->m_nextpkt;
290 m->m_data += max_linkhdr;
291 mhip6 = mtod(m, struct ip6_hdr *);
293 m->m_len = sizeof(*mhip6);
294 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
296 IP6STAT_INC(ip6s_odropped);
299 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
300 if (off + fraglen >= tlen)
301 fraglen = tlen - off;
303 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
304 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
305 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
306 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
307 IP6STAT_INC(ip6s_odropped);
311 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
312 ip6f->ip6f_reserved = 0;
313 ip6f->ip6f_ident = id;
314 ip6f->ip6f_nxt = nextproto;
315 IP6STAT_INC(ip6s_ofragments);
316 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
323 ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
324 struct mbuf *m, struct sockaddr_in6 *dst, struct route_in6 *ro)
327 struct ktls_session *tls = NULL;
329 struct m_snd_tag *mst;
332 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
337 * If this is an unencrypted TLS record, save a reference to
338 * the record. This local reference is used to call
339 * ktls_output_eagain after the mbuf has been freed (thus
340 * dropping the mbuf's reference) in if_output.
342 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
343 tls = ktls_hold(m->m_next->m_ext.ext_pgs->tls);
347 * If a TLS session doesn't have a valid tag, it must
348 * have had an earlier ifp mismatch, so drop this
358 if (inp != NULL && mst == NULL) {
359 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
360 (inp->inp_snd_tag != NULL &&
361 inp->inp_snd_tag->ifp != ifp))
362 in_pcboutput_txrtlmt(inp, ifp, m);
364 if (inp->inp_snd_tag != NULL)
365 mst = inp->inp_snd_tag;
369 KASSERT(m->m_pkthdr.rcvif == NULL,
370 ("trying to add a send tag to a forwarded packet"));
371 if (mst->ifp != ifp) {
376 /* stamp send tag on mbuf */
377 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
378 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
381 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
384 /* Check for route change invalidating send tags. */
388 error = ktls_output_eagain(inp, tls);
394 in_pcboutput_eagain(inp);
401 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
402 * nxt, hlim, src, dst).
403 * This function may modify ver and hlim only.
404 * The mbuf chain containing the packet will be freed.
405 * The mbuf opt, if present, will not be freed.
406 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
407 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
408 * then result of route lookup is stored in ro->ro_rt.
410 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
411 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
413 * ifpp - XXX: just for statistics
416 * XXX TODO: no flowid is assigned for outbound flows?
419 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
420 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
421 struct ifnet **ifpp, struct inpcb *inp)
424 struct ifnet *ifp, *origifp;
427 struct route_in6 *ro_pmtu;
429 struct sockaddr_in6 *dst, sin6, src_sa, dst_sa;
430 struct in6_addr odst;
434 struct in6_ifaddr *ia = NULL;
436 int alwaysfrag, dontfrag;
437 u_int32_t optlen, plen = 0, unfragpartlen;
438 struct ip6_exthdrs exthdrs;
439 struct in6_addr src0, dst0;
445 struct m_tag *fwd_tag = NULL;
451 INP_LOCK_ASSERT(inp);
452 M_SETFIB(m, inp->inp_inc.inc_fibnum);
453 if ((flags & IP_NODEFAULTFLOWID) == 0) {
454 /* Unconditionally set flowid. */
455 m->m_pkthdr.flowid = inp->inp_flowid;
456 M_HASHTYPE_SET(m, inp->inp_flowtype);
459 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
463 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
465 * IPSec checking which handles several cases.
466 * FAST IPSEC: We re-injected the packet.
467 * XXX: need scope argument.
469 if (IPSEC_ENABLED(ipv6)) {
470 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
471 if (error == EINPROGRESS)
478 /* Source address validation. */
479 ip6 = mtod(m, struct ip6_hdr *);
480 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
481 (flags & IPV6_UNSPECSRC) == 0) {
483 IP6STAT_INC(ip6s_badscope);
486 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
488 IP6STAT_INC(ip6s_badscope);
493 * If we are given packet options to add extension headers prepare them.
494 * Calculate the total length of the extension header chain.
495 * Keep the length of the unfragmentable part for fragmentation.
497 bzero(&exthdrs, sizeof(exthdrs));
499 unfragpartlen = sizeof(struct ip6_hdr);
501 /* Hop-by-Hop options header. */
502 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
504 /* Destination options header (1st part). */
505 if (opt->ip6po_rthdr) {
506 #ifndef RTHDR_SUPPORT_IMPLEMENTED
508 * If there is a routing header, discard the packet
509 * right away here. RH0/1 are obsolete and we do not
510 * currently support RH2/3/4.
511 * People trying to use RH253/254 may want to disable
513 * The moment we do support any routing header (again)
514 * this block should check the routing type more
522 * Destination options header (1st part).
523 * This only makes sense with a routing header.
524 * See Section 9.2 of RFC 3542.
525 * Disabling this part just for MIP6 convenience is
526 * a bad idea. We need to think carefully about a
527 * way to make the advanced API coexist with MIP6
528 * options, which might automatically be inserted in
531 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
534 /* Routing header. */
535 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
537 unfragpartlen += optlen;
540 * NOTE: we don't add AH/ESP length here (done in
541 * ip6_ipsec_output()).
544 /* Destination options header (2nd part). */
545 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
549 * If there is at least one extension header,
550 * separate IP6 header from the payload.
554 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
558 m = exthdrs.ip6e_ip6;
559 ip6 = mtod(m, struct ip6_hdr *);
563 /* Adjust mbuf packet header length. */
564 m->m_pkthdr.len += optlen;
565 plen = m->m_pkthdr.len - sizeof(*ip6);
567 /* If this is a jumbo payload, insert a jumbo payload option. */
568 if (plen > IPV6_MAXPACKET) {
570 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
574 m = exthdrs.ip6e_ip6;
575 ip6 = mtod(m, struct ip6_hdr *);
578 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
582 ip6->ip6_plen = htons(plen);
583 nexthdrp = &ip6->ip6_nxt;
587 * Concatenate headers and fill in next header fields.
588 * Here we have, on "m"
590 * and we insert headers accordingly.
591 * Finally, we should be getting:
592 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
594 * During the header composing process "m" points to IPv6
595 * header. "mprev" points to an extension header prior to esp.
600 * We treat dest2 specially. This makes IPsec processing
601 * much easier. The goal here is to make mprev point the
602 * mbuf prior to dest2.
604 * Result: IPv6 dest2 payload.
605 * m and mprev will point to IPv6 header.
607 if (exthdrs.ip6e_dest2) {
609 panic("%s:%d: assumption failed: "
610 "hdr not split: hdrsplit %d exthdrs %p",
611 __func__, __LINE__, hdrsplit, &exthdrs);
612 exthdrs.ip6e_dest2->m_next = m->m_next;
613 m->m_next = exthdrs.ip6e_dest2;
614 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
615 ip6->ip6_nxt = IPPROTO_DSTOPTS;
619 * Result: IPv6 hbh dest1 rthdr dest2 payload.
620 * m will point to IPv6 header. mprev will point to the
621 * extension header prior to dest2 (rthdr in the above case).
623 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
624 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
626 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
630 IP6STAT_INC(ip6s_localout);
634 if (opt && opt->ip6po_rthdr)
635 ro = &opt->ip6po_route;
637 dst = (struct sockaddr_in6 *)&ro->ro_dst;
640 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
644 * If specified, try to fill in the traffic class field.
645 * Do not override if a non-zero value is already set.
646 * We check the diffserv field and the ECN field separately.
648 if (opt && opt->ip6po_tclass >= 0) {
651 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
653 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
656 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
659 /* Fill in or override the hop limit field, if necessary. */
660 if (opt && opt->ip6po_hlim != -1)
661 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
662 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
664 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
666 ip6->ip6_hlim = V_ip6_defmcasthlim;
669 if (ro == NULL || ro->ro_rt == NULL) {
670 bzero(dst, sizeof(*dst));
671 dst->sin6_family = AF_INET6;
672 dst->sin6_len = sizeof(*dst);
673 dst->sin6_addr = ip6->ip6_dst;
676 * Validate route against routing table changes.
677 * Make sure that the address family is set in route.
683 if (ro->ro_rt != NULL && inp != NULL) {
684 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
685 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
688 if (ro->ro_rt != NULL && fwd_tag == NULL &&
689 ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
690 ro->ro_rt->rt_ifp == NULL ||
691 !RT_LINK_IS_UP(ro->ro_rt->rt_ifp) ||
692 ro->ro_dst.sin6_family != AF_INET6 ||
693 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
694 RO_INVALIDATE_CACHE(ro);
696 if (ro->ro_rt != NULL && fwd_tag == NULL &&
697 (ro->ro_rt->rt_flags & RTF_UP) &&
698 ro->ro_dst.sin6_family == AF_INET6 &&
699 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
701 ifp = ro->ro_rt->rt_ifp;
704 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
706 if (fwd_tag == NULL) {
707 bzero(&dst_sa, sizeof(dst_sa));
708 dst_sa.sin6_family = AF_INET6;
709 dst_sa.sin6_len = sizeof(dst_sa);
710 dst_sa.sin6_addr = ip6->ip6_dst;
712 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
715 IP6STAT_INC(ip6s_noroute);
717 in6_ifstat_inc(ifp, ifs6_out_discard);
725 * If in6_selectroute() does not return a route entry
726 * dst may not have been updated.
728 *dst = dst_sa; /* XXX */
730 if (rt->rt_flags & RTF_HOST)
732 ia = (struct in6_ifaddr *)(rt->rt_ifa);
733 counter_u64_add(rt->rt_pksent, 1);
736 struct nhop6_extended nh6;
737 struct in6_addr kdst;
740 if (fwd_tag == NULL) {
741 bzero(&dst_sa, sizeof(dst_sa));
742 dst_sa.sin6_family = AF_INET6;
743 dst_sa.sin6_len = sizeof(dst_sa);
744 dst_sa.sin6_addr = ip6->ip6_dst;
747 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
749 (ifp = im6o->im6o_multicast_ifp) != NULL) {
750 /* We do not need a route lookup. */
751 *dst = dst_sa; /* XXX */
755 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
757 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
758 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
760 ifp = in6_getlinkifnet(scopeid);
761 *dst = dst_sa; /* XXX */
766 error = fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0,
769 IP6STAT_INC(ip6s_noroute);
770 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
771 error = EHOSTUNREACH;;
777 dst->sin6_addr = nh6.nh_addr;
779 fib6_free_nh_ext(fibnum, &nh6);
784 /* Then rt (for unicast) and ifp must be non-NULL valid values. */
785 if ((flags & IPV6_FORWARDING) == 0) {
786 /* XXX: the FORWARDING flag can be set for mrouting. */
787 in6_ifstat_inc(ifp, ifs6_out_request);
790 /* Setup data structures for scope ID checks. */
792 bzero(&src_sa, sizeof(src_sa));
793 src_sa.sin6_family = AF_INET6;
794 src_sa.sin6_len = sizeof(src_sa);
795 src_sa.sin6_addr = ip6->ip6_src;
798 /* Re-initialize to be sure. */
799 bzero(&dst_sa, sizeof(dst_sa));
800 dst_sa.sin6_family = AF_INET6;
801 dst_sa.sin6_len = sizeof(dst_sa);
802 dst_sa.sin6_addr = ip6->ip6_dst;
804 /* Check for valid scope ID. */
805 if (in6_setscope(&src0, ifp, &zone) == 0 &&
806 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
807 in6_setscope(&dst0, ifp, &zone) == 0 &&
808 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
810 * The outgoing interface is in the zone of the source
811 * and destination addresses.
813 * Because the loopback interface cannot receive
814 * packets with a different scope ID than its own,
815 * there is a trick to pretend the outgoing packet
816 * was received by the real network interface, by
817 * setting "origifp" different from "ifp". This is
818 * only allowed when "ifp" is a loopback network
819 * interface. Refer to code in nd6_output_ifp() for
825 * We should use ia_ifp to support the case of sending
826 * packets to an address of our own.
828 if (ia != NULL && ia->ia_ifp)
831 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
832 sa6_recoverscope(&src_sa) != 0 ||
833 sa6_recoverscope(&dst_sa) != 0 ||
834 dst_sa.sin6_scope_id == 0 ||
835 (src_sa.sin6_scope_id != 0 &&
836 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
837 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
839 * If the destination network interface is not a
840 * loopback interface, or the destination network
841 * address has no scope ID, or the source address has
842 * a scope ID set which is different from the
843 * destination address one, or there is no network
844 * interface representing this scope ID, the address
845 * pair is considered invalid.
847 IP6STAT_INC(ip6s_badscope);
848 in6_ifstat_inc(ifp, ifs6_out_discard);
850 error = EHOSTUNREACH; /* XXX */
853 /* All scope ID checks are successful. */
855 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
856 if (opt && opt->ip6po_nextroute.ro_rt) {
858 * The nexthop is explicitly specified by the
859 * application. We assume the next hop is an IPv6
862 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
864 else if ((rt->rt_flags & RTF_GATEWAY))
865 dst = (struct sockaddr_in6 *)rt->rt_gateway;
868 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
869 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
871 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
872 in6_ifstat_inc(ifp, ifs6_out_mcast);
874 /* Confirm that the outgoing interface supports multicast. */
875 if (!(ifp->if_flags & IFF_MULTICAST)) {
876 IP6STAT_INC(ip6s_noroute);
877 in6_ifstat_inc(ifp, ifs6_out_discard);
881 if ((im6o == NULL && in6_mcast_loop) ||
882 (im6o && im6o->im6o_multicast_loop)) {
884 * Loop back multicast datagram if not expressly
885 * forbidden to do so, even if we have not joined
886 * the address; protocols will filter it later,
887 * thus deferring a hash lookup and lock acquisition
888 * at the expense of an m_copym().
890 ip6_mloopback(ifp, m);
893 * If we are acting as a multicast router, perform
894 * multicast forwarding as if the packet had just
895 * arrived on the interface to which we are about
896 * to send. The multicast forwarding function
897 * recursively calls this function, using the
898 * IPV6_FORWARDING flag to prevent infinite recursion.
900 * Multicasts that are looped back by ip6_mloopback(),
901 * above, will be forwarded by the ip6_input() routine,
904 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
906 * XXX: ip6_mforward expects that rcvif is NULL
907 * when it is called from the originating path.
908 * However, it may not always be the case.
910 m->m_pkthdr.rcvif = NULL;
911 if (ip6_mforward(ip6, ifp, m) != 0) {
918 * Multicasts with a hoplimit of zero may be looped back,
919 * above, but must not be transmitted on a network.
920 * Also, multicasts addressed to the loopback interface
921 * are not sent -- the above call to ip6_mloopback() will
922 * loop back a copy if this host actually belongs to the
923 * destination group on the loopback interface.
925 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
926 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
933 * Fill the outgoing inteface to tell the upper layer
934 * to increment per-interface statistics.
939 /* Determine path MTU. */
940 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
941 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
943 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
944 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
945 ifp, alwaysfrag, fibnum));
948 * The caller of this function may specify to use the minimum MTU
950 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
951 * setting. The logic is a bit complicated; by default, unicast
952 * packets will follow path MTU while multicast packets will be sent at
953 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
954 * including unicast ones will be sent at the minimum MTU. Multicast
955 * packets will always be sent at the minimum MTU unless
956 * IP6PO_MINMTU_DISABLE is explicitly specified.
957 * See RFC 3542 for more details.
959 if (mtu > IPV6_MMTU) {
960 if ((flags & IPV6_MINMTU))
962 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
964 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
966 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
972 * Clear embedded scope identifiers if necessary.
973 * in6_clearscope() will touch the addresses only when necessary.
975 in6_clearscope(&ip6->ip6_src);
976 in6_clearscope(&ip6->ip6_dst);
979 * If the outgoing packet contains a hop-by-hop options header,
980 * it must be examined and processed even by the source node.
981 * (RFC 2460, section 4.)
983 if (exthdrs.ip6e_hbh) {
984 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
985 u_int32_t dummy; /* XXX unused */
986 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
989 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
990 panic("ip6e_hbh is not contiguous");
993 * XXX: if we have to send an ICMPv6 error to the sender,
994 * we need the M_LOOP flag since icmp6_error() expects
995 * the IPv6 and the hop-by-hop options header are
996 * contiguous unless the flag is set.
998 m->m_flags |= M_LOOP;
999 m->m_pkthdr.rcvif = ifp;
1000 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1001 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1002 &dummy, &plen) < 0) {
1003 /* m was already freed at this point. */
1004 error = EINVAL;/* better error? */
1007 m->m_flags &= ~M_LOOP; /* XXX */
1008 m->m_pkthdr.rcvif = NULL;
1011 /* Jump over all PFIL processing if hooks are not active. */
1012 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1015 odst = ip6->ip6_dst;
1016 /* Run through list of hooks for output packets. */
1017 switch (pfil_run_hooks(V_inet6_pfil_head, &m, ifp, PFIL_OUT, inp)) {
1019 ip6 = mtod(m, struct ip6_hdr *);
1029 /* See if destination IP address was changed by packet filter. */
1030 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1031 m->m_flags |= M_SKIP_FIREWALL;
1032 /* If destination is now ourself drop to ip6_input(). */
1033 if (in6_localip(&ip6->ip6_dst)) {
1034 m->m_flags |= M_FASTFWD_OURS;
1035 if (m->m_pkthdr.rcvif == NULL)
1036 m->m_pkthdr.rcvif = V_loif;
1037 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1038 m->m_pkthdr.csum_flags |=
1039 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1040 m->m_pkthdr.csum_data = 0xffff;
1043 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1044 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1046 error = netisr_queue(NETISR_IPV6, m);
1050 RO_INVALIDATE_CACHE(ro);
1051 needfiblookup = 1; /* Redo the routing table lookup. */
1054 /* See if fib was changed by packet filter. */
1055 if (fibnum != M_GETFIB(m)) {
1056 m->m_flags |= M_SKIP_FIREWALL;
1057 fibnum = M_GETFIB(m);
1059 RO_INVALIDATE_CACHE(ro);
1065 /* See if local, if yes, send it to netisr. */
1066 if (m->m_flags & M_FASTFWD_OURS) {
1067 if (m->m_pkthdr.rcvif == NULL)
1068 m->m_pkthdr.rcvif = V_loif;
1069 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1070 m->m_pkthdr.csum_flags |=
1071 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1072 m->m_pkthdr.csum_data = 0xffff;
1075 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1076 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1078 error = netisr_queue(NETISR_IPV6, m);
1081 /* Or forward to some other address? */
1082 if ((m->m_flags & M_IP6_NEXTHOP) &&
1083 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1085 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1088 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1089 m->m_flags |= M_SKIP_FIREWALL;
1090 m->m_flags &= ~M_IP6_NEXTHOP;
1091 m_tag_delete(m, fwd_tag);
1097 * Send the packet to the outgoing interface.
1098 * If necessary, do IPv6 fragmentation before sending.
1100 * The logic here is rather complex:
1101 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1102 * 1-a: send as is if tlen <= path mtu
1103 * 1-b: fragment if tlen > path mtu
1105 * 2: if user asks us not to fragment (dontfrag == 1)
1106 * 2-a: send as is if tlen <= interface mtu
1107 * 2-b: error if tlen > interface mtu
1109 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1112 * 4: if dontfrag == 1 && alwaysfrag == 1
1113 * error, as we cannot handle this conflicting request.
1115 sw_csum = m->m_pkthdr.csum_flags;
1117 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
1118 sw_csum &= ~ifp->if_hwassist;
1122 * If we added extension headers, we will not do TSO and calculate the
1123 * checksums ourselves for now.
1124 * XXX-BZ Need a framework to know when the NIC can handle it, even
1127 error = ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen, false);
1130 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1131 tlen = m->m_pkthdr.len;
1133 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1137 if (dontfrag && alwaysfrag) { /* Case 4. */
1138 /* Conflicting request - can't transmit. */
1142 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1144 * Even if the DONTFRAG option is specified, we cannot send the
1145 * packet when the data length is larger than the MTU of the
1146 * outgoing interface.
1147 * Notify the error by sending IPV6_PATHMTU ancillary data if
1148 * application wanted to know the MTU value. Also return an
1149 * error code (this is not described in the API spec).
1152 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1157 /* Transmit packet without fragmentation. */
1158 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1159 struct in6_ifaddr *ia6;
1161 ip6 = mtod(m, struct ip6_hdr *);
1162 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1164 /* Record statistics for this interface address. */
1165 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1166 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1168 ifa_free(&ia6->ia_ifa);
1170 error = ip6_output_send(inp, ifp, origifp, m, dst, ro);
1174 /* Try to fragment the packet. Cases 1-b and 3. */
1175 if (mtu < IPV6_MMTU) {
1176 /* Path MTU cannot be less than IPV6_MMTU. */
1178 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1180 } else if (ip6->ip6_plen == 0) {
1181 /* Jumbo payload cannot be fragmented. */
1183 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1189 * Too large for the destination or interface;
1190 * fragment if possible.
1191 * Must be able to put at least 8 bytes per fragment.
1193 if (mtu > IPV6_MAXPACKET)
1194 mtu = IPV6_MAXPACKET;
1196 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1199 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1204 * If the interface will not calculate checksums on
1205 * fragmented packets, then do it here.
1206 * XXX-BZ handle the hw offloading case. Need flags.
1208 error = ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags,
1209 plen, optlen, true);
1214 * Change the next header field of the last header in the
1215 * unfragmentable part.
1217 if (exthdrs.ip6e_rthdr) {
1218 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1219 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1220 } else if (exthdrs.ip6e_dest1) {
1221 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1222 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1223 } else if (exthdrs.ip6e_hbh) {
1224 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1225 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1227 ip6 = mtod(m, struct ip6_hdr *);
1228 nextproto = ip6->ip6_nxt;
1229 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1233 * Loop through length of segment after first fragment,
1234 * make new header and copy data of each part and link onto
1238 id = htonl(ip6_randomid());
1239 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1243 in6_ifstat_inc(ifp, ifs6_out_fragok);
1246 /* Remove leading garbage. */
1255 /* Record statistics for this interface address. */
1257 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1258 counter_u64_add(ia->ia_ifa.ifa_obytes,
1261 error = ip6_output_send(inp, ifp, origifp, m, dst, ro);
1267 IP6STAT_INC(ip6s_fragmented);
1273 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1274 m_freem(exthdrs.ip6e_dest1);
1275 m_freem(exthdrs.ip6e_rthdr);
1276 m_freem(exthdrs.ip6e_dest2);
1285 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1289 if (hlen > MCLBYTES)
1290 return (ENOBUFS); /* XXX */
1293 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1295 m = m_get(M_NOWAIT, MT_DATA);
1300 bcopy(hdr, mtod(m, caddr_t), hlen);
1307 * Insert jumbo payload option.
1310 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1316 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1319 * If there is no hop-by-hop options header, allocate new one.
1320 * If there is one but it doesn't have enough space to store the
1321 * jumbo payload option, allocate a cluster to store the whole options.
1322 * Otherwise, use it to store the options.
1324 if (exthdrs->ip6e_hbh == NULL) {
1325 mopt = m_get(M_NOWAIT, MT_DATA);
1328 mopt->m_len = JUMBOOPTLEN;
1329 optbuf = mtod(mopt, u_char *);
1330 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1331 exthdrs->ip6e_hbh = mopt;
1333 struct ip6_hbh *hbh;
1335 mopt = exthdrs->ip6e_hbh;
1336 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1339 * - exthdrs->ip6e_hbh is not referenced from places
1340 * other than exthdrs.
1341 * - exthdrs->ip6e_hbh is not an mbuf chain.
1343 int oldoptlen = mopt->m_len;
1347 * XXX: give up if the whole (new) hbh header does
1348 * not fit even in an mbuf cluster.
1350 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1354 * As a consequence, we must always prepare a cluster
1357 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1360 n->m_len = oldoptlen + JUMBOOPTLEN;
1361 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1363 optbuf = mtod(n, caddr_t) + oldoptlen;
1365 mopt = exthdrs->ip6e_hbh = n;
1367 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1368 mopt->m_len += JUMBOOPTLEN;
1370 optbuf[0] = IP6OPT_PADN;
1374 * Adjust the header length according to the pad and
1375 * the jumbo payload option.
1377 hbh = mtod(mopt, struct ip6_hbh *);
1378 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1381 /* fill in the option. */
1382 optbuf[2] = IP6OPT_JUMBO;
1384 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1385 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1387 /* finally, adjust the packet header length */
1388 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1395 * Insert fragment header and copy unfragmentable header portions.
1398 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1399 struct ip6_frag **frghdrp)
1401 struct mbuf *n, *mlast;
1403 if (hlen > sizeof(struct ip6_hdr)) {
1404 n = m_copym(m0, sizeof(struct ip6_hdr),
1405 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1412 /* Search for the last mbuf of unfragmentable part. */
1413 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1416 if (M_WRITABLE(mlast) &&
1417 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1418 /* use the trailing space of the last mbuf for the fragment hdr */
1419 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1421 mlast->m_len += sizeof(struct ip6_frag);
1422 m->m_pkthdr.len += sizeof(struct ip6_frag);
1424 /* allocate a new mbuf for the fragment header */
1427 mfrg = m_get(M_NOWAIT, MT_DATA);
1430 mfrg->m_len = sizeof(struct ip6_frag);
1431 *frghdrp = mtod(mfrg, struct ip6_frag *);
1432 mlast->m_next = mfrg;
1439 * Calculates IPv6 path mtu for destination @dst.
1440 * Resulting MTU is stored in @mtup.
1442 * Returns 0 on success.
1445 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1447 struct nhop6_extended nh6;
1448 struct in6_addr kdst;
1454 in6_splitscope(dst, &kdst, &scopeid);
1455 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1456 return (EHOSTUNREACH);
1461 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1462 fib6_free_nh_ext(fibnum, &nh6);
1468 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1469 * and cached data in @ro_pmtu.
1470 * MTU from (successful) route lookup is saved (along with dst)
1471 * inside @ro_pmtu to avoid subsequent route lookups after packet
1472 * filter processing.
1474 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1475 * Returns 0 on success.
1478 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1479 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1480 int *alwaysfragp, u_int fibnum, u_int proto)
1482 struct nhop6_basic nh6;
1483 struct in6_addr kdst;
1485 struct sockaddr_in6 *sa6_dst, sin6;
1489 if (ro_pmtu == NULL || do_lookup) {
1492 * Here ro_pmtu has final destination address, while
1493 * ro might represent immediate destination.
1494 * Use ro_pmtu destination since mtu might differ.
1496 if (ro_pmtu != NULL) {
1497 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1498 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1499 ro_pmtu->ro_mtu = 0;
1503 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1504 bzero(sa6_dst, sizeof(*sa6_dst));
1505 sa6_dst->sin6_family = AF_INET6;
1506 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1507 sa6_dst->sin6_addr = *dst;
1509 in6_splitscope(dst, &kdst, &scopeid);
1510 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1513 if (ro_pmtu != NULL)
1514 ro_pmtu->ro_mtu = mtu;
1517 mtu = ro_pmtu->ro_mtu;
1520 if (ro_pmtu != NULL && ro_pmtu->ro_rt != NULL)
1521 mtu = ro_pmtu->ro_rt->rt_mtu;
1523 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1527 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1528 * hostcache data for @dst.
1529 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1531 * Returns 0 on success.
1534 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1535 u_long *mtup, int *alwaysfragp, u_int proto)
1543 struct in_conninfo inc;
1545 bzero(&inc, sizeof(inc));
1546 inc.inc_flags |= INC_ISIPV6;
1547 inc.inc6_faddr = *dst;
1549 ifmtu = IN6_LINKMTU(ifp);
1551 /* TCP is known to react to pmtu changes so skip hc */
1552 if (proto != IPPROTO_TCP)
1553 mtu = tcp_hc_getmtu(&inc);
1556 mtu = min(mtu, rt_mtu);
1561 else if (mtu < IPV6_MMTU) {
1563 * RFC2460 section 5, last paragraph:
1564 * if we record ICMPv6 too big message with
1565 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1566 * or smaller, with framgent header attached.
1567 * (fragment header is needed regardless from the
1568 * packet size, for translators to identify packets)
1574 mtu = IN6_LINKMTU(ifp);
1576 error = EHOSTUNREACH; /* XXX */
1580 *alwaysfragp = alwaysfrag;
1585 * IP6 socket option processing.
1588 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1590 int optdatalen, uproto;
1592 struct inpcb *inp = sotoinpcb(so);
1594 int level, op, optname;
1598 uint32_t rss_bucket;
1603 * Don't use more than a quarter of mbuf clusters. N.B.:
1604 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1605 * on LP64 architectures, so cast to u_long to avoid undefined
1606 * behavior. ILP32 architectures cannot have nmbclusters
1607 * large enough to overflow for other reasons.
1609 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1611 level = sopt->sopt_level;
1612 op = sopt->sopt_dir;
1613 optname = sopt->sopt_name;
1614 optlen = sopt->sopt_valsize;
1618 uproto = (int)so->so_proto->pr_protocol;
1620 if (level != IPPROTO_IPV6) {
1623 if (sopt->sopt_level == SOL_SOCKET &&
1624 sopt->sopt_dir == SOPT_SET) {
1625 switch (sopt->sopt_name) {
1628 if ((so->so_options & SO_REUSEADDR) != 0)
1629 inp->inp_flags2 |= INP_REUSEADDR;
1631 inp->inp_flags2 &= ~INP_REUSEADDR;
1637 if ((so->so_options & SO_REUSEPORT) != 0)
1638 inp->inp_flags2 |= INP_REUSEPORT;
1640 inp->inp_flags2 &= ~INP_REUSEPORT;
1644 case SO_REUSEPORT_LB:
1646 if ((so->so_options & SO_REUSEPORT_LB) != 0)
1647 inp->inp_flags2 |= INP_REUSEPORT_LB;
1649 inp->inp_flags2 &= ~INP_REUSEPORT_LB;
1655 inp->inp_inc.inc_fibnum = so->so_fibnum;
1659 case SO_MAX_PACING_RATE:
1662 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1673 } else { /* level == IPPROTO_IPV6 */
1678 case IPV6_2292PKTOPTIONS:
1679 #ifdef IPV6_PKTOPTIONS
1680 case IPV6_PKTOPTIONS:
1685 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1686 printf("ip6_ctloutput: mbuf limit hit\n");
1691 error = soopt_getm(sopt, &m); /* XXX */
1694 error = soopt_mcopyin(sopt, m); /* XXX */
1698 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1701 m_freem(m); /* XXX */
1706 * Use of some Hop-by-Hop options or some
1707 * Destination options, might require special
1708 * privilege. That is, normal applications
1709 * (without special privilege) might be forbidden
1710 * from setting certain options in outgoing packets,
1711 * and might never see certain options in received
1712 * packets. [RFC 2292 Section 6]
1713 * KAME specific note:
1714 * KAME prevents non-privileged users from sending or
1715 * receiving ANY hbh/dst options in order to avoid
1716 * overhead of parsing options in the kernel.
1718 case IPV6_RECVHOPOPTS:
1719 case IPV6_RECVDSTOPTS:
1720 case IPV6_RECVRTHDRDSTOPTS:
1722 error = priv_check(td,
1723 PRIV_NETINET_SETHDROPTS);
1728 case IPV6_UNICAST_HOPS:
1731 case IPV6_RECVPKTINFO:
1732 case IPV6_RECVHOPLIMIT:
1733 case IPV6_RECVRTHDR:
1734 case IPV6_RECVPATHMTU:
1735 case IPV6_RECVTCLASS:
1736 case IPV6_RECVFLOWID:
1738 case IPV6_RECVRSSBUCKETID:
1741 case IPV6_AUTOFLOWLABEL:
1742 case IPV6_ORIGDSTADDR:
1744 case IPV6_BINDMULTI:
1746 case IPV6_RSS_LISTEN_BUCKET:
1748 if (optname == IPV6_BINDANY && td != NULL) {
1749 error = priv_check(td,
1750 PRIV_NETINET_BINDANY);
1755 if (optlen != sizeof(int)) {
1759 error = sooptcopyin(sopt, &optval,
1760 sizeof optval, sizeof optval);
1765 case IPV6_UNICAST_HOPS:
1766 if (optval < -1 || optval >= 256)
1769 /* -1 = kernel default */
1770 inp->in6p_hops = optval;
1771 if ((inp->inp_vflag &
1773 inp->inp_ip_ttl = optval;
1776 #define OPTSET(bit) \
1780 inp->inp_flags |= (bit); \
1782 inp->inp_flags &= ~(bit); \
1784 } while (/*CONSTCOND*/ 0)
1785 #define OPTSET2292(bit) \
1788 inp->inp_flags |= IN6P_RFC2292; \
1790 inp->inp_flags |= (bit); \
1792 inp->inp_flags &= ~(bit); \
1794 } while (/*CONSTCOND*/ 0)
1795 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1797 #define OPTSET2_N(bit, val) do { \
1799 inp->inp_flags2 |= bit; \
1801 inp->inp_flags2 &= ~bit; \
1803 #define OPTSET2(bit, val) do { \
1805 OPTSET2_N(bit, val); \
1808 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1809 #define OPTSET2292_EXCLUSIVE(bit) \
1812 if (OPTBIT(IN6P_RFC2292)) { \
1816 inp->inp_flags |= (bit); \
1818 inp->inp_flags &= ~(bit); \
1821 } while (/*CONSTCOND*/ 0)
1823 case IPV6_RECVPKTINFO:
1824 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1829 struct ip6_pktopts **optp;
1831 /* cannot mix with RFC2292 */
1832 if (OPTBIT(IN6P_RFC2292)) {
1837 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1839 return (ECONNRESET);
1841 optp = &inp->in6p_outputopts;
1842 error = ip6_pcbopt(IPV6_HOPLIMIT,
1843 (u_char *)&optval, sizeof(optval),
1844 optp, (td != NULL) ? td->td_ucred :
1850 case IPV6_RECVHOPLIMIT:
1851 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1854 case IPV6_RECVHOPOPTS:
1855 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1858 case IPV6_RECVDSTOPTS:
1859 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1862 case IPV6_RECVRTHDRDSTOPTS:
1863 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1866 case IPV6_RECVRTHDR:
1867 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1870 case IPV6_RECVPATHMTU:
1872 * We ignore this option for TCP
1874 * (RFC3542 leaves this case
1877 if (uproto != IPPROTO_TCP)
1881 case IPV6_RECVFLOWID:
1882 OPTSET2(INP_RECVFLOWID, optval);
1886 case IPV6_RECVRSSBUCKETID:
1887 OPTSET2(INP_RECVRSSBUCKETID, optval);
1893 if (inp->inp_lport ||
1894 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1896 * The socket is already bound.
1903 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1904 inp->inp_vflag &= ~INP_IPV4;
1906 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1907 inp->inp_vflag |= INP_IPV4;
1911 case IPV6_RECVTCLASS:
1912 /* cannot mix with RFC2292 XXX */
1913 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1915 case IPV6_AUTOFLOWLABEL:
1916 OPTSET(IN6P_AUTOFLOWLABEL);
1919 case IPV6_ORIGDSTADDR:
1920 OPTSET2(INP_ORIGDSTADDR, optval);
1923 OPTSET(INP_BINDANY);
1926 case IPV6_BINDMULTI:
1927 OPTSET2(INP_BINDMULTI, optval);
1930 case IPV6_RSS_LISTEN_BUCKET:
1931 if ((optval >= 0) &&
1932 (optval < rss_getnumbuckets())) {
1934 inp->inp_rss_listen_bucket = optval;
1935 OPTSET2_N(INP_RSS_BUCKET_SET, 1);
1947 case IPV6_USE_MIN_MTU:
1948 case IPV6_PREFER_TEMPADDR:
1949 if (optlen != sizeof(optval)) {
1953 error = sooptcopyin(sopt, &optval,
1954 sizeof optval, sizeof optval);
1958 struct ip6_pktopts **optp;
1960 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1962 return (ECONNRESET);
1964 optp = &inp->in6p_outputopts;
1965 error = ip6_pcbopt(optname,
1966 (u_char *)&optval, sizeof(optval),
1967 optp, (td != NULL) ? td->td_ucred :
1973 case IPV6_2292PKTINFO:
1974 case IPV6_2292HOPLIMIT:
1975 case IPV6_2292HOPOPTS:
1976 case IPV6_2292DSTOPTS:
1977 case IPV6_2292RTHDR:
1979 if (optlen != sizeof(int)) {
1983 error = sooptcopyin(sopt, &optval,
1984 sizeof optval, sizeof optval);
1988 case IPV6_2292PKTINFO:
1989 OPTSET2292(IN6P_PKTINFO);
1991 case IPV6_2292HOPLIMIT:
1992 OPTSET2292(IN6P_HOPLIMIT);
1994 case IPV6_2292HOPOPTS:
1996 * Check super-user privilege.
1997 * See comments for IPV6_RECVHOPOPTS.
2000 error = priv_check(td,
2001 PRIV_NETINET_SETHDROPTS);
2005 OPTSET2292(IN6P_HOPOPTS);
2007 case IPV6_2292DSTOPTS:
2009 error = priv_check(td,
2010 PRIV_NETINET_SETHDROPTS);
2014 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2016 case IPV6_2292RTHDR:
2017 OPTSET2292(IN6P_RTHDR);
2025 case IPV6_RTHDRDSTOPTS:
2028 /* new advanced API (RFC3542) */
2030 u_char optbuf_storage[MCLBYTES];
2032 struct ip6_pktopts **optp;
2034 /* cannot mix with RFC2292 */
2035 if (OPTBIT(IN6P_RFC2292)) {
2041 * We only ensure valsize is not too large
2042 * here. Further validation will be done
2045 error = sooptcopyin(sopt, optbuf_storage,
2046 sizeof(optbuf_storage), 0);
2049 optlen = sopt->sopt_valsize;
2050 optbuf = optbuf_storage;
2052 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
2054 return (ECONNRESET);
2056 optp = &inp->in6p_outputopts;
2057 error = ip6_pcbopt(optname, optbuf, optlen,
2058 optp, (td != NULL) ? td->td_ucred : NULL,
2065 case IPV6_MULTICAST_IF:
2066 case IPV6_MULTICAST_HOPS:
2067 case IPV6_MULTICAST_LOOP:
2068 case IPV6_JOIN_GROUP:
2069 case IPV6_LEAVE_GROUP:
2071 case MCAST_BLOCK_SOURCE:
2072 case MCAST_UNBLOCK_SOURCE:
2073 case MCAST_JOIN_GROUP:
2074 case MCAST_LEAVE_GROUP:
2075 case MCAST_JOIN_SOURCE_GROUP:
2076 case MCAST_LEAVE_SOURCE_GROUP:
2077 error = ip6_setmoptions(inp, sopt);
2080 case IPV6_PORTRANGE:
2081 error = sooptcopyin(sopt, &optval,
2082 sizeof optval, sizeof optval);
2088 case IPV6_PORTRANGE_DEFAULT:
2089 inp->inp_flags &= ~(INP_LOWPORT);
2090 inp->inp_flags &= ~(INP_HIGHPORT);
2093 case IPV6_PORTRANGE_HIGH:
2094 inp->inp_flags &= ~(INP_LOWPORT);
2095 inp->inp_flags |= INP_HIGHPORT;
2098 case IPV6_PORTRANGE_LOW:
2099 inp->inp_flags &= ~(INP_HIGHPORT);
2100 inp->inp_flags |= INP_LOWPORT;
2110 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2111 case IPV6_IPSEC_POLICY:
2112 if (IPSEC_ENABLED(ipv6)) {
2113 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2120 error = ENOPROTOOPT;
2128 case IPV6_2292PKTOPTIONS:
2129 #ifdef IPV6_PKTOPTIONS
2130 case IPV6_PKTOPTIONS:
2133 * RFC3542 (effectively) deprecated the
2134 * semantics of the 2292-style pktoptions.
2135 * Since it was not reliable in nature (i.e.,
2136 * applications had to expect the lack of some
2137 * information after all), it would make sense
2138 * to simplify this part by always returning
2141 sopt->sopt_valsize = 0;
2144 case IPV6_RECVHOPOPTS:
2145 case IPV6_RECVDSTOPTS:
2146 case IPV6_RECVRTHDRDSTOPTS:
2147 case IPV6_UNICAST_HOPS:
2148 case IPV6_RECVPKTINFO:
2149 case IPV6_RECVHOPLIMIT:
2150 case IPV6_RECVRTHDR:
2151 case IPV6_RECVPATHMTU:
2154 case IPV6_PORTRANGE:
2155 case IPV6_RECVTCLASS:
2156 case IPV6_AUTOFLOWLABEL:
2160 case IPV6_RECVFLOWID:
2162 case IPV6_RSSBUCKETID:
2163 case IPV6_RECVRSSBUCKETID:
2165 case IPV6_BINDMULTI:
2168 case IPV6_RECVHOPOPTS:
2169 optval = OPTBIT(IN6P_HOPOPTS);
2172 case IPV6_RECVDSTOPTS:
2173 optval = OPTBIT(IN6P_DSTOPTS);
2176 case IPV6_RECVRTHDRDSTOPTS:
2177 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2180 case IPV6_UNICAST_HOPS:
2181 optval = inp->in6p_hops;
2184 case IPV6_RECVPKTINFO:
2185 optval = OPTBIT(IN6P_PKTINFO);
2188 case IPV6_RECVHOPLIMIT:
2189 optval = OPTBIT(IN6P_HOPLIMIT);
2192 case IPV6_RECVRTHDR:
2193 optval = OPTBIT(IN6P_RTHDR);
2196 case IPV6_RECVPATHMTU:
2197 optval = OPTBIT(IN6P_MTU);
2201 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2204 case IPV6_PORTRANGE:
2207 flags = inp->inp_flags;
2208 if (flags & INP_HIGHPORT)
2209 optval = IPV6_PORTRANGE_HIGH;
2210 else if (flags & INP_LOWPORT)
2211 optval = IPV6_PORTRANGE_LOW;
2216 case IPV6_RECVTCLASS:
2217 optval = OPTBIT(IN6P_TCLASS);
2220 case IPV6_AUTOFLOWLABEL:
2221 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2224 case IPV6_ORIGDSTADDR:
2225 optval = OPTBIT2(INP_ORIGDSTADDR);
2229 optval = OPTBIT(INP_BINDANY);
2233 optval = inp->inp_flowid;
2237 optval = inp->inp_flowtype;
2240 case IPV6_RECVFLOWID:
2241 optval = OPTBIT2(INP_RECVFLOWID);
2244 case IPV6_RSSBUCKETID:
2246 rss_hash2bucket(inp->inp_flowid,
2250 optval = rss_bucket;
2255 case IPV6_RECVRSSBUCKETID:
2256 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2260 case IPV6_BINDMULTI:
2261 optval = OPTBIT2(INP_BINDMULTI);
2267 error = sooptcopyout(sopt, &optval,
2274 struct ip6_mtuinfo mtuinfo;
2275 struct in6_addr addr;
2277 if (!(so->so_state & SS_ISCONNECTED))
2280 * XXX: we dot not consider the case of source
2281 * routing, or optional information to specify
2282 * the outgoing interface.
2283 * Copy faddr out of inp to avoid holding lock
2284 * on inp during route lookup.
2287 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2289 error = ip6_getpmtu_ctl(so->so_fibnum,
2293 if (pmtu > IPV6_MAXPACKET)
2294 pmtu = IPV6_MAXPACKET;
2296 bzero(&mtuinfo, sizeof(mtuinfo));
2297 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2298 optdata = (void *)&mtuinfo;
2299 optdatalen = sizeof(mtuinfo);
2300 error = sooptcopyout(sopt, optdata,
2305 case IPV6_2292PKTINFO:
2306 case IPV6_2292HOPLIMIT:
2307 case IPV6_2292HOPOPTS:
2308 case IPV6_2292RTHDR:
2309 case IPV6_2292DSTOPTS:
2311 case IPV6_2292PKTINFO:
2312 optval = OPTBIT(IN6P_PKTINFO);
2314 case IPV6_2292HOPLIMIT:
2315 optval = OPTBIT(IN6P_HOPLIMIT);
2317 case IPV6_2292HOPOPTS:
2318 optval = OPTBIT(IN6P_HOPOPTS);
2320 case IPV6_2292RTHDR:
2321 optval = OPTBIT(IN6P_RTHDR);
2323 case IPV6_2292DSTOPTS:
2324 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2327 error = sooptcopyout(sopt, &optval,
2334 case IPV6_RTHDRDSTOPTS:
2338 case IPV6_USE_MIN_MTU:
2339 case IPV6_PREFER_TEMPADDR:
2340 error = ip6_getpcbopt(inp, optname, sopt);
2343 case IPV6_MULTICAST_IF:
2344 case IPV6_MULTICAST_HOPS:
2345 case IPV6_MULTICAST_LOOP:
2347 error = ip6_getmoptions(inp, sopt);
2350 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2351 case IPV6_IPSEC_POLICY:
2352 if (IPSEC_ENABLED(ipv6)) {
2353 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2359 error = ENOPROTOOPT;
2369 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2371 int error = 0, optval, optlen;
2372 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2373 struct inpcb *inp = sotoinpcb(so);
2374 int level, op, optname;
2376 level = sopt->sopt_level;
2377 op = sopt->sopt_dir;
2378 optname = sopt->sopt_name;
2379 optlen = sopt->sopt_valsize;
2381 if (level != IPPROTO_IPV6) {
2388 * For ICMPv6 sockets, no modification allowed for checksum
2389 * offset, permit "no change" values to help existing apps.
2391 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2392 * for an ICMPv6 socket will fail."
2393 * The current behavior does not meet RFC3542.
2397 if (optlen != sizeof(int)) {
2401 error = sooptcopyin(sopt, &optval, sizeof(optval),
2405 if (optval < -1 || (optval % 2) != 0) {
2407 * The API assumes non-negative even offset
2408 * values or -1 as a special value.
2411 } else if (so->so_proto->pr_protocol ==
2413 if (optval != icmp6off)
2416 inp->in6p_cksum = optval;
2420 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2423 optval = inp->in6p_cksum;
2425 error = sooptcopyout(sopt, &optval, sizeof(optval));
2435 error = ENOPROTOOPT;
2443 * Set up IP6 options in pcb for insertion in output packets or
2444 * specifying behavior of outgoing packets.
2447 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2448 struct socket *so, struct sockopt *sopt)
2450 struct ip6_pktopts *opt = *pktopt;
2452 struct thread *td = sopt->sopt_td;
2454 /* turn off any old options. */
2457 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2458 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2459 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2460 printf("ip6_pcbopts: all specified options are cleared.\n");
2462 ip6_clearpktopts(opt, -1);
2464 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2470 if (!m || m->m_len == 0) {
2472 * Only turning off any previous options, regardless of
2473 * whether the opt is just created or given.
2475 free(opt, M_IP6OPT);
2479 /* set options specified by user. */
2480 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2481 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2482 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2483 free(opt, M_IP6OPT);
2491 * initialize ip6_pktopts. beware that there are non-zero default values in
2495 ip6_initpktopts(struct ip6_pktopts *opt)
2498 bzero(opt, sizeof(*opt));
2499 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2500 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2501 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2502 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2506 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2507 struct ucred *cred, int uproto)
2509 struct ip6_pktopts *opt;
2511 if (*pktopt == NULL) {
2512 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2514 if (*pktopt == NULL)
2516 ip6_initpktopts(*pktopt);
2520 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2523 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2524 if (pktopt && pktopt->field) { \
2526 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2527 malloc_optdata = true; \
2529 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
2531 free(optdata, M_TEMP); \
2532 return (ECONNRESET); \
2534 pktopt = inp->in6p_outputopts; \
2535 if (pktopt && pktopt->field) { \
2536 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2537 bcopy(&pktopt->field, optdata, optdatalen); \
2539 free(optdata, M_TEMP); \
2541 malloc_optdata = false; \
2546 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2547 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2549 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2550 pktopt->field->sa_len)
2553 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2555 void *optdata = NULL;
2556 bool malloc_optdata = false;
2559 struct in6_pktinfo null_pktinfo;
2560 int deftclass = 0, on;
2561 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2562 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2563 struct ip6_pktopts *pktopt;
2566 pktopt = inp->in6p_outputopts;
2570 optdata = (void *)&null_pktinfo;
2571 if (pktopt && pktopt->ip6po_pktinfo) {
2572 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2573 sizeof(null_pktinfo));
2574 in6_clearscope(&null_pktinfo.ipi6_addr);
2576 /* XXX: we don't have to do this every time... */
2577 bzero(&null_pktinfo, sizeof(null_pktinfo));
2579 optdatalen = sizeof(struct in6_pktinfo);
2582 if (pktopt && pktopt->ip6po_tclass >= 0)
2583 deftclass = pktopt->ip6po_tclass;
2584 optdata = (void *)&deftclass;
2585 optdatalen = sizeof(int);
2588 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2591 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2593 case IPV6_RTHDRDSTOPTS:
2594 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2597 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2600 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2602 case IPV6_USE_MIN_MTU:
2604 defminmtu = pktopt->ip6po_minmtu;
2605 optdata = (void *)&defminmtu;
2606 optdatalen = sizeof(int);
2609 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2613 optdata = (void *)&on;
2614 optdatalen = sizeof(on);
2616 case IPV6_PREFER_TEMPADDR:
2618 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2619 optdata = (void *)&defpreftemp;
2620 optdatalen = sizeof(int);
2622 default: /* should not happen */
2624 panic("ip6_getpcbopt: unexpected option\n");
2627 return (ENOPROTOOPT);
2631 error = sooptcopyout(sopt, optdata, optdatalen);
2633 free(optdata, M_TEMP);
2639 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2644 if (optname == -1 || optname == IPV6_PKTINFO) {
2645 if (pktopt->ip6po_pktinfo)
2646 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2647 pktopt->ip6po_pktinfo = NULL;
2649 if (optname == -1 || optname == IPV6_HOPLIMIT)
2650 pktopt->ip6po_hlim = -1;
2651 if (optname == -1 || optname == IPV6_TCLASS)
2652 pktopt->ip6po_tclass = -1;
2653 if (optname == -1 || optname == IPV6_NEXTHOP) {
2654 if (pktopt->ip6po_nextroute.ro_rt) {
2655 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2656 pktopt->ip6po_nextroute.ro_rt = NULL;
2658 if (pktopt->ip6po_nexthop)
2659 free(pktopt->ip6po_nexthop, M_IP6OPT);
2660 pktopt->ip6po_nexthop = NULL;
2662 if (optname == -1 || optname == IPV6_HOPOPTS) {
2663 if (pktopt->ip6po_hbh)
2664 free(pktopt->ip6po_hbh, M_IP6OPT);
2665 pktopt->ip6po_hbh = NULL;
2667 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2668 if (pktopt->ip6po_dest1)
2669 free(pktopt->ip6po_dest1, M_IP6OPT);
2670 pktopt->ip6po_dest1 = NULL;
2672 if (optname == -1 || optname == IPV6_RTHDR) {
2673 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2674 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2675 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2676 if (pktopt->ip6po_route.ro_rt) {
2677 RTFREE(pktopt->ip6po_route.ro_rt);
2678 pktopt->ip6po_route.ro_rt = NULL;
2681 if (optname == -1 || optname == IPV6_DSTOPTS) {
2682 if (pktopt->ip6po_dest2)
2683 free(pktopt->ip6po_dest2, M_IP6OPT);
2684 pktopt->ip6po_dest2 = NULL;
2688 #define PKTOPT_EXTHDRCPY(type) \
2691 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2692 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2693 if (dst->type == NULL)\
2695 bcopy(src->type, dst->type, hlen);\
2697 } while (/*CONSTCOND*/ 0)
2700 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2702 if (dst == NULL || src == NULL) {
2703 printf("ip6_clearpktopts: invalid argument\n");
2707 dst->ip6po_hlim = src->ip6po_hlim;
2708 dst->ip6po_tclass = src->ip6po_tclass;
2709 dst->ip6po_flags = src->ip6po_flags;
2710 dst->ip6po_minmtu = src->ip6po_minmtu;
2711 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2712 if (src->ip6po_pktinfo) {
2713 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2715 if (dst->ip6po_pktinfo == NULL)
2717 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2719 if (src->ip6po_nexthop) {
2720 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2722 if (dst->ip6po_nexthop == NULL)
2724 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2725 src->ip6po_nexthop->sa_len);
2727 PKTOPT_EXTHDRCPY(ip6po_hbh);
2728 PKTOPT_EXTHDRCPY(ip6po_dest1);
2729 PKTOPT_EXTHDRCPY(ip6po_dest2);
2730 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2734 ip6_clearpktopts(dst, -1);
2737 #undef PKTOPT_EXTHDRCPY
2739 struct ip6_pktopts *
2740 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2743 struct ip6_pktopts *dst;
2745 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2748 ip6_initpktopts(dst);
2750 if ((error = copypktopts(dst, src, canwait)) != 0) {
2751 free(dst, M_IP6OPT);
2759 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2764 ip6_clearpktopts(pktopt, -1);
2766 free(pktopt, M_IP6OPT);
2770 * Set IPv6 outgoing packet options based on advanced API.
2773 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2774 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2776 struct cmsghdr *cm = NULL;
2778 if (control == NULL || opt == NULL)
2781 ip6_initpktopts(opt);
2786 * If stickyopt is provided, make a local copy of the options
2787 * for this particular packet, then override them by ancillary
2789 * XXX: copypktopts() does not copy the cached route to a next
2790 * hop (if any). This is not very good in terms of efficiency,
2791 * but we can allow this since this option should be rarely
2794 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2799 * XXX: Currently, we assume all the optional information is stored
2802 if (control->m_next)
2805 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2806 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2809 if (control->m_len < CMSG_LEN(0))
2812 cm = mtod(control, struct cmsghdr *);
2813 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2815 if (cm->cmsg_level != IPPROTO_IPV6)
2818 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2819 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2828 * Set a particular packet option, as a sticky option or an ancillary data
2829 * item. "len" can be 0 only when it's a sticky option.
2830 * We have 4 cases of combination of "sticky" and "cmsg":
2831 * "sticky=0, cmsg=0": impossible
2832 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2833 * "sticky=1, cmsg=0": RFC3542 socket option
2834 * "sticky=1, cmsg=1": RFC2292 socket option
2837 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2838 struct ucred *cred, int sticky, int cmsg, int uproto)
2840 int minmtupolicy, preftemp;
2843 if (!sticky && !cmsg) {
2845 printf("ip6_setpktopt: impossible case\n");
2851 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2852 * not be specified in the context of RFC3542. Conversely,
2853 * RFC3542 types should not be specified in the context of RFC2292.
2857 case IPV6_2292PKTINFO:
2858 case IPV6_2292HOPLIMIT:
2859 case IPV6_2292NEXTHOP:
2860 case IPV6_2292HOPOPTS:
2861 case IPV6_2292DSTOPTS:
2862 case IPV6_2292RTHDR:
2863 case IPV6_2292PKTOPTIONS:
2864 return (ENOPROTOOPT);
2867 if (sticky && cmsg) {
2874 case IPV6_RTHDRDSTOPTS:
2876 case IPV6_USE_MIN_MTU:
2879 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2880 return (ENOPROTOOPT);
2885 case IPV6_2292PKTINFO:
2888 struct ifnet *ifp = NULL;
2889 struct in6_pktinfo *pktinfo;
2891 if (len != sizeof(struct in6_pktinfo))
2894 pktinfo = (struct in6_pktinfo *)buf;
2897 * An application can clear any sticky IPV6_PKTINFO option by
2898 * doing a "regular" setsockopt with ipi6_addr being
2899 * in6addr_any and ipi6_ifindex being zero.
2900 * [RFC 3542, Section 6]
2902 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2903 pktinfo->ipi6_ifindex == 0 &&
2904 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2905 ip6_clearpktopts(opt, optname);
2909 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2910 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2913 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2915 /* validate the interface index if specified. */
2916 if (pktinfo->ipi6_ifindex > V_if_index)
2918 if (pktinfo->ipi6_ifindex) {
2919 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2923 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2924 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2928 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2929 struct in6_ifaddr *ia;
2931 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2932 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2934 return (EADDRNOTAVAIL);
2935 ifa_free(&ia->ia_ifa);
2938 * We store the address anyway, and let in6_selectsrc()
2939 * validate the specified address. This is because ipi6_addr
2940 * may not have enough information about its scope zone, and
2941 * we may need additional information (such as outgoing
2942 * interface or the scope zone of a destination address) to
2943 * disambiguate the scope.
2944 * XXX: the delay of the validation may confuse the
2945 * application when it is used as a sticky option.
2947 if (opt->ip6po_pktinfo == NULL) {
2948 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2949 M_IP6OPT, M_NOWAIT);
2950 if (opt->ip6po_pktinfo == NULL)
2953 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2957 case IPV6_2292HOPLIMIT:
2963 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2964 * to simplify the ordering among hoplimit options.
2966 if (optname == IPV6_HOPLIMIT && sticky)
2967 return (ENOPROTOOPT);
2969 if (len != sizeof(int))
2972 if (*hlimp < -1 || *hlimp > 255)
2975 opt->ip6po_hlim = *hlimp;
2983 if (len != sizeof(int))
2985 tclass = *(int *)buf;
2986 if (tclass < -1 || tclass > 255)
2989 opt->ip6po_tclass = tclass;
2993 case IPV6_2292NEXTHOP:
2996 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3001 if (len == 0) { /* just remove the option */
3002 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3006 /* check if cmsg_len is large enough for sa_len */
3007 if (len < sizeof(struct sockaddr) || len < *buf)
3010 switch (((struct sockaddr *)buf)->sa_family) {
3013 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3016 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3019 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3020 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3023 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3029 case AF_LINK: /* should eventually be supported */
3031 return (EAFNOSUPPORT);
3034 /* turn off the previous option, then set the new option. */
3035 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3036 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3037 if (opt->ip6po_nexthop == NULL)
3039 bcopy(buf, opt->ip6po_nexthop, *buf);
3042 case IPV6_2292HOPOPTS:
3045 struct ip6_hbh *hbh;
3049 * XXX: We don't allow a non-privileged user to set ANY HbH
3050 * options, since per-option restriction has too much
3054 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3060 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3061 break; /* just remove the option */
3064 /* message length validation */
3065 if (len < sizeof(struct ip6_hbh))
3067 hbh = (struct ip6_hbh *)buf;
3068 hbhlen = (hbh->ip6h_len + 1) << 3;
3072 /* turn off the previous option, then set the new option. */
3073 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3074 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3075 if (opt->ip6po_hbh == NULL)
3077 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3082 case IPV6_2292DSTOPTS:
3084 case IPV6_RTHDRDSTOPTS:
3086 struct ip6_dest *dest, **newdest = NULL;
3089 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3090 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3096 ip6_clearpktopts(opt, optname);
3097 break; /* just remove the option */
3100 /* message length validation */
3101 if (len < sizeof(struct ip6_dest))
3103 dest = (struct ip6_dest *)buf;
3104 destlen = (dest->ip6d_len + 1) << 3;
3109 * Determine the position that the destination options header
3110 * should be inserted; before or after the routing header.
3113 case IPV6_2292DSTOPTS:
3115 * The old advacned API is ambiguous on this point.
3116 * Our approach is to determine the position based
3117 * according to the existence of a routing header.
3118 * Note, however, that this depends on the order of the
3119 * extension headers in the ancillary data; the 1st
3120 * part of the destination options header must appear
3121 * before the routing header in the ancillary data,
3123 * RFC3542 solved the ambiguity by introducing
3124 * separate ancillary data or option types.
3126 if (opt->ip6po_rthdr == NULL)
3127 newdest = &opt->ip6po_dest1;
3129 newdest = &opt->ip6po_dest2;
3131 case IPV6_RTHDRDSTOPTS:
3132 newdest = &opt->ip6po_dest1;
3135 newdest = &opt->ip6po_dest2;
3139 /* turn off the previous option, then set the new option. */
3140 ip6_clearpktopts(opt, optname);
3141 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3142 if (*newdest == NULL)
3144 bcopy(dest, *newdest, destlen);
3149 case IPV6_2292RTHDR:
3152 struct ip6_rthdr *rth;
3156 ip6_clearpktopts(opt, IPV6_RTHDR);
3157 break; /* just remove the option */
3160 /* message length validation */
3161 if (len < sizeof(struct ip6_rthdr))
3163 rth = (struct ip6_rthdr *)buf;
3164 rthlen = (rth->ip6r_len + 1) << 3;
3168 switch (rth->ip6r_type) {
3169 case IPV6_RTHDR_TYPE_0:
3170 if (rth->ip6r_len == 0) /* must contain one addr */
3172 if (rth->ip6r_len % 2) /* length must be even */
3174 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3178 return (EINVAL); /* not supported */
3181 /* turn off the previous option */
3182 ip6_clearpktopts(opt, IPV6_RTHDR);
3183 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3184 if (opt->ip6po_rthdr == NULL)
3186 bcopy(rth, opt->ip6po_rthdr, rthlen);
3191 case IPV6_USE_MIN_MTU:
3192 if (len != sizeof(int))
3194 minmtupolicy = *(int *)buf;
3195 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3196 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3197 minmtupolicy != IP6PO_MINMTU_ALL) {
3200 opt->ip6po_minmtu = minmtupolicy;
3204 if (len != sizeof(int))
3207 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3209 * we ignore this option for TCP sockets.
3210 * (RFC3542 leaves this case unspecified.)
3212 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3214 opt->ip6po_flags |= IP6PO_DONTFRAG;
3217 case IPV6_PREFER_TEMPADDR:
3218 if (len != sizeof(int))
3220 preftemp = *(int *)buf;
3221 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3222 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3223 preftemp != IP6PO_TEMPADDR_PREFER) {
3226 opt->ip6po_prefer_tempaddr = preftemp;
3230 return (ENOPROTOOPT);
3231 } /* end of switch */
3237 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3238 * packet to the input queue of a specified interface. Note that this
3239 * calls the output routine of the loopback "driver", but with an interface
3240 * pointer that might NOT be &loif -- easier than replicating that code here.
3243 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3246 struct ip6_hdr *ip6;
3248 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3253 * Make sure to deep-copy IPv6 header portion in case the data
3254 * is in an mbuf cluster, so that we can safely override the IPv6
3255 * header portion later.
3257 if (!M_WRITABLE(copym) ||
3258 copym->m_len < sizeof(struct ip6_hdr)) {
3259 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3263 ip6 = mtod(copym, struct ip6_hdr *);
3265 * clear embedded scope identifiers if necessary.
3266 * in6_clearscope will touch the addresses only when necessary.
3268 in6_clearscope(&ip6->ip6_src);
3269 in6_clearscope(&ip6->ip6_dst);
3270 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3271 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3273 copym->m_pkthdr.csum_data = 0xffff;
3275 if_simloop(ifp, copym, AF_INET6, 0);
3279 * Chop IPv6 header off from the payload.
3282 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3285 struct ip6_hdr *ip6;
3287 ip6 = mtod(m, struct ip6_hdr *);
3288 if (m->m_len > sizeof(*ip6)) {
3289 mh = m_gethdr(M_NOWAIT, MT_DATA);
3294 m_move_pkthdr(mh, m);
3295 M_ALIGN(mh, sizeof(*ip6));
3296 m->m_len -= sizeof(*ip6);
3297 m->m_data += sizeof(*ip6);
3300 m->m_len = sizeof(*ip6);
3301 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3303 exthdrs->ip6e_ip6 = m;
3308 * Compute IPv6 extension header length.
3311 ip6_optlen(struct inpcb *inp)
3315 if (!inp->in6p_outputopts)
3320 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3322 len += elen(inp->in6p_outputopts->ip6po_hbh);
3323 if (inp->in6p_outputopts->ip6po_rthdr)
3324 /* dest1 is valid with rthdr only */
3325 len += elen(inp->in6p_outputopts->ip6po_dest1);
3326 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3327 len += elen(inp->in6p_outputopts->ip6po_dest2);